1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_renamerace.c,v 1.32 2014/07/29 09:15:48 gson Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc * Modified for rump and atf from a program supplied
511be35a1SLionel Sambuc * by Nicolas Joly in kern/40948
611be35a1SLionel Sambuc */
711be35a1SLionel Sambuc
811be35a1SLionel Sambuc #include <sys/types.h>
911be35a1SLionel Sambuc #include <sys/mount.h>
1011be35a1SLionel Sambuc #include <sys/utsname.h>
1111be35a1SLionel Sambuc
1211be35a1SLionel Sambuc #include <atf-c.h>
1311be35a1SLionel Sambuc #include <errno.h>
1411be35a1SLionel Sambuc #include <fcntl.h>
1511be35a1SLionel Sambuc #include <pthread.h>
1611be35a1SLionel Sambuc #include <stdio.h>
1711be35a1SLionel Sambuc #include <stdlib.h>
1811be35a1SLionel Sambuc #include <string.h>
1911be35a1SLionel Sambuc #include <unistd.h>
2011be35a1SLionel Sambuc
2111be35a1SLionel Sambuc #include <rump/rump.h>
2211be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
2311be35a1SLionel Sambuc
2411be35a1SLionel Sambuc /* Bump the size of the test file system image to a larger value.
2511be35a1SLionel Sambuc *
2611be35a1SLionel Sambuc * These tests cause a lot of churn in the file system by creating and
2711be35a1SLionel Sambuc * deleting files/directories in quick succession. A faster CPU will cause
2811be35a1SLionel Sambuc * more churn because the tests are capped by a run time period in seconds,
2911be35a1SLionel Sambuc * not number of operations.
3011be35a1SLionel Sambuc *
3111be35a1SLionel Sambuc * This is all fine except for LFS, because the lfs_cleanerd cannot keep up
3211be35a1SLionel Sambuc * with the churn and thus causes the test to fail on fast machines. Hence
3311be35a1SLionel Sambuc * the reason for this hack. */
3411be35a1SLionel Sambuc #define FSTEST_IMGSIZE (50000 * 512)
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include "../common/h_fsmacros.h"
3711be35a1SLionel Sambuc #include "../../h_macros.h"
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc static volatile int quittingtime;
4011be35a1SLionel Sambuc pid_t wrkpid;
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc static void *
w1(void * arg)4311be35a1SLionel Sambuc w1(void *arg)
4411be35a1SLionel Sambuc {
4511be35a1SLionel Sambuc int fd;
4611be35a1SLionel Sambuc
4711be35a1SLionel Sambuc rump_pub_lwproc_newlwp(wrkpid);
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc while (!quittingtime) {
5011be35a1SLionel Sambuc fd = rump_sys_open("rename.test1",
5111be35a1SLionel Sambuc O_WRONLY|O_CREAT|O_TRUNC, 0666);
5211be35a1SLionel Sambuc if (fd == -1 && errno != EEXIST)
5311be35a1SLionel Sambuc atf_tc_fail_errno("create");
5411be35a1SLionel Sambuc rump_sys_unlink("rename.test1");
5511be35a1SLionel Sambuc rump_sys_close(fd);
5611be35a1SLionel Sambuc }
5711be35a1SLionel Sambuc
5811be35a1SLionel Sambuc return NULL;
5911be35a1SLionel Sambuc }
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuc static void *
w1_dirs(void * arg)6211be35a1SLionel Sambuc w1_dirs(void *arg)
6311be35a1SLionel Sambuc {
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc rump_pub_lwproc_newlwp(wrkpid);
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc while (!quittingtime) {
6811be35a1SLionel Sambuc if (rump_sys_mkdir("rename.test1", 0777) == -1)
6911be35a1SLionel Sambuc atf_tc_fail_errno("mkdir");
7011be35a1SLionel Sambuc rump_sys_rmdir("rename.test1");
7111be35a1SLionel Sambuc }
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc return NULL;
7411be35a1SLionel Sambuc }
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc static void *
w2(void * arg)7711be35a1SLionel Sambuc w2(void *arg)
7811be35a1SLionel Sambuc {
7911be35a1SLionel Sambuc
8011be35a1SLionel Sambuc rump_pub_lwproc_newlwp(wrkpid);
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc while (!quittingtime) {
8311be35a1SLionel Sambuc rump_sys_rename("rename.test1", "rename.test2");
8411be35a1SLionel Sambuc }
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc return NULL;
8711be35a1SLionel Sambuc }
8811be35a1SLionel Sambuc
8911be35a1SLionel Sambuc #define NWRK 8
9011be35a1SLionel Sambuc static void
renamerace(const atf_tc_t * tc,const char * mp)9111be35a1SLionel Sambuc renamerace(const atf_tc_t *tc, const char *mp)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc pthread_t pt1[NWRK], pt2[NWRK];
9411be35a1SLionel Sambuc int i;
9511be35a1SLionel Sambuc
96*0a6a1f1dSLionel Sambuc /*
97*0a6a1f1dSLionel Sambuc * Sysvbfs supports only 8 inodes so this test would exhaust
98*0a6a1f1dSLionel Sambuc * the inode table and creating files would fail with ENOSPC.
99*0a6a1f1dSLionel Sambuc */
100*0a6a1f1dSLionel Sambuc if (FSTYPE_SYSVBFS(tc))
101*0a6a1f1dSLionel Sambuc atf_tc_skip("filesystem has not enough inodes");
10211be35a1SLionel Sambuc if (FSTYPE_RUMPFS(tc))
10311be35a1SLionel Sambuc atf_tc_skip("rename not supported by file system");
10411be35a1SLionel Sambuc if (FSTYPE_UDF(tc))
105*0a6a1f1dSLionel Sambuc atf_tc_expect_fail("PR kern/49046");
10611be35a1SLionel Sambuc
10711be35a1SLionel Sambuc RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
10811be35a1SLionel Sambuc RL(wrkpid = rump_sys_getpid());
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc RL(rump_sys_chdir(mp));
11111be35a1SLionel Sambuc for (i = 0; i < NWRK; i++)
11211be35a1SLionel Sambuc pthread_create(&pt1[i], NULL, w1, NULL);
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc for (i = 0; i < NWRK; i++)
11511be35a1SLionel Sambuc pthread_create(&pt2[i], NULL, w2, NULL);
11611be35a1SLionel Sambuc
11711be35a1SLionel Sambuc sleep(5);
11811be35a1SLionel Sambuc quittingtime = 1;
11911be35a1SLionel Sambuc
12011be35a1SLionel Sambuc for (i = 0; i < NWRK; i++)
12111be35a1SLionel Sambuc pthread_join(pt1[i], NULL);
12211be35a1SLionel Sambuc for (i = 0; i < NWRK; i++)
12311be35a1SLionel Sambuc pthread_join(pt2[i], NULL);
12411be35a1SLionel Sambuc RL(rump_sys_chdir("/"));
12511be35a1SLionel Sambuc
126*0a6a1f1dSLionel Sambuc if (FSTYPE_UDF(tc))
127*0a6a1f1dSLionel Sambuc atf_tc_fail("race did not trigger this time");
128*0a6a1f1dSLionel Sambuc
12911be35a1SLionel Sambuc if (FSTYPE_MSDOS(tc)) {
13011be35a1SLionel Sambuc atf_tc_expect_fail("PR kern/44661");
13111be35a1SLionel Sambuc /*
13211be35a1SLionel Sambuc * XXX: race does not trigger every time at least
13311be35a1SLionel Sambuc * on amd64/qemu.
13411be35a1SLionel Sambuc */
13511be35a1SLionel Sambuc if (msdosfs_fstest_unmount(tc, mp, 0) != 0) {
13611be35a1SLionel Sambuc rump_pub_vfs_mount_print(mp, 1);
13711be35a1SLionel Sambuc atf_tc_fail_errno("unmount failed");
13811be35a1SLionel Sambuc }
13911be35a1SLionel Sambuc atf_tc_fail("race did not trigger this time");
14011be35a1SLionel Sambuc }
14111be35a1SLionel Sambuc }
14211be35a1SLionel Sambuc
14311be35a1SLionel Sambuc static void
renamerace_dirs(const atf_tc_t * tc,const char * mp)14411be35a1SLionel Sambuc renamerace_dirs(const atf_tc_t *tc, const char *mp)
14511be35a1SLionel Sambuc {
14611be35a1SLionel Sambuc pthread_t pt1, pt2;
14711be35a1SLionel Sambuc
14811be35a1SLionel Sambuc if (FSTYPE_SYSVBFS(tc))
14911be35a1SLionel Sambuc atf_tc_skip("directories not supported by file system");
15011be35a1SLionel Sambuc
15111be35a1SLionel Sambuc if (FSTYPE_RUMPFS(tc))
15211be35a1SLionel Sambuc atf_tc_skip("rename not supported by file system");
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc /* XXX: msdosfs also sometimes hangs */
15511be35a1SLionel Sambuc if (FSTYPE_MSDOS(tc))
15611be35a1SLionel Sambuc atf_tc_expect_signal(-1, "PR kern/43626");
15711be35a1SLionel Sambuc
15811be35a1SLionel Sambuc RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
15911be35a1SLionel Sambuc RL(wrkpid = rump_sys_getpid());
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc RL(rump_sys_chdir(mp));
16211be35a1SLionel Sambuc pthread_create(&pt1, NULL, w1_dirs, NULL);
16311be35a1SLionel Sambuc pthread_create(&pt2, NULL, w2, NULL);
16411be35a1SLionel Sambuc
16511be35a1SLionel Sambuc sleep(5);
16611be35a1SLionel Sambuc quittingtime = 1;
16711be35a1SLionel Sambuc
16811be35a1SLionel Sambuc pthread_join(pt1, NULL);
16911be35a1SLionel Sambuc pthread_join(pt2, NULL);
17011be35a1SLionel Sambuc RL(rump_sys_chdir("/"));
17111be35a1SLionel Sambuc
17211be35a1SLionel Sambuc /*
17311be35a1SLionel Sambuc * Doesn't always trigger when run on a slow backend
17411be35a1SLionel Sambuc * (i.e. not on tmpfs/mfs). So do the usual kludge.
17511be35a1SLionel Sambuc */
17611be35a1SLionel Sambuc if (FSTYPE_MSDOS(tc))
17711be35a1SLionel Sambuc abort();
17811be35a1SLionel Sambuc }
17911be35a1SLionel Sambuc
18011be35a1SLionel Sambuc ATF_TC_FSAPPLY(renamerace, "rename(2) race with file unlinked mid-operation");
18111be35a1SLionel Sambuc ATF_TC_FSAPPLY(renamerace_dirs, "rename(2) race with directories");
18211be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)18311be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
18411be35a1SLionel Sambuc {
18511be35a1SLionel Sambuc
18611be35a1SLionel Sambuc ATF_TP_FSAPPLY(renamerace); /* PR kern/41128 */
18711be35a1SLionel Sambuc ATF_TP_FSAPPLY(renamerace_dirs);
18811be35a1SLionel Sambuc
18911be35a1SLionel Sambuc return atf_no_error();
19011be35a1SLionel Sambuc }
191