1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Configfs interface for the NVMe target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/stat.h>
11 #include <linux/ctype.h>
12 #include <linux/pci.h>
13 #include <linux/pci-p2pdma.h>
14
15 #include "nvmet.h"
16
17 static const struct config_item_type nvmet_host_type;
18 static const struct config_item_type nvmet_subsys_type;
19
20 static LIST_HEAD(nvmet_ports_list);
21 struct list_head *nvmet_ports = &nvmet_ports_list;
22
23 struct nvmet_type_name_map {
24 u8 type;
25 const char *name;
26 };
27
28 static struct nvmet_type_name_map nvmet_transport[] = {
29 { NVMF_TRTYPE_RDMA, "rdma" },
30 { NVMF_TRTYPE_FC, "fc" },
31 { NVMF_TRTYPE_TCP, "tcp" },
32 { NVMF_TRTYPE_LOOP, "loop" },
33 };
34
35 static const struct nvmet_type_name_map nvmet_addr_family[] = {
36 { NVMF_ADDR_FAMILY_PCI, "pcie" },
37 { NVMF_ADDR_FAMILY_IP4, "ipv4" },
38 { NVMF_ADDR_FAMILY_IP6, "ipv6" },
39 { NVMF_ADDR_FAMILY_IB, "ib" },
40 { NVMF_ADDR_FAMILY_FC, "fc" },
41 { NVMF_ADDR_FAMILY_LOOP, "loop" },
42 };
43
nvmet_is_port_enabled(struct nvmet_port * p,const char * caller)44 static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
45 {
46 if (p->enabled)
47 pr_err("Disable port '%u' before changing attribute in %s\n",
48 le16_to_cpu(p->disc_addr.portid), caller);
49 return p->enabled;
50 }
51
52 /*
53 * nvmet_port Generic ConfigFS definitions.
54 * Used in any place in the ConfigFS tree that refers to an address.
55 */
nvmet_addr_adrfam_show(struct config_item * item,char * page)56 static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
57 {
58 u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
59 int i;
60
61 for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
62 if (nvmet_addr_family[i].type == adrfam)
63 return sprintf(page, "%s\n", nvmet_addr_family[i].name);
64 }
65
66 return sprintf(page, "\n");
67 }
68
nvmet_addr_adrfam_store(struct config_item * item,const char * page,size_t count)69 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
70 const char *page, size_t count)
71 {
72 struct nvmet_port *port = to_nvmet_port(item);
73 int i;
74
75 if (nvmet_is_port_enabled(port, __func__))
76 return -EACCES;
77
78 for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
79 if (sysfs_streq(page, nvmet_addr_family[i].name))
80 goto found;
81 }
82
83 pr_err("Invalid value '%s' for adrfam\n", page);
84 return -EINVAL;
85
86 found:
87 port->disc_addr.adrfam = nvmet_addr_family[i].type;
88 return count;
89 }
90
91 CONFIGFS_ATTR(nvmet_, addr_adrfam);
92
nvmet_addr_portid_show(struct config_item * item,char * page)93 static ssize_t nvmet_addr_portid_show(struct config_item *item,
94 char *page)
95 {
96 struct nvmet_port *port = to_nvmet_port(item);
97
98 return snprintf(page, PAGE_SIZE, "%d\n",
99 le16_to_cpu(port->disc_addr.portid));
100 }
101
nvmet_addr_portid_store(struct config_item * item,const char * page,size_t count)102 static ssize_t nvmet_addr_portid_store(struct config_item *item,
103 const char *page, size_t count)
104 {
105 struct nvmet_port *port = to_nvmet_port(item);
106 u16 portid = 0;
107
108 if (kstrtou16(page, 0, &portid)) {
109 pr_err("Invalid value '%s' for portid\n", page);
110 return -EINVAL;
111 }
112
113 if (nvmet_is_port_enabled(port, __func__))
114 return -EACCES;
115
116 port->disc_addr.portid = cpu_to_le16(portid);
117 return count;
118 }
119
120 CONFIGFS_ATTR(nvmet_, addr_portid);
121
nvmet_addr_traddr_show(struct config_item * item,char * page)122 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
123 char *page)
124 {
125 struct nvmet_port *port = to_nvmet_port(item);
126
127 return snprintf(page, PAGE_SIZE, "%s\n",
128 port->disc_addr.traddr);
129 }
130
nvmet_addr_traddr_store(struct config_item * item,const char * page,size_t count)131 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
132 const char *page, size_t count)
133 {
134 struct nvmet_port *port = to_nvmet_port(item);
135
136 if (count > NVMF_TRADDR_SIZE) {
137 pr_err("Invalid value '%s' for traddr\n", page);
138 return -EINVAL;
139 }
140
141 if (nvmet_is_port_enabled(port, __func__))
142 return -EACCES;
143
144 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
145 return -EINVAL;
146 return count;
147 }
148
149 CONFIGFS_ATTR(nvmet_, addr_traddr);
150
151 static const struct nvmet_type_name_map nvmet_addr_treq[] = {
152 { NVMF_TREQ_NOT_SPECIFIED, "not specified" },
153 { NVMF_TREQ_REQUIRED, "required" },
154 { NVMF_TREQ_NOT_REQUIRED, "not required" },
155 };
156
nvmet_addr_treq_show(struct config_item * item,char * page)157 static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
158 {
159 u8 treq = to_nvmet_port(item)->disc_addr.treq &
160 NVME_TREQ_SECURE_CHANNEL_MASK;
161 int i;
162
163 for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
164 if (treq == nvmet_addr_treq[i].type)
165 return sprintf(page, "%s\n", nvmet_addr_treq[i].name);
166 }
167
168 return sprintf(page, "\n");
169 }
170
nvmet_addr_treq_store(struct config_item * item,const char * page,size_t count)171 static ssize_t nvmet_addr_treq_store(struct config_item *item,
172 const char *page, size_t count)
173 {
174 struct nvmet_port *port = to_nvmet_port(item);
175 u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
176 int i;
177
178 if (nvmet_is_port_enabled(port, __func__))
179 return -EACCES;
180
181 for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
182 if (sysfs_streq(page, nvmet_addr_treq[i].name))
183 goto found;
184 }
185
186 pr_err("Invalid value '%s' for treq\n", page);
187 return -EINVAL;
188
189 found:
190 treq |= nvmet_addr_treq[i].type;
191 port->disc_addr.treq = treq;
192 return count;
193 }
194
195 CONFIGFS_ATTR(nvmet_, addr_treq);
196
nvmet_addr_trsvcid_show(struct config_item * item,char * page)197 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
198 char *page)
199 {
200 struct nvmet_port *port = to_nvmet_port(item);
201
202 return snprintf(page, PAGE_SIZE, "%s\n",
203 port->disc_addr.trsvcid);
204 }
205
nvmet_addr_trsvcid_store(struct config_item * item,const char * page,size_t count)206 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
207 const char *page, size_t count)
208 {
209 struct nvmet_port *port = to_nvmet_port(item);
210
211 if (count > NVMF_TRSVCID_SIZE) {
212 pr_err("Invalid value '%s' for trsvcid\n", page);
213 return -EINVAL;
214 }
215 if (nvmet_is_port_enabled(port, __func__))
216 return -EACCES;
217
218 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
219 return -EINVAL;
220 return count;
221 }
222
223 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
224
nvmet_param_inline_data_size_show(struct config_item * item,char * page)225 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
226 char *page)
227 {
228 struct nvmet_port *port = to_nvmet_port(item);
229
230 return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
231 }
232
nvmet_param_inline_data_size_store(struct config_item * item,const char * page,size_t count)233 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
234 const char *page, size_t count)
235 {
236 struct nvmet_port *port = to_nvmet_port(item);
237 int ret;
238
239 if (nvmet_is_port_enabled(port, __func__))
240 return -EACCES;
241 ret = kstrtoint(page, 0, &port->inline_data_size);
242 if (ret) {
243 pr_err("Invalid value '%s' for inline_data_size\n", page);
244 return -EINVAL;
245 }
246 return count;
247 }
248
249 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
250
251 #ifdef CONFIG_BLK_DEV_INTEGRITY
nvmet_param_pi_enable_show(struct config_item * item,char * page)252 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
253 char *page)
254 {
255 struct nvmet_port *port = to_nvmet_port(item);
256
257 return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
258 }
259
nvmet_param_pi_enable_store(struct config_item * item,const char * page,size_t count)260 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
261 const char *page, size_t count)
262 {
263 struct nvmet_port *port = to_nvmet_port(item);
264 bool val;
265
266 if (strtobool(page, &val))
267 return -EINVAL;
268
269 if (nvmet_is_port_enabled(port, __func__))
270 return -EACCES;
271
272 port->pi_enable = val;
273 return count;
274 }
275
276 CONFIGFS_ATTR(nvmet_, param_pi_enable);
277 #endif
278
nvmet_addr_trtype_show(struct config_item * item,char * page)279 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
280 char *page)
281 {
282 struct nvmet_port *port = to_nvmet_port(item);
283 int i;
284
285 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
286 if (port->disc_addr.trtype == nvmet_transport[i].type)
287 return sprintf(page, "%s\n", nvmet_transport[i].name);
288 }
289
290 return sprintf(page, "\n");
291 }
292
nvmet_port_init_tsas_rdma(struct nvmet_port * port)293 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
294 {
295 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
296 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
297 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
298 }
299
nvmet_addr_trtype_store(struct config_item * item,const char * page,size_t count)300 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
301 const char *page, size_t count)
302 {
303 struct nvmet_port *port = to_nvmet_port(item);
304 int i;
305
306 if (nvmet_is_port_enabled(port, __func__))
307 return -EACCES;
308
309 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
310 if (sysfs_streq(page, nvmet_transport[i].name))
311 goto found;
312 }
313
314 pr_err("Invalid value '%s' for trtype\n", page);
315 return -EINVAL;
316
317 found:
318 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
319 port->disc_addr.trtype = nvmet_transport[i].type;
320 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
321 nvmet_port_init_tsas_rdma(port);
322 return count;
323 }
324
325 CONFIGFS_ATTR(nvmet_, addr_trtype);
326
327 /*
328 * Namespace structures & file operation functions below
329 */
nvmet_ns_device_path_show(struct config_item * item,char * page)330 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
331 {
332 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
333 }
334
nvmet_ns_device_path_store(struct config_item * item,const char * page,size_t count)335 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
336 const char *page, size_t count)
337 {
338 struct nvmet_ns *ns = to_nvmet_ns(item);
339 struct nvmet_subsys *subsys = ns->subsys;
340 size_t len;
341 int ret;
342
343 mutex_lock(&subsys->lock);
344 ret = -EBUSY;
345 if (ns->enabled)
346 goto out_unlock;
347
348 ret = -EINVAL;
349 len = strcspn(page, "\n");
350 if (!len)
351 goto out_unlock;
352
353 kfree(ns->device_path);
354 ret = -ENOMEM;
355 ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
356 if (!ns->device_path)
357 goto out_unlock;
358
359 mutex_unlock(&subsys->lock);
360 return count;
361
362 out_unlock:
363 mutex_unlock(&subsys->lock);
364 return ret;
365 }
366
367 CONFIGFS_ATTR(nvmet_ns_, device_path);
368
369 #ifdef CONFIG_PCI_P2PDMA
nvmet_ns_p2pmem_show(struct config_item * item,char * page)370 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
371 {
372 struct nvmet_ns *ns = to_nvmet_ns(item);
373
374 return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
375 }
376
nvmet_ns_p2pmem_store(struct config_item * item,const char * page,size_t count)377 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
378 const char *page, size_t count)
379 {
380 struct nvmet_ns *ns = to_nvmet_ns(item);
381 struct pci_dev *p2p_dev = NULL;
382 bool use_p2pmem;
383 int ret = count;
384 int error;
385
386 mutex_lock(&ns->subsys->lock);
387 if (ns->enabled) {
388 ret = -EBUSY;
389 goto out_unlock;
390 }
391
392 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
393 if (error) {
394 ret = error;
395 goto out_unlock;
396 }
397
398 ns->use_p2pmem = use_p2pmem;
399 pci_dev_put(ns->p2p_dev);
400 ns->p2p_dev = p2p_dev;
401
402 out_unlock:
403 mutex_unlock(&ns->subsys->lock);
404
405 return ret;
406 }
407
408 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
409 #endif /* CONFIG_PCI_P2PDMA */
410
nvmet_ns_device_uuid_show(struct config_item * item,char * page)411 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
412 {
413 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
414 }
415
nvmet_ns_device_uuid_store(struct config_item * item,const char * page,size_t count)416 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
417 const char *page, size_t count)
418 {
419 struct nvmet_ns *ns = to_nvmet_ns(item);
420 struct nvmet_subsys *subsys = ns->subsys;
421 int ret = 0;
422
423 mutex_lock(&subsys->lock);
424 if (ns->enabled) {
425 ret = -EBUSY;
426 goto out_unlock;
427 }
428
429 if (uuid_parse(page, &ns->uuid))
430 ret = -EINVAL;
431
432 out_unlock:
433 mutex_unlock(&subsys->lock);
434 return ret ? ret : count;
435 }
436
437 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
438
nvmet_ns_device_nguid_show(struct config_item * item,char * page)439 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
440 {
441 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
442 }
443
nvmet_ns_device_nguid_store(struct config_item * item,const char * page,size_t count)444 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
445 const char *page, size_t count)
446 {
447 struct nvmet_ns *ns = to_nvmet_ns(item);
448 struct nvmet_subsys *subsys = ns->subsys;
449 u8 nguid[16];
450 const char *p = page;
451 int i;
452 int ret = 0;
453
454 mutex_lock(&subsys->lock);
455 if (ns->enabled) {
456 ret = -EBUSY;
457 goto out_unlock;
458 }
459
460 for (i = 0; i < 16; i++) {
461 if (p + 2 > page + count) {
462 ret = -EINVAL;
463 goto out_unlock;
464 }
465 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
466 ret = -EINVAL;
467 goto out_unlock;
468 }
469
470 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
471 p += 2;
472
473 if (*p == '-' || *p == ':')
474 p++;
475 }
476
477 memcpy(&ns->nguid, nguid, sizeof(nguid));
478 out_unlock:
479 mutex_unlock(&subsys->lock);
480 return ret ? ret : count;
481 }
482
483 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
484
nvmet_ns_ana_grpid_show(struct config_item * item,char * page)485 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
486 {
487 return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
488 }
489
nvmet_ns_ana_grpid_store(struct config_item * item,const char * page,size_t count)490 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
491 const char *page, size_t count)
492 {
493 struct nvmet_ns *ns = to_nvmet_ns(item);
494 u32 oldgrpid, newgrpid;
495 int ret;
496
497 ret = kstrtou32(page, 0, &newgrpid);
498 if (ret)
499 return ret;
500
501 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
502 return -EINVAL;
503
504 down_write(&nvmet_ana_sem);
505 oldgrpid = ns->anagrpid;
506 nvmet_ana_group_enabled[newgrpid]++;
507 ns->anagrpid = newgrpid;
508 nvmet_ana_group_enabled[oldgrpid]--;
509 nvmet_ana_chgcnt++;
510 up_write(&nvmet_ana_sem);
511
512 nvmet_send_ana_event(ns->subsys, NULL);
513 return count;
514 }
515
516 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
517
nvmet_ns_enable_show(struct config_item * item,char * page)518 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
519 {
520 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
521 }
522
nvmet_ns_enable_store(struct config_item * item,const char * page,size_t count)523 static ssize_t nvmet_ns_enable_store(struct config_item *item,
524 const char *page, size_t count)
525 {
526 struct nvmet_ns *ns = to_nvmet_ns(item);
527 bool enable;
528 int ret = 0;
529
530 if (strtobool(page, &enable))
531 return -EINVAL;
532
533 if (enable)
534 ret = nvmet_ns_enable(ns);
535 else
536 nvmet_ns_disable(ns);
537
538 return ret ? ret : count;
539 }
540
541 CONFIGFS_ATTR(nvmet_ns_, enable);
542
nvmet_ns_buffered_io_show(struct config_item * item,char * page)543 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
544 {
545 return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
546 }
547
nvmet_ns_buffered_io_store(struct config_item * item,const char * page,size_t count)548 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
549 const char *page, size_t count)
550 {
551 struct nvmet_ns *ns = to_nvmet_ns(item);
552 bool val;
553
554 if (strtobool(page, &val))
555 return -EINVAL;
556
557 mutex_lock(&ns->subsys->lock);
558 if (ns->enabled) {
559 pr_err("disable ns before setting buffered_io value.\n");
560 mutex_unlock(&ns->subsys->lock);
561 return -EINVAL;
562 }
563
564 ns->buffered_io = val;
565 mutex_unlock(&ns->subsys->lock);
566 return count;
567 }
568
569 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
570
nvmet_ns_revalidate_size_store(struct config_item * item,const char * page,size_t count)571 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
572 const char *page, size_t count)
573 {
574 struct nvmet_ns *ns = to_nvmet_ns(item);
575 bool val;
576
577 if (strtobool(page, &val))
578 return -EINVAL;
579
580 if (!val)
581 return -EINVAL;
582
583 mutex_lock(&ns->subsys->lock);
584 if (!ns->enabled) {
585 pr_err("enable ns before revalidate.\n");
586 mutex_unlock(&ns->subsys->lock);
587 return -EINVAL;
588 }
589 if (nvmet_ns_revalidate(ns))
590 nvmet_ns_changed(ns->subsys, ns->nsid);
591 mutex_unlock(&ns->subsys->lock);
592 return count;
593 }
594
595 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
596
597 static struct configfs_attribute *nvmet_ns_attrs[] = {
598 &nvmet_ns_attr_device_path,
599 &nvmet_ns_attr_device_nguid,
600 &nvmet_ns_attr_device_uuid,
601 &nvmet_ns_attr_ana_grpid,
602 &nvmet_ns_attr_enable,
603 &nvmet_ns_attr_buffered_io,
604 &nvmet_ns_attr_revalidate_size,
605 #ifdef CONFIG_PCI_P2PDMA
606 &nvmet_ns_attr_p2pmem,
607 #endif
608 NULL,
609 };
610
nvmet_ns_release(struct config_item * item)611 static void nvmet_ns_release(struct config_item *item)
612 {
613 struct nvmet_ns *ns = to_nvmet_ns(item);
614
615 nvmet_ns_free(ns);
616 }
617
618 static struct configfs_item_operations nvmet_ns_item_ops = {
619 .release = nvmet_ns_release,
620 };
621
622 static const struct config_item_type nvmet_ns_type = {
623 .ct_item_ops = &nvmet_ns_item_ops,
624 .ct_attrs = nvmet_ns_attrs,
625 .ct_owner = THIS_MODULE,
626 };
627
nvmet_ns_make(struct config_group * group,const char * name)628 static struct config_group *nvmet_ns_make(struct config_group *group,
629 const char *name)
630 {
631 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
632 struct nvmet_ns *ns;
633 int ret;
634 u32 nsid;
635
636 ret = kstrtou32(name, 0, &nsid);
637 if (ret)
638 goto out;
639
640 ret = -EINVAL;
641 if (nsid == 0 || nsid == NVME_NSID_ALL) {
642 pr_err("invalid nsid %#x", nsid);
643 goto out;
644 }
645
646 ret = -ENOMEM;
647 ns = nvmet_ns_alloc(subsys, nsid);
648 if (!ns)
649 goto out;
650 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
651
652 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
653
654 return &ns->group;
655 out:
656 return ERR_PTR(ret);
657 }
658
659 static struct configfs_group_operations nvmet_namespaces_group_ops = {
660 .make_group = nvmet_ns_make,
661 };
662
663 static const struct config_item_type nvmet_namespaces_type = {
664 .ct_group_ops = &nvmet_namespaces_group_ops,
665 .ct_owner = THIS_MODULE,
666 };
667
668 #ifdef CONFIG_NVME_TARGET_PASSTHRU
669
nvmet_passthru_device_path_show(struct config_item * item,char * page)670 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
671 char *page)
672 {
673 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
674
675 return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
676 }
677
nvmet_passthru_device_path_store(struct config_item * item,const char * page,size_t count)678 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
679 const char *page, size_t count)
680 {
681 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
682 size_t len;
683 int ret;
684
685 mutex_lock(&subsys->lock);
686
687 ret = -EBUSY;
688 if (subsys->passthru_ctrl)
689 goto out_unlock;
690
691 ret = -EINVAL;
692 len = strcspn(page, "\n");
693 if (!len)
694 goto out_unlock;
695
696 kfree(subsys->passthru_ctrl_path);
697 ret = -ENOMEM;
698 subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
699 if (!subsys->passthru_ctrl_path)
700 goto out_unlock;
701
702 mutex_unlock(&subsys->lock);
703
704 return count;
705 out_unlock:
706 mutex_unlock(&subsys->lock);
707 return ret;
708 }
709 CONFIGFS_ATTR(nvmet_passthru_, device_path);
710
nvmet_passthru_enable_show(struct config_item * item,char * page)711 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
712 char *page)
713 {
714 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
715
716 return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
717 }
718
nvmet_passthru_enable_store(struct config_item * item,const char * page,size_t count)719 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
720 const char *page, size_t count)
721 {
722 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
723 bool enable;
724 int ret = 0;
725
726 if (strtobool(page, &enable))
727 return -EINVAL;
728
729 if (enable)
730 ret = nvmet_passthru_ctrl_enable(subsys);
731 else
732 nvmet_passthru_ctrl_disable(subsys);
733
734 return ret ? ret : count;
735 }
736 CONFIGFS_ATTR(nvmet_passthru_, enable);
737
nvmet_passthru_admin_timeout_show(struct config_item * item,char * page)738 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
739 char *page)
740 {
741 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
742 }
743
nvmet_passthru_admin_timeout_store(struct config_item * item,const char * page,size_t count)744 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
745 const char *page, size_t count)
746 {
747 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
748 unsigned int timeout;
749
750 if (kstrtouint(page, 0, &timeout))
751 return -EINVAL;
752 subsys->admin_timeout = timeout;
753 return count;
754 }
755 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
756
nvmet_passthru_io_timeout_show(struct config_item * item,char * page)757 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
758 char *page)
759 {
760 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
761 }
762
nvmet_passthru_io_timeout_store(struct config_item * item,const char * page,size_t count)763 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
764 const char *page, size_t count)
765 {
766 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
767 unsigned int timeout;
768
769 if (kstrtouint(page, 0, &timeout))
770 return -EINVAL;
771 subsys->io_timeout = timeout;
772 return count;
773 }
774 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
775
776 static struct configfs_attribute *nvmet_passthru_attrs[] = {
777 &nvmet_passthru_attr_device_path,
778 &nvmet_passthru_attr_enable,
779 &nvmet_passthru_attr_admin_timeout,
780 &nvmet_passthru_attr_io_timeout,
781 NULL,
782 };
783
784 static const struct config_item_type nvmet_passthru_type = {
785 .ct_attrs = nvmet_passthru_attrs,
786 .ct_owner = THIS_MODULE,
787 };
788
nvmet_add_passthru_group(struct nvmet_subsys * subsys)789 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
790 {
791 config_group_init_type_name(&subsys->passthru_group,
792 "passthru", &nvmet_passthru_type);
793 configfs_add_default_group(&subsys->passthru_group,
794 &subsys->group);
795 }
796
797 #else /* CONFIG_NVME_TARGET_PASSTHRU */
798
nvmet_add_passthru_group(struct nvmet_subsys * subsys)799 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
800 {
801 }
802
803 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
804
nvmet_port_subsys_allow_link(struct config_item * parent,struct config_item * target)805 static int nvmet_port_subsys_allow_link(struct config_item *parent,
806 struct config_item *target)
807 {
808 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
809 struct nvmet_subsys *subsys;
810 struct nvmet_subsys_link *link, *p;
811 int ret;
812
813 if (target->ci_type != &nvmet_subsys_type) {
814 pr_err("can only link subsystems into the subsystems dir.!\n");
815 return -EINVAL;
816 }
817 subsys = to_subsys(target);
818 link = kmalloc(sizeof(*link), GFP_KERNEL);
819 if (!link)
820 return -ENOMEM;
821 link->subsys = subsys;
822
823 down_write(&nvmet_config_sem);
824 ret = -EEXIST;
825 list_for_each_entry(p, &port->subsystems, entry) {
826 if (p->subsys == subsys)
827 goto out_free_link;
828 }
829
830 if (list_empty(&port->subsystems)) {
831 ret = nvmet_enable_port(port);
832 if (ret)
833 goto out_free_link;
834 }
835
836 list_add_tail(&link->entry, &port->subsystems);
837 nvmet_port_disc_changed(port, subsys);
838
839 up_write(&nvmet_config_sem);
840 return 0;
841
842 out_free_link:
843 up_write(&nvmet_config_sem);
844 kfree(link);
845 return ret;
846 }
847
nvmet_port_subsys_drop_link(struct config_item * parent,struct config_item * target)848 static void nvmet_port_subsys_drop_link(struct config_item *parent,
849 struct config_item *target)
850 {
851 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
852 struct nvmet_subsys *subsys = to_subsys(target);
853 struct nvmet_subsys_link *p;
854
855 down_write(&nvmet_config_sem);
856 list_for_each_entry(p, &port->subsystems, entry) {
857 if (p->subsys == subsys)
858 goto found;
859 }
860 up_write(&nvmet_config_sem);
861 return;
862
863 found:
864 list_del(&p->entry);
865 nvmet_port_del_ctrls(port, subsys);
866 nvmet_port_disc_changed(port, subsys);
867
868 if (list_empty(&port->subsystems))
869 nvmet_disable_port(port);
870 up_write(&nvmet_config_sem);
871 kfree(p);
872 }
873
874 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
875 .allow_link = nvmet_port_subsys_allow_link,
876 .drop_link = nvmet_port_subsys_drop_link,
877 };
878
879 static const struct config_item_type nvmet_port_subsys_type = {
880 .ct_item_ops = &nvmet_port_subsys_item_ops,
881 .ct_owner = THIS_MODULE,
882 };
883
nvmet_allowed_hosts_allow_link(struct config_item * parent,struct config_item * target)884 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
885 struct config_item *target)
886 {
887 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
888 struct nvmet_host *host;
889 struct nvmet_host_link *link, *p;
890 int ret;
891
892 if (target->ci_type != &nvmet_host_type) {
893 pr_err("can only link hosts into the allowed_hosts directory!\n");
894 return -EINVAL;
895 }
896
897 host = to_host(target);
898 link = kmalloc(sizeof(*link), GFP_KERNEL);
899 if (!link)
900 return -ENOMEM;
901 link->host = host;
902
903 down_write(&nvmet_config_sem);
904 ret = -EINVAL;
905 if (subsys->allow_any_host) {
906 pr_err("can't add hosts when allow_any_host is set!\n");
907 goto out_free_link;
908 }
909
910 ret = -EEXIST;
911 list_for_each_entry(p, &subsys->hosts, entry) {
912 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
913 goto out_free_link;
914 }
915 list_add_tail(&link->entry, &subsys->hosts);
916 nvmet_subsys_disc_changed(subsys, host);
917
918 up_write(&nvmet_config_sem);
919 return 0;
920 out_free_link:
921 up_write(&nvmet_config_sem);
922 kfree(link);
923 return ret;
924 }
925
nvmet_allowed_hosts_drop_link(struct config_item * parent,struct config_item * target)926 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
927 struct config_item *target)
928 {
929 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
930 struct nvmet_host *host = to_host(target);
931 struct nvmet_host_link *p;
932
933 down_write(&nvmet_config_sem);
934 list_for_each_entry(p, &subsys->hosts, entry) {
935 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
936 goto found;
937 }
938 up_write(&nvmet_config_sem);
939 return;
940
941 found:
942 list_del(&p->entry);
943 nvmet_subsys_disc_changed(subsys, host);
944
945 up_write(&nvmet_config_sem);
946 kfree(p);
947 }
948
949 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
950 .allow_link = nvmet_allowed_hosts_allow_link,
951 .drop_link = nvmet_allowed_hosts_drop_link,
952 };
953
954 static const struct config_item_type nvmet_allowed_hosts_type = {
955 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
956 .ct_owner = THIS_MODULE,
957 };
958
nvmet_subsys_attr_allow_any_host_show(struct config_item * item,char * page)959 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
960 char *page)
961 {
962 return snprintf(page, PAGE_SIZE, "%d\n",
963 to_subsys(item)->allow_any_host);
964 }
965
nvmet_subsys_attr_allow_any_host_store(struct config_item * item,const char * page,size_t count)966 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
967 const char *page, size_t count)
968 {
969 struct nvmet_subsys *subsys = to_subsys(item);
970 bool allow_any_host;
971 int ret = 0;
972
973 if (strtobool(page, &allow_any_host))
974 return -EINVAL;
975
976 down_write(&nvmet_config_sem);
977 if (allow_any_host && !list_empty(&subsys->hosts)) {
978 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
979 ret = -EINVAL;
980 goto out_unlock;
981 }
982
983 if (subsys->allow_any_host != allow_any_host) {
984 subsys->allow_any_host = allow_any_host;
985 nvmet_subsys_disc_changed(subsys, NULL);
986 }
987
988 out_unlock:
989 up_write(&nvmet_config_sem);
990 return ret ? ret : count;
991 }
992
993 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
994
nvmet_subsys_attr_version_show(struct config_item * item,char * page)995 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
996 char *page)
997 {
998 struct nvmet_subsys *subsys = to_subsys(item);
999
1000 if (NVME_TERTIARY(subsys->ver))
1001 return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1002 NVME_MAJOR(subsys->ver),
1003 NVME_MINOR(subsys->ver),
1004 NVME_TERTIARY(subsys->ver));
1005
1006 return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1007 NVME_MAJOR(subsys->ver),
1008 NVME_MINOR(subsys->ver));
1009 }
1010
1011 static ssize_t
nvmet_subsys_attr_version_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)1012 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1013 const char *page, size_t count)
1014 {
1015 int major, minor, tertiary = 0;
1016 int ret;
1017
1018 if (subsys->subsys_discovered) {
1019 if (NVME_TERTIARY(subsys->ver))
1020 pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1021 NVME_MAJOR(subsys->ver),
1022 NVME_MINOR(subsys->ver),
1023 NVME_TERTIARY(subsys->ver));
1024 else
1025 pr_err("Can't set version number. %llu.%llu is already assigned\n",
1026 NVME_MAJOR(subsys->ver),
1027 NVME_MINOR(subsys->ver));
1028 return -EINVAL;
1029 }
1030
1031 /* passthru subsystems use the underlying controller's version */
1032 if (nvmet_is_passthru_subsys(subsys))
1033 return -EINVAL;
1034
1035 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1036 if (ret != 2 && ret != 3)
1037 return -EINVAL;
1038
1039 subsys->ver = NVME_VS(major, minor, tertiary);
1040
1041 return count;
1042 }
1043
nvmet_subsys_attr_version_store(struct config_item * item,const char * page,size_t count)1044 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1045 const char *page, size_t count)
1046 {
1047 struct nvmet_subsys *subsys = to_subsys(item);
1048 ssize_t ret;
1049
1050 down_write(&nvmet_config_sem);
1051 mutex_lock(&subsys->lock);
1052 ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1053 mutex_unlock(&subsys->lock);
1054 up_write(&nvmet_config_sem);
1055
1056 return ret;
1057 }
1058 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1059
1060 /* See Section 1.5 of NVMe 1.4 */
nvmet_is_ascii(const char c)1061 static bool nvmet_is_ascii(const char c)
1062 {
1063 return c >= 0x20 && c <= 0x7e;
1064 }
1065
nvmet_subsys_attr_serial_show(struct config_item * item,char * page)1066 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1067 char *page)
1068 {
1069 struct nvmet_subsys *subsys = to_subsys(item);
1070
1071 return snprintf(page, PAGE_SIZE, "%.*s\n",
1072 NVMET_SN_MAX_SIZE, subsys->serial);
1073 }
1074
1075 static ssize_t
nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)1076 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1077 const char *page, size_t count)
1078 {
1079 int pos, len = strcspn(page, "\n");
1080
1081 if (subsys->subsys_discovered) {
1082 pr_err("Can't set serial number. %s is already assigned\n",
1083 subsys->serial);
1084 return -EINVAL;
1085 }
1086
1087 if (!len || len > NVMET_SN_MAX_SIZE) {
1088 pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1089 NVMET_SN_MAX_SIZE);
1090 return -EINVAL;
1091 }
1092
1093 for (pos = 0; pos < len; pos++) {
1094 if (!nvmet_is_ascii(page[pos])) {
1095 pr_err("Serial Number must contain only ASCII strings\n");
1096 return -EINVAL;
1097 }
1098 }
1099
1100 memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1101
1102 return count;
1103 }
1104
nvmet_subsys_attr_serial_store(struct config_item * item,const char * page,size_t count)1105 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1106 const char *page, size_t count)
1107 {
1108 struct nvmet_subsys *subsys = to_subsys(item);
1109 ssize_t ret;
1110
1111 down_write(&nvmet_config_sem);
1112 mutex_lock(&subsys->lock);
1113 ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1114 mutex_unlock(&subsys->lock);
1115 up_write(&nvmet_config_sem);
1116
1117 return ret;
1118 }
1119 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1120
nvmet_subsys_attr_cntlid_min_show(struct config_item * item,char * page)1121 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1122 char *page)
1123 {
1124 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1125 }
1126
nvmet_subsys_attr_cntlid_min_store(struct config_item * item,const char * page,size_t cnt)1127 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1128 const char *page, size_t cnt)
1129 {
1130 u16 cntlid_min;
1131
1132 if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1133 return -EINVAL;
1134
1135 if (cntlid_min == 0)
1136 return -EINVAL;
1137
1138 down_write(&nvmet_config_sem);
1139 if (cntlid_min >= to_subsys(item)->cntlid_max)
1140 goto out_unlock;
1141 to_subsys(item)->cntlid_min = cntlid_min;
1142 up_write(&nvmet_config_sem);
1143 return cnt;
1144
1145 out_unlock:
1146 up_write(&nvmet_config_sem);
1147 return -EINVAL;
1148 }
1149 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1150
nvmet_subsys_attr_cntlid_max_show(struct config_item * item,char * page)1151 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1152 char *page)
1153 {
1154 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1155 }
1156
nvmet_subsys_attr_cntlid_max_store(struct config_item * item,const char * page,size_t cnt)1157 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1158 const char *page, size_t cnt)
1159 {
1160 u16 cntlid_max;
1161
1162 if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1163 return -EINVAL;
1164
1165 if (cntlid_max == 0)
1166 return -EINVAL;
1167
1168 down_write(&nvmet_config_sem);
1169 if (cntlid_max <= to_subsys(item)->cntlid_min)
1170 goto out_unlock;
1171 to_subsys(item)->cntlid_max = cntlid_max;
1172 up_write(&nvmet_config_sem);
1173 return cnt;
1174
1175 out_unlock:
1176 up_write(&nvmet_config_sem);
1177 return -EINVAL;
1178 }
1179 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1180
nvmet_subsys_attr_model_show(struct config_item * item,char * page)1181 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1182 char *page)
1183 {
1184 struct nvmet_subsys *subsys = to_subsys(item);
1185
1186 return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1187 }
1188
nvmet_subsys_attr_model_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)1189 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1190 const char *page, size_t count)
1191 {
1192 int pos = 0, len;
1193 char *val;
1194
1195 if (subsys->subsys_discovered) {
1196 pr_err("Can't set model number. %s is already assigned\n",
1197 subsys->model_number);
1198 return -EINVAL;
1199 }
1200
1201 len = strcspn(page, "\n");
1202 if (!len)
1203 return -EINVAL;
1204
1205 if (len > NVMET_MN_MAX_SIZE) {
1206 pr_err("Model number size can not exceed %d Bytes\n",
1207 NVMET_MN_MAX_SIZE);
1208 return -EINVAL;
1209 }
1210
1211 for (pos = 0; pos < len; pos++) {
1212 if (!nvmet_is_ascii(page[pos]))
1213 return -EINVAL;
1214 }
1215
1216 val = kmemdup_nul(page, len, GFP_KERNEL);
1217 if (!val)
1218 return -ENOMEM;
1219 kfree(subsys->model_number);
1220 subsys->model_number = val;
1221 return count;
1222 }
1223
nvmet_subsys_attr_model_store(struct config_item * item,const char * page,size_t count)1224 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1225 const char *page, size_t count)
1226 {
1227 struct nvmet_subsys *subsys = to_subsys(item);
1228 ssize_t ret;
1229
1230 down_write(&nvmet_config_sem);
1231 mutex_lock(&subsys->lock);
1232 ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1233 mutex_unlock(&subsys->lock);
1234 up_write(&nvmet_config_sem);
1235
1236 return ret;
1237 }
1238 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1239
1240 #ifdef CONFIG_BLK_DEV_INTEGRITY
nvmet_subsys_attr_pi_enable_show(struct config_item * item,char * page)1241 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1242 char *page)
1243 {
1244 return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1245 }
1246
nvmet_subsys_attr_pi_enable_store(struct config_item * item,const char * page,size_t count)1247 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1248 const char *page, size_t count)
1249 {
1250 struct nvmet_subsys *subsys = to_subsys(item);
1251 bool pi_enable;
1252
1253 if (strtobool(page, &pi_enable))
1254 return -EINVAL;
1255
1256 subsys->pi_support = pi_enable;
1257 return count;
1258 }
1259 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1260 #endif
1261
1262 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1263 &nvmet_subsys_attr_attr_allow_any_host,
1264 &nvmet_subsys_attr_attr_version,
1265 &nvmet_subsys_attr_attr_serial,
1266 &nvmet_subsys_attr_attr_cntlid_min,
1267 &nvmet_subsys_attr_attr_cntlid_max,
1268 &nvmet_subsys_attr_attr_model,
1269 #ifdef CONFIG_BLK_DEV_INTEGRITY
1270 &nvmet_subsys_attr_attr_pi_enable,
1271 #endif
1272 NULL,
1273 };
1274
1275 /*
1276 * Subsystem structures & folder operation functions below
1277 */
nvmet_subsys_release(struct config_item * item)1278 static void nvmet_subsys_release(struct config_item *item)
1279 {
1280 struct nvmet_subsys *subsys = to_subsys(item);
1281
1282 nvmet_subsys_del_ctrls(subsys);
1283 nvmet_subsys_put(subsys);
1284 }
1285
1286 static struct configfs_item_operations nvmet_subsys_item_ops = {
1287 .release = nvmet_subsys_release,
1288 };
1289
1290 static const struct config_item_type nvmet_subsys_type = {
1291 .ct_item_ops = &nvmet_subsys_item_ops,
1292 .ct_attrs = nvmet_subsys_attrs,
1293 .ct_owner = THIS_MODULE,
1294 };
1295
nvmet_subsys_make(struct config_group * group,const char * name)1296 static struct config_group *nvmet_subsys_make(struct config_group *group,
1297 const char *name)
1298 {
1299 struct nvmet_subsys *subsys;
1300
1301 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1302 pr_err("can't create discovery subsystem through configfs\n");
1303 return ERR_PTR(-EINVAL);
1304 }
1305
1306 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1307 if (IS_ERR(subsys))
1308 return ERR_CAST(subsys);
1309
1310 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1311
1312 config_group_init_type_name(&subsys->namespaces_group,
1313 "namespaces", &nvmet_namespaces_type);
1314 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1315
1316 config_group_init_type_name(&subsys->allowed_hosts_group,
1317 "allowed_hosts", &nvmet_allowed_hosts_type);
1318 configfs_add_default_group(&subsys->allowed_hosts_group,
1319 &subsys->group);
1320
1321 nvmet_add_passthru_group(subsys);
1322
1323 return &subsys->group;
1324 }
1325
1326 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1327 .make_group = nvmet_subsys_make,
1328 };
1329
1330 static const struct config_item_type nvmet_subsystems_type = {
1331 .ct_group_ops = &nvmet_subsystems_group_ops,
1332 .ct_owner = THIS_MODULE,
1333 };
1334
nvmet_referral_enable_show(struct config_item * item,char * page)1335 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1336 char *page)
1337 {
1338 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1339 }
1340
nvmet_referral_enable_store(struct config_item * item,const char * page,size_t count)1341 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1342 const char *page, size_t count)
1343 {
1344 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1345 struct nvmet_port *port = to_nvmet_port(item);
1346 bool enable;
1347
1348 if (strtobool(page, &enable))
1349 goto inval;
1350
1351 if (enable)
1352 nvmet_referral_enable(parent, port);
1353 else
1354 nvmet_referral_disable(parent, port);
1355
1356 return count;
1357 inval:
1358 pr_err("Invalid value '%s' for enable\n", page);
1359 return -EINVAL;
1360 }
1361
1362 CONFIGFS_ATTR(nvmet_referral_, enable);
1363
1364 /*
1365 * Discovery Service subsystem definitions
1366 */
1367 static struct configfs_attribute *nvmet_referral_attrs[] = {
1368 &nvmet_attr_addr_adrfam,
1369 &nvmet_attr_addr_portid,
1370 &nvmet_attr_addr_treq,
1371 &nvmet_attr_addr_traddr,
1372 &nvmet_attr_addr_trsvcid,
1373 &nvmet_attr_addr_trtype,
1374 &nvmet_referral_attr_enable,
1375 NULL,
1376 };
1377
nvmet_referral_notify(struct config_group * group,struct config_item * item)1378 static void nvmet_referral_notify(struct config_group *group,
1379 struct config_item *item)
1380 {
1381 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1382 struct nvmet_port *port = to_nvmet_port(item);
1383
1384 nvmet_referral_disable(parent, port);
1385 }
1386
nvmet_referral_release(struct config_item * item)1387 static void nvmet_referral_release(struct config_item *item)
1388 {
1389 struct nvmet_port *port = to_nvmet_port(item);
1390
1391 kfree(port);
1392 }
1393
1394 static struct configfs_item_operations nvmet_referral_item_ops = {
1395 .release = nvmet_referral_release,
1396 };
1397
1398 static const struct config_item_type nvmet_referral_type = {
1399 .ct_owner = THIS_MODULE,
1400 .ct_attrs = nvmet_referral_attrs,
1401 .ct_item_ops = &nvmet_referral_item_ops,
1402 };
1403
nvmet_referral_make(struct config_group * group,const char * name)1404 static struct config_group *nvmet_referral_make(
1405 struct config_group *group, const char *name)
1406 {
1407 struct nvmet_port *port;
1408
1409 port = kzalloc(sizeof(*port), GFP_KERNEL);
1410 if (!port)
1411 return ERR_PTR(-ENOMEM);
1412
1413 INIT_LIST_HEAD(&port->entry);
1414 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1415
1416 return &port->group;
1417 }
1418
1419 static struct configfs_group_operations nvmet_referral_group_ops = {
1420 .make_group = nvmet_referral_make,
1421 .disconnect_notify = nvmet_referral_notify,
1422 };
1423
1424 static const struct config_item_type nvmet_referrals_type = {
1425 .ct_owner = THIS_MODULE,
1426 .ct_group_ops = &nvmet_referral_group_ops,
1427 };
1428
1429 static struct nvmet_type_name_map nvmet_ana_state[] = {
1430 { NVME_ANA_OPTIMIZED, "optimized" },
1431 { NVME_ANA_NONOPTIMIZED, "non-optimized" },
1432 { NVME_ANA_INACCESSIBLE, "inaccessible" },
1433 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
1434 { NVME_ANA_CHANGE, "change" },
1435 };
1436
nvmet_ana_group_ana_state_show(struct config_item * item,char * page)1437 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1438 char *page)
1439 {
1440 struct nvmet_ana_group *grp = to_ana_group(item);
1441 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1442 int i;
1443
1444 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1445 if (state == nvmet_ana_state[i].type)
1446 return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1447 }
1448
1449 return sprintf(page, "\n");
1450 }
1451
nvmet_ana_group_ana_state_store(struct config_item * item,const char * page,size_t count)1452 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1453 const char *page, size_t count)
1454 {
1455 struct nvmet_ana_group *grp = to_ana_group(item);
1456 enum nvme_ana_state *ana_state = grp->port->ana_state;
1457 int i;
1458
1459 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1460 if (sysfs_streq(page, nvmet_ana_state[i].name))
1461 goto found;
1462 }
1463
1464 pr_err("Invalid value '%s' for ana_state\n", page);
1465 return -EINVAL;
1466
1467 found:
1468 down_write(&nvmet_ana_sem);
1469 ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1470 nvmet_ana_chgcnt++;
1471 up_write(&nvmet_ana_sem);
1472 nvmet_port_send_ana_event(grp->port);
1473 return count;
1474 }
1475
1476 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1477
1478 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1479 &nvmet_ana_group_attr_ana_state,
1480 NULL,
1481 };
1482
nvmet_ana_group_release(struct config_item * item)1483 static void nvmet_ana_group_release(struct config_item *item)
1484 {
1485 struct nvmet_ana_group *grp = to_ana_group(item);
1486
1487 if (grp == &grp->port->ana_default_group)
1488 return;
1489
1490 down_write(&nvmet_ana_sem);
1491 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1492 nvmet_ana_group_enabled[grp->grpid]--;
1493 up_write(&nvmet_ana_sem);
1494
1495 nvmet_port_send_ana_event(grp->port);
1496 kfree(grp);
1497 }
1498
1499 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1500 .release = nvmet_ana_group_release,
1501 };
1502
1503 static const struct config_item_type nvmet_ana_group_type = {
1504 .ct_item_ops = &nvmet_ana_group_item_ops,
1505 .ct_attrs = nvmet_ana_group_attrs,
1506 .ct_owner = THIS_MODULE,
1507 };
1508
nvmet_ana_groups_make_group(struct config_group * group,const char * name)1509 static struct config_group *nvmet_ana_groups_make_group(
1510 struct config_group *group, const char *name)
1511 {
1512 struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1513 struct nvmet_ana_group *grp;
1514 u32 grpid;
1515 int ret;
1516
1517 ret = kstrtou32(name, 0, &grpid);
1518 if (ret)
1519 goto out;
1520
1521 ret = -EINVAL;
1522 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1523 goto out;
1524
1525 ret = -ENOMEM;
1526 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1527 if (!grp)
1528 goto out;
1529 grp->port = port;
1530 grp->grpid = grpid;
1531
1532 down_write(&nvmet_ana_sem);
1533 nvmet_ana_group_enabled[grpid]++;
1534 up_write(&nvmet_ana_sem);
1535
1536 nvmet_port_send_ana_event(grp->port);
1537
1538 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1539 return &grp->group;
1540 out:
1541 return ERR_PTR(ret);
1542 }
1543
1544 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1545 .make_group = nvmet_ana_groups_make_group,
1546 };
1547
1548 static const struct config_item_type nvmet_ana_groups_type = {
1549 .ct_group_ops = &nvmet_ana_groups_group_ops,
1550 .ct_owner = THIS_MODULE,
1551 };
1552
1553 /*
1554 * Ports definitions.
1555 */
nvmet_port_release(struct config_item * item)1556 static void nvmet_port_release(struct config_item *item)
1557 {
1558 struct nvmet_port *port = to_nvmet_port(item);
1559
1560 /* Let inflight controllers teardown complete */
1561 flush_workqueue(nvmet_wq);
1562 list_del(&port->global_entry);
1563
1564 kfree(port->ana_state);
1565 kfree(port);
1566 }
1567
1568 static struct configfs_attribute *nvmet_port_attrs[] = {
1569 &nvmet_attr_addr_adrfam,
1570 &nvmet_attr_addr_treq,
1571 &nvmet_attr_addr_traddr,
1572 &nvmet_attr_addr_trsvcid,
1573 &nvmet_attr_addr_trtype,
1574 &nvmet_attr_param_inline_data_size,
1575 #ifdef CONFIG_BLK_DEV_INTEGRITY
1576 &nvmet_attr_param_pi_enable,
1577 #endif
1578 NULL,
1579 };
1580
1581 static struct configfs_item_operations nvmet_port_item_ops = {
1582 .release = nvmet_port_release,
1583 };
1584
1585 static const struct config_item_type nvmet_port_type = {
1586 .ct_attrs = nvmet_port_attrs,
1587 .ct_item_ops = &nvmet_port_item_ops,
1588 .ct_owner = THIS_MODULE,
1589 };
1590
nvmet_ports_make(struct config_group * group,const char * name)1591 static struct config_group *nvmet_ports_make(struct config_group *group,
1592 const char *name)
1593 {
1594 struct nvmet_port *port;
1595 u16 portid;
1596 u32 i;
1597
1598 if (kstrtou16(name, 0, &portid))
1599 return ERR_PTR(-EINVAL);
1600
1601 port = kzalloc(sizeof(*port), GFP_KERNEL);
1602 if (!port)
1603 return ERR_PTR(-ENOMEM);
1604
1605 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1606 sizeof(*port->ana_state), GFP_KERNEL);
1607 if (!port->ana_state) {
1608 kfree(port);
1609 return ERR_PTR(-ENOMEM);
1610 }
1611
1612 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1613 if (i == NVMET_DEFAULT_ANA_GRPID)
1614 port->ana_state[1] = NVME_ANA_OPTIMIZED;
1615 else
1616 port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1617 }
1618
1619 list_add(&port->global_entry, &nvmet_ports_list);
1620
1621 INIT_LIST_HEAD(&port->entry);
1622 INIT_LIST_HEAD(&port->subsystems);
1623 INIT_LIST_HEAD(&port->referrals);
1624 port->inline_data_size = -1; /* < 0 == let the transport choose */
1625
1626 port->disc_addr.portid = cpu_to_le16(portid);
1627 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1628 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1629 config_group_init_type_name(&port->group, name, &nvmet_port_type);
1630
1631 config_group_init_type_name(&port->subsys_group,
1632 "subsystems", &nvmet_port_subsys_type);
1633 configfs_add_default_group(&port->subsys_group, &port->group);
1634
1635 config_group_init_type_name(&port->referrals_group,
1636 "referrals", &nvmet_referrals_type);
1637 configfs_add_default_group(&port->referrals_group, &port->group);
1638
1639 config_group_init_type_name(&port->ana_groups_group,
1640 "ana_groups", &nvmet_ana_groups_type);
1641 configfs_add_default_group(&port->ana_groups_group, &port->group);
1642
1643 port->ana_default_group.port = port;
1644 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1645 config_group_init_type_name(&port->ana_default_group.group,
1646 __stringify(NVMET_DEFAULT_ANA_GRPID),
1647 &nvmet_ana_group_type);
1648 configfs_add_default_group(&port->ana_default_group.group,
1649 &port->ana_groups_group);
1650
1651 return &port->group;
1652 }
1653
1654 static struct configfs_group_operations nvmet_ports_group_ops = {
1655 .make_group = nvmet_ports_make,
1656 };
1657
1658 static const struct config_item_type nvmet_ports_type = {
1659 .ct_group_ops = &nvmet_ports_group_ops,
1660 .ct_owner = THIS_MODULE,
1661 };
1662
1663 static struct config_group nvmet_subsystems_group;
1664 static struct config_group nvmet_ports_group;
1665
nvmet_host_release(struct config_item * item)1666 static void nvmet_host_release(struct config_item *item)
1667 {
1668 struct nvmet_host *host = to_host(item);
1669
1670 kfree(host);
1671 }
1672
1673 static struct configfs_item_operations nvmet_host_item_ops = {
1674 .release = nvmet_host_release,
1675 };
1676
1677 static const struct config_item_type nvmet_host_type = {
1678 .ct_item_ops = &nvmet_host_item_ops,
1679 .ct_owner = THIS_MODULE,
1680 };
1681
nvmet_hosts_make_group(struct config_group * group,const char * name)1682 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1683 const char *name)
1684 {
1685 struct nvmet_host *host;
1686
1687 host = kzalloc(sizeof(*host), GFP_KERNEL);
1688 if (!host)
1689 return ERR_PTR(-ENOMEM);
1690
1691 config_group_init_type_name(&host->group, name, &nvmet_host_type);
1692
1693 return &host->group;
1694 }
1695
1696 static struct configfs_group_operations nvmet_hosts_group_ops = {
1697 .make_group = nvmet_hosts_make_group,
1698 };
1699
1700 static const struct config_item_type nvmet_hosts_type = {
1701 .ct_group_ops = &nvmet_hosts_group_ops,
1702 .ct_owner = THIS_MODULE,
1703 };
1704
1705 static struct config_group nvmet_hosts_group;
1706
1707 static const struct config_item_type nvmet_root_type = {
1708 .ct_owner = THIS_MODULE,
1709 };
1710
1711 static struct configfs_subsystem nvmet_configfs_subsystem = {
1712 .su_group = {
1713 .cg_item = {
1714 .ci_namebuf = "nvmet",
1715 .ci_type = &nvmet_root_type,
1716 },
1717 },
1718 };
1719
nvmet_init_configfs(void)1720 int __init nvmet_init_configfs(void)
1721 {
1722 int ret;
1723
1724 config_group_init(&nvmet_configfs_subsystem.su_group);
1725 mutex_init(&nvmet_configfs_subsystem.su_mutex);
1726
1727 config_group_init_type_name(&nvmet_subsystems_group,
1728 "subsystems", &nvmet_subsystems_type);
1729 configfs_add_default_group(&nvmet_subsystems_group,
1730 &nvmet_configfs_subsystem.su_group);
1731
1732 config_group_init_type_name(&nvmet_ports_group,
1733 "ports", &nvmet_ports_type);
1734 configfs_add_default_group(&nvmet_ports_group,
1735 &nvmet_configfs_subsystem.su_group);
1736
1737 config_group_init_type_name(&nvmet_hosts_group,
1738 "hosts", &nvmet_hosts_type);
1739 configfs_add_default_group(&nvmet_hosts_group,
1740 &nvmet_configfs_subsystem.su_group);
1741
1742 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1743 if (ret) {
1744 pr_err("configfs_register_subsystem: %d\n", ret);
1745 return ret;
1746 }
1747
1748 return 0;
1749 }
1750
nvmet_exit_configfs(void)1751 void __exit nvmet_exit_configfs(void)
1752 {
1753 configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1754 }
1755