1#!/bin/sh
2# Copyright (c) 2015-2016 Oracle and/or its affiliates. All Rights Reserved.
3# Copyright (c) International Business Machines  Corp., 2001
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License as
7# published by the Free Software Foundation; either version 2 of
8# the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it would be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18VERSION=${VERSION:=3}
19NFILES=${NFILES:=1000}
20SOCKET_TYPE="${SOCKET_TYPE:-udp}"
21NFS_TYPE=${NFS_TYPE:=nfs}
22
23while getopts :ht:v:6 opt; do
24	case "$opt" in
25	h)
26		echo "Usage:"
27		echo "h        help"
28		echo "t x      socket type, tcp or udp, default is udp"
29		echo "v x      NFS version, default is '3'"
30		echo "6        run over IPv6"
31		exit 0
32	;;
33	v) VERSION=$OPTARG ;;
34	t) SOCKET_TYPE=$OPTARG ;;
35	6) # skip, test_net library already processed it
36	;;
37	*)
38		tst_brkm TBROK "unknown option: $opt"
39	;;
40	esac
41done
42
43get_socket_type()
44{
45	local t
46	local k=0
47	for t in $SOCKET_TYPE; do
48		if [ "$k" -eq "$1" ]; then
49			echo "${t}${TST_IPV6}"
50			return
51		fi
52		k=$(( k + 1 ))
53	done
54}
55
56nfs_setup()
57{
58	tst_check_cmds mount exportfs
59
60	tst_tmpdir
61
62	# Check if current filesystem is NFS
63	if [ "$(stat -f . | grep "Type: nfs")" ]; then
64		tst_brkm TCONF "Cannot run nfs-stress test on mounted NFS"
65	fi
66
67	local i
68	local type
69	local n=0
70	local opts
71	local local_dir
72	local remote_dir
73	local mount_dir
74	for i in $VERSION; do
75		type=$(get_socket_type $n)
76		tst_resm TINFO "setup NFSv$i, socket type $type"
77
78		local_dir="$TST_TMPDIR/$i/$n"
79		remote_dir="$TST_TMPDIR/$i/$type"
80
81		mkdir -p $local_dir
82
83		tst_rhost_run -c "test -d $remote_dir"
84		if [ "$?" -ne 0  ]; then
85			tst_rhost_run -s -c "mkdir -p $remote_dir"
86			tst_rhost_run -s -c "exportfs -i -o no_root_squash,rw \
87				*:$remote_dir"
88		fi
89
90		opts="-o proto=$type,vers=$i"
91
92		if [ $TST_IPV6 ]; then
93			mount_dir="[$(tst_ipaddr rhost)]:$remote_dir"
94		else
95			mount_dir="$(tst_ipaddr rhost):$remote_dir"
96		fi
97
98
99		tst_resm TINFO "Mounting NFS '$mount_dir'"
100		tst_resm TINFO "to '$local_dir' with options '$opts'"
101
102		ROD mount -t nfs $opts $mount_dir $local_dir
103
104		n=$(( n + 1 ))
105	done
106
107	if [ "$n" -eq 1 ]; then
108		cd ${VERSION}/0
109	fi
110}
111
112nfs_cleanup()
113{
114	tst_resm TINFO "Cleaning up testcase"
115	cd $LTPROOT
116
117	local i
118	local type
119	local local_dir
120	local remote_dir
121
122	local n=0
123	for i in $VERSION; do
124		local_dir="$TST_TMPDIR/$i/$n"
125		grep -q "$local_dir" /proc/mounts && umount $local_dir
126		n=$(( n + 1 ))
127	done
128
129	n=0
130	for i in $VERSION; do
131		type=$(get_socket_type $n)
132		remote_dir="$TST_TMPDIR/$i/$type"
133		tst_rhost_run -c "test -d $remote_dir && exportfs -u *:$remote_dir"
134		tst_rhost_run -c "test -d $remote_dir && rm -rf $remote_dir"
135		n=$(( n + 1 ))
136	done
137}
138