#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2005,2006 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)84 1.10.1.4 src/avs/fs/mmfs/ts/admin/mmchpolicy.sh, mmfs, avs_rgpfs24, rgpfs24s005a 6/14/06 14:39:55
#############################################################################
#
# Usage:
#   mmchpolicy Device PolicyFilename [-t DescriptiveName] [-I {yes|test}]
#
# where
#
#   Device            is the device name of the file system.
#
#   PolicyFilename    is the name of the file containing the policy rules.
#                     If DEFAULT is specified, the current policy file is
#                     replaced with a single default rule that assigns all
#                     newly created files to the system storage pool.
#
#   DescriptiveName   is a user-provided descriptive name for the new
#                     policy rules.  Defaults to the name of the file.
#                     The total length cannot exceed 255 characters.
#
#   -I                if yes, the rules will be installed immediately.
#                     if test, the rules will not be installed.
#                     The default -I yes.
#
#############################################################################

# Include global declarations and service routines.
. /usr/lpp/mmfs/bin/mmglobfuncs
. /usr/lpp/mmfs/bin/mmsdrfsdef
. /usr/lpp/mmfs/bin/mmfsfuncs

sourceFile="mmchpolicy.sh"
[[ -n $DEBUG || -n $DEBUGmmchpolicy ]] && set -x
$mmTRACE_ENTER "$*"

# Local work files.  Names should be of the form:
#   fn=${tmpDir}fn.${mmcmd}.$$

tspolicyFile=${tmpDir}tspolicyFile.${mmcmd}.$$

LOCAL_FILES=" $tspolicyFile "


# Local variables
usageMsg=361


######################
# Mainline processing
######################


#################################
# Process the command arguments.
#################################
[[ $arg1 = '-?' || $arg1 = '-h' || $arg1 = '--help' || $arg1 = '--' ]] &&  \
  syntaxError "help" $usageMsg

[[ $argc -lt 2  ]] &&  \
  syntaxError "missingArgs" $usageMsg

# Save the positional parameters.
device=$arg1
inputPolicyFile=$arg2

# Move past the required parameters.
shift 2

# Process the rest of the parameters.
while getopts :I:t: OPT
do
  case $OPT in
    I) [[ -n $Iflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Iflag="-$OPT"; Iarg=$OPTARG;
       [[ $Iarg != yes && $Iarg != test ]] &&  \
	   syntaxError "invalidOption" $usageMsg "-$OPT $OPTARG"
       ;;

    t) # Comment string.
       [[ -n $tflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       tflag="-$OPT"; targ="$OPTARG"
       policyName="$targ"
       checkName lengthCheckOnly 255 "$policyName"
       [[ $? -ne 0 ]] && cleanupAndExit
       ;;

    +[It]) # Invalid option.
       syntaxError "invalidOption" $usageMsg $OPT
       ;;

    :) # Missing argument.
       syntaxError "missingValue" $usageMsg $OPTARG
       ;;

    *) # Invalid option.
       syntaxError "invalidOption" $usageMsg $OPTARG
       ;;
  esac
done  # end of while getopts :t: OPT do
shift OPTIND-1

[[ $# != 0 ]] && syntaxError "extraArg" $usageMsg $1

if [[ $inputPolicyFile = DEFAULT ]]
then
  # If the user wants to remove the current policy,
  # replace it with a system default.
  print -- "/* Default system rule */" > $tspolicyFile
  checkForErrors "writing to $tspolicyFile" $?
  print -- "RULE 'DEFAULT' SET POOL 'system'" >> $tspolicyFile
  checkForErrors "writing to $tspolicyFile" $?
  policyName="DEFAULT"
else
  # Check the input file parameter and create our own copy.
  checkUserFile $inputPolicyFile $tspolicyFile
  [[ $? -ne 0 ]] && cleanupAndExit
fi

# Assign a descriptive policy name if not provided by the user.
[[ -z $policyName ]] && policyName="${inputPolicyFile##*/}"


#####################################################################
# Set up trap exception handling and ensure that the local copy of
# the mmsdrfs is up-to-date.  There is no need to lock mmsdrfs file.
#####################################################################
trap pretrap2 HUP INT QUIT KILL
gpfsInitOutput=$(gpfsInit nolock)
setGlobalVar $? $gpfsInitOutput


###########################################################
# Make sure the specified file system exists and is local.
###########################################################
findFSoutput=$(findFS "$device" $mmsdrfsFile)
[[ -z $findFSoutput ]] && cleanupAndExit

# Parse the output from the findFS function.
set -f ; set -- $findFSoutput ; set +f
fqDeviceName=$1
deviceName=$2
fsHomeCluster=$3

# Exit with a message if the command was invoked for a remote file system.
if [[ $fsHomeCluster != $HOME_CLUSTER ]]
then
  # Command is not allowed for remote file systems.
  printErrorMsg 106 $mmcmd $device $fsHomeCluster
  cleanupAndExit
fi

# If removing the current policy and -I test is specified,
# there is nothing else to do.
[[ $inputPolicyFile = DEFAULT && $Iarg = test ]] &&  \
  cleanupAndExit 0


########################################################################
# Invoke the tschpolicy command on the local node.
# Display any error messages and exit if any of the following are true:
#   - the command completed successfully
#   - there is an unacceptable error
#       (anything other than daemon down or quorum wait)
#   - the policy name string contains blanks or tabs
#       (the existing infrastructure cannot deal with imbedded blanks)
#   - this is a single node cluster
########################################################################
$tschpolicy $fqDeviceName $tspolicyFile -t "$policyName" $Iflag $Iarg 2>$errMsg
rc=$(remapRC $?)

if [[ $rc -ne $MM_DaemonDown && $rc -ne $MM_QuorumWait ||
      "$policyName" = *+([;$BLANKchar$TABchar])*       ||
      $MMMODE = single ]]
then
  if [[ $rc -eq $MM_DaemonDown ]]
  then
    # GPFS is down on this node.
    printErrorMsg 109 $mmcmd
  elif [[ $rc -eq $MM_QuorumWait ]]
  then
    # GPFS is not ready for commands.
    printErrorMsg 110 $mmcmd
  elif [[ $rc -eq $MM_ConnectionReset ]]
  then
    # An internode connection was reset.
    printErrorMsg 257 $mmcmd
  else
    # Either the command worked, or it is an unexpected error.
    [[ -s $errMsg ]] && $cat $errMsg 1>&2
#   [[ $rc -ne 0 ]] && printErrorMsg 113 "$mmcmd" "tschpolicy" $rc
  fi  # end of if [[ $rc -eq $MM_DaemonDown ]]
  cleanupAndExit $rc
fi  # end of if [[ ($rc -ne $MM_DaemonDown && ... ]]
$rm -f $errMsg


#####################################################################
# We come here if the local GPFS daemon is not ready for commands.
# Find an active node and send the command there.
#####################################################################

# Create a file with the reliable names of the nodes in the cluster.
getNodeList $REL_HOSTNAME_Field $GLOBAL_ID $mmsdrfsFile > $nodefile
preferredNode=$ourNodeName

# Try the nodes one by one until you find a node that can execute the command.
$mmcommon onactive $preferredNode $nodefile     \
   $tspolicyFile $NO_MOUNT_CHECK NULL $NO_LINK  \
   tschpolicy "$fqDeviceName $tspolicyFile -t $policyName $Iflag $Iarg"
rc=$?

# if [[ $rc -ne 0 ]]
# then
#   # tschpolicy failed.
#   printErrorMsg 104 $mmcmd tschpolicy
#   cleanupAndExit $rc
# fi

cleanupAndExit $rc

