Blame SOURCES/0114-util-Implement-common-bind-unbind-helpers.patch

2eb93d
From e31fc778998b4d02ffec68e61869aaeccfd99be8 Mon Sep 17 00:00:00 2001
2eb93d
From: Dan Williams <dan.j.williams@intel.com>
2eb93d
Date: Sun, 23 Jan 2022 16:54:17 -0800
2eb93d
Subject: [PATCH 114/217] util: Implement common bind/unbind helpers
2eb93d
2eb93d
Refactor ndctl_{bind,unbind}() into util_{bind,unbind}() for libcxl to
2eb93d
reuse.
2eb93d
2eb93d
daxctl can not join the party for now as it needs to play games with
2eb93d
'new_id'.
2eb93d
2eb93d
Link: https://lore.kernel.org/r/164298565707.3021641.7763459936156744907.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 | 103 +++++--------------------------------------
2eb93d
 util/sysfs.c         |  76 +++++++++++++++++++++++++++++++
2eb93d
 util/sysfs.h         |   8 ++++
2eb93d
 3 files changed, 96 insertions(+), 91 deletions(-)
2eb93d
2eb93d
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
2eb93d
index 1374ad9..98d184b 100644
2eb93d
--- a/ndctl/lib/libndctl.c
2eb93d
+++ b/ndctl/lib/libndctl.c
2eb93d
@@ -1665,10 +1665,6 @@ static enum ndctl_fwa_result fwa_result_to_result(const char *result)
2eb93d
 	return NDCTL_FWA_RESULT_INVALID;
2eb93d
 }
2eb93d
 
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
-
2eb93d
 static int populate_dimm_attributes(struct ndctl_dimm *dimm,
2eb93d
 				    const char *dimm_base,
2eb93d
 				    const char *bus_prefix)
2eb93d
@@ -2305,7 +2301,7 @@ NDCTL_EXPORT int ndctl_dimm_disable(struct ndctl_dimm *dimm)
2eb93d
 	if (!ndctl_dimm_is_enabled(dimm))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_unbind(ctx, dimm->dimm_path);
2eb93d
+	util_unbind(dimm->dimm_path, ctx);
2eb93d
 
2eb93d
 	if (ndctl_dimm_is_enabled(dimm)) {
2eb93d
 		err(ctx, "%s: failed to disable\n", devname);
2eb93d
@@ -2324,7 +2320,7 @@ NDCTL_EXPORT int ndctl_dimm_enable(struct ndctl_dimm *dimm)
2eb93d
 	if (ndctl_dimm_is_enabled(dimm))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_bind(ctx, dimm->module, devname);
2eb93d
+	util_bind(devname, dimm->module, "nd", ctx);
2eb93d
 
2eb93d
 	if (!ndctl_dimm_is_enabled(dimm)) {
2eb93d
 		err(ctx, "%s: failed to enable\n", devname);
2eb93d
@@ -3573,7 +3569,7 @@ NDCTL_EXPORT int ndctl_region_enable(struct ndctl_region *region)
2eb93d
 	if (ndctl_region_is_enabled(region))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_bind(ctx, region->module, devname);
2eb93d
+	util_bind(devname, region->module, "nd", ctx);
2eb93d
 
2eb93d
 	if (!ndctl_region_is_enabled(region)) {
2eb93d
 		err(ctx, "%s: failed to enable\n", devname);
2eb93d
@@ -3610,7 +3606,7 @@ static int ndctl_region_disable(struct ndctl_region *region, int cleanup)
2eb93d
 	if (!ndctl_region_is_enabled(region))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_unbind(ctx, region->region_path);
2eb93d
+	util_unbind(region->region_path, ctx);
2eb93d
 
2eb93d
 	if (ndctl_region_is_enabled(region)) {
2eb93d
 		err(ctx, "%s: failed to disable\n", devname);
2eb93d
@@ -4373,81 +4369,6 @@ NDCTL_EXPORT struct badblock *ndctl_namespace_get_first_badblock(
2eb93d
 	return badblocks_iter_first(&ndns->bb_iter, ctx, path);
2eb93d
 }
2eb93d
 
2eb93d
-static int ndctl_bind(struct ndctl_ctx *ctx, struct kmod_module *module,
2eb93d
-		const char *devname)
2eb93d
-{
2eb93d
-	DIR *dir;
2eb93d
-	int rc = 0;
2eb93d
-	char path[200];
2eb93d
-	struct dirent *de;
2eb93d
-	const int len = sizeof(path);
2eb93d
-
2eb93d
-	if (!devname) {
2eb93d
-		err(ctx, "missing devname\n");
2eb93d
-		return -EINVAL;
2eb93d
-	}
2eb93d
-
2eb93d
-	if (module) {
2eb93d
-		rc = kmod_module_probe_insert_module(module,
2eb93d
-				KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL,
2eb93d
-				NULL);
2eb93d
-		if (rc < 0) {
2eb93d
-			err(ctx, "%s: insert failure: %d\n", __func__, rc);
2eb93d
-			return rc;
2eb93d
-		}
2eb93d
-	}
2eb93d
-
2eb93d
-	if (snprintf(path, len, "/sys/bus/nd/drivers") >= len) {
2eb93d
-		err(ctx, "%s: buffer too small!\n", devname);
2eb93d
-		return -ENXIO;
2eb93d
-	}
2eb93d
-
2eb93d
-	dir = opendir(path);
2eb93d
-	if (!dir) {
2eb93d
-		err(ctx, "%s: opendir(\"%s\") failed\n", devname, path);
2eb93d
-		return -ENXIO;
2eb93d
-	}
2eb93d
-
2eb93d
-	while ((de = readdir(dir)) != NULL) {
2eb93d
-		char *drv_path;
2eb93d
-
2eb93d
-		if (de->d_ino == 0)
2eb93d
-			continue;
2eb93d
-		if (de->d_name[0] == '.')
2eb93d
-			continue;
2eb93d
-		if (asprintf(&drv_path, "%s/%s/bind", path, de->d_name) < 0) {
2eb93d
-			err(ctx, "%s: path allocation failure\n", devname);
2eb93d
-			continue;
2eb93d
-		}
2eb93d
-
2eb93d
-		rc = sysfs_write_attr_quiet(ctx, drv_path, devname);
2eb93d
-		free(drv_path);
2eb93d
-		if (rc == 0)
2eb93d
-			break;
2eb93d
-	}
2eb93d
-	closedir(dir);
2eb93d
-
2eb93d
-	if (rc) {
2eb93d
-		dbg(ctx, "%s: bind failed\n", devname);
2eb93d
-		return -ENXIO;
2eb93d
-	}
2eb93d
-	return 0;
2eb93d
-}
2eb93d
-
2eb93d
-static int ndctl_unbind(struct ndctl_ctx *ctx, const char *devpath)
2eb93d
-{
2eb93d
-	const char *devname = devpath_to_devname(devpath);
2eb93d
-	char path[200];
2eb93d
-	const int len = sizeof(path);
2eb93d
-
2eb93d
-	if (snprintf(path, len, "%s/driver/unbind", devpath) >= len) {
2eb93d
-		err(ctx, "%s: buffer too small!\n", devname);
2eb93d
-		return -ENXIO;
2eb93d
-	}
2eb93d
-
2eb93d
-	return sysfs_write_attr(ctx, path, devname);
2eb93d
-}
2eb93d
-
2eb93d
 static void *add_btt(void *parent, int id, const char *btt_base);
2eb93d
 static void *add_pfn(void *parent, int id, const char *pfn_base);
2eb93d
 static void *add_dax(void *parent, int id, const char *dax_base);
2eb93d
@@ -4533,7 +4454,7 @@ NDCTL_EXPORT int ndctl_namespace_enable(struct ndctl_namespace *ndns)
2eb93d
 	if (ndctl_namespace_is_enabled(ndns))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	rc = ndctl_bind(ctx, ndns->module, devname);
2eb93d
+	rc = util_bind(devname, ndns->module, "nd", ctx);
2eb93d
 
2eb93d
 	/*
2eb93d
 	 * Rescan now as successfully enabling a namespace device leads
2eb93d
@@ -4581,7 +4502,7 @@ NDCTL_EXPORT int ndctl_namespace_disable(struct ndctl_namespace *ndns)
2eb93d
 	if (!ndctl_namespace_is_enabled(ndns))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_unbind(ctx, ndns->ndns_path);
2eb93d
+	util_unbind(ndns->ndns_path, ctx);
2eb93d
 
2eb93d
 	if (ndctl_namespace_is_enabled(ndns)) {
2eb93d
 		err(ctx, "%s: failed to disable\n", devname);
2eb93d
@@ -5420,7 +5341,7 @@ NDCTL_EXPORT int ndctl_btt_enable(struct ndctl_btt *btt)
2eb93d
 	if (ndctl_btt_is_enabled(btt))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_bind(ctx, btt->module, devname);
2eb93d
+	util_bind(devname, btt->module, "nd", ctx);
2eb93d
 
2eb93d
 	if (!ndctl_btt_is_enabled(btt)) {
2eb93d
 		err(ctx, "%s: failed to enable\n", devname);
2eb93d
@@ -5457,7 +5378,7 @@ NDCTL_EXPORT int ndctl_btt_delete(struct ndctl_btt *btt)
2eb93d
 		return 0;
2eb93d
 	}
2eb93d
 
2eb93d
-	ndctl_unbind(ctx, btt->btt_path);
2eb93d
+	util_unbind(btt->btt_path, ctx);
2eb93d
 
2eb93d
 	rc = ndctl_btt_set_namespace(btt, NULL);
2eb93d
 	if (rc) {
2eb93d
@@ -5908,7 +5829,7 @@ NDCTL_EXPORT int ndctl_pfn_enable(struct ndctl_pfn *pfn)
2eb93d
 	if (ndctl_pfn_is_enabled(pfn))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_bind(ctx, pfn->module, devname);
2eb93d
+	util_bind(devname, pfn->module, "nd", ctx);
2eb93d
 
2eb93d
 	if (!ndctl_pfn_is_enabled(pfn)) {
2eb93d
 		err(ctx, "%s: failed to enable\n", devname);
2eb93d
@@ -5945,7 +5866,7 @@ NDCTL_EXPORT int ndctl_pfn_delete(struct ndctl_pfn *pfn)
2eb93d
 		return 0;
2eb93d
 	}
2eb93d
 
2eb93d
-	ndctl_unbind(ctx, pfn->pfn_path);
2eb93d
+	util_unbind(pfn->pfn_path, ctx);
2eb93d
 
2eb93d
 	rc = ndctl_pfn_set_namespace(pfn, NULL);
2eb93d
 	if (rc) {
2eb93d
@@ -6101,7 +6022,7 @@ NDCTL_EXPORT int ndctl_dax_enable(struct ndctl_dax *dax)
2eb93d
 	if (ndctl_dax_is_enabled(dax))
2eb93d
 		return 0;
2eb93d
 
2eb93d
-	ndctl_bind(ctx, pfn->module, devname);
2eb93d
+	util_bind(devname, pfn->module, "nd", ctx);
2eb93d
 
2eb93d
 	if (!ndctl_dax_is_enabled(dax)) {
2eb93d
 		err(ctx, "%s: failed to enable\n", devname);
2eb93d
@@ -6132,7 +6053,7 @@ NDCTL_EXPORT int ndctl_dax_delete(struct ndctl_dax *dax)
2eb93d
 		return 0;
2eb93d
 	}
2eb93d
 
2eb93d
-	ndctl_unbind(ctx, pfn->pfn_path);
2eb93d
+	util_unbind(pfn->pfn_path, ctx);
2eb93d
 
2eb93d
 	rc = ndctl_dax_set_namespace(dax, NULL);
2eb93d
 	if (rc) {
2eb93d
diff --git a/util/sysfs.c b/util/sysfs.c
2eb93d
index 23330cb..968683b 100644
2eb93d
--- a/util/sysfs.c
2eb93d
+++ b/util/sysfs.c
2eb93d
@@ -145,3 +145,79 @@ struct kmod_module *__util_modalias_to_module(struct kmod_ctx *kmod_ctx,
2eb93d
 
2eb93d
 	return mod;
2eb93d
 }
2eb93d
+
2eb93d
+int __util_bind(const char *devname, struct kmod_module *module,
2eb93d
+		const char *bus, struct log_ctx *ctx)
2eb93d
+{
2eb93d
+	DIR *dir;
2eb93d
+	int rc = 0;
2eb93d
+	char path[200];
2eb93d
+	struct dirent *de;
2eb93d
+	const int len = sizeof(path);
2eb93d
+
2eb93d
+	if (!devname) {
2eb93d
+		log_err(ctx, "missing devname\n");
2eb93d
+		return -EINVAL;
2eb93d
+	}
2eb93d
+
2eb93d
+	if (module) {
2eb93d
+		rc = kmod_module_probe_insert_module(module,
2eb93d
+						     KMOD_PROBE_APPLY_BLACKLIST,
2eb93d
+						     NULL, NULL, NULL, NULL);
2eb93d
+		if (rc < 0) {
2eb93d
+			log_err(ctx, "%s: insert failure: %d\n", __func__, rc);
2eb93d
+			return rc;
2eb93d
+		}
2eb93d
+	}
2eb93d
+
2eb93d
+	if (snprintf(path, len, "/sys/bus/%s/drivers", bus) >= len) {
2eb93d
+		log_err(ctx, "%s: buffer too small!\n", devname);
2eb93d
+		return -ENXIO;
2eb93d
+	}
2eb93d
+
2eb93d
+	dir = opendir(path);
2eb93d
+	if (!dir) {
2eb93d
+		log_err(ctx, "%s: opendir(\"%s\") failed\n", devname, path);
2eb93d
+		return -ENXIO;
2eb93d
+	}
2eb93d
+
2eb93d
+	while ((de = readdir(dir)) != NULL) {
2eb93d
+		char *drv_path;
2eb93d
+
2eb93d
+		if (de->d_ino == 0)
2eb93d
+			continue;
2eb93d
+		if (de->d_name[0] == '.')
2eb93d
+			continue;
2eb93d
+
2eb93d
+		if (asprintf(&drv_path, "%s/%s/bind", path, de->d_name) < 0) {
2eb93d
+			log_err(ctx, "%s: path allocation failure\n", devname);
2eb93d
+			continue;
2eb93d
+		}
2eb93d
+
2eb93d
+		rc = __sysfs_write_attr_quiet(ctx, drv_path, devname);
2eb93d
+		free(drv_path);
2eb93d
+		if (rc == 0)
2eb93d
+			break;
2eb93d
+	}
2eb93d
+	closedir(dir);
2eb93d
+
2eb93d
+	if (rc) {
2eb93d
+		log_dbg(ctx, "%s: bind failed\n", devname);
2eb93d
+		return -ENXIO;
2eb93d
+	}
2eb93d
+	return 0;
2eb93d
+}
2eb93d
+
2eb93d
+int __util_unbind(const char *devpath, struct log_ctx *ctx)
2eb93d
+{
2eb93d
+	const char *devname = devpath_to_devname(devpath);
2eb93d
+	char path[200];
2eb93d
+	const int len = sizeof(path);
2eb93d
+
2eb93d
+	if (snprintf(path, len, "%s/driver/unbind", devpath) >= len) {
2eb93d
+		log_err(ctx, "%s: buffer too small!\n", devname);
2eb93d
+		return -ENXIO;
2eb93d
+	}
2eb93d
+
2eb93d
+	return __sysfs_write_attr(ctx, path, devname);
2eb93d
+}
2eb93d
diff --git a/util/sysfs.h b/util/sysfs.h
2eb93d
index bdee4f5..4c95c70 100644
2eb93d
--- a/util/sysfs.h
2eb93d
+++ b/util/sysfs.h
2eb93d
@@ -35,4 +35,12 @@ struct kmod_module *__util_modalias_to_module(struct kmod_ctx *kmod_ctx,
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
+
2eb93d
+int __util_bind(const char *devname, struct kmod_module *module, const char *bus,
2eb93d
+	      struct log_ctx *ctx);
2eb93d
+#define util_bind(n, m, b, c) __util_bind(n, m, b, &(c)->ctx)
2eb93d
+
2eb93d
+int __util_unbind(const char *devpath, struct log_ctx *ctx);
2eb93d
+#define util_unbind(p, c) __util_unbind(p, &(c)->ctx)
2eb93d
+
2eb93d
 #endif /* __UTIL_SYSFS_H__ */
2eb93d
-- 
2eb93d
2.27.0
2eb93d