1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Intel Corporation. */
10 #include <sys/types.h>
14 #define PAGE(a) ((a)*0x1000)
17 void err_exit(char *op)
19 fprintf(stderr, "%s: %s\n", op, strerror(errno));
23 void chattr_cmd(char *chattr, char *cmd, char *file)
28 ret = snprintf(command, STRLEN, "%s %s %s 2>/dev/null", chattr, cmd, file);
32 ret = system(command);
33 if (ret) /* Success - the kernel fix is to have this chattr fail */
37 int main(int argc, char *argv[])
39 int fd, err, len = PAGE(1);
40 char *data, *dax_data, *chattr, *file;
44 printf("Usage: %s <chattr program> <file>\n", basename(argv[0]));
52 snprintf(string, STRLEN, "random number %d\n", rand());
54 fd = open(file, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
58 /* begin with journaling off and DAX on */
59 chattr_cmd(chattr, "-j", file);
62 fallocate(fd, 0, 0, len);
64 dax_data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
65 if (dax_data == MAP_FAILED)
66 err_exit("mmap dax_data");
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.
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
78 chattr_cmd(chattr, "+j", file);
80 data = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
81 if (data == MAP_FAILED)
82 err_exit("mmap data");
85 * Write the data using the non-DAX mapping, and try and read it back
86 * using the DAX mapping.
89 if (strcmp(dax_data, string) != 0)
90 printf("Data miscompare\n");
92 err = munmap(data, len);
94 err_exit("munmap data");
96 err = munmap(dax_data, len);
98 err_exit("munmap dax_data");