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

# We need to kill mountd before we unmount /ifs, else mountd will
# complain about the exports line.
killall -TERM mountd 2> /dev/null
echo -n " mountd"

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

	if [ -e /var/run/nfsd.pid ]; then
		if [ `sysctl -n vfs.nfsrv.rpc.shutdown_state` -lt 3 ]; then
			sysctl vfs.nfsrv.rpc.shutdown_state=1
			nfs_timer=0
			while [ `sysctl -n vfs.nfsrv.rpc.shutdown_state` -lt 3 -a $nfs_timer -lt 600 ]; do
				sleep 0.1
				nfs_timer=`expr $nfs_timer + 1`
			done
			if [ $nfs_timer -ge 600 ]; then
				echo "NFS timed out on shutdown state"
			fi
		fi
	fi

	# 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

	if [ -e /var/run/nfsd.pid ]; then
		# See nfsd(8) man page for details on USR1 instead of TERM.
		kill -USR1 `cat /var/run/nfsd.pid`
		echo -n " nfsd"
	fi

	# 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

# Save the journal. Theoretically, 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). 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
