xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision ba936421)
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>
14 
15 #include "nvmet.h"
16 
17 static const struct config_item_type nvmet_host_type;
18 static const struct config_item_type nvmet_subsys_type;
19 
20 static LIST_HEAD(nvmet_ports_list);
21 struct list_head *nvmet_ports = &nvmet_ports_list;
22 
23 struct nvmet_type_name_map {
24 	u8		type;
25 	const char	*name;
26 };
27 
28 static struct nvmet_type_name_map nvmet_transport[] = {
29 	{ NVMF_TRTYPE_RDMA,	"rdma" },
30 	{ NVMF_TRTYPE_FC,	"fc" },
31 	{ NVMF_TRTYPE_TCP,	"tcp" },
32 	{ NVMF_TRTYPE_LOOP,	"loop" },
33 };
34 
35 static const struct nvmet_type_name_map nvmet_addr_family[] = {
36 	{ NVMF_ADDR_FAMILY_PCI,		"pcie" },
37 	{ NVMF_ADDR_FAMILY_IP4,		"ipv4" },
38 	{ NVMF_ADDR_FAMILY_IP6,		"ipv6" },
39 	{ NVMF_ADDR_FAMILY_IB,		"ib" },
40 	{ NVMF_ADDR_FAMILY_FC,		"fc" },
41 	{ NVMF_ADDR_FAMILY_LOOP,	"loop" },
42 };
43 
44 static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
45 {
46 	if (p->enabled)
47 		pr_err("Disable port '%u' before changing attribute in %s\n",
48 		       le16_to_cpu(p->disc_addr.portid), caller);
49 	return p->enabled;
50 }
51 
52 /*
53  * nvmet_port Generic ConfigFS definitions.
54  * Used in any place in the ConfigFS tree that refers to an address.
55  */
56 static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
57 {
58 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
59 	int i;
60 
61 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
62 		if (nvmet_addr_family[i].type == adrfam)
63 			return snprintf(page, PAGE_SIZE, "%s\n",
64 					nvmet_addr_family[i].name);
65 	}
66 
67 	return snprintf(page, PAGE_SIZE, "\n");
68 }
69 
70 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
71 		const char *page, size_t count)
72 {
73 	struct nvmet_port *port = to_nvmet_port(item);
74 	int i;
75 
76 	if (nvmet_is_port_enabled(port, __func__))
77 		return -EACCES;
78 
79 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
80 		if (sysfs_streq(page, nvmet_addr_family[i].name))
81 			goto found;
82 	}
83 
84 	pr_err("Invalid value '%s' for adrfam\n", page);
85 	return -EINVAL;
86 
87 found:
88 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
89 	return count;
90 }
91 
92 CONFIGFS_ATTR(nvmet_, addr_adrfam);
93 
94 static ssize_t nvmet_addr_portid_show(struct config_item *item,
95 		char *page)
96 {
97 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
98 
99 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
100 }
101 
102 static ssize_t nvmet_addr_portid_store(struct config_item *item,
103 		const char *page, size_t count)
104 {
105 	struct nvmet_port *port = to_nvmet_port(item);
106 	u16 portid = 0;
107 
108 	if (kstrtou16(page, 0, &portid)) {
109 		pr_err("Invalid value '%s' for portid\n", page);
110 		return -EINVAL;
111 	}
112 
113 	if (nvmet_is_port_enabled(port, __func__))
114 		return -EACCES;
115 
116 	port->disc_addr.portid = cpu_to_le16(portid);
117 	return count;
118 }
119 
120 CONFIGFS_ATTR(nvmet_, addr_portid);
121 
122 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
123 		char *page)
124 {
125 	struct nvmet_port *port = to_nvmet_port(item);
126 
127 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
128 }
129 
130 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
131 		const char *page, size_t count)
132 {
133 	struct nvmet_port *port = to_nvmet_port(item);
134 
135 	if (count > NVMF_TRADDR_SIZE) {
136 		pr_err("Invalid value '%s' for traddr\n", page);
137 		return -EINVAL;
138 	}
139 
140 	if (nvmet_is_port_enabled(port, __func__))
141 		return -EACCES;
142 
143 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
144 		return -EINVAL;
145 	return count;
146 }
147 
148 CONFIGFS_ATTR(nvmet_, addr_traddr);
149 
150 static const struct nvmet_type_name_map nvmet_addr_treq[] = {
151 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
152 	{ NVMF_TREQ_REQUIRED,		"required" },
153 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
154 };
155 
156 static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
157 {
158 	u8 treq = to_nvmet_port(item)->disc_addr.treq &
159 		NVME_TREQ_SECURE_CHANNEL_MASK;
160 	int i;
161 
162 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
163 		if (treq == nvmet_addr_treq[i].type)
164 			return snprintf(page, PAGE_SIZE, "%s\n",
165 					nvmet_addr_treq[i].name);
166 	}
167 
168 	return snprintf(page, PAGE_SIZE, "\n");
169 }
170 
171 static ssize_t nvmet_addr_treq_store(struct config_item *item,
172 		const char *page, size_t count)
173 {
174 	struct nvmet_port *port = to_nvmet_port(item);
175 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
176 	int i;
177 
178 	if (nvmet_is_port_enabled(port, __func__))
179 		return -EACCES;
180 
181 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
182 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
183 			goto found;
184 	}
185 
186 	pr_err("Invalid value '%s' for treq\n", page);
187 	return -EINVAL;
188 
189 found:
190 	treq |= nvmet_addr_treq[i].type;
191 	port->disc_addr.treq = treq;
192 	return count;
193 }
194 
195 CONFIGFS_ATTR(nvmet_, addr_treq);
196 
197 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
198 		char *page)
199 {
200 	struct nvmet_port *port = to_nvmet_port(item);
201 
202 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
203 }
204 
205 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
206 		const char *page, size_t count)
207 {
208 	struct nvmet_port *port = to_nvmet_port(item);
209 
210 	if (count > NVMF_TRSVCID_SIZE) {
211 		pr_err("Invalid value '%s' for trsvcid\n", page);
212 		return -EINVAL;
213 	}
214 	if (nvmet_is_port_enabled(port, __func__))
215 		return -EACCES;
216 
217 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
218 		return -EINVAL;
219 	return count;
220 }
221 
222 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
223 
224 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
225 		char *page)
226 {
227 	struct nvmet_port *port = to_nvmet_port(item);
228 
229 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
230 }
231 
232 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
233 		const char *page, size_t count)
234 {
235 	struct nvmet_port *port = to_nvmet_port(item);
236 	int ret;
237 
238 	if (nvmet_is_port_enabled(port, __func__))
239 		return -EACCES;
240 	ret = kstrtoint(page, 0, &port->inline_data_size);
241 	if (ret) {
242 		pr_err("Invalid value '%s' for inline_data_size\n", page);
243 		return -EINVAL;
244 	}
245 	return count;
246 }
247 
248 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
249 
250 #ifdef CONFIG_BLK_DEV_INTEGRITY
251 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
252 		char *page)
253 {
254 	struct nvmet_port *port = to_nvmet_port(item);
255 
256 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
257 }
258 
259 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
260 		const char *page, size_t count)
261 {
262 	struct nvmet_port *port = to_nvmet_port(item);
263 	bool val;
264 
265 	if (strtobool(page, &val))
266 		return -EINVAL;
267 
268 	if (nvmet_is_port_enabled(port, __func__))
269 		return -EACCES;
270 
271 	port->pi_enable = val;
272 	return count;
273 }
274 
275 CONFIGFS_ATTR(nvmet_, param_pi_enable);
276 #endif
277 
278 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
279 		char *page)
280 {
281 	struct nvmet_port *port = to_nvmet_port(item);
282 	int i;
283 
284 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
285 		if (port->disc_addr.trtype == nvmet_transport[i].type)
286 			return snprintf(page, PAGE_SIZE,
287 					"%s\n", nvmet_transport[i].name);
288 	}
289 
290 	return sprintf(page, "\n");
291 }
292 
293 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
294 {
295 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
296 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
297 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
298 }
299 
300 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
301 		const char *page, size_t count)
302 {
303 	struct nvmet_port *port = to_nvmet_port(item);
304 	int i;
305 
306 	if (nvmet_is_port_enabled(port, __func__))
307 		return -EACCES;
308 
309 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
310 		if (sysfs_streq(page, nvmet_transport[i].name))
311 			goto found;
312 	}
313 
314 	pr_err("Invalid value '%s' for trtype\n", page);
315 	return -EINVAL;
316 
317 found:
318 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
319 	port->disc_addr.trtype = nvmet_transport[i].type;
320 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
321 		nvmet_port_init_tsas_rdma(port);
322 	return count;
323 }
324 
325 CONFIGFS_ATTR(nvmet_, addr_trtype);
326 
327 /*
328  * Namespace structures & file operation functions below
329  */
330 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
331 {
332 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
333 }
334 
335 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
336 		const char *page, size_t count)
337 {
338 	struct nvmet_ns *ns = to_nvmet_ns(item);
339 	struct nvmet_subsys *subsys = ns->subsys;
340 	size_t len;
341 	int ret;
342 
343 	mutex_lock(&subsys->lock);
344 	ret = -EBUSY;
345 	if (ns->enabled)
346 		goto out_unlock;
347 
348 	ret = -EINVAL;
349 	len = strcspn(page, "\n");
350 	if (!len)
351 		goto out_unlock;
352 
353 	kfree(ns->device_path);
354 	ret = -ENOMEM;
355 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
356 	if (!ns->device_path)
357 		goto out_unlock;
358 
359 	mutex_unlock(&subsys->lock);
360 	return count;
361 
362 out_unlock:
363 	mutex_unlock(&subsys->lock);
364 	return ret;
365 }
366 
367 CONFIGFS_ATTR(nvmet_ns_, device_path);
368 
369 #ifdef CONFIG_PCI_P2PDMA
370 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
371 {
372 	struct nvmet_ns *ns = to_nvmet_ns(item);
373 
374 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
375 }
376 
377 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
378 		const char *page, size_t count)
379 {
380 	struct nvmet_ns *ns = to_nvmet_ns(item);
381 	struct pci_dev *p2p_dev = NULL;
382 	bool use_p2pmem;
383 	int ret = count;
384 	int error;
385 
386 	mutex_lock(&ns->subsys->lock);
387 	if (ns->enabled) {
388 		ret = -EBUSY;
389 		goto out_unlock;
390 	}
391 
392 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
393 	if (error) {
394 		ret = error;
395 		goto out_unlock;
396 	}
397 
398 	ns->use_p2pmem = use_p2pmem;
399 	pci_dev_put(ns->p2p_dev);
400 	ns->p2p_dev = p2p_dev;
401 
402 out_unlock:
403 	mutex_unlock(&ns->subsys->lock);
404 
405 	return ret;
406 }
407 
408 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
409 #endif /* CONFIG_PCI_P2PDMA */
410 
411 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
412 {
413 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
414 }
415 
416 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
417 					  const char *page, size_t count)
418 {
419 	struct nvmet_ns *ns = to_nvmet_ns(item);
420 	struct nvmet_subsys *subsys = ns->subsys;
421 	int ret = 0;
422 
423 	mutex_lock(&subsys->lock);
424 	if (ns->enabled) {
425 		ret = -EBUSY;
426 		goto out_unlock;
427 	}
428 
429 	if (uuid_parse(page, &ns->uuid))
430 		ret = -EINVAL;
431 
432 out_unlock:
433 	mutex_unlock(&subsys->lock);
434 	return ret ? ret : count;
435 }
436 
437 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
438 
439 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
440 {
441 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
442 }
443 
444 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
445 		const char *page, size_t count)
446 {
447 	struct nvmet_ns *ns = to_nvmet_ns(item);
448 	struct nvmet_subsys *subsys = ns->subsys;
449 	u8 nguid[16];
450 	const char *p = page;
451 	int i;
452 	int ret = 0;
453 
454 	mutex_lock(&subsys->lock);
455 	if (ns->enabled) {
456 		ret = -EBUSY;
457 		goto out_unlock;
458 	}
459 
460 	for (i = 0; i < 16; i++) {
461 		if (p + 2 > page + count) {
462 			ret = -EINVAL;
463 			goto out_unlock;
464 		}
465 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
466 			ret = -EINVAL;
467 			goto out_unlock;
468 		}
469 
470 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
471 		p += 2;
472 
473 		if (*p == '-' || *p == ':')
474 			p++;
475 	}
476 
477 	memcpy(&ns->nguid, nguid, sizeof(nguid));
478 out_unlock:
479 	mutex_unlock(&subsys->lock);
480 	return ret ? ret : count;
481 }
482 
483 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
484 
485 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
486 {
487 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
488 }
489 
490 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
491 		const char *page, size_t count)
492 {
493 	struct nvmet_ns *ns = to_nvmet_ns(item);
494 	u32 oldgrpid, newgrpid;
495 	int ret;
496 
497 	ret = kstrtou32(page, 0, &newgrpid);
498 	if (ret)
499 		return ret;
500 
501 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
502 		return -EINVAL;
503 
504 	down_write(&nvmet_ana_sem);
505 	oldgrpid = ns->anagrpid;
506 	nvmet_ana_group_enabled[newgrpid]++;
507 	ns->anagrpid = newgrpid;
508 	nvmet_ana_group_enabled[oldgrpid]--;
509 	nvmet_ana_chgcnt++;
510 	up_write(&nvmet_ana_sem);
511 
512 	nvmet_send_ana_event(ns->subsys, NULL);
513 	return count;
514 }
515 
516 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
517 
518 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
519 {
520 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
521 }
522 
523 static ssize_t nvmet_ns_enable_store(struct config_item *item,
524 		const char *page, size_t count)
525 {
526 	struct nvmet_ns *ns = to_nvmet_ns(item);
527 	bool enable;
528 	int ret = 0;
529 
530 	if (strtobool(page, &enable))
531 		return -EINVAL;
532 
533 	if (enable)
534 		ret = nvmet_ns_enable(ns);
535 	else
536 		nvmet_ns_disable(ns);
537 
538 	return ret ? ret : count;
539 }
540 
541 CONFIGFS_ATTR(nvmet_ns_, enable);
542 
543 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
544 {
545 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
546 }
547 
548 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
549 		const char *page, size_t count)
550 {
551 	struct nvmet_ns *ns = to_nvmet_ns(item);
552 	bool val;
553 
554 	if (strtobool(page, &val))
555 		return -EINVAL;
556 
557 	mutex_lock(&ns->subsys->lock);
558 	if (ns->enabled) {
559 		pr_err("disable ns before setting buffered_io value.\n");
560 		mutex_unlock(&ns->subsys->lock);
561 		return -EINVAL;
562 	}
563 
564 	ns->buffered_io = val;
565 	mutex_unlock(&ns->subsys->lock);
566 	return count;
567 }
568 
569 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
570 
571 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
572 		const char *page, size_t count)
573 {
574 	struct nvmet_ns *ns = to_nvmet_ns(item);
575 	bool val;
576 
577 	if (strtobool(page, &val))
578 		return -EINVAL;
579 
580 	if (!val)
581 		return -EINVAL;
582 
583 	mutex_lock(&ns->subsys->lock);
584 	if (!ns->enabled) {
585 		pr_err("enable ns before revalidate.\n");
586 		mutex_unlock(&ns->subsys->lock);
587 		return -EINVAL;
588 	}
589 	if (nvmet_ns_revalidate(ns))
590 		nvmet_ns_changed(ns->subsys, ns->nsid);
591 	mutex_unlock(&ns->subsys->lock);
592 	return count;
593 }
594 
595 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
596 
597 static struct configfs_attribute *nvmet_ns_attrs[] = {
598 	&nvmet_ns_attr_device_path,
599 	&nvmet_ns_attr_device_nguid,
600 	&nvmet_ns_attr_device_uuid,
601 	&nvmet_ns_attr_ana_grpid,
602 	&nvmet_ns_attr_enable,
603 	&nvmet_ns_attr_buffered_io,
604 	&nvmet_ns_attr_revalidate_size,
605 #ifdef CONFIG_PCI_P2PDMA
606 	&nvmet_ns_attr_p2pmem,
607 #endif
608 	NULL,
609 };
610 
611 static void nvmet_ns_release(struct config_item *item)
612 {
613 	struct nvmet_ns *ns = to_nvmet_ns(item);
614 
615 	nvmet_ns_free(ns);
616 }
617 
618 static struct configfs_item_operations nvmet_ns_item_ops = {
619 	.release		= nvmet_ns_release,
620 };
621 
622 static const struct config_item_type nvmet_ns_type = {
623 	.ct_item_ops		= &nvmet_ns_item_ops,
624 	.ct_attrs		= nvmet_ns_attrs,
625 	.ct_owner		= THIS_MODULE,
626 };
627 
628 static struct config_group *nvmet_ns_make(struct config_group *group,
629 		const char *name)
630 {
631 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
632 	struct nvmet_ns *ns;
633 	int ret;
634 	u32 nsid;
635 
636 	ret = kstrtou32(name, 0, &nsid);
637 	if (ret)
638 		goto out;
639 
640 	ret = -EINVAL;
641 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
642 		pr_err("invalid nsid %#x", nsid);
643 		goto out;
644 	}
645 
646 	ret = -ENOMEM;
647 	ns = nvmet_ns_alloc(subsys, nsid);
648 	if (!ns)
649 		goto out;
650 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
651 
652 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
653 
654 	return &ns->group;
655 out:
656 	return ERR_PTR(ret);
657 }
658 
659 static struct configfs_group_operations nvmet_namespaces_group_ops = {
660 	.make_group		= nvmet_ns_make,
661 };
662 
663 static const struct config_item_type nvmet_namespaces_type = {
664 	.ct_group_ops		= &nvmet_namespaces_group_ops,
665 	.ct_owner		= THIS_MODULE,
666 };
667 
668 #ifdef CONFIG_NVME_TARGET_PASSTHRU
669 
670 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
671 		char *page)
672 {
673 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
674 
675 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
676 }
677 
678 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
679 		const char *page, size_t count)
680 {
681 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
682 	size_t len;
683 	int ret;
684 
685 	mutex_lock(&subsys->lock);
686 
687 	ret = -EBUSY;
688 	if (subsys->passthru_ctrl)
689 		goto out_unlock;
690 
691 	ret = -EINVAL;
692 	len = strcspn(page, "\n");
693 	if (!len)
694 		goto out_unlock;
695 
696 	kfree(subsys->passthru_ctrl_path);
697 	ret = -ENOMEM;
698 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
699 	if (!subsys->passthru_ctrl_path)
700 		goto out_unlock;
701 
702 	mutex_unlock(&subsys->lock);
703 
704 	return count;
705 out_unlock:
706 	mutex_unlock(&subsys->lock);
707 	return ret;
708 }
709 CONFIGFS_ATTR(nvmet_passthru_, device_path);
710 
711 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
712 		char *page)
713 {
714 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
715 
716 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
717 }
718 
719 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
720 		const char *page, size_t count)
721 {
722 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
723 	bool enable;
724 	int ret = 0;
725 
726 	if (strtobool(page, &enable))
727 		return -EINVAL;
728 
729 	if (enable)
730 		ret = nvmet_passthru_ctrl_enable(subsys);
731 	else
732 		nvmet_passthru_ctrl_disable(subsys);
733 
734 	return ret ? ret : count;
735 }
736 CONFIGFS_ATTR(nvmet_passthru_, enable);
737 
738 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
739 		char *page)
740 {
741 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
742 }
743 
744 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
745 		const char *page, size_t count)
746 {
747 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
748 	unsigned int timeout;
749 
750 	if (kstrtouint(page, 0, &timeout))
751 		return -EINVAL;
752 	subsys->admin_timeout = timeout;
753 	return count;
754 }
755 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
756 
757 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
758 		char *page)
759 {
760 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
761 }
762 
763 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
764 		const char *page, size_t count)
765 {
766 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
767 	unsigned int timeout;
768 
769 	if (kstrtouint(page, 0, &timeout))
770 		return -EINVAL;
771 	subsys->io_timeout = timeout;
772 	return count;
773 }
774 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
775 
776 static struct configfs_attribute *nvmet_passthru_attrs[] = {
777 	&nvmet_passthru_attr_device_path,
778 	&nvmet_passthru_attr_enable,
779 	&nvmet_passthru_attr_admin_timeout,
780 	&nvmet_passthru_attr_io_timeout,
781 	NULL,
782 };
783 
784 static const struct config_item_type nvmet_passthru_type = {
785 	.ct_attrs		= nvmet_passthru_attrs,
786 	.ct_owner		= THIS_MODULE,
787 };
788 
789 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
790 {
791 	config_group_init_type_name(&subsys->passthru_group,
792 				    "passthru", &nvmet_passthru_type);
793 	configfs_add_default_group(&subsys->passthru_group,
794 				   &subsys->group);
795 }
796 
797 #else /* CONFIG_NVME_TARGET_PASSTHRU */
798 
799 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
800 {
801 }
802 
803 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
804 
805 static int nvmet_port_subsys_allow_link(struct config_item *parent,
806 		struct config_item *target)
807 {
808 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
809 	struct nvmet_subsys *subsys;
810 	struct nvmet_subsys_link *link, *p;
811 	int ret;
812 
813 	if (target->ci_type != &nvmet_subsys_type) {
814 		pr_err("can only link subsystems into the subsystems dir.!\n");
815 		return -EINVAL;
816 	}
817 	subsys = to_subsys(target);
818 	link = kmalloc(sizeof(*link), GFP_KERNEL);
819 	if (!link)
820 		return -ENOMEM;
821 	link->subsys = subsys;
822 
823 	down_write(&nvmet_config_sem);
824 	ret = -EEXIST;
825 	list_for_each_entry(p, &port->subsystems, entry) {
826 		if (p->subsys == subsys)
827 			goto out_free_link;
828 	}
829 
830 	if (list_empty(&port->subsystems)) {
831 		ret = nvmet_enable_port(port);
832 		if (ret)
833 			goto out_free_link;
834 	}
835 
836 	list_add_tail(&link->entry, &port->subsystems);
837 	nvmet_port_disc_changed(port, subsys);
838 
839 	up_write(&nvmet_config_sem);
840 	return 0;
841 
842 out_free_link:
843 	up_write(&nvmet_config_sem);
844 	kfree(link);
845 	return ret;
846 }
847 
848 static void nvmet_port_subsys_drop_link(struct config_item *parent,
849 		struct config_item *target)
850 {
851 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
852 	struct nvmet_subsys *subsys = to_subsys(target);
853 	struct nvmet_subsys_link *p;
854 
855 	down_write(&nvmet_config_sem);
856 	list_for_each_entry(p, &port->subsystems, entry) {
857 		if (p->subsys == subsys)
858 			goto found;
859 	}
860 	up_write(&nvmet_config_sem);
861 	return;
862 
863 found:
864 	list_del(&p->entry);
865 	nvmet_port_del_ctrls(port, subsys);
866 	nvmet_port_disc_changed(port, subsys);
867 
868 	if (list_empty(&port->subsystems))
869 		nvmet_disable_port(port);
870 	up_write(&nvmet_config_sem);
871 	kfree(p);
872 }
873 
874 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
875 	.allow_link		= nvmet_port_subsys_allow_link,
876 	.drop_link		= nvmet_port_subsys_drop_link,
877 };
878 
879 static const struct config_item_type nvmet_port_subsys_type = {
880 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
881 	.ct_owner		= THIS_MODULE,
882 };
883 
884 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
885 		struct config_item *target)
886 {
887 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
888 	struct nvmet_host *host;
889 	struct nvmet_host_link *link, *p;
890 	int ret;
891 
892 	if (target->ci_type != &nvmet_host_type) {
893 		pr_err("can only link hosts into the allowed_hosts directory!\n");
894 		return -EINVAL;
895 	}
896 
897 	host = to_host(target);
898 	link = kmalloc(sizeof(*link), GFP_KERNEL);
899 	if (!link)
900 		return -ENOMEM;
901 	link->host = host;
902 
903 	down_write(&nvmet_config_sem);
904 	ret = -EINVAL;
905 	if (subsys->allow_any_host) {
906 		pr_err("can't add hosts when allow_any_host is set!\n");
907 		goto out_free_link;
908 	}
909 
910 	ret = -EEXIST;
911 	list_for_each_entry(p, &subsys->hosts, entry) {
912 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
913 			goto out_free_link;
914 	}
915 	list_add_tail(&link->entry, &subsys->hosts);
916 	nvmet_subsys_disc_changed(subsys, host);
917 
918 	up_write(&nvmet_config_sem);
919 	return 0;
920 out_free_link:
921 	up_write(&nvmet_config_sem);
922 	kfree(link);
923 	return ret;
924 }
925 
926 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
927 		struct config_item *target)
928 {
929 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
930 	struct nvmet_host *host = to_host(target);
931 	struct nvmet_host_link *p;
932 
933 	down_write(&nvmet_config_sem);
934 	list_for_each_entry(p, &subsys->hosts, entry) {
935 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
936 			goto found;
937 	}
938 	up_write(&nvmet_config_sem);
939 	return;
940 
941 found:
942 	list_del(&p->entry);
943 	nvmet_subsys_disc_changed(subsys, host);
944 
945 	up_write(&nvmet_config_sem);
946 	kfree(p);
947 }
948 
949 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
950 	.allow_link		= nvmet_allowed_hosts_allow_link,
951 	.drop_link		= nvmet_allowed_hosts_drop_link,
952 };
953 
954 static const struct config_item_type nvmet_allowed_hosts_type = {
955 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
956 	.ct_owner		= THIS_MODULE,
957 };
958 
959 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
960 		char *page)
961 {
962 	return snprintf(page, PAGE_SIZE, "%d\n",
963 		to_subsys(item)->allow_any_host);
964 }
965 
966 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
967 		const char *page, size_t count)
968 {
969 	struct nvmet_subsys *subsys = to_subsys(item);
970 	bool allow_any_host;
971 	int ret = 0;
972 
973 	if (strtobool(page, &allow_any_host))
974 		return -EINVAL;
975 
976 	down_write(&nvmet_config_sem);
977 	if (allow_any_host && !list_empty(&subsys->hosts)) {
978 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
979 		ret = -EINVAL;
980 		goto out_unlock;
981 	}
982 
983 	if (subsys->allow_any_host != allow_any_host) {
984 		subsys->allow_any_host = allow_any_host;
985 		nvmet_subsys_disc_changed(subsys, NULL);
986 	}
987 
988 out_unlock:
989 	up_write(&nvmet_config_sem);
990 	return ret ? ret : count;
991 }
992 
993 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
994 
995 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
996 					      char *page)
997 {
998 	struct nvmet_subsys *subsys = to_subsys(item);
999 
1000 	if (NVME_TERTIARY(subsys->ver))
1001 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1002 				NVME_MAJOR(subsys->ver),
1003 				NVME_MINOR(subsys->ver),
1004 				NVME_TERTIARY(subsys->ver));
1005 
1006 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1007 			NVME_MAJOR(subsys->ver),
1008 			NVME_MINOR(subsys->ver));
1009 }
1010 
1011 static ssize_t
1012 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1013 		const char *page, size_t count)
1014 {
1015 	int major, minor, tertiary = 0;
1016 	int ret;
1017 
1018 	if (subsys->subsys_discovered) {
1019 		if (NVME_TERTIARY(subsys->ver))
1020 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1021 			       NVME_MAJOR(subsys->ver),
1022 			       NVME_MINOR(subsys->ver),
1023 			       NVME_TERTIARY(subsys->ver));
1024 		else
1025 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
1026 			       NVME_MAJOR(subsys->ver),
1027 			       NVME_MINOR(subsys->ver));
1028 		return -EINVAL;
1029 	}
1030 
1031 	/* passthru subsystems use the underlying controller's version */
1032 	if (nvmet_is_passthru_subsys(subsys))
1033 		return -EINVAL;
1034 
1035 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1036 	if (ret != 2 && ret != 3)
1037 		return -EINVAL;
1038 
1039 	subsys->ver = NVME_VS(major, minor, tertiary);
1040 
1041 	return count;
1042 }
1043 
1044 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1045 					       const char *page, size_t count)
1046 {
1047 	struct nvmet_subsys *subsys = to_subsys(item);
1048 	ssize_t ret;
1049 
1050 	down_write(&nvmet_config_sem);
1051 	mutex_lock(&subsys->lock);
1052 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1053 	mutex_unlock(&subsys->lock);
1054 	up_write(&nvmet_config_sem);
1055 
1056 	return ret;
1057 }
1058 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1059 
1060 /* See Section 1.5 of NVMe 1.4 */
1061 static bool nvmet_is_ascii(const char c)
1062 {
1063 	return c >= 0x20 && c <= 0x7e;
1064 }
1065 
1066 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1067 					     char *page)
1068 {
1069 	struct nvmet_subsys *subsys = to_subsys(item);
1070 
1071 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1072 			NVMET_SN_MAX_SIZE, subsys->serial);
1073 }
1074 
1075 static ssize_t
1076 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1077 		const char *page, size_t count)
1078 {
1079 	int pos, len = strcspn(page, "\n");
1080 
1081 	if (subsys->subsys_discovered) {
1082 		pr_err("Can't set serial number. %s is already assigned\n",
1083 		       subsys->serial);
1084 		return -EINVAL;
1085 	}
1086 
1087 	if (!len || len > NVMET_SN_MAX_SIZE) {
1088 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1089 		       NVMET_SN_MAX_SIZE);
1090 		return -EINVAL;
1091 	}
1092 
1093 	for (pos = 0; pos < len; pos++) {
1094 		if (!nvmet_is_ascii(page[pos])) {
1095 			pr_err("Serial Number must contain only ASCII strings\n");
1096 			return -EINVAL;
1097 		}
1098 	}
1099 
1100 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1101 
1102 	return count;
1103 }
1104 
1105 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1106 					      const char *page, size_t count)
1107 {
1108 	struct nvmet_subsys *subsys = to_subsys(item);
1109 	ssize_t ret;
1110 
1111 	down_write(&nvmet_config_sem);
1112 	mutex_lock(&subsys->lock);
1113 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1114 	mutex_unlock(&subsys->lock);
1115 	up_write(&nvmet_config_sem);
1116 
1117 	return ret;
1118 }
1119 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1120 
1121 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1122 						 char *page)
1123 {
1124 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1125 }
1126 
1127 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1128 						  const char *page, size_t cnt)
1129 {
1130 	u16 cntlid_min;
1131 
1132 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1133 		return -EINVAL;
1134 
1135 	if (cntlid_min == 0)
1136 		return -EINVAL;
1137 
1138 	down_write(&nvmet_config_sem);
1139 	if (cntlid_min >= to_subsys(item)->cntlid_max)
1140 		goto out_unlock;
1141 	to_subsys(item)->cntlid_min = cntlid_min;
1142 	up_write(&nvmet_config_sem);
1143 	return cnt;
1144 
1145 out_unlock:
1146 	up_write(&nvmet_config_sem);
1147 	return -EINVAL;
1148 }
1149 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1150 
1151 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1152 						 char *page)
1153 {
1154 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1155 }
1156 
1157 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1158 						  const char *page, size_t cnt)
1159 {
1160 	u16 cntlid_max;
1161 
1162 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1163 		return -EINVAL;
1164 
1165 	if (cntlid_max == 0)
1166 		return -EINVAL;
1167 
1168 	down_write(&nvmet_config_sem);
1169 	if (cntlid_max <= to_subsys(item)->cntlid_min)
1170 		goto out_unlock;
1171 	to_subsys(item)->cntlid_max = cntlid_max;
1172 	up_write(&nvmet_config_sem);
1173 	return cnt;
1174 
1175 out_unlock:
1176 	up_write(&nvmet_config_sem);
1177 	return -EINVAL;
1178 }
1179 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1180 
1181 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1182 					    char *page)
1183 {
1184 	struct nvmet_subsys *subsys = to_subsys(item);
1185 
1186 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1187 }
1188 
1189 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1190 		const char *page, size_t count)
1191 {
1192 	int pos = 0, len;
1193 
1194 	if (subsys->subsys_discovered) {
1195 		pr_err("Can't set model number. %s is already assigned\n",
1196 		       subsys->model_number);
1197 		return -EINVAL;
1198 	}
1199 
1200 	len = strcspn(page, "\n");
1201 	if (!len)
1202 		return -EINVAL;
1203 
1204 	if (len > NVMET_MN_MAX_SIZE) {
1205 		pr_err("Model number size can not exceed %d Bytes\n",
1206 		       NVMET_MN_MAX_SIZE);
1207 		return -EINVAL;
1208 	}
1209 
1210 	for (pos = 0; pos < len; pos++) {
1211 		if (!nvmet_is_ascii(page[pos]))
1212 			return -EINVAL;
1213 	}
1214 
1215 	subsys->model_number = kmemdup_nul(page, len, GFP_KERNEL);
1216 	if (!subsys->model_number)
1217 		return -ENOMEM;
1218 	return count;
1219 }
1220 
1221 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1222 					     const char *page, size_t count)
1223 {
1224 	struct nvmet_subsys *subsys = to_subsys(item);
1225 	ssize_t ret;
1226 
1227 	down_write(&nvmet_config_sem);
1228 	mutex_lock(&subsys->lock);
1229 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1230 	mutex_unlock(&subsys->lock);
1231 	up_write(&nvmet_config_sem);
1232 
1233 	return ret;
1234 }
1235 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1236 
1237 #ifdef CONFIG_BLK_DEV_INTEGRITY
1238 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1239 						char *page)
1240 {
1241 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1242 }
1243 
1244 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1245 						 const char *page, size_t count)
1246 {
1247 	struct nvmet_subsys *subsys = to_subsys(item);
1248 	bool pi_enable;
1249 
1250 	if (strtobool(page, &pi_enable))
1251 		return -EINVAL;
1252 
1253 	subsys->pi_support = pi_enable;
1254 	return count;
1255 }
1256 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1257 #endif
1258 
1259 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1260 	&nvmet_subsys_attr_attr_allow_any_host,
1261 	&nvmet_subsys_attr_attr_version,
1262 	&nvmet_subsys_attr_attr_serial,
1263 	&nvmet_subsys_attr_attr_cntlid_min,
1264 	&nvmet_subsys_attr_attr_cntlid_max,
1265 	&nvmet_subsys_attr_attr_model,
1266 #ifdef CONFIG_BLK_DEV_INTEGRITY
1267 	&nvmet_subsys_attr_attr_pi_enable,
1268 #endif
1269 	NULL,
1270 };
1271 
1272 /*
1273  * Subsystem structures & folder operation functions below
1274  */
1275 static void nvmet_subsys_release(struct config_item *item)
1276 {
1277 	struct nvmet_subsys *subsys = to_subsys(item);
1278 
1279 	nvmet_subsys_del_ctrls(subsys);
1280 	nvmet_subsys_put(subsys);
1281 }
1282 
1283 static struct configfs_item_operations nvmet_subsys_item_ops = {
1284 	.release		= nvmet_subsys_release,
1285 };
1286 
1287 static const struct config_item_type nvmet_subsys_type = {
1288 	.ct_item_ops		= &nvmet_subsys_item_ops,
1289 	.ct_attrs		= nvmet_subsys_attrs,
1290 	.ct_owner		= THIS_MODULE,
1291 };
1292 
1293 static struct config_group *nvmet_subsys_make(struct config_group *group,
1294 		const char *name)
1295 {
1296 	struct nvmet_subsys *subsys;
1297 
1298 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1299 		pr_err("can't create discovery subsystem through configfs\n");
1300 		return ERR_PTR(-EINVAL);
1301 	}
1302 
1303 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1304 	if (IS_ERR(subsys))
1305 		return ERR_CAST(subsys);
1306 
1307 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1308 
1309 	config_group_init_type_name(&subsys->namespaces_group,
1310 			"namespaces", &nvmet_namespaces_type);
1311 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1312 
1313 	config_group_init_type_name(&subsys->allowed_hosts_group,
1314 			"allowed_hosts", &nvmet_allowed_hosts_type);
1315 	configfs_add_default_group(&subsys->allowed_hosts_group,
1316 			&subsys->group);
1317 
1318 	nvmet_add_passthru_group(subsys);
1319 
1320 	return &subsys->group;
1321 }
1322 
1323 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1324 	.make_group		= nvmet_subsys_make,
1325 };
1326 
1327 static const struct config_item_type nvmet_subsystems_type = {
1328 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1329 	.ct_owner		= THIS_MODULE,
1330 };
1331 
1332 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1333 		char *page)
1334 {
1335 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1336 }
1337 
1338 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1339 		const char *page, size_t count)
1340 {
1341 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1342 	struct nvmet_port *port = to_nvmet_port(item);
1343 	bool enable;
1344 
1345 	if (strtobool(page, &enable))
1346 		goto inval;
1347 
1348 	if (enable)
1349 		nvmet_referral_enable(parent, port);
1350 	else
1351 		nvmet_referral_disable(parent, port);
1352 
1353 	return count;
1354 inval:
1355 	pr_err("Invalid value '%s' for enable\n", page);
1356 	return -EINVAL;
1357 }
1358 
1359 CONFIGFS_ATTR(nvmet_referral_, enable);
1360 
1361 /*
1362  * Discovery Service subsystem definitions
1363  */
1364 static struct configfs_attribute *nvmet_referral_attrs[] = {
1365 	&nvmet_attr_addr_adrfam,
1366 	&nvmet_attr_addr_portid,
1367 	&nvmet_attr_addr_treq,
1368 	&nvmet_attr_addr_traddr,
1369 	&nvmet_attr_addr_trsvcid,
1370 	&nvmet_attr_addr_trtype,
1371 	&nvmet_referral_attr_enable,
1372 	NULL,
1373 };
1374 
1375 static void nvmet_referral_notify(struct config_group *group,
1376 		struct config_item *item)
1377 {
1378 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1379 	struct nvmet_port *port = to_nvmet_port(item);
1380 
1381 	nvmet_referral_disable(parent, port);
1382 }
1383 
1384 static void nvmet_referral_release(struct config_item *item)
1385 {
1386 	struct nvmet_port *port = to_nvmet_port(item);
1387 
1388 	kfree(port);
1389 }
1390 
1391 static struct configfs_item_operations nvmet_referral_item_ops = {
1392 	.release	= nvmet_referral_release,
1393 };
1394 
1395 static const struct config_item_type nvmet_referral_type = {
1396 	.ct_owner	= THIS_MODULE,
1397 	.ct_attrs	= nvmet_referral_attrs,
1398 	.ct_item_ops	= &nvmet_referral_item_ops,
1399 };
1400 
1401 static struct config_group *nvmet_referral_make(
1402 		struct config_group *group, const char *name)
1403 {
1404 	struct nvmet_port *port;
1405 
1406 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1407 	if (!port)
1408 		return ERR_PTR(-ENOMEM);
1409 
1410 	INIT_LIST_HEAD(&port->entry);
1411 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1412 
1413 	return &port->group;
1414 }
1415 
1416 static struct configfs_group_operations nvmet_referral_group_ops = {
1417 	.make_group		= nvmet_referral_make,
1418 	.disconnect_notify	= nvmet_referral_notify,
1419 };
1420 
1421 static const struct config_item_type nvmet_referrals_type = {
1422 	.ct_owner	= THIS_MODULE,
1423 	.ct_group_ops	= &nvmet_referral_group_ops,
1424 };
1425 
1426 static struct nvmet_type_name_map nvmet_ana_state[] = {
1427 	{ NVME_ANA_OPTIMIZED,		"optimized" },
1428 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
1429 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
1430 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
1431 	{ NVME_ANA_CHANGE,		"change" },
1432 };
1433 
1434 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1435 		char *page)
1436 {
1437 	struct nvmet_ana_group *grp = to_ana_group(item);
1438 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1439 	int i;
1440 
1441 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1442 		if (state == nvmet_ana_state[i].type)
1443 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1444 	}
1445 
1446 	return sprintf(page, "\n");
1447 }
1448 
1449 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1450 		const char *page, size_t count)
1451 {
1452 	struct nvmet_ana_group *grp = to_ana_group(item);
1453 	enum nvme_ana_state *ana_state = grp->port->ana_state;
1454 	int i;
1455 
1456 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1457 		if (sysfs_streq(page, nvmet_ana_state[i].name))
1458 			goto found;
1459 	}
1460 
1461 	pr_err("Invalid value '%s' for ana_state\n", page);
1462 	return -EINVAL;
1463 
1464 found:
1465 	down_write(&nvmet_ana_sem);
1466 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1467 	nvmet_ana_chgcnt++;
1468 	up_write(&nvmet_ana_sem);
1469 	nvmet_port_send_ana_event(grp->port);
1470 	return count;
1471 }
1472 
1473 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1474 
1475 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1476 	&nvmet_ana_group_attr_ana_state,
1477 	NULL,
1478 };
1479 
1480 static void nvmet_ana_group_release(struct config_item *item)
1481 {
1482 	struct nvmet_ana_group *grp = to_ana_group(item);
1483 
1484 	if (grp == &grp->port->ana_default_group)
1485 		return;
1486 
1487 	down_write(&nvmet_ana_sem);
1488 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1489 	nvmet_ana_group_enabled[grp->grpid]--;
1490 	up_write(&nvmet_ana_sem);
1491 
1492 	nvmet_port_send_ana_event(grp->port);
1493 	kfree(grp);
1494 }
1495 
1496 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1497 	.release		= nvmet_ana_group_release,
1498 };
1499 
1500 static const struct config_item_type nvmet_ana_group_type = {
1501 	.ct_item_ops		= &nvmet_ana_group_item_ops,
1502 	.ct_attrs		= nvmet_ana_group_attrs,
1503 	.ct_owner		= THIS_MODULE,
1504 };
1505 
1506 static struct config_group *nvmet_ana_groups_make_group(
1507 		struct config_group *group, const char *name)
1508 {
1509 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1510 	struct nvmet_ana_group *grp;
1511 	u32 grpid;
1512 	int ret;
1513 
1514 	ret = kstrtou32(name, 0, &grpid);
1515 	if (ret)
1516 		goto out;
1517 
1518 	ret = -EINVAL;
1519 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1520 		goto out;
1521 
1522 	ret = -ENOMEM;
1523 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1524 	if (!grp)
1525 		goto out;
1526 	grp->port = port;
1527 	grp->grpid = grpid;
1528 
1529 	down_write(&nvmet_ana_sem);
1530 	nvmet_ana_group_enabled[grpid]++;
1531 	up_write(&nvmet_ana_sem);
1532 
1533 	nvmet_port_send_ana_event(grp->port);
1534 
1535 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1536 	return &grp->group;
1537 out:
1538 	return ERR_PTR(ret);
1539 }
1540 
1541 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1542 	.make_group		= nvmet_ana_groups_make_group,
1543 };
1544 
1545 static const struct config_item_type nvmet_ana_groups_type = {
1546 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
1547 	.ct_owner		= THIS_MODULE,
1548 };
1549 
1550 /*
1551  * Ports definitions.
1552  */
1553 static void nvmet_port_release(struct config_item *item)
1554 {
1555 	struct nvmet_port *port = to_nvmet_port(item);
1556 
1557 	/* Let inflight controllers teardown complete */
1558 	flush_workqueue(nvmet_wq);
1559 	list_del(&port->global_entry);
1560 
1561 	kfree(port->ana_state);
1562 	kfree(port);
1563 }
1564 
1565 static struct configfs_attribute *nvmet_port_attrs[] = {
1566 	&nvmet_attr_addr_adrfam,
1567 	&nvmet_attr_addr_treq,
1568 	&nvmet_attr_addr_traddr,
1569 	&nvmet_attr_addr_trsvcid,
1570 	&nvmet_attr_addr_trtype,
1571 	&nvmet_attr_param_inline_data_size,
1572 #ifdef CONFIG_BLK_DEV_INTEGRITY
1573 	&nvmet_attr_param_pi_enable,
1574 #endif
1575 	NULL,
1576 };
1577 
1578 static struct configfs_item_operations nvmet_port_item_ops = {
1579 	.release		= nvmet_port_release,
1580 };
1581 
1582 static const struct config_item_type nvmet_port_type = {
1583 	.ct_attrs		= nvmet_port_attrs,
1584 	.ct_item_ops		= &nvmet_port_item_ops,
1585 	.ct_owner		= THIS_MODULE,
1586 };
1587 
1588 static struct config_group *nvmet_ports_make(struct config_group *group,
1589 		const char *name)
1590 {
1591 	struct nvmet_port *port;
1592 	u16 portid;
1593 	u32 i;
1594 
1595 	if (kstrtou16(name, 0, &portid))
1596 		return ERR_PTR(-EINVAL);
1597 
1598 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1599 	if (!port)
1600 		return ERR_PTR(-ENOMEM);
1601 
1602 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1603 			sizeof(*port->ana_state), GFP_KERNEL);
1604 	if (!port->ana_state) {
1605 		kfree(port);
1606 		return ERR_PTR(-ENOMEM);
1607 	}
1608 
1609 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1610 		if (i == NVMET_DEFAULT_ANA_GRPID)
1611 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
1612 		else
1613 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1614 	}
1615 
1616 	list_add(&port->global_entry, &nvmet_ports_list);
1617 
1618 	INIT_LIST_HEAD(&port->entry);
1619 	INIT_LIST_HEAD(&port->subsystems);
1620 	INIT_LIST_HEAD(&port->referrals);
1621 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1622 
1623 	port->disc_addr.portid = cpu_to_le16(portid);
1624 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1625 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1626 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1627 
1628 	config_group_init_type_name(&port->subsys_group,
1629 			"subsystems", &nvmet_port_subsys_type);
1630 	configfs_add_default_group(&port->subsys_group, &port->group);
1631 
1632 	config_group_init_type_name(&port->referrals_group,
1633 			"referrals", &nvmet_referrals_type);
1634 	configfs_add_default_group(&port->referrals_group, &port->group);
1635 
1636 	config_group_init_type_name(&port->ana_groups_group,
1637 			"ana_groups", &nvmet_ana_groups_type);
1638 	configfs_add_default_group(&port->ana_groups_group, &port->group);
1639 
1640 	port->ana_default_group.port = port;
1641 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1642 	config_group_init_type_name(&port->ana_default_group.group,
1643 			__stringify(NVMET_DEFAULT_ANA_GRPID),
1644 			&nvmet_ana_group_type);
1645 	configfs_add_default_group(&port->ana_default_group.group,
1646 			&port->ana_groups_group);
1647 
1648 	return &port->group;
1649 }
1650 
1651 static struct configfs_group_operations nvmet_ports_group_ops = {
1652 	.make_group		= nvmet_ports_make,
1653 };
1654 
1655 static const struct config_item_type nvmet_ports_type = {
1656 	.ct_group_ops		= &nvmet_ports_group_ops,
1657 	.ct_owner		= THIS_MODULE,
1658 };
1659 
1660 static struct config_group nvmet_subsystems_group;
1661 static struct config_group nvmet_ports_group;
1662 
1663 static void nvmet_host_release(struct config_item *item)
1664 {
1665 	struct nvmet_host *host = to_host(item);
1666 
1667 	kfree(host);
1668 }
1669 
1670 static struct configfs_item_operations nvmet_host_item_ops = {
1671 	.release		= nvmet_host_release,
1672 };
1673 
1674 static const struct config_item_type nvmet_host_type = {
1675 	.ct_item_ops		= &nvmet_host_item_ops,
1676 	.ct_owner		= THIS_MODULE,
1677 };
1678 
1679 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1680 		const char *name)
1681 {
1682 	struct nvmet_host *host;
1683 
1684 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1685 	if (!host)
1686 		return ERR_PTR(-ENOMEM);
1687 
1688 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1689 
1690 	return &host->group;
1691 }
1692 
1693 static struct configfs_group_operations nvmet_hosts_group_ops = {
1694 	.make_group		= nvmet_hosts_make_group,
1695 };
1696 
1697 static const struct config_item_type nvmet_hosts_type = {
1698 	.ct_group_ops		= &nvmet_hosts_group_ops,
1699 	.ct_owner		= THIS_MODULE,
1700 };
1701 
1702 static struct config_group nvmet_hosts_group;
1703 
1704 static const struct config_item_type nvmet_root_type = {
1705 	.ct_owner		= THIS_MODULE,
1706 };
1707 
1708 static struct configfs_subsystem nvmet_configfs_subsystem = {
1709 	.su_group = {
1710 		.cg_item = {
1711 			.ci_namebuf	= "nvmet",
1712 			.ci_type	= &nvmet_root_type,
1713 		},
1714 	},
1715 };
1716 
1717 int __init nvmet_init_configfs(void)
1718 {
1719 	int ret;
1720 
1721 	config_group_init(&nvmet_configfs_subsystem.su_group);
1722 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1723 
1724 	config_group_init_type_name(&nvmet_subsystems_group,
1725 			"subsystems", &nvmet_subsystems_type);
1726 	configfs_add_default_group(&nvmet_subsystems_group,
1727 			&nvmet_configfs_subsystem.su_group);
1728 
1729 	config_group_init_type_name(&nvmet_ports_group,
1730 			"ports", &nvmet_ports_type);
1731 	configfs_add_default_group(&nvmet_ports_group,
1732 			&nvmet_configfs_subsystem.su_group);
1733 
1734 	config_group_init_type_name(&nvmet_hosts_group,
1735 			"hosts", &nvmet_hosts_type);
1736 	configfs_add_default_group(&nvmet_hosts_group,
1737 			&nvmet_configfs_subsystem.su_group);
1738 
1739 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1740 	if (ret) {
1741 		pr_err("configfs_register_subsystem: %d\n", ret);
1742 		return ret;
1743 	}
1744 
1745 	return 0;
1746 }
1747 
1748 void __exit nvmet_exit_configfs(void)
1749 {
1750 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1751 }
1752