#!/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 
# @(#)81 1.38 src/avs/fs/mmfs/ts/admin/mmdf.sh, mmfs, avs_rgpfs24, rgpfs240610b 1/23/06 01:56:50
#######################################################################
#
# Usage:  mmdf Device [-d | -F | -m ] [-q] [-P PoolName]
#
#######################################################################

# Include global declarations and service routines.
. /usr/lpp/mmfs/bin/mmglobfuncs
if [[ $ourUid -eq 0 ]]
then
  . /usr/lpp/mmfs/bin/mmsdrfsdef
  . /usr/lpp/mmfs/bin/mmfsfuncs
fi

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

usageMsg=298
integer rc=0
integer nodeCount=0


#####################################################################
# Process the command arguments.
# The detailed syntax checking is left for the ts command.
# Here we make sure that we have the minimum needed to continue.
#####################################################################
[[ $arg1 = '-?' || $arg1 = '-h' || $arg1 = '--help' || $arg1 = '--' ]] &&  \
  syntaxError "help" $usageMsg

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

device=$arg1     # Save stripe group device (always the first parameter).
shift 1          # Drop the device name from the parameter list.
argList=$@       # Save the remainder of the argument list.


###################################
# Set up trap exception handling.
###################################
trap pretrap2 HUP INT QUIT KILL


####################################################################
# If invoked by a root user, call the gpfsInit function to ensure
# that the local copy of the mmsdrfs file and the rest of the GPFS
# system files are up-to-date.  There is no need to lock the sdr.
# Non-root users are not allowed to invoke commands on other nodes.
####################################################################
if [[ $ourUid -eq 0 ]]
then
  gpfsInitOutput=$(gpfsInit nolock)
  setGlobalVar $? $gpfsInitOutput
fi


####################################
# Make sure the file system exists.
####################################
# If the invocation is not for an explicitly-remote device, obtain the
# needed information about the filesystem from the mmsdrfs file.
if [[ $device != *:* ]]
then
  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
  remoteDevice=$4

  # If this is a remote file system, set fqDeviceName appropriately.
  [[ $fsHomeCluster != $HOME_CLUSTER ]] &&  \
    fqDeviceName="$fsHomeCluster:/dev/$remoteDevice"
else
  fqDeviceName=$device
  deviceName=${fqDeviceName##*:}
  fsHomeCluster=${fqDeviceName%%:*}
  remoteDevice=$deviceName
fi


########################################################################
# Invoke the 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 file system is remote
#   - we are not running as UID 0
#   - this is a single node cluster
########################################################################
${mmcmdDir}/${links}/mmdf $fqDeviceName $argList 2>$errMsg
rc=$(remapRC $?)
if [[ ($rc -ne $MM_DaemonDown && $rc -ne $MM_QuorumWait) ||
      $fsHomeCluster != $HOME_CLUSTER                    ||
      $ourUid -ne 0                                      ||
      $MMMODE = single ]]
then
  if [[ $rc -eq $MM_FsNotFound ]]
  then
    if [[ $fsHomeCluster != $HOME_CLUSTER ]]
    then
      # The remote cluster does not know anything about this file system.
      printErrorMsg 108 $mmcmd $remoteDevice $fsHomeCluster
    else
      # Unexpected error.
      printErrorMsg 171 $mmcmd "file system $deviceName not found" $rc
    fi
  elif [[ $rc -eq $MM_Remotefs && $fsHomeCluster != $HOME_CLUSTER ]]
  then
    # The file system is not owned by the remote cluster.
    [[ $device != *:* ]] &&  \
      printErrorMsg 111 $mmcmd $device $remoteDevice $fsHomeCluster
    printErrorMsg 112 $mmcmd $remoteDevice $fsHomeCluster
  elif [[ ($rc -eq $MM_HostDown    ||
           $rc -eq $MM_TimedOut    ||
           $rc -eq $MM_SecurityCfg ||
           $rc -eq $MM_AuthorizationFailed ||
           $rc -eq $MM_UnknownCluster)    &&
          $fsHomeCluster != $HOME_CLUSTER ]]
  then
    # Failed to connect to the remote cluster.
    [[ $rc -eq $MM_SecurityCfg ]] &&  \
      printErrorMsg 150 $mmcmd
    [[ $rc -eq $MM_AuthorizationFailed ]] &&  \
      printErrorMsg 151 $mmcmd
    printErrorMsg 105 $mmcmd $fsHomeCluster
  elif [[ $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.
    if [[ -s $errMsg ]]
    then
      $cat $errMsg 1>&2
    else
      [[ $rc -ne 0 ]] && printErrorMsg 113 "$mmcmd" "tsdf $fqDeviceName" $rc
    fi
  fi  # end of if [[ $rc -eq $MM_FsNotFound ]]
  cleanupAndExit $rc
fi  # end of if [[ ($rc -ne $MM_DaemonDown && ... ]]
$rm -f $errMsg


#######################################################################
# We come here if the file system is local but the local daemon is not
# available.  Find an active node and send the command there.
#######################################################################

# Create a file with the reliable names that form
# 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 mmdf $fqDeviceName $argList
rc=$?

cleanupAndExit $rc

