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/slab.h> 16 #include <linux/pmem.h> 17 #include <linux/nd.h> 18 #include "nd-core.h" 19 #include "nd.h" 20 21 static void namespace_io_release(struct device *dev) 22 { 23 struct nd_namespace_io *nsio = to_nd_namespace_io(dev); 24 25 kfree(nsio); 26 } 27 28 static void namespace_pmem_release(struct device *dev) 29 { 30 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 31 32 kfree(nspm->alt_name); 33 kfree(nspm->uuid); 34 kfree(nspm); 35 } 36 37 static void namespace_blk_release(struct device *dev) 38 { 39 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 40 struct nd_region *nd_region = to_nd_region(dev->parent); 41 42 if (nsblk->id >= 0) 43 ida_simple_remove(&nd_region->ns_ida, nsblk->id); 44 kfree(nsblk->alt_name); 45 kfree(nsblk->uuid); 46 kfree(nsblk->res); 47 kfree(nsblk); 48 } 49 50 static struct device_type namespace_io_device_type = { 51 .name = "nd_namespace_io", 52 .release = namespace_io_release, 53 }; 54 55 static struct device_type namespace_pmem_device_type = { 56 .name = "nd_namespace_pmem", 57 .release = namespace_pmem_release, 58 }; 59 60 static struct device_type namespace_blk_device_type = { 61 .name = "nd_namespace_blk", 62 .release = namespace_blk_release, 63 }; 64 65 static bool is_namespace_pmem(struct device *dev) 66 { 67 return dev ? dev->type == &namespace_pmem_device_type : false; 68 } 69 70 static bool is_namespace_blk(struct device *dev) 71 { 72 return dev ? dev->type == &namespace_blk_device_type : false; 73 } 74 75 static bool is_namespace_io(struct device *dev) 76 { 77 return dev ? dev->type == &namespace_io_device_type : false; 78 } 79 80 bool pmem_should_map_pages(struct device *dev) 81 { 82 struct nd_region *nd_region = to_nd_region(dev->parent); 83 84 if (!IS_ENABLED(CONFIG_ZONE_DEVICE)) 85 return false; 86 87 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags)) 88 return false; 89 90 if (is_nd_pfn(dev) || is_nd_btt(dev)) 91 return false; 92 93 #ifdef ARCH_MEMREMAP_PMEM 94 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB; 95 #else 96 return false; 97 #endif 98 } 99 EXPORT_SYMBOL(pmem_should_map_pages); 100 101 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, 102 char *name) 103 { 104 struct nd_region *nd_region = to_nd_region(ndns->dev.parent); 105 const char *suffix = NULL; 106 107 if (ndns->claim) { 108 if (is_nd_btt(ndns->claim)) 109 suffix = "s"; 110 else if (is_nd_pfn(ndns->claim)) 111 suffix = "m"; 112 else 113 dev_WARN_ONCE(&ndns->dev, 1, 114 "unknown claim type by %s\n", 115 dev_name(ndns->claim)); 116 } 117 118 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) { 119 if (!suffix && pmem_should_map_pages(&ndns->dev)) 120 suffix = "m"; 121 sprintf(name, "pmem%d%s", nd_region->id, suffix ? suffix : ""); 122 } else if (is_namespace_blk(&ndns->dev)) { 123 struct nd_namespace_blk *nsblk; 124 125 nsblk = to_nd_namespace_blk(&ndns->dev); 126 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, 127 suffix ? suffix : ""); 128 } else { 129 return NULL; 130 } 131 132 return name; 133 } 134 EXPORT_SYMBOL(nvdimm_namespace_disk_name); 135 136 const u8 *nd_dev_to_uuid(struct device *dev) 137 { 138 static const u8 null_uuid[16]; 139 140 if (!dev) 141 return null_uuid; 142 143 if (is_namespace_pmem(dev)) { 144 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 145 146 return nspm->uuid; 147 } else if (is_namespace_blk(dev)) { 148 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 149 150 return nsblk->uuid; 151 } else 152 return null_uuid; 153 } 154 EXPORT_SYMBOL(nd_dev_to_uuid); 155 156 static ssize_t nstype_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158 { 159 struct nd_region *nd_region = to_nd_region(dev->parent); 160 161 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); 162 } 163 static DEVICE_ATTR_RO(nstype); 164 165 static ssize_t __alt_name_store(struct device *dev, const char *buf, 166 const size_t len) 167 { 168 char *input, *pos, *alt_name, **ns_altname; 169 ssize_t rc; 170 171 if (is_namespace_pmem(dev)) { 172 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 173 174 ns_altname = &nspm->alt_name; 175 } else if (is_namespace_blk(dev)) { 176 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 177 178 ns_altname = &nsblk->alt_name; 179 } else 180 return -ENXIO; 181 182 if (dev->driver || to_ndns(dev)->claim) 183 return -EBUSY; 184 185 input = kmemdup(buf, len + 1, GFP_KERNEL); 186 if (!input) 187 return -ENOMEM; 188 189 input[len] = '\0'; 190 pos = strim(input); 191 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) { 192 rc = -EINVAL; 193 goto out; 194 } 195 196 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL); 197 if (!alt_name) { 198 rc = -ENOMEM; 199 goto out; 200 } 201 kfree(*ns_altname); 202 *ns_altname = alt_name; 203 sprintf(*ns_altname, "%s", pos); 204 rc = len; 205 206 out: 207 kfree(input); 208 return rc; 209 } 210 211 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk) 212 { 213 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); 214 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 215 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 216 struct nd_label_id label_id; 217 resource_size_t size = 0; 218 struct resource *res; 219 220 if (!nsblk->uuid) 221 return 0; 222 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); 223 for_each_dpa_resource(ndd, res) 224 if (strcmp(res->name, label_id.id) == 0) 225 size += resource_size(res); 226 return size; 227 } 228 229 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) 230 { 231 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); 232 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 233 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 234 struct nd_label_id label_id; 235 struct resource *res; 236 int count, i; 237 238 if (!nsblk->uuid || !nsblk->lbasize || !ndd) 239 return false; 240 241 count = 0; 242 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); 243 for_each_dpa_resource(ndd, res) { 244 if (strcmp(res->name, label_id.id) != 0) 245 continue; 246 /* 247 * Resources with unacknoweldged adjustments indicate a 248 * failure to update labels 249 */ 250 if (res->flags & DPA_RESOURCE_ADJUSTED) 251 return false; 252 count++; 253 } 254 255 /* These values match after a successful label update */ 256 if (count != nsblk->num_resources) 257 return false; 258 259 for (i = 0; i < nsblk->num_resources; i++) { 260 struct resource *found = NULL; 261 262 for_each_dpa_resource(ndd, res) 263 if (res == nsblk->res[i]) { 264 found = res; 265 break; 266 } 267 /* stale resource */ 268 if (!found) 269 return false; 270 } 271 272 return true; 273 } 274 275 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) 276 { 277 resource_size_t size; 278 279 nvdimm_bus_lock(&nsblk->common.dev); 280 size = __nd_namespace_blk_validate(nsblk); 281 nvdimm_bus_unlock(&nsblk->common.dev); 282 283 return size; 284 } 285 EXPORT_SYMBOL(nd_namespace_blk_validate); 286 287 288 static int nd_namespace_label_update(struct nd_region *nd_region, 289 struct device *dev) 290 { 291 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim, 292 "namespace must be idle during label update\n"); 293 if (dev->driver || to_ndns(dev)->claim) 294 return 0; 295 296 /* 297 * Only allow label writes that will result in a valid namespace 298 * or deletion of an existing namespace. 299 */ 300 if (is_namespace_pmem(dev)) { 301 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 302 resource_size_t size = resource_size(&nspm->nsio.res); 303 304 if (size == 0 && nspm->uuid) 305 /* delete allocation */; 306 else if (!nspm->uuid) 307 return 0; 308 309 return nd_pmem_namespace_label_update(nd_region, nspm, size); 310 } else if (is_namespace_blk(dev)) { 311 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 312 resource_size_t size = nd_namespace_blk_size(nsblk); 313 314 if (size == 0 && nsblk->uuid) 315 /* delete allocation */; 316 else if (!nsblk->uuid || !nsblk->lbasize) 317 return 0; 318 319 return nd_blk_namespace_label_update(nd_region, nsblk, size); 320 } else 321 return -ENXIO; 322 } 323 324 static ssize_t alt_name_store(struct device *dev, 325 struct device_attribute *attr, const char *buf, size_t len) 326 { 327 struct nd_region *nd_region = to_nd_region(dev->parent); 328 ssize_t rc; 329 330 device_lock(dev); 331 nvdimm_bus_lock(dev); 332 wait_nvdimm_bus_probe_idle(dev); 333 rc = __alt_name_store(dev, buf, len); 334 if (rc >= 0) 335 rc = nd_namespace_label_update(nd_region, dev); 336 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc); 337 nvdimm_bus_unlock(dev); 338 device_unlock(dev); 339 340 return rc < 0 ? rc : len; 341 } 342 343 static ssize_t alt_name_show(struct device *dev, 344 struct device_attribute *attr, char *buf) 345 { 346 char *ns_altname; 347 348 if (is_namespace_pmem(dev)) { 349 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 350 351 ns_altname = nspm->alt_name; 352 } else if (is_namespace_blk(dev)) { 353 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 354 355 ns_altname = nsblk->alt_name; 356 } else 357 return -ENXIO; 358 359 return sprintf(buf, "%s\n", ns_altname ? ns_altname : ""); 360 } 361 static DEVICE_ATTR_RW(alt_name); 362 363 static int scan_free(struct nd_region *nd_region, 364 struct nd_mapping *nd_mapping, struct nd_label_id *label_id, 365 resource_size_t n) 366 { 367 bool is_blk = strncmp(label_id->id, "blk", 3) == 0; 368 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 369 int rc = 0; 370 371 while (n) { 372 struct resource *res, *last; 373 resource_size_t new_start; 374 375 last = NULL; 376 for_each_dpa_resource(ndd, res) 377 if (strcmp(res->name, label_id->id) == 0) 378 last = res; 379 res = last; 380 if (!res) 381 return 0; 382 383 if (n >= resource_size(res)) { 384 n -= resource_size(res); 385 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc); 386 nvdimm_free_dpa(ndd, res); 387 /* retry with last resource deleted */ 388 continue; 389 } 390 391 /* 392 * Keep BLK allocations relegated to high DPA as much as 393 * possible 394 */ 395 if (is_blk) 396 new_start = res->start + n; 397 else 398 new_start = res->start; 399 400 rc = adjust_resource(res, new_start, resource_size(res) - n); 401 if (rc == 0) 402 res->flags |= DPA_RESOURCE_ADJUSTED; 403 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc); 404 break; 405 } 406 407 return rc; 408 } 409 410 /** 411 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id 412 * @nd_region: the set of dimms to reclaim @n bytes from 413 * @label_id: unique identifier for the namespace consuming this dpa range 414 * @n: number of bytes per-dimm to release 415 * 416 * Assumes resources are ordered. Starting from the end try to 417 * adjust_resource() the allocation to @n, but if @n is larger than the 418 * allocation delete it and find the 'new' last allocation in the label 419 * set. 420 */ 421 static int shrink_dpa_allocation(struct nd_region *nd_region, 422 struct nd_label_id *label_id, resource_size_t n) 423 { 424 int i; 425 426 for (i = 0; i < nd_region->ndr_mappings; i++) { 427 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 428 int rc; 429 430 rc = scan_free(nd_region, nd_mapping, label_id, n); 431 if (rc) 432 return rc; 433 } 434 435 return 0; 436 } 437 438 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id, 439 struct nd_region *nd_region, struct nd_mapping *nd_mapping, 440 resource_size_t n) 441 { 442 bool is_blk = strncmp(label_id->id, "blk", 3) == 0; 443 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 444 resource_size_t first_dpa; 445 struct resource *res; 446 int rc = 0; 447 448 /* allocate blk from highest dpa first */ 449 if (is_blk) 450 first_dpa = nd_mapping->start + nd_mapping->size - n; 451 else 452 first_dpa = nd_mapping->start; 453 454 /* first resource allocation for this label-id or dimm */ 455 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n); 456 if (!res) 457 rc = -EBUSY; 458 459 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc); 460 return rc ? n : 0; 461 } 462 463 static bool space_valid(bool is_pmem, bool is_reserve, 464 struct nd_label_id *label_id, struct resource *res) 465 { 466 /* 467 * For BLK-space any space is valid, for PMEM-space, it must be 468 * contiguous with an existing allocation unless we are 469 * reserving pmem. 470 */ 471 if (is_reserve || !is_pmem) 472 return true; 473 if (!res || strcmp(res->name, label_id->id) == 0) 474 return true; 475 return false; 476 } 477 478 enum alloc_loc { 479 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER, 480 }; 481 482 static resource_size_t scan_allocate(struct nd_region *nd_region, 483 struct nd_mapping *nd_mapping, struct nd_label_id *label_id, 484 resource_size_t n) 485 { 486 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1; 487 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0; 488 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; 489 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 490 const resource_size_t to_allocate = n; 491 struct resource *res; 492 int first; 493 494 retry: 495 first = 0; 496 for_each_dpa_resource(ndd, res) { 497 resource_size_t allocate, available = 0, free_start, free_end; 498 struct resource *next = res->sibling, *new_res = NULL; 499 enum alloc_loc loc = ALLOC_ERR; 500 const char *action; 501 int rc = 0; 502 503 /* ignore resources outside this nd_mapping */ 504 if (res->start > mapping_end) 505 continue; 506 if (res->end < nd_mapping->start) 507 continue; 508 509 /* space at the beginning of the mapping */ 510 if (!first++ && res->start > nd_mapping->start) { 511 free_start = nd_mapping->start; 512 available = res->start - free_start; 513 if (space_valid(is_pmem, is_reserve, label_id, NULL)) 514 loc = ALLOC_BEFORE; 515 } 516 517 /* space between allocations */ 518 if (!loc && next) { 519 free_start = res->start + resource_size(res); 520 free_end = min(mapping_end, next->start - 1); 521 if (space_valid(is_pmem, is_reserve, label_id, res) 522 && free_start < free_end) { 523 available = free_end + 1 - free_start; 524 loc = ALLOC_MID; 525 } 526 } 527 528 /* space at the end of the mapping */ 529 if (!loc && !next) { 530 free_start = res->start + resource_size(res); 531 free_end = mapping_end; 532 if (space_valid(is_pmem, is_reserve, label_id, res) 533 && free_start < free_end) { 534 available = free_end + 1 - free_start; 535 loc = ALLOC_AFTER; 536 } 537 } 538 539 if (!loc || !available) 540 continue; 541 allocate = min(available, n); 542 switch (loc) { 543 case ALLOC_BEFORE: 544 if (strcmp(res->name, label_id->id) == 0) { 545 /* adjust current resource up */ 546 if (is_pmem && !is_reserve) 547 return n; 548 rc = adjust_resource(res, res->start - allocate, 549 resource_size(res) + allocate); 550 action = "cur grow up"; 551 } else 552 action = "allocate"; 553 break; 554 case ALLOC_MID: 555 if (strcmp(next->name, label_id->id) == 0) { 556 /* adjust next resource up */ 557 if (is_pmem && !is_reserve) 558 return n; 559 rc = adjust_resource(next, next->start 560 - allocate, resource_size(next) 561 + allocate); 562 new_res = next; 563 action = "next grow up"; 564 } else if (strcmp(res->name, label_id->id) == 0) { 565 action = "grow down"; 566 } else 567 action = "allocate"; 568 break; 569 case ALLOC_AFTER: 570 if (strcmp(res->name, label_id->id) == 0) 571 action = "grow down"; 572 else 573 action = "allocate"; 574 break; 575 default: 576 return n; 577 } 578 579 if (strcmp(action, "allocate") == 0) { 580 /* BLK allocate bottom up */ 581 if (!is_pmem) 582 free_start += available - allocate; 583 else if (!is_reserve && free_start != nd_mapping->start) 584 return n; 585 586 new_res = nvdimm_allocate_dpa(ndd, label_id, 587 free_start, allocate); 588 if (!new_res) 589 rc = -EBUSY; 590 } else if (strcmp(action, "grow down") == 0) { 591 /* adjust current resource down */ 592 rc = adjust_resource(res, res->start, resource_size(res) 593 + allocate); 594 if (rc == 0) 595 res->flags |= DPA_RESOURCE_ADJUSTED; 596 } 597 598 if (!new_res) 599 new_res = res; 600 601 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n", 602 action, loc, rc); 603 604 if (rc) 605 return n; 606 607 n -= allocate; 608 if (n) { 609 /* 610 * Retry scan with newly inserted resources. 611 * For example, if we did an ALLOC_BEFORE 612 * insertion there may also have been space 613 * available for an ALLOC_AFTER insertion, so we 614 * need to check this same resource again 615 */ 616 goto retry; 617 } else 618 return 0; 619 } 620 621 /* 622 * If we allocated nothing in the BLK case it may be because we are in 623 * an initial "pmem-reserve pass". Only do an initial BLK allocation 624 * when none of the DPA space is reserved. 625 */ 626 if ((is_pmem || !ndd->dpa.child) && n == to_allocate) 627 return init_dpa_allocation(label_id, nd_region, nd_mapping, n); 628 return n; 629 } 630 631 static int merge_dpa(struct nd_region *nd_region, 632 struct nd_mapping *nd_mapping, struct nd_label_id *label_id) 633 { 634 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 635 struct resource *res; 636 637 if (strncmp("pmem", label_id->id, 4) == 0) 638 return 0; 639 retry: 640 for_each_dpa_resource(ndd, res) { 641 int rc; 642 struct resource *next = res->sibling; 643 resource_size_t end = res->start + resource_size(res); 644 645 if (!next || strcmp(res->name, label_id->id) != 0 646 || strcmp(next->name, label_id->id) != 0 647 || end != next->start) 648 continue; 649 end += resource_size(next); 650 nvdimm_free_dpa(ndd, next); 651 rc = adjust_resource(res, res->start, end - res->start); 652 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc); 653 if (rc) 654 return rc; 655 res->flags |= DPA_RESOURCE_ADJUSTED; 656 goto retry; 657 } 658 659 return 0; 660 } 661 662 static int __reserve_free_pmem(struct device *dev, void *data) 663 { 664 struct nvdimm *nvdimm = data; 665 struct nd_region *nd_region; 666 struct nd_label_id label_id; 667 int i; 668 669 if (!is_nd_pmem(dev)) 670 return 0; 671 672 nd_region = to_nd_region(dev); 673 if (nd_region->ndr_mappings == 0) 674 return 0; 675 676 memset(&label_id, 0, sizeof(label_id)); 677 strcat(label_id.id, "pmem-reserve"); 678 for (i = 0; i < nd_region->ndr_mappings; i++) { 679 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 680 resource_size_t n, rem = 0; 681 682 if (nd_mapping->nvdimm != nvdimm) 683 continue; 684 685 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem); 686 if (n == 0) 687 return 0; 688 rem = scan_allocate(nd_region, nd_mapping, &label_id, n); 689 dev_WARN_ONCE(&nd_region->dev, rem, 690 "pmem reserve underrun: %#llx of %#llx bytes\n", 691 (unsigned long long) n - rem, 692 (unsigned long long) n); 693 return rem ? -ENXIO : 0; 694 } 695 696 return 0; 697 } 698 699 static void release_free_pmem(struct nvdimm_bus *nvdimm_bus, 700 struct nd_mapping *nd_mapping) 701 { 702 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 703 struct resource *res, *_res; 704 705 for_each_dpa_resource_safe(ndd, res, _res) 706 if (strcmp(res->name, "pmem-reserve") == 0) 707 nvdimm_free_dpa(ndd, res); 708 } 709 710 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus, 711 struct nd_mapping *nd_mapping) 712 { 713 struct nvdimm *nvdimm = nd_mapping->nvdimm; 714 int rc; 715 716 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm, 717 __reserve_free_pmem); 718 if (rc) 719 release_free_pmem(nvdimm_bus, nd_mapping); 720 return rc; 721 } 722 723 /** 724 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id 725 * @nd_region: the set of dimms to allocate @n more bytes from 726 * @label_id: unique identifier for the namespace consuming this dpa range 727 * @n: number of bytes per-dimm to add to the existing allocation 728 * 729 * Assumes resources are ordered. For BLK regions, first consume 730 * BLK-only available DPA free space, then consume PMEM-aliased DPA 731 * space starting at the highest DPA. For PMEM regions start 732 * allocations from the start of an interleave set and end at the first 733 * BLK allocation or the end of the interleave set, whichever comes 734 * first. 735 */ 736 static int grow_dpa_allocation(struct nd_region *nd_region, 737 struct nd_label_id *label_id, resource_size_t n) 738 { 739 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); 740 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; 741 int i; 742 743 for (i = 0; i < nd_region->ndr_mappings; i++) { 744 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 745 resource_size_t rem = n; 746 int rc, j; 747 748 /* 749 * In the BLK case try once with all unallocated PMEM 750 * reserved, and once without 751 */ 752 for (j = is_pmem; j < 2; j++) { 753 bool blk_only = j == 0; 754 755 if (blk_only) { 756 rc = reserve_free_pmem(nvdimm_bus, nd_mapping); 757 if (rc) 758 return rc; 759 } 760 rem = scan_allocate(nd_region, nd_mapping, 761 label_id, rem); 762 if (blk_only) 763 release_free_pmem(nvdimm_bus, nd_mapping); 764 765 /* try again and allow encroachments into PMEM */ 766 if (rem == 0) 767 break; 768 } 769 770 dev_WARN_ONCE(&nd_region->dev, rem, 771 "allocation underrun: %#llx of %#llx bytes\n", 772 (unsigned long long) n - rem, 773 (unsigned long long) n); 774 if (rem) 775 return -ENXIO; 776 777 rc = merge_dpa(nd_region, nd_mapping, label_id); 778 if (rc) 779 return rc; 780 } 781 782 return 0; 783 } 784 785 static void nd_namespace_pmem_set_size(struct nd_region *nd_region, 786 struct nd_namespace_pmem *nspm, resource_size_t size) 787 { 788 struct resource *res = &nspm->nsio.res; 789 790 res->start = nd_region->ndr_start; 791 res->end = nd_region->ndr_start + size - 1; 792 } 793 794 static ssize_t __size_store(struct device *dev, unsigned long long val) 795 { 796 resource_size_t allocated = 0, available = 0; 797 struct nd_region *nd_region = to_nd_region(dev->parent); 798 struct nd_mapping *nd_mapping; 799 struct nvdimm_drvdata *ndd; 800 struct nd_label_id label_id; 801 u32 flags = 0, remainder; 802 u8 *uuid = NULL; 803 int rc, i; 804 805 if (dev->driver || to_ndns(dev)->claim) 806 return -EBUSY; 807 808 if (is_namespace_pmem(dev)) { 809 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 810 811 uuid = nspm->uuid; 812 } else if (is_namespace_blk(dev)) { 813 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 814 815 uuid = nsblk->uuid; 816 flags = NSLABEL_FLAG_LOCAL; 817 } 818 819 /* 820 * We need a uuid for the allocation-label and dimm(s) on which 821 * to store the label. 822 */ 823 if (!uuid || nd_region->ndr_mappings == 0) 824 return -ENXIO; 825 826 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder); 827 if (remainder) { 828 dev_dbg(dev, "%llu is not %dK aligned\n", val, 829 (SZ_4K * nd_region->ndr_mappings) / SZ_1K); 830 return -EINVAL; 831 } 832 833 nd_label_gen_id(&label_id, uuid, flags); 834 for (i = 0; i < nd_region->ndr_mappings; i++) { 835 nd_mapping = &nd_region->mapping[i]; 836 ndd = to_ndd(nd_mapping); 837 838 /* 839 * All dimms in an interleave set, or the base dimm for a blk 840 * region, need to be enabled for the size to be changed. 841 */ 842 if (!ndd) 843 return -ENXIO; 844 845 allocated += nvdimm_allocated_dpa(ndd, &label_id); 846 } 847 available = nd_region_available_dpa(nd_region); 848 849 if (val > available + allocated) 850 return -ENOSPC; 851 852 if (val == allocated) 853 return 0; 854 855 val = div_u64(val, nd_region->ndr_mappings); 856 allocated = div_u64(allocated, nd_region->ndr_mappings); 857 if (val < allocated) 858 rc = shrink_dpa_allocation(nd_region, &label_id, 859 allocated - val); 860 else 861 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated); 862 863 if (rc) 864 return rc; 865 866 if (is_namespace_pmem(dev)) { 867 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 868 869 nd_namespace_pmem_set_size(nd_region, nspm, 870 val * nd_region->ndr_mappings); 871 } else if (is_namespace_blk(dev)) { 872 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 873 874 /* 875 * Try to delete the namespace if we deleted all of its 876 * allocation, this is not the seed device for the 877 * region, and it is not actively claimed by a btt 878 * instance. 879 */ 880 if (val == 0 && nd_region->ns_seed != dev 881 && !nsblk->common.claim) 882 nd_device_unregister(dev, ND_ASYNC); 883 } 884 885 return rc; 886 } 887 888 static ssize_t size_store(struct device *dev, 889 struct device_attribute *attr, const char *buf, size_t len) 890 { 891 struct nd_region *nd_region = to_nd_region(dev->parent); 892 unsigned long long val; 893 u8 **uuid = NULL; 894 int rc; 895 896 rc = kstrtoull(buf, 0, &val); 897 if (rc) 898 return rc; 899 900 device_lock(dev); 901 nvdimm_bus_lock(dev); 902 wait_nvdimm_bus_probe_idle(dev); 903 rc = __size_store(dev, val); 904 if (rc >= 0) 905 rc = nd_namespace_label_update(nd_region, dev); 906 907 if (is_namespace_pmem(dev)) { 908 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 909 910 uuid = &nspm->uuid; 911 } else if (is_namespace_blk(dev)) { 912 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 913 914 uuid = &nsblk->uuid; 915 } 916 917 if (rc == 0 && val == 0 && uuid) { 918 /* setting size zero == 'delete namespace' */ 919 kfree(*uuid); 920 *uuid = NULL; 921 } 922 923 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0 924 ? "fail" : "success", rc); 925 926 nvdimm_bus_unlock(dev); 927 device_unlock(dev); 928 929 return rc < 0 ? rc : len; 930 } 931 932 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns) 933 { 934 struct device *dev = &ndns->dev; 935 936 if (is_namespace_pmem(dev)) { 937 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 938 939 return resource_size(&nspm->nsio.res); 940 } else if (is_namespace_blk(dev)) { 941 return nd_namespace_blk_size(to_nd_namespace_blk(dev)); 942 } else if (is_namespace_io(dev)) { 943 struct nd_namespace_io *nsio = to_nd_namespace_io(dev); 944 945 return resource_size(&nsio->res); 946 } else 947 WARN_ONCE(1, "unknown namespace type\n"); 948 return 0; 949 } 950 951 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) 952 { 953 resource_size_t size; 954 955 nvdimm_bus_lock(&ndns->dev); 956 size = __nvdimm_namespace_capacity(ndns); 957 nvdimm_bus_unlock(&ndns->dev); 958 959 return size; 960 } 961 EXPORT_SYMBOL(nvdimm_namespace_capacity); 962 963 static ssize_t size_show(struct device *dev, 964 struct device_attribute *attr, char *buf) 965 { 966 return sprintf(buf, "%llu\n", (unsigned long long) 967 nvdimm_namespace_capacity(to_ndns(dev))); 968 } 969 static DEVICE_ATTR(size, S_IRUGO, size_show, size_store); 970 971 static ssize_t uuid_show(struct device *dev, 972 struct device_attribute *attr, char *buf) 973 { 974 u8 *uuid; 975 976 if (is_namespace_pmem(dev)) { 977 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 978 979 uuid = nspm->uuid; 980 } else if (is_namespace_blk(dev)) { 981 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 982 983 uuid = nsblk->uuid; 984 } else 985 return -ENXIO; 986 987 if (uuid) 988 return sprintf(buf, "%pUb\n", uuid); 989 return sprintf(buf, "\n"); 990 } 991 992 /** 993 * namespace_update_uuid - check for a unique uuid and whether we're "renaming" 994 * @nd_region: parent region so we can updates all dimms in the set 995 * @dev: namespace type for generating label_id 996 * @new_uuid: incoming uuid 997 * @old_uuid: reference to the uuid storage location in the namespace object 998 */ 999 static int namespace_update_uuid(struct nd_region *nd_region, 1000 struct device *dev, u8 *new_uuid, u8 **old_uuid) 1001 { 1002 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0; 1003 struct nd_label_id old_label_id; 1004 struct nd_label_id new_label_id; 1005 int i; 1006 1007 if (!nd_is_uuid_unique(dev, new_uuid)) 1008 return -EINVAL; 1009 1010 if (*old_uuid == NULL) 1011 goto out; 1012 1013 /* 1014 * If we've already written a label with this uuid, then it's 1015 * too late to rename because we can't reliably update the uuid 1016 * without losing the old namespace. Userspace must delete this 1017 * namespace to abandon the old uuid. 1018 */ 1019 for (i = 0; i < nd_region->ndr_mappings; i++) { 1020 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1021 1022 /* 1023 * This check by itself is sufficient because old_uuid 1024 * would be NULL above if this uuid did not exist in the 1025 * currently written set. 1026 * 1027 * FIXME: can we delete uuid with zero dpa allocated? 1028 */ 1029 if (nd_mapping->labels) 1030 return -EBUSY; 1031 } 1032 1033 nd_label_gen_id(&old_label_id, *old_uuid, flags); 1034 nd_label_gen_id(&new_label_id, new_uuid, flags); 1035 for (i = 0; i < nd_region->ndr_mappings; i++) { 1036 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1037 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1038 struct resource *res; 1039 1040 for_each_dpa_resource(ndd, res) 1041 if (strcmp(res->name, old_label_id.id) == 0) 1042 sprintf((void *) res->name, "%s", 1043 new_label_id.id); 1044 } 1045 kfree(*old_uuid); 1046 out: 1047 *old_uuid = new_uuid; 1048 return 0; 1049 } 1050 1051 static ssize_t uuid_store(struct device *dev, 1052 struct device_attribute *attr, const char *buf, size_t len) 1053 { 1054 struct nd_region *nd_region = to_nd_region(dev->parent); 1055 u8 *uuid = NULL; 1056 ssize_t rc = 0; 1057 u8 **ns_uuid; 1058 1059 if (is_namespace_pmem(dev)) { 1060 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1061 1062 ns_uuid = &nspm->uuid; 1063 } else if (is_namespace_blk(dev)) { 1064 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1065 1066 ns_uuid = &nsblk->uuid; 1067 } else 1068 return -ENXIO; 1069 1070 device_lock(dev); 1071 nvdimm_bus_lock(dev); 1072 wait_nvdimm_bus_probe_idle(dev); 1073 if (to_ndns(dev)->claim) 1074 rc = -EBUSY; 1075 if (rc >= 0) 1076 rc = nd_uuid_store(dev, &uuid, buf, len); 1077 if (rc >= 0) 1078 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid); 1079 if (rc >= 0) 1080 rc = nd_namespace_label_update(nd_region, dev); 1081 else 1082 kfree(uuid); 1083 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, 1084 rc, buf, buf[len - 1] == '\n' ? "" : "\n"); 1085 nvdimm_bus_unlock(dev); 1086 device_unlock(dev); 1087 1088 return rc < 0 ? rc : len; 1089 } 1090 static DEVICE_ATTR_RW(uuid); 1091 1092 static ssize_t resource_show(struct device *dev, 1093 struct device_attribute *attr, char *buf) 1094 { 1095 struct resource *res; 1096 1097 if (is_namespace_pmem(dev)) { 1098 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1099 1100 res = &nspm->nsio.res; 1101 } else if (is_namespace_io(dev)) { 1102 struct nd_namespace_io *nsio = to_nd_namespace_io(dev); 1103 1104 res = &nsio->res; 1105 } else 1106 return -ENXIO; 1107 1108 /* no address to convey if the namespace has no allocation */ 1109 if (resource_size(res) == 0) 1110 return -ENXIO; 1111 return sprintf(buf, "%#llx\n", (unsigned long long) res->start); 1112 } 1113 static DEVICE_ATTR_RO(resource); 1114 1115 static const unsigned long ns_lbasize_supported[] = { 512, 520, 528, 1116 4096, 4104, 4160, 4224, 0 }; 1117 1118 static ssize_t sector_size_show(struct device *dev, 1119 struct device_attribute *attr, char *buf) 1120 { 1121 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1122 1123 if (!is_namespace_blk(dev)) 1124 return -ENXIO; 1125 1126 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf); 1127 } 1128 1129 static ssize_t sector_size_store(struct device *dev, 1130 struct device_attribute *attr, const char *buf, size_t len) 1131 { 1132 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1133 struct nd_region *nd_region = to_nd_region(dev->parent); 1134 ssize_t rc = 0; 1135 1136 if (!is_namespace_blk(dev)) 1137 return -ENXIO; 1138 1139 device_lock(dev); 1140 nvdimm_bus_lock(dev); 1141 if (to_ndns(dev)->claim) 1142 rc = -EBUSY; 1143 if (rc >= 0) 1144 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize, 1145 ns_lbasize_supported); 1146 if (rc >= 0) 1147 rc = nd_namespace_label_update(nd_region, dev); 1148 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__, 1149 rc, rc < 0 ? "tried" : "wrote", buf, 1150 buf[len - 1] == '\n' ? "" : "\n"); 1151 nvdimm_bus_unlock(dev); 1152 device_unlock(dev); 1153 1154 return rc ? rc : len; 1155 } 1156 static DEVICE_ATTR_RW(sector_size); 1157 1158 static ssize_t dpa_extents_show(struct device *dev, 1159 struct device_attribute *attr, char *buf) 1160 { 1161 struct nd_region *nd_region = to_nd_region(dev->parent); 1162 struct nd_label_id label_id; 1163 int count = 0, i; 1164 u8 *uuid = NULL; 1165 u32 flags = 0; 1166 1167 nvdimm_bus_lock(dev); 1168 if (is_namespace_pmem(dev)) { 1169 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); 1170 1171 uuid = nspm->uuid; 1172 flags = 0; 1173 } else if (is_namespace_blk(dev)) { 1174 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); 1175 1176 uuid = nsblk->uuid; 1177 flags = NSLABEL_FLAG_LOCAL; 1178 } 1179 1180 if (!uuid) 1181 goto out; 1182 1183 nd_label_gen_id(&label_id, uuid, flags); 1184 for (i = 0; i < nd_region->ndr_mappings; i++) { 1185 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1186 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1187 struct resource *res; 1188 1189 for_each_dpa_resource(ndd, res) 1190 if (strcmp(res->name, label_id.id) == 0) 1191 count++; 1192 } 1193 out: 1194 nvdimm_bus_unlock(dev); 1195 1196 return sprintf(buf, "%d\n", count); 1197 } 1198 static DEVICE_ATTR_RO(dpa_extents); 1199 1200 static ssize_t holder_show(struct device *dev, 1201 struct device_attribute *attr, char *buf) 1202 { 1203 struct nd_namespace_common *ndns = to_ndns(dev); 1204 ssize_t rc; 1205 1206 device_lock(dev); 1207 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); 1208 device_unlock(dev); 1209 1210 return rc; 1211 } 1212 static DEVICE_ATTR_RO(holder); 1213 1214 static ssize_t force_raw_store(struct device *dev, 1215 struct device_attribute *attr, const char *buf, size_t len) 1216 { 1217 bool force_raw; 1218 int rc = strtobool(buf, &force_raw); 1219 1220 if (rc) 1221 return rc; 1222 1223 to_ndns(dev)->force_raw = force_raw; 1224 return len; 1225 } 1226 1227 static ssize_t force_raw_show(struct device *dev, 1228 struct device_attribute *attr, char *buf) 1229 { 1230 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw); 1231 } 1232 static DEVICE_ATTR_RW(force_raw); 1233 1234 static struct attribute *nd_namespace_attributes[] = { 1235 &dev_attr_nstype.attr, 1236 &dev_attr_size.attr, 1237 &dev_attr_uuid.attr, 1238 &dev_attr_holder.attr, 1239 &dev_attr_resource.attr, 1240 &dev_attr_alt_name.attr, 1241 &dev_attr_force_raw.attr, 1242 &dev_attr_sector_size.attr, 1243 &dev_attr_dpa_extents.attr, 1244 NULL, 1245 }; 1246 1247 static umode_t namespace_visible(struct kobject *kobj, 1248 struct attribute *a, int n) 1249 { 1250 struct device *dev = container_of(kobj, struct device, kobj); 1251 1252 if (a == &dev_attr_resource.attr) { 1253 if (is_namespace_blk(dev)) 1254 return 0; 1255 return a->mode; 1256 } 1257 1258 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) { 1259 if (a == &dev_attr_size.attr) 1260 return S_IWUSR | S_IRUGO; 1261 1262 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr) 1263 return 0; 1264 1265 return a->mode; 1266 } 1267 1268 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr 1269 || a == &dev_attr_holder.attr 1270 || a == &dev_attr_force_raw.attr) 1271 return a->mode; 1272 1273 return 0; 1274 } 1275 1276 static struct attribute_group nd_namespace_attribute_group = { 1277 .attrs = nd_namespace_attributes, 1278 .is_visible = namespace_visible, 1279 }; 1280 1281 static const struct attribute_group *nd_namespace_attribute_groups[] = { 1282 &nd_device_attribute_group, 1283 &nd_namespace_attribute_group, 1284 &nd_numa_attribute_group, 1285 NULL, 1286 }; 1287 1288 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) 1289 { 1290 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; 1291 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL; 1292 struct nd_namespace_common *ndns; 1293 resource_size_t size; 1294 1295 if (nd_btt || nd_pfn) { 1296 struct device *host = NULL; 1297 1298 if (nd_btt) { 1299 host = &nd_btt->dev; 1300 ndns = nd_btt->ndns; 1301 } else if (nd_pfn) { 1302 host = &nd_pfn->dev; 1303 ndns = nd_pfn->ndns; 1304 } 1305 1306 if (!ndns || !host) 1307 return ERR_PTR(-ENODEV); 1308 1309 /* 1310 * Flush any in-progess probes / removals in the driver 1311 * for the raw personality of this namespace. 1312 */ 1313 device_lock(&ndns->dev); 1314 device_unlock(&ndns->dev); 1315 if (ndns->dev.driver) { 1316 dev_dbg(&ndns->dev, "is active, can't bind %s\n", 1317 dev_name(host)); 1318 return ERR_PTR(-EBUSY); 1319 } 1320 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != host, 1321 "host (%s) vs claim (%s) mismatch\n", 1322 dev_name(host), 1323 dev_name(ndns->claim))) 1324 return ERR_PTR(-ENXIO); 1325 } else { 1326 ndns = to_ndns(dev); 1327 if (ndns->claim) { 1328 dev_dbg(dev, "claimed by %s, failing probe\n", 1329 dev_name(ndns->claim)); 1330 1331 return ERR_PTR(-ENXIO); 1332 } 1333 } 1334 1335 size = nvdimm_namespace_capacity(ndns); 1336 if (size < ND_MIN_NAMESPACE_SIZE) { 1337 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", 1338 &size, ND_MIN_NAMESPACE_SIZE); 1339 return ERR_PTR(-ENODEV); 1340 } 1341 1342 if (is_namespace_pmem(&ndns->dev)) { 1343 struct nd_namespace_pmem *nspm; 1344 1345 nspm = to_nd_namespace_pmem(&ndns->dev); 1346 if (!nspm->uuid) { 1347 dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__); 1348 return ERR_PTR(-ENODEV); 1349 } 1350 } else if (is_namespace_blk(&ndns->dev)) { 1351 struct nd_namespace_blk *nsblk; 1352 1353 nsblk = to_nd_namespace_blk(&ndns->dev); 1354 if (!nd_namespace_blk_validate(nsblk)) 1355 return ERR_PTR(-ENODEV); 1356 } 1357 1358 return ndns; 1359 } 1360 EXPORT_SYMBOL(nvdimm_namespace_common_probe); 1361 1362 static struct device **create_namespace_io(struct nd_region *nd_region) 1363 { 1364 struct nd_namespace_io *nsio; 1365 struct device *dev, **devs; 1366 struct resource *res; 1367 1368 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL); 1369 if (!nsio) 1370 return NULL; 1371 1372 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); 1373 if (!devs) { 1374 kfree(nsio); 1375 return NULL; 1376 } 1377 1378 dev = &nsio->common.dev; 1379 dev->type = &namespace_io_device_type; 1380 dev->parent = &nd_region->dev; 1381 res = &nsio->res; 1382 res->name = dev_name(&nd_region->dev); 1383 res->flags = IORESOURCE_MEM; 1384 res->start = nd_region->ndr_start; 1385 res->end = res->start + nd_region->ndr_size - 1; 1386 1387 devs[0] = dev; 1388 return devs; 1389 } 1390 1391 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid, 1392 u64 cookie, u16 pos) 1393 { 1394 struct nd_namespace_label *found = NULL; 1395 int i; 1396 1397 for (i = 0; i < nd_region->ndr_mappings; i++) { 1398 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1399 struct nd_namespace_label *nd_label; 1400 bool found_uuid = false; 1401 int l; 1402 1403 for_each_label(l, nd_label, nd_mapping->labels) { 1404 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie); 1405 u16 position = __le16_to_cpu(nd_label->position); 1406 u16 nlabel = __le16_to_cpu(nd_label->nlabel); 1407 1408 if (isetcookie != cookie) 1409 continue; 1410 1411 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0) 1412 continue; 1413 1414 if (found_uuid) { 1415 dev_dbg(to_ndd(nd_mapping)->dev, 1416 "%s duplicate entry for uuid\n", 1417 __func__); 1418 return false; 1419 } 1420 found_uuid = true; 1421 if (nlabel != nd_region->ndr_mappings) 1422 continue; 1423 if (position != pos) 1424 continue; 1425 found = nd_label; 1426 break; 1427 } 1428 if (found) 1429 break; 1430 } 1431 return found != NULL; 1432 } 1433 1434 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id) 1435 { 1436 struct nd_namespace_label *select = NULL; 1437 int i; 1438 1439 if (!pmem_id) 1440 return -ENODEV; 1441 1442 for (i = 0; i < nd_region->ndr_mappings; i++) { 1443 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1444 struct nd_namespace_label *nd_label; 1445 u64 hw_start, hw_end, pmem_start, pmem_end; 1446 int l; 1447 1448 for_each_label(l, nd_label, nd_mapping->labels) 1449 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0) 1450 break; 1451 1452 if (!nd_label) { 1453 WARN_ON(1); 1454 return -EINVAL; 1455 } 1456 1457 select = nd_label; 1458 /* 1459 * Check that this label is compliant with the dpa 1460 * range published in NFIT 1461 */ 1462 hw_start = nd_mapping->start; 1463 hw_end = hw_start + nd_mapping->size; 1464 pmem_start = __le64_to_cpu(select->dpa); 1465 pmem_end = pmem_start + __le64_to_cpu(select->rawsize); 1466 if (pmem_start == hw_start && pmem_end <= hw_end) 1467 /* pass */; 1468 else 1469 return -EINVAL; 1470 1471 nd_mapping->labels[0] = select; 1472 nd_mapping->labels[1] = NULL; 1473 } 1474 return 0; 1475 } 1476 1477 /** 1478 * find_pmem_label_set - validate interleave set labelling, retrieve label0 1479 * @nd_region: region with mappings to validate 1480 */ 1481 static int find_pmem_label_set(struct nd_region *nd_region, 1482 struct nd_namespace_pmem *nspm) 1483 { 1484 u64 cookie = nd_region_interleave_set_cookie(nd_region); 1485 struct nd_namespace_label *nd_label; 1486 u8 select_id[NSLABEL_UUID_LEN]; 1487 resource_size_t size = 0; 1488 u8 *pmem_id = NULL; 1489 int rc = -ENODEV, l; 1490 u16 i; 1491 1492 if (cookie == 0) 1493 return -ENXIO; 1494 1495 /* 1496 * Find a complete set of labels by uuid. By definition we can start 1497 * with any mapping as the reference label 1498 */ 1499 for_each_label(l, nd_label, nd_region->mapping[0].labels) { 1500 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie); 1501 1502 if (isetcookie != cookie) 1503 continue; 1504 1505 for (i = 0; nd_region->ndr_mappings; i++) 1506 if (!has_uuid_at_pos(nd_region, nd_label->uuid, 1507 cookie, i)) 1508 break; 1509 if (i < nd_region->ndr_mappings) { 1510 /* 1511 * Give up if we don't find an instance of a 1512 * uuid at each position (from 0 to 1513 * nd_region->ndr_mappings - 1), or if we find a 1514 * dimm with two instances of the same uuid. 1515 */ 1516 rc = -EINVAL; 1517 goto err; 1518 } else if (pmem_id) { 1519 /* 1520 * If there is more than one valid uuid set, we 1521 * need userspace to clean this up. 1522 */ 1523 rc = -EBUSY; 1524 goto err; 1525 } 1526 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN); 1527 pmem_id = select_id; 1528 } 1529 1530 /* 1531 * Fix up each mapping's 'labels' to have the validated pmem label for 1532 * that position at labels[0], and NULL at labels[1]. In the process, 1533 * check that the namespace aligns with interleave-set. We know 1534 * that it does not overlap with any blk namespaces by virtue of 1535 * the dimm being enabled (i.e. nd_label_reserve_dpa() 1536 * succeeded). 1537 */ 1538 rc = select_pmem_id(nd_region, pmem_id); 1539 if (rc) 1540 goto err; 1541 1542 /* Calculate total size and populate namespace properties from label0 */ 1543 for (i = 0; i < nd_region->ndr_mappings; i++) { 1544 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1545 struct nd_namespace_label *label0 = nd_mapping->labels[0]; 1546 1547 size += __le64_to_cpu(label0->rawsize); 1548 if (__le16_to_cpu(label0->position) != 0) 1549 continue; 1550 WARN_ON(nspm->alt_name || nspm->uuid); 1551 nspm->alt_name = kmemdup((void __force *) label0->name, 1552 NSLABEL_NAME_LEN, GFP_KERNEL); 1553 nspm->uuid = kmemdup((void __force *) label0->uuid, 1554 NSLABEL_UUID_LEN, GFP_KERNEL); 1555 } 1556 1557 if (!nspm->alt_name || !nspm->uuid) { 1558 rc = -ENOMEM; 1559 goto err; 1560 } 1561 1562 nd_namespace_pmem_set_size(nd_region, nspm, size); 1563 1564 return 0; 1565 err: 1566 switch (rc) { 1567 case -EINVAL: 1568 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__); 1569 break; 1570 case -ENODEV: 1571 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__); 1572 break; 1573 default: 1574 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n", 1575 __func__, rc); 1576 break; 1577 } 1578 return rc; 1579 } 1580 1581 static struct device **create_namespace_pmem(struct nd_region *nd_region) 1582 { 1583 struct nd_namespace_pmem *nspm; 1584 struct device *dev, **devs; 1585 struct resource *res; 1586 int rc; 1587 1588 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); 1589 if (!nspm) 1590 return NULL; 1591 1592 dev = &nspm->nsio.common.dev; 1593 dev->type = &namespace_pmem_device_type; 1594 dev->parent = &nd_region->dev; 1595 res = &nspm->nsio.res; 1596 res->name = dev_name(&nd_region->dev); 1597 res->flags = IORESOURCE_MEM; 1598 rc = find_pmem_label_set(nd_region, nspm); 1599 if (rc == -ENODEV) { 1600 int i; 1601 1602 /* Pass, try to permit namespace creation... */ 1603 for (i = 0; i < nd_region->ndr_mappings; i++) { 1604 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1605 1606 kfree(nd_mapping->labels); 1607 nd_mapping->labels = NULL; 1608 } 1609 1610 /* Publish a zero-sized namespace for userspace to configure. */ 1611 nd_namespace_pmem_set_size(nd_region, nspm, 0); 1612 1613 rc = 0; 1614 } else if (rc) 1615 goto err; 1616 1617 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); 1618 if (!devs) 1619 goto err; 1620 1621 devs[0] = dev; 1622 return devs; 1623 1624 err: 1625 namespace_pmem_release(&nspm->nsio.common.dev); 1626 return NULL; 1627 } 1628 1629 struct resource *nsblk_add_resource(struct nd_region *nd_region, 1630 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk, 1631 resource_size_t start) 1632 { 1633 struct nd_label_id label_id; 1634 struct resource *res; 1635 1636 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); 1637 res = krealloc(nsblk->res, 1638 sizeof(void *) * (nsblk->num_resources + 1), 1639 GFP_KERNEL); 1640 if (!res) 1641 return NULL; 1642 nsblk->res = (struct resource **) res; 1643 for_each_dpa_resource(ndd, res) 1644 if (strcmp(res->name, label_id.id) == 0 1645 && res->start == start) { 1646 nsblk->res[nsblk->num_resources++] = res; 1647 return res; 1648 } 1649 return NULL; 1650 } 1651 1652 static struct device *nd_namespace_blk_create(struct nd_region *nd_region) 1653 { 1654 struct nd_namespace_blk *nsblk; 1655 struct device *dev; 1656 1657 if (!is_nd_blk(&nd_region->dev)) 1658 return NULL; 1659 1660 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); 1661 if (!nsblk) 1662 return NULL; 1663 1664 dev = &nsblk->common.dev; 1665 dev->type = &namespace_blk_device_type; 1666 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); 1667 if (nsblk->id < 0) { 1668 kfree(nsblk); 1669 return NULL; 1670 } 1671 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id); 1672 dev->parent = &nd_region->dev; 1673 dev->groups = nd_namespace_attribute_groups; 1674 1675 return &nsblk->common.dev; 1676 } 1677 1678 void nd_region_create_blk_seed(struct nd_region *nd_region) 1679 { 1680 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 1681 nd_region->ns_seed = nd_namespace_blk_create(nd_region); 1682 /* 1683 * Seed creation failures are not fatal, provisioning is simply 1684 * disabled until memory becomes available 1685 */ 1686 if (!nd_region->ns_seed) 1687 dev_err(&nd_region->dev, "failed to create blk namespace\n"); 1688 else 1689 nd_device_register(nd_region->ns_seed); 1690 } 1691 1692 void nd_region_create_btt_seed(struct nd_region *nd_region) 1693 { 1694 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); 1695 nd_region->btt_seed = nd_btt_create(nd_region); 1696 /* 1697 * Seed creation failures are not fatal, provisioning is simply 1698 * disabled until memory becomes available 1699 */ 1700 if (!nd_region->btt_seed) 1701 dev_err(&nd_region->dev, "failed to create btt namespace\n"); 1702 } 1703 1704 static struct device **create_namespace_blk(struct nd_region *nd_region) 1705 { 1706 struct nd_mapping *nd_mapping = &nd_region->mapping[0]; 1707 struct nd_namespace_label *nd_label; 1708 struct device *dev, **devs = NULL; 1709 struct nd_namespace_blk *nsblk; 1710 struct nvdimm_drvdata *ndd; 1711 int i, l, count = 0; 1712 struct resource *res; 1713 1714 if (nd_region->ndr_mappings == 0) 1715 return NULL; 1716 1717 ndd = to_ndd(nd_mapping); 1718 for_each_label(l, nd_label, nd_mapping->labels) { 1719 u32 flags = __le32_to_cpu(nd_label->flags); 1720 char *name[NSLABEL_NAME_LEN]; 1721 struct device **__devs; 1722 1723 if (flags & NSLABEL_FLAG_LOCAL) 1724 /* pass */; 1725 else 1726 continue; 1727 1728 for (i = 0; i < count; i++) { 1729 nsblk = to_nd_namespace_blk(devs[i]); 1730 if (memcmp(nsblk->uuid, nd_label->uuid, 1731 NSLABEL_UUID_LEN) == 0) { 1732 res = nsblk_add_resource(nd_region, ndd, nsblk, 1733 __le64_to_cpu(nd_label->dpa)); 1734 if (!res) 1735 goto err; 1736 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n", 1737 dev_name(&nsblk->common.dev)); 1738 break; 1739 } 1740 } 1741 if (i < count) 1742 continue; 1743 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL); 1744 if (!__devs) 1745 goto err; 1746 memcpy(__devs, devs, sizeof(dev) * count); 1747 kfree(devs); 1748 devs = __devs; 1749 1750 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); 1751 if (!nsblk) 1752 goto err; 1753 dev = &nsblk->common.dev; 1754 dev->type = &namespace_blk_device_type; 1755 dev->parent = &nd_region->dev; 1756 dev_set_name(dev, "namespace%d.%d", nd_region->id, count); 1757 devs[count++] = dev; 1758 nsblk->id = -1; 1759 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize); 1760 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN, 1761 GFP_KERNEL); 1762 if (!nsblk->uuid) 1763 goto err; 1764 memcpy(name, nd_label->name, NSLABEL_NAME_LEN); 1765 if (name[0]) 1766 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, 1767 GFP_KERNEL); 1768 res = nsblk_add_resource(nd_region, ndd, nsblk, 1769 __le64_to_cpu(nd_label->dpa)); 1770 if (!res) 1771 goto err; 1772 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n", 1773 dev_name(&nsblk->common.dev)); 1774 } 1775 1776 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n", 1777 __func__, count, count == 1 ? "" : "s"); 1778 1779 if (count == 0) { 1780 /* Publish a zero-sized namespace for userspace to configure. */ 1781 for (i = 0; i < nd_region->ndr_mappings; i++) { 1782 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1783 1784 kfree(nd_mapping->labels); 1785 nd_mapping->labels = NULL; 1786 } 1787 1788 devs = kcalloc(2, sizeof(dev), GFP_KERNEL); 1789 if (!devs) 1790 goto err; 1791 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); 1792 if (!nsblk) 1793 goto err; 1794 dev = &nsblk->common.dev; 1795 dev->type = &namespace_blk_device_type; 1796 dev->parent = &nd_region->dev; 1797 devs[count++] = dev; 1798 } 1799 1800 return devs; 1801 1802 err: 1803 for (i = 0; i < count; i++) { 1804 nsblk = to_nd_namespace_blk(devs[i]); 1805 namespace_blk_release(&nsblk->common.dev); 1806 } 1807 kfree(devs); 1808 return NULL; 1809 } 1810 1811 static int init_active_labels(struct nd_region *nd_region) 1812 { 1813 int i; 1814 1815 for (i = 0; i < nd_region->ndr_mappings; i++) { 1816 struct nd_mapping *nd_mapping = &nd_region->mapping[i]; 1817 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); 1818 struct nvdimm *nvdimm = nd_mapping->nvdimm; 1819 int count, j; 1820 1821 /* 1822 * If the dimm is disabled then prevent the region from 1823 * being activated if it aliases DPA. 1824 */ 1825 if (!ndd) { 1826 if ((nvdimm->flags & NDD_ALIASING) == 0) 1827 return 0; 1828 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n", 1829 dev_name(&nd_mapping->nvdimm->dev)); 1830 return -ENXIO; 1831 } 1832 nd_mapping->ndd = ndd; 1833 atomic_inc(&nvdimm->busy); 1834 get_ndd(ndd); 1835 1836 count = nd_label_active_count(ndd); 1837 dev_dbg(ndd->dev, "%s: %d\n", __func__, count); 1838 if (!count) 1839 continue; 1840 nd_mapping->labels = kcalloc(count + 1, sizeof(void *), 1841 GFP_KERNEL); 1842 if (!nd_mapping->labels) 1843 return -ENOMEM; 1844 for (j = 0; j < count; j++) { 1845 struct nd_namespace_label *label; 1846 1847 label = nd_label_active(ndd, j); 1848 nd_mapping->labels[j] = label; 1849 } 1850 } 1851 1852 return 0; 1853 } 1854 1855 int nd_region_register_namespaces(struct nd_region *nd_region, int *err) 1856 { 1857 struct device **devs = NULL; 1858 int i, rc = 0, type; 1859 1860 *err = 0; 1861 nvdimm_bus_lock(&nd_region->dev); 1862 rc = init_active_labels(nd_region); 1863 if (rc) { 1864 nvdimm_bus_unlock(&nd_region->dev); 1865 return rc; 1866 } 1867 1868 type = nd_region_to_nstype(nd_region); 1869 switch (type) { 1870 case ND_DEVICE_NAMESPACE_IO: 1871 devs = create_namespace_io(nd_region); 1872 break; 1873 case ND_DEVICE_NAMESPACE_PMEM: 1874 devs = create_namespace_pmem(nd_region); 1875 break; 1876 case ND_DEVICE_NAMESPACE_BLK: 1877 devs = create_namespace_blk(nd_region); 1878 break; 1879 default: 1880 break; 1881 } 1882 nvdimm_bus_unlock(&nd_region->dev); 1883 1884 if (!devs) 1885 return -ENODEV; 1886 1887 for (i = 0; devs[i]; i++) { 1888 struct device *dev = devs[i]; 1889 int id; 1890 1891 if (type == ND_DEVICE_NAMESPACE_BLK) { 1892 struct nd_namespace_blk *nsblk; 1893 1894 nsblk = to_nd_namespace_blk(dev); 1895 id = ida_simple_get(&nd_region->ns_ida, 0, 0, 1896 GFP_KERNEL); 1897 nsblk->id = id; 1898 } else 1899 id = i; 1900 1901 if (id < 0) 1902 break; 1903 dev_set_name(dev, "namespace%d.%d", nd_region->id, id); 1904 dev->groups = nd_namespace_attribute_groups; 1905 nd_device_register(dev); 1906 } 1907 if (i) 1908 nd_region->ns_seed = devs[0]; 1909 1910 if (devs[i]) { 1911 int j; 1912 1913 for (j = i; devs[j]; j++) { 1914 struct device *dev = devs[j]; 1915 1916 device_initialize(dev); 1917 put_device(dev); 1918 } 1919 *err = j - i; 1920 /* 1921 * All of the namespaces we tried to register failed, so 1922 * fail region activation. 1923 */ 1924 if (*err == 0) 1925 rc = -ENODEV; 1926 } 1927 kfree(devs); 1928 1929 if (rc == -ENODEV) 1930 return rc; 1931 1932 return i; 1933 } 1934