#!/bin/ksh
# @(#)11	1.1  src/avs/fs/mmfs/samples/nfscluster/takeover, mmfs, avs_rgpfs24, rgpfs24s011a 3/7/07 20:57:09
#

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

forcedTakeover() {
    me=$1
    failed=$2
    typeset -i do_reclaim=0

    [ "$me" == "$failed" ] && return

    msg "Initiating forced IP takeover of $failed due to node failure"

    _mkdir ${SHARED_RECOVERY}/$me

    stopNode $failed
    nfsIPs=$(getNfsIPs $failed)
    debugmsg "forcedTakeover ips: $nfsIPs"
    for ip in $nfsIPs; do
        # Takeover IP and issue gratuitous ARP to the clients for the node
        # that failed so that clients can reconnect to the new address
	recoverNode $ip $failed
       	[ $? -eq 0 ] && do_reclaim=1
    done

    if [ $do_reclaim -ne 0 ]; then
	# got the IP
        debuglog touch ${SHARED_RECOVERY}/$me/$failed
    else
        # did not get IP, takeover failed, remove the entry
	_unlink ${SHARED_RECOVERY}/$me/$failed
	return
    fi
    debugmsg "forcedTakeover: File contents:"
    debugmsg $(ls -R ${SHARED_RECOVERY}/$me)
    _cp ${SHARED_NFS}/$failed/statd/sm/* ${SHARED_NFS}/$me/statd/sm
    checkDynamicGrace
    [ $? -eq 1 ] && startReclaim $failed
}
# Main

[ $# -eq 1 ] || die "Usage: $0 GPFS-IP"

node=$1
me=$(myGPFSIP)

# Check if failed node is a valid GPFS IP address
let valid=0
gpfsIPs=$(getAllGPFSIPs)
for ip in $gpfsIPs; do
    if [ "$ip" == "$node" ]; then
        valid=1
        break
    fi
done
[ $valid -eq 1 ] || die "$node is not a valid GPFS IP address"

debugmsg "Start forced node recovery for $node"
forcedTakeover $me $node
debuglog ls -A $SHARED_RECOVERY/$node
# recover other IPs that are serviced by the failed node
for ip in $(getFailedNodes $node); do
   debugmsg "Do 2nd recovery for node $ip"
   forcedTakeover $me $ip
   _unlink ${SHARED_RECOVERY}/$node/$ip
done
