summaryrefslogtreecommitdiff
path: root/regress/sys/fileops/fileops.c
blob: e9970485ac98da125ef571dc94a8e8b687333b36 (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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* $OpenBSD: fileops.c,v 1.2 2017/05/29 13:49:40 bluhm Exp $ */
/*
 * Copyright (c) 2017 Stefan Fritsch <sf@sfritsch.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#define BUFSIZE (16 * 1024)
#define HOLESIZE (16 * BUFSIZE)

static int	 debug = 0;
static int	 fd = -1;
static off_t	 curpos = 0;
static char	*fname;
static char	*gbuf;
static char	*mbuf;

void
gen_data(void *buf, size_t size, uint32_t seed)
{
	assert(size % 4 == 0);
	if (debug)
		printf("%s: size %zd seed %#08x\n", __func__, size, seed);
	uint32_t *ibuf = buf;
	for (size_t i = 0; i < size / 4; i++)
		ibuf[i] = seed + i;
}

void
check_data(const void *buf, size_t size, uint32_t seed)
{
	assert(size % 4 == 0);
	const uint32_t *ibuf = buf;
	for (size_t i = 0; i < size / 4; i++) {
		if (ibuf[i] != seed + i) {
			errx(3, "%s: pos %zd/%zd: expected %#08zx got %#08x",
			    __func__, 4 * i, size, seed + i, ibuf[i]);
		}
	}
}

void
check_zero(const void *buf, size_t size)
{
	assert(size % 4 == 0);
	const uint32_t *ibuf = buf;
	for (size_t i = 0; i < size / 4; i++) {
		if (ibuf[i] != 0) {
			errx(3, "%s: pos %zd/%zd: expected 0 got %#08x",
			    __func__, 4 * i, size, ibuf[i]);
		}
	}
}

void
check(const char *what, int64_t have, int64_t want)
{
	if (have != want) {
		if (have == -1)
			err(2, "%s returned %lld, expected %lld",
			    what, have, want);
		else
			errx(2, "%s returned %lld, expected %lld",
			    what, have, want);
	}

	if (debug)
		printf("%s returned %lld\n", what, have);
}

void
c_write(void *buf, size_t size)
{
	ssize_t ret = write(fd, buf, size);
	check("write", ret, size);
	curpos += ret;
}

void
c_read(void *buf, size_t size)
{
	ssize_t ret = read(fd, buf, size);
	check("read", ret, size);
	curpos += ret;
}

void
c_fsync(void)
{
	int ret = fsync(fd);
	check("fsync", ret, 0);
}

void
c_lseek(off_t offset, int whence)
{
	off_t ret = lseek(fd, offset, whence);
	switch (whence) {
		case SEEK_SET:
			curpos = offset;
			break;
		case SEEK_CUR:
			curpos += offset;
			break;
		default:
			errx(1, "c_lseek not supported");
	}
	check("lseek", ret, curpos);
	if (debug)
		printf("curpos: %lld\n", (long long int)curpos);
}

void
c_mmap(size_t size)
{
	mbuf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, curpos);
	if (mbuf == MAP_FAILED)
		err(2, "mmap %zd pos %lld failed", size, (long long)curpos);
	curpos += size;
	if (debug)
		printf("mmap: %p\n", mbuf);
}

void
c_munmap(size_t size)
{
	int ret = munmap(mbuf, size);
	if (ret != 0)
		err(2, "munmap");
}

void
c_open(int flags)
{
	fd = open(fname, flags, S_IRUSR|S_IWUSR);
	if (fd == -1)
		err(1, "open");
}

void
check_read(size_t size, int hole)
{
	size_t pos = 0;
	while (pos < size) {
		size_t to_read = size - pos;
		uint32_t seed = curpos;
		if (to_read > BUFSIZE)
			to_read = BUFSIZE;
		c_read(gbuf, to_read);
		if (hole)
			check_zero(gbuf, to_read);
		else
			check_data(gbuf, to_read, seed);
		pos += to_read;
	}
}

/* XXX this assumes size is a multiple of the page size */
void
check_mmap(size_t size, int hole)
{
	size_t pos = 0;
	while (pos < size) {
		size_t to_read = size - pos;
		uint32_t seed = curpos;
		if (to_read > BUFSIZE)
			to_read = BUFSIZE;
		c_mmap(to_read);
		if (hole)
			check_zero(mbuf, to_read);
		else
			check_data(mbuf, to_read, seed);
		c_munmap(to_read);
		pos += to_read;
	}
}

void
do_create(void)
{
	unlink(fname);
	c_open(O_EXCL|O_CREAT|O_RDWR);

	gen_data(gbuf, BUFSIZE, curpos);
	c_write(gbuf, BUFSIZE);

	c_lseek(HOLESIZE, SEEK_CUR);

	gen_data(gbuf, BUFSIZE, curpos);
	c_write(gbuf, BUFSIZE);
	c_fsync();
}

void
do_read(void)
{
	c_open(O_RDWR);
	check_read(BUFSIZE, 0);
	check_read(HOLESIZE, 1);
	check_read(BUFSIZE, 0);
}

void
do_mmap(void)
{
	c_open(O_RDWR);
	check_mmap(BUFSIZE, 0);
	check_mmap(HOLESIZE, 1);
	check_mmap(BUFSIZE, 0);
}

void
usage(void)
{
	errx(1, "usage: fileops (create|read|mmap) filename");
}

int main(int argc, char **argv)
{
	if (argc != 3)
		usage();

	fname = argv[2];
	gbuf = malloc(BUFSIZE);
	if (gbuf == NULL)
		err(1, "malloc");

	if (strcmp(argv[1], "create") == 0) {
		do_create();
	} else if (strcmp(argv[1], "read") == 0) {
		do_read();
	} else if (strcmp(argv[1], "mmap") == 0) {
		do_mmap();
	} else {
		usage();
	}

	printf("pass\n");
	return 0;
}