+ ret = poll(&pfd, 1, -1);
+ if (ret <= 0)
+ return 0;
+
+ assert(pfd.revents & POLLIN);
+
+ event_num = rbd_poll_io_events(rbd->image, comps, min_evts);
+
+ for (i = 0; i < event_num; i++) {
+ fri = rbd_aio_get_arg(comps[i]);
+ io_u = fri->io_u;
+#else
+ io_u_qiter(&td->io_u_all, io_u, i) {
+#endif
+ if (!(io_u->flags & IO_U_F_FLIGHT))
+ continue;
+ if (rbd_io_u_seen(io_u))
+ continue;
+
+ if (fri_check_complete(rbd, io_u, events))
+ this_events++;
+ else if (wait)
+ rbd->sort_events[sidx++] = io_u;
+ }
+
+ if (!wait || !sidx)
+ return this_events;
+
+ /*
+ * Sort events, oldest issue first, then wait on as many as we
+ * need in order of age. If we have enough events, stop waiting,
+ * and just check if any of the older ones are done.
+ */
+ if (sidx > 1)
+ qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
+
+ for (i = 0; i < sidx; i++) {
+ io_u = rbd->sort_events[i];