block.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * QEMU live block migration
  3. *
  4. * Copyright IBM, Corp. 2009
  5. *
  6. * Authors:
  7. * Liran Schour <lirans@il.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qapi/error.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/main-loop.h"
  19. #include "qemu/cutils.h"
  20. #include "qemu/queue.h"
  21. #include "block.h"
  22. #include "migration/misc.h"
  23. #include "migration.h"
  24. #include "migration/register.h"
  25. #include "qemu-file.h"
  26. #include "migration/vmstate.h"
  27. #include "sysemu/block-backend.h"
  28. #define BLOCK_SIZE (1 << 20)
  29. #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
  30. #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
  31. #define BLK_MIG_FLAG_EOS 0x02
  32. #define BLK_MIG_FLAG_PROGRESS 0x04
  33. #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
  34. #define MAX_IS_ALLOCATED_SEARCH (65536 * BDRV_SECTOR_SIZE)
  35. #define MAX_IO_BUFFERS 512
  36. #define MAX_PARALLEL_IO 16
  37. //#define DEBUG_BLK_MIGRATION
  38. #ifdef DEBUG_BLK_MIGRATION
  39. #define DPRINTF(fmt, ...) \
  40. do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
  41. #else
  42. #define DPRINTF(fmt, ...) \
  43. do { } while (0)
  44. #endif
  45. typedef struct BlkMigDevState {
  46. /* Written during setup phase. Can be read without a lock. */
  47. BlockBackend *blk;
  48. char *blk_name;
  49. int shared_base;
  50. int64_t total_sectors;
  51. QSIMPLEQ_ENTRY(BlkMigDevState) entry;
  52. Error *blocker;
  53. /* Only used by migration thread. Does not need a lock. */
  54. int bulk_completed;
  55. int64_t cur_sector;
  56. int64_t cur_dirty;
  57. /* Data in the aio_bitmap is protected by block migration lock.
  58. * Allocation and free happen during setup and cleanup respectively.
  59. */
  60. unsigned long *aio_bitmap;
  61. /* Protected by block migration lock. */
  62. int64_t completed_sectors;
  63. /* During migration this is protected by iothread lock / AioContext.
  64. * Allocation and free happen during setup and cleanup respectively.
  65. */
  66. BdrvDirtyBitmap *dirty_bitmap;
  67. } BlkMigDevState;
  68. typedef struct BlkMigBlock {
  69. /* Only used by migration thread. */
  70. uint8_t *buf;
  71. BlkMigDevState *bmds;
  72. int64_t sector;
  73. int nr_sectors;
  74. QEMUIOVector qiov;
  75. BlockAIOCB *aiocb;
  76. /* Protected by block migration lock. */
  77. int ret;
  78. QSIMPLEQ_ENTRY(BlkMigBlock) entry;
  79. } BlkMigBlock;
  80. typedef struct BlkMigState {
  81. QSIMPLEQ_HEAD(, BlkMigDevState) bmds_list;
  82. int64_t total_sector_sum;
  83. bool zero_blocks;
  84. /* Protected by lock. */
  85. QSIMPLEQ_HEAD(, BlkMigBlock) blk_list;
  86. int submitted;
  87. int read_done;
  88. /* Only used by migration thread. Does not need a lock. */
  89. int transferred;
  90. int prev_progress;
  91. int bulk_completed;
  92. /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
  93. QemuMutex lock;
  94. } BlkMigState;
  95. static BlkMigState block_mig_state;
  96. static void blk_mig_lock(void)
  97. {
  98. qemu_mutex_lock(&block_mig_state.lock);
  99. }
  100. static void blk_mig_unlock(void)
  101. {
  102. qemu_mutex_unlock(&block_mig_state.lock);
  103. }
  104. /* Must run outside of the iothread lock during the bulk phase,
  105. * or the VM will stall.
  106. */
  107. static void blk_send(QEMUFile *f, BlkMigBlock * blk)
  108. {
  109. int len;
  110. uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
  111. if (block_mig_state.zero_blocks &&
  112. buffer_is_zero(blk->buf, BLOCK_SIZE)) {
  113. flags |= BLK_MIG_FLAG_ZERO_BLOCK;
  114. }
  115. /* sector number and flags */
  116. qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
  117. | flags);
  118. /* device name */
  119. len = strlen(blk->bmds->blk_name);
  120. qemu_put_byte(f, len);
  121. qemu_put_buffer(f, (uint8_t *) blk->bmds->blk_name, len);
  122. /* if a block is zero we need to flush here since the network
  123. * bandwidth is now a lot higher than the storage device bandwidth.
  124. * thus if we queue zero blocks we slow down the migration */
  125. if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
  126. qemu_fflush(f);
  127. return;
  128. }
  129. qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
  130. }
  131. int blk_mig_active(void)
  132. {
  133. return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
  134. }
  135. int blk_mig_bulk_active(void)
  136. {
  137. return blk_mig_active() && !block_mig_state.bulk_completed;
  138. }
  139. uint64_t blk_mig_bytes_transferred(void)
  140. {
  141. BlkMigDevState *bmds;
  142. uint64_t sum = 0;
  143. blk_mig_lock();
  144. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  145. sum += bmds->completed_sectors;
  146. }
  147. blk_mig_unlock();
  148. return sum << BDRV_SECTOR_BITS;
  149. }
  150. uint64_t blk_mig_bytes_remaining(void)
  151. {
  152. return blk_mig_bytes_total() - blk_mig_bytes_transferred();
  153. }
  154. uint64_t blk_mig_bytes_total(void)
  155. {
  156. BlkMigDevState *bmds;
  157. uint64_t sum = 0;
  158. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  159. sum += bmds->total_sectors;
  160. }
  161. return sum << BDRV_SECTOR_BITS;
  162. }
  163. /* Called with migration lock held. */
  164. static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
  165. {
  166. int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
  167. if (sector < blk_nb_sectors(bmds->blk)) {
  168. return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
  169. (1UL << (chunk % (sizeof(unsigned long) * 8))));
  170. } else {
  171. return 0;
  172. }
  173. }
  174. /* Called with migration lock held. */
  175. static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
  176. int nb_sectors, int set)
  177. {
  178. int64_t start, end;
  179. unsigned long val, idx, bit;
  180. start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
  181. end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
  182. for (; start <= end; start++) {
  183. idx = start / (sizeof(unsigned long) * 8);
  184. bit = start % (sizeof(unsigned long) * 8);
  185. val = bmds->aio_bitmap[idx];
  186. if (set) {
  187. val |= 1UL << bit;
  188. } else {
  189. val &= ~(1UL << bit);
  190. }
  191. bmds->aio_bitmap[idx] = val;
  192. }
  193. }
  194. static void alloc_aio_bitmap(BlkMigDevState *bmds)
  195. {
  196. BlockBackend *bb = bmds->blk;
  197. int64_t bitmap_size;
  198. bitmap_size = blk_nb_sectors(bb) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
  199. bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
  200. bmds->aio_bitmap = g_malloc0(bitmap_size);
  201. }
  202. /* Never hold migration lock when yielding to the main loop! */
  203. static void blk_mig_read_cb(void *opaque, int ret)
  204. {
  205. BlkMigBlock *blk = opaque;
  206. blk_mig_lock();
  207. blk->ret = ret;
  208. QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
  209. bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
  210. block_mig_state.submitted--;
  211. block_mig_state.read_done++;
  212. assert(block_mig_state.submitted >= 0);
  213. blk_mig_unlock();
  214. }
  215. /* Called with no lock taken. */
  216. static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
  217. {
  218. int64_t total_sectors = bmds->total_sectors;
  219. int64_t cur_sector = bmds->cur_sector;
  220. BlockBackend *bb = bmds->blk;
  221. BlkMigBlock *blk;
  222. int nr_sectors;
  223. int64_t count;
  224. if (bmds->shared_base) {
  225. qemu_mutex_lock_iothread();
  226. aio_context_acquire(blk_get_aio_context(bb));
  227. /* Skip unallocated sectors; intentionally treats failure or
  228. * partial sector as an allocated sector */
  229. while (cur_sector < total_sectors &&
  230. !bdrv_is_allocated(blk_bs(bb), cur_sector * BDRV_SECTOR_SIZE,
  231. MAX_IS_ALLOCATED_SEARCH, &count)) {
  232. if (count < BDRV_SECTOR_SIZE) {
  233. break;
  234. }
  235. cur_sector += count >> BDRV_SECTOR_BITS;
  236. }
  237. aio_context_release(blk_get_aio_context(bb));
  238. qemu_mutex_unlock_iothread();
  239. }
  240. if (cur_sector >= total_sectors) {
  241. bmds->cur_sector = bmds->completed_sectors = total_sectors;
  242. return 1;
  243. }
  244. bmds->completed_sectors = cur_sector;
  245. cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
  246. /* we are going to transfer a full block even if it is not allocated */
  247. nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
  248. if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
  249. nr_sectors = total_sectors - cur_sector;
  250. }
  251. blk = g_new(BlkMigBlock, 1);
  252. blk->buf = g_malloc(BLOCK_SIZE);
  253. blk->bmds = bmds;
  254. blk->sector = cur_sector;
  255. blk->nr_sectors = nr_sectors;
  256. qemu_iovec_init_buf(&blk->qiov, blk->buf, nr_sectors * BDRV_SECTOR_SIZE);
  257. blk_mig_lock();
  258. block_mig_state.submitted++;
  259. blk_mig_unlock();
  260. /* We do not know if bs is under the main thread (and thus does
  261. * not acquire the AioContext when doing AIO) or rather under
  262. * dataplane. Thus acquire both the iothread mutex and the
  263. * AioContext.
  264. *
  265. * This is ugly and will disappear when we make bdrv_* thread-safe,
  266. * without the need to acquire the AioContext.
  267. */
  268. qemu_mutex_lock_iothread();
  269. aio_context_acquire(blk_get_aio_context(bmds->blk));
  270. bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector * BDRV_SECTOR_SIZE,
  271. nr_sectors * BDRV_SECTOR_SIZE);
  272. blk->aiocb = blk_aio_preadv(bb, cur_sector * BDRV_SECTOR_SIZE, &blk->qiov,
  273. 0, blk_mig_read_cb, blk);
  274. aio_context_release(blk_get_aio_context(bmds->blk));
  275. qemu_mutex_unlock_iothread();
  276. bmds->cur_sector = cur_sector + nr_sectors;
  277. return (bmds->cur_sector >= total_sectors);
  278. }
  279. /* Called with iothread lock taken. */
  280. static int set_dirty_tracking(void)
  281. {
  282. BlkMigDevState *bmds;
  283. int ret;
  284. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  285. bmds->dirty_bitmap = bdrv_create_dirty_bitmap(blk_bs(bmds->blk),
  286. BLOCK_SIZE, NULL, NULL);
  287. if (!bmds->dirty_bitmap) {
  288. ret = -errno;
  289. goto fail;
  290. }
  291. }
  292. return 0;
  293. fail:
  294. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  295. if (bmds->dirty_bitmap) {
  296. bdrv_release_dirty_bitmap(bmds->dirty_bitmap);
  297. }
  298. }
  299. return ret;
  300. }
  301. /* Called with iothread lock taken. */
  302. static void unset_dirty_tracking(void)
  303. {
  304. BlkMigDevState *bmds;
  305. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  306. bdrv_release_dirty_bitmap(bmds->dirty_bitmap);
  307. }
  308. }
  309. static int init_blk_migration(QEMUFile *f)
  310. {
  311. BlockDriverState *bs;
  312. BlkMigDevState *bmds;
  313. int64_t sectors;
  314. BdrvNextIterator it;
  315. int i, num_bs = 0;
  316. struct {
  317. BlkMigDevState *bmds;
  318. BlockDriverState *bs;
  319. } *bmds_bs;
  320. Error *local_err = NULL;
  321. int ret;
  322. block_mig_state.submitted = 0;
  323. block_mig_state.read_done = 0;
  324. block_mig_state.transferred = 0;
  325. block_mig_state.total_sector_sum = 0;
  326. block_mig_state.prev_progress = -1;
  327. block_mig_state.bulk_completed = 0;
  328. block_mig_state.zero_blocks = migrate_zero_blocks();
  329. for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
  330. num_bs++;
  331. }
  332. bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs));
  333. for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) {
  334. if (bdrv_is_read_only(bs)) {
  335. continue;
  336. }
  337. sectors = bdrv_nb_sectors(bs);
  338. if (sectors <= 0) {
  339. ret = sectors;
  340. bdrv_next_cleanup(&it);
  341. goto out;
  342. }
  343. bmds = g_new0(BlkMigDevState, 1);
  344. bmds->blk = blk_new(qemu_get_aio_context(),
  345. BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
  346. bmds->blk_name = g_strdup(bdrv_get_device_name(bs));
  347. bmds->bulk_completed = 0;
  348. bmds->total_sectors = sectors;
  349. bmds->completed_sectors = 0;
  350. bmds->shared_base = migrate_use_block_incremental();
  351. assert(i < num_bs);
  352. bmds_bs[i].bmds = bmds;
  353. bmds_bs[i].bs = bs;
  354. block_mig_state.total_sector_sum += sectors;
  355. if (bmds->shared_base) {
  356. DPRINTF("Start migration for %s with shared base image\n",
  357. bdrv_get_device_name(bs));
  358. } else {
  359. DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
  360. }
  361. QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
  362. }
  363. /* Can only insert new BDSes now because doing so while iterating block
  364. * devices may end up in a deadlock (iterating the new BDSes, too). */
  365. for (i = 0; i < num_bs; i++) {
  366. BlkMigDevState *bmds = bmds_bs[i].bmds;
  367. BlockDriverState *bs = bmds_bs[i].bs;
  368. if (bmds) {
  369. ret = blk_insert_bs(bmds->blk, bs, &local_err);
  370. if (ret < 0) {
  371. error_report_err(local_err);
  372. goto out;
  373. }
  374. alloc_aio_bitmap(bmds);
  375. error_setg(&bmds->blocker, "block device is in use by migration");
  376. bdrv_op_block_all(bs, bmds->blocker);
  377. }
  378. }
  379. ret = 0;
  380. out:
  381. g_free(bmds_bs);
  382. return ret;
  383. }
  384. /* Called with no lock taken. */
  385. static int blk_mig_save_bulked_block(QEMUFile *f)
  386. {
  387. int64_t completed_sector_sum = 0;
  388. BlkMigDevState *bmds;
  389. int progress;
  390. int ret = 0;
  391. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  392. if (bmds->bulk_completed == 0) {
  393. if (mig_save_device_bulk(f, bmds) == 1) {
  394. /* completed bulk section for this device */
  395. bmds->bulk_completed = 1;
  396. }
  397. completed_sector_sum += bmds->completed_sectors;
  398. ret = 1;
  399. break;
  400. } else {
  401. completed_sector_sum += bmds->completed_sectors;
  402. }
  403. }
  404. if (block_mig_state.total_sector_sum != 0) {
  405. progress = completed_sector_sum * 100 /
  406. block_mig_state.total_sector_sum;
  407. } else {
  408. progress = 100;
  409. }
  410. if (progress != block_mig_state.prev_progress) {
  411. block_mig_state.prev_progress = progress;
  412. qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
  413. | BLK_MIG_FLAG_PROGRESS);
  414. DPRINTF("Completed %d %%\r", progress);
  415. }
  416. return ret;
  417. }
  418. static void blk_mig_reset_dirty_cursor(void)
  419. {
  420. BlkMigDevState *bmds;
  421. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  422. bmds->cur_dirty = 0;
  423. }
  424. }
  425. /* Called with iothread lock and AioContext taken. */
  426. static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
  427. int is_async)
  428. {
  429. BlkMigBlock *blk;
  430. int64_t total_sectors = bmds->total_sectors;
  431. int64_t sector;
  432. int nr_sectors;
  433. int ret = -EIO;
  434. for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
  435. blk_mig_lock();
  436. if (bmds_aio_inflight(bmds, sector)) {
  437. blk_mig_unlock();
  438. blk_drain(bmds->blk);
  439. } else {
  440. blk_mig_unlock();
  441. }
  442. bdrv_dirty_bitmap_lock(bmds->dirty_bitmap);
  443. if (bdrv_dirty_bitmap_get_locked(bmds->dirty_bitmap,
  444. sector * BDRV_SECTOR_SIZE)) {
  445. if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
  446. nr_sectors = total_sectors - sector;
  447. } else {
  448. nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
  449. }
  450. bdrv_reset_dirty_bitmap_locked(bmds->dirty_bitmap,
  451. sector * BDRV_SECTOR_SIZE,
  452. nr_sectors * BDRV_SECTOR_SIZE);
  453. bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap);
  454. blk = g_new(BlkMigBlock, 1);
  455. blk->buf = g_malloc(BLOCK_SIZE);
  456. blk->bmds = bmds;
  457. blk->sector = sector;
  458. blk->nr_sectors = nr_sectors;
  459. if (is_async) {
  460. qemu_iovec_init_buf(&blk->qiov, blk->buf,
  461. nr_sectors * BDRV_SECTOR_SIZE);
  462. blk->aiocb = blk_aio_preadv(bmds->blk,
  463. sector * BDRV_SECTOR_SIZE,
  464. &blk->qiov, 0, blk_mig_read_cb,
  465. blk);
  466. blk_mig_lock();
  467. block_mig_state.submitted++;
  468. bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
  469. blk_mig_unlock();
  470. } else {
  471. ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, blk->buf,
  472. nr_sectors * BDRV_SECTOR_SIZE);
  473. if (ret < 0) {
  474. goto error;
  475. }
  476. blk_send(f, blk);
  477. g_free(blk->buf);
  478. g_free(blk);
  479. }
  480. sector += nr_sectors;
  481. bmds->cur_dirty = sector;
  482. break;
  483. }
  484. bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap);
  485. sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
  486. bmds->cur_dirty = sector;
  487. }
  488. return (bmds->cur_dirty >= bmds->total_sectors);
  489. error:
  490. DPRINTF("Error reading sector %" PRId64 "\n", sector);
  491. g_free(blk->buf);
  492. g_free(blk);
  493. return ret;
  494. }
  495. /* Called with iothread lock taken.
  496. *
  497. * return value:
  498. * 0: too much data for max_downtime
  499. * 1: few enough data for max_downtime
  500. */
  501. static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
  502. {
  503. BlkMigDevState *bmds;
  504. int ret = 1;
  505. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  506. aio_context_acquire(blk_get_aio_context(bmds->blk));
  507. ret = mig_save_device_dirty(f, bmds, is_async);
  508. aio_context_release(blk_get_aio_context(bmds->blk));
  509. if (ret <= 0) {
  510. break;
  511. }
  512. }
  513. return ret;
  514. }
  515. /* Called with no locks taken. */
  516. static int flush_blks(QEMUFile *f)
  517. {
  518. BlkMigBlock *blk;
  519. int ret = 0;
  520. DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
  521. __func__, block_mig_state.submitted, block_mig_state.read_done,
  522. block_mig_state.transferred);
  523. blk_mig_lock();
  524. while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
  525. if (qemu_file_rate_limit(f)) {
  526. break;
  527. }
  528. if (blk->ret < 0) {
  529. ret = blk->ret;
  530. break;
  531. }
  532. QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
  533. blk_mig_unlock();
  534. blk_send(f, blk);
  535. blk_mig_lock();
  536. g_free(blk->buf);
  537. g_free(blk);
  538. block_mig_state.read_done--;
  539. block_mig_state.transferred++;
  540. assert(block_mig_state.read_done >= 0);
  541. }
  542. blk_mig_unlock();
  543. DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __func__,
  544. block_mig_state.submitted, block_mig_state.read_done,
  545. block_mig_state.transferred);
  546. return ret;
  547. }
  548. /* Called with iothread lock taken. */
  549. static int64_t get_remaining_dirty(void)
  550. {
  551. BlkMigDevState *bmds;
  552. int64_t dirty = 0;
  553. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  554. aio_context_acquire(blk_get_aio_context(bmds->blk));
  555. dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
  556. aio_context_release(blk_get_aio_context(bmds->blk));
  557. }
  558. return dirty;
  559. }
  560. /* Called with iothread lock taken. */
  561. static void block_migration_cleanup_bmds(void)
  562. {
  563. BlkMigDevState *bmds;
  564. AioContext *ctx;
  565. unset_dirty_tracking();
  566. while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
  567. QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
  568. bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker);
  569. error_free(bmds->blocker);
  570. /* Save ctx, because bmds->blk can disappear during blk_unref. */
  571. ctx = blk_get_aio_context(bmds->blk);
  572. aio_context_acquire(ctx);
  573. blk_unref(bmds->blk);
  574. aio_context_release(ctx);
  575. g_free(bmds->blk_name);
  576. g_free(bmds->aio_bitmap);
  577. g_free(bmds);
  578. }
  579. }
  580. /* Called with iothread lock taken. */
  581. static void block_migration_cleanup(void *opaque)
  582. {
  583. BlkMigBlock *blk;
  584. bdrv_drain_all();
  585. block_migration_cleanup_bmds();
  586. blk_mig_lock();
  587. while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
  588. QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
  589. g_free(blk->buf);
  590. g_free(blk);
  591. }
  592. blk_mig_unlock();
  593. }
  594. static int block_save_setup(QEMUFile *f, void *opaque)
  595. {
  596. int ret;
  597. DPRINTF("Enter save live setup submitted %d transferred %d\n",
  598. block_mig_state.submitted, block_mig_state.transferred);
  599. qemu_mutex_lock_iothread();
  600. ret = init_blk_migration(f);
  601. if (ret < 0) {
  602. qemu_mutex_unlock_iothread();
  603. return ret;
  604. }
  605. /* start track dirty blocks */
  606. ret = set_dirty_tracking();
  607. qemu_mutex_unlock_iothread();
  608. if (ret) {
  609. return ret;
  610. }
  611. ret = flush_blks(f);
  612. blk_mig_reset_dirty_cursor();
  613. qemu_put_be64(f, BLK_MIG_FLAG_EOS);
  614. return ret;
  615. }
  616. static int block_save_iterate(QEMUFile *f, void *opaque)
  617. {
  618. int ret;
  619. int64_t last_ftell = qemu_ftell(f);
  620. int64_t delta_ftell;
  621. DPRINTF("Enter save live iterate submitted %d transferred %d\n",
  622. block_mig_state.submitted, block_mig_state.transferred);
  623. ret = flush_blks(f);
  624. if (ret) {
  625. return ret;
  626. }
  627. blk_mig_reset_dirty_cursor();
  628. /* control the rate of transfer */
  629. blk_mig_lock();
  630. while (block_mig_state.read_done * BLOCK_SIZE <
  631. qemu_file_get_rate_limit(f) &&
  632. block_mig_state.submitted < MAX_PARALLEL_IO &&
  633. (block_mig_state.submitted + block_mig_state.read_done) <
  634. MAX_IO_BUFFERS) {
  635. blk_mig_unlock();
  636. if (block_mig_state.bulk_completed == 0) {
  637. /* first finish the bulk phase */
  638. if (blk_mig_save_bulked_block(f) == 0) {
  639. /* finished saving bulk on all devices */
  640. block_mig_state.bulk_completed = 1;
  641. }
  642. ret = 0;
  643. } else {
  644. /* Always called with iothread lock taken for
  645. * simplicity, block_save_complete also calls it.
  646. */
  647. qemu_mutex_lock_iothread();
  648. ret = blk_mig_save_dirty_block(f, 1);
  649. qemu_mutex_unlock_iothread();
  650. }
  651. if (ret < 0) {
  652. return ret;
  653. }
  654. blk_mig_lock();
  655. if (ret != 0) {
  656. /* no more dirty blocks */
  657. break;
  658. }
  659. }
  660. blk_mig_unlock();
  661. ret = flush_blks(f);
  662. if (ret) {
  663. return ret;
  664. }
  665. qemu_put_be64(f, BLK_MIG_FLAG_EOS);
  666. delta_ftell = qemu_ftell(f) - last_ftell;
  667. if (delta_ftell > 0) {
  668. return 1;
  669. } else if (delta_ftell < 0) {
  670. return -1;
  671. } else {
  672. return 0;
  673. }
  674. }
  675. /* Called with iothread lock taken. */
  676. static int block_save_complete(QEMUFile *f, void *opaque)
  677. {
  678. int ret;
  679. DPRINTF("Enter save live complete submitted %d transferred %d\n",
  680. block_mig_state.submitted, block_mig_state.transferred);
  681. ret = flush_blks(f);
  682. if (ret) {
  683. return ret;
  684. }
  685. blk_mig_reset_dirty_cursor();
  686. /* we know for sure that save bulk is completed and
  687. all async read completed */
  688. blk_mig_lock();
  689. assert(block_mig_state.submitted == 0);
  690. blk_mig_unlock();
  691. do {
  692. ret = blk_mig_save_dirty_block(f, 0);
  693. if (ret < 0) {
  694. return ret;
  695. }
  696. } while (ret == 0);
  697. /* report completion */
  698. qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
  699. DPRINTF("Block migration completed\n");
  700. qemu_put_be64(f, BLK_MIG_FLAG_EOS);
  701. /* Make sure that our BlockBackends are gone, so that the block driver
  702. * nodes can be inactivated. */
  703. block_migration_cleanup_bmds();
  704. return 0;
  705. }
  706. static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
  707. uint64_t *res_precopy_only,
  708. uint64_t *res_compatible,
  709. uint64_t *res_postcopy_only)
  710. {
  711. /* Estimate pending number of bytes to send */
  712. uint64_t pending;
  713. qemu_mutex_lock_iothread();
  714. pending = get_remaining_dirty();
  715. qemu_mutex_unlock_iothread();
  716. blk_mig_lock();
  717. pending += block_mig_state.submitted * BLOCK_SIZE +
  718. block_mig_state.read_done * BLOCK_SIZE;
  719. blk_mig_unlock();
  720. /* Report at least one block pending during bulk phase */
  721. if (pending <= max_size && !block_mig_state.bulk_completed) {
  722. pending = max_size + BLOCK_SIZE;
  723. }
  724. DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
  725. /* We don't do postcopy */
  726. *res_precopy_only += pending;
  727. }
  728. static int block_load(QEMUFile *f, void *opaque, int version_id)
  729. {
  730. static int banner_printed;
  731. int len, flags;
  732. char device_name[256];
  733. int64_t addr;
  734. BlockBackend *blk, *blk_prev = NULL;
  735. Error *local_err = NULL;
  736. uint8_t *buf;
  737. int64_t total_sectors = 0;
  738. int nr_sectors;
  739. int ret;
  740. BlockDriverInfo bdi;
  741. int cluster_size = BLOCK_SIZE;
  742. do {
  743. addr = qemu_get_be64(f);
  744. flags = addr & (BDRV_SECTOR_SIZE - 1);
  745. addr >>= BDRV_SECTOR_BITS;
  746. if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
  747. /* get device name */
  748. len = qemu_get_byte(f);
  749. qemu_get_buffer(f, (uint8_t *)device_name, len);
  750. device_name[len] = '\0';
  751. blk = blk_by_name(device_name);
  752. if (!blk) {
  753. fprintf(stderr, "Error unknown block device %s\n",
  754. device_name);
  755. return -EINVAL;
  756. }
  757. if (blk != blk_prev) {
  758. blk_prev = blk;
  759. total_sectors = blk_nb_sectors(blk);
  760. if (total_sectors <= 0) {
  761. error_report("Error getting length of block device %s",
  762. device_name);
  763. return -EINVAL;
  764. }
  765. blk_invalidate_cache(blk, &local_err);
  766. if (local_err) {
  767. error_report_err(local_err);
  768. return -EINVAL;
  769. }
  770. ret = bdrv_get_info(blk_bs(blk), &bdi);
  771. if (ret == 0 && bdi.cluster_size > 0 &&
  772. bdi.cluster_size <= BLOCK_SIZE &&
  773. BLOCK_SIZE % bdi.cluster_size == 0) {
  774. cluster_size = bdi.cluster_size;
  775. } else {
  776. cluster_size = BLOCK_SIZE;
  777. }
  778. }
  779. if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
  780. nr_sectors = total_sectors - addr;
  781. } else {
  782. nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
  783. }
  784. if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
  785. ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE,
  786. nr_sectors * BDRV_SECTOR_SIZE,
  787. BDRV_REQ_MAY_UNMAP);
  788. } else {
  789. int i;
  790. int64_t cur_addr;
  791. uint8_t *cur_buf;
  792. buf = g_malloc(BLOCK_SIZE);
  793. qemu_get_buffer(f, buf, BLOCK_SIZE);
  794. for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
  795. cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size;
  796. cur_buf = buf + i * cluster_size;
  797. if ((!block_mig_state.zero_blocks ||
  798. cluster_size < BLOCK_SIZE) &&
  799. buffer_is_zero(cur_buf, cluster_size)) {
  800. ret = blk_pwrite_zeroes(blk, cur_addr,
  801. cluster_size,
  802. BDRV_REQ_MAY_UNMAP);
  803. } else {
  804. ret = blk_pwrite(blk, cur_addr, cur_buf,
  805. cluster_size, 0);
  806. }
  807. if (ret < 0) {
  808. break;
  809. }
  810. }
  811. g_free(buf);
  812. }
  813. if (ret < 0) {
  814. return ret;
  815. }
  816. } else if (flags & BLK_MIG_FLAG_PROGRESS) {
  817. if (!banner_printed) {
  818. printf("Receiving block device images\n");
  819. banner_printed = 1;
  820. }
  821. printf("Completed %d %%%c", (int)addr,
  822. (addr == 100) ? '\n' : '\r');
  823. fflush(stdout);
  824. } else if (!(flags & BLK_MIG_FLAG_EOS)) {
  825. fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
  826. return -EINVAL;
  827. }
  828. ret = qemu_file_get_error(f);
  829. if (ret != 0) {
  830. return ret;
  831. }
  832. } while (!(flags & BLK_MIG_FLAG_EOS));
  833. return 0;
  834. }
  835. static bool block_is_active(void *opaque)
  836. {
  837. return migrate_use_block();
  838. }
  839. static SaveVMHandlers savevm_block_handlers = {
  840. .save_setup = block_save_setup,
  841. .save_live_iterate = block_save_iterate,
  842. .save_live_complete_precopy = block_save_complete,
  843. .save_live_pending = block_save_pending,
  844. .load_state = block_load,
  845. .save_cleanup = block_migration_cleanup,
  846. .is_active = block_is_active,
  847. };
  848. void blk_mig_init(void)
  849. {
  850. QSIMPLEQ_INIT(&block_mig_state.bmds_list);
  851. QSIMPLEQ_INIT(&block_mig_state.blk_list);
  852. qemu_mutex_init(&block_mig_state.lock);
  853. register_savevm_live("block", 0, 1, &savevm_block_handlers,
  854. &block_mig_state);
  855. }