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