file-register.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various file registration 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 <sys/resource.h>
  15. #include "helpers.h"
  16. #include "liburing.h"
  17. static int no_update = 0;
  18. static void close_files(int *files, int nr_files, int add)
  19. {
  20. char fname[32];
  21. int i;
  22. for (i = 0; i < nr_files; i++) {
  23. if (files)
  24. close(files[i]);
  25. if (!add)
  26. sprintf(fname, ".reg.%d", i);
  27. else
  28. sprintf(fname, ".add.%d", i + add);
  29. unlink(fname);
  30. }
  31. if (files)
  32. free(files);
  33. }
  34. static int *open_files(int nr_files, int extra, int add)
  35. {
  36. char fname[32];
  37. int *files;
  38. int i;
  39. files = t_calloc(nr_files + extra, sizeof(int));
  40. for (i = 0; i < nr_files; i++) {
  41. if (!add)
  42. sprintf(fname, ".reg.%d", i);
  43. else
  44. sprintf(fname, ".add.%d", i + add);
  45. files[i] = open(fname, O_RDWR | O_CREAT, 0644);
  46. if (files[i] < 0) {
  47. perror("open");
  48. free(files);
  49. files = NULL;
  50. break;
  51. }
  52. }
  53. if (extra) {
  54. for (i = nr_files; i < nr_files + extra; i++)
  55. files[i] = -1;
  56. }
  57. return files;
  58. }
  59. static int test_shrink(struct io_uring *ring)
  60. {
  61. int ret, off, fd;
  62. int *files;
  63. files = open_files(50, 0, 0);
  64. ret = io_uring_register_files(ring, files, 50);
  65. if (ret) {
  66. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  67. goto err;
  68. }
  69. off = 0;
  70. do {
  71. fd = -1;
  72. ret = io_uring_register_files_update(ring, off, &fd, 1);
  73. if (ret != 1) {
  74. if (off == 50 && ret == -EINVAL)
  75. break;
  76. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  77. break;
  78. }
  79. off++;
  80. } while (1);
  81. ret = io_uring_unregister_files(ring);
  82. if (ret) {
  83. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  84. goto err;
  85. }
  86. close_files(files, 50, 0);
  87. return 0;
  88. err:
  89. close_files(files, 50, 0);
  90. return 1;
  91. }
  92. static int test_grow(struct io_uring *ring)
  93. {
  94. int ret, off;
  95. int *files, *fds = NULL;
  96. files = open_files(50, 250, 0);
  97. ret = io_uring_register_files(ring, files, 300);
  98. if (ret) {
  99. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  100. goto err;
  101. }
  102. off = 50;
  103. do {
  104. fds = open_files(1, 0, off);
  105. ret = io_uring_register_files_update(ring, off, fds, 1);
  106. if (ret != 1) {
  107. if (off == 300 && ret == -EINVAL)
  108. break;
  109. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  110. break;
  111. }
  112. if (off >= 300) {
  113. fprintf(stderr, "%s: Succeeded beyond end-of-list?\n", __FUNCTION__);
  114. goto err;
  115. }
  116. off++;
  117. } while (1);
  118. ret = io_uring_unregister_files(ring);
  119. if (ret) {
  120. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  121. goto err;
  122. }
  123. close_files(files, 100, 0);
  124. close_files(NULL, 251, 50);
  125. return 0;
  126. err:
  127. close_files(files, 100, 0);
  128. close_files(NULL, 251, 50);
  129. return 1;
  130. }
  131. static int test_replace_all(struct io_uring *ring)
  132. {
  133. int *files, *fds = NULL;
  134. int ret, i;
  135. files = open_files(100, 0, 0);
  136. ret = io_uring_register_files(ring, files, 100);
  137. if (ret) {
  138. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  139. goto err;
  140. }
  141. fds = t_malloc(100 * sizeof(int));
  142. for (i = 0; i < 100; i++)
  143. fds[i] = -1;
  144. ret = io_uring_register_files_update(ring, 0, fds, 100);
  145. if (ret != 100) {
  146. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  147. goto err;
  148. }
  149. ret = io_uring_unregister_files(ring);
  150. if (ret) {
  151. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  152. goto err;
  153. }
  154. close_files(files, 100, 0);
  155. if (fds)
  156. free(fds);
  157. return 0;
  158. err:
  159. close_files(files, 100, 0);
  160. if (fds)
  161. free(fds);
  162. return 1;
  163. }
  164. static int test_replace(struct io_uring *ring)
  165. {
  166. int *files, *fds = NULL;
  167. int ret;
  168. files = open_files(100, 0, 0);
  169. ret = io_uring_register_files(ring, files, 100);
  170. if (ret) {
  171. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  172. goto err;
  173. }
  174. fds = open_files(10, 0, 1);
  175. ret = io_uring_register_files_update(ring, 90, fds, 10);
  176. if (ret != 10) {
  177. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  178. goto err;
  179. }
  180. ret = io_uring_unregister_files(ring);
  181. if (ret) {
  182. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  183. goto err;
  184. }
  185. close_files(files, 100, 0);
  186. if (fds)
  187. close_files(fds, 10, 1);
  188. return 0;
  189. err:
  190. close_files(files, 100, 0);
  191. if (fds)
  192. close_files(fds, 10, 1);
  193. return 1;
  194. }
  195. static int test_removals(struct io_uring *ring)
  196. {
  197. int *files, *fds = NULL;
  198. int ret, i;
  199. files = open_files(100, 0, 0);
  200. ret = io_uring_register_files(ring, files, 100);
  201. if (ret) {
  202. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  203. goto err;
  204. }
  205. fds = t_calloc(10, sizeof(int));
  206. for (i = 0; i < 10; i++)
  207. fds[i] = -1;
  208. ret = io_uring_register_files_update(ring, 50, fds, 10);
  209. if (ret != 10) {
  210. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  211. goto err;
  212. }
  213. ret = io_uring_unregister_files(ring);
  214. if (ret) {
  215. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  216. goto err;
  217. }
  218. close_files(files, 100, 0);
  219. if (fds)
  220. free(fds);
  221. return 0;
  222. err:
  223. close_files(files, 100, 0);
  224. if (fds)
  225. free(fds);
  226. return 1;
  227. }
  228. static int test_additions(struct io_uring *ring)
  229. {
  230. int *files, *fds = NULL;
  231. int ret;
  232. files = open_files(100, 100, 0);
  233. ret = io_uring_register_files(ring, files, 200);
  234. if (ret) {
  235. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  236. goto err;
  237. }
  238. fds = open_files(2, 0, 1);
  239. ret = io_uring_register_files_update(ring, 100, fds, 2);
  240. if (ret != 2) {
  241. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  242. goto err;
  243. }
  244. ret = io_uring_unregister_files(ring);
  245. if (ret) {
  246. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  247. goto err;
  248. }
  249. close_files(files, 100, 0);
  250. if (fds)
  251. close_files(fds, 2, 1);
  252. return 0;
  253. err:
  254. close_files(files, 100, 0);
  255. if (fds)
  256. close_files(fds, 2, 1);
  257. return 1;
  258. }
  259. static int test_sparse(struct io_uring *ring)
  260. {
  261. int *files;
  262. int ret;
  263. files = open_files(100, 100, 0);
  264. ret = io_uring_register_files(ring, files, 200);
  265. if (ret) {
  266. if (ret == -EBADF) {
  267. fprintf(stdout, "Sparse files not supported, skipping\n");
  268. no_update = 1;
  269. goto done;
  270. }
  271. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  272. goto err;
  273. }
  274. ret = io_uring_unregister_files(ring);
  275. if (ret) {
  276. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  277. goto err;
  278. }
  279. done:
  280. close_files(files, 100, 0);
  281. return 0;
  282. err:
  283. close_files(files, 100, 0);
  284. return 1;
  285. }
  286. static int test_basic_many(struct io_uring *ring)
  287. {
  288. int *files;
  289. int ret;
  290. files = open_files(768, 0, 0);
  291. ret = io_uring_register_files(ring, files, 768);
  292. if (ret) {
  293. fprintf(stderr, "%s: register %d\n", __FUNCTION__, ret);
  294. goto err;
  295. }
  296. ret = io_uring_unregister_files(ring);
  297. if (ret) {
  298. fprintf(stderr, "%s: unregister %d\n", __FUNCTION__, ret);
  299. goto err;
  300. }
  301. close_files(files, 768, 0);
  302. return 0;
  303. err:
  304. close_files(files, 768, 0);
  305. return 1;
  306. }
  307. static int test_basic(struct io_uring *ring, int fail)
  308. {
  309. int *files;
  310. int ret;
  311. int nr_files = fail ? 10 : 100;
  312. files = open_files(nr_files, 0, 0);
  313. ret = io_uring_register_files(ring, files, 100);
  314. if (ret) {
  315. if (fail) {
  316. if (ret == -EBADF || ret == -EFAULT)
  317. return 0;
  318. }
  319. fprintf(stderr, "%s: register %d\n", __FUNCTION__, ret);
  320. goto err;
  321. }
  322. if (fail) {
  323. fprintf(stderr, "Registration succeeded, but expected fail\n");
  324. goto err;
  325. }
  326. ret = io_uring_unregister_files(ring);
  327. if (ret) {
  328. fprintf(stderr, "%s: unregister %d\n", __FUNCTION__, ret);
  329. goto err;
  330. }
  331. close_files(files, nr_files, 0);
  332. return 0;
  333. err:
  334. close_files(files, nr_files, 0);
  335. return 1;
  336. }
  337. /*
  338. * Register 0 files, but reserve space for 10. Then add one file.
  339. */
  340. static int test_zero(struct io_uring *ring)
  341. {
  342. int *files, *fds = NULL;
  343. int ret;
  344. files = open_files(0, 10, 0);
  345. ret = io_uring_register_files(ring, files, 10);
  346. if (ret) {
  347. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  348. goto err;
  349. }
  350. fds = open_files(1, 0, 1);
  351. ret = io_uring_register_files_update(ring, 0, fds, 1);
  352. if (ret != 1) {
  353. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  354. goto err;
  355. }
  356. ret = io_uring_unregister_files(ring);
  357. if (ret) {
  358. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  359. goto err;
  360. }
  361. if (fds)
  362. close_files(fds, 1, 1);
  363. free(files);
  364. return 0;
  365. err:
  366. if (fds)
  367. close_files(fds, 1, 1);
  368. free(files);
  369. return 1;
  370. }
  371. static int test_fixed_read_write(struct io_uring *ring, int index)
  372. {
  373. struct io_uring_sqe *sqe;
  374. struct io_uring_cqe *cqe;
  375. struct iovec iov[2];
  376. int ret;
  377. iov[0].iov_base = t_malloc(4096);
  378. iov[0].iov_len = 4096;
  379. memset(iov[0].iov_base, 0x5a, 4096);
  380. iov[1].iov_base = t_malloc(4096);
  381. iov[1].iov_len = 4096;
  382. sqe = io_uring_get_sqe(ring);
  383. if (!sqe) {
  384. fprintf(stderr, "%s: failed to get sqe\n", __FUNCTION__);
  385. return 1;
  386. }
  387. io_uring_prep_writev(sqe, index, &iov[0], 1, 0);
  388. sqe->flags |= IOSQE_FIXED_FILE;
  389. sqe->user_data = 1;
  390. ret = io_uring_submit(ring);
  391. if (ret != 1) {
  392. fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
  393. return 1;
  394. }
  395. ret = io_uring_wait_cqe(ring, &cqe);
  396. if (ret < 0) {
  397. fprintf(stderr, "%s: io_uring_wait_cqe=%d\n", __FUNCTION__, ret);
  398. return 1;
  399. }
  400. if (cqe->res != 4096) {
  401. fprintf(stderr, "%s: write cqe->res=%d\n", __FUNCTION__, cqe->res);
  402. return 1;
  403. }
  404. io_uring_cqe_seen(ring, cqe);
  405. sqe = io_uring_get_sqe(ring);
  406. if (!sqe) {
  407. fprintf(stderr, "%s: failed to get sqe\n", __FUNCTION__);
  408. return 1;
  409. }
  410. io_uring_prep_readv(sqe, index, &iov[1], 1, 0);
  411. sqe->flags |= IOSQE_FIXED_FILE;
  412. sqe->user_data = 2;
  413. ret = io_uring_submit(ring);
  414. if (ret != 1) {
  415. fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
  416. return 1;
  417. }
  418. ret = io_uring_wait_cqe(ring, &cqe);
  419. if (ret < 0) {
  420. fprintf(stderr, "%s: io_uring_wait_cqe=%d\n", __FUNCTION__, ret);
  421. return 1;
  422. }
  423. if (cqe->res != 4096) {
  424. fprintf(stderr, "%s: read cqe->res=%d\n", __FUNCTION__, cqe->res);
  425. return 1;
  426. }
  427. io_uring_cqe_seen(ring, cqe);
  428. if (memcmp(iov[1].iov_base, iov[0].iov_base, 4096)) {
  429. fprintf(stderr, "%s: data mismatch\n", __FUNCTION__);
  430. return 1;
  431. }
  432. free(iov[0].iov_base);
  433. free(iov[1].iov_base);
  434. return 0;
  435. }
  436. static void adjust_nfiles(int want_files)
  437. {
  438. struct rlimit rlim;
  439. if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
  440. return;
  441. if (rlim.rlim_cur >= want_files)
  442. return;
  443. rlim.rlim_cur = want_files;
  444. setrlimit(RLIMIT_NOFILE, &rlim);
  445. }
  446. /*
  447. * Register 8K of sparse files, update one at a random spot, then do some
  448. * file IO to verify it works.
  449. */
  450. static int test_huge(struct io_uring *ring)
  451. {
  452. int *files;
  453. int ret;
  454. adjust_nfiles(16384);
  455. files = open_files(0, 8192, 0);
  456. ret = io_uring_register_files(ring, files, 8192);
  457. if (ret) {
  458. /* huge sets not supported */
  459. if (ret == -EMFILE) {
  460. fprintf(stdout, "%s: No huge file set support, skipping\n", __FUNCTION__);
  461. goto out;
  462. }
  463. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  464. goto err;
  465. }
  466. files[7193] = open(".reg.7193", O_RDWR | O_CREAT, 0644);
  467. if (files[7193] < 0) {
  468. fprintf(stderr, "%s: open=%d\n", __FUNCTION__, errno);
  469. goto err;
  470. }
  471. ret = io_uring_register_files_update(ring, 7193, &files[7193], 1);
  472. if (ret != 1) {
  473. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  474. goto err;
  475. }
  476. if (test_fixed_read_write(ring, 7193))
  477. goto err;
  478. ret = io_uring_unregister_files(ring);
  479. if (ret) {
  480. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  481. goto err;
  482. }
  483. if (files[7193] != -1) {
  484. close(files[7193]);
  485. unlink(".reg.7193");
  486. }
  487. out:
  488. free(files);
  489. return 0;
  490. err:
  491. if (files[7193] != -1) {
  492. close(files[7193]);
  493. unlink(".reg.7193");
  494. }
  495. free(files);
  496. return 1;
  497. }
  498. static int test_skip(struct io_uring *ring)
  499. {
  500. int *files;
  501. int ret;
  502. files = open_files(100, 0, 0);
  503. ret = io_uring_register_files(ring, files, 100);
  504. if (ret) {
  505. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  506. goto err;
  507. }
  508. files[90] = IORING_REGISTER_FILES_SKIP;
  509. ret = io_uring_register_files_update(ring, 90, &files[90], 1);
  510. if (ret != 1) {
  511. if (ret == -EBADF) {
  512. fprintf(stdout, "Skipping files not supported\n");
  513. goto done;
  514. }
  515. fprintf(stderr, "%s: update ret=%d\n", __FUNCTION__, ret);
  516. goto err;
  517. }
  518. /* verify can still use file index 90 */
  519. if (test_fixed_read_write(ring, 90))
  520. goto err;
  521. ret = io_uring_unregister_files(ring);
  522. if (ret) {
  523. fprintf(stderr, "%s: unregister ret=%d\n", __FUNCTION__, ret);
  524. goto err;
  525. }
  526. done:
  527. close_files(files, 100, 0);
  528. return 0;
  529. err:
  530. close_files(files, 100, 0);
  531. return 1;
  532. }
  533. static int test_sparse_updates(void)
  534. {
  535. struct io_uring ring;
  536. int ret, i, *fds, newfd;
  537. ret = io_uring_queue_init(8, &ring, 0);
  538. if (ret) {
  539. fprintf(stderr, "queue_init: %d\n", ret);
  540. return ret;
  541. }
  542. fds = t_malloc(256 * sizeof(int));
  543. for (i = 0; i < 256; i++)
  544. fds[i] = -1;
  545. ret = io_uring_register_files(&ring, fds, 256);
  546. if (ret) {
  547. fprintf(stderr, "file_register: %d\n", ret);
  548. return ret;
  549. }
  550. newfd = 1;
  551. for (i = 0; i < 256; i++) {
  552. ret = io_uring_register_files_update(&ring, i, &newfd, 1);
  553. if (ret != 1) {
  554. fprintf(stderr, "file_update: %d\n", ret);
  555. return ret;
  556. }
  557. }
  558. io_uring_unregister_files(&ring);
  559. for (i = 0; i < 256; i++)
  560. fds[i] = 1;
  561. ret = io_uring_register_files(&ring, fds, 256);
  562. if (ret) {
  563. fprintf(stderr, "file_register: %d\n", ret);
  564. return ret;
  565. }
  566. newfd = -1;
  567. for (i = 0; i < 256; i++) {
  568. ret = io_uring_register_files_update(&ring, i, &newfd, 1);
  569. if (ret != 1) {
  570. fprintf(stderr, "file_update: %d\n", ret);
  571. return ret;
  572. }
  573. }
  574. io_uring_unregister_files(&ring);
  575. io_uring_queue_exit(&ring);
  576. return 0;
  577. }
  578. static int test_fixed_removal_ordering(void)
  579. {
  580. char buffer[128];
  581. struct io_uring ring;
  582. struct io_uring_sqe *sqe;
  583. struct io_uring_cqe *cqe;
  584. struct __kernel_timespec ts;
  585. int ret, fd, i, fds[2];
  586. ret = io_uring_queue_init(8, &ring, 0);
  587. if (ret < 0) {
  588. fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
  589. return ret;
  590. }
  591. if (pipe(fds)) {
  592. perror("pipe");
  593. return -1;
  594. }
  595. ret = io_uring_register_files(&ring, fds, 2);
  596. if (ret) {
  597. fprintf(stderr, "file_register: %d\n", ret);
  598. return ret;
  599. }
  600. /* ring should have fds referenced, can close them */
  601. close(fds[0]);
  602. close(fds[1]);
  603. sqe = io_uring_get_sqe(&ring);
  604. if (!sqe) {
  605. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  606. return 1;
  607. }
  608. /* outwait file recycling delay */
  609. ts.tv_sec = 3;
  610. ts.tv_nsec = 0;
  611. io_uring_prep_timeout(sqe, &ts, 0, 0);
  612. sqe->flags |= IOSQE_IO_LINK | IOSQE_IO_HARDLINK;
  613. sqe->user_data = 1;
  614. sqe = io_uring_get_sqe(&ring);
  615. if (!sqe) {
  616. printf("get sqe failed\n");
  617. return -1;
  618. }
  619. io_uring_prep_write(sqe, 1, buffer, sizeof(buffer), 0);
  620. sqe->flags |= IOSQE_FIXED_FILE;
  621. sqe->user_data = 2;
  622. ret = io_uring_submit(&ring);
  623. if (ret != 2) {
  624. fprintf(stderr, "%s: got %d, wanted 2\n", __FUNCTION__, ret);
  625. return -1;
  626. }
  627. /* remove unused pipe end */
  628. fd = -1;
  629. ret = io_uring_register_files_update(&ring, 0, &fd, 1);
  630. if (ret != 1) {
  631. fprintf(stderr, "update off=0 failed\n");
  632. return -1;
  633. }
  634. /* remove used pipe end */
  635. fd = -1;
  636. ret = io_uring_register_files_update(&ring, 1, &fd, 1);
  637. if (ret != 1) {
  638. fprintf(stderr, "update off=1 failed\n");
  639. return -1;
  640. }
  641. for (i = 0; i < 2; ++i) {
  642. ret = io_uring_wait_cqe(&ring, &cqe);
  643. if (ret < 0) {
  644. fprintf(stderr, "%s: io_uring_wait_cqe=%d\n", __FUNCTION__, ret);
  645. return 1;
  646. }
  647. io_uring_cqe_seen(&ring, cqe);
  648. }
  649. io_uring_queue_exit(&ring);
  650. return 0;
  651. }
  652. /* mix files requiring SCM-accounting and not in a single register */
  653. static int test_mixed_af_unix(void)
  654. {
  655. struct io_uring ring;
  656. int i, ret, fds[2];
  657. int reg_fds[32];
  658. int sp[2];
  659. ret = io_uring_queue_init(8, &ring, 0);
  660. if (ret < 0) {
  661. fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
  662. return ret;
  663. }
  664. if (pipe(fds)) {
  665. perror("pipe");
  666. return -1;
  667. }
  668. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sp) != 0) {
  669. perror("Failed to create Unix-domain socket pair\n");
  670. return 1;
  671. }
  672. for (i = 0; i < 16; i++) {
  673. reg_fds[i * 2] = fds[0];
  674. reg_fds[i * 2 + 1] = sp[0];
  675. }
  676. ret = io_uring_register_files(&ring, reg_fds, 32);
  677. if (ret) {
  678. fprintf(stderr, "file_register: %d\n", ret);
  679. return ret;
  680. }
  681. close(fds[0]);
  682. close(fds[1]);
  683. close(sp[0]);
  684. close(sp[1]);
  685. io_uring_queue_exit(&ring);
  686. return 0;
  687. }
  688. static int test_partial_register_fail(void)
  689. {
  690. char buffer[128];
  691. struct io_uring ring;
  692. int ret, fds[2];
  693. int reg_fds[5];
  694. ret = io_uring_queue_init(8, &ring, 0);
  695. if (ret < 0) {
  696. fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
  697. return ret;
  698. }
  699. if (pipe(fds)) {
  700. perror("pipe");
  701. return -1;
  702. }
  703. /*
  704. * Expect register to fail as it doesn't support io_uring fds, shouldn't
  705. * leave any fds referenced afterwards.
  706. */
  707. reg_fds[0] = fds[0];
  708. reg_fds[1] = fds[1];
  709. reg_fds[2] = -1;
  710. reg_fds[3] = ring.ring_fd;
  711. reg_fds[4] = -1;
  712. ret = io_uring_register_files(&ring, reg_fds, 5);
  713. if (!ret) {
  714. fprintf(stderr, "file_register unexpectedly succeeded\n");
  715. return 1;
  716. }
  717. /* ring should have fds referenced, can close them */
  718. close(fds[1]);
  719. /* confirm that fds[1] is actually close and to ref'ed by io_uring */
  720. ret = read(fds[0], buffer, 10);
  721. if (ret < 0)
  722. perror("read");
  723. close(fds[0]);
  724. io_uring_queue_exit(&ring);
  725. return 0;
  726. }
  727. static int file_update_alloc(struct io_uring *ring, int *fd)
  728. {
  729. struct io_uring_sqe *sqe;
  730. struct io_uring_cqe *cqe;
  731. int ret;
  732. sqe = io_uring_get_sqe(ring);
  733. io_uring_prep_files_update(sqe, fd, 1, IORING_FILE_INDEX_ALLOC);
  734. ret = io_uring_submit(ring);
  735. if (ret != 1) {
  736. fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
  737. return -1;
  738. }
  739. ret = io_uring_wait_cqe(ring, &cqe);
  740. if (ret < 0) {
  741. fprintf(stderr, "%s: io_uring_wait_cqe=%d\n", __FUNCTION__, ret);
  742. return -1;
  743. }
  744. ret = cqe->res;
  745. io_uring_cqe_seen(ring, cqe);
  746. return ret;
  747. }
  748. static int test_out_of_range_file_ranges(struct io_uring *ring)
  749. {
  750. int ret;
  751. ret = io_uring_register_file_alloc_range(ring, 8, 3);
  752. if (ret != -EINVAL) {
  753. fprintf(stderr, "overlapping range %i\n", ret);
  754. return 1;
  755. }
  756. ret = io_uring_register_file_alloc_range(ring, 10, 1);
  757. if (ret != -EINVAL) {
  758. fprintf(stderr, "out of range index %i\n", ret);
  759. return 1;
  760. }
  761. ret = io_uring_register_file_alloc_range(ring, 7, ~1U);
  762. if (ret != -EOVERFLOW) {
  763. fprintf(stderr, "overflow %i\n", ret);
  764. return 1;
  765. }
  766. return 0;
  767. }
  768. static int test_overallocating_file_range(struct io_uring *ring, int fds[2])
  769. {
  770. int roff = 7, rlen = 2;
  771. int ret, i, fd;
  772. ret = io_uring_register_file_alloc_range(ring, roff, rlen);
  773. if (ret) {
  774. fprintf(stderr, "io_uring_register_file_alloc_range %i\n", ret);
  775. return 1;
  776. }
  777. for (i = 0; i < rlen; i++) {
  778. fd = fds[0];
  779. ret = file_update_alloc(ring, &fd);
  780. if (ret != 1) {
  781. fprintf(stderr, "file_update_alloc\n");
  782. return 1;
  783. }
  784. if (fd < roff || fd >= roff + rlen) {
  785. fprintf(stderr, "invalid off result %i\n", fd);
  786. return 1;
  787. }
  788. }
  789. fd = fds[0];
  790. ret = file_update_alloc(ring, &fd);
  791. if (ret != -ENFILE) {
  792. fprintf(stderr, "overallocated %i, off %i\n", ret, fd);
  793. return 1;
  794. }
  795. return 0;
  796. }
  797. static int test_zero_range_alloc(struct io_uring *ring, int fds[2])
  798. {
  799. int ret, fd;
  800. ret = io_uring_register_file_alloc_range(ring, 7, 0);
  801. if (ret) {
  802. fprintf(stderr, "io_uring_register_file_alloc_range failed %i\n", ret);
  803. return 1;
  804. }
  805. fd = fds[0];
  806. ret = file_update_alloc(ring, &fd);
  807. if (ret != -ENFILE) {
  808. fprintf(stderr, "zero alloc %i\n", ret);
  809. return 1;
  810. }
  811. return 0;
  812. }
  813. static int test_defer_taskrun(void)
  814. {
  815. struct io_uring_sqe *sqe;
  816. struct io_uring ring;
  817. int ret, fds[2];
  818. char buff = 'x';
  819. ret = io_uring_queue_init(8, &ring,
  820. IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_SINGLE_ISSUER);
  821. if (ret) {
  822. fprintf(stderr, "ring init\n");
  823. return 1;
  824. }
  825. ret = pipe(fds);
  826. if (ret) {
  827. fprintf(stderr, "bad pipes\n");
  828. return 1;
  829. }
  830. ret = io_uring_register_files(&ring, &fds[0], 2);
  831. if (ret) {
  832. fprintf(stderr, "bad register %d\n", ret);
  833. return 1;
  834. }
  835. sqe = io_uring_get_sqe(&ring);
  836. io_uring_prep_read(sqe, 0, &buff, 1, 0);
  837. sqe->flags |= IOSQE_FIXED_FILE;
  838. ret = io_uring_submit(&ring);
  839. if (ret != 1) {
  840. fprintf(stderr, "bad submit\n");
  841. return 1;
  842. }
  843. ret = write(fds[1], &buff, 1);
  844. if (ret != 1) {
  845. fprintf(stderr, "bad pipe write\n");
  846. return 1;
  847. }
  848. ret = io_uring_unregister_files(&ring);
  849. if (ret) {
  850. fprintf(stderr, "bad unregister %d\n", ret);
  851. return 1;
  852. }
  853. close(fds[0]);
  854. close(fds[1]);
  855. io_uring_queue_exit(&ring);
  856. return 0;
  857. }
  858. static int test_file_alloc_ranges(void)
  859. {
  860. struct io_uring ring;
  861. int ret, pipe_fds[2];
  862. if (pipe(pipe_fds)) {
  863. fprintf(stderr, "pipes\n");
  864. return 1;
  865. }
  866. ret = io_uring_queue_init(8, &ring, 0);
  867. if (ret) {
  868. fprintf(stderr, "queue_init: %d\n", ret);
  869. return 1;
  870. }
  871. ret = io_uring_register_files_sparse(&ring, 10);
  872. if (ret == -EINVAL) {
  873. not_supported:
  874. close(pipe_fds[0]);
  875. close(pipe_fds[1]);
  876. io_uring_queue_exit(&ring);
  877. printf("file alloc ranges are not supported, skip\n");
  878. return 0;
  879. } else if (ret) {
  880. fprintf(stderr, "io_uring_register_files_sparse %i\n", ret);
  881. return ret;
  882. }
  883. ret = io_uring_register_file_alloc_range(&ring, 0, 1);
  884. if (ret) {
  885. if (ret == -EINVAL)
  886. goto not_supported;
  887. fprintf(stderr, "io_uring_register_file_alloc_range %i\n", ret);
  888. return 1;
  889. }
  890. ret = test_overallocating_file_range(&ring, pipe_fds);
  891. if (ret) {
  892. fprintf(stderr, "test_overallocating_file_range() failed\n");
  893. return 1;
  894. }
  895. ret = test_out_of_range_file_ranges(&ring);
  896. if (ret) {
  897. fprintf(stderr, "test_out_of_range_file_ranges() failed\n");
  898. return 1;
  899. }
  900. ret = test_zero_range_alloc(&ring, pipe_fds);
  901. if (ret) {
  902. fprintf(stderr, "test_zero_range_alloc() failed\n");
  903. return 1;
  904. }
  905. close(pipe_fds[0]);
  906. close(pipe_fds[1]);
  907. io_uring_queue_exit(&ring);
  908. return 0;
  909. }
  910. int main(int argc, char *argv[])
  911. {
  912. struct io_uring ring;
  913. int ret;
  914. if (argc > 1)
  915. return T_EXIT_SKIP;
  916. ret = io_uring_queue_init(8, &ring, 0);
  917. if (ret) {
  918. fprintf(stderr, "ring setup failed\n");
  919. return T_EXIT_FAIL;
  920. }
  921. ret = test_basic(&ring, 0);
  922. if (ret) {
  923. fprintf(stderr, "test_basic failed\n");
  924. return T_EXIT_FAIL;
  925. }
  926. ret = test_basic(&ring, 1);
  927. if (ret) {
  928. fprintf(stderr, "test_basic failed\n");
  929. return T_EXIT_FAIL;
  930. }
  931. ret = test_basic_many(&ring);
  932. if (ret) {
  933. fprintf(stderr, "test_basic_many failed\n");
  934. return T_EXIT_FAIL;
  935. }
  936. ret = test_sparse(&ring);
  937. if (ret) {
  938. fprintf(stderr, "test_sparse failed\n");
  939. return T_EXIT_FAIL;
  940. }
  941. if (no_update)
  942. return T_EXIT_SKIP;
  943. ret = test_additions(&ring);
  944. if (ret) {
  945. fprintf(stderr, "test_additions failed\n");
  946. return T_EXIT_FAIL;
  947. }
  948. ret = test_removals(&ring);
  949. if (ret) {
  950. fprintf(stderr, "test_removals failed\n");
  951. return T_EXIT_FAIL;
  952. }
  953. ret = test_replace(&ring);
  954. if (ret) {
  955. fprintf(stderr, "test_replace failed\n");
  956. return T_EXIT_FAIL;
  957. }
  958. ret = test_replace_all(&ring);
  959. if (ret) {
  960. fprintf(stderr, "test_replace_all failed\n");
  961. return T_EXIT_FAIL;
  962. }
  963. ret = test_grow(&ring);
  964. if (ret) {
  965. fprintf(stderr, "test_grow failed\n");
  966. return T_EXIT_FAIL;
  967. }
  968. ret = test_shrink(&ring);
  969. if (ret) {
  970. fprintf(stderr, "test_shrink failed\n");
  971. return T_EXIT_FAIL;
  972. }
  973. ret = test_zero(&ring);
  974. if (ret) {
  975. fprintf(stderr, "test_zero failed\n");
  976. return T_EXIT_FAIL;
  977. }
  978. ret = test_huge(&ring);
  979. if (ret) {
  980. fprintf(stderr, "test_huge failed\n");
  981. return T_EXIT_FAIL;
  982. }
  983. ret = test_skip(&ring);
  984. if (ret) {
  985. fprintf(stderr, "test_skip failed\n");
  986. return T_EXIT_FAIL;
  987. }
  988. ret = test_sparse_updates();
  989. if (ret) {
  990. fprintf(stderr, "test_sparse_updates failed\n");
  991. return T_EXIT_FAIL;
  992. }
  993. ret = test_fixed_removal_ordering();
  994. if (ret) {
  995. fprintf(stderr, "test_fixed_removal_ordering failed\n");
  996. return T_EXIT_FAIL;
  997. }
  998. ret = test_mixed_af_unix();
  999. if (ret) {
  1000. fprintf(stderr, "test_mixed_af_unix failed\n");
  1001. return T_EXIT_FAIL;
  1002. }
  1003. ret = test_partial_register_fail();
  1004. if (ret) {
  1005. fprintf(stderr, "test_partial_register_fail failed\n");
  1006. return T_EXIT_FAIL;
  1007. }
  1008. ret = test_file_alloc_ranges();
  1009. if (ret) {
  1010. fprintf(stderr, "test_partial_register_fail failed\n");
  1011. return T_EXIT_FAIL;
  1012. }
  1013. if (t_probe_defer_taskrun()) {
  1014. ret = test_defer_taskrun();
  1015. if (ret) {
  1016. fprintf(stderr, "test_defer_taskrun failed\n");
  1017. return T_EXIT_FAIL;
  1018. }
  1019. }
  1020. return T_EXIT_PASS;
  1021. }