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>
20 #include <linux/pci.h>
21 #include <linux/pci-p2pdma.h>
25 static const struct config_item_type nvmet_host_type;
26 static const struct config_item_type nvmet_subsys_type;
28 static LIST_HEAD(nvmet_ports_list);
29 struct list_head *nvmet_ports = &nvmet_ports_list;
31 static const struct nvmet_transport_name {
34 } nvmet_transport_names[] = {
35 { NVMF_TRTYPE_RDMA, "rdma" },
36 { NVMF_TRTYPE_FC, "fc" },
37 { NVMF_TRTYPE_LOOP, "loop" },
41 * nvmet_port Generic ConfigFS definitions.
42 * Used in any place in the ConfigFS tree that refers to an address.
44 static ssize_t nvmet_addr_adrfam_show(struct config_item *item,
47 switch (to_nvmet_port(item)->disc_addr.adrfam) {
48 case NVMF_ADDR_FAMILY_IP4:
49 return sprintf(page, "ipv4\n");
50 case NVMF_ADDR_FAMILY_IP6:
51 return sprintf(page, "ipv6\n");
52 case NVMF_ADDR_FAMILY_IB:
53 return sprintf(page, "ib\n");
54 case NVMF_ADDR_FAMILY_FC:
55 return sprintf(page, "fc\n");
57 return sprintf(page, "\n");
61 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
62 const char *page, size_t count)
64 struct nvmet_port *port = to_nvmet_port(item);
67 pr_err("Cannot modify address while enabled\n");
68 pr_err("Disable the address before modifying\n");
72 if (sysfs_streq(page, "ipv4")) {
73 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP4;
74 } else if (sysfs_streq(page, "ipv6")) {
75 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP6;
76 } else if (sysfs_streq(page, "ib")) {
77 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IB;
78 } else if (sysfs_streq(page, "fc")) {
79 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_FC;
81 pr_err("Invalid value '%s' for adrfam\n", page);
88 CONFIGFS_ATTR(nvmet_, addr_adrfam);
90 static ssize_t nvmet_addr_portid_show(struct config_item *item,
93 struct nvmet_port *port = to_nvmet_port(item);
95 return snprintf(page, PAGE_SIZE, "%d\n",
96 le16_to_cpu(port->disc_addr.portid));
99 static ssize_t nvmet_addr_portid_store(struct config_item *item,
100 const char *page, size_t count)
102 struct nvmet_port *port = to_nvmet_port(item);
105 if (kstrtou16(page, 0, &portid)) {
106 pr_err("Invalid value '%s' for portid\n", page);
111 pr_err("Cannot modify address while enabled\n");
112 pr_err("Disable the address before modifying\n");
115 port->disc_addr.portid = cpu_to_le16(portid);
119 CONFIGFS_ATTR(nvmet_, addr_portid);
121 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
124 struct nvmet_port *port = to_nvmet_port(item);
126 return snprintf(page, PAGE_SIZE, "%s\n",
127 port->disc_addr.traddr);
130 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
131 const char *page, size_t count)
133 struct nvmet_port *port = to_nvmet_port(item);
135 if (count > NVMF_TRADDR_SIZE) {
136 pr_err("Invalid value '%s' for traddr\n", page);
141 pr_err("Cannot modify address while enabled\n");
142 pr_err("Disable the address before modifying\n");
146 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
151 CONFIGFS_ATTR(nvmet_, addr_traddr);
153 static ssize_t nvmet_addr_treq_show(struct config_item *item,
156 switch (to_nvmet_port(item)->disc_addr.treq &
157 NVME_TREQ_SECURE_CHANNEL_MASK) {
158 case NVMF_TREQ_NOT_SPECIFIED:
159 return sprintf(page, "not specified\n");
160 case NVMF_TREQ_REQUIRED:
161 return sprintf(page, "required\n");
162 case NVMF_TREQ_NOT_REQUIRED:
163 return sprintf(page, "not required\n");
165 return sprintf(page, "\n");
169 static ssize_t nvmet_addr_treq_store(struct config_item *item,
170 const char *page, size_t count)
172 struct nvmet_port *port = to_nvmet_port(item);
173 u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
176 pr_err("Cannot modify address while enabled\n");
177 pr_err("Disable the address before modifying\n");
181 if (sysfs_streq(page, "not specified")) {
182 treq |= NVMF_TREQ_NOT_SPECIFIED;
183 } else if (sysfs_streq(page, "required")) {
184 treq |= NVMF_TREQ_REQUIRED;
185 } else if (sysfs_streq(page, "not required")) {
186 treq |= NVMF_TREQ_NOT_REQUIRED;
188 pr_err("Invalid value '%s' for treq\n", page);
191 port->disc_addr.treq = treq;
196 CONFIGFS_ATTR(nvmet_, addr_treq);
198 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
201 struct nvmet_port *port = to_nvmet_port(item);
203 return snprintf(page, PAGE_SIZE, "%s\n",
204 port->disc_addr.trsvcid);
207 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
208 const char *page, size_t count)
210 struct nvmet_port *port = to_nvmet_port(item);
212 if (count > NVMF_TRSVCID_SIZE) {
213 pr_err("Invalid value '%s' for trsvcid\n", page);
217 pr_err("Cannot modify address while enabled\n");
218 pr_err("Disable the address before modifying\n");
222 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
227 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
229 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
232 struct nvmet_port *port = to_nvmet_port(item);
234 return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
237 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
238 const char *page, size_t count)
240 struct nvmet_port *port = to_nvmet_port(item);
244 pr_err("Cannot modify inline_data_size while port enabled\n");
245 pr_err("Disable the port before modifying\n");
248 ret = kstrtoint(page, 0, &port->inline_data_size);
250 pr_err("Invalid value '%s' for inline_data_size\n", page);
256 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
258 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
261 struct nvmet_port *port = to_nvmet_port(item);
264 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
265 if (port->disc_addr.trtype != nvmet_transport_names[i].type)
267 return sprintf(page, "%s\n", nvmet_transport_names[i].name);
270 return sprintf(page, "\n");
273 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
275 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
276 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
277 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
280 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
281 const char *page, size_t count)
283 struct nvmet_port *port = to_nvmet_port(item);
287 pr_err("Cannot modify address while enabled\n");
288 pr_err("Disable the address before modifying\n");
292 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
293 if (sysfs_streq(page, nvmet_transport_names[i].name))
297 pr_err("Invalid value '%s' for trtype\n", page);
300 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
301 port->disc_addr.trtype = nvmet_transport_names[i].type;
302 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
303 nvmet_port_init_tsas_rdma(port);
307 CONFIGFS_ATTR(nvmet_, addr_trtype);
310 * Namespace structures & file operation functions below
312 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
314 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
317 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
318 const char *page, size_t count)
320 struct nvmet_ns *ns = to_nvmet_ns(item);
321 struct nvmet_subsys *subsys = ns->subsys;
325 mutex_lock(&subsys->lock);
331 len = strcspn(page, "\n");
335 kfree(ns->device_path);
337 ns->device_path = kstrndup(page, len, GFP_KERNEL);
338 if (!ns->device_path)
341 mutex_unlock(&subsys->lock);
345 mutex_unlock(&subsys->lock);
349 CONFIGFS_ATTR(nvmet_ns_, device_path);
351 #ifdef CONFIG_PCI_P2PDMA
352 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
354 struct nvmet_ns *ns = to_nvmet_ns(item);
356 return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
359 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
360 const char *page, size_t count)
362 struct nvmet_ns *ns = to_nvmet_ns(item);
363 struct pci_dev *p2p_dev = NULL;
368 mutex_lock(&ns->subsys->lock);
374 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
380 ns->use_p2pmem = use_p2pmem;
381 pci_dev_put(ns->p2p_dev);
382 ns->p2p_dev = p2p_dev;
385 mutex_unlock(&ns->subsys->lock);
390 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
391 #endif /* CONFIG_PCI_P2PDMA */
393 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
395 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
398 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
399 const char *page, size_t count)
401 struct nvmet_ns *ns = to_nvmet_ns(item);
402 struct nvmet_subsys *subsys = ns->subsys;
406 mutex_lock(&subsys->lock);
413 if (uuid_parse(page, &ns->uuid))
417 mutex_unlock(&subsys->lock);
418 return ret ? ret : count;
421 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
423 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
425 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
428 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
429 const char *page, size_t count)
431 struct nvmet_ns *ns = to_nvmet_ns(item);
432 struct nvmet_subsys *subsys = ns->subsys;
434 const char *p = page;
438 mutex_lock(&subsys->lock);
444 for (i = 0; i < 16; i++) {
445 if (p + 2 > page + count) {
449 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
454 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
457 if (*p == '-' || *p == ':')
461 memcpy(&ns->nguid, nguid, sizeof(nguid));
463 mutex_unlock(&subsys->lock);
464 return ret ? ret : count;
467 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
469 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
471 return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
474 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
475 const char *page, size_t count)
477 struct nvmet_ns *ns = to_nvmet_ns(item);
478 u32 oldgrpid, newgrpid;
481 ret = kstrtou32(page, 0, &newgrpid);
485 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
488 down_write(&nvmet_ana_sem);
489 oldgrpid = ns->anagrpid;
490 nvmet_ana_group_enabled[newgrpid]++;
491 ns->anagrpid = newgrpid;
492 nvmet_ana_group_enabled[oldgrpid]--;
494 up_write(&nvmet_ana_sem);
496 nvmet_send_ana_event(ns->subsys, NULL);
500 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
502 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
504 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
507 static ssize_t nvmet_ns_enable_store(struct config_item *item,
508 const char *page, size_t count)
510 struct nvmet_ns *ns = to_nvmet_ns(item);
514 if (strtobool(page, &enable))
518 ret = nvmet_ns_enable(ns);
520 nvmet_ns_disable(ns);
522 return ret ? ret : count;
525 CONFIGFS_ATTR(nvmet_ns_, enable);
527 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
529 return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
532 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
533 const char *page, size_t count)
535 struct nvmet_ns *ns = to_nvmet_ns(item);
538 if (strtobool(page, &val))
541 mutex_lock(&ns->subsys->lock);
543 pr_err("disable ns before setting buffered_io value.\n");
544 mutex_unlock(&ns->subsys->lock);
548 ns->buffered_io = val;
549 mutex_unlock(&ns->subsys->lock);
553 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
555 static struct configfs_attribute *nvmet_ns_attrs[] = {
556 &nvmet_ns_attr_device_path,
557 &nvmet_ns_attr_device_nguid,
558 &nvmet_ns_attr_device_uuid,
559 &nvmet_ns_attr_ana_grpid,
560 &nvmet_ns_attr_enable,
561 &nvmet_ns_attr_buffered_io,
562 #ifdef CONFIG_PCI_P2PDMA
563 &nvmet_ns_attr_p2pmem,
568 static void nvmet_ns_release(struct config_item *item)
570 struct nvmet_ns *ns = to_nvmet_ns(item);
575 static struct configfs_item_operations nvmet_ns_item_ops = {
576 .release = nvmet_ns_release,
579 static const struct config_item_type nvmet_ns_type = {
580 .ct_item_ops = &nvmet_ns_item_ops,
581 .ct_attrs = nvmet_ns_attrs,
582 .ct_owner = THIS_MODULE,
585 static struct config_group *nvmet_ns_make(struct config_group *group,
588 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
593 ret = kstrtou32(name, 0, &nsid);
598 if (nsid == 0 || nsid == NVME_NSID_ALL)
602 ns = nvmet_ns_alloc(subsys, nsid);
605 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
607 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
614 static struct configfs_group_operations nvmet_namespaces_group_ops = {
615 .make_group = nvmet_ns_make,
618 static const struct config_item_type nvmet_namespaces_type = {
619 .ct_group_ops = &nvmet_namespaces_group_ops,
620 .ct_owner = THIS_MODULE,
623 static int nvmet_port_subsys_allow_link(struct config_item *parent,
624 struct config_item *target)
626 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
627 struct nvmet_subsys *subsys;
628 struct nvmet_subsys_link *link, *p;
631 if (target->ci_type != &nvmet_subsys_type) {
632 pr_err("can only link subsystems into the subsystems dir.!\n");
635 subsys = to_subsys(target);
636 link = kmalloc(sizeof(*link), GFP_KERNEL);
639 link->subsys = subsys;
641 down_write(&nvmet_config_sem);
643 list_for_each_entry(p, &port->subsystems, entry) {
644 if (p->subsys == subsys)
648 if (list_empty(&port->subsystems)) {
649 ret = nvmet_enable_port(port);
654 list_add_tail(&link->entry, &port->subsystems);
655 nvmet_port_disc_changed(port, subsys);
657 up_write(&nvmet_config_sem);
661 up_write(&nvmet_config_sem);
666 static void nvmet_port_subsys_drop_link(struct config_item *parent,
667 struct config_item *target)
669 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
670 struct nvmet_subsys *subsys = to_subsys(target);
671 struct nvmet_subsys_link *p;
673 down_write(&nvmet_config_sem);
674 list_for_each_entry(p, &port->subsystems, entry) {
675 if (p->subsys == subsys)
678 up_write(&nvmet_config_sem);
683 nvmet_port_disc_changed(port, subsys);
685 if (list_empty(&port->subsystems))
686 nvmet_disable_port(port);
687 up_write(&nvmet_config_sem);
691 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
692 .allow_link = nvmet_port_subsys_allow_link,
693 .drop_link = nvmet_port_subsys_drop_link,
696 static const struct config_item_type nvmet_port_subsys_type = {
697 .ct_item_ops = &nvmet_port_subsys_item_ops,
698 .ct_owner = THIS_MODULE,
701 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
702 struct config_item *target)
704 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
705 struct nvmet_host *host;
706 struct nvmet_host_link *link, *p;
709 if (target->ci_type != &nvmet_host_type) {
710 pr_err("can only link hosts into the allowed_hosts directory!\n");
714 host = to_host(target);
715 link = kmalloc(sizeof(*link), GFP_KERNEL);
720 down_write(&nvmet_config_sem);
722 if (subsys->allow_any_host) {
723 pr_err("can't add hosts when allow_any_host is set!\n");
728 list_for_each_entry(p, &subsys->hosts, entry) {
729 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
732 list_add_tail(&link->entry, &subsys->hosts);
733 nvmet_subsys_disc_changed(subsys, host);
735 up_write(&nvmet_config_sem);
738 up_write(&nvmet_config_sem);
743 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
744 struct config_item *target)
746 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
747 struct nvmet_host *host = to_host(target);
748 struct nvmet_host_link *p;
750 down_write(&nvmet_config_sem);
751 list_for_each_entry(p, &subsys->hosts, entry) {
752 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
755 up_write(&nvmet_config_sem);
760 nvmet_subsys_disc_changed(subsys, host);
762 up_write(&nvmet_config_sem);
766 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
767 .allow_link = nvmet_allowed_hosts_allow_link,
768 .drop_link = nvmet_allowed_hosts_drop_link,
771 static const struct config_item_type nvmet_allowed_hosts_type = {
772 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
773 .ct_owner = THIS_MODULE,
776 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
779 return snprintf(page, PAGE_SIZE, "%d\n",
780 to_subsys(item)->allow_any_host);
783 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
784 const char *page, size_t count)
786 struct nvmet_subsys *subsys = to_subsys(item);
790 if (strtobool(page, &allow_any_host))
793 down_write(&nvmet_config_sem);
794 if (allow_any_host && !list_empty(&subsys->hosts)) {
795 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
800 if (subsys->allow_any_host != allow_any_host) {
801 subsys->allow_any_host = allow_any_host;
802 nvmet_subsys_disc_changed(subsys, NULL);
806 up_write(&nvmet_config_sem);
807 return ret ? ret : count;
810 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
812 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
815 struct nvmet_subsys *subsys = to_subsys(item);
817 if (NVME_TERTIARY(subsys->ver))
818 return snprintf(page, PAGE_SIZE, "%d.%d.%d\n",
819 (int)NVME_MAJOR(subsys->ver),
820 (int)NVME_MINOR(subsys->ver),
821 (int)NVME_TERTIARY(subsys->ver));
823 return snprintf(page, PAGE_SIZE, "%d.%d\n",
824 (int)NVME_MAJOR(subsys->ver),
825 (int)NVME_MINOR(subsys->ver));
828 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
829 const char *page, size_t count)
831 struct nvmet_subsys *subsys = to_subsys(item);
832 int major, minor, tertiary = 0;
836 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
837 if (ret != 2 && ret != 3)
840 down_write(&nvmet_config_sem);
841 subsys->ver = NVME_VS(major, minor, tertiary);
842 up_write(&nvmet_config_sem);
846 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
848 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
851 struct nvmet_subsys *subsys = to_subsys(item);
853 return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
856 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
857 const char *page, size_t count)
859 struct nvmet_subsys *subsys = to_subsys(item);
861 down_write(&nvmet_config_sem);
862 sscanf(page, "%llx\n", &subsys->serial);
863 up_write(&nvmet_config_sem);
867 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
869 static struct configfs_attribute *nvmet_subsys_attrs[] = {
870 &nvmet_subsys_attr_attr_allow_any_host,
871 &nvmet_subsys_attr_attr_version,
872 &nvmet_subsys_attr_attr_serial,
877 * Subsystem structures & folder operation functions below
879 static void nvmet_subsys_release(struct config_item *item)
881 struct nvmet_subsys *subsys = to_subsys(item);
883 nvmet_subsys_del_ctrls(subsys);
884 nvmet_subsys_put(subsys);
887 static struct configfs_item_operations nvmet_subsys_item_ops = {
888 .release = nvmet_subsys_release,
891 static const struct config_item_type nvmet_subsys_type = {
892 .ct_item_ops = &nvmet_subsys_item_ops,
893 .ct_attrs = nvmet_subsys_attrs,
894 .ct_owner = THIS_MODULE,
897 static struct config_group *nvmet_subsys_make(struct config_group *group,
900 struct nvmet_subsys *subsys;
902 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
903 pr_err("can't create discovery subsystem through configfs\n");
904 return ERR_PTR(-EINVAL);
907 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
909 return ERR_PTR(-ENOMEM);
911 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
913 config_group_init_type_name(&subsys->namespaces_group,
914 "namespaces", &nvmet_namespaces_type);
915 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
917 config_group_init_type_name(&subsys->allowed_hosts_group,
918 "allowed_hosts", &nvmet_allowed_hosts_type);
919 configfs_add_default_group(&subsys->allowed_hosts_group,
922 return &subsys->group;
925 static struct configfs_group_operations nvmet_subsystems_group_ops = {
926 .make_group = nvmet_subsys_make,
929 static const struct config_item_type nvmet_subsystems_type = {
930 .ct_group_ops = &nvmet_subsystems_group_ops,
931 .ct_owner = THIS_MODULE,
934 static ssize_t nvmet_referral_enable_show(struct config_item *item,
937 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
940 static ssize_t nvmet_referral_enable_store(struct config_item *item,
941 const char *page, size_t count)
943 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
944 struct nvmet_port *port = to_nvmet_port(item);
947 if (strtobool(page, &enable))
951 nvmet_referral_enable(parent, port);
953 nvmet_referral_disable(parent, port);
957 pr_err("Invalid value '%s' for enable\n", page);
961 CONFIGFS_ATTR(nvmet_referral_, enable);
964 * Discovery Service subsystem definitions
966 static struct configfs_attribute *nvmet_referral_attrs[] = {
967 &nvmet_attr_addr_adrfam,
968 &nvmet_attr_addr_portid,
969 &nvmet_attr_addr_treq,
970 &nvmet_attr_addr_traddr,
971 &nvmet_attr_addr_trsvcid,
972 &nvmet_attr_addr_trtype,
973 &nvmet_referral_attr_enable,
977 static void nvmet_referral_release(struct config_item *item)
979 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
980 struct nvmet_port *port = to_nvmet_port(item);
982 nvmet_referral_disable(parent, port);
986 static struct configfs_item_operations nvmet_referral_item_ops = {
987 .release = nvmet_referral_release,
990 static const struct config_item_type nvmet_referral_type = {
991 .ct_owner = THIS_MODULE,
992 .ct_attrs = nvmet_referral_attrs,
993 .ct_item_ops = &nvmet_referral_item_ops,
996 static struct config_group *nvmet_referral_make(
997 struct config_group *group, const char *name)
999 struct nvmet_port *port;
1001 port = kzalloc(sizeof(*port), GFP_KERNEL);
1003 return ERR_PTR(-ENOMEM);
1005 INIT_LIST_HEAD(&port->entry);
1006 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1008 return &port->group;
1011 static struct configfs_group_operations nvmet_referral_group_ops = {
1012 .make_group = nvmet_referral_make,
1015 static const struct config_item_type nvmet_referrals_type = {
1016 .ct_owner = THIS_MODULE,
1017 .ct_group_ops = &nvmet_referral_group_ops,
1021 enum nvme_ana_state state;
1023 } nvmet_ana_state_names[] = {
1024 { NVME_ANA_OPTIMIZED, "optimized" },
1025 { NVME_ANA_NONOPTIMIZED, "non-optimized" },
1026 { NVME_ANA_INACCESSIBLE, "inaccessible" },
1027 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
1028 { NVME_ANA_CHANGE, "change" },
1031 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1034 struct nvmet_ana_group *grp = to_ana_group(item);
1035 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1038 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1039 if (state != nvmet_ana_state_names[i].state)
1041 return sprintf(page, "%s\n", nvmet_ana_state_names[i].name);
1044 return sprintf(page, "\n");
1047 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1048 const char *page, size_t count)
1050 struct nvmet_ana_group *grp = to_ana_group(item);
1053 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1054 if (sysfs_streq(page, nvmet_ana_state_names[i].name))
1058 pr_err("Invalid value '%s' for ana_state\n", page);
1062 down_write(&nvmet_ana_sem);
1063 grp->port->ana_state[grp->grpid] = nvmet_ana_state_names[i].state;
1065 up_write(&nvmet_ana_sem);
1067 nvmet_port_send_ana_event(grp->port);
1071 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1073 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1074 &nvmet_ana_group_attr_ana_state,
1078 static void nvmet_ana_group_release(struct config_item *item)
1080 struct nvmet_ana_group *grp = to_ana_group(item);
1082 if (grp == &grp->port->ana_default_group)
1085 down_write(&nvmet_ana_sem);
1086 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1087 nvmet_ana_group_enabled[grp->grpid]--;
1088 up_write(&nvmet_ana_sem);
1090 nvmet_port_send_ana_event(grp->port);
1094 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1095 .release = nvmet_ana_group_release,
1098 static const struct config_item_type nvmet_ana_group_type = {
1099 .ct_item_ops = &nvmet_ana_group_item_ops,
1100 .ct_attrs = nvmet_ana_group_attrs,
1101 .ct_owner = THIS_MODULE,
1104 static struct config_group *nvmet_ana_groups_make_group(
1105 struct config_group *group, const char *name)
1107 struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1108 struct nvmet_ana_group *grp;
1112 ret = kstrtou32(name, 0, &grpid);
1117 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1121 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1127 down_write(&nvmet_ana_sem);
1128 nvmet_ana_group_enabled[grpid]++;
1129 up_write(&nvmet_ana_sem);
1131 nvmet_port_send_ana_event(grp->port);
1133 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1136 return ERR_PTR(ret);
1139 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1140 .make_group = nvmet_ana_groups_make_group,
1143 static const struct config_item_type nvmet_ana_groups_type = {
1144 .ct_group_ops = &nvmet_ana_groups_group_ops,
1145 .ct_owner = THIS_MODULE,
1149 * Ports definitions.
1151 static void nvmet_port_release(struct config_item *item)
1153 struct nvmet_port *port = to_nvmet_port(item);
1155 list_del(&port->global_entry);
1157 kfree(port->ana_state);
1161 static struct configfs_attribute *nvmet_port_attrs[] = {
1162 &nvmet_attr_addr_adrfam,
1163 &nvmet_attr_addr_treq,
1164 &nvmet_attr_addr_traddr,
1165 &nvmet_attr_addr_trsvcid,
1166 &nvmet_attr_addr_trtype,
1167 &nvmet_attr_param_inline_data_size,
1171 static struct configfs_item_operations nvmet_port_item_ops = {
1172 .release = nvmet_port_release,
1175 static const struct config_item_type nvmet_port_type = {
1176 .ct_attrs = nvmet_port_attrs,
1177 .ct_item_ops = &nvmet_port_item_ops,
1178 .ct_owner = THIS_MODULE,
1181 static struct config_group *nvmet_ports_make(struct config_group *group,
1184 struct nvmet_port *port;
1188 if (kstrtou16(name, 0, &portid))
1189 return ERR_PTR(-EINVAL);
1191 port = kzalloc(sizeof(*port), GFP_KERNEL);
1193 return ERR_PTR(-ENOMEM);
1195 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1196 sizeof(*port->ana_state), GFP_KERNEL);
1197 if (!port->ana_state) {
1199 return ERR_PTR(-ENOMEM);
1202 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1203 if (i == NVMET_DEFAULT_ANA_GRPID)
1204 port->ana_state[1] = NVME_ANA_OPTIMIZED;
1206 port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1209 list_add(&port->global_entry, &nvmet_ports_list);
1211 INIT_LIST_HEAD(&port->entry);
1212 INIT_LIST_HEAD(&port->subsystems);
1213 INIT_LIST_HEAD(&port->referrals);
1214 port->inline_data_size = -1; /* < 0 == let the transport choose */
1216 port->disc_addr.portid = cpu_to_le16(portid);
1217 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1218 config_group_init_type_name(&port->group, name, &nvmet_port_type);
1220 config_group_init_type_name(&port->subsys_group,
1221 "subsystems", &nvmet_port_subsys_type);
1222 configfs_add_default_group(&port->subsys_group, &port->group);
1224 config_group_init_type_name(&port->referrals_group,
1225 "referrals", &nvmet_referrals_type);
1226 configfs_add_default_group(&port->referrals_group, &port->group);
1228 config_group_init_type_name(&port->ana_groups_group,
1229 "ana_groups", &nvmet_ana_groups_type);
1230 configfs_add_default_group(&port->ana_groups_group, &port->group);
1232 port->ana_default_group.port = port;
1233 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1234 config_group_init_type_name(&port->ana_default_group.group,
1235 __stringify(NVMET_DEFAULT_ANA_GRPID),
1236 &nvmet_ana_group_type);
1237 configfs_add_default_group(&port->ana_default_group.group,
1238 &port->ana_groups_group);
1240 return &port->group;
1243 static struct configfs_group_operations nvmet_ports_group_ops = {
1244 .make_group = nvmet_ports_make,
1247 static const struct config_item_type nvmet_ports_type = {
1248 .ct_group_ops = &nvmet_ports_group_ops,
1249 .ct_owner = THIS_MODULE,
1252 static struct config_group nvmet_subsystems_group;
1253 static struct config_group nvmet_ports_group;
1255 static void nvmet_host_release(struct config_item *item)
1257 struct nvmet_host *host = to_host(item);
1262 static struct configfs_item_operations nvmet_host_item_ops = {
1263 .release = nvmet_host_release,
1266 static const struct config_item_type nvmet_host_type = {
1267 .ct_item_ops = &nvmet_host_item_ops,
1268 .ct_owner = THIS_MODULE,
1271 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1274 struct nvmet_host *host;
1276 host = kzalloc(sizeof(*host), GFP_KERNEL);
1278 return ERR_PTR(-ENOMEM);
1280 config_group_init_type_name(&host->group, name, &nvmet_host_type);
1282 return &host->group;
1285 static struct configfs_group_operations nvmet_hosts_group_ops = {
1286 .make_group = nvmet_hosts_make_group,
1289 static const struct config_item_type nvmet_hosts_type = {
1290 .ct_group_ops = &nvmet_hosts_group_ops,
1291 .ct_owner = THIS_MODULE,
1294 static struct config_group nvmet_hosts_group;
1296 static const struct config_item_type nvmet_root_type = {
1297 .ct_owner = THIS_MODULE,
1300 static struct configfs_subsystem nvmet_configfs_subsystem = {
1303 .ci_namebuf = "nvmet",
1304 .ci_type = &nvmet_root_type,
1309 int __init nvmet_init_configfs(void)
1313 config_group_init(&nvmet_configfs_subsystem.su_group);
1314 mutex_init(&nvmet_configfs_subsystem.su_mutex);
1316 config_group_init_type_name(&nvmet_subsystems_group,
1317 "subsystems", &nvmet_subsystems_type);
1318 configfs_add_default_group(&nvmet_subsystems_group,
1319 &nvmet_configfs_subsystem.su_group);
1321 config_group_init_type_name(&nvmet_ports_group,
1322 "ports", &nvmet_ports_type);
1323 configfs_add_default_group(&nvmet_ports_group,
1324 &nvmet_configfs_subsystem.su_group);
1326 config_group_init_type_name(&nvmet_hosts_group,
1327 "hosts", &nvmet_hosts_type);
1328 configfs_add_default_group(&nvmet_hosts_group,
1329 &nvmet_configfs_subsystem.su_group);
1331 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1333 pr_err("configfs_register_subsystem: %d\n", ret);
1340 void __exit nvmet_exit_configfs(void)
1342 configfs_unregister_subsystem(&nvmet_configfs_subsystem);