• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015-2018 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) International Business Machines  Corp., 2001
5
6VERSION=${VERSION:=3}
7NFILES=${NFILES:=1000}
8SOCKET_TYPE="${SOCKET_TYPE:-udp}"
9NFS_TYPE=${NFS_TYPE:=nfs}
10
11nfs_usage()
12{
13	echo "-t x    Socket type, tcp or udp, default is udp"
14	echo "-v x    NFS version, default is '3'"
15}
16
17nfs_parse_args()
18{
19	case "$1" in
20	v) VERSION="$(echo $2 | tr ',' ' ')";;
21	t) SOCKET_TYPE="$(echo $2 | tr ',' ' ')";;
22	esac
23}
24
25TST_OPTS="v:t:"
26TST_PARSE_ARGS=nfs_parse_args
27TST_USAGE=nfs_usage
28TST_NEEDS_TMPDIR=1
29TST_NEEDS_ROOT=1
30TST_NEEDS_CMDS="$TST_NEEDS_CMDS mount exportfs"
31TST_SETUP="${TST_SETUP:-nfs_setup}"
32TST_CLEANUP="${TST_CLEANUP:-nfs_cleanup}"
33
34# When set and test is using netns ($TST_USE_NETNS set) NFS traffic will go
35# through lo interface instead of ltp_ns_veth* netns interfaces (useful for
36# debugging whether test failures are related to veth/netns).
37LTP_NFS_NETNS_USE_LO=${LTP_NFS_NETNS_USE_LO:-}
38
39. tst_net.sh
40
41get_socket_type()
42{
43	local t
44	local k=0
45	for t in $SOCKET_TYPE; do
46		if [ "$k" -eq "$1" ]; then
47			echo "${t}${TST_IPV6}"
48			return
49		fi
50		k=$(( k + 1 ))
51	done
52}
53
54nfs_server_udp_enabled()
55{
56	local config f
57
58	tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
59	config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ] && printf "$f "; done')
60
61	tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"
62}
63
64nfs_setup_server()
65{
66	local export_cmd="exportfs -i -o fsid=$$,no_root_squash,rw *:$remote_dir"
67
68	if ! tst_rhost_run -c "test -d $remote_dir"; then
69		tst_rhost_run -s -c "mkdir -p $remote_dir; $export_cmd"
70	fi
71}
72
73nfs_mount()
74{
75	local host_type=rhost
76	local mount_dir
77
78	[ -n "$LTP_NETNS" ] && host_type=
79
80	if [ $TST_IPV6 ]; then
81		mount_dir="[$(tst_ipaddr $host_type)]:$remote_dir"
82	else
83		mount_dir="$(tst_ipaddr $host_type):$remote_dir"
84	fi
85
86	local mnt_cmd="mount -t nfs $opts $mount_dir $local_dir"
87
88	tst_res TINFO "Mounting NFS: $mnt_cmd"
89	if [ -n "$LTP_NETNS" ] && [ -z "$LTP_NFS_NETNS_USE_LO" ]; then
90		tst_rhost_run -c "$mnt_cmd"
91	else
92		$mnt_cmd > /dev/null
93	fi
94
95	if [ $? -ne 0 ]; then
96		if [ "$type" = "udp" -o "$type" = "udp6" ] && tst_kvcmp -ge 5.6; then
97			tst_brk TCONF "UDP support disabled with the kernel config NFS_DISABLE_UDP_SUPPORT?"
98		fi
99		tst_brk TBROK "mount command failed"
100	fi
101}
102
103nfs_setup()
104{
105	# Check if current filesystem is NFS
106	if [ "$(stat -f . | grep "Type: nfs")" ]; then
107		tst_brk TCONF "Cannot run nfs-stress test on mounted NFS"
108	fi
109
110	local i
111	local type
112	local n=0
113	local opts
114	local local_dir
115	local remote_dir
116	local mount_dir
117
118	for i in $VERSION; do
119		type=$(get_socket_type $n)
120		tst_res TINFO "setup NFSv$i, socket type $type"
121
122		if [ "$type" = "udp" -o "$type" = "udp6" ] && ! nfs_server_udp_enabled; then
123			tst_brk TCONF "UDP support disabled on NFS server"
124		fi
125
126		local_dir="$TST_TMPDIR/$i/$n"
127		remote_dir="$TST_TMPDIR/$i/$type"
128		mkdir -p $local_dir
129
130		nfs_setup_server
131
132		opts="-o proto=$type,vers=$i"
133		nfs_mount
134
135		n=$(( n + 1 ))
136	done
137
138	if [ "$n" -eq 1 ]; then
139		cd ${VERSION}/0
140	fi
141}
142
143nfs_cleanup()
144{
145	tst_res TINFO "Cleaning up testcase"
146	cd $LTPROOT
147
148	local i
149	local type
150	local local_dir
151	local remote_dir
152
153	local n=0
154	for i in $VERSION; do
155		local_dir="$TST_TMPDIR/$i/$n"
156		grep -q "$local_dir" /proc/mounts && umount $local_dir
157		n=$(( n + 1 ))
158	done
159
160	n=0
161	for i in $VERSION; do
162		type=$(get_socket_type $n)
163		remote_dir="$TST_TMPDIR/$i/$type"
164		tst_rhost_run -c "test -d $remote_dir && exportfs -u *:$remote_dir"
165		tst_rhost_run -c "test -d $remote_dir && rm -rf $remote_dir"
166		n=$(( n + 1 ))
167	done
168}
169