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

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