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