cmd-discard.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <sys/ioctl.h>
  9. #include <linux/fs.h>
  10. #include "liburing.h"
  11. #include "helpers.h"
  12. #define MAX_TEST_LBAS 1024
  13. static const char *filename;
  14. struct opcode {
  15. int op;
  16. bool test;
  17. bool not_supported;
  18. };
  19. #define TEST_BLOCK_URING_CMD_MAX 3
  20. static struct opcode opcodes[TEST_BLOCK_URING_CMD_MAX] = {
  21. { .op = BLOCK_URING_CMD_DISCARD, .test = true, },
  22. { .test = false, },
  23. { .test = false, },
  24. };
  25. static int lba_size;
  26. static uint64_t bdev_size;
  27. static uint64_t bdev_size_lbas;
  28. static char *buffer;
  29. static void prep_blk_cmd(struct io_uring_sqe *sqe, int fd,
  30. uint64_t from, uint64_t len,
  31. int cmd_op)
  32. {
  33. assert(cmd_op == BLOCK_URING_CMD_DISCARD);
  34. io_uring_prep_cmd_discard(sqe, fd, from, len);
  35. }
  36. static int queue_cmd_range(struct io_uring *ring, int bdev_fd,
  37. uint64_t from, uint64_t len, int cmd_op)
  38. {
  39. struct io_uring_sqe *sqe;
  40. struct io_uring_cqe *cqe;
  41. int err;
  42. sqe = io_uring_get_sqe(ring);
  43. assert(sqe != NULL);
  44. prep_blk_cmd(sqe, bdev_fd, from, len, cmd_op);
  45. err = io_uring_submit_and_wait(ring, 1);
  46. if (err != 1) {
  47. fprintf(stderr, "io_uring_submit_and_wait failed %d\n", err);
  48. exit(1);
  49. }
  50. err = io_uring_wait_cqe(ring, &cqe);
  51. if (err) {
  52. fprintf(stderr, "io_uring_wait_cqe failed %d (op %i)\n",
  53. err, cmd_op);
  54. exit(1);
  55. }
  56. err = cqe->res;
  57. io_uring_cqe_seen(ring, cqe);
  58. return err;
  59. }
  60. static int queue_cmd_lba(struct io_uring *ring, int bdev_fd,
  61. uint64_t from, uint64_t nr_lba, int cmd_op)
  62. {
  63. return queue_cmd_range(ring, bdev_fd, from * lba_size,
  64. nr_lba * lba_size, cmd_op);
  65. }
  66. static int queue_discard_lba(struct io_uring *ring, int bdev_fd,
  67. uint64_t from, uint64_t nr_lba)
  68. {
  69. return queue_cmd_lba(ring, bdev_fd, from, nr_lba,
  70. BLOCK_URING_CMD_DISCARD);
  71. }
  72. static int test_parallel(struct io_uring *ring, int fd, int cmd_op)
  73. {
  74. struct io_uring_sqe *sqe;
  75. struct io_uring_cqe *cqe;
  76. int inflight = 0;
  77. int max_inflight = 16;
  78. int left = 1000;
  79. int ret;
  80. while (left || inflight) {
  81. int queued = 0;
  82. unsigned head, nr_cqes = 0;
  83. int lba_len = 8;
  84. while (inflight < max_inflight && left) {
  85. int off = rand() % (MAX_TEST_LBAS - lba_len);
  86. sqe = io_uring_get_sqe(ring);
  87. assert(sqe != NULL);
  88. prep_blk_cmd(sqe, fd, off * lba_size,
  89. lba_len * lba_size, cmd_op);
  90. if (rand() & 1)
  91. sqe->flags |= IOSQE_ASYNC;
  92. queued++;
  93. left--;
  94. inflight++;
  95. }
  96. if (queued) {
  97. ret = io_uring_submit(ring);
  98. if (ret != queued) {
  99. fprintf(stderr, "io_uring_submit failed %d\n", ret);
  100. return T_EXIT_FAIL;
  101. }
  102. }
  103. ret = io_uring_wait_cqe(ring, &cqe);
  104. if (ret) {
  105. fprintf(stderr, "io_uring_wait_cqe failed %d\n", ret);
  106. exit(1);
  107. }
  108. io_uring_for_each_cqe(ring, head, cqe) {
  109. nr_cqes++;
  110. inflight--;
  111. if (cqe->res != 0) {
  112. fprintf(stderr, "cmd %i failed %i\n", cmd_op,
  113. cqe->res);
  114. return T_EXIT_FAIL;
  115. }
  116. }
  117. io_uring_cq_advance(ring, nr_cqes);
  118. }
  119. return 0;
  120. }
  121. static int cmd_issue_verify(struct io_uring *ring, int fd, int lba, int len,
  122. int cmd_op)
  123. {
  124. int verify = (cmd_op != BLOCK_URING_CMD_DISCARD);
  125. int ret, i;
  126. ssize_t res;
  127. if (verify) {
  128. for (i = 0; i < len; i++) {
  129. size_t off = (i + lba) * lba_size;
  130. res = pwrite(fd, buffer, lba_size, off);
  131. if (res == -1) {
  132. fprintf(stderr, "pwrite failed\n");
  133. return T_EXIT_FAIL;
  134. }
  135. }
  136. }
  137. ret = queue_cmd_lba(ring, fd, lba, len, cmd_op);
  138. if (ret) {
  139. if (ret == -EINVAL || ret == -EOPNOTSUPP)
  140. return T_EXIT_SKIP;
  141. fprintf(stderr, "cmd_issue_verify %i fail lba %i len %i ret %i\n",
  142. cmd_op, lba, len, ret);
  143. return T_EXIT_FAIL;
  144. }
  145. if (verify) {
  146. for (i = 0; i < len; i++) {
  147. size_t off = (i + lba) * lba_size;
  148. res = pread(fd, buffer, lba_size, off);
  149. if (res == -1) {
  150. fprintf(stderr, "pread failed\n");
  151. return T_EXIT_FAIL;
  152. }
  153. if (!memchr(buffer, 0, lba_size)) {
  154. fprintf(stderr, "mem cmp failed, lba %i\n", lba + i);
  155. return T_EXIT_FAIL;
  156. }
  157. }
  158. }
  159. return 0;
  160. }
  161. static int basic_cmd_test(struct io_uring *ring, int op)
  162. {
  163. int cmd_op = opcodes[op].op;
  164. int ret, fd;
  165. if (!opcodes[op].test)
  166. return T_EXIT_SKIP;
  167. fd = open(filename, O_DIRECT | O_RDWR | O_EXCL);
  168. if (fd < 0) {
  169. fprintf(stderr, "open failed %i\n", errno);
  170. return T_EXIT_FAIL;
  171. }
  172. ret = cmd_issue_verify(ring, fd, 0, 1, cmd_op);
  173. if (ret == T_EXIT_SKIP) {
  174. printf("cmd %i not supported, skip\n", cmd_op);
  175. opcodes[op].not_supported = 1;
  176. close(fd);
  177. return T_EXIT_SKIP;
  178. } else if (ret) {
  179. fprintf(stderr, "cmd %i fail 0 1\n", cmd_op);
  180. return T_EXIT_FAIL;
  181. }
  182. ret = cmd_issue_verify(ring, fd, 7, 15, cmd_op);
  183. if (ret) {
  184. fprintf(stderr, "cmd %i fail 7 15 %i\n", cmd_op, ret);
  185. return T_EXIT_FAIL;
  186. }
  187. ret = cmd_issue_verify(ring, fd, 1, MAX_TEST_LBAS - 1, cmd_op);
  188. if (ret) {
  189. fprintf(stderr, "large cmd %i failed %i\n", cmd_op, ret);
  190. return T_EXIT_FAIL;
  191. }
  192. ret = test_parallel(ring, fd, cmd_op);
  193. if (ret) {
  194. fprintf(stderr, "test_parallel() %i failed %i\n", cmd_op, ret);
  195. return T_EXIT_FAIL;
  196. }
  197. close(fd);
  198. return 0;
  199. }
  200. static int test_fail_edge_cases(struct io_uring *ring, int op)
  201. {
  202. int cmd_op = opcodes[op].op;
  203. int ret, fd;
  204. if (!opcodes[op].test)
  205. return T_EXIT_SKIP;
  206. fd = open(filename, O_DIRECT | O_RDWR | O_EXCL);
  207. if (fd < 0) {
  208. fprintf(stderr, "open failed %i\n", errno);
  209. return T_EXIT_FAIL;
  210. }
  211. ret = queue_cmd_lba(ring, fd, bdev_size_lbas, 1, cmd_op);
  212. if (ret >= 0) {
  213. fprintf(stderr, "cmd %i beyond capacity %i\n",
  214. cmd_op, ret);
  215. return 1;
  216. }
  217. ret = queue_cmd_lba(ring, fd, bdev_size_lbas - 1, 2, cmd_op);
  218. if (ret >= 0) {
  219. fprintf(stderr, "cmd %i beyond capacity with overlap %i\n",
  220. cmd_op, ret);
  221. return 1;
  222. }
  223. ret = queue_cmd_range(ring, fd, (uint64_t)-lba_size, lba_size + 2,
  224. cmd_op);
  225. if (ret >= 0) {
  226. fprintf(stderr, "cmd %i range overflow %i\n",
  227. cmd_op, ret);
  228. return 1;
  229. }
  230. ret = queue_cmd_range(ring, fd, lba_size / 2, lba_size, cmd_op);
  231. if (ret >= 0) {
  232. fprintf(stderr, "cmd %i unaligned offset %i\n",
  233. cmd_op, ret);
  234. return 1;
  235. }
  236. ret = queue_cmd_range(ring, fd, 0, lba_size / 2, cmd_op);
  237. if (ret >= 0) {
  238. fprintf(stderr, "cmd %i unaligned size %i\n",
  239. cmd_op, ret);
  240. return 1;
  241. }
  242. close(fd);
  243. return 0;
  244. }
  245. static int test_rdonly(struct io_uring *ring, int op)
  246. {
  247. int ret, fd;
  248. int ro;
  249. if (!opcodes[op].test)
  250. return T_EXIT_SKIP;
  251. fd = open(filename, O_DIRECT | O_RDONLY | O_EXCL);
  252. if (fd < 0) {
  253. fprintf(stderr, "open failed %i\n", errno);
  254. return T_EXIT_FAIL;
  255. }
  256. ret = queue_discard_lba(ring, fd, 0, 1);
  257. if (ret >= 0) {
  258. fprintf(stderr, "discarded with O_RDONLY %i\n", ret);
  259. return 1;
  260. }
  261. close(fd);
  262. fd = open(filename, O_DIRECT | O_RDWR | O_EXCL);
  263. if (fd < 0) {
  264. fprintf(stderr, "open failed %i\n", errno);
  265. return T_EXIT_FAIL;
  266. }
  267. ro = 1;
  268. ret = ioctl(fd, BLKROSET, &ro);
  269. if (ret) {
  270. fprintf(stderr, "BLKROSET 1 failed %i\n", errno);
  271. return T_EXIT_FAIL;
  272. }
  273. ret = queue_discard_lba(ring, fd, 0, 1);
  274. if (ret >= 0) {
  275. fprintf(stderr, "discarded with O_RDONLY %i\n", ret);
  276. return 1;
  277. }
  278. ro = 0;
  279. ret = ioctl(fd, BLKROSET, &ro);
  280. if (ret) {
  281. fprintf(stderr, "BLKROSET 0 failed %i\n", errno);
  282. return T_EXIT_FAIL;
  283. }
  284. close(fd);
  285. return 0;
  286. }
  287. int main(int argc, char *argv[])
  288. {
  289. struct io_uring ring;
  290. int fd, ret, i, fret;
  291. int cmd_op;
  292. if (argc != 2)
  293. return T_EXIT_SKIP;
  294. filename = argv[1];
  295. fd = open(filename, O_DIRECT | O_RDONLY | O_EXCL);
  296. if (fd < 0) {
  297. fprintf(stderr, "open failed %i\n", errno);
  298. return T_EXIT_FAIL;
  299. }
  300. ret = ioctl(fd, BLKGETSIZE64, &bdev_size);
  301. if (ret < 0) {
  302. fprintf(stderr, "BLKGETSIZE64 failed %i\n", errno);
  303. return T_EXIT_FAIL;
  304. }
  305. ret = ioctl(fd, BLKSSZGET, &lba_size);
  306. if (ret < 0) {
  307. fprintf(stderr, "BLKSSZGET failed %i\n", errno);
  308. return T_EXIT_FAIL;
  309. }
  310. assert(bdev_size % lba_size == 0);
  311. bdev_size_lbas = bdev_size / lba_size;
  312. close(fd);
  313. buffer = aligned_alloc(lba_size, lba_size);
  314. if (!buffer) {
  315. fprintf(stderr, "aligned_alloc failed\n");
  316. return T_EXIT_FAIL;
  317. }
  318. for (i = 0; i < lba_size; i++)
  319. buffer[i] = i ^ 0xA7;
  320. if (bdev_size_lbas < MAX_TEST_LBAS) {
  321. fprintf(stderr, "the device is too small, skip\n");
  322. return T_EXIT_SKIP;
  323. }
  324. ret = io_uring_queue_init(16, &ring, 0);
  325. if (ret) {
  326. fprintf(stderr, "queue init failed: %d\n", ret);
  327. return T_EXIT_FAIL;
  328. }
  329. fret = T_EXIT_SKIP;
  330. for (cmd_op = 0; cmd_op < TEST_BLOCK_URING_CMD_MAX; cmd_op++) {
  331. if (!opcodes[cmd_op].test)
  332. continue;
  333. ret = basic_cmd_test(&ring, cmd_op);
  334. if (ret) {
  335. if (ret == T_EXIT_SKIP)
  336. continue;
  337. fprintf(stderr, "basic_cmd_test() failed, cmd %i\n",
  338. cmd_op);
  339. return T_EXIT_FAIL;
  340. }
  341. ret = test_rdonly(&ring, cmd_op);
  342. if (ret) {
  343. fprintf(stderr, "test_rdonly() failed, cmd %i\n",
  344. cmd_op);
  345. return T_EXIT_FAIL;
  346. }
  347. ret = test_fail_edge_cases(&ring, cmd_op);
  348. if (ret) {
  349. fprintf(stderr, "test_fail_edge_cases() failed, cmd %i\n",
  350. cmd_op);
  351. return T_EXIT_FAIL;
  352. }
  353. fret = T_EXIT_PASS;
  354. }
  355. io_uring_queue_exit(&ring);
  356. free(buffer);
  357. return fret;
  358. }