1 /* 2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 */ 13 #include <linux/module.h> 14 #include <linux/device.h> 15 #include <linux/sort.h> 16 #include <linux/slab.h> 17 #include <linux/list.h> 18 #include <linux/nd.h> 19 #include "nd-core.h" 20 #include "pmem.h" 21 #include "nd.h" 22 23 static void namespace_io_release(struct device *dev) 24 { 25 struct nd_namespace_io *nsio = to_nd_namespace_io(dev); 26 27 kfree(nsio); 28 } 29 30 static void namespace_pmem_release(struct device *dev) 31 { 32 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 33 struct nd_region *nd_region = to_nd_region(dev->parent); 34 35 if (nspm->id >= 0) 36 ida_simple_remove(&nd_region->ns_ida, nspm->id); 37 kfree(nspm->alt_name); 38 kfree(nspm->uuid); 39 kfree(nspm); 40 } 41 42 static void namespace_blk_release(struct device *dev) 43 { 44 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 45 struct nd_region *nd_region = to_nd_region(dev->parent); 46 47 if (nsblk->id >= 0) 48 ida_simple_remove(&nd_region->ns_ida, nsblk->id); 49 kfree(nsblk->alt_name); 50 kfree(nsblk->uuid); 51 kfree(nsblk->res); 52 kfree(nsblk); 53 } 54 55 static const struct device_type namespace_io_device_type = { 56 .name = "nd_namespace_io", 57 .release = namespace_io_release, 58 }; 59 60 static const struct device_type namespace_pmem_device_type = { 61 .name = "nd_namespace_pmem", 62 .release = namespace_pmem_release, 63 }; 64 65 static const struct device_type namespace_blk_device_type = { 66 .name = "nd_namespace_blk", 67 .release = namespace_blk_release, 68 }; 69 70 static bool is_namespace_pmem(const struct device *dev) 71 { 72 return dev ? dev->type == &namespace_pmem_device_type : false; 73 } 74 75 static bool is_namespace_blk(const struct device *dev) 76 { 77 return dev ? dev->type == &namespace_blk_device_type : false; 78 } 79 80 static bool is_namespace_io(const struct device *dev) 81 { 82 return dev ? dev->type == &namespace_io_device_type : false; 83 } 84 85 static int is_uuid_busy(struct device *dev, void *data) 86 { 87 u8 *uuid1 = data, *uuid2 = NULL; 88 89 if (is_namespace_pmem(dev)) { 90 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 91 92 uuid2 = nspm->uuid; 93 } else if (is_namespace_blk(dev)) { 94 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 95 96 uuid2 = nsblk->uuid; 97 } else if (is_nd_btt(dev)) { 98 struct nd_btt *nd_btt = to_nd_btt(dev); 99 100 uuid2 = nd_btt->uuid; 101 } else if (is_nd_pfn(dev)) { 102 struct nd_pfn *nd_pfn = to_nd_pfn(dev); 103 104 uuid2 = nd_pfn->uuid; 105 } 106 107 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0) 108 return -EBUSY; 109 110 return 0; 111 } 112 113 static int is_namespace_uuid_busy(struct device *dev, void *data) 114 { 115 if (is_nd_region(dev)) 116 return device_for_each_child(dev, data, is_uuid_busy); 117 return 0; 118 } 119 120 /** 121 * nd_is_uuid_unique - verify that no other namespace has @uuid 122 * @dev: any device on a nvdimm_bus 123 * @uuid: uuid to check 124 */ 125 bool nd_is_uuid_unique(struct device *dev, u8 *uuid) 126 { 127 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); 128 129 if (!nvdimm_bus) 130 return false; 131 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev)); 132 if (device_for_each_child(&nvdimm_bus->dev, uuid, 133 is_namespace_uuid_busy) != 0) 134 return false; 135 return true; 136 } 137 138 bool pmem_should_map_pages(struct device *dev) 139 { 140 struct nd_region *nd_region = to_nd_region(dev->parent); 141 struct nd_namespace_io *nsio; 142 143 if (!IS_ENABLED(CONFIG_ZONE_DEVICE)) 144 return false; 145 146 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags)) 147 return false; 148 149 if (is_nd_pfn(dev) || is_nd_btt(dev)) 150 return false; 151 152 nsio = to_nd_namespace_io(dev); 153 if (region_intersects(nsio->res.start, resource_size(&nsio->res), 154 IORESOURCE_SYSTEM_RAM, 155 IORES_DESC_NONE) == REGION_MIXED) 156 return false; 157 158 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB; 159 } 160 EXPORT_SYMBOL(pmem_should_map_pages); 161 162 unsigned int pmem_sector_size(struct nd_namespace_common *ndns) 163 { 164 if (is_namespace_pmem(&ndns->dev)) { 165 struct nd_namespace_pmem *nspm; 166 167 nspm = to_nd_namespace_pmem(&ndns->dev); 168 if (nspm->lbasize == 0 || nspm->lbasize == 512) 169 /* default */; 170 else if (nspm->lbasize == 4096) 171 return 4096; 172 else 173 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n", 174 nspm->lbasize); 175 } 176 177 /* 178 * There is no namespace label (is_namespace_io()), or the label 179 * indicates the default sector size. 180 */ 181 return 512; 182 } 183 EXPORT_SYMBOL(pmem_sector_size); 184 185 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, 186 char *name) 187 { 188 struct nd_region *nd_region = to_nd_region(ndns->dev.parent); 189 const char *suffix = NULL; 190 191 if (ndns->claim && is_nd_btt(ndns->claim)) 192 suffix = "s"; 193 194 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) { 195 int nsidx = 0; 196 197 if (is_namespace_pmem(&ndns->dev)) { 198 struct nd_namespace_pmem *nspm; 199 200 nspm = to_nd_namespace_pmem(&ndns->dev); 201 nsidx = nspm->id; 202 } 203 204 if (nsidx) 205 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx, 206 suffix ? suffix : ""); 207 else 208 sprintf(name, "pmem%d%s", nd_region->id, 209 suffix ? suffix : ""); 210 } else if (is_namespace_blk(&ndns->dev)) { 211 struct nd_namespace_blk *nsblk; 212 213 nsblk = to_nd_namespace_blk(&ndns->dev); 214 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, 215 suffix ? suffix : ""); 216 } else { 217 return NULL; 218 } 219 220 return name; 221 } 222 EXPORT_SYMBOL(nvdimm_namespace_disk_name); 223 224 const u8 *nd_dev_to_uuid(struct device *dev) 225 { 226 static const u8 null_uuid[16]; 227 228 if (!dev) 229 return null_uuid; 230 231 if (is_namespace_pmem(dev)) { 232 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 233 234 return nspm->uuid; 235 } else if (is_namespace_blk(dev)) { 236 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 237 238 return nsblk->uuid; 239 } else 240 return null_uuid; 241 } 242 EXPORT_SYMBOL(nd_dev_to_uuid); 243 244 static ssize_t nstype_show(struct device *dev, 245 struct device_attribute *attr, char *buf) 246 { 247 struct nd_region *nd_region = to_nd_region(dev->parent); 248 249 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); 250 } 251 static DEVICE_ATTR_RO(nstype); 252 253 static ssize_t __alt_name_store(struct device *dev, const char *buf, 254 const size_t len) 255 { 256 char *input, *pos, *alt_name, **ns_altname; 257 ssize_t rc; 258 259 if (is_namespace_pmem(dev)) { 260 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 261 262 ns_altname = &nspm->alt_name; 263 } else if (is_namespace_blk(dev)) { 264 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 265 266 ns_altname = &nsblk->alt_name; 267 } else 268 return -ENXIO; 269 270 if (dev->driver || to_ndns(dev)->claim) 271 return -EBUSY; 272 273 input = kstrndup(buf, len, GFP_KERNEL); 274 if (!input) 275 return -ENOMEM; 276 277 pos = strim(input); 278 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) { 279 rc = -EINVAL; 280 goto out; 281 } 282 283 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL); 284 if (!alt_name) { 285 rc = -ENOMEM; 286 goto out; 287 } 288 kfree(*ns_altname); 289 *ns_altname = alt_name; 290 sprintf(*ns_altname, "%s", pos); 291 rc = len; 292 293 out: 294 kfree(input); 295 return rc; 296 } 297 298 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk) 299 { 300 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); 301 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 302 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 303 struct nd_label_id label_id; 304 resource_size_t size = 0; 305 struct resource *res; 306 307 if (!nsblk->uuid) 308 return 0; 309 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); 310 for_each_dpa_resource(ndd, res) 311 if (strcmp(res->name, label_id.id) == 0) 312 size += resource_size(res); 313 return size; 314 } 315 316 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) 317 { 318 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); 319 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 320 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 321 struct nd_label_id label_id; 322 struct resource *res; 323 int count, i; 324 325 if (!nsblk->uuid || !nsblk->lbasize || !ndd) 326 return false; 327 328 count = 0; 329 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); 330 for_each_dpa_resource(ndd, res) { 331 if (strcmp(res->name, label_id.id) != 0) 332 continue; 333 /* 334 * Resources with unacknowledged adjustments indicate a 335 * failure to update labels 336 */ 337 if (res->flags & DPA_RESOURCE_ADJUSTED) 338 return false; 339 count++; 340 } 341 342 /* These values match after a successful label update */ 343 if (count != nsblk->num_resources) 344 return false; 345 346 for (i = 0; i < nsblk->num_resources; i++) { 347 struct resource *found = NULL; 348 349 for_each_dpa_resource(ndd, res) 350 if (res == nsblk->res[i]) { 351 found = res; 352 break; 353 } 354 /* stale resource */ 355 if (!found) 356 return false; 357 } 358 359 return true; 360 } 361 362 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) 363 { 364 resource_size_t size; 365 366 nvdimm_bus_lock(&nsblk->common.dev); 367 size = __nd_namespace_blk_validate(nsblk); 368 nvdimm_bus_unlock(&nsblk->common.dev); 369 370 return size; 371 } 372 EXPORT_SYMBOL(nd_namespace_blk_validate); 373 374 375 static int nd_namespace_label_update(struct nd_region *nd_region, 376 struct device *dev) 377 { 378 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim, 379 "namespace must be idle during label update\n"); 380 if (dev->driver || to_ndns(dev)->claim) 381 return 0; 382 383 /* 384 * Only allow label writes that will result in a valid namespace 385 * or deletion of an existing namespace. 386 */ 387 if (is_namespace_pmem(dev)) { 388 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 389 resource_size_t size = resource_size(&nspm->nsio.res); 390 391 if (size == 0 && nspm->uuid) 392 /* delete allocation */; 393 else if (!nspm->uuid) 394 return 0; 395 396 return nd_pmem_namespace_label_update(nd_region, nspm, size); 397 } else if (is_namespace_blk(dev)) { 398 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 399 resource_size_t size = nd_namespace_blk_size(nsblk); 400 401 if (size == 0 && nsblk->uuid) 402 /* delete allocation */; 403 else if (!nsblk->uuid || !nsblk->lbasize) 404 return 0; 405 406 return nd_blk_namespace_label_update(nd_region, nsblk, size); 407 } else 408 return -ENXIO; 409 } 410 411 static ssize_t alt_name_store(struct device *dev, 412 struct device_attribute *attr, const char *buf, size_t len) 413 { 414 struct nd_region *nd_region = to_nd_region(dev->parent); 415 ssize_t rc; 416 417 device_lock(dev); 418 nvdimm_bus_lock(dev); 419 wait_nvdimm_bus_probe_idle(dev); 420 rc = __alt_name_store(dev, buf, len); 421 if (rc >= 0) 422 rc = nd_namespace_label_update(nd_region, dev); 423 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc); 424 nvdimm_bus_unlock(dev); 425 device_unlock(dev); 426 427 return rc < 0 ? rc : len; 428 } 429 430 static ssize_t alt_name_show(struct device *dev, 431 struct device_attribute *attr, char *buf) 432 { 433 char *ns_altname; 434 435 if (is_namespace_pmem(dev)) { 436 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 437 438 ns_altname = nspm->alt_name; 439 } else if (is_namespace_blk(dev)) { 440 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 441 442 ns_altname = nsblk->alt_name; 443 } else 444 return -ENXIO; 445 446 return sprintf(buf, "%s\n", ns_altname ? ns_altname : ""); 447 } 448 static DEVICE_ATTR_RW(alt_name); 449 450 static int scan_free(struct nd_region *nd_region, 451 struct nd_mapping *nd_mapping, struct nd_label_id *label_id, 452 resource_size_t n) 453 { 454 bool is_blk = strncmp(label_id->id, "blk", 3) == 0; 455 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 456 int rc = 0; 457 458 while (n) { 459 struct resource *res, *last; 460 resource_size_t new_start; 461 462 last = NULL; 463 for_each_dpa_resource(ndd, res) 464 if (strcmp(res->name, label_id->id) == 0) 465 last = res; 466 res = last; 467 if (!res) 468 return 0; 469 470 if (n >= resource_size(res)) { 471 n -= resource_size(res); 472 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc); 473 nvdimm_free_dpa(ndd, res); 474 /* retry with last resource deleted */ 475 continue; 476 } 477 478 /* 479 * Keep BLK allocations relegated to high DPA as much as 480 * possible 481 */ 482 if (is_blk) 483 new_start = res->start + n; 484 else 485 new_start = res->start; 486 487 rc = adjust_resource(res, new_start, resource_size(res) - n); 488 if (rc == 0) 489 res->flags |= DPA_RESOURCE_ADJUSTED; 490 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc); 491 break; 492 } 493 494 return rc; 495 } 496 497 /** 498 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id 499 * @nd_region: the set of dimms to reclaim @n bytes from 500 * @label_id: unique identifier for the namespace consuming this dpa range 501 * @n: number of bytes per-dimm to release 502 * 503 * Assumes resources are ordered. Starting from the end try to 504 * adjust_resource() the allocation to @n, but if @n is larger than the 505 * allocation delete it and find the 'new' last allocation in the label 506 * set. 507 */ 508 static int shrink_dpa_allocation(struct nd_region *nd_region, 509 struct nd_label_id *label_id, resource_size_t n) 510 { 511 int i; 512 513 for (i = 0; i < nd_region->ndr_mappings; i++) { 514 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 515 int rc; 516 517 rc = scan_free(nd_region, nd_mapping, label_id, n); 518 if (rc) 519 return rc; 520 } 521 522 return 0; 523 } 524 525 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id, 526 struct nd_region *nd_region, struct nd_mapping *nd_mapping, 527 resource_size_t n) 528 { 529 bool is_blk = strncmp(label_id->id, "blk", 3) == 0; 530 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 531 resource_size_t first_dpa; 532 struct resource *res; 533 int rc = 0; 534 535 /* allocate blk from highest dpa first */ 536 if (is_blk) 537 first_dpa = nd_mapping->start + nd_mapping->size - n; 538 else 539 first_dpa = nd_mapping->start; 540 541 /* first resource allocation for this label-id or dimm */ 542 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n); 543 if (!res) 544 rc = -EBUSY; 545 546 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc); 547 return rc ? n : 0; 548 } 549 550 551 /** 552 * space_valid() - validate free dpa space against constraints 553 * @nd_region: hosting region of the free space 554 * @ndd: dimm device data for debug 555 * @label_id: namespace id to allocate space 556 * @prev: potential allocation that precedes free space 557 * @next: allocation that follows the given free space range 558 * @exist: first allocation with same id in the mapping 559 * @n: range that must satisfied for pmem allocations 560 * @valid: free space range to validate 561 * 562 * BLK-space is valid as long as it does not precede a PMEM 563 * allocation in a given region. PMEM-space must be contiguous 564 * and adjacent to an existing existing allocation (if one 565 * exists). If reserving PMEM any space is valid. 566 */ 567 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd, 568 struct nd_label_id *label_id, struct resource *prev, 569 struct resource *next, struct resource *exist, 570 resource_size_t n, struct resource *valid) 571 { 572 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0; 573 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; 574 575 if (valid->start >= valid->end) 576 goto invalid; 577 578 if (is_reserve) 579 return; 580 581 if (!is_pmem) { 582 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 583 struct nvdimm_bus *nvdimm_bus; 584 struct blk_alloc_info info = { 585 .nd_mapping = nd_mapping, 586 .available = nd_mapping->size, 587 .res = valid, 588 }; 589 590 WARN_ON(!is_nd_blk(&nd_region->dev)); 591 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); 592 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy); 593 return; 594 } 595 596 /* allocation needs to be contiguous, so this is all or nothing */ 597 if (resource_size(valid) < n) 598 goto invalid; 599 600 /* we've got all the space we need and no existing allocation */ 601 if (!exist) 602 return; 603 604 /* allocation needs to be contiguous with the existing namespace */ 605 if (valid->start == exist->end + 1 606 || valid->end == exist->start - 1) 607 return; 608 609 invalid: 610 /* truncate @valid size to 0 */ 611 valid->end = valid->start - 1; 612 } 613 614 enum alloc_loc { 615 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER, 616 }; 617 618 static resource_size_t scan_allocate(struct nd_region *nd_region, 619 struct nd_mapping *nd_mapping, struct nd_label_id *label_id, 620 resource_size_t n) 621 { 622 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1; 623 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; 624 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 625 struct resource *res, *exist = NULL, valid; 626 const resource_size_t to_allocate = n; 627 int first; 628 629 for_each_dpa_resource(ndd, res) 630 if (strcmp(label_id->id, res->name) == 0) 631 exist = res; 632 633 valid.start = nd_mapping->start; 634 valid.end = mapping_end; 635 valid.name = "free space"; 636 retry: 637 first = 0; 638 for_each_dpa_resource(ndd, res) { 639 struct resource *next = res->sibling, *new_res = NULL; 640 resource_size_t allocate, available = 0; 641 enum alloc_loc loc = ALLOC_ERR; 642 const char *action; 643 int rc = 0; 644 645 /* ignore resources outside this nd_mapping */ 646 if (res->start > mapping_end) 647 continue; 648 if (res->end < nd_mapping->start) 649 continue; 650 651 /* space at the beginning of the mapping */ 652 if (!first++ && res->start > nd_mapping->start) { 653 valid.start = nd_mapping->start; 654 valid.end = res->start - 1; 655 space_valid(nd_region, ndd, label_id, NULL, next, exist, 656 to_allocate, &valid); 657 available = resource_size(&valid); 658 if (available) 659 loc = ALLOC_BEFORE; 660 } 661 662 /* space between allocations */ 663 if (!loc && next) { 664 valid.start = res->start + resource_size(res); 665 valid.end = min(mapping_end, next->start - 1); 666 space_valid(nd_region, ndd, label_id, res, next, exist, 667 to_allocate, &valid); 668 available = resource_size(&valid); 669 if (available) 670 loc = ALLOC_MID; 671 } 672 673 /* space at the end of the mapping */ 674 if (!loc && !next) { 675 valid.start = res->start + resource_size(res); 676 valid.end = mapping_end; 677 space_valid(nd_region, ndd, label_id, res, next, exist, 678 to_allocate, &valid); 679 available = resource_size(&valid); 680 if (available) 681 loc = ALLOC_AFTER; 682 } 683 684 if (!loc || !available) 685 continue; 686 allocate = min(available, n); 687 switch (loc) { 688 case ALLOC_BEFORE: 689 if (strcmp(res->name, label_id->id) == 0) { 690 /* adjust current resource up */ 691 rc = adjust_resource(res, res->start - allocate, 692 resource_size(res) + allocate); 693 action = "cur grow up"; 694 } else 695 action = "allocate"; 696 break; 697 case ALLOC_MID: 698 if (strcmp(next->name, label_id->id) == 0) { 699 /* adjust next resource up */ 700 rc = adjust_resource(next, next->start 701 - allocate, resource_size(next) 702 + allocate); 703 new_res = next; 704 action = "next grow up"; 705 } else if (strcmp(res->name, label_id->id) == 0) { 706 action = "grow down"; 707 } else 708 action = "allocate"; 709 break; 710 case ALLOC_AFTER: 711 if (strcmp(res->name, label_id->id) == 0) 712 action = "grow down"; 713 else 714 action = "allocate"; 715 break; 716 default: 717 return n; 718 } 719 720 if (strcmp(action, "allocate") == 0) { 721 /* BLK allocate bottom up */ 722 if (!is_pmem) 723 valid.start += available - allocate; 724 725 new_res = nvdimm_allocate_dpa(ndd, label_id, 726 valid.start, allocate); 727 if (!new_res) 728 rc = -EBUSY; 729 } else if (strcmp(action, "grow down") == 0) { 730 /* adjust current resource down */ 731 rc = adjust_resource(res, res->start, resource_size(res) 732 + allocate); 733 if (rc == 0) 734 res->flags |= DPA_RESOURCE_ADJUSTED; 735 } 736 737 if (!new_res) 738 new_res = res; 739 740 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n", 741 action, loc, rc); 742 743 if (rc) 744 return n; 745 746 n -= allocate; 747 if (n) { 748 /* 749 * Retry scan with newly inserted resources. 750 * For example, if we did an ALLOC_BEFORE 751 * insertion there may also have been space 752 * available for an ALLOC_AFTER insertion, so we 753 * need to check this same resource again 754 */ 755 goto retry; 756 } else 757 return 0; 758 } 759 760 /* 761 * If we allocated nothing in the BLK case it may be because we are in 762 * an initial "pmem-reserve pass". Only do an initial BLK allocation 763 * when none of the DPA space is reserved. 764 */ 765 if ((is_pmem || !ndd->dpa.child) && n == to_allocate) 766 return init_dpa_allocation(label_id, nd_region, nd_mapping, n); 767 return n; 768 } 769 770 static int merge_dpa(struct nd_region *nd_region, 771 struct nd_mapping *nd_mapping, struct nd_label_id *label_id) 772 { 773 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 774 struct resource *res; 775 776 if (strncmp("pmem", label_id->id, 4) == 0) 777 return 0; 778 retry: 779 for_each_dpa_resource(ndd, res) { 780 int rc; 781 struct resource *next = res->sibling; 782 resource_size_t end = res->start + resource_size(res); 783 784 if (!next || strcmp(res->name, label_id->id) != 0 785 || strcmp(next->name, label_id->id) != 0 786 || end != next->start) 787 continue; 788 end += resource_size(next); 789 nvdimm_free_dpa(ndd, next); 790 rc = adjust_resource(res, res->start, end - res->start); 791 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc); 792 if (rc) 793 return rc; 794 res->flags |= DPA_RESOURCE_ADJUSTED; 795 goto retry; 796 } 797 798 return 0; 799 } 800 801 int __reserve_free_pmem(struct device *dev, void *data) 802 { 803 struct nvdimm *nvdimm = data; 804 struct nd_region *nd_region; 805 struct nd_label_id label_id; 806 int i; 807 808 if (!is_memory(dev)) 809 return 0; 810 811 nd_region = to_nd_region(dev); 812 if (nd_region->ndr_mappings == 0) 813 return 0; 814 815 memset(&label_id, 0, sizeof(label_id)); 816 strcat(label_id.id, "pmem-reserve"); 817 for (i = 0; i < nd_region->ndr_mappings; i++) { 818 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 819 resource_size_t n, rem = 0; 820 821 if (nd_mapping->nvdimm != nvdimm) 822 continue; 823 824 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem); 825 if (n == 0) 826 return 0; 827 rem = scan_allocate(nd_region, nd_mapping, &label_id, n); 828 dev_WARN_ONCE(&nd_region->dev, rem, 829 "pmem reserve underrun: %#llx of %#llx bytes\n", 830 (unsigned long long) n - rem, 831 (unsigned long long) n); 832 return rem ? -ENXIO : 0; 833 } 834 835 return 0; 836 } 837 838 void release_free_pmem(struct nvdimm_bus *nvdimm_bus, 839 struct nd_mapping *nd_mapping) 840 { 841 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 842 struct resource *res, *_res; 843 844 for_each_dpa_resource_safe(ndd, res, _res) 845 if (strcmp(res->name, "pmem-reserve") == 0) 846 nvdimm_free_dpa(ndd, res); 847 } 848 849 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus, 850 struct nd_mapping *nd_mapping) 851 { 852 struct nvdimm *nvdimm = nd_mapping->nvdimm; 853 int rc; 854 855 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm, 856 __reserve_free_pmem); 857 if (rc) 858 release_free_pmem(nvdimm_bus, nd_mapping); 859 return rc; 860 } 861 862 /** 863 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id 864 * @nd_region: the set of dimms to allocate @n more bytes from 865 * @label_id: unique identifier for the namespace consuming this dpa range 866 * @n: number of bytes per-dimm to add to the existing allocation 867 * 868 * Assumes resources are ordered. For BLK regions, first consume 869 * BLK-only available DPA free space, then consume PMEM-aliased DPA 870 * space starting at the highest DPA. For PMEM regions start 871 * allocations from the start of an interleave set and end at the first 872 * BLK allocation or the end of the interleave set, whichever comes 873 * first. 874 */ 875 static int grow_dpa_allocation(struct nd_region *nd_region, 876 struct nd_label_id *label_id, resource_size_t n) 877 { 878 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); 879 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; 880 int i; 881 882 for (i = 0; i < nd_region->ndr_mappings; i++) { 883 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 884 resource_size_t rem = n; 885 int rc, j; 886 887 /* 888 * In the BLK case try once with all unallocated PMEM 889 * reserved, and once without 890 */ 891 for (j = is_pmem; j < 2; j++) { 892 bool blk_only = j == 0; 893 894 if (blk_only) { 895 rc = reserve_free_pmem(nvdimm_bus, nd_mapping); 896 if (rc) 897 return rc; 898 } 899 rem = scan_allocate(nd_region, nd_mapping, 900 label_id, rem); 901 if (blk_only) 902 release_free_pmem(nvdimm_bus, nd_mapping); 903 904 /* try again and allow encroachments into PMEM */ 905 if (rem == 0) 906 break; 907 } 908 909 dev_WARN_ONCE(&nd_region->dev, rem, 910 "allocation underrun: %#llx of %#llx bytes\n", 911 (unsigned long long) n - rem, 912 (unsigned long long) n); 913 if (rem) 914 return -ENXIO; 915 916 rc = merge_dpa(nd_region, nd_mapping, label_id); 917 if (rc) 918 return rc; 919 } 920 921 return 0; 922 } 923 924 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region, 925 struct nd_namespace_pmem *nspm, resource_size_t size) 926 { 927 struct resource *res = &nspm->nsio.res; 928 resource_size_t offset = 0; 929 930 if (size && !nspm->uuid) { 931 WARN_ON_ONCE(1); 932 size = 0; 933 } 934 935 if (size && nspm->uuid) { 936 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 937 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 938 struct nd_label_id label_id; 939 struct resource *res; 940 941 if (!ndd) { 942 size = 0; 943 goto out; 944 } 945 946 nd_label_gen_id(&label_id, nspm->uuid, 0); 947 948 /* calculate a spa offset from the dpa allocation offset */ 949 for_each_dpa_resource(ndd, res) 950 if (strcmp(res->name, label_id.id) == 0) { 951 offset = (res->start - nd_mapping->start) 952 * nd_region->ndr_mappings; 953 goto out; 954 } 955 956 WARN_ON_ONCE(1); 957 size = 0; 958 } 959 960 out: 961 res->start = nd_region->ndr_start + offset; 962 res->end = res->start + size - 1; 963 } 964 965 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where) 966 { 967 if (!uuid) { 968 dev_dbg(dev, "%s: uuid not set\n", where); 969 return true; 970 } 971 return false; 972 } 973 974 static ssize_t __size_store(struct device *dev, unsigned long long val) 975 { 976 resource_size_t allocated = 0, available = 0; 977 struct nd_region *nd_region = to_nd_region(dev->parent); 978 struct nd_namespace_common *ndns = to_ndns(dev); 979 struct nd_mapping *nd_mapping; 980 struct nvdimm_drvdata *ndd; 981 struct nd_label_id label_id; 982 u32 flags = 0, remainder; 983 int rc, i, id = -1; 984 u8 *uuid = NULL; 985 986 if (dev->driver || ndns->claim) 987 return -EBUSY; 988 989 if (is_namespace_pmem(dev)) { 990 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 991 992 uuid = nspm->uuid; 993 id = nspm->id; 994 } else if (is_namespace_blk(dev)) { 995 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 996 997 uuid = nsblk->uuid; 998 flags = NSLABEL_FLAG_LOCAL; 999 id = nsblk->id; 1000 } 1001 1002 /* 1003 * We need a uuid for the allocation-label and dimm(s) on which 1004 * to store the label. 1005 */ 1006 if (uuid_not_set(uuid, dev, __func__)) 1007 return -ENXIO; 1008 if (nd_region->ndr_mappings == 0) { 1009 dev_dbg(dev, "not associated with dimm(s)\n"); 1010 return -ENXIO; 1011 } 1012 1013 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder); 1014 if (remainder) { 1015 dev_dbg(dev, "%llu is not %dK aligned\n", val, 1016 (SZ_4K * nd_region->ndr_mappings) / SZ_1K); 1017 return -EINVAL; 1018 } 1019 1020 nd_label_gen_id(&label_id, uuid, flags); 1021 for (i = 0; i < nd_region->ndr_mappings; i++) { 1022 nd_mapping = &nd_region->mapping[i]; 1023 ndd = to_ndd(nd_mapping); 1024 1025 /* 1026 * All dimms in an interleave set, or the base dimm for a blk 1027 * region, need to be enabled for the size to be changed. 1028 */ 1029 if (!ndd) 1030 return -ENXIO; 1031 1032 allocated += nvdimm_allocated_dpa(ndd, &label_id); 1033 } 1034 available = nd_region_allocatable_dpa(nd_region); 1035 1036 if (val > available + allocated) 1037 return -ENOSPC; 1038 1039 if (val == allocated) 1040 return 0; 1041 1042 val = div_u64(val, nd_region->ndr_mappings); 1043 allocated = div_u64(allocated, nd_region->ndr_mappings); 1044 if (val < allocated) 1045 rc = shrink_dpa_allocation(nd_region, &label_id, 1046 allocated - val); 1047 else 1048 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated); 1049 1050 if (rc) 1051 return rc; 1052 1053 if (is_namespace_pmem(dev)) { 1054 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1055 1056 nd_namespace_pmem_set_resource(nd_region, nspm, 1057 val * nd_region->ndr_mappings); 1058 } 1059 1060 /* 1061 * Try to delete the namespace if we deleted all of its 1062 * allocation, this is not the seed or 0th device for the 1063 * region, and it is not actively claimed by a btt, pfn, or dax 1064 * instance. 1065 */ 1066 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim) 1067 nd_device_unregister(dev, ND_ASYNC); 1068 1069 return rc; 1070 } 1071 1072 static ssize_t size_store(struct device *dev, 1073 struct device_attribute *attr, const char *buf, size_t len) 1074 { 1075 struct nd_region *nd_region = to_nd_region(dev->parent); 1076 unsigned long long val; 1077 u8 **uuid = NULL; 1078 int rc; 1079 1080 rc = kstrtoull(buf, 0, &val); 1081 if (rc) 1082 return rc; 1083 1084 device_lock(dev); 1085 nvdimm_bus_lock(dev); 1086 wait_nvdimm_bus_probe_idle(dev); 1087 rc = __size_store(dev, val); 1088 if (rc >= 0) 1089 rc = nd_namespace_label_update(nd_region, dev); 1090 1091 if (is_namespace_pmem(dev)) { 1092 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1093 1094 uuid = &nspm->uuid; 1095 } else if (is_namespace_blk(dev)) { 1096 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1097 1098 uuid = &nsblk->uuid; 1099 } 1100 1101 if (rc == 0 && val == 0 && uuid) { 1102 /* setting size zero == 'delete namespace' */ 1103 kfree(*uuid); 1104 *uuid = NULL; 1105 } 1106 1107 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc); 1108 1109 nvdimm_bus_unlock(dev); 1110 device_unlock(dev); 1111 1112 return rc < 0 ? rc : len; 1113 } 1114 1115 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns) 1116 { 1117 struct device *dev = &ndns->dev; 1118 1119 if (is_namespace_pmem(dev)) { 1120 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1121 1122 return resource_size(&nspm->nsio.res); 1123 } else if (is_namespace_blk(dev)) { 1124 return nd_namespace_blk_size(to_nd_namespace_blk(dev)); 1125 } else if (is_namespace_io(dev)) { 1126 struct nd_namespace_io *nsio = to_nd_namespace_io(dev); 1127 1128 return resource_size(&nsio->res); 1129 } else 1130 WARN_ONCE(1, "unknown namespace type\n"); 1131 return 0; 1132 } 1133 1134 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) 1135 { 1136 resource_size_t size; 1137 1138 nvdimm_bus_lock(&ndns->dev); 1139 size = __nvdimm_namespace_capacity(ndns); 1140 nvdimm_bus_unlock(&ndns->dev); 1141 1142 return size; 1143 } 1144 EXPORT_SYMBOL(nvdimm_namespace_capacity); 1145 1146 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns) 1147 { 1148 int i; 1149 bool locked = false; 1150 struct device *dev = &ndns->dev; 1151 struct nd_region *nd_region = to_nd_region(dev->parent); 1152 1153 for (i = 0; i < nd_region->ndr_mappings; i++) { 1154 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1155 struct nvdimm *nvdimm = nd_mapping->nvdimm; 1156 1157 if (test_bit(NDD_LOCKED, &nvdimm->flags)) { 1158 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm)); 1159 locked = true; 1160 } 1161 } 1162 return locked; 1163 } 1164 EXPORT_SYMBOL(nvdimm_namespace_locked); 1165 1166 static ssize_t size_show(struct device *dev, 1167 struct device_attribute *attr, char *buf) 1168 { 1169 return sprintf(buf, "%llu\n", (unsigned long long) 1170 nvdimm_namespace_capacity(to_ndns(dev))); 1171 } 1172 static DEVICE_ATTR(size, 0444, size_show, size_store); 1173 1174 static u8 *namespace_to_uuid(struct device *dev) 1175 { 1176 if (is_namespace_pmem(dev)) { 1177 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1178 1179 return nspm->uuid; 1180 } else if (is_namespace_blk(dev)) { 1181 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1182 1183 return nsblk->uuid; 1184 } else 1185 return ERR_PTR(-ENXIO); 1186 } 1187 1188 static ssize_t uuid_show(struct device *dev, 1189 struct device_attribute *attr, char *buf) 1190 { 1191 u8 *uuid = namespace_to_uuid(dev); 1192 1193 if (IS_ERR(uuid)) 1194 return PTR_ERR(uuid); 1195 if (uuid) 1196 return sprintf(buf, "%pUb\n", uuid); 1197 return sprintf(buf, "\n"); 1198 } 1199 1200 /** 1201 * namespace_update_uuid - check for a unique uuid and whether we're "renaming" 1202 * @nd_region: parent region so we can updates all dimms in the set 1203 * @dev: namespace type for generating label_id 1204 * @new_uuid: incoming uuid 1205 * @old_uuid: reference to the uuid storage location in the namespace object 1206 */ 1207 static int namespace_update_uuid(struct nd_region *nd_region, 1208 struct device *dev, u8 *new_uuid, u8 **old_uuid) 1209 { 1210 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0; 1211 struct nd_label_id old_label_id; 1212 struct nd_label_id new_label_id; 1213 int i; 1214 1215 if (!nd_is_uuid_unique(dev, new_uuid)) 1216 return -EINVAL; 1217 1218 if (*old_uuid == NULL) 1219 goto out; 1220 1221 /* 1222 * If we've already written a label with this uuid, then it's 1223 * too late to rename because we can't reliably update the uuid 1224 * without losing the old namespace. Userspace must delete this 1225 * namespace to abandon the old uuid. 1226 */ 1227 for (i = 0; i < nd_region->ndr_mappings; i++) { 1228 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1229 1230 /* 1231 * This check by itself is sufficient because old_uuid 1232 * would be NULL above if this uuid did not exist in the 1233 * currently written set. 1234 * 1235 * FIXME: can we delete uuid with zero dpa allocated? 1236 */ 1237 if (list_empty(&nd_mapping->labels)) 1238 return -EBUSY; 1239 } 1240 1241 nd_label_gen_id(&old_label_id, *old_uuid, flags); 1242 nd_label_gen_id(&new_label_id, new_uuid, flags); 1243 for (i = 0; i < nd_region->ndr_mappings; i++) { 1244 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1245 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1246 struct resource *res; 1247 1248 for_each_dpa_resource(ndd, res) 1249 if (strcmp(res->name, old_label_id.id) == 0) 1250 sprintf((void *) res->name, "%s", 1251 new_label_id.id); 1252 } 1253 kfree(*old_uuid); 1254 out: 1255 *old_uuid = new_uuid; 1256 return 0; 1257 } 1258 1259 static ssize_t uuid_store(struct device *dev, 1260 struct device_attribute *attr, const char *buf, size_t len) 1261 { 1262 struct nd_region *nd_region = to_nd_region(dev->parent); 1263 u8 *uuid = NULL; 1264 ssize_t rc = 0; 1265 u8 **ns_uuid; 1266 1267 if (is_namespace_pmem(dev)) { 1268 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1269 1270 ns_uuid = &nspm->uuid; 1271 } else if (is_namespace_blk(dev)) { 1272 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1273 1274 ns_uuid = &nsblk->uuid; 1275 } else 1276 return -ENXIO; 1277 1278 device_lock(dev); 1279 nvdimm_bus_lock(dev); 1280 wait_nvdimm_bus_probe_idle(dev); 1281 if (to_ndns(dev)->claim) 1282 rc = -EBUSY; 1283 if (rc >= 0) 1284 rc = nd_uuid_store(dev, &uuid, buf, len); 1285 if (rc >= 0) 1286 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid); 1287 if (rc >= 0) 1288 rc = nd_namespace_label_update(nd_region, dev); 1289 else 1290 kfree(uuid); 1291 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, 1292 buf[len - 1] == '\n' ? "" : "\n"); 1293 nvdimm_bus_unlock(dev); 1294 device_unlock(dev); 1295 1296 return rc < 0 ? rc : len; 1297 } 1298 static DEVICE_ATTR_RW(uuid); 1299 1300 static ssize_t resource_show(struct device *dev, 1301 struct device_attribute *attr, char *buf) 1302 { 1303 struct resource *res; 1304 1305 if (is_namespace_pmem(dev)) { 1306 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1307 1308 res = &nspm->nsio.res; 1309 } else if (is_namespace_io(dev)) { 1310 struct nd_namespace_io *nsio = to_nd_namespace_io(dev); 1311 1312 res = &nsio->res; 1313 } else 1314 return -ENXIO; 1315 1316 /* no address to convey if the namespace has no allocation */ 1317 if (resource_size(res) == 0) 1318 return -ENXIO; 1319 return sprintf(buf, "%#llx\n", (unsigned long long) res->start); 1320 } 1321 static DEVICE_ATTR_RO(resource); 1322 1323 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528, 1324 4096, 4104, 4160, 4224, 0 }; 1325 1326 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 }; 1327 1328 static ssize_t sector_size_show(struct device *dev, 1329 struct device_attribute *attr, char *buf) 1330 { 1331 if (is_namespace_blk(dev)) { 1332 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1333 1334 return nd_size_select_show(nsblk->lbasize, 1335 blk_lbasize_supported, buf); 1336 } 1337 1338 if (is_namespace_pmem(dev)) { 1339 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1340 1341 return nd_size_select_show(nspm->lbasize, 1342 pmem_lbasize_supported, buf); 1343 } 1344 return -ENXIO; 1345 } 1346 1347 static ssize_t sector_size_store(struct device *dev, 1348 struct device_attribute *attr, const char *buf, size_t len) 1349 { 1350 struct nd_region *nd_region = to_nd_region(dev->parent); 1351 const unsigned long *supported; 1352 unsigned long *lbasize; 1353 ssize_t rc = 0; 1354 1355 if (is_namespace_blk(dev)) { 1356 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1357 1358 lbasize = &nsblk->lbasize; 1359 supported = blk_lbasize_supported; 1360 } else if (is_namespace_pmem(dev)) { 1361 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1362 1363 lbasize = &nspm->lbasize; 1364 supported = pmem_lbasize_supported; 1365 } else 1366 return -ENXIO; 1367 1368 device_lock(dev); 1369 nvdimm_bus_lock(dev); 1370 if (to_ndns(dev)->claim) 1371 rc = -EBUSY; 1372 if (rc >= 0) 1373 rc = nd_size_select_store(dev, buf, lbasize, supported); 1374 if (rc >= 0) 1375 rc = nd_namespace_label_update(nd_region, dev); 1376 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote", 1377 buf, buf[len - 1] == '\n' ? "" : "\n"); 1378 nvdimm_bus_unlock(dev); 1379 device_unlock(dev); 1380 1381 return rc ? rc : len; 1382 } 1383 static DEVICE_ATTR_RW(sector_size); 1384 1385 static ssize_t dpa_extents_show(struct device *dev, 1386 struct device_attribute *attr, char *buf) 1387 { 1388 struct nd_region *nd_region = to_nd_region(dev->parent); 1389 struct nd_label_id label_id; 1390 int count = 0, i; 1391 u8 *uuid = NULL; 1392 u32 flags = 0; 1393 1394 nvdimm_bus_lock(dev); 1395 if (is_namespace_pmem(dev)) { 1396 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1397 1398 uuid = nspm->uuid; 1399 flags = 0; 1400 } else if (is_namespace_blk(dev)) { 1401 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1402 1403 uuid = nsblk->uuid; 1404 flags = NSLABEL_FLAG_LOCAL; 1405 } 1406 1407 if (!uuid) 1408 goto out; 1409 1410 nd_label_gen_id(&label_id, uuid, flags); 1411 for (i = 0; i < nd_region->ndr_mappings; i++) { 1412 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1413 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1414 struct resource *res; 1415 1416 for_each_dpa_resource(ndd, res) 1417 if (strcmp(res->name, label_id.id) == 0) 1418 count++; 1419 } 1420 out: 1421 nvdimm_bus_unlock(dev); 1422 1423 return sprintf(buf, "%d\n", count); 1424 } 1425 static DEVICE_ATTR_RO(dpa_extents); 1426 1427 static int btt_claim_class(struct device *dev) 1428 { 1429 struct nd_region *nd_region = to_nd_region(dev->parent); 1430 int i, loop_bitmask = 0; 1431 1432 for (i = 0; i < nd_region->ndr_mappings; i++) { 1433 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1434 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1435 struct nd_namespace_index *nsindex; 1436 1437 /* 1438 * If any of the DIMMs do not support labels the only 1439 * possible BTT format is v1. 1440 */ 1441 if (!ndd) { 1442 loop_bitmask = 0; 1443 break; 1444 } 1445 1446 nsindex = to_namespace_index(ndd, ndd->ns_current); 1447 if (nsindex == NULL) 1448 loop_bitmask |= 1; 1449 else { 1450 /* check whether existing labels are v1.1 or v1.2 */ 1451 if (__le16_to_cpu(nsindex->major) == 1 1452 && __le16_to_cpu(nsindex->minor) == 1) 1453 loop_bitmask |= 2; 1454 else 1455 loop_bitmask |= 4; 1456 } 1457 } 1458 /* 1459 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index 1460 * block is found, a v1.1 label for any mapping will set bit 1, and a 1461 * v1.2 label will set bit 2. 1462 * 1463 * At the end of the loop, at most one of the three bits must be set. 1464 * If multiple bits were set, it means the different mappings disagree 1465 * about their labels, and this must be cleaned up first. 1466 * 1467 * If all the label index blocks are found to agree, nsindex of NULL 1468 * implies labels haven't been initialized yet, and when they will, 1469 * they will be of the 1.2 format, so we can assume BTT2.0 1470 * 1471 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are 1472 * found, we enforce BTT2.0 1473 * 1474 * If the loop was never entered, default to BTT1.1 (legacy namespaces) 1475 */ 1476 switch (loop_bitmask) { 1477 case 0: 1478 case 2: 1479 return NVDIMM_CCLASS_BTT; 1480 case 1: 1481 case 4: 1482 return NVDIMM_CCLASS_BTT2; 1483 default: 1484 return -ENXIO; 1485 } 1486 } 1487 1488 static ssize_t holder_show(struct device *dev, 1489 struct device_attribute *attr, char *buf) 1490 { 1491 struct nd_namespace_common *ndns = to_ndns(dev); 1492 ssize_t rc; 1493 1494 device_lock(dev); 1495 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); 1496 device_unlock(dev); 1497 1498 return rc; 1499 } 1500 static DEVICE_ATTR_RO(holder); 1501 1502 static ssize_t __holder_class_store(struct device *dev, const char *buf) 1503 { 1504 struct nd_namespace_common *ndns = to_ndns(dev); 1505 1506 if (dev->driver || ndns->claim) 1507 return -EBUSY; 1508 1509 if (strcmp(buf, "btt") == 0 || strcmp(buf, "btt\n") == 0) 1510 ndns->claim_class = btt_claim_class(dev); 1511 else if (strcmp(buf, "pfn") == 0 || strcmp(buf, "pfn\n") == 0) 1512 ndns->claim_class = NVDIMM_CCLASS_PFN; 1513 else if (strcmp(buf, "dax") == 0 || strcmp(buf, "dax\n") == 0) 1514 ndns->claim_class = NVDIMM_CCLASS_DAX; 1515 else if (strcmp(buf, "") == 0 || strcmp(buf, "\n") == 0) 1516 ndns->claim_class = NVDIMM_CCLASS_NONE; 1517 else 1518 return -EINVAL; 1519 1520 /* btt_claim_class() could've returned an error */ 1521 if (ndns->claim_class < 0) 1522 return ndns->claim_class; 1523 1524 return 0; 1525 } 1526 1527 static ssize_t holder_class_store(struct device *dev, 1528 struct device_attribute *attr, const char *buf, size_t len) 1529 { 1530 struct nd_region *nd_region = to_nd_region(dev->parent); 1531 ssize_t rc; 1532 1533 device_lock(dev); 1534 nvdimm_bus_lock(dev); 1535 wait_nvdimm_bus_probe_idle(dev); 1536 rc = __holder_class_store(dev, buf); 1537 if (rc >= 0) 1538 rc = nd_namespace_label_update(nd_region, dev); 1539 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc); 1540 nvdimm_bus_unlock(dev); 1541 device_unlock(dev); 1542 1543 return rc < 0 ? rc : len; 1544 } 1545 1546 static ssize_t holder_class_show(struct device *dev, 1547 struct device_attribute *attr, char *buf) 1548 { 1549 struct nd_namespace_common *ndns = to_ndns(dev); 1550 ssize_t rc; 1551 1552 device_lock(dev); 1553 if (ndns->claim_class == NVDIMM_CCLASS_NONE) 1554 rc = sprintf(buf, "\n"); 1555 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) || 1556 (ndns->claim_class == NVDIMM_CCLASS_BTT2)) 1557 rc = sprintf(buf, "btt\n"); 1558 else if (ndns->claim_class == NVDIMM_CCLASS_PFN) 1559 rc = sprintf(buf, "pfn\n"); 1560 else if (ndns->claim_class == NVDIMM_CCLASS_DAX) 1561 rc = sprintf(buf, "dax\n"); 1562 else 1563 rc = sprintf(buf, "<unknown>\n"); 1564 device_unlock(dev); 1565 1566 return rc; 1567 } 1568 static DEVICE_ATTR_RW(holder_class); 1569 1570 static ssize_t mode_show(struct device *dev, 1571 struct device_attribute *attr, char *buf) 1572 { 1573 struct nd_namespace_common *ndns = to_ndns(dev); 1574 struct device *claim; 1575 char *mode; 1576 ssize_t rc; 1577 1578 device_lock(dev); 1579 claim = ndns->claim; 1580 if (claim && is_nd_btt(claim)) 1581 mode = "safe"; 1582 else if (claim && is_nd_pfn(claim)) 1583 mode = "memory"; 1584 else if (claim && is_nd_dax(claim)) 1585 mode = "dax"; 1586 else if (!claim && pmem_should_map_pages(dev)) 1587 mode = "memory"; 1588 else 1589 mode = "raw"; 1590 rc = sprintf(buf, "%s\n", mode); 1591 device_unlock(dev); 1592 1593 return rc; 1594 } 1595 static DEVICE_ATTR_RO(mode); 1596 1597 static ssize_t force_raw_store(struct device *dev, 1598 struct device_attribute *attr, const char *buf, size_t len) 1599 { 1600 bool force_raw; 1601 int rc = strtobool(buf, &force_raw); 1602 1603 if (rc) 1604 return rc; 1605 1606 to_ndns(dev)->force_raw = force_raw; 1607 return len; 1608 } 1609 1610 static ssize_t force_raw_show(struct device *dev, 1611 struct device_attribute *attr, char *buf) 1612 { 1613 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw); 1614 } 1615 static DEVICE_ATTR_RW(force_raw); 1616 1617 static struct attribute *nd_namespace_attributes[] = { 1618 &dev_attr_nstype.attr, 1619 &dev_attr_size.attr, 1620 &dev_attr_mode.attr, 1621 &dev_attr_uuid.attr, 1622 &dev_attr_holder.attr, 1623 &dev_attr_resource.attr, 1624 &dev_attr_alt_name.attr, 1625 &dev_attr_force_raw.attr, 1626 &dev_attr_sector_size.attr, 1627 &dev_attr_dpa_extents.attr, 1628 &dev_attr_holder_class.attr, 1629 NULL, 1630 }; 1631 1632 static umode_t namespace_visible(struct kobject *kobj, 1633 struct attribute *a, int n) 1634 { 1635 struct device *dev = container_of(kobj, struct device, kobj); 1636 1637 if (a == &dev_attr_resource.attr) { 1638 if (is_namespace_blk(dev)) 1639 return 0; 1640 return 0400; 1641 } 1642 1643 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) { 1644 if (a == &dev_attr_size.attr) 1645 return 0644; 1646 1647 return a->mode; 1648 } 1649 1650 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr 1651 || a == &dev_attr_holder.attr 1652 || a == &dev_attr_holder_class.attr 1653 || a == &dev_attr_force_raw.attr 1654 || a == &dev_attr_mode.attr) 1655 return a->mode; 1656 1657 return 0; 1658 } 1659 1660 static struct attribute_group nd_namespace_attribute_group = { 1661 .attrs = nd_namespace_attributes, 1662 .is_visible = namespace_visible, 1663 }; 1664 1665 static const struct attribute_group *nd_namespace_attribute_groups[] = { 1666 &nd_device_attribute_group, 1667 &nd_namespace_attribute_group, 1668 &nd_numa_attribute_group, 1669 NULL, 1670 }; 1671 1672 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) 1673 { 1674 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; 1675 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL; 1676 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL; 1677 struct nd_namespace_common *ndns = NULL; 1678 resource_size_t size; 1679 1680 if (nd_btt || nd_pfn || nd_dax) { 1681 if (nd_btt) 1682 ndns = nd_btt->ndns; 1683 else if (nd_pfn) 1684 ndns = nd_pfn->ndns; 1685 else if (nd_dax) 1686 ndns = nd_dax->nd_pfn.ndns; 1687 1688 if (!ndns) 1689 return ERR_PTR(-ENODEV); 1690 1691 /* 1692 * Flush any in-progess probes / removals in the driver 1693 * for the raw personality of this namespace. 1694 */ 1695 device_lock(&ndns->dev); 1696 device_unlock(&ndns->dev); 1697 if (ndns->dev.driver) { 1698 dev_dbg(&ndns->dev, "is active, can't bind %s\n", 1699 dev_name(dev)); 1700 return ERR_PTR(-EBUSY); 1701 } 1702 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev, 1703 "host (%s) vs claim (%s) mismatch\n", 1704 dev_name(dev), 1705 dev_name(ndns->claim))) 1706 return ERR_PTR(-ENXIO); 1707 } else { 1708 ndns = to_ndns(dev); 1709 if (ndns->claim) { 1710 dev_dbg(dev, "claimed by %s, failing probe\n", 1711 dev_name(ndns->claim)); 1712 1713 return ERR_PTR(-ENXIO); 1714 } 1715 } 1716 1717 if (nvdimm_namespace_locked(ndns)) 1718 return ERR_PTR(-EACCES); 1719 1720 size = nvdimm_namespace_capacity(ndns); 1721 if (size < ND_MIN_NAMESPACE_SIZE) { 1722 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", 1723 &size, ND_MIN_NAMESPACE_SIZE); 1724 return ERR_PTR(-ENODEV); 1725 } 1726 1727 if (is_namespace_pmem(&ndns->dev)) { 1728 struct nd_namespace_pmem *nspm; 1729 1730 nspm = to_nd_namespace_pmem(&ndns->dev); 1731 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__)) 1732 return ERR_PTR(-ENODEV); 1733 } else if (is_namespace_blk(&ndns->dev)) { 1734 struct nd_namespace_blk *nsblk; 1735 1736 nsblk = to_nd_namespace_blk(&ndns->dev); 1737 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__)) 1738 return ERR_PTR(-ENODEV); 1739 if (!nsblk->lbasize) { 1740 dev_dbg(&ndns->dev, "sector size not set\n"); 1741 return ERR_PTR(-ENODEV); 1742 } 1743 if (!nd_namespace_blk_validate(nsblk)) 1744 return ERR_PTR(-ENODEV); 1745 } 1746 1747 return ndns; 1748 } 1749 EXPORT_SYMBOL(nvdimm_namespace_common_probe); 1750 1751 static struct device **create_namespace_io(struct nd_region *nd_region) 1752 { 1753 struct nd_namespace_io *nsio; 1754 struct device *dev, **devs; 1755 struct resource *res; 1756 1757 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL); 1758 if (!nsio) 1759 return NULL; 1760 1761 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); 1762 if (!devs) { 1763 kfree(nsio); 1764 return NULL; 1765 } 1766 1767 dev = &nsio->common.dev; 1768 dev->type = &namespace_io_device_type; 1769 dev->parent = &nd_region->dev; 1770 res = &nsio->res; 1771 res->name = dev_name(&nd_region->dev); 1772 res->flags = IORESOURCE_MEM; 1773 res->start = nd_region->ndr_start; 1774 res->end = res->start + nd_region->ndr_size - 1; 1775 1776 devs[0] = dev; 1777 return devs; 1778 } 1779 1780 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid, 1781 u64 cookie, u16 pos) 1782 { 1783 struct nd_namespace_label *found = NULL; 1784 int i; 1785 1786 for (i = 0; i < nd_region->ndr_mappings; i++) { 1787 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1788 struct nd_interleave_set *nd_set = nd_region->nd_set; 1789 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1790 struct nd_label_ent *label_ent; 1791 bool found_uuid = false; 1792 1793 list_for_each_entry(label_ent, &nd_mapping->labels, list) { 1794 struct nd_namespace_label *nd_label = label_ent->label; 1795 u16 position, nlabel; 1796 u64 isetcookie; 1797 1798 if (!nd_label) 1799 continue; 1800 isetcookie = __le64_to_cpu(nd_label->isetcookie); 1801 position = __le16_to_cpu(nd_label->position); 1802 nlabel = __le16_to_cpu(nd_label->nlabel); 1803 1804 if (isetcookie != cookie) 1805 continue; 1806 1807 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0) 1808 continue; 1809 1810 if (namespace_label_has(ndd, type_guid) 1811 && !guid_equal(&nd_set->type_guid, 1812 &nd_label->type_guid)) { 1813 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", 1814 nd_set->type_guid.b, 1815 nd_label->type_guid.b); 1816 continue; 1817 } 1818 1819 if (found_uuid) { 1820 dev_dbg(ndd->dev, "duplicate entry for uuid\n"); 1821 return false; 1822 } 1823 found_uuid = true; 1824 if (nlabel != nd_region->ndr_mappings) 1825 continue; 1826 if (position != pos) 1827 continue; 1828 found = nd_label; 1829 break; 1830 } 1831 if (found) 1832 break; 1833 } 1834 return found != NULL; 1835 } 1836 1837 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id) 1838 { 1839 int i; 1840 1841 if (!pmem_id) 1842 return -ENODEV; 1843 1844 for (i = 0; i < nd_region->ndr_mappings; i++) { 1845 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1846 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1847 struct nd_namespace_label *nd_label = NULL; 1848 u64 hw_start, hw_end, pmem_start, pmem_end; 1849 struct nd_label_ent *label_ent; 1850 1851 lockdep_assert_held(&nd_mapping->lock); 1852 list_for_each_entry(label_ent, &nd_mapping->labels, list) { 1853 nd_label = label_ent->label; 1854 if (!nd_label) 1855 continue; 1856 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0) 1857 break; 1858 nd_label = NULL; 1859 } 1860 1861 if (!nd_label) { 1862 WARN_ON(1); 1863 return -EINVAL; 1864 } 1865 1866 /* 1867 * Check that this label is compliant with the dpa 1868 * range published in NFIT 1869 */ 1870 hw_start = nd_mapping->start; 1871 hw_end = hw_start + nd_mapping->size; 1872 pmem_start = __le64_to_cpu(nd_label->dpa); 1873 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize); 1874 if (pmem_start >= hw_start && pmem_start < hw_end 1875 && pmem_end <= hw_end && pmem_end > hw_start) 1876 /* pass */; 1877 else { 1878 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n", 1879 dev_name(ndd->dev), nd_label->uuid); 1880 return -EINVAL; 1881 } 1882 1883 /* move recently validated label to the front of the list */ 1884 list_move(&label_ent->list, &nd_mapping->labels); 1885 } 1886 return 0; 1887 } 1888 1889 /** 1890 * create_namespace_pmem - validate interleave set labelling, retrieve label0 1891 * @nd_region: region with mappings to validate 1892 * @nspm: target namespace to create 1893 * @nd_label: target pmem namespace label to evaluate 1894 */ 1895 static struct device *create_namespace_pmem(struct nd_region *nd_region, 1896 struct nd_namespace_index *nsindex, 1897 struct nd_namespace_label *nd_label) 1898 { 1899 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex); 1900 u64 altcookie = nd_region_interleave_set_altcookie(nd_region); 1901 struct nd_label_ent *label_ent; 1902 struct nd_namespace_pmem *nspm; 1903 struct nd_mapping *nd_mapping; 1904 resource_size_t size = 0; 1905 struct resource *res; 1906 struct device *dev; 1907 int rc = 0; 1908 u16 i; 1909 1910 if (cookie == 0) { 1911 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n"); 1912 return ERR_PTR(-ENXIO); 1913 } 1914 1915 if (__le64_to_cpu(nd_label->isetcookie) != cookie) { 1916 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n", 1917 nd_label->uuid); 1918 if (__le64_to_cpu(nd_label->isetcookie) != altcookie) 1919 return ERR_PTR(-EAGAIN); 1920 1921 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n", 1922 nd_label->uuid); 1923 } 1924 1925 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); 1926 if (!nspm) 1927 return ERR_PTR(-ENOMEM); 1928 1929 nspm->id = -1; 1930 dev = &nspm->nsio.common.dev; 1931 dev->type = &namespace_pmem_device_type; 1932 dev->parent = &nd_region->dev; 1933 res = &nspm->nsio.res; 1934 res->name = dev_name(&nd_region->dev); 1935 res->flags = IORESOURCE_MEM; 1936 1937 for (i = 0; i < nd_region->ndr_mappings; i++) { 1938 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i)) 1939 continue; 1940 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i)) 1941 continue; 1942 break; 1943 } 1944 1945 if (i < nd_region->ndr_mappings) { 1946 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm; 1947 1948 /* 1949 * Give up if we don't find an instance of a uuid at each 1950 * position (from 0 to nd_region->ndr_mappings - 1), or if we 1951 * find a dimm with two instances of the same uuid. 1952 */ 1953 dev_err(&nd_region->dev, "%s missing label for %pUb\n", 1954 nvdimm_name(nvdimm), nd_label->uuid); 1955 rc = -EINVAL; 1956 goto err; 1957 } 1958 1959 /* 1960 * Fix up each mapping's 'labels' to have the validated pmem label for 1961 * that position at labels[0], and NULL at labels[1]. In the process, 1962 * check that the namespace aligns with interleave-set. We know 1963 * that it does not overlap with any blk namespaces by virtue of 1964 * the dimm being enabled (i.e. nd_label_reserve_dpa() 1965 * succeeded). 1966 */ 1967 rc = select_pmem_id(nd_region, nd_label->uuid); 1968 if (rc) 1969 goto err; 1970 1971 /* Calculate total size and populate namespace properties from label0 */ 1972 for (i = 0; i < nd_region->ndr_mappings; i++) { 1973 struct nd_namespace_label *label0; 1974 struct nvdimm_drvdata *ndd; 1975 1976 nd_mapping = &nd_region->mapping[i]; 1977 label_ent = list_first_entry_or_null(&nd_mapping->labels, 1978 typeof(*label_ent), list); 1979 label0 = label_ent ? label_ent->label : 0; 1980 1981 if (!label0) { 1982 WARN_ON(1); 1983 continue; 1984 } 1985 1986 size += __le64_to_cpu(label0->rawsize); 1987 if (__le16_to_cpu(label0->position) != 0) 1988 continue; 1989 WARN_ON(nspm->alt_name || nspm->uuid); 1990 nspm->alt_name = kmemdup((void __force *) label0->name, 1991 NSLABEL_NAME_LEN, GFP_KERNEL); 1992 nspm->uuid = kmemdup((void __force *) label0->uuid, 1993 NSLABEL_UUID_LEN, GFP_KERNEL); 1994 nspm->lbasize = __le64_to_cpu(label0->lbasize); 1995 ndd = to_ndd(nd_mapping); 1996 if (namespace_label_has(ndd, abstraction_guid)) 1997 nspm->nsio.common.claim_class 1998 = to_nvdimm_cclass(&label0->abstraction_guid); 1999 2000 } 2001 2002 if (!nspm->alt_name || !nspm->uuid) { 2003 rc = -ENOMEM; 2004 goto err; 2005 } 2006 2007 nd_namespace_pmem_set_resource(nd_region, nspm, size); 2008 2009 return dev; 2010 err: 2011 namespace_pmem_release(dev); 2012 switch (rc) { 2013 case -EINVAL: 2014 dev_dbg(&nd_region->dev, "invalid label(s)\n"); 2015 break; 2016 case -ENODEV: 2017 dev_dbg(&nd_region->dev, "label not found\n"); 2018 break; 2019 default: 2020 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc); 2021 break; 2022 } 2023 return ERR_PTR(rc); 2024 } 2025 2026 struct resource *nsblk_add_resource(struct nd_region *nd_region, 2027 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk, 2028 resource_size_t start) 2029 { 2030 struct nd_label_id label_id; 2031 struct resource *res; 2032 2033 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); 2034 res = krealloc(nsblk->res, 2035 sizeof(void *) * (nsblk->num_resources + 1), 2036 GFP_KERNEL); 2037 if (!res) 2038 return NULL; 2039 nsblk->res = (struct resource **) res; 2040 for_each_dpa_resource(ndd, res) 2041 if (strcmp(res->name, label_id.id) == 0 2042 && res->start == start) { 2043 nsblk->res[nsblk->num_resources++] = res; 2044 return res; 2045 } 2046 return NULL; 2047 } 2048 2049 static struct device *nd_namespace_blk_create(struct nd_region *nd_region) 2050 { 2051 struct nd_namespace_blk *nsblk; 2052 struct device *dev; 2053 2054 if (!is_nd_blk(&nd_region->dev)) 2055 return NULL; 2056 2057 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); 2058 if (!nsblk) 2059 return NULL; 2060 2061 dev = &nsblk->common.dev; 2062 dev->type = &namespace_blk_device_type; 2063 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); 2064 if (nsblk->id < 0) { 2065 kfree(nsblk); 2066 return NULL; 2067 } 2068 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id); 2069 dev->parent = &nd_region->dev; 2070 dev->groups = nd_namespace_attribute_groups; 2071 2072 return &nsblk->common.dev; 2073 } 2074 2075 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region) 2076 { 2077 struct nd_namespace_pmem *nspm; 2078 struct resource *res; 2079 struct device *dev; 2080 2081 if (!is_memory(&nd_region->dev)) 2082 return NULL; 2083 2084 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); 2085 if (!nspm) 2086 return NULL; 2087 2088 dev = &nspm->nsio.common.dev; 2089 dev->type = &namespace_pmem_device_type; 2090 dev->parent = &nd_region->dev; 2091 res = &nspm->nsio.res; 2092 res->name = dev_name(&nd_region->dev); 2093 res->flags = IORESOURCE_MEM; 2094 2095 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); 2096 if (nspm->id < 0) { 2097 kfree(nspm); 2098 return NULL; 2099 } 2100 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id); 2101 dev->groups = nd_namespace_attribute_groups; 2102 nd_namespace_pmem_set_resource(nd_region, nspm, 0); 2103 2104 return dev; 2105 } 2106 2107 void nd_region_create_ns_seed(struct nd_region *nd_region) 2108 { 2109 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 2110 2111 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO) 2112 return; 2113 2114 if (is_nd_blk(&nd_region->dev)) 2115 nd_region->ns_seed = nd_namespace_blk_create(nd_region); 2116 else 2117 nd_region->ns_seed = nd_namespace_pmem_create(nd_region); 2118 2119 /* 2120 * Seed creation failures are not fatal, provisioning is simply 2121 * disabled until memory becomes available 2122 */ 2123 if (!nd_region->ns_seed) 2124 dev_err(&nd_region->dev, "failed to create %s namespace\n", 2125 is_nd_blk(&nd_region->dev) ? "blk" : "pmem"); 2126 else 2127 nd_device_register(nd_region->ns_seed); 2128 } 2129 2130 void nd_region_create_dax_seed(struct nd_region *nd_region) 2131 { 2132 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 2133 nd_region->dax_seed = nd_dax_create(nd_region); 2134 /* 2135 * Seed creation failures are not fatal, provisioning is simply 2136 * disabled until memory becomes available 2137 */ 2138 if (!nd_region->dax_seed) 2139 dev_err(&nd_region->dev, "failed to create dax namespace\n"); 2140 } 2141 2142 void nd_region_create_pfn_seed(struct nd_region *nd_region) 2143 { 2144 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 2145 nd_region->pfn_seed = nd_pfn_create(nd_region); 2146 /* 2147 * Seed creation failures are not fatal, provisioning is simply 2148 * disabled until memory becomes available 2149 */ 2150 if (!nd_region->pfn_seed) 2151 dev_err(&nd_region->dev, "failed to create pfn namespace\n"); 2152 } 2153 2154 void nd_region_create_btt_seed(struct nd_region *nd_region) 2155 { 2156 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 2157 nd_region->btt_seed = nd_btt_create(nd_region); 2158 /* 2159 * Seed creation failures are not fatal, provisioning is simply 2160 * disabled until memory becomes available 2161 */ 2162 if (!nd_region->btt_seed) 2163 dev_err(&nd_region->dev, "failed to create btt namespace\n"); 2164 } 2165 2166 static int add_namespace_resource(struct nd_region *nd_region, 2167 struct nd_namespace_label *nd_label, struct device **devs, 2168 int count) 2169 { 2170 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 2171 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 2172 int i; 2173 2174 for (i = 0; i < count; i++) { 2175 u8 *uuid = namespace_to_uuid(devs[i]); 2176 struct resource *res; 2177 2178 if (IS_ERR_OR_NULL(uuid)) { 2179 WARN_ON(1); 2180 continue; 2181 } 2182 2183 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0) 2184 continue; 2185 if (is_namespace_blk(devs[i])) { 2186 res = nsblk_add_resource(nd_region, ndd, 2187 to_nd_namespace_blk(devs[i]), 2188 __le64_to_cpu(nd_label->dpa)); 2189 if (!res) 2190 return -ENXIO; 2191 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count); 2192 } else { 2193 dev_err(&nd_region->dev, 2194 "error: conflicting extents for uuid: %pUb\n", 2195 nd_label->uuid); 2196 return -ENXIO; 2197 } 2198 break; 2199 } 2200 2201 return i; 2202 } 2203 2204 static struct device *create_namespace_blk(struct nd_region *nd_region, 2205 struct nd_namespace_label *nd_label, int count) 2206 { 2207 2208 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 2209 struct nd_interleave_set *nd_set = nd_region->nd_set; 2210 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 2211 struct nd_namespace_blk *nsblk; 2212 char name[NSLABEL_NAME_LEN]; 2213 struct device *dev = NULL; 2214 struct resource *res; 2215 2216 if (namespace_label_has(ndd, type_guid)) { 2217 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) { 2218 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", 2219 nd_set->type_guid.b, 2220 nd_label->type_guid.b); 2221 return ERR_PTR(-EAGAIN); 2222 } 2223 2224 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) { 2225 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n", 2226 nd_set->cookie2, 2227 __le64_to_cpu(nd_label->isetcookie)); 2228 return ERR_PTR(-EAGAIN); 2229 } 2230 } 2231 2232 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); 2233 if (!nsblk) 2234 return ERR_PTR(-ENOMEM); 2235 dev = &nsblk->common.dev; 2236 dev->type = &namespace_blk_device_type; 2237 dev->parent = &nd_region->dev; 2238 nsblk->id = -1; 2239 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize); 2240 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN, 2241 GFP_KERNEL); 2242 if (namespace_label_has(ndd, abstraction_guid)) 2243 nsblk->common.claim_class 2244 = to_nvdimm_cclass(&nd_label->abstraction_guid); 2245 if (!nsblk->uuid) 2246 goto blk_err; 2247 memcpy(name, nd_label->name, NSLABEL_NAME_LEN); 2248 if (name[0]) 2249 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, 2250 GFP_KERNEL); 2251 res = nsblk_add_resource(nd_region, ndd, nsblk, 2252 __le64_to_cpu(nd_label->dpa)); 2253 if (!res) 2254 goto blk_err; 2255 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count); 2256 return dev; 2257 blk_err: 2258 namespace_blk_release(dev); 2259 return ERR_PTR(-ENXIO); 2260 } 2261 2262 static int cmp_dpa(const void *a, const void *b) 2263 { 2264 const struct device *dev_a = *(const struct device **) a; 2265 const struct device *dev_b = *(const struct device **) b; 2266 struct nd_namespace_blk *nsblk_a, *nsblk_b; 2267 struct nd_namespace_pmem *nspm_a, *nspm_b; 2268 2269 if (is_namespace_io(dev_a)) 2270 return 0; 2271 2272 if (is_namespace_blk(dev_a)) { 2273 nsblk_a = to_nd_namespace_blk(dev_a); 2274 nsblk_b = to_nd_namespace_blk(dev_b); 2275 2276 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start, 2277 sizeof(resource_size_t)); 2278 } 2279 2280 nspm_a = to_nd_namespace_pmem(dev_a); 2281 nspm_b = to_nd_namespace_pmem(dev_b); 2282 2283 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start, 2284 sizeof(resource_size_t)); 2285 } 2286 2287 static struct device **scan_labels(struct nd_region *nd_region) 2288 { 2289 int i, count = 0; 2290 struct device *dev, **devs = NULL; 2291 struct nd_label_ent *label_ent, *e; 2292 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 2293 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1; 2294 2295 /* "safe" because create_namespace_pmem() might list_move() label_ent */ 2296 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { 2297 struct nd_namespace_label *nd_label = label_ent->label; 2298 struct device **__devs; 2299 u32 flags; 2300 2301 if (!nd_label) 2302 continue; 2303 flags = __le32_to_cpu(nd_label->flags); 2304 if (is_nd_blk(&nd_region->dev) 2305 == !!(flags & NSLABEL_FLAG_LOCAL)) 2306 /* pass, region matches label type */; 2307 else 2308 continue; 2309 2310 /* skip labels that describe extents outside of the region */ 2311 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end) 2312 continue; 2313 2314 i = add_namespace_resource(nd_region, nd_label, devs, count); 2315 if (i < 0) 2316 goto err; 2317 if (i < count) 2318 continue; 2319 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL); 2320 if (!__devs) 2321 goto err; 2322 memcpy(__devs, devs, sizeof(dev) * count); 2323 kfree(devs); 2324 devs = __devs; 2325 2326 if (is_nd_blk(&nd_region->dev)) 2327 dev = create_namespace_blk(nd_region, nd_label, count); 2328 else { 2329 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 2330 struct nd_namespace_index *nsindex; 2331 2332 nsindex = to_namespace_index(ndd, ndd->ns_current); 2333 dev = create_namespace_pmem(nd_region, nsindex, nd_label); 2334 } 2335 2336 if (IS_ERR(dev)) { 2337 switch (PTR_ERR(dev)) { 2338 case -EAGAIN: 2339 /* skip invalid labels */ 2340 continue; 2341 case -ENODEV: 2342 /* fallthrough to seed creation */ 2343 break; 2344 default: 2345 goto err; 2346 } 2347 } else 2348 devs[count++] = dev; 2349 2350 } 2351 2352 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n", 2353 count, is_nd_blk(&nd_region->dev) 2354 ? "blk" : "pmem", count == 1 ? "" : "s"); 2355 2356 if (count == 0) { 2357 /* Publish a zero-sized namespace for userspace to configure. */ 2358 nd_mapping_free_labels(nd_mapping); 2359 2360 devs = kcalloc(2, sizeof(dev), GFP_KERNEL); 2361 if (!devs) 2362 goto err; 2363 if (is_nd_blk(&nd_region->dev)) { 2364 struct nd_namespace_blk *nsblk; 2365 2366 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); 2367 if (!nsblk) 2368 goto err; 2369 dev = &nsblk->common.dev; 2370 dev->type = &namespace_blk_device_type; 2371 } else { 2372 struct nd_namespace_pmem *nspm; 2373 2374 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); 2375 if (!nspm) 2376 goto err; 2377 dev = &nspm->nsio.common.dev; 2378 dev->type = &namespace_pmem_device_type; 2379 nd_namespace_pmem_set_resource(nd_region, nspm, 0); 2380 } 2381 dev->parent = &nd_region->dev; 2382 devs[count++] = dev; 2383 } else if (is_memory(&nd_region->dev)) { 2384 /* clean unselected labels */ 2385 for (i = 0; i < nd_region->ndr_mappings; i++) { 2386 struct list_head *l, *e; 2387 LIST_HEAD(list); 2388 int j; 2389 2390 nd_mapping = &nd_region->mapping[i]; 2391 if (list_empty(&nd_mapping->labels)) { 2392 WARN_ON(1); 2393 continue; 2394 } 2395 2396 j = count; 2397 list_for_each_safe(l, e, &nd_mapping->labels) { 2398 if (!j--) 2399 break; 2400 list_move_tail(l, &list); 2401 } 2402 nd_mapping_free_labels(nd_mapping); 2403 list_splice_init(&list, &nd_mapping->labels); 2404 } 2405 } 2406 2407 if (count > 1) 2408 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL); 2409 2410 return devs; 2411 2412 err: 2413 if (devs) { 2414 for (i = 0; devs[i]; i++) 2415 if (is_nd_blk(&nd_region->dev)) 2416 namespace_blk_release(devs[i]); 2417 else 2418 namespace_pmem_release(devs[i]); 2419 kfree(devs); 2420 } 2421 return NULL; 2422 } 2423 2424 static struct device **create_namespaces(struct nd_region *nd_region) 2425 { 2426 struct nd_mapping *nd_mapping; 2427 struct device **devs; 2428 int i; 2429 2430 if (nd_region->ndr_mappings == 0) 2431 return NULL; 2432 2433 /* lock down all mappings while we scan labels */ 2434 for (i = 0; i < nd_region->ndr_mappings; i++) { 2435 nd_mapping = &nd_region->mapping[i]; 2436 mutex_lock_nested(&nd_mapping->lock, i); 2437 } 2438 2439 devs = scan_labels(nd_region); 2440 2441 for (i = 0; i < nd_region->ndr_mappings; i++) { 2442 int reverse = nd_region->ndr_mappings - 1 - i; 2443 2444 nd_mapping = &nd_region->mapping[reverse]; 2445 mutex_unlock(&nd_mapping->lock); 2446 } 2447 2448 return devs; 2449 } 2450 2451 static int init_active_labels(struct nd_region *nd_region) 2452 { 2453 int i; 2454 2455 for (i = 0; i < nd_region->ndr_mappings; i++) { 2456 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 2457 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 2458 struct nvdimm *nvdimm = nd_mapping->nvdimm; 2459 struct nd_label_ent *label_ent; 2460 int count, j; 2461 2462 /* 2463 * If the dimm is disabled then we may need to prevent 2464 * the region from being activated. 2465 */ 2466 if (!ndd) { 2467 if (test_bit(NDD_LOCKED, &nvdimm->flags)) 2468 /* fail, label data may be unreadable */; 2469 else if (test_bit(NDD_ALIASING, &nvdimm->flags)) 2470 /* fail, labels needed to disambiguate dpa */; 2471 else 2472 return 0; 2473 2474 dev_err(&nd_region->dev, "%s: is %s, failing probe\n", 2475 dev_name(&nd_mapping->nvdimm->dev), 2476 test_bit(NDD_LOCKED, &nvdimm->flags) 2477 ? "locked" : "disabled"); 2478 return -ENXIO; 2479 } 2480 nd_mapping->ndd = ndd; 2481 atomic_inc(&nvdimm->busy); 2482 get_ndd(ndd); 2483 2484 count = nd_label_active_count(ndd); 2485 dev_dbg(ndd->dev, "count: %d\n", count); 2486 if (!count) 2487 continue; 2488 for (j = 0; j < count; j++) { 2489 struct nd_namespace_label *label; 2490 2491 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL); 2492 if (!label_ent) 2493 break; 2494 label = nd_label_active(ndd, j); 2495 label_ent->label = label; 2496 2497 mutex_lock(&nd_mapping->lock); 2498 list_add_tail(&label_ent->list, &nd_mapping->labels); 2499 mutex_unlock(&nd_mapping->lock); 2500 } 2501 2502 if (j >= count) 2503 continue; 2504 2505 mutex_lock(&nd_mapping->lock); 2506 nd_mapping_free_labels(nd_mapping); 2507 mutex_unlock(&nd_mapping->lock); 2508 return -ENOMEM; 2509 } 2510 2511 return 0; 2512 } 2513 2514 int nd_region_register_namespaces(struct nd_region *nd_region, int *err) 2515 { 2516 struct device **devs = NULL; 2517 int i, rc = 0, type; 2518 2519 *err = 0; 2520 nvdimm_bus_lock(&nd_region->dev); 2521 rc = init_active_labels(nd_region); 2522 if (rc) { 2523 nvdimm_bus_unlock(&nd_region->dev); 2524 return rc; 2525 } 2526 2527 type = nd_region_to_nstype(nd_region); 2528 switch (type) { 2529 case ND_DEVICE_NAMESPACE_IO: 2530 devs = create_namespace_io(nd_region); 2531 break; 2532 case ND_DEVICE_NAMESPACE_PMEM: 2533 case ND_DEVICE_NAMESPACE_BLK: 2534 devs = create_namespaces(nd_region); 2535 break; 2536 default: 2537 break; 2538 } 2539 nvdimm_bus_unlock(&nd_region->dev); 2540 2541 if (!devs) 2542 return -ENODEV; 2543 2544 for (i = 0; devs[i]; i++) { 2545 struct device *dev = devs[i]; 2546 int id; 2547 2548 if (type == ND_DEVICE_NAMESPACE_BLK) { 2549 struct nd_namespace_blk *nsblk; 2550 2551 nsblk = to_nd_namespace_blk(dev); 2552 id = ida_simple_get(&nd_region->ns_ida, 0, 0, 2553 GFP_KERNEL); 2554 nsblk->id = id; 2555 } else if (type == ND_DEVICE_NAMESPACE_PMEM) { 2556 struct nd_namespace_pmem *nspm; 2557 2558 nspm = to_nd_namespace_pmem(dev); 2559 id = ida_simple_get(&nd_region->ns_ida, 0, 0, 2560 GFP_KERNEL); 2561 nspm->id = id; 2562 } else 2563 id = i; 2564 2565 if (id < 0) 2566 break; 2567 dev_set_name(dev, "namespace%d.%d", nd_region->id, id); 2568 dev->groups = nd_namespace_attribute_groups; 2569 nd_device_register(dev); 2570 } 2571 if (i) 2572 nd_region->ns_seed = devs[0]; 2573 2574 if (devs[i]) { 2575 int j; 2576 2577 for (j = i; devs[j]; j++) { 2578 struct device *dev = devs[j]; 2579 2580 device_initialize(dev); 2581 put_device(dev); 2582 } 2583 *err = j - i; 2584 /* 2585 * All of the namespaces we tried to register failed, so 2586 * fail region activation. 2587 */ 2588 if (*err == 0) 2589 rc = -ENODEV; 2590 } 2591 kfree(devs); 2592 2593 if (rc == -ENODEV) 2594 return rc; 2595 2596 return i; 2597 } 2598