xfs: wrap xfs_db calls to the test device
[xfstests-dev.git] / src / t_ext4_dax_inline_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 int main(int argc, char *argv[])
24 {
25         int fd, err, len = PAGE(1);
26         char *dax_data, *data;
27         char string[STRLEN];
28
29         if (argc < 2) {
30                 printf("Usage: %s <file>\n", basename(argv[0]));
31                 exit(0);
32         }
33
34         srand(time(NULL));
35         snprintf(string, STRLEN, "random number %d\n", rand());
36
37         fd = open(argv[1], O_RDWR);
38         if (fd < 0)
39                 err_exit("fd");
40
41         data = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
42         if (data == MAP_FAILED)
43                 err_exit("mmap data");
44
45         /* this fallocate turns off inline data and turns on DAX */
46         fallocate(fd, 0, 0, PAGE(2));
47
48         dax_data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
49         if (dax_data == MAP_FAILED)
50                 err_exit("mmap dax_data");
51
52         /*
53          * Write the data using the non-DAX mapping, and try and read it back
54          * using the DAX mapping.
55          */
56         strcpy(data, string);
57         if (strcmp(dax_data, string) != 0)
58                 printf("Data miscompare\n");
59
60         err = munmap(dax_data, len);
61         if (err < 0)
62                 err_exit("munmap dax_data");
63
64         err = munmap(data, len);
65         if (err < 0)
66                 err_exit("munmap data");
67
68         err = close(fd);
69         if (err < 0)
70                 err_exit("close");
71         return 0;
72 }