fallocate.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring fallocate
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sys/resource.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <signal.h>
  17. #include "liburing.h"
  18. #include "helpers.h"
  19. static int no_fallocate;
  20. static int test_fallocate_rlimit(struct io_uring *ring)
  21. {
  22. struct io_uring_cqe *cqe;
  23. struct io_uring_sqe *sqe;
  24. struct rlimit rlim;
  25. char buf[32];
  26. int fd, ret;
  27. if (getrlimit(RLIMIT_FSIZE, &rlim) < 0) {
  28. perror("getrlimit");
  29. return 1;
  30. }
  31. rlim.rlim_cur = 64 * 1024;
  32. rlim.rlim_max = 64 * 1024;
  33. if (setrlimit(RLIMIT_FSIZE, &rlim) < 0) {
  34. perror("setrlimit");
  35. return 1;
  36. }
  37. sprintf(buf, "./XXXXXX");
  38. fd = mkstemp(buf);
  39. if (fd < 0) {
  40. perror("open");
  41. return 1;
  42. }
  43. unlink(buf);
  44. sqe = io_uring_get_sqe(ring);
  45. if (!sqe) {
  46. fprintf(stderr, "get sqe failed\n");
  47. goto err;
  48. }
  49. io_uring_prep_fallocate(sqe, fd, 0, 0, 128*1024);
  50. ret = io_uring_submit(ring);
  51. if (ret <= 0) {
  52. fprintf(stderr, "sqe submit failed: %d\n", ret);
  53. goto err;
  54. }
  55. ret = io_uring_wait_cqe(ring, &cqe);
  56. if (ret < 0) {
  57. fprintf(stderr, "wait completion %d\n", ret);
  58. goto err;
  59. }
  60. if (cqe->res == -EINVAL) {
  61. fprintf(stdout, "Fallocate not supported, skipping\n");
  62. no_fallocate = 1;
  63. goto skip;
  64. } else if (cqe->res != -EFBIG) {
  65. fprintf(stderr, "Expected -EFBIG: %d\n", cqe->res);
  66. goto err;
  67. }
  68. io_uring_cqe_seen(ring, cqe);
  69. return 0;
  70. skip:
  71. return T_EXIT_SKIP;
  72. err:
  73. return 1;
  74. }
  75. static int test_fallocate(struct io_uring *ring)
  76. {
  77. struct io_uring_cqe *cqe;
  78. struct io_uring_sqe *sqe;
  79. struct stat st;
  80. char buf[32];
  81. int fd, ret;
  82. sprintf(buf, "./XXXXXX");
  83. fd = mkstemp(buf);
  84. if (fd < 0) {
  85. perror("open");
  86. return 1;
  87. }
  88. unlink(buf);
  89. sqe = io_uring_get_sqe(ring);
  90. if (!sqe) {
  91. fprintf(stderr, "get sqe failed\n");
  92. goto err;
  93. }
  94. io_uring_prep_fallocate(sqe, fd, 0, 0, 128*1024);
  95. ret = io_uring_submit(ring);
  96. if (ret <= 0) {
  97. fprintf(stderr, "sqe submit failed: %d\n", ret);
  98. goto err;
  99. }
  100. ret = io_uring_wait_cqe(ring, &cqe);
  101. if (ret < 0) {
  102. fprintf(stderr, "wait completion %d\n", ret);
  103. goto err;
  104. }
  105. if (cqe->res == -EINVAL) {
  106. fprintf(stdout, "Fallocate not supported, skipping\n");
  107. no_fallocate = 1;
  108. goto skip;
  109. }
  110. if (cqe->res) {
  111. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  112. goto err;
  113. }
  114. io_uring_cqe_seen(ring, cqe);
  115. if (fstat(fd, &st) < 0) {
  116. perror("stat");
  117. goto err;
  118. }
  119. if (st.st_size != 128*1024) {
  120. fprintf(stderr, "Size mismatch: %llu\n",
  121. (unsigned long long) st.st_size);
  122. goto err;
  123. }
  124. return 0;
  125. skip:
  126. return T_EXIT_SKIP;
  127. err:
  128. return 1;
  129. }
  130. static int test_fallocate_fsync(struct io_uring *ring)
  131. {
  132. struct io_uring_cqe *cqe;
  133. struct io_uring_sqe *sqe;
  134. struct stat st;
  135. char buf[32];
  136. int fd, ret, i;
  137. if (no_fallocate)
  138. return 0;
  139. sprintf(buf, "./XXXXXX");
  140. fd = mkstemp(buf);
  141. if (fd < 0) {
  142. perror("open");
  143. return 1;
  144. }
  145. unlink(buf);
  146. sqe = io_uring_get_sqe(ring);
  147. if (!sqe) {
  148. fprintf(stderr, "get sqe failed\n");
  149. goto err;
  150. }
  151. io_uring_prep_fallocate(sqe, fd, 0, 0, 128*1024);
  152. sqe->flags |= IOSQE_IO_LINK;
  153. sqe->user_data = 1;
  154. sqe = io_uring_get_sqe(ring);
  155. if (!sqe) {
  156. fprintf(stderr, "get sqe failed\n");
  157. goto err;
  158. }
  159. io_uring_prep_fsync(sqe, fd, 0);
  160. sqe->user_data = 2;
  161. ret = io_uring_submit(ring);
  162. if (ret <= 0) {
  163. fprintf(stderr, "sqe submit failed: %d\n", ret);
  164. goto err;
  165. }
  166. for (i = 0; i < 2; i++) {
  167. ret = io_uring_wait_cqe(ring, &cqe);
  168. if (ret < 0) {
  169. fprintf(stderr, "wait completion %d\n", ret);
  170. goto err;
  171. }
  172. if (cqe->res) {
  173. fprintf(stderr, "cqe->res=%d,data=%" PRIu64 "\n", cqe->res,
  174. (uint64_t) cqe->user_data);
  175. goto err;
  176. }
  177. io_uring_cqe_seen(ring, cqe);
  178. }
  179. if (fstat(fd, &st) < 0) {
  180. perror("stat");
  181. goto err;
  182. }
  183. if (st.st_size != 128*1024) {
  184. fprintf(stderr, "Size mismatch: %llu\n",
  185. (unsigned long long) st.st_size);
  186. goto err;
  187. }
  188. return 0;
  189. err:
  190. return 1;
  191. }
  192. static void sig_xfsz(int sig)
  193. {
  194. }
  195. int main(int argc, char *argv[])
  196. {
  197. struct sigaction act = { };
  198. struct io_uring ring;
  199. int ret;
  200. if (argc > 1)
  201. return T_EXIT_SKIP;
  202. act.sa_handler = sig_xfsz;
  203. sigaction(SIGXFSZ, &act, NULL);
  204. ret = io_uring_queue_init(8, &ring, 0);
  205. if (ret) {
  206. fprintf(stderr, "ring setup failed\n");
  207. return T_EXIT_FAIL;
  208. }
  209. ret = test_fallocate(&ring);
  210. if (ret) {
  211. if (ret != T_EXIT_SKIP) {
  212. fprintf(stderr, "test_fallocate failed\n");
  213. }
  214. return ret;
  215. }
  216. ret = test_fallocate_fsync(&ring);
  217. if (ret) {
  218. fprintf(stderr, "test_fallocate_fsync failed\n");
  219. return ret;
  220. }
  221. ret = test_fallocate_rlimit(&ring);
  222. if (ret) {
  223. if (ret != T_EXIT_SKIP) {
  224. fprintf(stderr, "test_fallocate_rlimit failed\n");
  225. }
  226. return ret;
  227. }
  228. return T_EXIT_PASS;
  229. }