common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / src / t_ext4_dax_journal_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 void chattr_cmd(char *chattr, char *cmd, char *file)
22 {
23         int ret;
24         char command[STRLEN];
25
26         ret = snprintf(command, STRLEN, "%s %s %s 2>/dev/null", chattr, cmd, file);
27         if (ret < 0)
28                 err_exit("snprintf");
29
30         ret = system(command);
31         if (ret) /* Success - the kernel fix is to have this chattr fail */
32                 exit(77);
33 }
34
35 int main(int argc, char *argv[])
36 {
37         int fd, err, len = PAGE(1);
38         char *data, *dax_data, *chattr, *file;
39         char string[STRLEN];
40
41         if (argc < 3) {
42                 printf("Usage: %s <chattr program> <file>\n", basename(argv[0]));
43                 exit(0);
44         }
45
46         chattr = argv[1];
47         file = argv[2];
48
49         srand(time(NULL));
50         snprintf(string, STRLEN, "random number %d\n", rand());
51
52         fd = open(file, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
53         if (fd < 0)
54                 err_exit("fd");
55
56         /* begin with journaling off and DAX on */
57         chattr_cmd(chattr, "-j", file);
58
59         ftruncate(fd, 0);
60         fallocate(fd, 0, 0, len);
61
62         dax_data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
63         if (!dax_data)
64                 err_exit("mmap dax_data");
65
66         /*
67          * This turns on journaling.  It also has the side-effect that it
68          * turns off DAX for the given inode since journaling and DAX aren't
69          * allowed to be on at the same time.  This happens in
70          * ext4_change_inode_journal_flag() in kernel v4.14 and before.
71          *
72          * Note that this turns off the runtime DAX flag (S_DAX) in the
73          * in-memory inode, and has nothing to do with per-inode on-media DAX
74          * inode flags.
75          */
76         chattr_cmd(chattr, "+j", file);
77
78         data = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
79         if (!data)
80                 err_exit("mmap data");
81
82         /*
83          * Write the data using the non-DAX mapping, and try and read it back
84          * using the DAX mapping.
85          */
86         strcpy(data, string);
87         if (strcmp(dax_data, string) != 0)
88                 printf("Data miscompare\n");
89
90         err = munmap(data, len);
91         if (err < 0)
92                 err_exit("munmap data");
93
94         err = munmap(dax_data, len);
95         if (err < 0)
96                 err_exit("munmap dax_data");
97
98         err = close(fd);
99         if (err < 0)
100                 err_exit("close");
101         return 0;
102 }