read-inc-file.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test reading a normal file with incremental buffer
  5. * consumption. Some kernels had a bug where the initial part
  6. * of the buffer got skipped, test for that.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "liburing.h"
  14. #include "helpers.h"
  15. #define BUF_BGID 4
  16. #define BUF_BID 8
  17. static void arm_read(struct io_uring *ring, int fd, int offset)
  18. {
  19. struct io_uring_sqe *sqe;
  20. sqe = io_uring_get_sqe(ring);
  21. io_uring_prep_read(sqe, fd, NULL, 80, offset);
  22. sqe->flags = IOSQE_BUFFER_SELECT;
  23. sqe->buf_group = BUF_BGID;
  24. io_uring_submit(ring);
  25. }
  26. static int create_test_file(const char *fname)
  27. {
  28. char buf[80], c;
  29. int fd, i, ret;
  30. fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  31. if (fd < 0) {
  32. perror("open");
  33. return T_EXIT_FAIL;
  34. }
  35. c = 'a';
  36. for (i = 0; i < 8; i++) {
  37. memset(buf, c, sizeof(buf));
  38. ret = write(fd, buf, sizeof(buf));
  39. if (ret < 0) {
  40. perror("write");
  41. unlink(fname);
  42. return T_EXIT_FAIL;
  43. } else if (ret != sizeof(buf)) {
  44. fprintf(stderr, "Short write: %d\n", ret);
  45. unlink(fname);
  46. return T_EXIT_FAIL;
  47. }
  48. c++;
  49. }
  50. close(fd);
  51. return 0;
  52. }
  53. int main(int argc, char *argv[])
  54. {
  55. struct io_uring_buf_ring *br;
  56. struct io_uring_params p = { };
  57. struct io_uring_cqe *cqe;
  58. struct io_uring ring;
  59. int tret, ret, fd, i;
  60. char fname[64];
  61. char c = 'a';
  62. char *buf;
  63. void *ptr;
  64. if (argc > 1)
  65. return T_EXIT_SKIP;
  66. sprintf(fname, ".buf-inc-file.%d", getpid());
  67. if (create_test_file(fname))
  68. return T_EXIT_FAIL;
  69. fd = open(fname, O_RDONLY);
  70. if (fd < 0) {
  71. perror("open");
  72. goto err;
  73. }
  74. ret = io_uring_queue_init_params(64, &ring, &p);
  75. if (ret) {
  76. fprintf(stderr, "ring setup failed: %d\n", ret);
  77. goto err;
  78. }
  79. if (posix_memalign((void **) &buf, 4096, 65536))
  80. goto err;
  81. tret = T_EXIT_SKIP;
  82. br = io_uring_setup_buf_ring(&ring, 32, BUF_BGID, IOU_PBUF_RING_INC, &ret);
  83. if (!br) {
  84. if (ret == -EINVAL)
  85. goto out;
  86. fprintf(stderr, "Buffer ring register failed %d\n", ret);
  87. goto err;
  88. }
  89. tret = T_EXIT_PASS;
  90. io_uring_buf_ring_add(br, buf, 65536, BUF_BID, 31, 0);
  91. io_uring_buf_ring_advance(br, 1);
  92. memset(buf, 0, 65536);
  93. ptr = buf;
  94. for (i = 0; i < 4; i++) {
  95. int bid;
  96. arm_read(&ring, fd, i * 80);
  97. ret = io_uring_wait_cqe(&ring, &cqe);
  98. if (ret) {
  99. fprintf(stderr, "wait %d\n", ret);
  100. goto err;
  101. }
  102. if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
  103. fprintf(stderr, "buffer not assigned\n");
  104. goto err;
  105. }
  106. bid = cqe->flags >> IORING_CQE_BUFFER_SHIFT;
  107. if (bid != BUF_BID) {
  108. fprintf(stderr, "got wrong buffer bid %d\n", bid);
  109. goto err;
  110. }
  111. if (cqe->res != 80) {
  112. fprintf(stderr, "bad read size %d\n", ret);
  113. goto err;
  114. }
  115. io_uring_cqe_seen(&ring, cqe);
  116. if (!memchr(ptr, c, cqe->res)) {
  117. fprintf(stderr, "fail buffer check loop %d\n", i);
  118. goto err;
  119. }
  120. c++;
  121. ptr += cqe->res;
  122. }
  123. io_uring_free_buf_ring(&ring, br, 32, BUF_BGID);
  124. io_uring_queue_exit(&ring);
  125. out:
  126. free(buf);
  127. unlink(fname);
  128. return tret;
  129. err:
  130. unlink(fname);
  131. return T_EXIT_FAIL;
  132. }