xref: /freebsd/tools/test/stress2/misc/nullfs22.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# fcntl(2) locking scenario, using UFS and a nullfs mount.
30# No problems seen.
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34. ../default.cfg
35
36here=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $here/$0 > nullfs22.c
39mycc -o nullfs22 -Wall -Wextra -O0 -g nullfs22.c || exit 1
40rm -f nullfs22.c
41
42mp2=${mntpoint}2
43[ -d $mp2 ] || mkdir -p $mp2
44mount | grep -q "on $mp2 " && umount $mp2
45mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
46[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
47mdconfig -a -t swap -s 512m -u $mdstart || exit 1
48newfs -n md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50
51mount -t nullfs $mntpoint $mp2
52
53/tmp/nullfs22 $mntpoint $mp2
54status=$?
55
56while mount | grep -q "on $mp2 "; do
57	umount $mp2
58done
59while mount | grep "on $mntpoint " | grep -q /dev/md; do
60	umount $mntpoint || sleep 1
61done
62mdconfig -d -u $mdstart
63rm -f /tmp/nullfs22
64exit $status
65EOF
66#include <sys/param.h>
67#include <sys/mman.h>
68#include <sys/wait.h>
69
70#include <machine/atomic.h>
71
72#include <err.h>
73#include <errno.h>
74#include <fcntl.h>
75#include <signal.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
79#include <unistd.h>
80
81#define LOOPS 1024
82#define N (512)
83#define PARALLEL 4
84
85#define DONE 1
86#define SYNC 0
87
88int fd;
89volatile u_int *share;
90char name1[80], name2[80];
91
92static void
93ahandler(int s __unused)
94{
95	fprintf(stderr, "In alarm handler\n");
96	unlink(name1);
97	_exit(1);
98}
99
100void
101add(int n, int increment)
102{
103        struct flock fl;
104	off_t pos;
105	long val, oval __unused;
106	int r;
107
108	pos = n * sizeof(val);
109	memset(&fl, 0, sizeof(fl));
110        fl.l_start = pos;
111        fl.l_len = sizeof(val);
112        fl.l_type = F_WRLCK;
113        fl.l_whence = SEEK_SET;
114
115	while (fcntl(fd, F_SETLKW, &fl) < 0) {
116		if (errno != EAGAIN)
117			err(1, "F_SETLKW (child)");
118		usleep(100);
119	}
120
121	if (lseek(fd, pos, SEEK_SET) == -1)
122		err(1, "lseek");
123	oval = 999999;
124	while ((r = read(fd, &val, sizeof(val)) != sizeof(val))) {
125		if (r == -1 && errno != EAGAIN)
126			err(1, "read");
127		if (lseek(fd, pos, SEEK_SET) == -1)
128			err(1, "lseek");
129	}
130	oval = val;
131	val = val + increment;
132#if defined(DEBUG)
133	fprintf(stderr, "add(%d, %d) @ pos %ld: %ld = %ld + %d\n",
134	    n, increment, (long)pos, val, oval, increment);
135#endif
136	if (lseek(fd, pos, SEEK_SET) == -1)
137		err(1, "lseek");
138	while ((r = write(fd, &val, sizeof(val)) != sizeof(val))) {
139		if (r == -1 && errno != EAGAIN)
140			err(1, "write");
141		if (lseek(fd, pos, SEEK_SET) == -1)
142			err(1, "lseek");
143	}
144
145        fl.l_type = F_UNLCK;
146        if (fcntl(fd, F_SETLK, &fl) < 0)
147                err(1, "F_UNLCK");
148
149}
150
151void
152count(int val)
153{
154	int i, j;
155	char help[80], *name;
156
157	if (val == 1)
158		name = name1;
159	else
160		name = name2;
161	snprintf(help, sizeof(help), "%s %d %s", __func__, val, name);
162	setproctitle("%s", help);
163	atomic_add_int(&share[SYNC], 1);
164	while (share[SYNC] != 2 * PARALLEL)
165		;
166
167	/* Need to re-open after a fork() */
168	close(fd);
169	if ((fd = open(name, O_RDWR)) == -1)
170		err(1, "open(%s)", name);
171
172	for (i = 0; i < LOOPS; i++) {
173		for (j = 0; j < N; j++)
174			add(j, val);
175	}
176
177	atomic_add_int(&share[DONE], 1);
178
179	_exit(0);
180}
181
182int
183main(int argc, char *argv[])
184{
185	off_t len;
186	size_t mlen;
187	long val, sum;
188	int i, s, stat;
189
190	if (argc != 3) {
191		fprintf(stderr, "Usage: %s <dir1> <dir2>\n", argv[0]);
192		exit(1);
193	}
194
195	mlen = PAGE_SIZE;
196	if ((share = mmap(NULL, mlen, PROT_READ | PROT_WRITE,
197	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
198		err(1, "mmap");
199	snprintf(name1, sizeof(name1), "%s/work", argv[1]);
200	snprintf(name2, sizeof(name2), "%s/work", argv[2]);
201	signal(SIGALRM, ahandler);
202	alarm(300);
203	if ((fd = open(name1, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
204		err(1, "open(%s)", name1);
205	len = N * sizeof(val);
206	if (ftruncate(fd, len) == -1)
207		err(1, "ftruncate");
208
209	for (i = 0; i < PARALLEL; i++) {
210		if (fork() == 0)
211			count(1);
212	}
213	for (i = 0; i < PARALLEL; i++) {
214		if (fork() == 0)
215			count(-1);
216	}
217
218	while (share[DONE] != 2 * PARALLEL)
219		usleep(10000);
220
221	if (lseek(fd, 0, SEEK_SET) == -1)
222		err(1, "lseek");
223	sum = 0;
224	for (i = 0; i < N; i++) {
225		if (read(fd, &val, sizeof(val)) != sizeof(val))
226			err(1, "Final read");
227		if (val != 0)
228			fprintf(stderr, "index %d: %ld\n", i, val);
229		sum += val;
230	}
231	if (sum != 0)
232		fprintf(stderr, "FAIL\n");
233	unlink(name1);
234
235	s = 0;
236	for (i = 0; i < PARALLEL; i++) {
237		wait(&stat);
238		s += WEXITSTATUS(stat);
239		wait(&stat);
240		s += WEXITSTATUS(stat);
241	}
242
243	close(fd);
244
245	return (sum != 0 || s != 0);
246}
247