treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / nvme / target / configfs.c
blob98613a45bd3b408e343b8e89f9605475f1cb47b9
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Configfs interface for the NVMe target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 */
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>
15 #include "nvmet.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 {
24 u8 type;
25 const char *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,
38 char *page)
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");
49 default:
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);
59 if (port->enabled) {
60 pr_err("Cannot modify address while enabled\n");
61 pr_err("Disable the address before modifying\n");
62 return -EACCES;
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;
73 } else {
74 pr_err("Invalid value '%s' for adrfam\n", page);
75 return -EINVAL;
78 return count;
81 CONFIGFS_ATTR(nvmet_, addr_adrfam);
83 static ssize_t nvmet_addr_portid_show(struct config_item *item,
84 char *page)
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);
96 u16 portid = 0;
98 if (kstrtou16(page, 0, &portid)) {
99 pr_err("Invalid value '%s' for portid\n", page);
100 return -EINVAL;
103 if (port->enabled) {
104 pr_err("Cannot modify address while enabled\n");
105 pr_err("Disable the address before modifying\n");
106 return -EACCES;
108 port->disc_addr.portid = cpu_to_le16(portid);
109 return count;
112 CONFIGFS_ATTR(nvmet_, addr_portid);
114 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
115 char *page)
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);
130 return -EINVAL;
133 if (port->enabled) {
134 pr_err("Cannot modify address while enabled\n");
135 pr_err("Disable the address before modifying\n");
136 return -EACCES;
139 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
140 return -EINVAL;
141 return count;
144 CONFIGFS_ATTR(nvmet_, addr_traddr);
146 static ssize_t nvmet_addr_treq_show(struct config_item *item,
147 char *page)
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");
157 default:
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;
168 if (port->enabled) {
169 pr_err("Cannot modify address while enabled\n");
170 pr_err("Disable the address before modifying\n");
171 return -EACCES;
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;
180 } else {
181 pr_err("Invalid value '%s' for treq\n", page);
182 return -EINVAL;
184 port->disc_addr.treq = treq;
186 return count;
189 CONFIGFS_ATTR(nvmet_, addr_treq);
191 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
192 char *page)
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);
207 return -EINVAL;
209 if (port->enabled) {
210 pr_err("Cannot modify address while enabled\n");
211 pr_err("Disable the address before modifying\n");
212 return -EACCES;
215 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
216 return -EINVAL;
217 return count;
220 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
222 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
223 char *page)
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);
234 int ret;
236 if (port->enabled) {
237 pr_err("Cannot modify inline_data_size while port enabled\n");
238 pr_err("Disable the port before modifying\n");
239 return -EACCES;
241 ret = kstrtoint(page, 0, &port->inline_data_size);
242 if (ret) {
243 pr_err("Invalid value '%s' for inline_data_size\n", page);
244 return -EINVAL;
246 return count;
249 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
251 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
252 char *page)
254 struct nvmet_port *port = to_nvmet_port(item);
255 int i;
257 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
258 if (port->disc_addr.trtype != nvmet_transport_names[i].type)
259 continue;
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);
277 int i;
279 if (port->enabled) {
280 pr_err("Cannot modify address while enabled\n");
281 pr_err("Disable the address before modifying\n");
282 return -EACCES;
285 for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
286 if (sysfs_streq(page, nvmet_transport_names[i].name))
287 goto found;
290 pr_err("Invalid value '%s' for trtype\n", page);
291 return -EINVAL;
292 found:
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);
297 return count;
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;
315 size_t len;
316 int ret;
318 mutex_lock(&subsys->lock);
319 ret = -EBUSY;
320 if (ns->enabled)
321 goto out_unlock;
323 ret = -EINVAL;
324 len = strcspn(page, "\n");
325 if (!len)
326 goto out_unlock;
328 kfree(ns->device_path);
329 ret = -ENOMEM;
330 ns->device_path = kstrndup(page, len, GFP_KERNEL);
331 if (!ns->device_path)
332 goto out_unlock;
334 mutex_unlock(&subsys->lock);
335 return count;
337 out_unlock:
338 mutex_unlock(&subsys->lock);
339 return ret;
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;
357 bool use_p2pmem;
358 int ret = count;
359 int error;
361 mutex_lock(&ns->subsys->lock);
362 if (ns->enabled) {
363 ret = -EBUSY;
364 goto out_unlock;
367 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
368 if (error) {
369 ret = error;
370 goto out_unlock;
373 ns->use_p2pmem = use_p2pmem;
374 pci_dev_put(ns->p2p_dev);
375 ns->p2p_dev = p2p_dev;
377 out_unlock:
378 mutex_unlock(&ns->subsys->lock);
380 return ret;
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;
396 int ret = 0;
399 mutex_lock(&subsys->lock);
400 if (ns->enabled) {
401 ret = -EBUSY;
402 goto out_unlock;
406 if (uuid_parse(page, &ns->uuid))
407 ret = -EINVAL;
409 out_unlock:
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;
426 u8 nguid[16];
427 const char *p = page;
428 int i;
429 int ret = 0;
431 mutex_lock(&subsys->lock);
432 if (ns->enabled) {
433 ret = -EBUSY;
434 goto out_unlock;
437 for (i = 0; i < 16; i++) {
438 if (p + 2 > page + count) {
439 ret = -EINVAL;
440 goto out_unlock;
442 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
443 ret = -EINVAL;
444 goto out_unlock;
447 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
448 p += 2;
450 if (*p == '-' || *p == ':')
451 p++;
454 memcpy(&ns->nguid, nguid, sizeof(nguid));
455 out_unlock:
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;
472 int ret;
474 ret = kstrtou32(page, 0, &newgrpid);
475 if (ret)
476 return ret;
478 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
479 return -EINVAL;
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]--;
486 nvmet_ana_chgcnt++;
487 up_write(&nvmet_ana_sem);
489 nvmet_send_ana_event(ns->subsys, NULL);
490 return count;
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);
504 bool enable;
505 int ret = 0;
507 if (strtobool(page, &enable))
508 return -EINVAL;
510 if (enable)
511 ret = nvmet_ns_enable(ns);
512 else
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);
529 bool val;
531 if (strtobool(page, &val))
532 return -EINVAL;
534 mutex_lock(&ns->subsys->lock);
535 if (ns->enabled) {
536 pr_err("disable ns before setting buffered_io value.\n");
537 mutex_unlock(&ns->subsys->lock);
538 return -EINVAL;
541 ns->buffered_io = val;
542 mutex_unlock(&ns->subsys->lock);
543 return count;
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,
557 #endif
558 NULL,
561 static void nvmet_ns_release(struct config_item *item)
563 struct nvmet_ns *ns = to_nvmet_ns(item);
565 nvmet_ns_free(ns);
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,
579 const char *name)
581 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
582 struct nvmet_ns *ns;
583 int ret;
584 u32 nsid;
586 ret = kstrtou32(name, 0, &nsid);
587 if (ret)
588 goto out;
590 ret = -EINVAL;
591 if (nsid == 0 || nsid == NVME_NSID_ALL) {
592 pr_err("invalid nsid %#x", nsid);
593 goto out;
596 ret = -ENOMEM;
597 ns = nvmet_ns_alloc(subsys, nsid);
598 if (!ns)
599 goto out;
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);
604 return &ns->group;
605 out:
606 return ERR_PTR(ret);
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;
624 int ret;
626 if (target->ci_type != &nvmet_subsys_type) {
627 pr_err("can only link subsystems into the subsystems dir.!\n");
628 return -EINVAL;
630 subsys = to_subsys(target);
631 link = kmalloc(sizeof(*link), GFP_KERNEL);
632 if (!link)
633 return -ENOMEM;
634 link->subsys = subsys;
636 down_write(&nvmet_config_sem);
637 ret = -EEXIST;
638 list_for_each_entry(p, &port->subsystems, entry) {
639 if (p->subsys == subsys)
640 goto out_free_link;
643 if (list_empty(&port->subsystems)) {
644 ret = nvmet_enable_port(port);
645 if (ret)
646 goto out_free_link;
649 list_add_tail(&link->entry, &port->subsystems);
650 nvmet_port_disc_changed(port, subsys);
652 up_write(&nvmet_config_sem);
653 return 0;
655 out_free_link:
656 up_write(&nvmet_config_sem);
657 kfree(link);
658 return ret;
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)
671 goto found;
673 up_write(&nvmet_config_sem);
674 return;
676 found:
677 list_del(&p->entry);
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);
684 kfree(p);
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;
703 int ret;
705 if (target->ci_type != &nvmet_host_type) {
706 pr_err("can only link hosts into the allowed_hosts directory!\n");
707 return -EINVAL;
710 host = to_host(target);
711 link = kmalloc(sizeof(*link), GFP_KERNEL);
712 if (!link)
713 return -ENOMEM;
714 link->host = host;
716 down_write(&nvmet_config_sem);
717 ret = -EINVAL;
718 if (subsys->allow_any_host) {
719 pr_err("can't add hosts when allow_any_host is set!\n");
720 goto out_free_link;
723 ret = -EEXIST;
724 list_for_each_entry(p, &subsys->hosts, entry) {
725 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
726 goto out_free_link;
728 list_add_tail(&link->entry, &subsys->hosts);
729 nvmet_subsys_disc_changed(subsys, host);
731 up_write(&nvmet_config_sem);
732 return 0;
733 out_free_link:
734 up_write(&nvmet_config_sem);
735 kfree(link);
736 return ret;
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)))
749 goto found;
751 up_write(&nvmet_config_sem);
752 return;
754 found:
755 list_del(&p->entry);
756 nvmet_subsys_disc_changed(subsys, host);
758 up_write(&nvmet_config_sem);
759 kfree(p);
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,
773 char *page)
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);
783 bool allow_any_host;
784 int ret = 0;
786 if (strtobool(page, &allow_any_host))
787 return -EINVAL;
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");
792 ret = -EINVAL;
793 goto out_unlock;
796 if (subsys->allow_any_host != allow_any_host) {
797 subsys->allow_any_host = allow_any_host;
798 nvmet_subsys_disc_changed(subsys, NULL);
801 out_unlock:
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,
809 char *page)
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));
818 else
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;
829 int ret;
832 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
833 if (ret != 2 && ret != 3)
834 return -EINVAL;
836 down_write(&nvmet_config_sem);
837 subsys->ver = NVME_VS(major, minor, tertiary);
838 up_write(&nvmet_config_sem);
840 return count;
842 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
844 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
845 char *page)
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);
861 return count;
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,
869 NULL,
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,
894 const char *name)
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);
904 if (IS_ERR(subsys))
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,
916 &subsys->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,
931 char *page)
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);
941 bool enable;
943 if (strtobool(page, &enable))
944 goto inval;
946 if (enable)
947 nvmet_referral_enable(parent, port);
948 else
949 nvmet_referral_disable(parent, port);
951 return count;
952 inval:
953 pr_err("Invalid value '%s' for enable\n", page);
954 return -EINVAL;
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,
970 NULL,
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);
979 kfree(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);
998 if (!port)
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,
1016 static struct {
1017 enum nvme_ana_state state;
1018 const char *name;
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,
1028 char *page)
1030 struct nvmet_ana_group *grp = to_ana_group(item);
1031 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1032 int i;
1034 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1035 if (state != nvmet_ana_state_names[i].state)
1036 continue;
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);
1047 int i;
1049 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1050 if (sysfs_streq(page, nvmet_ana_state_names[i].name))
1051 goto found;
1054 pr_err("Invalid value '%s' for ana_state\n", page);
1055 return -EINVAL;
1057 found:
1058 down_write(&nvmet_ana_sem);
1059 grp->port->ana_state[grp->grpid] = nvmet_ana_state_names[i].state;
1060 nvmet_ana_chgcnt++;
1061 up_write(&nvmet_ana_sem);
1063 nvmet_port_send_ana_event(grp->port);
1064 return count;
1067 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1069 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1070 &nvmet_ana_group_attr_ana_state,
1071 NULL,
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)
1079 return;
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);
1087 kfree(grp);
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;
1105 u32 grpid;
1106 int ret;
1108 ret = kstrtou32(name, 0, &grpid);
1109 if (ret)
1110 goto out;
1112 ret = -EINVAL;
1113 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1114 goto out;
1116 ret = -ENOMEM;
1117 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1118 if (!grp)
1119 goto out;
1120 grp->port = port;
1121 grp->grpid = grpid;
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);
1130 return &grp->group;
1131 out:
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);
1154 kfree(port);
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,
1164 NULL,
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,
1178 const char *name)
1180 struct nvmet_port *port;
1181 u16 portid;
1182 u32 i;
1184 if (kstrtou16(name, 0, &portid))
1185 return ERR_PTR(-EINVAL);
1187 port = kzalloc(sizeof(*port), GFP_KERNEL);
1188 if (!port)
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) {
1194 kfree(port);
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;
1201 else
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);
1255 kfree(host);
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,
1268 const char *name)
1270 struct nvmet_host *host;
1272 host = kzalloc(sizeof(*host), GFP_KERNEL);
1273 if (!host)
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 = {
1297 .su_group = {
1298 .cg_item = {
1299 .ci_namebuf = "nvmet",
1300 .ci_type = &nvmet_root_type,
1305 int __init nvmet_init_configfs(void)
1307 int ret;
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);
1328 if (ret) {
1329 pr_err("configfs_register_subsystem: %d\n", ret);
1330 return ret;
1333 return 0;
1336 void __exit nvmet_exit_configfs(void)
1338 configfs_unregister_subsystem(&nvmet_configfs_subsystem);