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