aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/mmap48.sh
blob: 35da78a98c26fb92a032d08f0d9afd759a76f375 (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/sh

#
# Copyright (c) 2024 Peter Holm <pho@FreeBSD.org>
#
# SPDX-License-Identifier: BSD-2-Clause
#

# Demonstrate issue described in:
# [Bug 276002] nfscl: data corruption using both copy_file_range and mmap'd I/O

# This version only uses mapped read/write, read(2)/write(2), fstat(2)  and ftruncate(2)

# Issue seen:
# 20241003 10:04:24 all: mmap48.sh
# 5257c5257
# < 0244200    80  81  82  83  84  85  86  87  88  89  8a  8b  8c  8d  8e  8f
# ---
# > 0244200    80  81  82  83  84  85  86  87  88  ee  8a  8b  8c  8d  8e  8f
# 256 -rw-------  1 root wheel 262144 Oct  3 10:05 file
# 256 -rw-------  1 root wheel 262144 Oct  3 10:04 file.orig
# FAIL mmap48.sh exit code 2

[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
. ../default.cfg
set -u
prog=$(basename "$0" .sh)
log=/tmp/$prog.log
serial=/tmp/$prog.serial
grep -q $mntpoint /etc/exports ||
    { echo "$mntpoint missing from /etc/exports"; exit 0; }
rpcinfo 2>/dev/null | grep -q mountd || exit 0

cat > /tmp/$prog.c <<EOF
#include <sys/mman.h>
#include <sys/stat.h>

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <pthread.h>
#include <pthread_np.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static off_t siz;
static pthread_mutex_t write_mutex;
static int fd, go;
static char *cp;

#define THREADS 100

static void *
memread(void *arg __unused)
{
	int i;
	char c;

	if (arc4random() % 100 < 10)
		return (0);
	pthread_set_name_np(pthread_self(), __func__);
	while (go == -1)
		usleep(50);
	while (go == 1) {
		i = arc4random() % siz;
		c = cp[i];
		if (c != 0x77) /* No unused vars here */
			usleep(arc4random() % 400);
	}
	return (NULL);
}

static void *
memwrite(void *arg __unused)
{
	int i;
	char c;

	if (arc4random() % 100 < 10)
		return (0);
	pthread_set_name_np(pthread_self(), __func__);
	while (go == -1)
		usleep(50);
	while (go == 1) {
		i = arc4random() % siz;
		pthread_mutex_lock(&write_mutex);
		c = cp[i];
		cp[i] = 0xee;	/* This value seems to linger with NFS */
		cp[i] = c;
		pthread_mutex_unlock(&write_mutex);
		usleep(arc4random() % 400);
	}
	return (NULL);
}

static void *
wr(void *arg __unused)
{
	off_t pos;
	int r, s;
	char buf[1024];

	if (arc4random() % 100 < 10)
		return (0);
	pthread_set_name_np(pthread_self(), __func__);
	while (go == -1)
		usleep(50);
	while (go == 1) {
		s = arc4random() % sizeof(buf) + 1;
		pos = arc4random() % (siz - s);
		pthread_mutex_lock(&write_mutex);
		if (lseek(fd, pos, SEEK_SET) == -1)
			err(1, "lseek(%d)", (int)pos);
		if ((r = read(fd, buf, s)) != s) {
			fprintf(stderr, "r = %d, s = %d, pos = %d\n", r, s, (int)pos);
			err(1, "read():2");
		}
		if (lseek(fd, pos, SEEK_SET) == -1)
			err(1, "lseek(%d)", (int)pos);
		if (write(fd, buf, s) != s)
			err(1, "write()");
		pthread_mutex_unlock(&write_mutex);
		usleep(arc4random() % 200);
	}
	return (NULL);
}

/* Both ftruncate() and fdatasync() triggers the problem */

static void *
sy(void *arg __unused)
{

	if (arc4random() % 100 < 10)
		return (0);
	pthread_set_name_np(pthread_self(), __func__);
	while (go == -1)
		usleep(50);
	while (go == 1) {
		if (fdatasync(fd) == -1)
			err(1, "fdatasync()");
		usleep(arc4random() % 1000);
	}
	return (NULL);
}

static void *
tr(void *arg __unused)
{

	if (arc4random() % 100 < 10)
		return (0);
	pthread_set_name_np(pthread_self(), __func__);
	while (go == -1)
		usleep(50);
	while (go == 1) {
		if (ftruncate(fd, siz) == -1) /* No size change */
			err(1, "truncate)");
		usleep(arc4random() % 1000);
	}
	return (NULL);
}

static void *
fs(void *arg __unused)
{
	struct stat st;

	if (arc4random() % 100 < 10)
		return (0);
	pthread_set_name_np(pthread_self(), __func__);
	while (go == -1)
		usleep(50);
	while (go == 1) {
		if (fstat(fd, &st) == -1)
			err(1, "stat()");
		usleep(arc4random() % 1000);
	}
	return (NULL);
}

int
main(int argc, char *argv[])
{
	struct stat st;
	pthread_t tp[THREADS];
	int e, i, idx;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <file>\n", argv[0]);
		exit(1);
	}
	if ((fd = open(argv[1], O_RDWR)) == -1)
		err(1, "open(%s)", argv[1]);
	if (fstat(fd, &st) == -1)
		err(1, "stat(%s)", argv[1]);
	siz = st.st_size;
	cp = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (cp == MAP_FAILED)
		err(1, "mmap()");

	go = -1;
	pthread_mutex_init(&write_mutex, NULL);
	idx = 0;
	for (i = 0; i < (int)(arc4random() % 3 + 1); i++) {
		if ((e = pthread_create(&tp[idx++], NULL, memread, NULL)) != 0)
			errc(1, e, "pthread_create");
	}
	for (i = 0; i < (int)(arc4random() % 3 + 1); i++) {
		if ((e = pthread_create(&tp[idx++], NULL, memwrite, NULL)) != 0)
			errc(1, e, "pthread_create");
	}
	for (i = 0; i < (int)(arc4random() % 3 + 1); i++) {
		if ((e = pthread_create(&tp[idx++], NULL, wr, NULL)) != 0)
			errc(1, e, "pthread_create");
	}
	for (i = 0; i < (int)(arc4random() % 3 + 1); i++) {
		if ((e = pthread_create(&tp[idx++], NULL, fs, NULL)) != 0)
			errc(1, e, "pthread_create");
	}
	for (i = 0; i < (int)(arc4random() % 3 + 1); i++) {
		if ((e = pthread_create(&tp[idx++], NULL, tr, NULL)) != 0)
			errc(1, e, "pthread_create");
	}
	for (i = 0; i < (int)(arc4random() % 3 + 1); i++) {
		if ((e = pthread_create(&tp[idx++], NULL, sy, NULL)) != 0)
			errc(1, e, "pthread_create");
	}
	assert(idx <= THREADS);

	sleep(1);
	go = 1;
	sleep(60);
	go = 0;
	for (i = 0; i < idx; i++)
		pthread_join(tp[i], NULL);
	if (munmap(cp, siz) == -1)
		err(1, "munmap()");
	close(fd);
}
EOF
mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1

mycc -o $serial -Wall -Wextra -O2 ../tools/serial.c || exit 1
mount | grep -q "on $mntpoint " && umount -f $mntpoint
mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
mdconfig -s 5g -u $mdstart
newfs -n $newfs_flags /dev/md$mdstart > /dev/null
mount /dev/md$mdstart $mntpoint

mp2=${mntpoint}2
mkdir -p $mp2
mount | grep -q "on $mp2 " && umount -f $mp2
mount -t nfs -o retrycnt=3 127.0.0.1:$mntpoint $mp2 || exit 1
sleep .2

here=`pwd`
cd $mp2
#$here/../testcases/swap/swap -t 5m -i 20 > /dev/null &
sleep 2

size=262144
$serial file $size
cp file file.orig

s=0
#ktrace -id -f $here/ktrace.out /tmp/$prog file || s=1
/tmp/$prog file || s=1

while pgrep -q swap; do pkill swap; done
wait
if ! cmp -s file.orig file; then
	od -t x1 file.orig > /var/tmp/$prog.file1
	od -t x1 file      > /var/tmp/$prog.file2
	diff /var/tmp/$prog.file1 /var/tmp/$prog.file2 > $log
	head -20 $log
	rm /var/tmp/$prog.file1 /var/tmp/$prog.file2
	ls -ls file.orig file
	s=2
fi

cd $here
umount $mp2
umount $mntpoint
mdconfig -d -u $mdstart
rm -f $serial /tmp/$prog /tmp/$prog.c $log
exit $s