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