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 -s -c "$mnt_cmd" 91 return 92 fi 93 94 ROD $mnt_cmd 95} 96 97nfs_setup() 98{ 99 # Check if current filesystem is NFS 100 if [ "$(stat -f . | grep "Type: nfs")" ]; then 101 tst_brk TCONF "Cannot run nfs-stress test on mounted NFS" 102 fi 103 104 local i 105 local type 106 local n=0 107 local opts 108 local local_dir 109 local remote_dir 110 local mount_dir 111 112 for i in $VERSION; do 113 type=$(get_socket_type $n) 114 tst_res TINFO "setup NFSv$i, socket type $type" 115 116 if [ "$type" = "udp" -o "$type" = "udp6" ] && ! nfs_server_udp_enabled; then 117 tst_brk TCONF "UDP support disabled on NFS server" 118 fi 119 120 local_dir="$TST_TMPDIR/$i/$n" 121 remote_dir="$TST_TMPDIR/$i/$type" 122 mkdir -p $local_dir 123 124 nfs_setup_server 125 126 opts="-o proto=$type,vers=$i" 127 nfs_mount 128 129 n=$(( n + 1 )) 130 done 131 132 if [ "$n" -eq 1 ]; then 133 cd ${VERSION}/0 134 fi 135} 136 137nfs_cleanup() 138{ 139 tst_res TINFO "Cleaning up testcase" 140 cd $LTPROOT 141 142 local i 143 local type 144 local local_dir 145 local remote_dir 146 147 local n=0 148 for i in $VERSION; do 149 local_dir="$TST_TMPDIR/$i/$n" 150 grep -q "$local_dir" /proc/mounts && umount $local_dir 151 n=$(( n + 1 )) 152 done 153 154 n=0 155 for i in $VERSION; do 156 type=$(get_socket_type $n) 157 remote_dir="$TST_TMPDIR/$i/$type" 158 tst_rhost_run -c "test -d $remote_dir && exportfs -u *:$remote_dir" 159 tst_rhost_run -c "test -d $remote_dir && rm -rf $remote_dir" 160 n=$(( n + 1 )) 161 done 162} 163