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