xref: /linux/drivers/nvme/target/configfs.c (revision 44f57d78)
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 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" },
31 };
32 
33 /*
34  * nvmet_port Generic ConfigFS definitions.
35  * Used in any place in the ConfigFS tree that refers to an address.
36  */
37 static ssize_t nvmet_addr_adrfam_show(struct config_item *item,
38 		char *page)
39 {
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");
51 	}
52 }
53 
54 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
55 		const char *page, size_t count)
56 {
57 	struct nvmet_port *port = to_nvmet_port(item);
58 
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;
63 	}
64 
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;
76 	}
77 
78 	return count;
79 }
80 
81 CONFIGFS_ATTR(nvmet_, addr_adrfam);
82 
83 static ssize_t nvmet_addr_portid_show(struct config_item *item,
84 		char *page)
85 {
86 	struct nvmet_port *port = to_nvmet_port(item);
87 
88 	return snprintf(page, PAGE_SIZE, "%d\n",
89 			le16_to_cpu(port->disc_addr.portid));
90 }
91 
92 static ssize_t nvmet_addr_portid_store(struct config_item *item,
93 		const char *page, size_t count)
94 {
95 	struct nvmet_port *port = to_nvmet_port(item);
96 	u16 portid = 0;
97 
98 	if (kstrtou16(page, 0, &portid)) {
99 		pr_err("Invalid value '%s' for portid\n", page);
100 		return -EINVAL;
101 	}
102 
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;
107 	}
108 	port->disc_addr.portid = cpu_to_le16(portid);
109 	return count;
110 }
111 
112 CONFIGFS_ATTR(nvmet_, addr_portid);
113 
114 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
115 		char *page)
116 {
117 	struct nvmet_port *port = to_nvmet_port(item);
118 
119 	return snprintf(page, PAGE_SIZE, "%s\n",
120 			port->disc_addr.traddr);
121 }
122 
123 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
124 		const char *page, size_t count)
125 {
126 	struct nvmet_port *port = to_nvmet_port(item);
127 
128 	if (count > NVMF_TRADDR_SIZE) {
129 		pr_err("Invalid value '%s' for traddr\n", page);
130 		return -EINVAL;
131 	}
132 
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;
137 	}
138 
139 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
140 		return -EINVAL;
141 	return count;
142 }
143 
144 CONFIGFS_ATTR(nvmet_, addr_traddr);
145 
146 static ssize_t nvmet_addr_treq_show(struct config_item *item,
147 		char *page)
148 {
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");
159 	}
160 }
161 
162 static ssize_t nvmet_addr_treq_store(struct config_item *item,
163 		const char *page, size_t count)
164 {
165 	struct nvmet_port *port = to_nvmet_port(item);
166 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
167 
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;
172 	}
173 
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;
183 	}
184 	port->disc_addr.treq = treq;
185 
186 	return count;
187 }
188 
189 CONFIGFS_ATTR(nvmet_, addr_treq);
190 
191 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
192 		char *page)
193 {
194 	struct nvmet_port *port = to_nvmet_port(item);
195 
196 	return snprintf(page, PAGE_SIZE, "%s\n",
197 			port->disc_addr.trsvcid);
198 }
199 
200 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
201 		const char *page, size_t count)
202 {
203 	struct nvmet_port *port = to_nvmet_port(item);
204 
205 	if (count > NVMF_TRSVCID_SIZE) {
206 		pr_err("Invalid value '%s' for trsvcid\n", page);
207 		return -EINVAL;
208 	}
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;
213 	}
214 
215 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
216 		return -EINVAL;
217 	return count;
218 }
219 
220 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
221 
222 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
223 		char *page)
224 {
225 	struct nvmet_port *port = to_nvmet_port(item);
226 
227 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
228 }
229 
230 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
231 		const char *page, size_t count)
232 {
233 	struct nvmet_port *port = to_nvmet_port(item);
234 	int ret;
235 
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;
240 	}
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;
245 	}
246 	return count;
247 }
248 
249 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
250 
251 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
252 		char *page)
253 {
254 	struct nvmet_port *port = to_nvmet_port(item);
255 	int i;
256 
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);
261 	}
262 
263 	return sprintf(page, "\n");
264 }
265 
266 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
267 {
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;
271 }
272 
273 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
274 		const char *page, size_t count)
275 {
276 	struct nvmet_port *port = to_nvmet_port(item);
277 	int i;
278 
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;
283 	}
284 
285 	for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) {
286 		if (sysfs_streq(page, nvmet_transport_names[i].name))
287 			goto found;
288 	}
289 
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;
298 }
299 
300 CONFIGFS_ATTR(nvmet_, addr_trtype);
301 
302 /*
303  * Namespace structures & file operation functions below
304  */
305 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
306 {
307 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
308 }
309 
310 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
311 		const char *page, size_t count)
312 {
313 	struct nvmet_ns *ns = to_nvmet_ns(item);
314 	struct nvmet_subsys *subsys = ns->subsys;
315 	size_t len;
316 	int ret;
317 
318 	mutex_lock(&subsys->lock);
319 	ret = -EBUSY;
320 	if (ns->enabled)
321 		goto out_unlock;
322 
323 	ret = -EINVAL;
324 	len = strcspn(page, "\n");
325 	if (!len)
326 		goto out_unlock;
327 
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;
333 
334 	mutex_unlock(&subsys->lock);
335 	return count;
336 
337 out_unlock:
338 	mutex_unlock(&subsys->lock);
339 	return ret;
340 }
341 
342 CONFIGFS_ATTR(nvmet_ns_, device_path);
343 
344 #ifdef CONFIG_PCI_P2PDMA
345 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
346 {
347 	struct nvmet_ns *ns = to_nvmet_ns(item);
348 
349 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
350 }
351 
352 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
353 		const char *page, size_t count)
354 {
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;
360 
361 	mutex_lock(&ns->subsys->lock);
362 	if (ns->enabled) {
363 		ret = -EBUSY;
364 		goto out_unlock;
365 	}
366 
367 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
368 	if (error) {
369 		ret = error;
370 		goto out_unlock;
371 	}
372 
373 	ns->use_p2pmem = use_p2pmem;
374 	pci_dev_put(ns->p2p_dev);
375 	ns->p2p_dev = p2p_dev;
376 
377 out_unlock:
378 	mutex_unlock(&ns->subsys->lock);
379 
380 	return ret;
381 }
382 
383 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
384 #endif /* CONFIG_PCI_P2PDMA */
385 
386 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
387 {
388 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
389 }
390 
391 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
392 					  const char *page, size_t count)
393 {
394 	struct nvmet_ns *ns = to_nvmet_ns(item);
395 	struct nvmet_subsys *subsys = ns->subsys;
396 	int ret = 0;
397 
398 
399 	mutex_lock(&subsys->lock);
400 	if (ns->enabled) {
401 		ret = -EBUSY;
402 		goto out_unlock;
403 	}
404 
405 
406 	if (uuid_parse(page, &ns->uuid))
407 		ret = -EINVAL;
408 
409 out_unlock:
410 	mutex_unlock(&subsys->lock);
411 	return ret ? ret : count;
412 }
413 
414 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
415 
416 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
417 {
418 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
419 }
420 
421 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
422 		const char *page, size_t count)
423 {
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;
430 
431 	mutex_lock(&subsys->lock);
432 	if (ns->enabled) {
433 		ret = -EBUSY;
434 		goto out_unlock;
435 	}
436 
437 	for (i = 0; i < 16; i++) {
438 		if (p + 2 > page + count) {
439 			ret = -EINVAL;
440 			goto out_unlock;
441 		}
442 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
443 			ret = -EINVAL;
444 			goto out_unlock;
445 		}
446 
447 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
448 		p += 2;
449 
450 		if (*p == '-' || *p == ':')
451 			p++;
452 	}
453 
454 	memcpy(&ns->nguid, nguid, sizeof(nguid));
455 out_unlock:
456 	mutex_unlock(&subsys->lock);
457 	return ret ? ret : count;
458 }
459 
460 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
461 
462 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
463 {
464 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
465 }
466 
467 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
468 		const char *page, size_t count)
469 {
470 	struct nvmet_ns *ns = to_nvmet_ns(item);
471 	u32 oldgrpid, newgrpid;
472 	int ret;
473 
474 	ret = kstrtou32(page, 0, &newgrpid);
475 	if (ret)
476 		return ret;
477 
478 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
479 		return -EINVAL;
480 
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);
488 
489 	nvmet_send_ana_event(ns->subsys, NULL);
490 	return count;
491 }
492 
493 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
494 
495 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
496 {
497 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
498 }
499 
500 static ssize_t nvmet_ns_enable_store(struct config_item *item,
501 		const char *page, size_t count)
502 {
503 	struct nvmet_ns *ns = to_nvmet_ns(item);
504 	bool enable;
505 	int ret = 0;
506 
507 	if (strtobool(page, &enable))
508 		return -EINVAL;
509 
510 	if (enable)
511 		ret = nvmet_ns_enable(ns);
512 	else
513 		nvmet_ns_disable(ns);
514 
515 	return ret ? ret : count;
516 }
517 
518 CONFIGFS_ATTR(nvmet_ns_, enable);
519 
520 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
521 {
522 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
523 }
524 
525 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
526 		const char *page, size_t count)
527 {
528 	struct nvmet_ns *ns = to_nvmet_ns(item);
529 	bool val;
530 
531 	if (strtobool(page, &val))
532 		return -EINVAL;
533 
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;
539 	}
540 
541 	ns->buffered_io = val;
542 	mutex_unlock(&ns->subsys->lock);
543 	return count;
544 }
545 
546 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
547 
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,
559 };
560 
561 static void nvmet_ns_release(struct config_item *item)
562 {
563 	struct nvmet_ns *ns = to_nvmet_ns(item);
564 
565 	nvmet_ns_free(ns);
566 }
567 
568 static struct configfs_item_operations nvmet_ns_item_ops = {
569 	.release		= nvmet_ns_release,
570 };
571 
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,
576 };
577 
578 static struct config_group *nvmet_ns_make(struct config_group *group,
579 		const char *name)
580 {
581 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
582 	struct nvmet_ns *ns;
583 	int ret;
584 	u32 nsid;
585 
586 	ret = kstrtou32(name, 0, &nsid);
587 	if (ret)
588 		goto out;
589 
590 	ret = -EINVAL;
591 	if (nsid == 0 || nsid == NVME_NSID_ALL)
592 		goto out;
593 
594 	ret = -ENOMEM;
595 	ns = nvmet_ns_alloc(subsys, nsid);
596 	if (!ns)
597 		goto out;
598 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
599 
600 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
601 
602 	return &ns->group;
603 out:
604 	return ERR_PTR(ret);
605 }
606 
607 static struct configfs_group_operations nvmet_namespaces_group_ops = {
608 	.make_group		= nvmet_ns_make,
609 };
610 
611 static const struct config_item_type nvmet_namespaces_type = {
612 	.ct_group_ops		= &nvmet_namespaces_group_ops,
613 	.ct_owner		= THIS_MODULE,
614 };
615 
616 static int nvmet_port_subsys_allow_link(struct config_item *parent,
617 		struct config_item *target)
618 {
619 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
620 	struct nvmet_subsys *subsys;
621 	struct nvmet_subsys_link *link, *p;
622 	int ret;
623 
624 	if (target->ci_type != &nvmet_subsys_type) {
625 		pr_err("can only link subsystems into the subsystems dir.!\n");
626 		return -EINVAL;
627 	}
628 	subsys = to_subsys(target);
629 	link = kmalloc(sizeof(*link), GFP_KERNEL);
630 	if (!link)
631 		return -ENOMEM;
632 	link->subsys = subsys;
633 
634 	down_write(&nvmet_config_sem);
635 	ret = -EEXIST;
636 	list_for_each_entry(p, &port->subsystems, entry) {
637 		if (p->subsys == subsys)
638 			goto out_free_link;
639 	}
640 
641 	if (list_empty(&port->subsystems)) {
642 		ret = nvmet_enable_port(port);
643 		if (ret)
644 			goto out_free_link;
645 	}
646 
647 	list_add_tail(&link->entry, &port->subsystems);
648 	nvmet_port_disc_changed(port, subsys);
649 
650 	up_write(&nvmet_config_sem);
651 	return 0;
652 
653 out_free_link:
654 	up_write(&nvmet_config_sem);
655 	kfree(link);
656 	return ret;
657 }
658 
659 static void nvmet_port_subsys_drop_link(struct config_item *parent,
660 		struct config_item *target)
661 {
662 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
663 	struct nvmet_subsys *subsys = to_subsys(target);
664 	struct nvmet_subsys_link *p;
665 
666 	down_write(&nvmet_config_sem);
667 	list_for_each_entry(p, &port->subsystems, entry) {
668 		if (p->subsys == subsys)
669 			goto found;
670 	}
671 	up_write(&nvmet_config_sem);
672 	return;
673 
674 found:
675 	list_del(&p->entry);
676 	nvmet_port_disc_changed(port, subsys);
677 
678 	if (list_empty(&port->subsystems))
679 		nvmet_disable_port(port);
680 	up_write(&nvmet_config_sem);
681 	kfree(p);
682 }
683 
684 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
685 	.allow_link		= nvmet_port_subsys_allow_link,
686 	.drop_link		= nvmet_port_subsys_drop_link,
687 };
688 
689 static const struct config_item_type nvmet_port_subsys_type = {
690 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
691 	.ct_owner		= THIS_MODULE,
692 };
693 
694 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
695 		struct config_item *target)
696 {
697 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
698 	struct nvmet_host *host;
699 	struct nvmet_host_link *link, *p;
700 	int ret;
701 
702 	if (target->ci_type != &nvmet_host_type) {
703 		pr_err("can only link hosts into the allowed_hosts directory!\n");
704 		return -EINVAL;
705 	}
706 
707 	host = to_host(target);
708 	link = kmalloc(sizeof(*link), GFP_KERNEL);
709 	if (!link)
710 		return -ENOMEM;
711 	link->host = host;
712 
713 	down_write(&nvmet_config_sem);
714 	ret = -EINVAL;
715 	if (subsys->allow_any_host) {
716 		pr_err("can't add hosts when allow_any_host is set!\n");
717 		goto out_free_link;
718 	}
719 
720 	ret = -EEXIST;
721 	list_for_each_entry(p, &subsys->hosts, entry) {
722 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
723 			goto out_free_link;
724 	}
725 	list_add_tail(&link->entry, &subsys->hosts);
726 	nvmet_subsys_disc_changed(subsys, host);
727 
728 	up_write(&nvmet_config_sem);
729 	return 0;
730 out_free_link:
731 	up_write(&nvmet_config_sem);
732 	kfree(link);
733 	return ret;
734 }
735 
736 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
737 		struct config_item *target)
738 {
739 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
740 	struct nvmet_host *host = to_host(target);
741 	struct nvmet_host_link *p;
742 
743 	down_write(&nvmet_config_sem);
744 	list_for_each_entry(p, &subsys->hosts, entry) {
745 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
746 			goto found;
747 	}
748 	up_write(&nvmet_config_sem);
749 	return;
750 
751 found:
752 	list_del(&p->entry);
753 	nvmet_subsys_disc_changed(subsys, host);
754 
755 	up_write(&nvmet_config_sem);
756 	kfree(p);
757 }
758 
759 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
760 	.allow_link		= nvmet_allowed_hosts_allow_link,
761 	.drop_link		= nvmet_allowed_hosts_drop_link,
762 };
763 
764 static const struct config_item_type nvmet_allowed_hosts_type = {
765 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
766 	.ct_owner		= THIS_MODULE,
767 };
768 
769 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
770 		char *page)
771 {
772 	return snprintf(page, PAGE_SIZE, "%d\n",
773 		to_subsys(item)->allow_any_host);
774 }
775 
776 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
777 		const char *page, size_t count)
778 {
779 	struct nvmet_subsys *subsys = to_subsys(item);
780 	bool allow_any_host;
781 	int ret = 0;
782 
783 	if (strtobool(page, &allow_any_host))
784 		return -EINVAL;
785 
786 	down_write(&nvmet_config_sem);
787 	if (allow_any_host && !list_empty(&subsys->hosts)) {
788 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
789 		ret = -EINVAL;
790 		goto out_unlock;
791 	}
792 
793 	if (subsys->allow_any_host != allow_any_host) {
794 		subsys->allow_any_host = allow_any_host;
795 		nvmet_subsys_disc_changed(subsys, NULL);
796 	}
797 
798 out_unlock:
799 	up_write(&nvmet_config_sem);
800 	return ret ? ret : count;
801 }
802 
803 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
804 
805 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
806 					      char *page)
807 {
808 	struct nvmet_subsys *subsys = to_subsys(item);
809 
810 	if (NVME_TERTIARY(subsys->ver))
811 		return snprintf(page, PAGE_SIZE, "%d.%d.%d\n",
812 				(int)NVME_MAJOR(subsys->ver),
813 				(int)NVME_MINOR(subsys->ver),
814 				(int)NVME_TERTIARY(subsys->ver));
815 	else
816 		return snprintf(page, PAGE_SIZE, "%d.%d\n",
817 				(int)NVME_MAJOR(subsys->ver),
818 				(int)NVME_MINOR(subsys->ver));
819 }
820 
821 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
822 					       const char *page, size_t count)
823 {
824 	struct nvmet_subsys *subsys = to_subsys(item);
825 	int major, minor, tertiary = 0;
826 	int ret;
827 
828 
829 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
830 	if (ret != 2 && ret != 3)
831 		return -EINVAL;
832 
833 	down_write(&nvmet_config_sem);
834 	subsys->ver = NVME_VS(major, minor, tertiary);
835 	up_write(&nvmet_config_sem);
836 
837 	return count;
838 }
839 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
840 
841 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
842 					     char *page)
843 {
844 	struct nvmet_subsys *subsys = to_subsys(item);
845 
846 	return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
847 }
848 
849 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
850 					      const char *page, size_t count)
851 {
852 	struct nvmet_subsys *subsys = to_subsys(item);
853 
854 	down_write(&nvmet_config_sem);
855 	sscanf(page, "%llx\n", &subsys->serial);
856 	up_write(&nvmet_config_sem);
857 
858 	return count;
859 }
860 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
861 
862 static struct configfs_attribute *nvmet_subsys_attrs[] = {
863 	&nvmet_subsys_attr_attr_allow_any_host,
864 	&nvmet_subsys_attr_attr_version,
865 	&nvmet_subsys_attr_attr_serial,
866 	NULL,
867 };
868 
869 /*
870  * Subsystem structures & folder operation functions below
871  */
872 static void nvmet_subsys_release(struct config_item *item)
873 {
874 	struct nvmet_subsys *subsys = to_subsys(item);
875 
876 	nvmet_subsys_del_ctrls(subsys);
877 	nvmet_subsys_put(subsys);
878 }
879 
880 static struct configfs_item_operations nvmet_subsys_item_ops = {
881 	.release		= nvmet_subsys_release,
882 };
883 
884 static const struct config_item_type nvmet_subsys_type = {
885 	.ct_item_ops		= &nvmet_subsys_item_ops,
886 	.ct_attrs		= nvmet_subsys_attrs,
887 	.ct_owner		= THIS_MODULE,
888 };
889 
890 static struct config_group *nvmet_subsys_make(struct config_group *group,
891 		const char *name)
892 {
893 	struct nvmet_subsys *subsys;
894 
895 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
896 		pr_err("can't create discovery subsystem through configfs\n");
897 		return ERR_PTR(-EINVAL);
898 	}
899 
900 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
901 	if (IS_ERR(subsys))
902 		return ERR_CAST(subsys);
903 
904 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
905 
906 	config_group_init_type_name(&subsys->namespaces_group,
907 			"namespaces", &nvmet_namespaces_type);
908 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
909 
910 	config_group_init_type_name(&subsys->allowed_hosts_group,
911 			"allowed_hosts", &nvmet_allowed_hosts_type);
912 	configfs_add_default_group(&subsys->allowed_hosts_group,
913 			&subsys->group);
914 
915 	return &subsys->group;
916 }
917 
918 static struct configfs_group_operations nvmet_subsystems_group_ops = {
919 	.make_group		= nvmet_subsys_make,
920 };
921 
922 static const struct config_item_type nvmet_subsystems_type = {
923 	.ct_group_ops		= &nvmet_subsystems_group_ops,
924 	.ct_owner		= THIS_MODULE,
925 };
926 
927 static ssize_t nvmet_referral_enable_show(struct config_item *item,
928 		char *page)
929 {
930 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
931 }
932 
933 static ssize_t nvmet_referral_enable_store(struct config_item *item,
934 		const char *page, size_t count)
935 {
936 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
937 	struct nvmet_port *port = to_nvmet_port(item);
938 	bool enable;
939 
940 	if (strtobool(page, &enable))
941 		goto inval;
942 
943 	if (enable)
944 		nvmet_referral_enable(parent, port);
945 	else
946 		nvmet_referral_disable(parent, port);
947 
948 	return count;
949 inval:
950 	pr_err("Invalid value '%s' for enable\n", page);
951 	return -EINVAL;
952 }
953 
954 CONFIGFS_ATTR(nvmet_referral_, enable);
955 
956 /*
957  * Discovery Service subsystem definitions
958  */
959 static struct configfs_attribute *nvmet_referral_attrs[] = {
960 	&nvmet_attr_addr_adrfam,
961 	&nvmet_attr_addr_portid,
962 	&nvmet_attr_addr_treq,
963 	&nvmet_attr_addr_traddr,
964 	&nvmet_attr_addr_trsvcid,
965 	&nvmet_attr_addr_trtype,
966 	&nvmet_referral_attr_enable,
967 	NULL,
968 };
969 
970 static void nvmet_referral_release(struct config_item *item)
971 {
972 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
973 	struct nvmet_port *port = to_nvmet_port(item);
974 
975 	nvmet_referral_disable(parent, port);
976 	kfree(port);
977 }
978 
979 static struct configfs_item_operations nvmet_referral_item_ops = {
980 	.release	= nvmet_referral_release,
981 };
982 
983 static const struct config_item_type nvmet_referral_type = {
984 	.ct_owner	= THIS_MODULE,
985 	.ct_attrs	= nvmet_referral_attrs,
986 	.ct_item_ops	= &nvmet_referral_item_ops,
987 };
988 
989 static struct config_group *nvmet_referral_make(
990 		struct config_group *group, const char *name)
991 {
992 	struct nvmet_port *port;
993 
994 	port = kzalloc(sizeof(*port), GFP_KERNEL);
995 	if (!port)
996 		return ERR_PTR(-ENOMEM);
997 
998 	INIT_LIST_HEAD(&port->entry);
999 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1000 
1001 	return &port->group;
1002 }
1003 
1004 static struct configfs_group_operations nvmet_referral_group_ops = {
1005 	.make_group		= nvmet_referral_make,
1006 };
1007 
1008 static const struct config_item_type nvmet_referrals_type = {
1009 	.ct_owner	= THIS_MODULE,
1010 	.ct_group_ops	= &nvmet_referral_group_ops,
1011 };
1012 
1013 static struct {
1014 	enum nvme_ana_state	state;
1015 	const char		*name;
1016 } nvmet_ana_state_names[] = {
1017 	{ NVME_ANA_OPTIMIZED,		"optimized" },
1018 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
1019 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
1020 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
1021 	{ NVME_ANA_CHANGE,		"change" },
1022 };
1023 
1024 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1025 		char *page)
1026 {
1027 	struct nvmet_ana_group *grp = to_ana_group(item);
1028 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1029 	int i;
1030 
1031 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1032 		if (state != nvmet_ana_state_names[i].state)
1033 			continue;
1034 		return sprintf(page, "%s\n", nvmet_ana_state_names[i].name);
1035 	}
1036 
1037 	return sprintf(page, "\n");
1038 }
1039 
1040 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1041 		const char *page, size_t count)
1042 {
1043 	struct nvmet_ana_group *grp = to_ana_group(item);
1044 	int i;
1045 
1046 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
1047 		if (sysfs_streq(page, nvmet_ana_state_names[i].name))
1048 			goto found;
1049 	}
1050 
1051 	pr_err("Invalid value '%s' for ana_state\n", page);
1052 	return -EINVAL;
1053 
1054 found:
1055 	down_write(&nvmet_ana_sem);
1056 	grp->port->ana_state[grp->grpid] = nvmet_ana_state_names[i].state;
1057 	nvmet_ana_chgcnt++;
1058 	up_write(&nvmet_ana_sem);
1059 
1060 	nvmet_port_send_ana_event(grp->port);
1061 	return count;
1062 }
1063 
1064 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1065 
1066 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1067 	&nvmet_ana_group_attr_ana_state,
1068 	NULL,
1069 };
1070 
1071 static void nvmet_ana_group_release(struct config_item *item)
1072 {
1073 	struct nvmet_ana_group *grp = to_ana_group(item);
1074 
1075 	if (grp == &grp->port->ana_default_group)
1076 		return;
1077 
1078 	down_write(&nvmet_ana_sem);
1079 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1080 	nvmet_ana_group_enabled[grp->grpid]--;
1081 	up_write(&nvmet_ana_sem);
1082 
1083 	nvmet_port_send_ana_event(grp->port);
1084 	kfree(grp);
1085 }
1086 
1087 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1088 	.release		= nvmet_ana_group_release,
1089 };
1090 
1091 static const struct config_item_type nvmet_ana_group_type = {
1092 	.ct_item_ops		= &nvmet_ana_group_item_ops,
1093 	.ct_attrs		= nvmet_ana_group_attrs,
1094 	.ct_owner		= THIS_MODULE,
1095 };
1096 
1097 static struct config_group *nvmet_ana_groups_make_group(
1098 		struct config_group *group, const char *name)
1099 {
1100 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1101 	struct nvmet_ana_group *grp;
1102 	u32 grpid;
1103 	int ret;
1104 
1105 	ret = kstrtou32(name, 0, &grpid);
1106 	if (ret)
1107 		goto out;
1108 
1109 	ret = -EINVAL;
1110 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1111 		goto out;
1112 
1113 	ret = -ENOMEM;
1114 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1115 	if (!grp)
1116 		goto out;
1117 	grp->port = port;
1118 	grp->grpid = grpid;
1119 
1120 	down_write(&nvmet_ana_sem);
1121 	nvmet_ana_group_enabled[grpid]++;
1122 	up_write(&nvmet_ana_sem);
1123 
1124 	nvmet_port_send_ana_event(grp->port);
1125 
1126 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1127 	return &grp->group;
1128 out:
1129 	return ERR_PTR(ret);
1130 }
1131 
1132 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1133 	.make_group		= nvmet_ana_groups_make_group,
1134 };
1135 
1136 static const struct config_item_type nvmet_ana_groups_type = {
1137 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
1138 	.ct_owner		= THIS_MODULE,
1139 };
1140 
1141 /*
1142  * Ports definitions.
1143  */
1144 static void nvmet_port_release(struct config_item *item)
1145 {
1146 	struct nvmet_port *port = to_nvmet_port(item);
1147 
1148 	list_del(&port->global_entry);
1149 
1150 	kfree(port->ana_state);
1151 	kfree(port);
1152 }
1153 
1154 static struct configfs_attribute *nvmet_port_attrs[] = {
1155 	&nvmet_attr_addr_adrfam,
1156 	&nvmet_attr_addr_treq,
1157 	&nvmet_attr_addr_traddr,
1158 	&nvmet_attr_addr_trsvcid,
1159 	&nvmet_attr_addr_trtype,
1160 	&nvmet_attr_param_inline_data_size,
1161 	NULL,
1162 };
1163 
1164 static struct configfs_item_operations nvmet_port_item_ops = {
1165 	.release		= nvmet_port_release,
1166 };
1167 
1168 static const struct config_item_type nvmet_port_type = {
1169 	.ct_attrs		= nvmet_port_attrs,
1170 	.ct_item_ops		= &nvmet_port_item_ops,
1171 	.ct_owner		= THIS_MODULE,
1172 };
1173 
1174 static struct config_group *nvmet_ports_make(struct config_group *group,
1175 		const char *name)
1176 {
1177 	struct nvmet_port *port;
1178 	u16 portid;
1179 	u32 i;
1180 
1181 	if (kstrtou16(name, 0, &portid))
1182 		return ERR_PTR(-EINVAL);
1183 
1184 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1185 	if (!port)
1186 		return ERR_PTR(-ENOMEM);
1187 
1188 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1189 			sizeof(*port->ana_state), GFP_KERNEL);
1190 	if (!port->ana_state) {
1191 		kfree(port);
1192 		return ERR_PTR(-ENOMEM);
1193 	}
1194 
1195 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1196 		if (i == NVMET_DEFAULT_ANA_GRPID)
1197 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
1198 		else
1199 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1200 	}
1201 
1202 	list_add(&port->global_entry, &nvmet_ports_list);
1203 
1204 	INIT_LIST_HEAD(&port->entry);
1205 	INIT_LIST_HEAD(&port->subsystems);
1206 	INIT_LIST_HEAD(&port->referrals);
1207 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1208 
1209 	port->disc_addr.portid = cpu_to_le16(portid);
1210 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1211 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1212 
1213 	config_group_init_type_name(&port->subsys_group,
1214 			"subsystems", &nvmet_port_subsys_type);
1215 	configfs_add_default_group(&port->subsys_group, &port->group);
1216 
1217 	config_group_init_type_name(&port->referrals_group,
1218 			"referrals", &nvmet_referrals_type);
1219 	configfs_add_default_group(&port->referrals_group, &port->group);
1220 
1221 	config_group_init_type_name(&port->ana_groups_group,
1222 			"ana_groups", &nvmet_ana_groups_type);
1223 	configfs_add_default_group(&port->ana_groups_group, &port->group);
1224 
1225 	port->ana_default_group.port = port;
1226 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1227 	config_group_init_type_name(&port->ana_default_group.group,
1228 			__stringify(NVMET_DEFAULT_ANA_GRPID),
1229 			&nvmet_ana_group_type);
1230 	configfs_add_default_group(&port->ana_default_group.group,
1231 			&port->ana_groups_group);
1232 
1233 	return &port->group;
1234 }
1235 
1236 static struct configfs_group_operations nvmet_ports_group_ops = {
1237 	.make_group		= nvmet_ports_make,
1238 };
1239 
1240 static const struct config_item_type nvmet_ports_type = {
1241 	.ct_group_ops		= &nvmet_ports_group_ops,
1242 	.ct_owner		= THIS_MODULE,
1243 };
1244 
1245 static struct config_group nvmet_subsystems_group;
1246 static struct config_group nvmet_ports_group;
1247 
1248 static void nvmet_host_release(struct config_item *item)
1249 {
1250 	struct nvmet_host *host = to_host(item);
1251 
1252 	kfree(host);
1253 }
1254 
1255 static struct configfs_item_operations nvmet_host_item_ops = {
1256 	.release		= nvmet_host_release,
1257 };
1258 
1259 static const struct config_item_type nvmet_host_type = {
1260 	.ct_item_ops		= &nvmet_host_item_ops,
1261 	.ct_owner		= THIS_MODULE,
1262 };
1263 
1264 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1265 		const char *name)
1266 {
1267 	struct nvmet_host *host;
1268 
1269 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1270 	if (!host)
1271 		return ERR_PTR(-ENOMEM);
1272 
1273 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1274 
1275 	return &host->group;
1276 }
1277 
1278 static struct configfs_group_operations nvmet_hosts_group_ops = {
1279 	.make_group		= nvmet_hosts_make_group,
1280 };
1281 
1282 static const struct config_item_type nvmet_hosts_type = {
1283 	.ct_group_ops		= &nvmet_hosts_group_ops,
1284 	.ct_owner		= THIS_MODULE,
1285 };
1286 
1287 static struct config_group nvmet_hosts_group;
1288 
1289 static const struct config_item_type nvmet_root_type = {
1290 	.ct_owner		= THIS_MODULE,
1291 };
1292 
1293 static struct configfs_subsystem nvmet_configfs_subsystem = {
1294 	.su_group = {
1295 		.cg_item = {
1296 			.ci_namebuf	= "nvmet",
1297 			.ci_type	= &nvmet_root_type,
1298 		},
1299 	},
1300 };
1301 
1302 int __init nvmet_init_configfs(void)
1303 {
1304 	int ret;
1305 
1306 	config_group_init(&nvmet_configfs_subsystem.su_group);
1307 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1308 
1309 	config_group_init_type_name(&nvmet_subsystems_group,
1310 			"subsystems", &nvmet_subsystems_type);
1311 	configfs_add_default_group(&nvmet_subsystems_group,
1312 			&nvmet_configfs_subsystem.su_group);
1313 
1314 	config_group_init_type_name(&nvmet_ports_group,
1315 			"ports", &nvmet_ports_type);
1316 	configfs_add_default_group(&nvmet_ports_group,
1317 			&nvmet_configfs_subsystem.su_group);
1318 
1319 	config_group_init_type_name(&nvmet_hosts_group,
1320 			"hosts", &nvmet_hosts_type);
1321 	configfs_add_default_group(&nvmet_hosts_group,
1322 			&nvmet_configfs_subsystem.su_group);
1323 
1324 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1325 	if (ret) {
1326 		pr_err("configfs_register_subsystem: %d\n", ret);
1327 		return ret;
1328 	}
1329 
1330 	return 0;
1331 }
1332 
1333 void __exit nvmet_exit_configfs(void)
1334 {
1335 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1336 }
1337