channel-block.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * QEMU I/O channels block driver
  3. *
  4. * Copyright (c) 2022 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "migration/channel-block.h"
  22. #include "qapi/error.h"
  23. #include "block/block.h"
  24. #include "trace.h"
  25. QIOChannelBlock *
  26. qio_channel_block_new(BlockDriverState *bs)
  27. {
  28. QIOChannelBlock *ioc;
  29. ioc = QIO_CHANNEL_BLOCK(object_new(TYPE_QIO_CHANNEL_BLOCK));
  30. bdrv_ref(bs);
  31. ioc->bs = bs;
  32. return ioc;
  33. }
  34. static void
  35. qio_channel_block_finalize(Object *obj)
  36. {
  37. QIOChannelBlock *ioc = QIO_CHANNEL_BLOCK(obj);
  38. g_clear_pointer(&ioc->bs, bdrv_unref);
  39. }
  40. static ssize_t
  41. qio_channel_block_readv(QIOChannel *ioc,
  42. const struct iovec *iov,
  43. size_t niov,
  44. int **fds,
  45. size_t *nfds,
  46. int flags,
  47. Error **errp)
  48. {
  49. QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
  50. QEMUIOVector qiov;
  51. int ret;
  52. qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
  53. ret = bdrv_readv_vmstate(bioc->bs, &qiov, bioc->offset);
  54. if (ret < 0) {
  55. error_setg_errno(errp, -ret, "bdrv_readv_vmstate failed");
  56. return -1;
  57. }
  58. bioc->offset += qiov.size;
  59. return qiov.size;
  60. }
  61. static ssize_t
  62. qio_channel_block_writev(QIOChannel *ioc,
  63. const struct iovec *iov,
  64. size_t niov,
  65. int *fds,
  66. size_t nfds,
  67. int flags,
  68. Error **errp)
  69. {
  70. QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
  71. QEMUIOVector qiov;
  72. int ret;
  73. qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
  74. ret = bdrv_writev_vmstate(bioc->bs, &qiov, bioc->offset);
  75. if (ret < 0) {
  76. error_setg_errno(errp, -ret, "bdrv_writev_vmstate failed");
  77. return -1;
  78. }
  79. bioc->offset += qiov.size;
  80. return qiov.size;
  81. }
  82. static int
  83. qio_channel_block_set_blocking(QIOChannel *ioc,
  84. bool enabled,
  85. Error **errp)
  86. {
  87. if (!enabled) {
  88. error_setg(errp, "Non-blocking mode not supported for block devices");
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. static off_t
  94. qio_channel_block_seek(QIOChannel *ioc,
  95. off_t offset,
  96. int whence,
  97. Error **errp)
  98. {
  99. QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
  100. switch (whence) {
  101. case SEEK_SET:
  102. bioc->offset = offset;
  103. break;
  104. case SEEK_CUR:
  105. bioc->offset += whence;
  106. break;
  107. case SEEK_END:
  108. error_setg(errp, "Size of VMstate region is unknown");
  109. return (off_t)-1;
  110. default:
  111. g_assert_not_reached();
  112. }
  113. return bioc->offset;
  114. }
  115. static int
  116. qio_channel_block_close(QIOChannel *ioc,
  117. Error **errp)
  118. {
  119. QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc);
  120. int rv = bdrv_flush(bioc->bs);
  121. if (rv < 0) {
  122. error_setg_errno(errp, -rv,
  123. "Unable to flush VMState");
  124. return -1;
  125. }
  126. g_clear_pointer(&bioc->bs, bdrv_unref);
  127. bioc->offset = 0;
  128. return 0;
  129. }
  130. static void
  131. qio_channel_block_set_aio_fd_handler(QIOChannel *ioc,
  132. AioContext *read_ctx,
  133. IOHandler *io_read,
  134. AioContext *write_ctx,
  135. IOHandler *io_write,
  136. void *opaque)
  137. {
  138. /* XXX anything we can do here ? */
  139. }
  140. static void
  141. qio_channel_block_class_init(ObjectClass *klass,
  142. void *class_data G_GNUC_UNUSED)
  143. {
  144. QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
  145. ioc_klass->io_writev = qio_channel_block_writev;
  146. ioc_klass->io_readv = qio_channel_block_readv;
  147. ioc_klass->io_set_blocking = qio_channel_block_set_blocking;
  148. ioc_klass->io_seek = qio_channel_block_seek;
  149. ioc_klass->io_close = qio_channel_block_close;
  150. ioc_klass->io_set_aio_fd_handler = qio_channel_block_set_aio_fd_handler;
  151. }
  152. static const TypeInfo qio_channel_block_info = {
  153. .parent = TYPE_QIO_CHANNEL,
  154. .name = TYPE_QIO_CHANNEL_BLOCK,
  155. .instance_size = sizeof(QIOChannelBlock),
  156. .instance_finalize = qio_channel_block_finalize,
  157. .class_init = qio_channel_block_class_init,
  158. };
  159. static void
  160. qio_channel_block_register_types(void)
  161. {
  162. type_register_static(&qio_channel_block_info);
  163. }
  164. type_init(qio_channel_block_register_types);