xref: /freebsd/tools/test/stress2/misc/nfs16.sh (revision 8a272653)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2016 EMC Corp.
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# Variation of nfs15.sh, using lockd(8).
308a272653SPeter Holm# "panic: Failed to register NFS lock locally - error=11" seen:
318a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik897.txt
328a272653SPeter Holm
338a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
348a272653SPeter Holm
358a272653SPeter Holm. ../default.cfg
368a272653SPeter Holm
378a272653SPeter Holm[ -z "$nfs_export" ] && exit 0
388a272653SPeter Holmping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
398a272653SPeter Holm    exit 0
408a272653SPeter Holm
418a272653SPeter Holmhere=`pwd`
428a272653SPeter Holmcd /tmp
438a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > nfs16.c
448a272653SPeter Holmmycc -o nfs16 -Wall -Wextra -O2 -g nfs16.c -lpthread || exit 1
458a272653SPeter Holmrm -f nfs16.c
468a272653SPeter Holmcd $here
478a272653SPeter Holm
488a272653SPeter Holmmount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
498a272653SPeter Holm
508a272653SPeter Holmmount -t nfs -o tcp -o retrycnt=3 -o soft -o rw \
518a272653SPeter Holm    $nfs_export $mntpoint
528a272653SPeter Holmsleep 2
538a272653SPeter Holmwd=$mntpoint/nfs16.dir
548a272653SPeter Holmrm -rf $wd
558a272653SPeter Holmmkdir $wd
568a272653SPeter Holm
578a272653SPeter Holm(cd $wd; /tmp/nfs16)
588a272653SPeter Holmrm -rf $wd
598a272653SPeter Holm
608a272653SPeter Holmwhile mount | grep "on $mntpoint " | grep -q nfs; do
618a272653SPeter Holm	umount $mntpoint || sleep 1
628a272653SPeter Holmdone
638a272653SPeter Holm
648a272653SPeter Holmrm -f /tmp/nfs16
658a272653SPeter Holmexit 0
668a272653SPeter HolmEOF
678a272653SPeter Holm#include <sys/stat.h>
688a272653SPeter Holm#include <sys/param.h>
698a272653SPeter Holm#include <sys/mman.h>
708a272653SPeter Holm#include <sys/wait.h>
718a272653SPeter Holm
728a272653SPeter Holm#include <machine/atomic.h>
738a272653SPeter Holm
748a272653SPeter Holm#include <err.h>
758a272653SPeter Holm#include <errno.h>
768a272653SPeter Holm#include <fcntl.h>
778a272653SPeter Holm#include <pthread.h>
788a272653SPeter Holm#include <signal.h>
798a272653SPeter Holm#include <stdio.h>
808a272653SPeter Holm#include <stdlib.h>
818a272653SPeter Holm#include <string.h>
828a272653SPeter Holm#include <unistd.h>
838a272653SPeter Holm
848a272653SPeter Holm#define PARALLEL 4
858a272653SPeter Holm#define RUNTIME 300
868a272653SPeter Holm#define SYNC 0
878a272653SPeter Holm
888a272653SPeter Holmvolatile u_int *share;
898a272653SPeter Holm
908a272653SPeter Holmstatic void *
918a272653SPeter Holmt1(void *data __unused)
928a272653SPeter Holm{
938a272653SPeter Holm	atomic_add_int(&share[SYNC], 1);
948a272653SPeter Holm	usleep(arc4random() % 8000);
958a272653SPeter Holm	raise(SIGABRT);
968a272653SPeter Holm
978a272653SPeter Holm	return (NULL);
988a272653SPeter Holm}
998a272653SPeter Holm
1008a272653SPeter Holmstatic void *
1018a272653SPeter Holmt2(void *data __unused)
1028a272653SPeter Holm{
1038a272653SPeter Holm	int fd, i, r;
1048a272653SPeter Holm	char file[80];
1058a272653SPeter Holm
1068a272653SPeter Holm	for (i = 0; i < 100; i++) {
1078a272653SPeter Holm		atomic_add_int(&share[SYNC], 1);
1088a272653SPeter Holm		snprintf(file, sizeof(file), "file.%06d", i);
1098a272653SPeter Holm		if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE)) == -1)
1108a272653SPeter Holm			err(1, "open(%s)", file);
1118a272653SPeter Holm		do {
1128a272653SPeter Holm			r = lockf(fd, F_LOCK, 0);
1138a272653SPeter Holm		} while (r == -1 && errno == EDEADLK);
1148a272653SPeter Holm		if (r == -1)
1158a272653SPeter Holm			err(1, "lockf(%s, F_LOCK)", file);
1168a272653SPeter Holm		write(fd, "x", 1);
1178a272653SPeter Holm		usleep(arc4random() % 1000);
1188a272653SPeter Holm		if (lseek(fd, 0, SEEK_SET) == -1)
1198a272653SPeter Holm			err(1, "lseek");
1208a272653SPeter Holm		if (lockf(fd, F_ULOCK, 0) == -1)
1218a272653SPeter Holm			err(1, "lockf(%s, F_ULOCK)", file);
1228a272653SPeter Holm		close(fd);
1238a272653SPeter Holm	}
1248a272653SPeter Holm
1258a272653SPeter Holm	return (NULL);
1268a272653SPeter Holm}
1278a272653SPeter Holm
1288a272653SPeter Holmint
1298a272653SPeter Holmtest(void)
1308a272653SPeter Holm{
1318a272653SPeter Holm	pthread_t tid[3];
1328a272653SPeter Holm	int i, rc;
1338a272653SPeter Holm
1348a272653SPeter Holm	for (i = 0; i < 10; i++) {
1358a272653SPeter Holm		if ((rc = pthread_create(&tid[0], NULL, t2, NULL)) == -1)
1368a272653SPeter Holm			errc(1, rc, "pthread_create");
1378a272653SPeter Holm		if ((rc = pthread_create(&tid[1], NULL, t2, NULL)) == -1)
1388a272653SPeter Holm			errc(1, rc, "pthread_create");
1398a272653SPeter Holm		if ((rc = pthread_create(&tid[2], NULL, t1, NULL)) == -1)
1408a272653SPeter Holm			errc(1, rc, "pthread_create");
1418a272653SPeter Holm
1428a272653SPeter Holm		if ((rc = pthread_join(tid[0], NULL)) == -1)
1438a272653SPeter Holm			errc(1, rc, "pthread_join");
1448a272653SPeter Holm		if ((rc = pthread_join(tid[1], NULL)) == -1)
1458a272653SPeter Holm			errc(1, rc, "pthread_join");
1468a272653SPeter Holm		if ((rc = pthread_join(tid[2], NULL)) == -1)
1478a272653SPeter Holm			errc(1, rc, "pthread_join");
1488a272653SPeter Holm	}
1498a272653SPeter Holm
1508a272653SPeter Holm	_exit(0);
1518a272653SPeter Holm}
1528a272653SPeter Holm
1538a272653SPeter Holmint
1548a272653SPeter Holmmain(void)
1558a272653SPeter Holm{
1568a272653SPeter Holm	pid_t pids[PARALLEL];
1578a272653SPeter Holm	size_t len;
1588a272653SPeter Holm	time_t start;
1598a272653SPeter Holm        int i, status;
1608a272653SPeter Holm
1618a272653SPeter Holm	len = PAGE_SIZE;
1628a272653SPeter Holm	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
1638a272653SPeter Holm	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
1648a272653SPeter Holm		err(1, "mmap");
1658a272653SPeter Holm
1668a272653SPeter Holm	start = time(NULL);
1678a272653SPeter Holm	while (time(NULL) - start < RUNTIME) {
1688a272653SPeter Holm		for (i = 0; i < PARALLEL; i++) {
1698a272653SPeter Holm			if ((pids[i] = fork()) == 0)
1708a272653SPeter Holm				test();
1718a272653SPeter Holm		}
1728a272653SPeter Holm
1738a272653SPeter Holm		for(;;) {
1748a272653SPeter Holm			if (share[SYNC] > 0)
1758a272653SPeter Holm				atomic_add_int(&share[SYNC], -1);
1768a272653SPeter Holm			for (i = 0; i < PARALLEL; i++)
1778a272653SPeter Holm				kill(pids[i], SIGSTOP);
1788a272653SPeter Holm			usleep(1000);
1798a272653SPeter Holm			for (i = 0; i < PARALLEL; i++)
1808a272653SPeter Holm				kill(pids[i], SIGCONT);
1818a272653SPeter Holm			usleep(100 + arc4random() % 400);
1828a272653SPeter Holm			if (share[SYNC] == 0) { /* If all procs are done */
1838a272653SPeter Holm				usleep(500);
1848a272653SPeter Holm				if (share[SYNC] == 0)
1858a272653SPeter Holm					break;
1868a272653SPeter Holm			}
1878a272653SPeter Holm		}
1888a272653SPeter Holm
1898a272653SPeter Holm		for (i = 0; i < PARALLEL; i++) {
1908a272653SPeter Holm			if (waitpid(pids[i], &status, 0) != pids[i])
1918a272653SPeter Holm				err(1, "waitpid");
1928a272653SPeter Holm		}
1938a272653SPeter Holm	}
1948a272653SPeter Holm
1958a272653SPeter Holm	return (0);
1968a272653SPeter Holm}
197