#!/bin/bash
#
# Bring up/down NFSoRDMA support
#
# chkconfig: - 31 61
# description: Enables/Disables NFS over RDMA interfaces
# config:	/etc/rdma/rdma.conf
#
### BEGIN INIT INFO
# Provides:       nfs-rdma
# Default-Stop: 0 1 2 3 4 5 6
# Required-Start: $network $rdma $nfs
# Required-Stop: $nfs
# Short-Description: Enables/Disables NFS over RDMA interfaces
# Description: Enables/Disables NFS over RDMA interfaces
### END INIT INFO

CONFIG=/etc/rdma/rdma.conf

. /etc/rc.d/init.d/functions

LOAD_ULP_MODULES=""
if [ -f $CONFIG ]; then
    . $CONFIG

    if [ "${NFSoRDMA_LOAD}" == "yes" ]; then
	LOAD_ULP_MODULES="svcrdma xprtrdma"
    fi
    if [ -n "${NFSoRDMA_PORT}" ]; then
	PORT=${NFSoRDMA_PORT}
    else
	PORT=2050
    fi
fi

UNLOAD_ULP_MODULES="xprtrdma svcrdma"


# If module $1 is loaded return - 0 else - 1
is_module()
{
    /sbin/lsmod | grep -w "$1" > /dev/null 2>&1
    return $?    
}

load_modules()
{
    local RC=0

    for module in $*; do
	if ! is_module $module; then
	    /sbin/modprobe $module
	    res=$?
	    RC=$[ $RC + $res ]
	    if [ $res -ne 0 ]; then
		echo
		echo -n "Failed to load module $mod"
	    fi
	fi
    done
    return $RC
}

unload_module()
{
    local mod=$1
    # Unload module $1
    if is_module $mod; then
	/sbin/rmmod $mod > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    echo
	    echo "Failed to unload $mod"
	    return 1
	fi
    fi
    return 0
}

start()
{
    local RC=0
    local loaded=0

    echo -n "Enabling NFSoRDMA support:"

    load_modules $LOAD_ULP_MODULES
    RC=$[ $RC + $? ]

    if [ $RC -gt 0 ]; then
    	for mod in $UNLOAD_ULP_MODULES; do
	    unload_module $mod
	done
	echo_failure
	echo
	return $RC
    fi

    echo "rdma $PORT" > /proc/fs/nfsd/portlist
    sleep 1
    entry=$(grep rdma /proc/fs/nfsd/portlist)
    if [ -z "$entry" ]; then
    	for mod in $UNLOAD_ULP_MODULES; do
	    unload_module $mod
	done
	echo_failure
	echo
	return 1
    fi
 
    touch /var/lock/subsys/nfs-rdma
    echo_success
    echo
    return $RC    
}

stop()
{
    echo -n "Disabling NFSoRDMA support:"

    if ! is_module svcrdma; then
	# Nothing to do, make sure lock file is gone and return
	rm -f /var/lock/subsys/nfs-rdma
	echo_success
	echo
	return 0
    fi

    # Tell the nfs server to quit listening on the rdma port
    port=$(grep rdma /proc/fs/nfsd/portlist)
    if [ -n "$port" ]; then
	echo "-$port" > /proc/fs/nfsd/portlist
	# Small sleep to let nfsd process our request
	sleep 1
    fi

    # Unload NFSoRDMA modules
    for mod in $UNLOAD_ULP_MODULES
    do
	unload_module $mod
	RC=$[ $RC + $? ]
    done

    [ $RC -eq 0 ] && rm -f /var/lock/subsys/nfs-rdma
    [ $RC -eq 0 ] && echo_success || echo_failure
    echo
    return $RC
}

status()
{
    entry=$(grep rdma /proc/fs/nfsd/portlist)

    if [ -z "$entry" ]; then
	if [ -f /var/lock/subsys/nfs-rdma ]; then
	    return 2
	else
	    return 3
	fi
    else
	return 0
    fi
}

restart ()
{
    stop
    start
}

condrestart ()
{
    [ -e /var/lock/subsys/nfs-rdma ] && restart || return 0
}

usage ()
{
    echo
    echo "Usage: `basename $0` {start|stop|restart|condrestart|try-restart|force-reload|status}"
    echo
    return 2
}

case $1 in
    start|stop|restart|condrestart|try-restart|force-reload)
	[ `id -u` != "0" ] && exit 4 ;;
esac

case $1 in
    start) start; RC=$? ;;
    stop) stop; RC=$? ;;
    restart) restart; RC=$? ;;
    reload) RC=3 ;;
    condrestart) condrestart; RC=$? ;;
    try-restart) condrestart; RC=$? ;;
    force-reload) condrestart; RC=$? ;;
    status) status; RC=$? ;;
    *) usage; RC=$? ;;
esac

exit $RC
