#!/bin/ksh
# @(#)67	1.13.1.10  src/avs/fs/mmfs/samples/nfscluster/start.nfs, mmfs, avs_rgpfs24, rgpfs24s010a 2/16/07 02:32:51
#
# start.nfs <GPFS_IP_ADDRESS>
#

if [ ! -f /var/mmfs/etc/nfsfuncs ]; then
    echo "$0: Can't find NFS functions in /var/mmfs/etc"
    exit 0
fi
. /var/mmfs/etc/nfsfuncs

myip=$1
[ "$myip" == "" ] && myip=$(myGPFSIP)
[ "$myip" != "" ] || die "Usage: start.nfs <GPFS IP address>"

debugmsg "start.nfs called with ip=$myip"

# Setup
rotatelog
_unlink /tmp/ha-nfs-reboot

configHA $myip
[ $? -ne 0 ] && die "start.nfs: configHA failed"

# did we mount any of the exported fs
isAnyExported
[ $? -ne 0 ] && die "start.nfs: mount failed"

# Clear any old locks in Linux.
resetLockd

# Set nfsd proc fs, used for grace control
debuglog mount -t nfsd nfsd /proc/fs/nfsd

# Stop NFSD before doing failback
debuglog rpc.nfsd 0

# Is this a failback?
[ $iptakeover -eq 1 ] && IPfailback $myip

# Bring up all nfs ip address
nfsips=$(getNfsIPs $myip)
msg "Enabling interface(s) for IP address(es) $nfsips"

typeset -i connections=0
while true; do
    for ip in $nfsips; do
	ifUp $ip
	iface=$(getifname $ip)
	checkLinkStatus $iface
	if [ $? -ne 1 ]; then
	    gwIP=$(route -n |awk '/UG/ {print $2}')
	    if [ -n "$gwIP" ]; then
		ping -c 1 -w 5 $gwIP > /dev/null
		if [ $? -ne 0 ]; then
		    debugmsg "Failed to ping the gateway at $gwIP"
		else
		    connections=1
		fi
	    else
		connections=1
	    fi
	fi
    done
    if [ $connections -eq 0 ]; then
	sleep 10
    else
	break
    fi
done

# Start nfs server.
debuglog umount /proc/fs/nfsd
nfsService start

_mkdir $SHARED_RECOVERY/$myip

# takeover nodes that were recovered by this node before failing
for failed_node in $(ls -A $SHARED_RECOVERY/$myip 2> /dev/null); do
    debugmsg "Do recovery for node $failed_node at $myip start up"
    IPtakeover $myip $failed_node
done

# takeover nodes that are not up
next=$(getNextNode $myip $myip)
debugmsg check other nodes starting with $next
while [ -n "$next" ]; do
    isNodeUp $next
    if [ $? -ne 0 ]; then
	# was already on my list and was handled above
	if [ ! -f $SHARED_RECOVERY/$myip/$next ]; then
	   isNodeUp $next
	   if [ $? -ne 0 ]; then
	      touch $SHARED_RECOVERY/$myip/$next
	      sleep 10
	      if [ -f $SHARED_RECOVERY/$myip/$next ]; then
	         isNodeUp $next
	         if [ $? -ne 0 ]; then
		    debugmsg "Do start recovery for node $next"
		    IPtakeover $myip $next
                    next=$(getNextNode $next $myip)
                    continue
	         fi
	      fi
	   fi
	fi
    fi
    break
done


# reclaim locks
startReclaim $myip
backupSmDir /tmp/statd

if [ $monitor -eq 1 ]; then
    nfsmonitor -s
fi
exit 0
