summaryrefslogtreecommitdiff
path: root/src/t_ext4_dax_inline_corruption.c
blob: e1a39a6c1e46ff3c43c26cf9a54d444567125640 (plain)
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
// 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);
}

int main(int argc, char *argv[])
{
	int fd, err, len = PAGE(1);
	char *dax_data, *data;
	char string[STRLEN];

	if (argc < 2) {
		printf("Usage: %s <file>\n", basename(argv[0]));
		exit(0);
	}

	srand(time(NULL));
	snprintf(string, STRLEN, "random number %d\n", rand());

	fd = open(argv[1], O_RDWR);
	if (fd < 0)
		err_exit("fd");

	data = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (data == MAP_FAILED)
		err_exit("mmap data");

	/* this fallocate turns off inline data and turns on DAX */
	fallocate(fd, 0, 0, PAGE(2));

	dax_data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
	if (dax_data == MAP_FAILED)
		err_exit("mmap dax_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(dax_data, len);
	if (err < 0)
		err_exit("munmap dax_data");

	err = munmap(data, len);
	if (err < 0)
		err_exit("munmap data");

	err = close(fd);
	if (err < 0)
		err_exit("close");
	return 0;
}