aio-posix.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * QEMU aio implementation
  3. *
  4. * Copyright IBM, Corp. 2008
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.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-common.h"
  16. #include "block/block.h"
  17. #include "qemu/queue.h"
  18. #include "qemu/sockets.h"
  19. struct AioHandler
  20. {
  21. GPollFD pfd;
  22. IOHandler *io_read;
  23. IOHandler *io_write;
  24. int deleted;
  25. int pollfds_idx;
  26. void *opaque;
  27. QLIST_ENTRY(AioHandler) node;
  28. };
  29. static AioHandler *find_aio_handler(AioContext *ctx, int fd)
  30. {
  31. AioHandler *node;
  32. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  33. if (node->pfd.fd == fd)
  34. if (!node->deleted)
  35. return node;
  36. }
  37. return NULL;
  38. }
  39. void aio_set_fd_handler(AioContext *ctx,
  40. int fd,
  41. IOHandler *io_read,
  42. IOHandler *io_write,
  43. void *opaque)
  44. {
  45. AioHandler *node;
  46. node = find_aio_handler(ctx, fd);
  47. /* Are we deleting the fd handler? */
  48. if (!io_read && !io_write) {
  49. if (node) {
  50. g_source_remove_poll(&ctx->source, &node->pfd);
  51. /* If the lock is held, just mark the node as deleted */
  52. if (ctx->walking_handlers) {
  53. node->deleted = 1;
  54. node->pfd.revents = 0;
  55. } else {
  56. /* Otherwise, delete it for real. We can't just mark it as
  57. * deleted because deleted nodes are only cleaned up after
  58. * releasing the walking_handlers lock.
  59. */
  60. QLIST_REMOVE(node, node);
  61. g_free(node);
  62. }
  63. }
  64. } else {
  65. if (node == NULL) {
  66. /* Alloc and insert if it's not already there */
  67. node = g_malloc0(sizeof(AioHandler));
  68. node->pfd.fd = fd;
  69. QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
  70. g_source_add_poll(&ctx->source, &node->pfd);
  71. }
  72. /* Update handler with latest information */
  73. node->io_read = io_read;
  74. node->io_write = io_write;
  75. node->opaque = opaque;
  76. node->pollfds_idx = -1;
  77. node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
  78. node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
  79. }
  80. aio_notify(ctx);
  81. }
  82. void aio_set_event_notifier(AioContext *ctx,
  83. EventNotifier *notifier,
  84. EventNotifierHandler *io_read)
  85. {
  86. aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
  87. (IOHandler *)io_read, NULL, notifier);
  88. }
  89. bool aio_pending(AioContext *ctx)
  90. {
  91. AioHandler *node;
  92. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  93. int revents;
  94. revents = node->pfd.revents & node->pfd.events;
  95. if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
  96. return true;
  97. }
  98. if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. static bool aio_dispatch(AioContext *ctx)
  105. {
  106. AioHandler *node;
  107. bool progress = false;
  108. /*
  109. * We have to walk very carefully in case qemu_aio_set_fd_handler is
  110. * called while we're walking.
  111. */
  112. node = QLIST_FIRST(&ctx->aio_handlers);
  113. while (node) {
  114. AioHandler *tmp;
  115. int revents;
  116. ctx->walking_handlers++;
  117. revents = node->pfd.revents & node->pfd.events;
  118. node->pfd.revents = 0;
  119. if (!node->deleted &&
  120. (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
  121. node->io_read) {
  122. node->io_read(node->opaque);
  123. /* aio_notify() does not count as progress */
  124. if (node->opaque != &ctx->notifier) {
  125. progress = true;
  126. }
  127. }
  128. if (!node->deleted &&
  129. (revents & (G_IO_OUT | G_IO_ERR)) &&
  130. node->io_write) {
  131. node->io_write(node->opaque);
  132. progress = true;
  133. }
  134. tmp = node;
  135. node = QLIST_NEXT(node, node);
  136. ctx->walking_handlers--;
  137. if (!ctx->walking_handlers && tmp->deleted) {
  138. QLIST_REMOVE(tmp, node);
  139. g_free(tmp);
  140. }
  141. }
  142. /* Run our timers */
  143. progress |= timerlistgroup_run_timers(&ctx->tlg);
  144. return progress;
  145. }
  146. bool aio_poll(AioContext *ctx, bool blocking)
  147. {
  148. AioHandler *node;
  149. int ret;
  150. bool progress;
  151. progress = false;
  152. /*
  153. * If there are callbacks left that have been queued, we need to call them.
  154. * Do not call select in this case, because it is possible that the caller
  155. * does not need a complete flush (as is the case for qemu_aio_wait loops).
  156. */
  157. if (aio_bh_poll(ctx)) {
  158. blocking = false;
  159. progress = true;
  160. }
  161. if (aio_dispatch(ctx)) {
  162. progress = true;
  163. }
  164. if (progress && !blocking) {
  165. return true;
  166. }
  167. ctx->walking_handlers++;
  168. g_array_set_size(ctx->pollfds, 0);
  169. /* fill pollfds */
  170. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  171. node->pollfds_idx = -1;
  172. if (!node->deleted && node->pfd.events) {
  173. GPollFD pfd = {
  174. .fd = node->pfd.fd,
  175. .events = node->pfd.events,
  176. };
  177. node->pollfds_idx = ctx->pollfds->len;
  178. g_array_append_val(ctx->pollfds, pfd);
  179. }
  180. }
  181. ctx->walking_handlers--;
  182. /* wait until next event */
  183. ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
  184. ctx->pollfds->len,
  185. blocking ? timerlistgroup_deadline_ns(&ctx->tlg) : 0);
  186. /* if we have any readable fds, dispatch event */
  187. if (ret > 0) {
  188. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  189. if (node->pollfds_idx != -1) {
  190. GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
  191. node->pollfds_idx);
  192. node->pfd.revents = pfd->revents;
  193. }
  194. }
  195. }
  196. /* Run dispatch even if there were no readable fds to run timers */
  197. if (aio_dispatch(ctx)) {
  198. progress = true;
  199. }
  200. return progress;
  201. }