#!/usr/bin/bash
#
# Copyright (c) 2006, 2024, Oracle and/or its affiliates.
#
# Display current status of the oracleasm driver.
#


# Force LC_ALL=C
export LC_ALL=C

USAGE="[-v]"

exec 3>/dev/null

help=
verbose=
version=
usage=
while case "$#" in 0) break ;; esac
do
    case "$1" in
    -v|--verbose)
        verbose=t
        exec 3>&2
        ;;
    -V|--version)
        version=t
        ;;
    -h|--help)
        help=t
        ;;
    *)
        usage=t
        ;;
    esac
    shift
done

# Load configuration
. oracleasm-Xshlib
check_oracleasm_config

if [ "$help" = "t" -o "$usage" = "t" ]
then
    usage
fi

if [ $(echo $UID) != 0 ]
then
    echo "This operation requires root privileges. Please run as root"
    exit 1
fi

if [ "$version" = "t" ]
then
    version
fi

STATUS1=0
STATUS2=0

echo -n $"Checking if the oracleasm kernel module is loaded: "

# if no oracleasm driver is supported then nothing to check
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -e $"no (not required with ${KERNEL_NAME})"
else
    grep '^oracleasm ' /proc/modules >/dev/null 2>&1
    STATUS1="$?"
    if [ "$STATUS1" -eq  0 ]
    then
	echo "yes"
    else
	echo "no"
    fi
fi

echo -n $"Checking if $ORACLE_ASMMANAGER is mounted: "

if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -e $"no (not required with ${KERNEL_NAME})"
else
    ORACLE_ASMMANAGERSEARCH="$(echo $ORACLE_ASMMANAGER | sed -e 's/\//\\\//g')"
    grep "^oracleasmfs $ORACLE_ASMMANAGERSEARCH oracleasmfs" /proc/mounts >/dev/null 2>&1
    STATUS2="$?"

    if [ "$STATUS2" -eq  0 ]
    then
	echo "yes"
    else
        echo "no"
    fi
fi

# Report which I/O interface is in use: KABI_V2 or KABI_V3
echo -n "Checking which I/O Interface is in use: "
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
then
    echo "oracleasm driver (KABI_V2)"
else
    echo "io_uring (KABI_V3)"
fi

# Validate that libasm.so can be loaded using oracleasm-discover
echo -n "Checking if ASMLIB can be loaded: "
if [ ! -f "${ASMLIB_PATH}" ]
then
    STATUS2=1
    echo "no (library not installed)"
else
    OD="$(oracleasm-discover 2>&1)"
    if [ $? -gt 1 ]
    then
	STATUS2=1
	echo "no (discover failed)"
    elif [[ ! "${OD}" =~ "Using ASMLIB" ]]
    then
	STATUS2=1
	echo "no (loading library failed)"
    elif [[ "{$OD}" =~ "Unable to find" ]] || [[ "{$OD}" =~ "Can't resolve" ]]
    then
	STATUS2=1
	echo "no (missing symbol)"
    else
	echo "yes"
    fi
fi

# io_uring is used for UEK7+ kernels so check if the feature is
# enabled/disabled
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -n "Checking if io_uring is enabled: "
    check_io_uring_disabled
    if [ $? -eq 0 ]
    then
	echo "yes"
    else
	echo "no"
    fi
fi

# Check that a process running with ORACLEASM_{UID,GID} credentials
# can use io_uring.
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -n "Checking if io_uring is accessible to the configured DB user: "
    URING="$(oracleasm-validate-uring 2>&1)"
    RC=$?
    if [ $RC -eq 0 ]
    then
	echo "yes"
	echo "Checking if io_uring supports integrity passthrough: no"
    elif [ $RC -eq 2 ]
    then
	echo "yes"
	echo "Checking if io_uring supports integrity passthrough: yes"
    elif [ $RC -eq 1 ]
    then
	STATUS2=1
	echo "no (${URING})"
    fi
fi

# For V3, check if ASM disks have the correct ownership and
# permissions.
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -n $"Checking if ASM disks have the correct ownership" \
	"and permissions: "
    oracleasm-listdisks 2>&3 | while read LINE
    do
        check_perm "$(asm_disk_path "${LINE}")" 2>&3
        if [ $? = 1 ]
        then
            #XXX echo -e "NO. \nFix permissions on ASM disk \"$LINE\"" | asm_log 1
	    exit 1
        fi
    done
    if [ $? -eq 0 ]
    then
	echo "yes"
    else
	STATUS2=1
	echo "no"
    fi
    # Check if iofilter path is setup
    echo -n $"Checking if ASM I/O filter is set up: "
    if [ -e "${ORACLEASM_IOFILTER_MAP_PATH}" ]
    then
	# map already exists
	echo "yes"
    else
	if [ "${ORACLEASM_IOFILTER_SUPPORTED}" = "false" ]
	then
	    echo "no (not supported with ${KERNEL_NAME})"
	else
	    echo "no"
	fi

	if [[ "${ORACLEASM_ENABLE_IOFILTER}" == "true"
	   && "${ORACLEASM_IOFILTER_SUPPORTED}" == "false" ]]
	then
	    STATUS2=1
	fi
    fi
fi

if [ "$STATUS1" -ne 0 ] || [ "$STATUS2" -ne 0 ]
then
    exit 1
else
    exit 0
fi
