949addd
From 4ff8602c62ed908a7b7d5f51e863ed0ab4de2659 Mon Sep 17 00:00:00 2001
949addd
From: Chris Leech <cleech@redhat.com>
949addd
Date: Tue, 16 Feb 2016 16:45:26 -0800
949addd
Subject: [PATCH] remove sysfs attr_list
949addd
949addd
The global cache is not well designed, it quickly can grow to the point
949addd
where lookups take much longer than just doing the sysfs read in the
949addd
first place.
949addd
---
949addd
 usr/host.c         |  1 +
949addd
 usr/session_info.c |  1 +
949addd
 usr/sysfs.c        | 62 ++++++++++++------------------------------------------
949addd
 3 files changed, 16 insertions(+), 48 deletions(-)
949addd
949addd
diff --git a/usr/host.c b/usr/host.c
949addd
index f2052d3..6333490 100644
949addd
--- a/usr/host.c
949addd
+++ b/usr/host.c
949addd
@@ -274,6 +274,7 @@ int host_info_print(int info_level, uint32_t host_no)
949addd
 			printf("iSCSI Transport Class version %s\n",
949addd
 			       version);
949addd
 			printf("version %s\n", ISCSI_VERSION_STR);
949addd
+			free(version);
949addd
 		}
949addd
 
949addd
 		flags |= SESSION_INFO_SCSI_DEVS;
949addd
diff --git a/usr/session_info.c b/usr/session_info.c
949addd
index 2f48e65..89422d8 100644
949addd
--- a/usr/session_info.c
949addd
+++ b/usr/session_info.c
949addd
@@ -390,6 +390,7 @@ int session_info_print(int info_level, struct session_info *info, int do_show)
949addd
 			printf("iSCSI Transport Class version %s\n",
949addd
 				version);
949addd
 			printf("version %s\n", ISCSI_VERSION_STR);
949addd
+			free(version);
949addd
 		}
949addd
 
949addd
 		flags |= (SESSION_INFO_SCSI_DEVS | SESSION_INFO_HOST_DEVS);
949addd
diff --git a/usr/sysfs.c b/usr/sysfs.c
949addd
index 6520bf6..efd4f74 100644
949addd
--- a/usr/sysfs.c
949addd
+++ b/usr/sysfs.c
949addd
@@ -63,15 +63,6 @@ char sysfs_path[PATH_SIZE];
949addd
 /* device cache */
949addd
 static LIST_HEAD(dev_list);
949addd
 
949addd
-/* attribute value cache */
949addd
-static LIST_HEAD(attr_list);
949addd
-
949addd
-struct sysfs_attr {
949addd
-	struct list_head node;
949addd
-	char path[PATH_SIZE];
949addd
-	char *value;			/* points to value_local if value is cached */
949addd
-	char value_local[NAME_SIZE];
949addd
-};
949addd
 int sysfs_init(void)
949addd
 {
949addd
 	const char *env;
949addd
@@ -85,22 +76,14 @@ int sysfs_init(void)
949addd
 	dbg("sysfs_path='%s'", sysfs_path);
949addd
 
949addd
 	INIT_LIST_HEAD(&dev_list);
949addd
-	INIT_LIST_HEAD(&attr_list);
949addd
 	return 0;
949addd
 }
949addd
 
949addd
 void sysfs_cleanup(void)
949addd
 {
949addd
-	struct sysfs_attr *attr_loop;
949addd
-	struct sysfs_attr *attr_temp;
949addd
 	struct sysfs_device *dev_loop;
949addd
 	struct sysfs_device *dev_temp;
949addd
 
949addd
-	list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
949addd
-		list_del_init(&attr_loop->node);
949addd
-		free(attr_loop);
949addd
-	}
949addd
-
949addd
 	list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
949addd
 		list_del_init(&dev_loop->node);
949addd
 		free(dev_loop);
949addd
@@ -355,10 +338,7 @@ struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device
949addd
 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
949addd
 {
949addd
 	char path_full[PATH_SIZE];
949addd
-	const char *path;
949addd
 	char value[NAME_SIZE];
949addd
-	struct sysfs_attr *attr_loop;
949addd
-	struct sysfs_attr *attr;
949addd
 	struct stat statbuf;
949addd
 	int fd;
949addd
 	ssize_t size;
949addd
@@ -368,29 +348,10 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
949addd
 	sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
949addd
 	if(sysfs_len >= sizeof(path_full))
949addd
 		sysfs_len = sizeof(path_full) - 1;
949addd
-	path = &path_full[sysfs_len];
949addd
 	strlcat(path_full, devpath, sizeof(path_full));
949addd
 	strlcat(path_full, "/", sizeof(path_full));
949addd
 	strlcat(path_full, attr_name, sizeof(path_full));
949addd
 
949addd
-	/* look for attribute in cache */
949addd
-	list_for_each_entry(attr_loop, &attr_list, node) {
949addd
-		if (strcmp(attr_loop->path, path) == 0) {
949addd
-			dbg("found in cache '%s'", attr_loop->path);
949addd
-			return attr_loop->value;
949addd
-		}
949addd
-	}
949addd
-
949addd
-	/* store attribute in cache (also negatives are kept in cache) */
949addd
-	dbg("new uncached attribute '%s'", path_full);
949addd
-	attr = malloc(sizeof(struct sysfs_attr));
949addd
-	if (attr == NULL)
949addd
-		return NULL;
949addd
-	memset(attr, 0x00, sizeof(struct sysfs_attr));
949addd
-	strlcpy(attr->path, path, sizeof(attr->path));
949addd
-	dbg("add to cache '%s'", path_full);
949addd
-	list_add(&attr->node, &attr_list);
949addd
-
949addd
 	if (lstat(path_full, &statbuf) != 0) {
949addd
 		dbg("stat '%s' failed: %s", path_full, strerror(errno));
949addd
 		goto out;
949addd
@@ -408,8 +369,7 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
949addd
 			pos = strrchr(link_target, '/');
949addd
 			if (pos != NULL) {
949addd
 				dbg("cache '%s' with link value '%s'", path_full, value);
949addd
-				strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
949addd
-				attr->value = attr->value_local;
949addd
+				strlcpy(value, &pos[1], NAME_SIZE);
949addd
 			}
949addd
 		}
949addd
 		goto out;
949addd
@@ -439,12 +399,8 @@ char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
949addd
 	/* got a valid value, store and return it */
949addd
 	value[size] = '\0';
949addd
 	remove_trailing_chars(value, '\n');
949addd
-	dbg("cache '%s' with attribute value '%s'", path_full, value);
949addd
-	strlcpy(attr->value_local, value, sizeof(attr->value_local));
949addd
-	attr->value = attr->value_local;
949addd
-
949addd
 out:
949addd
-	return attr->value;
949addd
+	return strdup(value);
949addd
 }
949addd
 
949addd
 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
949addd
@@ -567,8 +523,10 @@ char *sysfs_get_value(const char *id, char *subsys, char *param)
949addd
 	}
949addd
 
949addd
 	if (!strncmp(sysfs_value, "<NULL>", 6) ||
949addd
-	    !strncmp(sysfs_value, "(null)", 6))
949addd
+	    !strncmp(sysfs_value, "(null)", 6)) {
949addd
+		free(sysfs_value);
949addd
 		return NULL;
949addd
+	}
949addd
 
949addd
 	return sysfs_value;
949addd
 }
949addd
@@ -585,6 +543,7 @@ int sysfs_get_uint(char *id, char *subsys, char *param,
949addd
 
949addd
 	errno = 0;
949addd
 	*value = strtoul(sysfs_value, NULL, 0);
949addd
+	free(sysfs_value);
949addd
 	if (errno)
949addd
 		return errno;
949addd
 	return 0;
949addd
@@ -600,6 +559,7 @@ int sysfs_get_int(const char *id, char *subsys, char *param, int *value)
949addd
 		return EIO;
949addd
 
949addd
 	*value = atoi(sysfs_value);
949addd
+	free(sysfs_value);
949addd
 	return 0;
949addd
 }
949addd
 
949addd
@@ -619,6 +579,7 @@ int sysfs_get_str(char *id, char *subsys, char *param, char *value,
949addd
 		sysfs_value[len - 1] = '\0';
949addd
 	strncpy(value, sysfs_value, value_size);
949addd
 	value[value_size - 1] = '\0';
949addd
+	free(sysfs_value);
949addd
 	return 0;
949addd
 }
949addd
 
949addd
@@ -631,8 +592,11 @@ int sysfs_get_uint64(char *id, char *subsys, char *param, uint64_t *value)
949addd
 	if (!sysfs_value)
949addd
 		return EIO;
949addd
 
949addd
-	if (sscanf(sysfs_value, "%" PRIu64 "\n", value) != 1)
949addd
+	if (sscanf(sysfs_value, "%" PRIu64 "\n", value) != 1) {
949addd
+		free(sysfs_value);
949addd
 		return EINVAL;
949addd
+	}
949addd
+	free(sysfs_value);
949addd
 	return 0;
949addd
 }
949addd
 
949addd
@@ -647,6 +611,7 @@ int sysfs_get_uint8(char *id, char *subsys, char *param,
949addd
 		return EIO;
949addd
 
949addd
 	*value = (uint8_t)atoi(sysfs_value);
949addd
+	free(sysfs_value);
949addd
 	return 0;
949addd
 }
949addd
 
949addd
@@ -661,6 +626,7 @@ int sysfs_get_uint16(char *id, char *subsys, char *param,
949addd
 		return EIO;
949addd
 
949addd
 	*value = (uint16_t)atoi(sysfs_value);
949addd
+	free(sysfs_value);
949addd
 	return 0;
949addd
 }
949addd
 
949addd
-- 
949addd
2.5.0
949addd