fixed-reuse.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: link <open file><read from file><close file> with an existing
  5. * file present in the opened slot, verifying that we get the new file
  6. * rather than the old one.
  7. *
  8. */
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <fcntl.h>
  15. #include "liburing.h"
  16. #include "helpers.h"
  17. #define MAX_FILES 8
  18. #define FNAME1 ".slot.reuse.1"
  19. #define FNAME2 ".slot.reuse.2"
  20. #define PAT1 0xaa
  21. #define PAT2 0x55
  22. #define BSIZE 4096
  23. static int test(struct io_uring *ring)
  24. {
  25. struct io_uring_cqe *cqe;
  26. struct io_uring_sqe *sqe;
  27. char buf[BSIZE];
  28. int ret, i;
  29. /* open FNAME1 in slot 0 */
  30. sqe = io_uring_get_sqe(ring);
  31. io_uring_prep_openat_direct(sqe, AT_FDCWD, FNAME1, O_RDONLY, 0, 0);
  32. sqe->user_data = 1;
  33. ret = io_uring_submit(ring);
  34. if (ret != 1) {
  35. fprintf(stderr, "sqe submit failed: %d\n", ret);
  36. goto err;
  37. }
  38. ret = io_uring_wait_cqe(ring, &cqe);
  39. if (ret < 0) {
  40. fprintf(stderr, "wait completion %d\n", ret);
  41. goto err;
  42. }
  43. if (cqe->res != 0) {
  44. fprintf(stderr, "open res %d\n", ret);
  45. goto err;
  46. }
  47. io_uring_cqe_seen(ring, cqe);
  48. /*
  49. * Now open FNAME2 in that same slot, verifying we get data from
  50. * FNAME2 and not FNAME1.
  51. */
  52. sqe = io_uring_get_sqe(ring);
  53. io_uring_prep_openat_direct(sqe, AT_FDCWD, FNAME2, O_RDONLY, 0, 0);
  54. sqe->flags |= IOSQE_IO_LINK;
  55. sqe->user_data = 2;
  56. sqe = io_uring_get_sqe(ring);
  57. io_uring_prep_read(sqe, 0, buf, sizeof(buf), 0);
  58. sqe->flags |= IOSQE_FIXED_FILE;
  59. sqe->flags |= IOSQE_IO_LINK;
  60. sqe->user_data = 3;
  61. sqe = io_uring_get_sqe(ring);
  62. io_uring_prep_close_direct(sqe, 0);
  63. sqe->user_data = 4;
  64. ret = io_uring_submit(ring);
  65. if (ret != 3) {
  66. fprintf(stderr, "sqe submit failed: %d\n", ret);
  67. goto err;
  68. }
  69. for (i = 0; i < 3; i++) {
  70. ret = io_uring_wait_cqe(ring, &cqe);
  71. if (ret < 0) {
  72. fprintf(stderr, "wait completion %d\n", ret);
  73. goto err;
  74. }
  75. switch (cqe->user_data) {
  76. case 2:
  77. if (cqe->res) {
  78. fprintf(stderr, "bad open %d\n", cqe->res);
  79. goto err;
  80. }
  81. break;
  82. case 3:
  83. if (cqe->res != sizeof(buf)) {
  84. fprintf(stderr, "bad read %d\n", cqe->res);
  85. goto err;
  86. }
  87. break;
  88. case 4:
  89. if (cqe->res) {
  90. fprintf(stderr, "bad close %d\n", cqe->res);
  91. goto err;
  92. }
  93. break;
  94. }
  95. io_uring_cqe_seen(ring, cqe);
  96. }
  97. for (i = 0; i < sizeof(buf); i++) {
  98. if (buf[i] == PAT2)
  99. continue;
  100. fprintf(stderr, "Bad pattern %x at %d\n", buf[i], i);
  101. goto err;
  102. }
  103. return 0;
  104. err:
  105. return 1;
  106. }
  107. int main(int argc, char *argv[])
  108. {
  109. struct io_uring ring;
  110. struct io_uring_params p = { };
  111. int ret, files[MAX_FILES];
  112. if (argc > 1)
  113. return T_EXIT_SKIP;
  114. ret = io_uring_queue_init_params(8, &ring, &p);
  115. if (ret) {
  116. fprintf(stderr, "ring setup failed: %d\n", ret);
  117. return T_EXIT_FAIL;
  118. }
  119. if (!(p.features & IORING_FEAT_CQE_SKIP))
  120. return T_EXIT_SKIP;
  121. memset(files, -1, sizeof(files));
  122. ret = io_uring_register_files(&ring, files, ARRAY_SIZE(files));
  123. if (ret) {
  124. fprintf(stderr, "Failed registering files\n");
  125. return T_EXIT_FAIL;
  126. }
  127. t_create_file_pattern(FNAME1, 4096, PAT1);
  128. t_create_file_pattern(FNAME2, 4096, PAT2);
  129. ret = test(&ring);
  130. if (ret) {
  131. fprintf(stderr, "test failed\n");
  132. goto err;
  133. }
  134. unlink(FNAME1);
  135. unlink(FNAME2);
  136. return T_EXIT_PASS;
  137. err:
  138. unlink(FNAME1);
  139. unlink(FNAME2);
  140. return T_EXIT_FAIL;
  141. }