xfs: make sure our default quota warning limits and grace periods survive quotacheck
[xfstests-dev.git] / src / t_mmap_stale_pmd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 Intel Corporation. */
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <libgen.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #define MiB(a) ((a)*1024*1024)
15
16 void err_exit(char *op)
17 {
18         fprintf(stderr, "%s: %s\n", op, strerror(errno));
19         exit(1);
20 }
21
22 int main(int argc, char *argv[])
23 {
24         volatile int a __attribute__((__unused__));
25         char *buffer = "HELLO WORLD!";
26         char *data;
27         int fd, err, ret = 0;
28
29         if (argc < 2) {
30                 printf("Usage: %s <pmem file>\n", basename(argv[0]));
31                 exit(0);
32         }
33
34         fd = open(argv[1], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
35         if (fd < 0)
36                 err_exit("fd");
37
38         /*
39          * This allows us to map a huge zero page, and we do it at a non-zero
40          * offset for a little additional testing.
41          */
42         ftruncate(fd, 0);
43         ftruncate(fd, MiB(4));
44
45         data = mmap(NULL, MiB(2), PROT_READ, MAP_SHARED, fd, MiB(2));
46         if (data == MAP_FAILED)
47                 err_exit("mmap");
48
49         /*
50          * This faults in a 2MiB zero page to satisfy the read.
51          * 'a' is volatile so this read doesn't get optimized out.
52          */
53         a = data[0];
54
55         pwrite(fd, buffer, strlen(buffer), MiB(2));
56
57         /*
58          * Try and use the mmap to read back the data we just wrote with
59          * pwrite().  If the kernel bug is present the mapping from the 2MiB
60          * zero page will still be intact, and we'll read back zeros instead.
61          */
62         if (strncmp(buffer, data, strlen(buffer))) {
63                 fprintf(stderr, "strncmp mismatch: '%s' vs '%s'\n", buffer,
64                                 data);
65                 ret = 1;
66         }
67
68         err = munmap(data, MiB(2));
69         if (err < 0)
70                 err_exit("munmap");
71
72         err = close(fd);
73         if (err < 0)
74                 err_exit("close");
75
76         return ret;
77 }