--- resource-agents-orig/heartbeat/iSCSILogicalUnit	2012-05-25 09:23:59.000000000 -0700
+++ resource-agents/heartbeat/iSCSILogicalUnit	2012-07-03 00:16:15.073159572 -0700
@@ -38,7 +38,7 @@
     OCF_RESKEY_implementation_default="iet"
 elif have_binary tgtadm; then
     OCF_RESKEY_implementation_default="tgt"
-elif have_binary lio_node; then
+elif have_binary tcm_node; then
     OCF_RESKEY_implementation_default="lio"
 fi
 : ${OCF_RESKEY_implementation=${OCF_RESKEY_implementation_default}}
@@ -99,7 +99,7 @@
 <content type="integer" />
 </parameter>
 
-<parameter name="path" required="1" unique="0">
+<parameter name="path" required="0" unique="0">
 <longdesc lang="en">
 The path to the block device exposed. Some implementations allow this
 to be a regular file, too.
@@ -142,6 +142,30 @@
 <content type="string" />
 </parameter>
 
+<parameter name="backstore_type" required="0" unique="0">
+<longdesc lang="en">
+The backstore type used for the LUN - block iblock fileio ramdisk supported.
+</longdesc>
+<shortdesc lang="en">Backstore Type</shortdesc>
+<content type="string" />
+</parameter>
+
+<parameter name="hba_name" required="0" unique="0">
+<longdesc lang="en">
+The Host Bus Adaptor name
+</longdesc>
+<shortdesc lang="en">HBA Name</shortdesc>
+<content type="string" />
+</parameter>
+
+<parameter name="ramdisk_pages" required="0" unique="0">
+<longdesc lang="en">
+The ramdisk pages used for the LUN
+</longdesc>
+<shortdesc lang="en">Ramdisk Pages</shortdesc>
+<content type="string" />
+</parameter>
+
 <parameter name="additional_parameters" required="0" unique="0">
 <longdesc lang="en">
 Additional LU parameters. A space-separated list of "name=value" pairs
@@ -153,21 +177,6 @@
 <content type="string" />
 </parameter>
 
-<parameter name="allowed_initiators" required="0" unique="0">
-<longdesc lang="en">
-Allowed initiators. A space-separated list of initiators allowed to
-connect to this lun. Initiators may be listed in any syntax
-the target implementation allows. If this parameter is empty or
-not set, access to this lun will not be allowed from any initiator,
-if target is not in demo mode.
-
-This parameter is only necessary, when using LIO.
-</longdesc>
-<shortdesc lang="en">List of iSCSI initiators allowed to connect
-to this lun.</shortdesc>
-<content type="string" default=""/>
-</parameter>
-
 </parameters>
 
 <actions>
@@ -250,23 +259,29 @@
 	    fi
 	    ;;
 	lio)
-	    # For lio, we first have to create a target device, then
-	    # add it to the Target Portal Group as an LU.
-	    ocf_run tcm_node --createdev=iblock_0/${OCF_RESOURCE_INSTANCE} \
-		${OCF_RESKEY_path} || exit $OCF_ERR_GENERIC
+	    if [ "${OCF_RESKEY_backstore_type}" = "block" ] || [ "${OCF_RESKEY_backstore_type}" = "iblock" ]; then
+		ocf_run tcm_node --createdev=${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE} \
+		    ${OCF_RESKEY_path} || exit $OCF_ERR_GENERIC
+	    elif [ "${OCF_RESKEY_backstore_type}" = "fileio" ]; then
+		# Do nothing if the file isn't there
+		if [ -f "${OCF_RESKEY_path}" ]; then
+		    # This might not be the best way to identify file size
+		    fileSize="$(ls -l ${OCF_RESKEY_path} | cut -d ' ' -f 5)"
+			ocf_run tcm_node --fileio ${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE} \
+			    ${OCF_RESKEY_path} ${fileSize} || exit $OCF_ERR_GENERIC
+		else
+		    ocf_log error "Missing file ${OCF_RESKEY_path}"
+		    exit $OCF_ERR_GENERIC
+		fi
+	    elif [ "${OCF_RESKEY_backstore_type}" = "ramdisk" ]; then
+		# TODO: test this
+		ocf_run tcm_node --ramdisk=${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE} \
+		    ${OCF_RESKEY_ramdisk_pages} || exit $OCF_ERR_GENERIC
+	    fi
 	    if [ -n "${OCF_RESKEY_scsi_sn}" ]; then
-		ocf_run tcm_node --setunitserial=iblock_0/${OCF_RESOURCE_INSTANCE} \
+		ocf_run tcm_node --setunitserial=${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE} \
 		    ${OCF_RESKEY_scsi_sn} || exit $OCF_ERR_GENERIC
 	    fi
-	    ocf_run lio_node --addlun=${OCF_RESKEY_target_iqn} 1 ${OCF_RESKEY_lun} \
-		${OCF_RESOURCE_INSTANCE} iblock_0/${OCF_RESOURCE_INSTANCE} || exit $OCF_ERR_GENERIC
-
-           if [ -n "${OCF_RESKEY_allowed_initiators}" ]; then
-               for initiator in ${OCF_RESKEY_allowed_initiators}; do
-                       ocf_run lio_node --addlunacl=${OCF_RESKEY_target_iqn} 1 \
-                       ${initiator} ${OCF_RESKEY_lun} ${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
-               done
-           fi
 	    ;;
     esac
 
@@ -297,14 +312,7 @@
 		done
 		;;
 	    lio)
-               if [ -n "${OCF_RESKEY_allowed_initiators}" ]; then
-                       for initiator in ${OCF_RESKEY_allowed_initiators}; do
-                               ocf_run lio_node --dellunacl=${OCF_RESKEY_target_iqn} 1 \
-                               ${initiator} ${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
-                       done
-               fi
-		ocf_run lio_node --dellun=${OCF_RESKEY_target_iqn} 1 ${OCF_RESKEY_lun} || exit $OCF_ERR_GENERIC
-		ocf_run tcm_node --freedev=iblock_0/${OCF_RESOURCE_INSTANCE} || exit $OCF_ERR_GENERIC
+		ocf_run tcm_node --freedev=${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE} || exit $OCF_ERR_GENERIC
 	esac
     fi
     
@@ -340,8 +348,19 @@
 		| grep -E -q "[[:space:]]+Backing store.*: ${OCF_RESKEY_path}$" && return $OCF_SUCCESS
 	    ;;
 	lio)
-	    configfs_path="/sys/kernel/config/target/iscsi/${OCF_RESKEY_target_iqn}/tpgt_1/lun/lun_${OCF_RESKEY_lun}/${OCF_RESOURCE_INSTANCE}/udev_path"
-	    [ -e ${configfs_path} ] && [ `cat ${configfs_path}` = "${OCF_RESKEY_path}" ] && return $OCF_SUCCESS
+	    if [ "${OCF_RESKEY_backstore_type}" = "block" ] || [ "${OCF_RESKEY_backstore_type}" = "iblock" ]; then
+		configfs_path="/sys/kernel/config/target/core/${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE}/udev_path"
+		[ -e ${configfs_path} ] && [ `cat ${configfs_path}` = "${OCF_RESKEY_path}" ] && return $OCF_SUCCESS
+	    elif [ "${OCF_RESKEY_backstore_type}" = "fileio" ]; then
+		if [ -f "/sys/kernel/config/target/core/${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE}/info" ]; then
+		    # This may not be the best way to identify the file
+		    configfs_path="$(cat /sys/kernel/config/target/core/${OCF_RESKEY_hba_name}/${OCF_RESOURCE_INSTANCE}/info |grep File|cut -d : -f 3|cut -d ' ' -f 2)"
+		    [ -e ${configfs_path} ] && [ "$(echo ${configfs_path})" = "${OCF_RESKEY_path}" ] && return $OCF_SUCCESS
+		fi
+	    elif [ "${OCF_RESKEY_backstore_type}" = "ramdisk" ]; then
+		# fixme: perform ramdisk check here
+		dummyVariable="0"
+	    fi
 	    ;;
     esac
     
@@ -349,15 +368,6 @@
 }
 
 iSCSILogicalUnit_validate() {
-    # Do we have all required variables?
-    for var in target_iqn lun path; do
-	param="OCF_RESKEY_${var}"
-	if [ -z "${!param}" ]; then
-	    ocf_log error "Missing resource parameter \"$var\"!"
-	    exit $OCF_ERR_CONFIGURED
-	fi
-    done
-
     # Is the configured implementation supported?
     case "$OCF_RESKEY_implementation" in
 	"iet"|"tgt"|"lio")
@@ -376,6 +386,54 @@
 	    ;;
     esac
 
+    # Do we have all required variables?
+    local required_vars
+    case $OCF_RESKEY_implementation in
+        iet)
+            required_vars="target_iqn lun path"
+            ;;
+        tgt)
+            required_vars="target_iqn lun path"
+            ;;
+        lio)
+            required_vars="target_iqn lun path backstore_type hba_name"
+            ;;
+    esac
+    for var in ${required_vars}; do
+	param="OCF_RESKEY_${var}"
+	if [ -z "${!param}" ]; then
+	    ocf_log error "Missing resource parameter \"$var\"!"
+	    exit $OCF_ERR_CONFIGURED
+	fi
+    done
+
+    # Do we have a valid Backstore Type?
+    case $OCF_RESKEY_implementation in
+	lio)
+	    # lio allows block, iblock, fileio, ramdisk
+	    case ${OCF_RESKEY_backstore_type} in
+		block|iblock)
+		    # OK
+		    ;;
+		fileio)
+		    # OK
+		    ;;
+		ramdisk)
+		    # Test if ramdisk_pages parameter exists
+		    if [ -n "${OCF_RESKEY_ramdisk_pages}" ]; then
+			ocf_log error "Backstore Type ramdisk must include parameter: ramdisk_pages"
+			exit $OCF_ERR_CONFIGURED
+		    fi
+		    # OK
+		    ;;
+		*)
+		    ocf_log error "Invalid Backstore Type $OCF_RESKEY_backstore_type (must be: block,iblock,fileio,ramdisk)."
+		    exit $OCF_ERR_CONFIGURED
+		    ;;
+	    esac
+	    ;;
+    esac
+
     # Do we have a valid LUN?
     case $OCF_RESKEY_implementation in
 	iet)
@@ -412,6 +470,23 @@
 		    ;;
 	    esac
 	    ;;
+	lio)
+	    # lio allows LUN 0 and up
+	    [ $OCF_RESKEY_lun -ge 0 ]
+	    case $? in
+		0)
+	            # OK
+		    ;;
+		1)
+		    ocf_log err "Invalid LUN $OCF_RESKEY_lun (must be a non-negative integer)."
+		    exit $OCF_ERR_CONFIGURED
+		    ;;
+		*)
+		    ocf_log err "Invalid LUN $OCF_RESKEY_lun (must be an integer)."
+		    exit $OCF_ERR_CONFIGURED
+		    ;;
+	    esac
+	    ;;
     esac
 
     # Do we have any configuration parameters that the current
@@ -423,13 +498,17 @@
 	iet)
 	    # IET does not support setting the vendor and product ID
 	    # (it always uses "IET" and "VIRTUAL-DISK")
-	    unsupported_params="vendor_id product_id allowed_initiators"
+	    unsupported_params="vendor_id product_id backstore_type hba_name ramdisk_pages"
 	    ;;
 	tgt)
-	    unsupported_params="allowed_initiators"
+	    unsupported_params="backstore_type hba_name ramdisk_pages"
 	    ;;
 	lio)
-	    unsupported_params="scsi_id vendor_id product_id"
+	    # We don't list scsi_id here because we set this at the begining of this script
+	    # regardless of implementation - in doing that and having scsi_id listed
+	    # here: we end up with non a stop log warning
+	    # - just let it be silently ignored
+	    unsupported_params="vendor_id product_id"
 	    ;;
     esac
     for var in ${unsupported_params}; do
@@ -452,7 +531,6 @@
 		;;
 	    lio)
 		check_binary tcm_node
-		check_binary lio_node
 		;;
 	esac
 
@@ -468,6 +546,18 @@
 	    tgt)
 	        # tgt is userland only
 		;;
+	    lio)
+		# lio needs configfs to be mounted
+		if ! grep -Eq "^.*/sys/kernel/config[[:space:]]+configfs" /proc/mounts; then
+		    ocf_log err "configfs not mounted at /sys/kernel/config -- check if required modules are loaded."
+		    exit $OCF_ERR_INSTALLED
+		fi
+		# check for configfs entries created by target_core_mod
+		if [ ! -d /sys/kernel/config/target/core ]; then
+		    ocf_log err "/sys/kernel/config/target/core does not exist or is not a directory -- check if required modules are loaded."
+		    exit $OCF_ERR_INSTALLED
+		fi
+		;;
 	esac
     fi
 
