lfs-openat.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <sys/resource.h>
  11. #include <unistd.h>
  12. #include "liburing.h"
  13. #define DIE(...) \
  14. do { \
  15. fprintf(stderr, __VA_ARGS__); \
  16. abort(); \
  17. } while(0)
  18. static const int RSIZE = 2;
  19. static const int OPEN_FLAGS = O_RDWR | O_CREAT | O_LARGEFILE;
  20. static const mode_t OPEN_MODE = S_IRUSR | S_IWUSR;
  21. static int open_io_uring(struct io_uring *ring, int dfd, const char *fn)
  22. {
  23. struct io_uring_sqe *sqe;
  24. struct io_uring_cqe *cqe;
  25. int ret, fd;
  26. sqe = io_uring_get_sqe(ring);
  27. if (!sqe) {
  28. fprintf(stderr, "failed to get sqe\n");
  29. return 1;
  30. }
  31. io_uring_prep_openat(sqe, dfd, fn, OPEN_FLAGS, OPEN_MODE);
  32. ret = io_uring_submit(ring);
  33. if (ret < 0) {
  34. fprintf(stderr, "failed to submit openat: %s\n", strerror(-ret));
  35. return 1;
  36. }
  37. ret = io_uring_wait_cqe(ring, &cqe);
  38. fd = cqe->res;
  39. io_uring_cqe_seen(ring, cqe);
  40. if (ret < 0) {
  41. fprintf(stderr, "wait_cqe failed: %s\n", strerror(-ret));
  42. return 1;
  43. } else if (fd < 0) {
  44. fprintf(stderr, "io_uring openat failed: %s\n", strerror(-fd));
  45. return 1;
  46. }
  47. close(fd);
  48. return 0;
  49. }
  50. static int prepare_file(int dfd, const char* fn)
  51. {
  52. const char buf[] = "foo";
  53. int fd, res;
  54. fd = openat(dfd, fn, OPEN_FLAGS, OPEN_MODE);
  55. if (fd < 0) {
  56. fprintf(stderr, "prepare/open: %s\n", strerror(errno));
  57. return -1;
  58. }
  59. res = pwrite(fd, buf, sizeof(buf), 1ull << 32);
  60. if (res < 0)
  61. fprintf(stderr, "prepare/pwrite: %s\n", strerror(errno));
  62. close(fd);
  63. return res < 0 ? res : 0;
  64. }
  65. static int test_linked_files(int dfd, const char *fn, bool async)
  66. {
  67. struct io_uring ring;
  68. struct io_uring_sqe *sqe;
  69. char buffer[128];
  70. struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer), };
  71. int ret, fd;
  72. int fds[2];
  73. ret = io_uring_queue_init(10, &ring, 0);
  74. if (ret < 0)
  75. DIE("failed to init io_uring: %s\n", strerror(-ret));
  76. if (pipe(fds)) {
  77. perror("pipe");
  78. return 1;
  79. }
  80. sqe = io_uring_get_sqe(&ring);
  81. if (!sqe) {
  82. printf("get sqe failed\n");
  83. return -1;
  84. }
  85. io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
  86. sqe->flags |= IOSQE_IO_LINK;
  87. if (async)
  88. sqe->flags |= IOSQE_ASYNC;
  89. sqe = io_uring_get_sqe(&ring);
  90. if (!sqe) {
  91. fprintf(stderr, "failed to get sqe\n");
  92. return 1;
  93. }
  94. io_uring_prep_openat(sqe, dfd, fn, OPEN_FLAGS, OPEN_MODE);
  95. ret = io_uring_submit(&ring);
  96. if (ret != 2) {
  97. fprintf(stderr, "failed to submit openat: %s\n", strerror(-ret));
  98. return 1;
  99. }
  100. fd = dup(ring.ring_fd);
  101. if (fd < 0) {
  102. fprintf(stderr, "dup() failed: %s\n", strerror(-fd));
  103. return 1;
  104. }
  105. /* io_uring->flush() */
  106. close(fd);
  107. io_uring_queue_exit(&ring);
  108. return 0;
  109. }
  110. static int test_drained_files(int dfd, const char *fn, bool linked, bool prepend)
  111. {
  112. struct io_uring ring;
  113. struct io_uring_sqe *sqe;
  114. char buffer[128];
  115. struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer), };
  116. int ret, fd, fds[2], to_cancel = 0;
  117. ret = io_uring_queue_init(10, &ring, 0);
  118. if (ret < 0)
  119. DIE("failed to init io_uring: %s\n", strerror(-ret));
  120. if (pipe(fds)) {
  121. perror("pipe");
  122. return 1;
  123. }
  124. sqe = io_uring_get_sqe(&ring);
  125. if (!sqe) {
  126. printf("get sqe failed\n");
  127. return -1;
  128. }
  129. io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
  130. sqe->user_data = 0;
  131. if (prepend) {
  132. sqe = io_uring_get_sqe(&ring);
  133. if (!sqe) {
  134. fprintf(stderr, "failed to get sqe\n");
  135. return 1;
  136. }
  137. io_uring_prep_nop(sqe);
  138. sqe->flags |= IOSQE_IO_DRAIN;
  139. to_cancel++;
  140. sqe->user_data = to_cancel;
  141. }
  142. if (linked) {
  143. sqe = io_uring_get_sqe(&ring);
  144. if (!sqe) {
  145. fprintf(stderr, "failed to get sqe\n");
  146. return 1;
  147. }
  148. io_uring_prep_nop(sqe);
  149. sqe->flags |= IOSQE_IO_DRAIN | IOSQE_IO_LINK;
  150. to_cancel++;
  151. sqe->user_data = to_cancel;
  152. }
  153. sqe = io_uring_get_sqe(&ring);
  154. if (!sqe) {
  155. fprintf(stderr, "failed to get sqe\n");
  156. return 1;
  157. }
  158. io_uring_prep_openat(sqe, dfd, fn, OPEN_FLAGS, OPEN_MODE);
  159. sqe->flags |= IOSQE_IO_DRAIN;
  160. to_cancel++;
  161. sqe->user_data = to_cancel;
  162. ret = io_uring_submit(&ring);
  163. if (ret != 1 + to_cancel) {
  164. fprintf(stderr, "failed to submit openat: %s\n", strerror(-ret));
  165. return 1;
  166. }
  167. fd = dup(ring.ring_fd);
  168. if (fd < 0) {
  169. fprintf(stderr, "dup() failed: %s\n", strerror(-fd));
  170. return 1;
  171. }
  172. /*
  173. * close(), which triggers ->flush(), and io_uring_queue_exit()
  174. * should successfully return and not hang.
  175. */
  176. close(fd);
  177. io_uring_queue_exit(&ring);
  178. return 0;
  179. }
  180. int main(int argc, char *argv[])
  181. {
  182. const char *fn = "io_uring_openat_test";
  183. struct io_uring ring;
  184. int ret, dfd;
  185. if (argc > 1)
  186. return 0;
  187. dfd = open("/tmp", O_PATH);
  188. if (dfd < 0)
  189. DIE("open /tmp: %s\n", strerror(errno));
  190. ret = io_uring_queue_init(RSIZE, &ring, 0);
  191. if (ret < 0)
  192. DIE("failed to init io_uring: %s\n", strerror(-ret));
  193. if (prepare_file(dfd, fn))
  194. return 1;
  195. ret = open_io_uring(&ring, dfd, fn);
  196. if (ret) {
  197. fprintf(stderr, "open_io_uring() failed\n");
  198. goto out;
  199. }
  200. ret = test_linked_files(dfd, fn, false);
  201. if (ret) {
  202. fprintf(stderr, "test_linked_files() !async failed\n");
  203. goto out;
  204. }
  205. ret = test_linked_files(dfd, fn, true);
  206. if (ret) {
  207. fprintf(stderr, "test_linked_files() async failed\n");
  208. goto out;
  209. }
  210. ret = test_drained_files(dfd, fn, false, false);
  211. if (ret) {
  212. fprintf(stderr, "test_drained_files() failed\n");
  213. goto out;
  214. }
  215. ret = test_drained_files(dfd, fn, false, true);
  216. if (ret) {
  217. fprintf(stderr, "test_drained_files() middle failed\n");
  218. goto out;
  219. }
  220. ret = test_drained_files(dfd, fn, true, false);
  221. if (ret) {
  222. fprintf(stderr, "test_drained_files() linked failed\n");
  223. goto out;
  224. }
  225. out:
  226. io_uring_queue_exit(&ring);
  227. close(dfd);
  228. unlink("/tmp/io_uring_openat_test");
  229. return ret;
  230. }