common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / src / t_mmap_cow_race.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <libgen.h>
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/mman.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 #define MiB(a) ((a)*1024*1024)
14 #define NUM_THREADS 2
15
16 void err_exit(char *op)
17 {
18         fprintf(stderr, "%s: %s\n", op, strerror(errno));
19         exit(1);
20 }
21
22 void worker_fn(void *ptr)
23 {
24         char *data = (char *)ptr;
25         volatile int a;
26         int i, err;
27
28         for (i = 0; i < 10; i++) {
29                 a = data[0];
30                 data[0] = a;
31
32                 err = madvise(data, MiB(2), MADV_DONTNEED);
33                 if (err < 0)
34                         err_exit("madvise");
35
36                 /* Mix up the thread timings to encourage the race. */
37                 err = usleep(rand() % 100);
38                 if (err < 0)
39                         err_exit("usleep");
40         }
41 }
42
43 int main(int argc, char *argv[])
44 {
45         pthread_t thread[NUM_THREADS];
46         int i, j, fd, err;
47         char *data;
48
49         if (argc < 2) {
50                 printf("Usage: %s <file>\n", basename(argv[0]));
51                 exit(0);
52         }
53
54         fd = open(argv[1], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
55         if (fd < 0)
56                 err_exit("fd");
57
58         /* This allows us to map a huge page. */
59         ftruncate(fd, 0);
60         ftruncate(fd, MiB(2));
61
62         /*
63          * First we set up a shared mapping.  Our write will (hopefully) get
64          * the filesystem to give us a 2MiB huge page DAX mapping.  We will
65          * then use this 2MiB page for our private mapping race.
66          */
67         data = mmap(NULL, MiB(2), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
68         if (data == MAP_FAILED)
69                 err_exit("shared mmap");
70
71         data[0] = 1;
72
73         err = munmap(data, MiB(2));
74         if (err < 0)
75                 err_exit("shared munmap");
76
77         for (i = 0; i < 500; i++) {
78                 data = mmap(NULL, MiB(2), PROT_READ|PROT_WRITE, MAP_PRIVATE,
79                                 fd, 0);
80                 if (data == MAP_FAILED)
81                         err_exit("private mmap");
82
83                 for (j = 0; j < NUM_THREADS; j++) {
84                         err = pthread_create(&thread[j], NULL,
85                                         (void*)&worker_fn, data);
86                         if (err)
87                                 err_exit("pthread_create");
88                 }
89
90                 for (j = 0; j < NUM_THREADS; j++) {
91                         err = pthread_join(thread[j], NULL);
92                         if (err)
93                                 err_exit("pthread_join");
94                 }
95
96                 err = munmap(data, MiB(2));
97                 if (err < 0)
98                         err_exit("private munmap");
99         }
100
101         err = close(fd);
102         if (err < 0)
103                 err_exit("close");
104
105         return 0;
106 }