From: Valentine Fatiev <valentinef@nvidia.com>
Subject: [PATCH] BACKPORT: drivers/nvme/target/configfs.c

Change-Id: I29301dc54b2dae1eb408390ee655686625fdcabd
---
 drivers/nvme/target/configfs.c | 95 +++++++++++++++++++++++++++-------
 1 file changed, 75 insertions(+), 20 deletions(-)

--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -18,8 +18,13 @@
 
 #include "nvmet.h"
 
-static const struct config_item_type nvmet_host_type;
-static const struct config_item_type nvmet_subsys_type;
+#ifdef MLX_CONFIG_ITEM_TYPE_CONST
+#undef MLX_CONFIG_ITEM_TYPE_CONST
+#endif
+#define MLX_CONFIG_ITEM_TYPE_CONST
+
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_host_type;
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_subsys_type;
 
 static LIST_HEAD(nvmet_ports_list);
 struct list_head *nvmet_ports = &nvmet_ports_list;
@@ -253,6 +258,7 @@ static ssize_t nvmet_param_inline_data_s
 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
 
 #ifdef CONFIG_BLK_DEV_INTEGRITY
+#ifdef HAVE_BLKDEV_BIO_INTEGRITY_BYTES
 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
 		char *page)
 {
@@ -279,6 +285,7 @@ static ssize_t nvmet_param_pi_enable_sto
 
 CONFIGFS_ATTR(nvmet_, param_pi_enable);
 #endif
+#endif
 
 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
 		char *page)
@@ -495,7 +502,12 @@ static ssize_t nvmet_ns_device_path_stor
 
 	kfree(ns->device_path);
 	ret = -ENOMEM;
+#ifdef HAVE_KMEMDUP_NUL
 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
+#else
+	ns->device_path = kstrndup(page, len, GFP_KERNEL);
+#endif
+
 	if (!ns->device_path)
 		goto out_unlock;
 
@@ -905,12 +917,13 @@ static struct configfs_item_operations n
 	.release		= nvmet_ns_release,
 };
 
-static const struct config_item_type nvmet_ns_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_ns_type = {
 	.ct_item_ops		= &nvmet_ns_item_ops,
 	.ct_attrs		= nvmet_ns_attrs,
 	.ct_owner		= THIS_MODULE,
 };
 
+#ifdef HAVE_CONFIGFS_REGISTER_GROUP
 static ssize_t nvmet_offload_ctx_traddr_show(struct config_item *item, char *page)
 {
 	struct nvmet_offload_ctx *ctx = to_nvmet_offload_ctx(item);
@@ -968,14 +981,15 @@ static struct configfs_attribute *nvmet_
 	NULL,
 };
 
-static const struct config_item_type nvmet_offload_ctx_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_offload_ctx_type = {
 	.ct_attrs		= nvmet_offload_ctx_attrs,
 	.ct_owner		= THIS_MODULE,
 };
 
-static const struct config_item_type nvmet_offload_ctxs_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_offload_ctxs_type = {
 	.ct_owner		= THIS_MODULE,
 };
+#endif
 
 static struct config_group *nvmet_ns_make(struct config_group *group,
 		const char *name)
@@ -1001,10 +1015,12 @@ static struct config_group *nvmet_ns_mak
 		goto out;
 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
 
+#ifdef HAVE_CONFIGFS_REGISTER_GROUP
 	config_group_init_type_name(&ns->offload_ctxs_group,
 			"offload_ctxs", &nvmet_offload_ctxs_type);
 	configfs_add_default_group(&ns->offload_ctxs_group,
 			&ns->group);
+#endif
 
 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
 
@@ -1017,7 +1033,7 @@ static struct configfs_group_operations
 	.make_group		= nvmet_ns_make,
 };
 
-static const struct config_item_type nvmet_namespaces_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_namespaces_type = {
 	.ct_group_ops		= &nvmet_namespaces_group_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -1158,7 +1174,7 @@ static struct configfs_attribute *nvmet_
 	NULL,
 };
 
-static const struct config_item_type nvmet_passthru_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_passthru_type = {
 	.ct_attrs		= nvmet_passthru_attrs,
 	.ct_owner		= THIS_MODULE,
 };
@@ -1238,8 +1254,13 @@ out_free_link:
 	return ret;
 }
 
+#ifndef HAVE_CONFIGFS_DROP_LINK_RETURNS_INT
 static void nvmet_port_subsys_drop_link(struct config_item *parent,
 		struct config_item *target)
+#else
+static int nvmet_port_subsys_drop_link(struct config_item *parent,
+		struct config_item *target)
+#endif
 {
 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
 	struct nvmet_subsys *subsys = to_subsys(target);
@@ -1251,7 +1272,11 @@ static void nvmet_port_subsys_drop_link(
 			goto found;
 	}
 	up_write(&nvmet_config_sem);
+#ifndef HAVE_CONFIGFS_DROP_LINK_RETURNS_INT
 	return;
+#else
+	return -EINVAL;
+#endif
 
 found:
 	list_del(&p->entry);
@@ -1266,6 +1291,9 @@ found:
 		nvmet_uninit_offload_subsystem_port_attrs(subsys);
 	up_write(&nvmet_config_sem);
 	kfree(p);
+#ifdef HAVE_CONFIGFS_DROP_LINK_RETURNS_INT
+	return 0;
+#endif
 }
 
 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
@@ -1273,7 +1301,7 @@ static struct configfs_item_operations n
 	.drop_link		= nvmet_port_subsys_drop_link,
 };
 
-static const struct config_item_type nvmet_port_subsys_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_port_subsys_type = {
 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -1320,8 +1348,14 @@ out_free_link:
 	return ret;
 }
 
+#ifndef HAVE_CONFIGFS_DROP_LINK_RETURNS_INT
 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
 		struct config_item *target)
+#else
+static int nvmet_allowed_hosts_drop_link(struct config_item *parent,
+		struct config_item *target)
+
+#endif
 {
 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
 	struct nvmet_host *host = to_host(target);
@@ -1333,7 +1367,11 @@ static void nvmet_allowed_hosts_drop_lin
 			goto found;
 	}
 	up_write(&nvmet_config_sem);
+#ifndef HAVE_CONFIGFS_DROP_LINK_RETURNS_INT
 	return;
+#else
+	return -EINVAL;
+#endif
 
 found:
 	list_del(&p->entry);
@@ -1341,6 +1379,9 @@ found:
 
 	up_write(&nvmet_config_sem);
 	kfree(p);
+#ifdef HAVE_CONFIGFS_DROP_LINK_RETURNS_INT
+	return 0;
+#endif
 }
 
 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
@@ -1348,7 +1389,7 @@ static struct configfs_item_operations n
 	.drop_link		= nvmet_allowed_hosts_drop_link,
 };
 
-static const struct config_item_type nvmet_allowed_hosts_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_allowed_hosts_type = {
 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -1609,7 +1650,11 @@ static ssize_t nvmet_subsys_attr_model_s
 			return -EINVAL;
 	}
 
+#ifdef HAVE_KMEMDUP_NUL
 	subsys->model_number = kmemdup_nul(page, len, GFP_KERNEL);
+#else
+	subsys->model_number = kstrndup(page, len, GFP_KERNEL);
+#endif
 	if (!subsys->model_number)
 		return -ENOMEM;
 	return count;
@@ -1632,6 +1677,7 @@ static ssize_t nvmet_subsys_attr_model_s
 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
 
 #ifdef CONFIG_BLK_DEV_INTEGRITY
+#ifdef HAVE_BLKDEV_BIO_INTEGRITY_BYTES
 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
 						char *page)
 {
@@ -1652,6 +1698,7 @@ static ssize_t nvmet_subsys_attr_pi_enab
 }
 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
 #endif
+#endif
  
 static ssize_t
 nvmet_subsys_attr_offload_subsys_unknown_ns_cmds_show(struct config_item *item,
@@ -1750,9 +1797,11 @@ static struct configfs_attribute *nvmet_
 	&nvmet_subsys_attr_attr_cntlid_min,
 	&nvmet_subsys_attr_attr_cntlid_max,
 	&nvmet_subsys_attr_attr_model,
+#ifdef HAVE_BLKDEV_BIO_INTEGRITY_BYTES
 #ifdef CONFIG_BLK_DEV_INTEGRITY
 	&nvmet_subsys_attr_attr_pi_enable,
 #endif
+#endif
 	&nvmet_subsys_attr_attr_offload,
 	&nvmet_subsys_attr_attr_offload_subsys_unknown_ns_cmds,
 	NULL,
@@ -1773,7 +1822,7 @@ static struct configfs_item_operations n
 	.release		= nvmet_subsys_release,
 };
 
-static const struct config_item_type nvmet_subsys_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_subsys_type = {
 	.ct_item_ops		= &nvmet_subsys_item_ops,
 	.ct_attrs		= nvmet_subsys_attrs,
 	.ct_owner		= THIS_MODULE,
@@ -1813,7 +1862,7 @@ static struct configfs_group_operations
 	.make_group		= nvmet_subsys_make,
 };
 
-static const struct config_item_type nvmet_subsystems_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_subsystems_type = {
 	.ct_group_ops		= &nvmet_subsystems_group_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -1881,7 +1930,7 @@ static struct configfs_item_operations n
 	.release	= nvmet_referral_release,
 };
 
-static const struct config_item_type nvmet_referral_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_referral_type = {
 	.ct_owner	= THIS_MODULE,
 	.ct_attrs	= nvmet_referral_attrs,
 	.ct_item_ops	= &nvmet_referral_item_ops,
@@ -1907,7 +1956,7 @@ static struct configfs_group_operations
 	.disconnect_notify	= nvmet_referral_notify,
 };
 
-static const struct config_item_type nvmet_referrals_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_referrals_type = {
 	.ct_owner	= THIS_MODULE,
 	.ct_group_ops	= &nvmet_referral_group_ops,
 };
@@ -1986,7 +2035,7 @@ static struct configfs_item_operations n
 	.release		= nvmet_ana_group_release,
 };
 
-static const struct config_item_type nvmet_ana_group_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_ana_group_type = {
 	.ct_item_ops		= &nvmet_ana_group_item_ops,
 	.ct_attrs		= nvmet_ana_group_attrs,
 	.ct_owner		= THIS_MODULE,
@@ -2031,7 +2080,7 @@ static struct configfs_group_operations
 	.make_group		= nvmet_ana_groups_make_group,
 };
 
-static const struct config_item_type nvmet_ana_groups_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_ana_groups_type = {
 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -2064,8 +2113,10 @@ static struct configfs_attribute *nvmet_
 	&nvmet_attr_param_offload_queue_size,
 	&nvmet_attr_param_offload_passthrough_sqe_rw,
 #ifdef CONFIG_BLK_DEV_INTEGRITY
+#ifdef HAVE_BLKDEV_BIO_INTEGRITY_BYTES
 	&nvmet_attr_param_pi_enable,
 #endif
+#endif
 	NULL,
 };
 
@@ -2073,7 +2124,7 @@ static struct configfs_item_operations n
 	.release		= nvmet_port_release,
 };
 
-static const struct config_item_type nvmet_port_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_port_type = {
 	.ct_attrs		= nvmet_port_attrs,
 	.ct_item_ops		= &nvmet_port_item_ops,
 	.ct_owner		= THIS_MODULE,
@@ -2149,7 +2200,7 @@ static struct configfs_group_operations
 	.make_group		= nvmet_ports_make,
 };
 
-static const struct config_item_type nvmet_ports_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_ports_type = {
 	.ct_group_ops		= &nvmet_ports_group_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -2168,7 +2219,7 @@ static struct configfs_item_operations n
 	.release		= nvmet_host_release,
 };
 
-static const struct config_item_type nvmet_host_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_host_type = {
 	.ct_item_ops		= &nvmet_host_item_ops,
 	.ct_owner		= THIS_MODULE,
 };
@@ -2191,14 +2242,14 @@ static struct configfs_group_operations
 	.make_group		= nvmet_hosts_make_group,
 };
 
-static const struct config_item_type nvmet_hosts_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_hosts_type = {
 	.ct_group_ops		= &nvmet_hosts_group_ops,
 	.ct_owner		= THIS_MODULE,
 };
 
 static struct config_group nvmet_hosts_group;
 
-static const struct config_item_type nvmet_root_type = {
+static MLX_CONFIG_ITEM_TYPE_CONST struct config_item_type nvmet_root_type = {
 	.ct_owner		= THIS_MODULE,
 };
 
@@ -2213,16 +2264,19 @@ static struct configfs_subsystem nvmet_c
 
 void nvmet_offload_ctx_configfs_del(struct nvmet_offload_ctx *ctx)
 {
+#ifdef HAVE_CONFIGFS_REGISTER_GROUP
 	if (d_inode(ctx->group.cg_item.ci_dentry)) {
 		pr_info("Removing offload ctx %d from configfs\n", ctx->id);
 		configfs_unregister_group(&ctx->group);
 	}
+#endif
 }
 EXPORT_SYMBOL_GPL(nvmet_offload_ctx_configfs_del);
 
 int nvmet_offload_ctx_configfs_create(struct nvmet_offload_ctx *ctx)
 {
 	int res = 0;
+#ifdef HAVE_CONFIGFS_REGISTER_GROUP
 	char name[CONFIGFS_ITEM_NAME_LEN];
 
 	sprintf(name, "%d", ctx->id);
@@ -2235,6 +2289,7 @@ int nvmet_offload_ctx_configfs_create(st
 	if (res)
 		pr_err("failed to register configfs group for offload ctx %s\n",
 		       name);
+#endif
 
 	return res;
 }
