xref: /linux/drivers/nvme/target/configfs.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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/hex.h>
8 #include <linux/kstrtox.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/stat.h>
13 #include <linux/ctype.h>
14 #include <linux/pci.h>
15 #include <linux/pci-p2pdma.h>
16 #ifdef CONFIG_NVME_TARGET_AUTH
17 #include <linux/nvme-auth.h>
18 #endif
19 #include <linux/nvme-keyring.h>
20 #include <crypto/kpp.h>
21 #include <linux/nospec.h>
22 
23 #include "nvmet.h"
24 
25 static const struct config_item_type nvmet_host_type;
26 static const struct config_item_type nvmet_subsys_type;
27 
28 static LIST_HEAD(nvmet_ports_list);
29 struct list_head *nvmet_ports = &nvmet_ports_list;
30 
31 struct nvmet_type_name_map {
32 	u8		type;
33 	const char	*name;
34 };
35 
36 static struct nvmet_type_name_map nvmet_transport[] = {
37 	{ NVMF_TRTYPE_RDMA,	"rdma" },
38 	{ NVMF_TRTYPE_FC,	"fc" },
39 	{ NVMF_TRTYPE_TCP,	"tcp" },
40 	{ NVMF_TRTYPE_PCI,	"pci" },
41 	{ NVMF_TRTYPE_LOOP,	"loop" },
42 };
43 
44 static const struct nvmet_type_name_map nvmet_addr_family[] = {
45 	{ NVMF_ADDR_FAMILY_PCI,		"pcie" },
46 	{ NVMF_ADDR_FAMILY_IP4,		"ipv4" },
47 	{ NVMF_ADDR_FAMILY_IP6,		"ipv6" },
48 	{ NVMF_ADDR_FAMILY_IB,		"ib" },
49 	{ NVMF_ADDR_FAMILY_FC,		"fc" },
50 	{ NVMF_ADDR_FAMILY_PCI,		"pci" },
51 	{ NVMF_ADDR_FAMILY_LOOP,	"loop" },
52 };
53 
54 static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
55 {
56 	if (p->enabled)
57 		pr_err("Disable port '%u' before changing attribute in %s\n",
58 		       le16_to_cpu(p->disc_addr.portid), caller);
59 	return p->enabled;
60 }
61 
62 /*
63  * nvmet_port Generic ConfigFS definitions.
64  * Used in any place in the ConfigFS tree that refers to an address.
65  */
66 static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
67 {
68 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
69 	int i;
70 
71 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
72 		if (nvmet_addr_family[i].type == adrfam)
73 			return snprintf(page, PAGE_SIZE, "%s\n",
74 					nvmet_addr_family[i].name);
75 	}
76 
77 	return snprintf(page, PAGE_SIZE, "\n");
78 }
79 
80 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
81 		const char *page, size_t count)
82 {
83 	struct nvmet_port *port = to_nvmet_port(item);
84 	int i;
85 
86 	if (nvmet_is_port_enabled(port, __func__))
87 		return -EACCES;
88 
89 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
90 		if (sysfs_streq(page, nvmet_addr_family[i].name))
91 			goto found;
92 	}
93 
94 	pr_err("Invalid value '%s' for adrfam\n", page);
95 	return -EINVAL;
96 
97 found:
98 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
99 	return count;
100 }
101 
102 CONFIGFS_ATTR(nvmet_, addr_adrfam);
103 
104 static ssize_t nvmet_addr_portid_show(struct config_item *item,
105 		char *page)
106 {
107 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
108 
109 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
110 }
111 
112 static ssize_t nvmet_addr_portid_store(struct config_item *item,
113 		const char *page, size_t count)
114 {
115 	struct nvmet_port *port = to_nvmet_port(item);
116 	u16 portid = 0;
117 
118 	if (kstrtou16(page, 0, &portid)) {
119 		pr_err("Invalid value '%s' for portid\n", page);
120 		return -EINVAL;
121 	}
122 
123 	if (nvmet_is_port_enabled(port, __func__))
124 		return -EACCES;
125 
126 	port->disc_addr.portid = cpu_to_le16(portid);
127 	return count;
128 }
129 
130 CONFIGFS_ATTR(nvmet_, addr_portid);
131 
132 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
133 		char *page)
134 {
135 	struct nvmet_port *port = to_nvmet_port(item);
136 
137 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
138 }
139 
140 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
141 		const char *page, size_t count)
142 {
143 	struct nvmet_port *port = to_nvmet_port(item);
144 
145 	if (count > NVMF_TRADDR_SIZE) {
146 		pr_err("Invalid value '%s' for traddr\n", page);
147 		return -EINVAL;
148 	}
149 
150 	if (nvmet_is_port_enabled(port, __func__))
151 		return -EACCES;
152 
153 	if (sscanf(page, "%255s", port->disc_addr.traddr) != 1)
154 		return -EINVAL;
155 	return count;
156 }
157 
158 CONFIGFS_ATTR(nvmet_, addr_traddr);
159 
160 static const struct nvmet_type_name_map nvmet_addr_treq[] = {
161 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
162 	{ NVMF_TREQ_REQUIRED,		"required" },
163 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
164 };
165 
166 static inline u8 nvmet_port_disc_addr_treq_mask(struct nvmet_port *port)
167 {
168 	return (port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK);
169 }
170 
171 static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
172 {
173 	u8 treq = nvmet_port_disc_addr_treq_secure_channel(to_nvmet_port(item));
174 	int i;
175 
176 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
177 		if (treq == nvmet_addr_treq[i].type)
178 			return snprintf(page, PAGE_SIZE, "%s\n",
179 					nvmet_addr_treq[i].name);
180 	}
181 
182 	return snprintf(page, PAGE_SIZE, "\n");
183 }
184 
185 static ssize_t nvmet_addr_treq_store(struct config_item *item,
186 		const char *page, size_t count)
187 {
188 	struct nvmet_port *port = to_nvmet_port(item);
189 	u8 treq = nvmet_port_disc_addr_treq_mask(port);
190 	int i;
191 
192 	if (nvmet_is_port_enabled(port, __func__))
193 		return -EACCES;
194 
195 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
196 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
197 			goto found;
198 	}
199 
200 	pr_err("Invalid value '%s' for treq\n", page);
201 	return -EINVAL;
202 
203 found:
204 	if (port->disc_addr.trtype == NVMF_TRTYPE_TCP &&
205 	    port->disc_addr.tsas.tcp.sectype == NVMF_TCP_SECTYPE_TLS13) {
206 		switch (nvmet_addr_treq[i].type) {
207 		case NVMF_TREQ_NOT_SPECIFIED:
208 			pr_debug("treq '%s' not allowed for TLS1.3\n",
209 				 nvmet_addr_treq[i].name);
210 			return -EINVAL;
211 		case NVMF_TREQ_NOT_REQUIRED:
212 			pr_warn("Allow non-TLS connections while TLS1.3 is enabled\n");
213 			break;
214 		default:
215 			break;
216 		}
217 	}
218 	treq |= nvmet_addr_treq[i].type;
219 	port->disc_addr.treq = treq;
220 	return count;
221 }
222 
223 CONFIGFS_ATTR(nvmet_, addr_treq);
224 
225 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
226 		char *page)
227 {
228 	struct nvmet_port *port = to_nvmet_port(item);
229 
230 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
231 }
232 
233 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
234 		const char *page, size_t count)
235 {
236 	struct nvmet_port *port = to_nvmet_port(item);
237 
238 	if (count > NVMF_TRSVCID_SIZE) {
239 		pr_err("Invalid value '%s' for trsvcid\n", page);
240 		return -EINVAL;
241 	}
242 	if (nvmet_is_port_enabled(port, __func__))
243 		return -EACCES;
244 
245 	if (sscanf(page, "%31s", port->disc_addr.trsvcid) != 1)
246 		return -EINVAL;
247 	return count;
248 }
249 
250 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
251 
252 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
253 		char *page)
254 {
255 	struct nvmet_port *port = to_nvmet_port(item);
256 
257 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
258 }
259 
260 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
261 		const char *page, size_t count)
262 {
263 	struct nvmet_port *port = to_nvmet_port(item);
264 	int ret;
265 
266 	if (nvmet_is_port_enabled(port, __func__))
267 		return -EACCES;
268 	ret = kstrtoint(page, 0, &port->inline_data_size);
269 	if (ret) {
270 		pr_err("Invalid value '%s' for inline_data_size\n", page);
271 		return -EINVAL;
272 	}
273 	return count;
274 }
275 
276 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
277 
278 static ssize_t nvmet_param_max_queue_size_show(struct config_item *item,
279 		char *page)
280 {
281 	struct nvmet_port *port = to_nvmet_port(item);
282 
283 	return snprintf(page, PAGE_SIZE, "%d\n", port->max_queue_size);
284 }
285 
286 static ssize_t nvmet_param_max_queue_size_store(struct config_item *item,
287 		const char *page, size_t count)
288 {
289 	struct nvmet_port *port = to_nvmet_port(item);
290 	int ret;
291 
292 	if (nvmet_is_port_enabled(port, __func__))
293 		return -EACCES;
294 	ret = kstrtoint(page, 0, &port->max_queue_size);
295 	if (ret) {
296 		pr_err("Invalid value '%s' for max_queue_size\n", page);
297 		return -EINVAL;
298 	}
299 	return count;
300 }
301 
302 CONFIGFS_ATTR(nvmet_, param_max_queue_size);
303 
304 static ssize_t nvmet_param_mdts_show(struct config_item *item, char *page)
305 {
306 	struct nvmet_port *port = to_nvmet_port(item);
307 
308 	return snprintf(page, PAGE_SIZE, "%d\n", port->mdts);
309 }
310 
311 static ssize_t nvmet_param_mdts_store(struct config_item *item,
312 		const char *page, size_t count)
313 {
314 	struct nvmet_port *port = to_nvmet_port(item);
315 	int ret, mdts;
316 
317 	if (nvmet_is_port_enabled(port, __func__))
318 		return -EACCES;
319 	ret = kstrtoint(page, 0, &mdts);
320 	if (ret || mdts < 0 || mdts > NVMET_MAX_MDTS) {
321 		pr_err("Invalid value '%s' for mdts, should be 0-%d\n",
322 		       page, NVMET_MAX_MDTS);
323 		return -EINVAL;
324 	}
325 	port->mdts = mdts;
326 	return count;
327 }
328 
329 CONFIGFS_ATTR(nvmet_, param_mdts);
330 
331 #ifdef CONFIG_BLK_DEV_INTEGRITY
332 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
333 		char *page)
334 {
335 	struct nvmet_port *port = to_nvmet_port(item);
336 
337 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
338 }
339 
340 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
341 		const char *page, size_t count)
342 {
343 	struct nvmet_port *port = to_nvmet_port(item);
344 	bool val;
345 
346 	if (kstrtobool(page, &val))
347 		return -EINVAL;
348 
349 	if (nvmet_is_port_enabled(port, __func__))
350 		return -EACCES;
351 
352 	port->pi_enable = val;
353 	return count;
354 }
355 
356 CONFIGFS_ATTR(nvmet_, param_pi_enable);
357 #endif
358 
359 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
360 		char *page)
361 {
362 	struct nvmet_port *port = to_nvmet_port(item);
363 	int i;
364 
365 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
366 		if (port->disc_addr.trtype == nvmet_transport[i].type)
367 			return snprintf(page, PAGE_SIZE,
368 					"%s\n", nvmet_transport[i].name);
369 	}
370 
371 	return sprintf(page, "\n");
372 }
373 
374 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
375 {
376 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
377 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
378 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
379 }
380 
381 static void nvmet_port_init_tsas_tcp(struct nvmet_port *port, int sectype)
382 {
383 	port->disc_addr.tsas.tcp.sectype = sectype;
384 }
385 
386 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
387 		const char *page, size_t count)
388 {
389 	struct nvmet_port *port = to_nvmet_port(item);
390 	ssize_t ret = count;
391 	int i;
392 
393 	down_write(&nvmet_config_sem);
394 
395 	if (nvmet_is_port_enabled(port, __func__)) {
396 		ret = -EACCES;
397 		goto out;
398 	}
399 
400 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
401 		if (sysfs_streq(page, nvmet_transport[i].name))
402 			goto found;
403 	}
404 
405 	pr_err("Invalid value '%s' for trtype\n", page);
406 	ret = -EINVAL;
407 	goto out;
408 
409 found:
410 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
411 	port->disc_addr.trtype = nvmet_transport[i].type;
412 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
413 		nvmet_port_init_tsas_rdma(port);
414 	else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP)
415 		nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE);
416 out:
417 	up_write(&nvmet_config_sem);
418 	return ret;
419 }
420 
421 CONFIGFS_ATTR(nvmet_, addr_trtype);
422 
423 static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = {
424 	{ NVMF_TCP_SECTYPE_NONE,	"none" },
425 	{ NVMF_TCP_SECTYPE_TLS13,	"tls1.3" },
426 };
427 
428 static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = {
429 	{ NVMF_RDMA_QPTYPE_CONNECTED,	"connected" },
430 	{ NVMF_RDMA_QPTYPE_DATAGRAM,	"datagram"  },
431 };
432 
433 static ssize_t nvmet_addr_tsas_show(struct config_item *item,
434 		char *page)
435 {
436 	struct nvmet_port *port = to_nvmet_port(item);
437 	int i;
438 
439 	if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
440 		for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
441 			if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type)
442 				return sprintf(page, "%s\n", nvmet_addr_tsas_tcp[i].name);
443 		}
444 	} else if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
445 		for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
446 			if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type)
447 				return sprintf(page, "%s\n", nvmet_addr_tsas_rdma[i].name);
448 		}
449 	}
450 	return sprintf(page, "\n");
451 }
452 
453 static u8 nvmet_addr_tsas_rdma_store(const char *page)
454 {
455 	int i;
456 
457 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
458 		if (sysfs_streq(page, nvmet_addr_tsas_rdma[i].name))
459 			return nvmet_addr_tsas_rdma[i].type;
460 	}
461 	return NVMF_RDMA_QPTYPE_INVALID;
462 }
463 
464 static u8 nvmet_addr_tsas_tcp_store(const char *page)
465 {
466 	int i;
467 
468 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
469 		if (sysfs_streq(page, nvmet_addr_tsas_tcp[i].name))
470 			return nvmet_addr_tsas_tcp[i].type;
471 	}
472 	return NVMF_TCP_SECTYPE_INVALID;
473 }
474 
475 static ssize_t nvmet_addr_tsas_store(struct config_item *item,
476 		const char *page, size_t count)
477 {
478 	struct nvmet_port *port = to_nvmet_port(item);
479 	u8 treq = nvmet_port_disc_addr_treq_mask(port);
480 	u8 sectype, qptype;
481 
482 	if (nvmet_is_port_enabled(port, __func__))
483 		return -EACCES;
484 
485 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
486 		qptype = nvmet_addr_tsas_rdma_store(page);
487 		if (qptype == port->disc_addr.tsas.rdma.qptype)
488 			return count;
489 	} else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
490 		sectype = nvmet_addr_tsas_tcp_store(page);
491 		if (sectype != NVMF_TCP_SECTYPE_INVALID)
492 			goto found;
493 	}
494 
495 	pr_err("Invalid value '%s' for tsas\n", page);
496 	return -EINVAL;
497 
498 found:
499 	if (sectype == NVMF_TCP_SECTYPE_TLS13) {
500 		if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS)) {
501 			pr_err("TLS is not supported\n");
502 			return -EINVAL;
503 		}
504 		if (!port->keyring) {
505 			pr_err("TLS keyring not configured\n");
506 			return -EINVAL;
507 		}
508 	}
509 
510 	nvmet_port_init_tsas_tcp(port, sectype);
511 	/*
512 	 * If TLS is enabled TREQ should be set to 'required' per default
513 	 */
514 	if (sectype == NVMF_TCP_SECTYPE_TLS13) {
515 		u8 sc = nvmet_port_disc_addr_treq_secure_channel(port);
516 
517 		if (sc == NVMF_TREQ_NOT_SPECIFIED)
518 			treq |= NVMF_TREQ_REQUIRED;
519 		else
520 			treq |= sc;
521 	} else {
522 		treq |= NVMF_TREQ_NOT_SPECIFIED;
523 	}
524 	port->disc_addr.treq = treq;
525 	return count;
526 }
527 
528 CONFIGFS_ATTR(nvmet_, addr_tsas);
529 
530 /*
531  * Namespace structures & file operation functions below
532  */
533 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
534 {
535 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
536 }
537 
538 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
539 		const char *page, size_t count)
540 {
541 	struct nvmet_ns *ns = to_nvmet_ns(item);
542 	struct nvmet_subsys *subsys = ns->subsys;
543 	size_t len;
544 	int ret;
545 	char *new_path = NULL;
546 
547 	mutex_lock(&subsys->lock);
548 	ret = -EBUSY;
549 	if (ns->enabled)
550 		goto out_unlock;
551 
552 	ret = -EINVAL;
553 	len = strcspn(page, "\n");
554 	if (!len)
555 		goto out_unlock;
556 
557 	ret = -ENOMEM;
558 	new_path = kmemdup_nul(page, len, GFP_KERNEL);
559 	if (!new_path)
560 		goto out_unlock;
561 
562 	kfree(ns->device_path);
563 	ns->device_path = new_path;
564 
565 	mutex_unlock(&subsys->lock);
566 	return count;
567 
568 out_unlock:
569 	mutex_unlock(&subsys->lock);
570 	return ret;
571 }
572 
573 CONFIGFS_ATTR(nvmet_ns_, device_path);
574 
575 #ifdef CONFIG_PCI_P2PDMA
576 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
577 {
578 	struct nvmet_ns *ns = to_nvmet_ns(item);
579 
580 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
581 }
582 
583 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
584 		const char *page, size_t count)
585 {
586 	struct nvmet_ns *ns = to_nvmet_ns(item);
587 	struct pci_dev *p2p_dev = NULL;
588 	bool use_p2pmem;
589 	int ret = count;
590 	int error;
591 
592 	mutex_lock(&ns->subsys->lock);
593 	if (ns->enabled) {
594 		ret = -EBUSY;
595 		goto out_unlock;
596 	}
597 
598 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
599 	if (error) {
600 		ret = error;
601 		goto out_unlock;
602 	}
603 
604 	ns->use_p2pmem = use_p2pmem;
605 	pci_dev_put(ns->p2p_dev);
606 	ns->p2p_dev = p2p_dev;
607 
608 out_unlock:
609 	mutex_unlock(&ns->subsys->lock);
610 
611 	return ret;
612 }
613 
614 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
615 #endif /* CONFIG_PCI_P2PDMA */
616 
617 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
618 {
619 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
620 }
621 
622 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
623 					  const char *page, size_t count)
624 {
625 	struct nvmet_ns *ns = to_nvmet_ns(item);
626 	struct nvmet_subsys *subsys = ns->subsys;
627 	int ret = 0;
628 
629 	mutex_lock(&subsys->lock);
630 	if (ns->enabled) {
631 		ret = -EBUSY;
632 		goto out_unlock;
633 	}
634 
635 	if (uuid_parse(page, &ns->uuid))
636 		ret = -EINVAL;
637 
638 out_unlock:
639 	mutex_unlock(&subsys->lock);
640 	return ret ? ret : count;
641 }
642 
643 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
644 
645 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
646 {
647 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
648 }
649 
650 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
651 		const char *page, size_t count)
652 {
653 	struct nvmet_ns *ns = to_nvmet_ns(item);
654 	struct nvmet_subsys *subsys = ns->subsys;
655 	u8 nguid[16];
656 	const char *p = page;
657 	int i;
658 	int ret = 0;
659 
660 	mutex_lock(&subsys->lock);
661 	if (ns->enabled) {
662 		ret = -EBUSY;
663 		goto out_unlock;
664 	}
665 
666 	for (i = 0; i < 16; i++) {
667 		if (p + 2 > page + count) {
668 			ret = -EINVAL;
669 			goto out_unlock;
670 		}
671 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
672 			ret = -EINVAL;
673 			goto out_unlock;
674 		}
675 
676 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
677 		p += 2;
678 
679 		if (*p == '-' || *p == ':')
680 			p++;
681 	}
682 
683 	memcpy(&ns->nguid, nguid, sizeof(nguid));
684 out_unlock:
685 	mutex_unlock(&subsys->lock);
686 	return ret ? ret : count;
687 }
688 
689 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
690 
691 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
692 {
693 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
694 }
695 
696 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
697 		const char *page, size_t count)
698 {
699 	struct nvmet_ns *ns = to_nvmet_ns(item);
700 	u32 oldgrpid, newgrpid;
701 	int ret;
702 
703 	ret = kstrtou32(page, 0, &newgrpid);
704 	if (ret)
705 		return ret;
706 
707 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
708 		return -EINVAL;
709 
710 	down_write(&nvmet_ana_sem);
711 	oldgrpid = ns->anagrpid;
712 	newgrpid = array_index_nospec(newgrpid, NVMET_MAX_ANAGRPS);
713 	nvmet_ana_group_enabled[newgrpid]++;
714 	ns->anagrpid = newgrpid;
715 	nvmet_ana_group_enabled[oldgrpid]--;
716 	nvmet_ana_chgcnt++;
717 	up_write(&nvmet_ana_sem);
718 
719 	nvmet_send_ana_event(ns->subsys, NULL);
720 	return count;
721 }
722 
723 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
724 
725 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
726 {
727 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
728 }
729 
730 static ssize_t nvmet_ns_enable_store(struct config_item *item,
731 		const char *page, size_t count)
732 {
733 	struct nvmet_ns *ns = to_nvmet_ns(item);
734 	bool enable;
735 	int ret = 0;
736 
737 	if (kstrtobool(page, &enable))
738 		return -EINVAL;
739 
740 	/*
741 	 * take a global nvmet_config_sem because the disable routine has a
742 	 * window where it releases the subsys-lock, giving a chance to
743 	 * a parallel enable to concurrently execute causing the disable to
744 	 * have a misaccounting of the ns percpu_ref.
745 	 */
746 	down_write(&nvmet_config_sem);
747 	if (enable)
748 		ret = nvmet_ns_enable(ns);
749 	else
750 		nvmet_ns_disable(ns);
751 	up_write(&nvmet_config_sem);
752 
753 	return ret ? ret : count;
754 }
755 
756 CONFIGFS_ATTR(nvmet_ns_, enable);
757 
758 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
759 {
760 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
761 }
762 
763 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
764 		const char *page, size_t count)
765 {
766 	struct nvmet_ns *ns = to_nvmet_ns(item);
767 	bool val;
768 
769 	if (kstrtobool(page, &val))
770 		return -EINVAL;
771 
772 	mutex_lock(&ns->subsys->lock);
773 	if (ns->enabled) {
774 		pr_err("disable ns before setting buffered_io value.\n");
775 		mutex_unlock(&ns->subsys->lock);
776 		return -EINVAL;
777 	}
778 
779 	ns->buffered_io = val;
780 	mutex_unlock(&ns->subsys->lock);
781 	return count;
782 }
783 
784 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
785 
786 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
787 		const char *page, size_t count)
788 {
789 	struct nvmet_ns *ns = to_nvmet_ns(item);
790 	bool val;
791 
792 	if (kstrtobool(page, &val))
793 		return -EINVAL;
794 
795 	if (!val)
796 		return -EINVAL;
797 
798 	mutex_lock(&ns->subsys->lock);
799 	if (!ns->enabled) {
800 		pr_err("enable ns before revalidate.\n");
801 		mutex_unlock(&ns->subsys->lock);
802 		return -EINVAL;
803 	}
804 	if (nvmet_ns_revalidate(ns))
805 		nvmet_ns_changed(ns->subsys, ns->nsid);
806 	mutex_unlock(&ns->subsys->lock);
807 	return count;
808 }
809 
810 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
811 
812 static ssize_t nvmet_ns_resv_enable_show(struct config_item *item, char *page)
813 {
814 	return sysfs_emit(page, "%d\n", to_nvmet_ns(item)->pr.enable);
815 }
816 
817 static ssize_t nvmet_ns_resv_enable_store(struct config_item *item,
818 		const char *page, size_t count)
819 {
820 	struct nvmet_ns *ns = to_nvmet_ns(item);
821 	bool val;
822 
823 	if (kstrtobool(page, &val))
824 		return -EINVAL;
825 
826 	mutex_lock(&ns->subsys->lock);
827 	if (ns->enabled) {
828 		pr_err("the ns:%u is already enabled.\n", ns->nsid);
829 		mutex_unlock(&ns->subsys->lock);
830 		return -EINVAL;
831 	}
832 	ns->pr.enable = val;
833 	mutex_unlock(&ns->subsys->lock);
834 	return count;
835 }
836 CONFIGFS_ATTR(nvmet_ns_, resv_enable);
837 
838 static struct configfs_attribute *nvmet_ns_attrs[] = {
839 	&nvmet_ns_attr_device_path,
840 	&nvmet_ns_attr_device_nguid,
841 	&nvmet_ns_attr_device_uuid,
842 	&nvmet_ns_attr_ana_grpid,
843 	&nvmet_ns_attr_enable,
844 	&nvmet_ns_attr_buffered_io,
845 	&nvmet_ns_attr_revalidate_size,
846 	&nvmet_ns_attr_resv_enable,
847 #ifdef CONFIG_PCI_P2PDMA
848 	&nvmet_ns_attr_p2pmem,
849 #endif
850 	NULL,
851 };
852 
853 static void nvmet_ns_release(struct config_item *item)
854 {
855 	struct nvmet_ns *ns = to_nvmet_ns(item);
856 
857 	nvmet_ns_free(ns);
858 }
859 
860 static struct configfs_item_operations nvmet_ns_item_ops = {
861 	.release		= nvmet_ns_release,
862 };
863 
864 static const struct config_item_type nvmet_ns_type = {
865 	.ct_item_ops		= &nvmet_ns_item_ops,
866 	.ct_attrs		= nvmet_ns_attrs,
867 	.ct_owner		= THIS_MODULE,
868 };
869 
870 static struct config_group *nvmet_ns_make(struct config_group *group,
871 		const char *name)
872 {
873 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
874 	struct nvmet_ns *ns;
875 	int ret;
876 	u32 nsid;
877 
878 	ret = kstrtou32(name, 0, &nsid);
879 	if (ret)
880 		goto out;
881 
882 	ret = -EINVAL;
883 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
884 		pr_err("invalid nsid %#x", nsid);
885 		goto out;
886 	}
887 
888 	ret = -ENOMEM;
889 	ns = nvmet_ns_alloc(subsys, nsid);
890 	if (!ns)
891 		goto out;
892 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
893 
894 	pr_info("adding nsid %u to subsystem %s\n", nsid, subsys->subsysnqn);
895 
896 	return &ns->group;
897 out:
898 	return ERR_PTR(ret);
899 }
900 
901 static struct configfs_group_operations nvmet_namespaces_group_ops = {
902 	.make_group		= nvmet_ns_make,
903 };
904 
905 static const struct config_item_type nvmet_namespaces_type = {
906 	.ct_group_ops		= &nvmet_namespaces_group_ops,
907 	.ct_owner		= THIS_MODULE,
908 };
909 
910 #ifdef CONFIG_NVME_TARGET_PASSTHRU
911 
912 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
913 		char *page)
914 {
915 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
916 	ssize_t ret;
917 
918 	mutex_lock(&subsys->lock);
919 	ret = snprintf(page, PAGE_SIZE, "%s\n",
920 		       subsys->passthru && subsys->passthru->ctrl_path ?
921 		       subsys->passthru->ctrl_path : "");
922 	mutex_unlock(&subsys->lock);
923 	return ret;
924 }
925 
926 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
927 		const char *page, size_t count)
928 {
929 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
930 	struct nvmet_passthru *passthru;
931 	size_t len;
932 	int ret;
933 
934 	mutex_lock(&subsys->lock);
935 
936 	passthru = nvmet_subsys_passthru(subsys);
937 	if (!passthru) {
938 		ret = -ENOMEM;
939 		goto out_unlock;
940 	}
941 
942 	ret = -EBUSY;
943 	if (passthru->ctrl)
944 		goto out_unlock;
945 
946 	ret = -EINVAL;
947 	len = strcspn(page, "\n");
948 	if (!len)
949 		goto out_unlock;
950 
951 	kfree(passthru->ctrl_path);
952 	ret = -ENOMEM;
953 	passthru->ctrl_path = kstrndup(page, len, GFP_KERNEL);
954 	if (!passthru->ctrl_path)
955 		goto out_unlock;
956 
957 	mutex_unlock(&subsys->lock);
958 
959 	return count;
960 out_unlock:
961 	mutex_unlock(&subsys->lock);
962 	return ret;
963 }
964 CONFIGFS_ATTR(nvmet_passthru_, device_path);
965 
966 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
967 		char *page)
968 {
969 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
970 	ssize_t ret;
971 
972 	mutex_lock(&subsys->lock);
973 	ret = sprintf(page, "%d\n",
974 		subsys->passthru && subsys->passthru->ctrl ? 1 : 0);
975 	mutex_unlock(&subsys->lock);
976 	return ret;
977 }
978 
979 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
980 		const char *page, size_t count)
981 {
982 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
983 	bool enable;
984 	int ret = 0;
985 
986 	if (kstrtobool(page, &enable))
987 		return -EINVAL;
988 
989 	if (enable)
990 		ret = nvmet_passthru_ctrl_enable(subsys);
991 	else
992 		nvmet_passthru_ctrl_disable(subsys);
993 
994 	return ret ? ret : count;
995 }
996 CONFIGFS_ATTR(nvmet_passthru_, enable);
997 
998 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
999 		char *page)
1000 {
1001 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
1002 	ssize_t ret;
1003 
1004 	mutex_lock(&subsys->lock);
1005 	ret = sprintf(page, "%u\n", subsys->passthru ?
1006 			subsys->passthru->admin_timeout : 0);
1007 	mutex_unlock(&subsys->lock);
1008 	return ret;
1009 }
1010 
1011 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
1012 		const char *page, size_t count)
1013 {
1014 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
1015 	struct nvmet_passthru *passthru;
1016 	unsigned int timeout;
1017 
1018 	if (kstrtouint(page, 0, &timeout))
1019 		return -EINVAL;
1020 
1021 	mutex_lock(&subsys->lock);
1022 	passthru = nvmet_subsys_passthru(subsys);
1023 	if (!passthru) {
1024 		mutex_unlock(&subsys->lock);
1025 		return -ENOMEM;
1026 	}
1027 	passthru->admin_timeout = timeout;
1028 	mutex_unlock(&subsys->lock);
1029 	return count;
1030 }
1031 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
1032 
1033 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
1034 		char *page)
1035 {
1036 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
1037 	ssize_t ret;
1038 
1039 	mutex_lock(&subsys->lock);
1040 	ret = sprintf(page, "%u\n",
1041 		subsys->passthru ? subsys->passthru->io_timeout : 0);
1042 	mutex_unlock(&subsys->lock);
1043 	return ret;
1044 }
1045 
1046 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
1047 		const char *page, size_t count)
1048 {
1049 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
1050 	struct nvmet_passthru *passthru;
1051 	unsigned int timeout;
1052 
1053 	if (kstrtouint(page, 0, &timeout))
1054 		return -EINVAL;
1055 
1056 	mutex_lock(&subsys->lock);
1057 	passthru = nvmet_subsys_passthru(subsys);
1058 	if (!passthru) {
1059 		mutex_unlock(&subsys->lock);
1060 		return -ENOMEM;
1061 	}
1062 	passthru->io_timeout = timeout;
1063 	mutex_unlock(&subsys->lock);
1064 	return count;
1065 }
1066 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
1067 
1068 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
1069 		char *page)
1070 {
1071 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
1072 	ssize_t ret;
1073 
1074 	mutex_lock(&subsys->lock);
1075 	ret = sprintf(page, "%u\n",
1076 		subsys->passthru ? subsys->passthru->clear_ids : 0);
1077 	mutex_unlock(&subsys->lock);
1078 	return ret;
1079 }
1080 
1081 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
1082 		const char *page, size_t count)
1083 {
1084 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
1085 	struct nvmet_passthru *passthru;
1086 	unsigned int clear_ids;
1087 
1088 	if (kstrtouint(page, 0, &clear_ids))
1089 		return -EINVAL;
1090 
1091 	mutex_lock(&subsys->lock);
1092 	passthru = nvmet_subsys_passthru(subsys);
1093 	if (!passthru) {
1094 		mutex_unlock(&subsys->lock);
1095 		return -ENOMEM;
1096 	}
1097 	passthru->clear_ids = clear_ids;
1098 	mutex_unlock(&subsys->lock);
1099 	return count;
1100 }
1101 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
1102 
1103 static struct configfs_attribute *nvmet_passthru_attrs[] = {
1104 	&nvmet_passthru_attr_device_path,
1105 	&nvmet_passthru_attr_enable,
1106 	&nvmet_passthru_attr_admin_timeout,
1107 	&nvmet_passthru_attr_io_timeout,
1108 	&nvmet_passthru_attr_clear_ids,
1109 	NULL,
1110 };
1111 
1112 static const struct config_item_type nvmet_passthru_type = {
1113 	.ct_attrs		= nvmet_passthru_attrs,
1114 	.ct_owner		= THIS_MODULE,
1115 };
1116 
1117 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
1118 {
1119 	config_group_init_type_name(&subsys->passthru_group,
1120 				    "passthru", &nvmet_passthru_type);
1121 	configfs_add_default_group(&subsys->passthru_group,
1122 				   &subsys->group);
1123 }
1124 
1125 #else /* CONFIG_NVME_TARGET_PASSTHRU */
1126 
1127 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
1128 {
1129 }
1130 
1131 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
1132 
1133 static int nvmet_port_subsys_allow_link(struct config_item *parent,
1134 		struct config_item *target)
1135 {
1136 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
1137 	struct nvmet_subsys *subsys;
1138 	struct nvmet_subsys_link *link, *p;
1139 	int ret;
1140 
1141 	if (target->ci_type != &nvmet_subsys_type) {
1142 		pr_err("can only link subsystems into the subsystems dir.!\n");
1143 		return -EINVAL;
1144 	}
1145 	subsys = to_subsys(target);
1146 	link = kmalloc_obj(*link);
1147 	if (!link)
1148 		return -ENOMEM;
1149 	link->subsys = subsys;
1150 
1151 	down_write(&nvmet_config_sem);
1152 	ret = -EEXIST;
1153 	list_for_each_entry(p, &port->subsystems, entry) {
1154 		if (p->subsys == subsys)
1155 			goto out_free_link;
1156 	}
1157 
1158 	if (list_empty(&port->subsystems)) {
1159 		ret = nvmet_enable_port(port);
1160 		if (ret)
1161 			goto out_free_link;
1162 	}
1163 
1164 	list_add_tail(&link->entry, &port->subsystems);
1165 	nvmet_port_disc_changed(port, subsys);
1166 
1167 	up_write(&nvmet_config_sem);
1168 	return 0;
1169 
1170 out_free_link:
1171 	up_write(&nvmet_config_sem);
1172 	kfree(link);
1173 	return ret;
1174 }
1175 
1176 static void nvmet_port_subsys_drop_link(struct config_item *parent,
1177 		struct config_item *target)
1178 {
1179 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
1180 	struct nvmet_subsys *subsys = to_subsys(target);
1181 	struct nvmet_subsys_link *p;
1182 
1183 	down_write(&nvmet_config_sem);
1184 	list_for_each_entry(p, &port->subsystems, entry) {
1185 		if (p->subsys == subsys)
1186 			goto found;
1187 	}
1188 	up_write(&nvmet_config_sem);
1189 	return;
1190 
1191 found:
1192 	list_del(&p->entry);
1193 	nvmet_port_del_ctrls(port, subsys);
1194 	nvmet_port_disc_changed(port, subsys);
1195 
1196 	if (list_empty(&port->subsystems))
1197 		nvmet_disable_port(port);
1198 	up_write(&nvmet_config_sem);
1199 	kfree(p);
1200 }
1201 
1202 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
1203 	.allow_link		= nvmet_port_subsys_allow_link,
1204 	.drop_link		= nvmet_port_subsys_drop_link,
1205 };
1206 
1207 static const struct config_item_type nvmet_port_subsys_type = {
1208 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
1209 	.ct_owner		= THIS_MODULE,
1210 };
1211 
1212 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
1213 		struct config_item *target)
1214 {
1215 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1216 	struct nvmet_host *host;
1217 	struct nvmet_host_link *link, *p;
1218 	int ret;
1219 
1220 	if (target->ci_type != &nvmet_host_type) {
1221 		pr_err("can only link hosts into the allowed_hosts directory!\n");
1222 		return -EINVAL;
1223 	}
1224 
1225 	host = to_host(target);
1226 	link = kmalloc_obj(*link);
1227 	if (!link)
1228 		return -ENOMEM;
1229 	link->host = host;
1230 
1231 	down_write(&nvmet_config_sem);
1232 	ret = -EINVAL;
1233 	if (subsys->allow_any_host) {
1234 		pr_err("can't add hosts when allow_any_host is set!\n");
1235 		goto out_free_link;
1236 	}
1237 
1238 	ret = -EEXIST;
1239 	list_for_each_entry(p, &subsys->hosts, entry) {
1240 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1241 			goto out_free_link;
1242 	}
1243 	list_add_tail(&link->entry, &subsys->hosts);
1244 	nvmet_subsys_disc_changed(subsys, host);
1245 
1246 	up_write(&nvmet_config_sem);
1247 	return 0;
1248 out_free_link:
1249 	up_write(&nvmet_config_sem);
1250 	kfree(link);
1251 	return ret;
1252 }
1253 
1254 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
1255 		struct config_item *target)
1256 {
1257 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1258 	struct nvmet_host *host = to_host(target);
1259 	struct nvmet_host_link *p;
1260 
1261 	down_write(&nvmet_config_sem);
1262 	list_for_each_entry(p, &subsys->hosts, entry) {
1263 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1264 			goto found;
1265 	}
1266 	up_write(&nvmet_config_sem);
1267 	return;
1268 
1269 found:
1270 	list_del(&p->entry);
1271 	nvmet_subsys_disc_changed(subsys, host);
1272 
1273 	up_write(&nvmet_config_sem);
1274 	kfree(p);
1275 }
1276 
1277 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
1278 	.allow_link		= nvmet_allowed_hosts_allow_link,
1279 	.drop_link		= nvmet_allowed_hosts_drop_link,
1280 };
1281 
1282 static const struct config_item_type nvmet_allowed_hosts_type = {
1283 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
1284 	.ct_owner		= THIS_MODULE,
1285 };
1286 
1287 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
1288 		char *page)
1289 {
1290 	return snprintf(page, PAGE_SIZE, "%d\n",
1291 		to_subsys(item)->allow_any_host);
1292 }
1293 
1294 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
1295 		const char *page, size_t count)
1296 {
1297 	struct nvmet_subsys *subsys = to_subsys(item);
1298 	bool allow_any_host;
1299 	int ret = 0;
1300 
1301 	if (kstrtobool(page, &allow_any_host))
1302 		return -EINVAL;
1303 
1304 	down_write(&nvmet_config_sem);
1305 	if (allow_any_host && !list_empty(&subsys->hosts)) {
1306 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1307 		ret = -EINVAL;
1308 		goto out_unlock;
1309 	}
1310 
1311 	if (subsys->allow_any_host != allow_any_host) {
1312 		subsys->allow_any_host = allow_any_host;
1313 		nvmet_subsys_disc_changed(subsys, NULL);
1314 	}
1315 
1316 out_unlock:
1317 	up_write(&nvmet_config_sem);
1318 	return ret ? ret : count;
1319 }
1320 
1321 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1322 
1323 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1324 					      char *page)
1325 {
1326 	struct nvmet_subsys *subsys = to_subsys(item);
1327 
1328 	if (NVME_TERTIARY(subsys->ver))
1329 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1330 				NVME_MAJOR(subsys->ver),
1331 				NVME_MINOR(subsys->ver),
1332 				NVME_TERTIARY(subsys->ver));
1333 
1334 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1335 			NVME_MAJOR(subsys->ver),
1336 			NVME_MINOR(subsys->ver));
1337 }
1338 
1339 static ssize_t
1340 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1341 		const char *page, size_t count)
1342 {
1343 	int major, minor, tertiary = 0;
1344 	int ret;
1345 
1346 	if (subsys->subsys_discovered) {
1347 		if (NVME_TERTIARY(subsys->ver))
1348 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1349 			       NVME_MAJOR(subsys->ver),
1350 			       NVME_MINOR(subsys->ver),
1351 			       NVME_TERTIARY(subsys->ver));
1352 		else
1353 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
1354 			       NVME_MAJOR(subsys->ver),
1355 			       NVME_MINOR(subsys->ver));
1356 		return -EINVAL;
1357 	}
1358 
1359 	/* passthru subsystems use the underlying controller's version */
1360 	if (nvmet_is_passthru_subsys(subsys))
1361 		return -EINVAL;
1362 
1363 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1364 	if (ret != 2 && ret != 3)
1365 		return -EINVAL;
1366 
1367 	subsys->ver = NVME_VS(major, minor, tertiary);
1368 
1369 	return count;
1370 }
1371 
1372 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1373 					       const char *page, size_t count)
1374 {
1375 	struct nvmet_subsys *subsys = to_subsys(item);
1376 	ssize_t ret;
1377 
1378 	down_write(&nvmet_config_sem);
1379 	mutex_lock(&subsys->lock);
1380 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1381 	mutex_unlock(&subsys->lock);
1382 	up_write(&nvmet_config_sem);
1383 
1384 	return ret;
1385 }
1386 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1387 
1388 /* See Section 1.5 of NVMe 1.4 */
1389 static bool nvmet_is_ascii(const char c)
1390 {
1391 	return c >= 0x20 && c <= 0x7e;
1392 }
1393 
1394 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1395 					     char *page)
1396 {
1397 	struct nvmet_subsys *subsys = to_subsys(item);
1398 
1399 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1400 			NVMET_SN_MAX_SIZE, subsys->serial);
1401 }
1402 
1403 static ssize_t
1404 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1405 		const char *page, size_t count)
1406 {
1407 	int pos, len = strcspn(page, "\n");
1408 
1409 	if (subsys->subsys_discovered) {
1410 		pr_err("Can't set serial number. %s is already assigned\n",
1411 		       subsys->serial);
1412 		return -EINVAL;
1413 	}
1414 
1415 	if (!len || len > NVMET_SN_MAX_SIZE) {
1416 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1417 		       NVMET_SN_MAX_SIZE);
1418 		return -EINVAL;
1419 	}
1420 
1421 	for (pos = 0; pos < len; pos++) {
1422 		if (!nvmet_is_ascii(page[pos])) {
1423 			pr_err("Serial Number must contain only ASCII strings\n");
1424 			return -EINVAL;
1425 		}
1426 	}
1427 
1428 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1429 
1430 	return count;
1431 }
1432 
1433 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1434 					      const char *page, size_t count)
1435 {
1436 	struct nvmet_subsys *subsys = to_subsys(item);
1437 	ssize_t ret;
1438 
1439 	down_write(&nvmet_config_sem);
1440 	mutex_lock(&subsys->lock);
1441 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1442 	mutex_unlock(&subsys->lock);
1443 	up_write(&nvmet_config_sem);
1444 
1445 	return ret;
1446 }
1447 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1448 
1449 static ssize_t nvmet_subsys_attr_tbkas_show(struct config_item *item,
1450 					     char *page)
1451 {
1452 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->tbkas);
1453 }
1454 
1455 static ssize_t nvmet_subsys_attr_tbkas_store(struct config_item *item,
1456 					      const char *page, size_t count)
1457 {
1458 	struct nvmet_subsys *subsys = to_subsys(item);
1459 	struct nvmet_ctrl *ctrl;
1460 	bool tbkas;
1461 
1462 	if (kstrtobool(page, &tbkas))
1463 		return -EINVAL;
1464 
1465 	down_write(&nvmet_config_sem);
1466 	subsys->tbkas = tbkas;
1467 
1468 	/* Force reconnect */
1469 	mutex_lock(&subsys->lock);
1470 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1471 		ctrl->ops->delete_ctrl(ctrl);
1472 	mutex_unlock(&subsys->lock);
1473 	up_write(&nvmet_config_sem);
1474 
1475 	return count;
1476 }
1477 CONFIGFS_ATTR(nvmet_subsys_, attr_tbkas);
1478 
1479 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1480 						 char *page)
1481 {
1482 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1483 }
1484 
1485 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1486 						  const char *page, size_t cnt)
1487 {
1488 	u16 cntlid_min;
1489 
1490 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1491 		return -EINVAL;
1492 
1493 	if (cntlid_min == 0)
1494 		return -EINVAL;
1495 
1496 	down_write(&nvmet_config_sem);
1497 	if (cntlid_min > to_subsys(item)->cntlid_max)
1498 		goto out_unlock;
1499 	to_subsys(item)->cntlid_min = cntlid_min;
1500 	up_write(&nvmet_config_sem);
1501 	return cnt;
1502 
1503 out_unlock:
1504 	up_write(&nvmet_config_sem);
1505 	return -EINVAL;
1506 }
1507 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1508 
1509 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1510 						 char *page)
1511 {
1512 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1513 }
1514 
1515 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1516 						  const char *page, size_t cnt)
1517 {
1518 	u16 cntlid_max;
1519 
1520 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1521 		return -EINVAL;
1522 
1523 	if (cntlid_max == 0)
1524 		return -EINVAL;
1525 
1526 	down_write(&nvmet_config_sem);
1527 	if (cntlid_max < to_subsys(item)->cntlid_min)
1528 		goto out_unlock;
1529 	to_subsys(item)->cntlid_max = cntlid_max;
1530 	up_write(&nvmet_config_sem);
1531 	return cnt;
1532 
1533 out_unlock:
1534 	up_write(&nvmet_config_sem);
1535 	return -EINVAL;
1536 }
1537 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1538 
1539 static ssize_t nvmet_subsys_attr_vendor_id_show(struct config_item *item,
1540 		char *page)
1541 {
1542 	return snprintf(page, PAGE_SIZE, "0x%x\n", to_subsys(item)->vendor_id);
1543 }
1544 
1545 static ssize_t nvmet_subsys_attr_vendor_id_store(struct config_item *item,
1546 		const char *page, size_t count)
1547 {
1548 	u16 vid;
1549 
1550 	if (kstrtou16(page, 0, &vid))
1551 		return -EINVAL;
1552 
1553 	down_write(&nvmet_config_sem);
1554 	to_subsys(item)->vendor_id = vid;
1555 	up_write(&nvmet_config_sem);
1556 	return count;
1557 }
1558 CONFIGFS_ATTR(nvmet_subsys_, attr_vendor_id);
1559 
1560 static ssize_t nvmet_subsys_attr_subsys_vendor_id_show(struct config_item *item,
1561 		char *page)
1562 {
1563 	return snprintf(page, PAGE_SIZE, "0x%x\n",
1564 			to_subsys(item)->subsys_vendor_id);
1565 }
1566 
1567 static ssize_t nvmet_subsys_attr_subsys_vendor_id_store(struct config_item *item,
1568 		const char *page, size_t count)
1569 {
1570 	u16 ssvid;
1571 
1572 	if (kstrtou16(page, 0, &ssvid))
1573 		return -EINVAL;
1574 
1575 	down_write(&nvmet_config_sem);
1576 	to_subsys(item)->subsys_vendor_id = ssvid;
1577 	up_write(&nvmet_config_sem);
1578 	return count;
1579 }
1580 CONFIGFS_ATTR(nvmet_subsys_, attr_subsys_vendor_id);
1581 
1582 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1583 					    char *page)
1584 {
1585 	struct nvmet_subsys *subsys = to_subsys(item);
1586 
1587 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1588 }
1589 
1590 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1591 		const char *page, size_t count)
1592 {
1593 	int pos = 0, len;
1594 	char *val;
1595 
1596 	if (subsys->subsys_discovered) {
1597 		pr_err("Can't set model number. %s is already assigned\n",
1598 		       subsys->model_number);
1599 		return -EINVAL;
1600 	}
1601 
1602 	len = strcspn(page, "\n");
1603 	if (!len)
1604 		return -EINVAL;
1605 
1606 	if (len > NVMET_MN_MAX_SIZE) {
1607 		pr_err("Model number size can not exceed %d Bytes\n",
1608 		       NVMET_MN_MAX_SIZE);
1609 		return -EINVAL;
1610 	}
1611 
1612 	for (pos = 0; pos < len; pos++) {
1613 		if (!nvmet_is_ascii(page[pos]))
1614 			return -EINVAL;
1615 	}
1616 
1617 	val = kmemdup_nul(page, len, GFP_KERNEL);
1618 	if (!val)
1619 		return -ENOMEM;
1620 	kfree(subsys->model_number);
1621 	subsys->model_number = val;
1622 	return count;
1623 }
1624 
1625 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1626 					     const char *page, size_t count)
1627 {
1628 	struct nvmet_subsys *subsys = to_subsys(item);
1629 	ssize_t ret;
1630 
1631 	down_write(&nvmet_config_sem);
1632 	mutex_lock(&subsys->lock);
1633 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1634 	mutex_unlock(&subsys->lock);
1635 	up_write(&nvmet_config_sem);
1636 
1637 	return ret;
1638 }
1639 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1640 
1641 static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
1642 					    char *page)
1643 {
1644 	struct nvmet_subsys *subsys = to_subsys(item);
1645 
1646 	return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
1647 }
1648 
1649 static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
1650 		const char *page, size_t count)
1651 {
1652 	uint32_t val = 0;
1653 	int ret;
1654 
1655 	if (subsys->subsys_discovered) {
1656 		pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
1657 		      subsys->ieee_oui);
1658 		return -EINVAL;
1659 	}
1660 
1661 	ret = kstrtou32(page, 0, &val);
1662 	if (ret < 0)
1663 		return ret;
1664 
1665 	if (val >= 0x1000000)
1666 		return -EINVAL;
1667 
1668 	subsys->ieee_oui = val;
1669 
1670 	return count;
1671 }
1672 
1673 static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
1674 					     const char *page, size_t count)
1675 {
1676 	struct nvmet_subsys *subsys = to_subsys(item);
1677 	ssize_t ret;
1678 
1679 	down_write(&nvmet_config_sem);
1680 	mutex_lock(&subsys->lock);
1681 	ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
1682 	mutex_unlock(&subsys->lock);
1683 	up_write(&nvmet_config_sem);
1684 
1685 	return ret;
1686 }
1687 CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
1688 
1689 static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
1690 					    char *page)
1691 {
1692 	struct nvmet_subsys *subsys = to_subsys(item);
1693 
1694 	return sysfs_emit(page, "%s\n", subsys->firmware_rev);
1695 }
1696 
1697 static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
1698 		const char *page, size_t count)
1699 {
1700 	int pos = 0, len;
1701 	char *val;
1702 
1703 	if (subsys->subsys_discovered) {
1704 		pr_err("Can't set firmware revision. %s is already assigned\n",
1705 		       subsys->firmware_rev);
1706 		return -EINVAL;
1707 	}
1708 
1709 	len = strcspn(page, "\n");
1710 	if (!len)
1711 		return -EINVAL;
1712 
1713 	if (len > NVMET_FR_MAX_SIZE) {
1714 		pr_err("Firmware revision size can not exceed %d Bytes\n",
1715 		       NVMET_FR_MAX_SIZE);
1716 		return -EINVAL;
1717 	}
1718 
1719 	for (pos = 0; pos < len; pos++) {
1720 		if (!nvmet_is_ascii(page[pos]))
1721 			return -EINVAL;
1722 	}
1723 
1724 	val = kmemdup_nul(page, len, GFP_KERNEL);
1725 	if (!val)
1726 		return -ENOMEM;
1727 
1728 	kfree(subsys->firmware_rev);
1729 
1730 	subsys->firmware_rev = val;
1731 
1732 	return count;
1733 }
1734 
1735 static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
1736 					     const char *page, size_t count)
1737 {
1738 	struct nvmet_subsys *subsys = to_subsys(item);
1739 	ssize_t ret;
1740 
1741 	down_write(&nvmet_config_sem);
1742 	mutex_lock(&subsys->lock);
1743 	ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
1744 	mutex_unlock(&subsys->lock);
1745 	up_write(&nvmet_config_sem);
1746 
1747 	return ret;
1748 }
1749 CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
1750 
1751 #ifdef CONFIG_BLK_DEV_INTEGRITY
1752 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1753 						char *page)
1754 {
1755 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1756 }
1757 
1758 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1759 						 const char *page, size_t count)
1760 {
1761 	struct nvmet_subsys *subsys = to_subsys(item);
1762 	bool pi_enable;
1763 
1764 	if (kstrtobool(page, &pi_enable))
1765 		return -EINVAL;
1766 
1767 	subsys->pi_support = pi_enable;
1768 	return count;
1769 }
1770 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1771 #endif
1772 
1773 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
1774 					      char *page)
1775 {
1776 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
1777 }
1778 
1779 static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
1780 					       const char *page, size_t cnt)
1781 {
1782 	struct nvmet_subsys *subsys = to_subsys(item);
1783 	struct nvmet_ctrl *ctrl;
1784 	u16 qid_max;
1785 
1786 	if (sscanf(page, "%hu\n", &qid_max) != 1)
1787 		return -EINVAL;
1788 
1789 	if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
1790 		return -EINVAL;
1791 
1792 	down_write(&nvmet_config_sem);
1793 	subsys->max_qid = qid_max;
1794 
1795 	/* Force reconnect */
1796 	mutex_lock(&subsys->lock);
1797 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1798 		ctrl->ops->delete_ctrl(ctrl);
1799 	mutex_unlock(&subsys->lock);
1800 	up_write(&nvmet_config_sem);
1801 
1802 	return cnt;
1803 }
1804 CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
1805 
1806 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1807 	&nvmet_subsys_attr_attr_allow_any_host,
1808 	&nvmet_subsys_attr_attr_version,
1809 	&nvmet_subsys_attr_attr_serial,
1810 	&nvmet_subsys_attr_attr_cntlid_min,
1811 	&nvmet_subsys_attr_attr_cntlid_max,
1812 	&nvmet_subsys_attr_attr_vendor_id,
1813 	&nvmet_subsys_attr_attr_subsys_vendor_id,
1814 	&nvmet_subsys_attr_attr_model,
1815 	&nvmet_subsys_attr_attr_qid_max,
1816 	&nvmet_subsys_attr_attr_ieee_oui,
1817 	&nvmet_subsys_attr_attr_firmware,
1818 	&nvmet_subsys_attr_attr_tbkas,
1819 #ifdef CONFIG_BLK_DEV_INTEGRITY
1820 	&nvmet_subsys_attr_attr_pi_enable,
1821 #endif
1822 	NULL,
1823 };
1824 
1825 /*
1826  * Subsystem structures & folder operation functions below
1827  */
1828 static void nvmet_subsys_release(struct config_item *item)
1829 {
1830 	struct nvmet_subsys *subsys = to_subsys(item);
1831 
1832 	nvmet_subsys_del_ctrls(subsys);
1833 	nvmet_subsys_put(subsys);
1834 }
1835 
1836 static struct configfs_item_operations nvmet_subsys_item_ops = {
1837 	.release		= nvmet_subsys_release,
1838 };
1839 
1840 static const struct config_item_type nvmet_subsys_type = {
1841 	.ct_item_ops		= &nvmet_subsys_item_ops,
1842 	.ct_attrs		= nvmet_subsys_attrs,
1843 	.ct_owner		= THIS_MODULE,
1844 };
1845 
1846 static struct config_group *nvmet_subsys_make(struct config_group *group,
1847 		const char *name)
1848 {
1849 	struct nvmet_subsys *subsys;
1850 
1851 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1852 		pr_err("can't create discovery subsystem through configfs\n");
1853 		return ERR_PTR(-EINVAL);
1854 	}
1855 
1856 	if (sysfs_streq(name, nvmet_disc_subsys->subsysnqn)) {
1857 		pr_err("can't create subsystem using unique discovery NQN\n");
1858 		return ERR_PTR(-EINVAL);
1859 	}
1860 
1861 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1862 	if (IS_ERR(subsys))
1863 		return ERR_CAST(subsys);
1864 
1865 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1866 
1867 	config_group_init_type_name(&subsys->namespaces_group,
1868 			"namespaces", &nvmet_namespaces_type);
1869 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1870 
1871 	config_group_init_type_name(&subsys->allowed_hosts_group,
1872 			"allowed_hosts", &nvmet_allowed_hosts_type);
1873 	configfs_add_default_group(&subsys->allowed_hosts_group,
1874 			&subsys->group);
1875 
1876 	nvmet_add_passthru_group(subsys);
1877 
1878 	return &subsys->group;
1879 }
1880 
1881 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1882 	.make_group		= nvmet_subsys_make,
1883 };
1884 
1885 static const struct config_item_type nvmet_subsystems_type = {
1886 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1887 	.ct_owner		= THIS_MODULE,
1888 };
1889 
1890 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1891 		char *page)
1892 {
1893 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1894 }
1895 
1896 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1897 		const char *page, size_t count)
1898 {
1899 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1900 	struct nvmet_port *port = to_nvmet_port(item);
1901 	bool enable;
1902 
1903 	if (kstrtobool(page, &enable))
1904 		goto inval;
1905 
1906 	if (enable)
1907 		nvmet_referral_enable(parent, port);
1908 	else
1909 		nvmet_referral_disable(parent, port);
1910 
1911 	return count;
1912 inval:
1913 	pr_err("Invalid value '%s' for enable\n", page);
1914 	return -EINVAL;
1915 }
1916 
1917 CONFIGFS_ATTR(nvmet_referral_, enable);
1918 
1919 /*
1920  * Discovery Service subsystem definitions
1921  */
1922 static struct configfs_attribute *nvmet_referral_attrs[] = {
1923 	&nvmet_attr_addr_adrfam,
1924 	&nvmet_attr_addr_portid,
1925 	&nvmet_attr_addr_treq,
1926 	&nvmet_attr_addr_traddr,
1927 	&nvmet_attr_addr_trsvcid,
1928 	&nvmet_attr_addr_trtype,
1929 	&nvmet_referral_attr_enable,
1930 	NULL,
1931 };
1932 
1933 static void nvmet_referral_notify(struct config_group *group,
1934 		struct config_item *item)
1935 {
1936 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1937 	struct nvmet_port *port = to_nvmet_port(item);
1938 
1939 	nvmet_referral_disable(parent, port);
1940 }
1941 
1942 static void nvmet_referral_release(struct config_item *item)
1943 {
1944 	struct nvmet_port *port = to_nvmet_port(item);
1945 
1946 	kfree(port);
1947 }
1948 
1949 static struct configfs_item_operations nvmet_referral_item_ops = {
1950 	.release	= nvmet_referral_release,
1951 };
1952 
1953 static const struct config_item_type nvmet_referral_type = {
1954 	.ct_owner	= THIS_MODULE,
1955 	.ct_attrs	= nvmet_referral_attrs,
1956 	.ct_item_ops	= &nvmet_referral_item_ops,
1957 };
1958 
1959 static struct config_group *nvmet_referral_make(
1960 		struct config_group *group, const char *name)
1961 {
1962 	struct nvmet_port *port;
1963 
1964 	port = kzalloc_obj(*port);
1965 	if (!port)
1966 		return ERR_PTR(-ENOMEM);
1967 
1968 	INIT_LIST_HEAD(&port->entry);
1969 	port->disc_addr.trtype = NVMF_TRTYPE_MAX;
1970 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1971 
1972 	return &port->group;
1973 }
1974 
1975 static struct configfs_group_operations nvmet_referral_group_ops = {
1976 	.make_group		= nvmet_referral_make,
1977 	.disconnect_notify	= nvmet_referral_notify,
1978 };
1979 
1980 static const struct config_item_type nvmet_referrals_type = {
1981 	.ct_owner	= THIS_MODULE,
1982 	.ct_group_ops	= &nvmet_referral_group_ops,
1983 };
1984 
1985 static struct nvmet_type_name_map nvmet_ana_state[] = {
1986 	{ NVME_ANA_OPTIMIZED,		"optimized" },
1987 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
1988 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
1989 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
1990 	{ NVME_ANA_CHANGE,		"change" },
1991 };
1992 
1993 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1994 		char *page)
1995 {
1996 	struct nvmet_ana_group *grp = to_ana_group(item);
1997 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1998 	int i;
1999 
2000 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
2001 		if (state == nvmet_ana_state[i].type)
2002 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
2003 	}
2004 
2005 	return sprintf(page, "\n");
2006 }
2007 
2008 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
2009 		const char *page, size_t count)
2010 {
2011 	struct nvmet_ana_group *grp = to_ana_group(item);
2012 	enum nvme_ana_state *ana_state = grp->port->ana_state;
2013 	int i;
2014 
2015 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
2016 		if (sysfs_streq(page, nvmet_ana_state[i].name))
2017 			goto found;
2018 	}
2019 
2020 	pr_err("Invalid value '%s' for ana_state\n", page);
2021 	return -EINVAL;
2022 
2023 found:
2024 	down_write(&nvmet_ana_sem);
2025 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
2026 	nvmet_ana_chgcnt++;
2027 	up_write(&nvmet_ana_sem);
2028 	nvmet_port_send_ana_event(grp->port);
2029 	return count;
2030 }
2031 
2032 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
2033 
2034 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
2035 	&nvmet_ana_group_attr_ana_state,
2036 	NULL,
2037 };
2038 
2039 static void nvmet_ana_group_release(struct config_item *item)
2040 {
2041 	struct nvmet_ana_group *grp = to_ana_group(item);
2042 
2043 	if (grp == &grp->port->ana_default_group)
2044 		return;
2045 
2046 	down_write(&nvmet_ana_sem);
2047 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
2048 	nvmet_ana_group_enabled[grp->grpid]--;
2049 	up_write(&nvmet_ana_sem);
2050 
2051 	nvmet_port_send_ana_event(grp->port);
2052 	kfree(grp);
2053 }
2054 
2055 static struct configfs_item_operations nvmet_ana_group_item_ops = {
2056 	.release		= nvmet_ana_group_release,
2057 };
2058 
2059 static const struct config_item_type nvmet_ana_group_type = {
2060 	.ct_item_ops		= &nvmet_ana_group_item_ops,
2061 	.ct_attrs		= nvmet_ana_group_attrs,
2062 	.ct_owner		= THIS_MODULE,
2063 };
2064 
2065 static struct config_group *nvmet_ana_groups_make_group(
2066 		struct config_group *group, const char *name)
2067 {
2068 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
2069 	struct nvmet_ana_group *grp;
2070 	u32 grpid;
2071 	int ret;
2072 
2073 	ret = kstrtou32(name, 0, &grpid);
2074 	if (ret)
2075 		goto out;
2076 
2077 	ret = -EINVAL;
2078 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
2079 		goto out;
2080 
2081 	ret = -ENOMEM;
2082 	grp = kzalloc_obj(*grp);
2083 	if (!grp)
2084 		goto out;
2085 	grp->port = port;
2086 	grp->grpid = grpid;
2087 
2088 	down_write(&nvmet_ana_sem);
2089 	grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
2090 	nvmet_ana_group_enabled[grpid]++;
2091 	up_write(&nvmet_ana_sem);
2092 
2093 	nvmet_port_send_ana_event(grp->port);
2094 
2095 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
2096 	return &grp->group;
2097 out:
2098 	return ERR_PTR(ret);
2099 }
2100 
2101 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
2102 	.make_group		= nvmet_ana_groups_make_group,
2103 };
2104 
2105 static const struct config_item_type nvmet_ana_groups_type = {
2106 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
2107 	.ct_owner		= THIS_MODULE,
2108 };
2109 
2110 /*
2111  * Ports definitions.
2112  */
2113 static void nvmet_port_release(struct config_item *item)
2114 {
2115 	struct nvmet_port *port = to_nvmet_port(item);
2116 
2117 	/* Let inflight controllers teardown complete */
2118 	flush_workqueue(nvmet_wq);
2119 	down_write(&nvmet_config_sem);
2120 	list_del(&port->global_entry);
2121 	up_write(&nvmet_config_sem);
2122 
2123 	key_put(port->keyring);
2124 	kfree(port);
2125 }
2126 
2127 static struct configfs_attribute *nvmet_port_attrs[] = {
2128 	&nvmet_attr_addr_adrfam,
2129 	&nvmet_attr_addr_treq,
2130 	&nvmet_attr_addr_traddr,
2131 	&nvmet_attr_addr_trsvcid,
2132 	&nvmet_attr_addr_trtype,
2133 	&nvmet_attr_addr_tsas,
2134 	&nvmet_attr_param_inline_data_size,
2135 	&nvmet_attr_param_max_queue_size,
2136 	&nvmet_attr_param_mdts,
2137 #ifdef CONFIG_BLK_DEV_INTEGRITY
2138 	&nvmet_attr_param_pi_enable,
2139 #endif
2140 	NULL,
2141 };
2142 
2143 static struct configfs_item_operations nvmet_port_item_ops = {
2144 	.release		= nvmet_port_release,
2145 };
2146 
2147 static const struct config_item_type nvmet_port_type = {
2148 	.ct_attrs		= nvmet_port_attrs,
2149 	.ct_item_ops		= &nvmet_port_item_ops,
2150 	.ct_owner		= THIS_MODULE,
2151 };
2152 
2153 static struct config_group *nvmet_ports_make(struct config_group *group,
2154 		const char *name)
2155 {
2156 	struct nvmet_port *port;
2157 	u16 portid;
2158 	u32 i;
2159 
2160 	if (kstrtou16(name, 0, &portid))
2161 		return ERR_PTR(-EINVAL);
2162 
2163 	port = kzalloc_flex(*port, ana_state, NVMET_MAX_ANAGRPS + 1);
2164 	if (!port)
2165 		return ERR_PTR(-ENOMEM);
2166 
2167 	if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS) && nvme_keyring_id()) {
2168 		port->keyring = key_lookup(nvme_keyring_id());
2169 		if (IS_ERR(port->keyring)) {
2170 			pr_warn("NVMe keyring not available, disabling TLS\n");
2171 			port->keyring = NULL;
2172 		}
2173 	}
2174 
2175 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
2176 		if (i == NVMET_DEFAULT_ANA_GRPID)
2177 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
2178 		else
2179 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
2180 	}
2181 
2182 	INIT_LIST_HEAD(&port->entry);
2183 	INIT_LIST_HEAD(&port->subsystems);
2184 	INIT_LIST_HEAD(&port->referrals);
2185 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
2186 	port->max_queue_size = -1;	/* < 0 == let the transport choose */
2187 	port->mdts = -1;		/* < 0 == let the transport choose */
2188 
2189 	port->disc_addr.trtype = NVMF_TRTYPE_MAX;
2190 	port->disc_addr.portid = cpu_to_le16(portid);
2191 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
2192 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
2193 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
2194 
2195 	config_group_init_type_name(&port->subsys_group,
2196 			"subsystems", &nvmet_port_subsys_type);
2197 	configfs_add_default_group(&port->subsys_group, &port->group);
2198 
2199 	config_group_init_type_name(&port->referrals_group,
2200 			"referrals", &nvmet_referrals_type);
2201 	configfs_add_default_group(&port->referrals_group, &port->group);
2202 
2203 	config_group_init_type_name(&port->ana_groups_group,
2204 			"ana_groups", &nvmet_ana_groups_type);
2205 	configfs_add_default_group(&port->ana_groups_group, &port->group);
2206 
2207 	port->ana_default_group.port = port;
2208 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
2209 	config_group_init_type_name(&port->ana_default_group.group,
2210 			__stringify(NVMET_DEFAULT_ANA_GRPID),
2211 			&nvmet_ana_group_type);
2212 	configfs_add_default_group(&port->ana_default_group.group,
2213 			&port->ana_groups_group);
2214 
2215 	down_write(&nvmet_config_sem);
2216 	list_add(&port->global_entry, &nvmet_ports_list);
2217 	up_write(&nvmet_config_sem);
2218 
2219 	return &port->group;
2220 }
2221 
2222 static struct configfs_group_operations nvmet_ports_group_ops = {
2223 	.make_group		= nvmet_ports_make,
2224 };
2225 
2226 static const struct config_item_type nvmet_ports_type = {
2227 	.ct_group_ops		= &nvmet_ports_group_ops,
2228 	.ct_owner		= THIS_MODULE,
2229 };
2230 
2231 static struct config_group nvmet_subsystems_group;
2232 static struct config_group nvmet_ports_group;
2233 
2234 #ifdef CONFIG_NVME_TARGET_AUTH
2235 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
2236 		char *page)
2237 {
2238 	u8 *dhchap_secret;
2239 	ssize_t ret;
2240 
2241 	down_read(&nvmet_config_sem);
2242 	dhchap_secret = to_host(item)->dhchap_secret;
2243 	if (!dhchap_secret)
2244 		ret = sprintf(page, "\n");
2245 	else
2246 		ret = sprintf(page, "%s\n", dhchap_secret);
2247 	up_read(&nvmet_config_sem);
2248 	return ret;
2249 }
2250 
2251 static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
2252 		const char *page, size_t count)
2253 {
2254 	struct nvmet_host *host = to_host(item);
2255 	int ret;
2256 
2257 	ret = nvmet_auth_set_key(host, page, false);
2258 	/*
2259 	 * Re-authentication is a soft state, so keep the
2260 	 * current authentication valid until the host
2261 	 * requests re-authentication.
2262 	 */
2263 	return ret < 0 ? ret : count;
2264 }
2265 
2266 CONFIGFS_ATTR(nvmet_host_, dhchap_key);
2267 
2268 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
2269 		char *page)
2270 {
2271 	u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
2272 	ssize_t ret;
2273 
2274 	down_read(&nvmet_config_sem);
2275 	dhchap_secret = to_host(item)->dhchap_ctrl_secret;
2276 	if (!dhchap_secret)
2277 		ret = sprintf(page, "\n");
2278 	else
2279 		ret = sprintf(page, "%s\n", dhchap_secret);
2280 	up_read(&nvmet_config_sem);
2281 	return ret;
2282 }
2283 
2284 static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
2285 		const char *page, size_t count)
2286 {
2287 	struct nvmet_host *host = to_host(item);
2288 	int ret;
2289 
2290 	ret = nvmet_auth_set_key(host, page, true);
2291 	/*
2292 	 * Re-authentication is a soft state, so keep the
2293 	 * current authentication valid until the host
2294 	 * requests re-authentication.
2295 	 */
2296 	return ret < 0 ? ret : count;
2297 }
2298 
2299 CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
2300 
2301 static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
2302 		char *page)
2303 {
2304 	struct nvmet_host *host = to_host(item);
2305 	const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
2306 
2307 	return sprintf(page, "%s\n", hash_name ? hash_name : "none");
2308 }
2309 
2310 static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
2311 		const char *page, size_t count)
2312 {
2313 	struct nvmet_host *host = to_host(item);
2314 	u8 hmac_id;
2315 
2316 	hmac_id = nvme_auth_hmac_id(page);
2317 	if (hmac_id == NVME_AUTH_HASH_INVALID)
2318 		return -EINVAL;
2319 	host->dhchap_hash_id = hmac_id;
2320 	return count;
2321 }
2322 
2323 CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
2324 
2325 static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
2326 		char *page)
2327 {
2328 	struct nvmet_host *host = to_host(item);
2329 	const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
2330 
2331 	return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
2332 }
2333 
2334 static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
2335 		const char *page, size_t count)
2336 {
2337 	struct nvmet_host *host = to_host(item);
2338 	int dhgroup_id;
2339 
2340 	dhgroup_id = nvme_auth_dhgroup_id(page);
2341 	if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
2342 		return -EINVAL;
2343 	if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
2344 		const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
2345 
2346 		if (!crypto_has_kpp(kpp, 0, 0))
2347 			return -EINVAL;
2348 	}
2349 	host->dhchap_dhgroup_id = dhgroup_id;
2350 	return count;
2351 }
2352 
2353 CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
2354 
2355 static struct configfs_attribute *nvmet_host_attrs[] = {
2356 	&nvmet_host_attr_dhchap_key,
2357 	&nvmet_host_attr_dhchap_ctrl_key,
2358 	&nvmet_host_attr_dhchap_hash,
2359 	&nvmet_host_attr_dhchap_dhgroup,
2360 	NULL,
2361 };
2362 #endif /* CONFIG_NVME_TARGET_AUTH */
2363 
2364 static void nvmet_host_release(struct config_item *item)
2365 {
2366 	struct nvmet_host *host = to_host(item);
2367 
2368 #ifdef CONFIG_NVME_TARGET_AUTH
2369 	kfree(host->dhchap_secret);
2370 	kfree(host->dhchap_ctrl_secret);
2371 #endif
2372 	kfree(host);
2373 }
2374 
2375 static struct configfs_item_operations nvmet_host_item_ops = {
2376 	.release		= nvmet_host_release,
2377 };
2378 
2379 static const struct config_item_type nvmet_host_type = {
2380 	.ct_item_ops		= &nvmet_host_item_ops,
2381 #ifdef CONFIG_NVME_TARGET_AUTH
2382 	.ct_attrs		= nvmet_host_attrs,
2383 #endif
2384 	.ct_owner		= THIS_MODULE,
2385 };
2386 
2387 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
2388 		const char *name)
2389 {
2390 	struct nvmet_host *host;
2391 
2392 	host = kzalloc_obj(*host);
2393 	if (!host)
2394 		return ERR_PTR(-ENOMEM);
2395 
2396 #ifdef CONFIG_NVME_TARGET_AUTH
2397 	/* Default to SHA256 */
2398 	host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
2399 #endif
2400 
2401 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
2402 
2403 	return &host->group;
2404 }
2405 
2406 static struct configfs_group_operations nvmet_hosts_group_ops = {
2407 	.make_group		= nvmet_hosts_make_group,
2408 };
2409 
2410 static const struct config_item_type nvmet_hosts_type = {
2411 	.ct_group_ops		= &nvmet_hosts_group_ops,
2412 	.ct_owner		= THIS_MODULE,
2413 };
2414 
2415 static struct config_group nvmet_hosts_group;
2416 
2417 static ssize_t nvmet_root_discovery_nqn_show(struct config_item *item,
2418 					     char *page)
2419 {
2420 	return snprintf(page, PAGE_SIZE, "%s\n", nvmet_disc_subsys->subsysnqn);
2421 }
2422 
2423 static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
2424 		const char *page, size_t count)
2425 {
2426 	struct list_head *entry;
2427 	char *old_nqn, *new_nqn;
2428 	size_t len;
2429 
2430 	len = strcspn(page, "\n");
2431 	if (!len || len > NVMF_NQN_FIELD_LEN - 1)
2432 		return -EINVAL;
2433 
2434 	new_nqn = kstrndup(page, len, GFP_KERNEL);
2435 	if (!new_nqn)
2436 		return -ENOMEM;
2437 
2438 	down_write(&nvmet_config_sem);
2439 	list_for_each(entry, &nvmet_subsystems_group.cg_children) {
2440 		struct config_item *item =
2441 			container_of(entry, struct config_item, ci_entry);
2442 
2443 		if (!strncmp(config_item_name(item), page, len)) {
2444 			pr_err("duplicate NQN %s\n", config_item_name(item));
2445 			up_write(&nvmet_config_sem);
2446 			kfree(new_nqn);
2447 			return -EINVAL;
2448 		}
2449 	}
2450 	old_nqn = nvmet_disc_subsys->subsysnqn;
2451 	nvmet_disc_subsys->subsysnqn = new_nqn;
2452 	up_write(&nvmet_config_sem);
2453 
2454 	kfree(old_nqn);
2455 	return len;
2456 }
2457 
2458 CONFIGFS_ATTR(nvmet_root_, discovery_nqn);
2459 
2460 static struct configfs_attribute *nvmet_root_attrs[] = {
2461 	&nvmet_root_attr_discovery_nqn,
2462 	NULL,
2463 };
2464 
2465 static const struct config_item_type nvmet_root_type = {
2466 	.ct_attrs		= nvmet_root_attrs,
2467 	.ct_owner		= THIS_MODULE,
2468 };
2469 
2470 static struct configfs_subsystem nvmet_configfs_subsystem = {
2471 	.su_group = {
2472 		.cg_item = {
2473 			.ci_namebuf	= "nvmet",
2474 			.ci_type	= &nvmet_root_type,
2475 		},
2476 	},
2477 };
2478 
2479 int __init nvmet_init_configfs(void)
2480 {
2481 	int ret;
2482 
2483 	config_group_init(&nvmet_configfs_subsystem.su_group);
2484 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
2485 
2486 	config_group_init_type_name(&nvmet_subsystems_group,
2487 			"subsystems", &nvmet_subsystems_type);
2488 	configfs_add_default_group(&nvmet_subsystems_group,
2489 			&nvmet_configfs_subsystem.su_group);
2490 
2491 	config_group_init_type_name(&nvmet_ports_group,
2492 			"ports", &nvmet_ports_type);
2493 	configfs_add_default_group(&nvmet_ports_group,
2494 			&nvmet_configfs_subsystem.su_group);
2495 
2496 	config_group_init_type_name(&nvmet_hosts_group,
2497 			"hosts", &nvmet_hosts_type);
2498 	configfs_add_default_group(&nvmet_hosts_group,
2499 			&nvmet_configfs_subsystem.su_group);
2500 
2501 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2502 	if (ret) {
2503 		pr_err("configfs_register_subsystem: %d\n", ret);
2504 		return ret;
2505 	}
2506 
2507 	return 0;
2508 }
2509 
2510 void __exit nvmet_exit_configfs(void)
2511 {
2512 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
2513 }
2514