1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/module.h>
6 #include <linux/device.h>
7 #include <linux/sort.h>
8 #include <linux/slab.h>
9 #include <linux/list.h>
15 static void namespace_io_release(struct device *dev)
17 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
22 static void namespace_pmem_release(struct device *dev)
24 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
25 struct nd_region *nd_region = to_nd_region(dev->parent);
28 ida_simple_remove(&nd_region->ns_ida, nspm->id);
29 kfree(nspm->alt_name);
34 static void namespace_blk_release(struct device *dev)
36 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
37 struct nd_region *nd_region = to_nd_region(dev->parent);
40 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
41 kfree(nsblk->alt_name);
47 static const struct device_type namespace_io_device_type = {
48 .name = "nd_namespace_io",
49 .release = namespace_io_release,
52 static const struct device_type namespace_pmem_device_type = {
53 .name = "nd_namespace_pmem",
54 .release = namespace_pmem_release,
57 static const struct device_type namespace_blk_device_type = {
58 .name = "nd_namespace_blk",
59 .release = namespace_blk_release,
62 static bool is_namespace_pmem(const struct device *dev)
64 return dev ? dev->type == &namespace_pmem_device_type : false;
67 static bool is_namespace_blk(const struct device *dev)
69 return dev ? dev->type == &namespace_blk_device_type : false;
72 static bool is_namespace_io(const struct device *dev)
74 return dev ? dev->type == &namespace_io_device_type : false;
77 static int is_uuid_busy(struct device *dev, void *data)
79 u8 *uuid1 = data, *uuid2 = NULL;
81 if (is_namespace_pmem(dev)) {
82 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
85 } else if (is_namespace_blk(dev)) {
86 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
89 } else if (is_nd_btt(dev)) {
90 struct nd_btt *nd_btt = to_nd_btt(dev);
93 } else if (is_nd_pfn(dev)) {
94 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
99 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
105 static int is_namespace_uuid_busy(struct device *dev, void *data)
107 if (is_nd_region(dev))
108 return device_for_each_child(dev, data, is_uuid_busy);
113 * nd_is_uuid_unique - verify that no other namespace has @uuid
114 * @dev: any device on a nvdimm_bus
115 * @uuid: uuid to check
117 bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
119 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
123 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
124 if (device_for_each_child(&nvdimm_bus->dev, uuid,
125 is_namespace_uuid_busy) != 0)
130 bool pmem_should_map_pages(struct device *dev)
132 struct nd_region *nd_region = to_nd_region(dev->parent);
133 struct nd_namespace_common *ndns = to_ndns(dev);
134 struct nd_namespace_io *nsio;
136 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
139 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
142 if (is_nd_pfn(dev) || is_nd_btt(dev))
148 nsio = to_nd_namespace_io(dev);
149 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
150 IORESOURCE_SYSTEM_RAM,
151 IORES_DESC_NONE) == REGION_MIXED)
154 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
156 EXPORT_SYMBOL(pmem_should_map_pages);
158 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
160 if (is_namespace_pmem(&ndns->dev)) {
161 struct nd_namespace_pmem *nspm;
163 nspm = to_nd_namespace_pmem(&ndns->dev);
164 if (nspm->lbasize == 0 || nspm->lbasize == 512)
166 else if (nspm->lbasize == 4096)
169 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
174 * There is no namespace label (is_namespace_io()), or the label
175 * indicates the default sector size.
179 EXPORT_SYMBOL(pmem_sector_size);
181 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
184 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
185 const char *suffix = NULL;
187 if (ndns->claim && is_nd_btt(ndns->claim))
190 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
193 if (is_namespace_pmem(&ndns->dev)) {
194 struct nd_namespace_pmem *nspm;
196 nspm = to_nd_namespace_pmem(&ndns->dev);
201 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
202 suffix ? suffix : "");
204 sprintf(name, "pmem%d%s", nd_region->id,
205 suffix ? suffix : "");
206 } else if (is_namespace_blk(&ndns->dev)) {
207 struct nd_namespace_blk *nsblk;
209 nsblk = to_nd_namespace_blk(&ndns->dev);
210 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
211 suffix ? suffix : "");
218 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
220 const u8 *nd_dev_to_uuid(struct device *dev)
222 static const u8 null_uuid[16];
227 if (is_namespace_pmem(dev)) {
228 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
231 } else if (is_namespace_blk(dev)) {
232 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
238 EXPORT_SYMBOL(nd_dev_to_uuid);
240 static ssize_t nstype_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
243 struct nd_region *nd_region = to_nd_region(dev->parent);
245 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
247 static DEVICE_ATTR_RO(nstype);
249 static ssize_t __alt_name_store(struct device *dev, const char *buf,
252 char *input, *pos, *alt_name, **ns_altname;
255 if (is_namespace_pmem(dev)) {
256 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
258 ns_altname = &nspm->alt_name;
259 } else if (is_namespace_blk(dev)) {
260 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
262 ns_altname = &nsblk->alt_name;
266 if (dev->driver || to_ndns(dev)->claim)
269 input = kstrndup(buf, len, GFP_KERNEL);
274 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
279 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
285 *ns_altname = alt_name;
286 sprintf(*ns_altname, "%s", pos);
294 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
296 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
297 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
298 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
299 struct nd_label_id label_id;
300 resource_size_t size = 0;
301 struct resource *res;
305 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
306 for_each_dpa_resource(ndd, res)
307 if (strcmp(res->name, label_id.id) == 0)
308 size += resource_size(res);
312 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
314 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
315 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
316 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
317 struct nd_label_id label_id;
318 struct resource *res;
321 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
325 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
326 for_each_dpa_resource(ndd, res) {
327 if (strcmp(res->name, label_id.id) != 0)
330 * Resources with unacknowledged adjustments indicate a
331 * failure to update labels
333 if (res->flags & DPA_RESOURCE_ADJUSTED)
338 /* These values match after a successful label update */
339 if (count != nsblk->num_resources)
342 for (i = 0; i < nsblk->num_resources; i++) {
343 struct resource *found = NULL;
345 for_each_dpa_resource(ndd, res)
346 if (res == nsblk->res[i]) {
358 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
360 resource_size_t size;
362 nvdimm_bus_lock(&nsblk->common.dev);
363 size = __nd_namespace_blk_validate(nsblk);
364 nvdimm_bus_unlock(&nsblk->common.dev);
368 EXPORT_SYMBOL(nd_namespace_blk_validate);
371 static int nd_namespace_label_update(struct nd_region *nd_region,
374 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
375 "namespace must be idle during label update\n");
376 if (dev->driver || to_ndns(dev)->claim)
380 * Only allow label writes that will result in a valid namespace
381 * or deletion of an existing namespace.
383 if (is_namespace_pmem(dev)) {
384 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
385 resource_size_t size = resource_size(&nspm->nsio.res);
387 if (size == 0 && nspm->uuid)
388 /* delete allocation */;
389 else if (!nspm->uuid)
392 return nd_pmem_namespace_label_update(nd_region, nspm, size);
393 } else if (is_namespace_blk(dev)) {
394 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
395 resource_size_t size = nd_namespace_blk_size(nsblk);
397 if (size == 0 && nsblk->uuid)
398 /* delete allocation */;
399 else if (!nsblk->uuid || !nsblk->lbasize)
402 return nd_blk_namespace_label_update(nd_region, nsblk, size);
407 static ssize_t alt_name_store(struct device *dev,
408 struct device_attribute *attr, const char *buf, size_t len)
410 struct nd_region *nd_region = to_nd_region(dev->parent);
414 nvdimm_bus_lock(dev);
415 wait_nvdimm_bus_probe_idle(dev);
416 rc = __alt_name_store(dev, buf, len);
418 rc = nd_namespace_label_update(nd_region, dev);
419 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
420 nvdimm_bus_unlock(dev);
423 return rc < 0 ? rc : len;
426 static ssize_t alt_name_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
431 if (is_namespace_pmem(dev)) {
432 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
434 ns_altname = nspm->alt_name;
435 } else if (is_namespace_blk(dev)) {
436 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
438 ns_altname = nsblk->alt_name;
442 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
444 static DEVICE_ATTR_RW(alt_name);
446 static int scan_free(struct nd_region *nd_region,
447 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
450 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
451 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
455 struct resource *res, *last;
456 resource_size_t new_start;
459 for_each_dpa_resource(ndd, res)
460 if (strcmp(res->name, label_id->id) == 0)
466 if (n >= resource_size(res)) {
467 n -= resource_size(res);
468 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
469 nvdimm_free_dpa(ndd, res);
470 /* retry with last resource deleted */
475 * Keep BLK allocations relegated to high DPA as much as
479 new_start = res->start + n;
481 new_start = res->start;
483 rc = adjust_resource(res, new_start, resource_size(res) - n);
485 res->flags |= DPA_RESOURCE_ADJUSTED;
486 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
494 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
495 * @nd_region: the set of dimms to reclaim @n bytes from
496 * @label_id: unique identifier for the namespace consuming this dpa range
497 * @n: number of bytes per-dimm to release
499 * Assumes resources are ordered. Starting from the end try to
500 * adjust_resource() the allocation to @n, but if @n is larger than the
501 * allocation delete it and find the 'new' last allocation in the label
504 static int shrink_dpa_allocation(struct nd_region *nd_region,
505 struct nd_label_id *label_id, resource_size_t n)
509 for (i = 0; i < nd_region->ndr_mappings; i++) {
510 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
513 rc = scan_free(nd_region, nd_mapping, label_id, n);
521 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
522 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
525 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
526 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
527 resource_size_t first_dpa;
528 struct resource *res;
531 /* allocate blk from highest dpa first */
533 first_dpa = nd_mapping->start + nd_mapping->size - n;
535 first_dpa = nd_mapping->start;
537 /* first resource allocation for this label-id or dimm */
538 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
542 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
548 * space_valid() - validate free dpa space against constraints
549 * @nd_region: hosting region of the free space
550 * @ndd: dimm device data for debug
551 * @label_id: namespace id to allocate space
552 * @prev: potential allocation that precedes free space
553 * @next: allocation that follows the given free space range
554 * @exist: first allocation with same id in the mapping
555 * @n: range that must satisfied for pmem allocations
556 * @valid: free space range to validate
558 * BLK-space is valid as long as it does not precede a PMEM
559 * allocation in a given region. PMEM-space must be contiguous
560 * and adjacent to an existing existing allocation (if one
561 * exists). If reserving PMEM any space is valid.
563 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
564 struct nd_label_id *label_id, struct resource *prev,
565 struct resource *next, struct resource *exist,
566 resource_size_t n, struct resource *valid)
568 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
569 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
571 if (valid->start >= valid->end)
578 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
579 struct nvdimm_bus *nvdimm_bus;
580 struct blk_alloc_info info = {
581 .nd_mapping = nd_mapping,
582 .available = nd_mapping->size,
586 WARN_ON(!is_nd_blk(&nd_region->dev));
587 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
588 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
592 /* allocation needs to be contiguous, so this is all or nothing */
593 if (resource_size(valid) < n)
596 /* we've got all the space we need and no existing allocation */
600 /* allocation needs to be contiguous with the existing namespace */
601 if (valid->start == exist->end + 1
602 || valid->end == exist->start - 1)
606 /* truncate @valid size to 0 */
607 valid->end = valid->start - 1;
611 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
614 static resource_size_t scan_allocate(struct nd_region *nd_region,
615 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
618 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
619 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
620 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
621 struct resource *res, *exist = NULL, valid;
622 const resource_size_t to_allocate = n;
625 for_each_dpa_resource(ndd, res)
626 if (strcmp(label_id->id, res->name) == 0)
629 valid.start = nd_mapping->start;
630 valid.end = mapping_end;
631 valid.name = "free space";
634 for_each_dpa_resource(ndd, res) {
635 struct resource *next = res->sibling, *new_res = NULL;
636 resource_size_t allocate, available = 0;
637 enum alloc_loc loc = ALLOC_ERR;
641 /* ignore resources outside this nd_mapping */
642 if (res->start > mapping_end)
644 if (res->end < nd_mapping->start)
647 /* space at the beginning of the mapping */
648 if (!first++ && res->start > nd_mapping->start) {
649 valid.start = nd_mapping->start;
650 valid.end = res->start - 1;
651 space_valid(nd_region, ndd, label_id, NULL, next, exist,
652 to_allocate, &valid);
653 available = resource_size(&valid);
658 /* space between allocations */
660 valid.start = res->start + resource_size(res);
661 valid.end = min(mapping_end, next->start - 1);
662 space_valid(nd_region, ndd, label_id, res, next, exist,
663 to_allocate, &valid);
664 available = resource_size(&valid);
669 /* space at the end of the mapping */
671 valid.start = res->start + resource_size(res);
672 valid.end = mapping_end;
673 space_valid(nd_region, ndd, label_id, res, next, exist,
674 to_allocate, &valid);
675 available = resource_size(&valid);
680 if (!loc || !available)
682 allocate = min(available, n);
685 if (strcmp(res->name, label_id->id) == 0) {
686 /* adjust current resource up */
687 rc = adjust_resource(res, res->start - allocate,
688 resource_size(res) + allocate);
689 action = "cur grow up";
694 if (strcmp(next->name, label_id->id) == 0) {
695 /* adjust next resource up */
696 rc = adjust_resource(next, next->start
697 - allocate, resource_size(next)
700 action = "next grow up";
701 } else if (strcmp(res->name, label_id->id) == 0) {
702 action = "grow down";
707 if (strcmp(res->name, label_id->id) == 0)
708 action = "grow down";
716 if (strcmp(action, "allocate") == 0) {
717 /* BLK allocate bottom up */
719 valid.start += available - allocate;
721 new_res = nvdimm_allocate_dpa(ndd, label_id,
722 valid.start, allocate);
725 } else if (strcmp(action, "grow down") == 0) {
726 /* adjust current resource down */
727 rc = adjust_resource(res, res->start, resource_size(res)
730 res->flags |= DPA_RESOURCE_ADJUSTED;
736 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
745 * Retry scan with newly inserted resources.
746 * For example, if we did an ALLOC_BEFORE
747 * insertion there may also have been space
748 * available for an ALLOC_AFTER insertion, so we
749 * need to check this same resource again
757 * If we allocated nothing in the BLK case it may be because we are in
758 * an initial "pmem-reserve pass". Only do an initial BLK allocation
759 * when none of the DPA space is reserved.
761 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
762 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
766 static int merge_dpa(struct nd_region *nd_region,
767 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
769 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
770 struct resource *res;
772 if (strncmp("pmem", label_id->id, 4) == 0)
775 for_each_dpa_resource(ndd, res) {
777 struct resource *next = res->sibling;
778 resource_size_t end = res->start + resource_size(res);
780 if (!next || strcmp(res->name, label_id->id) != 0
781 || strcmp(next->name, label_id->id) != 0
782 || end != next->start)
784 end += resource_size(next);
785 nvdimm_free_dpa(ndd, next);
786 rc = adjust_resource(res, res->start, end - res->start);
787 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
790 res->flags |= DPA_RESOURCE_ADJUSTED;
797 int __reserve_free_pmem(struct device *dev, void *data)
799 struct nvdimm *nvdimm = data;
800 struct nd_region *nd_region;
801 struct nd_label_id label_id;
807 nd_region = to_nd_region(dev);
808 if (nd_region->ndr_mappings == 0)
811 memset(&label_id, 0, sizeof(label_id));
812 strcat(label_id.id, "pmem-reserve");
813 for (i = 0; i < nd_region->ndr_mappings; i++) {
814 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
815 resource_size_t n, rem = 0;
817 if (nd_mapping->nvdimm != nvdimm)
820 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
823 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
824 dev_WARN_ONCE(&nd_region->dev, rem,
825 "pmem reserve underrun: %#llx of %#llx bytes\n",
826 (unsigned long long) n - rem,
827 (unsigned long long) n);
828 return rem ? -ENXIO : 0;
834 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
835 struct nd_mapping *nd_mapping)
837 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
838 struct resource *res, *_res;
840 for_each_dpa_resource_safe(ndd, res, _res)
841 if (strcmp(res->name, "pmem-reserve") == 0)
842 nvdimm_free_dpa(ndd, res);
845 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
846 struct nd_mapping *nd_mapping)
848 struct nvdimm *nvdimm = nd_mapping->nvdimm;
851 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
852 __reserve_free_pmem);
854 release_free_pmem(nvdimm_bus, nd_mapping);
859 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
860 * @nd_region: the set of dimms to allocate @n more bytes from
861 * @label_id: unique identifier for the namespace consuming this dpa range
862 * @n: number of bytes per-dimm to add to the existing allocation
864 * Assumes resources are ordered. For BLK regions, first consume
865 * BLK-only available DPA free space, then consume PMEM-aliased DPA
866 * space starting at the highest DPA. For PMEM regions start
867 * allocations from the start of an interleave set and end at the first
868 * BLK allocation or the end of the interleave set, whichever comes
871 static int grow_dpa_allocation(struct nd_region *nd_region,
872 struct nd_label_id *label_id, resource_size_t n)
874 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
875 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
878 for (i = 0; i < nd_region->ndr_mappings; i++) {
879 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
880 resource_size_t rem = n;
884 * In the BLK case try once with all unallocated PMEM
885 * reserved, and once without
887 for (j = is_pmem; j < 2; j++) {
888 bool blk_only = j == 0;
891 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
895 rem = scan_allocate(nd_region, nd_mapping,
898 release_free_pmem(nvdimm_bus, nd_mapping);
900 /* try again and allow encroachments into PMEM */
905 dev_WARN_ONCE(&nd_region->dev, rem,
906 "allocation underrun: %#llx of %#llx bytes\n",
907 (unsigned long long) n - rem,
908 (unsigned long long) n);
912 rc = merge_dpa(nd_region, nd_mapping, label_id);
920 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
921 struct nd_namespace_pmem *nspm, resource_size_t size)
923 struct resource *res = &nspm->nsio.res;
924 resource_size_t offset = 0;
926 if (size && !nspm->uuid) {
931 if (size && nspm->uuid) {
932 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
933 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
934 struct nd_label_id label_id;
935 struct resource *res;
942 nd_label_gen_id(&label_id, nspm->uuid, 0);
944 /* calculate a spa offset from the dpa allocation offset */
945 for_each_dpa_resource(ndd, res)
946 if (strcmp(res->name, label_id.id) == 0) {
947 offset = (res->start - nd_mapping->start)
948 * nd_region->ndr_mappings;
957 res->start = nd_region->ndr_start + offset;
958 res->end = res->start + size - 1;
961 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
964 dev_dbg(dev, "%s: uuid not set\n", where);
970 static ssize_t __size_store(struct device *dev, unsigned long long val)
972 resource_size_t allocated = 0, available = 0;
973 struct nd_region *nd_region = to_nd_region(dev->parent);
974 struct nd_namespace_common *ndns = to_ndns(dev);
975 struct nd_mapping *nd_mapping;
976 struct nvdimm_drvdata *ndd;
977 struct nd_label_id label_id;
978 u32 flags = 0, remainder;
982 if (dev->driver || ndns->claim)
985 if (is_namespace_pmem(dev)) {
986 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
990 } else if (is_namespace_blk(dev)) {
991 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
994 flags = NSLABEL_FLAG_LOCAL;
999 * We need a uuid for the allocation-label and dimm(s) on which
1000 * to store the label.
1002 if (uuid_not_set(uuid, dev, __func__))
1004 if (nd_region->ndr_mappings == 0) {
1005 dev_dbg(dev, "not associated with dimm(s)\n");
1009 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
1011 dev_dbg(dev, "%llu is not %dK aligned\n", val,
1012 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
1016 nd_label_gen_id(&label_id, uuid, flags);
1017 for (i = 0; i < nd_region->ndr_mappings; i++) {
1018 nd_mapping = &nd_region->mapping[i];
1019 ndd = to_ndd(nd_mapping);
1022 * All dimms in an interleave set, or the base dimm for a blk
1023 * region, need to be enabled for the size to be changed.
1028 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1030 available = nd_region_allocatable_dpa(nd_region);
1032 if (val > available + allocated)
1035 if (val == allocated)
1038 val = div_u64(val, nd_region->ndr_mappings);
1039 allocated = div_u64(allocated, nd_region->ndr_mappings);
1040 if (val < allocated)
1041 rc = shrink_dpa_allocation(nd_region, &label_id,
1044 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1049 if (is_namespace_pmem(dev)) {
1050 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1052 nd_namespace_pmem_set_resource(nd_region, nspm,
1053 val * nd_region->ndr_mappings);
1057 * Try to delete the namespace if we deleted all of its
1058 * allocation, this is not the seed or 0th device for the
1059 * region, and it is not actively claimed by a btt, pfn, or dax
1062 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
1063 nd_device_unregister(dev, ND_ASYNC);
1068 static ssize_t size_store(struct device *dev,
1069 struct device_attribute *attr, const char *buf, size_t len)
1071 struct nd_region *nd_region = to_nd_region(dev->parent);
1072 unsigned long long val;
1076 rc = kstrtoull(buf, 0, &val);
1081 nvdimm_bus_lock(dev);
1082 wait_nvdimm_bus_probe_idle(dev);
1083 rc = __size_store(dev, val);
1085 rc = nd_namespace_label_update(nd_region, dev);
1087 if (is_namespace_pmem(dev)) {
1088 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1091 } else if (is_namespace_blk(dev)) {
1092 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1094 uuid = &nsblk->uuid;
1097 if (rc == 0 && val == 0 && uuid) {
1098 /* setting size zero == 'delete namespace' */
1103 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
1105 nvdimm_bus_unlock(dev);
1108 return rc < 0 ? rc : len;
1111 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1113 struct device *dev = &ndns->dev;
1115 if (is_namespace_pmem(dev)) {
1116 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1118 return resource_size(&nspm->nsio.res);
1119 } else if (is_namespace_blk(dev)) {
1120 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1121 } else if (is_namespace_io(dev)) {
1122 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1124 return resource_size(&nsio->res);
1126 WARN_ONCE(1, "unknown namespace type\n");
1130 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1132 resource_size_t size;
1134 nvdimm_bus_lock(&ndns->dev);
1135 size = __nvdimm_namespace_capacity(ndns);
1136 nvdimm_bus_unlock(&ndns->dev);
1140 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1142 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1145 bool locked = false;
1146 struct device *dev = &ndns->dev;
1147 struct nd_region *nd_region = to_nd_region(dev->parent);
1149 for (i = 0; i < nd_region->ndr_mappings; i++) {
1150 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1151 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1153 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1154 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1160 EXPORT_SYMBOL(nvdimm_namespace_locked);
1162 static ssize_t size_show(struct device *dev,
1163 struct device_attribute *attr, char *buf)
1165 return sprintf(buf, "%llu\n", (unsigned long long)
1166 nvdimm_namespace_capacity(to_ndns(dev)));
1168 static DEVICE_ATTR(size, 0444, size_show, size_store);
1170 static u8 *namespace_to_uuid(struct device *dev)
1172 if (is_namespace_pmem(dev)) {
1173 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1176 } else if (is_namespace_blk(dev)) {
1177 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1181 return ERR_PTR(-ENXIO);
1184 static ssize_t uuid_show(struct device *dev,
1185 struct device_attribute *attr, char *buf)
1187 u8 *uuid = namespace_to_uuid(dev);
1190 return PTR_ERR(uuid);
1192 return sprintf(buf, "%pUb\n", uuid);
1193 return sprintf(buf, "\n");
1197 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1198 * @nd_region: parent region so we can updates all dimms in the set
1199 * @dev: namespace type for generating label_id
1200 * @new_uuid: incoming uuid
1201 * @old_uuid: reference to the uuid storage location in the namespace object
1203 static int namespace_update_uuid(struct nd_region *nd_region,
1204 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1206 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1207 struct nd_label_id old_label_id;
1208 struct nd_label_id new_label_id;
1211 if (!nd_is_uuid_unique(dev, new_uuid))
1214 if (*old_uuid == NULL)
1218 * If we've already written a label with this uuid, then it's
1219 * too late to rename because we can't reliably update the uuid
1220 * without losing the old namespace. Userspace must delete this
1221 * namespace to abandon the old uuid.
1223 for (i = 0; i < nd_region->ndr_mappings; i++) {
1224 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1227 * This check by itself is sufficient because old_uuid
1228 * would be NULL above if this uuid did not exist in the
1229 * currently written set.
1231 * FIXME: can we delete uuid with zero dpa allocated?
1233 if (list_empty(&nd_mapping->labels))
1237 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1238 nd_label_gen_id(&new_label_id, new_uuid, flags);
1239 for (i = 0; i < nd_region->ndr_mappings; i++) {
1240 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1241 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1242 struct nd_label_ent *label_ent;
1243 struct resource *res;
1245 for_each_dpa_resource(ndd, res)
1246 if (strcmp(res->name, old_label_id.id) == 0)
1247 sprintf((void *) res->name, "%s",
1250 mutex_lock(&nd_mapping->lock);
1251 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1252 struct nd_namespace_label *nd_label = label_ent->label;
1253 struct nd_label_id label_id;
1257 nd_label_gen_id(&label_id, nd_label->uuid,
1258 __le32_to_cpu(nd_label->flags));
1259 if (strcmp(old_label_id.id, label_id.id) == 0)
1260 set_bit(ND_LABEL_REAP, &label_ent->flags);
1262 mutex_unlock(&nd_mapping->lock);
1266 *old_uuid = new_uuid;
1270 static ssize_t uuid_store(struct device *dev,
1271 struct device_attribute *attr, const char *buf, size_t len)
1273 struct nd_region *nd_region = to_nd_region(dev->parent);
1278 if (is_namespace_pmem(dev)) {
1279 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1281 ns_uuid = &nspm->uuid;
1282 } else if (is_namespace_blk(dev)) {
1283 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1285 ns_uuid = &nsblk->uuid;
1290 nvdimm_bus_lock(dev);
1291 wait_nvdimm_bus_probe_idle(dev);
1292 if (to_ndns(dev)->claim)
1295 rc = nd_uuid_store(dev, &uuid, buf, len);
1297 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1299 rc = nd_namespace_label_update(nd_region, dev);
1302 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1303 buf[len - 1] == '\n' ? "" : "\n");
1304 nvdimm_bus_unlock(dev);
1307 return rc < 0 ? rc : len;
1309 static DEVICE_ATTR_RW(uuid);
1311 static ssize_t resource_show(struct device *dev,
1312 struct device_attribute *attr, char *buf)
1314 struct resource *res;
1316 if (is_namespace_pmem(dev)) {
1317 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1319 res = &nspm->nsio.res;
1320 } else if (is_namespace_io(dev)) {
1321 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1327 /* no address to convey if the namespace has no allocation */
1328 if (resource_size(res) == 0)
1330 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1332 static DEVICE_ATTR_RO(resource);
1334 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
1335 4096, 4104, 4160, 4224, 0 };
1337 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1339 static ssize_t sector_size_show(struct device *dev,
1340 struct device_attribute *attr, char *buf)
1342 if (is_namespace_blk(dev)) {
1343 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1345 return nd_size_select_show(nsblk->lbasize,
1346 blk_lbasize_supported, buf);
1349 if (is_namespace_pmem(dev)) {
1350 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1352 return nd_size_select_show(nspm->lbasize,
1353 pmem_lbasize_supported, buf);
1358 static ssize_t sector_size_store(struct device *dev,
1359 struct device_attribute *attr, const char *buf, size_t len)
1361 struct nd_region *nd_region = to_nd_region(dev->parent);
1362 const unsigned long *supported;
1363 unsigned long *lbasize;
1366 if (is_namespace_blk(dev)) {
1367 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1369 lbasize = &nsblk->lbasize;
1370 supported = blk_lbasize_supported;
1371 } else if (is_namespace_pmem(dev)) {
1372 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1374 lbasize = &nspm->lbasize;
1375 supported = pmem_lbasize_supported;
1380 nvdimm_bus_lock(dev);
1381 if (to_ndns(dev)->claim)
1384 rc = nd_size_select_store(dev, buf, lbasize, supported);
1386 rc = nd_namespace_label_update(nd_region, dev);
1387 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1388 buf, buf[len - 1] == '\n' ? "" : "\n");
1389 nvdimm_bus_unlock(dev);
1392 return rc ? rc : len;
1394 static DEVICE_ATTR_RW(sector_size);
1396 static ssize_t dpa_extents_show(struct device *dev,
1397 struct device_attribute *attr, char *buf)
1399 struct nd_region *nd_region = to_nd_region(dev->parent);
1400 struct nd_label_id label_id;
1405 nvdimm_bus_lock(dev);
1406 if (is_namespace_pmem(dev)) {
1407 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1411 } else if (is_namespace_blk(dev)) {
1412 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1415 flags = NSLABEL_FLAG_LOCAL;
1421 nd_label_gen_id(&label_id, uuid, flags);
1422 for (i = 0; i < nd_region->ndr_mappings; i++) {
1423 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1424 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1425 struct resource *res;
1427 for_each_dpa_resource(ndd, res)
1428 if (strcmp(res->name, label_id.id) == 0)
1432 nvdimm_bus_unlock(dev);
1434 return sprintf(buf, "%d\n", count);
1436 static DEVICE_ATTR_RO(dpa_extents);
1438 static int btt_claim_class(struct device *dev)
1440 struct nd_region *nd_region = to_nd_region(dev->parent);
1441 int i, loop_bitmask = 0;
1443 for (i = 0; i < nd_region->ndr_mappings; i++) {
1444 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1445 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1446 struct nd_namespace_index *nsindex;
1449 * If any of the DIMMs do not support labels the only
1450 * possible BTT format is v1.
1457 nsindex = to_namespace_index(ndd, ndd->ns_current);
1458 if (nsindex == NULL)
1461 /* check whether existing labels are v1.1 or v1.2 */
1462 if (__le16_to_cpu(nsindex->major) == 1
1463 && __le16_to_cpu(nsindex->minor) == 1)
1470 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1471 * block is found, a v1.1 label for any mapping will set bit 1, and a
1472 * v1.2 label will set bit 2.
1474 * At the end of the loop, at most one of the three bits must be set.
1475 * If multiple bits were set, it means the different mappings disagree
1476 * about their labels, and this must be cleaned up first.
1478 * If all the label index blocks are found to agree, nsindex of NULL
1479 * implies labels haven't been initialized yet, and when they will,
1480 * they will be of the 1.2 format, so we can assume BTT2.0
1482 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1483 * found, we enforce BTT2.0
1485 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1487 switch (loop_bitmask) {
1490 return NVDIMM_CCLASS_BTT;
1493 return NVDIMM_CCLASS_BTT2;
1499 static ssize_t holder_show(struct device *dev,
1500 struct device_attribute *attr, char *buf)
1502 struct nd_namespace_common *ndns = to_ndns(dev);
1506 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1511 static DEVICE_ATTR_RO(holder);
1513 static ssize_t __holder_class_store(struct device *dev, const char *buf)
1515 struct nd_namespace_common *ndns = to_ndns(dev);
1517 if (dev->driver || ndns->claim)
1520 if (sysfs_streq(buf, "btt"))
1521 ndns->claim_class = btt_claim_class(dev);
1522 else if (sysfs_streq(buf, "pfn"))
1523 ndns->claim_class = NVDIMM_CCLASS_PFN;
1524 else if (sysfs_streq(buf, "dax"))
1525 ndns->claim_class = NVDIMM_CCLASS_DAX;
1526 else if (sysfs_streq(buf, ""))
1527 ndns->claim_class = NVDIMM_CCLASS_NONE;
1531 /* btt_claim_class() could've returned an error */
1532 if (ndns->claim_class < 0)
1533 return ndns->claim_class;
1538 static ssize_t holder_class_store(struct device *dev,
1539 struct device_attribute *attr, const char *buf, size_t len)
1541 struct nd_region *nd_region = to_nd_region(dev->parent);
1545 nvdimm_bus_lock(dev);
1546 wait_nvdimm_bus_probe_idle(dev);
1547 rc = __holder_class_store(dev, buf);
1549 rc = nd_namespace_label_update(nd_region, dev);
1550 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
1551 nvdimm_bus_unlock(dev);
1554 return rc < 0 ? rc : len;
1557 static ssize_t holder_class_show(struct device *dev,
1558 struct device_attribute *attr, char *buf)
1560 struct nd_namespace_common *ndns = to_ndns(dev);
1564 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1565 rc = sprintf(buf, "\n");
1566 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1567 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1568 rc = sprintf(buf, "btt\n");
1569 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1570 rc = sprintf(buf, "pfn\n");
1571 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1572 rc = sprintf(buf, "dax\n");
1574 rc = sprintf(buf, "<unknown>\n");
1579 static DEVICE_ATTR_RW(holder_class);
1581 static ssize_t mode_show(struct device *dev,
1582 struct device_attribute *attr, char *buf)
1584 struct nd_namespace_common *ndns = to_ndns(dev);
1585 struct device *claim;
1590 claim = ndns->claim;
1591 if (claim && is_nd_btt(claim))
1593 else if (claim && is_nd_pfn(claim))
1595 else if (claim && is_nd_dax(claim))
1597 else if (!claim && pmem_should_map_pages(dev))
1601 rc = sprintf(buf, "%s\n", mode);
1606 static DEVICE_ATTR_RO(mode);
1608 static ssize_t force_raw_store(struct device *dev,
1609 struct device_attribute *attr, const char *buf, size_t len)
1612 int rc = strtobool(buf, &force_raw);
1617 to_ndns(dev)->force_raw = force_raw;
1621 static ssize_t force_raw_show(struct device *dev,
1622 struct device_attribute *attr, char *buf)
1624 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1626 static DEVICE_ATTR_RW(force_raw);
1628 static struct attribute *nd_namespace_attributes[] = {
1629 &dev_attr_nstype.attr,
1630 &dev_attr_size.attr,
1631 &dev_attr_mode.attr,
1632 &dev_attr_uuid.attr,
1633 &dev_attr_holder.attr,
1634 &dev_attr_resource.attr,
1635 &dev_attr_alt_name.attr,
1636 &dev_attr_force_raw.attr,
1637 &dev_attr_sector_size.attr,
1638 &dev_attr_dpa_extents.attr,
1639 &dev_attr_holder_class.attr,
1643 static umode_t namespace_visible(struct kobject *kobj,
1644 struct attribute *a, int n)
1646 struct device *dev = container_of(kobj, struct device, kobj);
1648 if (a == &dev_attr_resource.attr) {
1649 if (is_namespace_blk(dev))
1654 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1655 if (a == &dev_attr_size.attr)
1661 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1662 || a == &dev_attr_holder.attr
1663 || a == &dev_attr_holder_class.attr
1664 || a == &dev_attr_force_raw.attr
1665 || a == &dev_attr_mode.attr)
1671 static struct attribute_group nd_namespace_attribute_group = {
1672 .attrs = nd_namespace_attributes,
1673 .is_visible = namespace_visible,
1676 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1677 &nd_device_attribute_group,
1678 &nd_namespace_attribute_group,
1679 &nd_numa_attribute_group,
1683 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1685 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1686 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1687 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1688 struct nd_namespace_common *ndns = NULL;
1689 resource_size_t size;
1691 if (nd_btt || nd_pfn || nd_dax) {
1693 ndns = nd_btt->ndns;
1695 ndns = nd_pfn->ndns;
1697 ndns = nd_dax->nd_pfn.ndns;
1700 return ERR_PTR(-ENODEV);
1703 * Flush any in-progess probes / removals in the driver
1704 * for the raw personality of this namespace.
1706 device_lock(&ndns->dev);
1707 device_unlock(&ndns->dev);
1708 if (ndns->dev.driver) {
1709 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1711 return ERR_PTR(-EBUSY);
1713 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1714 "host (%s) vs claim (%s) mismatch\n",
1716 dev_name(ndns->claim)))
1717 return ERR_PTR(-ENXIO);
1719 ndns = to_ndns(dev);
1721 dev_dbg(dev, "claimed by %s, failing probe\n",
1722 dev_name(ndns->claim));
1724 return ERR_PTR(-ENXIO);
1728 if (nvdimm_namespace_locked(ndns))
1729 return ERR_PTR(-EACCES);
1731 size = nvdimm_namespace_capacity(ndns);
1732 if (size < ND_MIN_NAMESPACE_SIZE) {
1733 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1734 &size, ND_MIN_NAMESPACE_SIZE);
1735 return ERR_PTR(-ENODEV);
1738 if (is_namespace_pmem(&ndns->dev)) {
1739 struct nd_namespace_pmem *nspm;
1741 nspm = to_nd_namespace_pmem(&ndns->dev);
1742 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1743 return ERR_PTR(-ENODEV);
1744 } else if (is_namespace_blk(&ndns->dev)) {
1745 struct nd_namespace_blk *nsblk;
1747 nsblk = to_nd_namespace_blk(&ndns->dev);
1748 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1749 return ERR_PTR(-ENODEV);
1750 if (!nsblk->lbasize) {
1751 dev_dbg(&ndns->dev, "sector size not set\n");
1752 return ERR_PTR(-ENODEV);
1754 if (!nd_namespace_blk_validate(nsblk))
1755 return ERR_PTR(-ENODEV);
1760 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1762 static struct device **create_namespace_io(struct nd_region *nd_region)
1764 struct nd_namespace_io *nsio;
1765 struct device *dev, **devs;
1766 struct resource *res;
1768 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1772 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1778 dev = &nsio->common.dev;
1779 dev->type = &namespace_io_device_type;
1780 dev->parent = &nd_region->dev;
1782 res->name = dev_name(&nd_region->dev);
1783 res->flags = IORESOURCE_MEM;
1784 res->start = nd_region->ndr_start;
1785 res->end = res->start + nd_region->ndr_size - 1;
1791 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1792 u64 cookie, u16 pos)
1794 struct nd_namespace_label *found = NULL;
1797 for (i = 0; i < nd_region->ndr_mappings; i++) {
1798 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1799 struct nd_interleave_set *nd_set = nd_region->nd_set;
1800 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1801 struct nd_label_ent *label_ent;
1802 bool found_uuid = false;
1804 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1805 struct nd_namespace_label *nd_label = label_ent->label;
1806 u16 position, nlabel;
1811 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1812 position = __le16_to_cpu(nd_label->position);
1813 nlabel = __le16_to_cpu(nd_label->nlabel);
1815 if (isetcookie != cookie)
1818 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1821 if (namespace_label_has(ndd, type_guid)
1822 && !guid_equal(&nd_set->type_guid,
1823 &nd_label->type_guid)) {
1824 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1826 &nd_label->type_guid);
1831 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1835 if (nlabel != nd_region->ndr_mappings)
1837 if (position != pos)
1845 return found != NULL;
1848 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1855 for (i = 0; i < nd_region->ndr_mappings; i++) {
1856 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1857 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1858 struct nd_namespace_label *nd_label = NULL;
1859 u64 hw_start, hw_end, pmem_start, pmem_end;
1860 struct nd_label_ent *label_ent;
1862 lockdep_assert_held(&nd_mapping->lock);
1863 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1864 nd_label = label_ent->label;
1867 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1878 * Check that this label is compliant with the dpa
1879 * range published in NFIT
1881 hw_start = nd_mapping->start;
1882 hw_end = hw_start + nd_mapping->size;
1883 pmem_start = __le64_to_cpu(nd_label->dpa);
1884 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1885 if (pmem_start >= hw_start && pmem_start < hw_end
1886 && pmem_end <= hw_end && pmem_end > hw_start)
1889 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1890 dev_name(ndd->dev), nd_label->uuid);
1894 /* move recently validated label to the front of the list */
1895 list_move(&label_ent->list, &nd_mapping->labels);
1901 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1902 * @nd_region: region with mappings to validate
1903 * @nspm: target namespace to create
1904 * @nd_label: target pmem namespace label to evaluate
1906 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1907 struct nd_namespace_index *nsindex,
1908 struct nd_namespace_label *nd_label)
1910 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1911 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1912 struct nd_label_ent *label_ent;
1913 struct nd_namespace_pmem *nspm;
1914 struct nd_mapping *nd_mapping;
1915 resource_size_t size = 0;
1916 struct resource *res;
1922 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1923 return ERR_PTR(-ENXIO);
1926 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1927 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1929 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1930 return ERR_PTR(-EAGAIN);
1932 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1936 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1938 return ERR_PTR(-ENOMEM);
1941 dev = &nspm->nsio.common.dev;
1942 dev->type = &namespace_pmem_device_type;
1943 dev->parent = &nd_region->dev;
1944 res = &nspm->nsio.res;
1945 res->name = dev_name(&nd_region->dev);
1946 res->flags = IORESOURCE_MEM;
1948 for (i = 0; i < nd_region->ndr_mappings; i++) {
1949 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1951 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1956 if (i < nd_region->ndr_mappings) {
1957 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1960 * Give up if we don't find an instance of a uuid at each
1961 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1962 * find a dimm with two instances of the same uuid.
1964 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1965 nvdimm_name(nvdimm), nd_label->uuid);
1971 * Fix up each mapping's 'labels' to have the validated pmem label for
1972 * that position at labels[0], and NULL at labels[1]. In the process,
1973 * check that the namespace aligns with interleave-set. We know
1974 * that it does not overlap with any blk namespaces by virtue of
1975 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1978 rc = select_pmem_id(nd_region, nd_label->uuid);
1982 /* Calculate total size and populate namespace properties from label0 */
1983 for (i = 0; i < nd_region->ndr_mappings; i++) {
1984 struct nd_namespace_label *label0;
1985 struct nvdimm_drvdata *ndd;
1987 nd_mapping = &nd_region->mapping[i];
1988 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1989 typeof(*label_ent), list);
1990 label0 = label_ent ? label_ent->label : 0;
1997 size += __le64_to_cpu(label0->rawsize);
1998 if (__le16_to_cpu(label0->position) != 0)
2000 WARN_ON(nspm->alt_name || nspm->uuid);
2001 nspm->alt_name = kmemdup((void __force *) label0->name,
2002 NSLABEL_NAME_LEN, GFP_KERNEL);
2003 nspm->uuid = kmemdup((void __force *) label0->uuid,
2004 NSLABEL_UUID_LEN, GFP_KERNEL);
2005 nspm->lbasize = __le64_to_cpu(label0->lbasize);
2006 ndd = to_ndd(nd_mapping);
2007 if (namespace_label_has(ndd, abstraction_guid))
2008 nspm->nsio.common.claim_class
2009 = to_nvdimm_cclass(&label0->abstraction_guid);
2013 if (!nspm->alt_name || !nspm->uuid) {
2018 nd_namespace_pmem_set_resource(nd_region, nspm, size);
2022 namespace_pmem_release(dev);
2025 dev_dbg(&nd_region->dev, "invalid label(s)\n");
2028 dev_dbg(&nd_region->dev, "label not found\n");
2031 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
2037 struct resource *nsblk_add_resource(struct nd_region *nd_region,
2038 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2039 resource_size_t start)
2041 struct nd_label_id label_id;
2042 struct resource *res;
2044 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2045 res = krealloc(nsblk->res,
2046 sizeof(void *) * (nsblk->num_resources + 1),
2050 nsblk->res = (struct resource **) res;
2051 for_each_dpa_resource(ndd, res)
2052 if (strcmp(res->name, label_id.id) == 0
2053 && res->start == start) {
2054 nsblk->res[nsblk->num_resources++] = res;
2060 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2062 struct nd_namespace_blk *nsblk;
2065 if (!is_nd_blk(&nd_region->dev))
2068 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2072 dev = &nsblk->common.dev;
2073 dev->type = &namespace_blk_device_type;
2074 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2075 if (nsblk->id < 0) {
2079 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2080 dev->parent = &nd_region->dev;
2081 dev->groups = nd_namespace_attribute_groups;
2083 return &nsblk->common.dev;
2086 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2088 struct nd_namespace_pmem *nspm;
2089 struct resource *res;
2092 if (!is_memory(&nd_region->dev))
2095 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2099 dev = &nspm->nsio.common.dev;
2100 dev->type = &namespace_pmem_device_type;
2101 dev->parent = &nd_region->dev;
2102 res = &nspm->nsio.res;
2103 res->name = dev_name(&nd_region->dev);
2104 res->flags = IORESOURCE_MEM;
2106 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2111 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
2112 dev->groups = nd_namespace_attribute_groups;
2113 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2118 void nd_region_create_ns_seed(struct nd_region *nd_region)
2120 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2122 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2125 if (is_nd_blk(&nd_region->dev))
2126 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2128 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2131 * Seed creation failures are not fatal, provisioning is simply
2132 * disabled until memory becomes available
2134 if (!nd_region->ns_seed)
2135 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2136 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
2138 nd_device_register(nd_region->ns_seed);
2141 void nd_region_create_dax_seed(struct nd_region *nd_region)
2143 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2144 nd_region->dax_seed = nd_dax_create(nd_region);
2146 * Seed creation failures are not fatal, provisioning is simply
2147 * disabled until memory becomes available
2149 if (!nd_region->dax_seed)
2150 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2153 void nd_region_create_pfn_seed(struct nd_region *nd_region)
2155 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2156 nd_region->pfn_seed = nd_pfn_create(nd_region);
2158 * Seed creation failures are not fatal, provisioning is simply
2159 * disabled until memory becomes available
2161 if (!nd_region->pfn_seed)
2162 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2165 void nd_region_create_btt_seed(struct nd_region *nd_region)
2167 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2168 nd_region->btt_seed = nd_btt_create(nd_region);
2170 * Seed creation failures are not fatal, provisioning is simply
2171 * disabled until memory becomes available
2173 if (!nd_region->btt_seed)
2174 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2177 static int add_namespace_resource(struct nd_region *nd_region,
2178 struct nd_namespace_label *nd_label, struct device **devs,
2181 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2182 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2185 for (i = 0; i < count; i++) {
2186 u8 *uuid = namespace_to_uuid(devs[i]);
2187 struct resource *res;
2189 if (IS_ERR_OR_NULL(uuid)) {
2194 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2196 if (is_namespace_blk(devs[i])) {
2197 res = nsblk_add_resource(nd_region, ndd,
2198 to_nd_namespace_blk(devs[i]),
2199 __le64_to_cpu(nd_label->dpa));
2202 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2204 dev_err(&nd_region->dev,
2205 "error: conflicting extents for uuid: %pUb\n",
2215 static struct device *create_namespace_blk(struct nd_region *nd_region,
2216 struct nd_namespace_label *nd_label, int count)
2219 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2220 struct nd_interleave_set *nd_set = nd_region->nd_set;
2221 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2222 struct nd_namespace_blk *nsblk;
2223 char name[NSLABEL_NAME_LEN];
2224 struct device *dev = NULL;
2225 struct resource *res;
2227 if (namespace_label_has(ndd, type_guid)) {
2228 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
2229 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
2231 &nd_label->type_guid);
2232 return ERR_PTR(-EAGAIN);
2235 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
2236 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
2238 __le64_to_cpu(nd_label->isetcookie));
2239 return ERR_PTR(-EAGAIN);
2243 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2245 return ERR_PTR(-ENOMEM);
2246 dev = &nsblk->common.dev;
2247 dev->type = &namespace_blk_device_type;
2248 dev->parent = &nd_region->dev;
2250 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2251 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2253 if (namespace_label_has(ndd, abstraction_guid))
2254 nsblk->common.claim_class
2255 = to_nvdimm_cclass(&nd_label->abstraction_guid);
2258 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2260 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2262 if (!nsblk->alt_name)
2265 res = nsblk_add_resource(nd_region, ndd, nsblk,
2266 __le64_to_cpu(nd_label->dpa));
2269 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2272 namespace_blk_release(dev);
2273 return ERR_PTR(-ENXIO);
2276 static int cmp_dpa(const void *a, const void *b)
2278 const struct device *dev_a = *(const struct device **) a;
2279 const struct device *dev_b = *(const struct device **) b;
2280 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2281 struct nd_namespace_pmem *nspm_a, *nspm_b;
2283 if (is_namespace_io(dev_a))
2286 if (is_namespace_blk(dev_a)) {
2287 nsblk_a = to_nd_namespace_blk(dev_a);
2288 nsblk_b = to_nd_namespace_blk(dev_b);
2290 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2291 sizeof(resource_size_t));
2294 nspm_a = to_nd_namespace_pmem(dev_a);
2295 nspm_b = to_nd_namespace_pmem(dev_b);
2297 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2298 sizeof(resource_size_t));
2301 static struct device **scan_labels(struct nd_region *nd_region)
2304 struct device *dev, **devs = NULL;
2305 struct nd_label_ent *label_ent, *e;
2306 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2307 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
2309 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2310 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
2311 struct nd_namespace_label *nd_label = label_ent->label;
2312 struct device **__devs;
2317 flags = __le32_to_cpu(nd_label->flags);
2318 if (is_nd_blk(&nd_region->dev)
2319 == !!(flags & NSLABEL_FLAG_LOCAL))
2320 /* pass, region matches label type */;
2324 /* skip labels that describe extents outside of the region */
2325 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
2328 i = add_namespace_resource(nd_region, nd_label, devs, count);
2333 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2336 memcpy(__devs, devs, sizeof(dev) * count);
2340 if (is_nd_blk(&nd_region->dev))
2341 dev = create_namespace_blk(nd_region, nd_label, count);
2343 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2344 struct nd_namespace_index *nsindex;
2346 nsindex = to_namespace_index(ndd, ndd->ns_current);
2347 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
2351 switch (PTR_ERR(dev)) {
2353 /* skip invalid labels */
2356 /* fallthrough to seed creation */
2362 devs[count++] = dev;
2366 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2367 count, is_nd_blk(&nd_region->dev)
2368 ? "blk" : "pmem", count == 1 ? "" : "s");
2371 /* Publish a zero-sized namespace for userspace to configure. */
2372 nd_mapping_free_labels(nd_mapping);
2374 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2377 if (is_nd_blk(&nd_region->dev)) {
2378 struct nd_namespace_blk *nsblk;
2380 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2383 dev = &nsblk->common.dev;
2384 dev->type = &namespace_blk_device_type;
2386 struct nd_namespace_pmem *nspm;
2388 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2391 dev = &nspm->nsio.common.dev;
2392 dev->type = &namespace_pmem_device_type;
2393 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2395 dev->parent = &nd_region->dev;
2396 devs[count++] = dev;
2397 } else if (is_memory(&nd_region->dev)) {
2398 /* clean unselected labels */
2399 for (i = 0; i < nd_region->ndr_mappings; i++) {
2400 struct list_head *l, *e;
2404 nd_mapping = &nd_region->mapping[i];
2405 if (list_empty(&nd_mapping->labels)) {
2411 list_for_each_safe(l, e, &nd_mapping->labels) {
2414 list_move_tail(l, &list);
2416 nd_mapping_free_labels(nd_mapping);
2417 list_splice_init(&list, &nd_mapping->labels);
2422 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2428 for (i = 0; devs[i]; i++)
2429 if (is_nd_blk(&nd_region->dev))
2430 namespace_blk_release(devs[i]);
2432 namespace_pmem_release(devs[i]);
2438 static struct device **create_namespaces(struct nd_region *nd_region)
2440 struct nd_mapping *nd_mapping;
2441 struct device **devs;
2444 if (nd_region->ndr_mappings == 0)
2447 /* lock down all mappings while we scan labels */
2448 for (i = 0; i < nd_region->ndr_mappings; i++) {
2449 nd_mapping = &nd_region->mapping[i];
2450 mutex_lock_nested(&nd_mapping->lock, i);
2453 devs = scan_labels(nd_region);
2455 for (i = 0; i < nd_region->ndr_mappings; i++) {
2456 int reverse = nd_region->ndr_mappings - 1 - i;
2458 nd_mapping = &nd_region->mapping[reverse];
2459 mutex_unlock(&nd_mapping->lock);
2465 static int init_active_labels(struct nd_region *nd_region)
2469 for (i = 0; i < nd_region->ndr_mappings; i++) {
2470 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2471 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2472 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2473 struct nd_label_ent *label_ent;
2477 * If the dimm is disabled then we may need to prevent
2478 * the region from being activated.
2481 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2482 /* fail, label data may be unreadable */;
2483 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2484 /* fail, labels needed to disambiguate dpa */;
2488 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2489 dev_name(&nd_mapping->nvdimm->dev),
2490 test_bit(NDD_LOCKED, &nvdimm->flags)
2491 ? "locked" : "disabled");
2494 nd_mapping->ndd = ndd;
2495 atomic_inc(&nvdimm->busy);
2498 count = nd_label_active_count(ndd);
2499 dev_dbg(ndd->dev, "count: %d\n", count);
2502 for (j = 0; j < count; j++) {
2503 struct nd_namespace_label *label;
2505 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2508 label = nd_label_active(ndd, j);
2509 if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2510 u32 flags = __le32_to_cpu(label->flags);
2512 flags &= ~NSLABEL_FLAG_LOCAL;
2513 label->flags = __cpu_to_le32(flags);
2515 label_ent->label = label;
2517 mutex_lock(&nd_mapping->lock);
2518 list_add_tail(&label_ent->list, &nd_mapping->labels);
2519 mutex_unlock(&nd_mapping->lock);
2525 mutex_lock(&nd_mapping->lock);
2526 nd_mapping_free_labels(nd_mapping);
2527 mutex_unlock(&nd_mapping->lock);
2534 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2536 struct device **devs = NULL;
2537 int i, rc = 0, type;
2540 nvdimm_bus_lock(&nd_region->dev);
2541 rc = init_active_labels(nd_region);
2543 nvdimm_bus_unlock(&nd_region->dev);
2547 type = nd_region_to_nstype(nd_region);
2549 case ND_DEVICE_NAMESPACE_IO:
2550 devs = create_namespace_io(nd_region);
2552 case ND_DEVICE_NAMESPACE_PMEM:
2553 case ND_DEVICE_NAMESPACE_BLK:
2554 devs = create_namespaces(nd_region);
2559 nvdimm_bus_unlock(&nd_region->dev);
2564 for (i = 0; devs[i]; i++) {
2565 struct device *dev = devs[i];
2568 if (type == ND_DEVICE_NAMESPACE_BLK) {
2569 struct nd_namespace_blk *nsblk;
2571 nsblk = to_nd_namespace_blk(dev);
2572 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2575 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2576 struct nd_namespace_pmem *nspm;
2578 nspm = to_nd_namespace_pmem(dev);
2579 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2587 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2588 dev->groups = nd_namespace_attribute_groups;
2589 nd_device_register(dev);
2592 nd_region->ns_seed = devs[0];
2597 for (j = i; devs[j]; j++) {
2598 struct device *dev = devs[j];
2600 device_initialize(dev);
2605 * All of the namespaces we tried to register failed, so
2606 * fail region activation.