1 // SPDX-License-Identifier: GPL-2.0
3 * Configfs interface for the NVMe target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/stat.h>
11 #include <linux/ctype.h>
12 #include <linux/pci.h>
13 #include <linux/pci-p2pdma.h>
17 static const struct config_item_type nvmet_host_type;
18 static const struct config_item_type nvmet_subsys_type;
20 static LIST_HEAD(nvmet_ports_list);
21 struct list_head *nvmet_ports = &nvmet_ports_list;
23 static const struct nvmet_transport_name {
26 } nvmet_transport_names[] = {
27 { NVMF_TRTYPE_RDMA, "rdma" },
28 { NVMF_TRTYPE_FC, "fc" },
29 { NVMF_TRTYPE_TCP, "tcp" },
30 { NVMF_TRTYPE_LOOP, "loop" },
34 * nvmet_port Generic ConfigFS definitions.
35 * Used in any place in the ConfigFS tree that refers to an address.
37 static ssize_t nvmet_addr_adrfam_show(struct config_item *item,
40 switch (to_nvmet_port(item)->disc_addr.adrfam) {
41 case NVMF_ADDR_FAMILY_IP4:
42 return sprintf(page, "ipv4\n");
43 case NVMF_ADDR_FAMILY_IP6:
44 return sprintf(page, "ipv6\n");
45 case NVMF_ADDR_FAMILY_IB:
46 return sprintf(page, "ib\n");
47 case NVMF_ADDR_FAMILY_FC:
48 return sprintf(page, "fc\n");
50 return sprintf(page, "\n");
54 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
55 const char *page, size_t count)
57 struct nvmet_port *port = to_nvmet_port(item);
60 pr_err("Cannot modify address while enabled\n");
61 pr_err("Disable the address before modifying\n");
65 if (sysfs_streq(page, "ipv4")) {
66 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP4;
67 } else if (sysfs_streq(page, "ipv6")) {
68 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP6;
69 } else if (sysfs_streq(page, "ib")) {
70 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IB;
71 } else if (sysfs_streq(page, "fc")) {
72 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_FC;
74 pr_err("Invalid value '%s' for adrfam\n", page);
81 CONFIGFS_ATTR(nvmet_, addr_adrfam);
83 static ssize_t nvmet_addr_portid_show(struct config_item *item,
86 struct nvmet_port *port = to_nvmet_port(item);
88 return snprintf(page, PAGE_SIZE, "%d\n",
89 le16_to_cpu(port->disc_addr.portid));
92 static ssize_t nvmet_addr_portid_store(struct config_item *item,
93 const char *page, size_t count)
95 struct nvmet_port *port = to_nvmet_port(item);
98 if (kstrtou16(page, 0, &portid)) {
99 pr_err("Invalid value '%s' for portid\n", page);
104 pr_err("Cannot modify address while enabled\n");
105 pr_err("Disable the address before modifying\n");
108 port->disc_addr.portid = cpu_to_le16(portid);
112 CONFIGFS_ATTR(nvmet_, addr_portid);
114 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
117 struct nvmet_port *port = to_nvmet_port(item);
119 return snprintf(page, PAGE_SIZE, "%s\n",
120 port->disc_addr.traddr);
123 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
124 const char *page, size_t count)
126 struct nvmet_port *port = to_nvmet_port(item);
128 if (count > NVMF_TRADDR_SIZE) {
129 pr_err("Invalid value '%s' for traddr\n", page);
134 pr_err("Cannot modify address while enabled\n");
135 pr_err("Disable the address before modifying\n");
139 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
144 CONFIGFS_ATTR(nvmet_, addr_traddr);
146 static ssize_t nvmet_addr_treq_show(struct config_item *item,
149 switch (to_nvmet_port(item)->disc_addr.treq &
150 NVME_TREQ_SECURE_CHANNEL_MASK) {
151 case NVMF_TREQ_NOT_SPECIFIED:
152 return sprintf(page, "not specified\n");
153 case NVMF_TREQ_REQUIRED:
154 return sprintf(page, "required\n");
155 case NVMF_TREQ_NOT_REQUIRED:
156 return sprintf(page, "not required\n");
158 return sprintf(page, "\n");
162 static ssize_t nvmet_addr_treq_store(struct config_item *item,
163 const char *page, size_t count)
165 struct nvmet_port *port = to_nvmet_port(item);
166 u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
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 treq |= NVMF_TREQ_NOT_SPECIFIED;
176 } else if (sysfs_streq(page, "required")) {
177 treq |= NVMF_TREQ_REQUIRED;
178 } else if (sysfs_streq(page, "not required")) {
179 treq |= NVMF_TREQ_NOT_REQUIRED;
181 pr_err("Invalid value '%s' for treq\n", page);
184 port->disc_addr.treq = treq;
189 CONFIGFS_ATTR(nvmet_, addr_treq);
191 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
194 struct nvmet_port *port = to_nvmet_port(item);
196 return snprintf(page, PAGE_SIZE, "%s\n",
197 port->disc_addr.trsvcid);
200 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
201 const char *page, size_t count)
203 struct nvmet_port *port = to_nvmet_port(item);
205 if (count > NVMF_TRSVCID_SIZE) {
206 pr_err("Invalid value '%s' for trsvcid\n", page);
210 pr_err("Cannot modify address while enabled\n");
211 pr_err("Disable the address before modifying\n");
215 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
220 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
222 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
225 struct nvmet_port *port = to_nvmet_port(item);
227 return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
230 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
231 const char *page, size_t count)
233 struct nvmet_port *port = to_nvmet_port(item);
237 pr_err("Cannot modify inline_data_size while port enabled\n");
238 pr_err("Disable the port before modifying\n");
241 ret = kstrtoint(page, 0, &port->inline_data_size);
243 pr_err("Invalid value '%s' for inline_data_size\n", page);
249 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
251 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
254 struct nvmet_port *port = to_nvmet_port(item);
257 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
258 if (port->disc_addr.trtype != nvmet_transport_names[i].type)
260 return sprintf(page, "%s\n", nvmet_transport_names[i].name);
263 return sprintf(page, "\n");
266 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
268 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
269 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
270 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
273 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
274 const char *page, size_t count)
276 struct nvmet_port *port = to_nvmet_port(item);
280 pr_err("Cannot modify address while enabled\n");
281 pr_err("Disable the address before modifying\n");
285 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
286 if (sysfs_streq(page, nvmet_transport_names[i].name))
290 pr_err("Invalid value '%s' for trtype\n", page);
293 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
294 port->disc_addr.trtype = nvmet_transport_names[i].type;
295 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
296 nvmet_port_init_tsas_rdma(port);
300 CONFIGFS_ATTR(nvmet_, addr_trtype);
303 * Namespace structures & file operation functions below
305 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
307 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
310 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
311 const char *page, size_t count)
313 struct nvmet_ns *ns = to_nvmet_ns(item);
314 struct nvmet_subsys *subsys = ns->subsys;
318 mutex_lock(&subsys->lock);
324 len = strcspn(page, "\n");
328 kfree(ns->device_path);
330 ns->device_path = kstrndup(page, len, GFP_KERNEL);
331 if (!ns->device_path)
334 mutex_unlock(&subsys->lock);
338 mutex_unlock(&subsys->lock);
342 CONFIGFS_ATTR(nvmet_ns_, device_path);
344 #ifdef CONFIG_PCI_P2PDMA
345 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
347 struct nvmet_ns *ns = to_nvmet_ns(item);
349 return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
352 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
353 const char *page, size_t count)
355 struct nvmet_ns *ns = to_nvmet_ns(item);
356 struct pci_dev *p2p_dev = NULL;
361 mutex_lock(&ns->subsys->lock);
367 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
373 ns->use_p2pmem = use_p2pmem;
374 pci_dev_put(ns->p2p_dev);
375 ns->p2p_dev = p2p_dev;
378 mutex_unlock(&ns->subsys->lock);
383 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
384 #endif /* CONFIG_PCI_P2PDMA */
386 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
388 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
391 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
392 const char *page, size_t count)
394 struct nvmet_ns *ns = to_nvmet_ns(item);
395 struct nvmet_subsys *subsys = ns->subsys;
399 mutex_lock(&subsys->lock);
406 if (uuid_parse(page, &ns->uuid))
410 mutex_unlock(&subsys->lock);
411 return ret ? ret : count;
414 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
416 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
418 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
421 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
422 const char *page, size_t count)
424 struct nvmet_ns *ns = to_nvmet_ns(item);
425 struct nvmet_subsys *subsys = ns->subsys;
427 const char *p = page;
431 mutex_lock(&subsys->lock);
437 for (i = 0; i < 16; i++) {
438 if (p + 2 > page + count) {
442 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
447 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
450 if (*p == '-' || *p == ':')
454 memcpy(&ns->nguid, nguid, sizeof(nguid));
456 mutex_unlock(&subsys->lock);
457 return ret ? ret : count;
460 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
462 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
464 return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
467 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
468 const char *page, size_t count)
470 struct nvmet_ns *ns = to_nvmet_ns(item);
471 u32 oldgrpid, newgrpid;
474 ret = kstrtou32(page, 0, &newgrpid);
478 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
481 down_write(&nvmet_ana_sem);
482 oldgrpid = ns->anagrpid;
483 nvmet_ana_group_enabled[newgrpid]++;
484 ns->anagrpid = newgrpid;
485 nvmet_ana_group_enabled[oldgrpid]--;
487 up_write(&nvmet_ana_sem);
489 nvmet_send_ana_event(ns->subsys, NULL);
493 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
495 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
497 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
500 static ssize_t nvmet_ns_enable_store(struct config_item *item,
501 const char *page, size_t count)
503 struct nvmet_ns *ns = to_nvmet_ns(item);
507 if (strtobool(page, &enable))
511 ret = nvmet_ns_enable(ns);
513 nvmet_ns_disable(ns);
515 return ret ? ret : count;
518 CONFIGFS_ATTR(nvmet_ns_, enable);
520 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
522 return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
525 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
526 const char *page, size_t count)
528 struct nvmet_ns *ns = to_nvmet_ns(item);
531 if (strtobool(page, &val))
534 mutex_lock(&ns->subsys->lock);
536 pr_err("disable ns before setting buffered_io value.\n");
537 mutex_unlock(&ns->subsys->lock);
541 ns->buffered_io = val;
542 mutex_unlock(&ns->subsys->lock);
546 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
548 static struct configfs_attribute *nvmet_ns_attrs[] = {
549 &nvmet_ns_attr_device_path,
550 &nvmet_ns_attr_device_nguid,
551 &nvmet_ns_attr_device_uuid,
552 &nvmet_ns_attr_ana_grpid,
553 &nvmet_ns_attr_enable,
554 &nvmet_ns_attr_buffered_io,
555 #ifdef CONFIG_PCI_P2PDMA
556 &nvmet_ns_attr_p2pmem,
561 static void nvmet_ns_release(struct config_item *item)
563 struct nvmet_ns *ns = to_nvmet_ns(item);
568 static struct configfs_item_operations nvmet_ns_item_ops = {
569 .release = nvmet_ns_release,
572 static const struct config_item_type nvmet_ns_type = {
573 .ct_item_ops = &nvmet_ns_item_ops,
574 .ct_attrs = nvmet_ns_attrs,
575 .ct_owner = THIS_MODULE,
578 static struct config_group *nvmet_ns_make(struct config_group *group,
581 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
586 ret = kstrtou32(name, 0, &nsid);
591 if (nsid == 0 || nsid == NVME_NSID_ALL)
595 ns = nvmet_ns_alloc(subsys, nsid);
598 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
600 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
607 static struct configfs_group_operations nvmet_namespaces_group_ops = {
608 .make_group = nvmet_ns_make,
611 static const struct config_item_type nvmet_namespaces_type = {
612 .ct_group_ops = &nvmet_namespaces_group_ops,
613 .ct_owner = THIS_MODULE,
616 static int nvmet_port_subsys_allow_link(struct config_item *parent,
617 struct config_item *target)
619 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
620 struct nvmet_subsys *subsys;
621 struct nvmet_subsys_link *link, *p;
624 if (target->ci_type != &nvmet_subsys_type) {
625 pr_err("can only link subsystems into the subsystems dir.!\n");
628 subsys = to_subsys(target);
629 link = kmalloc(sizeof(*link), GFP_KERNEL);
632 link->subsys = subsys;
634 down_write(&nvmet_config_sem);
636 list_for_each_entry(p, &port->subsystems, entry) {
637 if (p->subsys == subsys)
641 if (list_empty(&port->subsystems)) {
642 ret = nvmet_enable_port(port);
647 list_add_tail(&link->entry, &port->subsystems);
648 nvmet_port_disc_changed(port, subsys);
650 up_write(&nvmet_config_sem);
654 up_write(&nvmet_config_sem);
659 static void nvmet_port_subsys_drop_link(struct config_item *parent,
660 struct config_item *target)
662 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
663 struct nvmet_subsys *subsys = to_subsys(target);
664 struct nvmet_subsys_link *p;
666 down_write(&nvmet_config_sem);
667 list_for_each_entry(p, &port->subsystems, entry) {
668 if (p->subsys == subsys)
671 up_write(&nvmet_config_sem);
676 nvmet_port_disc_changed(port, subsys);
678 if (list_empty(&port->subsystems))
679 nvmet_disable_port(port);
680 up_write(&nvmet_config_sem);
684 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
685 .allow_link = nvmet_port_subsys_allow_link,
686 .drop_link = nvmet_port_subsys_drop_link,
689 static const struct config_item_type nvmet_port_subsys_type = {
690 .ct_item_ops = &nvmet_port_subsys_item_ops,
691 .ct_owner = THIS_MODULE,
694 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
695 struct config_item *target)
697 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
698 struct nvmet_host *host;
699 struct nvmet_host_link *link, *p;
702 if (target->ci_type != &nvmet_host_type) {
703 pr_err("can only link hosts into the allowed_hosts directory!\n");
707 host = to_host(target);
708 link = kmalloc(sizeof(*link), GFP_KERNEL);
713 down_write(&nvmet_config_sem);
715 if (subsys->allow_any_host) {
716 pr_err("can't add hosts when allow_any_host is set!\n");
721 list_for_each_entry(p, &subsys->hosts, entry) {
722 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
725 list_add_tail(&link->entry, &subsys->hosts);
726 nvmet_subsys_disc_changed(subsys, host);
728 up_write(&nvmet_config_sem);
731 up_write(&nvmet_config_sem);
736 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
737 struct config_item *target)
739 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
740 struct nvmet_host *host = to_host(target);
741 struct nvmet_host_link *p;
743 down_write(&nvmet_config_sem);
744 list_for_each_entry(p, &subsys->hosts, entry) {
745 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
748 up_write(&nvmet_config_sem);
753 nvmet_subsys_disc_changed(subsys, host);
755 up_write(&nvmet_config_sem);
759 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
760 .allow_link = nvmet_allowed_hosts_allow_link,
761 .drop_link = nvmet_allowed_hosts_drop_link,
764 static const struct config_item_type nvmet_allowed_hosts_type = {
765 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
766 .ct_owner = THIS_MODULE,
769 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
772 return snprintf(page, PAGE_SIZE, "%d\n",
773 to_subsys(item)->allow_any_host);
776 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
777 const char *page, size_t count)
779 struct nvmet_subsys *subsys = to_subsys(item);
783 if (strtobool(page, &allow_any_host))
786 down_write(&nvmet_config_sem);
787 if (allow_any_host && !list_empty(&subsys->hosts)) {
788 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
793 if (subsys->allow_any_host != allow_any_host) {
794 subsys->allow_any_host = allow_any_host;
795 nvmet_subsys_disc_changed(subsys, NULL);
799 up_write(&nvmet_config_sem);
800 return ret ? ret : count;
803 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
805 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
808 struct nvmet_subsys *subsys = to_subsys(item);
810 if (NVME_TERTIARY(subsys->ver))
811 return snprintf(page, PAGE_SIZE, "%d.%d.%d\n",
812 (int)NVME_MAJOR(subsys->ver),
813 (int)NVME_MINOR(subsys->ver),
814 (int)NVME_TERTIARY(subsys->ver));
816 return snprintf(page, PAGE_SIZE, "%d.%d\n",
817 (int)NVME_MAJOR(subsys->ver),
818 (int)NVME_MINOR(subsys->ver));
821 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
822 const char *page, size_t count)
824 struct nvmet_subsys *subsys = to_subsys(item);
825 int major, minor, tertiary = 0;
829 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
830 if (ret != 2 && ret != 3)
833 down_write(&nvmet_config_sem);
834 subsys->ver = NVME_VS(major, minor, tertiary);
835 up_write(&nvmet_config_sem);
839 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
841 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
844 struct nvmet_subsys *subsys = to_subsys(item);
846 return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
849 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
850 const char *page, size_t count)
852 struct nvmet_subsys *subsys = to_subsys(item);
854 down_write(&nvmet_config_sem);
855 sscanf(page, "%llx\n", &subsys->serial);
856 up_write(&nvmet_config_sem);
860 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
862 static struct configfs_attribute *nvmet_subsys_attrs[] = {
863 &nvmet_subsys_attr_attr_allow_any_host,
864 &nvmet_subsys_attr_attr_version,
865 &nvmet_subsys_attr_attr_serial,
870 * Subsystem structures & folder operation functions below
872 static void nvmet_subsys_release(struct config_item *item)
874 struct nvmet_subsys *subsys = to_subsys(item);
876 nvmet_subsys_del_ctrls(subsys);
877 nvmet_subsys_put(subsys);
880 static struct configfs_item_operations nvmet_subsys_item_ops = {
881 .release = nvmet_subsys_release,
884 static const struct config_item_type nvmet_subsys_type = {
885 .ct_item_ops = &nvmet_subsys_item_ops,
886 .ct_attrs = nvmet_subsys_attrs,
887 .ct_owner = THIS_MODULE,
890 static struct config_group *nvmet_subsys_make(struct config_group *group,
893 struct nvmet_subsys *subsys;
895 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
896 pr_err("can't create discovery subsystem through configfs\n");
897 return ERR_PTR(-EINVAL);
900 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
902 return ERR_CAST(subsys);
904 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
906 config_group_init_type_name(&subsys->namespaces_group,
907 "namespaces", &nvmet_namespaces_type);
908 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
910 config_group_init_type_name(&subsys->allowed_hosts_group,
911 "allowed_hosts", &nvmet_allowed_hosts_type);
912 configfs_add_default_group(&subsys->allowed_hosts_group,
915 return &subsys->group;
918 static struct configfs_group_operations nvmet_subsystems_group_ops = {
919 .make_group = nvmet_subsys_make,
922 static const struct config_item_type nvmet_subsystems_type = {
923 .ct_group_ops = &nvmet_subsystems_group_ops,
924 .ct_owner = THIS_MODULE,
927 static ssize_t nvmet_referral_enable_show(struct config_item *item,
930 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
933 static ssize_t nvmet_referral_enable_store(struct config_item *item,
934 const char *page, size_t count)
936 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
937 struct nvmet_port *port = to_nvmet_port(item);
940 if (strtobool(page, &enable))
944 nvmet_referral_enable(parent, port);
946 nvmet_referral_disable(parent, port);
950 pr_err("Invalid value '%s' for enable\n", page);
954 CONFIGFS_ATTR(nvmet_referral_, enable);
957 * Discovery Service subsystem definitions
959 static struct configfs_attribute *nvmet_referral_attrs[] = {
960 &nvmet_attr_addr_adrfam,
961 &nvmet_attr_addr_portid,
962 &nvmet_attr_addr_treq,
963 &nvmet_attr_addr_traddr,
964 &nvmet_attr_addr_trsvcid,
965 &nvmet_attr_addr_trtype,
966 &nvmet_referral_attr_enable,
970 static void nvmet_referral_release(struct config_item *item)
972 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
973 struct nvmet_port *port = to_nvmet_port(item);
975 nvmet_referral_disable(parent, port);
979 static struct configfs_item_operations nvmet_referral_item_ops = {
980 .release = nvmet_referral_release,
983 static const struct config_item_type nvmet_referral_type = {
984 .ct_owner = THIS_MODULE,
985 .ct_attrs = nvmet_referral_attrs,
986 .ct_item_ops = &nvmet_referral_item_ops,
989 static struct config_group *nvmet_referral_make(
990 struct config_group *group, const char *name)
992 struct nvmet_port *port;
994 port = kzalloc(sizeof(*port), GFP_KERNEL);
996 return ERR_PTR(-ENOMEM);
998 INIT_LIST_HEAD(&port->entry);
999 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1001 return &port->group;
1004 static struct configfs_group_operations nvmet_referral_group_ops = {
1005 .make_group = nvmet_referral_make,
1008 static const struct config_item_type nvmet_referrals_type = {
1009 .ct_owner = THIS_MODULE,
1010 .ct_group_ops = &nvmet_referral_group_ops,
1014 enum nvme_ana_state state;
1016 } nvmet_ana_state_names[] = {
1017 { NVME_ANA_OPTIMIZED, "optimized" },
1018 { NVME_ANA_NONOPTIMIZED, "non-optimized" },
1019 { NVME_ANA_INACCESSIBLE, "inaccessible" },
1020 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
1021 { NVME_ANA_CHANGE, "change" },
1024 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1027 struct nvmet_ana_group *grp = to_ana_group(item);
1028 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1031 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1032 if (state != nvmet_ana_state_names[i].state)
1034 return sprintf(page, "%s\n", nvmet_ana_state_names[i].name);
1037 return sprintf(page, "\n");
1040 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1041 const char *page, size_t count)
1043 struct nvmet_ana_group *grp = to_ana_group(item);
1046 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1047 if (sysfs_streq(page, nvmet_ana_state_names[i].name))
1051 pr_err("Invalid value '%s' for ana_state\n", page);
1055 down_write(&nvmet_ana_sem);
1056 grp->port->ana_state[grp->grpid] = nvmet_ana_state_names[i].state;
1058 up_write(&nvmet_ana_sem);
1060 nvmet_port_send_ana_event(grp->port);
1064 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1066 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1067 &nvmet_ana_group_attr_ana_state,
1071 static void nvmet_ana_group_release(struct config_item *item)
1073 struct nvmet_ana_group *grp = to_ana_group(item);
1075 if (grp == &grp->port->ana_default_group)
1078 down_write(&nvmet_ana_sem);
1079 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1080 nvmet_ana_group_enabled[grp->grpid]--;
1081 up_write(&nvmet_ana_sem);
1083 nvmet_port_send_ana_event(grp->port);
1087 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1088 .release = nvmet_ana_group_release,
1091 static const struct config_item_type nvmet_ana_group_type = {
1092 .ct_item_ops = &nvmet_ana_group_item_ops,
1093 .ct_attrs = nvmet_ana_group_attrs,
1094 .ct_owner = THIS_MODULE,
1097 static struct config_group *nvmet_ana_groups_make_group(
1098 struct config_group *group, const char *name)
1100 struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1101 struct nvmet_ana_group *grp;
1105 ret = kstrtou32(name, 0, &grpid);
1110 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1114 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1120 down_write(&nvmet_ana_sem);
1121 nvmet_ana_group_enabled[grpid]++;
1122 up_write(&nvmet_ana_sem);
1124 nvmet_port_send_ana_event(grp->port);
1126 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1129 return ERR_PTR(ret);
1132 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1133 .make_group = nvmet_ana_groups_make_group,
1136 static const struct config_item_type nvmet_ana_groups_type = {
1137 .ct_group_ops = &nvmet_ana_groups_group_ops,
1138 .ct_owner = THIS_MODULE,
1142 * Ports definitions.
1144 static void nvmet_port_release(struct config_item *item)
1146 struct nvmet_port *port = to_nvmet_port(item);
1148 list_del(&port->global_entry);
1150 kfree(port->ana_state);
1154 static struct configfs_attribute *nvmet_port_attrs[] = {
1155 &nvmet_attr_addr_adrfam,
1156 &nvmet_attr_addr_treq,
1157 &nvmet_attr_addr_traddr,
1158 &nvmet_attr_addr_trsvcid,
1159 &nvmet_attr_addr_trtype,
1160 &nvmet_attr_param_inline_data_size,
1164 static struct configfs_item_operations nvmet_port_item_ops = {
1165 .release = nvmet_port_release,
1168 static const struct config_item_type nvmet_port_type = {
1169 .ct_attrs = nvmet_port_attrs,
1170 .ct_item_ops = &nvmet_port_item_ops,
1171 .ct_owner = THIS_MODULE,
1174 static struct config_group *nvmet_ports_make(struct config_group *group,
1177 struct nvmet_port *port;
1181 if (kstrtou16(name, 0, &portid))
1182 return ERR_PTR(-EINVAL);
1184 port = kzalloc(sizeof(*port), GFP_KERNEL);
1186 return ERR_PTR(-ENOMEM);
1188 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1189 sizeof(*port->ana_state), GFP_KERNEL);
1190 if (!port->ana_state) {
1192 return ERR_PTR(-ENOMEM);
1195 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1196 if (i == NVMET_DEFAULT_ANA_GRPID)
1197 port->ana_state[1] = NVME_ANA_OPTIMIZED;
1199 port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1202 list_add(&port->global_entry, &nvmet_ports_list);
1204 INIT_LIST_HEAD(&port->entry);
1205 INIT_LIST_HEAD(&port->subsystems);
1206 INIT_LIST_HEAD(&port->referrals);
1207 port->inline_data_size = -1; /* < 0 == let the transport choose */
1209 port->disc_addr.portid = cpu_to_le16(portid);
1210 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1211 config_group_init_type_name(&port->group, name, &nvmet_port_type);
1213 config_group_init_type_name(&port->subsys_group,
1214 "subsystems", &nvmet_port_subsys_type);
1215 configfs_add_default_group(&port->subsys_group, &port->group);
1217 config_group_init_type_name(&port->referrals_group,
1218 "referrals", &nvmet_referrals_type);
1219 configfs_add_default_group(&port->referrals_group, &port->group);
1221 config_group_init_type_name(&port->ana_groups_group,
1222 "ana_groups", &nvmet_ana_groups_type);
1223 configfs_add_default_group(&port->ana_groups_group, &port->group);
1225 port->ana_default_group.port = port;
1226 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1227 config_group_init_type_name(&port->ana_default_group.group,
1228 __stringify(NVMET_DEFAULT_ANA_GRPID),
1229 &nvmet_ana_group_type);
1230 configfs_add_default_group(&port->ana_default_group.group,
1231 &port->ana_groups_group);
1233 return &port->group;
1236 static struct configfs_group_operations nvmet_ports_group_ops = {
1237 .make_group = nvmet_ports_make,
1240 static const struct config_item_type nvmet_ports_type = {
1241 .ct_group_ops = &nvmet_ports_group_ops,
1242 .ct_owner = THIS_MODULE,
1245 static struct config_group nvmet_subsystems_group;
1246 static struct config_group nvmet_ports_group;
1248 static void nvmet_host_release(struct config_item *item)
1250 struct nvmet_host *host = to_host(item);
1255 static struct configfs_item_operations nvmet_host_item_ops = {
1256 .release = nvmet_host_release,
1259 static const struct config_item_type nvmet_host_type = {
1260 .ct_item_ops = &nvmet_host_item_ops,
1261 .ct_owner = THIS_MODULE,
1264 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1267 struct nvmet_host *host;
1269 host = kzalloc(sizeof(*host), GFP_KERNEL);
1271 return ERR_PTR(-ENOMEM);
1273 config_group_init_type_name(&host->group, name, &nvmet_host_type);
1275 return &host->group;
1278 static struct configfs_group_operations nvmet_hosts_group_ops = {
1279 .make_group = nvmet_hosts_make_group,
1282 static const struct config_item_type nvmet_hosts_type = {
1283 .ct_group_ops = &nvmet_hosts_group_ops,
1284 .ct_owner = THIS_MODULE,
1287 static struct config_group nvmet_hosts_group;
1289 static const struct config_item_type nvmet_root_type = {
1290 .ct_owner = THIS_MODULE,
1293 static struct configfs_subsystem nvmet_configfs_subsystem = {
1296 .ci_namebuf = "nvmet",
1297 .ci_type = &nvmet_root_type,
1302 int __init nvmet_init_configfs(void)
1306 config_group_init(&nvmet_configfs_subsystem.su_group);
1307 mutex_init(&nvmet_configfs_subsystem.su_mutex);
1309 config_group_init_type_name(&nvmet_subsystems_group,
1310 "subsystems", &nvmet_subsystems_type);
1311 configfs_add_default_group(&nvmet_subsystems_group,
1312 &nvmet_configfs_subsystem.su_group);
1314 config_group_init_type_name(&nvmet_ports_group,
1315 "ports", &nvmet_ports_type);
1316 configfs_add_default_group(&nvmet_ports_group,
1317 &nvmet_configfs_subsystem.su_group);
1319 config_group_init_type_name(&nvmet_hosts_group,
1320 "hosts", &nvmet_hosts_type);
1321 configfs_add_default_group(&nvmet_hosts_group,
1322 &nvmet_configfs_subsystem.su_group);
1324 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1326 pr_err("configfs_register_subsystem: %d\n", ret);
1333 void __exit nvmet_exit_configfs(void)
1335 configfs_unregister_subsystem(&nvmet_configfs_subsystem);