snapshot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. #include "qemu/osdep.h"
  25. #include "block/snapshot.h"
  26. #include "block/block_int.h"
  27. #include "block/qdict.h"
  28. #include "qapi/error.h"
  29. #include "qobject/qdict.h"
  30. #include "qobject/qstring.h"
  31. #include "qemu/option.h"
  32. #include "system/block-backend.h"
  33. QemuOptsList internal_snapshot_opts = {
  34. .name = "snapshot",
  35. .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),
  36. .desc = {
  37. {
  38. .name = SNAPSHOT_OPT_ID,
  39. .type = QEMU_OPT_STRING,
  40. .help = "snapshot id"
  41. },{
  42. .name = SNAPSHOT_OPT_NAME,
  43. .type = QEMU_OPT_STRING,
  44. .help = "snapshot name"
  45. },{
  46. /* end of list */
  47. }
  48. },
  49. };
  50. int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
  51. const char *name)
  52. {
  53. QEMUSnapshotInfo *sn_tab, *sn;
  54. int nb_sns, i, ret;
  55. GLOBAL_STATE_CODE();
  56. ret = -ENOENT;
  57. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  58. if (nb_sns < 0) {
  59. return ret;
  60. }
  61. for (i = 0; i < nb_sns; i++) {
  62. sn = &sn_tab[i];
  63. if (!strcmp(sn->name, name)) {
  64. *sn_info = *sn;
  65. ret = 0;
  66. break;
  67. }
  68. }
  69. g_free(sn_tab);
  70. return ret;
  71. }
  72. /**
  73. * Look up an internal snapshot by @id and @name.
  74. * @bs: block device to search
  75. * @id: unique snapshot ID, or NULL
  76. * @name: snapshot name, or NULL
  77. * @sn_info: location to store information on the snapshot found
  78. * @errp: location to store error, will be set only for exception
  79. *
  80. * This function will traverse snapshot list in @bs to search the matching
  81. * one, @id and @name are the matching condition:
  82. * If both @id and @name are specified, find the first one with id @id and
  83. * name @name.
  84. * If only @id is specified, find the first one with id @id.
  85. * If only @name is specified, find the first one with name @name.
  86. * if none is specified, abort().
  87. *
  88. * Returns: true when a snapshot is found and @sn_info will be filled, false
  89. * when error or not found. If all operation succeed but no matching one is
  90. * found, @errp will NOT be set.
  91. */
  92. bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
  93. const char *id,
  94. const char *name,
  95. QEMUSnapshotInfo *sn_info,
  96. Error **errp)
  97. {
  98. QEMUSnapshotInfo *sn_tab, *sn;
  99. int nb_sns, i;
  100. bool ret = false;
  101. assert(id || name);
  102. GLOBAL_STATE_CODE();
  103. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  104. if (nb_sns < 0) {
  105. error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
  106. return false;
  107. } else if (nb_sns == 0) {
  108. return false;
  109. }
  110. if (id && name) {
  111. for (i = 0; i < nb_sns; i++) {
  112. sn = &sn_tab[i];
  113. if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
  114. *sn_info = *sn;
  115. ret = true;
  116. break;
  117. }
  118. }
  119. } else if (id) {
  120. for (i = 0; i < nb_sns; i++) {
  121. sn = &sn_tab[i];
  122. if (!strcmp(sn->id_str, id)) {
  123. *sn_info = *sn;
  124. ret = true;
  125. break;
  126. }
  127. }
  128. } else if (name) {
  129. for (i = 0; i < nb_sns; i++) {
  130. sn = &sn_tab[i];
  131. if (!strcmp(sn->name, name)) {
  132. *sn_info = *sn;
  133. ret = true;
  134. break;
  135. }
  136. }
  137. }
  138. g_free(sn_tab);
  139. return ret;
  140. }
  141. /**
  142. * Return a pointer to child of given BDS to which we can fall
  143. * back if the given BDS does not support snapshots.
  144. * Return NULL if there is no BDS to (safely) fall back to.
  145. */
  146. static BdrvChild * GRAPH_RDLOCK
  147. bdrv_snapshot_fallback_child(BlockDriverState *bs)
  148. {
  149. BdrvChild *fallback = bdrv_primary_child(bs);
  150. BdrvChild *child;
  151. GLOBAL_STATE_CODE();
  152. assert_bdrv_graph_readable();
  153. /* We allow fallback only to primary child */
  154. if (!fallback) {
  155. return NULL;
  156. }
  157. /*
  158. * Check that there are no other children that would need to be
  159. * snapshotted. If there are, it is not safe to fall back to
  160. * fallback.
  161. */
  162. QLIST_FOREACH(child, &bs->children, next) {
  163. if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
  164. BDRV_CHILD_FILTERED) &&
  165. child != fallback)
  166. {
  167. return NULL;
  168. }
  169. }
  170. return fallback;
  171. }
  172. static BlockDriverState * GRAPH_RDLOCK
  173. bdrv_snapshot_fallback(BlockDriverState *bs)
  174. {
  175. GLOBAL_STATE_CODE();
  176. return child_bs(bdrv_snapshot_fallback_child(bs));
  177. }
  178. int bdrv_can_snapshot(BlockDriverState *bs)
  179. {
  180. BlockDriver *drv = bs->drv;
  181. GLOBAL_STATE_CODE();
  182. if (!drv || !bdrv_is_inserted(bs) || !bdrv_is_writable(bs)) {
  183. return 0;
  184. }
  185. if (!drv->bdrv_snapshot_create) {
  186. BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
  187. if (fallback_bs) {
  188. return bdrv_can_snapshot(fallback_bs);
  189. }
  190. return 0;
  191. }
  192. return 1;
  193. }
  194. int bdrv_snapshot_create(BlockDriverState *bs,
  195. QEMUSnapshotInfo *sn_info)
  196. {
  197. BlockDriver *drv = bs->drv;
  198. BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
  199. GLOBAL_STATE_CODE();
  200. if (!drv) {
  201. return -ENOMEDIUM;
  202. }
  203. if (drv->bdrv_snapshot_create) {
  204. return drv->bdrv_snapshot_create(bs, sn_info);
  205. }
  206. if (fallback_bs) {
  207. return bdrv_snapshot_create(fallback_bs, sn_info);
  208. }
  209. return -ENOTSUP;
  210. }
  211. int bdrv_snapshot_goto(BlockDriverState *bs,
  212. const char *snapshot_id,
  213. Error **errp)
  214. {
  215. BlockDriver *drv = bs->drv;
  216. BdrvChild *fallback;
  217. int ret, open_ret;
  218. GLOBAL_STATE_CODE();
  219. if (!drv) {
  220. error_setg(errp, "Block driver is closed");
  221. return -ENOMEDIUM;
  222. }
  223. if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
  224. error_setg(errp, "Device has active dirty bitmaps");
  225. return -EBUSY;
  226. }
  227. if (drv->bdrv_snapshot_goto) {
  228. ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
  229. if (ret < 0) {
  230. error_setg_errno(errp, -ret, "Failed to load snapshot");
  231. }
  232. return ret;
  233. }
  234. bdrv_graph_rdlock_main_loop();
  235. fallback = bdrv_snapshot_fallback_child(bs);
  236. bdrv_graph_rdunlock_main_loop();
  237. if (fallback) {
  238. QDict *options;
  239. QDict *file_options;
  240. Error *local_err = NULL;
  241. BlockDriverState *fallback_bs = fallback->bs;
  242. char *subqdict_prefix = g_strdup_printf("%s.", fallback->name);
  243. options = qdict_clone_shallow(bs->options);
  244. /* Prevent it from getting deleted when detached from bs */
  245. bdrv_ref(fallback_bs);
  246. qdict_extract_subqdict(options, &file_options, subqdict_prefix);
  247. qobject_unref(file_options);
  248. g_free(subqdict_prefix);
  249. /* Force .bdrv_open() below to re-attach fallback_bs on fallback */
  250. qdict_put_str(options, fallback->name,
  251. bdrv_get_node_name(fallback_bs));
  252. /* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
  253. if (drv->bdrv_close) {
  254. drv->bdrv_close(bs);
  255. }
  256. /* .bdrv_open() will re-attach it */
  257. bdrv_graph_wrlock();
  258. bdrv_unref_child(bs, fallback);
  259. bdrv_graph_wrunlock();
  260. ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
  261. open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
  262. qobject_unref(options);
  263. if (open_ret < 0) {
  264. bdrv_unref(fallback_bs);
  265. bs->drv = NULL;
  266. /* A bdrv_snapshot_goto() error takes precedence */
  267. error_propagate(errp, local_err);
  268. return ret < 0 ? ret : open_ret;
  269. }
  270. /*
  271. * fallback was a primary child. It was closed above and set to NULL,
  272. * but the .bdrv_open() call has opened it again, because we set the
  273. * respective option (with the qdict_put_str() call above).
  274. * Assert that .bdrv_open() has attached the right BDS as primary child.
  275. */
  276. bdrv_graph_rdlock_main_loop();
  277. assert(bdrv_primary_bs(bs) == fallback_bs);
  278. bdrv_graph_rdunlock_main_loop();
  279. bdrv_unref(fallback_bs);
  280. return ret;
  281. }
  282. error_setg(errp, "Block driver does not support snapshots");
  283. return -ENOTSUP;
  284. }
  285. /**
  286. * Delete an internal snapshot by @snapshot_id and @name.
  287. * @bs: block device used in the operation
  288. * @snapshot_id: unique snapshot ID, or NULL
  289. * @name: snapshot name, or NULL
  290. * @errp: location to store error
  291. *
  292. * If both @snapshot_id and @name are specified, delete the first one with
  293. * id @snapshot_id and name @name.
  294. * If only @snapshot_id is specified, delete the first one with id
  295. * @snapshot_id.
  296. * If only @name is specified, delete the first one with name @name.
  297. * if none is specified, return -EINVAL.
  298. *
  299. * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
  300. * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
  301. * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
  302. * not support parameter @snapshot_id or @name, or one of them is not correctly
  303. * specified, return -EINVAL. If @bs can't find one matching @id and @name,
  304. * return -ENOENT. If @errp != NULL, it will always be filled with error
  305. * message on failure.
  306. */
  307. int bdrv_snapshot_delete(BlockDriverState *bs,
  308. const char *snapshot_id,
  309. const char *name,
  310. Error **errp)
  311. {
  312. BlockDriver *drv = bs->drv;
  313. BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
  314. int ret;
  315. GLOBAL_STATE_CODE();
  316. if (!drv) {
  317. error_setg(errp, "Device '%s' has no medium",
  318. bdrv_get_device_name(bs));
  319. return -ENOMEDIUM;
  320. }
  321. if (!snapshot_id && !name) {
  322. error_setg(errp, "snapshot_id and name are both NULL");
  323. return -EINVAL;
  324. }
  325. /* drain all pending i/o before deleting snapshot */
  326. bdrv_drained_begin(bs);
  327. if (drv->bdrv_snapshot_delete) {
  328. ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
  329. } else if (fallback_bs) {
  330. ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
  331. } else {
  332. error_setg(errp, "Block format '%s' used by device '%s' "
  333. "does not support internal snapshot deletion",
  334. drv->format_name, bdrv_get_device_name(bs));
  335. ret = -ENOTSUP;
  336. }
  337. bdrv_drained_end(bs);
  338. return ret;
  339. }
  340. int bdrv_snapshot_list(BlockDriverState *bs,
  341. QEMUSnapshotInfo **psn_info)
  342. {
  343. GLOBAL_STATE_CODE();
  344. GRAPH_RDLOCK_GUARD_MAINLOOP();
  345. BlockDriver *drv = bs->drv;
  346. BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
  347. if (!drv) {
  348. return -ENOMEDIUM;
  349. }
  350. if (drv->bdrv_snapshot_list) {
  351. return drv->bdrv_snapshot_list(bs, psn_info);
  352. }
  353. if (fallback_bs) {
  354. return bdrv_snapshot_list(fallback_bs, psn_info);
  355. }
  356. return -ENOTSUP;
  357. }
  358. /**
  359. * Temporarily load an internal snapshot by @snapshot_id and @name.
  360. * @bs: block device used in the operation
  361. * @snapshot_id: unique snapshot ID, or NULL
  362. * @name: snapshot name, or NULL
  363. * @errp: location to store error
  364. *
  365. * If both @snapshot_id and @name are specified, load the first one with
  366. * id @snapshot_id and name @name.
  367. * If only @snapshot_id is specified, load the first one with id
  368. * @snapshot_id.
  369. * If only @name is specified, load the first one with name @name.
  370. * if none is specified, return -EINVAL.
  371. *
  372. * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
  373. * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
  374. * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
  375. * @name, return -ENOENT. If @errp != NULL, it will always be filled on
  376. * failure.
  377. */
  378. int bdrv_snapshot_load_tmp(BlockDriverState *bs,
  379. const char *snapshot_id,
  380. const char *name,
  381. Error **errp)
  382. {
  383. BlockDriver *drv = bs->drv;
  384. GLOBAL_STATE_CODE();
  385. GRAPH_RDLOCK_GUARD_MAINLOOP();
  386. if (!drv) {
  387. error_setg(errp, "Device '%s' has no medium",
  388. bdrv_get_device_name(bs));
  389. return -ENOMEDIUM;
  390. }
  391. if (!snapshot_id && !name) {
  392. error_setg(errp, "snapshot_id and name are both NULL");
  393. return -EINVAL;
  394. }
  395. if (!bdrv_is_read_only(bs)) {
  396. error_setg(errp, "Device is not readonly");
  397. return -EINVAL;
  398. }
  399. if (drv->bdrv_snapshot_load_tmp) {
  400. return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
  401. }
  402. error_setg(errp, "Block format '%s' used by device '%s' "
  403. "does not support temporarily loading internal snapshots",
  404. drv->format_name, bdrv_get_device_name(bs));
  405. return -ENOTSUP;
  406. }
  407. int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
  408. const char *id_or_name,
  409. Error **errp)
  410. {
  411. int ret;
  412. Error *local_err = NULL;
  413. GLOBAL_STATE_CODE();
  414. ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
  415. if (ret == -ENOENT || ret == -EINVAL) {
  416. error_free(local_err);
  417. local_err = NULL;
  418. ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
  419. }
  420. error_propagate(errp, local_err);
  421. return ret;
  422. }
  423. static int GRAPH_RDLOCK
  424. bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
  425. GList **all_bdrvs, Error **errp)
  426. {
  427. g_autoptr(GList) bdrvs = NULL;
  428. if (has_devices) {
  429. if (!devices) {
  430. error_setg(errp, "At least one device is required for snapshot");
  431. return -1;
  432. }
  433. while (devices) {
  434. BlockDriverState *bs = bdrv_find_node(devices->value);
  435. if (!bs) {
  436. error_setg(errp, "No block device node '%s'", devices->value);
  437. return -1;
  438. }
  439. bdrvs = g_list_append(bdrvs, bs);
  440. devices = devices->next;
  441. }
  442. } else {
  443. BlockDriverState *bs;
  444. BdrvNextIterator it;
  445. for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
  446. bdrvs = g_list_append(bdrvs, bs);
  447. }
  448. }
  449. *all_bdrvs = g_steal_pointer(&bdrvs);
  450. return 0;
  451. }
  452. static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
  453. {
  454. GLOBAL_STATE_CODE();
  455. assert_bdrv_graph_readable();
  456. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  457. return false;
  458. }
  459. /* Include all nodes that are either in use by a BlockBackend, or that
  460. * aren't attached to any node, but owned by the monitor. */
  461. return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
  462. }
  463. /* Group operations. All block drivers are involved. */
  464. bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
  465. Error **errp)
  466. {
  467. g_autoptr(GList) bdrvs = NULL;
  468. GList *iterbdrvs;
  469. GLOBAL_STATE_CODE();
  470. GRAPH_RDLOCK_GUARD_MAINLOOP();
  471. if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
  472. return false;
  473. }
  474. iterbdrvs = bdrvs;
  475. while (iterbdrvs) {
  476. BlockDriverState *bs = iterbdrvs->data;
  477. bool ok = true;
  478. if (devices || bdrv_all_snapshots_includes_bs(bs)) {
  479. ok = bdrv_can_snapshot(bs);
  480. }
  481. if (!ok) {
  482. error_setg(errp, "Device '%s' is writable but does not support "
  483. "snapshots", bdrv_get_device_or_node_name(bs));
  484. return false;
  485. }
  486. iterbdrvs = iterbdrvs->next;
  487. }
  488. return true;
  489. }
  490. int bdrv_all_delete_snapshot(const char *name,
  491. bool has_devices, strList *devices,
  492. Error **errp)
  493. {
  494. ERRP_GUARD();
  495. g_autoptr(GList) bdrvs = NULL;
  496. GList *iterbdrvs;
  497. GLOBAL_STATE_CODE();
  498. GRAPH_RDLOCK_GUARD_MAINLOOP();
  499. if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
  500. return -1;
  501. }
  502. iterbdrvs = bdrvs;
  503. while (iterbdrvs) {
  504. BlockDriverState *bs = iterbdrvs->data;
  505. QEMUSnapshotInfo sn1, *snapshot = &sn1;
  506. int ret = 0;
  507. if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
  508. bdrv_snapshot_find(bs, snapshot, name) >= 0)
  509. {
  510. ret = bdrv_snapshot_delete(bs, snapshot->id_str,
  511. snapshot->name, errp);
  512. }
  513. if (ret < 0) {
  514. error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
  515. name, bdrv_get_device_or_node_name(bs));
  516. return -1;
  517. }
  518. iterbdrvs = iterbdrvs->next;
  519. }
  520. return 0;
  521. }
  522. int bdrv_all_goto_snapshot(const char *name,
  523. bool has_devices, strList *devices,
  524. Error **errp)
  525. {
  526. ERRP_GUARD();
  527. g_autoptr(GList) bdrvs = NULL;
  528. GList *iterbdrvs;
  529. int ret;
  530. GLOBAL_STATE_CODE();
  531. bdrv_graph_rdlock_main_loop();
  532. ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp);
  533. bdrv_graph_rdunlock_main_loop();
  534. if (ret < 0) {
  535. return -1;
  536. }
  537. iterbdrvs = bdrvs;
  538. while (iterbdrvs) {
  539. BlockDriverState *bs = iterbdrvs->data;
  540. bool all_snapshots_includes_bs;
  541. bdrv_graph_rdlock_main_loop();
  542. all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
  543. bdrv_graph_rdunlock_main_loop();
  544. ret = (devices || all_snapshots_includes_bs) ?
  545. bdrv_snapshot_goto(bs, name, errp) : 0;
  546. if (ret < 0) {
  547. bdrv_graph_rdlock_main_loop();
  548. error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
  549. name, bdrv_get_device_or_node_name(bs));
  550. bdrv_graph_rdunlock_main_loop();
  551. return -1;
  552. }
  553. iterbdrvs = iterbdrvs->next;
  554. }
  555. return 0;
  556. }
  557. int bdrv_all_has_snapshot(const char *name,
  558. bool has_devices, strList *devices,
  559. Error **errp)
  560. {
  561. g_autoptr(GList) bdrvs = NULL;
  562. GList *iterbdrvs;
  563. GLOBAL_STATE_CODE();
  564. GRAPH_RDLOCK_GUARD_MAINLOOP();
  565. if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
  566. return -1;
  567. }
  568. iterbdrvs = bdrvs;
  569. while (iterbdrvs) {
  570. BlockDriverState *bs = iterbdrvs->data;
  571. QEMUSnapshotInfo sn;
  572. int ret = 0;
  573. if (devices || bdrv_all_snapshots_includes_bs(bs)) {
  574. ret = bdrv_snapshot_find(bs, &sn, name);
  575. }
  576. if (ret < 0) {
  577. if (ret == -ENOENT) {
  578. return 0;
  579. } else {
  580. error_setg_errno(errp, errno,
  581. "Could not check snapshot '%s' on '%s'",
  582. name, bdrv_get_device_or_node_name(bs));
  583. return -1;
  584. }
  585. }
  586. iterbdrvs = iterbdrvs->next;
  587. }
  588. return 1;
  589. }
  590. int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
  591. BlockDriverState *vm_state_bs,
  592. uint64_t vm_state_size,
  593. bool has_devices, strList *devices,
  594. Error **errp)
  595. {
  596. g_autoptr(GList) bdrvs = NULL;
  597. GList *iterbdrvs;
  598. GLOBAL_STATE_CODE();
  599. GRAPH_RDLOCK_GUARD_MAINLOOP();
  600. if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
  601. return -1;
  602. }
  603. iterbdrvs = bdrvs;
  604. while (iterbdrvs) {
  605. BlockDriverState *bs = iterbdrvs->data;
  606. int ret = 0;
  607. if (bs == vm_state_bs) {
  608. sn->vm_state_size = vm_state_size;
  609. ret = bdrv_snapshot_create(bs, sn);
  610. } else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
  611. sn->vm_state_size = 0;
  612. ret = bdrv_snapshot_create(bs, sn);
  613. }
  614. if (ret < 0) {
  615. error_setg(errp, "Could not create snapshot '%s' on '%s'",
  616. sn->name, bdrv_get_device_or_node_name(bs));
  617. return -1;
  618. }
  619. iterbdrvs = iterbdrvs->next;
  620. }
  621. return 0;
  622. }
  623. BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
  624. bool has_devices, strList *devices,
  625. Error **errp)
  626. {
  627. g_autoptr(GList) bdrvs = NULL;
  628. GList *iterbdrvs;
  629. GLOBAL_STATE_CODE();
  630. GRAPH_RDLOCK_GUARD_MAINLOOP();
  631. if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
  632. return NULL;
  633. }
  634. iterbdrvs = bdrvs;
  635. while (iterbdrvs) {
  636. BlockDriverState *bs = iterbdrvs->data;
  637. bool found = false;
  638. found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
  639. bdrv_can_snapshot(bs);
  640. if (vmstate_bs) {
  641. if (g_str_equal(vmstate_bs,
  642. bdrv_get_node_name(bs))) {
  643. if (found) {
  644. return bs;
  645. } else {
  646. error_setg(errp,
  647. "vmstate block device '%s' does not support snapshots",
  648. vmstate_bs);
  649. return NULL;
  650. }
  651. }
  652. } else if (found) {
  653. return bs;
  654. }
  655. iterbdrvs = iterbdrvs->next;
  656. }
  657. if (vmstate_bs) {
  658. error_setg(errp,
  659. "vmstate block device '%s' does not exist", vmstate_bs);
  660. } else {
  661. error_setg(errp,
  662. "no block device can store vmstate for snapshot");
  663. }
  664. return NULL;
  665. }