2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/sort.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
23 static void namespace_io_release(struct device *dev)
25 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
30 static void namespace_pmem_release(struct device *dev)
32 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
33 struct nd_region *nd_region = to_nd_region(dev->parent);
36 ida_simple_remove(&nd_region->ns_ida, nspm->id);
37 kfree(nspm->alt_name);
42 static void namespace_blk_release(struct device *dev)
44 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
45 struct nd_region *nd_region = to_nd_region(dev->parent);
48 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
49 kfree(nsblk->alt_name);
55 static const struct device_type namespace_io_device_type = {
56 .name = "nd_namespace_io",
57 .release = namespace_io_release,
60 static const struct device_type namespace_pmem_device_type = {
61 .name = "nd_namespace_pmem",
62 .release = namespace_pmem_release,
65 static const struct device_type namespace_blk_device_type = {
66 .name = "nd_namespace_blk",
67 .release = namespace_blk_release,
70 static bool is_namespace_pmem(const struct device *dev)
72 return dev ? dev->type == &namespace_pmem_device_type : false;
75 static bool is_namespace_blk(const struct device *dev)
77 return dev ? dev->type == &namespace_blk_device_type : false;
80 static bool is_namespace_io(const struct device *dev)
82 return dev ? dev->type == &namespace_io_device_type : false;
85 static int is_uuid_busy(struct device *dev, void *data)
87 u8 *uuid1 = data, *uuid2 = NULL;
89 if (is_namespace_pmem(dev)) {
90 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
93 } else if (is_namespace_blk(dev)) {
94 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
97 } else if (is_nd_btt(dev)) {
98 struct nd_btt *nd_btt = to_nd_btt(dev);
100 uuid2 = nd_btt->uuid;
101 } else if (is_nd_pfn(dev)) {
102 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
104 uuid2 = nd_pfn->uuid;
107 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
113 static int is_namespace_uuid_busy(struct device *dev, void *data)
115 if (is_nd_region(dev))
116 return device_for_each_child(dev, data, is_uuid_busy);
121 * nd_is_uuid_unique - verify that no other namespace has @uuid
122 * @dev: any device on a nvdimm_bus
123 * @uuid: uuid to check
125 bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
127 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
131 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
132 if (device_for_each_child(&nvdimm_bus->dev, uuid,
133 is_namespace_uuid_busy) != 0)
138 bool pmem_should_map_pages(struct device *dev)
140 struct nd_region *nd_region = to_nd_region(dev->parent);
141 struct nd_namespace_common *ndns = to_ndns(dev);
142 struct nd_namespace_io *nsio;
144 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
147 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
150 if (is_nd_pfn(dev) || is_nd_btt(dev))
156 nsio = to_nd_namespace_io(dev);
157 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
158 IORESOURCE_SYSTEM_RAM,
159 IORES_DESC_NONE) == REGION_MIXED)
162 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
164 EXPORT_SYMBOL(pmem_should_map_pages);
166 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
168 if (is_namespace_pmem(&ndns->dev)) {
169 struct nd_namespace_pmem *nspm;
171 nspm = to_nd_namespace_pmem(&ndns->dev);
172 if (nspm->lbasize == 0 || nspm->lbasize == 512)
174 else if (nspm->lbasize == 4096)
177 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
182 * There is no namespace label (is_namespace_io()), or the label
183 * indicates the default sector size.
187 EXPORT_SYMBOL(pmem_sector_size);
189 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
192 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
193 const char *suffix = NULL;
195 if (ndns->claim && is_nd_btt(ndns->claim))
198 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
201 if (is_namespace_pmem(&ndns->dev)) {
202 struct nd_namespace_pmem *nspm;
204 nspm = to_nd_namespace_pmem(&ndns->dev);
209 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
210 suffix ? suffix : "");
212 sprintf(name, "pmem%d%s", nd_region->id,
213 suffix ? suffix : "");
214 } else if (is_namespace_blk(&ndns->dev)) {
215 struct nd_namespace_blk *nsblk;
217 nsblk = to_nd_namespace_blk(&ndns->dev);
218 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
219 suffix ? suffix : "");
226 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
228 const u8 *nd_dev_to_uuid(struct device *dev)
230 static const u8 null_uuid[16];
235 if (is_namespace_pmem(dev)) {
236 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
239 } else if (is_namespace_blk(dev)) {
240 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
246 EXPORT_SYMBOL(nd_dev_to_uuid);
248 static ssize_t nstype_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct nd_region *nd_region = to_nd_region(dev->parent);
253 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
255 static DEVICE_ATTR_RO(nstype);
257 static ssize_t __alt_name_store(struct device *dev, const char *buf,
260 char *input, *pos, *alt_name, **ns_altname;
263 if (is_namespace_pmem(dev)) {
264 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
266 ns_altname = &nspm->alt_name;
267 } else if (is_namespace_blk(dev)) {
268 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
270 ns_altname = &nsblk->alt_name;
274 if (dev->driver || to_ndns(dev)->claim)
277 input = kstrndup(buf, len, GFP_KERNEL);
282 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
287 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
293 *ns_altname = alt_name;
294 sprintf(*ns_altname, "%s", pos);
302 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
304 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
305 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
306 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
307 struct nd_label_id label_id;
308 resource_size_t size = 0;
309 struct resource *res;
313 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
314 for_each_dpa_resource(ndd, res)
315 if (strcmp(res->name, label_id.id) == 0)
316 size += resource_size(res);
320 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
322 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
323 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
324 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
325 struct nd_label_id label_id;
326 struct resource *res;
329 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
333 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
334 for_each_dpa_resource(ndd, res) {
335 if (strcmp(res->name, label_id.id) != 0)
338 * Resources with unacknowledged adjustments indicate a
339 * failure to update labels
341 if (res->flags & DPA_RESOURCE_ADJUSTED)
346 /* These values match after a successful label update */
347 if (count != nsblk->num_resources)
350 for (i = 0; i < nsblk->num_resources; i++) {
351 struct resource *found = NULL;
353 for_each_dpa_resource(ndd, res)
354 if (res == nsblk->res[i]) {
366 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
368 resource_size_t size;
370 nvdimm_bus_lock(&nsblk->common.dev);
371 size = __nd_namespace_blk_validate(nsblk);
372 nvdimm_bus_unlock(&nsblk->common.dev);
376 EXPORT_SYMBOL(nd_namespace_blk_validate);
379 static int nd_namespace_label_update(struct nd_region *nd_region,
382 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
383 "namespace must be idle during label update\n");
384 if (dev->driver || to_ndns(dev)->claim)
388 * Only allow label writes that will result in a valid namespace
389 * or deletion of an existing namespace.
391 if (is_namespace_pmem(dev)) {
392 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
393 resource_size_t size = resource_size(&nspm->nsio.res);
395 if (size == 0 && nspm->uuid)
396 /* delete allocation */;
397 else if (!nspm->uuid)
400 return nd_pmem_namespace_label_update(nd_region, nspm, size);
401 } else if (is_namespace_blk(dev)) {
402 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
403 resource_size_t size = nd_namespace_blk_size(nsblk);
405 if (size == 0 && nsblk->uuid)
406 /* delete allocation */;
407 else if (!nsblk->uuid || !nsblk->lbasize)
410 return nd_blk_namespace_label_update(nd_region, nsblk, size);
415 static ssize_t alt_name_store(struct device *dev,
416 struct device_attribute *attr, const char *buf, size_t len)
418 struct nd_region *nd_region = to_nd_region(dev->parent);
422 nvdimm_bus_lock(dev);
423 wait_nvdimm_bus_probe_idle(dev);
424 rc = __alt_name_store(dev, buf, len);
426 rc = nd_namespace_label_update(nd_region, dev);
427 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
428 nvdimm_bus_unlock(dev);
431 return rc < 0 ? rc : len;
434 static ssize_t alt_name_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
439 if (is_namespace_pmem(dev)) {
440 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
442 ns_altname = nspm->alt_name;
443 } else if (is_namespace_blk(dev)) {
444 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
446 ns_altname = nsblk->alt_name;
450 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
452 static DEVICE_ATTR_RW(alt_name);
454 static int scan_free(struct nd_region *nd_region,
455 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
458 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
459 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
463 struct resource *res, *last;
464 resource_size_t new_start;
467 for_each_dpa_resource(ndd, res)
468 if (strcmp(res->name, label_id->id) == 0)
474 if (n >= resource_size(res)) {
475 n -= resource_size(res);
476 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
477 nvdimm_free_dpa(ndd, res);
478 /* retry with last resource deleted */
483 * Keep BLK allocations relegated to high DPA as much as
487 new_start = res->start + n;
489 new_start = res->start;
491 rc = adjust_resource(res, new_start, resource_size(res) - n);
493 res->flags |= DPA_RESOURCE_ADJUSTED;
494 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
502 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
503 * @nd_region: the set of dimms to reclaim @n bytes from
504 * @label_id: unique identifier for the namespace consuming this dpa range
505 * @n: number of bytes per-dimm to release
507 * Assumes resources are ordered. Starting from the end try to
508 * adjust_resource() the allocation to @n, but if @n is larger than the
509 * allocation delete it and find the 'new' last allocation in the label
512 static int shrink_dpa_allocation(struct nd_region *nd_region,
513 struct nd_label_id *label_id, resource_size_t n)
517 for (i = 0; i < nd_region->ndr_mappings; i++) {
518 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
521 rc = scan_free(nd_region, nd_mapping, label_id, n);
529 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
530 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
533 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
534 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
535 resource_size_t first_dpa;
536 struct resource *res;
539 /* allocate blk from highest dpa first */
541 first_dpa = nd_mapping->start + nd_mapping->size - n;
543 first_dpa = nd_mapping->start;
545 /* first resource allocation for this label-id or dimm */
546 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
550 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
556 * space_valid() - validate free dpa space against constraints
557 * @nd_region: hosting region of the free space
558 * @ndd: dimm device data for debug
559 * @label_id: namespace id to allocate space
560 * @prev: potential allocation that precedes free space
561 * @next: allocation that follows the given free space range
562 * @exist: first allocation with same id in the mapping
563 * @n: range that must satisfied for pmem allocations
564 * @valid: free space range to validate
566 * BLK-space is valid as long as it does not precede a PMEM
567 * allocation in a given region. PMEM-space must be contiguous
568 * and adjacent to an existing existing allocation (if one
569 * exists). If reserving PMEM any space is valid.
571 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
572 struct nd_label_id *label_id, struct resource *prev,
573 struct resource *next, struct resource *exist,
574 resource_size_t n, struct resource *valid)
576 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
577 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
579 if (valid->start >= valid->end)
586 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
587 struct nvdimm_bus *nvdimm_bus;
588 struct blk_alloc_info info = {
589 .nd_mapping = nd_mapping,
590 .available = nd_mapping->size,
594 WARN_ON(!is_nd_blk(&nd_region->dev));
595 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
596 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
600 /* allocation needs to be contiguous, so this is all or nothing */
601 if (resource_size(valid) < n)
604 /* we've got all the space we need and no existing allocation */
608 /* allocation needs to be contiguous with the existing namespace */
609 if (valid->start == exist->end + 1
610 || valid->end == exist->start - 1)
614 /* truncate @valid size to 0 */
615 valid->end = valid->start - 1;
619 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
622 static resource_size_t scan_allocate(struct nd_region *nd_region,
623 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
626 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
627 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
628 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
629 struct resource *res, *exist = NULL, valid;
630 const resource_size_t to_allocate = n;
633 for_each_dpa_resource(ndd, res)
634 if (strcmp(label_id->id, res->name) == 0)
637 valid.start = nd_mapping->start;
638 valid.end = mapping_end;
639 valid.name = "free space";
642 for_each_dpa_resource(ndd, res) {
643 struct resource *next = res->sibling, *new_res = NULL;
644 resource_size_t allocate, available = 0;
645 enum alloc_loc loc = ALLOC_ERR;
649 /* ignore resources outside this nd_mapping */
650 if (res->start > mapping_end)
652 if (res->end < nd_mapping->start)
655 /* space at the beginning of the mapping */
656 if (!first++ && res->start > nd_mapping->start) {
657 valid.start = nd_mapping->start;
658 valid.end = res->start - 1;
659 space_valid(nd_region, ndd, label_id, NULL, next, exist,
660 to_allocate, &valid);
661 available = resource_size(&valid);
666 /* space between allocations */
668 valid.start = res->start + resource_size(res);
669 valid.end = min(mapping_end, next->start - 1);
670 space_valid(nd_region, ndd, label_id, res, next, exist,
671 to_allocate, &valid);
672 available = resource_size(&valid);
677 /* space at the end of the mapping */
679 valid.start = res->start + resource_size(res);
680 valid.end = mapping_end;
681 space_valid(nd_region, ndd, label_id, res, next, exist,
682 to_allocate, &valid);
683 available = resource_size(&valid);
688 if (!loc || !available)
690 allocate = min(available, n);
693 if (strcmp(res->name, label_id->id) == 0) {
694 /* adjust current resource up */
695 rc = adjust_resource(res, res->start - allocate,
696 resource_size(res) + allocate);
697 action = "cur grow up";
702 if (strcmp(next->name, label_id->id) == 0) {
703 /* adjust next resource up */
704 rc = adjust_resource(next, next->start
705 - allocate, resource_size(next)
708 action = "next grow up";
709 } else if (strcmp(res->name, label_id->id) == 0) {
710 action = "grow down";
715 if (strcmp(res->name, label_id->id) == 0)
716 action = "grow down";
724 if (strcmp(action, "allocate") == 0) {
725 /* BLK allocate bottom up */
727 valid.start += available - allocate;
729 new_res = nvdimm_allocate_dpa(ndd, label_id,
730 valid.start, allocate);
733 } else if (strcmp(action, "grow down") == 0) {
734 /* adjust current resource down */
735 rc = adjust_resource(res, res->start, resource_size(res)
738 res->flags |= DPA_RESOURCE_ADJUSTED;
744 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
753 * Retry scan with newly inserted resources.
754 * For example, if we did an ALLOC_BEFORE
755 * insertion there may also have been space
756 * available for an ALLOC_AFTER insertion, so we
757 * need to check this same resource again
765 * If we allocated nothing in the BLK case it may be because we are in
766 * an initial "pmem-reserve pass". Only do an initial BLK allocation
767 * when none of the DPA space is reserved.
769 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
770 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
774 static int merge_dpa(struct nd_region *nd_region,
775 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
777 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
778 struct resource *res;
780 if (strncmp("pmem", label_id->id, 4) == 0)
783 for_each_dpa_resource(ndd, res) {
785 struct resource *next = res->sibling;
786 resource_size_t end = res->start + resource_size(res);
788 if (!next || strcmp(res->name, label_id->id) != 0
789 || strcmp(next->name, label_id->id) != 0
790 || end != next->start)
792 end += resource_size(next);
793 nvdimm_free_dpa(ndd, next);
794 rc = adjust_resource(res, res->start, end - res->start);
795 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
798 res->flags |= DPA_RESOURCE_ADJUSTED;
805 int __reserve_free_pmem(struct device *dev, void *data)
807 struct nvdimm *nvdimm = data;
808 struct nd_region *nd_region;
809 struct nd_label_id label_id;
815 nd_region = to_nd_region(dev);
816 if (nd_region->ndr_mappings == 0)
819 memset(&label_id, 0, sizeof(label_id));
820 strcat(label_id.id, "pmem-reserve");
821 for (i = 0; i < nd_region->ndr_mappings; i++) {
822 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
823 resource_size_t n, rem = 0;
825 if (nd_mapping->nvdimm != nvdimm)
828 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
831 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
832 dev_WARN_ONCE(&nd_region->dev, rem,
833 "pmem reserve underrun: %#llx of %#llx bytes\n",
834 (unsigned long long) n - rem,
835 (unsigned long long) n);
836 return rem ? -ENXIO : 0;
842 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
843 struct nd_mapping *nd_mapping)
845 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
846 struct resource *res, *_res;
848 for_each_dpa_resource_safe(ndd, res, _res)
849 if (strcmp(res->name, "pmem-reserve") == 0)
850 nvdimm_free_dpa(ndd, res);
853 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
854 struct nd_mapping *nd_mapping)
856 struct nvdimm *nvdimm = nd_mapping->nvdimm;
859 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
860 __reserve_free_pmem);
862 release_free_pmem(nvdimm_bus, nd_mapping);
867 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
868 * @nd_region: the set of dimms to allocate @n more bytes from
869 * @label_id: unique identifier for the namespace consuming this dpa range
870 * @n: number of bytes per-dimm to add to the existing allocation
872 * Assumes resources are ordered. For BLK regions, first consume
873 * BLK-only available DPA free space, then consume PMEM-aliased DPA
874 * space starting at the highest DPA. For PMEM regions start
875 * allocations from the start of an interleave set and end at the first
876 * BLK allocation or the end of the interleave set, whichever comes
879 static int grow_dpa_allocation(struct nd_region *nd_region,
880 struct nd_label_id *label_id, resource_size_t n)
882 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
883 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
886 for (i = 0; i < nd_region->ndr_mappings; i++) {
887 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
888 resource_size_t rem = n;
892 * In the BLK case try once with all unallocated PMEM
893 * reserved, and once without
895 for (j = is_pmem; j < 2; j++) {
896 bool blk_only = j == 0;
899 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
903 rem = scan_allocate(nd_region, nd_mapping,
906 release_free_pmem(nvdimm_bus, nd_mapping);
908 /* try again and allow encroachments into PMEM */
913 dev_WARN_ONCE(&nd_region->dev, rem,
914 "allocation underrun: %#llx of %#llx bytes\n",
915 (unsigned long long) n - rem,
916 (unsigned long long) n);
920 rc = merge_dpa(nd_region, nd_mapping, label_id);
928 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
929 struct nd_namespace_pmem *nspm, resource_size_t size)
931 struct resource *res = &nspm->nsio.res;
932 resource_size_t offset = 0;
934 if (size && !nspm->uuid) {
939 if (size && nspm->uuid) {
940 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
941 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
942 struct nd_label_id label_id;
943 struct resource *res;
950 nd_label_gen_id(&label_id, nspm->uuid, 0);
952 /* calculate a spa offset from the dpa allocation offset */
953 for_each_dpa_resource(ndd, res)
954 if (strcmp(res->name, label_id.id) == 0) {
955 offset = (res->start - nd_mapping->start)
956 * nd_region->ndr_mappings;
965 res->start = nd_region->ndr_start + offset;
966 res->end = res->start + size - 1;
969 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
972 dev_dbg(dev, "%s: uuid not set\n", where);
978 static ssize_t __size_store(struct device *dev, unsigned long long val)
980 resource_size_t allocated = 0, available = 0;
981 struct nd_region *nd_region = to_nd_region(dev->parent);
982 struct nd_namespace_common *ndns = to_ndns(dev);
983 struct nd_mapping *nd_mapping;
984 struct nvdimm_drvdata *ndd;
985 struct nd_label_id label_id;
986 u32 flags = 0, remainder;
990 if (dev->driver || ndns->claim)
993 if (is_namespace_pmem(dev)) {
994 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
998 } else if (is_namespace_blk(dev)) {
999 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1002 flags = NSLABEL_FLAG_LOCAL;
1007 * We need a uuid for the allocation-label and dimm(s) on which
1008 * to store the label.
1010 if (uuid_not_set(uuid, dev, __func__))
1012 if (nd_region->ndr_mappings == 0) {
1013 dev_dbg(dev, "not associated with dimm(s)\n");
1017 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
1019 dev_dbg(dev, "%llu is not %dK aligned\n", val,
1020 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
1024 nd_label_gen_id(&label_id, uuid, flags);
1025 for (i = 0; i < nd_region->ndr_mappings; i++) {
1026 nd_mapping = &nd_region->mapping[i];
1027 ndd = to_ndd(nd_mapping);
1030 * All dimms in an interleave set, or the base dimm for a blk
1031 * region, need to be enabled for the size to be changed.
1036 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1038 available = nd_region_allocatable_dpa(nd_region);
1040 if (val > available + allocated)
1043 if (val == allocated)
1046 val = div_u64(val, nd_region->ndr_mappings);
1047 allocated = div_u64(allocated, nd_region->ndr_mappings);
1048 if (val < allocated)
1049 rc = shrink_dpa_allocation(nd_region, &label_id,
1052 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1057 if (is_namespace_pmem(dev)) {
1058 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1060 nd_namespace_pmem_set_resource(nd_region, nspm,
1061 val * nd_region->ndr_mappings);
1065 * Try to delete the namespace if we deleted all of its
1066 * allocation, this is not the seed or 0th device for the
1067 * region, and it is not actively claimed by a btt, pfn, or dax
1070 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
1071 nd_device_unregister(dev, ND_ASYNC);
1076 static ssize_t size_store(struct device *dev,
1077 struct device_attribute *attr, const char *buf, size_t len)
1079 struct nd_region *nd_region = to_nd_region(dev->parent);
1080 unsigned long long val;
1084 rc = kstrtoull(buf, 0, &val);
1089 nvdimm_bus_lock(dev);
1090 wait_nvdimm_bus_probe_idle(dev);
1091 rc = __size_store(dev, val);
1093 rc = nd_namespace_label_update(nd_region, dev);
1095 if (is_namespace_pmem(dev)) {
1096 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1099 } else if (is_namespace_blk(dev)) {
1100 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1102 uuid = &nsblk->uuid;
1105 if (rc == 0 && val == 0 && uuid) {
1106 /* setting size zero == 'delete namespace' */
1111 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
1113 nvdimm_bus_unlock(dev);
1116 return rc < 0 ? rc : len;
1119 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1121 struct device *dev = &ndns->dev;
1123 if (is_namespace_pmem(dev)) {
1124 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1126 return resource_size(&nspm->nsio.res);
1127 } else if (is_namespace_blk(dev)) {
1128 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1129 } else if (is_namespace_io(dev)) {
1130 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1132 return resource_size(&nsio->res);
1134 WARN_ONCE(1, "unknown namespace type\n");
1138 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1140 resource_size_t size;
1142 nvdimm_bus_lock(&ndns->dev);
1143 size = __nvdimm_namespace_capacity(ndns);
1144 nvdimm_bus_unlock(&ndns->dev);
1148 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1150 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1153 bool locked = false;
1154 struct device *dev = &ndns->dev;
1155 struct nd_region *nd_region = to_nd_region(dev->parent);
1157 for (i = 0; i < nd_region->ndr_mappings; i++) {
1158 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1159 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1161 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1162 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1168 EXPORT_SYMBOL(nvdimm_namespace_locked);
1170 static ssize_t size_show(struct device *dev,
1171 struct device_attribute *attr, char *buf)
1173 return sprintf(buf, "%llu\n", (unsigned long long)
1174 nvdimm_namespace_capacity(to_ndns(dev)));
1176 static DEVICE_ATTR(size, 0444, size_show, size_store);
1178 static u8 *namespace_to_uuid(struct device *dev)
1180 if (is_namespace_pmem(dev)) {
1181 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1184 } else if (is_namespace_blk(dev)) {
1185 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1189 return ERR_PTR(-ENXIO);
1192 static ssize_t uuid_show(struct device *dev,
1193 struct device_attribute *attr, char *buf)
1195 u8 *uuid = namespace_to_uuid(dev);
1198 return PTR_ERR(uuid);
1200 return sprintf(buf, "%pUb\n", uuid);
1201 return sprintf(buf, "\n");
1205 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1206 * @nd_region: parent region so we can updates all dimms in the set
1207 * @dev: namespace type for generating label_id
1208 * @new_uuid: incoming uuid
1209 * @old_uuid: reference to the uuid storage location in the namespace object
1211 static int namespace_update_uuid(struct nd_region *nd_region,
1212 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1214 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1215 struct nd_label_id old_label_id;
1216 struct nd_label_id new_label_id;
1219 if (!nd_is_uuid_unique(dev, new_uuid))
1222 if (*old_uuid == NULL)
1226 * If we've already written a label with this uuid, then it's
1227 * too late to rename because we can't reliably update the uuid
1228 * without losing the old namespace. Userspace must delete this
1229 * namespace to abandon the old uuid.
1231 for (i = 0; i < nd_region->ndr_mappings; i++) {
1232 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1235 * This check by itself is sufficient because old_uuid
1236 * would be NULL above if this uuid did not exist in the
1237 * currently written set.
1239 * FIXME: can we delete uuid with zero dpa allocated?
1241 if (list_empty(&nd_mapping->labels))
1245 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1246 nd_label_gen_id(&new_label_id, new_uuid, flags);
1247 for (i = 0; i < nd_region->ndr_mappings; i++) {
1248 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1249 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1250 struct resource *res;
1252 for_each_dpa_resource(ndd, res)
1253 if (strcmp(res->name, old_label_id.id) == 0)
1254 sprintf((void *) res->name, "%s",
1259 *old_uuid = new_uuid;
1263 static ssize_t uuid_store(struct device *dev,
1264 struct device_attribute *attr, const char *buf, size_t len)
1266 struct nd_region *nd_region = to_nd_region(dev->parent);
1271 if (is_namespace_pmem(dev)) {
1272 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1274 ns_uuid = &nspm->uuid;
1275 } else if (is_namespace_blk(dev)) {
1276 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1278 ns_uuid = &nsblk->uuid;
1283 nvdimm_bus_lock(dev);
1284 wait_nvdimm_bus_probe_idle(dev);
1285 if (to_ndns(dev)->claim)
1288 rc = nd_uuid_store(dev, &uuid, buf, len);
1290 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1292 rc = nd_namespace_label_update(nd_region, dev);
1295 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1296 buf[len - 1] == '\n' ? "" : "\n");
1297 nvdimm_bus_unlock(dev);
1300 return rc < 0 ? rc : len;
1302 static DEVICE_ATTR_RW(uuid);
1304 static ssize_t resource_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1307 struct resource *res;
1309 if (is_namespace_pmem(dev)) {
1310 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1312 res = &nspm->nsio.res;
1313 } else if (is_namespace_io(dev)) {
1314 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1320 /* no address to convey if the namespace has no allocation */
1321 if (resource_size(res) == 0)
1323 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1325 static DEVICE_ATTR_RO(resource);
1327 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
1328 4096, 4104, 4160, 4224, 0 };
1330 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1332 static ssize_t sector_size_show(struct device *dev,
1333 struct device_attribute *attr, char *buf)
1335 if (is_namespace_blk(dev)) {
1336 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1338 return nd_size_select_show(nsblk->lbasize,
1339 blk_lbasize_supported, buf);
1342 if (is_namespace_pmem(dev)) {
1343 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1345 return nd_size_select_show(nspm->lbasize,
1346 pmem_lbasize_supported, buf);
1351 static ssize_t sector_size_store(struct device *dev,
1352 struct device_attribute *attr, const char *buf, size_t len)
1354 struct nd_region *nd_region = to_nd_region(dev->parent);
1355 const unsigned long *supported;
1356 unsigned long *lbasize;
1359 if (is_namespace_blk(dev)) {
1360 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1362 lbasize = &nsblk->lbasize;
1363 supported = blk_lbasize_supported;
1364 } else if (is_namespace_pmem(dev)) {
1365 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1367 lbasize = &nspm->lbasize;
1368 supported = pmem_lbasize_supported;
1373 nvdimm_bus_lock(dev);
1374 if (to_ndns(dev)->claim)
1377 rc = nd_size_select_store(dev, buf, lbasize, supported);
1379 rc = nd_namespace_label_update(nd_region, dev);
1380 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1381 buf, buf[len - 1] == '\n' ? "" : "\n");
1382 nvdimm_bus_unlock(dev);
1385 return rc ? rc : len;
1387 static DEVICE_ATTR_RW(sector_size);
1389 static ssize_t dpa_extents_show(struct device *dev,
1390 struct device_attribute *attr, char *buf)
1392 struct nd_region *nd_region = to_nd_region(dev->parent);
1393 struct nd_label_id label_id;
1398 nvdimm_bus_lock(dev);
1399 if (is_namespace_pmem(dev)) {
1400 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1404 } else if (is_namespace_blk(dev)) {
1405 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1408 flags = NSLABEL_FLAG_LOCAL;
1414 nd_label_gen_id(&label_id, uuid, flags);
1415 for (i = 0; i < nd_region->ndr_mappings; i++) {
1416 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1417 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1418 struct resource *res;
1420 for_each_dpa_resource(ndd, res)
1421 if (strcmp(res->name, label_id.id) == 0)
1425 nvdimm_bus_unlock(dev);
1427 return sprintf(buf, "%d\n", count);
1429 static DEVICE_ATTR_RO(dpa_extents);
1431 static int btt_claim_class(struct device *dev)
1433 struct nd_region *nd_region = to_nd_region(dev->parent);
1434 int i, loop_bitmask = 0;
1436 for (i = 0; i < nd_region->ndr_mappings; i++) {
1437 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1438 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1439 struct nd_namespace_index *nsindex;
1442 * If any of the DIMMs do not support labels the only
1443 * possible BTT format is v1.
1450 nsindex = to_namespace_index(ndd, ndd->ns_current);
1451 if (nsindex == NULL)
1454 /* check whether existing labels are v1.1 or v1.2 */
1455 if (__le16_to_cpu(nsindex->major) == 1
1456 && __le16_to_cpu(nsindex->minor) == 1)
1463 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1464 * block is found, a v1.1 label for any mapping will set bit 1, and a
1465 * v1.2 label will set bit 2.
1467 * At the end of the loop, at most one of the three bits must be set.
1468 * If multiple bits were set, it means the different mappings disagree
1469 * about their labels, and this must be cleaned up first.
1471 * If all the label index blocks are found to agree, nsindex of NULL
1472 * implies labels haven't been initialized yet, and when they will,
1473 * they will be of the 1.2 format, so we can assume BTT2.0
1475 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1476 * found, we enforce BTT2.0
1478 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1480 switch (loop_bitmask) {
1483 return NVDIMM_CCLASS_BTT;
1486 return NVDIMM_CCLASS_BTT2;
1492 static ssize_t holder_show(struct device *dev,
1493 struct device_attribute *attr, char *buf)
1495 struct nd_namespace_common *ndns = to_ndns(dev);
1499 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1504 static DEVICE_ATTR_RO(holder);
1506 static ssize_t __holder_class_store(struct device *dev, const char *buf)
1508 struct nd_namespace_common *ndns = to_ndns(dev);
1510 if (dev->driver || ndns->claim)
1513 if (sysfs_streq(buf, "btt"))
1514 ndns->claim_class = btt_claim_class(dev);
1515 else if (sysfs_streq(buf, "pfn"))
1516 ndns->claim_class = NVDIMM_CCLASS_PFN;
1517 else if (sysfs_streq(buf, "dax"))
1518 ndns->claim_class = NVDIMM_CCLASS_DAX;
1519 else if (sysfs_streq(buf, ""))
1520 ndns->claim_class = NVDIMM_CCLASS_NONE;
1524 /* btt_claim_class() could've returned an error */
1525 if (ndns->claim_class < 0)
1526 return ndns->claim_class;
1531 static ssize_t holder_class_store(struct device *dev,
1532 struct device_attribute *attr, const char *buf, size_t len)
1534 struct nd_region *nd_region = to_nd_region(dev->parent);
1538 nvdimm_bus_lock(dev);
1539 wait_nvdimm_bus_probe_idle(dev);
1540 rc = __holder_class_store(dev, buf);
1542 rc = nd_namespace_label_update(nd_region, dev);
1543 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
1544 nvdimm_bus_unlock(dev);
1547 return rc < 0 ? rc : len;
1550 static ssize_t holder_class_show(struct device *dev,
1551 struct device_attribute *attr, char *buf)
1553 struct nd_namespace_common *ndns = to_ndns(dev);
1557 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1558 rc = sprintf(buf, "\n");
1559 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1560 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1561 rc = sprintf(buf, "btt\n");
1562 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1563 rc = sprintf(buf, "pfn\n");
1564 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1565 rc = sprintf(buf, "dax\n");
1567 rc = sprintf(buf, "<unknown>\n");
1572 static DEVICE_ATTR_RW(holder_class);
1574 static ssize_t mode_show(struct device *dev,
1575 struct device_attribute *attr, char *buf)
1577 struct nd_namespace_common *ndns = to_ndns(dev);
1578 struct device *claim;
1583 claim = ndns->claim;
1584 if (claim && is_nd_btt(claim))
1586 else if (claim && is_nd_pfn(claim))
1588 else if (claim && is_nd_dax(claim))
1590 else if (!claim && pmem_should_map_pages(dev))
1594 rc = sprintf(buf, "%s\n", mode);
1599 static DEVICE_ATTR_RO(mode);
1601 static ssize_t force_raw_store(struct device *dev,
1602 struct device_attribute *attr, const char *buf, size_t len)
1605 int rc = strtobool(buf, &force_raw);
1610 to_ndns(dev)->force_raw = force_raw;
1614 static ssize_t force_raw_show(struct device *dev,
1615 struct device_attribute *attr, char *buf)
1617 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1619 static DEVICE_ATTR_RW(force_raw);
1621 static struct attribute *nd_namespace_attributes[] = {
1622 &dev_attr_nstype.attr,
1623 &dev_attr_size.attr,
1624 &dev_attr_mode.attr,
1625 &dev_attr_uuid.attr,
1626 &dev_attr_holder.attr,
1627 &dev_attr_resource.attr,
1628 &dev_attr_alt_name.attr,
1629 &dev_attr_force_raw.attr,
1630 &dev_attr_sector_size.attr,
1631 &dev_attr_dpa_extents.attr,
1632 &dev_attr_holder_class.attr,
1636 static umode_t namespace_visible(struct kobject *kobj,
1637 struct attribute *a, int n)
1639 struct device *dev = container_of(kobj, struct device, kobj);
1641 if (a == &dev_attr_resource.attr) {
1642 if (is_namespace_blk(dev))
1647 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1648 if (a == &dev_attr_size.attr)
1654 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1655 || a == &dev_attr_holder.attr
1656 || a == &dev_attr_holder_class.attr
1657 || a == &dev_attr_force_raw.attr
1658 || a == &dev_attr_mode.attr)
1664 static struct attribute_group nd_namespace_attribute_group = {
1665 .attrs = nd_namespace_attributes,
1666 .is_visible = namespace_visible,
1669 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1670 &nd_device_attribute_group,
1671 &nd_namespace_attribute_group,
1672 &nd_numa_attribute_group,
1676 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1678 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1679 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1680 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1681 struct nd_namespace_common *ndns = NULL;
1682 resource_size_t size;
1684 if (nd_btt || nd_pfn || nd_dax) {
1686 ndns = nd_btt->ndns;
1688 ndns = nd_pfn->ndns;
1690 ndns = nd_dax->nd_pfn.ndns;
1693 return ERR_PTR(-ENODEV);
1696 * Flush any in-progess probes / removals in the driver
1697 * for the raw personality of this namespace.
1699 device_lock(&ndns->dev);
1700 device_unlock(&ndns->dev);
1701 if (ndns->dev.driver) {
1702 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1704 return ERR_PTR(-EBUSY);
1706 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1707 "host (%s) vs claim (%s) mismatch\n",
1709 dev_name(ndns->claim)))
1710 return ERR_PTR(-ENXIO);
1712 ndns = to_ndns(dev);
1714 dev_dbg(dev, "claimed by %s, failing probe\n",
1715 dev_name(ndns->claim));
1717 return ERR_PTR(-ENXIO);
1721 if (nvdimm_namespace_locked(ndns))
1722 return ERR_PTR(-EACCES);
1724 size = nvdimm_namespace_capacity(ndns);
1725 if (size < ND_MIN_NAMESPACE_SIZE) {
1726 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1727 &size, ND_MIN_NAMESPACE_SIZE);
1728 return ERR_PTR(-ENODEV);
1731 if (is_namespace_pmem(&ndns->dev)) {
1732 struct nd_namespace_pmem *nspm;
1734 nspm = to_nd_namespace_pmem(&ndns->dev);
1735 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1736 return ERR_PTR(-ENODEV);
1737 } else if (is_namespace_blk(&ndns->dev)) {
1738 struct nd_namespace_blk *nsblk;
1740 nsblk = to_nd_namespace_blk(&ndns->dev);
1741 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1742 return ERR_PTR(-ENODEV);
1743 if (!nsblk->lbasize) {
1744 dev_dbg(&ndns->dev, "sector size not set\n");
1745 return ERR_PTR(-ENODEV);
1747 if (!nd_namespace_blk_validate(nsblk))
1748 return ERR_PTR(-ENODEV);
1753 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1755 static struct device **create_namespace_io(struct nd_region *nd_region)
1757 struct nd_namespace_io *nsio;
1758 struct device *dev, **devs;
1759 struct resource *res;
1761 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1765 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1771 dev = &nsio->common.dev;
1772 dev->type = &namespace_io_device_type;
1773 dev->parent = &nd_region->dev;
1775 res->name = dev_name(&nd_region->dev);
1776 res->flags = IORESOURCE_MEM;
1777 res->start = nd_region->ndr_start;
1778 res->end = res->start + nd_region->ndr_size - 1;
1784 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1785 u64 cookie, u16 pos)
1787 struct nd_namespace_label *found = NULL;
1790 for (i = 0; i < nd_region->ndr_mappings; i++) {
1791 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1792 struct nd_interleave_set *nd_set = nd_region->nd_set;
1793 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1794 struct nd_label_ent *label_ent;
1795 bool found_uuid = false;
1797 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1798 struct nd_namespace_label *nd_label = label_ent->label;
1799 u16 position, nlabel;
1804 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1805 position = __le16_to_cpu(nd_label->position);
1806 nlabel = __le16_to_cpu(nd_label->nlabel);
1808 if (isetcookie != cookie)
1811 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1814 if (namespace_label_has(ndd, type_guid)
1815 && !guid_equal(&nd_set->type_guid,
1816 &nd_label->type_guid)) {
1817 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1818 nd_set->type_guid.b,
1819 nd_label->type_guid.b);
1824 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1828 if (nlabel != nd_region->ndr_mappings)
1830 if (position != pos)
1838 return found != NULL;
1841 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1848 for (i = 0; i < nd_region->ndr_mappings; i++) {
1849 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1850 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1851 struct nd_namespace_label *nd_label = NULL;
1852 u64 hw_start, hw_end, pmem_start, pmem_end;
1853 struct nd_label_ent *label_ent;
1855 lockdep_assert_held(&nd_mapping->lock);
1856 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1857 nd_label = label_ent->label;
1860 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1871 * Check that this label is compliant with the dpa
1872 * range published in NFIT
1874 hw_start = nd_mapping->start;
1875 hw_end = hw_start + nd_mapping->size;
1876 pmem_start = __le64_to_cpu(nd_label->dpa);
1877 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1878 if (pmem_start >= hw_start && pmem_start < hw_end
1879 && pmem_end <= hw_end && pmem_end > hw_start)
1882 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1883 dev_name(ndd->dev), nd_label->uuid);
1887 /* move recently validated label to the front of the list */
1888 list_move(&label_ent->list, &nd_mapping->labels);
1894 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1895 * @nd_region: region with mappings to validate
1896 * @nspm: target namespace to create
1897 * @nd_label: target pmem namespace label to evaluate
1899 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1900 struct nd_namespace_index *nsindex,
1901 struct nd_namespace_label *nd_label)
1903 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1904 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1905 struct nd_label_ent *label_ent;
1906 struct nd_namespace_pmem *nspm;
1907 struct nd_mapping *nd_mapping;
1908 resource_size_t size = 0;
1909 struct resource *res;
1915 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1916 return ERR_PTR(-ENXIO);
1919 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1920 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1922 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1923 return ERR_PTR(-EAGAIN);
1925 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1929 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1931 return ERR_PTR(-ENOMEM);
1934 dev = &nspm->nsio.common.dev;
1935 dev->type = &namespace_pmem_device_type;
1936 dev->parent = &nd_region->dev;
1937 res = &nspm->nsio.res;
1938 res->name = dev_name(&nd_region->dev);
1939 res->flags = IORESOURCE_MEM;
1941 for (i = 0; i < nd_region->ndr_mappings; i++) {
1942 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1944 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1949 if (i < nd_region->ndr_mappings) {
1950 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1953 * Give up if we don't find an instance of a uuid at each
1954 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1955 * find a dimm with two instances of the same uuid.
1957 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1958 nvdimm_name(nvdimm), nd_label->uuid);
1964 * Fix up each mapping's 'labels' to have the validated pmem label for
1965 * that position at labels[0], and NULL at labels[1]. In the process,
1966 * check that the namespace aligns with interleave-set. We know
1967 * that it does not overlap with any blk namespaces by virtue of
1968 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1971 rc = select_pmem_id(nd_region, nd_label->uuid);
1975 /* Calculate total size and populate namespace properties from label0 */
1976 for (i = 0; i < nd_region->ndr_mappings; i++) {
1977 struct nd_namespace_label *label0;
1978 struct nvdimm_drvdata *ndd;
1980 nd_mapping = &nd_region->mapping[i];
1981 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1982 typeof(*label_ent), list);
1983 label0 = label_ent ? label_ent->label : 0;
1990 size += __le64_to_cpu(label0->rawsize);
1991 if (__le16_to_cpu(label0->position) != 0)
1993 WARN_ON(nspm->alt_name || nspm->uuid);
1994 nspm->alt_name = kmemdup((void __force *) label0->name,
1995 NSLABEL_NAME_LEN, GFP_KERNEL);
1996 nspm->uuid = kmemdup((void __force *) label0->uuid,
1997 NSLABEL_UUID_LEN, GFP_KERNEL);
1998 nspm->lbasize = __le64_to_cpu(label0->lbasize);
1999 ndd = to_ndd(nd_mapping);
2000 if (namespace_label_has(ndd, abstraction_guid))
2001 nspm->nsio.common.claim_class
2002 = to_nvdimm_cclass(&label0->abstraction_guid);
2006 if (!nspm->alt_name || !nspm->uuid) {
2011 nd_namespace_pmem_set_resource(nd_region, nspm, size);
2015 namespace_pmem_release(dev);
2018 dev_dbg(&nd_region->dev, "invalid label(s)\n");
2021 dev_dbg(&nd_region->dev, "label not found\n");
2024 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
2030 struct resource *nsblk_add_resource(struct nd_region *nd_region,
2031 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2032 resource_size_t start)
2034 struct nd_label_id label_id;
2035 struct resource *res;
2037 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2038 res = krealloc(nsblk->res,
2039 sizeof(void *) * (nsblk->num_resources + 1),
2043 nsblk->res = (struct resource **) res;
2044 for_each_dpa_resource(ndd, res)
2045 if (strcmp(res->name, label_id.id) == 0
2046 && res->start == start) {
2047 nsblk->res[nsblk->num_resources++] = res;
2053 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2055 struct nd_namespace_blk *nsblk;
2058 if (!is_nd_blk(&nd_region->dev))
2061 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2065 dev = &nsblk->common.dev;
2066 dev->type = &namespace_blk_device_type;
2067 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2068 if (nsblk->id < 0) {
2072 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2073 dev->parent = &nd_region->dev;
2074 dev->groups = nd_namespace_attribute_groups;
2076 return &nsblk->common.dev;
2079 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2081 struct nd_namespace_pmem *nspm;
2082 struct resource *res;
2085 if (!is_memory(&nd_region->dev))
2088 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2092 dev = &nspm->nsio.common.dev;
2093 dev->type = &namespace_pmem_device_type;
2094 dev->parent = &nd_region->dev;
2095 res = &nspm->nsio.res;
2096 res->name = dev_name(&nd_region->dev);
2097 res->flags = IORESOURCE_MEM;
2099 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2104 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
2105 dev->groups = nd_namespace_attribute_groups;
2106 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2111 void nd_region_create_ns_seed(struct nd_region *nd_region)
2113 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2115 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2118 if (is_nd_blk(&nd_region->dev))
2119 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2121 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2124 * Seed creation failures are not fatal, provisioning is simply
2125 * disabled until memory becomes available
2127 if (!nd_region->ns_seed)
2128 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2129 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
2131 nd_device_register(nd_region->ns_seed);
2134 void nd_region_create_dax_seed(struct nd_region *nd_region)
2136 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2137 nd_region->dax_seed = nd_dax_create(nd_region);
2139 * Seed creation failures are not fatal, provisioning is simply
2140 * disabled until memory becomes available
2142 if (!nd_region->dax_seed)
2143 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2146 void nd_region_create_pfn_seed(struct nd_region *nd_region)
2148 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2149 nd_region->pfn_seed = nd_pfn_create(nd_region);
2151 * Seed creation failures are not fatal, provisioning is simply
2152 * disabled until memory becomes available
2154 if (!nd_region->pfn_seed)
2155 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2158 void nd_region_create_btt_seed(struct nd_region *nd_region)
2160 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2161 nd_region->btt_seed = nd_btt_create(nd_region);
2163 * Seed creation failures are not fatal, provisioning is simply
2164 * disabled until memory becomes available
2166 if (!nd_region->btt_seed)
2167 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2170 static int add_namespace_resource(struct nd_region *nd_region,
2171 struct nd_namespace_label *nd_label, struct device **devs,
2174 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2175 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2178 for (i = 0; i < count; i++) {
2179 u8 *uuid = namespace_to_uuid(devs[i]);
2180 struct resource *res;
2182 if (IS_ERR_OR_NULL(uuid)) {
2187 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2189 if (is_namespace_blk(devs[i])) {
2190 res = nsblk_add_resource(nd_region, ndd,
2191 to_nd_namespace_blk(devs[i]),
2192 __le64_to_cpu(nd_label->dpa));
2195 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2197 dev_err(&nd_region->dev,
2198 "error: conflicting extents for uuid: %pUb\n",
2208 static struct device *create_namespace_blk(struct nd_region *nd_region,
2209 struct nd_namespace_label *nd_label, int count)
2212 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2213 struct nd_interleave_set *nd_set = nd_region->nd_set;
2214 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2215 struct nd_namespace_blk *nsblk;
2216 char name[NSLABEL_NAME_LEN];
2217 struct device *dev = NULL;
2218 struct resource *res;
2220 if (namespace_label_has(ndd, type_guid)) {
2221 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
2222 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
2223 nd_set->type_guid.b,
2224 nd_label->type_guid.b);
2225 return ERR_PTR(-EAGAIN);
2228 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
2229 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
2231 __le64_to_cpu(nd_label->isetcookie));
2232 return ERR_PTR(-EAGAIN);
2236 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2238 return ERR_PTR(-ENOMEM);
2239 dev = &nsblk->common.dev;
2240 dev->type = &namespace_blk_device_type;
2241 dev->parent = &nd_region->dev;
2243 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2244 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2246 if (namespace_label_has(ndd, abstraction_guid))
2247 nsblk->common.claim_class
2248 = to_nvdimm_cclass(&nd_label->abstraction_guid);
2251 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2253 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2255 if (!nsblk->alt_name)
2258 res = nsblk_add_resource(nd_region, ndd, nsblk,
2259 __le64_to_cpu(nd_label->dpa));
2262 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2265 namespace_blk_release(dev);
2266 return ERR_PTR(-ENXIO);
2269 static int cmp_dpa(const void *a, const void *b)
2271 const struct device *dev_a = *(const struct device **) a;
2272 const struct device *dev_b = *(const struct device **) b;
2273 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2274 struct nd_namespace_pmem *nspm_a, *nspm_b;
2276 if (is_namespace_io(dev_a))
2279 if (is_namespace_blk(dev_a)) {
2280 nsblk_a = to_nd_namespace_blk(dev_a);
2281 nsblk_b = to_nd_namespace_blk(dev_b);
2283 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2284 sizeof(resource_size_t));
2287 nspm_a = to_nd_namespace_pmem(dev_a);
2288 nspm_b = to_nd_namespace_pmem(dev_b);
2290 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2291 sizeof(resource_size_t));
2294 static struct device **scan_labels(struct nd_region *nd_region)
2297 struct device *dev, **devs = NULL;
2298 struct nd_label_ent *label_ent, *e;
2299 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2300 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
2302 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2303 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
2304 struct nd_namespace_label *nd_label = label_ent->label;
2305 struct device **__devs;
2310 flags = __le32_to_cpu(nd_label->flags);
2311 if (is_nd_blk(&nd_region->dev)
2312 == !!(flags & NSLABEL_FLAG_LOCAL))
2313 /* pass, region matches label type */;
2317 /* skip labels that describe extents outside of the region */
2318 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
2321 i = add_namespace_resource(nd_region, nd_label, devs, count);
2326 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2329 memcpy(__devs, devs, sizeof(dev) * count);
2333 if (is_nd_blk(&nd_region->dev))
2334 dev = create_namespace_blk(nd_region, nd_label, count);
2336 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2337 struct nd_namespace_index *nsindex;
2339 nsindex = to_namespace_index(ndd, ndd->ns_current);
2340 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
2344 switch (PTR_ERR(dev)) {
2346 /* skip invalid labels */
2349 /* fallthrough to seed creation */
2355 devs[count++] = dev;
2359 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2360 count, is_nd_blk(&nd_region->dev)
2361 ? "blk" : "pmem", count == 1 ? "" : "s");
2364 /* Publish a zero-sized namespace for userspace to configure. */
2365 nd_mapping_free_labels(nd_mapping);
2367 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2370 if (is_nd_blk(&nd_region->dev)) {
2371 struct nd_namespace_blk *nsblk;
2373 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2376 dev = &nsblk->common.dev;
2377 dev->type = &namespace_blk_device_type;
2379 struct nd_namespace_pmem *nspm;
2381 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2384 dev = &nspm->nsio.common.dev;
2385 dev->type = &namespace_pmem_device_type;
2386 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2388 dev->parent = &nd_region->dev;
2389 devs[count++] = dev;
2390 } else if (is_memory(&nd_region->dev)) {
2391 /* clean unselected labels */
2392 for (i = 0; i < nd_region->ndr_mappings; i++) {
2393 struct list_head *l, *e;
2397 nd_mapping = &nd_region->mapping[i];
2398 if (list_empty(&nd_mapping->labels)) {
2404 list_for_each_safe(l, e, &nd_mapping->labels) {
2407 list_move_tail(l, &list);
2409 nd_mapping_free_labels(nd_mapping);
2410 list_splice_init(&list, &nd_mapping->labels);
2415 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2421 for (i = 0; devs[i]; i++)
2422 if (is_nd_blk(&nd_region->dev))
2423 namespace_blk_release(devs[i]);
2425 namespace_pmem_release(devs[i]);
2431 static struct device **create_namespaces(struct nd_region *nd_region)
2433 struct nd_mapping *nd_mapping;
2434 struct device **devs;
2437 if (nd_region->ndr_mappings == 0)
2440 /* lock down all mappings while we scan labels */
2441 for (i = 0; i < nd_region->ndr_mappings; i++) {
2442 nd_mapping = &nd_region->mapping[i];
2443 mutex_lock_nested(&nd_mapping->lock, i);
2446 devs = scan_labels(nd_region);
2448 for (i = 0; i < nd_region->ndr_mappings; i++) {
2449 int reverse = nd_region->ndr_mappings - 1 - i;
2451 nd_mapping = &nd_region->mapping[reverse];
2452 mutex_unlock(&nd_mapping->lock);
2458 static int init_active_labels(struct nd_region *nd_region)
2462 for (i = 0; i < nd_region->ndr_mappings; i++) {
2463 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2464 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2465 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2466 struct nd_label_ent *label_ent;
2470 * If the dimm is disabled then we may need to prevent
2471 * the region from being activated.
2474 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2475 /* fail, label data may be unreadable */;
2476 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2477 /* fail, labels needed to disambiguate dpa */;
2481 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2482 dev_name(&nd_mapping->nvdimm->dev),
2483 test_bit(NDD_LOCKED, &nvdimm->flags)
2484 ? "locked" : "disabled");
2487 nd_mapping->ndd = ndd;
2488 atomic_inc(&nvdimm->busy);
2491 count = nd_label_active_count(ndd);
2492 dev_dbg(ndd->dev, "count: %d\n", count);
2495 for (j = 0; j < count; j++) {
2496 struct nd_namespace_label *label;
2498 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2501 label = nd_label_active(ndd, j);
2502 if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2503 u32 flags = __le32_to_cpu(label->flags);
2505 flags &= ~NSLABEL_FLAG_LOCAL;
2506 label->flags = __cpu_to_le32(flags);
2508 label_ent->label = label;
2510 mutex_lock(&nd_mapping->lock);
2511 list_add_tail(&label_ent->list, &nd_mapping->labels);
2512 mutex_unlock(&nd_mapping->lock);
2518 mutex_lock(&nd_mapping->lock);
2519 nd_mapping_free_labels(nd_mapping);
2520 mutex_unlock(&nd_mapping->lock);
2527 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2529 struct device **devs = NULL;
2530 int i, rc = 0, type;
2533 nvdimm_bus_lock(&nd_region->dev);
2534 rc = init_active_labels(nd_region);
2536 nvdimm_bus_unlock(&nd_region->dev);
2540 type = nd_region_to_nstype(nd_region);
2542 case ND_DEVICE_NAMESPACE_IO:
2543 devs = create_namespace_io(nd_region);
2545 case ND_DEVICE_NAMESPACE_PMEM:
2546 case ND_DEVICE_NAMESPACE_BLK:
2547 devs = create_namespaces(nd_region);
2552 nvdimm_bus_unlock(&nd_region->dev);
2557 for (i = 0; devs[i]; i++) {
2558 struct device *dev = devs[i];
2561 if (type == ND_DEVICE_NAMESPACE_BLK) {
2562 struct nd_namespace_blk *nsblk;
2564 nsblk = to_nd_namespace_blk(dev);
2565 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2568 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2569 struct nd_namespace_pmem *nspm;
2571 nspm = to_nd_namespace_pmem(dev);
2572 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2580 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2581 dev->groups = nd_namespace_attribute_groups;
2582 nd_device_register(dev);
2585 nd_region->ns_seed = devs[0];
2590 for (j = i; devs[j]; j++) {
2591 struct device *dev = devs[j];
2593 device_initialize(dev);
2598 * All of the namespaces we tried to register failed, so
2599 * fail region activation.