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

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