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. tst_net.sh 35 36get_socket_type() 37{ 38 local t 39 local k=0 40 for t in $SOCKET_TYPE; do 41 if [ "$k" -eq "$1" ]; then 42 echo "${t}${TST_IPV6}" 43 return 44 fi 45 k=$(( k + 1 )) 46 done 47} 48 49nfs_setup_server() 50{ 51 local export_cmd="exportfs -i -o no_root_squash,rw *:$remote_dir" 52 53 if [ -n "$LTP_NETNS" ]; then 54 if [ ! -d $remote_dir ]; then 55 mkdir -p $remote_dir 56 ROD $export_cmd 57 fi 58 return 59 fi 60 61 if ! tst_rhost_run -c "test -d $remote_dir"; then 62 tst_rhost_run -s -c "mkdir -p $remote_dir; $export_cmd" 63 fi 64} 65 66nfs_mount() 67{ 68 local host_type=rhost 69 local mount_dir 70 71 [ -n "$LTP_NETNS" ] && host_type= 72 73 if [ $TST_IPV6 ]; then 74 mount_dir="[$(tst_ipaddr $host_type)]:$remote_dir" 75 else 76 mount_dir="$(tst_ipaddr $host_type):$remote_dir" 77 fi 78 79 local mnt_cmd="mount -t nfs $opts $mount_dir $local_dir" 80 81 tst_res TINFO "Mounting NFS: $mnt_cmd" 82 if [ -n "$LTP_NETNS" ]; then 83 tst_rhost_run -s -c "$mnt_cmd" 84 return 85 fi 86 87 ROD $mnt_cmd 88} 89 90nfs_setup() 91{ 92 # Check if current filesystem is NFS 93 if [ "$(stat -f . | grep "Type: nfs")" ]; then 94 tst_brk TCONF "Cannot run nfs-stress test on mounted NFS" 95 fi 96 97 local i 98 local type 99 local n=0 100 local opts 101 local local_dir 102 local remote_dir 103 local mount_dir 104 105 for i in $VERSION; do 106 type=$(get_socket_type $n) 107 tst_res TINFO "setup NFSv$i, socket type $type" 108 109 local_dir="$TST_TMPDIR/$i/$n" 110 remote_dir="$TST_TMPDIR/$i/$type" 111 mkdir -p $local_dir 112 113 nfs_setup_server 114 115 opts="-o proto=$type,vers=$i" 116 nfs_mount 117 118 n=$(( n + 1 )) 119 done 120 121 if [ "$n" -eq 1 ]; then 122 cd ${VERSION}/0 123 fi 124} 125 126nfs_cleanup() 127{ 128 tst_res TINFO "Cleaning up testcase" 129 cd $LTPROOT 130 131 local i 132 local type 133 local local_dir 134 local remote_dir 135 136 local n=0 137 for i in $VERSION; do 138 local_dir="$TST_TMPDIR/$i/$n" 139 grep -q "$local_dir" /proc/mounts && umount $local_dir 140 n=$(( n + 1 )) 141 done 142 143 n=0 144 for i in $VERSION; do 145 type=$(get_socket_type $n) 146 remote_dir="$TST_TMPDIR/$i/$type" 147 tst_rhost_run -c "test -d $remote_dir && exportfs -u *:$remote_dir" 148 tst_rhost_run -c "test -d $remote_dir && rm -rf $remote_dir" 149 n=$(( n + 1 )) 150 done 151} 152