Blame SOURCES/0102-util-sysfs-Uplevel-modalias-lookup-helper-to-util.patch

2eb93d
From ecd7e6e7aabfa2592f3f739a725d135eb43d6314 Mon Sep 17 00:00:00 2001
2eb93d
From: Dan Williams <dan.j.williams@intel.com>
2eb93d
Date: Sun, 23 Jan 2022 16:53:13 -0800
2eb93d
Subject: [PATCH 102/217] util/sysfs: Uplevel modalias lookup helper to util/
2eb93d
2eb93d
The to_module() helper looks up modules relative to a modalias. Uplevel
2eb93d
this to share with libcxl.
2eb93d
2eb93d
Link: https://lore.kernel.org/r/164298559346.3021641.11059026790676662837.stgit@dwillia2-desk3.amr.corp.intel.com
2eb93d
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
---
2eb93d
 ndctl/lib/libndctl.c | 33 +++++----------------------------
2eb93d
 util/sysfs.c         | 27 +++++++++++++++++++++++++++
2eb93d
 util/sysfs.h         |  8 ++++++++
2eb93d
 3 files changed, 40 insertions(+), 28 deletions(-)
2eb93d
2eb93d
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
2eb93d
index 47a234c..1374ad9 100644
2eb93d
--- a/ndctl/lib/libndctl.c
2eb93d
+++ b/ndctl/lib/libndctl.c
2eb93d
@@ -1668,7 +1668,6 @@ static enum ndctl_fwa_result fwa_result_to_result(const char *result)
2eb93d
 static int ndctl_bind(struct ndctl_ctx *ctx, struct kmod_module *module,
2eb93d
 		const char *devname);
2eb93d
 static int ndctl_unbind(struct ndctl_ctx *ctx, const char *devpath);
2eb93d
-static struct kmod_module *to_module(struct ndctl_ctx *ctx, const char *alias);
2eb93d
 
2eb93d
 static int populate_dimm_attributes(struct ndctl_dimm *dimm,
2eb93d
 				    const char *dimm_base,
2eb93d
@@ -1878,7 +1877,7 @@ static void *add_dimm(void *parent, int id, const char *dimm_base)
2eb93d
 	sprintf(path, "%s/modalias", dimm_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
 		goto err_read;
2eb93d
-	dimm->module = to_module(ctx, buf);
2eb93d
+	dimm->module = util_modalias_to_module(ctx, buf);
2eb93d
 
2eb93d
 	dimm->handle = -1;
2eb93d
 	dimm->phys_id = -1;
2eb93d
@@ -2597,7 +2596,7 @@ static void *add_region(void *parent, int id, const char *region_base)
2eb93d
 	sprintf(path, "%s/modalias", region_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
 		goto err_read;
2eb93d
-	region->module = to_module(ctx, buf);
2eb93d
+	region->module = util_modalias_to_module(ctx, buf);
2eb93d
 
2eb93d
 	sprintf(path, "%s/numa_node", region_base);
2eb93d
 	if ((rc = sysfs_read_attr(ctx, path, buf)) == 0)
2eb93d
@@ -3885,28 +3884,6 @@ NDCTL_EXPORT struct ndctl_ctx *ndctl_mapping_get_ctx(
2eb93d
 	return ndctl_mapping_get_bus(mapping)->ctx;
2eb93d
 }
2eb93d
 
2eb93d
-static struct kmod_module *to_module(struct ndctl_ctx *ctx, const char *alias)
2eb93d
-{
2eb93d
-	struct kmod_list *list = NULL;
2eb93d
-	struct kmod_module *mod;
2eb93d
-	int rc;
2eb93d
-
2eb93d
-	if (!ctx->kmod_ctx)
2eb93d
-		return NULL;
2eb93d
-
2eb93d
-	rc = kmod_module_new_from_lookup(ctx->kmod_ctx, alias, &list);
2eb93d
-	if (rc < 0 || !list) {
2eb93d
-		dbg(ctx, "failed to find module for alias: %s %d list: %s\n",
2eb93d
-				alias, rc, list ? "populated" : "empty");
2eb93d
-		return NULL;
2eb93d
-	}
2eb93d
-	mod = kmod_module_get_module(list);
2eb93d
-	dbg(ctx, "alias: %s module: %s\n", alias, kmod_module_get_name(mod));
2eb93d
-	kmod_module_unref_list(list);
2eb93d
-
2eb93d
-	return mod;
2eb93d
-}
2eb93d
-
2eb93d
 static char *get_block_device(struct ndctl_ctx *ctx, const char *block_path)
2eb93d
 {
2eb93d
 	char *bdev_name = NULL;
2eb93d
@@ -4069,7 +4046,7 @@ static void *add_namespace(void *parent, int id, const char *ndns_base)
2eb93d
 	sprintf(path, "%s/modalias", ndns_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
 		goto err_read;
2eb93d
-	ndns->module = to_module(ctx, buf);
2eb93d
+	ndns->module = util_modalias_to_module(ctx, buf);
2eb93d
 
2eb93d
 	ndctl_namespace_foreach(region, ndns_dup)
2eb93d
 		if (ndns_dup->id == ndns->id) {
2eb93d
@@ -5182,7 +5159,7 @@ static void *add_btt(void *parent, int id, const char *btt_base)
2eb93d
 	sprintf(path, "%s/modalias", btt_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
 		goto err_read;
2eb93d
-	btt->module = to_module(ctx, buf);
2eb93d
+	btt->module = util_modalias_to_module(ctx, buf);
2eb93d
 
2eb93d
 	sprintf(path, "%s/uuid", btt_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
@@ -5533,7 +5510,7 @@ static void *__add_pfn(struct ndctl_pfn *pfn, const char *pfn_base)
2eb93d
 	sprintf(path, "%s/modalias", pfn_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
 		goto err_read;
2eb93d
-	pfn->module = to_module(ctx, buf);
2eb93d
+	pfn->module = util_modalias_to_module(ctx, buf);
2eb93d
 
2eb93d
 	sprintf(path, "%s/uuid", pfn_base);
2eb93d
 	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
diff --git a/util/sysfs.c b/util/sysfs.c
2eb93d
index cfbab7d..23330cb 100644
2eb93d
--- a/util/sysfs.c
2eb93d
+++ b/util/sysfs.c
2eb93d
@@ -10,6 +10,7 @@
2eb93d
 #include <ctype.h>
2eb93d
 #include <fcntl.h>
2eb93d
 #include <dirent.h>
2eb93d
+#include <libkmod.h>
2eb93d
 #include <sys/stat.h>
2eb93d
 #include <sys/types.h>
2eb93d
 #include <sys/ioctl.h>
2eb93d
@@ -118,3 +119,29 @@ int __sysfs_device_parse(struct log_ctx *ctx, const char *base_path,
2eb93d
 
2eb93d
 	return add_errors;
2eb93d
 }
2eb93d
+
2eb93d
+struct kmod_module *__util_modalias_to_module(struct kmod_ctx *kmod_ctx,
2eb93d
+					      const char *alias,
2eb93d
+					      struct log_ctx *log)
2eb93d
+{
2eb93d
+	struct kmod_list *list = NULL;
2eb93d
+	struct kmod_module *mod;
2eb93d
+	int rc;
2eb93d
+
2eb93d
+	if (!kmod_ctx)
2eb93d
+		return NULL;
2eb93d
+
2eb93d
+	rc = kmod_module_new_from_lookup(kmod_ctx, alias, &list);
2eb93d
+	if (rc < 0 || !list) {
2eb93d
+		log_dbg(log,
2eb93d
+			"failed to find module for alias: %s %d list: %s\n",
2eb93d
+			alias, rc, list ? "populated" : "empty");
2eb93d
+		return NULL;
2eb93d
+	}
2eb93d
+	mod = kmod_module_get_module(list);
2eb93d
+	log_dbg(log, "alias: %s module: %s\n", alias,
2eb93d
+		kmod_module_get_name(mod));
2eb93d
+	kmod_module_unref_list(list);
2eb93d
+
2eb93d
+	return mod;
2eb93d
+}
2eb93d
diff --git a/util/sysfs.h b/util/sysfs.h
2eb93d
index 6485a73..bdee4f5 100644
2eb93d
--- a/util/sysfs.h
2eb93d
+++ b/util/sysfs.h
2eb93d
@@ -27,4 +27,12 @@ static inline const char *devpath_to_devname(const char *devpath)
2eb93d
 {
2eb93d
 	return strrchr(devpath, '/') + 1;
2eb93d
 }
2eb93d
+
2eb93d
+struct kmod_ctx;
2eb93d
+struct kmod_module;
2eb93d
+struct kmod_module *__util_modalias_to_module(struct kmod_ctx *kmod_ctx,
2eb93d
+					      const char *alias,
2eb93d
+					      struct log_ctx *log);
2eb93d
+#define util_modalias_to_module(ctx, buf)                                      \
2eb93d
+	__util_modalias_to_module((ctx)->kmod_ctx, buf, &(ctx)->ctx)
2eb93d
 #endif /* __UTIL_SYSFS_H__ */
2eb93d
-- 
2eb93d
2.27.0
2eb93d