12 #define PAGE(a) ((a)*0x1000)
15 void err_exit(char *op)
17 fprintf(stderr, "%s: %s\n", op, strerror(errno));
21 void chattr_cmd(char *chattr, char *cmd, char *file)
26 ret = snprintf(command, STRLEN, "%s %s %s 2>/dev/null", chattr, cmd, file);
30 ret = system(command);
31 if (ret) /* Success - the kernel fix is to have this chattr fail */
35 int main(int argc, char *argv[])
37 int fd, err, len = PAGE(1);
38 char *data, *dax_data, *chattr, *file;
42 printf("Usage: %s <chattr program> <file>\n", basename(argv[0]));
50 snprintf(string, STRLEN, "random number %d\n", rand());
52 fd = open(file, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
56 /* begin with journaling off and DAX on */
57 chattr_cmd(chattr, "-j", file);
60 fallocate(fd, 0, 0, len);
62 dax_data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
64 err_exit("mmap dax_data");
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.
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
76 chattr_cmd(chattr, "+j", file);
78 data = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
80 err_exit("mmap data");
83 * Write the data using the non-DAX mapping, and try and read it back
84 * using the DAX mapping.
87 if (strcmp(dax_data, string) != 0)
88 printf("Data miscompare\n");
90 err = munmap(data, len);
92 err_exit("munmap data");
94 err = munmap(dax_data, len);
96 err_exit("munmap dax_data");