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