block-dirty-bitmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Block dirty bitmap postcopy migration
  3. *
  4. * Copyright IBM, Corp. 2009
  5. * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
  6. *
  7. * Authors:
  8. * Liran Schour <lirans@il.ibm.com>
  9. * Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. * This file is derived from migration/block.c, so it's author and IBM copyright
  14. * are here, although content is quite different.
  15. *
  16. * Contributions after 2012-01-13 are licensed under the terms of the
  17. * GNU GPL, version 2 or (at your option) any later version.
  18. *
  19. * ***
  20. *
  21. * Here postcopy migration of dirty bitmaps is realized. Only QMP-addressable
  22. * bitmaps are migrated.
  23. *
  24. * Bitmap migration implies creating bitmap with the same name and granularity
  25. * in destination QEMU. If the bitmap with the same name (for the same node)
  26. * already exists on destination an error will be generated.
  27. *
  28. * format of migration:
  29. *
  30. * # Header (shared for different chunk types)
  31. * 1, 2 or 4 bytes: flags (see qemu_{put,put}_flags)
  32. * [ 1 byte: node name size ] \ flags & DEVICE_NAME
  33. * [ n bytes: node name ] /
  34. * [ 1 byte: bitmap name size ] \ flags & BITMAP_NAME
  35. * [ n bytes: bitmap name ] /
  36. *
  37. * # Start of bitmap migration (flags & START)
  38. * header
  39. * be64: granularity
  40. * 1 byte: bitmap flags (corresponds to BdrvDirtyBitmap)
  41. * bit 0 - bitmap is enabled
  42. * bit 1 - bitmap is persistent
  43. * bit 2 - bitmap is autoloading
  44. * bits 3-7 - reserved, must be zero
  45. *
  46. * # Complete of bitmap migration (flags & COMPLETE)
  47. * header
  48. *
  49. * # Data chunk of bitmap migration
  50. * header
  51. * be64: start sector
  52. * be32: number of sectors
  53. * [ be64: buffer size ] \ ! (flags & ZEROES)
  54. * [ n bytes: buffer ] /
  55. *
  56. * The last chunk in stream should contain flags & EOS. The chunk may skip
  57. * device and/or bitmap names, assuming them to be the same with the previous
  58. * chunk.
  59. */
  60. #include "qemu/osdep.h"
  61. #include "block/block.h"
  62. #include "block/block_int.h"
  63. #include "sysemu/block-backend.h"
  64. #include "sysemu/runstate.h"
  65. #include "qemu/main-loop.h"
  66. #include "qemu/error-report.h"
  67. #include "migration/misc.h"
  68. #include "migration/migration.h"
  69. #include "qemu-file.h"
  70. #include "migration/vmstate.h"
  71. #include "migration/register.h"
  72. #include "qemu/hbitmap.h"
  73. #include "qemu/cutils.h"
  74. #include "qapi/error.h"
  75. #include "trace.h"
  76. #define CHUNK_SIZE (1 << 10)
  77. /* Flags occupy one, two or four bytes (Big Endian). The size is determined as
  78. * follows:
  79. * in first (most significant) byte bit 8 is clear --> one byte
  80. * in first byte bit 8 is set --> two or four bytes, depending on second
  81. * byte:
  82. * | in second byte bit 8 is clear --> two bytes
  83. * | in second byte bit 8 is set --> four bytes
  84. */
  85. #define DIRTY_BITMAP_MIG_FLAG_EOS 0x01
  86. #define DIRTY_BITMAP_MIG_FLAG_ZEROES 0x02
  87. #define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME 0x04
  88. #define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME 0x08
  89. #define DIRTY_BITMAP_MIG_FLAG_START 0x10
  90. #define DIRTY_BITMAP_MIG_FLAG_COMPLETE 0x20
  91. #define DIRTY_BITMAP_MIG_FLAG_BITS 0x40
  92. #define DIRTY_BITMAP_MIG_EXTRA_FLAGS 0x80
  93. #define DIRTY_BITMAP_MIG_START_FLAG_ENABLED 0x01
  94. #define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT 0x02
  95. /* 0x04 was "AUTOLOAD" flags on elder versions, no it is ignored */
  96. #define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK 0xf8
  97. typedef struct DirtyBitmapMigBitmapState {
  98. /* Written during setup phase. */
  99. BlockDriverState *bs;
  100. const char *node_name;
  101. BdrvDirtyBitmap *bitmap;
  102. uint64_t total_sectors;
  103. uint64_t sectors_per_chunk;
  104. QSIMPLEQ_ENTRY(DirtyBitmapMigBitmapState) entry;
  105. uint8_t flags;
  106. /* For bulk phase. */
  107. bool bulk_completed;
  108. uint64_t cur_sector;
  109. } DirtyBitmapMigBitmapState;
  110. typedef struct DirtyBitmapMigState {
  111. QSIMPLEQ_HEAD(, DirtyBitmapMigBitmapState) dbms_list;
  112. bool bulk_completed;
  113. bool no_bitmaps;
  114. /* for send_bitmap_bits() */
  115. BlockDriverState *prev_bs;
  116. BdrvDirtyBitmap *prev_bitmap;
  117. } DirtyBitmapMigState;
  118. typedef struct DirtyBitmapLoadState {
  119. uint32_t flags;
  120. char node_name[256];
  121. char bitmap_name[256];
  122. BlockDriverState *bs;
  123. BdrvDirtyBitmap *bitmap;
  124. } DirtyBitmapLoadState;
  125. static DirtyBitmapMigState dirty_bitmap_mig_state;
  126. typedef struct DirtyBitmapLoadBitmapState {
  127. BlockDriverState *bs;
  128. BdrvDirtyBitmap *bitmap;
  129. bool migrated;
  130. } DirtyBitmapLoadBitmapState;
  131. static GSList *enabled_bitmaps;
  132. QemuMutex finish_lock;
  133. void init_dirty_bitmap_incoming_migration(void)
  134. {
  135. qemu_mutex_init(&finish_lock);
  136. }
  137. static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
  138. {
  139. uint8_t flags = qemu_get_byte(f);
  140. if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
  141. flags = flags << 8 | qemu_get_byte(f);
  142. if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
  143. flags = flags << 16 | qemu_get_be16(f);
  144. }
  145. }
  146. return flags;
  147. }
  148. static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
  149. {
  150. /* The code currently do not send flags more than one byte */
  151. assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
  152. qemu_put_byte(f, flags);
  153. }
  154. static void send_bitmap_header(QEMUFile *f, DirtyBitmapMigBitmapState *dbms,
  155. uint32_t additional_flags)
  156. {
  157. BlockDriverState *bs = dbms->bs;
  158. BdrvDirtyBitmap *bitmap = dbms->bitmap;
  159. uint32_t flags = additional_flags;
  160. trace_send_bitmap_header_enter();
  161. if (bs != dirty_bitmap_mig_state.prev_bs) {
  162. dirty_bitmap_mig_state.prev_bs = bs;
  163. flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
  164. }
  165. if (bitmap != dirty_bitmap_mig_state.prev_bitmap) {
  166. dirty_bitmap_mig_state.prev_bitmap = bitmap;
  167. flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
  168. }
  169. qemu_put_bitmap_flags(f, flags);
  170. if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
  171. qemu_put_counted_string(f, dbms->node_name);
  172. }
  173. if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
  174. qemu_put_counted_string(f, bdrv_dirty_bitmap_name(bitmap));
  175. }
  176. }
  177. static void send_bitmap_start(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
  178. {
  179. send_bitmap_header(f, dbms, DIRTY_BITMAP_MIG_FLAG_START);
  180. qemu_put_be32(f, bdrv_dirty_bitmap_granularity(dbms->bitmap));
  181. qemu_put_byte(f, dbms->flags);
  182. }
  183. static void send_bitmap_complete(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
  184. {
  185. send_bitmap_header(f, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
  186. }
  187. static void send_bitmap_bits(QEMUFile *f, DirtyBitmapMigBitmapState *dbms,
  188. uint64_t start_sector, uint32_t nr_sectors)
  189. {
  190. /* align for buffer_is_zero() */
  191. uint64_t align = 4 * sizeof(long);
  192. uint64_t unaligned_size =
  193. bdrv_dirty_bitmap_serialization_size(
  194. dbms->bitmap, start_sector << BDRV_SECTOR_BITS,
  195. (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
  196. uint64_t buf_size = QEMU_ALIGN_UP(unaligned_size, align);
  197. uint8_t *buf = g_malloc0(buf_size);
  198. uint32_t flags = DIRTY_BITMAP_MIG_FLAG_BITS;
  199. bdrv_dirty_bitmap_serialize_part(
  200. dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
  201. (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
  202. if (buffer_is_zero(buf, buf_size)) {
  203. g_free(buf);
  204. buf = NULL;
  205. flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
  206. }
  207. trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
  208. send_bitmap_header(f, dbms, flags);
  209. qemu_put_be64(f, start_sector);
  210. qemu_put_be32(f, nr_sectors);
  211. /* if a block is zero we need to flush here since the network
  212. * bandwidth is now a lot higher than the storage device bandwidth.
  213. * thus if we queue zero blocks we slow down the migration. */
  214. if (flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
  215. qemu_fflush(f);
  216. } else {
  217. qemu_put_be64(f, buf_size);
  218. qemu_put_buffer(f, buf, buf_size);
  219. }
  220. g_free(buf);
  221. }
  222. /* Called with iothread lock taken. */
  223. static void dirty_bitmap_mig_cleanup(void)
  224. {
  225. DirtyBitmapMigBitmapState *dbms;
  226. while ((dbms = QSIMPLEQ_FIRST(&dirty_bitmap_mig_state.dbms_list)) != NULL) {
  227. QSIMPLEQ_REMOVE_HEAD(&dirty_bitmap_mig_state.dbms_list, entry);
  228. bdrv_dirty_bitmap_set_busy(dbms->bitmap, false);
  229. bdrv_unref(dbms->bs);
  230. g_free(dbms);
  231. }
  232. }
  233. /* Called with iothread lock taken. */
  234. static int init_dirty_bitmap_migration(void)
  235. {
  236. BlockDriverState *bs;
  237. BdrvDirtyBitmap *bitmap;
  238. DirtyBitmapMigBitmapState *dbms;
  239. Error *local_err = NULL;
  240. dirty_bitmap_mig_state.bulk_completed = false;
  241. dirty_bitmap_mig_state.prev_bs = NULL;
  242. dirty_bitmap_mig_state.prev_bitmap = NULL;
  243. dirty_bitmap_mig_state.no_bitmaps = false;
  244. for (bs = bdrv_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
  245. const char *name = bdrv_get_device_or_node_name(bs);
  246. FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
  247. if (!bdrv_dirty_bitmap_name(bitmap)) {
  248. continue;
  249. }
  250. if (!name || strcmp(name, "") == 0) {
  251. error_report("Found bitmap '%s' in unnamed node %p. It can't "
  252. "be migrated", bdrv_dirty_bitmap_name(bitmap), bs);
  253. goto fail;
  254. }
  255. if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT,
  256. &local_err)) {
  257. error_report_err(local_err);
  258. goto fail;
  259. }
  260. bdrv_ref(bs);
  261. bdrv_dirty_bitmap_set_busy(bitmap, true);
  262. dbms = g_new0(DirtyBitmapMigBitmapState, 1);
  263. dbms->bs = bs;
  264. dbms->node_name = name;
  265. dbms->bitmap = bitmap;
  266. dbms->total_sectors = bdrv_nb_sectors(bs);
  267. dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
  268. bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
  269. if (bdrv_dirty_bitmap_enabled(bitmap)) {
  270. dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
  271. }
  272. if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
  273. dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
  274. }
  275. QSIMPLEQ_INSERT_TAIL(&dirty_bitmap_mig_state.dbms_list,
  276. dbms, entry);
  277. }
  278. }
  279. /* unset migration flags here, to not roll back it */
  280. QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
  281. bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
  282. }
  283. if (QSIMPLEQ_EMPTY(&dirty_bitmap_mig_state.dbms_list)) {
  284. dirty_bitmap_mig_state.no_bitmaps = true;
  285. }
  286. return 0;
  287. fail:
  288. dirty_bitmap_mig_cleanup();
  289. return -1;
  290. }
  291. /* Called with no lock taken. */
  292. static void bulk_phase_send_chunk(QEMUFile *f, DirtyBitmapMigBitmapState *dbms)
  293. {
  294. uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
  295. dbms->sectors_per_chunk);
  296. send_bitmap_bits(f, dbms, dbms->cur_sector, nr_sectors);
  297. dbms->cur_sector += nr_sectors;
  298. if (dbms->cur_sector >= dbms->total_sectors) {
  299. dbms->bulk_completed = true;
  300. }
  301. }
  302. /* Called with no lock taken. */
  303. static void bulk_phase(QEMUFile *f, bool limit)
  304. {
  305. DirtyBitmapMigBitmapState *dbms;
  306. QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
  307. while (!dbms->bulk_completed) {
  308. bulk_phase_send_chunk(f, dbms);
  309. if (limit && qemu_file_rate_limit(f)) {
  310. return;
  311. }
  312. }
  313. }
  314. dirty_bitmap_mig_state.bulk_completed = true;
  315. }
  316. /* for SaveVMHandlers */
  317. static void dirty_bitmap_save_cleanup(void *opaque)
  318. {
  319. dirty_bitmap_mig_cleanup();
  320. }
  321. static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
  322. {
  323. trace_dirty_bitmap_save_iterate(migration_in_postcopy());
  324. if (migration_in_postcopy() && !dirty_bitmap_mig_state.bulk_completed) {
  325. bulk_phase(f, true);
  326. }
  327. qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
  328. return dirty_bitmap_mig_state.bulk_completed;
  329. }
  330. /* Called with iothread lock taken. */
  331. static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
  332. {
  333. DirtyBitmapMigBitmapState *dbms;
  334. trace_dirty_bitmap_save_complete_enter();
  335. if (!dirty_bitmap_mig_state.bulk_completed) {
  336. bulk_phase(f, false);
  337. }
  338. QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
  339. send_bitmap_complete(f, dbms);
  340. }
  341. qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
  342. trace_dirty_bitmap_save_complete_finish();
  343. dirty_bitmap_mig_cleanup();
  344. return 0;
  345. }
  346. static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
  347. uint64_t max_size,
  348. uint64_t *res_precopy_only,
  349. uint64_t *res_compatible,
  350. uint64_t *res_postcopy_only)
  351. {
  352. DirtyBitmapMigBitmapState *dbms;
  353. uint64_t pending = 0;
  354. qemu_mutex_lock_iothread();
  355. QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
  356. uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
  357. uint64_t sectors = dbms->bulk_completed ? 0 :
  358. dbms->total_sectors - dbms->cur_sector;
  359. pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
  360. }
  361. qemu_mutex_unlock_iothread();
  362. trace_dirty_bitmap_save_pending(pending, max_size);
  363. *res_postcopy_only += pending;
  364. }
  365. /* First occurrence of this bitmap. It should be created if doesn't exist */
  366. static int dirty_bitmap_load_start(QEMUFile *f, DirtyBitmapLoadState *s)
  367. {
  368. Error *local_err = NULL;
  369. uint32_t granularity = qemu_get_be32(f);
  370. uint8_t flags = qemu_get_byte(f);
  371. if (s->bitmap) {
  372. error_report("Bitmap with the same name ('%s') already exists on "
  373. "destination", bdrv_dirty_bitmap_name(s->bitmap));
  374. return -EINVAL;
  375. } else {
  376. s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
  377. s->bitmap_name, &local_err);
  378. if (!s->bitmap) {
  379. error_report_err(local_err);
  380. return -EINVAL;
  381. }
  382. }
  383. if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
  384. error_report("Unknown flags in migrated dirty bitmap header: %x",
  385. flags);
  386. return -EINVAL;
  387. }
  388. if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
  389. bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
  390. }
  391. bdrv_disable_dirty_bitmap(s->bitmap);
  392. if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
  393. DirtyBitmapLoadBitmapState *b;
  394. bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
  395. if (local_err) {
  396. error_report_err(local_err);
  397. return -EINVAL;
  398. }
  399. b = g_new(DirtyBitmapLoadBitmapState, 1);
  400. b->bs = s->bs;
  401. b->bitmap = s->bitmap;
  402. b->migrated = false;
  403. enabled_bitmaps = g_slist_prepend(enabled_bitmaps, b);
  404. }
  405. return 0;
  406. }
  407. void dirty_bitmap_mig_before_vm_start(void)
  408. {
  409. GSList *item;
  410. qemu_mutex_lock(&finish_lock);
  411. for (item = enabled_bitmaps; item; item = g_slist_next(item)) {
  412. DirtyBitmapLoadBitmapState *b = item->data;
  413. if (b->migrated) {
  414. bdrv_enable_dirty_bitmap_locked(b->bitmap);
  415. } else {
  416. bdrv_dirty_bitmap_enable_successor(b->bitmap);
  417. }
  418. g_free(b);
  419. }
  420. g_slist_free(enabled_bitmaps);
  421. enabled_bitmaps = NULL;
  422. qemu_mutex_unlock(&finish_lock);
  423. }
  424. static void dirty_bitmap_load_complete(QEMUFile *f, DirtyBitmapLoadState *s)
  425. {
  426. GSList *item;
  427. trace_dirty_bitmap_load_complete();
  428. bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
  429. qemu_mutex_lock(&finish_lock);
  430. for (item = enabled_bitmaps; item; item = g_slist_next(item)) {
  431. DirtyBitmapLoadBitmapState *b = item->data;
  432. if (b->bitmap == s->bitmap) {
  433. b->migrated = true;
  434. break;
  435. }
  436. }
  437. if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
  438. bdrv_dirty_bitmap_lock(s->bitmap);
  439. if (enabled_bitmaps == NULL) {
  440. /* in postcopy */
  441. bdrv_reclaim_dirty_bitmap_locked(s->bitmap, &error_abort);
  442. bdrv_enable_dirty_bitmap_locked(s->bitmap);
  443. } else {
  444. /* target not started, successor must be empty */
  445. int64_t count = bdrv_get_dirty_count(s->bitmap);
  446. BdrvDirtyBitmap *ret = bdrv_reclaim_dirty_bitmap_locked(s->bitmap,
  447. NULL);
  448. /* bdrv_reclaim_dirty_bitmap can fail only on no successor (it
  449. * must be) or on merge fail, but merge can't fail when second
  450. * bitmap is empty
  451. */
  452. assert(ret == s->bitmap &&
  453. count == bdrv_get_dirty_count(s->bitmap));
  454. }
  455. bdrv_dirty_bitmap_unlock(s->bitmap);
  456. }
  457. qemu_mutex_unlock(&finish_lock);
  458. }
  459. static int dirty_bitmap_load_bits(QEMUFile *f, DirtyBitmapLoadState *s)
  460. {
  461. uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
  462. uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
  463. trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
  464. nr_bytes >> BDRV_SECTOR_BITS);
  465. if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
  466. trace_dirty_bitmap_load_bits_zeroes();
  467. bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte, nr_bytes,
  468. false);
  469. } else {
  470. size_t ret;
  471. uint8_t *buf;
  472. uint64_t buf_size = qemu_get_be64(f);
  473. uint64_t needed_size =
  474. bdrv_dirty_bitmap_serialization_size(s->bitmap,
  475. first_byte, nr_bytes);
  476. if (needed_size > buf_size ||
  477. buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
  478. /* Here used same alignment as in send_bitmap_bits */
  479. ) {
  480. error_report("Migrated bitmap granularity doesn't "
  481. "match the destination bitmap '%s' granularity",
  482. bdrv_dirty_bitmap_name(s->bitmap));
  483. return -EINVAL;
  484. }
  485. buf = g_malloc(buf_size);
  486. ret = qemu_get_buffer(f, buf, buf_size);
  487. if (ret != buf_size) {
  488. error_report("Failed to read bitmap bits");
  489. g_free(buf);
  490. return -EIO;
  491. }
  492. bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
  493. false);
  494. g_free(buf);
  495. }
  496. return 0;
  497. }
  498. static int dirty_bitmap_load_header(QEMUFile *f, DirtyBitmapLoadState *s)
  499. {
  500. Error *local_err = NULL;
  501. bool nothing;
  502. s->flags = qemu_get_bitmap_flags(f);
  503. trace_dirty_bitmap_load_header(s->flags);
  504. nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
  505. if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
  506. if (!qemu_get_counted_string(f, s->node_name)) {
  507. error_report("Unable to read node name string");
  508. return -EINVAL;
  509. }
  510. s->bs = bdrv_lookup_bs(s->node_name, s->node_name, &local_err);
  511. if (!s->bs) {
  512. error_report_err(local_err);
  513. return -EINVAL;
  514. }
  515. } else if (!s->bs && !nothing) {
  516. error_report("Error: block device name is not set");
  517. return -EINVAL;
  518. }
  519. if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
  520. if (!qemu_get_counted_string(f, s->bitmap_name)) {
  521. error_report("Unable to read bitmap name string");
  522. return -EINVAL;
  523. }
  524. s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
  525. /* bitmap may be NULL here, it wouldn't be an error if it is the
  526. * first occurrence of the bitmap */
  527. if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
  528. error_report("Error: unknown dirty bitmap "
  529. "'%s' for block device '%s'",
  530. s->bitmap_name, s->node_name);
  531. return -EINVAL;
  532. }
  533. } else if (!s->bitmap && !nothing) {
  534. error_report("Error: block device name is not set");
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
  540. {
  541. static DirtyBitmapLoadState s;
  542. int ret = 0;
  543. trace_dirty_bitmap_load_enter();
  544. if (version_id != 1) {
  545. return -EINVAL;
  546. }
  547. do {
  548. ret = dirty_bitmap_load_header(f, &s);
  549. if (ret < 0) {
  550. return ret;
  551. }
  552. if (s.flags & DIRTY_BITMAP_MIG_FLAG_START) {
  553. ret = dirty_bitmap_load_start(f, &s);
  554. } else if (s.flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
  555. dirty_bitmap_load_complete(f, &s);
  556. } else if (s.flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
  557. ret = dirty_bitmap_load_bits(f, &s);
  558. }
  559. if (!ret) {
  560. ret = qemu_file_get_error(f);
  561. }
  562. if (ret) {
  563. return ret;
  564. }
  565. } while (!(s.flags & DIRTY_BITMAP_MIG_FLAG_EOS));
  566. trace_dirty_bitmap_load_success();
  567. return 0;
  568. }
  569. static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
  570. {
  571. DirtyBitmapMigBitmapState *dbms = NULL;
  572. if (init_dirty_bitmap_migration() < 0) {
  573. return -1;
  574. }
  575. QSIMPLEQ_FOREACH(dbms, &dirty_bitmap_mig_state.dbms_list, entry) {
  576. send_bitmap_start(f, dbms);
  577. }
  578. qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
  579. return 0;
  580. }
  581. static bool dirty_bitmap_is_active(void *opaque)
  582. {
  583. return migrate_dirty_bitmaps() && !dirty_bitmap_mig_state.no_bitmaps;
  584. }
  585. static bool dirty_bitmap_is_active_iterate(void *opaque)
  586. {
  587. return dirty_bitmap_is_active(opaque) && !runstate_is_running();
  588. }
  589. static bool dirty_bitmap_has_postcopy(void *opaque)
  590. {
  591. return true;
  592. }
  593. static SaveVMHandlers savevm_dirty_bitmap_handlers = {
  594. .save_setup = dirty_bitmap_save_setup,
  595. .save_live_complete_postcopy = dirty_bitmap_save_complete,
  596. .save_live_complete_precopy = dirty_bitmap_save_complete,
  597. .has_postcopy = dirty_bitmap_has_postcopy,
  598. .save_live_pending = dirty_bitmap_save_pending,
  599. .save_live_iterate = dirty_bitmap_save_iterate,
  600. .is_active_iterate = dirty_bitmap_is_active_iterate,
  601. .load_state = dirty_bitmap_load,
  602. .save_cleanup = dirty_bitmap_save_cleanup,
  603. .is_active = dirty_bitmap_is_active,
  604. };
  605. void dirty_bitmap_mig_init(void)
  606. {
  607. QSIMPLEQ_INIT(&dirty_bitmap_mig_state.dbms_list);
  608. register_savevm_live("dirty-bitmap", 0, 1,
  609. &savevm_dirty_bitmap_handlers,
  610. &dirty_bitmap_mig_state);
  611. }