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