#!/bin/sh

# Copyright C 2002-2004 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.

# PROVIDE: isi_post_ifs
# REQUIRE: isi_mount_ifs

. /etc/rc.subr

# This script can be run from either rc or from the isi_group_change_d daemon
# when nodes join/rejoin the cluster

# Default permissions for /ifs, /ifs/.ifsvar, and /ifs/data
PERM=777
IFSVAR_PERM=775

# Try a couple of times to make sure ifs is mounted before running
skip=0
/sbin/mount -p -t efs | /usr/bin/grep efs 2>&1 >> /dev/null
if [ $? -ne 0 ] ;  then
  /usr/bin/logger -i -p ifs.err "Attempting to run isi_post_ifs when IFS is not mounted.";
  count=0
  while [ $count -lt 5 ]; do
    count=$(($count + 1))
    /sbin/mount -p -t efs | /usr/bin/grep efs 2>&1 >> /dev/null
    if [ $? -eq 0 ]; then
      break
    fi
    /bin/sleep 1
  done
  /usr/bin/logger -i -p ifs.err "IFS still unmounted. Not performing ifs operations."
  skip=1
fi

# Some sysctls could be efs specific. Should be no harm in reseting them all.

if [ -r /etc/sysctl.conf ]; then
	/bin/sh /etc/rc.d/sysctl start
fi

# set the efs.bam.layout.directories_at_one_higher sysctl
/etc/mcp/scripts/gconfig.py

# Only perform operations on /ifs if ifsvar is ready
# Check if ifsvar is ready

/usr/bin/isi_ifsvar_ready
ifsvar_ready=$?

if [ ${ifsvar_ready} -eq 0 -a ${skip} -ne 1 ]; then


    # Root inode starts at 1x on new fs, so reprotect ASAP.
    isi set -r /ifs

    # Create the .ifsvar directory
    if [ ! -d /ifs/.ifsvar ]; then
        /bin/mkdir -p /ifs/.ifsvar
        if [ $? -eq 0 ]; then
            # The smartpools apply behavior for /ifs/.ifsvar is hardcoded and
            # doesn't need configuration
            /usr/bin/isi filepool apply -q /ifs/.ifsvar
            /bin/chmod ${IFSVAR_PERM} /ifs/.ifsvar
            /bin/chflags sunlnk /ifs/.ifsvar
        fi
    fi
    
    # Check for the magic file that tells us that permissions on ifs have been
    # set and if not, set.
    if [ ! -f /ifs/.ifsvar/ifs/default_perms_set ]; then
        chmod ${PERM} /ifs
        if /bin/mkdir -p /ifs/.ifsvar/ifs; then
            ( echo "magic file for isi_post_ifs" > \
                /ifs/.ifsvar/ifs/default_perms_set )
            chmod 640 /ifs/.ifsvar/ifs/default_perms_set
        fi
    fi
    # Create an IFS data directory in accordance with bug 6474

    if [ ! -d /ifs/data ]; then
        /bin/mkdir -p /ifs/data  
        if [ $? -eq 0 ]; then
            /bin/chmod ${PERM} /ifs/data
            /usr/bin/isi set -Fc on -A off /ifs/data 
            if [ -e /etc/README.Isilon -a ! -e /ifs/README.txt ]; then
                /bin/cp /etc/README.Isilon /ifs/README.txt
            fi
        fi
    fi

    # Check for the magic file that tells us that coalescing has been set.
    # If it is not there set coalescing for /ifs.
    if [ ! -f /ifs/.ifsvar/ifs/default_coalescing_set ]; then
        /usr/bin/isi set -Fc on -A off /ifs
        if /bin/mkdir -p /ifs/.ifsvar/ifs; then
            ( echo "magic file for isi_post_ifs" > \
                /ifs/.ifsvar/ifs/default_coalescing_set )
            chmod 640 /ifs/.ifsvar/ifs/default_coalescing_set
        fi
    fi

    # Create the run subdirectory to store wait-for-master locks
    if [ ! -d /ifs/.ifsvar/run ]; then
        /bin/mkdir -p /ifs/.ifsvar/run
        if [ $? -eq 0 ]; then
            /bin/chmod 755 /ifs/.ifsvar/run
        fi
    fi

    # Create the etc subdirectory since we need it below
    if [ ! -d /ifs/.ifsvar/etc ]; then
        /bin/mkdir -p /ifs/.ifsvar/etc
        if [ $? -eq 0 ]; then
            /bin/chmod 755 /ifs/.ifsvar/etc
        fi
    fi

    # Create the usr/local/private subdirectory since we need it as well
    if [ ! -d /ifs/.ifsvar/usr/local/private ]; then
        /bin/mkdir -p /ifs/.ifsvar/usr/local/private
        if [ $? -eq 0 ]; then
            /bin/chmod 755 /ifs/.ifsvar/usr/local/private
        fi
    fi

    if [ ! -d /ifs/home ]; then
        /bin/mkdir -p /ifs/home
	if [ $? -eq 0 ]; then
            /bin/chmod 775 /ifs/home
        fi
    fi

    # Create user directories and protect
    if [ ! -d /ifs/home/admin ]; then
        # protect /ifs/home
        /bin/mkdir -p /ifs/home/admin
        if [ $? -eq 0 ]; then
            /bin/cp /usr/share/skel/dot.zshrc /ifs/home/admin/.zshrc
            /bin/chmod u+wrX,go-w /ifs/home/admin/.zshrc 
            /usr/sbin/chown -R admin:admin /ifs/home/admin
        fi
    fi
    
    # Create anonymous FTP home directory
    if [ ! -d /ifs/home/ftp ]; then
        /bin/mkdir -p /ifs/home/ftp
        if [ $? -eq 0 ]; then
            # add internal ftp directories
            /bin/mkdir -p /ifs/home/ftp/incoming
            /bin/mkdir -p /ifs/home/ftp/pub
            # change ownership and permissions
            /usr/sbin/chown -R root:wheel /ifs/home/ftp
            /bin/chmod -R 755 /ifs/home/ftp
            # write and execute perms on anonymous incoming directory
            /bin/chmod 753 /ifs/home/ftp/incoming
        fi
    fi

    # Create dirs relied upon by SyncIQ, and move files from Cherry locations
    REPLDIR="/ifs/.ifsvar/modules/replication"
    [ -d $REPLDIR/repeat ] || /bin/mkdir -p $REPLDIR/repeat
    for f in replicate.conf bandwidth.conf throttle.conf; do
        if [ -f $REPLDIR/$f ]; then
            mv $REPLDIR/$f /etc/mcp/sync/$f
        fi
    done
    
    # Create SyncIQ configuration directory
    SIQCONFIGDIR="/ifs/.ifsvar/modules/tsm/config"
    if [ ! -d $SIQCONFIGDIR ]; then
        /bin/mkdir -p $SIQCONFIGDIR
    fi

    # Created run directory on /ifs for cluster wide run state.
    if [ ! -d /ifs/.ifsvar/run ]; then
        /bin/mkdir /ifs/.ifsvar/run
    fi

    # Create dirs relied upon by Quotas and copy necessary files.
    QUOTASDIR="/ifs/.ifsvar/modules/quotas"
    [ -d $QUOTASDIR/qnr ] || /bin/mkdir -p $QUOTASDIR/qnr
    [ -d $QUOTASDIR/qgc ] || /bin/mkdir -p $QUOTASDIR/qgc
    [ -d $QUOTASDIR/notify ] || /bin/mkdir -p $QUOTASDIR/notify
    if [ ! -f $QUOTASDIR/qnr/qnr_map_default.xml ]; then
        cp /etc/mcp/sys/qnr_map_default.xml $QUOTASDIR/qnr
        chmod 444 $QUOTASDIR/qnr/qnr_map_default.xml
    fi
    if [ ! -f $QUOTASDIR/qgc/qgc_default.xml ]; then
        cp /etc/mcp/sys/qgc_default.xml $QUOTASDIR/qgc
        chmod 444 $QUOTASDIR/qgc/qgc_default.xml
    fi

    RESTRIPERDIR="/ifs/.ifsvar/modules/restriper"
    [ -d $RESTRIPERDIR ] || /bin/mkdir -p $RESTRIPERDIR

    JOBDIR="/ifs/.ifsvar/modules/jobengine"
    [ -d $JOBDIR ] || /bin/mkdir -p $JOBDIR

    FLEXNETDIR="/ifs/.ifsvar/modules/flexnet"
    [ -d $FLEXNETDIR ] || /bin/mkdir -p $FLEXNETDIR

    # Create anti-virus scanning directory
    AVSCANDIR="/ifs/.ifsvar/modules/avscan"
    [ -d $AVSCANDIR ] || /bin/mkdir -p $AVSCANDIR
    
    # Create smartpools directory
    SMARTPOOLSDIR="/ifs/.ifsvar/modules/smartpools"
    [ -d $SMARTPOOLSDIR ] || /bin/mkdir -p $SMARTPOOLSDIR
    
    # Create celog directory
    CELOGDIR="/ifs/.ifsvar/modules/celog"
    [ -d $CELOGDIR ] || /bin/mkdir -p $CELOGDIR

    # Create auth configuration directory
    AUTHCONFIGDIR="/ifs/.ifsvar/modules/auth"
    if [ ! -d $AUTHCONFIGDIR ]; then
        /bin/mkdir -p $AUTHCONFIGDIR
        /bin/chmod 700 $AUTHCONFIGDIR
    fi
    AUTHSAMCONFIGDIR="/ifs/.ifsvar/modules/auth/sam"
    if [ ! -d $AUTHSAMCONFIGDIR ]; then
        /bin/mkdir -p $AUTHSAMCONFIGDIR
        /bin/chmod 700 $AUTHSAMCONFIGDIR
        /usr/bin/isi set -c on -F $AUTHSAMCONFIGDIR
    fi

    # Create krb5 configuration directory
    KRB5CONFIGDIR="/ifs/.ifsvar/modules/krb5"
    [ -d $KRB5CONFIGDIR ] || /bin/mkdir -p $KRB5CONFIGDIR

    # Create httpd configuration directory
    HTTPCONFIGDIR="/ifs/.ifsvar/modules/http"
    [ -d $HTTPCONFIGDIR ] || /bin/mkdir -p $HTTPCONFIGDIR

    # Create cbind directory
    CBINDDIR="/ifs/.ifsvar/modules/cbind"
    [ -d $CBINDDIR ] || /bin/mkdir -p $CBINDDIR

    # Create dedupe/bedrs directory
    BEDRSDIR="/ifs/.ifsvar/modules/bedrs"
    [ -d $BEDRSDIR ] || /bin/mkdir -p $BEDRSDIR

    # Create default NFS configuration file, and upgrade exports
    # 6.5 has new exports prototype for defaults, set in nfs_default.xml
    NFSDIR="/ifs/.ifsvar/modules/nfs"
    UNAME=`/usr/bin/uname -n`
    NFSTMPXML=$NFSDIR/nfs_default_upgrade-$UNAME.xml
    [ -d $NFSDIR ] || /bin/mkdir -p $NFSDIR
    /bin/cat > $NFSTMPXML << HERE
<?xml version="1.0" ?>
<nfs>
  <export comment="Export option defaults" type="prototype">
    <isi-meta-tag id="nfsdata0" can-delete="yes"/>
    <options>
      <all_dirs>false</all_dirs>
      <bsize>8192</bsize>
      <cansettime>true</cansettime>
      <commit_asynchronous>false</commit_asynchronous>
      <dtpref>131072</dtpref>
      <encoding>default</encoding>
      <map_groups></map_groups>
      <map_lookup_uid>false</map_lookup_uid>
      <map_nogroups>true</map_nogroups>
      <map_retry>true</map_retry>
      <map_type>root</map_type>
      <map_user>nobody</map_user>
      <map_user_full>true</map_user_full>
      <maxfilesize>9223372036854775807</maxfilesize>
      <read_only>false</read_only>
      <readdirplus_enable>true</readdirplus_enable>
      <readdirplus_prefetch>10</readdirplus_prefetch>
      <return_32bit_file_ids>false</return_32bit_file_ids>
      <rtmax>1048576</rtmax>
      <rtmult>512</rtmult>
      <rtpref>131072</rtpref>
      <security_flavors>sys</security_flavors>
      <setattr_asynchronous>false</setattr_asynchronous>
      <symlink_supported>true</symlink_supported>
      <time_delta>0.000000001</time_delta>
      <write_datasync_action>datasync</write_datasync_action>
      <write_datasync_reply>datasync</write_datasync_reply>
      <write_filesync_action>filesync</write_filesync_action>
      <write_filesync_reply>filesync</write_filesync_reply>
      <write_unstable_action>unstable</write_unstable_action>
      <write_unstable_reply>unstable</write_unstable_reply>
      <wtmax>1048576</wtmax>
      <wtmult>512</wtmult>
      <wtpref>524288</wtpref>
    </options>
  </export>

  <isi-meta-tag id="nfsdata1" can-add-child="yes"/>
  <export comment="Default export" id="1" type="export">
    <isi-meta-tag id="nfsdata2" can-delete="yes"/>
    <options>
      <all_dirs>true</all_dirs>
    </options>
    <path>/ifs</path>
  </export>
</nfs>
HERE

    # Only overwrite the defaults if they have been altered, as this HUPs mountd
    if ! /usr/bin/cmp -s $NFSTMPXML $NFSDIR/nfs_default.xml > /dev/null 2>&1; then
	/usr/bin/logger -i -p ifs.info "NFS: Updating nfs_default.xml"
	/bin/mv $NFSTMPXML $NFSDIR/nfs_default.xml
	/bin/chmod 0640 $NFSDIR/nfs_default.xml
    fi

    if [ -f /etc/exports ]; then
        EXPORTS=$NFSDIR/exports.`/usr/bin/uname -n | awk -F- '{print $(NF)}'`
        /bin/mv /etc/exports $EXPORTS
        if [ ! -f $NFSDIR/nfs_override.xml ]; then
            /usr/bin/isi_exports_upgrade $EXPORTS
        fi
    fi

    # Create the NFSv4 stable file
    if [ ! -f /var/db/nfs-stablerestart ]; then
        touch /var/db/nfs-stablerestart
        chmod 640 /var/db/nfs-stablerestart
    fi

    # Create dirs relied on by FSA
    FSADIR="/ifs/.ifsvar/modules/fsa"
    if [ ! -d $FSADIR ]; then
	/bin/mkdir -p $FSADIR
    fi
    
    # Create dirs relied on by stats
    STATSDIR="/ifs/.ifsvar/modules/stats"
    if [ ! -d $STATSDIR ]; then
	/bin/mkdir -p -m0770 $STATSDIR
    fi

    # Create the etc/ifs subdirectory since we need it below
    if [ ! -d /ifs/.ifsvar/etc/ifs ]; then
	/bin/mkdir /ifs/.ifsvar/etc/ifs
    fi

    if [ ! -d /ifs/.ifsvar/etc/mcp ]; then
	/bin/mkdir /ifs/.ifsvar/etc/mcp
    fi

    if [ ! -d /ifs/.ifsvar/tmp ]; then
	/bin/mkdir /ifs/.ifsvar/tmp
    fi

    if [ ! -d /ifs/.ifsvar/db ]; then
	/bin/mkdir /ifs/.ifsvar/db
    fi
    if [ ! -d /ifs/.ifsvar/db/pkg ]; then
	/bin/mkdir /ifs/.ifsvar/db/pkg
    fi
    if [ ! -d /ifs/.ifsvar/db/celog ]; then
	/bin/mkdir /ifs/.ifsvar/db/celog
    fi
    
    if [ ! -d /ifs/.ifsvar/modules/ndmp ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/ndmp
	cp /etc/mcp/sys/ndmp_default.xml /ifs/.ifsvar/modules/ndmp/config.xml
	chmod 0640 /ifs/.ifsvar/modules/ndmp/config.xml
	/bin/mkdir -p /ifs/.ifsvar/modules/ndmp/contexts
	cp /etc/mcp/sys/ndmp_context_list_default.xml /ifs/.ifsvar/modules/ndmp/context_list.xml
	chmod 0640 /ifs/.ifsvar/modules/ndmp/context_list.xml
    fi

    if [ ! -d /ifs/.ifsvar/modules/fc ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/fc
	cp /etc/mcp/sys/fc_default.xml /ifs/.ifsvar/modules/fc/devices.xml
	chmod 400 /ifs/.ifsvar/modules/fc/devices.xml
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc/locks ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc/locks
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc/gconfig ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc/gconfig
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_vc/jobs ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_vc/jobs
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_sra ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_sra
    fi

    if [ ! -d /ifs/.ifsvar/modules/isi_sra/gconfig ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_sra/gconfig
    fi

    # Used by snapshot PAPI handler and snapshot daemon, snapshot_manage.h.
    if [ ! -d /ifs/.ifsvar/snapshots ]; then
	/bin/mkdir -p /ifs/.ifsvar/snapshots
    fi

    # Used by snapshot daemon.
    if [ ! -d /ifs/.ifsvar/modules/isi_snapshots ]; then
	/bin/mkdir -p /ifs/.ifsvar/modules/isi_snapshots
    fi

    # If this is a backup accelerator, perform FC device discovery.
    if [ -e /dev/isp0 ]; then
	/usr/bin/isi_fc_rescan
    fi

    # Signal rpc.lockd to check to see if there's a non-default value
    # for its RPC timeout. The lockd RPC timeout variable is stored in
    # /ifs/.ifsvar/etc/lockd_rpc_timeout
    # this is no longer supported with our new NLM in 5.5
    # /usr/bin/killall -USR1 rpc.lockd > /dev/null 2>&1

    # Update the hardware info cache
    echo "import isi.sys.version as v; v.update_cache()" | /usr/bin/python

    # Update cached ABR for CTO nodes
    /usr/bin/isi_hwtools/isi_hal_predicate supportsCto > /dev/null 2>&1
    supports_cto=$?
    if [ ${supports_cto} -eq 1 ]; then
        /usr/bin/isi_inventory_tool --cache --fileType=asbuilt
    fi

    # Make sure we're not in an auto-suspend pool (unless we're updating)
    /usr/bin/isi_sc_auto_resume > /dev/null 2>&1
    
    # This will make the update lock files if needed
    if [ ! -e /ifs/.ifsvar/isi_update_slash_pkg.status -o \
            ! -e /ifs/.ifsvar/isi_update_slash_pkg.lock ]; then
	/bin/cat /dev/null | /usr/sbin/isi_grab_update_lock > /dev/null
    fi

    # SmartPools Upgrade
    if [ -f /usr/libdata/patchmgr/smartpools-upgrade ]; then
        /usr/libdata/patchmgr/smartpools-upgrade
    fi

    # For 6.0 initialize Smartpools.
    if [ -f /usr/libdata/patchmgr/init_smartpools ]; then
        /usr/libdata/patchmgr/init_smartpools
    fi

    # Update CELOG settings with support notification changes, if any
    /usr/bin/isi_promptsupport -c

    # Run one-time upgrade script after IFS is mounted.  
    # ***Should be kept at the end of isi_post_ifs***
    /usr/sbin/isi_first_post_ifs
    
fi

# These bits below get executed whether or not IFS was mounted
# which begs the question of why they are here instead of somewhere else

# Make sure things that are based on the list of group membership 
# are taken care of.
/usr/libexec/isilon/on_group_change

# backup the drives.xml file only on storage node
/usr/bin/isi_run_script -s /usr/sbin/isi_backup /etc/ifs/drives.xml

# Run isi_bootdisk_finish every time
/usr/sbin/isi_bootdisk_finish
