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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2012 Jilin Xpd <jilinxpd@gmail.com>
* Copyright 2018 Nexenta Systems, Inc.
*/
/*
* Test if file read/write is coherent with mmap, perform 2 tests:
* modify file through mmap, and check the result through file read.
* modify file through file write, and check the result through mmap.
*/
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
void
usage(void)
{
fprintf(stderr,
"usage: rw_mmap -n <size>[b|k|m|g] -f <filename>\n");
exit(1);
}
int
main(int argc, char **argv)
{
char *suffix;
char *filename = NULL;
char *file_addr;
char *p;
size_t filesize;
ssize_t blksize;
size_t cnt = 1;
size_t mul = 1;
int c, fid;
char *buf;
/*
* parse arguments
*/
while ((c = getopt(argc, argv, "n:f:")) != -1) {
switch (c) {
case 'n':
cnt = (size_t)strtoul(optarg, &suffix, 0);
if (cnt == 0)
goto bad_n_arg;
switch (*suffix) {
case '\0':
case 'b':
mul = 1;
break;
case 'k':
mul = 1024;
break;
case 'm':
mul = (1024 * 1024);
break;
case 'g':
mul = (1024 * 1024 * 1024);
break;
default:
bad_n_arg:
fprintf(stderr, "-n %s: invalid size\n",
optarg);
return (1);
}
cnt = cnt * mul;
break;
case 'f': /* target file */
filename = optarg;
break;
case ':': /* missing optarg */
fprintf(stderr,
"Option -%c requires an arg\n", optopt);
usage();
break;
case '?':
fprintf(stderr,
"Unrecognized option: -%c\n", optopt);
usage();
break;
}
}
/* open test file */
fid = open(filename, O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
if (fid == -1) {
fprintf(stderr, "open %s error=%d\n", filename, errno);
return (1);
}
/* extend file */
filesize = cnt;
if (ftruncate(fid, filesize) == -1) {
fprintf(stderr, "ftrunc %s error=%d\n", filename, errno);
return (1);
}
/* map file */
file_addr = mmap(NULL, filesize,
PROT_READ | PROT_WRITE, MAP_SHARED, fid, 0);
if (file_addr == MAP_FAILED) {
fprintf(stderr, "mmap %s error=%d\n", filename, errno);
return (1);
}
blksize = 4096;
buf = malloc(blksize);
if (buf == NULL) {
fprintf(stderr, "malloc failed\n");
return (1);
}
/* verify fread and mmap see the same data */
p = file_addr + 2013; /* not aligned to 4KB, on purpose */
lseek(fid, 2013, SEEK_SET);
while (p < file_addr + filesize) {
blksize = read(fid, buf, blksize);
if (blksize < 0) {
perror(filename);
return (1);
}
if (blksize == 0)
break;
if (memcmp(buf, p, blksize) != 0) {
fprintf(stderr, "memcmp failed 1\n");
return (1);
}
p += blksize;
}
/* modify file through mmap, verify fread can see the change */
blksize = 4096;
p = file_addr + 2013; /* not aligned to 4KB */
lseek(fid, 2013, SEEK_SET);
c = 'a';
while (p < file_addr + filesize) {
if (p + blksize > file_addr + filesize)
blksize = file_addr + filesize - p;
memset(p, c++, blksize);
blksize = read(fid, buf, blksize);
if (blksize < 0) {
perror(filename);
return (1);
}
if (blksize == 0)
break;
if (memcmp(buf, p, blksize) != 0) {
fprintf(stderr, "memcmp failed 2\n");
return (1);
}
p += blksize;
}
/* modify file through fwrite, verify mmap can see the change */
blksize = 4096;
p = file_addr + 2013; /* not aligned to 4KB */
lseek(fid, 2013, SEEK_SET);
c = 'Z';
while (p < file_addr + filesize) {
if (p + blksize > file_addr + filesize)
blksize = file_addr + filesize - p;
memset(buf, c--, blksize);
blksize = write(fid, buf, blksize);
if (blksize < 0) {
perror(filename);
return (1);
}
if (blksize == 0)
break;
if (memcmp(buf, p, blksize) != 0) {
fprintf(stderr, "memcmp failed 3\n");
return (1);
}
p += blksize;
}
/* sync pages to file */
if (msync(file_addr, filesize, MS_SYNC) == -1) {
fprintf(stderr, "msync %s error=%d\n", filename, errno);
return (1);
}
/* unmap file */
if (munmap(file_addr, filesize) == -1) {
fprintf(stderr, "munmap %s error=%d\n", filename, errno);
return (1);
}
/* close file */
if (close(fid) == -1) {
fprintf(stderr, "close %s error=%d\n", filename, errno);
return (1);
}
return (0);
}
|