#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1999,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 
# @(#)87 1.33.1.2 src/avs/fs/mmfs/ts/admin/mmrestripefs.sh, mmfs, avs_rgpfs24, rgpfs24s008a 11/20/06 19:31:37
###########################################################################
#
# Usage:
#
# mmrestripefs Device {-m | -r | -b | -p | -R} [-P PoolName]
#              [-N {all | mount | Node[,Node...] | NodeFile | NodeClass}]
#
###########################################################################

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

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

usageMsg=302
integer rc=0
integer nodeCount=0


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

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

device=$arg1     # Save the file system name (always the first parameter).
shift 1          # Drop the device name from the parameter list.

# Parse the optional parameters.
while getopts :bmrRN:pP: OPT
do
   case $OPT in
      b) # Restripe all files.
         [[ -n $bflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         [[ -n $mflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-b" "-m"
         [[ -n $pflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-b" "-p"
         [[ -n $rflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-b" "-r"
         [[ -n $Rflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-b" "-R"
         bflag=yes
         argList="$argList -$OPT "
         ;;

      m) # Migrate critical data.
         [[ -n $mflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         [[ -n $bflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-b" "-m"
         [[ -n $pflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-m" "-p"
         [[ -n $rflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-m" "-r"
         [[ -n $Rflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-m" "-R"
         mflag=yes
         argList="$argList -$OPT "
         ;;

      p) # Restore storage pool placement.
         [[ -n $pflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         [[ -n $bflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-b" "-p"
         [[ -n $mflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-m" "-p"
         [[ -n $rflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-p" "-r"
         [[ -n $Rflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-p" "-R"
         pflag=yes
         argList="$argList -$OPT "
         ;;

      r) # Restore replication.
         [[ -n $rflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         [[ -n $mflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-m" "-r"
         [[ -n $bflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-b" "-r"
         [[ -n $pflag ]] &&  \
           syntaxError "invalidCombination"  $usageMsg "-p" "-r"
         [[ -n $Rflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-R" "-r"
         rflag=yes
         argList="$argList -$OPT "
         ;;

      R) # Restore replication to system defaults.
         [[ -n $Rflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         [[ -n $mflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-m" "-R"
         [[ -n $bflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-b" "-R"
         [[ -n $pflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-p" "-R"
         [[ -n $rflag ]] &&  \
           syntaxError "invalidCombination" $usageMsg "-r" "-R"
         Rflag=yes
         argList="$argList -$OPT "
         ;;

      N) # Change the state of the specified disks.
         [[ -n $Nflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         nodeList="$OPTARG"
         Nflag=yes
         ;;

      P) # Change the state of the specified disks.
         [[ -n $Pflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
         poolName="$OPTARG"
         Pflag=yes
         argList="$argList -$OPT $OPTARG "
         ;;

      :) # missing required value after an option
         syntaxError "missingValue" $usageMsg $OPTARG
         ;;

     +[bmrNpP]) # plus flags are not allowed
         syntaxError "invalidOption" $usageMsg $OPT
         ;;

      *) # invalid option specified
         syntaxError "invalidOption" $usageMsg $OPTARG
         ;;
   esac
done

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

[[ -z $bflag && -z $mflag && -z $rflag && -z $pflag && -z $Rflag ]] &&  \
  syntaxError "missingArgs" $usageMsg


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

# Determine the lookup order for resolving host names.
[[ $osName != AIX ]] && resolveOrder=$(setHostResolveOrder)


###########################################################
# 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 a keyword or a list of nodes was specified via the -N option,
# verify the input and add it to the argList and argListQ strings.
###################################################################
if [[ -n $Nflag ]]
then
  if [[ $nodeList = all || $nodeList = mount ]]
  then
    # Add the keyword ("all" or "mount") to the argList string.
    argList="$argList -N $nodeList "
  else
    # Verify the node list and convert it to a file of daemon node names.
    createVerifiedNodefile "$nodeList" $DAEMON_NODENAME_Field no $nodefile
    [[ $? -ne 0 ]] && cleanupAndExit

    # Convert the output data from a file to a comma-separated list.
    newNodeList=$(print -- $(cat $nodefile) | $sed 's/ /,/g')

    # Add the verified node list to the argList string.
    argList="$argList -N $newNodeList "
  fi  # end of if [[ $nodeList = all || $nodeList = mount ]]
fi  # end of if [[ -n $Nflag ]]


#######################################################################
# If the cluster to which this node belongs is the same as the cluster
# in which the file system resides, invoke the command directly.
#######################################################################
if [[ $nsId = $fsHomeCluster ]]
then
  ${mmcmdDir}/${links}/mmrestripefs $fqDeviceName $argList 2>$errMsg
  rc=$(remapRC $?)

  # If the command completed successfully, or if there is an
  # unacceptable error, display any error messages and get out.
  if [[ $rc -ne $MM_DaemonDown && $rc -ne $MM_QuorumWait ]]
  then
    [[ -s $errMsg ]] && $cat $errMsg 1>&2
    if [[ $rc -eq $MM_ConnectionReset ]]
    then
      # An internode connection was reset.
      printErrorMsg 257 $mmcmd
    fi
    cleanupAndExit $rc
  fi
fi
$rm -f $errMsg


############################################################################
# If the local daemon is not available, send the command to an active node.
############################################################################

# Create a file with the reliable names of the nodes
# in the cluster to which the file system belongs.
nodeCount=$(getNodeFile $REL_HOSTNAME_Field $fsHomeCluster $mmsdrfsFile $nodefile)
if [[ $nodeCount -eq 0 ]]
then
  # The cluster is empty; there is nobody to run the command.
  printErrorMsg 171 $mmcmd "getNodeFile (nodeCount=0)" 1
  cleanupAndExit
fi

# Try the nodes one by one until you find a node that can execute the command.
preferredNode=0     # We have no idea where to go first; let mmcommon decide.
$mmcommon linkCommand $preferredNode $nodefile mmrestripefs $fqDeviceName "$argList"
rc=$?

cleanupAndExit $rc

