xattr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "../config-host.h"
  2. #include <assert.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/xattr.h>
  8. #include <unistd.h>
  9. #include "helpers.h"
  10. #include "liburing.h"
  11. static int no_xattr;
  12. /* Define constants. */
  13. #define XATTR_SIZE 255
  14. #define QUEUE_DEPTH 32
  15. #define FILENAME "xattr.test"
  16. #define KEY1 "user.val1"
  17. #define KEY2 "user.val2"
  18. #define VALUE1 "value1"
  19. #define VALUE2 "value2-a-lot-longer"
  20. /* Call fsetxattr. */
  21. static int io_uring_fsetxattr(struct io_uring *ring, int fd, const char *name,
  22. const void *value, size_t size, int flags)
  23. {
  24. struct io_uring_sqe *sqe;
  25. struct io_uring_cqe *cqe;
  26. int ret;
  27. sqe = io_uring_get_sqe(ring);
  28. if (!sqe) {
  29. fprintf(stderr, "Error cannot get sqe\n");
  30. return -1;
  31. }
  32. io_uring_prep_fsetxattr(sqe, fd, name, value, flags, size);
  33. ret = io_uring_submit(ring);
  34. if (ret != 1) {
  35. fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
  36. return -1;
  37. }
  38. ret = io_uring_wait_cqe(ring, &cqe);
  39. if (ret) {
  40. fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
  41. return -1;
  42. }
  43. ret = cqe->res;
  44. if (ret < 0) {
  45. if (cqe->res == -EINVAL || cqe->res == -EOPNOTSUPP)
  46. no_xattr = 1;
  47. }
  48. io_uring_cqe_seen(ring, cqe);
  49. return ret;
  50. }
  51. /* Submit fgetxattr request. */
  52. static int io_uring_fgetxattr(struct io_uring *ring, int fd, const char *name,
  53. void *value, size_t size)
  54. {
  55. struct io_uring_sqe *sqe;
  56. struct io_uring_cqe *cqe;
  57. int ret;
  58. sqe = io_uring_get_sqe(ring);
  59. if (!sqe) {
  60. fprintf(stderr, "Error cannot get sqe\n");
  61. return -1;
  62. }
  63. io_uring_prep_fgetxattr(sqe, fd, name, value, size);
  64. ret = io_uring_submit(ring);
  65. if (ret != 1) {
  66. fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
  67. return -1;
  68. }
  69. ret = io_uring_wait_cqe(ring, &cqe);
  70. if (ret) {
  71. fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
  72. return -1;
  73. }
  74. ret = cqe->res;
  75. if (ret == -1) {
  76. fprintf(stderr, "Error couldn'tget value\n");
  77. return -1;
  78. }
  79. io_uring_cqe_seen(ring, cqe);
  80. return ret;
  81. }
  82. /* Call setxattr. */
  83. static int io_uring_setxattr(struct io_uring *ring, const char *path,
  84. const char *name, const void *value, size_t size,
  85. int flags)
  86. {
  87. struct io_uring_sqe *sqe;
  88. struct io_uring_cqe *cqe;
  89. int ret;
  90. sqe = io_uring_get_sqe(ring);
  91. if (!sqe) {
  92. fprintf(stderr, "Error cannot get sqe\n");
  93. return -1;
  94. }
  95. io_uring_prep_setxattr(sqe, name, value, path, flags, size);
  96. ret = io_uring_submit_and_wait(ring, 1);
  97. if (ret != 1) {
  98. fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
  99. return -1;
  100. }
  101. ret = io_uring_wait_cqe(ring, &cqe);
  102. if (ret) {
  103. fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
  104. return -1;
  105. }
  106. ret = cqe->res;
  107. if (ret < 0) {
  108. if (ret == -EINVAL || ret == -EOPNOTSUPP)
  109. no_xattr = 1;
  110. }
  111. io_uring_cqe_seen(ring, cqe);
  112. return ret;
  113. }
  114. /* Submit getxattr request. */
  115. static int io_uring_getxattr(struct io_uring *ring, const char *path,
  116. const char *name, void *value, size_t size)
  117. {
  118. struct io_uring_sqe *sqe;
  119. struct io_uring_cqe *cqe;
  120. int ret;
  121. sqe = io_uring_get_sqe(ring);
  122. if (!sqe) {
  123. fprintf(stderr, "Error cannot get sqe\n");
  124. return -1;
  125. }
  126. io_uring_prep_getxattr(sqe, name, value, path, size);
  127. ret = io_uring_submit(ring);
  128. if (ret != 1) {
  129. fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
  130. return -1;
  131. }
  132. ret = io_uring_wait_cqe(ring, &cqe);
  133. if (ret) {
  134. fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
  135. return -1;
  136. }
  137. ret = cqe->res;
  138. if (ret == -1) {
  139. fprintf(stderr, "Error couldn'tget value\n");
  140. return -1;
  141. }
  142. io_uring_cqe_seen(ring, cqe);
  143. return ret;
  144. }
  145. /* Test driver for fsetxattr and fgetxattr. */
  146. static int test_fxattr(void)
  147. {
  148. int rc = 0;
  149. size_t value_len;
  150. struct io_uring ring;
  151. char value[XATTR_SIZE];
  152. /* Init io-uring queue. */
  153. int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
  154. if (ret) {
  155. fprintf(stderr, "child: ring setup failed: %d\n", ret);
  156. return -1;
  157. }
  158. /* Create the test file. */
  159. int fd = open(FILENAME, O_CREAT | O_RDWR, 0644);
  160. if (fd < 0) {
  161. fprintf(stderr, "Error: cannot open file: ret=%d\n", fd);
  162. return -1;
  163. }
  164. /* Test writing attributes. */
  165. if (io_uring_fsetxattr(&ring, fd, KEY1, VALUE1, strlen(VALUE1), 0) < 0) {
  166. if (no_xattr) {
  167. fprintf(stdout, "No xattr support, skipping\n");
  168. goto Exit;
  169. }
  170. fprintf(stderr, "Error fsetxattr cannot write key1\n");
  171. rc = -1;
  172. goto Exit;
  173. }
  174. if (io_uring_fsetxattr(&ring, fd, KEY2, VALUE2, strlen(VALUE2), 0) < 0) {
  175. fprintf(stderr, "Error fsetxattr cannot write key1\n");
  176. rc = -1;
  177. goto Exit;
  178. }
  179. /* Test reading attributes. */
  180. value_len = io_uring_fgetxattr(&ring, fd, KEY1, value, XATTR_SIZE);
  181. if (value_len != strlen(VALUE1) || strncmp(value, VALUE1, value_len)) {
  182. fprintf(stderr, "Error: fgetxattr expected value: %s, returned value: %s\n", VALUE1, value);
  183. rc = -1;
  184. goto Exit;
  185. }
  186. value_len = io_uring_fgetxattr(&ring, fd, KEY2, value, XATTR_SIZE);
  187. if (value_len != strlen(VALUE2) || strncmp(value, VALUE2, value_len)) {
  188. fprintf(stderr, "Error: fgetxattr expected value: %s, returned value: %s\n", VALUE2, value);
  189. rc = -1;
  190. goto Exit;
  191. }
  192. /* Cleanup. */
  193. Exit:
  194. close(fd);
  195. unlink(FILENAME);
  196. io_uring_queue_exit(&ring);
  197. return rc;
  198. }
  199. /* Test driver for setxattr and getxattr. */
  200. static int test_xattr(void)
  201. {
  202. int rc = 0;
  203. int value_len;
  204. struct io_uring ring;
  205. char value[XATTR_SIZE];
  206. /* Init io-uring queue. */
  207. int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
  208. if (ret) {
  209. fprintf(stderr, "child: ring setup failed: %d\n", ret);
  210. return -1;
  211. }
  212. /* Create the test file. */
  213. t_create_file(FILENAME, 0);
  214. /* Test writing attributes. */
  215. if (io_uring_setxattr(&ring, FILENAME, KEY1, VALUE1, strlen(VALUE1), 0) < 0) {
  216. fprintf(stderr, "Error setxattr cannot write key1\n");
  217. rc = -1;
  218. goto Exit;
  219. }
  220. if (io_uring_setxattr(&ring, FILENAME, KEY2, VALUE2, strlen(VALUE2), 0) < 0) {
  221. fprintf(stderr, "Error setxattr cannot write key1\n");
  222. rc = -1;
  223. goto Exit;
  224. }
  225. /* Test reading attributes. */
  226. value_len = io_uring_getxattr(&ring, FILENAME, KEY1, value, XATTR_SIZE);
  227. if (value_len != strlen(VALUE1) || strncmp(value, VALUE1, value_len)) {
  228. fprintf(stderr, "Error: getxattr expected value: %s, returned value: %s\n", VALUE1, value);
  229. rc = -1;
  230. goto Exit;
  231. }
  232. value_len = io_uring_getxattr(&ring, FILENAME, KEY2, value, XATTR_SIZE);
  233. if (value_len != strlen(VALUE2) || strncmp(value, VALUE2, value_len)) {
  234. fprintf(stderr, "Error: getxattr expected value: %s, returned value: %s\n", VALUE2, value);
  235. rc = -1;
  236. goto Exit;
  237. }
  238. /* Cleanup. */
  239. Exit:
  240. io_uring_queue_exit(&ring);
  241. unlink(FILENAME);
  242. return rc;
  243. }
  244. /* Test driver for failure cases of fsetxattr and fgetxattr. */
  245. static int test_failure_fxattr(void)
  246. {
  247. int rc = 0;
  248. struct io_uring ring;
  249. char value[XATTR_SIZE];
  250. /* Init io-uring queue. */
  251. int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
  252. if (ret) {
  253. fprintf(stderr, "child: ring setup failed: %d\n", ret);
  254. return -1;
  255. }
  256. /* Create the test file. */
  257. int fd = open(FILENAME, O_CREAT | O_RDWR, 0644);
  258. if (fd < 0) {
  259. fprintf(stderr, "Error: cannot open file: ret=%d\n", fd);
  260. return -1;
  261. }
  262. /* Test writing attributes. */
  263. assert(io_uring_fsetxattr(&ring, -1, KEY1, VALUE1, strlen(VALUE1), 0) < 0);
  264. assert(io_uring_fsetxattr(&ring, fd, NULL, VALUE1, strlen(VALUE1), 0) < 0);
  265. assert(io_uring_fsetxattr(&ring, fd, KEY1, NULL, strlen(VALUE1), 0) < 0);
  266. assert(io_uring_fsetxattr(&ring, fd, KEY1, VALUE1, 0, 0) == 0);
  267. assert(io_uring_fsetxattr(&ring, fd, KEY1, VALUE1, -1, 0) < 0);
  268. /* Test reading attributes. */
  269. assert(io_uring_fgetxattr(&ring, -1, KEY1, value, XATTR_SIZE) < 0);
  270. assert(io_uring_fgetxattr(&ring, fd, NULL, value, XATTR_SIZE) < 0);
  271. assert(io_uring_fgetxattr(&ring, fd, KEY1, value, 0) == 0);
  272. /* Cleanup. */
  273. close(fd);
  274. unlink(FILENAME);
  275. io_uring_queue_exit(&ring);
  276. return rc;
  277. }
  278. /* Test driver for failure cases for setxattr and getxattr. */
  279. static int test_failure_xattr(void)
  280. {
  281. int rc = 0;
  282. struct io_uring ring;
  283. char value[XATTR_SIZE];
  284. /* Init io-uring queue. */
  285. int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
  286. if (ret) {
  287. fprintf(stderr, "child: ring setup failed: %d\n", ret);
  288. return -1;
  289. }
  290. /* Create the test file. */
  291. t_create_file(FILENAME, 0);
  292. /* Test writing attributes. */
  293. assert(io_uring_setxattr(&ring, "complete garbage", KEY1, VALUE1, strlen(VALUE1), 0) < 0);
  294. assert(io_uring_setxattr(&ring, NULL, KEY1, VALUE1, strlen(VALUE1), 0) < 0);
  295. assert(io_uring_setxattr(&ring, FILENAME, NULL, VALUE1, strlen(VALUE1), 0) < 0);
  296. assert(io_uring_setxattr(&ring, FILENAME, KEY1, NULL, strlen(VALUE1), 0) < 0);
  297. assert(io_uring_setxattr(&ring, FILENAME, KEY1, VALUE1, 0, 0) == 0);
  298. /* Test reading attributes. */
  299. assert(io_uring_getxattr(&ring, "complete garbage", KEY1, value, XATTR_SIZE) < 0);
  300. assert(io_uring_getxattr(&ring, NULL, KEY1, value, XATTR_SIZE) < 0);
  301. assert(io_uring_getxattr(&ring, FILENAME, NULL, value, XATTR_SIZE) < 0);
  302. assert(io_uring_getxattr(&ring, FILENAME, KEY1, NULL, XATTR_SIZE) == 0);
  303. assert(io_uring_getxattr(&ring, FILENAME, KEY1, value, 0) == 0);
  304. /* Cleanup. */
  305. io_uring_queue_exit(&ring);
  306. unlink(FILENAME);
  307. return rc;
  308. }
  309. /* Test for invalid SQE, this will cause a segmentation fault if enabled. */
  310. static int test_invalid_sqe(void)
  311. {
  312. #ifdef DESTRUCTIVE_TEST
  313. struct io_uring_sqe *sqe = NULL;
  314. struct io_uring_cqe *cqe = NULL;
  315. struct io_uring ring;
  316. /* Init io-uring queue. */
  317. int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
  318. if (ret) {
  319. fprintf(stderr, "child: ring setup failed: %d\n", ret);
  320. return -1;
  321. }
  322. /* Pass invalid SQE. */
  323. io_uring_prep_setxattr(sqe, FILENAME, KEY1, VALUE1, strlen(VALUE1), 0);
  324. ret = io_uring_submit(&ring);
  325. if (ret != 1) {
  326. fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
  327. return -1;
  328. }
  329. ret = io_uring_wait_cqe(&ring, &cqe);
  330. if (ret) {
  331. fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
  332. return -1;
  333. }
  334. ret = cqe->res;
  335. io_uring_cqe_seen(&ring, cqe);
  336. return ret;
  337. #else
  338. return 0;
  339. #endif
  340. }
  341. /* Test driver. */
  342. int main(int argc, char *argv[])
  343. {
  344. if (argc > 1)
  345. return 0;
  346. if (test_fxattr())
  347. return EXIT_FAILURE;
  348. if (no_xattr)
  349. return EXIT_SUCCESS;
  350. if (test_xattr() || test_failure_fxattr() || test_failure_xattr() ||
  351. test_invalid_sqe())
  352. return EXIT_FAILURE;
  353. return EXIT_SUCCESS;
  354. }