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
) {
592 pr_err("invalid nsid %#x", nsid
);
597 ns
= nvmet_ns_alloc(subsys
, nsid
);
600 config_group_init_type_name(&ns
->group
, name
, &nvmet_ns_type
);
602 pr_info("adding nsid %d to subsystem %s\n", nsid
, subsys
->subsysnqn
);
609 static struct configfs_group_operations nvmet_namespaces_group_ops
= {
610 .make_group
= nvmet_ns_make
,
613 static const struct config_item_type nvmet_namespaces_type
= {
614 .ct_group_ops
= &nvmet_namespaces_group_ops
,
615 .ct_owner
= THIS_MODULE
,
618 static int nvmet_port_subsys_allow_link(struct config_item
*parent
,
619 struct config_item
*target
)
621 struct nvmet_port
*port
= to_nvmet_port(parent
->ci_parent
);
622 struct nvmet_subsys
*subsys
;
623 struct nvmet_subsys_link
*link
, *p
;
626 if (target
->ci_type
!= &nvmet_subsys_type
) {
627 pr_err("can only link subsystems into the subsystems dir.!\n");
630 subsys
= to_subsys(target
);
631 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
634 link
->subsys
= subsys
;
636 down_write(&nvmet_config_sem
);
638 list_for_each_entry(p
, &port
->subsystems
, entry
) {
639 if (p
->subsys
== subsys
)
643 if (list_empty(&port
->subsystems
)) {
644 ret
= nvmet_enable_port(port
);
649 list_add_tail(&link
->entry
, &port
->subsystems
);
650 nvmet_port_disc_changed(port
, subsys
);
652 up_write(&nvmet_config_sem
);
656 up_write(&nvmet_config_sem
);
661 static void nvmet_port_subsys_drop_link(struct config_item
*parent
,
662 struct config_item
*target
)
664 struct nvmet_port
*port
= to_nvmet_port(parent
->ci_parent
);
665 struct nvmet_subsys
*subsys
= to_subsys(target
);
666 struct nvmet_subsys_link
*p
;
668 down_write(&nvmet_config_sem
);
669 list_for_each_entry(p
, &port
->subsystems
, entry
) {
670 if (p
->subsys
== subsys
)
673 up_write(&nvmet_config_sem
);
678 nvmet_port_del_ctrls(port
, subsys
);
679 nvmet_port_disc_changed(port
, subsys
);
681 if (list_empty(&port
->subsystems
))
682 nvmet_disable_port(port
);
683 up_write(&nvmet_config_sem
);
687 static struct configfs_item_operations nvmet_port_subsys_item_ops
= {
688 .allow_link
= nvmet_port_subsys_allow_link
,
689 .drop_link
= nvmet_port_subsys_drop_link
,
692 static const struct config_item_type nvmet_port_subsys_type
= {
693 .ct_item_ops
= &nvmet_port_subsys_item_ops
,
694 .ct_owner
= THIS_MODULE
,
697 static int nvmet_allowed_hosts_allow_link(struct config_item
*parent
,
698 struct config_item
*target
)
700 struct nvmet_subsys
*subsys
= to_subsys(parent
->ci_parent
);
701 struct nvmet_host
*host
;
702 struct nvmet_host_link
*link
, *p
;
705 if (target
->ci_type
!= &nvmet_host_type
) {
706 pr_err("can only link hosts into the allowed_hosts directory!\n");
710 host
= to_host(target
);
711 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
716 down_write(&nvmet_config_sem
);
718 if (subsys
->allow_any_host
) {
719 pr_err("can't add hosts when allow_any_host is set!\n");
724 list_for_each_entry(p
, &subsys
->hosts
, entry
) {
725 if (!strcmp(nvmet_host_name(p
->host
), nvmet_host_name(host
)))
728 list_add_tail(&link
->entry
, &subsys
->hosts
);
729 nvmet_subsys_disc_changed(subsys
, host
);
731 up_write(&nvmet_config_sem
);
734 up_write(&nvmet_config_sem
);
739 static void nvmet_allowed_hosts_drop_link(struct config_item
*parent
,
740 struct config_item
*target
)
742 struct nvmet_subsys
*subsys
= to_subsys(parent
->ci_parent
);
743 struct nvmet_host
*host
= to_host(target
);
744 struct nvmet_host_link
*p
;
746 down_write(&nvmet_config_sem
);
747 list_for_each_entry(p
, &subsys
->hosts
, entry
) {
748 if (!strcmp(nvmet_host_name(p
->host
), nvmet_host_name(host
)))
751 up_write(&nvmet_config_sem
);
756 nvmet_subsys_disc_changed(subsys
, host
);
758 up_write(&nvmet_config_sem
);
762 static struct configfs_item_operations nvmet_allowed_hosts_item_ops
= {
763 .allow_link
= nvmet_allowed_hosts_allow_link
,
764 .drop_link
= nvmet_allowed_hosts_drop_link
,
767 static const struct config_item_type nvmet_allowed_hosts_type
= {
768 .ct_item_ops
= &nvmet_allowed_hosts_item_ops
,
769 .ct_owner
= THIS_MODULE
,
772 static ssize_t
nvmet_subsys_attr_allow_any_host_show(struct config_item
*item
,
775 return snprintf(page
, PAGE_SIZE
, "%d\n",
776 to_subsys(item
)->allow_any_host
);
779 static ssize_t
nvmet_subsys_attr_allow_any_host_store(struct config_item
*item
,
780 const char *page
, size_t count
)
782 struct nvmet_subsys
*subsys
= to_subsys(item
);
786 if (strtobool(page
, &allow_any_host
))
789 down_write(&nvmet_config_sem
);
790 if (allow_any_host
&& !list_empty(&subsys
->hosts
)) {
791 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
796 if (subsys
->allow_any_host
!= allow_any_host
) {
797 subsys
->allow_any_host
= allow_any_host
;
798 nvmet_subsys_disc_changed(subsys
, NULL
);
802 up_write(&nvmet_config_sem
);
803 return ret
? ret
: count
;
806 CONFIGFS_ATTR(nvmet_subsys_
, attr_allow_any_host
);
808 static ssize_t
nvmet_subsys_attr_version_show(struct config_item
*item
,
811 struct nvmet_subsys
*subsys
= to_subsys(item
);
813 if (NVME_TERTIARY(subsys
->ver
))
814 return snprintf(page
, PAGE_SIZE
, "%d.%d.%d\n",
815 (int)NVME_MAJOR(subsys
->ver
),
816 (int)NVME_MINOR(subsys
->ver
),
817 (int)NVME_TERTIARY(subsys
->ver
));
819 return snprintf(page
, PAGE_SIZE
, "%d.%d\n",
820 (int)NVME_MAJOR(subsys
->ver
),
821 (int)NVME_MINOR(subsys
->ver
));
824 static ssize_t
nvmet_subsys_attr_version_store(struct config_item
*item
,
825 const char *page
, size_t count
)
827 struct nvmet_subsys
*subsys
= to_subsys(item
);
828 int major
, minor
, tertiary
= 0;
832 ret
= sscanf(page
, "%d.%d.%d\n", &major
, &minor
, &tertiary
);
833 if (ret
!= 2 && ret
!= 3)
836 down_write(&nvmet_config_sem
);
837 subsys
->ver
= NVME_VS(major
, minor
, tertiary
);
838 up_write(&nvmet_config_sem
);
842 CONFIGFS_ATTR(nvmet_subsys_
, attr_version
);
844 static ssize_t
nvmet_subsys_attr_serial_show(struct config_item
*item
,
847 struct nvmet_subsys
*subsys
= to_subsys(item
);
849 return snprintf(page
, PAGE_SIZE
, "%llx\n", subsys
->serial
);
852 static ssize_t
nvmet_subsys_attr_serial_store(struct config_item
*item
,
853 const char *page
, size_t count
)
855 struct nvmet_subsys
*subsys
= to_subsys(item
);
857 down_write(&nvmet_config_sem
);
858 sscanf(page
, "%llx\n", &subsys
->serial
);
859 up_write(&nvmet_config_sem
);
863 CONFIGFS_ATTR(nvmet_subsys_
, attr_serial
);
865 static struct configfs_attribute
*nvmet_subsys_attrs
[] = {
866 &nvmet_subsys_attr_attr_allow_any_host
,
867 &nvmet_subsys_attr_attr_version
,
868 &nvmet_subsys_attr_attr_serial
,
873 * Subsystem structures & folder operation functions below
875 static void nvmet_subsys_release(struct config_item
*item
)
877 struct nvmet_subsys
*subsys
= to_subsys(item
);
879 nvmet_subsys_del_ctrls(subsys
);
880 nvmet_subsys_put(subsys
);
883 static struct configfs_item_operations nvmet_subsys_item_ops
= {
884 .release
= nvmet_subsys_release
,
887 static const struct config_item_type nvmet_subsys_type
= {
888 .ct_item_ops
= &nvmet_subsys_item_ops
,
889 .ct_attrs
= nvmet_subsys_attrs
,
890 .ct_owner
= THIS_MODULE
,
893 static struct config_group
*nvmet_subsys_make(struct config_group
*group
,
896 struct nvmet_subsys
*subsys
;
898 if (sysfs_streq(name
, NVME_DISC_SUBSYS_NAME
)) {
899 pr_err("can't create discovery subsystem through configfs\n");
900 return ERR_PTR(-EINVAL
);
903 subsys
= nvmet_subsys_alloc(name
, NVME_NQN_NVME
);
905 return ERR_CAST(subsys
);
907 config_group_init_type_name(&subsys
->group
, name
, &nvmet_subsys_type
);
909 config_group_init_type_name(&subsys
->namespaces_group
,
910 "namespaces", &nvmet_namespaces_type
);
911 configfs_add_default_group(&subsys
->namespaces_group
, &subsys
->group
);
913 config_group_init_type_name(&subsys
->allowed_hosts_group
,
914 "allowed_hosts", &nvmet_allowed_hosts_type
);
915 configfs_add_default_group(&subsys
->allowed_hosts_group
,
918 return &subsys
->group
;
921 static struct configfs_group_operations nvmet_subsystems_group_ops
= {
922 .make_group
= nvmet_subsys_make
,
925 static const struct config_item_type nvmet_subsystems_type
= {
926 .ct_group_ops
= &nvmet_subsystems_group_ops
,
927 .ct_owner
= THIS_MODULE
,
930 static ssize_t
nvmet_referral_enable_show(struct config_item
*item
,
933 return snprintf(page
, PAGE_SIZE
, "%d\n", to_nvmet_port(item
)->enabled
);
936 static ssize_t
nvmet_referral_enable_store(struct config_item
*item
,
937 const char *page
, size_t count
)
939 struct nvmet_port
*parent
= to_nvmet_port(item
->ci_parent
->ci_parent
);
940 struct nvmet_port
*port
= to_nvmet_port(item
);
943 if (strtobool(page
, &enable
))
947 nvmet_referral_enable(parent
, port
);
949 nvmet_referral_disable(parent
, port
);
953 pr_err("Invalid value '%s' for enable\n", page
);
957 CONFIGFS_ATTR(nvmet_referral_
, enable
);
960 * Discovery Service subsystem definitions
962 static struct configfs_attribute
*nvmet_referral_attrs
[] = {
963 &nvmet_attr_addr_adrfam
,
964 &nvmet_attr_addr_portid
,
965 &nvmet_attr_addr_treq
,
966 &nvmet_attr_addr_traddr
,
967 &nvmet_attr_addr_trsvcid
,
968 &nvmet_attr_addr_trtype
,
969 &nvmet_referral_attr_enable
,
973 static void nvmet_referral_release(struct config_item
*item
)
975 struct nvmet_port
*parent
= to_nvmet_port(item
->ci_parent
->ci_parent
);
976 struct nvmet_port
*port
= to_nvmet_port(item
);
978 nvmet_referral_disable(parent
, port
);
982 static struct configfs_item_operations nvmet_referral_item_ops
= {
983 .release
= nvmet_referral_release
,
986 static const struct config_item_type nvmet_referral_type
= {
987 .ct_owner
= THIS_MODULE
,
988 .ct_attrs
= nvmet_referral_attrs
,
989 .ct_item_ops
= &nvmet_referral_item_ops
,
992 static struct config_group
*nvmet_referral_make(
993 struct config_group
*group
, const char *name
)
995 struct nvmet_port
*port
;
997 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
999 return ERR_PTR(-ENOMEM
);
1001 INIT_LIST_HEAD(&port
->entry
);
1002 config_group_init_type_name(&port
->group
, name
, &nvmet_referral_type
);
1004 return &port
->group
;
1007 static struct configfs_group_operations nvmet_referral_group_ops
= {
1008 .make_group
= nvmet_referral_make
,
1011 static const struct config_item_type nvmet_referrals_type
= {
1012 .ct_owner
= THIS_MODULE
,
1013 .ct_group_ops
= &nvmet_referral_group_ops
,
1017 enum nvme_ana_state state
;
1019 } nvmet_ana_state_names
[] = {
1020 { NVME_ANA_OPTIMIZED
, "optimized" },
1021 { NVME_ANA_NONOPTIMIZED
, "non-optimized" },
1022 { NVME_ANA_INACCESSIBLE
, "inaccessible" },
1023 { NVME_ANA_PERSISTENT_LOSS
, "persistent-loss" },
1024 { NVME_ANA_CHANGE
, "change" },
1027 static ssize_t
nvmet_ana_group_ana_state_show(struct config_item
*item
,
1030 struct nvmet_ana_group
*grp
= to_ana_group(item
);
1031 enum nvme_ana_state state
= grp
->port
->ana_state
[grp
->grpid
];
1034 for (i
= 0; i
< ARRAY_SIZE(nvmet_ana_state_names
); i
++) {
1035 if (state
!= nvmet_ana_state_names
[i
].state
)
1037 return sprintf(page
, "%s\n", nvmet_ana_state_names
[i
].name
);
1040 return sprintf(page
, "\n");
1043 static ssize_t
nvmet_ana_group_ana_state_store(struct config_item
*item
,
1044 const char *page
, size_t count
)
1046 struct nvmet_ana_group
*grp
= to_ana_group(item
);
1049 for (i
= 0; i
< ARRAY_SIZE(nvmet_ana_state_names
); i
++) {
1050 if (sysfs_streq(page
, nvmet_ana_state_names
[i
].name
))
1054 pr_err("Invalid value '%s' for ana_state\n", page
);
1058 down_write(&nvmet_ana_sem
);
1059 grp
->port
->ana_state
[grp
->grpid
] = nvmet_ana_state_names
[i
].state
;
1061 up_write(&nvmet_ana_sem
);
1063 nvmet_port_send_ana_event(grp
->port
);
1067 CONFIGFS_ATTR(nvmet_ana_group_
, ana_state
);
1069 static struct configfs_attribute
*nvmet_ana_group_attrs
[] = {
1070 &nvmet_ana_group_attr_ana_state
,
1074 static void nvmet_ana_group_release(struct config_item
*item
)
1076 struct nvmet_ana_group
*grp
= to_ana_group(item
);
1078 if (grp
== &grp
->port
->ana_default_group
)
1081 down_write(&nvmet_ana_sem
);
1082 grp
->port
->ana_state
[grp
->grpid
] = NVME_ANA_INACCESSIBLE
;
1083 nvmet_ana_group_enabled
[grp
->grpid
]--;
1084 up_write(&nvmet_ana_sem
);
1086 nvmet_port_send_ana_event(grp
->port
);
1090 static struct configfs_item_operations nvmet_ana_group_item_ops
= {
1091 .release
= nvmet_ana_group_release
,
1094 static const struct config_item_type nvmet_ana_group_type
= {
1095 .ct_item_ops
= &nvmet_ana_group_item_ops
,
1096 .ct_attrs
= nvmet_ana_group_attrs
,
1097 .ct_owner
= THIS_MODULE
,
1100 static struct config_group
*nvmet_ana_groups_make_group(
1101 struct config_group
*group
, const char *name
)
1103 struct nvmet_port
*port
= ana_groups_to_port(&group
->cg_item
);
1104 struct nvmet_ana_group
*grp
;
1108 ret
= kstrtou32(name
, 0, &grpid
);
1113 if (grpid
<= 1 || grpid
> NVMET_MAX_ANAGRPS
)
1117 grp
= kzalloc(sizeof(*grp
), GFP_KERNEL
);
1123 down_write(&nvmet_ana_sem
);
1124 nvmet_ana_group_enabled
[grpid
]++;
1125 up_write(&nvmet_ana_sem
);
1127 nvmet_port_send_ana_event(grp
->port
);
1129 config_group_init_type_name(&grp
->group
, name
, &nvmet_ana_group_type
);
1132 return ERR_PTR(ret
);
1135 static struct configfs_group_operations nvmet_ana_groups_group_ops
= {
1136 .make_group
= nvmet_ana_groups_make_group
,
1139 static const struct config_item_type nvmet_ana_groups_type
= {
1140 .ct_group_ops
= &nvmet_ana_groups_group_ops
,
1141 .ct_owner
= THIS_MODULE
,
1145 * Ports definitions.
1147 static void nvmet_port_release(struct config_item
*item
)
1149 struct nvmet_port
*port
= to_nvmet_port(item
);
1151 list_del(&port
->global_entry
);
1153 kfree(port
->ana_state
);
1157 static struct configfs_attribute
*nvmet_port_attrs
[] = {
1158 &nvmet_attr_addr_adrfam
,
1159 &nvmet_attr_addr_treq
,
1160 &nvmet_attr_addr_traddr
,
1161 &nvmet_attr_addr_trsvcid
,
1162 &nvmet_attr_addr_trtype
,
1163 &nvmet_attr_param_inline_data_size
,
1167 static struct configfs_item_operations nvmet_port_item_ops
= {
1168 .release
= nvmet_port_release
,
1171 static const struct config_item_type nvmet_port_type
= {
1172 .ct_attrs
= nvmet_port_attrs
,
1173 .ct_item_ops
= &nvmet_port_item_ops
,
1174 .ct_owner
= THIS_MODULE
,
1177 static struct config_group
*nvmet_ports_make(struct config_group
*group
,
1180 struct nvmet_port
*port
;
1184 if (kstrtou16(name
, 0, &portid
))
1185 return ERR_PTR(-EINVAL
);
1187 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1189 return ERR_PTR(-ENOMEM
);
1191 port
->ana_state
= kcalloc(NVMET_MAX_ANAGRPS
+ 1,
1192 sizeof(*port
->ana_state
), GFP_KERNEL
);
1193 if (!port
->ana_state
) {
1195 return ERR_PTR(-ENOMEM
);
1198 for (i
= 1; i
<= NVMET_MAX_ANAGRPS
; i
++) {
1199 if (i
== NVMET_DEFAULT_ANA_GRPID
)
1200 port
->ana_state
[1] = NVME_ANA_OPTIMIZED
;
1202 port
->ana_state
[i
] = NVME_ANA_INACCESSIBLE
;
1205 list_add(&port
->global_entry
, &nvmet_ports_list
);
1207 INIT_LIST_HEAD(&port
->entry
);
1208 INIT_LIST_HEAD(&port
->subsystems
);
1209 INIT_LIST_HEAD(&port
->referrals
);
1210 port
->inline_data_size
= -1; /* < 0 == let the transport choose */
1212 port
->disc_addr
.portid
= cpu_to_le16(portid
);
1213 port
->disc_addr
.treq
= NVMF_TREQ_DISABLE_SQFLOW
;
1214 config_group_init_type_name(&port
->group
, name
, &nvmet_port_type
);
1216 config_group_init_type_name(&port
->subsys_group
,
1217 "subsystems", &nvmet_port_subsys_type
);
1218 configfs_add_default_group(&port
->subsys_group
, &port
->group
);
1220 config_group_init_type_name(&port
->referrals_group
,
1221 "referrals", &nvmet_referrals_type
);
1222 configfs_add_default_group(&port
->referrals_group
, &port
->group
);
1224 config_group_init_type_name(&port
->ana_groups_group
,
1225 "ana_groups", &nvmet_ana_groups_type
);
1226 configfs_add_default_group(&port
->ana_groups_group
, &port
->group
);
1228 port
->ana_default_group
.port
= port
;
1229 port
->ana_default_group
.grpid
= NVMET_DEFAULT_ANA_GRPID
;
1230 config_group_init_type_name(&port
->ana_default_group
.group
,
1231 __stringify(NVMET_DEFAULT_ANA_GRPID
),
1232 &nvmet_ana_group_type
);
1233 configfs_add_default_group(&port
->ana_default_group
.group
,
1234 &port
->ana_groups_group
);
1236 return &port
->group
;
1239 static struct configfs_group_operations nvmet_ports_group_ops
= {
1240 .make_group
= nvmet_ports_make
,
1243 static const struct config_item_type nvmet_ports_type
= {
1244 .ct_group_ops
= &nvmet_ports_group_ops
,
1245 .ct_owner
= THIS_MODULE
,
1248 static struct config_group nvmet_subsystems_group
;
1249 static struct config_group nvmet_ports_group
;
1251 static void nvmet_host_release(struct config_item
*item
)
1253 struct nvmet_host
*host
= to_host(item
);
1258 static struct configfs_item_operations nvmet_host_item_ops
= {
1259 .release
= nvmet_host_release
,
1262 static const struct config_item_type nvmet_host_type
= {
1263 .ct_item_ops
= &nvmet_host_item_ops
,
1264 .ct_owner
= THIS_MODULE
,
1267 static struct config_group
*nvmet_hosts_make_group(struct config_group
*group
,
1270 struct nvmet_host
*host
;
1272 host
= kzalloc(sizeof(*host
), GFP_KERNEL
);
1274 return ERR_PTR(-ENOMEM
);
1276 config_group_init_type_name(&host
->group
, name
, &nvmet_host_type
);
1278 return &host
->group
;
1281 static struct configfs_group_operations nvmet_hosts_group_ops
= {
1282 .make_group
= nvmet_hosts_make_group
,
1285 static const struct config_item_type nvmet_hosts_type
= {
1286 .ct_group_ops
= &nvmet_hosts_group_ops
,
1287 .ct_owner
= THIS_MODULE
,
1290 static struct config_group nvmet_hosts_group
;
1292 static const struct config_item_type nvmet_root_type
= {
1293 .ct_owner
= THIS_MODULE
,
1296 static struct configfs_subsystem nvmet_configfs_subsystem
= {
1299 .ci_namebuf
= "nvmet",
1300 .ci_type
= &nvmet_root_type
,
1305 int __init
nvmet_init_configfs(void)
1309 config_group_init(&nvmet_configfs_subsystem
.su_group
);
1310 mutex_init(&nvmet_configfs_subsystem
.su_mutex
);
1312 config_group_init_type_name(&nvmet_subsystems_group
,
1313 "subsystems", &nvmet_subsystems_type
);
1314 configfs_add_default_group(&nvmet_subsystems_group
,
1315 &nvmet_configfs_subsystem
.su_group
);
1317 config_group_init_type_name(&nvmet_ports_group
,
1318 "ports", &nvmet_ports_type
);
1319 configfs_add_default_group(&nvmet_ports_group
,
1320 &nvmet_configfs_subsystem
.su_group
);
1322 config_group_init_type_name(&nvmet_hosts_group
,
1323 "hosts", &nvmet_hosts_type
);
1324 configfs_add_default_group(&nvmet_hosts_group
,
1325 &nvmet_configfs_subsystem
.su_group
);
1327 ret
= configfs_register_subsystem(&nvmet_configfs_subsystem
);
1329 pr_err("configfs_register_subsystem: %d\n", ret
);
1336 void __exit
nvmet_exit_configfs(void)
1338 configfs_unregister_subsystem(&nvmet_configfs_subsystem
);