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