block.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 BLK_MIG_BLOCK_SIZE (1 << 20)
  29. #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLK_MIG_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, BLK_MIG_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, BLK_MIG_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(BLK_MIG_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. BLK_MIG_BLOCK_SIZE,
  287. NULL, NULL);
  288. if (!bmds->dirty_bitmap) {
  289. ret = -errno;
  290. goto fail;
  291. }
  292. }
  293. return 0;
  294. fail:
  295. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  296. if (bmds->dirty_bitmap) {
  297. bdrv_release_dirty_bitmap(bmds->dirty_bitmap);
  298. }
  299. }
  300. return ret;
  301. }
  302. /* Called with iothread lock taken. */
  303. static void unset_dirty_tracking(void)
  304. {
  305. BlkMigDevState *bmds;
  306. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  307. bdrv_release_dirty_bitmap(bmds->dirty_bitmap);
  308. }
  309. }
  310. static int init_blk_migration(QEMUFile *f)
  311. {
  312. BlockDriverState *bs;
  313. BlkMigDevState *bmds;
  314. int64_t sectors;
  315. BdrvNextIterator it;
  316. int i, num_bs = 0;
  317. struct {
  318. BlkMigDevState *bmds;
  319. BlockDriverState *bs;
  320. } *bmds_bs;
  321. Error *local_err = NULL;
  322. int ret;
  323. block_mig_state.submitted = 0;
  324. block_mig_state.read_done = 0;
  325. block_mig_state.transferred = 0;
  326. block_mig_state.total_sector_sum = 0;
  327. block_mig_state.prev_progress = -1;
  328. block_mig_state.bulk_completed = 0;
  329. block_mig_state.zero_blocks = migrate_zero_blocks();
  330. for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
  331. num_bs++;
  332. }
  333. bmds_bs = g_malloc0(num_bs * sizeof(*bmds_bs));
  334. for (i = 0, bs = bdrv_first(&it); bs; bs = bdrv_next(&it), i++) {
  335. if (bdrv_is_read_only(bs)) {
  336. continue;
  337. }
  338. sectors = bdrv_nb_sectors(bs);
  339. if (sectors <= 0) {
  340. ret = sectors;
  341. bdrv_next_cleanup(&it);
  342. goto out;
  343. }
  344. bmds = g_new0(BlkMigDevState, 1);
  345. bmds->blk = blk_new(qemu_get_aio_context(),
  346. BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
  347. bmds->blk_name = g_strdup(bdrv_get_device_name(bs));
  348. bmds->bulk_completed = 0;
  349. bmds->total_sectors = sectors;
  350. bmds->completed_sectors = 0;
  351. bmds->shared_base = migrate_use_block_incremental();
  352. assert(i < num_bs);
  353. bmds_bs[i].bmds = bmds;
  354. bmds_bs[i].bs = bs;
  355. block_mig_state.total_sector_sum += sectors;
  356. if (bmds->shared_base) {
  357. DPRINTF("Start migration for %s with shared base image\n",
  358. bdrv_get_device_name(bs));
  359. } else {
  360. DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
  361. }
  362. QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
  363. }
  364. /* Can only insert new BDSes now because doing so while iterating block
  365. * devices may end up in a deadlock (iterating the new BDSes, too). */
  366. for (i = 0; i < num_bs; i++) {
  367. BlkMigDevState *bmds = bmds_bs[i].bmds;
  368. BlockDriverState *bs = bmds_bs[i].bs;
  369. if (bmds) {
  370. ret = blk_insert_bs(bmds->blk, bs, &local_err);
  371. if (ret < 0) {
  372. error_report_err(local_err);
  373. goto out;
  374. }
  375. alloc_aio_bitmap(bmds);
  376. error_setg(&bmds->blocker, "block device is in use by migration");
  377. bdrv_op_block_all(bs, bmds->blocker);
  378. }
  379. }
  380. ret = 0;
  381. out:
  382. g_free(bmds_bs);
  383. return ret;
  384. }
  385. /* Called with no lock taken. */
  386. static int blk_mig_save_bulked_block(QEMUFile *f)
  387. {
  388. int64_t completed_sector_sum = 0;
  389. BlkMigDevState *bmds;
  390. int progress;
  391. int ret = 0;
  392. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  393. if (bmds->bulk_completed == 0) {
  394. if (mig_save_device_bulk(f, bmds) == 1) {
  395. /* completed bulk section for this device */
  396. bmds->bulk_completed = 1;
  397. }
  398. completed_sector_sum += bmds->completed_sectors;
  399. ret = 1;
  400. break;
  401. } else {
  402. completed_sector_sum += bmds->completed_sectors;
  403. }
  404. }
  405. if (block_mig_state.total_sector_sum != 0) {
  406. progress = completed_sector_sum * 100 /
  407. block_mig_state.total_sector_sum;
  408. } else {
  409. progress = 100;
  410. }
  411. if (progress != block_mig_state.prev_progress) {
  412. block_mig_state.prev_progress = progress;
  413. qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
  414. | BLK_MIG_FLAG_PROGRESS);
  415. DPRINTF("Completed %d %%\r", progress);
  416. }
  417. return ret;
  418. }
  419. static void blk_mig_reset_dirty_cursor(void)
  420. {
  421. BlkMigDevState *bmds;
  422. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  423. bmds->cur_dirty = 0;
  424. }
  425. }
  426. /* Called with iothread lock and AioContext taken. */
  427. static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
  428. int is_async)
  429. {
  430. BlkMigBlock *blk;
  431. int64_t total_sectors = bmds->total_sectors;
  432. int64_t sector;
  433. int nr_sectors;
  434. int ret = -EIO;
  435. for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
  436. blk_mig_lock();
  437. if (bmds_aio_inflight(bmds, sector)) {
  438. blk_mig_unlock();
  439. blk_drain(bmds->blk);
  440. } else {
  441. blk_mig_unlock();
  442. }
  443. bdrv_dirty_bitmap_lock(bmds->dirty_bitmap);
  444. if (bdrv_dirty_bitmap_get_locked(bmds->dirty_bitmap,
  445. sector * BDRV_SECTOR_SIZE)) {
  446. if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
  447. nr_sectors = total_sectors - sector;
  448. } else {
  449. nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
  450. }
  451. bdrv_reset_dirty_bitmap_locked(bmds->dirty_bitmap,
  452. sector * BDRV_SECTOR_SIZE,
  453. nr_sectors * BDRV_SECTOR_SIZE);
  454. bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap);
  455. blk = g_new(BlkMigBlock, 1);
  456. blk->buf = g_malloc(BLK_MIG_BLOCK_SIZE);
  457. blk->bmds = bmds;
  458. blk->sector = sector;
  459. blk->nr_sectors = nr_sectors;
  460. if (is_async) {
  461. qemu_iovec_init_buf(&blk->qiov, blk->buf,
  462. nr_sectors * BDRV_SECTOR_SIZE);
  463. blk->aiocb = blk_aio_preadv(bmds->blk,
  464. sector * BDRV_SECTOR_SIZE,
  465. &blk->qiov, 0, blk_mig_read_cb,
  466. blk);
  467. blk_mig_lock();
  468. block_mig_state.submitted++;
  469. bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
  470. blk_mig_unlock();
  471. } else {
  472. ret = blk_pread(bmds->blk, sector * BDRV_SECTOR_SIZE, blk->buf,
  473. nr_sectors * BDRV_SECTOR_SIZE);
  474. if (ret < 0) {
  475. goto error;
  476. }
  477. blk_send(f, blk);
  478. g_free(blk->buf);
  479. g_free(blk);
  480. }
  481. sector += nr_sectors;
  482. bmds->cur_dirty = sector;
  483. break;
  484. }
  485. bdrv_dirty_bitmap_unlock(bmds->dirty_bitmap);
  486. sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
  487. bmds->cur_dirty = sector;
  488. }
  489. return (bmds->cur_dirty >= bmds->total_sectors);
  490. error:
  491. DPRINTF("Error reading sector %" PRId64 "\n", sector);
  492. g_free(blk->buf);
  493. g_free(blk);
  494. return ret;
  495. }
  496. /* Called with iothread lock taken.
  497. *
  498. * return value:
  499. * 0: too much data for max_downtime
  500. * 1: few enough data for max_downtime
  501. */
  502. static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
  503. {
  504. BlkMigDevState *bmds;
  505. int ret = 1;
  506. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  507. aio_context_acquire(blk_get_aio_context(bmds->blk));
  508. ret = mig_save_device_dirty(f, bmds, is_async);
  509. aio_context_release(blk_get_aio_context(bmds->blk));
  510. if (ret <= 0) {
  511. break;
  512. }
  513. }
  514. return ret;
  515. }
  516. /* Called with no locks taken. */
  517. static int flush_blks(QEMUFile *f)
  518. {
  519. BlkMigBlock *blk;
  520. int ret = 0;
  521. DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
  522. __func__, block_mig_state.submitted, block_mig_state.read_done,
  523. block_mig_state.transferred);
  524. blk_mig_lock();
  525. while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
  526. if (qemu_file_rate_limit(f)) {
  527. break;
  528. }
  529. if (blk->ret < 0) {
  530. ret = blk->ret;
  531. break;
  532. }
  533. QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
  534. blk_mig_unlock();
  535. blk_send(f, blk);
  536. blk_mig_lock();
  537. g_free(blk->buf);
  538. g_free(blk);
  539. block_mig_state.read_done--;
  540. block_mig_state.transferred++;
  541. assert(block_mig_state.read_done >= 0);
  542. }
  543. blk_mig_unlock();
  544. DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __func__,
  545. block_mig_state.submitted, block_mig_state.read_done,
  546. block_mig_state.transferred);
  547. return ret;
  548. }
  549. /* Called with iothread lock taken. */
  550. static int64_t get_remaining_dirty(void)
  551. {
  552. BlkMigDevState *bmds;
  553. int64_t dirty = 0;
  554. QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
  555. aio_context_acquire(blk_get_aio_context(bmds->blk));
  556. dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
  557. aio_context_release(blk_get_aio_context(bmds->blk));
  558. }
  559. return dirty;
  560. }
  561. /* Called with iothread lock taken. */
  562. static void block_migration_cleanup_bmds(void)
  563. {
  564. BlkMigDevState *bmds;
  565. AioContext *ctx;
  566. unset_dirty_tracking();
  567. while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
  568. QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
  569. bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker);
  570. error_free(bmds->blocker);
  571. /* Save ctx, because bmds->blk can disappear during blk_unref. */
  572. ctx = blk_get_aio_context(bmds->blk);
  573. aio_context_acquire(ctx);
  574. blk_unref(bmds->blk);
  575. aio_context_release(ctx);
  576. g_free(bmds->blk_name);
  577. g_free(bmds->aio_bitmap);
  578. g_free(bmds);
  579. }
  580. }
  581. /* Called with iothread lock taken. */
  582. static void block_migration_cleanup(void *opaque)
  583. {
  584. BlkMigBlock *blk;
  585. bdrv_drain_all();
  586. block_migration_cleanup_bmds();
  587. blk_mig_lock();
  588. while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
  589. QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
  590. g_free(blk->buf);
  591. g_free(blk);
  592. }
  593. blk_mig_unlock();
  594. }
  595. static int block_save_setup(QEMUFile *f, void *opaque)
  596. {
  597. int ret;
  598. DPRINTF("Enter save live setup submitted %d transferred %d\n",
  599. block_mig_state.submitted, block_mig_state.transferred);
  600. qemu_mutex_lock_iothread();
  601. ret = init_blk_migration(f);
  602. if (ret < 0) {
  603. qemu_mutex_unlock_iothread();
  604. return ret;
  605. }
  606. /* start track dirty blocks */
  607. ret = set_dirty_tracking();
  608. qemu_mutex_unlock_iothread();
  609. if (ret) {
  610. return ret;
  611. }
  612. ret = flush_blks(f);
  613. blk_mig_reset_dirty_cursor();
  614. qemu_put_be64(f, BLK_MIG_FLAG_EOS);
  615. return ret;
  616. }
  617. static int block_save_iterate(QEMUFile *f, void *opaque)
  618. {
  619. int ret;
  620. int64_t last_ftell = qemu_ftell(f);
  621. int64_t delta_ftell;
  622. DPRINTF("Enter save live iterate submitted %d transferred %d\n",
  623. block_mig_state.submitted, block_mig_state.transferred);
  624. ret = flush_blks(f);
  625. if (ret) {
  626. return ret;
  627. }
  628. blk_mig_reset_dirty_cursor();
  629. /* control the rate of transfer */
  630. blk_mig_lock();
  631. while (block_mig_state.read_done * BLK_MIG_BLOCK_SIZE <
  632. qemu_file_get_rate_limit(f) &&
  633. block_mig_state.submitted < MAX_PARALLEL_IO &&
  634. (block_mig_state.submitted + block_mig_state.read_done) <
  635. MAX_IO_BUFFERS) {
  636. blk_mig_unlock();
  637. if (block_mig_state.bulk_completed == 0) {
  638. /* first finish the bulk phase */
  639. if (blk_mig_save_bulked_block(f) == 0) {
  640. /* finished saving bulk on all devices */
  641. block_mig_state.bulk_completed = 1;
  642. }
  643. ret = 0;
  644. } else {
  645. /* Always called with iothread lock taken for
  646. * simplicity, block_save_complete also calls it.
  647. */
  648. qemu_mutex_lock_iothread();
  649. ret = blk_mig_save_dirty_block(f, 1);
  650. qemu_mutex_unlock_iothread();
  651. }
  652. if (ret < 0) {
  653. return ret;
  654. }
  655. blk_mig_lock();
  656. if (ret != 0) {
  657. /* no more dirty blocks */
  658. break;
  659. }
  660. }
  661. blk_mig_unlock();
  662. ret = flush_blks(f);
  663. if (ret) {
  664. return ret;
  665. }
  666. qemu_put_be64(f, BLK_MIG_FLAG_EOS);
  667. delta_ftell = qemu_ftell(f) - last_ftell;
  668. if (delta_ftell > 0) {
  669. return 1;
  670. } else if (delta_ftell < 0) {
  671. return -1;
  672. } else {
  673. return 0;
  674. }
  675. }
  676. /* Called with iothread lock taken. */
  677. static int block_save_complete(QEMUFile *f, void *opaque)
  678. {
  679. int ret;
  680. DPRINTF("Enter save live complete submitted %d transferred %d\n",
  681. block_mig_state.submitted, block_mig_state.transferred);
  682. ret = flush_blks(f);
  683. if (ret) {
  684. return ret;
  685. }
  686. blk_mig_reset_dirty_cursor();
  687. /* we know for sure that save bulk is completed and
  688. all async read completed */
  689. blk_mig_lock();
  690. assert(block_mig_state.submitted == 0);
  691. blk_mig_unlock();
  692. do {
  693. ret = blk_mig_save_dirty_block(f, 0);
  694. if (ret < 0) {
  695. return ret;
  696. }
  697. } while (ret == 0);
  698. /* report completion */
  699. qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
  700. DPRINTF("Block migration completed\n");
  701. qemu_put_be64(f, BLK_MIG_FLAG_EOS);
  702. /* Make sure that our BlockBackends are gone, so that the block driver
  703. * nodes can be inactivated. */
  704. block_migration_cleanup_bmds();
  705. return 0;
  706. }
  707. static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
  708. uint64_t *res_precopy_only,
  709. uint64_t *res_compatible,
  710. uint64_t *res_postcopy_only)
  711. {
  712. /* Estimate pending number of bytes to send */
  713. uint64_t pending;
  714. qemu_mutex_lock_iothread();
  715. pending = get_remaining_dirty();
  716. qemu_mutex_unlock_iothread();
  717. blk_mig_lock();
  718. pending += block_mig_state.submitted * BLK_MIG_BLOCK_SIZE +
  719. block_mig_state.read_done * BLK_MIG_BLOCK_SIZE;
  720. blk_mig_unlock();
  721. /* Report at least one block pending during bulk phase */
  722. if (pending <= max_size && !block_mig_state.bulk_completed) {
  723. pending = max_size + BLK_MIG_BLOCK_SIZE;
  724. }
  725. DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
  726. /* We don't do postcopy */
  727. *res_precopy_only += pending;
  728. }
  729. static int block_load(QEMUFile *f, void *opaque, int version_id)
  730. {
  731. static int banner_printed;
  732. int len, flags;
  733. char device_name[256];
  734. int64_t addr;
  735. BlockBackend *blk, *blk_prev = NULL;
  736. Error *local_err = NULL;
  737. uint8_t *buf;
  738. int64_t total_sectors = 0;
  739. int nr_sectors;
  740. int ret;
  741. BlockDriverInfo bdi;
  742. int cluster_size = BLK_MIG_BLOCK_SIZE;
  743. do {
  744. addr = qemu_get_be64(f);
  745. flags = addr & (BDRV_SECTOR_SIZE - 1);
  746. addr >>= BDRV_SECTOR_BITS;
  747. if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
  748. /* get device name */
  749. len = qemu_get_byte(f);
  750. qemu_get_buffer(f, (uint8_t *)device_name, len);
  751. device_name[len] = '\0';
  752. blk = blk_by_name(device_name);
  753. if (!blk) {
  754. fprintf(stderr, "Error unknown block device %s\n",
  755. device_name);
  756. return -EINVAL;
  757. }
  758. if (blk != blk_prev) {
  759. blk_prev = blk;
  760. total_sectors = blk_nb_sectors(blk);
  761. if (total_sectors <= 0) {
  762. error_report("Error getting length of block device %s",
  763. device_name);
  764. return -EINVAL;
  765. }
  766. blk_invalidate_cache(blk, &local_err);
  767. if (local_err) {
  768. error_report_err(local_err);
  769. return -EINVAL;
  770. }
  771. ret = bdrv_get_info(blk_bs(blk), &bdi);
  772. if (ret == 0 && bdi.cluster_size > 0 &&
  773. bdi.cluster_size <= BLK_MIG_BLOCK_SIZE &&
  774. BLK_MIG_BLOCK_SIZE % bdi.cluster_size == 0) {
  775. cluster_size = bdi.cluster_size;
  776. } else {
  777. cluster_size = BLK_MIG_BLOCK_SIZE;
  778. }
  779. }
  780. if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
  781. nr_sectors = total_sectors - addr;
  782. } else {
  783. nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
  784. }
  785. if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
  786. ret = blk_pwrite_zeroes(blk, addr * BDRV_SECTOR_SIZE,
  787. nr_sectors * BDRV_SECTOR_SIZE,
  788. BDRV_REQ_MAY_UNMAP);
  789. } else {
  790. int i;
  791. int64_t cur_addr;
  792. uint8_t *cur_buf;
  793. buf = g_malloc(BLK_MIG_BLOCK_SIZE);
  794. qemu_get_buffer(f, buf, BLK_MIG_BLOCK_SIZE);
  795. for (i = 0; i < BLK_MIG_BLOCK_SIZE / cluster_size; i++) {
  796. cur_addr = addr * BDRV_SECTOR_SIZE + i * cluster_size;
  797. cur_buf = buf + i * cluster_size;
  798. if ((!block_mig_state.zero_blocks ||
  799. cluster_size < BLK_MIG_BLOCK_SIZE) &&
  800. buffer_is_zero(cur_buf, cluster_size)) {
  801. ret = blk_pwrite_zeroes(blk, cur_addr,
  802. cluster_size,
  803. BDRV_REQ_MAY_UNMAP);
  804. } else {
  805. ret = blk_pwrite(blk, cur_addr, cur_buf,
  806. cluster_size, 0);
  807. }
  808. if (ret < 0) {
  809. break;
  810. }
  811. }
  812. g_free(buf);
  813. }
  814. if (ret < 0) {
  815. return ret;
  816. }
  817. } else if (flags & BLK_MIG_FLAG_PROGRESS) {
  818. if (!banner_printed) {
  819. printf("Receiving block device images\n");
  820. banner_printed = 1;
  821. }
  822. printf("Completed %d %%%c", (int)addr,
  823. (addr == 100) ? '\n' : '\r');
  824. fflush(stdout);
  825. } else if (!(flags & BLK_MIG_FLAG_EOS)) {
  826. fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
  827. return -EINVAL;
  828. }
  829. ret = qemu_file_get_error(f);
  830. if (ret != 0) {
  831. return ret;
  832. }
  833. } while (!(flags & BLK_MIG_FLAG_EOS));
  834. return 0;
  835. }
  836. static bool block_is_active(void *opaque)
  837. {
  838. return migrate_use_block();
  839. }
  840. static SaveVMHandlers savevm_block_handlers = {
  841. .save_setup = block_save_setup,
  842. .save_live_iterate = block_save_iterate,
  843. .save_live_complete_precopy = block_save_complete,
  844. .save_live_pending = block_save_pending,
  845. .load_state = block_load,
  846. .save_cleanup = block_migration_cleanup,
  847. .is_active = block_is_active,
  848. };
  849. void blk_mig_init(void)
  850. {
  851. QSIMPLEQ_INIT(&block_mig_state.bmds_list);
  852. QSIMPLEQ_INIT(&block_mig_state.blk_list);
  853. qemu_mutex_init(&block_mig_state.lock);
  854. register_savevm_live("block", 0, 1, &savevm_block_handlers,
  855. &block_mig_state);
  856. }