bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for the QCOW version 2 format |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 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 | */ |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Vladimir Sementsov-Ogievskiy | 2714f13 | 2018-06-20 17:48:36 +0300 | [diff] [blame] | 26 | |
Max Reitz | 609f45e | 2018-06-14 21:14:28 +0200 | [diff] [blame] | 27 | #include "block/qdict.h" |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 28 | #include "sysemu/block-backend.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 29 | #include "qemu/main-loop.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/module.h" |
Michael S. Tsirkin | 0d8c41d | 2018-05-03 22:50:20 +0300 | [diff] [blame] | 31 | #include "qcow2.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/error-report.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 33 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 34 | #include "qapi/qapi-events-block-core.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 35 | #include "qapi/qmp/qdict.h" |
| 36 | #include "qapi/qmp/qstring.h" |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 37 | #include "trace.h" |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 38 | #include "qemu/option_int.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 39 | #include "qemu/cutils.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 40 | #include "qemu/bswap.h" |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 41 | #include "qapi/qobject-input-visitor.h" |
| 42 | #include "qapi/qapi-visit-block-core.h" |
Michael S. Tsirkin | 0d8c41d | 2018-05-03 22:50:20 +0300 | [diff] [blame] | 43 | #include "crypto.h" |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 44 | #include "block/aio_task.h" |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | Differences with QCOW: |
| 48 | |
| 49 | - Support for multiple incremental snapshots. |
| 50 | - Memory management by reference counts. |
| 51 | - Clusters which have a reference count of one have the bit |
| 52 | QCOW_OFLAG_COPIED to optimize write performance. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 53 | - Size of compressed clusters is stored in sectors to reduce bit usage |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 54 | in the cluster offsets. |
| 55 | - Support for storing additional data (such as the VM state) in the |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 56 | snapshots. |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 57 | - If a backing store is used, the cluster size is not constrained |
| 58 | (could be backported to QCOW). |
| 59 | - L2 tables have always a size of one cluster. |
| 60 | */ |
| 61 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 62 | |
| 63 | typedef struct { |
| 64 | uint32_t magic; |
| 65 | uint32_t len; |
Jeff Cody | c4217f6 | 2013-09-25 12:08:50 -0400 | [diff] [blame] | 66 | } QEMU_PACKED QCowExtension; |
Jeff Cody | 21d82ac | 2012-09-20 15:13:28 -0400 | [diff] [blame] | 67 | |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 68 | #define QCOW2_EXT_MAGIC_END 0 |
Andrey Shinkevich | 8098969 | 2020-07-17 11:14:49 +0300 | [diff] [blame] | 69 | #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xe2792aca |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 70 | #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857 |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 71 | #define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77 |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 72 | #define QCOW2_EXT_MAGIC_BITMAPS 0x23852875 |
Kevin Wolf | 93c2493 | 2019-01-14 16:48:25 +0100 | [diff] [blame] | 73 | #define QCOW2_EXT_MAGIC_DATA_FILE 0x44415441 |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 74 | |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 75 | static int coroutine_fn |
| 76 | qcow2_co_preadv_compressed(BlockDriverState *bs, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 77 | uint64_t cluster_descriptor, |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 78 | uint64_t offset, |
| 79 | uint64_t bytes, |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 80 | QEMUIOVector *qiov, |
| 81 | size_t qiov_offset); |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 82 | |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 83 | static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 84 | { |
| 85 | const QCowHeader *cow_header = (const void *)buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 86 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 87 | if (buf_size >= sizeof(QCowHeader) && |
| 88 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 89 | be32_to_cpu(cow_header->version) >= 2) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 90 | return 100; |
| 91 | else |
| 92 | return 0; |
| 93 | } |
| 94 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 95 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 96 | static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset, |
| 97 | uint8_t *buf, size_t buflen, |
| 98 | void *opaque, Error **errp) |
| 99 | { |
| 100 | BlockDriverState *bs = opaque; |
| 101 | BDRVQcow2State *s = bs->opaque; |
| 102 | ssize_t ret; |
| 103 | |
| 104 | if ((offset + buflen) > s->crypto_header.length) { |
| 105 | error_setg(errp, "Request for data outside of extension header"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | ret = bdrv_pread(bs->file, |
| 110 | s->crypto_header.offset + offset, buf, buflen); |
| 111 | if (ret < 0) { |
| 112 | error_setg_errno(errp, -ret, "Could not read encryption header"); |
| 113 | return -1; |
| 114 | } |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen, |
| 120 | void *opaque, Error **errp) |
| 121 | { |
| 122 | BlockDriverState *bs = opaque; |
| 123 | BDRVQcow2State *s = bs->opaque; |
| 124 | int64_t ret; |
| 125 | int64_t clusterlen; |
| 126 | |
| 127 | ret = qcow2_alloc_clusters(bs, headerlen); |
| 128 | if (ret < 0) { |
| 129 | error_setg_errno(errp, -ret, |
| 130 | "Cannot allocate cluster for LUKS header size %zu", |
| 131 | headerlen); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | s->crypto_header.length = headerlen; |
| 136 | s->crypto_header.offset = ret; |
| 137 | |
Daniel P. Berrangé | 087ab8e | 2020-02-07 13:55:20 +0000 | [diff] [blame] | 138 | /* |
| 139 | * Zero fill all space in cluster so it has predictable |
| 140 | * content, as we may not initialize some regions of the |
| 141 | * header (eg only 1 out of 8 key slots will be initialized) |
| 142 | */ |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 143 | clusterlen = size_to_clusters(s, headerlen) * s->cluster_size; |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 144 | assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen, false) == 0); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 145 | ret = bdrv_pwrite_zeroes(bs->file, |
Daniel P. Berrangé | 087ab8e | 2020-02-07 13:55:20 +0000 | [diff] [blame] | 146 | ret, |
| 147 | clusterlen, 0); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 148 | if (ret < 0) { |
| 149 | error_setg_errno(errp, -ret, "Could not zero fill encryption header"); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset, |
| 158 | const uint8_t *buf, size_t buflen, |
| 159 | void *opaque, Error **errp) |
| 160 | { |
| 161 | BlockDriverState *bs = opaque; |
| 162 | BDRVQcow2State *s = bs->opaque; |
| 163 | ssize_t ret; |
| 164 | |
| 165 | if ((offset + buflen) > s->crypto_header.length) { |
| 166 | error_setg(errp, "Request for data outside of extension header"); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | ret = bdrv_pwrite(bs->file, |
| 171 | s->crypto_header.offset + offset, buf, buflen); |
| 172 | if (ret < 0) { |
| 173 | error_setg_errno(errp, -ret, "Could not read encryption header"); |
| 174 | return -1; |
| 175 | } |
| 176 | return ret; |
| 177 | } |
| 178 | |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 179 | static QDict* |
| 180 | qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) |
| 181 | { |
| 182 | QDict *cryptoopts_qdict; |
| 183 | QDict *opts_qdict; |
| 184 | |
| 185 | /* Extract "encrypt." options into a qdict */ |
| 186 | opts_qdict = qemu_opts_to_qdict(opts, NULL); |
| 187 | qdict_extract_subqdict(opts_qdict, &cryptoopts_qdict, "encrypt."); |
| 188 | qobject_unref(opts_qdict); |
| 189 | qdict_put_str(cryptoopts_qdict, "format", fmt); |
| 190 | return cryptoopts_qdict; |
| 191 | } |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 192 | |
Eric Blake | a951a63 | 2020-03-24 12:42:30 -0500 | [diff] [blame] | 193 | /* |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 194 | * read qcow2 extension and fill bs |
| 195 | * start reading from start_offset |
| 196 | * finish reading upon magic of value 0 or when end_offset reached |
| 197 | * unknown magic is skipped (future extension this version knows nothing about) |
| 198 | * return 0 upon success, non-0 otherwise |
| 199 | */ |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 200 | static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 201 | uint64_t end_offset, void **p_feature_table, |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 202 | int flags, bool *need_update_header, |
| 203 | Error **errp) |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 204 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 205 | BDRVQcow2State *s = bs->opaque; |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 206 | QCowExtension ext; |
| 207 | uint64_t offset; |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 208 | int ret; |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 209 | Qcow2BitmapHeaderExt bitmaps_ext; |
| 210 | |
| 211 | if (need_update_header != NULL) { |
| 212 | *need_update_header = false; |
| 213 | } |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 214 | |
| 215 | #ifdef DEBUG_EXT |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 216 | printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 217 | #endif |
| 218 | offset = start_offset; |
| 219 | while (offset < end_offset) { |
| 220 | |
| 221 | #ifdef DEBUG_EXT |
| 222 | /* Sanity check */ |
| 223 | if (offset > s->cluster_size) |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 224 | printf("qcow2_read_extension: suspicious offset %lu\n", offset); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 225 | |
Dong Xu Wang | 9b2260c | 2011-11-22 18:06:25 +0800 | [diff] [blame] | 226 | printf("attempting to read extended header in offset %lu\n", offset); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 227 | #endif |
| 228 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 229 | ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext)); |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 230 | if (ret < 0) { |
| 231 | error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: " |
| 232 | "pread fail from offset %" PRIu64, offset); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 233 | return 1; |
| 234 | } |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 235 | ext.magic = be32_to_cpu(ext.magic); |
| 236 | ext.len = be32_to_cpu(ext.len); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 237 | offset += sizeof(ext); |
| 238 | #ifdef DEBUG_EXT |
| 239 | printf("ext.magic = 0x%x\n", ext.magic); |
| 240 | #endif |
Kevin Wolf | 2ebafc8 | 2014-11-25 18:12:40 +0100 | [diff] [blame] | 241 | if (offset > end_offset || ext.len > end_offset - offset) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 242 | error_setg(errp, "Header extension too large"); |
Kevin Wolf | 64ca6ae | 2012-02-22 12:37:13 +0100 | [diff] [blame] | 243 | return -EINVAL; |
| 244 | } |
| 245 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 246 | switch (ext.magic) { |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 247 | case QCOW2_EXT_MAGIC_END: |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 248 | return 0; |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 249 | |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 250 | case QCOW2_EXT_MAGIC_BACKING_FORMAT: |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 251 | if (ext.len >= sizeof(bs->backing_format)) { |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 252 | error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32 |
| 253 | " too large (>=%zu)", ext.len, |
| 254 | sizeof(bs->backing_format)); |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 255 | return 2; |
| 256 | } |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 257 | ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len); |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 258 | if (ret < 0) { |
| 259 | error_setg_errno(errp, -ret, "ERROR: ext_backing_format: " |
| 260 | "Could not read format name"); |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 261 | return 3; |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 262 | } |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 263 | bs->backing_format[ext.len] = '\0'; |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 264 | s->image_backing_format = g_strdup(bs->backing_format); |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 265 | #ifdef DEBUG_EXT |
| 266 | printf("Qcow2: Got format extension %s\n", bs->backing_format); |
| 267 | #endif |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 268 | break; |
| 269 | |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 270 | case QCOW2_EXT_MAGIC_FEATURE_TABLE: |
| 271 | if (p_feature_table != NULL) { |
| 272 | void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 273 | ret = bdrv_pread(bs->file, offset , feature_table, ext.len); |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 274 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 275 | error_setg_errno(errp, -ret, "ERROR: ext_feature_table: " |
| 276 | "Could not read table"); |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | *p_feature_table = feature_table; |
| 281 | } |
| 282 | break; |
| 283 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 284 | case QCOW2_EXT_MAGIC_CRYPTO_HEADER: { |
| 285 | unsigned int cflags = 0; |
| 286 | if (s->crypt_method_header != QCOW_CRYPT_LUKS) { |
| 287 | error_setg(errp, "CRYPTO header extension only " |
| 288 | "expected with LUKS encryption method"); |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) { |
| 292 | error_setg(errp, "CRYPTO header extension size %u, " |
| 293 | "but expected size %zu", ext.len, |
| 294 | sizeof(Qcow2CryptoHeaderExtension)); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len); |
| 299 | if (ret < 0) { |
| 300 | error_setg_errno(errp, -ret, |
| 301 | "Unable to read CRYPTO header extension"); |
| 302 | return ret; |
| 303 | } |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 304 | s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset); |
| 305 | s->crypto_header.length = be64_to_cpu(s->crypto_header.length); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 306 | |
| 307 | if ((s->crypto_header.offset % s->cluster_size) != 0) { |
| 308 | error_setg(errp, "Encryption header offset '%" PRIu64 "' is " |
| 309 | "not a multiple of cluster size '%u'", |
| 310 | s->crypto_header.offset, s->cluster_size); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
| 314 | if (flags & BDRV_O_NO_IO) { |
| 315 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; |
| 316 | } |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 317 | s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.", |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 318 | qcow2_crypto_hdr_read_func, |
Vladimir Sementsov-Ogievskiy | 8ac0f15 | 2019-05-06 17:27:41 +0300 | [diff] [blame] | 319 | bs, cflags, QCOW2_MAX_THREADS, errp); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 320 | if (!s->crypto) { |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | } break; |
| 324 | |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 325 | case QCOW2_EXT_MAGIC_BITMAPS: |
| 326 | if (ext.len != sizeof(bitmaps_ext)) { |
| 327 | error_setg_errno(errp, -ret, "bitmaps_ext: " |
| 328 | "Invalid extension length"); |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | |
| 332 | if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) { |
Max Reitz | c9ceb3e | 2017-11-23 03:08:17 +0100 | [diff] [blame] | 333 | if (s->qcow_version < 3) { |
| 334 | /* Let's be a bit more specific */ |
| 335 | warn_report("This qcow2 v2 image contains bitmaps, but " |
| 336 | "they may have been modified by a program " |
| 337 | "without persistent bitmap support; so now " |
| 338 | "they must all be considered inconsistent"); |
| 339 | } else { |
| 340 | warn_report("a program lacking bitmap support " |
| 341 | "modified this file, so all bitmaps are now " |
| 342 | "considered inconsistent"); |
| 343 | } |
Alistair Francis | 55d527a | 2017-09-11 12:52:46 -0700 | [diff] [blame] | 344 | error_printf("Some clusters may be leaked, " |
| 345 | "run 'qemu-img check -r' on the image " |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 346 | "file to fix."); |
| 347 | if (need_update_header != NULL) { |
| 348 | /* Updating is needed to drop invalid bitmap extension. */ |
| 349 | *need_update_header = true; |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len); |
| 355 | if (ret < 0) { |
| 356 | error_setg_errno(errp, -ret, "bitmaps_ext: " |
| 357 | "Could not read ext header"); |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | if (bitmaps_ext.reserved32 != 0) { |
| 362 | error_setg_errno(errp, -ret, "bitmaps_ext: " |
| 363 | "Reserved field is not zero"); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 367 | bitmaps_ext.nb_bitmaps = be32_to_cpu(bitmaps_ext.nb_bitmaps); |
| 368 | bitmaps_ext.bitmap_directory_size = |
| 369 | be64_to_cpu(bitmaps_ext.bitmap_directory_size); |
| 370 | bitmaps_ext.bitmap_directory_offset = |
| 371 | be64_to_cpu(bitmaps_ext.bitmap_directory_offset); |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 372 | |
| 373 | if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) { |
| 374 | error_setg(errp, |
| 375 | "bitmaps_ext: Image has %" PRIu32 " bitmaps, " |
| 376 | "exceeding the QEMU supported maximum of %d", |
| 377 | bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS); |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | if (bitmaps_ext.nb_bitmaps == 0) { |
| 382 | error_setg(errp, "found bitmaps extension with zero bitmaps"); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
Alberto Garcia | 74e60fb | 2019-12-12 12:01:21 +0200 | [diff] [blame] | 386 | if (offset_into_cluster(s, bitmaps_ext.bitmap_directory_offset)) { |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 387 | error_setg(errp, "bitmaps_ext: " |
| 388 | "invalid bitmap directory offset"); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | |
| 392 | if (bitmaps_ext.bitmap_directory_size > |
| 393 | QCOW2_MAX_BITMAP_DIRECTORY_SIZE) { |
| 394 | error_setg(errp, "bitmaps_ext: " |
| 395 | "bitmap directory size (%" PRIu64 ") exceeds " |
| 396 | "the maximum supported size (%d)", |
| 397 | bitmaps_ext.bitmap_directory_size, |
| 398 | QCOW2_MAX_BITMAP_DIRECTORY_SIZE); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | s->nb_bitmaps = bitmaps_ext.nb_bitmaps; |
| 403 | s->bitmap_directory_offset = |
| 404 | bitmaps_ext.bitmap_directory_offset; |
| 405 | s->bitmap_directory_size = |
| 406 | bitmaps_ext.bitmap_directory_size; |
| 407 | |
| 408 | #ifdef DEBUG_EXT |
| 409 | printf("Qcow2: Got bitmaps extension: " |
| 410 | "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n", |
| 411 | s->bitmap_directory_offset, s->nb_bitmaps); |
| 412 | #endif |
| 413 | break; |
| 414 | |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 415 | case QCOW2_EXT_MAGIC_DATA_FILE: |
| 416 | { |
| 417 | s->image_data_file = g_malloc0(ext.len + 1); |
| 418 | ret = bdrv_pread(bs->file, offset, s->image_data_file, ext.len); |
| 419 | if (ret < 0) { |
| 420 | error_setg_errno(errp, -ret, |
| 421 | "ERROR: Could not read data file name"); |
| 422 | return ret; |
| 423 | } |
| 424 | #ifdef DEBUG_EXT |
| 425 | printf("Qcow2: Got external data file %s\n", s->image_data_file); |
| 426 | #endif |
| 427 | break; |
| 428 | } |
| 429 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 430 | default: |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 431 | /* unknown magic - save it in case we need to rewrite the header */ |
Eric Blake | 4096974 | 2017-11-17 10:47:47 -0600 | [diff] [blame] | 432 | /* If you add a new feature, make sure to also update the fast |
| 433 | * path of qcow2_make_empty() to deal with it. */ |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 434 | { |
| 435 | Qcow2UnknownHeaderExtension *uext; |
| 436 | |
| 437 | uext = g_malloc0(sizeof(*uext) + ext.len); |
| 438 | uext->magic = ext.magic; |
| 439 | uext->len = ext.len; |
| 440 | QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); |
| 441 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 442 | ret = bdrv_pread(bs->file, offset , uext->data, uext->len); |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 443 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 444 | error_setg_errno(errp, -ret, "ERROR: unknown extension: " |
| 445 | "Could not read data"); |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 446 | return ret; |
| 447 | } |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 448 | } |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
Kevin Wolf | fd29b4b | 2012-02-22 12:31:47 +0100 | [diff] [blame] | 451 | |
| 452 | offset += ((ext.len + 7) & ~7); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 458 | static void cleanup_unknown_header_ext(BlockDriverState *bs) |
| 459 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 460 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 461 | Qcow2UnknownHeaderExtension *uext, *next; |
| 462 | |
| 463 | QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { |
| 464 | QLIST_REMOVE(uext, next); |
| 465 | g_free(uext); |
| 466 | } |
| 467 | } |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 468 | |
Max Reitz | a55448b | 2016-03-16 19:54:33 +0100 | [diff] [blame] | 469 | static void report_unsupported_feature(Error **errp, Qcow2Feature *table, |
| 470 | uint64_t mask) |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 471 | { |
Alberto Garcia | 7cdca2e | 2020-01-15 14:56:26 +0100 | [diff] [blame] | 472 | g_autoptr(GString) features = g_string_sized_new(60); |
Kevin Wolf | 12ac6d3 | 2014-07-17 11:41:53 +0200 | [diff] [blame] | 473 | |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 474 | while (table && table->name[0] != '\0') { |
| 475 | if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { |
Kevin Wolf | 12ac6d3 | 2014-07-17 11:41:53 +0200 | [diff] [blame] | 476 | if (mask & (1ULL << table->bit)) { |
Alberto Garcia | 7cdca2e | 2020-01-15 14:56:26 +0100 | [diff] [blame] | 477 | if (features->len > 0) { |
| 478 | g_string_append(features, ", "); |
| 479 | } |
| 480 | g_string_append_printf(features, "%.46s", table->name); |
Kevin Wolf | 12ac6d3 | 2014-07-17 11:41:53 +0200 | [diff] [blame] | 481 | mask &= ~(1ULL << table->bit); |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | table++; |
| 485 | } |
| 486 | |
| 487 | if (mask) { |
Alberto Garcia | 7cdca2e | 2020-01-15 14:56:26 +0100 | [diff] [blame] | 488 | if (features->len > 0) { |
| 489 | g_string_append(features, ", "); |
| 490 | } |
| 491 | g_string_append_printf(features, |
| 492 | "Unknown incompatible feature: %" PRIx64, mask); |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 493 | } |
Kevin Wolf | 12ac6d3 | 2014-07-17 11:41:53 +0200 | [diff] [blame] | 494 | |
Alberto Garcia | 7cdca2e | 2020-01-15 14:56:26 +0100 | [diff] [blame] | 495 | error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str); |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 498 | /* |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 499 | * Sets the dirty bit and flushes afterwards if necessary. |
| 500 | * |
| 501 | * The incompatible_features bit is only set if the image file header was |
| 502 | * updated successfully. Therefore it is not required to check the return |
| 503 | * value of this function. |
| 504 | */ |
Kevin Wolf | 280d373 | 2012-12-07 18:08:47 +0100 | [diff] [blame] | 505 | int qcow2_mark_dirty(BlockDriverState *bs) |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 506 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 507 | BDRVQcow2State *s = bs->opaque; |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 508 | uint64_t val; |
| 509 | int ret; |
| 510 | |
| 511 | assert(s->qcow_version >= 3); |
| 512 | |
| 513 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { |
| 514 | return 0; /* already dirty */ |
| 515 | } |
| 516 | |
| 517 | val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 518 | ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features), |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 519 | &val, sizeof(val)); |
| 520 | if (ret < 0) { |
| 521 | return ret; |
| 522 | } |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 523 | ret = bdrv_flush(bs->file->bs); |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 524 | if (ret < 0) { |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | /* Only treat image as dirty if the header was updated successfully */ |
| 529 | s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 534 | * Clears the dirty bit and flushes before if necessary. Only call this |
| 535 | * function when there are no pending requests, it does not guard against |
| 536 | * concurrent requests dirtying the image. |
| 537 | */ |
| 538 | static int qcow2_mark_clean(BlockDriverState *bs) |
| 539 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 540 | BDRVQcow2State *s = bs->opaque; |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 541 | |
| 542 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { |
Kevin Wolf | 4c2e5f8 | 2014-04-03 13:47:50 +0200 | [diff] [blame] | 543 | int ret; |
| 544 | |
| 545 | s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; |
| 546 | |
Paolo Bonzini | 8b220eb | 2018-03-01 17:36:14 +0100 | [diff] [blame] | 547 | ret = qcow2_flush_caches(bs); |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 548 | if (ret < 0) { |
| 549 | return ret; |
| 550 | } |
| 551 | |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 552 | return qcow2_update_header(bs); |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
Max Reitz | 69c9872 | 2013-08-30 14:34:24 +0200 | [diff] [blame] | 557 | /* |
| 558 | * Marks the image as corrupt. |
| 559 | */ |
| 560 | int qcow2_mark_corrupt(BlockDriverState *bs) |
| 561 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 562 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 69c9872 | 2013-08-30 14:34:24 +0200 | [diff] [blame] | 563 | |
| 564 | s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT; |
| 565 | return qcow2_update_header(bs); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes |
| 570 | * before if necessary. |
| 571 | */ |
| 572 | int qcow2_mark_consistent(BlockDriverState *bs) |
| 573 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 574 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 69c9872 | 2013-08-30 14:34:24 +0200 | [diff] [blame] | 575 | |
| 576 | if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { |
Paolo Bonzini | 8b220eb | 2018-03-01 17:36:14 +0100 | [diff] [blame] | 577 | int ret = qcow2_flush_caches(bs); |
Max Reitz | 69c9872 | 2013-08-30 14:34:24 +0200 | [diff] [blame] | 578 | if (ret < 0) { |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT; |
| 583 | return qcow2_update_header(bs); |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 588 | static void qcow2_add_check_result(BdrvCheckResult *out, |
| 589 | const BdrvCheckResult *src, |
| 590 | bool set_allocation_info) |
| 591 | { |
| 592 | out->corruptions += src->corruptions; |
| 593 | out->leaks += src->leaks; |
| 594 | out->check_errors += src->check_errors; |
| 595 | out->corruptions_fixed += src->corruptions_fixed; |
| 596 | out->leaks_fixed += src->leaks_fixed; |
| 597 | |
| 598 | if (set_allocation_info) { |
| 599 | out->image_end_offset = src->image_end_offset; |
| 600 | out->bfi = src->bfi; |
| 601 | } |
| 602 | } |
| 603 | |
Paolo Bonzini | 2fd6163 | 2018-03-01 17:36:19 +0100 | [diff] [blame] | 604 | static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs, |
| 605 | BdrvCheckResult *result, |
| 606 | BdrvCheckMode fix) |
Stefan Hajnoczi | acbe598 | 2012-08-09 13:05:55 +0100 | [diff] [blame] | 607 | { |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 608 | BdrvCheckResult snapshot_res = {}; |
| 609 | BdrvCheckResult refcount_res = {}; |
| 610 | int ret; |
| 611 | |
| 612 | memset(result, 0, sizeof(*result)); |
| 613 | |
| 614 | ret = qcow2_check_read_snapshot_table(bs, &snapshot_res, fix); |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 615 | if (ret < 0) { |
Max Reitz | fe446b5 | 2019-10-11 17:28:07 +0200 | [diff] [blame] | 616 | qcow2_add_check_result(result, &snapshot_res, false); |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | ret = qcow2_check_refcounts(bs, &refcount_res, fix); |
| 621 | qcow2_add_check_result(result, &refcount_res, true); |
Stefan Hajnoczi | acbe598 | 2012-08-09 13:05:55 +0100 | [diff] [blame] | 622 | if (ret < 0) { |
Max Reitz | fe446b5 | 2019-10-11 17:28:07 +0200 | [diff] [blame] | 623 | qcow2_add_check_result(result, &snapshot_res, false); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | ret = qcow2_check_fix_snapshot_table(bs, &snapshot_res, fix); |
| 628 | qcow2_add_check_result(result, &snapshot_res, false); |
| 629 | if (ret < 0) { |
Stefan Hajnoczi | acbe598 | 2012-08-09 13:05:55 +0100 | [diff] [blame] | 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | if (fix && result->check_errors == 0 && result->corruptions == 0) { |
Max Reitz | 24530f3 | 2013-08-30 14:34:30 +0200 | [diff] [blame] | 634 | ret = qcow2_mark_clean(bs); |
| 635 | if (ret < 0) { |
| 636 | return ret; |
| 637 | } |
| 638 | return qcow2_mark_consistent(bs); |
Stefan Hajnoczi | acbe598 | 2012-08-09 13:05:55 +0100 | [diff] [blame] | 639 | } |
| 640 | return ret; |
| 641 | } |
| 642 | |
Paolo Bonzini | 2fd6163 | 2018-03-01 17:36:19 +0100 | [diff] [blame] | 643 | static int coroutine_fn qcow2_co_check(BlockDriverState *bs, |
| 644 | BdrvCheckResult *result, |
| 645 | BdrvCheckMode fix) |
| 646 | { |
| 647 | BDRVQcow2State *s = bs->opaque; |
| 648 | int ret; |
| 649 | |
| 650 | qemu_co_mutex_lock(&s->lock); |
| 651 | ret = qcow2_co_check_locked(bs, result, fix); |
| 652 | qemu_co_mutex_unlock(&s->lock); |
| 653 | return ret; |
| 654 | } |
| 655 | |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 656 | int qcow2_validate_table(BlockDriverState *bs, uint64_t offset, |
| 657 | uint64_t entries, size_t entry_len, |
| 658 | int64_t max_size_bytes, const char *table_name, |
| 659 | Error **errp) |
Kevin Wolf | 8c7de28 | 2014-03-26 13:05:44 +0100 | [diff] [blame] | 660 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 661 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 662 | |
| 663 | if (entries > max_size_bytes / entry_len) { |
| 664 | error_setg(errp, "%s too large", table_name); |
| 665 | return -EFBIG; |
| 666 | } |
Kevin Wolf | 8c7de28 | 2014-03-26 13:05:44 +0100 | [diff] [blame] | 667 | |
| 668 | /* Use signed INT64_MAX as the maximum even for uint64_t header fields, |
| 669 | * because values will be passed to qemu functions taking int64_t. */ |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 670 | if ((INT64_MAX - entries * entry_len < offset) || |
| 671 | (offset_into_cluster(s, offset) != 0)) { |
| 672 | error_setg(errp, "%s offset invalid", table_name); |
Kevin Wolf | 8c7de28 | 2014-03-26 13:05:44 +0100 | [diff] [blame] | 673 | return -EINVAL; |
| 674 | } |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
Alberto Garcia | 8a2ce0b | 2019-03-12 18:48:48 +0200 | [diff] [blame] | 679 | static const char *const mutable_opts[] = { |
| 680 | QCOW2_OPT_LAZY_REFCOUNTS, |
| 681 | QCOW2_OPT_DISCARD_REQUEST, |
| 682 | QCOW2_OPT_DISCARD_SNAPSHOT, |
| 683 | QCOW2_OPT_DISCARD_OTHER, |
| 684 | QCOW2_OPT_OVERLAP, |
| 685 | QCOW2_OPT_OVERLAP_TEMPLATE, |
| 686 | QCOW2_OPT_OVERLAP_MAIN_HEADER, |
| 687 | QCOW2_OPT_OVERLAP_ACTIVE_L1, |
| 688 | QCOW2_OPT_OVERLAP_ACTIVE_L2, |
| 689 | QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, |
| 690 | QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, |
| 691 | QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, |
| 692 | QCOW2_OPT_OVERLAP_INACTIVE_L1, |
| 693 | QCOW2_OPT_OVERLAP_INACTIVE_L2, |
| 694 | QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, |
| 695 | QCOW2_OPT_CACHE_SIZE, |
| 696 | QCOW2_OPT_L2_CACHE_SIZE, |
| 697 | QCOW2_OPT_L2_CACHE_ENTRY_SIZE, |
| 698 | QCOW2_OPT_REFCOUNT_CACHE_SIZE, |
| 699 | QCOW2_OPT_CACHE_CLEAN_INTERVAL, |
| 700 | NULL |
| 701 | }; |
| 702 | |
Kevin Wolf | 74c4510 | 2013-03-15 10:35:08 +0100 | [diff] [blame] | 703 | static QemuOptsList qcow2_runtime_opts = { |
| 704 | .name = "qcow2", |
| 705 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), |
| 706 | .desc = { |
| 707 | { |
Kevin Wolf | 64aa99d | 2013-07-17 14:45:34 +0200 | [diff] [blame] | 708 | .name = QCOW2_OPT_LAZY_REFCOUNTS, |
Kevin Wolf | 74c4510 | 2013-03-15 10:35:08 +0100 | [diff] [blame] | 709 | .type = QEMU_OPT_BOOL, |
| 710 | .help = "Postpone refcount updates", |
| 711 | }, |
Kevin Wolf | 67af674 | 2013-06-19 13:44:19 +0200 | [diff] [blame] | 712 | { |
| 713 | .name = QCOW2_OPT_DISCARD_REQUEST, |
| 714 | .type = QEMU_OPT_BOOL, |
| 715 | .help = "Pass guest discard requests to the layer below", |
| 716 | }, |
| 717 | { |
| 718 | .name = QCOW2_OPT_DISCARD_SNAPSHOT, |
| 719 | .type = QEMU_OPT_BOOL, |
| 720 | .help = "Generate discard requests when snapshot related space " |
| 721 | "is freed", |
| 722 | }, |
| 723 | { |
| 724 | .name = QCOW2_OPT_DISCARD_OTHER, |
| 725 | .type = QEMU_OPT_BOOL, |
| 726 | .help = "Generate discard requests when other clusters are freed", |
| 727 | }, |
Max Reitz | 05de7e8 | 2013-10-10 11:09:25 +0200 | [diff] [blame] | 728 | { |
| 729 | .name = QCOW2_OPT_OVERLAP, |
| 730 | .type = QEMU_OPT_STRING, |
| 731 | .help = "Selects which overlap checks to perform from a range of " |
| 732 | "templates (none, constant, cached, all)", |
| 733 | }, |
| 734 | { |
Max Reitz | ee42b5c | 2014-08-20 19:59:35 +0200 | [diff] [blame] | 735 | .name = QCOW2_OPT_OVERLAP_TEMPLATE, |
| 736 | .type = QEMU_OPT_STRING, |
| 737 | .help = "Selects which overlap checks to perform from a range of " |
| 738 | "templates (none, constant, cached, all)", |
| 739 | }, |
| 740 | { |
Max Reitz | 05de7e8 | 2013-10-10 11:09:25 +0200 | [diff] [blame] | 741 | .name = QCOW2_OPT_OVERLAP_MAIN_HEADER, |
| 742 | .type = QEMU_OPT_BOOL, |
| 743 | .help = "Check for unintended writes into the main qcow2 header", |
| 744 | }, |
| 745 | { |
| 746 | .name = QCOW2_OPT_OVERLAP_ACTIVE_L1, |
| 747 | .type = QEMU_OPT_BOOL, |
| 748 | .help = "Check for unintended writes into the active L1 table", |
| 749 | }, |
| 750 | { |
| 751 | .name = QCOW2_OPT_OVERLAP_ACTIVE_L2, |
| 752 | .type = QEMU_OPT_BOOL, |
| 753 | .help = "Check for unintended writes into an active L2 table", |
| 754 | }, |
| 755 | { |
| 756 | .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, |
| 757 | .type = QEMU_OPT_BOOL, |
| 758 | .help = "Check for unintended writes into the refcount table", |
| 759 | }, |
| 760 | { |
| 761 | .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, |
| 762 | .type = QEMU_OPT_BOOL, |
| 763 | .help = "Check for unintended writes into a refcount block", |
| 764 | }, |
| 765 | { |
| 766 | .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, |
| 767 | .type = QEMU_OPT_BOOL, |
| 768 | .help = "Check for unintended writes into the snapshot table", |
| 769 | }, |
| 770 | { |
| 771 | .name = QCOW2_OPT_OVERLAP_INACTIVE_L1, |
| 772 | .type = QEMU_OPT_BOOL, |
| 773 | .help = "Check for unintended writes into an inactive L1 table", |
| 774 | }, |
| 775 | { |
| 776 | .name = QCOW2_OPT_OVERLAP_INACTIVE_L2, |
| 777 | .type = QEMU_OPT_BOOL, |
| 778 | .help = "Check for unintended writes into an inactive L2 table", |
| 779 | }, |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 780 | { |
Vladimir Sementsov-Ogievskiy | 0e4e431 | 2018-07-05 18:15:15 +0300 | [diff] [blame] | 781 | .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, |
| 782 | .type = QEMU_OPT_BOOL, |
| 783 | .help = "Check for unintended writes into the bitmap directory", |
| 784 | }, |
| 785 | { |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 786 | .name = QCOW2_OPT_CACHE_SIZE, |
| 787 | .type = QEMU_OPT_SIZE, |
| 788 | .help = "Maximum combined metadata (L2 tables and refcount blocks) " |
| 789 | "cache size", |
| 790 | }, |
| 791 | { |
| 792 | .name = QCOW2_OPT_L2_CACHE_SIZE, |
| 793 | .type = QEMU_OPT_SIZE, |
| 794 | .help = "Maximum L2 table cache size", |
| 795 | }, |
| 796 | { |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 797 | .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE, |
| 798 | .type = QEMU_OPT_SIZE, |
| 799 | .help = "Size of each entry in the L2 cache", |
| 800 | }, |
| 801 | { |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 802 | .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE, |
| 803 | .type = QEMU_OPT_SIZE, |
| 804 | .help = "Maximum refcount block cache size", |
| 805 | }, |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 806 | { |
| 807 | .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL, |
| 808 | .type = QEMU_OPT_NUMBER, |
| 809 | .help = "Clean unused cache entries after this time (in seconds)", |
| 810 | }, |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 811 | BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.", |
| 812 | "ID of secret providing qcow2 AES key or LUKS passphrase"), |
Kevin Wolf | 74c4510 | 2013-03-15 10:35:08 +0100 | [diff] [blame] | 813 | { /* end of list */ } |
| 814 | }, |
| 815 | }; |
| 816 | |
Max Reitz | 4092e99 | 2013-10-10 11:09:26 +0200 | [diff] [blame] | 817 | static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = { |
Vladimir Sementsov-Ogievskiy | 0e4e431 | 2018-07-05 18:15:15 +0300 | [diff] [blame] | 818 | [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER, |
| 819 | [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1, |
| 820 | [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2, |
| 821 | [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, |
| 822 | [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, |
| 823 | [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, |
| 824 | [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1, |
| 825 | [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2, |
| 826 | [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, |
Max Reitz | 4092e99 | 2013-10-10 11:09:26 +0200 | [diff] [blame] | 827 | }; |
| 828 | |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 829 | static void cache_clean_timer_cb(void *opaque) |
| 830 | { |
| 831 | BlockDriverState *bs = opaque; |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 832 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | b2f68bf | 2018-02-05 16:33:09 +0200 | [diff] [blame] | 833 | qcow2_cache_clean_unused(s->l2_table_cache); |
| 834 | qcow2_cache_clean_unused(s->refcount_block_cache); |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 835 | timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + |
| 836 | (int64_t) s->cache_clean_interval * 1000); |
| 837 | } |
| 838 | |
| 839 | static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context) |
| 840 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 841 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 842 | if (s->cache_clean_interval > 0) { |
| 843 | s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL, |
| 844 | SCALE_MS, cache_clean_timer_cb, |
| 845 | bs); |
| 846 | timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + |
| 847 | (int64_t) s->cache_clean_interval * 1000); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | static void cache_clean_timer_del(BlockDriverState *bs) |
| 852 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 853 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 854 | if (s->cache_clean_timer) { |
| 855 | timer_del(s->cache_clean_timer); |
| 856 | timer_free(s->cache_clean_timer); |
| 857 | s->cache_clean_timer = NULL; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static void qcow2_detach_aio_context(BlockDriverState *bs) |
| 862 | { |
| 863 | cache_clean_timer_del(bs); |
| 864 | } |
| 865 | |
| 866 | static void qcow2_attach_aio_context(BlockDriverState *bs, |
| 867 | AioContext *new_context) |
| 868 | { |
| 869 | cache_clean_timer_init(bs, new_context); |
| 870 | } |
| 871 | |
Max Reitz | bc85ef2 | 2015-06-01 18:09:19 +0200 | [diff] [blame] | 872 | static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts, |
| 873 | uint64_t *l2_cache_size, |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 874 | uint64_t *l2_cache_entry_size, |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 875 | uint64_t *refcount_cache_size, Error **errp) |
| 876 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 877 | BDRVQcow2State *s = bs->opaque; |
Leonid Bloch | b749562 | 2018-09-26 19:04:43 +0300 | [diff] [blame] | 878 | uint64_t combined_cache_size, l2_cache_max_setting; |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 879 | bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set; |
Alberto Garcia | af39bd0 | 2019-02-13 18:48:53 +0200 | [diff] [blame] | 880 | bool l2_cache_entry_size_set; |
Alberto Garcia | 7af5eea | 2018-05-28 17:01:28 +0200 | [diff] [blame] | 881 | int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size; |
Leonid Bloch | b749562 | 2018-09-26 19:04:43 +0300 | [diff] [blame] | 882 | uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
Alberto Garcia | b70d082 | 2019-08-16 15:17:42 +0300 | [diff] [blame] | 883 | uint64_t max_l2_entries = DIV_ROUND_UP(virtual_disk_size, s->cluster_size); |
| 884 | /* An L2 table is always one cluster in size so the max cache size |
| 885 | * should be a multiple of the cluster size. */ |
Alberto Garcia | c8fd855 | 2020-07-10 18:12:54 +0200 | [diff] [blame] | 886 | uint64_t max_l2_cache = ROUND_UP(max_l2_entries * l2_entry_size(s), |
Alberto Garcia | b70d082 | 2019-08-16 15:17:42 +0300 | [diff] [blame] | 887 | s->cluster_size); |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 888 | |
| 889 | combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE); |
| 890 | l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE); |
| 891 | refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE); |
Alberto Garcia | af39bd0 | 2019-02-13 18:48:53 +0200 | [diff] [blame] | 892 | l2_cache_entry_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE); |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 893 | |
| 894 | combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0); |
Leonid Bloch | b749562 | 2018-09-26 19:04:43 +0300 | [diff] [blame] | 895 | l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, |
| 896 | DEFAULT_L2_CACHE_MAX_SIZE); |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 897 | *refcount_cache_size = qemu_opt_get_size(opts, |
| 898 | QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0); |
| 899 | |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 900 | *l2_cache_entry_size = qemu_opt_get_size( |
| 901 | opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size); |
| 902 | |
Leonid Bloch | b749562 | 2018-09-26 19:04:43 +0300 | [diff] [blame] | 903 | *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting); |
| 904 | |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 905 | if (combined_cache_size_set) { |
| 906 | if (l2_cache_size_set && refcount_cache_size_set) { |
| 907 | error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE |
| 908 | " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set " |
Leonid Bloch | 308999e | 2018-07-25 17:27:55 +0300 | [diff] [blame] | 909 | "at the same time"); |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 910 | return; |
Leonid Bloch | b749562 | 2018-09-26 19:04:43 +0300 | [diff] [blame] | 911 | } else if (l2_cache_size_set && |
| 912 | (l2_cache_max_setting > combined_cache_size)) { |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 913 | error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed " |
| 914 | QCOW2_OPT_CACHE_SIZE); |
| 915 | return; |
| 916 | } else if (*refcount_cache_size > combined_cache_size) { |
| 917 | error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed " |
| 918 | QCOW2_OPT_CACHE_SIZE); |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | if (l2_cache_size_set) { |
| 923 | *refcount_cache_size = combined_cache_size - *l2_cache_size; |
| 924 | } else if (refcount_cache_size_set) { |
| 925 | *l2_cache_size = combined_cache_size - *refcount_cache_size; |
| 926 | } else { |
Alberto Garcia | 5225399 | 2018-04-17 15:37:04 +0300 | [diff] [blame] | 927 | /* Assign as much memory as possible to the L2 cache, and |
| 928 | * use the remainder for the refcount cache */ |
| 929 | if (combined_cache_size >= max_l2_cache + min_refcount_cache) { |
| 930 | *l2_cache_size = max_l2_cache; |
| 931 | *refcount_cache_size = combined_cache_size - *l2_cache_size; |
| 932 | } else { |
| 933 | *refcount_cache_size = |
| 934 | MIN(combined_cache_size, min_refcount_cache); |
| 935 | *l2_cache_size = combined_cache_size - *refcount_cache_size; |
| 936 | } |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 937 | } |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 938 | } |
Alberto Garcia | af39bd0 | 2019-02-13 18:48:53 +0200 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * If the L2 cache is not enough to cover the whole disk then |
| 942 | * default to 4KB entries. Smaller entries reduce the cost of |
| 943 | * loads and evictions and increase I/O performance. |
| 944 | */ |
| 945 | if (*l2_cache_size < max_l2_cache && !l2_cache_entry_size_set) { |
| 946 | *l2_cache_entry_size = MIN(s->cluster_size, 4096); |
| 947 | } |
| 948 | |
Leonid Bloch | 657ada5 | 2018-09-26 19:04:42 +0300 | [diff] [blame] | 949 | /* l2_cache_size and refcount_cache_size are ensured to have at least |
| 950 | * their minimum values in qcow2_update_options_prepare() */ |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 951 | |
| 952 | if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) || |
| 953 | *l2_cache_entry_size > s->cluster_size || |
| 954 | !is_power_of_2(*l2_cache_entry_size)) { |
| 955 | error_setg(errp, "L2 cache entry size must be a power of two " |
| 956 | "between %d and the cluster size (%d)", |
| 957 | 1 << MIN_CLUSTER_BITS, s->cluster_size); |
| 958 | return; |
| 959 | } |
Max Reitz | 6c1c8d5 | 2014-08-18 22:07:33 +0200 | [diff] [blame] | 960 | } |
| 961 | |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 962 | typedef struct Qcow2ReopenState { |
| 963 | Qcow2Cache *l2_table_cache; |
| 964 | Qcow2Cache *refcount_block_cache; |
Alberto Garcia | 3c2e511 | 2018-02-05 16:33:13 +0200 | [diff] [blame] | 965 | int l2_slice_size; /* Number of entries in a slice of the L2 table */ |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 966 | bool use_lazy_refcounts; |
| 967 | int overlap_check; |
| 968 | bool discard_passthrough[QCOW2_DISCARD_MAX]; |
| 969 | uint64_t cache_clean_interval; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 970 | QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */ |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 971 | } Qcow2ReopenState; |
| 972 | |
| 973 | static int qcow2_update_options_prepare(BlockDriverState *bs, |
| 974 | Qcow2ReopenState *r, |
| 975 | QDict *options, int flags, |
| 976 | Error **errp) |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 977 | { |
| 978 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 979 | QemuOpts *opts = NULL; |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 980 | const char *opt_overlap_check, *opt_overlap_check_template; |
| 981 | int overlap_check_template = 0; |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 982 | uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size; |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 983 | int i; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 984 | const char *encryptfmt; |
| 985 | QDict *encryptopts = NULL; |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 986 | Error *local_err = NULL; |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 987 | int ret; |
| 988 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 989 | qdict_extract_subqdict(options, &encryptopts, "encrypt."); |
| 990 | encryptfmt = qdict_get_try_str(encryptopts, "format"); |
| 991 | |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 992 | opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort); |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 993 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 994 | ret = -EINVAL; |
| 995 | goto fail; |
| 996 | } |
| 997 | |
| 998 | /* get L2 table/refcount block cache size from command line options */ |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 999 | read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size, |
| 1000 | &refcount_cache_size, &local_err); |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1001 | if (local_err) { |
| 1002 | error_propagate(errp, local_err); |
| 1003 | ret = -EINVAL; |
| 1004 | goto fail; |
| 1005 | } |
| 1006 | |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 1007 | l2_cache_size /= l2_cache_entry_size; |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1008 | if (l2_cache_size < MIN_L2_CACHE_SIZE) { |
| 1009 | l2_cache_size = MIN_L2_CACHE_SIZE; |
| 1010 | } |
| 1011 | if (l2_cache_size > INT_MAX) { |
| 1012 | error_setg(errp, "L2 cache size too big"); |
| 1013 | ret = -EINVAL; |
| 1014 | goto fail; |
| 1015 | } |
| 1016 | |
| 1017 | refcount_cache_size /= s->cluster_size; |
| 1018 | if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) { |
| 1019 | refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE; |
| 1020 | } |
| 1021 | if (refcount_cache_size > INT_MAX) { |
| 1022 | error_setg(errp, "Refcount cache size too big"); |
| 1023 | ret = -EINVAL; |
| 1024 | goto fail; |
| 1025 | } |
| 1026 | |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1027 | /* alloc new L2 table/refcount block cache, flush old one */ |
| 1028 | if (s->l2_table_cache) { |
| 1029 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
| 1030 | if (ret) { |
| 1031 | error_setg_errno(errp, -ret, "Failed to flush the L2 table cache"); |
| 1032 | goto fail; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | if (s->refcount_block_cache) { |
| 1037 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
| 1038 | if (ret) { |
| 1039 | error_setg_errno(errp, -ret, |
| 1040 | "Failed to flush the refcount block cache"); |
| 1041 | goto fail; |
| 1042 | } |
| 1043 | } |
| 1044 | |
Alberto Garcia | c8fd855 | 2020-07-10 18:12:54 +0200 | [diff] [blame] | 1045 | r->l2_slice_size = l2_cache_entry_size / l2_entry_size(s); |
Alberto Garcia | 1221fe6 | 2018-02-05 16:33:36 +0200 | [diff] [blame] | 1046 | r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size, |
| 1047 | l2_cache_entry_size); |
| 1048 | r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size, |
| 1049 | s->cluster_size); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1050 | if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) { |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1051 | error_setg(errp, "Could not allocate metadata caches"); |
| 1052 | ret = -ENOMEM; |
| 1053 | goto fail; |
| 1054 | } |
| 1055 | |
| 1056 | /* New interval for cache cleanup timer */ |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1057 | r->cache_clean_interval = |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1058 | qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL, |
Leonid Bloch | e957b50 | 2018-09-26 19:04:46 +0300 | [diff] [blame] | 1059 | DEFAULT_CACHE_CLEAN_INTERVAL); |
Alberto Garcia | 91203f0 | 2016-11-25 13:27:44 +0200 | [diff] [blame] | 1060 | #ifndef CONFIG_LINUX |
| 1061 | if (r->cache_clean_interval != 0) { |
| 1062 | error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL |
| 1063 | " not supported on this host"); |
| 1064 | ret = -EINVAL; |
| 1065 | goto fail; |
| 1066 | } |
| 1067 | #endif |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1068 | if (r->cache_clean_interval > UINT_MAX) { |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1069 | error_setg(errp, "Cache clean interval too big"); |
| 1070 | ret = -EINVAL; |
| 1071 | goto fail; |
| 1072 | } |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1073 | |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1074 | /* lazy-refcounts; flush if going from enabled to disabled */ |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1075 | r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1076 | (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1077 | if (r->use_lazy_refcounts && s->qcow_version < 3) { |
Kevin Wolf | 007dbc3 | 2015-04-16 13:11:39 +0200 | [diff] [blame] | 1078 | error_setg(errp, "Lazy refcounts require a qcow2 image with at least " |
| 1079 | "qemu 1.1 compatibility level"); |
| 1080 | ret = -EINVAL; |
| 1081 | goto fail; |
| 1082 | } |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1083 | |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1084 | if (s->use_lazy_refcounts && !r->use_lazy_refcounts) { |
| 1085 | ret = qcow2_mark_clean(bs); |
| 1086 | if (ret < 0) { |
| 1087 | error_setg_errno(errp, -ret, "Failed to disable lazy refcounts"); |
| 1088 | goto fail; |
| 1089 | } |
| 1090 | } |
| 1091 | |
Kevin Wolf | 007dbc3 | 2015-04-16 13:11:39 +0200 | [diff] [blame] | 1092 | /* Overlap check options */ |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1093 | opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP); |
| 1094 | opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE); |
| 1095 | if (opt_overlap_check_template && opt_overlap_check && |
| 1096 | strcmp(opt_overlap_check_template, opt_overlap_check)) |
| 1097 | { |
| 1098 | error_setg(errp, "Conflicting values for qcow2 options '" |
| 1099 | QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE |
| 1100 | "' ('%s')", opt_overlap_check, opt_overlap_check_template); |
| 1101 | ret = -EINVAL; |
| 1102 | goto fail; |
| 1103 | } |
| 1104 | if (!opt_overlap_check) { |
| 1105 | opt_overlap_check = opt_overlap_check_template ?: "cached"; |
| 1106 | } |
| 1107 | |
| 1108 | if (!strcmp(opt_overlap_check, "none")) { |
| 1109 | overlap_check_template = 0; |
| 1110 | } else if (!strcmp(opt_overlap_check, "constant")) { |
| 1111 | overlap_check_template = QCOW2_OL_CONSTANT; |
| 1112 | } else if (!strcmp(opt_overlap_check, "cached")) { |
| 1113 | overlap_check_template = QCOW2_OL_CACHED; |
| 1114 | } else if (!strcmp(opt_overlap_check, "all")) { |
| 1115 | overlap_check_template = QCOW2_OL_ALL; |
| 1116 | } else { |
| 1117 | error_setg(errp, "Unsupported value '%s' for qcow2 option " |
| 1118 | "'overlap-check'. Allowed are any of the following: " |
| 1119 | "none, constant, cached, all", opt_overlap_check); |
| 1120 | ret = -EINVAL; |
| 1121 | goto fail; |
| 1122 | } |
| 1123 | |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1124 | r->overlap_check = 0; |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1125 | for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) { |
| 1126 | /* overlap-check defines a template bitmask, but every flag may be |
| 1127 | * overwritten through the associated boolean option */ |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1128 | r->overlap_check |= |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1129 | qemu_opt_get_bool(opts, overlap_bool_option_names[i], |
| 1130 | overlap_check_template & (1 << i)) << i; |
| 1131 | } |
| 1132 | |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1133 | r->discard_passthrough[QCOW2_DISCARD_NEVER] = false; |
| 1134 | r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; |
| 1135 | r->discard_passthrough[QCOW2_DISCARD_REQUEST] = |
Kevin Wolf | 007dbc3 | 2015-04-16 13:11:39 +0200 | [diff] [blame] | 1136 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, |
| 1137 | flags & BDRV_O_UNMAP); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1138 | r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = |
Kevin Wolf | 007dbc3 | 2015-04-16 13:11:39 +0200 | [diff] [blame] | 1139 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1140 | r->discard_passthrough[QCOW2_DISCARD_OTHER] = |
Kevin Wolf | 007dbc3 | 2015-04-16 13:11:39 +0200 | [diff] [blame] | 1141 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); |
| 1142 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1143 | switch (s->crypt_method_header) { |
| 1144 | case QCOW_CRYPT_NONE: |
| 1145 | if (encryptfmt) { |
| 1146 | error_setg(errp, "No encryption in image header, but options " |
| 1147 | "specified format '%s'", encryptfmt); |
| 1148 | ret = -EINVAL; |
| 1149 | goto fail; |
| 1150 | } |
| 1151 | break; |
| 1152 | |
| 1153 | case QCOW_CRYPT_AES: |
| 1154 | if (encryptfmt && !g_str_equal(encryptfmt, "aes")) { |
| 1155 | error_setg(errp, |
| 1156 | "Header reported 'aes' encryption format but " |
| 1157 | "options specify '%s'", encryptfmt); |
| 1158 | ret = -EINVAL; |
| 1159 | goto fail; |
| 1160 | } |
Markus Armbruster | 796d323 | 2018-06-26 19:41:19 +0200 | [diff] [blame] | 1161 | qdict_put_str(encryptopts, "format", "qcow"); |
| 1162 | r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp); |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1163 | break; |
| 1164 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 1165 | case QCOW_CRYPT_LUKS: |
| 1166 | if (encryptfmt && !g_str_equal(encryptfmt, "luks")) { |
| 1167 | error_setg(errp, |
| 1168 | "Header reported 'luks' encryption format but " |
| 1169 | "options specify '%s'", encryptfmt); |
| 1170 | ret = -EINVAL; |
| 1171 | goto fail; |
| 1172 | } |
Markus Armbruster | 796d323 | 2018-06-26 19:41:19 +0200 | [diff] [blame] | 1173 | qdict_put_str(encryptopts, "format", "luks"); |
| 1174 | r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 1175 | break; |
| 1176 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1177 | default: |
| 1178 | error_setg(errp, "Unsupported encryption method %d", |
| 1179 | s->crypt_method_header); |
| 1180 | break; |
| 1181 | } |
| 1182 | if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) { |
| 1183 | ret = -EINVAL; |
| 1184 | goto fail; |
| 1185 | } |
| 1186 | |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1187 | ret = 0; |
| 1188 | fail: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 1189 | qobject_unref(encryptopts); |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1190 | qemu_opts_del(opts); |
| 1191 | opts = NULL; |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1192 | return ret; |
| 1193 | } |
| 1194 | |
| 1195 | static void qcow2_update_options_commit(BlockDriverState *bs, |
| 1196 | Qcow2ReopenState *r) |
| 1197 | { |
| 1198 | BDRVQcow2State *s = bs->opaque; |
| 1199 | int i; |
| 1200 | |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1201 | if (s->l2_table_cache) { |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 1202 | qcow2_cache_destroy(s->l2_table_cache); |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1203 | } |
| 1204 | if (s->refcount_block_cache) { |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 1205 | qcow2_cache_destroy(s->refcount_block_cache); |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1206 | } |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1207 | s->l2_table_cache = r->l2_table_cache; |
| 1208 | s->refcount_block_cache = r->refcount_block_cache; |
Alberto Garcia | 3c2e511 | 2018-02-05 16:33:13 +0200 | [diff] [blame] | 1209 | s->l2_slice_size = r->l2_slice_size; |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1210 | |
| 1211 | s->overlap_check = r->overlap_check; |
| 1212 | s->use_lazy_refcounts = r->use_lazy_refcounts; |
| 1213 | |
| 1214 | for (i = 0; i < QCOW2_DISCARD_MAX; i++) { |
| 1215 | s->discard_passthrough[i] = r->discard_passthrough[i]; |
| 1216 | } |
| 1217 | |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1218 | if (s->cache_clean_interval != r->cache_clean_interval) { |
| 1219 | cache_clean_timer_del(bs); |
| 1220 | s->cache_clean_interval = r->cache_clean_interval; |
| 1221 | cache_clean_timer_init(bs, bdrv_get_aio_context(bs)); |
| 1222 | } |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1223 | |
| 1224 | qapi_free_QCryptoBlockOpenOptions(s->crypto_opts); |
| 1225 | s->crypto_opts = r->crypto_opts; |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static void qcow2_update_options_abort(BlockDriverState *bs, |
| 1229 | Qcow2ReopenState *r) |
| 1230 | { |
| 1231 | if (r->l2_table_cache) { |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 1232 | qcow2_cache_destroy(r->l2_table_cache); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1233 | } |
| 1234 | if (r->refcount_block_cache) { |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 1235 | qcow2_cache_destroy(r->refcount_block_cache); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1236 | } |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1237 | qapi_free_QCryptoBlockOpenOptions(r->crypto_opts); |
Kevin Wolf | ee55b17 | 2015-04-16 16:16:02 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static int qcow2_update_options(BlockDriverState *bs, QDict *options, |
| 1241 | int flags, Error **errp) |
| 1242 | { |
| 1243 | Qcow2ReopenState r = {}; |
| 1244 | int ret; |
| 1245 | |
| 1246 | ret = qcow2_update_options_prepare(bs, &r, options, flags, errp); |
| 1247 | if (ret >= 0) { |
| 1248 | qcow2_update_options_commit(bs, &r); |
| 1249 | } else { |
| 1250 | qcow2_update_options_abort(bs, &r); |
| 1251 | } |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1252 | |
Kevin Wolf | 4c75d1a | 2015-04-16 11:29:27 +0200 | [diff] [blame] | 1253 | return ret; |
| 1254 | } |
| 1255 | |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 1256 | static int validate_compression_type(BDRVQcow2State *s, Error **errp) |
| 1257 | { |
| 1258 | switch (s->compression_type) { |
| 1259 | case QCOW2_COMPRESSION_TYPE_ZLIB: |
Denis Plotnikov | d298ac1 | 2020-05-07 11:25:20 +0300 | [diff] [blame] | 1260 | #ifdef CONFIG_ZSTD |
| 1261 | case QCOW2_COMPRESSION_TYPE_ZSTD: |
| 1262 | #endif |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 1263 | break; |
| 1264 | |
| 1265 | default: |
| 1266 | error_setg(errp, "qcow2: unknown compression type: %u", |
| 1267 | s->compression_type); |
| 1268 | return -ENOTSUP; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * if the compression type differs from QCOW2_COMPRESSION_TYPE_ZLIB |
| 1273 | * the incompatible feature flag must be set |
| 1274 | */ |
| 1275 | if (s->compression_type == QCOW2_COMPRESSION_TYPE_ZLIB) { |
| 1276 | if (s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION) { |
| 1277 | error_setg(errp, "qcow2: Compression type incompatible feature " |
| 1278 | "bit must not be set"); |
| 1279 | return -EINVAL; |
| 1280 | } |
| 1281 | } else { |
| 1282 | if (!(s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION)) { |
| 1283 | error_setg(errp, "qcow2: Compression type incompatible feature " |
| 1284 | "bit must be set"); |
| 1285 | return -EINVAL; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Paolo Bonzini | 1fafcd9 | 2018-03-01 17:36:16 +0100 | [diff] [blame] | 1292 | /* Called with s->lock held. */ |
| 1293 | static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, |
| 1294 | int flags, Error **errp) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1295 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1296 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 6d33e8e | 2014-03-26 13:05:47 +0100 | [diff] [blame] | 1297 | unsigned int len, i; |
| 1298 | int ret = 0; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1299 | QCowHeader header; |
Kevin Wolf | 74c4510 | 2013-03-15 10:35:08 +0100 | [diff] [blame] | 1300 | Error *local_err = NULL; |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 1301 | uint64_t ext_end; |
Kevin Wolf | 2cf7cfa | 2013-05-14 16:14:33 +0200 | [diff] [blame] | 1302 | uint64_t l1_vm_state_index; |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 1303 | bool update_header = false; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1304 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1305 | ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1306 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1307 | error_setg_errno(errp, -ret, "Could not read qcow2 header"); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1308 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1309 | } |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 1310 | header.magic = be32_to_cpu(header.magic); |
| 1311 | header.version = be32_to_cpu(header.version); |
| 1312 | header.backing_file_offset = be64_to_cpu(header.backing_file_offset); |
| 1313 | header.backing_file_size = be32_to_cpu(header.backing_file_size); |
| 1314 | header.size = be64_to_cpu(header.size); |
| 1315 | header.cluster_bits = be32_to_cpu(header.cluster_bits); |
| 1316 | header.crypt_method = be32_to_cpu(header.crypt_method); |
| 1317 | header.l1_table_offset = be64_to_cpu(header.l1_table_offset); |
| 1318 | header.l1_size = be32_to_cpu(header.l1_size); |
| 1319 | header.refcount_table_offset = be64_to_cpu(header.refcount_table_offset); |
| 1320 | header.refcount_table_clusters = |
| 1321 | be32_to_cpu(header.refcount_table_clusters); |
| 1322 | header.snapshots_offset = be64_to_cpu(header.snapshots_offset); |
| 1323 | header.nb_snapshots = be32_to_cpu(header.nb_snapshots); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1324 | |
Kevin Wolf | e8cdcec | 2011-02-09 11:11:07 +0100 | [diff] [blame] | 1325 | if (header.magic != QCOW_MAGIC) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1326 | error_setg(errp, "Image is not in qcow2 format"); |
Paolo Bonzini | 76abe40 | 2014-02-17 14:44:06 +0100 | [diff] [blame] | 1327 | ret = -EINVAL; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1328 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1329 | } |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1330 | if (header.version < 2 || header.version > 3) { |
Max Reitz | a55448b | 2016-03-16 19:54:33 +0100 | [diff] [blame] | 1331 | error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version); |
Kevin Wolf | e8cdcec | 2011-02-09 11:11:07 +0100 | [diff] [blame] | 1332 | ret = -ENOTSUP; |
| 1333 | goto fail; |
| 1334 | } |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1335 | |
| 1336 | s->qcow_version = header.version; |
| 1337 | |
Kevin Wolf | 24342f2 | 2014-03-26 13:05:41 +0100 | [diff] [blame] | 1338 | /* Initialise cluster size */ |
| 1339 | if (header.cluster_bits < MIN_CLUSTER_BITS || |
| 1340 | header.cluster_bits > MAX_CLUSTER_BITS) { |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 1341 | error_setg(errp, "Unsupported cluster size: 2^%" PRIu32, |
| 1342 | header.cluster_bits); |
Kevin Wolf | 24342f2 | 2014-03-26 13:05:41 +0100 | [diff] [blame] | 1343 | ret = -EINVAL; |
| 1344 | goto fail; |
| 1345 | } |
| 1346 | |
| 1347 | s->cluster_bits = header.cluster_bits; |
| 1348 | s->cluster_size = 1 << s->cluster_bits; |
Kevin Wolf | 24342f2 | 2014-03-26 13:05:41 +0100 | [diff] [blame] | 1349 | |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1350 | /* Initialise version 3 header fields */ |
| 1351 | if (header.version == 2) { |
| 1352 | header.incompatible_features = 0; |
| 1353 | header.compatible_features = 0; |
| 1354 | header.autoclear_features = 0; |
| 1355 | header.refcount_order = 4; |
| 1356 | header.header_length = 72; |
| 1357 | } else { |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 1358 | header.incompatible_features = |
| 1359 | be64_to_cpu(header.incompatible_features); |
| 1360 | header.compatible_features = be64_to_cpu(header.compatible_features); |
| 1361 | header.autoclear_features = be64_to_cpu(header.autoclear_features); |
| 1362 | header.refcount_order = be32_to_cpu(header.refcount_order); |
| 1363 | header.header_length = be32_to_cpu(header.header_length); |
Kevin Wolf | 24342f2 | 2014-03-26 13:05:41 +0100 | [diff] [blame] | 1364 | |
| 1365 | if (header.header_length < 104) { |
| 1366 | error_setg(errp, "qcow2 header too short"); |
| 1367 | ret = -EINVAL; |
| 1368 | goto fail; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | if (header.header_length > s->cluster_size) { |
| 1373 | error_setg(errp, "qcow2 header exceeds cluster size"); |
| 1374 | ret = -EINVAL; |
| 1375 | goto fail; |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | if (header.header_length > sizeof(header)) { |
| 1379 | s->unknown_header_fields_size = header.header_length - sizeof(header); |
| 1380 | s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1381 | ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields, |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1382 | s->unknown_header_fields_size); |
| 1383 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1384 | error_setg_errno(errp, -ret, "Could not read unknown qcow2 header " |
| 1385 | "fields"); |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1386 | goto fail; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Kevin Wolf | a1b3955c | 2014-03-26 13:05:42 +0100 | [diff] [blame] | 1390 | if (header.backing_file_offset > s->cluster_size) { |
| 1391 | error_setg(errp, "Invalid backing file offset"); |
| 1392 | ret = -EINVAL; |
| 1393 | goto fail; |
| 1394 | } |
| 1395 | |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 1396 | if (header.backing_file_offset) { |
| 1397 | ext_end = header.backing_file_offset; |
| 1398 | } else { |
| 1399 | ext_end = 1 << header.cluster_bits; |
| 1400 | } |
| 1401 | |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1402 | /* Handle feature bits */ |
| 1403 | s->incompatible_features = header.incompatible_features; |
| 1404 | s->compatible_features = header.compatible_features; |
| 1405 | s->autoclear_features = header.autoclear_features; |
| 1406 | |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 1407 | /* |
| 1408 | * Handle compression type |
| 1409 | * Older qcow2 images don't contain the compression type header. |
| 1410 | * Distinguish them by the header length and use |
| 1411 | * the only valid (default) compression type in that case |
| 1412 | */ |
| 1413 | if (header.header_length > offsetof(QCowHeader, compression_type)) { |
| 1414 | s->compression_type = header.compression_type; |
| 1415 | } else { |
| 1416 | s->compression_type = QCOW2_COMPRESSION_TYPE_ZLIB; |
| 1417 | } |
| 1418 | |
| 1419 | ret = validate_compression_type(s, errp); |
| 1420 | if (ret) { |
| 1421 | goto fail; |
| 1422 | } |
| 1423 | |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 1424 | if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 1425 | void *feature_table = NULL; |
| 1426 | qcow2_read_extensions(bs, header.header_length, ext_end, |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 1427 | &feature_table, flags, NULL, NULL); |
Max Reitz | a55448b | 2016-03-16 19:54:33 +0100 | [diff] [blame] | 1428 | report_unsupported_feature(errp, feature_table, |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 1429 | s->incompatible_features & |
| 1430 | ~QCOW2_INCOMPAT_MASK); |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1431 | ret = -ENOTSUP; |
Prasad Joshi | c5a33ee | 2014-03-28 23:08:58 +0530 | [diff] [blame] | 1432 | g_free(feature_table); |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1433 | goto fail; |
| 1434 | } |
| 1435 | |
Max Reitz | 69c9872 | 2013-08-30 14:34:24 +0200 | [diff] [blame] | 1436 | if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { |
| 1437 | /* Corrupt images may not be written to unless they are being repaired |
| 1438 | */ |
| 1439 | if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1440 | error_setg(errp, "qcow2: Image is corrupt; cannot be opened " |
| 1441 | "read/write"); |
Max Reitz | 69c9872 | 2013-08-30 14:34:24 +0200 | [diff] [blame] | 1442 | ret = -EACCES; |
| 1443 | goto fail; |
| 1444 | } |
| 1445 | } |
| 1446 | |
Alberto Garcia | d0346b5 | 2020-07-10 18:12:51 +0200 | [diff] [blame] | 1447 | s->subclusters_per_cluster = |
| 1448 | has_subclusters(s) ? QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER : 1; |
| 1449 | s->subcluster_size = s->cluster_size / s->subclusters_per_cluster; |
| 1450 | s->subcluster_bits = ctz32(s->subcluster_size); |
| 1451 | |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 1452 | if (s->subcluster_size < (1 << MIN_CLUSTER_BITS)) { |
| 1453 | error_setg(errp, "Unsupported subcluster size: %d", s->subcluster_size); |
| 1454 | ret = -EINVAL; |
| 1455 | goto fail; |
| 1456 | } |
| 1457 | |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1458 | /* Check support for various header values */ |
Max Reitz | b72faf9 | 2015-02-10 15:28:52 -0500 | [diff] [blame] | 1459 | if (header.refcount_order > 6) { |
| 1460 | error_setg(errp, "Reference count entry width too large; may not " |
| 1461 | "exceed 64 bits"); |
| 1462 | ret = -EINVAL; |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1463 | goto fail; |
| 1464 | } |
Max Reitz | b6481f3 | 2013-09-03 10:09:53 +0200 | [diff] [blame] | 1465 | s->refcount_order = header.refcount_order; |
Max Reitz | 346a53d | 2015-02-10 15:28:43 -0500 | [diff] [blame] | 1466 | s->refcount_bits = 1 << s->refcount_order; |
| 1467 | s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1); |
| 1468 | s->refcount_max += s->refcount_max - 1; |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1469 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1470 | s->crypt_method_header = header.crypt_method; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1471 | if (s->crypt_method_header) { |
Daniel P. Berrange | e6ff69b | 2016-03-21 14:11:48 +0000 | [diff] [blame] | 1472 | if (bdrv_uses_whitelist() && |
| 1473 | s->crypt_method_header == QCOW_CRYPT_AES) { |
Daniel P. Berrange | 8c0dcbc | 2016-06-13 12:30:09 +0100 | [diff] [blame] | 1474 | error_setg(errp, |
| 1475 | "Use of AES-CBC encrypted qcow2 images is no longer " |
| 1476 | "supported in system emulators"); |
| 1477 | error_append_hint(errp, |
| 1478 | "You can use 'qemu-img convert' to convert your " |
| 1479 | "image to an alternative supported format, such " |
| 1480 | "as unencrypted qcow2, or raw with the LUKS " |
| 1481 | "format instead.\n"); |
| 1482 | ret = -ENOSYS; |
| 1483 | goto fail; |
Daniel P. Berrange | e6ff69b | 2016-03-21 14:11:48 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 1486 | if (s->crypt_method_header == QCOW_CRYPT_AES) { |
| 1487 | s->crypt_physical_offset = false; |
| 1488 | } else { |
| 1489 | /* Assuming LUKS and any future crypt methods we |
| 1490 | * add will all use physical offsets, due to the |
| 1491 | * fact that the alternative is insecure... */ |
| 1492 | s->crypt_physical_offset = true; |
| 1493 | } |
| 1494 | |
Eric Blake | 5411541 | 2016-06-23 16:37:26 -0600 | [diff] [blame] | 1495 | bs->encrypted = true; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1496 | } |
Kevin Wolf | 24342f2 | 2014-03-26 13:05:41 +0100 | [diff] [blame] | 1497 | |
Alberto Garcia | c8fd855 | 2020-07-10 18:12:54 +0200 | [diff] [blame] | 1498 | s->l2_bits = s->cluster_bits - ctz32(l2_entry_size(s)); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1499 | s->l2_size = 1 << s->l2_bits; |
Max Reitz | 1d13d65 | 2014-10-22 14:09:28 +0200 | [diff] [blame] | 1500 | /* 2^(s->refcount_order - 3) is the refcount width in bytes */ |
| 1501 | s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3); |
| 1502 | s->refcount_block_size = 1 << s->refcount_block_bits; |
Leonid Bloch | bd016b9 | 2018-09-26 19:04:47 +0300 | [diff] [blame] | 1503 | bs->total_sectors = header.size / BDRV_SECTOR_SIZE; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1504 | s->csize_shift = (62 - (s->cluster_bits - 8)); |
| 1505 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; |
| 1506 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; |
Kevin Wolf | 5dab2fa | 2014-03-26 13:05:43 +0100 | [diff] [blame] | 1507 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1508 | s->refcount_table_offset = header.refcount_table_offset; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1509 | s->refcount_table_size = |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1510 | header.refcount_table_clusters << (s->cluster_bits - 3); |
| 1511 | |
Alberto Garcia | 951053a | 2017-11-03 16:18:53 +0200 | [diff] [blame] | 1512 | if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) { |
| 1513 | error_setg(errp, "Image does not contain a reference count table"); |
| 1514 | ret = -EINVAL; |
| 1515 | goto fail; |
| 1516 | } |
| 1517 | |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 1518 | ret = qcow2_validate_table(bs, s->refcount_table_offset, |
| 1519 | header.refcount_table_clusters, |
| 1520 | s->cluster_size, QCOW_MAX_REFTABLE_SIZE, |
| 1521 | "Reference count table", errp); |
Kevin Wolf | 8c7de28 | 2014-03-26 13:05:44 +0100 | [diff] [blame] | 1522 | if (ret < 0) { |
Kevin Wolf | 8c7de28 | 2014-03-26 13:05:44 +0100 | [diff] [blame] | 1523 | goto fail; |
| 1524 | } |
| 1525 | |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 1526 | if (!(flags & BDRV_O_CHECK)) { |
| 1527 | /* |
| 1528 | * The total size in bytes of the snapshot table is checked in |
| 1529 | * qcow2_read_snapshots() because the size of each snapshot is |
| 1530 | * variable and we don't know it yet. |
| 1531 | * Here we only check the offset and number of snapshots. |
| 1532 | */ |
| 1533 | ret = qcow2_validate_table(bs, header.snapshots_offset, |
| 1534 | header.nb_snapshots, |
| 1535 | sizeof(QCowSnapshotHeader), |
| 1536 | sizeof(QCowSnapshotHeader) * |
| 1537 | QCOW_MAX_SNAPSHOTS, |
| 1538 | "Snapshot table", errp); |
| 1539 | if (ret < 0) { |
| 1540 | goto fail; |
| 1541 | } |
Kevin Wolf | ce48f2f4 | 2014-03-26 13:05:45 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1544 | /* read the level 1 table */ |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 1545 | ret = qcow2_validate_table(bs, header.l1_table_offset, |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 1546 | header.l1_size, L1E_SIZE, |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 1547 | QCOW_MAX_L1_SIZE, "Active L1 table", errp); |
| 1548 | if (ret < 0) { |
Kevin Wolf | 2d51c32 | 2014-03-26 13:05:46 +0100 | [diff] [blame] | 1549 | goto fail; |
| 1550 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1551 | s->l1_size = header.l1_size; |
Alberto Garcia | 0cf0e59 | 2018-03-06 18:14:06 +0200 | [diff] [blame] | 1552 | s->l1_table_offset = header.l1_table_offset; |
Kevin Wolf | 2cf7cfa | 2013-05-14 16:14:33 +0200 | [diff] [blame] | 1553 | |
| 1554 | l1_vm_state_index = size_to_l1(s, header.size); |
| 1555 | if (l1_vm_state_index > INT_MAX) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1556 | error_setg(errp, "Image is too big"); |
Kevin Wolf | 2cf7cfa | 2013-05-14 16:14:33 +0200 | [diff] [blame] | 1557 | ret = -EFBIG; |
| 1558 | goto fail; |
| 1559 | } |
| 1560 | s->l1_vm_state_index = l1_vm_state_index; |
| 1561 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1562 | /* the L1 table must contain at least enough entries to put |
| 1563 | header.size bytes */ |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1564 | if (s->l1_size < s->l1_vm_state_index) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1565 | error_setg(errp, "L1 table is too small"); |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1566 | ret = -EINVAL; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1567 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1568 | } |
Kevin Wolf | 2d51c32 | 2014-03-26 13:05:46 +0100 | [diff] [blame] | 1569 | |
Stefan Weil | d191d12 | 2009-10-26 16:11:16 +0100 | [diff] [blame] | 1570 | if (s->l1_size > 0) { |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 1571 | s->l1_table = qemu_try_blockalign(bs->file->bs, s->l1_size * L1E_SIZE); |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1572 | if (s->l1_table == NULL) { |
| 1573 | error_setg(errp, "Could not allocate L1 table"); |
| 1574 | ret = -ENOMEM; |
| 1575 | goto fail; |
| 1576 | } |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1577 | ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 1578 | s->l1_size * L1E_SIZE); |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1579 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1580 | error_setg_errno(errp, -ret, "Could not read L1 table"); |
Stefan Weil | d191d12 | 2009-10-26 16:11:16 +0100 | [diff] [blame] | 1581 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1582 | } |
Stefan Weil | d191d12 | 2009-10-26 16:11:16 +0100 | [diff] [blame] | 1583 | for(i = 0;i < s->l1_size; i++) { |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 1584 | s->l1_table[i] = be64_to_cpu(s->l1_table[i]); |
Stefan Weil | d191d12 | 2009-10-26 16:11:16 +0100 | [diff] [blame] | 1585 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1586 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1587 | |
Kevin Wolf | 94edf3f | 2015-04-16 11:44:26 +0200 | [diff] [blame] | 1588 | /* Parse driver-specific options */ |
| 1589 | ret = qcow2_update_options(bs, options, flags, errp); |
Kevin Wolf | 90efa0e | 2015-04-16 11:36:10 +0200 | [diff] [blame] | 1590 | if (ret < 0) { |
| 1591 | goto fail; |
| 1592 | } |
| 1593 | |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 1594 | s->flags = flags; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1595 | |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1596 | ret = qcow2_refcount_init(bs); |
| 1597 | if (ret != 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1598 | error_setg_errno(errp, -ret, "Could not initialize refcount handling"); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1599 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1600 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1601 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1602 | QLIST_INIT(&s->cluster_allocs); |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 1603 | QTAILQ_INIT(&s->discards); |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 1604 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 1605 | /* read qcow2 extensions */ |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1606 | if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL, |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 1607 | flags, &update_header, errp)) { |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1608 | ret = -EINVAL; |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 1609 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1610 | } |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 1611 | |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1612 | /* Open external data file */ |
Max Reitz | 8b1869d | 2020-05-13 13:05:35 +0200 | [diff] [blame] | 1613 | s->data_file = bdrv_open_child(NULL, options, "data-file", bs, |
| 1614 | &child_of_bds, BDRV_CHILD_DATA, |
| 1615 | true, &local_err); |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1616 | if (local_err) { |
| 1617 | error_propagate(errp, local_err); |
| 1618 | ret = -EINVAL; |
| 1619 | goto fail; |
| 1620 | } |
| 1621 | |
| 1622 | if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) { |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 1623 | if (!s->data_file && s->image_data_file) { |
| 1624 | s->data_file = bdrv_open_child(s->image_data_file, options, |
Max Reitz | 8b1869d | 2020-05-13 13:05:35 +0200 | [diff] [blame] | 1625 | "data-file", bs, &child_of_bds, |
| 1626 | BDRV_CHILD_DATA, false, errp); |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 1627 | if (!s->data_file) { |
| 1628 | ret = -EINVAL; |
| 1629 | goto fail; |
| 1630 | } |
| 1631 | } |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1632 | if (!s->data_file) { |
| 1633 | error_setg(errp, "'data-file' is required for this image"); |
| 1634 | ret = -EINVAL; |
| 1635 | goto fail; |
| 1636 | } |
Max Reitz | 8b1869d | 2020-05-13 13:05:35 +0200 | [diff] [blame] | 1637 | |
| 1638 | /* No data here */ |
| 1639 | bs->file->role &= ~BDRV_CHILD_DATA; |
| 1640 | |
| 1641 | /* Must succeed because we have given up permissions if anything */ |
| 1642 | bdrv_child_refresh_perms(bs, bs->file, &error_abort); |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1643 | } else { |
| 1644 | if (s->data_file) { |
| 1645 | error_setg(errp, "'data-file' can only be set for images with an " |
| 1646 | "external data file"); |
| 1647 | ret = -EINVAL; |
| 1648 | goto fail; |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | s->data_file = bs->file; |
| 1652 | |
| 1653 | if (data_file_is_raw(bs)) { |
| 1654 | error_setg(errp, "data-file-raw requires a data file"); |
| 1655 | ret = -EINVAL; |
| 1656 | goto fail; |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1657 | } |
| 1658 | } |
Kevin Wolf | 93c2493 | 2019-01-14 16:48:25 +0100 | [diff] [blame] | 1659 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 1660 | /* qcow2_read_extension may have set up the crypto context |
| 1661 | * if the crypt method needs a header region, some methods |
| 1662 | * don't need header extensions, so must check here |
| 1663 | */ |
| 1664 | if (s->crypt_method_header && !s->crypto) { |
| 1665 | if (s->crypt_method_header == QCOW_CRYPT_AES) { |
| 1666 | unsigned int cflags = 0; |
| 1667 | if (flags & BDRV_O_NO_IO) { |
| 1668 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; |
| 1669 | } |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 1670 | s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.", |
Vladimir Sementsov-Ogievskiy | 8ac0f15 | 2019-05-06 17:27:41 +0300 | [diff] [blame] | 1671 | NULL, NULL, cflags, |
| 1672 | QCOW2_MAX_THREADS, errp); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 1673 | if (!s->crypto) { |
| 1674 | ret = -EINVAL; |
| 1675 | goto fail; |
| 1676 | } |
| 1677 | } else if (!(flags & BDRV_O_NO_IO)) { |
| 1678 | error_setg(errp, "Missing CRYPTO header for crypt method %d", |
| 1679 | s->crypt_method_header); |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1680 | ret = -EINVAL; |
| 1681 | goto fail; |
| 1682 | } |
| 1683 | } |
| 1684 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1685 | /* read the backing file name */ |
| 1686 | if (header.backing_file_offset != 0) { |
| 1687 | len = header.backing_file_size; |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 1688 | if (len > MIN(1023, s->cluster_size - header.backing_file_offset) || |
Jeff Cody | e729fa6 | 2015-01-27 08:33:55 -0500 | [diff] [blame] | 1689 | len >= sizeof(bs->backing_file)) { |
Kevin Wolf | 6d33e8e | 2014-03-26 13:05:47 +0100 | [diff] [blame] | 1690 | error_setg(errp, "Backing file name too long"); |
| 1691 | ret = -EINVAL; |
| 1692 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1693 | } |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1694 | ret = bdrv_pread(bs->file, header.backing_file_offset, |
Max Reitz | 998c201 | 2019-02-01 20:29:08 +0100 | [diff] [blame] | 1695 | bs->auto_backing_file, len); |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1696 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1697 | error_setg_errno(errp, -ret, "Could not read backing file name"); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1698 | goto fail; |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1699 | } |
Max Reitz | 998c201 | 2019-02-01 20:29:08 +0100 | [diff] [blame] | 1700 | bs->auto_backing_file[len] = '\0'; |
| 1701 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
| 1702 | bs->auto_backing_file); |
| 1703 | s->image_backing_file = g_strdup(bs->auto_backing_file); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1704 | } |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 1705 | |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 1706 | /* |
| 1707 | * Internal snapshots; skip reading them in check mode, because |
| 1708 | * we do not need them then, and we do not want to abort because |
| 1709 | * of a broken table. |
| 1710 | */ |
| 1711 | if (!(flags & BDRV_O_CHECK)) { |
| 1712 | s->snapshots_offset = header.snapshots_offset; |
| 1713 | s->nb_snapshots = header.nb_snapshots; |
Kevin Wolf | 11b128f | 2014-03-26 13:06:04 +0100 | [diff] [blame] | 1714 | |
Max Reitz | 8bc584f | 2019-10-11 17:28:06 +0200 | [diff] [blame] | 1715 | ret = qcow2_read_snapshots(bs, errp); |
| 1716 | if (ret < 0) { |
| 1717 | goto fail; |
| 1718 | } |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1719 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1720 | |
Stefan Hajnoczi | af7b708 | 2012-06-14 11:42:23 +0100 | [diff] [blame] | 1721 | /* Clear unknown autoclear feature bits */ |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 1722 | update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK; |
Vladimir Sementsov-Ogievskiy | d1258dd | 2017-06-28 15:05:11 +0300 | [diff] [blame] | 1723 | update_header = |
| 1724 | update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE); |
| 1725 | if (update_header) { |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 1726 | s->autoclear_features &= QCOW2_AUTOCLEAR_MASK; |
Vladimir Sementsov-Ogievskiy | d1258dd | 2017-06-28 15:05:11 +0300 | [diff] [blame] | 1727 | } |
| 1728 | |
Vladimir Sementsov-Ogievskiy | 9c98f14 | 2018-10-29 16:23:17 -0400 | [diff] [blame] | 1729 | /* == Handle persistent dirty bitmaps == |
| 1730 | * |
| 1731 | * We want load dirty bitmaps in three cases: |
| 1732 | * |
| 1733 | * 1. Normal open of the disk in active mode, not related to invalidation |
| 1734 | * after migration. |
| 1735 | * |
| 1736 | * 2. Invalidation of the target vm after pre-copy phase of migration, if |
| 1737 | * bitmaps are _not_ migrating through migration channel, i.e. |
| 1738 | * 'dirty-bitmaps' capability is disabled. |
| 1739 | * |
| 1740 | * 3. Invalidation of source vm after failed or canceled migration. |
| 1741 | * This is a very interesting case. There are two possible types of |
| 1742 | * bitmaps: |
| 1743 | * |
| 1744 | * A. Stored on inactivation and removed. They should be loaded from the |
| 1745 | * image. |
| 1746 | * |
| 1747 | * B. Not stored: not-persistent bitmaps and bitmaps, migrated through |
| 1748 | * the migration channel (with dirty-bitmaps capability). |
| 1749 | * |
| 1750 | * On the other hand, there are two possible sub-cases: |
| 1751 | * |
| 1752 | * 3.1 disk was changed by somebody else while were inactive. In this |
| 1753 | * case all in-RAM dirty bitmaps (both persistent and not) are |
| 1754 | * definitely invalid. And we don't have any method to determine |
| 1755 | * this. |
| 1756 | * |
| 1757 | * Simple and safe thing is to just drop all the bitmaps of type B on |
| 1758 | * inactivation. But in this case we lose bitmaps in valid 4.2 case. |
| 1759 | * |
| 1760 | * On the other hand, resuming source vm, if disk was already changed |
| 1761 | * is a bad thing anyway: not only bitmaps, the whole vm state is |
| 1762 | * out of sync with disk. |
| 1763 | * |
| 1764 | * This means, that user or management tool, who for some reason |
| 1765 | * decided to resume source vm, after disk was already changed by |
| 1766 | * target vm, should at least drop all dirty bitmaps by hand. |
| 1767 | * |
| 1768 | * So, we can ignore this case for now, but TODO: "generation" |
| 1769 | * extension for qcow2, to determine, that image was changed after |
| 1770 | * last inactivation. And if it is changed, we will drop (or at least |
| 1771 | * mark as 'invalid' all the bitmaps of type B, both persistent |
| 1772 | * and not). |
| 1773 | * |
| 1774 | * 3.2 disk was _not_ changed while were inactive. Bitmaps may be saved |
| 1775 | * to disk ('dirty-bitmaps' capability disabled), or not saved |
| 1776 | * ('dirty-bitmaps' capability enabled), but we don't need to care |
| 1777 | * of: let's load bitmaps as always: stored bitmaps will be loaded, |
| 1778 | * and not stored has flag IN_USE=1 in the image and will be skipped |
| 1779 | * on loading. |
| 1780 | * |
| 1781 | * One remaining possible case when we don't want load bitmaps: |
| 1782 | * |
| 1783 | * 4. Open disk in inactive mode in target vm (bitmaps are migrating or |
| 1784 | * will be loaded on invalidation, no needs try loading them before) |
| 1785 | */ |
| 1786 | |
| 1787 | if (!(bdrv_get_flags(bs) & BDRV_O_INACTIVE)) { |
| 1788 | /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */ |
| 1789 | bool header_updated = qcow2_load_dirty_bitmaps(bs, &local_err); |
Tuguoyi | 66be5c3 | 2019-12-18 11:53:35 +0000 | [diff] [blame] | 1790 | if (local_err != NULL) { |
| 1791 | error_propagate(errp, local_err); |
| 1792 | ret = -EINVAL; |
| 1793 | goto fail; |
| 1794 | } |
Vladimir Sementsov-Ogievskiy | 9c98f14 | 2018-10-29 16:23:17 -0400 | [diff] [blame] | 1795 | |
| 1796 | update_header = update_header && !header_updated; |
Vladimir Sementsov-Ogievskiy | d1258dd | 2017-06-28 15:05:11 +0300 | [diff] [blame] | 1797 | } |
Vladimir Sementsov-Ogievskiy | d1258dd | 2017-06-28 15:05:11 +0300 | [diff] [blame] | 1798 | |
| 1799 | if (update_header) { |
Stefan Hajnoczi | af7b708 | 2012-06-14 11:42:23 +0100 | [diff] [blame] | 1800 | ret = qcow2_update_header(bs); |
| 1801 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1802 | error_setg_errno(errp, -ret, "Could not update qcow2 header"); |
Stefan Hajnoczi | af7b708 | 2012-06-14 11:42:23 +0100 | [diff] [blame] | 1803 | goto fail; |
| 1804 | } |
| 1805 | } |
| 1806 | |
Kevin Wolf | 3b65081 | 2019-11-22 16:57:48 +0100 | [diff] [blame] | 1807 | bs->supported_zero_flags = header.version >= 3 ? |
| 1808 | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK : 0; |
Kevin Wolf | f01643f | 2020-04-24 14:54:42 +0200 | [diff] [blame] | 1809 | bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; |
Kevin Wolf | 68d100e | 2011-06-30 17:42:09 +0200 | [diff] [blame] | 1810 | |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 1811 | /* Repair image if dirty */ |
Kevin Wolf | 04c01a5 | 2016-01-13 15:56:06 +0100 | [diff] [blame] | 1812 | if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only && |
Stefan Hajnoczi | 058f8f1 | 2012-08-09 13:05:56 +0100 | [diff] [blame] | 1813 | (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 1814 | BdrvCheckResult result = {0}; |
| 1815 | |
Paolo Bonzini | 2fd6163 | 2018-03-01 17:36:19 +0100 | [diff] [blame] | 1816 | ret = qcow2_co_check_locked(bs, &result, |
| 1817 | BDRV_FIX_ERRORS | BDRV_FIX_LEAKS); |
Max Reitz | 791fff5 | 2017-11-10 21:31:07 +0100 | [diff] [blame] | 1818 | if (ret < 0 || result.check_errors) { |
| 1819 | if (ret >= 0) { |
| 1820 | ret = -EIO; |
| 1821 | } |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 1822 | error_setg_errno(errp, -ret, "Could not repair dirty image"); |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 1823 | goto fail; |
| 1824 | } |
| 1825 | } |
| 1826 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1827 | #ifdef DEBUG_ALLOC |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 1828 | { |
| 1829 | BdrvCheckResult result = {0}; |
Stefan Hajnoczi | b35278f | 2012-06-15 16:41:07 +0100 | [diff] [blame] | 1830 | qcow2_check_refcounts(bs, &result, 0); |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 1831 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1832 | #endif |
Vladimir Sementsov-Ogievskiy | ceb029c | 2018-06-20 17:48:37 +0300 | [diff] [blame] | 1833 | |
Vladimir Sementsov-Ogievskiy | 6f13a31 | 2019-05-06 17:27:38 +0300 | [diff] [blame] | 1834 | qemu_co_queue_init(&s->thread_task_queue); |
Vladimir Sementsov-Ogievskiy | ceb029c | 2018-06-20 17:48:37 +0300 | [diff] [blame] | 1835 | |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1836 | return ret; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1837 | |
| 1838 | fail: |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 1839 | g_free(s->image_data_file); |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1840 | if (has_data_file(bs)) { |
| 1841 | bdrv_unref_child(bs, s->data_file); |
Vladimir Sementsov-Ogievskiy | 808cf3c | 2020-03-16 09:06:31 +0300 | [diff] [blame] | 1842 | s->data_file = NULL; |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 1843 | } |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 1844 | g_free(s->unknown_header_fields); |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 1845 | cleanup_unknown_header_ext(bs); |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 1846 | qcow2_free_snapshots(bs); |
| 1847 | qcow2_refcount_close(bs); |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1848 | qemu_vfree(s->l1_table); |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 1849 | /* else pre-write overlap checks in cache_destroy may crash */ |
| 1850 | s->l1_table = NULL; |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 1851 | cache_clean_timer_del(bs); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1852 | if (s->l2_table_cache) { |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 1853 | qcow2_cache_destroy(s->l2_table_cache); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1854 | } |
Prasad Joshi | c5a33ee | 2014-03-28 23:08:58 +0530 | [diff] [blame] | 1855 | if (s->refcount_block_cache) { |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 1856 | qcow2_cache_destroy(s->refcount_block_cache); |
Prasad Joshi | c5a33ee | 2014-03-28 23:08:58 +0530 | [diff] [blame] | 1857 | } |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 1858 | qcrypto_block_free(s->crypto); |
| 1859 | qapi_free_QCryptoBlockOpenOptions(s->crypto_opts); |
Jes Sorensen | 6d85a57 | 2010-12-17 16:02:40 +0100 | [diff] [blame] | 1860 | return ret; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Paolo Bonzini | 1fafcd9 | 2018-03-01 17:36:16 +0100 | [diff] [blame] | 1863 | typedef struct QCow2OpenCo { |
| 1864 | BlockDriverState *bs; |
| 1865 | QDict *options; |
| 1866 | int flags; |
| 1867 | Error **errp; |
| 1868 | int ret; |
| 1869 | } QCow2OpenCo; |
| 1870 | |
| 1871 | static void coroutine_fn qcow2_open_entry(void *opaque) |
| 1872 | { |
| 1873 | QCow2OpenCo *qoc = opaque; |
| 1874 | BDRVQcow2State *s = qoc->bs->opaque; |
| 1875 | |
| 1876 | qemu_co_mutex_lock(&s->lock); |
| 1877 | qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp); |
| 1878 | qemu_co_mutex_unlock(&s->lock); |
| 1879 | } |
| 1880 | |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 1881 | static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, |
| 1882 | Error **errp) |
| 1883 | { |
Paolo Bonzini | 1fafcd9 | 2018-03-01 17:36:16 +0100 | [diff] [blame] | 1884 | BDRVQcow2State *s = bs->opaque; |
| 1885 | QCow2OpenCo qoc = { |
| 1886 | .bs = bs, |
| 1887 | .options = options, |
| 1888 | .flags = flags, |
| 1889 | .errp = errp, |
| 1890 | .ret = -EINPROGRESS |
| 1891 | }; |
| 1892 | |
Max Reitz | 8b1869d | 2020-05-13 13:05:35 +0200 | [diff] [blame] | 1893 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, |
| 1894 | BDRV_CHILD_IMAGE, false, errp); |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 1895 | if (!bs->file) { |
| 1896 | return -EINVAL; |
| 1897 | } |
| 1898 | |
Paolo Bonzini | 1fafcd9 | 2018-03-01 17:36:16 +0100 | [diff] [blame] | 1899 | /* Initialise locks */ |
| 1900 | qemu_co_mutex_init(&s->lock); |
| 1901 | |
| 1902 | if (qemu_in_coroutine()) { |
| 1903 | /* From bdrv_co_create. */ |
| 1904 | qcow2_open_entry(&qoc); |
| 1905 | } else { |
Kevin Wolf | 4720cbe | 2019-01-07 13:02:48 +0100 | [diff] [blame] | 1906 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
Paolo Bonzini | 1fafcd9 | 2018-03-01 17:36:16 +0100 | [diff] [blame] | 1907 | qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc)); |
| 1908 | BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS); |
| 1909 | } |
| 1910 | return qoc.ret; |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Kevin Wolf | 3baca89 | 2014-07-16 17:48:16 +0200 | [diff] [blame] | 1913 | static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1914 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1915 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1916 | |
Eric Blake | a84178c | 2016-06-23 16:37:15 -0600 | [diff] [blame] | 1917 | if (bs->encrypted) { |
| 1918 | /* Encryption works on a sector granularity */ |
Alberto Garcia | 6f8f015 | 2018-10-11 13:58:02 +0300 | [diff] [blame] | 1919 | bs->bl.request_alignment = qcrypto_block_get_sector_size(s->crypto); |
Eric Blake | a84178c | 2016-06-23 16:37:15 -0600 | [diff] [blame] | 1920 | } |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 1921 | bs->bl.pwrite_zeroes_alignment = s->subcluster_size; |
Eric Blake | ecdbead | 2016-11-17 14:13:55 -0600 | [diff] [blame] | 1922 | bs->bl.pdiscard_alignment = s->cluster_size; |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 1923 | } |
| 1924 | |
Jeff Cody | 21d82ac | 2012-09-20 15:13:28 -0400 | [diff] [blame] | 1925 | static int qcow2_reopen_prepare(BDRVReopenState *state, |
| 1926 | BlockReopenQueue *queue, Error **errp) |
| 1927 | { |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1928 | Qcow2ReopenState *r; |
Kevin Wolf | 4c2e5f8 | 2014-04-03 13:47:50 +0200 | [diff] [blame] | 1929 | int ret; |
| 1930 | |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1931 | r = g_new0(Qcow2ReopenState, 1); |
| 1932 | state->opaque = r; |
| 1933 | |
| 1934 | ret = qcow2_update_options_prepare(state->bs, r, state->options, |
| 1935 | state->flags, errp); |
| 1936 | if (ret < 0) { |
| 1937 | goto fail; |
| 1938 | } |
| 1939 | |
| 1940 | /* We need to write out any unwritten data if we reopen read-only. */ |
Kevin Wolf | 4c2e5f8 | 2014-04-03 13:47:50 +0200 | [diff] [blame] | 1941 | if ((state->flags & BDRV_O_RDWR) == 0) { |
Vladimir Sementsov-Ogievskiy | 169b879 | 2017-06-28 15:05:20 +0300 | [diff] [blame] | 1942 | ret = qcow2_reopen_bitmaps_ro(state->bs, errp); |
| 1943 | if (ret < 0) { |
| 1944 | goto fail; |
| 1945 | } |
| 1946 | |
Kevin Wolf | 4c2e5f8 | 2014-04-03 13:47:50 +0200 | [diff] [blame] | 1947 | ret = bdrv_flush(state->bs); |
| 1948 | if (ret < 0) { |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1949 | goto fail; |
Kevin Wolf | 4c2e5f8 | 2014-04-03 13:47:50 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | ret = qcow2_mark_clean(state->bs); |
| 1953 | if (ret < 0) { |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1954 | goto fail; |
Kevin Wolf | 4c2e5f8 | 2014-04-03 13:47:50 +0200 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | |
Jeff Cody | 21d82ac | 2012-09-20 15:13:28 -0400 | [diff] [blame] | 1958 | return 0; |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1959 | |
| 1960 | fail: |
| 1961 | qcow2_update_options_abort(state->bs, r); |
| 1962 | g_free(r); |
| 1963 | return ret; |
| 1964 | } |
| 1965 | |
| 1966 | static void qcow2_reopen_commit(BDRVReopenState *state) |
| 1967 | { |
| 1968 | qcow2_update_options_commit(state->bs, state->opaque); |
Peter Krempa | 65eb7c8 | 2020-02-28 13:44:47 +0100 | [diff] [blame] | 1969 | g_free(state->opaque); |
| 1970 | } |
| 1971 | |
| 1972 | static void qcow2_reopen_commit_post(BDRVReopenState *state) |
| 1973 | { |
Vladimir Sementsov-Ogievskiy | 4dd09f6 | 2019-09-27 15:23:55 +0300 | [diff] [blame] | 1974 | if (state->flags & BDRV_O_RDWR) { |
| 1975 | Error *local_err = NULL; |
| 1976 | |
| 1977 | if (qcow2_reopen_bitmaps_rw(state->bs, &local_err) < 0) { |
| 1978 | /* |
| 1979 | * This is not fatal, bitmaps just left read-only, so all following |
| 1980 | * writes will fail. User can remove read-only bitmaps to unblock |
| 1981 | * writes or retry reopen. |
| 1982 | */ |
| 1983 | error_reportf_err(local_err, |
| 1984 | "%s: Failed to make dirty bitmaps writable: ", |
| 1985 | bdrv_get_node_name(state->bs)); |
| 1986 | } |
| 1987 | } |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | static void qcow2_reopen_abort(BDRVReopenState *state) |
| 1991 | { |
| 1992 | qcow2_update_options_abort(state->bs, state->opaque); |
| 1993 | g_free(state->opaque); |
Jeff Cody | 21d82ac | 2012-09-20 15:13:28 -0400 | [diff] [blame] | 1994 | } |
| 1995 | |
Kevin Wolf | 5365f44 | 2015-11-16 15:34:59 +0100 | [diff] [blame] | 1996 | static void qcow2_join_options(QDict *options, QDict *old_options) |
| 1997 | { |
| 1998 | bool has_new_overlap_template = |
| 1999 | qdict_haskey(options, QCOW2_OPT_OVERLAP) || |
| 2000 | qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE); |
| 2001 | bool has_new_total_cache_size = |
| 2002 | qdict_haskey(options, QCOW2_OPT_CACHE_SIZE); |
| 2003 | bool has_all_cache_options; |
| 2004 | |
| 2005 | /* New overlap template overrides all old overlap options */ |
| 2006 | if (has_new_overlap_template) { |
| 2007 | qdict_del(old_options, QCOW2_OPT_OVERLAP); |
| 2008 | qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE); |
| 2009 | qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER); |
| 2010 | qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1); |
| 2011 | qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2); |
| 2012 | qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE); |
| 2013 | qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK); |
| 2014 | qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE); |
| 2015 | qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1); |
| 2016 | qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2); |
| 2017 | } |
| 2018 | |
| 2019 | /* New total cache size overrides all old options */ |
| 2020 | if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) { |
| 2021 | qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE); |
| 2022 | qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE); |
| 2023 | } |
| 2024 | |
| 2025 | qdict_join(options, old_options, false); |
| 2026 | |
| 2027 | /* |
| 2028 | * If after merging all cache size options are set, an old total size is |
| 2029 | * overwritten. Do keep all options, however, if all three are new. The |
| 2030 | * resulting error message is what we want to happen. |
| 2031 | */ |
| 2032 | has_all_cache_options = |
| 2033 | qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) || |
| 2034 | qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) || |
| 2035 | qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE); |
| 2036 | |
| 2037 | if (has_all_cache_options && !has_new_total_cache_size) { |
| 2038 | qdict_del(options, QCOW2_OPT_CACHE_SIZE); |
| 2039 | } |
| 2040 | } |
| 2041 | |
Eric Blake | a320fb0 | 2018-02-13 14:26:52 -0600 | [diff] [blame] | 2042 | static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs, |
| 2043 | bool want_zero, |
| 2044 | int64_t offset, int64_t count, |
| 2045 | int64_t *pnum, int64_t *map, |
| 2046 | BlockDriverState **file) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2047 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2048 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | 388e581 | 2020-07-10 18:12:44 +0200 | [diff] [blame] | 2049 | uint64_t host_offset; |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 2050 | unsigned int bytes; |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2051 | QCow2SubclusterType type; |
Alberto Garcia | 74e60fb | 2019-12-12 12:01:21 +0200 | [diff] [blame] | 2052 | int ret, status = 0; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2053 | |
Kevin Wolf | 5e97855 | 2019-10-24 16:26:58 +0200 | [diff] [blame] | 2054 | qemu_co_mutex_lock(&s->lock); |
| 2055 | |
Vladimir Sementsov-Ogievskiy | 69f4750 | 2019-04-08 19:26:17 +0300 | [diff] [blame] | 2056 | if (!s->metadata_preallocation_checked) { |
| 2057 | ret = qcow2_detect_metadata_preallocation(bs); |
| 2058 | s->metadata_preallocation = (ret == 1); |
| 2059 | s->metadata_preallocation_checked = true; |
| 2060 | } |
| 2061 | |
Eric Blake | a320fb0 | 2018-02-13 14:26:52 -0600 | [diff] [blame] | 2062 | bytes = MIN(INT_MAX, count); |
Alberto Garcia | ca4a0bb | 2020-07-10 18:12:59 +0200 | [diff] [blame] | 2063 | ret = qcow2_get_host_offset(bs, offset, &bytes, &host_offset, &type); |
Stefan Hajnoczi | f8a2e5e | 2011-11-14 12:44:21 +0000 | [diff] [blame] | 2064 | qemu_co_mutex_unlock(&s->lock); |
Kevin Wolf | 1c46efa | 2010-05-21 17:59:36 +0200 | [diff] [blame] | 2065 | if (ret < 0) { |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 2066 | return ret; |
Kevin Wolf | 1c46efa | 2010-05-21 17:59:36 +0200 | [diff] [blame] | 2067 | } |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 2068 | |
Eric Blake | a320fb0 | 2018-02-13 14:26:52 -0600 | [diff] [blame] | 2069 | *pnum = bytes; |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 2070 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2071 | if ((type == QCOW2_SUBCLUSTER_NORMAL || |
Alberto Garcia | 97490a1 | 2020-07-10 18:13:01 +0200 | [diff] [blame] | 2072 | type == QCOW2_SUBCLUSTER_ZERO_ALLOC || |
| 2073 | type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC) && !s->crypto) { |
Alberto Garcia | 388e581 | 2020-07-10 18:12:44 +0200 | [diff] [blame] | 2074 | *map = host_offset; |
Kevin Wolf | 37be140 | 2019-02-27 13:22:56 +0100 | [diff] [blame] | 2075 | *file = s->data_file->bs; |
Eric Blake | a320fb0 | 2018-02-13 14:26:52 -0600 | [diff] [blame] | 2076 | status |= BDRV_BLOCK_OFFSET_VALID; |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 2077 | } |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2078 | if (type == QCOW2_SUBCLUSTER_ZERO_PLAIN || |
| 2079 | type == QCOW2_SUBCLUSTER_ZERO_ALLOC) { |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 2080 | status |= BDRV_BLOCK_ZERO; |
Alberto Garcia | 97490a1 | 2020-07-10 18:13:01 +0200 | [diff] [blame] | 2081 | } else if (type != QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN && |
| 2082 | type != QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC) { |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 2083 | status |= BDRV_BLOCK_DATA; |
| 2084 | } |
Vladimir Sementsov-Ogievskiy | 69f4750 | 2019-04-08 19:26:17 +0300 | [diff] [blame] | 2085 | if (s->metadata_preallocation && (status & BDRV_BLOCK_DATA) && |
| 2086 | (status & BDRV_BLOCK_OFFSET_VALID)) |
| 2087 | { |
| 2088 | status |= BDRV_BLOCK_RECURSE; |
| 2089 | } |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 2090 | return status; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 2093 | static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs, |
| 2094 | QCowL2Meta **pl2meta, |
| 2095 | bool link_l2) |
| 2096 | { |
| 2097 | int ret = 0; |
| 2098 | QCowL2Meta *l2meta = *pl2meta; |
| 2099 | |
| 2100 | while (l2meta != NULL) { |
| 2101 | QCowL2Meta *next; |
| 2102 | |
Fam Zheng | 354d930 | 2018-06-27 11:57:51 +0800 | [diff] [blame] | 2103 | if (link_l2) { |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 2104 | ret = qcow2_alloc_cluster_link_l2(bs, l2meta); |
| 2105 | if (ret) { |
| 2106 | goto out; |
| 2107 | } |
Kevin Wolf | 8b24cd1 | 2018-06-28 17:05:45 +0200 | [diff] [blame] | 2108 | } else { |
| 2109 | qcow2_alloc_cluster_abort(bs, l2meta); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /* Take the request off the list of running requests */ |
Alberto Garcia | f7bd5bb | 2020-09-03 18:37:48 +0200 | [diff] [blame] | 2113 | QLIST_REMOVE(l2meta, next_in_flight); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 2114 | |
| 2115 | qemu_co_queue_restart_all(&l2meta->dependent_requests); |
| 2116 | |
| 2117 | next = l2meta->next; |
| 2118 | g_free(l2meta); |
| 2119 | l2meta = next; |
| 2120 | } |
| 2121 | out: |
| 2122 | *pl2meta = l2meta; |
| 2123 | return ret; |
| 2124 | } |
| 2125 | |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2126 | static coroutine_fn int |
| 2127 | qcow2_co_preadv_encrypted(BlockDriverState *bs, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2128 | uint64_t host_offset, |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2129 | uint64_t offset, |
| 2130 | uint64_t bytes, |
| 2131 | QEMUIOVector *qiov, |
| 2132 | uint64_t qiov_offset) |
| 2133 | { |
| 2134 | int ret; |
| 2135 | BDRVQcow2State *s = bs->opaque; |
| 2136 | uint8_t *buf; |
| 2137 | |
| 2138 | assert(bs->encrypted && s->crypto); |
| 2139 | assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
| 2140 | |
| 2141 | /* |
| 2142 | * For encrypted images, read everything into a temporary |
| 2143 | * contiguous buffer on which the AES functions can work. |
| 2144 | * Also, decryption in a separate buffer is better as it |
| 2145 | * prevents the guest from learning information about the |
| 2146 | * encrypted nature of the virtual disk. |
| 2147 | */ |
| 2148 | |
| 2149 | buf = qemu_try_blockalign(s->data_file->bs, bytes); |
| 2150 | if (buf == NULL) { |
| 2151 | return -ENOMEM; |
| 2152 | } |
| 2153 | |
| 2154 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2155 | ret = bdrv_co_pread(s->data_file, host_offset, bytes, buf, 0); |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2156 | if (ret < 0) { |
| 2157 | goto fail; |
| 2158 | } |
| 2159 | |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2160 | if (qcow2_co_decrypt(bs, host_offset, offset, buf, bytes) < 0) |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2161 | { |
| 2162 | ret = -EIO; |
| 2163 | goto fail; |
| 2164 | } |
| 2165 | qemu_iovec_from_buf(qiov, qiov_offset, buf, bytes); |
| 2166 | |
| 2167 | fail: |
| 2168 | qemu_vfree(buf); |
| 2169 | |
| 2170 | return ret; |
| 2171 | } |
| 2172 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2173 | typedef struct Qcow2AioTask { |
| 2174 | AioTask task; |
| 2175 | |
| 2176 | BlockDriverState *bs; |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2177 | QCow2SubclusterType subcluster_type; /* only for read */ |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2178 | uint64_t host_offset; /* or full descriptor in compressed clusters */ |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2179 | uint64_t offset; |
| 2180 | uint64_t bytes; |
| 2181 | QEMUIOVector *qiov; |
| 2182 | uint64_t qiov_offset; |
| 2183 | QCowL2Meta *l2meta; /* only for write */ |
| 2184 | } Qcow2AioTask; |
| 2185 | |
| 2186 | static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task); |
| 2187 | static coroutine_fn int qcow2_add_task(BlockDriverState *bs, |
| 2188 | AioTaskPool *pool, |
| 2189 | AioTaskFunc func, |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2190 | QCow2SubclusterType subcluster_type, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2191 | uint64_t host_offset, |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2192 | uint64_t offset, |
| 2193 | uint64_t bytes, |
| 2194 | QEMUIOVector *qiov, |
| 2195 | size_t qiov_offset, |
| 2196 | QCowL2Meta *l2meta) |
| 2197 | { |
| 2198 | Qcow2AioTask local_task; |
| 2199 | Qcow2AioTask *task = pool ? g_new(Qcow2AioTask, 1) : &local_task; |
| 2200 | |
| 2201 | *task = (Qcow2AioTask) { |
| 2202 | .task.func = func, |
| 2203 | .bs = bs, |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2204 | .subcluster_type = subcluster_type, |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2205 | .qiov = qiov, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2206 | .host_offset = host_offset, |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2207 | .offset = offset, |
| 2208 | .bytes = bytes, |
| 2209 | .qiov_offset = qiov_offset, |
| 2210 | .l2meta = l2meta, |
| 2211 | }; |
| 2212 | |
| 2213 | trace_qcow2_add_task(qemu_coroutine_self(), bs, pool, |
| 2214 | func == qcow2_co_preadv_task_entry ? "read" : "write", |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2215 | subcluster_type, host_offset, offset, bytes, |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2216 | qiov, qiov_offset); |
| 2217 | |
| 2218 | if (!pool) { |
| 2219 | return func(&task->task); |
| 2220 | } |
| 2221 | |
| 2222 | aio_task_pool_start_task(pool, &task->task); |
| 2223 | |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2227 | static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs, |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2228 | QCow2SubclusterType subc_type, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2229 | uint64_t host_offset, |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2230 | uint64_t offset, uint64_t bytes, |
| 2231 | QEMUIOVector *qiov, |
| 2232 | size_t qiov_offset) |
| 2233 | { |
| 2234 | BDRVQcow2State *s = bs->opaque; |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2235 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2236 | switch (subc_type) { |
| 2237 | case QCOW2_SUBCLUSTER_ZERO_PLAIN: |
| 2238 | case QCOW2_SUBCLUSTER_ZERO_ALLOC: |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2239 | /* Both zero types are handled in qcow2_co_preadv_part */ |
| 2240 | g_assert_not_reached(); |
| 2241 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2242 | case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN: |
Alberto Garcia | 97490a1 | 2020-07-10 18:13:01 +0200 | [diff] [blame] | 2243 | case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC: |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2244 | assert(bs->backing); /* otherwise handled in qcow2_co_preadv_part */ |
| 2245 | |
| 2246 | BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); |
| 2247 | return bdrv_co_preadv_part(bs->backing, offset, bytes, |
| 2248 | qiov, qiov_offset, 0); |
| 2249 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2250 | case QCOW2_SUBCLUSTER_COMPRESSED: |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2251 | return qcow2_co_preadv_compressed(bs, host_offset, |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2252 | offset, bytes, qiov, qiov_offset); |
| 2253 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2254 | case QCOW2_SUBCLUSTER_NORMAL: |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2255 | if (bs->encrypted) { |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2256 | return qcow2_co_preadv_encrypted(bs, host_offset, |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2257 | offset, bytes, qiov, qiov_offset); |
| 2258 | } |
| 2259 | |
| 2260 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2261 | return bdrv_co_preadv_part(s->data_file, host_offset, |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2262 | bytes, qiov, qiov_offset, 0); |
| 2263 | |
| 2264 | default: |
| 2265 | g_assert_not_reached(); |
| 2266 | } |
| 2267 | |
| 2268 | g_assert_not_reached(); |
| 2269 | } |
| 2270 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2271 | static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task) |
| 2272 | { |
| 2273 | Qcow2AioTask *t = container_of(task, Qcow2AioTask, task); |
| 2274 | |
| 2275 | assert(!t->l2meta); |
| 2276 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2277 | return qcow2_co_preadv_task(t->bs, t->subcluster_type, |
| 2278 | t->host_offset, t->offset, t->bytes, |
| 2279 | t->qiov, t->qiov_offset); |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2280 | } |
| 2281 | |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 2282 | static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs, |
| 2283 | uint64_t offset, uint64_t bytes, |
| 2284 | QEMUIOVector *qiov, |
| 2285 | size_t qiov_offset, int flags) |
aliguori | 1490791 | 2008-10-31 17:28:00 +0000 | [diff] [blame] | 2286 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2287 | BDRVQcow2State *s = bs->opaque; |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2288 | int ret = 0; |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 2289 | unsigned int cur_bytes; /* number of bytes in current iteration */ |
Alberto Garcia | 388e581 | 2020-07-10 18:12:44 +0200 | [diff] [blame] | 2290 | uint64_t host_offset = 0; |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2291 | QCow2SubclusterType type; |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2292 | AioTaskPool *aio = NULL; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2293 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2294 | while (bytes != 0 && aio_task_pool_status(aio) == 0) { |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2295 | /* prepare next request */ |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 2296 | cur_bytes = MIN(bytes, INT_MAX); |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 2297 | if (s->crypto) { |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 2298 | cur_bytes = MIN(cur_bytes, |
| 2299 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
Kevin Wolf | bd28f83 | 2010-09-13 18:08:52 +0200 | [diff] [blame] | 2300 | } |
| 2301 | |
Vladimir Sementsov-Ogievskiy | f24196d | 2019-05-06 17:27:39 +0300 | [diff] [blame] | 2302 | qemu_co_mutex_lock(&s->lock); |
Alberto Garcia | ca4a0bb | 2020-07-10 18:12:59 +0200 | [diff] [blame] | 2303 | ret = qcow2_get_host_offset(bs, offset, &cur_bytes, |
| 2304 | &host_offset, &type); |
Vladimir Sementsov-Ogievskiy | f24196d | 2019-05-06 17:27:39 +0300 | [diff] [blame] | 2305 | qemu_co_mutex_unlock(&s->lock); |
Kevin Wolf | 68d100e | 2011-06-30 17:42:09 +0200 | [diff] [blame] | 2306 | if (ret < 0) { |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2307 | goto out; |
Kevin Wolf | 171e3d6 | 2010-04-06 15:30:09 +0200 | [diff] [blame] | 2308 | } |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2309 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2310 | if (type == QCOW2_SUBCLUSTER_ZERO_PLAIN || |
| 2311 | type == QCOW2_SUBCLUSTER_ZERO_ALLOC || |
Alberto Garcia | 97490a1 | 2020-07-10 18:13:01 +0200 | [diff] [blame] | 2312 | (type == QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN && !bs->backing) || |
| 2313 | (type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC && !bs->backing)) |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2314 | { |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 2315 | qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes); |
Vladimir Sementsov-Ogievskiy | 88f468e | 2019-09-16 20:53:22 +0300 | [diff] [blame] | 2316 | } else { |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2317 | if (!aio && cur_bytes != bytes) { |
| 2318 | aio = aio_task_pool_new(QCOW2_MAX_WORKERS); |
| 2319 | } |
Alberto Garcia | ca4a0bb | 2020-07-10 18:12:59 +0200 | [diff] [blame] | 2320 | ret = qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, type, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2321 | host_offset, offset, cur_bytes, |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2322 | qiov, qiov_offset, NULL); |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2323 | if (ret < 0) { |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2324 | goto out; |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2325 | } |
Frediano Ziglio | faf575c | 2011-08-23 15:21:14 +0200 | [diff] [blame] | 2326 | } |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2327 | |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 2328 | bytes -= cur_bytes; |
| 2329 | offset += cur_bytes; |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 2330 | qiov_offset += cur_bytes; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2331 | } |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2332 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2333 | out: |
| 2334 | if (aio) { |
| 2335 | aio_task_pool_wait_all(aio); |
| 2336 | if (ret == 0) { |
| 2337 | ret = aio_task_pool_status(aio); |
| 2338 | } |
| 2339 | g_free(aio); |
| 2340 | } |
| 2341 | |
| 2342 | return ret; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Alberto Garcia | ee22a9d | 2017-06-19 16:40:08 +0300 | [diff] [blame] | 2345 | /* Check if it's possible to merge a write request with the writing of |
| 2346 | * the data from the COW regions */ |
| 2347 | static bool merge_cow(uint64_t offset, unsigned bytes, |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 2348 | QEMUIOVector *qiov, size_t qiov_offset, |
| 2349 | QCowL2Meta *l2meta) |
Alberto Garcia | ee22a9d | 2017-06-19 16:40:08 +0300 | [diff] [blame] | 2350 | { |
| 2351 | QCowL2Meta *m; |
| 2352 | |
| 2353 | for (m = l2meta; m != NULL; m = m->next) { |
| 2354 | /* If both COW regions are empty then there's nothing to merge */ |
| 2355 | if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) { |
| 2356 | continue; |
| 2357 | } |
| 2358 | |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2359 | /* If COW regions are handled already, skip this too */ |
| 2360 | if (m->skip_cow) { |
| 2361 | continue; |
| 2362 | } |
| 2363 | |
Alberto Garcia | ee22a9d | 2017-06-19 16:40:08 +0300 | [diff] [blame] | 2364 | /* The data (middle) region must be immediately after the |
| 2365 | * start region */ |
| 2366 | if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) { |
| 2367 | continue; |
| 2368 | } |
| 2369 | |
| 2370 | /* The end region must be immediately after the data (middle) |
| 2371 | * region */ |
| 2372 | if (m->offset + m->cow_end.offset != offset + bytes) { |
| 2373 | continue; |
| 2374 | } |
| 2375 | |
| 2376 | /* Make sure that adding both COW regions to the QEMUIOVector |
| 2377 | * does not exceed IOV_MAX */ |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 2378 | if (qemu_iovec_subvec_niov(qiov, qiov_offset, bytes) > IOV_MAX - 2) { |
Alberto Garcia | ee22a9d | 2017-06-19 16:40:08 +0300 | [diff] [blame] | 2379 | continue; |
| 2380 | } |
| 2381 | |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 2382 | m->data_qiov = qiov; |
| 2383 | m->data_qiov_offset = qiov_offset; |
Alberto Garcia | ee22a9d | 2017-06-19 16:40:08 +0300 | [diff] [blame] | 2384 | return true; |
| 2385 | } |
| 2386 | |
| 2387 | return false; |
| 2388 | } |
| 2389 | |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2390 | static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes) |
| 2391 | { |
| 2392 | int64_t nr; |
| 2393 | return !bytes || |
Andrey Shinkevich | 170d3bd | 2019-05-29 20:56:14 +0300 | [diff] [blame] | 2394 | (!bdrv_is_allocated_above(bs, NULL, false, offset, bytes, &nr) && |
| 2395 | nr == bytes); |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m) |
| 2399 | { |
| 2400 | /* |
| 2401 | * This check is designed for optimization shortcut so it must be |
| 2402 | * efficient. |
| 2403 | * Instead of is_zero(), use is_unallocated() as it is faster (but not |
| 2404 | * as accurate and can result in false negatives). |
| 2405 | */ |
| 2406 | return is_unallocated(bs, m->offset + m->cow_start.offset, |
| 2407 | m->cow_start.nb_bytes) && |
| 2408 | is_unallocated(bs, m->offset + m->cow_end.offset, |
| 2409 | m->cow_end.nb_bytes); |
| 2410 | } |
| 2411 | |
| 2412 | static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta) |
| 2413 | { |
| 2414 | BDRVQcow2State *s = bs->opaque; |
| 2415 | QCowL2Meta *m; |
| 2416 | |
| 2417 | if (!(s->data_file->bs->supported_zero_flags & BDRV_REQ_NO_FALLBACK)) { |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | if (bs->encrypted) { |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
| 2425 | for (m = l2meta; m != NULL; m = m->next) { |
| 2426 | int ret; |
Alberto Garcia | bf4a66e | 2020-07-10 18:13:09 +0200 | [diff] [blame] | 2427 | uint64_t start_offset = m->alloc_offset + m->cow_start.offset; |
| 2428 | unsigned nb_bytes = m->cow_end.offset + m->cow_end.nb_bytes - |
| 2429 | m->cow_start.offset; |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2430 | |
| 2431 | if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) { |
| 2432 | continue; |
| 2433 | } |
| 2434 | |
| 2435 | if (!is_zero_cow(bs, m)) { |
| 2436 | continue; |
| 2437 | } |
| 2438 | |
| 2439 | /* |
| 2440 | * instead of writing zero COW buffers, |
| 2441 | * efficiently zero out the whole clusters |
| 2442 | */ |
| 2443 | |
Alberto Garcia | bf4a66e | 2020-07-10 18:13:09 +0200 | [diff] [blame] | 2444 | ret = qcow2_pre_write_overlap_check(bs, 0, start_offset, nb_bytes, |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2445 | true); |
| 2446 | if (ret < 0) { |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
| 2450 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE); |
Alberto Garcia | bf4a66e | 2020-07-10 18:13:09 +0200 | [diff] [blame] | 2451 | ret = bdrv_co_pwrite_zeroes(s->data_file, start_offset, nb_bytes, |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2452 | BDRV_REQ_NO_FALLBACK); |
| 2453 | if (ret < 0) { |
| 2454 | if (ret != -ENOTSUP && ret != -EAGAIN) { |
| 2455 | return ret; |
| 2456 | } |
| 2457 | continue; |
| 2458 | } |
| 2459 | |
| 2460 | trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters); |
| 2461 | m->skip_cow = true; |
| 2462 | } |
| 2463 | return 0; |
| 2464 | } |
| 2465 | |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2466 | /* |
| 2467 | * qcow2_co_pwritev_task |
| 2468 | * Called with s->lock unlocked |
| 2469 | * l2meta - if not NULL, qcow2_co_pwritev_task() will consume it. Caller must |
| 2470 | * not use it somehow after qcow2_co_pwritev_task() call |
| 2471 | */ |
| 2472 | static coroutine_fn int qcow2_co_pwritev_task(BlockDriverState *bs, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2473 | uint64_t host_offset, |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2474 | uint64_t offset, uint64_t bytes, |
| 2475 | QEMUIOVector *qiov, |
| 2476 | uint64_t qiov_offset, |
| 2477 | QCowL2Meta *l2meta) |
| 2478 | { |
| 2479 | int ret; |
| 2480 | BDRVQcow2State *s = bs->opaque; |
| 2481 | void *crypt_buf = NULL; |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2482 | QEMUIOVector encrypted_qiov; |
| 2483 | |
| 2484 | if (bs->encrypted) { |
| 2485 | assert(s->crypto); |
| 2486 | assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
| 2487 | crypt_buf = qemu_try_blockalign(bs->file->bs, bytes); |
| 2488 | if (crypt_buf == NULL) { |
| 2489 | ret = -ENOMEM; |
| 2490 | goto out_unlocked; |
| 2491 | } |
| 2492 | qemu_iovec_to_buf(qiov, qiov_offset, crypt_buf, bytes); |
| 2493 | |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2494 | if (qcow2_co_encrypt(bs, host_offset, offset, crypt_buf, bytes) < 0) { |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2495 | ret = -EIO; |
| 2496 | goto out_unlocked; |
| 2497 | } |
| 2498 | |
| 2499 | qemu_iovec_init_buf(&encrypted_qiov, crypt_buf, bytes); |
| 2500 | qiov = &encrypted_qiov; |
| 2501 | qiov_offset = 0; |
| 2502 | } |
| 2503 | |
| 2504 | /* Try to efficiently initialize the physical space with zeroes */ |
| 2505 | ret = handle_alloc_space(bs, l2meta); |
| 2506 | if (ret < 0) { |
| 2507 | goto out_unlocked; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * If we need to do COW, check if it's possible to merge the |
| 2512 | * writing of the guest data together with that of the COW regions. |
| 2513 | * If it's not possible (or not necessary) then write the |
| 2514 | * guest data now. |
| 2515 | */ |
| 2516 | if (!merge_cow(offset, bytes, qiov, qiov_offset, l2meta)) { |
| 2517 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2518 | trace_qcow2_writev_data(qemu_coroutine_self(), host_offset); |
| 2519 | ret = bdrv_co_pwritev_part(s->data_file, host_offset, |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2520 | bytes, qiov, qiov_offset, 0); |
| 2521 | if (ret < 0) { |
| 2522 | goto out_unlocked; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | qemu_co_mutex_lock(&s->lock); |
| 2527 | |
| 2528 | ret = qcow2_handle_l2meta(bs, &l2meta, true); |
| 2529 | goto out_locked; |
| 2530 | |
| 2531 | out_unlocked: |
| 2532 | qemu_co_mutex_lock(&s->lock); |
| 2533 | |
| 2534 | out_locked: |
| 2535 | qcow2_handle_l2meta(bs, &l2meta, false); |
| 2536 | qemu_co_mutex_unlock(&s->lock); |
| 2537 | |
| 2538 | qemu_vfree(crypt_buf); |
| 2539 | |
| 2540 | return ret; |
| 2541 | } |
| 2542 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2543 | static coroutine_fn int qcow2_co_pwritev_task_entry(AioTask *task) |
| 2544 | { |
| 2545 | Qcow2AioTask *t = container_of(task, Qcow2AioTask, task); |
| 2546 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 2547 | assert(!t->subcluster_type); |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2548 | |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2549 | return qcow2_co_pwritev_task(t->bs, t->host_offset, |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2550 | t->offset, t->bytes, t->qiov, t->qiov_offset, |
| 2551 | t->l2meta); |
| 2552 | } |
| 2553 | |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 2554 | static coroutine_fn int qcow2_co_pwritev_part( |
| 2555 | BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
| 2556 | QEMUIOVector *qiov, size_t qiov_offset, int flags) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2557 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2558 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 2559 | int offset_in_cluster; |
Kevin Wolf | 68d100e | 2011-06-30 17:42:09 +0200 | [diff] [blame] | 2560 | int ret; |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 2561 | unsigned int cur_bytes; /* number of sectors in current iteration */ |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 2562 | uint64_t host_offset; |
Kevin Wolf | 8d2497c | 2013-01-14 17:31:31 +0100 | [diff] [blame] | 2563 | QCowL2Meta *l2meta = NULL; |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2564 | AioTaskPool *aio = NULL; |
Frediano Ziglio | c227140 | 2011-08-23 15:21:15 +0200 | [diff] [blame] | 2565 | |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 2566 | trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes); |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 2567 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2568 | while (bytes != 0 && aio_task_pool_status(aio) == 0) { |
Frediano Ziglio | 3fc48d0 | 2011-08-23 15:21:19 +0200 | [diff] [blame] | 2569 | |
Kevin Wolf | f50f88b | 2012-12-07 18:08:46 +0100 | [diff] [blame] | 2570 | l2meta = NULL; |
Kevin Wolf | cf5c1a2 | 2012-12-07 18:08:44 +0100 | [diff] [blame] | 2571 | |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 2572 | trace_qcow2_writev_start_part(qemu_coroutine_self()); |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 2573 | offset_in_cluster = offset_into_cluster(s, offset); |
| 2574 | cur_bytes = MIN(bytes, INT_MAX); |
| 2575 | if (bs->encrypted) { |
| 2576 | cur_bytes = MIN(cur_bytes, |
| 2577 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
| 2578 | - offset_in_cluster); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2579 | } |
Kevin Wolf | 6f5f060 | 2010-09-13 18:24:10 +0200 | [diff] [blame] | 2580 | |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2581 | qemu_co_mutex_lock(&s->lock); |
| 2582 | |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 2583 | ret = qcow2_alloc_host_offset(bs, offset, &cur_bytes, |
| 2584 | &host_offset, &l2meta); |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2585 | if (ret < 0) { |
Vladimir Sementsov-Ogievskiy | 5447c3a | 2019-05-06 17:27:40 +0300 | [diff] [blame] | 2586 | goto out_locked; |
Frediano Ziglio | 5ebaa27 | 2011-08-23 15:21:18 +0200 | [diff] [blame] | 2587 | } |
Kevin Wolf | 6f5f060 | 2010-09-13 18:24:10 +0200 | [diff] [blame] | 2588 | |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 2589 | ret = qcow2_pre_write_overlap_check(bs, 0, host_offset, |
Vladimir Sementsov-Ogievskiy | 5447c3a | 2019-05-06 17:27:40 +0300 | [diff] [blame] | 2590 | cur_bytes, true); |
| 2591 | if (ret < 0) { |
| 2592 | goto out_locked; |
| 2593 | } |
| 2594 | |
| 2595 | qemu_co_mutex_unlock(&s->lock); |
| 2596 | |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2597 | if (!aio && cur_bytes != bytes) { |
| 2598 | aio = aio_task_pool_new(QCOW2_MAX_WORKERS); |
| 2599 | } |
| 2600 | ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_task_entry, 0, |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 2601 | host_offset, offset, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 2602 | cur_bytes, qiov, qiov_offset, l2meta); |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2603 | l2meta = NULL; /* l2meta is consumed by qcow2_co_pwritev_task() */ |
Anton Nefedov | c8bb23c | 2019-05-16 17:27:49 +0300 | [diff] [blame] | 2604 | if (ret < 0) { |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2605 | goto fail_nometa; |
Kevin Wolf | f50f88b | 2012-12-07 18:08:46 +0100 | [diff] [blame] | 2606 | } |
Kevin Wolf | 0fa9131 | 2011-09-01 15:02:13 +0200 | [diff] [blame] | 2607 | |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 2608 | bytes -= cur_bytes; |
| 2609 | offset += cur_bytes; |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2610 | qiov_offset += cur_bytes; |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 2611 | trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2612 | } |
Frediano Ziglio | 3fc48d0 | 2011-08-23 15:21:19 +0200 | [diff] [blame] | 2613 | ret = 0; |
Kevin Wolf | 6f5f060 | 2010-09-13 18:24:10 +0200 | [diff] [blame] | 2614 | |
Vladimir Sementsov-Ogievskiy | 5447c3a | 2019-05-06 17:27:40 +0300 | [diff] [blame] | 2615 | qemu_co_mutex_lock(&s->lock); |
| 2616 | |
| 2617 | out_locked: |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 2618 | qcow2_handle_l2meta(bs, &l2meta, false); |
Kevin Wolf | 0fa9131 | 2011-09-01 15:02:13 +0200 | [diff] [blame] | 2619 | |
Paolo Bonzini | a8c5740 | 2017-06-29 15:27:39 +0200 | [diff] [blame] | 2620 | qemu_co_mutex_unlock(&s->lock); |
| 2621 | |
Vladimir Sementsov-Ogievskiy | 6aa7a26 | 2019-09-16 20:53:23 +0300 | [diff] [blame] | 2622 | fail_nometa: |
Vladimir Sementsov-Ogievskiy | d710cf5 | 2019-09-16 20:53:24 +0300 | [diff] [blame] | 2623 | if (aio) { |
| 2624 | aio_task_pool_wait_all(aio); |
| 2625 | if (ret == 0) { |
| 2626 | ret = aio_task_pool_status(aio); |
| 2627 | } |
| 2628 | g_free(aio); |
| 2629 | } |
| 2630 | |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 2631 | trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); |
Kevin Wolf | 42496d6 | 2011-06-07 15:04:32 +0200 | [diff] [blame] | 2632 | |
Kevin Wolf | 68d100e | 2011-06-30 17:42:09 +0200 | [diff] [blame] | 2633 | return ret; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Kevin Wolf | ec6d891 | 2015-12-22 16:04:57 +0100 | [diff] [blame] | 2636 | static int qcow2_inactivate(BlockDriverState *bs) |
| 2637 | { |
| 2638 | BDRVQcow2State *s = bs->opaque; |
| 2639 | int ret, result = 0; |
Vladimir Sementsov-Ogievskiy | 5f72826 | 2017-06-28 15:05:19 +0300 | [diff] [blame] | 2640 | Error *local_err = NULL; |
Kevin Wolf | ec6d891 | 2015-12-22 16:04:57 +0100 | [diff] [blame] | 2641 | |
Vladimir Sementsov-Ogievskiy | 644ddbb | 2019-09-27 15:23:52 +0300 | [diff] [blame] | 2642 | qcow2_store_persistent_dirty_bitmaps(bs, true, &local_err); |
Pavel Butsykin | 83a8c77 | 2017-09-04 13:18:00 +0300 | [diff] [blame] | 2643 | if (local_err != NULL) { |
| 2644 | result = -EINVAL; |
Vladimir Sementsov-Ogievskiy | 132adb6 | 2018-10-29 16:23:15 -0400 | [diff] [blame] | 2645 | error_reportf_err(local_err, "Lost persistent bitmaps during " |
| 2646 | "inactivation of node '%s': ", |
| 2647 | bdrv_get_device_or_node_name(bs)); |
Pavel Butsykin | 83a8c77 | 2017-09-04 13:18:00 +0300 | [diff] [blame] | 2648 | } |
| 2649 | |
Kevin Wolf | ec6d891 | 2015-12-22 16:04:57 +0100 | [diff] [blame] | 2650 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
| 2651 | if (ret) { |
| 2652 | result = ret; |
| 2653 | error_report("Failed to flush the L2 table cache: %s", |
| 2654 | strerror(-ret)); |
| 2655 | } |
| 2656 | |
| 2657 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
| 2658 | if (ret) { |
| 2659 | result = ret; |
| 2660 | error_report("Failed to flush the refcount block cache: %s", |
| 2661 | strerror(-ret)); |
| 2662 | } |
| 2663 | |
| 2664 | if (result == 0) { |
| 2665 | qcow2_mark_clean(bs); |
| 2666 | } |
| 2667 | |
| 2668 | return result; |
| 2669 | } |
| 2670 | |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 2671 | static void qcow2_close(BlockDriverState *bs) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2672 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2673 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 2674 | qemu_vfree(s->l1_table); |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 2675 | /* else pre-write overlap checks in cache_destroy may crash */ |
| 2676 | s->l1_table = NULL; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 2677 | |
Kevin Wolf | 140fd5a | 2015-12-22 16:10:32 +0100 | [diff] [blame] | 2678 | if (!(s->flags & BDRV_O_INACTIVE)) { |
Kevin Wolf | ec6d891 | 2015-12-22 16:04:57 +0100 | [diff] [blame] | 2679 | qcow2_inactivate(bs); |
Kevin Wolf | 27eb6c0 | 2014-03-11 15:15:03 +0100 | [diff] [blame] | 2680 | } |
Stefan Hajnoczi | c61d000 | 2012-07-27 09:05:19 +0100 | [diff] [blame] | 2681 | |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 2682 | cache_clean_timer_del(bs); |
Alberto Garcia | e64d407 | 2018-02-05 16:33:08 +0200 | [diff] [blame] | 2683 | qcow2_cache_destroy(s->l2_table_cache); |
| 2684 | qcow2_cache_destroy(s->refcount_block_cache); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 2685 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 2686 | qcrypto_block_free(s->crypto); |
| 2687 | s->crypto = NULL; |
Pan Nengyuan | 4aebf0f | 2020-02-27 09:29:49 +0800 | [diff] [blame] | 2688 | qapi_free_QCryptoBlockOpenOptions(s->crypto_opts); |
Daniel P. Berrange | f6fa64f | 2015-07-01 18:10:37 +0100 | [diff] [blame] | 2689 | |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2690 | g_free(s->unknown_header_fields); |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 2691 | cleanup_unknown_header_ext(bs); |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2692 | |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 2693 | g_free(s->image_data_file); |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 2694 | g_free(s->image_backing_file); |
| 2695 | g_free(s->image_backing_format); |
| 2696 | |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 2697 | if (has_data_file(bs)) { |
| 2698 | bdrv_unref_child(bs, s->data_file); |
Vladimir Sementsov-Ogievskiy | 808cf3c | 2020-03-16 09:06:31 +0300 | [diff] [blame] | 2699 | s->data_file = NULL; |
Kevin Wolf | 0e8c08b | 2019-01-29 17:13:57 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 2702 | qcow2_refcount_close(bs); |
Li Zhi Hui | 28c1202 | 2011-12-07 17:25:48 +0800 | [diff] [blame] | 2703 | qcow2_free_snapshots(bs); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 2706 | static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs, |
| 2707 | Error **errp) |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 2708 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2709 | BDRVQcow2State *s = bs->opaque; |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 2710 | int flags = s->flags; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 2711 | QCryptoBlock *crypto = NULL; |
Kevin Wolf | acdfb48 | 2013-03-18 13:08:10 +0100 | [diff] [blame] | 2712 | QDict *options; |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 2713 | Error *local_err = NULL; |
| 2714 | int ret; |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 2715 | |
| 2716 | /* |
| 2717 | * Backing files are read-only which makes all of their metadata immutable, |
| 2718 | * that means we don't have to worry about reopening them here. |
| 2719 | */ |
| 2720 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 2721 | crypto = s->crypto; |
| 2722 | s->crypto = NULL; |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 2723 | |
| 2724 | qcow2_close(bs); |
| 2725 | |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2726 | memset(s, 0, sizeof(BDRVQcow2State)); |
Kevin Wolf | d475e5a | 2014-03-11 17:42:41 +0100 | [diff] [blame] | 2727 | options = qdict_clone_shallow(bs->options); |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 2728 | |
Kevin Wolf | 140fd5a | 2015-12-22 16:10:32 +0100 | [diff] [blame] | 2729 | flags &= ~BDRV_O_INACTIVE; |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 2730 | qemu_co_mutex_lock(&s->lock); |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 2731 | ret = qcow2_do_open(bs, options, flags, &local_err); |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 2732 | qemu_co_mutex_unlock(&s->lock); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2733 | qobject_unref(options); |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 2734 | if (local_err) { |
Markus Armbruster | 4b57664 | 2018-10-17 10:26:25 +0200 | [diff] [blame] | 2735 | error_propagate_prepend(errp, local_err, |
| 2736 | "Could not reopen qcow2 layer: "); |
Kevin Wolf | 191fb11 | 2015-12-22 16:14:10 +0100 | [diff] [blame] | 2737 | bs->drv = NULL; |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 2738 | return; |
| 2739 | } else if (ret < 0) { |
| 2740 | error_setg_errno(errp, -ret, "Could not reopen qcow2 layer"); |
Kevin Wolf | 191fb11 | 2015-12-22 16:14:10 +0100 | [diff] [blame] | 2741 | bs->drv = NULL; |
Kevin Wolf | 5a8a30d | 2014-03-12 15:59:16 +0100 | [diff] [blame] | 2742 | return; |
| 2743 | } |
Kevin Wolf | acdfb48 | 2013-03-18 13:08:10 +0100 | [diff] [blame] | 2744 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 2745 | s->crypto = crypto; |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 2746 | } |
| 2747 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2748 | static size_t header_ext_add(char *buf, uint32_t magic, const void *s, |
| 2749 | size_t len, size_t buflen) |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2750 | { |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2751 | QCowExtension *ext_backing_fmt = (QCowExtension*) buf; |
| 2752 | size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2753 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2754 | if (buflen < ext_len) { |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2755 | return -ENOSPC; |
| 2756 | } |
| 2757 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2758 | *ext_backing_fmt = (QCowExtension) { |
| 2759 | .magic = cpu_to_be32(magic), |
| 2760 | .len = cpu_to_be32(len), |
| 2761 | }; |
Stefan Hajnoczi | 0647d47 | 2016-09-13 09:56:27 +0100 | [diff] [blame] | 2762 | |
| 2763 | if (len) { |
| 2764 | memcpy(buf + sizeof(QCowExtension), s, len); |
| 2765 | } |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2766 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2767 | return ext_len; |
| 2768 | } |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2769 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2770 | /* |
| 2771 | * Updates the qcow2 header, including the variable length parts of it, i.e. |
| 2772 | * the backing file name and all extensions. qcow2 was not designed to allow |
| 2773 | * such changes, so if we run out of space (we can only use the first cluster) |
| 2774 | * this function may fail. |
| 2775 | * |
| 2776 | * Returns 0 on success, -errno in error cases. |
| 2777 | */ |
| 2778 | int qcow2_update_header(BlockDriverState *bs) |
| 2779 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2780 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2781 | QCowHeader *header; |
| 2782 | char *buf; |
| 2783 | size_t buflen = s->cluster_size; |
| 2784 | int ret; |
| 2785 | uint64_t total_size; |
| 2786 | uint32_t refcount_table_clusters; |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2787 | size_t header_length; |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 2788 | Qcow2UnknownHeaderExtension *uext; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2789 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2790 | buf = qemu_blockalign(bs, buflen); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2791 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2792 | /* Header structure */ |
| 2793 | header = (QCowHeader*) buf; |
| 2794 | |
| 2795 | if (buflen < sizeof(*header)) { |
| 2796 | ret = -ENOSPC; |
| 2797 | goto fail; |
| 2798 | } |
| 2799 | |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2800 | header_length = sizeof(*header) + s->unknown_header_fields_size; |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2801 | total_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
| 2802 | refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); |
| 2803 | |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 2804 | ret = validate_compression_type(s, NULL); |
| 2805 | if (ret) { |
| 2806 | goto fail; |
| 2807 | } |
| 2808 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2809 | *header = (QCowHeader) { |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2810 | /* Version 2 fields */ |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2811 | .magic = cpu_to_be32(QCOW_MAGIC), |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2812 | .version = cpu_to_be32(s->qcow_version), |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2813 | .backing_file_offset = 0, |
| 2814 | .backing_file_size = 0, |
| 2815 | .cluster_bits = cpu_to_be32(s->cluster_bits), |
| 2816 | .size = cpu_to_be64(total_size), |
| 2817 | .crypt_method = cpu_to_be32(s->crypt_method_header), |
| 2818 | .l1_size = cpu_to_be32(s->l1_size), |
| 2819 | .l1_table_offset = cpu_to_be64(s->l1_table_offset), |
| 2820 | .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), |
| 2821 | .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), |
| 2822 | .nb_snapshots = cpu_to_be32(s->nb_snapshots), |
| 2823 | .snapshots_offset = cpu_to_be64(s->snapshots_offset), |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2824 | |
| 2825 | /* Version 3 fields */ |
| 2826 | .incompatible_features = cpu_to_be64(s->incompatible_features), |
| 2827 | .compatible_features = cpu_to_be64(s->compatible_features), |
| 2828 | .autoclear_features = cpu_to_be64(s->autoclear_features), |
Max Reitz | b6481f3 | 2013-09-03 10:09:53 +0200 | [diff] [blame] | 2829 | .refcount_order = cpu_to_be32(s->refcount_order), |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2830 | .header_length = cpu_to_be32(header_length), |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 2831 | .compression_type = s->compression_type, |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2832 | }; |
| 2833 | |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2834 | /* For older versions, write a shorter header */ |
| 2835 | switch (s->qcow_version) { |
| 2836 | case 2: |
| 2837 | ret = offsetof(QCowHeader, incompatible_features); |
| 2838 | break; |
| 2839 | case 3: |
| 2840 | ret = sizeof(*header); |
| 2841 | break; |
| 2842 | default: |
Jim Meyering | b6c1476 | 2012-05-21 13:06:54 +0200 | [diff] [blame] | 2843 | ret = -EINVAL; |
| 2844 | goto fail; |
Kevin Wolf | 6744cba | 2011-12-15 12:20:58 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | buf += ret; |
| 2848 | buflen -= ret; |
| 2849 | memset(buf, 0, buflen); |
| 2850 | |
| 2851 | /* Preserve any unknown field in the header */ |
| 2852 | if (s->unknown_header_fields_size) { |
| 2853 | if (buflen < s->unknown_header_fields_size) { |
| 2854 | ret = -ENOSPC; |
| 2855 | goto fail; |
| 2856 | } |
| 2857 | |
| 2858 | memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); |
| 2859 | buf += s->unknown_header_fields_size; |
| 2860 | buflen -= s->unknown_header_fields_size; |
| 2861 | } |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2862 | |
| 2863 | /* Backing file format header extension */ |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 2864 | if (s->image_backing_format) { |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2865 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 2866 | s->image_backing_format, |
| 2867 | strlen(s->image_backing_format), |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2868 | buflen); |
| 2869 | if (ret < 0) { |
| 2870 | goto fail; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2873 | buf += ret; |
| 2874 | buflen -= ret; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2875 | } |
| 2876 | |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 2877 | /* External data file header extension */ |
| 2878 | if (has_data_file(bs) && s->image_data_file) { |
| 2879 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DATA_FILE, |
| 2880 | s->image_data_file, strlen(s->image_data_file), |
| 2881 | buflen); |
| 2882 | if (ret < 0) { |
| 2883 | goto fail; |
| 2884 | } |
| 2885 | |
| 2886 | buf += ret; |
| 2887 | buflen -= ret; |
| 2888 | } |
| 2889 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 2890 | /* Full disk encryption header pointer extension */ |
| 2891 | if (s->crypto_header.offset != 0) { |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 2892 | s->crypto_header.offset = cpu_to_be64(s->crypto_header.offset); |
| 2893 | s->crypto_header.length = cpu_to_be64(s->crypto_header.length); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 2894 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER, |
| 2895 | &s->crypto_header, sizeof(s->crypto_header), |
| 2896 | buflen); |
Peter Maydell | 3b698f5 | 2018-10-09 18:24:59 +0100 | [diff] [blame] | 2897 | s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset); |
| 2898 | s->crypto_header.length = be64_to_cpu(s->crypto_header.length); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 2899 | if (ret < 0) { |
| 2900 | goto fail; |
| 2901 | } |
| 2902 | buf += ret; |
| 2903 | buflen -= ret; |
| 2904 | } |
| 2905 | |
Eric Blake | e7be13a | 2020-03-24 12:42:32 -0500 | [diff] [blame] | 2906 | /* |
| 2907 | * Feature table. A mere 8 feature names occupies 392 bytes, and |
| 2908 | * when coupled with the v3 minimum header of 104 bytes plus the |
| 2909 | * 8-byte end-of-extension marker, that would leave only 8 bytes |
| 2910 | * for a backing file name in an image with 512-byte clusters. |
| 2911 | * Thus, we choose to omit this header for cluster sizes 4k and |
| 2912 | * smaller. |
| 2913 | */ |
| 2914 | if (s->qcow_version >= 3 && s->cluster_size > 4096) { |
Eric Blake | bb40ebc | 2020-03-24 12:42:31 -0500 | [diff] [blame] | 2915 | static const Qcow2Feature features[] = { |
Kevin Wolf | 1a4828c | 2015-12-02 19:11:04 +0100 | [diff] [blame] | 2916 | { |
| 2917 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, |
| 2918 | .bit = QCOW2_INCOMPAT_DIRTY_BITNR, |
| 2919 | .name = "dirty bit", |
| 2920 | }, |
| 2921 | { |
| 2922 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, |
| 2923 | .bit = QCOW2_INCOMPAT_CORRUPT_BITNR, |
| 2924 | .name = "corrupt bit", |
| 2925 | }, |
| 2926 | { |
Kevin Wolf | 93c2493 | 2019-01-14 16:48:25 +0100 | [diff] [blame] | 2927 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, |
| 2928 | .bit = QCOW2_INCOMPAT_DATA_FILE_BITNR, |
| 2929 | .name = "external data file", |
| 2930 | }, |
| 2931 | { |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 2932 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, |
| 2933 | .bit = QCOW2_INCOMPAT_COMPRESSION_BITNR, |
| 2934 | .name = "compression type", |
| 2935 | }, |
| 2936 | { |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 2937 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, |
| 2938 | .bit = QCOW2_INCOMPAT_EXTL2_BITNR, |
| 2939 | .name = "extended L2 entries", |
| 2940 | }, |
| 2941 | { |
Kevin Wolf | 1a4828c | 2015-12-02 19:11:04 +0100 | [diff] [blame] | 2942 | .type = QCOW2_FEAT_TYPE_COMPATIBLE, |
| 2943 | .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, |
| 2944 | .name = "lazy refcounts", |
| 2945 | }, |
Eric Blake | bb40ebc | 2020-03-24 12:42:31 -0500 | [diff] [blame] | 2946 | { |
| 2947 | .type = QCOW2_FEAT_TYPE_AUTOCLEAR, |
| 2948 | .bit = QCOW2_AUTOCLEAR_BITMAPS_BITNR, |
| 2949 | .name = "bitmaps", |
| 2950 | }, |
| 2951 | { |
| 2952 | .type = QCOW2_FEAT_TYPE_AUTOCLEAR, |
| 2953 | .bit = QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR, |
| 2954 | .name = "raw external data", |
| 2955 | }, |
Kevin Wolf | 1a4828c | 2015-12-02 19:11:04 +0100 | [diff] [blame] | 2956 | }; |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 2957 | |
Kevin Wolf | 1a4828c | 2015-12-02 19:11:04 +0100 | [diff] [blame] | 2958 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, |
| 2959 | features, sizeof(features), buflen); |
| 2960 | if (ret < 0) { |
| 2961 | goto fail; |
| 2962 | } |
| 2963 | buf += ret; |
| 2964 | buflen -= ret; |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 2965 | } |
Kevin Wolf | cfcc4c6 | 2012-04-12 15:20:27 +0200 | [diff] [blame] | 2966 | |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 2967 | /* Bitmap extension */ |
| 2968 | if (s->nb_bitmaps > 0) { |
| 2969 | Qcow2BitmapHeaderExt bitmaps_header = { |
| 2970 | .nb_bitmaps = cpu_to_be32(s->nb_bitmaps), |
| 2971 | .bitmap_directory_size = |
| 2972 | cpu_to_be64(s->bitmap_directory_size), |
| 2973 | .bitmap_directory_offset = |
| 2974 | cpu_to_be64(s->bitmap_directory_offset) |
| 2975 | }; |
| 2976 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS, |
| 2977 | &bitmaps_header, sizeof(bitmaps_header), |
| 2978 | buflen); |
| 2979 | if (ret < 0) { |
| 2980 | goto fail; |
| 2981 | } |
| 2982 | buf += ret; |
| 2983 | buflen -= ret; |
| 2984 | } |
| 2985 | |
Kevin Wolf | 75bab85 | 2012-02-02 14:52:08 +0100 | [diff] [blame] | 2986 | /* Keep unknown header extensions */ |
| 2987 | QLIST_FOREACH(uext, &s->unknown_header_ext, next) { |
| 2988 | ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); |
| 2989 | if (ret < 0) { |
| 2990 | goto fail; |
| 2991 | } |
| 2992 | |
| 2993 | buf += ret; |
| 2994 | buflen -= ret; |
| 2995 | } |
| 2996 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 2997 | /* End of header extensions */ |
| 2998 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2999 | if (ret < 0) { |
| 3000 | goto fail; |
| 3001 | } |
| 3002 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3003 | buf += ret; |
| 3004 | buflen -= ret; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 3005 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3006 | /* Backing file name */ |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 3007 | if (s->image_backing_file) { |
| 3008 | size_t backing_file_len = strlen(s->image_backing_file); |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3009 | |
| 3010 | if (buflen < backing_file_len) { |
| 3011 | ret = -ENOSPC; |
| 3012 | goto fail; |
| 3013 | } |
| 3014 | |
Jim Meyering | 00ea188 | 2012-10-04 13:10:01 +0200 | [diff] [blame] | 3015 | /* Using strncpy is ok here, since buf is not NUL-terminated. */ |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 3016 | strncpy(buf, s->image_backing_file, buflen); |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3017 | |
| 3018 | header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); |
| 3019 | header->backing_file_size = cpu_to_be32(backing_file_len); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3022 | /* Write the new header */ |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 3023 | ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 3024 | if (ret < 0) { |
| 3025 | goto fail; |
| 3026 | } |
| 3027 | |
| 3028 | ret = 0; |
| 3029 | fail: |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3030 | qemu_vfree(header); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 3031 | return ret; |
| 3032 | } |
| 3033 | |
| 3034 | static int qcow2_change_backing_file(BlockDriverState *bs, |
| 3035 | const char *backing_file, const char *backing_fmt) |
| 3036 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 3037 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 3038 | |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 3039 | /* Adding a backing file means that the external data file alone won't be |
| 3040 | * enough to make sense of the content */ |
| 3041 | if (backing_file && data_file_is_raw(bs)) { |
| 3042 | return -EINVAL; |
| 3043 | } |
| 3044 | |
Max Reitz | 4e876bc | 2016-04-06 18:32:48 +0200 | [diff] [blame] | 3045 | if (backing_file && strlen(backing_file) > 1023) { |
| 3046 | return -EINVAL; |
| 3047 | } |
| 3048 | |
Max Reitz | 998c201 | 2019-02-01 20:29:08 +0100 | [diff] [blame] | 3049 | pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), |
| 3050 | backing_file ?: ""); |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3051 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
| 3052 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); |
| 3053 | |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 3054 | g_free(s->image_backing_file); |
| 3055 | g_free(s->image_backing_format); |
| 3056 | |
| 3057 | s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL; |
| 3058 | s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL; |
| 3059 | |
Kevin Wolf | e24e49e | 2012-02-02 12:32:31 +0100 | [diff] [blame] | 3060 | return qcow2_update_header(bs); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 3061 | } |
| 3062 | |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3063 | static int qcow2_set_up_encryption(BlockDriverState *bs, |
| 3064 | QCryptoBlockCreateOptions *cryptoopts, |
| 3065 | Error **errp) |
| 3066 | { |
| 3067 | BDRVQcow2State *s = bs->opaque; |
| 3068 | QCryptoBlock *crypto = NULL; |
| 3069 | int fmt, ret; |
| 3070 | |
| 3071 | switch (cryptoopts->format) { |
| 3072 | case Q_CRYPTO_BLOCK_FORMAT_LUKS: |
| 3073 | fmt = QCOW_CRYPT_LUKS; |
| 3074 | break; |
| 3075 | case Q_CRYPTO_BLOCK_FORMAT_QCOW: |
| 3076 | fmt = QCOW_CRYPT_AES; |
| 3077 | break; |
| 3078 | default: |
| 3079 | error_setg(errp, "Crypto format not supported in qcow2"); |
| 3080 | return -EINVAL; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3081 | } |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3082 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 3083 | s->crypt_method_header = fmt; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3084 | |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 3085 | crypto = qcrypto_block_create(cryptoopts, "encrypt.", |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 3086 | qcow2_crypto_hdr_init_func, |
| 3087 | qcow2_crypto_hdr_write_func, |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3088 | bs, errp); |
| 3089 | if (!crypto) { |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3090 | return -EINVAL; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | ret = qcow2_update_header(bs); |
| 3094 | if (ret < 0) { |
| 3095 | error_setg_errno(errp, -ret, "Could not write encryption header"); |
| 3096 | goto out; |
| 3097 | } |
| 3098 | |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3099 | ret = 0; |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3100 | out: |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3101 | qcrypto_block_free(crypto); |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3102 | return ret; |
| 3103 | } |
| 3104 | |
Max Reitz | 7bc45dc | 2017-06-13 22:21:00 +0200 | [diff] [blame] | 3105 | /** |
| 3106 | * Preallocates metadata structures for data clusters between @offset (in the |
| 3107 | * guest disk) and @new_length (which is thus generally the new guest disk |
| 3108 | * size). |
| 3109 | * |
| 3110 | * Returns: 0 on success, -errno on failure. |
| 3111 | */ |
Kevin Wolf | 47e86b8 | 2018-06-26 15:52:13 +0200 | [diff] [blame] | 3112 | static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset, |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 3113 | uint64_t new_length, PreallocMode mode, |
| 3114 | Error **errp) |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3115 | { |
Kevin Wolf | 93e32b3 | 2019-04-15 17:54:50 +0200 | [diff] [blame] | 3116 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 3117 | uint64_t bytes; |
Kevin Wolf | 060bee8 | 2012-12-07 18:08:45 +0100 | [diff] [blame] | 3118 | uint64_t host_offset = 0; |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 3119 | int64_t file_length; |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 3120 | unsigned int cur_bytes; |
Kevin Wolf | 148da7e | 2010-01-20 15:03:01 +0100 | [diff] [blame] | 3121 | int ret; |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3122 | QCowL2Meta *meta = NULL, *m; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3123 | |
Max Reitz | 7bc45dc | 2017-06-13 22:21:00 +0200 | [diff] [blame] | 3124 | assert(offset <= new_length); |
| 3125 | bytes = new_length - offset; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3126 | |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 3127 | while (bytes) { |
Kevin Wolf | f29fbf7 | 2019-04-15 16:25:01 +0200 | [diff] [blame] | 3128 | cur_bytes = MIN(bytes, QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size)); |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 3129 | ret = qcow2_alloc_host_offset(bs, offset, &cur_bytes, |
| 3130 | &host_offset, &meta); |
Kevin Wolf | 148da7e | 2010-01-20 15:03:01 +0100 | [diff] [blame] | 3131 | if (ret < 0) { |
Kevin Wolf | 360bd07 | 2019-04-15 16:56:07 +0200 | [diff] [blame] | 3132 | error_setg_errno(errp, -ret, "Allocating clusters failed"); |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3133 | goto out; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3134 | } |
| 3135 | |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3136 | for (m = meta; m != NULL; m = m->next) { |
| 3137 | m->prealloc = true; |
| 3138 | } |
Stefan Hajnoczi | c792707 | 2014-04-01 11:12:57 +0200 | [diff] [blame] | 3139 | |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3140 | ret = qcow2_handle_l2meta(bs, &meta, true); |
| 3141 | if (ret < 0) { |
| 3142 | error_setg_errno(errp, -ret, "Mapping clusters failed"); |
| 3143 | goto out; |
Kevin Wolf | f50f88b | 2012-12-07 18:08:46 +0100 | [diff] [blame] | 3144 | } |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 3145 | |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3146 | /* TODO Preallocate data if requested */ |
| 3147 | |
Kevin Wolf | d46a0bb | 2016-06-01 16:55:05 +0200 | [diff] [blame] | 3148 | bytes -= cur_bytes; |
| 3149 | offset += cur_bytes; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | /* |
| 3153 | * It is expected that the image file is large enough to actually contain |
| 3154 | * all of the allocated clusters (otherwise we get failing reads after |
| 3155 | * EOF). Extend the image to the last allocated sector. |
| 3156 | */ |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 3157 | file_length = bdrv_getlength(s->data_file->bs); |
| 3158 | if (file_length < 0) { |
| 3159 | error_setg_errno(errp, -file_length, "Could not get file size"); |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3160 | ret = file_length; |
| 3161 | goto out; |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | if (host_offset + cur_bytes > file_length) { |
| 3165 | if (mode == PREALLOC_MODE_METADATA) { |
| 3166 | mode = PREALLOC_MODE_OFF; |
| 3167 | } |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 3168 | ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false, |
Kevin Wolf | 7b8e485 | 2020-04-24 14:54:40 +0200 | [diff] [blame] | 3169 | mode, 0, errp); |
Kevin Wolf | 19dbcbf | 2010-06-22 16:59:46 +0200 | [diff] [blame] | 3170 | if (ret < 0) { |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3171 | goto out; |
Kevin Wolf | 19dbcbf | 2010-06-22 16:59:46 +0200 | [diff] [blame] | 3172 | } |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
Alberto Garcia | 1a52b73 | 2020-09-08 16:08:27 +0200 | [diff] [blame] | 3175 | ret = 0; |
| 3176 | |
| 3177 | out: |
| 3178 | qcow2_handle_l2meta(bs, &meta, false); |
| 3179 | return ret; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3182 | /* qcow2_refcount_metadata_size: |
| 3183 | * @clusters: number of clusters to refcount (including data and L1/L2 tables) |
| 3184 | * @cluster_size: size of a cluster, in bytes |
| 3185 | * @refcount_order: refcount bits power-of-2 exponent |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 3186 | * @generous_increase: allow for the refcount table to be 1.5x as large as it |
| 3187 | * needs to be |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3188 | * |
| 3189 | * Returns: Number of bytes required for refcount blocks and table metadata. |
| 3190 | */ |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 3191 | int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size, |
| 3192 | int refcount_order, bool generous_increase, |
| 3193 | uint64_t *refblock_count) |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3194 | { |
| 3195 | /* |
| 3196 | * Every host cluster is reference-counted, including metadata (even |
| 3197 | * refcount metadata is recursively included). |
| 3198 | * |
| 3199 | * An accurate formula for the size of refcount metadata size is difficult |
| 3200 | * to derive. An easier method of calculation is finding the fixed point |
| 3201 | * where no further refcount blocks or table clusters are required to |
| 3202 | * reference count every cluster. |
| 3203 | */ |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 3204 | int64_t blocks_per_table_cluster = cluster_size / REFTABLE_ENTRY_SIZE; |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3205 | int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order); |
| 3206 | int64_t table = 0; /* number of refcount table clusters */ |
| 3207 | int64_t blocks = 0; /* number of refcount block clusters */ |
| 3208 | int64_t last; |
| 3209 | int64_t n = 0; |
| 3210 | |
| 3211 | do { |
| 3212 | last = n; |
| 3213 | blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block); |
| 3214 | table = DIV_ROUND_UP(blocks, blocks_per_table_cluster); |
| 3215 | n = clusters + blocks + table; |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 3216 | |
| 3217 | if (n == last && generous_increase) { |
| 3218 | clusters += DIV_ROUND_UP(table, 2); |
| 3219 | n = 0; /* force another loop */ |
| 3220 | generous_increase = false; |
| 3221 | } |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3222 | } while (n != last); |
| 3223 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 3224 | if (refblock_count) { |
| 3225 | *refblock_count = blocks; |
| 3226 | } |
| 3227 | |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3228 | return (blocks + table) * cluster_size; |
| 3229 | } |
| 3230 | |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3231 | /** |
| 3232 | * qcow2_calc_prealloc_size: |
| 3233 | * @total_size: virtual disk size in bytes |
| 3234 | * @cluster_size: cluster size in bytes |
| 3235 | * @refcount_order: refcount bits power-of-2 exponent |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 3236 | * @extended_l2: true if the image has extended L2 entries |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3237 | * |
| 3238 | * Returns: Total number of bytes required for the fully allocated image |
| 3239 | * (including metadata). |
| 3240 | */ |
| 3241 | static int64_t qcow2_calc_prealloc_size(int64_t total_size, |
| 3242 | size_t cluster_size, |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 3243 | int refcount_order, |
| 3244 | bool extended_l2) |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3245 | { |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3246 | int64_t meta_size = 0; |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3247 | uint64_t nl1e, nl2e; |
Alberto Garcia | 9e02968 | 2018-02-15 15:10:08 +0200 | [diff] [blame] | 3248 | int64_t aligned_total_size = ROUND_UP(total_size, cluster_size); |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 3249 | size_t l2e_size = extended_l2 ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL; |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3250 | |
| 3251 | /* header: 1 cluster */ |
| 3252 | meta_size += cluster_size; |
| 3253 | |
| 3254 | /* total size of L2 tables */ |
| 3255 | nl2e = aligned_total_size / cluster_size; |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 3256 | nl2e = ROUND_UP(nl2e, cluster_size / l2e_size); |
| 3257 | meta_size += nl2e * l2e_size; |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3258 | |
| 3259 | /* total size of L1 tables */ |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 3260 | nl1e = nl2e * l2e_size / cluster_size; |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 3261 | nl1e = ROUND_UP(nl1e, cluster_size / L1E_SIZE); |
| 3262 | meta_size += nl1e * L1E_SIZE; |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3263 | |
Stefan Hajnoczi | 7c5bcc4 | 2017-07-05 13:57:33 +0100 | [diff] [blame] | 3264 | /* total size of refcount table and blocks */ |
| 3265 | meta_size += qcow2_refcount_metadata_size( |
| 3266 | (meta_size + aligned_total_size) / cluster_size, |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 3267 | cluster_size, refcount_order, false, NULL); |
Stefan Hajnoczi | 95c67e3 | 2017-07-05 13:57:32 +0100 | [diff] [blame] | 3268 | |
| 3269 | return meta_size + aligned_total_size; |
| 3270 | } |
| 3271 | |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3272 | static bool validate_cluster_size(size_t cluster_size, bool extended_l2, |
| 3273 | Error **errp) |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3274 | { |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3275 | int cluster_bits = ctz32(cluster_size); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3276 | if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || |
| 3277 | (1 << cluster_bits) != cluster_size) |
| 3278 | { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3279 | error_setg(errp, "Cluster size must be a power of two between %d and " |
| 3280 | "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3281 | return false; |
| 3282 | } |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3283 | |
| 3284 | if (extended_l2) { |
| 3285 | unsigned min_cluster_size = |
| 3286 | (1 << MIN_CLUSTER_BITS) * QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER; |
| 3287 | if (cluster_size < min_cluster_size) { |
| 3288 | error_setg(errp, "Extended L2 entries are only supported with " |
| 3289 | "cluster sizes of at least %u bytes", min_cluster_size); |
| 3290 | return false; |
| 3291 | } |
| 3292 | } |
| 3293 | |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3294 | return true; |
| 3295 | } |
| 3296 | |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3297 | static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, bool extended_l2, |
| 3298 | Error **errp) |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3299 | { |
| 3300 | size_t cluster_size; |
| 3301 | |
| 3302 | cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, |
| 3303 | DEFAULT_CLUSTER_SIZE); |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3304 | if (!validate_cluster_size(cluster_size, extended_l2, errp)) { |
Stefan Hajnoczi | 0eb4a8c | 2017-07-05 13:57:34 +0100 | [diff] [blame] | 3305 | return 0; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3306 | } |
Stefan Hajnoczi | 0eb4a8c | 2017-07-05 13:57:34 +0100 | [diff] [blame] | 3307 | return cluster_size; |
| 3308 | } |
| 3309 | |
| 3310 | static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp) |
| 3311 | { |
| 3312 | char *buf; |
| 3313 | int ret; |
| 3314 | |
| 3315 | buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL); |
| 3316 | if (!buf) { |
| 3317 | ret = 3; /* default */ |
| 3318 | } else if (!strcmp(buf, "0.10")) { |
| 3319 | ret = 2; |
| 3320 | } else if (!strcmp(buf, "1.1")) { |
| 3321 | ret = 3; |
| 3322 | } else { |
| 3323 | error_setg(errp, "Invalid compatibility level: '%s'", buf); |
| 3324 | ret = -EINVAL; |
| 3325 | } |
| 3326 | g_free(buf); |
| 3327 | return ret; |
| 3328 | } |
| 3329 | |
| 3330 | static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version, |
| 3331 | Error **errp) |
| 3332 | { |
| 3333 | uint64_t refcount_bits; |
| 3334 | |
| 3335 | refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16); |
| 3336 | if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) { |
| 3337 | error_setg(errp, "Refcount width must be a power of two and may not " |
| 3338 | "exceed 64 bits"); |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
| 3342 | if (version < 3 && refcount_bits != 16) { |
| 3343 | error_setg(errp, "Different refcount widths than 16 bits require " |
| 3344 | "compatibility level 1.1 or above (use compat=1.1 or " |
| 3345 | "greater)"); |
| 3346 | return 0; |
| 3347 | } |
| 3348 | |
| 3349 | return refcount_bits; |
| 3350 | } |
| 3351 | |
Stefan Hajnoczi | c274393 | 2018-01-18 13:43:46 +0100 | [diff] [blame] | 3352 | static int coroutine_fn |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3353 | qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) |
Stefan Hajnoczi | 0eb4a8c | 2017-07-05 13:57:34 +0100 | [diff] [blame] | 3354 | { |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3355 | BlockdevCreateOptionsQcow2 *qcow2_opts; |
Stefan Hajnoczi | 0eb4a8c | 2017-07-05 13:57:34 +0100 | [diff] [blame] | 3356 | QDict *options; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3357 | |
| 3358 | /* |
| 3359 | * Open the image file and write a minimal qcow2 header. |
| 3360 | * |
| 3361 | * We keep things simple and start with a zero-sized image. We also |
| 3362 | * do without refcount blocks or a L1 table for now. We'll fix the |
| 3363 | * inconsistency later. |
| 3364 | * |
| 3365 | * We do need a refcount table because growing the refcount table means |
Eric Blake | a951a63 | 2020-03-24 12:42:30 -0500 | [diff] [blame] | 3366 | * allocating two new refcount blocks - the second of which would be at |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3367 | * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file |
| 3368 | * size for any qcow2 image. |
| 3369 | */ |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3370 | BlockBackend *blk = NULL; |
| 3371 | BlockDriverState *bs = NULL; |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3372 | BlockDriverState *data_bs = NULL; |
Kevin Wolf | f8413b3 | 2013-12-04 11:06:36 +0100 | [diff] [blame] | 3373 | QCowHeader *header; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3374 | size_t cluster_size; |
| 3375 | int version; |
| 3376 | int refcount_order; |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 3377 | uint64_t* refcount_table; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3378 | int ret; |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 3379 | uint8_t compression_type = QCOW2_COMPRESSION_TYPE_ZLIB; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3380 | |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3381 | assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2); |
| 3382 | qcow2_opts = &create_options->u.qcow2; |
| 3383 | |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3384 | bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp); |
| 3385 | if (bs == NULL) { |
| 3386 | return -EIO; |
| 3387 | } |
| 3388 | |
| 3389 | /* Validate options and set default values */ |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3390 | if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) { |
Alberto Garcia | 3afea40 | 2020-01-18 20:09:30 +0100 | [diff] [blame] | 3391 | error_setg(errp, "Image size must be a multiple of %u bytes", |
| 3392 | (unsigned) BDRV_SECTOR_SIZE); |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3393 | ret = -EINVAL; |
| 3394 | goto out; |
| 3395 | } |
| 3396 | |
| 3397 | if (qcow2_opts->has_version) { |
| 3398 | switch (qcow2_opts->version) { |
| 3399 | case BLOCKDEV_QCOW2_VERSION_V2: |
| 3400 | version = 2; |
| 3401 | break; |
| 3402 | case BLOCKDEV_QCOW2_VERSION_V3: |
| 3403 | version = 3; |
| 3404 | break; |
| 3405 | default: |
| 3406 | g_assert_not_reached(); |
| 3407 | } |
| 3408 | } else { |
| 3409 | version = 3; |
| 3410 | } |
| 3411 | |
| 3412 | if (qcow2_opts->has_cluster_size) { |
| 3413 | cluster_size = qcow2_opts->cluster_size; |
| 3414 | } else { |
| 3415 | cluster_size = DEFAULT_CLUSTER_SIZE; |
| 3416 | } |
| 3417 | |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3418 | if (!qcow2_opts->has_extended_l2) { |
| 3419 | qcow2_opts->extended_l2 = false; |
| 3420 | } |
| 3421 | if (qcow2_opts->extended_l2) { |
| 3422 | if (version < 3) { |
| 3423 | error_setg(errp, "Extended L2 entries are only supported with " |
| 3424 | "compatibility level 1.1 and above (use version=v3 or " |
| 3425 | "greater)"); |
| 3426 | ret = -EINVAL; |
| 3427 | goto out; |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | if (!validate_cluster_size(cluster_size, qcow2_opts->extended_l2, errp)) { |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3432 | ret = -EINVAL; |
| 3433 | goto out; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | if (!qcow2_opts->has_preallocation) { |
| 3437 | qcow2_opts->preallocation = PREALLOC_MODE_OFF; |
| 3438 | } |
| 3439 | if (qcow2_opts->has_backing_file && |
Alberto Garcia | 2118771 | 2020-07-10 18:13:14 +0200 | [diff] [blame] | 3440 | qcow2_opts->preallocation != PREALLOC_MODE_OFF && |
| 3441 | !qcow2_opts->extended_l2) |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3442 | { |
Alberto Garcia | 2118771 | 2020-07-10 18:13:14 +0200 | [diff] [blame] | 3443 | error_setg(errp, "Backing file and preallocation can only be used at " |
| 3444 | "the same time if extended_l2 is on"); |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3445 | ret = -EINVAL; |
| 3446 | goto out; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3447 | } |
| 3448 | if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) { |
| 3449 | error_setg(errp, "Backing format cannot be used without backing file"); |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3450 | ret = -EINVAL; |
| 3451 | goto out; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | if (!qcow2_opts->has_lazy_refcounts) { |
| 3455 | qcow2_opts->lazy_refcounts = false; |
| 3456 | } |
| 3457 | if (version < 3 && qcow2_opts->lazy_refcounts) { |
| 3458 | error_setg(errp, "Lazy refcounts only supported with compatibility " |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3459 | "level 1.1 and above (use version=v3 or greater)"); |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3460 | ret = -EINVAL; |
| 3461 | goto out; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | if (!qcow2_opts->has_refcount_bits) { |
| 3465 | qcow2_opts->refcount_bits = 16; |
| 3466 | } |
| 3467 | if (qcow2_opts->refcount_bits > 64 || |
| 3468 | !is_power_of_2(qcow2_opts->refcount_bits)) |
| 3469 | { |
| 3470 | error_setg(errp, "Refcount width must be a power of two and may not " |
| 3471 | "exceed 64 bits"); |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3472 | ret = -EINVAL; |
| 3473 | goto out; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3474 | } |
| 3475 | if (version < 3 && qcow2_opts->refcount_bits != 16) { |
| 3476 | error_setg(errp, "Different refcount widths than 16 bits require " |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3477 | "compatibility level 1.1 or above (use version=v3 or " |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3478 | "greater)"); |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3479 | ret = -EINVAL; |
| 3480 | goto out; |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3481 | } |
| 3482 | refcount_order = ctz32(qcow2_opts->refcount_bits); |
| 3483 | |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 3484 | if (qcow2_opts->data_file_raw && !qcow2_opts->data_file) { |
| 3485 | error_setg(errp, "data-file-raw requires data-file"); |
| 3486 | ret = -EINVAL; |
| 3487 | goto out; |
| 3488 | } |
| 3489 | if (qcow2_opts->data_file_raw && qcow2_opts->has_backing_file) { |
| 3490 | error_setg(errp, "Backing file and data-file-raw cannot be used at " |
| 3491 | "the same time"); |
| 3492 | ret = -EINVAL; |
| 3493 | goto out; |
| 3494 | } |
| 3495 | |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3496 | if (qcow2_opts->data_file) { |
| 3497 | if (version < 3) { |
| 3498 | error_setg(errp, "External data files are only supported with " |
| 3499 | "compatibility level 1.1 and above (use version=v3 or " |
| 3500 | "greater)"); |
| 3501 | ret = -EINVAL; |
| 3502 | goto out; |
| 3503 | } |
| 3504 | data_bs = bdrv_open_blockdev_ref(qcow2_opts->data_file, errp); |
Kevin Wolf | a0cf836 | 2019-03-13 15:22:38 +0100 | [diff] [blame] | 3505 | if (data_bs == NULL) { |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3506 | ret = -EIO; |
| 3507 | goto out; |
| 3508 | } |
| 3509 | } |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3510 | |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 3511 | if (qcow2_opts->has_compression_type && |
| 3512 | qcow2_opts->compression_type != QCOW2_COMPRESSION_TYPE_ZLIB) { |
| 3513 | |
| 3514 | ret = -EINVAL; |
| 3515 | |
| 3516 | if (version < 3) { |
| 3517 | error_setg(errp, "Non-zlib compression type is only supported with " |
| 3518 | "compatibility level 1.1 and above (use version=v3 or " |
| 3519 | "greater)"); |
| 3520 | goto out; |
| 3521 | } |
| 3522 | |
| 3523 | switch (qcow2_opts->compression_type) { |
Denis Plotnikov | d298ac1 | 2020-05-07 11:25:20 +0300 | [diff] [blame] | 3524 | #ifdef CONFIG_ZSTD |
| 3525 | case QCOW2_COMPRESSION_TYPE_ZSTD: |
| 3526 | break; |
| 3527 | #endif |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 3528 | default: |
| 3529 | error_setg(errp, "Unknown compression type"); |
| 3530 | goto out; |
| 3531 | } |
| 3532 | |
| 3533 | compression_type = qcow2_opts->compression_type; |
| 3534 | } |
| 3535 | |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3536 | /* Create BlockBackend to write to the image */ |
Eric Blake | a3aeeab | 2020-04-28 14:26:46 -0500 | [diff] [blame] | 3537 | blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL, |
| 3538 | errp); |
| 3539 | if (!blk) { |
| 3540 | ret = -EPERM; |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3541 | goto out; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3542 | } |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3543 | blk_set_allow_write_beyond_eof(blk, true); |
| 3544 | |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3545 | /* Write the header */ |
Kevin Wolf | f8413b3 | 2013-12-04 11:06:36 +0100 | [diff] [blame] | 3546 | QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header)); |
| 3547 | header = g_malloc0(cluster_size); |
| 3548 | *header = (QCowHeader) { |
| 3549 | .magic = cpu_to_be32(QCOW_MAGIC), |
| 3550 | .version = cpu_to_be32(version), |
Stefan Hajnoczi | 0eb4a8c | 2017-07-05 13:57:34 +0100 | [diff] [blame] | 3551 | .cluster_bits = cpu_to_be32(ctz32(cluster_size)), |
Kevin Wolf | f8413b3 | 2013-12-04 11:06:36 +0100 | [diff] [blame] | 3552 | .size = cpu_to_be64(0), |
| 3553 | .l1_table_offset = cpu_to_be64(0), |
| 3554 | .l1_size = cpu_to_be32(0), |
| 3555 | .refcount_table_offset = cpu_to_be64(cluster_size), |
| 3556 | .refcount_table_clusters = cpu_to_be32(1), |
Max Reitz | bd4b167 | 2015-02-18 17:40:46 -0500 | [diff] [blame] | 3557 | .refcount_order = cpu_to_be32(refcount_order), |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 3558 | /* don't deal with endianness since compression_type is 1 byte long */ |
| 3559 | .compression_type = compression_type, |
Kevin Wolf | f8413b3 | 2013-12-04 11:06:36 +0100 | [diff] [blame] | 3560 | .header_length = cpu_to_be32(sizeof(*header)), |
| 3561 | }; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3562 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3563 | /* We'll update this to correct value later */ |
| 3564 | header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3565 | |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3566 | if (qcow2_opts->lazy_refcounts) { |
Kevin Wolf | f8413b3 | 2013-12-04 11:06:36 +0100 | [diff] [blame] | 3567 | header->compatible_features |= |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 3568 | cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); |
| 3569 | } |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3570 | if (data_bs) { |
| 3571 | header->incompatible_features |= |
| 3572 | cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE); |
| 3573 | } |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 3574 | if (qcow2_opts->data_file_raw) { |
| 3575 | header->autoclear_features |= |
| 3576 | cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW); |
| 3577 | } |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 3578 | if (compression_type != QCOW2_COMPRESSION_TYPE_ZLIB) { |
| 3579 | header->incompatible_features |= |
| 3580 | cpu_to_be64(QCOW2_INCOMPAT_COMPRESSION); |
| 3581 | } |
Stefan Hajnoczi | bfe8043 | 2012-07-27 09:05:22 +0100 | [diff] [blame] | 3582 | |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3583 | if (qcow2_opts->extended_l2) { |
| 3584 | header->incompatible_features |= |
| 3585 | cpu_to_be64(QCOW2_INCOMPAT_EXTL2); |
| 3586 | } |
| 3587 | |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 3588 | ret = blk_pwrite(blk, 0, header, cluster_size, 0); |
Kevin Wolf | f8413b3 | 2013-12-04 11:06:36 +0100 | [diff] [blame] | 3589 | g_free(header); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3590 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3591 | error_setg_errno(errp, -ret, "Could not write qcow2 header"); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3592 | goto out; |
| 3593 | } |
| 3594 | |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 3595 | /* Write a refcount table with one refcount block */ |
| 3596 | refcount_table = g_malloc0(2 * cluster_size); |
| 3597 | refcount_table[0] = cpu_to_be64(2 * cluster_size); |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 3598 | ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3599 | g_free(refcount_table); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3600 | |
| 3601 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3602 | error_setg_errno(errp, -ret, "Could not write refcount table"); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3603 | goto out; |
| 3604 | } |
| 3605 | |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3606 | blk_unref(blk); |
| 3607 | blk = NULL; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3608 | |
| 3609 | /* |
| 3610 | * And now open the image and make it consistent first (i.e. increase the |
| 3611 | * refcount of the cluster that is occupied by the header and the refcount |
| 3612 | * table) |
| 3613 | */ |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3614 | options = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3615 | qdict_put_str(options, "driver", "qcow2"); |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3616 | qdict_put_str(options, "file", bs->node_name); |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3617 | if (data_bs) { |
| 3618 | qdict_put_str(options, "data-file", data_bs->node_name); |
| 3619 | } |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3620 | blk = blk_new_open(NULL, NULL, options, |
Kevin Wolf | 5588060 | 2017-02-17 15:07:38 +0100 | [diff] [blame] | 3621 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH, |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 3622 | errp); |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3623 | if (blk == NULL) { |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3624 | ret = -EIO; |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3625 | goto out; |
| 3626 | } |
| 3627 | |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3628 | ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3629 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3630 | error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 " |
| 3631 | "header and refcount table"); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3632 | goto out; |
| 3633 | |
| 3634 | } else if (ret != 0) { |
| 3635 | error_report("Huh, first cluster in empty image is already in use?"); |
| 3636 | abort(); |
| 3637 | } |
| 3638 | |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 3639 | /* Set the external data file if necessary */ |
| 3640 | if (data_bs) { |
| 3641 | BDRVQcow2State *s = blk_bs(blk)->opaque; |
| 3642 | s->image_data_file = g_strdup(data_bs->filename); |
| 3643 | } |
| 3644 | |
Kevin Wolf | b527c9b | 2015-12-02 18:34:39 +0100 | [diff] [blame] | 3645 | /* Create a full header (including things like feature table) */ |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3646 | ret = qcow2_update_header(blk_bs(blk)); |
Kevin Wolf | b527c9b | 2015-12-02 18:34:39 +0100 | [diff] [blame] | 3647 | if (ret < 0) { |
| 3648 | error_setg_errno(errp, -ret, "Could not update qcow2 header"); |
| 3649 | goto out; |
| 3650 | } |
| 3651 | |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3652 | /* Okay, now that we have a valid image, let's give it the right size */ |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 3653 | ret = blk_truncate(blk, qcow2_opts->size, false, qcow2_opts->preallocation, |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 3654 | 0, errp); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3655 | if (ret < 0) { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 3656 | error_prepend(errp, "Could not resize image: "); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3657 | goto out; |
| 3658 | } |
| 3659 | |
Eric Blake | a951a63 | 2020-03-24 12:42:30 -0500 | [diff] [blame] | 3660 | /* Want a backing file? There you go. */ |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3661 | if (qcow2_opts->has_backing_file) { |
| 3662 | const char *backing_format = NULL; |
| 3663 | |
| 3664 | if (qcow2_opts->has_backing_fmt) { |
| 3665 | backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt); |
| 3666 | } |
| 3667 | |
| 3668 | ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file, |
Eric Blake | e54ee1b | 2020-07-06 15:39:53 -0500 | [diff] [blame] | 3669 | backing_format, false); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3670 | if (ret < 0) { |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3671 | error_setg_errno(errp, -ret, "Could not assign backing file '%s' " |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3672 | "with format '%s'", qcow2_opts->backing_file, |
| 3673 | backing_format); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3674 | goto out; |
| 3675 | } |
| 3676 | } |
| 3677 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3678 | /* Want encryption? There you go. */ |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3679 | if (qcow2_opts->has_encrypt) { |
| 3680 | ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp); |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3681 | if (ret < 0) { |
| 3682 | goto out; |
| 3683 | } |
| 3684 | } |
| 3685 | |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3686 | blk_unref(blk); |
| 3687 | blk = NULL; |
Max Reitz | ba2ab2f | 2013-10-24 20:35:06 +0200 | [diff] [blame] | 3688 | |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3689 | /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning. |
| 3690 | * Using BDRV_O_NO_IO, since encryption is now setup we don't want to |
| 3691 | * have to setup decryption context. We're not doing any I/O on the top |
| 3692 | * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does |
| 3693 | * not have effect. |
| 3694 | */ |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3695 | options = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3696 | qdict_put_str(options, "driver", "qcow2"); |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3697 | qdict_put_str(options, "file", bs->node_name); |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3698 | if (data_bs) { |
| 3699 | qdict_put_str(options, "data-file", data_bs->node_name); |
| 3700 | } |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3701 | blk = blk_new_open(NULL, NULL, options, |
Daniel P. Berrange | b25b387 | 2017-06-23 17:24:10 +0100 | [diff] [blame] | 3702 | BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO, |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 3703 | errp); |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3704 | if (blk == NULL) { |
Kevin Wolf | 2358879 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 3705 | ret = -EIO; |
Max Reitz | ba2ab2f | 2013-10-24 20:35:06 +0200 | [diff] [blame] | 3706 | goto out; |
| 3707 | } |
| 3708 | |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3709 | ret = 0; |
| 3710 | out: |
Kevin Wolf | e1d74bc | 2018-01-10 15:52:33 +0100 | [diff] [blame] | 3711 | blk_unref(blk); |
| 3712 | bdrv_unref(bs); |
Kevin Wolf | dcc9868 | 2019-01-14 16:57:27 +0100 | [diff] [blame] | 3713 | bdrv_unref(data_bs); |
Kevin Wolf | a942073 | 2010-06-11 21:37:37 +0200 | [diff] [blame] | 3714 | return ret; |
| 3715 | } |
Kevin Wolf | de5f3f4 | 2010-05-07 12:43:45 +0200 | [diff] [blame] | 3716 | |
Maxim Levitsky | b92902d | 2020-03-26 03:12:17 +0200 | [diff] [blame] | 3717 | static int coroutine_fn qcow2_co_create_opts(BlockDriver *drv, |
| 3718 | const char *filename, |
| 3719 | QemuOpts *opts, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 3720 | Error **errp) |
Kevin Wolf | de5f3f4 | 2010-05-07 12:43:45 +0200 | [diff] [blame] | 3721 | { |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3722 | BlockdevCreateOptions *create_options = NULL; |
Markus Armbruster | 92adf9d | 2018-06-14 21:14:32 +0200 | [diff] [blame] | 3723 | QDict *qdict; |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3724 | Visitor *v; |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3725 | BlockDriverState *bs = NULL; |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 3726 | BlockDriverState *data_bs = NULL; |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3727 | const char *val; |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3728 | int ret; |
Kevin Wolf | de5f3f4 | 2010-05-07 12:43:45 +0200 | [diff] [blame] | 3729 | |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3730 | /* Only the keyval visitor supports the dotted syntax needed for |
| 3731 | * encryption, so go through a QDict before getting a QAPI type. Ignore |
| 3732 | * options meant for the protocol layer so that the visitor doesn't |
| 3733 | * complain. */ |
| 3734 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts, |
| 3735 | true); |
| 3736 | |
| 3737 | /* Handle encryption options */ |
| 3738 | val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT); |
| 3739 | if (val && !strcmp(val, "on")) { |
| 3740 | qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow"); |
| 3741 | } else if (val && !strcmp(val, "off")) { |
| 3742 | qdict_del(qdict, BLOCK_OPT_ENCRYPT); |
| 3743 | } |
| 3744 | |
| 3745 | val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT); |
| 3746 | if (val && !strcmp(val, "aes")) { |
| 3747 | qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow"); |
| 3748 | } |
| 3749 | |
| 3750 | /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into |
| 3751 | * version=v2/v3 below. */ |
| 3752 | val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL); |
| 3753 | if (val && !strcmp(val, "0.10")) { |
| 3754 | qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2"); |
| 3755 | } else if (val && !strcmp(val, "1.1")) { |
| 3756 | qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3"); |
| 3757 | } |
| 3758 | |
| 3759 | /* Change legacy command line options into QMP ones */ |
| 3760 | static const QDictRenames opt_renames[] = { |
| 3761 | { BLOCK_OPT_BACKING_FILE, "backing-file" }, |
| 3762 | { BLOCK_OPT_BACKING_FMT, "backing-fmt" }, |
| 3763 | { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, |
| 3764 | { BLOCK_OPT_LAZY_REFCOUNTS, "lazy-refcounts" }, |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 3765 | { BLOCK_OPT_EXTL2, "extended-l2" }, |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3766 | { BLOCK_OPT_REFCOUNT_BITS, "refcount-bits" }, |
| 3767 | { BLOCK_OPT_ENCRYPT, BLOCK_OPT_ENCRYPT_FORMAT }, |
| 3768 | { BLOCK_OPT_COMPAT_LEVEL, "version" }, |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 3769 | { BLOCK_OPT_DATA_FILE_RAW, "data-file-raw" }, |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 3770 | { BLOCK_OPT_COMPRESSION_TYPE, "compression-type" }, |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3771 | { NULL, NULL }, |
| 3772 | }; |
| 3773 | |
| 3774 | if (!qdict_rename_keys(qdict, opt_renames, errp)) { |
Kevin Wolf | 29ca9e4 | 2018-01-09 19:44:33 +0100 | [diff] [blame] | 3775 | ret = -EINVAL; |
| 3776 | goto finish; |
| 3777 | } |
Kevin Wolf | 60900b7 | 2018-01-10 17:55:16 +0100 | [diff] [blame] | 3778 | |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3779 | /* Create and open the file (protocol layer) */ |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3780 | ret = bdrv_create_file(filename, opts, errp); |
| 3781 | if (ret < 0) { |
| 3782 | goto finish; |
| 3783 | } |
| 3784 | |
| 3785 | bs = bdrv_open(filename, NULL, NULL, |
| 3786 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); |
| 3787 | if (bs == NULL) { |
| 3788 | ret = -EIO; |
| 3789 | goto finish; |
| 3790 | } |
| 3791 | |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 3792 | /* Create and open an external data file (protocol layer) */ |
| 3793 | val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE); |
| 3794 | if (val) { |
| 3795 | ret = bdrv_create_file(val, opts, errp); |
| 3796 | if (ret < 0) { |
| 3797 | goto finish; |
| 3798 | } |
| 3799 | |
| 3800 | data_bs = bdrv_open(val, NULL, NULL, |
| 3801 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, |
| 3802 | errp); |
| 3803 | if (data_bs == NULL) { |
| 3804 | ret = -EIO; |
| 3805 | goto finish; |
| 3806 | } |
| 3807 | |
| 3808 | qdict_del(qdict, BLOCK_OPT_DATA_FILE); |
| 3809 | qdict_put_str(qdict, "data-file", data_bs->node_name); |
| 3810 | } |
| 3811 | |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3812 | /* Set 'driver' and 'node' options */ |
| 3813 | qdict_put_str(qdict, "driver", "qcow2"); |
| 3814 | qdict_put_str(qdict, "file", bs->node_name); |
| 3815 | |
| 3816 | /* Now get the QAPI type BlockdevCreateOptions */ |
Markus Armbruster | af91062 | 2018-06-14 21:14:33 +0200 | [diff] [blame] | 3817 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
| 3818 | if (!v) { |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3819 | ret = -EINVAL; |
| 3820 | goto finish; |
| 3821 | } |
| 3822 | |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 3823 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp); |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3824 | visit_free(v); |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 3825 | if (!create_options) { |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3826 | ret = -EINVAL; |
| 3827 | goto finish; |
| 3828 | } |
| 3829 | |
| 3830 | /* Silently round up size */ |
| 3831 | create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size, |
| 3832 | BDRV_SECTOR_SIZE); |
| 3833 | |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3834 | /* Create the qcow2 image (format layer) */ |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3835 | ret = qcow2_co_create(create_options, errp); |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3836 | if (ret < 0) { |
| 3837 | goto finish; |
| 3838 | } |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 3839 | |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3840 | ret = 0; |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 3841 | finish: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 3842 | qobject_unref(qdict); |
Kevin Wolf | cbf2b7c | 2018-01-09 17:35:26 +0100 | [diff] [blame] | 3843 | bdrv_unref(bs); |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 3844 | bdrv_unref(data_bs); |
Kevin Wolf | b76b4f6 | 2018-01-11 16:18:08 +0100 | [diff] [blame] | 3845 | qapi_free_BlockdevCreateOptions(create_options); |
Max Reitz | 3ef6c40 | 2013-09-05 09:40:43 +0200 | [diff] [blame] | 3846 | return ret; |
Kevin Wolf | de5f3f4 | 2010-05-07 12:43:45 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3849 | |
Eric Blake | f06f6b6 | 2017-10-11 22:47:00 -0500 | [diff] [blame] | 3850 | static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes) |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3851 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 3852 | int64_t nr; |
| 3853 | int res; |
Eric Blake | f06f6b6 | 2017-10-11 22:47:00 -0500 | [diff] [blame] | 3854 | |
| 3855 | /* Clamp to image length, before checking status of underlying sectors */ |
Eric Blake | 8cbf74b | 2017-10-11 22:47:19 -0500 | [diff] [blame] | 3856 | if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) { |
| 3857 | bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset; |
Eric Blake | fbaa6bb | 2017-05-06 19:05:50 -0500 | [diff] [blame] | 3858 | } |
| 3859 | |
Eric Blake | f06f6b6 | 2017-10-11 22:47:00 -0500 | [diff] [blame] | 3860 | if (!bytes) { |
Eric Blake | ebb718a | 2016-05-25 21:48:49 -0600 | [diff] [blame] | 3861 | return true; |
| 3862 | } |
Eric Blake | 8cbf74b | 2017-10-11 22:47:19 -0500 | [diff] [blame] | 3863 | res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL); |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 3864 | return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes; |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3865 | } |
| 3866 | |
Eric Blake | 5544b59 | 2016-06-01 15:10:06 -0600 | [diff] [blame] | 3867 | static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs, |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3868 | int64_t offset, int bytes, BdrvRequestFlags flags) |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 3869 | { |
| 3870 | int ret; |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 3871 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 3872 | |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 3873 | uint32_t head = offset_into_subcluster(s, offset); |
| 3874 | uint32_t tail = ROUND_UP(offset + bytes, s->subcluster_size) - |
| 3875 | (offset + bytes); |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3876 | |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3877 | trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes); |
| 3878 | if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) { |
Eric Blake | fbaa6bb | 2017-05-06 19:05:50 -0500 | [diff] [blame] | 3879 | tail = 0; |
| 3880 | } |
Denis V. Lunev | 5a64e94 | 2016-05-25 21:48:47 -0600 | [diff] [blame] | 3881 | |
Eric Blake | ebb718a | 2016-05-25 21:48:49 -0600 | [diff] [blame] | 3882 | if (head || tail) { |
Eric Blake | ebb718a | 2016-05-25 21:48:49 -0600 | [diff] [blame] | 3883 | uint64_t off; |
Kevin Wolf | ecfe186 | 2016-05-31 16:13:07 +0200 | [diff] [blame] | 3884 | unsigned int nr; |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 3885 | QCow2SubclusterType type; |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3886 | |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 3887 | assert(head + bytes + tail <= s->subcluster_size); |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3888 | |
Eric Blake | ebb718a | 2016-05-25 21:48:49 -0600 | [diff] [blame] | 3889 | /* check whether remainder of cluster already reads as zero */ |
Eric Blake | f06f6b6 | 2017-10-11 22:47:00 -0500 | [diff] [blame] | 3890 | if (!(is_zero(bs, offset - head, head) && |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 3891 | is_zero(bs, offset + bytes, tail))) { |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3892 | return -ENOTSUP; |
| 3893 | } |
| 3894 | |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3895 | qemu_co_mutex_lock(&s->lock); |
| 3896 | /* We can have new write after previous check */ |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 3897 | offset -= head; |
| 3898 | bytes = s->subcluster_size; |
| 3899 | nr = s->subcluster_size; |
Alberto Garcia | ca4a0bb | 2020-07-10 18:12:59 +0200 | [diff] [blame] | 3900 | ret = qcow2_get_host_offset(bs, offset, &nr, &off, &type); |
| 3901 | if (ret < 0 || |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 3902 | (type != QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN && |
Alberto Garcia | 97490a1 | 2020-07-10 18:13:01 +0200 | [diff] [blame] | 3903 | type != QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC && |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 3904 | type != QCOW2_SUBCLUSTER_ZERO_PLAIN && |
| 3905 | type != QCOW2_SUBCLUSTER_ZERO_ALLOC)) { |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3906 | qemu_co_mutex_unlock(&s->lock); |
Alberto Garcia | 580384d | 2020-09-09 14:37:39 +0200 | [diff] [blame] | 3907 | return ret < 0 ? ret : -ENOTSUP; |
Denis V. Lunev | 2928abc | 2016-05-11 10:00:14 +0300 | [diff] [blame] | 3908 | } |
| 3909 | } else { |
| 3910 | qemu_co_mutex_lock(&s->lock); |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 3911 | } |
| 3912 | |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3913 | trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes); |
Denis V. Lunev | 5a64e94 | 2016-05-25 21:48:47 -0600 | [diff] [blame] | 3914 | |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 3915 | /* Whatever is left can use real zero subclusters */ |
| 3916 | ret = qcow2_subcluster_zeroize(bs, offset, bytes, flags); |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 3917 | qemu_co_mutex_unlock(&s->lock); |
| 3918 | |
| 3919 | return ret; |
| 3920 | } |
| 3921 | |
Eric Blake | 82e8a78 | 2016-07-15 17:23:03 -0600 | [diff] [blame] | 3922 | static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs, |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3923 | int64_t offset, int bytes) |
Kevin Wolf | 5ea929e | 2011-01-26 16:56:48 +0100 | [diff] [blame] | 3924 | { |
Paolo Bonzini | 6db39ae | 2011-10-20 13:16:25 +0200 | [diff] [blame] | 3925 | int ret; |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 3926 | BDRVQcow2State *s = bs->opaque; |
Paolo Bonzini | 6db39ae | 2011-10-20 13:16:25 +0200 | [diff] [blame] | 3927 | |
Alberto Garcia | 80f5c01 | 2020-03-31 13:43:45 +0200 | [diff] [blame] | 3928 | /* If the image does not support QCOW_OFLAG_ZERO then discarding |
| 3929 | * clusters could expose stale data from the backing file. */ |
| 3930 | if (s->qcow_version < 3 && bs->backing) { |
| 3931 | return -ENOTSUP; |
| 3932 | } |
| 3933 | |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3934 | if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) { |
| 3935 | assert(bytes < s->cluster_size); |
Eric Blake | 048c5fd | 2017-04-06 20:37:09 -0500 | [diff] [blame] | 3936 | /* Ignore partial clusters, except for the special case of the |
| 3937 | * complete partial cluster at the end of an unaligned file */ |
| 3938 | if (!QEMU_IS_ALIGNED(offset, s->cluster_size) || |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3939 | offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) { |
Eric Blake | 048c5fd | 2017-04-06 20:37:09 -0500 | [diff] [blame] | 3940 | return -ENOTSUP; |
| 3941 | } |
Eric Blake | 49228d1 | 2016-11-17 14:13:57 -0600 | [diff] [blame] | 3942 | } |
| 3943 | |
Paolo Bonzini | 6db39ae | 2011-10-20 13:16:25 +0200 | [diff] [blame] | 3944 | qemu_co_mutex_lock(&s->lock); |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 3945 | ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST, |
Eric Blake | d2cb36a | 2017-05-06 19:05:52 -0500 | [diff] [blame] | 3946 | false); |
Paolo Bonzini | 6db39ae | 2011-10-20 13:16:25 +0200 | [diff] [blame] | 3947 | qemu_co_mutex_unlock(&s->lock); |
| 3948 | return ret; |
Kevin Wolf | 5ea929e | 2011-01-26 16:56:48 +0100 | [diff] [blame] | 3949 | } |
| 3950 | |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3951 | static int coroutine_fn |
| 3952 | qcow2_co_copy_range_from(BlockDriverState *bs, |
| 3953 | BdrvChild *src, uint64_t src_offset, |
| 3954 | BdrvChild *dst, uint64_t dst_offset, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 3955 | uint64_t bytes, BdrvRequestFlags read_flags, |
| 3956 | BdrvRequestFlags write_flags) |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3957 | { |
| 3958 | BDRVQcow2State *s = bs->opaque; |
| 3959 | int ret; |
| 3960 | unsigned int cur_bytes; /* number of bytes in current iteration */ |
| 3961 | BdrvChild *child = NULL; |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 3962 | BdrvRequestFlags cur_write_flags; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3963 | |
| 3964 | assert(!bs->encrypted); |
| 3965 | qemu_co_mutex_lock(&s->lock); |
| 3966 | |
| 3967 | while (bytes != 0) { |
| 3968 | uint64_t copy_offset = 0; |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 3969 | QCow2SubclusterType type; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3970 | /* prepare next request */ |
| 3971 | cur_bytes = MIN(bytes, INT_MAX); |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 3972 | cur_write_flags = write_flags; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3973 | |
Alberto Garcia | ca4a0bb | 2020-07-10 18:12:59 +0200 | [diff] [blame] | 3974 | ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes, |
| 3975 | ©_offset, &type); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3976 | if (ret < 0) { |
| 3977 | goto out; |
| 3978 | } |
| 3979 | |
Alberto Garcia | ca4a0bb | 2020-07-10 18:12:59 +0200 | [diff] [blame] | 3980 | switch (type) { |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 3981 | case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN: |
Alberto Garcia | 97490a1 | 2020-07-10 18:13:01 +0200 | [diff] [blame] | 3982 | case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC: |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3983 | if (bs->backing && bs->backing->bs) { |
| 3984 | int64_t backing_length = bdrv_getlength(bs->backing->bs); |
| 3985 | if (src_offset >= backing_length) { |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 3986 | cur_write_flags |= BDRV_REQ_ZERO_WRITE; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3987 | } else { |
| 3988 | child = bs->backing; |
| 3989 | cur_bytes = MIN(cur_bytes, backing_length - src_offset); |
| 3990 | copy_offset = src_offset; |
| 3991 | } |
| 3992 | } else { |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 3993 | cur_write_flags |= BDRV_REQ_ZERO_WRITE; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 3994 | } |
| 3995 | break; |
| 3996 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 3997 | case QCOW2_SUBCLUSTER_ZERO_PLAIN: |
| 3998 | case QCOW2_SUBCLUSTER_ZERO_ALLOC: |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 3999 | cur_write_flags |= BDRV_REQ_ZERO_WRITE; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4000 | break; |
| 4001 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 4002 | case QCOW2_SUBCLUSTER_COMPRESSED: |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4003 | ret = -ENOTSUP; |
| 4004 | goto out; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4005 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 4006 | case QCOW2_SUBCLUSTER_NORMAL: |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 4007 | child = s->data_file; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4008 | break; |
| 4009 | |
| 4010 | default: |
| 4011 | abort(); |
| 4012 | } |
| 4013 | qemu_co_mutex_unlock(&s->lock); |
| 4014 | ret = bdrv_co_copy_range_from(child, |
| 4015 | copy_offset, |
| 4016 | dst, dst_offset, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 4017 | cur_bytes, read_flags, cur_write_flags); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4018 | qemu_co_mutex_lock(&s->lock); |
| 4019 | if (ret < 0) { |
| 4020 | goto out; |
| 4021 | } |
| 4022 | |
| 4023 | bytes -= cur_bytes; |
| 4024 | src_offset += cur_bytes; |
| 4025 | dst_offset += cur_bytes; |
| 4026 | } |
| 4027 | ret = 0; |
| 4028 | |
| 4029 | out: |
| 4030 | qemu_co_mutex_unlock(&s->lock); |
| 4031 | return ret; |
| 4032 | } |
| 4033 | |
| 4034 | static int coroutine_fn |
| 4035 | qcow2_co_copy_range_to(BlockDriverState *bs, |
| 4036 | BdrvChild *src, uint64_t src_offset, |
| 4037 | BdrvChild *dst, uint64_t dst_offset, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 4038 | uint64_t bytes, BdrvRequestFlags read_flags, |
| 4039 | BdrvRequestFlags write_flags) |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4040 | { |
| 4041 | BDRVQcow2State *s = bs->opaque; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4042 | int ret; |
| 4043 | unsigned int cur_bytes; /* number of sectors in current iteration */ |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 4044 | uint64_t host_offset; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4045 | QCowL2Meta *l2meta = NULL; |
| 4046 | |
| 4047 | assert(!bs->encrypted); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4048 | |
| 4049 | qemu_co_mutex_lock(&s->lock); |
| 4050 | |
| 4051 | while (bytes != 0) { |
| 4052 | |
| 4053 | l2meta = NULL; |
| 4054 | |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4055 | cur_bytes = MIN(bytes, INT_MAX); |
| 4056 | |
| 4057 | /* TODO: |
| 4058 | * If src->bs == dst->bs, we could simply copy by incrementing |
| 4059 | * the refcnt, without copying user data. |
| 4060 | * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */ |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 4061 | ret = qcow2_alloc_host_offset(bs, dst_offset, &cur_bytes, |
| 4062 | &host_offset, &l2meta); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4063 | if (ret < 0) { |
| 4064 | goto fail; |
| 4065 | } |
| 4066 | |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 4067 | ret = qcow2_pre_write_overlap_check(bs, 0, host_offset, cur_bytes, |
| 4068 | true); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4069 | if (ret < 0) { |
| 4070 | goto fail; |
| 4071 | } |
| 4072 | |
| 4073 | qemu_co_mutex_unlock(&s->lock); |
Alberto Garcia | bfd0989 | 2020-09-11 16:09:42 +0200 | [diff] [blame] | 4074 | ret = bdrv_co_copy_range_to(src, src_offset, s->data_file, host_offset, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 4075 | cur_bytes, read_flags, write_flags); |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4076 | qemu_co_mutex_lock(&s->lock); |
| 4077 | if (ret < 0) { |
| 4078 | goto fail; |
| 4079 | } |
| 4080 | |
| 4081 | ret = qcow2_handle_l2meta(bs, &l2meta, true); |
| 4082 | if (ret) { |
| 4083 | goto fail; |
| 4084 | } |
| 4085 | |
| 4086 | bytes -= cur_bytes; |
Fam Zheng | e06f463 | 2018-06-29 14:03:26 +0800 | [diff] [blame] | 4087 | src_offset += cur_bytes; |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4088 | dst_offset += cur_bytes; |
| 4089 | } |
| 4090 | ret = 0; |
| 4091 | |
| 4092 | fail: |
| 4093 | qcow2_handle_l2meta(bs, &l2meta, false); |
| 4094 | |
| 4095 | qemu_co_mutex_unlock(&s->lock); |
| 4096 | |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 4097 | trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); |
| 4098 | |
| 4099 | return ret; |
| 4100 | } |
| 4101 | |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4102 | static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset, |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 4103 | bool exact, PreallocMode prealloc, |
Kevin Wolf | 92b9279 | 2020-04-24 14:54:39 +0200 | [diff] [blame] | 4104 | BdrvRequestFlags flags, Error **errp) |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4105 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 4106 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4107 | uint64_t old_length; |
Kevin Wolf | 2cf7cfa | 2013-05-14 16:14:33 +0200 | [diff] [blame] | 4108 | int64_t new_l1_size; |
| 4109 | int ret; |
Leonid Bloch | 45b4949 | 2018-09-26 19:04:45 +0300 | [diff] [blame] | 4110 | QDict *options; |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4111 | |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4112 | if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA && |
| 4113 | prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL) |
| 4114 | { |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 4115 | error_setg(errp, "Unsupported preallocation mode '%s'", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 4116 | PreallocMode_str(prealloc)); |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 4117 | return -ENOTSUP; |
| 4118 | } |
| 4119 | |
Alberto Garcia | 3afea40 | 2020-01-18 20:09:30 +0100 | [diff] [blame] | 4120 | if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { |
| 4121 | error_setg(errp, "The new size must be a multiple of %u", |
| 4122 | (unsigned) BDRV_SECTOR_SIZE); |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4123 | return -EINVAL; |
| 4124 | } |
| 4125 | |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4126 | qemu_co_mutex_lock(&s->lock); |
| 4127 | |
Eric Blake | 7fa140a | 2020-04-28 14:26:47 -0500 | [diff] [blame] | 4128 | /* |
| 4129 | * Even though we store snapshot size for all images, it was not |
| 4130 | * required until v3, so it is not safe to proceed for v2. |
| 4131 | */ |
| 4132 | if (s->nb_snapshots && s->qcow_version < 3) { |
| 4133 | error_setg(errp, "Can't resize a v2 image which has snapshots"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4134 | ret = -ENOTSUP; |
| 4135 | goto fail; |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4136 | } |
| 4137 | |
Eric Blake | ee1244a | 2020-04-28 14:26:48 -0500 | [diff] [blame] | 4138 | /* See qcow2-bitmap.c for which bitmap scenarios prevent a resize. */ |
John Snow | d19c6b3 | 2019-03-11 21:51:46 +0300 | [diff] [blame] | 4139 | if (qcow2_truncate_bitmaps_check(bs, errp)) { |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4140 | ret = -ENOTSUP; |
| 4141 | goto fail; |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 4142 | } |
| 4143 | |
Leonid Bloch | bd016b9 | 2018-09-26 19:04:47 +0300 | [diff] [blame] | 4144 | old_length = bs->total_sectors * BDRV_SECTOR_SIZE; |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4145 | new_l1_size = size_to_l1(s, offset); |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4146 | |
| 4147 | if (offset < old_length) { |
Pavel Butsykin | 163bc39 | 2017-09-29 15:16:13 +0300 | [diff] [blame] | 4148 | int64_t last_cluster, old_file_size; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4149 | if (prealloc != PREALLOC_MODE_OFF) { |
| 4150 | error_setg(errp, |
| 4151 | "Preallocation can't be used for shrinking an image"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4152 | ret = -EINVAL; |
| 4153 | goto fail; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size), |
| 4157 | old_length - ROUND_UP(offset, |
| 4158 | s->cluster_size), |
| 4159 | QCOW2_DISCARD_ALWAYS, true); |
| 4160 | if (ret < 0) { |
| 4161 | error_setg_errno(errp, -ret, "Failed to discard cropped clusters"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4162 | goto fail; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | ret = qcow2_shrink_l1_table(bs, new_l1_size); |
| 4166 | if (ret < 0) { |
| 4167 | error_setg_errno(errp, -ret, |
| 4168 | "Failed to reduce the number of L2 tables"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4169 | goto fail; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | ret = qcow2_shrink_reftable(bs); |
| 4173 | if (ret < 0) { |
| 4174 | error_setg_errno(errp, -ret, |
| 4175 | "Failed to discard unused refblocks"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4176 | goto fail; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4177 | } |
Pavel Butsykin | 163bc39 | 2017-09-29 15:16:13 +0300 | [diff] [blame] | 4178 | |
| 4179 | old_file_size = bdrv_getlength(bs->file->bs); |
| 4180 | if (old_file_size < 0) { |
| 4181 | error_setg_errno(errp, -old_file_size, |
| 4182 | "Failed to inquire current file length"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4183 | ret = old_file_size; |
| 4184 | goto fail; |
Pavel Butsykin | 163bc39 | 2017-09-29 15:16:13 +0300 | [diff] [blame] | 4185 | } |
| 4186 | last_cluster = qcow2_get_last_cluster(bs, old_file_size); |
| 4187 | if (last_cluster < 0) { |
| 4188 | error_setg_errno(errp, -last_cluster, |
| 4189 | "Failed to find the last cluster"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4190 | ret = last_cluster; |
| 4191 | goto fail; |
Pavel Butsykin | 163bc39 | 2017-09-29 15:16:13 +0300 | [diff] [blame] | 4192 | } |
| 4193 | if ((last_cluster + 1) * s->cluster_size < old_file_size) { |
Max Reitz | 233521b | 2017-10-09 17:54:31 +0200 | [diff] [blame] | 4194 | Error *local_err = NULL; |
| 4195 | |
Max Reitz | e61a28a | 2019-09-18 11:51:42 +0200 | [diff] [blame] | 4196 | /* |
| 4197 | * Do not pass @exact here: It will not help the user if |
| 4198 | * we get an error here just because they wanted to shrink |
| 4199 | * their qcow2 image (on a block device) with qemu-img. |
| 4200 | * (And on the qcow2 layer, the @exact requirement is |
| 4201 | * always fulfilled, so there is no need to pass it on.) |
| 4202 | */ |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4203 | bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size, |
Kevin Wolf | 7b8e485 | 2020-04-24 14:54:40 +0200 | [diff] [blame] | 4204 | false, PREALLOC_MODE_OFF, 0, &local_err); |
Max Reitz | 233521b | 2017-10-09 17:54:31 +0200 | [diff] [blame] | 4205 | if (local_err) { |
| 4206 | warn_reportf_err(local_err, |
| 4207 | "Failed to truncate the tail of the image: "); |
Pavel Butsykin | 163bc39 | 2017-09-29 15:16:13 +0300 | [diff] [blame] | 4208 | } |
| 4209 | } |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4210 | } else { |
| 4211 | ret = qcow2_grow_l1_table(bs, new_l1_size, true); |
| 4212 | if (ret < 0) { |
| 4213 | error_setg_errno(errp, -ret, "Failed to grow the L1 table"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4214 | goto fail; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 4215 | } |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4218 | switch (prealloc) { |
| 4219 | case PREALLOC_MODE_OFF: |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 4220 | if (has_data_file(bs)) { |
Max Reitz | e61a28a | 2019-09-18 11:51:42 +0200 | [diff] [blame] | 4221 | /* |
| 4222 | * If the caller wants an exact resize, the external data |
| 4223 | * file should be resized to the exact target size, too, |
| 4224 | * so we pass @exact here. |
| 4225 | */ |
Kevin Wolf | 7b8e485 | 2020-04-24 14:54:40 +0200 | [diff] [blame] | 4226 | ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, 0, |
| 4227 | errp); |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 4228 | if (ret < 0) { |
| 4229 | goto fail; |
| 4230 | } |
| 4231 | } |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4232 | break; |
| 4233 | |
| 4234 | case PREALLOC_MODE_METADATA: |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 4235 | ret = preallocate_co(bs, old_length, offset, prealloc, errp); |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4236 | if (ret < 0) { |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4237 | goto fail; |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4238 | } |
| 4239 | break; |
| 4240 | |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4241 | case PREALLOC_MODE_FALLOC: |
| 4242 | case PREALLOC_MODE_FULL: |
| 4243 | { |
| 4244 | int64_t allocation_start, host_offset, guest_offset; |
| 4245 | int64_t clusters_allocated; |
Max Reitz | 4b96fa3 | 2020-05-05 16:18:01 +0200 | [diff] [blame] | 4246 | int64_t old_file_size, last_cluster, new_file_size; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4247 | uint64_t nb_new_data_clusters, nb_new_l2_tables; |
Alberto Garcia | 40dee94 | 2020-07-10 18:13:12 +0200 | [diff] [blame] | 4248 | bool subclusters_need_allocation = false; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4249 | |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 4250 | /* With a data file, preallocation means just allocating the metadata |
| 4251 | * and forwarding the truncate request to the data file */ |
| 4252 | if (has_data_file(bs)) { |
Kevin Wolf | 718c0fc | 2019-04-15 16:34:30 +0200 | [diff] [blame] | 4253 | ret = preallocate_co(bs, old_length, offset, prealloc, errp); |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 4254 | if (ret < 0) { |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 4255 | goto fail; |
| 4256 | } |
| 4257 | break; |
| 4258 | } |
| 4259 | |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4260 | old_file_size = bdrv_getlength(bs->file->bs); |
| 4261 | if (old_file_size < 0) { |
| 4262 | error_setg_errno(errp, -old_file_size, |
| 4263 | "Failed to inquire current file length"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4264 | ret = old_file_size; |
| 4265 | goto fail; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4266 | } |
Max Reitz | 4b96fa3 | 2020-05-05 16:18:01 +0200 | [diff] [blame] | 4267 | |
| 4268 | last_cluster = qcow2_get_last_cluster(bs, old_file_size); |
| 4269 | if (last_cluster >= 0) { |
| 4270 | old_file_size = (last_cluster + 1) * s->cluster_size; |
| 4271 | } else { |
| 4272 | old_file_size = ROUND_UP(old_file_size, s->cluster_size); |
| 4273 | } |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4274 | |
Alberto Garcia | a5675f3 | 2020-06-17 16:00:36 +0200 | [diff] [blame] | 4275 | nb_new_data_clusters = (ROUND_UP(offset, s->cluster_size) - |
| 4276 | start_of_cluster(s, old_length)) >> s->cluster_bits; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4277 | |
| 4278 | /* This is an overestimation; we will not actually allocate space for |
| 4279 | * these in the file but just make sure the new refcount structures are |
| 4280 | * able to cover them so we will not have to allocate new refblocks |
| 4281 | * while entering the data blocks in the potentially new L2 tables. |
| 4282 | * (We do not actually care where the L2 tables are placed. Maybe they |
| 4283 | * are already allocated or they can be placed somewhere before |
| 4284 | * @old_file_size. It does not matter because they will be fully |
| 4285 | * allocated automatically, so they do not need to be covered by the |
| 4286 | * preallocation. All that matters is that we will not have to allocate |
| 4287 | * new refcount structures for them.) */ |
| 4288 | nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters, |
Alberto Garcia | c8fd855 | 2020-07-10 18:12:54 +0200 | [diff] [blame] | 4289 | s->cluster_size / l2_entry_size(s)); |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4290 | /* The cluster range may not be aligned to L2 boundaries, so add one L2 |
| 4291 | * table for a potential head/tail */ |
| 4292 | nb_new_l2_tables++; |
| 4293 | |
| 4294 | allocation_start = qcow2_refcount_area(bs, old_file_size, |
| 4295 | nb_new_data_clusters + |
| 4296 | nb_new_l2_tables, |
| 4297 | true, 0, 0); |
| 4298 | if (allocation_start < 0) { |
| 4299 | error_setg_errno(errp, -allocation_start, |
| 4300 | "Failed to resize refcount structures"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4301 | ret = allocation_start; |
| 4302 | goto fail; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start, |
| 4306 | nb_new_data_clusters); |
| 4307 | if (clusters_allocated < 0) { |
| 4308 | error_setg_errno(errp, -clusters_allocated, |
| 4309 | "Failed to allocate data clusters"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4310 | ret = clusters_allocated; |
| 4311 | goto fail; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | assert(clusters_allocated == nb_new_data_clusters); |
| 4315 | |
| 4316 | /* Allocate the data area */ |
| 4317 | new_file_size = allocation_start + |
| 4318 | nb_new_data_clusters * s->cluster_size; |
Kevin Wolf | eb8a0cf | 2020-04-24 16:27:01 +0200 | [diff] [blame] | 4319 | /* |
| 4320 | * Image file grows, so @exact does not matter. |
| 4321 | * |
| 4322 | * If we need to zero out the new area, try first whether the protocol |
| 4323 | * driver can already take care of this. |
| 4324 | */ |
| 4325 | if (flags & BDRV_REQ_ZERO_WRITE) { |
| 4326 | ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, |
| 4327 | BDRV_REQ_ZERO_WRITE, NULL); |
| 4328 | if (ret >= 0) { |
| 4329 | flags &= ~BDRV_REQ_ZERO_WRITE; |
Alberto Garcia | 40dee94 | 2020-07-10 18:13:12 +0200 | [diff] [blame] | 4330 | /* Ensure that we read zeroes and not backing file data */ |
| 4331 | subclusters_need_allocation = true; |
Kevin Wolf | eb8a0cf | 2020-04-24 16:27:01 +0200 | [diff] [blame] | 4332 | } |
| 4333 | } else { |
| 4334 | ret = -1; |
| 4335 | } |
| 4336 | if (ret < 0) { |
| 4337 | ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, 0, |
| 4338 | errp); |
| 4339 | } |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4340 | if (ret < 0) { |
| 4341 | error_prepend(errp, "Failed to resize underlying file: "); |
| 4342 | qcow2_free_clusters(bs, allocation_start, |
| 4343 | nb_new_data_clusters * s->cluster_size, |
| 4344 | QCOW2_DISCARD_OTHER); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4345 | goto fail; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | /* Create the necessary L2 entries */ |
| 4349 | host_offset = allocation_start; |
| 4350 | guest_offset = old_length; |
| 4351 | while (nb_new_data_clusters) { |
Alberto Garcia | 13bec22 | 2018-02-05 16:33:31 +0200 | [diff] [blame] | 4352 | int64_t nb_clusters = MIN( |
| 4353 | nb_new_data_clusters, |
| 4354 | s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset)); |
Alberto Garcia | a5675f3 | 2020-06-17 16:00:36 +0200 | [diff] [blame] | 4355 | unsigned cow_start_length = offset_into_cluster(s, guest_offset); |
| 4356 | QCowL2Meta allocation; |
| 4357 | guest_offset = start_of_cluster(s, guest_offset); |
| 4358 | allocation = (QCowL2Meta) { |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4359 | .offset = guest_offset, |
| 4360 | .alloc_offset = host_offset, |
| 4361 | .nb_clusters = nb_clusters, |
Alberto Garcia | a5675f3 | 2020-06-17 16:00:36 +0200 | [diff] [blame] | 4362 | .cow_start = { |
| 4363 | .offset = 0, |
| 4364 | .nb_bytes = cow_start_length, |
| 4365 | }, |
| 4366 | .cow_end = { |
| 4367 | .offset = nb_clusters << s->cluster_bits, |
| 4368 | .nb_bytes = 0, |
| 4369 | }, |
Alberto Garcia | 40dee94 | 2020-07-10 18:13:12 +0200 | [diff] [blame] | 4370 | .prealloc = !subclusters_need_allocation, |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4371 | }; |
| 4372 | qemu_co_queue_init(&allocation.dependent_requests); |
| 4373 | |
| 4374 | ret = qcow2_alloc_cluster_link_l2(bs, &allocation); |
| 4375 | if (ret < 0) { |
| 4376 | error_setg_errno(errp, -ret, "Failed to update L2 tables"); |
| 4377 | qcow2_free_clusters(bs, host_offset, |
| 4378 | nb_new_data_clusters * s->cluster_size, |
| 4379 | QCOW2_DISCARD_OTHER); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4380 | goto fail; |
Max Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | guest_offset += nb_clusters * s->cluster_size; |
| 4384 | host_offset += nb_clusters * s->cluster_size; |
| 4385 | nb_new_data_clusters -= nb_clusters; |
| 4386 | } |
| 4387 | break; |
| 4388 | } |
| 4389 | |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4390 | default: |
| 4391 | g_assert_not_reached(); |
| 4392 | } |
| 4393 | |
Kevin Wolf | f01643f | 2020-04-24 14:54:42 +0200 | [diff] [blame] | 4394 | if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) { |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 4395 | uint64_t zero_start = QEMU_ALIGN_UP(old_length, s->subcluster_size); |
Kevin Wolf | f01643f | 2020-04-24 14:54:42 +0200 | [diff] [blame] | 4396 | |
| 4397 | /* |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 4398 | * Use zero clusters as much as we can. qcow2_subcluster_zeroize() |
| 4399 | * requires a subcluster-aligned start. The end may be unaligned if |
| 4400 | * it is at the end of the image (which it is here). |
Kevin Wolf | f01643f | 2020-04-24 14:54:42 +0200 | [diff] [blame] | 4401 | */ |
Alberto Garcia | e4d7019 | 2020-05-04 17:52:17 +0200 | [diff] [blame] | 4402 | if (offset > zero_start) { |
Alberto Garcia | a6841a2 | 2020-07-10 18:13:10 +0200 | [diff] [blame] | 4403 | ret = qcow2_subcluster_zeroize(bs, zero_start, offset - zero_start, |
| 4404 | 0); |
Alberto Garcia | e4d7019 | 2020-05-04 17:52:17 +0200 | [diff] [blame] | 4405 | if (ret < 0) { |
| 4406 | error_setg_errno(errp, -ret, "Failed to zero out new clusters"); |
| 4407 | goto fail; |
| 4408 | } |
Kevin Wolf | f01643f | 2020-04-24 14:54:42 +0200 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | /* Write explicit zeros for the unaligned head */ |
| 4412 | if (zero_start > old_length) { |
Alberto Garcia | e4d7019 | 2020-05-04 17:52:17 +0200 | [diff] [blame] | 4413 | uint64_t len = MIN(zero_start, offset) - old_length; |
Kevin Wolf | f01643f | 2020-04-24 14:54:42 +0200 | [diff] [blame] | 4414 | uint8_t *buf = qemu_blockalign0(bs, len); |
| 4415 | QEMUIOVector qiov; |
| 4416 | qemu_iovec_init_buf(&qiov, buf, len); |
| 4417 | |
| 4418 | qemu_co_mutex_unlock(&s->lock); |
| 4419 | ret = qcow2_co_pwritev_part(bs, old_length, len, &qiov, 0, 0); |
| 4420 | qemu_co_mutex_lock(&s->lock); |
| 4421 | |
| 4422 | qemu_vfree(buf); |
| 4423 | if (ret < 0) { |
| 4424 | error_setg_errno(errp, -ret, "Failed to zero out the new area"); |
| 4425 | goto fail; |
| 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4430 | if (prealloc != PREALLOC_MODE_OFF) { |
| 4431 | /* Flush metadata before actually changing the image size */ |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4432 | ret = qcow2_write_caches(bs); |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4433 | if (ret < 0) { |
| 4434 | error_setg_errno(errp, -ret, |
| 4435 | "Failed to flush the preallocated area to disk"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4436 | goto fail; |
Max Reitz | 95b98f3 | 2017-06-13 22:21:02 +0200 | [diff] [blame] | 4437 | } |
| 4438 | } |
| 4439 | |
Leonid Bloch | 45b4949 | 2018-09-26 19:04:45 +0300 | [diff] [blame] | 4440 | bs->total_sectors = offset / BDRV_SECTOR_SIZE; |
| 4441 | |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4442 | /* write updated header.size */ |
| 4443 | offset = cpu_to_be64(offset); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 4444 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 4445 | &offset, sizeof(offset)); |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4446 | if (ret < 0) { |
Max Reitz | f59adb3 | 2017-03-28 22:51:29 +0200 | [diff] [blame] | 4447 | error_setg_errno(errp, -ret, "Failed to update the image size"); |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4448 | goto fail; |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | s->l1_vm_state_index = new_l1_size; |
Leonid Bloch | 45b4949 | 2018-09-26 19:04:45 +0300 | [diff] [blame] | 4452 | |
| 4453 | /* Update cache sizes */ |
| 4454 | options = qdict_clone_shallow(bs->options); |
| 4455 | ret = qcow2_update_options(bs, options, s->flags, errp); |
| 4456 | qobject_unref(options); |
| 4457 | if (ret < 0) { |
| 4458 | goto fail; |
| 4459 | } |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 4460 | ret = 0; |
| 4461 | fail: |
| 4462 | qemu_co_mutex_unlock(&s->lock); |
| 4463 | return ret; |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 4464 | } |
| 4465 | |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4466 | static coroutine_fn int |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4467 | qcow2_co_pwritev_compressed_task(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 4468 | uint64_t offset, uint64_t bytes, |
| 4469 | QEMUIOVector *qiov, size_t qiov_offset) |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4470 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 4471 | BDRVQcow2State *s = bs->opaque; |
Vladimir Sementsov-Ogievskiy | 2714f13 | 2018-06-20 17:48:36 +0300 | [diff] [blame] | 4472 | int ret; |
Alberto Garcia | e1f4a37 | 2019-04-30 13:08:02 +0300 | [diff] [blame] | 4473 | ssize_t out_len; |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4474 | uint8_t *buf, *out_buf; |
Kevin Wolf | 77e023f | 2019-02-27 10:26:24 +0100 | [diff] [blame] | 4475 | uint64_t cluster_offset; |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4476 | |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4477 | assert(bytes == s->cluster_size || (bytes < s->cluster_size && |
| 4478 | (offset + bytes == bs->total_sectors << BDRV_SECTOR_BITS))); |
Anton Nefedov | 3e3b838 | 2017-11-14 13:16:49 +0300 | [diff] [blame] | 4479 | |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4480 | buf = qemu_blockalign(bs, s->cluster_size); |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4481 | if (bytes < s->cluster_size) { |
Pavel Butsykin | a2c0ca6 | 2016-07-22 11:17:44 +0300 | [diff] [blame] | 4482 | /* Zero-pad last write if image size is not cluster aligned */ |
| 4483 | memset(buf + bytes, 0, s->cluster_size - bytes); |
| 4484 | } |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 4485 | qemu_iovec_to_buf(qiov, qiov_offset, buf, bytes); |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4486 | |
Vladimir Sementsov-Ogievskiy | ebf7bba | 2016-07-14 19:59:25 +0300 | [diff] [blame] | 4487 | out_buf = g_malloc(s->cluster_size); |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4488 | |
Vladimir Sementsov-Ogievskiy | 6994fd7 | 2018-11-01 21:27:33 +0300 | [diff] [blame] | 4489 | out_len = qcow2_co_compress(bs, out_buf, s->cluster_size - 1, |
| 4490 | buf, s->cluster_size); |
Alberto Garcia | e1f4a37 | 2019-04-30 13:08:02 +0300 | [diff] [blame] | 4491 | if (out_len == -ENOMEM) { |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4492 | /* could not compress: write normal cluster */ |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 4493 | ret = qcow2_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset, 0); |
Kevin Wolf | 8f1efd0 | 2011-10-18 17:12:44 +0200 | [diff] [blame] | 4494 | if (ret < 0) { |
| 4495 | goto fail; |
| 4496 | } |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4497 | goto success; |
Alberto Garcia | e1f4a37 | 2019-04-30 13:08:02 +0300 | [diff] [blame] | 4498 | } else if (out_len < 0) { |
| 4499 | ret = -EINVAL; |
| 4500 | goto fail; |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4503 | qemu_co_mutex_lock(&s->lock); |
Kevin Wolf | 77e023f | 2019-02-27 10:26:24 +0100 | [diff] [blame] | 4504 | ret = qcow2_alloc_compressed_cluster_offset(bs, offset, out_len, |
| 4505 | &cluster_offset); |
| 4506 | if (ret < 0) { |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4507 | qemu_co_mutex_unlock(&s->lock); |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4508 | goto fail; |
| 4509 | } |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4510 | |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 4511 | ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len, true); |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4512 | qemu_co_mutex_unlock(&s->lock); |
| 4513 | if (ret < 0) { |
| 4514 | goto fail; |
| 4515 | } |
| 4516 | |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 4517 | BLKDBG_EVENT(s->data_file, BLKDBG_WRITE_COMPRESSED); |
Vladimir Sementsov-Ogievskiy | b00cb15 | 2019-04-22 17:58:31 +0300 | [diff] [blame] | 4518 | ret = bdrv_co_pwrite(s->data_file, cluster_offset, out_len, out_buf, 0); |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4519 | if (ret < 0) { |
| 4520 | goto fail; |
| 4521 | } |
| 4522 | success: |
Kevin Wolf | 8f1efd0 | 2011-10-18 17:12:44 +0200 | [diff] [blame] | 4523 | ret = 0; |
| 4524 | fail: |
Pavel Butsykin | fcccefc | 2016-07-22 11:17:43 +0300 | [diff] [blame] | 4525 | qemu_vfree(buf); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 4526 | g_free(out_buf); |
Kevin Wolf | 8f1efd0 | 2011-10-18 17:12:44 +0200 | [diff] [blame] | 4527 | return ret; |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4530 | static coroutine_fn int qcow2_co_pwritev_compressed_task_entry(AioTask *task) |
| 4531 | { |
| 4532 | Qcow2AioTask *t = container_of(task, Qcow2AioTask, task); |
| 4533 | |
Alberto Garcia | 10dabdc5 | 2020-07-10 18:13:00 +0200 | [diff] [blame] | 4534 | assert(!t->subcluster_type && !t->l2meta); |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4535 | |
| 4536 | return qcow2_co_pwritev_compressed_task(t->bs, t->offset, t->bytes, t->qiov, |
| 4537 | t->qiov_offset); |
| 4538 | } |
| 4539 | |
| 4540 | /* |
| 4541 | * XXX: put compressed sectors first, then all the cluster aligned |
| 4542 | * tables to avoid losing bytes in alignment |
| 4543 | */ |
| 4544 | static coroutine_fn int |
| 4545 | qcow2_co_pwritev_compressed_part(BlockDriverState *bs, |
| 4546 | uint64_t offset, uint64_t bytes, |
| 4547 | QEMUIOVector *qiov, size_t qiov_offset) |
| 4548 | { |
| 4549 | BDRVQcow2State *s = bs->opaque; |
| 4550 | AioTaskPool *aio = NULL; |
| 4551 | int ret = 0; |
| 4552 | |
| 4553 | if (has_data_file(bs)) { |
| 4554 | return -ENOTSUP; |
| 4555 | } |
| 4556 | |
| 4557 | if (bytes == 0) { |
| 4558 | /* |
| 4559 | * align end of file to a sector boundary to ease reading with |
| 4560 | * sector based I/Os |
| 4561 | */ |
| 4562 | int64_t len = bdrv_getlength(bs->file->bs); |
| 4563 | if (len < 0) { |
| 4564 | return len; |
| 4565 | } |
Kevin Wolf | 7b8e485 | 2020-04-24 14:54:40 +0200 | [diff] [blame] | 4566 | return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, 0, |
| 4567 | NULL); |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | if (offset_into_cluster(s, offset)) { |
| 4571 | return -EINVAL; |
| 4572 | } |
| 4573 | |
Alberto Garcia | fb43d2d | 2020-04-06 16:34:01 +0200 | [diff] [blame] | 4574 | if (offset_into_cluster(s, bytes) && |
| 4575 | (offset + bytes) != (bs->total_sectors << BDRV_SECTOR_BITS)) { |
| 4576 | return -EINVAL; |
| 4577 | } |
| 4578 | |
Andrey Shinkevich | 0d483dc | 2019-12-02 15:15:05 +0300 | [diff] [blame] | 4579 | while (bytes && aio_task_pool_status(aio) == 0) { |
| 4580 | uint64_t chunk_size = MIN(bytes, s->cluster_size); |
| 4581 | |
| 4582 | if (!aio && chunk_size != bytes) { |
| 4583 | aio = aio_task_pool_new(QCOW2_MAX_WORKERS); |
| 4584 | } |
| 4585 | |
| 4586 | ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_compressed_task_entry, |
| 4587 | 0, 0, offset, chunk_size, qiov, qiov_offset, NULL); |
| 4588 | if (ret < 0) { |
| 4589 | break; |
| 4590 | } |
| 4591 | qiov_offset += chunk_size; |
| 4592 | offset += chunk_size; |
| 4593 | bytes -= chunk_size; |
| 4594 | } |
| 4595 | |
| 4596 | if (aio) { |
| 4597 | aio_task_pool_wait_all(aio); |
| 4598 | if (ret == 0) { |
| 4599 | ret = aio_task_pool_status(aio); |
| 4600 | } |
| 4601 | g_free(aio); |
| 4602 | } |
| 4603 | |
| 4604 | return ret; |
| 4605 | } |
| 4606 | |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4607 | static int coroutine_fn |
| 4608 | qcow2_co_preadv_compressed(BlockDriverState *bs, |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 4609 | uint64_t cluster_descriptor, |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4610 | uint64_t offset, |
| 4611 | uint64_t bytes, |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 4612 | QEMUIOVector *qiov, |
| 4613 | size_t qiov_offset) |
Vladimir Sementsov-Ogievskiy | f4b3e2a | 2018-11-01 21:27:34 +0300 | [diff] [blame] | 4614 | { |
| 4615 | BDRVQcow2State *s = bs->opaque; |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4616 | int ret = 0, csize, nb_csectors; |
Vladimir Sementsov-Ogievskiy | f4b3e2a | 2018-11-01 21:27:34 +0300 | [diff] [blame] | 4617 | uint64_t coffset; |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4618 | uint8_t *buf, *out_buf; |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4619 | int offset_in_cluster = offset_into_cluster(s, offset); |
Vladimir Sementsov-Ogievskiy | f4b3e2a | 2018-11-01 21:27:34 +0300 | [diff] [blame] | 4620 | |
Alberto Garcia | 9c4269d | 2020-07-10 18:12:43 +0200 | [diff] [blame] | 4621 | coffset = cluster_descriptor & s->cluster_offset_mask; |
| 4622 | nb_csectors = ((cluster_descriptor >> s->csize_shift) & s->csize_mask) + 1; |
Alberto Garcia | b6c2469 | 2019-05-10 19:22:54 +0300 | [diff] [blame] | 4623 | csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE - |
| 4624 | (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK); |
Vladimir Sementsov-Ogievskiy | f4b3e2a | 2018-11-01 21:27:34 +0300 | [diff] [blame] | 4625 | |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4626 | buf = g_try_malloc(csize); |
| 4627 | if (!buf) { |
| 4628 | return -ENOMEM; |
Vladimir Sementsov-Ogievskiy | f4b3e2a | 2018-11-01 21:27:34 +0300 | [diff] [blame] | 4629 | } |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4630 | |
| 4631 | out_buf = qemu_blockalign(bs, s->cluster_size); |
| 4632 | |
| 4633 | BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED); |
Vladimir Sementsov-Ogievskiy | b00cb15 | 2019-04-22 17:58:31 +0300 | [diff] [blame] | 4634 | ret = bdrv_co_pread(bs->file, coffset, csize, buf, 0); |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4635 | if (ret < 0) { |
| 4636 | goto fail; |
| 4637 | } |
| 4638 | |
Vladimir Sementsov-Ogievskiy | e23c9d7 | 2018-11-01 21:27:38 +0300 | [diff] [blame] | 4639 | if (qcow2_co_decompress(bs, out_buf, s->cluster_size, buf, csize) < 0) { |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4640 | ret = -EIO; |
| 4641 | goto fail; |
| 4642 | } |
| 4643 | |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 4644 | qemu_iovec_from_buf(qiov, qiov_offset, out_buf + offset_in_cluster, bytes); |
Vladimir Sementsov-Ogievskiy | c3c10f7 | 2018-11-01 21:27:37 +0300 | [diff] [blame] | 4645 | |
| 4646 | fail: |
| 4647 | qemu_vfree(out_buf); |
| 4648 | g_free(buf); |
| 4649 | |
| 4650 | return ret; |
Vladimir Sementsov-Ogievskiy | f4b3e2a | 2018-11-01 21:27:34 +0300 | [diff] [blame] | 4651 | } |
| 4652 | |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4653 | static int make_completely_empty(BlockDriverState *bs) |
| 4654 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 4655 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4656 | Error *local_err = NULL; |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4657 | int ret, l1_clusters; |
| 4658 | int64_t offset; |
| 4659 | uint64_t *new_reftable = NULL; |
| 4660 | uint64_t rt_entry, l1_size2; |
| 4661 | struct { |
| 4662 | uint64_t l1_offset; |
| 4663 | uint64_t reftable_offset; |
| 4664 | uint32_t reftable_clusters; |
| 4665 | } QEMU_PACKED l1_ofs_rt_ofs_cls; |
| 4666 | |
| 4667 | ret = qcow2_cache_empty(bs, s->l2_table_cache); |
| 4668 | if (ret < 0) { |
| 4669 | goto fail; |
| 4670 | } |
| 4671 | |
| 4672 | ret = qcow2_cache_empty(bs, s->refcount_block_cache); |
| 4673 | if (ret < 0) { |
| 4674 | goto fail; |
| 4675 | } |
| 4676 | |
| 4677 | /* Refcounts will be broken utterly */ |
| 4678 | ret = qcow2_mark_dirty(bs); |
| 4679 | if (ret < 0) { |
| 4680 | goto fail; |
| 4681 | } |
| 4682 | |
| 4683 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); |
| 4684 | |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 4685 | l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / L1E_SIZE); |
| 4686 | l1_size2 = (uint64_t)s->l1_size * L1E_SIZE; |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4687 | |
| 4688 | /* After this call, neither the in-memory nor the on-disk refcount |
| 4689 | * information accurately describe the actual references */ |
| 4690 | |
Kevin Wolf | 720ff28 | 2016-06-16 15:13:15 +0200 | [diff] [blame] | 4691 | ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset, |
Eric Blake | 74021bc | 2016-06-01 15:10:04 -0600 | [diff] [blame] | 4692 | l1_clusters * s->cluster_size, 0); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4693 | if (ret < 0) { |
| 4694 | goto fail_broken_refcounts; |
| 4695 | } |
| 4696 | memset(s->l1_table, 0, l1_size2); |
| 4697 | |
| 4698 | BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE); |
| 4699 | |
| 4700 | /* Overwrite enough clusters at the beginning of the sectors to place |
| 4701 | * the refcount table, a refcount block and the L1 table in; this may |
| 4702 | * overwrite parts of the existing refcount and L1 table, which is not |
| 4703 | * an issue because the dirty flag is set, complete data loss is in fact |
| 4704 | * desired and partial data loss is consequently fine as well */ |
Kevin Wolf | 720ff28 | 2016-06-16 15:13:15 +0200 | [diff] [blame] | 4705 | ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size, |
Eric Blake | 74021bc | 2016-06-01 15:10:04 -0600 | [diff] [blame] | 4706 | (2 + l1_clusters) * s->cluster_size, 0); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4707 | /* This call (even if it failed overall) may have overwritten on-disk |
| 4708 | * refcount structures; in that case, the in-memory refcount information |
| 4709 | * will probably differ from the on-disk information which makes the BDS |
| 4710 | * unusable */ |
| 4711 | if (ret < 0) { |
| 4712 | goto fail_broken_refcounts; |
| 4713 | } |
| 4714 | |
| 4715 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); |
| 4716 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE); |
| 4717 | |
| 4718 | /* "Create" an empty reftable (one cluster) directly after the image |
| 4719 | * header and an empty L1 table three clusters after the image header; |
| 4720 | * the cluster between those two will be used as the first refblock */ |
Peter Maydell | f1f7a1d | 2016-06-16 17:06:17 +0100 | [diff] [blame] | 4721 | l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size); |
| 4722 | l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size); |
| 4723 | l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 4724 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset), |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4725 | &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls)); |
| 4726 | if (ret < 0) { |
| 4727 | goto fail_broken_refcounts; |
| 4728 | } |
| 4729 | |
| 4730 | s->l1_table_offset = 3 * s->cluster_size; |
| 4731 | |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 4732 | new_reftable = g_try_new0(uint64_t, s->cluster_size / REFTABLE_ENTRY_SIZE); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4733 | if (!new_reftable) { |
| 4734 | ret = -ENOMEM; |
| 4735 | goto fail_broken_refcounts; |
| 4736 | } |
| 4737 | |
| 4738 | s->refcount_table_offset = s->cluster_size; |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 4739 | s->refcount_table_size = s->cluster_size / REFTABLE_ENTRY_SIZE; |
Alberto Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 4740 | s->max_refcount_table_index = 0; |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4741 | |
| 4742 | g_free(s->refcount_table); |
| 4743 | s->refcount_table = new_reftable; |
| 4744 | new_reftable = NULL; |
| 4745 | |
| 4746 | /* Now the in-memory refcount information again corresponds to the on-disk |
| 4747 | * information (reftable is empty and no refblocks (the refblock cache is |
| 4748 | * empty)); however, this means some clusters (e.g. the image header) are |
| 4749 | * referenced, but not refcounted, but the normal qcow2 code assumes that |
| 4750 | * the in-memory information is always correct */ |
| 4751 | |
| 4752 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC); |
| 4753 | |
| 4754 | /* Enter the first refblock into the reftable */ |
| 4755 | rt_entry = cpu_to_be64(2 * s->cluster_size); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 4756 | ret = bdrv_pwrite_sync(bs->file, s->cluster_size, |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4757 | &rt_entry, sizeof(rt_entry)); |
| 4758 | if (ret < 0) { |
| 4759 | goto fail_broken_refcounts; |
| 4760 | } |
| 4761 | s->refcount_table[0] = 2 * s->cluster_size; |
| 4762 | |
| 4763 | s->free_cluster_index = 0; |
| 4764 | assert(3 + l1_clusters <= s->refcount_block_size); |
| 4765 | offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2); |
| 4766 | if (offset < 0) { |
| 4767 | ret = offset; |
| 4768 | goto fail_broken_refcounts; |
| 4769 | } else if (offset > 0) { |
| 4770 | error_report("First cluster in emptied image is in use"); |
| 4771 | abort(); |
| 4772 | } |
| 4773 | |
| 4774 | /* Now finally the in-memory information corresponds to the on-disk |
| 4775 | * structures and is correct */ |
| 4776 | ret = qcow2_mark_clean(bs); |
| 4777 | if (ret < 0) { |
| 4778 | goto fail; |
| 4779 | } |
| 4780 | |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 4781 | ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false, |
Kevin Wolf | 7b8e485 | 2020-04-24 14:54:40 +0200 | [diff] [blame] | 4782 | PREALLOC_MODE_OFF, 0, &local_err); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4783 | if (ret < 0) { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4784 | error_report_err(local_err); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4785 | goto fail; |
| 4786 | } |
| 4787 | |
| 4788 | return 0; |
| 4789 | |
| 4790 | fail_broken_refcounts: |
| 4791 | /* The BDS is unusable at this point. If we wanted to make it usable, we |
| 4792 | * would have to call qcow2_refcount_close(), qcow2_refcount_init(), |
| 4793 | * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init() |
| 4794 | * again. However, because the functions which could have caused this error |
| 4795 | * path to be taken are used by those functions as well, it's very likely |
| 4796 | * that that sequence will fail as well. Therefore, just eject the BDS. */ |
| 4797 | bs->drv = NULL; |
| 4798 | |
| 4799 | fail: |
| 4800 | g_free(new_reftable); |
| 4801 | return ret; |
| 4802 | } |
| 4803 | |
Max Reitz | 491d27e | 2014-10-24 15:57:31 +0200 | [diff] [blame] | 4804 | static int qcow2_make_empty(BlockDriverState *bs) |
| 4805 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 4806 | BDRVQcow2State *s = bs->opaque; |
Eric Blake | d2cb36a | 2017-05-06 19:05:52 -0500 | [diff] [blame] | 4807 | uint64_t offset, end_offset; |
| 4808 | int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4809 | int l1_clusters, ret = 0; |
Max Reitz | 491d27e | 2014-10-24 15:57:31 +0200 | [diff] [blame] | 4810 | |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 4811 | l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / L1E_SIZE); |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4812 | |
Eric Blake | 4096974 | 2017-11-17 10:47:47 -0600 | [diff] [blame] | 4813 | if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps && |
Daniel P. Berrange | f060332 | 2017-11-17 11:29:13 +0000 | [diff] [blame] | 4814 | 3 + l1_clusters <= s->refcount_block_size && |
Kevin Wolf | db04524 | 2019-04-29 12:52:21 +0200 | [diff] [blame] | 4815 | s->crypt_method_header != QCOW_CRYPT_LUKS && |
| 4816 | !has_data_file(bs)) { |
Eric Blake | 4096974 | 2017-11-17 10:47:47 -0600 | [diff] [blame] | 4817 | /* The following function only works for qcow2 v3 images (it |
| 4818 | * requires the dirty flag) and only as long as there are no |
| 4819 | * features that reserve extra clusters (such as snapshots, |
| 4820 | * LUKS header, or persistent bitmaps), because it completely |
| 4821 | * empties the image. Furthermore, the L1 table and three |
| 4822 | * additional clusters (image header, refcount table, one |
Kevin Wolf | db04524 | 2019-04-29 12:52:21 +0200 | [diff] [blame] | 4823 | * refcount block) have to fit inside one refcount block. It |
| 4824 | * only resets the image file, i.e. does not work with an |
| 4825 | * external data file. */ |
Max Reitz | 9405418 | 2014-10-24 15:57:32 +0200 | [diff] [blame] | 4826 | return make_completely_empty(bs); |
| 4827 | } |
| 4828 | |
| 4829 | /* This fallback code simply discards every active cluster; this is slow, |
| 4830 | * but works in all cases */ |
Eric Blake | d2cb36a | 2017-05-06 19:05:52 -0500 | [diff] [blame] | 4831 | end_offset = bs->total_sectors * BDRV_SECTOR_SIZE; |
| 4832 | for (offset = 0; offset < end_offset; offset += step) { |
Max Reitz | 491d27e | 2014-10-24 15:57:31 +0200 | [diff] [blame] | 4833 | /* As this function is generally used after committing an external |
| 4834 | * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the |
| 4835 | * default action for this kind of discard is to pass the discard, |
| 4836 | * which will ideally result in an actually smaller image file, as |
| 4837 | * is probably desired. */ |
Eric Blake | d2cb36a | 2017-05-06 19:05:52 -0500 | [diff] [blame] | 4838 | ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset), |
| 4839 | QCOW2_DISCARD_SNAPSHOT, true); |
Max Reitz | 491d27e | 2014-10-24 15:57:31 +0200 | [diff] [blame] | 4840 | if (ret < 0) { |
| 4841 | break; |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | return ret; |
| 4846 | } |
| 4847 | |
Dong Xu Wang | a968168 | 2011-11-10 16:23:22 +0800 | [diff] [blame] | 4848 | static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 4849 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 4850 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 4851 | int ret; |
| 4852 | |
Paolo Bonzini | 8b94ff8 | 2011-10-20 13:16:24 +0200 | [diff] [blame] | 4853 | qemu_co_mutex_lock(&s->lock); |
Paolo Bonzini | 8b220eb | 2018-03-01 17:36:14 +0100 | [diff] [blame] | 4854 | ret = qcow2_write_caches(bs); |
Paolo Bonzini | 8b94ff8 | 2011-10-20 13:16:24 +0200 | [diff] [blame] | 4855 | qemu_co_mutex_unlock(&s->lock); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 4856 | |
Paolo Bonzini | 8b220eb | 2018-03-01 17:36:14 +0100 | [diff] [blame] | 4857 | return ret; |
Kevin Wolf | eb489bb | 2011-11-10 18:10:11 +0100 | [diff] [blame] | 4858 | } |
| 4859 | |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4860 | static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs, |
| 4861 | Error **errp) |
| 4862 | { |
| 4863 | Error *local_err = NULL; |
| 4864 | BlockMeasureInfo *info; |
| 4865 | uint64_t required = 0; /* bytes that contribute to required size */ |
| 4866 | uint64_t virtual_size; /* disk size as seen by guest */ |
| 4867 | uint64_t refcount_bits; |
| 4868 | uint64_t l2_tables; |
Stefan Hajnoczi | 61914f8 | 2019-02-18 10:45:24 +0000 | [diff] [blame] | 4869 | uint64_t luks_payload_size = 0; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4870 | size_t cluster_size; |
| 4871 | int version; |
| 4872 | char *optstr; |
| 4873 | PreallocMode prealloc; |
| 4874 | bool has_backing_file; |
Stefan Hajnoczi | 61914f8 | 2019-02-18 10:45:24 +0000 | [diff] [blame] | 4875 | bool has_luks; |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 4876 | bool extended_l2; |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 4877 | size_t l2e_size; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4878 | |
| 4879 | /* Parse image creation options */ |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 4880 | extended_l2 = qemu_opt_get_bool_del(opts, BLOCK_OPT_EXTL2, false); |
| 4881 | |
| 4882 | cluster_size = qcow2_opt_get_cluster_size_del(opts, extended_l2, |
| 4883 | &local_err); |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4884 | if (local_err) { |
| 4885 | goto err; |
| 4886 | } |
| 4887 | |
| 4888 | version = qcow2_opt_get_version_del(opts, &local_err); |
| 4889 | if (local_err) { |
| 4890 | goto err; |
| 4891 | } |
| 4892 | |
| 4893 | refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err); |
| 4894 | if (local_err) { |
| 4895 | goto err; |
| 4896 | } |
| 4897 | |
| 4898 | optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 4899 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 4900 | PREALLOC_MODE_OFF, &local_err); |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4901 | g_free(optstr); |
| 4902 | if (local_err) { |
| 4903 | goto err; |
| 4904 | } |
| 4905 | |
| 4906 | optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); |
| 4907 | has_backing_file = !!optstr; |
| 4908 | g_free(optstr); |
| 4909 | |
Stefan Hajnoczi | 61914f8 | 2019-02-18 10:45:24 +0000 | [diff] [blame] | 4910 | optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
| 4911 | has_luks = optstr && strcmp(optstr, "luks") == 0; |
| 4912 | g_free(optstr); |
| 4913 | |
| 4914 | if (has_luks) { |
Stefan Hajnoczi | 6d49d3a | 2020-02-21 11:25:19 +0000 | [diff] [blame] | 4915 | g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL; |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 4916 | QDict *cryptoopts = qcow2_extract_crypto_opts(opts, "luks", errp); |
Stefan Hajnoczi | 61914f8 | 2019-02-18 10:45:24 +0000 | [diff] [blame] | 4917 | size_t headerlen; |
| 4918 | |
Stefan Hajnoczi | 6d49d3a | 2020-02-21 11:25:19 +0000 | [diff] [blame] | 4919 | create_opts = block_crypto_create_opts_init(cryptoopts, errp); |
| 4920 | qobject_unref(cryptoopts); |
| 4921 | if (!create_opts) { |
| 4922 | goto err; |
| 4923 | } |
| 4924 | |
| 4925 | if (!qcrypto_block_calculate_payload_offset(create_opts, |
| 4926 | "encrypt.", |
| 4927 | &headerlen, |
| 4928 | &local_err)) { |
Stefan Hajnoczi | 61914f8 | 2019-02-18 10:45:24 +0000 | [diff] [blame] | 4929 | goto err; |
| 4930 | } |
| 4931 | |
| 4932 | luks_payload_size = ROUND_UP(headerlen, cluster_size); |
| 4933 | } |
| 4934 | |
Alberto Garcia | 9e02968 | 2018-02-15 15:10:08 +0200 | [diff] [blame] | 4935 | virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); |
| 4936 | virtual_size = ROUND_UP(virtual_size, cluster_size); |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4937 | |
| 4938 | /* Check that virtual disk size is valid */ |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 4939 | l2e_size = extended_l2 ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4940 | l2_tables = DIV_ROUND_UP(virtual_size / cluster_size, |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 4941 | cluster_size / l2e_size); |
Alberto Garcia | 02b1ecf | 2020-08-28 13:08:28 +0200 | [diff] [blame] | 4942 | if (l2_tables * L1E_SIZE > QCOW_MAX_L1_SIZE) { |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4943 | error_setg(&local_err, "The image size is too large " |
| 4944 | "(try using a larger cluster size)"); |
| 4945 | goto err; |
| 4946 | } |
| 4947 | |
| 4948 | /* Account for input image */ |
| 4949 | if (in_bs) { |
| 4950 | int64_t ssize = bdrv_getlength(in_bs); |
| 4951 | if (ssize < 0) { |
| 4952 | error_setg_errno(&local_err, -ssize, |
| 4953 | "Unable to get image virtual_size"); |
| 4954 | goto err; |
| 4955 | } |
| 4956 | |
Alberto Garcia | 9e02968 | 2018-02-15 15:10:08 +0200 | [diff] [blame] | 4957 | virtual_size = ROUND_UP(ssize, cluster_size); |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4958 | |
| 4959 | if (has_backing_file) { |
| 4960 | /* We don't how much of the backing chain is shared by the input |
| 4961 | * image and the new image file. In the worst case the new image's |
| 4962 | * backing file has nothing in common with the input image. Be |
| 4963 | * conservative and assume all clusters need to be written. |
| 4964 | */ |
| 4965 | required = virtual_size; |
| 4966 | } else { |
Eric Blake | b85ee45 | 2017-09-25 09:55:22 -0500 | [diff] [blame] | 4967 | int64_t offset; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 4968 | int64_t pnum = 0; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4969 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 4970 | for (offset = 0; offset < ssize; offset += pnum) { |
| 4971 | int ret; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4972 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 4973 | ret = bdrv_block_status_above(in_bs, NULL, offset, |
| 4974 | ssize - offset, &pnum, NULL, |
| 4975 | NULL); |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4976 | if (ret < 0) { |
| 4977 | error_setg_errno(&local_err, -ret, |
| 4978 | "Unable to get block status"); |
| 4979 | goto err; |
| 4980 | } |
| 4981 | |
| 4982 | if (ret & BDRV_BLOCK_ZERO) { |
| 4983 | /* Skip zero regions (safe with no backing file) */ |
| 4984 | } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) == |
| 4985 | (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) { |
| 4986 | /* Extend pnum to end of cluster for next iteration */ |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 4987 | pnum = ROUND_UP(offset + pnum, cluster_size) - offset; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4988 | |
| 4989 | /* Count clusters we've seen */ |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 4990 | required += offset % cluster_size + pnum; |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 4991 | } |
| 4992 | } |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | /* Take into account preallocation. Nothing special is needed for |
| 4997 | * PREALLOC_MODE_METADATA since metadata is always counted. |
| 4998 | */ |
| 4999 | if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) { |
| 5000 | required = virtual_size; |
| 5001 | } |
| 5002 | |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5003 | info = g_new0(BlockMeasureInfo, 1); |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 5004 | info->fully_allocated = luks_payload_size + |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 5005 | qcow2_calc_prealloc_size(virtual_size, cluster_size, |
Alberto Garcia | 0dd07b2 | 2020-07-10 18:13:11 +0200 | [diff] [blame] | 5006 | ctz32(refcount_bits), extended_l2); |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 5007 | |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5008 | /* |
| 5009 | * Remove data clusters that are not required. This overestimates the |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 5010 | * required size because metadata needed for the fully allocated file is |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5011 | * still counted. Show bitmaps only if both source and destination |
| 5012 | * would support them. |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 5013 | */ |
| 5014 | info->required = info->fully_allocated - virtual_size + required; |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5015 | info->has_bitmaps = version >= 3 && in_bs && |
| 5016 | bdrv_supports_persistent_dirty_bitmap(in_bs); |
| 5017 | if (info->has_bitmaps) { |
| 5018 | info->bitmaps = qcow2_get_persistent_dirty_bitmap_size(in_bs, |
| 5019 | cluster_size); |
| 5020 | } |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 5021 | return info; |
| 5022 | |
| 5023 | err: |
| 5024 | error_propagate(errp, local_err); |
| 5025 | return NULL; |
| 5026 | } |
| 5027 | |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 5028 | static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5029 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5030 | BDRVQcow2State *s = bs->opaque; |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5031 | bdi->cluster_size = s->cluster_size; |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 5032 | bdi->vm_state_offset = qcow2_vm_state_offset(s); |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5033 | return 0; |
| 5034 | } |
| 5035 | |
Andrey Shinkevich | 1bf6e9c | 2019-02-08 18:06:06 +0300 | [diff] [blame] | 5036 | static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs, |
| 5037 | Error **errp) |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5038 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5039 | BDRVQcow2State *s = bs->opaque; |
Daniel P. Berrange | 0a12f6f | 2017-06-23 17:24:18 +0100 | [diff] [blame] | 5040 | ImageInfoSpecific *spec_info; |
| 5041 | QCryptoBlockInfo *encrypt_info = NULL; |
Andrey Shinkevich | 1bf6e9c | 2019-02-08 18:06:06 +0300 | [diff] [blame] | 5042 | Error *local_err = NULL; |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5043 | |
Daniel P. Berrange | 0a12f6f | 2017-06-23 17:24:18 +0100 | [diff] [blame] | 5044 | if (s->crypto != NULL) { |
Andrey Shinkevich | 1bf6e9c | 2019-02-08 18:06:06 +0300 | [diff] [blame] | 5045 | encrypt_info = qcrypto_block_get_info(s->crypto, &local_err); |
| 5046 | if (local_err) { |
| 5047 | error_propagate(errp, local_err); |
| 5048 | return NULL; |
| 5049 | } |
Daniel P. Berrange | 0a12f6f | 2017-06-23 17:24:18 +0100 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | spec_info = g_new(ImageInfoSpecific, 1); |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5053 | *spec_info = (ImageInfoSpecific){ |
Eric Blake | 6a8f966 | 2015-10-26 16:34:54 -0600 | [diff] [blame] | 5054 | .type = IMAGE_INFO_SPECIFIC_KIND_QCOW2, |
Andrey Shinkevich | b8968c8 | 2019-02-08 18:06:07 +0300 | [diff] [blame] | 5055 | .u.qcow2.data = g_new0(ImageInfoSpecificQCow2, 1), |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5056 | }; |
| 5057 | if (s->qcow_version == 2) { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 5058 | *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){ |
Max Reitz | 0709c5a | 2015-02-10 15:28:44 -0500 | [diff] [blame] | 5059 | .compat = g_strdup("0.10"), |
| 5060 | .refcount_bits = s->refcount_bits, |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5061 | }; |
| 5062 | } else if (s->qcow_version == 3) { |
Andrey Shinkevich | b8968c8 | 2019-02-08 18:06:07 +0300 | [diff] [blame] | 5063 | Qcow2BitmapInfoList *bitmaps; |
| 5064 | bitmaps = qcow2_get_bitmap_info_list(bs, &local_err); |
| 5065 | if (local_err) { |
| 5066 | error_propagate(errp, local_err); |
| 5067 | qapi_free_ImageInfoSpecific(spec_info); |
Eric Blake | 71eaec2 | 2020-03-20 13:36:20 -0500 | [diff] [blame] | 5068 | qapi_free_QCryptoBlockInfo(encrypt_info); |
Andrey Shinkevich | b8968c8 | 2019-02-08 18:06:07 +0300 | [diff] [blame] | 5069 | return NULL; |
| 5070 | } |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 5071 | *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){ |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5072 | .compat = g_strdup("1.1"), |
| 5073 | .lazy_refcounts = s->compatible_features & |
| 5074 | QCOW2_COMPAT_LAZY_REFCOUNTS, |
| 5075 | .has_lazy_refcounts = true, |
Max Reitz | 9009b19 | 2014-09-30 21:31:28 +0200 | [diff] [blame] | 5076 | .corrupt = s->incompatible_features & |
| 5077 | QCOW2_INCOMPAT_CORRUPT, |
| 5078 | .has_corrupt = true, |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 5079 | .has_extended_l2 = true, |
| 5080 | .extended_l2 = has_subclusters(s), |
Max Reitz | 0709c5a | 2015-02-10 15:28:44 -0500 | [diff] [blame] | 5081 | .refcount_bits = s->refcount_bits, |
Andrey Shinkevich | b8968c8 | 2019-02-08 18:06:07 +0300 | [diff] [blame] | 5082 | .has_bitmaps = !!bitmaps, |
| 5083 | .bitmaps = bitmaps, |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 5084 | .has_data_file = !!s->image_data_file, |
| 5085 | .data_file = g_strdup(s->image_data_file), |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 5086 | .has_data_file_raw = has_data_file(bs), |
| 5087 | .data_file_raw = data_file_is_raw(bs), |
Denis Plotnikov | 572ad97 | 2020-05-07 11:25:18 +0300 | [diff] [blame] | 5088 | .compression_type = s->compression_type, |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5089 | }; |
Denis V. Lunev | b1fc8f9 | 2015-12-10 12:55:48 +0300 | [diff] [blame] | 5090 | } else { |
| 5091 | /* if this assertion fails, this probably means a new version was |
| 5092 | * added without having it covered here */ |
| 5093 | assert(false); |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5094 | } |
| 5095 | |
Daniel P. Berrange | 0a12f6f | 2017-06-23 17:24:18 +0100 | [diff] [blame] | 5096 | if (encrypt_info) { |
| 5097 | ImageInfoSpecificQCow2Encryption *qencrypt = |
| 5098 | g_new(ImageInfoSpecificQCow2Encryption, 1); |
| 5099 | switch (encrypt_info->format) { |
| 5100 | case Q_CRYPTO_BLOCK_FORMAT_QCOW: |
| 5101 | qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES; |
Daniel P. Berrange | 0a12f6f | 2017-06-23 17:24:18 +0100 | [diff] [blame] | 5102 | break; |
| 5103 | case Q_CRYPTO_BLOCK_FORMAT_LUKS: |
| 5104 | qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS; |
| 5105 | qencrypt->u.luks = encrypt_info->u.luks; |
| 5106 | break; |
| 5107 | default: |
| 5108 | abort(); |
| 5109 | } |
| 5110 | /* Since we did shallow copy above, erase any pointers |
| 5111 | * in the original info */ |
| 5112 | memset(&encrypt_info->u, 0, sizeof(encrypt_info->u)); |
| 5113 | qapi_free_QCryptoBlockInfo(encrypt_info); |
| 5114 | |
| 5115 | spec_info->u.qcow2.data->has_encrypt = true; |
| 5116 | spec_info->u.qcow2.data->encrypt = qencrypt; |
| 5117 | } |
| 5118 | |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5119 | return spec_info; |
| 5120 | } |
| 5121 | |
Max Reitz | 38841dc | 2019-07-24 19:12:34 +0200 | [diff] [blame] | 5122 | static int qcow2_has_zero_init(BlockDriverState *bs) |
| 5123 | { |
| 5124 | BDRVQcow2State *s = bs->opaque; |
| 5125 | bool preallocated; |
| 5126 | |
| 5127 | if (qemu_in_coroutine()) { |
| 5128 | qemu_co_mutex_lock(&s->lock); |
| 5129 | } |
| 5130 | /* |
| 5131 | * Check preallocation status: Preallocated images have all L2 |
| 5132 | * tables allocated, nonpreallocated images have none. It is |
| 5133 | * therefore enough to check the first one. |
| 5134 | */ |
| 5135 | preallocated = s->l1_size > 0 && s->l1_table[0] != 0; |
| 5136 | if (qemu_in_coroutine()) { |
| 5137 | qemu_co_mutex_unlock(&s->lock); |
| 5138 | } |
| 5139 | |
| 5140 | if (!preallocated) { |
| 5141 | return 1; |
| 5142 | } else if (bs->encrypted) { |
| 5143 | return 0; |
| 5144 | } else { |
| 5145 | return bdrv_has_zero_init(s->data_file->bs); |
| 5146 | } |
| 5147 | } |
| 5148 | |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 5149 | static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
| 5150 | int64_t pos) |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5151 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5152 | BDRVQcow2State *s = bs->opaque; |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5153 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 5154 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 5155 | return bs->drv->bdrv_co_pwritev_part(bs, qcow2_vm_state_offset(s) + pos, |
| 5156 | qiov->size, qiov, 0, 0); |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
Kevin Wolf | 5ddda0b | 2016-06-09 16:50:16 +0200 | [diff] [blame] | 5159 | static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
| 5160 | int64_t pos) |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5161 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5162 | BDRVQcow2State *s = bs->opaque; |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5163 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 5164 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 5165 | return bs->drv->bdrv_co_preadv_part(bs, qcow2_vm_state_offset(s) + pos, |
| 5166 | qiov->size, qiov, 0, 0); |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5169 | /* |
| 5170 | * Downgrades an image's version. To achieve this, any incompatible features |
| 5171 | * have to be removed. |
| 5172 | */ |
Max Reitz | 4057a2b | 2014-10-27 11:12:53 +0100 | [diff] [blame] | 5173 | static int qcow2_downgrade(BlockDriverState *bs, int target_version, |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5174 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, |
| 5175 | Error **errp) |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5176 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5177 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5178 | int current_version = s->qcow_version; |
| 5179 | int ret; |
Eric Blake | 7fa140a | 2020-04-28 14:26:47 -0500 | [diff] [blame] | 5180 | int i; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5181 | |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5182 | /* This is qcow2_downgrade(), not qcow2_upgrade() */ |
| 5183 | assert(target_version < current_version); |
| 5184 | |
| 5185 | /* There are no other versions (now) that you can downgrade to */ |
| 5186 | assert(target_version == 2); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5187 | |
| 5188 | if (s->refcount_order != 4) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5189 | error_setg(errp, "compat=0.10 requires refcount_bits=16"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5190 | return -ENOTSUP; |
| 5191 | } |
| 5192 | |
Kevin Wolf | 966b000 | 2019-01-15 20:39:06 +0100 | [diff] [blame] | 5193 | if (has_data_file(bs)) { |
| 5194 | error_setg(errp, "Cannot downgrade an image with a data file"); |
| 5195 | return -ENOTSUP; |
| 5196 | } |
| 5197 | |
Eric Blake | 7fa140a | 2020-04-28 14:26:47 -0500 | [diff] [blame] | 5198 | /* |
| 5199 | * If any internal snapshot has a different size than the current |
| 5200 | * image size, or VM state size that exceeds 32 bits, downgrading |
| 5201 | * is unsafe. Even though we would still use v3-compliant output |
| 5202 | * to preserve that data, other v2 programs might not realize |
| 5203 | * those optional fields are important. |
| 5204 | */ |
| 5205 | for (i = 0; i < s->nb_snapshots; i++) { |
| 5206 | if (s->snapshots[i].vm_state_size > UINT32_MAX || |
| 5207 | s->snapshots[i].disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) { |
| 5208 | error_setg(errp, "Internal snapshots prevent downgrade of image"); |
| 5209 | return -ENOTSUP; |
| 5210 | } |
| 5211 | } |
| 5212 | |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5213 | /* clear incompatible features */ |
| 5214 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { |
| 5215 | ret = qcow2_mark_clean(bs); |
| 5216 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5217 | error_setg_errno(errp, -ret, "Failed to make the image clean"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5218 | return ret; |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in |
| 5223 | * the first place; if that happens nonetheless, returning -ENOTSUP is the |
| 5224 | * best thing to do anyway */ |
| 5225 | |
| 5226 | if (s->incompatible_features) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5227 | error_setg(errp, "Cannot downgrade an image with incompatible features " |
| 5228 | "%#" PRIx64 " set", s->incompatible_features); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5229 | return -ENOTSUP; |
| 5230 | } |
| 5231 | |
| 5232 | /* since we can ignore compatible features, we can set them to 0 as well */ |
| 5233 | s->compatible_features = 0; |
| 5234 | /* if lazy refcounts have been used, they have already been fixed through |
| 5235 | * clearing the dirty flag */ |
| 5236 | |
| 5237 | /* clearing autoclear features is trivial */ |
| 5238 | s->autoclear_features = 0; |
| 5239 | |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 5240 | ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5241 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5242 | error_setg_errno(errp, -ret, "Failed to turn zero into data clusters"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5243 | return ret; |
| 5244 | } |
| 5245 | |
| 5246 | s->qcow_version = target_version; |
| 5247 | ret = qcow2_update_header(bs); |
| 5248 | if (ret < 0) { |
| 5249 | s->qcow_version = current_version; |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5250 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5251 | return ret; |
| 5252 | } |
| 5253 | return 0; |
| 5254 | } |
| 5255 | |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5256 | /* |
| 5257 | * Upgrades an image's version. While newer versions encompass all |
| 5258 | * features of older versions, some things may have to be presented |
| 5259 | * differently. |
| 5260 | */ |
| 5261 | static int qcow2_upgrade(BlockDriverState *bs, int target_version, |
| 5262 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, |
| 5263 | Error **errp) |
| 5264 | { |
| 5265 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 0a85af3 | 2019-10-11 17:28:05 +0200 | [diff] [blame] | 5266 | bool need_snapshot_update; |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5267 | int current_version = s->qcow_version; |
Max Reitz | 0a85af3 | 2019-10-11 17:28:05 +0200 | [diff] [blame] | 5268 | int i; |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5269 | int ret; |
| 5270 | |
| 5271 | /* This is qcow2_upgrade(), not qcow2_downgrade() */ |
| 5272 | assert(target_version > current_version); |
| 5273 | |
| 5274 | /* There are no other versions (yet) that you can upgrade to */ |
| 5275 | assert(target_version == 3); |
| 5276 | |
Max Reitz | 0a85af3 | 2019-10-11 17:28:05 +0200 | [diff] [blame] | 5277 | status_cb(bs, 0, 2, cb_opaque); |
| 5278 | |
| 5279 | /* |
| 5280 | * In v2, snapshots do not need to have extra data. v3 requires |
| 5281 | * the 64-bit VM state size and the virtual disk size to be |
| 5282 | * present. |
| 5283 | * qcow2_write_snapshots() will always write the list in the |
| 5284 | * v3-compliant format. |
| 5285 | */ |
| 5286 | need_snapshot_update = false; |
| 5287 | for (i = 0; i < s->nb_snapshots; i++) { |
| 5288 | if (s->snapshots[i].extra_data_size < |
| 5289 | sizeof_field(QCowSnapshotExtraData, vm_state_size_large) + |
| 5290 | sizeof_field(QCowSnapshotExtraData, disk_size)) |
| 5291 | { |
| 5292 | need_snapshot_update = true; |
| 5293 | break; |
| 5294 | } |
| 5295 | } |
| 5296 | if (need_snapshot_update) { |
| 5297 | ret = qcow2_write_snapshots(bs); |
| 5298 | if (ret < 0) { |
| 5299 | error_setg_errno(errp, -ret, "Failed to update the snapshot table"); |
| 5300 | return ret; |
| 5301 | } |
| 5302 | } |
| 5303 | status_cb(bs, 1, 2, cb_opaque); |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5304 | |
| 5305 | s->qcow_version = target_version; |
| 5306 | ret = qcow2_update_header(bs); |
| 5307 | if (ret < 0) { |
| 5308 | s->qcow_version = current_version; |
| 5309 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
| 5310 | return ret; |
| 5311 | } |
Max Reitz | 0a85af3 | 2019-10-11 17:28:05 +0200 | [diff] [blame] | 5312 | status_cb(bs, 2, 2, cb_opaque); |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5313 | |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
Max Reitz | c293a80 | 2015-07-27 17:51:36 +0200 | [diff] [blame] | 5317 | typedef enum Qcow2AmendOperation { |
| 5318 | /* This is the value Qcow2AmendHelperCBInfo::last_operation will be |
| 5319 | * statically initialized to so that the helper CB can discern the first |
| 5320 | * invocation from an operation change */ |
| 5321 | QCOW2_NO_OPERATION = 0, |
| 5322 | |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5323 | QCOW2_UPGRADING, |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 5324 | QCOW2_UPDATING_ENCRYPTION, |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5325 | QCOW2_CHANGING_REFCOUNT_ORDER, |
Max Reitz | c293a80 | 2015-07-27 17:51:36 +0200 | [diff] [blame] | 5326 | QCOW2_DOWNGRADING, |
| 5327 | } Qcow2AmendOperation; |
| 5328 | |
| 5329 | typedef struct Qcow2AmendHelperCBInfo { |
| 5330 | /* The code coordinating the amend operations should only modify |
| 5331 | * these four fields; the rest will be managed by the CB */ |
| 5332 | BlockDriverAmendStatusCB *original_status_cb; |
| 5333 | void *original_cb_opaque; |
| 5334 | |
| 5335 | Qcow2AmendOperation current_operation; |
| 5336 | |
| 5337 | /* Total number of operations to perform (only set once) */ |
| 5338 | int total_operations; |
| 5339 | |
| 5340 | /* The following fields are managed by the CB */ |
| 5341 | |
| 5342 | /* Number of operations completed */ |
| 5343 | int operations_completed; |
| 5344 | |
| 5345 | /* Cumulative offset of all completed operations */ |
| 5346 | int64_t offset_completed; |
| 5347 | |
| 5348 | Qcow2AmendOperation last_operation; |
| 5349 | int64_t last_work_size; |
| 5350 | } Qcow2AmendHelperCBInfo; |
| 5351 | |
| 5352 | static void qcow2_amend_helper_cb(BlockDriverState *bs, |
| 5353 | int64_t operation_offset, |
| 5354 | int64_t operation_work_size, void *opaque) |
| 5355 | { |
| 5356 | Qcow2AmendHelperCBInfo *info = opaque; |
| 5357 | int64_t current_work_size; |
| 5358 | int64_t projected_work_size; |
| 5359 | |
| 5360 | if (info->current_operation != info->last_operation) { |
| 5361 | if (info->last_operation != QCOW2_NO_OPERATION) { |
| 5362 | info->offset_completed += info->last_work_size; |
| 5363 | info->operations_completed++; |
| 5364 | } |
| 5365 | |
| 5366 | info->last_operation = info->current_operation; |
| 5367 | } |
| 5368 | |
| 5369 | assert(info->total_operations > 0); |
| 5370 | assert(info->operations_completed < info->total_operations); |
| 5371 | |
| 5372 | info->last_work_size = operation_work_size; |
| 5373 | |
| 5374 | current_work_size = info->offset_completed + operation_work_size; |
| 5375 | |
| 5376 | /* current_work_size is the total work size for (operations_completed + 1) |
| 5377 | * operations (which includes this one), so multiply it by the number of |
| 5378 | * operations not covered and divide it by the number of operations |
| 5379 | * covered to get a projection for the operations not covered */ |
| 5380 | projected_work_size = current_work_size * (info->total_operations - |
| 5381 | info->operations_completed - 1) |
| 5382 | / (info->operations_completed + 1); |
| 5383 | |
| 5384 | info->original_status_cb(bs, info->offset_completed + operation_offset, |
| 5385 | current_work_size + projected_work_size, |
| 5386 | info->original_cb_opaque); |
| 5387 | } |
| 5388 | |
Max Reitz | 7748543 | 2014-10-27 11:12:50 +0100 | [diff] [blame] | 5389 | static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 5390 | BlockDriverAmendStatusCB *status_cb, |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5391 | void *cb_opaque, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 5392 | bool force, |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5393 | Error **errp) |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5394 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5395 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5396 | int old_version = s->qcow_version, new_version = old_version; |
| 5397 | uint64_t new_size = 0; |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 5398 | const char *backing_file = NULL, *backing_format = NULL, *data_file = NULL; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5399 | bool lazy_refcounts = s->use_lazy_refcounts; |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 5400 | bool data_file_raw = data_file_is_raw(bs); |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5401 | const char *compat = NULL; |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5402 | int refcount_bits = s->refcount_bits; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5403 | int ret; |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5404 | QemuOptDesc *desc = opts->list->desc; |
Max Reitz | c293a80 | 2015-07-27 17:51:36 +0200 | [diff] [blame] | 5405 | Qcow2AmendHelperCBInfo helper_cb_info; |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 5406 | bool encryption_update = false; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5407 | |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5408 | while (desc && desc->name) { |
| 5409 | if (!qemu_opt_find(opts, desc->name)) { |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5410 | /* only change explicitly defined options */ |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5411 | desc++; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5412 | continue; |
| 5413 | } |
| 5414 | |
Max Reitz | 8a17b83 | 2015-02-18 17:40:47 -0500 | [diff] [blame] | 5415 | if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) { |
| 5416 | compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL); |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5417 | if (!compat) { |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5418 | /* preserve default */ |
Eric Blake | f7077c9 | 2019-07-05 10:28:12 -0500 | [diff] [blame] | 5419 | } else if (!strcmp(compat, "0.10") || !strcmp(compat, "v2")) { |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5420 | new_version = 2; |
Eric Blake | f7077c9 | 2019-07-05 10:28:12 -0500 | [diff] [blame] | 5421 | } else if (!strcmp(compat, "1.1") || !strcmp(compat, "v3")) { |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5422 | new_version = 3; |
| 5423 | } else { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5424 | error_setg(errp, "Unknown compatibility level %s", compat); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5425 | return -EINVAL; |
| 5426 | } |
Max Reitz | 8a17b83 | 2015-02-18 17:40:47 -0500 | [diff] [blame] | 5427 | } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) { |
| 5428 | new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); |
| 5429 | } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) { |
| 5430 | backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
| 5431 | } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) { |
| 5432 | backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 5433 | } else if (g_str_has_prefix(desc->name, "encrypt.")) { |
| 5434 | if (!s->crypto) { |
| 5435 | error_setg(errp, |
| 5436 | "Can't amend encryption options - encryption not present"); |
| 5437 | return -EINVAL; |
| 5438 | } |
| 5439 | if (s->crypt_method_header != QCOW_CRYPT_LUKS) { |
| 5440 | error_setg(errp, |
| 5441 | "Only LUKS encryption options can be amended"); |
| 5442 | return -ENOTSUP; |
| 5443 | } |
| 5444 | encryption_update = true; |
Max Reitz | 8a17b83 | 2015-02-18 17:40:47 -0500 | [diff] [blame] | 5445 | } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) { |
| 5446 | lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS, |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5447 | lazy_refcounts); |
Max Reitz | 06d05fa | 2015-02-18 17:40:49 -0500 | [diff] [blame] | 5448 | } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) { |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5449 | refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS, |
| 5450 | refcount_bits); |
| 5451 | |
| 5452 | if (refcount_bits <= 0 || refcount_bits > 64 || |
| 5453 | !is_power_of_2(refcount_bits)) |
| 5454 | { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5455 | error_setg(errp, "Refcount width must be a power of two and " |
| 5456 | "may not exceed 64 bits"); |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5457 | return -EINVAL; |
| 5458 | } |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 5459 | } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE)) { |
| 5460 | data_file = qemu_opt_get(opts, BLOCK_OPT_DATA_FILE); |
| 5461 | if (data_file && !has_data_file(bs)) { |
| 5462 | error_setg(errp, "data-file can only be set for images that " |
| 5463 | "use an external data file"); |
| 5464 | return -EINVAL; |
| 5465 | } |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 5466 | } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE_RAW)) { |
| 5467 | data_file_raw = qemu_opt_get_bool(opts, BLOCK_OPT_DATA_FILE_RAW, |
| 5468 | data_file_raw); |
| 5469 | if (data_file_raw && !data_file_is_raw(bs)) { |
| 5470 | error_setg(errp, "data-file-raw cannot be set on existing " |
| 5471 | "images"); |
| 5472 | return -EINVAL; |
| 5473 | } |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5474 | } else { |
Max Reitz | 164e0f8 | 2015-07-27 17:51:34 +0200 | [diff] [blame] | 5475 | /* if this point is reached, this probably means a new option was |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5476 | * added without having it covered here */ |
Max Reitz | 164e0f8 | 2015-07-27 17:51:34 +0200 | [diff] [blame] | 5477 | abort(); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5478 | } |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5479 | |
| 5480 | desc++; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5481 | } |
| 5482 | |
Max Reitz | c293a80 | 2015-07-27 17:51:36 +0200 | [diff] [blame] | 5483 | helper_cb_info = (Qcow2AmendHelperCBInfo){ |
| 5484 | .original_status_cb = status_cb, |
| 5485 | .original_cb_opaque = cb_opaque, |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5486 | .total_operations = (new_version != old_version) |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 5487 | + (s->refcount_bits != refcount_bits) + |
| 5488 | (encryption_update == true) |
Max Reitz | c293a80 | 2015-07-27 17:51:36 +0200 | [diff] [blame] | 5489 | }; |
| 5490 | |
Max Reitz | 1038bbb | 2015-07-27 17:51:35 +0200 | [diff] [blame] | 5491 | /* Upgrade first (some features may require compat=1.1) */ |
| 5492 | if (new_version > old_version) { |
Max Reitz | 722efb0 | 2019-10-11 17:28:04 +0200 | [diff] [blame] | 5493 | helper_cb_info.current_operation = QCOW2_UPGRADING; |
| 5494 | ret = qcow2_upgrade(bs, new_version, &qcow2_amend_helper_cb, |
| 5495 | &helper_cb_info, errp); |
Max Reitz | 1038bbb | 2015-07-27 17:51:35 +0200 | [diff] [blame] | 5496 | if (ret < 0) { |
Max Reitz | 1038bbb | 2015-07-27 17:51:35 +0200 | [diff] [blame] | 5497 | return ret; |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5498 | } |
| 5499 | } |
| 5500 | |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 5501 | if (encryption_update) { |
| 5502 | QDict *amend_opts_dict; |
| 5503 | QCryptoBlockAmendOptions *amend_opts; |
| 5504 | |
| 5505 | helper_cb_info.current_operation = QCOW2_UPDATING_ENCRYPTION; |
| 5506 | amend_opts_dict = qcow2_extract_crypto_opts(opts, "luks", errp); |
| 5507 | if (!amend_opts_dict) { |
| 5508 | return -EINVAL; |
| 5509 | } |
| 5510 | amend_opts = block_crypto_amend_opts_init(amend_opts_dict, errp); |
| 5511 | qobject_unref(amend_opts_dict); |
| 5512 | if (!amend_opts) { |
| 5513 | return -EINVAL; |
| 5514 | } |
| 5515 | ret = qcrypto_block_amend_options(s->crypto, |
| 5516 | qcow2_crypto_hdr_read_func, |
| 5517 | qcow2_crypto_hdr_write_func, |
| 5518 | bs, |
| 5519 | amend_opts, |
| 5520 | force, |
| 5521 | errp); |
| 5522 | qapi_free_QCryptoBlockAmendOptions(amend_opts); |
| 5523 | if (ret < 0) { |
| 5524 | return ret; |
| 5525 | } |
| 5526 | } |
| 5527 | |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5528 | if (s->refcount_bits != refcount_bits) { |
| 5529 | int refcount_order = ctz32(refcount_bits); |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5530 | |
| 5531 | if (new_version < 3 && refcount_bits != 16) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5532 | error_setg(errp, "Refcount widths other than 16 bits require " |
| 5533 | "compatibility level 1.1 or above (use compat=1.1 or " |
| 5534 | "greater)"); |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5535 | return -EINVAL; |
| 5536 | } |
| 5537 | |
| 5538 | helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER; |
| 5539 | ret = qcow2_change_refcount_order(bs, refcount_order, |
| 5540 | &qcow2_amend_helper_cb, |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5541 | &helper_cb_info, errp); |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5542 | if (ret < 0) { |
Max Reitz | 61ce55f | 2015-07-27 17:51:38 +0200 | [diff] [blame] | 5543 | return ret; |
| 5544 | } |
| 5545 | } |
| 5546 | |
Kevin Wolf | 6c3944d | 2019-02-22 14:29:38 +0100 | [diff] [blame] | 5547 | /* data-file-raw blocks backing files, so clear it first if requested */ |
| 5548 | if (data_file_raw) { |
| 5549 | s->autoclear_features |= QCOW2_AUTOCLEAR_DATA_FILE_RAW; |
| 5550 | } else { |
| 5551 | s->autoclear_features &= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW; |
| 5552 | } |
| 5553 | |
Kevin Wolf | 9b890bd | 2019-01-15 19:02:40 +0100 | [diff] [blame] | 5554 | if (data_file) { |
| 5555 | g_free(s->image_data_file); |
| 5556 | s->image_data_file = *data_file ? g_strdup(data_file) : NULL; |
| 5557 | } |
| 5558 | |
| 5559 | ret = qcow2_update_header(bs); |
| 5560 | if (ret < 0) { |
| 5561 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
| 5562 | return ret; |
| 5563 | } |
| 5564 | |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5565 | if (backing_file || backing_format) { |
Eric Blake | bc5ee6d | 2020-07-06 15:39:51 -0500 | [diff] [blame] | 5566 | if (g_strcmp0(backing_file, s->image_backing_file) || |
| 5567 | g_strcmp0(backing_format, s->image_backing_format)) { |
| 5568 | warn_report("Deprecated use of amend to alter the backing file; " |
| 5569 | "use qemu-img rebase instead"); |
| 5570 | } |
Kevin Wolf | e4603fe | 2015-04-07 15:03:16 +0200 | [diff] [blame] | 5571 | ret = qcow2_change_backing_file(bs, |
| 5572 | backing_file ?: s->image_backing_file, |
| 5573 | backing_format ?: s->image_backing_format); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5574 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5575 | error_setg_errno(errp, -ret, "Failed to change the backing file"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5576 | return ret; |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | if (s->use_lazy_refcounts != lazy_refcounts) { |
| 5581 | if (lazy_refcounts) { |
Max Reitz | 1038bbb | 2015-07-27 17:51:35 +0200 | [diff] [blame] | 5582 | if (new_version < 3) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5583 | error_setg(errp, "Lazy refcounts only supported with " |
| 5584 | "compatibility level 1.1 and above (use compat=1.1 " |
| 5585 | "or greater)"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5586 | return -EINVAL; |
| 5587 | } |
| 5588 | s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; |
| 5589 | ret = qcow2_update_header(bs); |
| 5590 | if (ret < 0) { |
| 5591 | s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5592 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5593 | return ret; |
| 5594 | } |
| 5595 | s->use_lazy_refcounts = true; |
| 5596 | } else { |
| 5597 | /* make image clean first */ |
| 5598 | ret = qcow2_mark_clean(bs); |
| 5599 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5600 | error_setg_errno(errp, -ret, "Failed to make the image clean"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5601 | return ret; |
| 5602 | } |
| 5603 | /* now disallow lazy refcounts */ |
| 5604 | s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; |
| 5605 | ret = qcow2_update_header(bs); |
| 5606 | if (ret < 0) { |
| 5607 | s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5608 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5609 | return ret; |
| 5610 | } |
| 5611 | s->use_lazy_refcounts = false; |
| 5612 | } |
| 5613 | } |
| 5614 | |
| 5615 | if (new_size) { |
Eric Blake | a3aeeab | 2020-04-28 14:26:46 -0500 | [diff] [blame] | 5616 | BlockBackend *blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, |
| 5617 | errp); |
| 5618 | if (!blk) { |
| 5619 | return -EPERM; |
Kevin Wolf | d708642 | 2017-01-13 19:02:32 +0100 | [diff] [blame] | 5620 | } |
| 5621 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 5622 | /* |
| 5623 | * Amending image options should ensure that the image has |
| 5624 | * exactly the given new values, so pass exact=true here. |
| 5625 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 5626 | ret = blk_truncate(blk, new_size, true, PREALLOC_MODE_OFF, 0, errp); |
Kevin Wolf | 70b27f3 | 2017-02-17 10:58:25 +0100 | [diff] [blame] | 5627 | blk_unref(blk); |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5628 | if (ret < 0) { |
| 5629 | return ret; |
| 5630 | } |
| 5631 | } |
| 5632 | |
Max Reitz | 1038bbb | 2015-07-27 17:51:35 +0200 | [diff] [blame] | 5633 | /* Downgrade last (so unsupported features can be removed before) */ |
| 5634 | if (new_version < old_version) { |
Max Reitz | c293a80 | 2015-07-27 17:51:36 +0200 | [diff] [blame] | 5635 | helper_cb_info.current_operation = QCOW2_DOWNGRADING; |
| 5636 | ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb, |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 5637 | &helper_cb_info, errp); |
Max Reitz | 1038bbb | 2015-07-27 17:51:35 +0200 | [diff] [blame] | 5638 | if (ret < 0) { |
| 5639 | return ret; |
| 5640 | } |
| 5641 | } |
| 5642 | |
Max Reitz | 9296b3e | 2013-09-03 10:09:54 +0200 | [diff] [blame] | 5643 | return 0; |
| 5644 | } |
| 5645 | |
Maxim Levitsky | 8ea1613 | 2020-06-25 14:55:47 +0200 | [diff] [blame] | 5646 | static int coroutine_fn qcow2_co_amend(BlockDriverState *bs, |
| 5647 | BlockdevAmendOptions *opts, |
| 5648 | bool force, |
| 5649 | Error **errp) |
| 5650 | { |
| 5651 | BlockdevAmendOptionsQcow2 *qopts = &opts->u.qcow2; |
| 5652 | BDRVQcow2State *s = bs->opaque; |
| 5653 | int ret = 0; |
| 5654 | |
| 5655 | if (qopts->has_encrypt) { |
| 5656 | if (!s->crypto) { |
| 5657 | error_setg(errp, "image is not encrypted, can't amend"); |
| 5658 | return -EOPNOTSUPP; |
| 5659 | } |
| 5660 | |
| 5661 | if (qopts->encrypt->format != Q_CRYPTO_BLOCK_FORMAT_LUKS) { |
| 5662 | error_setg(errp, |
| 5663 | "Amend can't be used to change the qcow2 encryption format"); |
| 5664 | return -EOPNOTSUPP; |
| 5665 | } |
| 5666 | |
| 5667 | if (s->crypt_method_header != QCOW_CRYPT_LUKS) { |
| 5668 | error_setg(errp, |
| 5669 | "Only LUKS encryption options can be amended for qcow2 with blockdev-amend"); |
| 5670 | return -EOPNOTSUPP; |
| 5671 | } |
| 5672 | |
| 5673 | ret = qcrypto_block_amend_options(s->crypto, |
| 5674 | qcow2_crypto_hdr_read_func, |
| 5675 | qcow2_crypto_hdr_write_func, |
| 5676 | bs, |
| 5677 | qopts->encrypt, |
| 5678 | force, |
| 5679 | errp); |
| 5680 | } |
| 5681 | return ret; |
| 5682 | } |
| 5683 | |
Max Reitz | 85186eb | 2014-09-05 16:07:16 +0200 | [diff] [blame] | 5684 | /* |
| 5685 | * If offset or size are negative, respectively, they will not be included in |
| 5686 | * the BLOCK_IMAGE_CORRUPTED event emitted. |
| 5687 | * fatal will be ignored for read-only BDS; corruptions found there will always |
| 5688 | * be considered non-fatal. |
| 5689 | */ |
| 5690 | void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset, |
| 5691 | int64_t size, const char *message_format, ...) |
| 5692 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5693 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | dc881b4 | 2015-04-08 12:29:20 +0300 | [diff] [blame] | 5694 | const char *node_name; |
Max Reitz | 85186eb | 2014-09-05 16:07:16 +0200 | [diff] [blame] | 5695 | char *message; |
| 5696 | va_list ap; |
| 5697 | |
Max Reitz | ddf3b47 | 2018-06-06 21:37:01 +0200 | [diff] [blame] | 5698 | fatal = fatal && bdrv_is_writable(bs); |
Max Reitz | 85186eb | 2014-09-05 16:07:16 +0200 | [diff] [blame] | 5699 | |
| 5700 | if (s->signaled_corruption && |
| 5701 | (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT))) |
| 5702 | { |
| 5703 | return; |
| 5704 | } |
| 5705 | |
| 5706 | va_start(ap, message_format); |
| 5707 | message = g_strdup_vprintf(message_format, ap); |
| 5708 | va_end(ap); |
| 5709 | |
| 5710 | if (fatal) { |
| 5711 | fprintf(stderr, "qcow2: Marking image as corrupt: %s; further " |
| 5712 | "corruption events will be suppressed\n", message); |
| 5713 | } else { |
| 5714 | fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal " |
| 5715 | "corruption events will be suppressed\n", message); |
| 5716 | } |
| 5717 | |
Alberto Garcia | dc881b4 | 2015-04-08 12:29:20 +0300 | [diff] [blame] | 5718 | node_name = bdrv_get_node_name(bs); |
| 5719 | qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs), |
| 5720 | *node_name != '\0', node_name, |
| 5721 | message, offset >= 0, offset, |
| 5722 | size >= 0, size, |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 5723 | fatal); |
Max Reitz | 85186eb | 2014-09-05 16:07:16 +0200 | [diff] [blame] | 5724 | g_free(message); |
| 5725 | |
| 5726 | if (fatal) { |
| 5727 | qcow2_mark_corrupt(bs); |
| 5728 | bs->drv = NULL; /* make BDS unusable */ |
| 5729 | } |
| 5730 | |
| 5731 | s->signaled_corruption = true; |
| 5732 | } |
| 5733 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 5734 | #define QCOW_COMMON_OPTIONS \ |
| 5735 | { \ |
| 5736 | .name = BLOCK_OPT_SIZE, \ |
| 5737 | .type = QEMU_OPT_SIZE, \ |
| 5738 | .help = "Virtual disk size" \ |
| 5739 | }, \ |
| 5740 | { \ |
| 5741 | .name = BLOCK_OPT_COMPAT_LEVEL, \ |
| 5742 | .type = QEMU_OPT_STRING, \ |
| 5743 | .help = "Compatibility level (v2 [0.10] or v3 [1.1])" \ |
| 5744 | }, \ |
| 5745 | { \ |
| 5746 | .name = BLOCK_OPT_BACKING_FILE, \ |
| 5747 | .type = QEMU_OPT_STRING, \ |
| 5748 | .help = "File name of a base image" \ |
| 5749 | }, \ |
| 5750 | { \ |
| 5751 | .name = BLOCK_OPT_BACKING_FMT, \ |
| 5752 | .type = QEMU_OPT_STRING, \ |
| 5753 | .help = "Image format of the base image" \ |
| 5754 | }, \ |
| 5755 | { \ |
| 5756 | .name = BLOCK_OPT_DATA_FILE, \ |
| 5757 | .type = QEMU_OPT_STRING, \ |
| 5758 | .help = "File name of an external data file" \ |
| 5759 | }, \ |
| 5760 | { \ |
| 5761 | .name = BLOCK_OPT_DATA_FILE_RAW, \ |
| 5762 | .type = QEMU_OPT_BOOL, \ |
| 5763 | .help = "The external data file must stay valid " \ |
| 5764 | "as a raw image" \ |
| 5765 | }, \ |
| 5766 | { \ |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 5767 | .name = BLOCK_OPT_LAZY_REFCOUNTS, \ |
| 5768 | .type = QEMU_OPT_BOOL, \ |
| 5769 | .help = "Postpone refcount updates", \ |
| 5770 | .def_value_str = "off" \ |
| 5771 | }, \ |
| 5772 | { \ |
| 5773 | .name = BLOCK_OPT_REFCOUNT_BITS, \ |
| 5774 | .type = QEMU_OPT_NUMBER, \ |
| 5775 | .help = "Width of a reference count entry in bits", \ |
| 5776 | .def_value_str = "16" \ |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 5777 | } |
| 5778 | |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5779 | static QemuOptsList qcow2_create_opts = { |
| 5780 | .name = "qcow2-create-opts", |
| 5781 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head), |
| 5782 | .desc = { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 5783 | { \ |
| 5784 | .name = BLOCK_OPT_ENCRYPT, \ |
| 5785 | .type = QEMU_OPT_BOOL, \ |
| 5786 | .help = "Encrypt the image with format 'aes'. (Deprecated " \ |
| 5787 | "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)", \ |
| 5788 | }, \ |
| 5789 | { \ |
| 5790 | .name = BLOCK_OPT_ENCRYPT_FORMAT, \ |
| 5791 | .type = QEMU_OPT_STRING, \ |
| 5792 | .help = "Encrypt the image, format choices: 'aes', 'luks'", \ |
| 5793 | }, \ |
| 5794 | BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.", \ |
| 5795 | "ID of secret providing qcow AES key or LUKS passphrase"), \ |
| 5796 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."), \ |
| 5797 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."), \ |
| 5798 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."), \ |
| 5799 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."), \ |
| 5800 | BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."), \ |
| 5801 | BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."), \ |
| 5802 | { \ |
| 5803 | .name = BLOCK_OPT_CLUSTER_SIZE, \ |
| 5804 | .type = QEMU_OPT_SIZE, \ |
| 5805 | .help = "qcow2 cluster size", \ |
| 5806 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE) \ |
| 5807 | }, \ |
| 5808 | { \ |
Alberto Garcia | 7be2025 | 2020-07-10 18:13:13 +0200 | [diff] [blame] | 5809 | .name = BLOCK_OPT_EXTL2, \ |
| 5810 | .type = QEMU_OPT_BOOL, \ |
| 5811 | .help = "Extended L2 tables", \ |
| 5812 | .def_value_str = "off" \ |
| 5813 | }, \ |
| 5814 | { \ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 5815 | .name = BLOCK_OPT_PREALLOC, \ |
| 5816 | .type = QEMU_OPT_STRING, \ |
| 5817 | .help = "Preallocation mode (allowed values: off, " \ |
| 5818 | "metadata, falloc, full)" \ |
| 5819 | }, \ |
| 5820 | { \ |
| 5821 | .name = BLOCK_OPT_COMPRESSION_TYPE, \ |
| 5822 | .type = QEMU_OPT_STRING, \ |
| 5823 | .help = "Compression method used for image cluster " \ |
| 5824 | "compression", \ |
| 5825 | .def_value_str = "zlib" \ |
| 5826 | }, |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 5827 | QCOW_COMMON_OPTIONS, |
| 5828 | { /* end of list */ } |
| 5829 | } |
| 5830 | }; |
| 5831 | |
| 5832 | static QemuOptsList qcow2_amend_opts = { |
| 5833 | .name = "qcow2-amend-opts", |
| 5834 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_amend_opts.head), |
| 5835 | .desc = { |
Maxim Levitsky | 90766d9 | 2020-06-25 14:55:43 +0200 | [diff] [blame] | 5836 | BLOCK_CRYPTO_OPT_DEF_LUKS_STATE("encrypt."), |
| 5837 | BLOCK_CRYPTO_OPT_DEF_LUKS_KEYSLOT("encrypt."), |
| 5838 | BLOCK_CRYPTO_OPT_DEF_LUKS_OLD_SECRET("encrypt."), |
| 5839 | BLOCK_CRYPTO_OPT_DEF_LUKS_NEW_SECRET("encrypt."), |
| 5840 | BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."), |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 5841 | QCOW_COMMON_OPTIONS, |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5842 | { /* end of list */ } |
| 5843 | } |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5844 | }; |
| 5845 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 5846 | static const char *const qcow2_strong_runtime_opts[] = { |
| 5847 | "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET, |
| 5848 | |
| 5849 | NULL |
| 5850 | }; |
| 5851 | |
Max Reitz | 5f535a9 | 2014-12-02 18:32:41 +0100 | [diff] [blame] | 5852 | BlockDriver bdrv_qcow2 = { |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 5853 | .format_name = "qcow2", |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 5854 | .instance_size = sizeof(BDRVQcow2State), |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 5855 | .bdrv_probe = qcow2_probe, |
| 5856 | .bdrv_open = qcow2_open, |
| 5857 | .bdrv_close = qcow2_close, |
Jeff Cody | 21d82ac | 2012-09-20 15:13:28 -0400 | [diff] [blame] | 5858 | .bdrv_reopen_prepare = qcow2_reopen_prepare, |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 5859 | .bdrv_reopen_commit = qcow2_reopen_commit, |
Peter Krempa | 65eb7c8 | 2020-02-28 13:44:47 +0100 | [diff] [blame] | 5860 | .bdrv_reopen_commit_post = qcow2_reopen_commit_post, |
Kevin Wolf | 5b0959a | 2015-04-16 13:42:27 +0200 | [diff] [blame] | 5861 | .bdrv_reopen_abort = qcow2_reopen_abort, |
Kevin Wolf | 5365f44 | 2015-11-16 15:34:59 +0100 | [diff] [blame] | 5862 | .bdrv_join_options = qcow2_join_options, |
Max Reitz | 69dca43 | 2020-05-13 13:05:39 +0200 | [diff] [blame] | 5863 | .bdrv_child_perm = bdrv_default_perms, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 5864 | .bdrv_co_create_opts = qcow2_co_create_opts, |
Kevin Wolf | b0292b8 | 2018-01-09 16:50:57 +0100 | [diff] [blame] | 5865 | .bdrv_co_create = qcow2_co_create, |
Max Reitz | 38841dc | 2019-07-24 19:12:34 +0200 | [diff] [blame] | 5866 | .bdrv_has_zero_init = qcow2_has_zero_init, |
Eric Blake | a320fb0 | 2018-02-13 14:26:52 -0600 | [diff] [blame] | 5867 | .bdrv_co_block_status = qcow2_co_block_status, |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5868 | |
Vladimir Sementsov-Ogievskiy | df893d2 | 2019-06-04 19:15:13 +0300 | [diff] [blame] | 5869 | .bdrv_co_preadv_part = qcow2_co_preadv_part, |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 5870 | .bdrv_co_pwritev_part = qcow2_co_pwritev_part, |
Kevin Wolf | eb489bb | 2011-11-10 18:10:11 +0100 | [diff] [blame] | 5871 | .bdrv_co_flush_to_os = qcow2_co_flush_to_os, |
Stefan Hajnoczi | 419b19d | 2010-04-28 11:36:11 +0100 | [diff] [blame] | 5872 | |
Eric Blake | 5544b59 | 2016-06-01 15:10:06 -0600 | [diff] [blame] | 5873 | .bdrv_co_pwrite_zeroes = qcow2_co_pwrite_zeroes, |
Eric Blake | 82e8a78 | 2016-07-15 17:23:03 -0600 | [diff] [blame] | 5874 | .bdrv_co_pdiscard = qcow2_co_pdiscard, |
Fam Zheng | fd9fcd3 | 2018-06-01 17:26:42 +0800 | [diff] [blame] | 5875 | .bdrv_co_copy_range_from = qcow2_co_copy_range_from, |
| 5876 | .bdrv_co_copy_range_to = qcow2_co_copy_range_to, |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 5877 | .bdrv_co_truncate = qcow2_co_truncate, |
Vladimir Sementsov-Ogievskiy | 5396234 | 2019-06-04 19:15:14 +0300 | [diff] [blame] | 5878 | .bdrv_co_pwritev_compressed_part = qcow2_co_pwritev_compressed_part, |
Max Reitz | 491d27e | 2014-10-24 15:57:31 +0200 | [diff] [blame] | 5879 | .bdrv_make_empty = qcow2_make_empty, |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5880 | |
| 5881 | .bdrv_snapshot_create = qcow2_snapshot_create, |
| 5882 | .bdrv_snapshot_goto = qcow2_snapshot_goto, |
| 5883 | .bdrv_snapshot_delete = qcow2_snapshot_delete, |
| 5884 | .bdrv_snapshot_list = qcow2_snapshot_list, |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5885 | .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, |
Stefan Hajnoczi | c501c35 | 2017-07-05 13:57:35 +0100 | [diff] [blame] | 5886 | .bdrv_measure = qcow2_measure, |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5887 | .bdrv_get_info = qcow2_get_info, |
Max Reitz | 37764df | 2013-10-09 10:46:18 +0200 | [diff] [blame] | 5888 | .bdrv_get_specific_info = qcow2_get_specific_info, |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5889 | |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 5890 | .bdrv_save_vmstate = qcow2_save_vmstate, |
| 5891 | .bdrv_load_vmstate = qcow2_load_vmstate, |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5892 | |
Max Reitz | d67066d | 2020-05-13 13:05:12 +0200 | [diff] [blame] | 5893 | .is_format = true, |
Kevin Wolf | 8ee79e7 | 2014-06-04 15:09:35 +0200 | [diff] [blame] | 5894 | .supports_backing = true, |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5895 | .bdrv_change_backing_file = qcow2_change_backing_file, |
| 5896 | |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 5897 | .bdrv_refresh_limits = qcow2_refresh_limits, |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 5898 | .bdrv_co_invalidate_cache = qcow2_co_invalidate_cache, |
Kevin Wolf | ec6d891 | 2015-12-22 16:04:57 +0100 | [diff] [blame] | 5899 | .bdrv_inactivate = qcow2_inactivate, |
Anthony Liguori | 06d9260 | 2011-11-14 15:09:46 -0600 | [diff] [blame] | 5900 | |
Chunyan Liu | 1bd0e2d | 2014-06-05 17:20:59 +0800 | [diff] [blame] | 5901 | .create_opts = &qcow2_create_opts, |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 5902 | .amend_opts = &qcow2_amend_opts, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 5903 | .strong_runtime_opts = qcow2_strong_runtime_opts, |
Alberto Garcia | 8a2ce0b | 2019-03-12 18:48:48 +0200 | [diff] [blame] | 5904 | .mutable_opts = mutable_opts, |
Paolo Bonzini | 2fd6163 | 2018-03-01 17:36:19 +0100 | [diff] [blame] | 5905 | .bdrv_co_check = qcow2_co_check, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 5906 | .bdrv_amend_options = qcow2_amend_options, |
Maxim Levitsky | 8ea1613 | 2020-06-25 14:55:47 +0200 | [diff] [blame] | 5907 | .bdrv_co_amend = qcow2_co_amend, |
Alberto Garcia | 279621c | 2015-08-04 15:14:40 +0300 | [diff] [blame] | 5908 | |
| 5909 | .bdrv_detach_aio_context = qcow2_detach_aio_context, |
| 5910 | .bdrv_attach_aio_context = qcow2_attach_aio_context, |
Vladimir Sementsov-Ogievskiy | 1b6b056 | 2017-06-28 15:05:14 +0300 | [diff] [blame] | 5911 | |
Eric Blake | ef893b5 | 2020-05-12 20:16:42 -0500 | [diff] [blame] | 5912 | .bdrv_supports_persistent_dirty_bitmap = |
| 5913 | qcow2_supports_persistent_dirty_bitmap, |
Vladimir Sementsov-Ogievskiy | d2c3080 | 2019-09-20 11:25:43 +0300 | [diff] [blame] | 5914 | .bdrv_co_can_store_new_dirty_bitmap = qcow2_co_can_store_new_dirty_bitmap, |
| 5915 | .bdrv_co_remove_persistent_dirty_bitmap = |
| 5916 | qcow2_co_remove_persistent_dirty_bitmap, |
Blue Swirl | 20d9735 | 2010-04-23 20:19:47 +0000 | [diff] [blame] | 5917 | }; |
| 5918 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 5919 | static void bdrv_qcow2_init(void) |
| 5920 | { |
| 5921 | bdrv_register(&bdrv_qcow2); |
| 5922 | } |
| 5923 | |
| 5924 | block_init(bdrv_qcow2_init); |