#!/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

echo -n "Isilon final-pass shutdown:"

# Let's kill checkjournal
if [ -e /var/run/isi_checkjournal.pid ]; then
	kill -QUIT `cat /var/run/isi_checkjournal.pid`
	echo -n " isi_checkjournal"
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.
	/usr/sbin/isi_kill_busy

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

	/usr/bin/isi_flush
	echo -n " isi_flush"

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

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

# Transition to read-only with system-shutdown bit set if this hasn't already
# been done. If it has, this call is harmless since we're already in read-only
# mode. This is necessary for proper behavior during "triple power button"
# shutdown.
echo -n " setting read-only with system-shutdown option"
/usr/bin/isi_hwtools/isi_read_only --set=system-shutdown

# Save the journal. This is also necessary for the "triple power button"
# shutdown. For normal CLI or WebUI shutdown, this should be redundant given
# the auto switch to read-only mode in our custom /sbin/shutdown script,
# but just to be sure (and just in case someone wants to disable that
# functionality altogether and get the previous, totally-fantastic-and-
# not-dangerous-in-any-way behviour, they can do so) we're going to make this
# call to isi_save_journal anyway. Regardless, isi_save_journal is smart
# enough to figure out if we need to save the journal.  If we need to save
# the journal we should ignore errors because it's the best we're going to get
# To log the journal save, this needs to be done before killing syslog
if [ -f /usr/sbin/isi_save_journal ]; then
	echo " isi_save_journal."
	/usr/bin/isi_run_script -s /usr/sbin/isi_save_journal
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 -n " isi_lock_drives"
		/usr/sbin/isi_lock_drives
	fi
fi
/bin/rm -f /etc/isi_keystore_unavailable

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

/usr/sbin/isi_down_internal_ifaces

sync
