2 * Configfs interface for the NVMe target.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 #include <linux/ctype.h>
23 static const struct config_item_type nvmet_host_type;
24 static const struct config_item_type nvmet_subsys_type;
26 static const struct nvmet_transport_name {
29 } nvmet_transport_names[] = {
30 { NVMF_TRTYPE_RDMA, "rdma" },
31 { NVMF_TRTYPE_FC, "fc" },
32 { NVMF_TRTYPE_LOOP, "loop" },
36 * nvmet_port Generic ConfigFS definitions.
37 * Used in any place in the ConfigFS tree that refers to an address.
39 static ssize_t nvmet_addr_adrfam_show(struct config_item *item,
42 switch (to_nvmet_port(item)->disc_addr.adrfam) {
43 case NVMF_ADDR_FAMILY_IP4:
44 return sprintf(page, "ipv4\n");
45 case NVMF_ADDR_FAMILY_IP6:
46 return sprintf(page, "ipv6\n");
47 case NVMF_ADDR_FAMILY_IB:
48 return sprintf(page, "ib\n");
49 case NVMF_ADDR_FAMILY_FC:
50 return sprintf(page, "fc\n");
52 return sprintf(page, "\n");
56 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
57 const char *page, size_t count)
59 struct nvmet_port *port = to_nvmet_port(item);
62 pr_err("Cannot modify address while enabled\n");
63 pr_err("Disable the address before modifying\n");
67 if (sysfs_streq(page, "ipv4")) {
68 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP4;
69 } else if (sysfs_streq(page, "ipv6")) {
70 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP6;
71 } else if (sysfs_streq(page, "ib")) {
72 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IB;
73 } else if (sysfs_streq(page, "fc")) {
74 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_FC;
76 pr_err("Invalid value '%s' for adrfam\n", page);
83 CONFIGFS_ATTR(nvmet_, addr_adrfam);
85 static ssize_t nvmet_addr_portid_show(struct config_item *item,
88 struct nvmet_port *port = to_nvmet_port(item);
90 return snprintf(page, PAGE_SIZE, "%d\n",
91 le16_to_cpu(port->disc_addr.portid));
94 static ssize_t nvmet_addr_portid_store(struct config_item *item,
95 const char *page, size_t count)
97 struct nvmet_port *port = to_nvmet_port(item);
100 if (kstrtou16(page, 0, &portid)) {
101 pr_err("Invalid value '%s' for portid\n", page);
106 pr_err("Cannot modify address while enabled\n");
107 pr_err("Disable the address before modifying\n");
110 port->disc_addr.portid = cpu_to_le16(portid);
114 CONFIGFS_ATTR(nvmet_, addr_portid);
116 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
119 struct nvmet_port *port = to_nvmet_port(item);
121 return snprintf(page, PAGE_SIZE, "%s\n",
122 port->disc_addr.traddr);
125 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
126 const char *page, size_t count)
128 struct nvmet_port *port = to_nvmet_port(item);
130 if (count > NVMF_TRADDR_SIZE) {
131 pr_err("Invalid value '%s' for traddr\n", page);
136 pr_err("Cannot modify address while enabled\n");
137 pr_err("Disable the address before modifying\n");
141 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
146 CONFIGFS_ATTR(nvmet_, addr_traddr);
148 static ssize_t nvmet_addr_treq_show(struct config_item *item,
151 switch (to_nvmet_port(item)->disc_addr.treq) {
152 case NVMF_TREQ_NOT_SPECIFIED:
153 return sprintf(page, "not specified\n");
154 case NVMF_TREQ_REQUIRED:
155 return sprintf(page, "required\n");
156 case NVMF_TREQ_NOT_REQUIRED:
157 return sprintf(page, "not required\n");
159 return sprintf(page, "\n");
163 static ssize_t nvmet_addr_treq_store(struct config_item *item,
164 const char *page, size_t count)
166 struct nvmet_port *port = to_nvmet_port(item);
169 pr_err("Cannot modify address while enabled\n");
170 pr_err("Disable the address before modifying\n");
174 if (sysfs_streq(page, "not specified")) {
175 port->disc_addr.treq = NVMF_TREQ_NOT_SPECIFIED;
176 } else if (sysfs_streq(page, "required")) {
177 port->disc_addr.treq = NVMF_TREQ_REQUIRED;
178 } else if (sysfs_streq(page, "not required")) {
179 port->disc_addr.treq = NVMF_TREQ_NOT_REQUIRED;
181 pr_err("Invalid value '%s' for treq\n", page);
188 CONFIGFS_ATTR(nvmet_, addr_treq);
190 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
193 struct nvmet_port *port = to_nvmet_port(item);
195 return snprintf(page, PAGE_SIZE, "%s\n",
196 port->disc_addr.trsvcid);
199 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
200 const char *page, size_t count)
202 struct nvmet_port *port = to_nvmet_port(item);
204 if (count > NVMF_TRSVCID_SIZE) {
205 pr_err("Invalid value '%s' for trsvcid\n", page);
209 pr_err("Cannot modify address while enabled\n");
210 pr_err("Disable the address before modifying\n");
214 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
219 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
221 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
224 struct nvmet_port *port = to_nvmet_port(item);
227 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
228 if (port->disc_addr.trtype != nvmet_transport_names[i].type)
230 return sprintf(page, "%s\n", nvmet_transport_names[i].name);
233 return sprintf(page, "\n");
236 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
238 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
239 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
240 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
243 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
244 const char *page, size_t count)
246 struct nvmet_port *port = to_nvmet_port(item);
250 pr_err("Cannot modify address while enabled\n");
251 pr_err("Disable the address before modifying\n");
255 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
256 if (sysfs_streq(page, nvmet_transport_names[i].name))
260 pr_err("Invalid value '%s' for trtype\n", page);
263 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
264 port->disc_addr.trtype = nvmet_transport_names[i].type;
265 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
266 nvmet_port_init_tsas_rdma(port);
270 CONFIGFS_ATTR(nvmet_, addr_trtype);
273 * Namespace structures & file operation functions below
275 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
277 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
280 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
281 const char *page, size_t count)
283 struct nvmet_ns *ns = to_nvmet_ns(item);
284 struct nvmet_subsys *subsys = ns->subsys;
288 mutex_lock(&subsys->lock);
294 len = strcspn(page, "\n");
298 kfree(ns->device_path);
300 ns->device_path = kstrndup(page, len, GFP_KERNEL);
301 if (!ns->device_path)
304 mutex_unlock(&subsys->lock);
308 mutex_unlock(&subsys->lock);
312 CONFIGFS_ATTR(nvmet_ns_, device_path);
314 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
316 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
319 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
320 const char *page, size_t count)
322 struct nvmet_ns *ns = to_nvmet_ns(item);
323 struct nvmet_subsys *subsys = ns->subsys;
327 mutex_lock(&subsys->lock);
334 if (uuid_parse(page, &ns->uuid))
338 mutex_unlock(&subsys->lock);
339 return ret ? ret : count;
342 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
344 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
346 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
349 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
350 const char *page, size_t count)
352 struct nvmet_ns *ns = to_nvmet_ns(item);
353 struct nvmet_subsys *subsys = ns->subsys;
355 const char *p = page;
359 mutex_lock(&subsys->lock);
365 for (i = 0; i < 16; i++) {
366 if (p + 2 > page + count) {
370 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
375 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
378 if (*p == '-' || *p == ':')
382 memcpy(&ns->nguid, nguid, sizeof(nguid));
384 mutex_unlock(&subsys->lock);
385 return ret ? ret : count;
388 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
390 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
392 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
395 static ssize_t nvmet_ns_enable_store(struct config_item *item,
396 const char *page, size_t count)
398 struct nvmet_ns *ns = to_nvmet_ns(item);
402 if (strtobool(page, &enable))
406 ret = nvmet_ns_enable(ns);
408 nvmet_ns_disable(ns);
410 return ret ? ret : count;
413 CONFIGFS_ATTR(nvmet_ns_, enable);
415 static struct configfs_attribute *nvmet_ns_attrs[] = {
416 &nvmet_ns_attr_device_path,
417 &nvmet_ns_attr_device_nguid,
418 &nvmet_ns_attr_device_uuid,
419 &nvmet_ns_attr_enable,
423 static void nvmet_ns_release(struct config_item *item)
425 struct nvmet_ns *ns = to_nvmet_ns(item);
430 static struct configfs_item_operations nvmet_ns_item_ops = {
431 .release = nvmet_ns_release,
434 static const struct config_item_type nvmet_ns_type = {
435 .ct_item_ops = &nvmet_ns_item_ops,
436 .ct_attrs = nvmet_ns_attrs,
437 .ct_owner = THIS_MODULE,
440 static struct config_group *nvmet_ns_make(struct config_group *group,
443 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
448 ret = kstrtou32(name, 0, &nsid);
453 if (nsid == 0 || nsid == NVME_NSID_ALL)
457 ns = nvmet_ns_alloc(subsys, nsid);
460 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
462 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
469 static struct configfs_group_operations nvmet_namespaces_group_ops = {
470 .make_group = nvmet_ns_make,
473 static const struct config_item_type nvmet_namespaces_type = {
474 .ct_group_ops = &nvmet_namespaces_group_ops,
475 .ct_owner = THIS_MODULE,
478 static int nvmet_port_subsys_allow_link(struct config_item *parent,
479 struct config_item *target)
481 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
482 struct nvmet_subsys *subsys;
483 struct nvmet_subsys_link *link, *p;
486 if (target->ci_type != &nvmet_subsys_type) {
487 pr_err("can only link subsystems into the subsystems dir.!\n");
490 subsys = to_subsys(target);
491 link = kmalloc(sizeof(*link), GFP_KERNEL);
494 link->subsys = subsys;
496 down_write(&nvmet_config_sem);
498 list_for_each_entry(p, &port->subsystems, entry) {
499 if (p->subsys == subsys)
503 if (list_empty(&port->subsystems)) {
504 ret = nvmet_enable_port(port);
509 list_add_tail(&link->entry, &port->subsystems);
511 up_write(&nvmet_config_sem);
515 up_write(&nvmet_config_sem);
520 static void nvmet_port_subsys_drop_link(struct config_item *parent,
521 struct config_item *target)
523 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
524 struct nvmet_subsys *subsys = to_subsys(target);
525 struct nvmet_subsys_link *p;
527 down_write(&nvmet_config_sem);
528 list_for_each_entry(p, &port->subsystems, entry) {
529 if (p->subsys == subsys)
532 up_write(&nvmet_config_sem);
538 if (list_empty(&port->subsystems))
539 nvmet_disable_port(port);
540 up_write(&nvmet_config_sem);
544 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
545 .allow_link = nvmet_port_subsys_allow_link,
546 .drop_link = nvmet_port_subsys_drop_link,
549 static const struct config_item_type nvmet_port_subsys_type = {
550 .ct_item_ops = &nvmet_port_subsys_item_ops,
551 .ct_owner = THIS_MODULE,
554 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
555 struct config_item *target)
557 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
558 struct nvmet_host *host;
559 struct nvmet_host_link *link, *p;
562 if (target->ci_type != &nvmet_host_type) {
563 pr_err("can only link hosts into the allowed_hosts directory!\n");
567 host = to_host(target);
568 link = kmalloc(sizeof(*link), GFP_KERNEL);
573 down_write(&nvmet_config_sem);
575 if (subsys->allow_any_host) {
576 pr_err("can't add hosts when allow_any_host is set!\n");
581 list_for_each_entry(p, &subsys->hosts, entry) {
582 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
585 list_add_tail(&link->entry, &subsys->hosts);
587 up_write(&nvmet_config_sem);
590 up_write(&nvmet_config_sem);
595 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
596 struct config_item *target)
598 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
599 struct nvmet_host *host = to_host(target);
600 struct nvmet_host_link *p;
602 down_write(&nvmet_config_sem);
603 list_for_each_entry(p, &subsys->hosts, entry) {
604 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
607 up_write(&nvmet_config_sem);
613 up_write(&nvmet_config_sem);
617 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
618 .allow_link = nvmet_allowed_hosts_allow_link,
619 .drop_link = nvmet_allowed_hosts_drop_link,
622 static const struct config_item_type nvmet_allowed_hosts_type = {
623 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
624 .ct_owner = THIS_MODULE,
627 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
630 return snprintf(page, PAGE_SIZE, "%d\n",
631 to_subsys(item)->allow_any_host);
634 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
635 const char *page, size_t count)
637 struct nvmet_subsys *subsys = to_subsys(item);
641 if (strtobool(page, &allow_any_host))
644 down_write(&nvmet_config_sem);
645 if (allow_any_host && !list_empty(&subsys->hosts)) {
646 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
651 subsys->allow_any_host = allow_any_host;
653 up_write(&nvmet_config_sem);
654 return ret ? ret : count;
657 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
659 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
662 struct nvmet_subsys *subsys = to_subsys(item);
664 if (NVME_TERTIARY(subsys->ver))
665 return snprintf(page, PAGE_SIZE, "%d.%d.%d\n",
666 (int)NVME_MAJOR(subsys->ver),
667 (int)NVME_MINOR(subsys->ver),
668 (int)NVME_TERTIARY(subsys->ver));
670 return snprintf(page, PAGE_SIZE, "%d.%d\n",
671 (int)NVME_MAJOR(subsys->ver),
672 (int)NVME_MINOR(subsys->ver));
675 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
676 const char *page, size_t count)
678 struct nvmet_subsys *subsys = to_subsys(item);
679 int major, minor, tertiary = 0;
683 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
684 if (ret != 2 && ret != 3)
687 down_write(&nvmet_config_sem);
688 subsys->ver = NVME_VS(major, minor, tertiary);
689 up_write(&nvmet_config_sem);
693 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
695 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
698 struct nvmet_subsys *subsys = to_subsys(item);
700 return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
703 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
704 const char *page, size_t count)
706 struct nvmet_subsys *subsys = to_subsys(item);
708 down_write(&nvmet_config_sem);
709 sscanf(page, "%llx\n", &subsys->serial);
710 up_write(&nvmet_config_sem);
714 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
716 static struct configfs_attribute *nvmet_subsys_attrs[] = {
717 &nvmet_subsys_attr_attr_allow_any_host,
718 &nvmet_subsys_attr_attr_version,
719 &nvmet_subsys_attr_attr_serial,
724 * Subsystem structures & folder operation functions below
726 static void nvmet_subsys_release(struct config_item *item)
728 struct nvmet_subsys *subsys = to_subsys(item);
730 nvmet_subsys_del_ctrls(subsys);
731 nvmet_subsys_put(subsys);
734 static struct configfs_item_operations nvmet_subsys_item_ops = {
735 .release = nvmet_subsys_release,
738 static const struct config_item_type nvmet_subsys_type = {
739 .ct_item_ops = &nvmet_subsys_item_ops,
740 .ct_attrs = nvmet_subsys_attrs,
741 .ct_owner = THIS_MODULE,
744 static struct config_group *nvmet_subsys_make(struct config_group *group,
747 struct nvmet_subsys *subsys;
749 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
750 pr_err("can't create discovery subsystem through configfs\n");
751 return ERR_PTR(-EINVAL);
754 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
756 return ERR_PTR(-ENOMEM);
758 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
760 config_group_init_type_name(&subsys->namespaces_group,
761 "namespaces", &nvmet_namespaces_type);
762 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
764 config_group_init_type_name(&subsys->allowed_hosts_group,
765 "allowed_hosts", &nvmet_allowed_hosts_type);
766 configfs_add_default_group(&subsys->allowed_hosts_group,
769 return &subsys->group;
772 static struct configfs_group_operations nvmet_subsystems_group_ops = {
773 .make_group = nvmet_subsys_make,
776 static const struct config_item_type nvmet_subsystems_type = {
777 .ct_group_ops = &nvmet_subsystems_group_ops,
778 .ct_owner = THIS_MODULE,
781 static ssize_t nvmet_referral_enable_show(struct config_item *item,
784 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
787 static ssize_t nvmet_referral_enable_store(struct config_item *item,
788 const char *page, size_t count)
790 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
791 struct nvmet_port *port = to_nvmet_port(item);
794 if (strtobool(page, &enable))
798 nvmet_referral_enable(parent, port);
800 nvmet_referral_disable(port);
804 pr_err("Invalid value '%s' for enable\n", page);
808 CONFIGFS_ATTR(nvmet_referral_, enable);
811 * Discovery Service subsystem definitions
813 static struct configfs_attribute *nvmet_referral_attrs[] = {
814 &nvmet_attr_addr_adrfam,
815 &nvmet_attr_addr_portid,
816 &nvmet_attr_addr_treq,
817 &nvmet_attr_addr_traddr,
818 &nvmet_attr_addr_trsvcid,
819 &nvmet_attr_addr_trtype,
820 &nvmet_referral_attr_enable,
824 static void nvmet_referral_release(struct config_item *item)
826 struct nvmet_port *port = to_nvmet_port(item);
828 nvmet_referral_disable(port);
832 static struct configfs_item_operations nvmet_referral_item_ops = {
833 .release = nvmet_referral_release,
836 static const struct config_item_type nvmet_referral_type = {
837 .ct_owner = THIS_MODULE,
838 .ct_attrs = nvmet_referral_attrs,
839 .ct_item_ops = &nvmet_referral_item_ops,
842 static struct config_group *nvmet_referral_make(
843 struct config_group *group, const char *name)
845 struct nvmet_port *port;
847 port = kzalloc(sizeof(*port), GFP_KERNEL);
849 return ERR_PTR(-ENOMEM);
851 INIT_LIST_HEAD(&port->entry);
852 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
857 static struct configfs_group_operations nvmet_referral_group_ops = {
858 .make_group = nvmet_referral_make,
861 static const struct config_item_type nvmet_referrals_type = {
862 .ct_owner = THIS_MODULE,
863 .ct_group_ops = &nvmet_referral_group_ops,
869 static void nvmet_port_release(struct config_item *item)
871 struct nvmet_port *port = to_nvmet_port(item);
876 static struct configfs_attribute *nvmet_port_attrs[] = {
877 &nvmet_attr_addr_adrfam,
878 &nvmet_attr_addr_treq,
879 &nvmet_attr_addr_traddr,
880 &nvmet_attr_addr_trsvcid,
881 &nvmet_attr_addr_trtype,
885 static struct configfs_item_operations nvmet_port_item_ops = {
886 .release = nvmet_port_release,
889 static const struct config_item_type nvmet_port_type = {
890 .ct_attrs = nvmet_port_attrs,
891 .ct_item_ops = &nvmet_port_item_ops,
892 .ct_owner = THIS_MODULE,
895 static struct config_group *nvmet_ports_make(struct config_group *group,
898 struct nvmet_port *port;
901 if (kstrtou16(name, 0, &portid))
902 return ERR_PTR(-EINVAL);
904 port = kzalloc(sizeof(*port), GFP_KERNEL);
906 return ERR_PTR(-ENOMEM);
908 INIT_LIST_HEAD(&port->entry);
909 INIT_LIST_HEAD(&port->subsystems);
910 INIT_LIST_HEAD(&port->referrals);
912 port->disc_addr.portid = cpu_to_le16(portid);
913 config_group_init_type_name(&port->group, name, &nvmet_port_type);
915 config_group_init_type_name(&port->subsys_group,
916 "subsystems", &nvmet_port_subsys_type);
917 configfs_add_default_group(&port->subsys_group, &port->group);
919 config_group_init_type_name(&port->referrals_group,
920 "referrals", &nvmet_referrals_type);
921 configfs_add_default_group(&port->referrals_group, &port->group);
926 static struct configfs_group_operations nvmet_ports_group_ops = {
927 .make_group = nvmet_ports_make,
930 static const struct config_item_type nvmet_ports_type = {
931 .ct_group_ops = &nvmet_ports_group_ops,
932 .ct_owner = THIS_MODULE,
935 static struct config_group nvmet_subsystems_group;
936 static struct config_group nvmet_ports_group;
938 static void nvmet_host_release(struct config_item *item)
940 struct nvmet_host *host = to_host(item);
945 static struct configfs_item_operations nvmet_host_item_ops = {
946 .release = nvmet_host_release,
949 static const struct config_item_type nvmet_host_type = {
950 .ct_item_ops = &nvmet_host_item_ops,
951 .ct_owner = THIS_MODULE,
954 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
957 struct nvmet_host *host;
959 host = kzalloc(sizeof(*host), GFP_KERNEL);
961 return ERR_PTR(-ENOMEM);
963 config_group_init_type_name(&host->group, name, &nvmet_host_type);
968 static struct configfs_group_operations nvmet_hosts_group_ops = {
969 .make_group = nvmet_hosts_make_group,
972 static const struct config_item_type nvmet_hosts_type = {
973 .ct_group_ops = &nvmet_hosts_group_ops,
974 .ct_owner = THIS_MODULE,
977 static struct config_group nvmet_hosts_group;
979 static const struct config_item_type nvmet_root_type = {
980 .ct_owner = THIS_MODULE,
983 static struct configfs_subsystem nvmet_configfs_subsystem = {
986 .ci_namebuf = "nvmet",
987 .ci_type = &nvmet_root_type,
992 int __init nvmet_init_configfs(void)
996 config_group_init(&nvmet_configfs_subsystem.su_group);
997 mutex_init(&nvmet_configfs_subsystem.su_mutex);
999 config_group_init_type_name(&nvmet_subsystems_group,
1000 "subsystems", &nvmet_subsystems_type);
1001 configfs_add_default_group(&nvmet_subsystems_group,
1002 &nvmet_configfs_subsystem.su_group);
1004 config_group_init_type_name(&nvmet_ports_group,
1005 "ports", &nvmet_ports_type);
1006 configfs_add_default_group(&nvmet_ports_group,
1007 &nvmet_configfs_subsystem.su_group);
1009 config_group_init_type_name(&nvmet_hosts_group,
1010 "hosts", &nvmet_hosts_type);
1011 configfs_add_default_group(&nvmet_hosts_group,
1012 &nvmet_configfs_subsystem.su_group);
1014 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1016 pr_err("configfs_register_subsystem: %d\n", ret);
1023 void __exit nvmet_exit_configfs(void)
1025 configfs_unregister_subsystem(&nvmet_configfs_subsystem);