xref: /freebsd/tools/test/stress2/misc/nfs12.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 Peter Holm <pho@FreeBSD.org>
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# "umount -f" test scenario (distill of nfs4.sh)
30# "panic: vputx: missed vn_close" seen.
31# Fixed in r248815
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37[ -z "$nfs_export" ] && exit 0
38ping -c 2 `echo $nfs_export | sed 's/:.*//'` > /dev/null 2>&1 ||
39    exit 0
40
41here=`pwd`
42cd /tmp
43sed '1,/^EOF/d' < $here/$0 > nfs12.c
44mycc -o nfs12 -Wall -Wextra -O2 -g nfs12.c
45rm -f nfs12.c
46cd $here
47
48mount | grep "on $mntpoint " | grep nfs > /dev/null && umount $mntpoint
49version="-o nfsv3"	# The default
50[ $# -eq 1 ] &&  [ "$1" -eq 4 ] && version="-o nfsv4"
51for i in `jot 10`; do
52	mount -t nfs $version -o tcp -o retrycnt=3 -o intr,soft -o rw \
53	    $nfs_export $mntpoint
54	sleep 2
55
56	if [ $i -eq 10 ]; then
57		rm -f $mntpoint/nfs12.p*
58	else
59		(cd $mntpoint; /tmp/nfs12 > /dev/null 2>&1) &
60		sleep 2
61	fi
62
63	while mount | grep "on $mntpoint " | grep -q nfs; do
64		umount -f $mntpoint
65	done
66	kill -9 $! > /dev/null 2>/dev/null && kill $!
67	wait
68done
69
70rm -f /tmp/nfs12
71exit
72EOF
73#include <sys/param.h>
74#include <sys/mman.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77
78#include <err.h>
79#include <errno.h>
80#include <fcntl.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <unistd.h>
85
86#define INPUTFILE "/bin/date"
87#define PARALLEL 5
88
89static int
90tmmap(void)
91{
92	struct stat statbuf;
93	pid_t pid;
94	char *src, *dst;
95	int i;
96	int fdin, fdout;
97	char file[128];
98
99	pid = getpid();
100	setproctitle("mmap");
101	for (i = 0; i < 50000; i++) {
102		sprintf(file,"nfs12.p%05d.%05d", pid, i);
103
104		if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
105			err(1, INPUTFILE);
106
107		if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
108			err(1, "%s", file);
109
110		if (fstat(fdin, &statbuf) < 0)
111			err(1, "fstat error");
112
113		if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
114			err(1, "lseek error");
115
116		/* write a dummy byte at the last location */
117		if (write(fdout, "", 1) != 1)
118			err(1, "write error");
119
120		if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
121			(caddr_t) - 1)
122			err(1, "mmap error for input");
123
124		if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
125			MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
126			err(1, "mmap error for output");
127
128		memcpy(dst, src, statbuf.st_size);
129
130		if (munmap(src, statbuf.st_size) == -1)
131			err(1, "munmap");
132		close(fdin);
133
134		if (munmap(dst, statbuf.st_size) == -1)
135			err(1, "munmap");
136		close(fdout);
137
138		if (unlink(file) == -1)
139			err(3, "unlink(%s)", file);
140	}
141
142	_exit(0);
143}
144
145int
146main(void)
147{
148        int i;
149
150	for (i = 0; i < PARALLEL; i++) {
151		if (fork() == 0)
152			tmmap();
153	}
154
155	for (i = 0; i < PARALLEL; i++) {
156		wait(NULL);
157	}
158
159	return (0);
160}
161