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_param_inline_data_size_show(struct config_item
*item
,
224 struct nvmet_port
*port
= to_nvmet_port(item
);
226 return snprintf(page
, PAGE_SIZE
, "%d\n", port
->inline_data_size
);
229 static ssize_t
nvmet_param_inline_data_size_store(struct config_item
*item
,
230 const char *page
, size_t count
)
232 struct nvmet_port
*port
= to_nvmet_port(item
);
236 pr_err("Cannot modify inline_data_size while port enabled\n");
237 pr_err("Disable the port before modifying\n");
240 ret
= kstrtoint(page
, 0, &port
->inline_data_size
);
242 pr_err("Invalid value '%s' for inline_data_size\n", page
);
248 CONFIGFS_ATTR(nvmet_
, param_inline_data_size
);
250 static ssize_t
nvmet_addr_trtype_show(struct config_item
*item
,
253 struct nvmet_port
*port
= to_nvmet_port(item
);
256 for (i
= 0; i
< ARRAY_SIZE(nvmet_transport_names
); i
++) {
257 if (port
->disc_addr
.trtype
!= nvmet_transport_names
[i
].type
)
259 return sprintf(page
, "%s\n", nvmet_transport_names
[i
].name
);
262 return sprintf(page
, "\n");
265 static void nvmet_port_init_tsas_rdma(struct nvmet_port
*port
)
267 port
->disc_addr
.tsas
.rdma
.qptype
= NVMF_RDMA_QPTYPE_CONNECTED
;
268 port
->disc_addr
.tsas
.rdma
.prtype
= NVMF_RDMA_PRTYPE_NOT_SPECIFIED
;
269 port
->disc_addr
.tsas
.rdma
.cms
= NVMF_RDMA_CMS_RDMA_CM
;
272 static ssize_t
nvmet_addr_trtype_store(struct config_item
*item
,
273 const char *page
, size_t count
)
275 struct nvmet_port
*port
= to_nvmet_port(item
);
279 pr_err("Cannot modify address while enabled\n");
280 pr_err("Disable the address before modifying\n");
284 for (i
= 0; i
< ARRAY_SIZE(nvmet_transport_names
); i
++) {
285 if (sysfs_streq(page
, nvmet_transport_names
[i
].name
))
289 pr_err("Invalid value '%s' for trtype\n", page
);
292 memset(&port
->disc_addr
.tsas
, 0, NVMF_TSAS_SIZE
);
293 port
->disc_addr
.trtype
= nvmet_transport_names
[i
].type
;
294 if (port
->disc_addr
.trtype
== NVMF_TRTYPE_RDMA
)
295 nvmet_port_init_tsas_rdma(port
);
299 CONFIGFS_ATTR(nvmet_
, addr_trtype
);
302 * Namespace structures & file operation functions below
304 static ssize_t
nvmet_ns_device_path_show(struct config_item
*item
, char *page
)
306 return sprintf(page
, "%s\n", to_nvmet_ns(item
)->device_path
);
309 static ssize_t
nvmet_ns_device_path_store(struct config_item
*item
,
310 const char *page
, size_t count
)
312 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
313 struct nvmet_subsys
*subsys
= ns
->subsys
;
317 mutex_lock(&subsys
->lock
);
323 len
= strcspn(page
, "\n");
327 kfree(ns
->device_path
);
329 ns
->device_path
= kstrndup(page
, len
, GFP_KERNEL
);
330 if (!ns
->device_path
)
333 mutex_unlock(&subsys
->lock
);
337 mutex_unlock(&subsys
->lock
);
341 CONFIGFS_ATTR(nvmet_ns_
, device_path
);
343 static ssize_t
nvmet_ns_device_uuid_show(struct config_item
*item
, char *page
)
345 return sprintf(page
, "%pUb\n", &to_nvmet_ns(item
)->uuid
);
348 static ssize_t
nvmet_ns_device_uuid_store(struct config_item
*item
,
349 const char *page
, size_t count
)
351 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
352 struct nvmet_subsys
*subsys
= ns
->subsys
;
356 mutex_lock(&subsys
->lock
);
363 if (uuid_parse(page
, &ns
->uuid
))
367 mutex_unlock(&subsys
->lock
);
368 return ret
? ret
: count
;
371 CONFIGFS_ATTR(nvmet_ns_
, device_uuid
);
373 static ssize_t
nvmet_ns_device_nguid_show(struct config_item
*item
, char *page
)
375 return sprintf(page
, "%pUb\n", &to_nvmet_ns(item
)->nguid
);
378 static ssize_t
nvmet_ns_device_nguid_store(struct config_item
*item
,
379 const char *page
, size_t count
)
381 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
382 struct nvmet_subsys
*subsys
= ns
->subsys
;
384 const char *p
= page
;
388 mutex_lock(&subsys
->lock
);
394 for (i
= 0; i
< 16; i
++) {
395 if (p
+ 2 > page
+ count
) {
399 if (!isxdigit(p
[0]) || !isxdigit(p
[1])) {
404 nguid
[i
] = (hex_to_bin(p
[0]) << 4) | hex_to_bin(p
[1]);
407 if (*p
== '-' || *p
== ':')
411 memcpy(&ns
->nguid
, nguid
, sizeof(nguid
));
413 mutex_unlock(&subsys
->lock
);
414 return ret
? ret
: count
;
417 CONFIGFS_ATTR(nvmet_ns_
, device_nguid
);
419 static ssize_t
nvmet_ns_ana_grpid_show(struct config_item
*item
, char *page
)
421 return sprintf(page
, "%u\n", to_nvmet_ns(item
)->anagrpid
);
424 static ssize_t
nvmet_ns_ana_grpid_store(struct config_item
*item
,
425 const char *page
, size_t count
)
427 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
428 u32 oldgrpid
, newgrpid
;
431 ret
= kstrtou32(page
, 0, &newgrpid
);
435 if (newgrpid
< 1 || newgrpid
> NVMET_MAX_ANAGRPS
)
438 down_write(&nvmet_ana_sem
);
439 oldgrpid
= ns
->anagrpid
;
440 nvmet_ana_group_enabled
[newgrpid
]++;
441 ns
->anagrpid
= newgrpid
;
442 nvmet_ana_group_enabled
[oldgrpid
]--;
444 up_write(&nvmet_ana_sem
);
446 nvmet_send_ana_event(ns
->subsys
, NULL
);
450 CONFIGFS_ATTR(nvmet_ns_
, ana_grpid
);
452 static ssize_t
nvmet_ns_enable_show(struct config_item
*item
, char *page
)
454 return sprintf(page
, "%d\n", to_nvmet_ns(item
)->enabled
);
457 static ssize_t
nvmet_ns_enable_store(struct config_item
*item
,
458 const char *page
, size_t count
)
460 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
464 if (strtobool(page
, &enable
))
468 ret
= nvmet_ns_enable(ns
);
470 nvmet_ns_disable(ns
);
472 return ret
? ret
: count
;
475 CONFIGFS_ATTR(nvmet_ns_
, enable
);
477 static ssize_t
nvmet_ns_buffered_io_show(struct config_item
*item
, char *page
)
479 return sprintf(page
, "%d\n", to_nvmet_ns(item
)->buffered_io
);
482 static ssize_t
nvmet_ns_buffered_io_store(struct config_item
*item
,
483 const char *page
, size_t count
)
485 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
488 if (strtobool(page
, &val
))
491 mutex_lock(&ns
->subsys
->lock
);
493 pr_err("disable ns before setting buffered_io value.\n");
494 mutex_unlock(&ns
->subsys
->lock
);
498 ns
->buffered_io
= val
;
499 mutex_unlock(&ns
->subsys
->lock
);
503 CONFIGFS_ATTR(nvmet_ns_
, buffered_io
);
505 static struct configfs_attribute
*nvmet_ns_attrs
[] = {
506 &nvmet_ns_attr_device_path
,
507 &nvmet_ns_attr_device_nguid
,
508 &nvmet_ns_attr_device_uuid
,
509 &nvmet_ns_attr_ana_grpid
,
510 &nvmet_ns_attr_enable
,
511 &nvmet_ns_attr_buffered_io
,
515 static void nvmet_ns_release(struct config_item
*item
)
517 struct nvmet_ns
*ns
= to_nvmet_ns(item
);
522 static struct configfs_item_operations nvmet_ns_item_ops
= {
523 .release
= nvmet_ns_release
,
526 static const struct config_item_type nvmet_ns_type
= {
527 .ct_item_ops
= &nvmet_ns_item_ops
,
528 .ct_attrs
= nvmet_ns_attrs
,
529 .ct_owner
= THIS_MODULE
,
532 static struct config_group
*nvmet_ns_make(struct config_group
*group
,
535 struct nvmet_subsys
*subsys
= namespaces_to_subsys(&group
->cg_item
);
540 ret
= kstrtou32(name
, 0, &nsid
);
545 if (nsid
== 0 || nsid
== NVME_NSID_ALL
)
549 ns
= nvmet_ns_alloc(subsys
, nsid
);
552 config_group_init_type_name(&ns
->group
, name
, &nvmet_ns_type
);
554 pr_info("adding nsid %d to subsystem %s\n", nsid
, subsys
->subsysnqn
);
561 static struct configfs_group_operations nvmet_namespaces_group_ops
= {
562 .make_group
= nvmet_ns_make
,
565 static const struct config_item_type nvmet_namespaces_type
= {
566 .ct_group_ops
= &nvmet_namespaces_group_ops
,
567 .ct_owner
= THIS_MODULE
,
570 static int nvmet_port_subsys_allow_link(struct config_item
*parent
,
571 struct config_item
*target
)
573 struct nvmet_port
*port
= to_nvmet_port(parent
->ci_parent
);
574 struct nvmet_subsys
*subsys
;
575 struct nvmet_subsys_link
*link
, *p
;
578 if (target
->ci_type
!= &nvmet_subsys_type
) {
579 pr_err("can only link subsystems into the subsystems dir.!\n");
582 subsys
= to_subsys(target
);
583 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
586 link
->subsys
= subsys
;
588 down_write(&nvmet_config_sem
);
590 list_for_each_entry(p
, &port
->subsystems
, entry
) {
591 if (p
->subsys
== subsys
)
595 if (list_empty(&port
->subsystems
)) {
596 ret
= nvmet_enable_port(port
);
601 list_add_tail(&link
->entry
, &port
->subsystems
);
603 up_write(&nvmet_config_sem
);
607 up_write(&nvmet_config_sem
);
612 static void nvmet_port_subsys_drop_link(struct config_item
*parent
,
613 struct config_item
*target
)
615 struct nvmet_port
*port
= to_nvmet_port(parent
->ci_parent
);
616 struct nvmet_subsys
*subsys
= to_subsys(target
);
617 struct nvmet_subsys_link
*p
;
619 down_write(&nvmet_config_sem
);
620 list_for_each_entry(p
, &port
->subsystems
, entry
) {
621 if (p
->subsys
== subsys
)
624 up_write(&nvmet_config_sem
);
630 if (list_empty(&port
->subsystems
))
631 nvmet_disable_port(port
);
632 up_write(&nvmet_config_sem
);
636 static struct configfs_item_operations nvmet_port_subsys_item_ops
= {
637 .allow_link
= nvmet_port_subsys_allow_link
,
638 .drop_link
= nvmet_port_subsys_drop_link
,
641 static const struct config_item_type nvmet_port_subsys_type
= {
642 .ct_item_ops
= &nvmet_port_subsys_item_ops
,
643 .ct_owner
= THIS_MODULE
,
646 static int nvmet_allowed_hosts_allow_link(struct config_item
*parent
,
647 struct config_item
*target
)
649 struct nvmet_subsys
*subsys
= to_subsys(parent
->ci_parent
);
650 struct nvmet_host
*host
;
651 struct nvmet_host_link
*link
, *p
;
654 if (target
->ci_type
!= &nvmet_host_type
) {
655 pr_err("can only link hosts into the allowed_hosts directory!\n");
659 host
= to_host(target
);
660 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
665 down_write(&nvmet_config_sem
);
667 if (subsys
->allow_any_host
) {
668 pr_err("can't add hosts when allow_any_host is set!\n");
673 list_for_each_entry(p
, &subsys
->hosts
, entry
) {
674 if (!strcmp(nvmet_host_name(p
->host
), nvmet_host_name(host
)))
677 list_add_tail(&link
->entry
, &subsys
->hosts
);
679 up_write(&nvmet_config_sem
);
682 up_write(&nvmet_config_sem
);
687 static void nvmet_allowed_hosts_drop_link(struct config_item
*parent
,
688 struct config_item
*target
)
690 struct nvmet_subsys
*subsys
= to_subsys(parent
->ci_parent
);
691 struct nvmet_host
*host
= to_host(target
);
692 struct nvmet_host_link
*p
;
694 down_write(&nvmet_config_sem
);
695 list_for_each_entry(p
, &subsys
->hosts
, entry
) {
696 if (!strcmp(nvmet_host_name(p
->host
), nvmet_host_name(host
)))
699 up_write(&nvmet_config_sem
);
705 up_write(&nvmet_config_sem
);
709 static struct configfs_item_operations nvmet_allowed_hosts_item_ops
= {
710 .allow_link
= nvmet_allowed_hosts_allow_link
,
711 .drop_link
= nvmet_allowed_hosts_drop_link
,
714 static const struct config_item_type nvmet_allowed_hosts_type
= {
715 .ct_item_ops
= &nvmet_allowed_hosts_item_ops
,
716 .ct_owner
= THIS_MODULE
,
719 static ssize_t
nvmet_subsys_attr_allow_any_host_show(struct config_item
*item
,
722 return snprintf(page
, PAGE_SIZE
, "%d\n",
723 to_subsys(item
)->allow_any_host
);
726 static ssize_t
nvmet_subsys_attr_allow_any_host_store(struct config_item
*item
,
727 const char *page
, size_t count
)
729 struct nvmet_subsys
*subsys
= to_subsys(item
);
733 if (strtobool(page
, &allow_any_host
))
736 down_write(&nvmet_config_sem
);
737 if (allow_any_host
&& !list_empty(&subsys
->hosts
)) {
738 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
743 subsys
->allow_any_host
= allow_any_host
;
745 up_write(&nvmet_config_sem
);
746 return ret
? ret
: count
;
749 CONFIGFS_ATTR(nvmet_subsys_
, attr_allow_any_host
);
751 static ssize_t
nvmet_subsys_attr_version_show(struct config_item
*item
,
754 struct nvmet_subsys
*subsys
= to_subsys(item
);
756 if (NVME_TERTIARY(subsys
->ver
))
757 return snprintf(page
, PAGE_SIZE
, "%d.%d.%d\n",
758 (int)NVME_MAJOR(subsys
->ver
),
759 (int)NVME_MINOR(subsys
->ver
),
760 (int)NVME_TERTIARY(subsys
->ver
));
762 return snprintf(page
, PAGE_SIZE
, "%d.%d\n",
763 (int)NVME_MAJOR(subsys
->ver
),
764 (int)NVME_MINOR(subsys
->ver
));
767 static ssize_t
nvmet_subsys_attr_version_store(struct config_item
*item
,
768 const char *page
, size_t count
)
770 struct nvmet_subsys
*subsys
= to_subsys(item
);
771 int major
, minor
, tertiary
= 0;
775 ret
= sscanf(page
, "%d.%d.%d\n", &major
, &minor
, &tertiary
);
776 if (ret
!= 2 && ret
!= 3)
779 down_write(&nvmet_config_sem
);
780 subsys
->ver
= NVME_VS(major
, minor
, tertiary
);
781 up_write(&nvmet_config_sem
);
785 CONFIGFS_ATTR(nvmet_subsys_
, attr_version
);
787 static ssize_t
nvmet_subsys_attr_serial_show(struct config_item
*item
,
790 struct nvmet_subsys
*subsys
= to_subsys(item
);
792 return snprintf(page
, PAGE_SIZE
, "%llx\n", subsys
->serial
);
795 static ssize_t
nvmet_subsys_attr_serial_store(struct config_item
*item
,
796 const char *page
, size_t count
)
798 struct nvmet_subsys
*subsys
= to_subsys(item
);
800 down_write(&nvmet_config_sem
);
801 sscanf(page
, "%llx\n", &subsys
->serial
);
802 up_write(&nvmet_config_sem
);
806 CONFIGFS_ATTR(nvmet_subsys_
, attr_serial
);
808 static struct configfs_attribute
*nvmet_subsys_attrs
[] = {
809 &nvmet_subsys_attr_attr_allow_any_host
,
810 &nvmet_subsys_attr_attr_version
,
811 &nvmet_subsys_attr_attr_serial
,
816 * Subsystem structures & folder operation functions below
818 static void nvmet_subsys_release(struct config_item
*item
)
820 struct nvmet_subsys
*subsys
= to_subsys(item
);
822 nvmet_subsys_del_ctrls(subsys
);
823 nvmet_subsys_put(subsys
);
826 static struct configfs_item_operations nvmet_subsys_item_ops
= {
827 .release
= nvmet_subsys_release
,
830 static const struct config_item_type nvmet_subsys_type
= {
831 .ct_item_ops
= &nvmet_subsys_item_ops
,
832 .ct_attrs
= nvmet_subsys_attrs
,
833 .ct_owner
= THIS_MODULE
,
836 static struct config_group
*nvmet_subsys_make(struct config_group
*group
,
839 struct nvmet_subsys
*subsys
;
841 if (sysfs_streq(name
, NVME_DISC_SUBSYS_NAME
)) {
842 pr_err("can't create discovery subsystem through configfs\n");
843 return ERR_PTR(-EINVAL
);
846 subsys
= nvmet_subsys_alloc(name
, NVME_NQN_NVME
);
848 return ERR_PTR(-ENOMEM
);
850 config_group_init_type_name(&subsys
->group
, name
, &nvmet_subsys_type
);
852 config_group_init_type_name(&subsys
->namespaces_group
,
853 "namespaces", &nvmet_namespaces_type
);
854 configfs_add_default_group(&subsys
->namespaces_group
, &subsys
->group
);
856 config_group_init_type_name(&subsys
->allowed_hosts_group
,
857 "allowed_hosts", &nvmet_allowed_hosts_type
);
858 configfs_add_default_group(&subsys
->allowed_hosts_group
,
861 return &subsys
->group
;
864 static struct configfs_group_operations nvmet_subsystems_group_ops
= {
865 .make_group
= nvmet_subsys_make
,
868 static const struct config_item_type nvmet_subsystems_type
= {
869 .ct_group_ops
= &nvmet_subsystems_group_ops
,
870 .ct_owner
= THIS_MODULE
,
873 static ssize_t
nvmet_referral_enable_show(struct config_item
*item
,
876 return snprintf(page
, PAGE_SIZE
, "%d\n", to_nvmet_port(item
)->enabled
);
879 static ssize_t
nvmet_referral_enable_store(struct config_item
*item
,
880 const char *page
, size_t count
)
882 struct nvmet_port
*parent
= to_nvmet_port(item
->ci_parent
->ci_parent
);
883 struct nvmet_port
*port
= to_nvmet_port(item
);
886 if (strtobool(page
, &enable
))
890 nvmet_referral_enable(parent
, port
);
892 nvmet_referral_disable(port
);
896 pr_err("Invalid value '%s' for enable\n", page
);
900 CONFIGFS_ATTR(nvmet_referral_
, enable
);
903 * Discovery Service subsystem definitions
905 static struct configfs_attribute
*nvmet_referral_attrs
[] = {
906 &nvmet_attr_addr_adrfam
,
907 &nvmet_attr_addr_portid
,
908 &nvmet_attr_addr_treq
,
909 &nvmet_attr_addr_traddr
,
910 &nvmet_attr_addr_trsvcid
,
911 &nvmet_attr_addr_trtype
,
912 &nvmet_referral_attr_enable
,
916 static void nvmet_referral_release(struct config_item
*item
)
918 struct nvmet_port
*port
= to_nvmet_port(item
);
920 nvmet_referral_disable(port
);
924 static struct configfs_item_operations nvmet_referral_item_ops
= {
925 .release
= nvmet_referral_release
,
928 static const struct config_item_type nvmet_referral_type
= {
929 .ct_owner
= THIS_MODULE
,
930 .ct_attrs
= nvmet_referral_attrs
,
931 .ct_item_ops
= &nvmet_referral_item_ops
,
934 static struct config_group
*nvmet_referral_make(
935 struct config_group
*group
, const char *name
)
937 struct nvmet_port
*port
;
939 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
941 return ERR_PTR(-ENOMEM
);
943 INIT_LIST_HEAD(&port
->entry
);
944 config_group_init_type_name(&port
->group
, name
, &nvmet_referral_type
);
949 static struct configfs_group_operations nvmet_referral_group_ops
= {
950 .make_group
= nvmet_referral_make
,
953 static const struct config_item_type nvmet_referrals_type
= {
954 .ct_owner
= THIS_MODULE
,
955 .ct_group_ops
= &nvmet_referral_group_ops
,
959 enum nvme_ana_state state
;
961 } nvmet_ana_state_names
[] = {
962 { NVME_ANA_OPTIMIZED
, "optimized" },
963 { NVME_ANA_NONOPTIMIZED
, "non-optimized" },
964 { NVME_ANA_INACCESSIBLE
, "inaccessible" },
965 { NVME_ANA_PERSISTENT_LOSS
, "persistent-loss" },
966 { NVME_ANA_CHANGE
, "change" },
969 static ssize_t
nvmet_ana_group_ana_state_show(struct config_item
*item
,
972 struct nvmet_ana_group
*grp
= to_ana_group(item
);
973 enum nvme_ana_state state
= grp
->port
->ana_state
[grp
->grpid
];
976 for (i
= 0; i
< ARRAY_SIZE(nvmet_ana_state_names
); i
++) {
977 if (state
!= nvmet_ana_state_names
[i
].state
)
979 return sprintf(page
, "%s\n", nvmet_ana_state_names
[i
].name
);
982 return sprintf(page
, "\n");
985 static ssize_t
nvmet_ana_group_ana_state_store(struct config_item
*item
,
986 const char *page
, size_t count
)
988 struct nvmet_ana_group
*grp
= to_ana_group(item
);
991 for (i
= 0; i
< ARRAY_SIZE(nvmet_ana_state_names
); i
++) {
992 if (sysfs_streq(page
, nvmet_ana_state_names
[i
].name
))
996 pr_err("Invalid value '%s' for ana_state\n", page
);
1000 down_write(&nvmet_ana_sem
);
1001 grp
->port
->ana_state
[grp
->grpid
] = nvmet_ana_state_names
[i
].state
;
1003 up_write(&nvmet_ana_sem
);
1005 nvmet_port_send_ana_event(grp
->port
);
1009 CONFIGFS_ATTR(nvmet_ana_group_
, ana_state
);
1011 static struct configfs_attribute
*nvmet_ana_group_attrs
[] = {
1012 &nvmet_ana_group_attr_ana_state
,
1016 static void nvmet_ana_group_release(struct config_item
*item
)
1018 struct nvmet_ana_group
*grp
= to_ana_group(item
);
1020 if (grp
== &grp
->port
->ana_default_group
)
1023 down_write(&nvmet_ana_sem
);
1024 grp
->port
->ana_state
[grp
->grpid
] = NVME_ANA_INACCESSIBLE
;
1025 nvmet_ana_group_enabled
[grp
->grpid
]--;
1026 up_write(&nvmet_ana_sem
);
1028 nvmet_port_send_ana_event(grp
->port
);
1032 static struct configfs_item_operations nvmet_ana_group_item_ops
= {
1033 .release
= nvmet_ana_group_release
,
1036 static const struct config_item_type nvmet_ana_group_type
= {
1037 .ct_item_ops
= &nvmet_ana_group_item_ops
,
1038 .ct_attrs
= nvmet_ana_group_attrs
,
1039 .ct_owner
= THIS_MODULE
,
1042 static struct config_group
*nvmet_ana_groups_make_group(
1043 struct config_group
*group
, const char *name
)
1045 struct nvmet_port
*port
= ana_groups_to_port(&group
->cg_item
);
1046 struct nvmet_ana_group
*grp
;
1050 ret
= kstrtou32(name
, 0, &grpid
);
1055 if (grpid
<= 1 || grpid
> NVMET_MAX_ANAGRPS
)
1059 grp
= kzalloc(sizeof(*grp
), GFP_KERNEL
);
1065 down_write(&nvmet_ana_sem
);
1066 nvmet_ana_group_enabled
[grpid
]++;
1067 up_write(&nvmet_ana_sem
);
1069 nvmet_port_send_ana_event(grp
->port
);
1071 config_group_init_type_name(&grp
->group
, name
, &nvmet_ana_group_type
);
1074 return ERR_PTR(ret
);
1077 static struct configfs_group_operations nvmet_ana_groups_group_ops
= {
1078 .make_group
= nvmet_ana_groups_make_group
,
1081 static const struct config_item_type nvmet_ana_groups_type
= {
1082 .ct_group_ops
= &nvmet_ana_groups_group_ops
,
1083 .ct_owner
= THIS_MODULE
,
1087 * Ports definitions.
1089 static void nvmet_port_release(struct config_item
*item
)
1091 struct nvmet_port
*port
= to_nvmet_port(item
);
1093 kfree(port
->ana_state
);
1097 static struct configfs_attribute
*nvmet_port_attrs
[] = {
1098 &nvmet_attr_addr_adrfam
,
1099 &nvmet_attr_addr_treq
,
1100 &nvmet_attr_addr_traddr
,
1101 &nvmet_attr_addr_trsvcid
,
1102 &nvmet_attr_addr_trtype
,
1103 &nvmet_attr_param_inline_data_size
,
1107 static struct configfs_item_operations nvmet_port_item_ops
= {
1108 .release
= nvmet_port_release
,
1111 static const struct config_item_type nvmet_port_type
= {
1112 .ct_attrs
= nvmet_port_attrs
,
1113 .ct_item_ops
= &nvmet_port_item_ops
,
1114 .ct_owner
= THIS_MODULE
,
1117 static struct config_group
*nvmet_ports_make(struct config_group
*group
,
1120 struct nvmet_port
*port
;
1124 if (kstrtou16(name
, 0, &portid
))
1125 return ERR_PTR(-EINVAL
);
1127 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1129 return ERR_PTR(-ENOMEM
);
1131 port
->ana_state
= kcalloc(NVMET_MAX_ANAGRPS
+ 1,
1132 sizeof(*port
->ana_state
), GFP_KERNEL
);
1133 if (!port
->ana_state
) {
1135 return ERR_PTR(-ENOMEM
);
1138 for (i
= 1; i
<= NVMET_MAX_ANAGRPS
; i
++) {
1139 if (i
== NVMET_DEFAULT_ANA_GRPID
)
1140 port
->ana_state
[1] = NVME_ANA_OPTIMIZED
;
1142 port
->ana_state
[i
] = NVME_ANA_INACCESSIBLE
;
1145 INIT_LIST_HEAD(&port
->entry
);
1146 INIT_LIST_HEAD(&port
->subsystems
);
1147 INIT_LIST_HEAD(&port
->referrals
);
1148 port
->inline_data_size
= -1; /* < 0 == let the transport choose */
1150 port
->disc_addr
.portid
= cpu_to_le16(portid
);
1151 config_group_init_type_name(&port
->group
, name
, &nvmet_port_type
);
1153 config_group_init_type_name(&port
->subsys_group
,
1154 "subsystems", &nvmet_port_subsys_type
);
1155 configfs_add_default_group(&port
->subsys_group
, &port
->group
);
1157 config_group_init_type_name(&port
->referrals_group
,
1158 "referrals", &nvmet_referrals_type
);
1159 configfs_add_default_group(&port
->referrals_group
, &port
->group
);
1161 config_group_init_type_name(&port
->ana_groups_group
,
1162 "ana_groups", &nvmet_ana_groups_type
);
1163 configfs_add_default_group(&port
->ana_groups_group
, &port
->group
);
1165 port
->ana_default_group
.port
= port
;
1166 port
->ana_default_group
.grpid
= NVMET_DEFAULT_ANA_GRPID
;
1167 config_group_init_type_name(&port
->ana_default_group
.group
,
1168 __stringify(NVMET_DEFAULT_ANA_GRPID
),
1169 &nvmet_ana_group_type
);
1170 configfs_add_default_group(&port
->ana_default_group
.group
,
1171 &port
->ana_groups_group
);
1173 return &port
->group
;
1176 static struct configfs_group_operations nvmet_ports_group_ops
= {
1177 .make_group
= nvmet_ports_make
,
1180 static const struct config_item_type nvmet_ports_type
= {
1181 .ct_group_ops
= &nvmet_ports_group_ops
,
1182 .ct_owner
= THIS_MODULE
,
1185 static struct config_group nvmet_subsystems_group
;
1186 static struct config_group nvmet_ports_group
;
1188 static void nvmet_host_release(struct config_item
*item
)
1190 struct nvmet_host
*host
= to_host(item
);
1195 static struct configfs_item_operations nvmet_host_item_ops
= {
1196 .release
= nvmet_host_release
,
1199 static const struct config_item_type nvmet_host_type
= {
1200 .ct_item_ops
= &nvmet_host_item_ops
,
1201 .ct_owner
= THIS_MODULE
,
1204 static struct config_group
*nvmet_hosts_make_group(struct config_group
*group
,
1207 struct nvmet_host
*host
;
1209 host
= kzalloc(sizeof(*host
), GFP_KERNEL
);
1211 return ERR_PTR(-ENOMEM
);
1213 config_group_init_type_name(&host
->group
, name
, &nvmet_host_type
);
1215 return &host
->group
;
1218 static struct configfs_group_operations nvmet_hosts_group_ops
= {
1219 .make_group
= nvmet_hosts_make_group
,
1222 static const struct config_item_type nvmet_hosts_type
= {
1223 .ct_group_ops
= &nvmet_hosts_group_ops
,
1224 .ct_owner
= THIS_MODULE
,
1227 static struct config_group nvmet_hosts_group
;
1229 static const struct config_item_type nvmet_root_type
= {
1230 .ct_owner
= THIS_MODULE
,
1233 static struct configfs_subsystem nvmet_configfs_subsystem
= {
1236 .ci_namebuf
= "nvmet",
1237 .ci_type
= &nvmet_root_type
,
1242 int __init
nvmet_init_configfs(void)
1246 config_group_init(&nvmet_configfs_subsystem
.su_group
);
1247 mutex_init(&nvmet_configfs_subsystem
.su_mutex
);
1249 config_group_init_type_name(&nvmet_subsystems_group
,
1250 "subsystems", &nvmet_subsystems_type
);
1251 configfs_add_default_group(&nvmet_subsystems_group
,
1252 &nvmet_configfs_subsystem
.su_group
);
1254 config_group_init_type_name(&nvmet_ports_group
,
1255 "ports", &nvmet_ports_type
);
1256 configfs_add_default_group(&nvmet_ports_group
,
1257 &nvmet_configfs_subsystem
.su_group
);
1259 config_group_init_type_name(&nvmet_hosts_group
,
1260 "hosts", &nvmet_hosts_type
);
1261 configfs_add_default_group(&nvmet_hosts_group
,
1262 &nvmet_configfs_subsystem
.su_group
);
1264 ret
= configfs_register_subsystem(&nvmet_configfs_subsystem
);
1266 pr_err("configfs_register_subsystem: %d\n", ret
);
1273 void __exit
nvmet_exit_configfs(void)
1275 configfs_unregister_subsystem(&nvmet_configfs_subsystem
);