Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Block layer snapshot related functions |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 26 | #include "block/snapshot.h" |
| 27 | #include "block/block_int.h" |
Max Reitz | 609f45e | 2018-06-14 21:14:28 +0200 | [diff] [blame] | 28 | #include "block/qdict.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 29 | #include "qapi/error.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 30 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 31 | #include "qapi/qmp/qerror.h" |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 32 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
Kevin Wolf | 05f4ace | 2019-09-17 12:26:23 +0200 | [diff] [blame] | 34 | #include "sysemu/block-backend.h" |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 35 | |
Wenchao Xia | 8c116b0 | 2013-12-04 17:10:55 +0800 | [diff] [blame] | 36 | QemuOptsList internal_snapshot_opts = { |
| 37 | .name = "snapshot", |
| 38 | .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head), |
| 39 | .desc = { |
| 40 | { |
| 41 | .name = SNAPSHOT_OPT_ID, |
| 42 | .type = QEMU_OPT_STRING, |
| 43 | .help = "snapshot id" |
| 44 | },{ |
| 45 | .name = SNAPSHOT_OPT_NAME, |
| 46 | .type = QEMU_OPT_STRING, |
| 47 | .help = "snapshot name" |
| 48 | },{ |
| 49 | /* end of list */ |
| 50 | } |
| 51 | }, |
| 52 | }; |
| 53 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 54 | int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 55 | const char *name) |
| 56 | { |
| 57 | QEMUSnapshotInfo *sn_tab, *sn; |
| 58 | int nb_sns, i, ret; |
| 59 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 60 | GLOBAL_STATE_CODE(); |
| 61 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 62 | ret = -ENOENT; |
| 63 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 64 | if (nb_sns < 0) { |
| 65 | return ret; |
| 66 | } |
| 67 | for (i = 0; i < nb_sns; i++) { |
| 68 | sn = &sn_tab[i]; |
Daniel Henrique Barboza | 6ca0804 | 2018-11-07 11:09:58 -0200 | [diff] [blame] | 69 | if (!strcmp(sn->name, name)) { |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 70 | *sn_info = *sn; |
| 71 | ret = 0; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | g_free(sn_tab); |
| 76 | return ret; |
| 77 | } |
| 78 | |
Wenchao Xia | 2ea1dd7 | 2013-09-11 14:04:32 +0800 | [diff] [blame] | 79 | /** |
| 80 | * Look up an internal snapshot by @id and @name. |
| 81 | * @bs: block device to search |
| 82 | * @id: unique snapshot ID, or NULL |
| 83 | * @name: snapshot name, or NULL |
| 84 | * @sn_info: location to store information on the snapshot found |
| 85 | * @errp: location to store error, will be set only for exception |
| 86 | * |
| 87 | * This function will traverse snapshot list in @bs to search the matching |
| 88 | * one, @id and @name are the matching condition: |
| 89 | * If both @id and @name are specified, find the first one with id @id and |
| 90 | * name @name. |
| 91 | * If only @id is specified, find the first one with id @id. |
| 92 | * If only @name is specified, find the first one with name @name. |
| 93 | * if none is specified, abort(). |
| 94 | * |
| 95 | * Returns: true when a snapshot is found and @sn_info will be filled, false |
| 96 | * when error or not found. If all operation succeed but no matching one is |
| 97 | * found, @errp will NOT be set. |
| 98 | */ |
| 99 | bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs, |
| 100 | const char *id, |
| 101 | const char *name, |
| 102 | QEMUSnapshotInfo *sn_info, |
| 103 | Error **errp) |
| 104 | { |
| 105 | QEMUSnapshotInfo *sn_tab, *sn; |
| 106 | int nb_sns, i; |
| 107 | bool ret = false; |
| 108 | |
| 109 | assert(id || name); |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 110 | GLOBAL_STATE_CODE(); |
Wenchao Xia | 2ea1dd7 | 2013-09-11 14:04:32 +0800 | [diff] [blame] | 111 | |
| 112 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 113 | if (nb_sns < 0) { |
| 114 | error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list"); |
| 115 | return false; |
| 116 | } else if (nb_sns == 0) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (id && name) { |
| 121 | for (i = 0; i < nb_sns; i++) { |
| 122 | sn = &sn_tab[i]; |
| 123 | if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) { |
| 124 | *sn_info = *sn; |
| 125 | ret = true; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } else if (id) { |
| 130 | for (i = 0; i < nb_sns; i++) { |
| 131 | sn = &sn_tab[i]; |
| 132 | if (!strcmp(sn->id_str, id)) { |
| 133 | *sn_info = *sn; |
| 134 | ret = true; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } else if (name) { |
| 139 | for (i = 0; i < nb_sns; i++) { |
| 140 | sn = &sn_tab[i]; |
| 141 | if (!strcmp(sn->name, name)) { |
| 142 | *sn_info = *sn; |
| 143 | ret = true; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | g_free(sn_tab); |
| 150 | return ret; |
| 151 | } |
| 152 | |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 153 | /** |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 154 | * Return a pointer to child of given BDS to which we can fall |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 155 | * back if the given BDS does not support snapshots. |
| 156 | * Return NULL if there is no BDS to (safely) fall back to. |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 157 | */ |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 158 | static BdrvChild *bdrv_snapshot_fallback_child(BlockDriverState *bs) |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 159 | { |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 160 | BdrvChild *fallback = bdrv_primary_child(bs); |
| 161 | BdrvChild *child; |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 162 | |
Vladimir Sementsov-Ogievskiy | 0c6100a | 2022-07-26 23:11:27 +0300 | [diff] [blame] | 163 | /* We allow fallback only to primary child */ |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 164 | if (!fallback) { |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Check that there are no other children that would need to be |
| 170 | * snapshotted. If there are, it is not safe to fall back to |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 171 | * fallback. |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 172 | */ |
| 173 | QLIST_FOREACH(child, &bs->children, next) { |
| 174 | if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA | |
| 175 | BDRV_CHILD_FILTERED) && |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 176 | child != fallback) |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 177 | { |
| 178 | return NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return fallback; |
| 183 | } |
| 184 | |
| 185 | static BlockDriverState *bdrv_snapshot_fallback(BlockDriverState *bs) |
| 186 | { |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 187 | return child_bs(bdrv_snapshot_fallback_child(bs)); |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 190 | int bdrv_can_snapshot(BlockDriverState *bs) |
| 191 | { |
| 192 | BlockDriver *drv = bs->drv; |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 193 | GLOBAL_STATE_CODE(); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 194 | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | if (!drv->bdrv_snapshot_create) { |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 199 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
| 200 | if (fallback_bs) { |
| 201 | return bdrv_can_snapshot(fallback_bs); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | int bdrv_snapshot_create(BlockDriverState *bs, |
| 210 | QEMUSnapshotInfo *sn_info) |
| 211 | { |
| 212 | BlockDriver *drv = bs->drv; |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 213 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 214 | |
| 215 | GLOBAL_STATE_CODE(); |
| 216 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 217 | if (!drv) { |
| 218 | return -ENOMEDIUM; |
| 219 | } |
| 220 | if (drv->bdrv_snapshot_create) { |
| 221 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 222 | } |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 223 | if (fallback_bs) { |
| 224 | return bdrv_snapshot_create(fallback_bs, sn_info); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 225 | } |
| 226 | return -ENOTSUP; |
| 227 | } |
| 228 | |
| 229 | int bdrv_snapshot_goto(BlockDriverState *bs, |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 230 | const char *snapshot_id, |
| 231 | Error **errp) |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 232 | { |
| 233 | BlockDriver *drv = bs->drv; |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 234 | BdrvChild *fallback; |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 235 | int ret, open_ret; |
| 236 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 237 | GLOBAL_STATE_CODE(); |
| 238 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 239 | if (!drv) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 240 | error_setg(errp, "Block driver is closed"); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 241 | return -ENOMEDIUM; |
| 242 | } |
Vladimir Sementsov-Ogievskiy | 04dec3c | 2017-10-23 12:29:45 +0300 | [diff] [blame] | 243 | |
Kevin Wolf | 70a5afe | 2017-11-20 15:41:31 +0100 | [diff] [blame] | 244 | if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { |
| 245 | error_setg(errp, "Device has active dirty bitmaps"); |
| 246 | return -EBUSY; |
Vladimir Sementsov-Ogievskiy | 04dec3c | 2017-10-23 12:29:45 +0300 | [diff] [blame] | 247 | } |
Vladimir Sementsov-Ogievskiy | 04dec3c | 2017-10-23 12:29:45 +0300 | [diff] [blame] | 248 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 249 | if (drv->bdrv_snapshot_goto) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 250 | ret = drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 251 | if (ret < 0) { |
| 252 | error_setg_errno(errp, -ret, "Failed to load snapshot"); |
| 253 | } |
| 254 | return ret; |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 255 | } |
| 256 | |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 257 | fallback = bdrv_snapshot_fallback_child(bs); |
| 258 | if (fallback) { |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 259 | QDict *options; |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 260 | QDict *file_options; |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 261 | Error *local_err = NULL; |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 262 | BlockDriverState *fallback_bs = fallback->bs; |
| 263 | char *subqdict_prefix = g_strdup_printf("%s.", fallback->name); |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 264 | |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 265 | options = qdict_clone_shallow(bs->options); |
| 266 | |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 267 | /* Prevent it from getting deleted when detached from bs */ |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 268 | bdrv_ref(fallback_bs); |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 269 | |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 270 | qdict_extract_subqdict(options, &file_options, subqdict_prefix); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 271 | qobject_unref(file_options); |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 272 | g_free(subqdict_prefix); |
| 273 | |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 274 | /* Force .bdrv_open() below to re-attach fallback_bs on fallback */ |
| 275 | qdict_put_str(options, fallback->name, |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 276 | bdrv_get_node_name(fallback_bs)); |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 277 | |
Max Reitz | 32a9a24 | 2021-05-03 02:54:18 -0700 | [diff] [blame] | 278 | /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */ |
Vladimir Sementsov-Ogievskiy | 3c00529 | 2018-08-14 15:43:19 +0300 | [diff] [blame] | 279 | if (drv->bdrv_close) { |
| 280 | drv->bdrv_close(bs); |
| 281 | } |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 282 | |
Max Reitz | 32a9a24 | 2021-05-03 02:54:18 -0700 | [diff] [blame] | 283 | /* .bdrv_open() will re-attach it */ |
Vladimir Sementsov-Ogievskiy | c5c2174 | 2022-07-26 23:11:33 +0300 | [diff] [blame] | 284 | bdrv_unref_child(bs, fallback); |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 285 | |
| 286 | ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp); |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 287 | open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 288 | qobject_unref(options); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 289 | if (open_ret < 0) { |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 290 | bdrv_unref(fallback_bs); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 291 | bs->drv = NULL; |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 292 | /* A bdrv_snapshot_goto() error takes precedence */ |
| 293 | error_propagate(errp, local_err); |
| 294 | return ret < 0 ? ret : open_ret; |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 295 | } |
Dong Jia Shi | 7a9e511 | 2017-04-05 11:19:09 +0200 | [diff] [blame] | 296 | |
Max Reitz | 32a9a24 | 2021-05-03 02:54:18 -0700 | [diff] [blame] | 297 | /* |
Vladimir Sementsov-Ogievskiy | 0c6100a | 2022-07-26 23:11:27 +0300 | [diff] [blame] | 298 | * fallback was a primary child. It was closed above and set to NULL, |
| 299 | * but the .bdrv_open() call has opened it again, because we set the |
| 300 | * respective option (with the qdict_put_str() call above). |
| 301 | * Assert that .bdrv_open() has attached the right BDS as primary child. |
Max Reitz | 32a9a24 | 2021-05-03 02:54:18 -0700 | [diff] [blame] | 302 | */ |
Vladimir Sementsov-Ogievskiy | 0c6100a | 2022-07-26 23:11:27 +0300 | [diff] [blame] | 303 | assert(bdrv_primary_bs(bs) == fallback_bs); |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 304 | bdrv_unref(fallback_bs); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 305 | return ret; |
| 306 | } |
| 307 | |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 308 | error_setg(errp, "Block driver does not support snapshots"); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 309 | return -ENOTSUP; |
| 310 | } |
| 311 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 312 | /** |
| 313 | * Delete an internal snapshot by @snapshot_id and @name. |
| 314 | * @bs: block device used in the operation |
| 315 | * @snapshot_id: unique snapshot ID, or NULL |
| 316 | * @name: snapshot name, or NULL |
| 317 | * @errp: location to store error |
| 318 | * |
| 319 | * If both @snapshot_id and @name are specified, delete the first one with |
| 320 | * id @snapshot_id and name @name. |
| 321 | * If only @snapshot_id is specified, delete the first one with id |
| 322 | * @snapshot_id. |
| 323 | * If only @name is specified, delete the first one with name @name. |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 324 | * if none is specified, return -EINVAL. |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 325 | * |
| 326 | * Returns: 0 on success, -errno on failure. If @bs is not inserted, return |
| 327 | * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs |
| 328 | * does not support internal snapshot deletion, return -ENOTSUP. If @bs does |
| 329 | * not support parameter @snapshot_id or @name, or one of them is not correctly |
| 330 | * specified, return -EINVAL. If @bs can't find one matching @id and @name, |
| 331 | * return -ENOENT. If @errp != NULL, it will always be filled with error |
| 332 | * message on failure. |
| 333 | */ |
| 334 | int bdrv_snapshot_delete(BlockDriverState *bs, |
| 335 | const char *snapshot_id, |
| 336 | const char *name, |
| 337 | Error **errp) |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 338 | { |
| 339 | BlockDriver *drv = bs->drv; |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 340 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
Paolo Bonzini | 27a7649 | 2015-12-16 19:33:45 +0100 | [diff] [blame] | 341 | int ret; |
| 342 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 343 | GLOBAL_STATE_CODE(); |
| 344 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 345 | if (!drv) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 346 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 347 | return -ENOMEDIUM; |
| 348 | } |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 349 | if (!snapshot_id && !name) { |
| 350 | error_setg(errp, "snapshot_id and name are both NULL"); |
| 351 | return -EINVAL; |
| 352 | } |
Zhang Haoyu | 3432a19 | 2014-10-21 16:38:01 +0800 | [diff] [blame] | 353 | |
| 354 | /* drain all pending i/o before deleting snapshot */ |
Paolo Bonzini | 27a7649 | 2015-12-16 19:33:45 +0100 | [diff] [blame] | 355 | bdrv_drained_begin(bs); |
Zhang Haoyu | 3432a19 | 2014-10-21 16:38:01 +0800 | [diff] [blame] | 356 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 357 | if (drv->bdrv_snapshot_delete) { |
Paolo Bonzini | 27a7649 | 2015-12-16 19:33:45 +0100 | [diff] [blame] | 358 | ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp); |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 359 | } else if (fallback_bs) { |
| 360 | ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp); |
Paolo Bonzini | 27a7649 | 2015-12-16 19:33:45 +0100 | [diff] [blame] | 361 | } else { |
| 362 | error_setg(errp, "Block format '%s' used by device '%s' " |
| 363 | "does not support internal snapshot deletion", |
| 364 | drv->format_name, bdrv_get_device_name(bs)); |
| 365 | ret = -ENOTSUP; |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 366 | } |
Paolo Bonzini | 27a7649 | 2015-12-16 19:33:45 +0100 | [diff] [blame] | 367 | |
| 368 | bdrv_drained_end(bs); |
| 369 | return ret; |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 370 | } |
| 371 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 372 | int bdrv_snapshot_list(BlockDriverState *bs, |
| 373 | QEMUSnapshotInfo **psn_info) |
| 374 | { |
| 375 | BlockDriver *drv = bs->drv; |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 376 | BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs); |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 377 | |
| 378 | GLOBAL_STATE_CODE(); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 379 | if (!drv) { |
| 380 | return -ENOMEDIUM; |
| 381 | } |
| 382 | if (drv->bdrv_snapshot_list) { |
| 383 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 384 | } |
Max Reitz | c8af875 | 2019-06-12 18:21:07 +0200 | [diff] [blame] | 385 | if (fallback_bs) { |
| 386 | return bdrv_snapshot_list(fallback_bs, psn_info); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 387 | } |
| 388 | return -ENOTSUP; |
| 389 | } |
| 390 | |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 391 | /** |
| 392 | * Temporarily load an internal snapshot by @snapshot_id and @name. |
| 393 | * @bs: block device used in the operation |
| 394 | * @snapshot_id: unique snapshot ID, or NULL |
| 395 | * @name: snapshot name, or NULL |
| 396 | * @errp: location to store error |
| 397 | * |
| 398 | * If both @snapshot_id and @name are specified, load the first one with |
| 399 | * id @snapshot_id and name @name. |
| 400 | * If only @snapshot_id is specified, load the first one with id |
| 401 | * @snapshot_id. |
| 402 | * If only @name is specified, load the first one with name @name. |
| 403 | * if none is specified, return -EINVAL. |
| 404 | * |
| 405 | * Returns: 0 on success, -errno on fail. If @bs is not inserted, return |
| 406 | * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support |
| 407 | * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and |
| 408 | * @name, return -ENOENT. If @errp != NULL, it will always be filled on |
| 409 | * failure. |
| 410 | */ |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 411 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 412 | const char *snapshot_id, |
| 413 | const char *name, |
| 414 | Error **errp) |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 415 | { |
| 416 | BlockDriver *drv = bs->drv; |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 417 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 418 | GLOBAL_STATE_CODE(); |
| 419 | |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 420 | if (!drv) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 421 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs)); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 422 | return -ENOMEDIUM; |
| 423 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 424 | if (!snapshot_id && !name) { |
| 425 | error_setg(errp, "snapshot_id and name are both NULL"); |
| 426 | return -EINVAL; |
| 427 | } |
Vladimir Sementsov-Ogievskiy | 307261b | 2021-05-27 18:40:54 +0300 | [diff] [blame] | 428 | if (!bdrv_is_read_only(bs)) { |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 429 | error_setg(errp, "Device is not readonly"); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 430 | return -EINVAL; |
| 431 | } |
| 432 | if (drv->bdrv_snapshot_load_tmp) { |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 433 | return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 434 | } |
Alberto Garcia | 81e5f78 | 2015-04-08 12:29:19 +0300 | [diff] [blame] | 435 | error_setg(errp, "Block format '%s' used by device '%s' " |
| 436 | "does not support temporarily loading internal snapshots", |
| 437 | drv->format_name, bdrv_get_device_name(bs)); |
Wenchao Xia | de08c60 | 2013-05-25 11:09:43 +0800 | [diff] [blame] | 438 | return -ENOTSUP; |
| 439 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 440 | |
| 441 | int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs, |
| 442 | const char *id_or_name, |
| 443 | Error **errp) |
| 444 | { |
| 445 | int ret; |
| 446 | Error *local_err = NULL; |
| 447 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 448 | GLOBAL_STATE_CODE(); |
| 449 | |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 450 | ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err); |
| 451 | if (ret == -ENOENT || ret == -EINVAL) { |
| 452 | error_free(local_err); |
| 453 | local_err = NULL; |
| 454 | ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err); |
| 455 | } |
| 456 | |
Eduardo Habkost | 621ff94 | 2016-06-13 18:57:56 -0300 | [diff] [blame] | 457 | error_propagate(errp, local_err); |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 458 | |
| 459 | return ret; |
| 460 | } |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 461 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 462 | |
| 463 | static int bdrv_all_get_snapshot_devices(bool has_devices, strList *devices, |
| 464 | GList **all_bdrvs, |
| 465 | Error **errp) |
| 466 | { |
| 467 | g_autoptr(GList) bdrvs = NULL; |
| 468 | |
| 469 | if (has_devices) { |
| 470 | if (!devices) { |
| 471 | error_setg(errp, "At least one device is required for snapshot"); |
| 472 | return -1; |
| 473 | } |
| 474 | |
| 475 | while (devices) { |
| 476 | BlockDriverState *bs = bdrv_find_node(devices->value); |
| 477 | if (!bs) { |
| 478 | error_setg(errp, "No block device node '%s'", devices->value); |
| 479 | return -1; |
| 480 | } |
| 481 | bdrvs = g_list_append(bdrvs, bs); |
| 482 | devices = devices->next; |
| 483 | } |
| 484 | } else { |
| 485 | BlockDriverState *bs; |
| 486 | BdrvNextIterator it; |
| 487 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
| 488 | bdrvs = g_list_append(bdrvs, bs); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | *all_bdrvs = g_steal_pointer(&bdrvs); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | |
Kevin Wolf | 05f4ace | 2019-09-17 12:26:23 +0200 | [diff] [blame] | 497 | static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs) |
| 498 | { |
| 499 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | /* Include all nodes that are either in use by a BlockBackend, or that |
| 504 | * aren't attached to any node, but owned by the monitor. */ |
| 505 | return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents); |
| 506 | } |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 507 | |
| 508 | /* Group operations. All block drivers are involved. |
| 509 | * These functions will properly handle dataplane (take aio_context_acquire |
| 510 | * when appropriate for appropriate block drivers) */ |
| 511 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 512 | bool bdrv_all_can_snapshot(bool has_devices, strList *devices, |
| 513 | Error **errp) |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 514 | { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 515 | g_autoptr(GList) bdrvs = NULL; |
| 516 | GList *iterbdrvs; |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 517 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 518 | GLOBAL_STATE_CODE(); |
| 519 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 520 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | iterbdrvs = bdrvs; |
| 525 | while (iterbdrvs) { |
| 526 | BlockDriverState *bs = iterbdrvs->data; |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 527 | AioContext *ctx = bdrv_get_aio_context(bs); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 528 | bool ok = true; |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 529 | |
| 530 | aio_context_acquire(ctx); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 531 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 532 | ok = bdrv_can_snapshot(bs); |
| 533 | } |
| 534 | aio_context_release(ctx); |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 535 | if (!ok) { |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 536 | error_setg(errp, "Device '%s' is writable but does not support " |
| 537 | "snapshots", bdrv_get_device_or_node_name(bs)); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 538 | return false; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 539 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 540 | |
| 541 | iterbdrvs = iterbdrvs->next; |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 542 | } |
| 543 | |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 544 | return true; |
Denis V. Lunev | e9ff957 | 2015-11-19 09:42:01 +0300 | [diff] [blame] | 545 | } |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 546 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 547 | int bdrv_all_delete_snapshot(const char *name, |
| 548 | bool has_devices, strList *devices, |
| 549 | Error **errp) |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 550 | { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 551 | g_autoptr(GList) bdrvs = NULL; |
| 552 | GList *iterbdrvs; |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 553 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 554 | GLOBAL_STATE_CODE(); |
| 555 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 556 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | iterbdrvs = bdrvs; |
| 561 | while (iterbdrvs) { |
| 562 | BlockDriverState *bs = iterbdrvs->data; |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 563 | AioContext *ctx = bdrv_get_aio_context(bs); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 564 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 565 | int ret = 0; |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 566 | |
| 567 | aio_context_acquire(ctx); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 568 | if ((devices || bdrv_all_snapshots_includes_bs(bs)) && |
Kevin Wolf | 05f4ace | 2019-09-17 12:26:23 +0200 | [diff] [blame] | 569 | bdrv_snapshot_find(bs, snapshot, name) >= 0) |
| 570 | { |
Daniel Henrique Barboza | 6ca0804 | 2018-11-07 11:09:58 -0200 | [diff] [blame] | 571 | ret = bdrv_snapshot_delete(bs, snapshot->id_str, |
Vladimir Sementsov-Ogievskiy | e53a578 | 2019-12-05 20:46:24 +0300 | [diff] [blame] | 572 | snapshot->name, errp); |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 573 | } |
| 574 | aio_context_release(ctx); |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 575 | if (ret < 0) { |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 576 | error_prepend(errp, "Could not delete snapshot '%s' on '%s': ", |
| 577 | name, bdrv_get_device_or_node_name(bs)); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 578 | return -1; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 579 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 580 | |
| 581 | iterbdrvs = iterbdrvs->next; |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 582 | } |
| 583 | |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 584 | return 0; |
Denis V. Lunev | 9b00ea3 | 2015-11-19 09:42:03 +0300 | [diff] [blame] | 585 | } |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 586 | |
| 587 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 588 | int bdrv_all_goto_snapshot(const char *name, |
| 589 | bool has_devices, strList *devices, |
| 590 | Error **errp) |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 591 | { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 592 | g_autoptr(GList) bdrvs = NULL; |
| 593 | GList *iterbdrvs; |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 594 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 595 | GLOBAL_STATE_CODE(); |
| 596 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 597 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 598 | return -1; |
| 599 | } |
| 600 | |
| 601 | iterbdrvs = bdrvs; |
| 602 | while (iterbdrvs) { |
| 603 | BlockDriverState *bs = iterbdrvs->data; |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 604 | AioContext *ctx = bdrv_get_aio_context(bs); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 605 | int ret = 0; |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 606 | |
| 607 | aio_context_acquire(ctx); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 608 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
Kevin Wolf | 2b624fe | 2017-11-20 15:36:48 +0100 | [diff] [blame] | 609 | ret = bdrv_snapshot_goto(bs, name, errp); |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 610 | } |
| 611 | aio_context_release(ctx); |
Kevin Wolf | 2b624fe | 2017-11-20 15:36:48 +0100 | [diff] [blame] | 612 | if (ret < 0) { |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 613 | error_prepend(errp, "Could not load snapshot '%s' on '%s': ", |
| 614 | name, bdrv_get_device_or_node_name(bs)); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 615 | return -1; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 616 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 617 | |
| 618 | iterbdrvs = iterbdrvs->next; |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 619 | } |
| 620 | |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 621 | return 0; |
Denis V. Lunev | 4c1cdba | 2015-11-19 09:42:04 +0300 | [diff] [blame] | 622 | } |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 623 | |
Daniel P. Berrangé | 3d3e9b1 | 2021-02-04 12:48:28 +0000 | [diff] [blame] | 624 | int bdrv_all_has_snapshot(const char *name, |
| 625 | bool has_devices, strList *devices, |
| 626 | Error **errp) |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 627 | { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 628 | g_autoptr(GList) bdrvs = NULL; |
| 629 | GList *iterbdrvs; |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 630 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 631 | GLOBAL_STATE_CODE(); |
| 632 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 633 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 634 | return -1; |
| 635 | } |
| 636 | |
| 637 | iterbdrvs = bdrvs; |
| 638 | while (iterbdrvs) { |
| 639 | BlockDriverState *bs = iterbdrvs->data; |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 640 | AioContext *ctx = bdrv_get_aio_context(bs); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 641 | QEMUSnapshotInfo sn; |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 642 | int ret = 0; |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 643 | |
| 644 | aio_context_acquire(ctx); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 645 | if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 646 | ret = bdrv_snapshot_find(bs, &sn, name); |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 647 | } |
| 648 | aio_context_release(ctx); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 649 | if (ret < 0) { |
Daniel P. Berrangé | 3d3e9b1 | 2021-02-04 12:48:28 +0000 | [diff] [blame] | 650 | if (ret == -ENOENT) { |
| 651 | return 0; |
| 652 | } else { |
| 653 | error_setg_errno(errp, errno, |
| 654 | "Could not check snapshot '%s' on '%s'", |
| 655 | name, bdrv_get_device_or_node_name(bs)); |
| 656 | return -1; |
| 657 | } |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 658 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 659 | |
| 660 | iterbdrvs = iterbdrvs->next; |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 661 | } |
| 662 | |
Daniel P. Berrangé | 3d3e9b1 | 2021-02-04 12:48:28 +0000 | [diff] [blame] | 663 | return 1; |
Denis V. Lunev | 723ccda | 2015-11-19 09:42:06 +0300 | [diff] [blame] | 664 | } |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 665 | |
| 666 | int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn, |
| 667 | BlockDriverState *vm_state_bs, |
| 668 | uint64_t vm_state_size, |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 669 | bool has_devices, strList *devices, |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 670 | Error **errp) |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 671 | { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 672 | g_autoptr(GList) bdrvs = NULL; |
| 673 | GList *iterbdrvs; |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 674 | GLOBAL_STATE_CODE(); |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 675 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 676 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 677 | return -1; |
| 678 | } |
| 679 | |
| 680 | iterbdrvs = bdrvs; |
| 681 | while (iterbdrvs) { |
| 682 | BlockDriverState *bs = iterbdrvs->data; |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 683 | AioContext *ctx = bdrv_get_aio_context(bs); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 684 | int ret = 0; |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 685 | |
| 686 | aio_context_acquire(ctx); |
| 687 | if (bs == vm_state_bs) { |
| 688 | sn->vm_state_size = vm_state_size; |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 689 | ret = bdrv_snapshot_create(bs, sn); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 690 | } else if (devices || bdrv_all_snapshots_includes_bs(bs)) { |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 691 | sn->vm_state_size = 0; |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 692 | ret = bdrv_snapshot_create(bs, sn); |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 693 | } |
| 694 | aio_context_release(ctx); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 695 | if (ret < 0) { |
| 696 | error_setg(errp, "Could not create snapshot '%s' on '%s'", |
| 697 | sn->name, bdrv_get_device_or_node_name(bs)); |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 698 | return -1; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 699 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 700 | |
| 701 | iterbdrvs = iterbdrvs->next; |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 702 | } |
| 703 | |
Daniel P. Berrangé | e26f98e | 2021-02-04 12:48:23 +0000 | [diff] [blame] | 704 | return 0; |
Denis V. Lunev | a9085f9 | 2015-11-19 09:42:08 +0300 | [diff] [blame] | 705 | } |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 706 | |
Daniel P. Berrangé | c22d644 | 2021-02-04 12:48:27 +0000 | [diff] [blame] | 707 | |
| 708 | BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs, |
| 709 | bool has_devices, strList *devices, |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 710 | Error **errp) |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 711 | { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 712 | g_autoptr(GList) bdrvs = NULL; |
| 713 | GList *iterbdrvs; |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 714 | |
Emanuele Giuseppe Esposito | 6b573ef | 2022-03-03 10:16:07 -0500 | [diff] [blame] | 715 | GLOBAL_STATE_CODE(); |
| 716 | |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 717 | if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) { |
| 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | iterbdrvs = bdrvs; |
| 722 | while (iterbdrvs) { |
| 723 | BlockDriverState *bs = iterbdrvs->data; |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 724 | AioContext *ctx = bdrv_get_aio_context(bs); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 725 | bool found = false; |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 726 | |
| 727 | aio_context_acquire(ctx); |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 728 | found = (devices || bdrv_all_snapshots_includes_bs(bs)) && |
| 729 | bdrv_can_snapshot(bs); |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 730 | aio_context_release(ctx); |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 731 | |
Daniel P. Berrangé | c22d644 | 2021-02-04 12:48:27 +0000 | [diff] [blame] | 732 | if (vmstate_bs) { |
| 733 | if (g_str_equal(vmstate_bs, |
| 734 | bdrv_get_node_name(bs))) { |
| 735 | if (found) { |
| 736 | return bs; |
| 737 | } else { |
| 738 | error_setg(errp, |
| 739 | "vmstate block device '%s' does not support snapshots", |
| 740 | vmstate_bs); |
| 741 | return NULL; |
| 742 | } |
| 743 | } |
| 744 | } else if (found) { |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 745 | return bs; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 746 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 747 | |
| 748 | iterbdrvs = iterbdrvs->next; |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 749 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 750 | |
Daniel P. Berrangé | c22d644 | 2021-02-04 12:48:27 +0000 | [diff] [blame] | 751 | if (vmstate_bs) { |
| 752 | error_setg(errp, |
| 753 | "vmstate block device '%s' does not exist", vmstate_bs); |
| 754 | } else { |
| 755 | error_setg(errp, |
| 756 | "no block device can store vmstate for snapshot"); |
| 757 | } |
Daniel P. Berrangé | cf3a74c | 2021-02-04 12:48:26 +0000 | [diff] [blame] | 758 | return NULL; |
Denis V. Lunev | 7cb1448 | 2015-11-19 09:42:10 +0300 | [diff] [blame] | 759 | } |