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 static int nvmet_port_subsys_allow_link(struct config_item *parent, 670 struct config_item *target) 671 { 672 struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 673 struct nvmet_subsys *subsys; 674 struct nvmet_subsys_link *link, *p; 675 int ret; 676 677 if (target->ci_type != &nvmet_subsys_type) { 678 pr_err("can only link subsystems into the subsystems dir.!\n"); 679 return -EINVAL; 680 } 681 subsys = to_subsys(target); 682 link = kmalloc(sizeof(*link), GFP_KERNEL); 683 if (!link) 684 return -ENOMEM; 685 link->subsys = subsys; 686 687 down_write(&nvmet_config_sem); 688 ret = -EEXIST; 689 list_for_each_entry(p, &port->subsystems, entry) { 690 if (p->subsys == subsys) 691 goto out_free_link; 692 } 693 694 if (list_empty(&port->subsystems)) { 695 ret = nvmet_enable_port(port); 696 if (ret) 697 goto out_free_link; 698 } 699 700 list_add_tail(&link->entry, &port->subsystems); 701 nvmet_port_disc_changed(port, subsys); 702 703 up_write(&nvmet_config_sem); 704 return 0; 705 706 out_free_link: 707 up_write(&nvmet_config_sem); 708 kfree(link); 709 return ret; 710 } 711 712 static void nvmet_port_subsys_drop_link(struct config_item *parent, 713 struct config_item *target) 714 { 715 struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 716 struct nvmet_subsys *subsys = to_subsys(target); 717 struct nvmet_subsys_link *p; 718 719 down_write(&nvmet_config_sem); 720 list_for_each_entry(p, &port->subsystems, entry) { 721 if (p->subsys == subsys) 722 goto found; 723 } 724 up_write(&nvmet_config_sem); 725 return; 726 727 found: 728 list_del(&p->entry); 729 nvmet_port_del_ctrls(port, subsys); 730 nvmet_port_disc_changed(port, subsys); 731 732 if (list_empty(&port->subsystems)) 733 nvmet_disable_port(port); 734 up_write(&nvmet_config_sem); 735 kfree(p); 736 } 737 738 static struct configfs_item_operations nvmet_port_subsys_item_ops = { 739 .allow_link = nvmet_port_subsys_allow_link, 740 .drop_link = nvmet_port_subsys_drop_link, 741 }; 742 743 static const struct config_item_type nvmet_port_subsys_type = { 744 .ct_item_ops = &nvmet_port_subsys_item_ops, 745 .ct_owner = THIS_MODULE, 746 }; 747 748 static int nvmet_allowed_hosts_allow_link(struct config_item *parent, 749 struct config_item *target) 750 { 751 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 752 struct nvmet_host *host; 753 struct nvmet_host_link *link, *p; 754 int ret; 755 756 if (target->ci_type != &nvmet_host_type) { 757 pr_err("can only link hosts into the allowed_hosts directory!\n"); 758 return -EINVAL; 759 } 760 761 host = to_host(target); 762 link = kmalloc(sizeof(*link), GFP_KERNEL); 763 if (!link) 764 return -ENOMEM; 765 link->host = host; 766 767 down_write(&nvmet_config_sem); 768 ret = -EINVAL; 769 if (subsys->allow_any_host) { 770 pr_err("can't add hosts when allow_any_host is set!\n"); 771 goto out_free_link; 772 } 773 774 ret = -EEXIST; 775 list_for_each_entry(p, &subsys->hosts, entry) { 776 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 777 goto out_free_link; 778 } 779 list_add_tail(&link->entry, &subsys->hosts); 780 nvmet_subsys_disc_changed(subsys, host); 781 782 up_write(&nvmet_config_sem); 783 return 0; 784 out_free_link: 785 up_write(&nvmet_config_sem); 786 kfree(link); 787 return ret; 788 } 789 790 static void nvmet_allowed_hosts_drop_link(struct config_item *parent, 791 struct config_item *target) 792 { 793 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 794 struct nvmet_host *host = to_host(target); 795 struct nvmet_host_link *p; 796 797 down_write(&nvmet_config_sem); 798 list_for_each_entry(p, &subsys->hosts, entry) { 799 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 800 goto found; 801 } 802 up_write(&nvmet_config_sem); 803 return; 804 805 found: 806 list_del(&p->entry); 807 nvmet_subsys_disc_changed(subsys, host); 808 809 up_write(&nvmet_config_sem); 810 kfree(p); 811 } 812 813 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = { 814 .allow_link = nvmet_allowed_hosts_allow_link, 815 .drop_link = nvmet_allowed_hosts_drop_link, 816 }; 817 818 static const struct config_item_type nvmet_allowed_hosts_type = { 819 .ct_item_ops = &nvmet_allowed_hosts_item_ops, 820 .ct_owner = THIS_MODULE, 821 }; 822 823 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item, 824 char *page) 825 { 826 return snprintf(page, PAGE_SIZE, "%d\n", 827 to_subsys(item)->allow_any_host); 828 } 829 830 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item, 831 const char *page, size_t count) 832 { 833 struct nvmet_subsys *subsys = to_subsys(item); 834 bool allow_any_host; 835 int ret = 0; 836 837 if (strtobool(page, &allow_any_host)) 838 return -EINVAL; 839 840 down_write(&nvmet_config_sem); 841 if (allow_any_host && !list_empty(&subsys->hosts)) { 842 pr_err("Can't set allow_any_host when explicit hosts are set!\n"); 843 ret = -EINVAL; 844 goto out_unlock; 845 } 846 847 if (subsys->allow_any_host != allow_any_host) { 848 subsys->allow_any_host = allow_any_host; 849 nvmet_subsys_disc_changed(subsys, NULL); 850 } 851 852 out_unlock: 853 up_write(&nvmet_config_sem); 854 return ret ? ret : count; 855 } 856 857 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host); 858 859 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, 860 char *page) 861 { 862 struct nvmet_subsys *subsys = to_subsys(item); 863 864 if (NVME_TERTIARY(subsys->ver)) 865 return snprintf(page, PAGE_SIZE, "%d.%d.%d\n", 866 (int)NVME_MAJOR(subsys->ver), 867 (int)NVME_MINOR(subsys->ver), 868 (int)NVME_TERTIARY(subsys->ver)); 869 870 return snprintf(page, PAGE_SIZE, "%d.%d\n", 871 (int)NVME_MAJOR(subsys->ver), 872 (int)NVME_MINOR(subsys->ver)); 873 } 874 875 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, 876 const char *page, size_t count) 877 { 878 struct nvmet_subsys *subsys = to_subsys(item); 879 int major, minor, tertiary = 0; 880 int ret; 881 882 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary); 883 if (ret != 2 && ret != 3) 884 return -EINVAL; 885 886 down_write(&nvmet_config_sem); 887 subsys->ver = NVME_VS(major, minor, tertiary); 888 up_write(&nvmet_config_sem); 889 890 return count; 891 } 892 CONFIGFS_ATTR(nvmet_subsys_, attr_version); 893 894 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item, 895 char *page) 896 { 897 struct nvmet_subsys *subsys = to_subsys(item); 898 899 return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial); 900 } 901 902 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item, 903 const char *page, size_t count) 904 { 905 u64 serial; 906 907 if (sscanf(page, "%llx\n", &serial) != 1) 908 return -EINVAL; 909 910 down_write(&nvmet_config_sem); 911 to_subsys(item)->serial = serial; 912 up_write(&nvmet_config_sem); 913 914 return count; 915 } 916 CONFIGFS_ATTR(nvmet_subsys_, attr_serial); 917 918 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item, 919 char *page) 920 { 921 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min); 922 } 923 924 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item, 925 const char *page, size_t cnt) 926 { 927 u16 cntlid_min; 928 929 if (sscanf(page, "%hu\n", &cntlid_min) != 1) 930 return -EINVAL; 931 932 if (cntlid_min == 0) 933 return -EINVAL; 934 935 down_write(&nvmet_config_sem); 936 if (cntlid_min >= to_subsys(item)->cntlid_max) 937 goto out_unlock; 938 to_subsys(item)->cntlid_min = cntlid_min; 939 up_write(&nvmet_config_sem); 940 return cnt; 941 942 out_unlock: 943 up_write(&nvmet_config_sem); 944 return -EINVAL; 945 } 946 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min); 947 948 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item, 949 char *page) 950 { 951 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max); 952 } 953 954 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item, 955 const char *page, size_t cnt) 956 { 957 u16 cntlid_max; 958 959 if (sscanf(page, "%hu\n", &cntlid_max) != 1) 960 return -EINVAL; 961 962 if (cntlid_max == 0) 963 return -EINVAL; 964 965 down_write(&nvmet_config_sem); 966 if (cntlid_max <= to_subsys(item)->cntlid_min) 967 goto out_unlock; 968 to_subsys(item)->cntlid_max = cntlid_max; 969 up_write(&nvmet_config_sem); 970 return cnt; 971 972 out_unlock: 973 up_write(&nvmet_config_sem); 974 return -EINVAL; 975 } 976 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max); 977 978 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item, 979 char *page) 980 { 981 struct nvmet_subsys *subsys = to_subsys(item); 982 struct nvmet_subsys_model *subsys_model; 983 char *model = NVMET_DEFAULT_CTRL_MODEL; 984 int ret; 985 986 rcu_read_lock(); 987 subsys_model = rcu_dereference(subsys->model); 988 if (subsys_model) 989 model = subsys_model->number; 990 ret = snprintf(page, PAGE_SIZE, "%s\n", model); 991 rcu_read_unlock(); 992 993 return ret; 994 } 995 996 /* See Section 1.5 of NVMe 1.4 */ 997 static bool nvmet_is_ascii(const char c) 998 { 999 return c >= 0x20 && c <= 0x7e; 1000 } 1001 1002 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item, 1003 const char *page, size_t count) 1004 { 1005 struct nvmet_subsys *subsys = to_subsys(item); 1006 struct nvmet_subsys_model *new_model; 1007 char *new_model_number; 1008 int pos = 0, len; 1009 1010 len = strcspn(page, "\n"); 1011 if (!len) 1012 return -EINVAL; 1013 1014 for (pos = 0; pos < len; pos++) { 1015 if (!nvmet_is_ascii(page[pos])) 1016 return -EINVAL; 1017 } 1018 1019 new_model_number = kmemdup_nul(page, len, GFP_KERNEL); 1020 if (!new_model_number) 1021 return -ENOMEM; 1022 1023 new_model = kzalloc(sizeof(*new_model) + len + 1, GFP_KERNEL); 1024 if (!new_model) { 1025 kfree(new_model_number); 1026 return -ENOMEM; 1027 } 1028 memcpy(new_model->number, new_model_number, len); 1029 1030 down_write(&nvmet_config_sem); 1031 mutex_lock(&subsys->lock); 1032 new_model = rcu_replace_pointer(subsys->model, new_model, 1033 mutex_is_locked(&subsys->lock)); 1034 mutex_unlock(&subsys->lock); 1035 up_write(&nvmet_config_sem); 1036 1037 kfree_rcu(new_model, rcuhead); 1038 1039 return count; 1040 } 1041 CONFIGFS_ATTR(nvmet_subsys_, attr_model); 1042 1043 #ifdef CONFIG_BLK_DEV_INTEGRITY 1044 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item, 1045 char *page) 1046 { 1047 return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support); 1048 } 1049 1050 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item, 1051 const char *page, size_t count) 1052 { 1053 struct nvmet_subsys *subsys = to_subsys(item); 1054 bool pi_enable; 1055 1056 if (strtobool(page, &pi_enable)) 1057 return -EINVAL; 1058 1059 subsys->pi_support = pi_enable; 1060 return count; 1061 } 1062 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable); 1063 #endif 1064 1065 static struct configfs_attribute *nvmet_subsys_attrs[] = { 1066 &nvmet_subsys_attr_attr_allow_any_host, 1067 &nvmet_subsys_attr_attr_version, 1068 &nvmet_subsys_attr_attr_serial, 1069 &nvmet_subsys_attr_attr_cntlid_min, 1070 &nvmet_subsys_attr_attr_cntlid_max, 1071 &nvmet_subsys_attr_attr_model, 1072 #ifdef CONFIG_BLK_DEV_INTEGRITY 1073 &nvmet_subsys_attr_attr_pi_enable, 1074 #endif 1075 NULL, 1076 }; 1077 1078 /* 1079 * Subsystem structures & folder operation functions below 1080 */ 1081 static void nvmet_subsys_release(struct config_item *item) 1082 { 1083 struct nvmet_subsys *subsys = to_subsys(item); 1084 1085 nvmet_subsys_del_ctrls(subsys); 1086 nvmet_subsys_put(subsys); 1087 } 1088 1089 static struct configfs_item_operations nvmet_subsys_item_ops = { 1090 .release = nvmet_subsys_release, 1091 }; 1092 1093 static const struct config_item_type nvmet_subsys_type = { 1094 .ct_item_ops = &nvmet_subsys_item_ops, 1095 .ct_attrs = nvmet_subsys_attrs, 1096 .ct_owner = THIS_MODULE, 1097 }; 1098 1099 static struct config_group *nvmet_subsys_make(struct config_group *group, 1100 const char *name) 1101 { 1102 struct nvmet_subsys *subsys; 1103 1104 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) { 1105 pr_err("can't create discovery subsystem through configfs\n"); 1106 return ERR_PTR(-EINVAL); 1107 } 1108 1109 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME); 1110 if (IS_ERR(subsys)) 1111 return ERR_CAST(subsys); 1112 1113 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type); 1114 1115 config_group_init_type_name(&subsys->namespaces_group, 1116 "namespaces", &nvmet_namespaces_type); 1117 configfs_add_default_group(&subsys->namespaces_group, &subsys->group); 1118 1119 config_group_init_type_name(&subsys->allowed_hosts_group, 1120 "allowed_hosts", &nvmet_allowed_hosts_type); 1121 configfs_add_default_group(&subsys->allowed_hosts_group, 1122 &subsys->group); 1123 1124 return &subsys->group; 1125 } 1126 1127 static struct configfs_group_operations nvmet_subsystems_group_ops = { 1128 .make_group = nvmet_subsys_make, 1129 }; 1130 1131 static const struct config_item_type nvmet_subsystems_type = { 1132 .ct_group_ops = &nvmet_subsystems_group_ops, 1133 .ct_owner = THIS_MODULE, 1134 }; 1135 1136 static ssize_t nvmet_referral_enable_show(struct config_item *item, 1137 char *page) 1138 { 1139 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled); 1140 } 1141 1142 static ssize_t nvmet_referral_enable_store(struct config_item *item, 1143 const char *page, size_t count) 1144 { 1145 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent); 1146 struct nvmet_port *port = to_nvmet_port(item); 1147 bool enable; 1148 1149 if (strtobool(page, &enable)) 1150 goto inval; 1151 1152 if (enable) 1153 nvmet_referral_enable(parent, port); 1154 else 1155 nvmet_referral_disable(parent, port); 1156 1157 return count; 1158 inval: 1159 pr_err("Invalid value '%s' for enable\n", page); 1160 return -EINVAL; 1161 } 1162 1163 CONFIGFS_ATTR(nvmet_referral_, enable); 1164 1165 /* 1166 * Discovery Service subsystem definitions 1167 */ 1168 static struct configfs_attribute *nvmet_referral_attrs[] = { 1169 &nvmet_attr_addr_adrfam, 1170 &nvmet_attr_addr_portid, 1171 &nvmet_attr_addr_treq, 1172 &nvmet_attr_addr_traddr, 1173 &nvmet_attr_addr_trsvcid, 1174 &nvmet_attr_addr_trtype, 1175 &nvmet_referral_attr_enable, 1176 NULL, 1177 }; 1178 1179 static void nvmet_referral_notify(struct config_group *group, 1180 struct config_item *item) 1181 { 1182 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent); 1183 struct nvmet_port *port = to_nvmet_port(item); 1184 1185 nvmet_referral_disable(parent, port); 1186 } 1187 1188 static void nvmet_referral_release(struct config_item *item) 1189 { 1190 struct nvmet_port *port = to_nvmet_port(item); 1191 1192 kfree(port); 1193 } 1194 1195 static struct configfs_item_operations nvmet_referral_item_ops = { 1196 .release = nvmet_referral_release, 1197 }; 1198 1199 static const struct config_item_type nvmet_referral_type = { 1200 .ct_owner = THIS_MODULE, 1201 .ct_attrs = nvmet_referral_attrs, 1202 .ct_item_ops = &nvmet_referral_item_ops, 1203 }; 1204 1205 static struct config_group *nvmet_referral_make( 1206 struct config_group *group, const char *name) 1207 { 1208 struct nvmet_port *port; 1209 1210 port = kzalloc(sizeof(*port), GFP_KERNEL); 1211 if (!port) 1212 return ERR_PTR(-ENOMEM); 1213 1214 INIT_LIST_HEAD(&port->entry); 1215 config_group_init_type_name(&port->group, name, &nvmet_referral_type); 1216 1217 return &port->group; 1218 } 1219 1220 static struct configfs_group_operations nvmet_referral_group_ops = { 1221 .make_group = nvmet_referral_make, 1222 .disconnect_notify = nvmet_referral_notify, 1223 }; 1224 1225 static const struct config_item_type nvmet_referrals_type = { 1226 .ct_owner = THIS_MODULE, 1227 .ct_group_ops = &nvmet_referral_group_ops, 1228 }; 1229 1230 static struct nvmet_type_name_map nvmet_ana_state[] = { 1231 { NVME_ANA_OPTIMIZED, "optimized" }, 1232 { NVME_ANA_NONOPTIMIZED, "non-optimized" }, 1233 { NVME_ANA_INACCESSIBLE, "inaccessible" }, 1234 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" }, 1235 { NVME_ANA_CHANGE, "change" }, 1236 }; 1237 1238 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item, 1239 char *page) 1240 { 1241 struct nvmet_ana_group *grp = to_ana_group(item); 1242 enum nvme_ana_state state = grp->port->ana_state[grp->grpid]; 1243 int i; 1244 1245 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) { 1246 if (state == nvmet_ana_state[i].type) 1247 return sprintf(page, "%s\n", nvmet_ana_state[i].name); 1248 } 1249 1250 return sprintf(page, "\n"); 1251 } 1252 1253 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item, 1254 const char *page, size_t count) 1255 { 1256 struct nvmet_ana_group *grp = to_ana_group(item); 1257 enum nvme_ana_state *ana_state = grp->port->ana_state; 1258 int i; 1259 1260 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) { 1261 if (sysfs_streq(page, nvmet_ana_state[i].name)) 1262 goto found; 1263 } 1264 1265 pr_err("Invalid value '%s' for ana_state\n", page); 1266 return -EINVAL; 1267 1268 found: 1269 down_write(&nvmet_ana_sem); 1270 ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type; 1271 nvmet_ana_chgcnt++; 1272 up_write(&nvmet_ana_sem); 1273 nvmet_port_send_ana_event(grp->port); 1274 return count; 1275 } 1276 1277 CONFIGFS_ATTR(nvmet_ana_group_, ana_state); 1278 1279 static struct configfs_attribute *nvmet_ana_group_attrs[] = { 1280 &nvmet_ana_group_attr_ana_state, 1281 NULL, 1282 }; 1283 1284 static void nvmet_ana_group_release(struct config_item *item) 1285 { 1286 struct nvmet_ana_group *grp = to_ana_group(item); 1287 1288 if (grp == &grp->port->ana_default_group) 1289 return; 1290 1291 down_write(&nvmet_ana_sem); 1292 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE; 1293 nvmet_ana_group_enabled[grp->grpid]--; 1294 up_write(&nvmet_ana_sem); 1295 1296 nvmet_port_send_ana_event(grp->port); 1297 kfree(grp); 1298 } 1299 1300 static struct configfs_item_operations nvmet_ana_group_item_ops = { 1301 .release = nvmet_ana_group_release, 1302 }; 1303 1304 static const struct config_item_type nvmet_ana_group_type = { 1305 .ct_item_ops = &nvmet_ana_group_item_ops, 1306 .ct_attrs = nvmet_ana_group_attrs, 1307 .ct_owner = THIS_MODULE, 1308 }; 1309 1310 static struct config_group *nvmet_ana_groups_make_group( 1311 struct config_group *group, const char *name) 1312 { 1313 struct nvmet_port *port = ana_groups_to_port(&group->cg_item); 1314 struct nvmet_ana_group *grp; 1315 u32 grpid; 1316 int ret; 1317 1318 ret = kstrtou32(name, 0, &grpid); 1319 if (ret) 1320 goto out; 1321 1322 ret = -EINVAL; 1323 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS) 1324 goto out; 1325 1326 ret = -ENOMEM; 1327 grp = kzalloc(sizeof(*grp), GFP_KERNEL); 1328 if (!grp) 1329 goto out; 1330 grp->port = port; 1331 grp->grpid = grpid; 1332 1333 down_write(&nvmet_ana_sem); 1334 nvmet_ana_group_enabled[grpid]++; 1335 up_write(&nvmet_ana_sem); 1336 1337 nvmet_port_send_ana_event(grp->port); 1338 1339 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type); 1340 return &grp->group; 1341 out: 1342 return ERR_PTR(ret); 1343 } 1344 1345 static struct configfs_group_operations nvmet_ana_groups_group_ops = { 1346 .make_group = nvmet_ana_groups_make_group, 1347 }; 1348 1349 static const struct config_item_type nvmet_ana_groups_type = { 1350 .ct_group_ops = &nvmet_ana_groups_group_ops, 1351 .ct_owner = THIS_MODULE, 1352 }; 1353 1354 /* 1355 * Ports definitions. 1356 */ 1357 static void nvmet_port_release(struct config_item *item) 1358 { 1359 struct nvmet_port *port = to_nvmet_port(item); 1360 1361 list_del(&port->global_entry); 1362 1363 kfree(port->ana_state); 1364 kfree(port); 1365 } 1366 1367 static struct configfs_attribute *nvmet_port_attrs[] = { 1368 &nvmet_attr_addr_adrfam, 1369 &nvmet_attr_addr_treq, 1370 &nvmet_attr_addr_traddr, 1371 &nvmet_attr_addr_trsvcid, 1372 &nvmet_attr_addr_trtype, 1373 &nvmet_attr_param_inline_data_size, 1374 #ifdef CONFIG_BLK_DEV_INTEGRITY 1375 &nvmet_attr_param_pi_enable, 1376 #endif 1377 NULL, 1378 }; 1379 1380 static struct configfs_item_operations nvmet_port_item_ops = { 1381 .release = nvmet_port_release, 1382 }; 1383 1384 static const struct config_item_type nvmet_port_type = { 1385 .ct_attrs = nvmet_port_attrs, 1386 .ct_item_ops = &nvmet_port_item_ops, 1387 .ct_owner = THIS_MODULE, 1388 }; 1389 1390 static struct config_group *nvmet_ports_make(struct config_group *group, 1391 const char *name) 1392 { 1393 struct nvmet_port *port; 1394 u16 portid; 1395 u32 i; 1396 1397 if (kstrtou16(name, 0, &portid)) 1398 return ERR_PTR(-EINVAL); 1399 1400 port = kzalloc(sizeof(*port), GFP_KERNEL); 1401 if (!port) 1402 return ERR_PTR(-ENOMEM); 1403 1404 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1, 1405 sizeof(*port->ana_state), GFP_KERNEL); 1406 if (!port->ana_state) { 1407 kfree(port); 1408 return ERR_PTR(-ENOMEM); 1409 } 1410 1411 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) { 1412 if (i == NVMET_DEFAULT_ANA_GRPID) 1413 port->ana_state[1] = NVME_ANA_OPTIMIZED; 1414 else 1415 port->ana_state[i] = NVME_ANA_INACCESSIBLE; 1416 } 1417 1418 list_add(&port->global_entry, &nvmet_ports_list); 1419 1420 INIT_LIST_HEAD(&port->entry); 1421 INIT_LIST_HEAD(&port->subsystems); 1422 INIT_LIST_HEAD(&port->referrals); 1423 port->inline_data_size = -1; /* < 0 == let the transport choose */ 1424 1425 port->disc_addr.portid = cpu_to_le16(portid); 1426 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX; 1427 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW; 1428 config_group_init_type_name(&port->group, name, &nvmet_port_type); 1429 1430 config_group_init_type_name(&port->subsys_group, 1431 "subsystems", &nvmet_port_subsys_type); 1432 configfs_add_default_group(&port->subsys_group, &port->group); 1433 1434 config_group_init_type_name(&port->referrals_group, 1435 "referrals", &nvmet_referrals_type); 1436 configfs_add_default_group(&port->referrals_group, &port->group); 1437 1438 config_group_init_type_name(&port->ana_groups_group, 1439 "ana_groups", &nvmet_ana_groups_type); 1440 configfs_add_default_group(&port->ana_groups_group, &port->group); 1441 1442 port->ana_default_group.port = port; 1443 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID; 1444 config_group_init_type_name(&port->ana_default_group.group, 1445 __stringify(NVMET_DEFAULT_ANA_GRPID), 1446 &nvmet_ana_group_type); 1447 configfs_add_default_group(&port->ana_default_group.group, 1448 &port->ana_groups_group); 1449 1450 return &port->group; 1451 } 1452 1453 static struct configfs_group_operations nvmet_ports_group_ops = { 1454 .make_group = nvmet_ports_make, 1455 }; 1456 1457 static const struct config_item_type nvmet_ports_type = { 1458 .ct_group_ops = &nvmet_ports_group_ops, 1459 .ct_owner = THIS_MODULE, 1460 }; 1461 1462 static struct config_group nvmet_subsystems_group; 1463 static struct config_group nvmet_ports_group; 1464 1465 static void nvmet_host_release(struct config_item *item) 1466 { 1467 struct nvmet_host *host = to_host(item); 1468 1469 kfree(host); 1470 } 1471 1472 static struct configfs_item_operations nvmet_host_item_ops = { 1473 .release = nvmet_host_release, 1474 }; 1475 1476 static const struct config_item_type nvmet_host_type = { 1477 .ct_item_ops = &nvmet_host_item_ops, 1478 .ct_owner = THIS_MODULE, 1479 }; 1480 1481 static struct config_group *nvmet_hosts_make_group(struct config_group *group, 1482 const char *name) 1483 { 1484 struct nvmet_host *host; 1485 1486 host = kzalloc(sizeof(*host), GFP_KERNEL); 1487 if (!host) 1488 return ERR_PTR(-ENOMEM); 1489 1490 config_group_init_type_name(&host->group, name, &nvmet_host_type); 1491 1492 return &host->group; 1493 } 1494 1495 static struct configfs_group_operations nvmet_hosts_group_ops = { 1496 .make_group = nvmet_hosts_make_group, 1497 }; 1498 1499 static const struct config_item_type nvmet_hosts_type = { 1500 .ct_group_ops = &nvmet_hosts_group_ops, 1501 .ct_owner = THIS_MODULE, 1502 }; 1503 1504 static struct config_group nvmet_hosts_group; 1505 1506 static const struct config_item_type nvmet_root_type = { 1507 .ct_owner = THIS_MODULE, 1508 }; 1509 1510 static struct configfs_subsystem nvmet_configfs_subsystem = { 1511 .su_group = { 1512 .cg_item = { 1513 .ci_namebuf = "nvmet", 1514 .ci_type = &nvmet_root_type, 1515 }, 1516 }, 1517 }; 1518 1519 int __init nvmet_init_configfs(void) 1520 { 1521 int ret; 1522 1523 config_group_init(&nvmet_configfs_subsystem.su_group); 1524 mutex_init(&nvmet_configfs_subsystem.su_mutex); 1525 1526 config_group_init_type_name(&nvmet_subsystems_group, 1527 "subsystems", &nvmet_subsystems_type); 1528 configfs_add_default_group(&nvmet_subsystems_group, 1529 &nvmet_configfs_subsystem.su_group); 1530 1531 config_group_init_type_name(&nvmet_ports_group, 1532 "ports", &nvmet_ports_type); 1533 configfs_add_default_group(&nvmet_ports_group, 1534 &nvmet_configfs_subsystem.su_group); 1535 1536 config_group_init_type_name(&nvmet_hosts_group, 1537 "hosts", &nvmet_hosts_type); 1538 configfs_add_default_group(&nvmet_hosts_group, 1539 &nvmet_configfs_subsystem.su_group); 1540 1541 ret = configfs_register_subsystem(&nvmet_configfs_subsystem); 1542 if (ret) { 1543 pr_err("configfs_register_subsystem: %d\n", ret); 1544 return ret; 1545 } 1546 1547 return 0; 1548 } 1549 1550 void __exit nvmet_exit_configfs(void) 1551 { 1552 configfs_unregister_subsystem(&nvmet_configfs_subsystem); 1553 } 1554