t_renamerace.c revision 1.37 1 1.37 riastrad /* $NetBSD: t_renamerace.c,v 1.37 2020/09/05 02:45:22 riastradh Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Modified for rump and atf from a program supplied
5 1.1 pooka * by Nicolas Joly in kern/40948
6 1.1 pooka */
7 1.1 pooka
8 1.1 pooka #include <sys/types.h>
9 1.1 pooka #include <sys/mount.h>
10 1.1 pooka #include <sys/utsname.h>
11 1.1 pooka
12 1.1 pooka #include <atf-c.h>
13 1.1 pooka #include <errno.h>
14 1.1 pooka #include <fcntl.h>
15 1.1 pooka #include <pthread.h>
16 1.1 pooka #include <stdio.h>
17 1.1 pooka #include <stdlib.h>
18 1.1 pooka #include <string.h>
19 1.1 pooka #include <unistd.h>
20 1.1 pooka
21 1.1 pooka #include <rump/rump.h>
22 1.1 pooka #include <rump/rump_syscalls.h>
23 1.1 pooka
24 1.27 jmmv /* Bump the size of the test file system image to a larger value.
25 1.27 jmmv *
26 1.27 jmmv * These tests cause a lot of churn in the file system by creating and
27 1.27 jmmv * deleting files/directories in quick succession. A faster CPU will cause
28 1.27 jmmv * more churn because the tests are capped by a run time period in seconds,
29 1.27 jmmv * not number of operations.
30 1.27 jmmv *
31 1.27 jmmv * This is all fine except for LFS, because the lfs_cleanerd cannot keep up
32 1.27 jmmv * with the churn and thus causes the test to fail on fast machines. Hence
33 1.27 jmmv * the reason for this hack. */
34 1.27 jmmv #define FSTEST_IMGSIZE (50000 * 512)
35 1.27 jmmv
36 1.1 pooka #include "../common/h_fsmacros.h"
37 1.34 christos #include "h_macros.h"
38 1.1 pooka
39 1.1 pooka static volatile int quittingtime;
40 1.12 pooka pid_t wrkpid;
41 1.1 pooka
42 1.1 pooka static void *
43 1.1 pooka w1(void *arg)
44 1.1 pooka {
45 1.1 pooka int fd;
46 1.1 pooka
47 1.12 pooka rump_pub_lwproc_newlwp(wrkpid);
48 1.1 pooka
49 1.1 pooka while (!quittingtime) {
50 1.1 pooka fd = rump_sys_open("rename.test1",
51 1.1 pooka O_WRONLY|O_CREAT|O_TRUNC, 0666);
52 1.9 pooka if (fd == -1 && errno != EEXIST)
53 1.1 pooka atf_tc_fail_errno("create");
54 1.1 pooka rump_sys_unlink("rename.test1");
55 1.1 pooka rump_sys_close(fd);
56 1.1 pooka }
57 1.1 pooka
58 1.1 pooka return NULL;
59 1.1 pooka }
60 1.1 pooka
61 1.1 pooka static void *
62 1.4 pooka w1_dirs(void *arg)
63 1.4 pooka {
64 1.4 pooka
65 1.12 pooka rump_pub_lwproc_newlwp(wrkpid);
66 1.4 pooka
67 1.4 pooka while (!quittingtime) {
68 1.4 pooka if (rump_sys_mkdir("rename.test1", 0777) == -1)
69 1.4 pooka atf_tc_fail_errno("mkdir");
70 1.4 pooka rump_sys_rmdir("rename.test1");
71 1.4 pooka }
72 1.4 pooka
73 1.4 pooka return NULL;
74 1.4 pooka }
75 1.4 pooka
76 1.4 pooka static void *
77 1.1 pooka w2(void *arg)
78 1.1 pooka {
79 1.1 pooka
80 1.12 pooka rump_pub_lwproc_newlwp(wrkpid);
81 1.1 pooka
82 1.1 pooka while (!quittingtime) {
83 1.1 pooka rump_sys_rename("rename.test1", "rename.test2");
84 1.1 pooka }
85 1.1 pooka
86 1.1 pooka return NULL;
87 1.1 pooka }
88 1.1 pooka
89 1.37 riastrad static void
90 1.37 riastrad w3_mkdir(void)
91 1.37 riastrad {
92 1.37 riastrad
93 1.37 riastrad if (rump_sys_mkdir("c", 0777) == -1)
94 1.37 riastrad ATF_CHECK_MSG(errno == ENOENT || errno == EEXIST,
95 1.37 riastrad "mkdir: %s (errno=%d)\n", strerror(errno), errno);
96 1.37 riastrad if (rump_sys_mkdir("c/d", 0777) == -1)
97 1.37 riastrad ATF_CHECK_MSG(errno == ENOENT || errno == EEXIST,
98 1.37 riastrad "mkdir: %s (errno=%d)\n", strerror(errno), errno);
99 1.37 riastrad if (rump_sys_mkdir("c/d/e", 0777) == -1)
100 1.37 riastrad ATF_CHECK_MSG(errno == ENOENT || errno == EEXIST,
101 1.37 riastrad "mkdir: %s (errno=%d)\n", strerror(errno), errno);
102 1.37 riastrad }
103 1.37 riastrad
104 1.37 riastrad static void *
105 1.37 riastrad w3_rmdir(void *arg)
106 1.37 riastrad {
107 1.37 riastrad
108 1.37 riastrad rump_pub_lwproc_newlwp(wrkpid);
109 1.37 riastrad
110 1.37 riastrad while (!quittingtime) {
111 1.37 riastrad w3_mkdir();
112 1.37 riastrad rump_sys_rmdir("c/d/e");
113 1.37 riastrad rump_sys_rmdir("c/d");
114 1.37 riastrad }
115 1.37 riastrad
116 1.37 riastrad return NULL;
117 1.37 riastrad }
118 1.37 riastrad
119 1.37 riastrad static void *
120 1.37 riastrad w3_rename1(void *arg)
121 1.37 riastrad {
122 1.37 riastrad
123 1.37 riastrad rump_pub_lwproc_newlwp(wrkpid);
124 1.37 riastrad
125 1.37 riastrad while (!quittingtime) {
126 1.37 riastrad w3_mkdir();
127 1.37 riastrad rump_sys_rename("c", "c/d/e");
128 1.37 riastrad }
129 1.37 riastrad
130 1.37 riastrad return NULL;
131 1.37 riastrad }
132 1.37 riastrad
133 1.37 riastrad static void *
134 1.37 riastrad w3_rename2(void *arg)
135 1.37 riastrad {
136 1.37 riastrad
137 1.37 riastrad rump_pub_lwproc_newlwp(wrkpid);
138 1.37 riastrad
139 1.37 riastrad while (!quittingtime) {
140 1.37 riastrad w3_mkdir();
141 1.37 riastrad rump_sys_rename("c/d/e", "c");
142 1.37 riastrad }
143 1.37 riastrad
144 1.37 riastrad return NULL;
145 1.37 riastrad }
146 1.37 riastrad
147 1.9 pooka #define NWRK 8
148 1.1 pooka static void
149 1.1 pooka renamerace(const atf_tc_t *tc, const char *mp)
150 1.1 pooka {
151 1.9 pooka pthread_t pt1[NWRK], pt2[NWRK];
152 1.9 pooka int i;
153 1.1 pooka
154 1.30 hannken /*
155 1.30 hannken * Sysvbfs supports only 8 inodes so this test would exhaust
156 1.30 hannken * the inode table and creating files would fail with ENOSPC.
157 1.30 hannken */
158 1.30 hannken if (FSTYPE_SYSVBFS(tc))
159 1.30 hannken atf_tc_skip("filesystem has not enough inodes");
160 1.14 pooka if (FSTYPE_RUMPFS(tc))
161 1.24 njoly atf_tc_skip("rename not supported by file system");
162 1.32 gson if (FSTYPE_UDF(tc))
163 1.36 gson atf_tc_expect_fail("PR kern/53865");
164 1.14 pooka
165 1.15 pooka RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
166 1.13 pooka RL(wrkpid = rump_sys_getpid());
167 1.13 pooka
168 1.11 pooka RL(rump_sys_chdir(mp));
169 1.9 pooka for (i = 0; i < NWRK; i++)
170 1.11 pooka pthread_create(&pt1[i], NULL, w1, NULL);
171 1.9 pooka
172 1.9 pooka for (i = 0; i < NWRK; i++)
173 1.11 pooka pthread_create(&pt2[i], NULL, w2, NULL);
174 1.1 pooka
175 1.1 pooka sleep(5);
176 1.1 pooka quittingtime = 1;
177 1.1 pooka
178 1.9 pooka for (i = 0; i < NWRK; i++)
179 1.9 pooka pthread_join(pt1[i], NULL);
180 1.9 pooka for (i = 0; i < NWRK; i++)
181 1.9 pooka pthread_join(pt2[i], NULL);
182 1.11 pooka RL(rump_sys_chdir("/"));
183 1.3 pooka
184 1.32 gson if (FSTYPE_UDF(tc))
185 1.32 gson atf_tc_fail("race did not trigger this time");
186 1.32 gson
187 1.22 pooka if (FSTYPE_MSDOS(tc)) {
188 1.33 dholland atf_tc_expect_fail("PR kern/43626");
189 1.22 pooka /*
190 1.22 pooka * XXX: race does not trigger every time at least
191 1.22 pooka * on amd64/qemu.
192 1.22 pooka */
193 1.22 pooka if (msdosfs_fstest_unmount(tc, mp, 0) != 0) {
194 1.22 pooka rump_pub_vfs_mount_print(mp, 1);
195 1.22 pooka atf_tc_fail_errno("unmount failed");
196 1.22 pooka }
197 1.22 pooka atf_tc_fail("race did not trigger this time");
198 1.22 pooka }
199 1.1 pooka }
200 1.1 pooka
201 1.4 pooka static void
202 1.4 pooka renamerace_dirs(const atf_tc_t *tc, const char *mp)
203 1.4 pooka {
204 1.4 pooka pthread_t pt1, pt2;
205 1.4 pooka
206 1.7 pooka if (FSTYPE_SYSVBFS(tc))
207 1.24 njoly atf_tc_skip("directories not supported by file system");
208 1.14 pooka if (FSTYPE_RUMPFS(tc))
209 1.24 njoly atf_tc_skip("rename not supported by file system");
210 1.35 gson if (FSTYPE_UDF(tc))
211 1.35 gson atf_tc_expect_fail("PR kern/53865");
212 1.14 pooka
213 1.4 pooka /* XXX: msdosfs also sometimes hangs */
214 1.26 riastrad if (FSTYPE_MSDOS(tc))
215 1.5 pooka atf_tc_expect_signal(-1, "PR kern/43626");
216 1.4 pooka
217 1.15 pooka RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
218 1.12 pooka RL(wrkpid = rump_sys_getpid());
219 1.12 pooka
220 1.11 pooka RL(rump_sys_chdir(mp));
221 1.11 pooka pthread_create(&pt1, NULL, w1_dirs, NULL);
222 1.11 pooka pthread_create(&pt2, NULL, w2, NULL);
223 1.4 pooka
224 1.4 pooka sleep(5);
225 1.4 pooka quittingtime = 1;
226 1.4 pooka
227 1.4 pooka pthread_join(pt1, NULL);
228 1.4 pooka pthread_join(pt2, NULL);
229 1.11 pooka RL(rump_sys_chdir("/"));
230 1.4 pooka
231 1.35 gson if (FSTYPE_UDF(tc))
232 1.35 gson atf_tc_fail("race did not trigger this time");
233 1.35 gson
234 1.4 pooka /*
235 1.4 pooka * Doesn't always trigger when run on a slow backend
236 1.4 pooka * (i.e. not on tmpfs/mfs). So do the usual kludge.
237 1.4 pooka */
238 1.26 riastrad if (FSTYPE_MSDOS(tc))
239 1.4 pooka abort();
240 1.4 pooka }
241 1.4 pooka
242 1.37 riastrad static void
243 1.37 riastrad renamerace_cycle(const atf_tc_t *tc, const char *mp)
244 1.37 riastrad {
245 1.37 riastrad pthread_t pt_rmdir, pt_rename1, pt_rename2;
246 1.37 riastrad
247 1.37 riastrad if (FSTYPE_SYSVBFS(tc))
248 1.37 riastrad atf_tc_skip("directories not supported by file system");
249 1.37 riastrad if (FSTYPE_RUMPFS(tc))
250 1.37 riastrad atf_tc_skip("rename not supported by file system");
251 1.37 riastrad if (FSTYPE_P2K_FFS(tc))
252 1.37 riastrad atf_tc_expect_fail("assertion \"vp->v_size == ip->i_size\" failed");
253 1.37 riastrad if (FSTYPE_PUFFS(tc))
254 1.37 riastrad atf_tc_expect_fail("assertion \"dfd\" failed");
255 1.37 riastrad if (FSTYPE_NFS(tc))
256 1.37 riastrad atf_tc_expect_fail("mkdir fails with ESTALE");
257 1.37 riastrad
258 1.37 riastrad /* XXX: msdosfs also sometimes hangs */
259 1.37 riastrad if (FSTYPE_MSDOS(tc))
260 1.37 riastrad atf_tc_expect_signal(-1, "PR kern/43626");
261 1.37 riastrad
262 1.37 riastrad RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
263 1.37 riastrad RL(wrkpid = rump_sys_getpid());
264 1.37 riastrad
265 1.37 riastrad RL(rump_sys_chdir(mp));
266 1.37 riastrad pthread_create(&pt_rmdir, NULL, w3_rmdir, NULL);
267 1.37 riastrad pthread_create(&pt_rename1, NULL, w3_rename1, NULL);
268 1.37 riastrad pthread_create(&pt_rename2, NULL, w3_rename2, NULL);
269 1.37 riastrad
270 1.37 riastrad sleep(10);
271 1.37 riastrad quittingtime = 1;
272 1.37 riastrad
273 1.37 riastrad if (FSTYPE_EXT2FS(tc) ||
274 1.37 riastrad FSTYPE_FFS(tc) ||
275 1.37 riastrad FSTYPE_FFSLOG(tc) ||
276 1.37 riastrad FSTYPE_LFS(tc) ||
277 1.37 riastrad FSTYPE_TMPFS(tc) ||
278 1.37 riastrad FSTYPE_UDF(tc))
279 1.37 riastrad atf_tc_expect_signal(SIGALRM, "genfs_rename is busted");
280 1.37 riastrad
281 1.37 riastrad alarm(1);
282 1.37 riastrad pthread_join(pt_rmdir, NULL);
283 1.37 riastrad pthread_join(pt_rename1, NULL);
284 1.37 riastrad pthread_join(pt_rename2, NULL);
285 1.37 riastrad alarm(0);
286 1.37 riastrad RL(rump_sys_chdir("/"));
287 1.37 riastrad
288 1.37 riastrad /*
289 1.37 riastrad * Doesn't always trigger when run on a slow backend
290 1.37 riastrad * (i.e. not on tmpfs/mfs). So do the usual kludge.
291 1.37 riastrad */
292 1.37 riastrad if (FSTYPE_MSDOS(tc))
293 1.37 riastrad abort();
294 1.37 riastrad }
295 1.37 riastrad
296 1.1 pooka ATF_TC_FSAPPLY(renamerace, "rename(2) race with file unlinked mid-operation");
297 1.4 pooka ATF_TC_FSAPPLY(renamerace_dirs, "rename(2) race with directories");
298 1.37 riastrad ATF_TC_FSAPPLY(renamerace_cycle, "rename(2) race making directory cycles");
299 1.1 pooka
300 1.1 pooka ATF_TP_ADD_TCS(tp)
301 1.1 pooka {
302 1.1 pooka
303 1.1 pooka ATF_TP_FSAPPLY(renamerace); /* PR kern/41128 */
304 1.4 pooka ATF_TP_FSAPPLY(renamerace_dirs);
305 1.37 riastrad ATF_TP_FSAPPLY(renamerace_cycle);
306 1.1 pooka
307 1.1 pooka return atf_no_error();
308 1.1 pooka }
309