Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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