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

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