#!/bin/sh
#
#                       RESTRICTED RIGHTS LEGEND
# Use, duplication, or disclosure by the U.S. Government is subject to
# restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
# Technical Data and Computer Software clause in DFARS 252.227-7013.
#
# RSF-1 (c) Copyright High-Availability.Com Ltd, 1995-2016. All Rights Reserved.
#
#                       High-Availability.com Ltd
#                        Suite B, Pentland House
#                         Village Way, Wilmslow
#                           Cheshire  SK9 2GH
#                            United  Kingdom
#
#                   http://www.high-availability.com
#
#                   For UK support call 0844 736 1974
#                   Outside UK call +44 800 756 1198
#                (or email support@high-availability.com)
#
# Script:	S15mhdc
#
# Description:	ZFS/MHDC integration.
#
# Platform:	Unix
#
# Author:	High-Availability.Com Ltd/Paul/Griffiths-Todd
#
. /opt/HAC/bin/rsf.sh
. /opt/HAC/bin/mhd.sh
# This additional line was in the version held ion disk_reserve - not sure it's used but added anyway.
DISK_LIST=/opt/HAC/RSF-1/bin/disk_list

#
# Environment setup.  Need to keep service as a defined name as it's used
# in logging. The script name is used in dated_echo. Leave these settings
# defined here at the top.
service=${RSF_SERVICE:-"service_name"}
script="`basename $0`"

#
# For linux test if multipathing is enabled and the multipath
# version of MHDC is installed, yes to both = switch to version
# supporting multipath drives (as well as normal attached drives).
if [ "${PROP_LINUX_MPATH_ENABLED}" = "${BOOL_STR_TRUE}" -a -x "${PROG_MHDC_LINUX_MP}" ] ; then
    PROG_MHDC=${PROG_MHDC_LINUX_MP}
    dated_echo "MHDC: multipath version selected"
fi

#
# Standard script arguments:
#
#  $1 - State which should either be start or stop
#  $2 - Optional attempt number, default 1, or that passed in.
#	(RSF-1 increments this value when attempting multiple times)
#
State=${1%%=*}
attempt=${2:-'3'}

#
# Print out properties in effect.
dated_echo "Property settings in effect:"
echo "-----------------------------------------"
echo ""
cdb_print_prop
echo ""
echo "-----------------------------------------"
dated_echo "End properties."

case "${State}" in
start)
	#
	# Exit OK now if reservations are disabled
	#
	check_res_disabled ${RSF_OK}
	#
	# Take ownership so any other hosts issuing MHDC reserve will panic,
	# set fail fast (panic) should any other host reserve these disks,
	# finally release the reserve as we have fail fast now enabled.
	MHDC_START_TIME=`${DATE}`
	exit_code=${RSF_OK}

	#
	# Clear any leftover MHDC cache from before a crash
	mhdc_cache_clear ${service}

	#
	# Get res_drives list(s). The result will either be a single list (res_drives)
	# or for MetroHA, 2 lists - local_res_drives and remote_res_drives.
	check_for_reservation_drives ${service}

	${PROG_MHDC} -V

	if is_metroha; then
		if [ -z ${remote_res_drives}${local_res_drives} ]; then
			rc_exit ${service} ${RSF_SAFE} "Failed to obtain list of reservation drives"
		fi

		remote_failed=${BOOL_STR_FALSE}

		dated_echo "Reserving remote drives"
		if ! reserve_drives ${service} ${remote_res_drives}; then
			remote_failed=${BOOL_STR_TRUE}
			err="Failed to reserve remote drives"
			if [ ${METRO_HA_BROKEN_SITE_OVERRIDE} = ${BOOL_STR_TRUE} ]; then
				dated_echo "${err} - Override in place so not aborting" WARN
				exit_code=${RSF_WARN}
			elif [ ${PROP_DISABLE_SITE_SAFE} = ${BOOL_STR_TRUE} ]; then
				dated_echo "${err} - prop_disable_site_safe = ${PROP_DISABLE_SITE_SAFE} so not aborting"
				exit_code=${RSF_WARN}
			else
				abort_unfenced_if_prop_true ${service} ${RSF_SITE_SAFE} ${err}
				exit_code=${RSF_WARN}
			fi
		fi

		dated_echo "Reserving local drives"
		if ! reserve_drives ${service} ${local_res_drives}; then
			dated_echo "Failed to reserve local drives" WARN
			if [ ${remote_failed} = ${BOOL_STR_TRUE} -o -z ${remote_res_drives} ]; then
				abort_unfenced_if_prop_true ${service} ${RSF_ABORT} "No reservation drives have been taken"
				exit_code=${RSF_WARN}
			else
				dated_echo "Continuing service startup because remote reservation drives have been taken" WARN
				exit_code=${RSF_WARN}
			fi
		fi
	else
		if ! reserve_drives ${service} ${res_drives}; then
			abort_unfenced_if_prop_true ${service} ${RSF_ABORT} "Failed to reserve drives"
			exit_code=${RSF_WARN}
		fi
	fi

	MHDC_TAKE_TIME=`expr \`${DATE}\` - ${MHDC_START_TIME} `
    rc_exit ${service} ${exit_code} "Reservations taken in ${MHDC_TAKE_TIME} seconds"
	;;

stop|forcestop)
	#
	# Exit OK now if reservations are disabled
	#
	check_res_disabled ${RSF_OK}
	MHDC_START_TIME=`${DATE}`

	# Try to get drive lists from mhdc cache file first
	if [ -f ${MHDC_CACHE}/${service} ]; then
		mhdc_cache_read ${service} pgr3_discs s2_discs reserved_discs
		mhdc_cache_clear ${service}
	fi

	if [ -z "${s2_discs}" -a -z "${pgr3_discs}" ]; then
		# Failed to get lists from mhdc cache file.
		# Fall back to searching for disks in same way as for service start
		check_for_reservation_drives ${service}
		if [ "${PROP_MHDC_USE_PGR3}" = "${BOOL_STR_TRUE}" ]; then
			pgr3_discs=${res_drives}
		else
			s2_discs=${res_drives}
		fi
	else
		dated_echo "Reservation drives taken from cache file (${MHDC_CACHE}/${service})"
	fi

	dated_echo "SCSI-2 disks: ${s2_discs}"
	dated_echo "PGR3   disks: ${pgr3_discs}"

	# SCSI-2 release
	if [ -n "${s2_discs}" ]; then
		dated_echo "Releasing SCSI-2 reserve on device(s) ${s2_discs}..."
		release_drives ${service} SCSI-2 ${BOOL_STR_FALSE} ${s2_discs}
	fi

	# PGR3 release
	if [ -n "${pgr3_discs}" ]; then
		dated_echo "Releasing PGR3 reserve on device(s) ${pgr3_discs}..."
		release_drives ${service} PGR-3 ${BOOL_STR_FALSE} ${pgr3_discs}
	fi

	MHDC_RELEASE_TIME=`expr \`${DATE}\` - ${MHDC_START_TIME} `
	rc_exit ${service} ${RSF_OK} "Reservations released in ${MHDC_RELEASE_TIME} seconds"
	;;

check)
	#
	# Exit NORESRC now if reservations are disabled
	#
	check_res_disabled ${RSF_CHECK_NORESRC}
	#
	# Get the list of reservation drives - exit NORESRC if there are none
	#
	check_for_reservation_drives ${service} --with-devid
	if [ -z ${res_drives} ]; then
		if [ -z ${local_res_drives} -a -z ${remote_res_drives} ]; then
			exit ${RSF_CHECK_NORESRC}
		else
			res_drives="${local_res_drives} ${remote_res_drives}"
		fi
	fi
	#
	# Now check res drive status
	#
	${HAC_PYTHON} ${PRODUCT_BIN}/rc_check_res.pyc ${res_drives}

	exit $?
	;;

*)
	rc_exit ${service} ${RSF_WARN} "usage: $0 [start|stop|check] <attempts>"
	;;

esac
