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