2
0

aio-posix.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_prepare(AioContext *ctx)
  90. {
  91. return false;
  92. }
  93. bool aio_pending(AioContext *ctx)
  94. {
  95. AioHandler *node;
  96. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  97. int revents;
  98. revents = node->pfd.revents & node->pfd.events;
  99. if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
  100. return true;
  101. }
  102. if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. bool aio_dispatch(AioContext *ctx)
  109. {
  110. AioHandler *node;
  111. bool progress = false;
  112. /*
  113. * If there are callbacks left that have been queued, we need to call them.
  114. * Do not call select in this case, because it is possible that the caller
  115. * does not need a complete flush (as is the case for aio_poll loops).
  116. */
  117. if (aio_bh_poll(ctx)) {
  118. progress = true;
  119. }
  120. /*
  121. * We have to walk very carefully in case aio_set_fd_handler is
  122. * called while we're walking.
  123. */
  124. node = QLIST_FIRST(&ctx->aio_handlers);
  125. while (node) {
  126. AioHandler *tmp;
  127. int revents;
  128. ctx->walking_handlers++;
  129. revents = node->pfd.revents & node->pfd.events;
  130. node->pfd.revents = 0;
  131. if (!node->deleted &&
  132. (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
  133. node->io_read) {
  134. node->io_read(node->opaque);
  135. /* aio_notify() does not count as progress */
  136. if (node->opaque != &ctx->notifier) {
  137. progress = true;
  138. }
  139. }
  140. if (!node->deleted &&
  141. (revents & (G_IO_OUT | G_IO_ERR)) &&
  142. node->io_write) {
  143. node->io_write(node->opaque);
  144. progress = true;
  145. }
  146. tmp = node;
  147. node = QLIST_NEXT(node, node);
  148. ctx->walking_handlers--;
  149. if (!ctx->walking_handlers && tmp->deleted) {
  150. QLIST_REMOVE(tmp, node);
  151. g_free(tmp);
  152. }
  153. }
  154. /* Run our timers */
  155. progress |= timerlistgroup_run_timers(&ctx->tlg);
  156. return progress;
  157. }
  158. bool aio_poll(AioContext *ctx, bool blocking)
  159. {
  160. AioHandler *node;
  161. bool was_dispatching;
  162. int ret;
  163. bool progress;
  164. was_dispatching = ctx->dispatching;
  165. progress = false;
  166. /* aio_notify can avoid the expensive event_notifier_set if
  167. * everything (file descriptors, bottom halves, timers) will
  168. * be re-evaluated before the next blocking poll(). This is
  169. * already true when aio_poll is called with blocking == false;
  170. * if blocking == true, it is only true after poll() returns.
  171. *
  172. * If we're in a nested event loop, ctx->dispatching might be true.
  173. * In that case we can restore it just before returning, but we
  174. * have to clear it now.
  175. */
  176. aio_set_dispatching(ctx, !blocking);
  177. ctx->walking_handlers++;
  178. g_array_set_size(ctx->pollfds, 0);
  179. /* fill pollfds */
  180. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  181. node->pollfds_idx = -1;
  182. if (!node->deleted && node->pfd.events) {
  183. GPollFD pfd = {
  184. .fd = node->pfd.fd,
  185. .events = node->pfd.events,
  186. };
  187. node->pollfds_idx = ctx->pollfds->len;
  188. g_array_append_val(ctx->pollfds, pfd);
  189. }
  190. }
  191. ctx->walking_handlers--;
  192. /* wait until next event */
  193. ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
  194. ctx->pollfds->len,
  195. blocking ? aio_compute_timeout(ctx) : 0);
  196. /* if we have any readable fds, dispatch event */
  197. if (ret > 0) {
  198. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  199. if (node->pollfds_idx != -1) {
  200. GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
  201. node->pollfds_idx);
  202. node->pfd.revents = pfd->revents;
  203. }
  204. }
  205. }
  206. /* Run dispatch even if there were no readable fds to run timers */
  207. aio_set_dispatching(ctx, true);
  208. if (aio_dispatch(ctx)) {
  209. progress = true;
  210. }
  211. aio_set_dispatching(ctx, was_dispatching);
  212. return progress;
  213. }