liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2017 Red Hat, Inc.
 * 01/02/2003    Port to LTP avenkat@us.ibm.com
 * 06/30/2001    Port to Linux    nsharoff@us.ibm.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * Mmap parts of a file, and then write to the file causing single
 * write requests to jump back and forth between mmaped io and regular io.
 */
 
#define _GNU_SOURCE
#include <stdlib.h>
#include "tst_test.h"
#include "tst_safe_macros.h"
 
#define NUM_PAGES (192)
#define TEST_FILE "mmapstress04-testfile"
 
static int page_size;
static unsigned char *mmap_area;
 
static void setup(void)
{
   page_size = getpagesize();
 
   /*
    * Pick large enough area, PROT_NONE doesn't matter,
    * because we remap it later.
    */
   mmap_area = SAFE_MMAP(NULL, page_size * NUM_PAGES, PROT_NONE,
       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
}
 
static void write_fully(int fd, void *buf, int len)
{
   int ret;
 
   do {
       ret = SAFE_WRITE(0, fd, buf, len);
       buf += ret;
       len -= ret;
   } while (len > 0);
}
 
static void mmapstress04(void)
{
   int i, j, rofd, rwfd;
   char *buf;
   int mapped_pages = 0;
 
   if (tst_fill_file(TEST_FILE, 'b', page_size, 1))
       tst_brk(TBROK | TERRNO, "fill_file");
 
   rofd = SAFE_OPEN(TEST_FILE, O_RDONLY | O_CREAT, 0777);
   /*
    * Assuming disk blocks are 8k, and logical pages are 4k, there are
    * two maps per block. In order to test mapping at the beginning and
    * ends of the block, mapping the whole block, or none of the block
    * with different mappings on preceding and following blocks, each
    * 3 blocks with 6 pages can be thought of as a binary number from 0 to
    * 64 with a bit set for mapped or cleared for unmapped. This number
    * is represented by i. The value j is used to look at the bits of i
    * and decided to map the page or not.
    * NOTE: None of the above assumptions are critical.
    */
   for (i = 0; i < 64; i++) {
       for (j = 0; j < 6; j++) {
           off_t mapoff;
 
           if (!(i & (1 << j)))
               continue;
 
           mapoff = page_size * (off_t)(6 * i + j);
           SAFE_MMAP(mmap_area + page_size * mapped_pages++,
                page_size, PROT_READ,
                MAP_FILE | MAP_PRIVATE | MAP_FIXED,
                rofd, mapoff);
       }
   }
   SAFE_CLOSE(rofd);
 
   /* write out 6 pages of stuff into each of the 64 six page sections */
   rwfd = SAFE_OPEN(TEST_FILE, O_RDWR);
   buf = SAFE_MALLOC(page_size);
   memset(buf, 'a', page_size);
   for (i = 0; i < 6 * 64; i++)
       write_fully(rwfd, buf, page_size);
   free(buf);
   SAFE_CLOSE(rwfd);
 
   /*
    * Just finished scribbling all over interwoven mmapped and unmapped
    * regions. Check the data.
    */
   for (i = 0; i < mapped_pages * page_size; i++) {
       unsigned char val = *(mmap_area + i);
 
       if (val != 'a') {
           tst_res(TFAIL, "unexpected value in map, "
               "i=%d,val=0x%x", i, val);
           goto done;
       }
   }
   tst_res(TPASS, "blocks have expected data");
done:
   SAFE_UNLINK(TEST_FILE);
}
 
static struct tst_test test = {
   .needs_tmpdir = 1,
   .test_all = mmapstress04,
   .setup = setup,
};