1 1.44 ryo /* $NetBSD: t_renamerace.c,v 1.44 2022/01/31 17:23:37 ryo 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.1 pooka } 187 1.1 pooka 188 1.4 pooka static void 189 1.4 pooka renamerace_dirs(const atf_tc_t *tc, const char *mp) 190 1.4 pooka { 191 1.4 pooka pthread_t pt1, pt2; 192 1.4 pooka 193 1.7 pooka if (FSTYPE_SYSVBFS(tc)) 194 1.24 njoly atf_tc_skip("directories not supported by file system"); 195 1.14 pooka if (FSTYPE_RUMPFS(tc)) 196 1.24 njoly atf_tc_skip("rename not supported by file system"); 197 1.35 gson if (FSTYPE_UDF(tc)) 198 1.35 gson atf_tc_expect_fail("PR kern/53865"); 199 1.14 pooka 200 1.15 pooka RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG)); 201 1.12 pooka RL(wrkpid = rump_sys_getpid()); 202 1.12 pooka 203 1.11 pooka RL(rump_sys_chdir(mp)); 204 1.11 pooka pthread_create(&pt1, NULL, w1_dirs, NULL); 205 1.11 pooka pthread_create(&pt2, NULL, w2, NULL); 206 1.4 pooka 207 1.4 pooka sleep(5); 208 1.4 pooka quittingtime = 1; 209 1.4 pooka 210 1.4 pooka pthread_join(pt1, NULL); 211 1.4 pooka pthread_join(pt2, NULL); 212 1.11 pooka RL(rump_sys_chdir("/")); 213 1.4 pooka 214 1.35 gson if (FSTYPE_UDF(tc)) 215 1.35 gson atf_tc_fail("race did not trigger this time"); 216 1.4 pooka } 217 1.4 pooka 218 1.37 riastrad static void 219 1.37 riastrad renamerace_cycle(const atf_tc_t *tc, const char *mp) 220 1.37 riastrad { 221 1.37 riastrad pthread_t pt_rmdir, pt_rename1, pt_rename2; 222 1.37 riastrad 223 1.37 riastrad if (FSTYPE_SYSVBFS(tc)) 224 1.37 riastrad atf_tc_skip("directories not supported by file system"); 225 1.37 riastrad if (FSTYPE_RUMPFS(tc)) 226 1.37 riastrad atf_tc_skip("rename not supported by file system"); 227 1.40 riastrad if (FSTYPE_P2K_FFS(tc)) 228 1.40 riastrad atf_tc_expect_fail("assertion \"vp->v_size == ip->i_size\" failed"); 229 1.37 riastrad if (FSTYPE_PUFFS(tc)) 230 1.37 riastrad atf_tc_expect_fail("assertion \"dfd\" failed"); 231 1.37 riastrad if (FSTYPE_NFS(tc)) 232 1.37 riastrad atf_tc_expect_fail("mkdir fails with ESTALE"); 233 1.41 riastrad if (FSTYPE_UDF(tc)) 234 1.41 riastrad atf_tc_expect_fail("sometimes fails with ENOSPC, PR kern/56253"); 235 1.37 riastrad 236 1.37 riastrad RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG)); 237 1.37 riastrad RL(wrkpid = rump_sys_getpid()); 238 1.37 riastrad 239 1.37 riastrad RL(rump_sys_chdir(mp)); 240 1.37 riastrad pthread_create(&pt_rmdir, NULL, w3_rmdir, NULL); 241 1.37 riastrad pthread_create(&pt_rename1, NULL, w3_rename1, NULL); 242 1.37 riastrad pthread_create(&pt_rename2, NULL, w3_rename2, NULL); 243 1.37 riastrad 244 1.37 riastrad sleep(10); 245 1.37 riastrad quittingtime = 1; 246 1.37 riastrad 247 1.44 ryo alarm(5); 248 1.37 riastrad pthread_join(pt_rmdir, NULL); 249 1.37 riastrad pthread_join(pt_rename1, NULL); 250 1.37 riastrad pthread_join(pt_rename2, NULL); 251 1.37 riastrad alarm(0); 252 1.37 riastrad RL(rump_sys_chdir("/")); 253 1.37 riastrad 254 1.41 riastrad if (FSTYPE_UDF(tc)) 255 1.41 riastrad atf_tc_fail("PR kern/56253 did not trigger this time"); 256 1.43 gson if (FSTYPE_P2K_FFS(tc)) 257 1.43 gson atf_tc_fail("did not fail this time"); 258 1.43 gson if (FSTYPE_PUFFS(tc)) 259 1.43 gson atf_tc_fail("did not fail this time"); 260 1.43 gson if (FSTYPE_NFS(tc)) 261 1.43 gson atf_tc_fail("did not fail this time"); 262 1.37 riastrad } 263 1.37 riastrad 264 1.1 pooka ATF_TC_FSAPPLY(renamerace, "rename(2) race with file unlinked mid-operation"); 265 1.4 pooka ATF_TC_FSAPPLY(renamerace_dirs, "rename(2) race with directories"); 266 1.37 riastrad ATF_TC_FSAPPLY(renamerace_cycle, "rename(2) race making directory cycles"); 267 1.1 pooka 268 1.1 pooka ATF_TP_ADD_TCS(tp) 269 1.1 pooka { 270 1.1 pooka 271 1.1 pooka ATF_TP_FSAPPLY(renamerace); /* PR kern/41128 */ 272 1.4 pooka ATF_TP_FSAPPLY(renamerace_dirs); 273 1.37 riastrad ATF_TP_FSAPPLY(renamerace_cycle); 274 1.1 pooka 275 1.1 pooka return atf_no_error(); 276 1.1 pooka } 277