blob: 316354063139f226290c8eaecef2aa08441bf351 [file] [log] [blame]
Jens Axboee5024352020-02-11 20:34:12 -07001/* SPDX-License-Identifier: MIT */
Jens Axboe41d442b2019-12-14 22:44:40 -07002/*
William Dauchy7e3188b2020-01-04 16:23:14 +01003 * Description: run various statx(2) tests
Jens Axboe41d442b2019-12-14 22:44:40 -07004 *
5 */
6#include <errno.h>
7#include <stdio.h>
8#include <unistd.h>
9#include <stdlib.h>
10#include <string.h>
11#include <fcntl.h>
12#include <sys/types.h>
Jens Axboe41d442b2019-12-14 22:44:40 -070013#include <sys/syscall.h>
14#include <linux/stat.h>
15
16#include "liburing.h"
17
18#if defined(__x86_64)
19static int do_statx(int dfd, const char *path, int flags, unsigned mask,
20 struct statx *statxbuf)
21{
22 return syscall(332, dfd, path, flags, mask, statxbuf);
23}
24#endif
25
26static int create_file(const char *file, size_t size)
27{
28 ssize_t ret;
29 char *buf;
30 int fd;
31
32 buf = malloc(size);
33 memset(buf, 0xaa, size);
34
35 fd = open(file, O_WRONLY | O_CREAT, 0644);
36 if (fd < 0) {
37 perror("open file");
38 return 1;
39 }
40 ret = write(fd, buf, size);
41 close(fd);
42 return ret != size;
43}
44
45static int test_statx(struct io_uring *ring, const char *path)
46{
47 struct io_uring_cqe *cqe;
48 struct io_uring_sqe *sqe;
49 struct statx x1;
50#if defined(__x86_64)
51 struct statx x2;
52#endif
53 int ret;
54
55 sqe = io_uring_get_sqe(ring);
56 if (!sqe) {
57 fprintf(stderr, "get sqe failed\n");
58 goto err;
59 }
60 io_uring_prep_statx(sqe, -1, path, 0, STATX_ALL, &x1);
61
62 ret = io_uring_submit(ring);
63 if (ret <= 0) {
64 fprintf(stderr, "sqe submit failed: %d\n", ret);
65 goto err;
66 }
67
68 ret = io_uring_wait_cqe(ring, &cqe);
69 if (ret < 0) {
70 fprintf(stderr, "wait completion %d\n", ret);
71 goto err;
72 }
73 ret = cqe->res;
74 io_uring_cqe_seen(ring, cqe);
75 if (ret)
76 return ret;
77#if defined(__x86_64)
78 ret = do_statx(-1, path, 0, STATX_ALL, &x2);
79 if (ret < 0)
80 return -1;
81 if (memcmp(&x1, &x2, sizeof(x1))) {
82 fprintf(stderr, "Miscompare between io_uring and statx\n");
83 goto err;
84 }
85#endif
86 return 0;
87err:
88 return -1;
89}
90
91int main(int argc, char *argv[])
92{
93 struct io_uring ring;
94 const char *fname;
95 int ret;
96
97 ret = io_uring_queue_init(8, &ring, 0);
98 if (ret) {
99 fprintf(stderr, "ring setup failed\n");
100 return 1;
101 }
102
103 if (argc > 1) {
104 fname = argv[1];
105 } else {
106 fname = "/tmp/.statx";
107 if (create_file(fname, 4096)) {
108 fprintf(stderr, "file create failed\n");
109 return 1;
110 }
111 }
112
113 ret = test_statx(&ring, fname);
114 if (ret) {
115 if (ret == -EINVAL) {
116 fprintf(stdout, "statx not supported, skipping\n");
117 goto done;
118 }
119 fprintf(stderr, "test_statx failed: %d\n", ret);
120 goto err;
121 }
122done:
123 if (fname != argv[1])
124 unlink(fname);
125 return 0;
126err:
127 if (fname != argv[1])
128 unlink(fname);
129 return 1;
130}