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