#!/bin/sh

# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# KEYWORD: shutdown nostart
# PROVIDE: isi_post_shutdown

. /etc/rc.subr

cspace() {
	sysctl efs.bam.coalescer_stats | \
	    awk -F '[, \t] ' '/^Space allocated:/ { cspace = $3 }'
}

coalescer_flush() {
        local n
        local space

	sync

	# wait up to 30 seconds for coalescers to drain
	n=0
	space=$(cspace)
	while [ $n -lt 30 ] && [ -n "$space" ] && [ $space -gt 0 ] ; do
                n=$(( n + 1 ))
		sleep 1
		space=$(cspace)
		#echo "n $n, coalescer space $space"
	done
	echo "coalescer space $space"
}

echo "Isilon final-pass shutdown:"

# Let's kill checkjournal
if [ -e /var/run/isi_checkjournal.pid ]; then
	echo "Killing isi_checkjournal"
	kill -QUIT `cat /var/run/isi_checkjournal.pid`
fi

# Unmount ifs
/sbin/mount -p -t efs | grep efs 2>&1 >> /dev/null
if [ $? -eq 0 ] ;  then

	# Bugs 46700, 51950: we must down all frontend interfaces before
	# we start killing off external protocol daemons.
	/usr/sbin/isi_down_external_ifaces

	# XXXNED: For bug 19696, we must kill any advisory lock waiters before
	# unmount, or we'll deadlock.
	echo "Killing programs using /ifs files"
	/usr/sbin/isi_kill_busy

	# Bring down lwsm.  isi_kill_busy doesn't catch it and it will core
	# on shutdown
	echo "Shutting down lwsm"
	/usr/likewise/bin/lwsm shutdown

	# A triple-power-button shutdown bypasses the shutdown wrapper
	# and sends SIGINT to init from the kernel's ACPI code, and
	# the shutdown_real command bypasses the wrapper as well, so
	# we're not guaranteed that the wrapper put us in read-only
	# mode and saved a journal.  Replicate some of the work the
	# shutdown wrapper would do, so that we're in a similar state
	# by the time we unmount, regardless of how we got here.

	# Ensure the coalescers are disabled and flushed, which has
	# already happened if the shutdown wrapper was used.
	coalenabled="$(/sbin/sysctl -n efs.bam.coalescer.enabled)"
	if [ -n "$coealenabled" ] && [ "$coealenabled" != "0" ] ; then
		echo "Disable and flush coalescers"
		/sbin/sysctl efs.bam.coalescer.enabled=0
		coalescer_flush
	fi

	# If the shutdown wrapper hasn't already, set read-only, and
	# save a journal if we went from rw -> ro.  It's better to
	# save early, so that we have a saved journal even if one of
	# the shutdown timers expires and kills this script.  We might
	# fail to go read-only, but shutdown has already started and
	# we can't stop it, so continue even if it fails.
	echo "Attempting to go read-only"
	read_only_cmd="/usr/bin/isi_hwtools/isi_read_only --set=system-shutdown -v"
	/usr/bin/isi_run_script -s ${read_only_cmd} # >/dev/null 2>&1

	# Flush now, before unmount, since unmount holds locks and a
	# slow umount can cause rootlock timeout panics.
	/usr/bin/isi_flush --shutdown

	echo "Unmounting /ifs";
	/sbin/umount -f -a -t efs
fi

# Let's kill ibootd ourself
if [ -e /var/run/isi_boot_d.pid ]; then
	echo "Killing isi_boot_d"
	kill -QUIT `cat /var/run/isi_boot_d.pid`
fi

# Save the journal if we haven't already.  This handles cases where
# someone manually unmounts /ifs prior to shutdown.  If shutdown
# started with /ifs mounted, then either the shutdown wrapper or this
# script has already called isi_read_only and hopefully saved the
# journal. isi_save_journal is smart enough to notice a valid backup
# and not save the journal again.
if [ -f /usr/sbin/isi_save_journal ]; then
	echo "Running isi_save_journal"
	/usr/bin/isi_run_script -s /usr/sbin/isi_save_journal --disarm
fi

# don't kill syslog or down the IB interfaces if we're doing a disk firmware update
if [ -e /tmp/isi_disk_firmware_update ] ; then
	echo "leaving syslog and IB up for disk firmware upgrade"
	sync
	sleep 2
	return 0
fi

# Lock all the disks before going down, so long as the keystore is
# still available. If it's not, then we're in the process of finishing
# an isi_reformat_node.
if [ -e /usr/sbin/isi_lock_drives ]; then
	if [ ! -e /etc/isi_keystore_unavailable ]; then
		echo "Running isi_lock_drives"
		/usr/sbin/isi_lock_drives
	fi
fi
/bin/rm -f /etc/isi_keystore_unavailable

# Do a BMC firwmare update if desired
/usr/sbin/isi_bmc_firmware_update

# Need to kill syslog before we unmount /var
if [ -e /var/run/syslog.pid ]; then
	echo "Killing syslog"
	kill -TERM `cat /var/run/syslog.pid`
fi

/usr/sbin/isi_down_internal_ifaces

sync
