Blame SOURCES/0010-libiscsi-introduce-sessions-API.patch

221d9a
From bca0b3a085b7a169aa40d81ed7997c73fde8b4d3 Mon Sep 17 00:00:00 2001
62f653
From: Peter Hatina <phatina@redhat.com>
62f653
Date: Mon, 5 Oct 2015 16:50:36 -0700
221d9a
Subject: [PATCH] libiscsi introduce sessions API
62f653
62f653
---
221d9a
 libiscsi/libiscsi.c | 125 ++++++++++++++++++++++++++++++++++++++++++++
221d9a
 libiscsi/libiscsi.h |  56 ++++++++++++++++++++
62f653
 usr/iscsi_sysfs.c   |   6 +++
62f653
 usr/iscsi_sysfs.h   |   2 +
62f653
 4 files changed, 189 insertions(+)
62f653
62f653
diff --git a/libiscsi/libiscsi.c b/libiscsi/libiscsi.c
221d9a
index 064e4b5..755c18c 100644
62f653
--- a/libiscsi/libiscsi.c
62f653
+++ b/libiscsi/libiscsi.c
62f653
@@ -3,6 +3,7 @@
62f653
  *
62f653
  * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
62f653
  * Copyright (C) 2008-2009 Hans de Goede <hdegoede@redhat.com>
62f653
+ * Copyright (C) 2015      Peter Hatina <phatina@redhat.com>
62f653
  * maintained by open-iscsi@googlegroups.com
62f653
  *
62f653
  * This program is free software; you can redistribute it and/or modify
62f653
@@ -469,6 +470,130 @@ leave:
62f653
 	return rc;
62f653
 }
62f653
 
62f653
+struct libiscsi_session_array {
62f653
+	int cnt;
62f653
+	int size;
62f653
+	struct libiscsi_session_info *data;
62f653
+};
62f653
+
62f653
+static void libiscsi_session_array_init(struct libiscsi_session_array *arr)
62f653
+{
62f653
+	arr->cnt = 0;
62f653
+	arr->size = 0;
62f653
+	arr->data = NULL;
62f653
+}
62f653
+
62f653
+static int libiscsi_session_array_grow(struct libiscsi_session_array *arr)
62f653
+{
62f653
+	if (arr->size == 0)
62f653
+		arr->size = 4;
62f653
+	else
62f653
+		arr->size *= 2;
62f653
+
62f653
+	arr->data = (struct libiscsi_session_info *) realloc(
62f653
+		arr->data,
62f653
+		arr->size * sizeof(struct libiscsi_session_info));
62f653
+
62f653
+	return arr->data ? 0 : 1;
62f653
+}
62f653
+
62f653
+static int libiscsi_session_array_grow_ondemand(struct libiscsi_session_array *arr)
62f653
+{
62f653
+	if (arr->size == arr->cnt)
62f653
+		return libiscsi_session_array_grow(arr);
62f653
+	return 0;
62f653
+}
62f653
+
62f653
+static int libiscsi_session_array_resize_precize(struct libiscsi_session_array *arr)
62f653
+{
62f653
+	arr->data = (struct libiscsi_session_info *) realloc(
62f653
+		arr->data,
62f653
+		arr->cnt * sizeof(struct libiscsi_session_info));
62f653
+	arr->size = arr->cnt;
62f653
+
62f653
+	return arr->data ? 0 : 1;
62f653
+}
62f653
+
62f653
+static void copy_session_info_to_libiscsi_session_info(
62f653
+	struct libiscsi_session_info *info,
62f653
+	struct session_info *s_info)
62f653
+{
62f653
+	/* Copy session info to public struct. */
62f653
+	info->sid = s_info->sid;
62f653
+	/* Timeouts */
62f653
+	memcpy(&info->tmo, &s_info->tmo, sizeof(struct libiscsi_session_timeout));
62f653
+	/* CHAP authentication information */
62f653
+	memcpy(&info->chap, &s_info->chap, sizeof(struct libiscsi_chap_auth_info));
62f653
+	/* Target information */
62f653
+	strncpy(info->targetname, s_info->targetname, LIBISCSI_VALUE_MAXLEN);
62f653
+	strncpy(info->address, s_info->address, NI_MAXHOST);
62f653
+	strncpy(info->persistent_address, s_info->persistent_address, NI_MAXHOST);
62f653
+	info->tpgt = s_info->tpgt;
62f653
+	info->persistent_port = s_info->persistent_port;
62f653
+}
62f653
+
62f653
+static int get_sessions_helper(void *data, struct session_info *s_info)
62f653
+{
62f653
+	struct libiscsi_session_array *arr = (struct libiscsi_session_array *) data;
62f653
+
62f653
+	if (libiscsi_session_array_grow_ondemand(arr) != 0)
62f653
+		return 1;
62f653
+
62f653
+	copy_session_info_to_libiscsi_session_info(&arr->data[arr->cnt++], s_info);
62f653
+
62f653
+	return 0;
62f653
+}
62f653
+
62f653
+int libiscsi_get_session_infos(struct libiscsi_context *context,
62f653
+	struct libiscsi_session_info **infos,
62f653
+	int *nr_sessions)
62f653
+{
62f653
+	int rc = 0;
62f653
+	int nr_found = 0;
62f653
+	struct libiscsi_session_array arr;
62f653
+
62f653
+	if (!context || !infos || !nr_sessions)
62f653
+		return 1;
62f653
+
62f653
+	libiscsi_session_array_init(&arr;;
62f653
+
62f653
+	rc = iscsi_sysfs_for_each_session((void *) &arr, &nr_found,
62f653
+		get_sessions_helper, 0);
62f653
+	if (rc != 0 || nr_found == 0) {
62f653
+		strcpy(context->error_str, "No matching session");
62f653
+		return ENODEV;
62f653
+	}
62f653
+
62f653
+	if (libiscsi_session_array_resize_precize(&arr) != 0) {
62f653
+		strcpy(context->error_str, "Can't allocate memory for session infos");
62f653
+		return ENOMEM;
62f653
+	}
62f653
+
62f653
+	*infos = arr.data;
62f653
+	*nr_sessions = nr_found;
62f653
+
62f653
+	return 0;
62f653
+}
62f653
+
62f653
+int libiscsi_get_session_info_by_id(struct libiscsi_context *context,
62f653
+	struct libiscsi_session_info *info,
62f653
+	const char *session)
62f653
+{
62f653
+	struct session_info s_info;
62f653
+
62f653
+	if (!context || !info || !session)
62f653
+		return 1;
62f653
+
62f653
+	if (iscsi_sysfs_get_sessioninfo_by_id(&s_info, (char*) session) != 0) {
62f653
+		strcpy(context->error_str, "No matching session");
62f653
+		return ENODEV;
62f653
+	}
62f653
+
62f653
+	copy_session_info_to_libiscsi_session_info(info, &s_info);
62f653
+
62f653
+	return 0;
62f653
+}
62f653
+
62f653
 int libiscsi_node_set_parameter(struct libiscsi_context *context,
62f653
 	const struct libiscsi_node *node,
62f653
 	const char *parameter, const char *value)
62f653
diff --git a/libiscsi/libiscsi.h b/libiscsi/libiscsi.h
221d9a
index 756590e..a9891f4 100644
62f653
--- a/libiscsi/libiscsi.h
62f653
+++ b/libiscsi/libiscsi.h
62f653
@@ -3,6 +3,7 @@
62f653
  *
62f653
  * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
62f653
  * Copyright (C) 2008-2009 Hans de Goede <hdegoede@redhat.com>
62f653
+ * Copyright (C) 2015      Peter Hatina <phatina@redhat.com>
62f653
  * maintained by open-iscsi@googlegroups.com
62f653
  *
62f653
  * This program is free software; you can redistribute it and/or modify
62f653
@@ -56,6 +57,17 @@ enum libiscsi_auth_t {
62f653
  */
62f653
 struct libiscsi_context;
62f653
 
62f653
+/** \brief iSCSI session timeouts
62f653
+ *
62f653
+ * Struct holding session timeouts.
62f653
+ */
62f653
+struct libiscsi_session_timeout {
62f653
+    int abort_tmo;
62f653
+    int lu_reset_tmo;
62f653
+    int recovery_tmo;
62f653
+    int tgt_reset_tmo;
62f653
+};
62f653
+
62f653
 /** \brief iSCSI node record
62f653
  *
62f653
  * Struct holding data uniquely identifying an iSCSI node.
62f653
@@ -84,6 +96,24 @@ struct libiscsi_chap_auth_info {
62f653
     char reverse_password[LIBISCSI_VALUE_MAXLEN] /** Reverse Password */;
62f653
 };
62f653
 
62f653
+/** \brief iSCSI session
62f653
+ *
62f653
+ * Struct hoding iSCSI session information.
62f653
+ */
62f653
+struct libiscsi_session_info {
62f653
+    int sid;
62f653
+
62f653
+    struct libiscsi_session_timeout tmo;
62f653
+    struct libiscsi_chap_auth_info chap;
62f653
+
62f653
+    char targetname[LIBISCSI_VALUE_MAXLEN];
62f653
+    int tpgt;
62f653
+    char address[NI_MAXHOST];
62f653
+    int port;
62f653
+    char persistent_address[NI_MAXHOST];
62f653
+    int persistent_port;
62f653
+};
62f653
+
62f653
 /** \brief generic libiscsi authentication information struct
62f653
  *
62f653
  * Struct holding authentication information for discovery and login.
62f653
@@ -248,6 +278,32 @@ PUBLIC int libiscsi_node_login(struct libiscsi_context *context,
62f653
 PUBLIC int libiscsi_node_logout(struct libiscsi_context *context,
62f653
     const struct libiscsi_node *node);
62f653
 
62f653
+/** \brief Get an array of iSCSI sessions.
62f653
+ *
62f653
+ * Get the array containing iSCSI sessions' information.
62f653
+ *
62f653
+ * \param context       libiscsi context to operate on.
62f653
+ * \param infos         Array of iSCSI sessions' information.
62f653
+ *                      Release with free().
62f653
+ * \param nr_sessions   The number of elements in \e infos.
62f653
+ * \return              0 on success, otherwise a standard error code
62f653
+ *                      (from errno.h).
62f653
+ */
62f653
+PUBLIC int libiscsi_get_session_infos(struct libiscsi_context *context,
62f653
+    struct libiscsi_session_info **infos, int *nr_sessions);
62f653
+
62f653
+/** \brief Get session information by session ID.
62f653
+ *
62f653
+ * \param context       libiscsi context to operate on.
62f653
+ * \param info          iSCSI session information.
62f653
+ * \param session       Session name.
62f653
+ * \return              0 on success, otherwise a standard error code
62f653
+ *                      (from errno.h)
62f653
+ */
62f653
+PUBLIC int libiscsi_get_session_info_by_id(struct libiscsi_context *context,
62f653
+    struct libiscsi_session_info *info,
62f653
+    const char *session);
62f653
+
62f653
 /** \brief Set an iSCSI parameter for the given node
62f653
  *
62f653
  * Set the given nodes iSCSI parameter named by \e parameter to value \e value.
62f653
diff --git a/usr/iscsi_sysfs.c b/usr/iscsi_sysfs.c
221d9a
index 418f51b..6febba2 100644
62f653
--- a/usr/iscsi_sysfs.c
62f653
+++ b/usr/iscsi_sysfs.c
62f653
@@ -3,6 +3,7 @@
62f653
  *
62f653
  * Copyright (C) 2006 Mike Christie
62f653
  * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
62f653
+ * Copyright (C) 2015      Peter Hatina <phatina@redhat.com>
62f653
  *
62f653
  * This program is free software; you can redistribute it and/or modify
62f653
  * it under the terms of the GNU General Public License as published
221d9a
@@ -1151,6 +1152,11 @@ free_info:
62f653
 	return rc;
62f653
 }
62f653
 
62f653
+const char *iscsi_sysfs_get_session_path(void)
62f653
+{
62f653
+	return ISCSI_SESSION_DIR;
62f653
+}
62f653
+
62f653
 int iscsi_sysfs_for_each_iface_on_host(void *data, uint32_t host_no,
62f653
 				       int *nr_found,
62f653
 				       iscsi_sysfs_iface_op_fn *fn)
62f653
diff --git a/usr/iscsi_sysfs.h b/usr/iscsi_sysfs.h
221d9a
index 1d0377f..909db34 100644
62f653
--- a/usr/iscsi_sysfs.h
62f653
+++ b/usr/iscsi_sysfs.h
62f653
@@ -3,6 +3,7 @@
62f653
  *
62f653
  * Copyright (C) 2006 Mike Christie
62f653
  * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
62f653
+ * Copyright (C) 2015      Peter Hatina <phatina@redhat.com>
62f653
  *
62f653
  * This program is free software; you can redistribute it and/or modify
62f653
  * it under the terms of the GNU General Public License as published
62f653
@@ -47,6 +48,7 @@ typedef int (iscsi_sysfs_flashnode_op_fn)(void *, struct flashnode_rec *,
62f653
 					  uint32_t, uint32_t);
62f653
 typedef int (iscsi_sysfs_iface_op_fn)(void *, struct iface_rec *);
62f653
 
62f653
+extern const char *iscsi_sysfs_get_session_path(void);
62f653
 extern int iscsi_sysfs_for_each_iface_on_host(void *data, uint32_t host_no,
62f653
 					      int *nr_found,
62f653
 					      iscsi_sysfs_iface_op_fn *fn);
62f653
-- 
221d9a
2.21.0
62f653