open_by_handle: make -h (help) a valid option
[xfstests-dev.git] / src / t_ext4_dax_inline_corruption.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <time.h>
10 #include <unistd.h>
11
12 #define PAGE(a) ((a)*0x1000)
13 #define STRLEN 256
14
15 void err_exit(char *op)
16 {
17         fprintf(stderr, "%s: %s\n", op, strerror(errno));
18         exit(1);
19 }
20
21 int main(int argc, char *argv[])
22 {
23         int fd, err, len = PAGE(1);
24         char *dax_data, *data;
25         char string[STRLEN];
26
27         if (argc < 2) {
28                 printf("Usage: %s <file>\n", basename(argv[0]));
29                 exit(0);
30         }
31
32         srand(time(NULL));
33         snprintf(string, STRLEN, "random number %d\n", rand());
34
35         fd = open(argv[1], O_RDWR);
36         if (fd < 0)
37                 err_exit("fd");
38
39         data = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
40         if (!data)
41                 err_exit("mmap data");
42
43         /* this fallocate turns off inline data and turns on DAX */
44         fallocate(fd, 0, 0, PAGE(2));
45
46         dax_data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
47         if (!dax_data)
48                 err_exit("mmap dax_data");
49
50         /*
51          * Write the data using the non-DAX mapping, and try and read it back
52          * using the DAX mapping.
53          */
54         strcpy(data, string);
55         if (strcmp(dax_data, string) != 0)
56                 printf("Data miscompare\n");
57
58         err = munmap(dax_data, len);
59         if (err < 0)
60                 err_exit("munmap dax_data");
61
62         err = munmap(data, len);
63         if (err < 0)
64                 err_exit("munmap data");
65
66         err = close(fd);
67         if (err < 0)
68                 err_exit("close");
69         return 0;
70 }