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