Patchwork [Open-FCoE,libhbalinux,2/6] scan for fc_host with libudev

login
register
mail settings
Submitter Chris Leech
Date Oct. 13, 2014, 11:13 p.m.
Message ID <1413242005-17786-3-git-send-email-cleech@redhat.com>
Download mbox | patch
Permalink /patch/165/
State RFC
Headers show

Comments

Chris Leech - Oct. 13, 2014, 11:13 p.m.
Signed-off-by: Chris Leech <cleech@redhat.com>
---
 adapt_impl.h |  2 +-
 lport.c      | 56 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 50 insertions(+), 8 deletions(-)

Patch

diff --git a/adapt_impl.h b/adapt_impl.h
index d86c2f8..9d9a347 100644
--- a/adapt_impl.h
+++ b/adapt_impl.h
@@ -151,7 +151,7 @@  HBA_STATUS scsi_report_luns_v2(HBA_HANDLE, HBA_WWN, HBA_WWN,
 HBA_STATUS sg_issue_inquiry(const char *, HBA_UINT8, HBA_UINT8,
 		void *, HBA_UINT32 *, HBA_UINT8 *, void *, HBA_UINT32 *);
 
-void adapter_init(void);
+int adapter_init(void);
 void adapter_shutdown(void);
 
 /* struct port_stats; */
diff --git a/lport.c b/lport.c
index bc34078..3b1030b 100644
--- a/lport.c
+++ b/lport.c
@@ -20,6 +20,8 @@ 
 #include "api_lib.h"
 #include "adapt_impl.h"
 
+#include <libudev.h>
+
 #ifndef HBA_STATUS_ERROR_ILLEGAL_FCID
 #define HBA_STATUS_ERROR_ILLEGAL_FCID 33	/* defined after HBA-API 2.2 */
 #endif
@@ -186,7 +188,7 @@  find_phys_if(char *hba_dir, char *buf, size_t len)
 }
 
 static int
-sysfs_scan(struct dirent *dp, void *arg)
+sysfs_scan(struct udev_device *fc_host)
 {
 	HBA_ADAPTERATTRIBUTES *atp;
 	HBA_PORTATTRIBUTES *pap;
@@ -204,6 +206,8 @@  sysfs_scan(struct dirent *dp, void *arg)
 	unsigned int ifindex;
 	unsigned int iflink;
 
+	const char *sysname = udev_device_get_sysname(fc_host);
+
 	memset(&hba_info, 0, sizeof(hba_info));
 
 	/*
@@ -217,7 +221,7 @@  sysfs_scan(struct dirent *dp, void *arg)
 		return HBA_STATUS_ERROR;
 	}
 	memset(ap, 0, sizeof(*ap));
-	ap->ad_kern_index = atoi(dp->d_name + sizeof("host") - 1);
+	ap->ad_kern_index = atoi(sysname + sizeof("host") - 1);
 	ap->ad_port_count = 1;
 
 	/* atp points to the HBA attributes structure */
@@ -239,14 +243,14 @@  sysfs_scan(struct dirent *dp, void *arg)
 	memset(pp, 0, sizeof(*pp));
 	pp->ap_adapt = ap;
 	pp->ap_index = ap->ad_port_count - 1;
-	pp->ap_kern_hba = atoi(dp->d_name + sizeof("host") - 1);
+	pp->ap_kern_hba = atoi(sysname + sizeof("host") - 1);
 
 	/* pap points to the local port attributes structure */
 	pap = &pp->ap_attr;
 
 	/* Construct the host directory name from the input name */
 	snprintf(host_dir, sizeof(host_dir),
-		SYSFS_HOST_DIR "/%s", dp->d_name);
+		SYSFS_HOST_DIR "/%s", sysname);
 
 	rc = sa_sys_read_line(host_dir, "symbolic_name", buf, sizeof(buf));
 
@@ -407,7 +411,7 @@  sysfs_scan(struct dirent *dp, void *arg)
 
 	/* Get OSDeviceName */
 	sa_strncpy_safe(pap->OSDeviceName, sizeof(pap->OSDeviceName),
-			dp->d_name, sizeof(dp->d_name));
+			sysname, sizeof(sysname));
 
 	/* Get NumberofDiscoveredPorts */
 	snprintf(buf, sizeof(buf), "%s/device", pp->host_dir);
@@ -662,10 +666,48 @@  sysfs_get_port_fc4stats(char *dir, HBA_FC4STATISTICS *fc4sp)
 /*
  * Open device and read adapter info if available.
  */
-void
+int
 adapter_init(void)
 {
-	sa_dir_read(SYSFS_HOST_DIR, sysfs_scan, NULL);
+	struct udev *udev;
+	struct udev_enumerate *ue;
+	struct udev_list_entry *head, *ul;
+	struct udev_device *fc_host;
+	const char *syspath;
+	int re = 0;
+
+	udev = udev_new();
+	if (!udev) {
+		return -ENOMEM;
+	}
+	ue = udev_enumerate_new(udev);
+	if (!ue) {
+		re = -ENOMEM;
+		goto err_enum_new;
+	}
+	re = udev_enumerate_add_match_subsystem(ue, "fc_host");
+	if (re) {
+		goto err_add_match;
+	}
+	re = udev_enumerate_scan_devices(ue);
+	if (re) {
+		goto err_scan_devs;
+	}
+	head = udev_enumerate_get_list_entry(ue);
+	udev_list_entry_foreach(ul, head) {
+		syspath = udev_list_entry_get_name(ul);
+		fc_host = udev_device_new_from_syspath(udev, syspath);
+		if (!fc_host)
+			continue;
+		sysfs_scan(fc_host);
+		udev_device_unref(fc_host);
+	}
+err_scan_devs:
+err_add_match:
+	udev_enumerate_unref(ue);
+err_enum_new:
+	udev_unref(udev);
+	return re;
 }
 
 void