open-direct-pick.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various openat(2) tests
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <limits.h>
  14. #include "helpers.h"
  15. #include "liburing.h"
  16. #define FDS 800
  17. static int no_direct_pick;
  18. static int submit_wait(struct io_uring *ring)
  19. {
  20. struct io_uring_cqe *cqe;
  21. int ret;
  22. ret = io_uring_submit(ring);
  23. if (ret <= 0) {
  24. fprintf(stderr, "sqe submit failed: %d\n", ret);
  25. return 1;
  26. }
  27. ret = io_uring_wait_cqe(ring, &cqe);
  28. if (ret < 0) {
  29. fprintf(stderr, "wait completion %d\n", ret);
  30. return 1;
  31. }
  32. ret = cqe->res;
  33. io_uring_cqe_seen(ring, cqe);
  34. return ret;
  35. }
  36. static inline int try_close(struct io_uring *ring, int slot)
  37. {
  38. struct io_uring_sqe *sqe;
  39. sqe = io_uring_get_sqe(ring);
  40. io_uring_prep_close_direct(sqe, slot);
  41. return submit_wait(ring);
  42. }
  43. static int do_opens(struct io_uring *ring, const char *path, int nr,
  44. int expect_enfile)
  45. {
  46. struct io_uring_cqe *cqe;
  47. struct io_uring_sqe *sqe;
  48. int i, ret;
  49. for (i = 0; i < nr; i++) {
  50. sqe = io_uring_get_sqe(ring);
  51. if (!sqe) {
  52. fprintf(stderr, "get sqe failed\n");
  53. goto err;
  54. }
  55. io_uring_prep_openat_direct(sqe, -1, path, O_RDONLY, 0, 0);
  56. sqe->file_index = UINT_MAX;
  57. ret = io_uring_submit(ring);
  58. if (ret <= 0) {
  59. fprintf(stderr, "sqe submit failed: %d\n", ret);
  60. goto err;
  61. }
  62. }
  63. for (i = 0; i < nr; i++) {
  64. ret = io_uring_wait_cqe(ring, &cqe);
  65. if (ret < 0) {
  66. fprintf(stderr, "wait completion %d\n", ret);
  67. goto err;
  68. }
  69. ret = cqe->res;
  70. if (ret < 0) {
  71. if (!expect_enfile || ret != -ENFILE) {
  72. printf("open=%d, %d\n", cqe->res, i);
  73. goto err;
  74. }
  75. if (!i && ret == -EINVAL) {
  76. no_direct_pick = 1;
  77. return 0;
  78. }
  79. }
  80. io_uring_cqe_seen(ring, cqe);
  81. }
  82. return 0;
  83. err:
  84. return 1;
  85. }
  86. static int test_openat(struct io_uring *ring, const char *path)
  87. {
  88. int ret, i;
  89. /* open all */
  90. ret = do_opens(ring, path, FDS, 0);
  91. if (ret)
  92. goto err;
  93. if (no_direct_pick)
  94. return 0;
  95. /* now close 100 randomly */
  96. for (i = 0; i < 100; i++) {
  97. do {
  98. int slot = rand() % FDS;
  99. ret = try_close(ring, slot);
  100. if (ret == -EBADF)
  101. continue;
  102. break;
  103. } while (1);
  104. }
  105. /* opening 100 should work, we closed 100 */
  106. ret = do_opens(ring, path, 100, 0);
  107. if (ret)
  108. goto err;
  109. /* we should be full now, expect -ENFILE */
  110. ret = do_opens(ring, path, 1, 1);
  111. if (ret)
  112. goto err;
  113. return ret;
  114. err:
  115. fprintf(stderr,"%s: err=%d\n", __FUNCTION__, ret);
  116. return -1;
  117. }
  118. int main(int argc, char *argv[])
  119. {
  120. struct io_uring ring;
  121. const char *path;
  122. int ret;
  123. if (argc > 1)
  124. return 0;
  125. ret = io_uring_queue_init(8, &ring, 0);
  126. if (ret) {
  127. fprintf(stderr, "ring setup failed\n");
  128. return 1;
  129. }
  130. ret = io_uring_register_files_sparse(&ring, FDS);
  131. if (ret ) {
  132. if (ret != -EINVAL) {
  133. fprintf(stderr, "Sparse file registration failed\n");
  134. return 1;
  135. }
  136. /* skip, kernel doesn't support sparse file array */
  137. return 0;
  138. }
  139. path = "/tmp/.open.direct.pick";
  140. t_create_file(path, 4096);
  141. ret = test_openat(&ring, path);
  142. if (ret < 0) {
  143. if (ret == -EINVAL) {
  144. fprintf(stdout, "Open not supported, skipping\n");
  145. goto done;
  146. }
  147. fprintf(stderr, "test_openat absolute failed: %d\n", ret);
  148. goto err;
  149. }
  150. done:
  151. unlink(path);
  152. return 0;
  153. err:
  154. unlink(path);
  155. return 1;
  156. }