1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2004 Daniel McNeil <[email protected]>
4 * 2004 Open Source Development Lab
5 * 2004 Marty Ridgeway <[email protected]>
6 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Append zeroed data to a file using libaio while other processes are doing
13 * buffered reads and check if the buffer reads always see zero.
14 */
15
16 #define _GNU_SOURCE
17
18 #include "tst_test.h"
19
20 #ifdef HAVE_LIBAIO
21 #include <stdlib.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24 #include <libaio.h>
25 #include "common.h"
26
27 static volatile int *run_child;
28
29 static char *str_numchildren;
30 static char *str_writesize;
31 static char *str_numaio;
32 static char *str_appends;
33
34 static int numchildren = 8;
35 static long long writesize = 64 * 1024;
36 static int numaio = 16;
37 static int appends = 1000;
38 static long long alignment;
39
40 /*
41 * append to the end of a file using AIO DIRECT.
42 */
aiodio_append(char * filename,int bcount,long long align,long long ws,int naio)43 static void aiodio_append(char *filename, int bcount, long long align, long long ws, int naio)
44 {
45 int fd;
46 void *bufptr;
47 int i;
48 int w;
49 struct iocb iocb_array[naio];
50 struct iocb *iocbs[naio];
51 off_t offset = 0;
52 io_context_t myctx;
53 struct io_event event;
54
55 fd = SAFE_OPEN(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
56
57 /*
58 * Prepare AIO write context.
59 */
60 memset(&myctx, 0, sizeof(myctx));
61 w = io_queue_init(naio, &myctx);
62 if (w < 0)
63 tst_brk(TBROK, "io_queue_init: %s", tst_strerrno(-w));
64
65 for (i = 0; i < naio; i++) {
66 bufptr = SAFE_MEMALIGN(align, ws);
67 memset(bufptr, 0, ws);
68 io_prep_pwrite(&iocb_array[i], fd, bufptr, ws, offset);
69 iocbs[i] = &iocb_array[i];
70 offset += ws;
71 }
72
73 /*
74 * Start the 1st AIO requests.
75 */
76 w = io_submit(myctx, naio, iocbs);
77 if (w < 0) {
78 io_destroy(myctx);
79 tst_brk(TBROK, "io_submit (multiple): %s", tst_strerrno(-w));
80 }
81
82 /*
83 * As AIO requests finish, keep issuing more AIOs.
84 */
85 for (; i < bcount; i++) {
86 int n = 0;
87 struct iocb *iocbp;
88
89 n = io_getevents(myctx, 1, 1, &event, NULL);
90 if (n > 0) {
91 iocbp = (struct iocb *)event.obj;
92 io_prep_pwrite(iocbp, fd, iocbp->u.c.buf, ws, offset);
93 offset += ws;
94 w = io_submit(myctx, 1, &iocbp);
95 if (w < 0) {
96 io_destroy(myctx);
97 tst_brk(TBROK, "io_submit (single): %s", tst_strerrno(-w));
98 }
99 }
100 }
101 }
102
setup(void)103 static void setup(void)
104 {
105 struct stat sb;
106 int maxaio;
107
108 if (tst_parse_int(str_numaio, &numaio, 1, INT_MAX))
109 tst_brk(TBROK, "Number of async IO blocks '%s'", str_numaio);
110
111 SAFE_FILE_SCANF("/proc/sys/fs/aio-max-nr", "%d", &maxaio);
112 tst_res(TINFO, "Maximum AIO blocks: %d", maxaio);
113
114 if (numaio > maxaio)
115 tst_res(TCONF, "Number of async IO blocks passed the maximum (%d)", maxaio);
116
117 if (tst_parse_int(str_numchildren, &numchildren, 1, INT_MAX))
118 tst_brk(TBROK, "Invalid number of children '%s'", str_numchildren);
119
120 if (tst_parse_filesize(str_writesize, &writesize, 1, LLONG_MAX))
121 tst_brk(TBROK, "Size of the file to write '%s'", str_writesize);
122
123 if (tst_parse_int(str_appends, &appends, 1, INT_MAX))
124 tst_brk(TBROK, "Invalid number of appends '%s'", str_appends);
125
126 SAFE_STAT(".", &sb);
127 alignment = sb.st_blksize;
128
129 run_child = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
130 }
131
cleanup(void)132 static void cleanup(void)
133 {
134 if (run_child) {
135 *run_child = 0;
136 SAFE_MUNMAP((void *)run_child, sizeof(int));
137 }
138 }
139
run(void)140 static void run(void)
141 {
142 char *filename = "aiodio_append";
143 int status;
144 int i, pid;
145
146 *run_child = 1;
147
148 for (i = 0; i < numchildren; i++) {
149 if (!SAFE_FORK()) {
150 io_read_eof(filename, run_child);
151 return;
152 }
153 }
154
155 pid = SAFE_FORK();
156 if (!pid) {
157 aiodio_append(filename, appends, alignment, writesize, numaio);
158 return;
159 }
160
161 tst_res(TINFO, "Child %i appends to a file", pid);
162
163 for (;;) {
164 if (SAFE_WAITPID(pid, NULL, WNOHANG))
165 break;
166
167 sleep(1);
168
169 if (!tst_remaining_runtime()) {
170 tst_res(TINFO, "Test out of runtime, exiting");
171 kill(pid, SIGKILL);
172 SAFE_WAITPID(pid, NULL, 0);
173 break;
174 }
175 }
176
177 if (SAFE_WAITPID(-1, &status, WNOHANG))
178 tst_res(TFAIL, "Non zero bytes read");
179 else
180 tst_res(TPASS, "All bytes read were zeroed");
181
182 *run_child = 0;
183
184 SAFE_UNLINK(filename);
185 }
186
187 static struct tst_test test = {
188 .test_all = run,
189 .setup = setup,
190 .cleanup = cleanup,
191 .needs_tmpdir = 1,
192 .forks_child = 1,
193 .max_runtime = 1800,
194 .options = (struct tst_option[]) {
195 {"n:", &str_numchildren, "Number of threads (default 16)"},
196 {"s:", &str_writesize, "Size of the file to write (default 64K)"},
197 {"c:", &str_appends, "Number of appends (default 1000)"},
198 {"b:", &str_numaio, "Number of async IO blocks (default 16)"},
199 {}
200 },
201 .skip_filesystems = (const char *[]) {
202 "tmpfs",
203 NULL
204 },
205 };
206 #else
207 TST_TEST_TCONF("test requires libaio and its development packages");
208 #endif
209