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 = kmemdup(buf, len + 1, GFP_KERNEL);
283 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
288 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
294 *ns_altname = alt_name;
295 sprintf(*ns_altname, "%s", pos);
303 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
305 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
306 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
307 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
308 struct nd_label_id label_id;
309 resource_size_t size = 0;
310 struct resource *res;
314 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
315 for_each_dpa_resource(ndd, res)
316 if (strcmp(res->name, label_id.id) == 0)
317 size += resource_size(res);
321 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
323 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
324 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
325 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
326 struct nd_label_id label_id;
327 struct resource *res;
330 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
334 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
335 for_each_dpa_resource(ndd, res) {
336 if (strcmp(res->name, label_id.id) != 0)
339 * Resources with unacknowledged adjustments indicate a
340 * failure to update labels
342 if (res->flags & DPA_RESOURCE_ADJUSTED)
347 /* These values match after a successful label update */
348 if (count != nsblk->num_resources)
351 for (i = 0; i < nsblk->num_resources; i++) {
352 struct resource *found = NULL;
354 for_each_dpa_resource(ndd, res)
355 if (res == nsblk->res[i]) {
367 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
369 resource_size_t size;
371 nvdimm_bus_lock(&nsblk->common.dev);
372 size = __nd_namespace_blk_validate(nsblk);
373 nvdimm_bus_unlock(&nsblk->common.dev);
377 EXPORT_SYMBOL(nd_namespace_blk_validate);
380 static int nd_namespace_label_update(struct nd_region *nd_region,
383 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
384 "namespace must be idle during label update\n");
385 if (dev->driver || to_ndns(dev)->claim)
389 * Only allow label writes that will result in a valid namespace
390 * or deletion of an existing namespace.
392 if (is_namespace_pmem(dev)) {
393 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
394 resource_size_t size = resource_size(&nspm->nsio.res);
396 if (size == 0 && nspm->uuid)
397 /* delete allocation */;
398 else if (!nspm->uuid)
401 return nd_pmem_namespace_label_update(nd_region, nspm, size);
402 } else if (is_namespace_blk(dev)) {
403 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
404 resource_size_t size = nd_namespace_blk_size(nsblk);
406 if (size == 0 && nsblk->uuid)
407 /* delete allocation */;
408 else if (!nsblk->uuid || !nsblk->lbasize)
411 return nd_blk_namespace_label_update(nd_region, nsblk, size);
416 static ssize_t alt_name_store(struct device *dev,
417 struct device_attribute *attr, const char *buf, size_t len)
419 struct nd_region *nd_region = to_nd_region(dev->parent);
423 nvdimm_bus_lock(dev);
424 wait_nvdimm_bus_probe_idle(dev);
425 rc = __alt_name_store(dev, buf, len);
427 rc = nd_namespace_label_update(nd_region, dev);
428 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
429 nvdimm_bus_unlock(dev);
432 return rc < 0 ? rc : len;
435 static ssize_t alt_name_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
440 if (is_namespace_pmem(dev)) {
441 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
443 ns_altname = nspm->alt_name;
444 } else if (is_namespace_blk(dev)) {
445 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
447 ns_altname = nsblk->alt_name;
451 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
453 static DEVICE_ATTR_RW(alt_name);
455 static int scan_free(struct nd_region *nd_region,
456 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
459 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
460 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
464 struct resource *res, *last;
465 resource_size_t new_start;
468 for_each_dpa_resource(ndd, res)
469 if (strcmp(res->name, label_id->id) == 0)
475 if (n >= resource_size(res)) {
476 n -= resource_size(res);
477 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
478 nvdimm_free_dpa(ndd, res);
479 /* retry with last resource deleted */
484 * Keep BLK allocations relegated to high DPA as much as
488 new_start = res->start + n;
490 new_start = res->start;
492 rc = adjust_resource(res, new_start, resource_size(res) - n);
494 res->flags |= DPA_RESOURCE_ADJUSTED;
495 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
503 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
504 * @nd_region: the set of dimms to reclaim @n bytes from
505 * @label_id: unique identifier for the namespace consuming this dpa range
506 * @n: number of bytes per-dimm to release
508 * Assumes resources are ordered. Starting from the end try to
509 * adjust_resource() the allocation to @n, but if @n is larger than the
510 * allocation delete it and find the 'new' last allocation in the label
513 static int shrink_dpa_allocation(struct nd_region *nd_region,
514 struct nd_label_id *label_id, resource_size_t n)
518 for (i = 0; i < nd_region->ndr_mappings; i++) {
519 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
522 rc = scan_free(nd_region, nd_mapping, label_id, n);
530 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
531 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
534 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
535 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
536 resource_size_t first_dpa;
537 struct resource *res;
540 /* allocate blk from highest dpa first */
542 first_dpa = nd_mapping->start + nd_mapping->size - n;
544 first_dpa = nd_mapping->start;
546 /* first resource allocation for this label-id or dimm */
547 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
551 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
557 * space_valid() - validate free dpa space against constraints
558 * @nd_region: hosting region of the free space
559 * @ndd: dimm device data for debug
560 * @label_id: namespace id to allocate space
561 * @prev: potential allocation that precedes free space
562 * @next: allocation that follows the given free space range
563 * @exist: first allocation with same id in the mapping
564 * @n: range that must satisfied for pmem allocations
565 * @valid: free space range to validate
567 * BLK-space is valid as long as it does not precede a PMEM
568 * allocation in a given region. PMEM-space must be contiguous
569 * and adjacent to an existing existing allocation (if one
570 * exists). If reserving PMEM any space is valid.
572 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
573 struct nd_label_id *label_id, struct resource *prev,
574 struct resource *next, struct resource *exist,
575 resource_size_t n, struct resource *valid)
577 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
578 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
580 if (valid->start >= valid->end)
587 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
588 struct nvdimm_bus *nvdimm_bus;
589 struct blk_alloc_info info = {
590 .nd_mapping = nd_mapping,
591 .available = nd_mapping->size,
595 WARN_ON(!is_nd_blk(&nd_region->dev));
596 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
597 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
601 /* allocation needs to be contiguous, so this is all or nothing */
602 if (resource_size(valid) < n)
605 /* we've got all the space we need and no existing allocation */
609 /* allocation needs to be contiguous with the existing namespace */
610 if (valid->start == exist->end + 1
611 || valid->end == exist->start - 1)
615 /* truncate @valid size to 0 */
616 valid->end = valid->start - 1;
620 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
623 static resource_size_t scan_allocate(struct nd_region *nd_region,
624 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
627 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
628 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
629 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
630 struct resource *res, *exist = NULL, valid;
631 const resource_size_t to_allocate = n;
634 for_each_dpa_resource(ndd, res)
635 if (strcmp(label_id->id, res->name) == 0)
638 valid.start = nd_mapping->start;
639 valid.end = mapping_end;
640 valid.name = "free space";
643 for_each_dpa_resource(ndd, res) {
644 struct resource *next = res->sibling, *new_res = NULL;
645 resource_size_t allocate, available = 0;
646 enum alloc_loc loc = ALLOC_ERR;
650 /* ignore resources outside this nd_mapping */
651 if (res->start > mapping_end)
653 if (res->end < nd_mapping->start)
656 /* space at the beginning of the mapping */
657 if (!first++ && res->start > nd_mapping->start) {
658 valid.start = nd_mapping->start;
659 valid.end = res->start - 1;
660 space_valid(nd_region, ndd, label_id, NULL, next, exist,
661 to_allocate, &valid);
662 available = resource_size(&valid);
667 /* space between allocations */
669 valid.start = res->start + resource_size(res);
670 valid.end = min(mapping_end, next->start - 1);
671 space_valid(nd_region, ndd, label_id, res, next, exist,
672 to_allocate, &valid);
673 available = resource_size(&valid);
678 /* space at the end of the mapping */
680 valid.start = res->start + resource_size(res);
681 valid.end = mapping_end;
682 space_valid(nd_region, ndd, label_id, res, next, exist,
683 to_allocate, &valid);
684 available = resource_size(&valid);
689 if (!loc || !available)
691 allocate = min(available, n);
694 if (strcmp(res->name, label_id->id) == 0) {
695 /* adjust current resource up */
696 rc = adjust_resource(res, res->start - allocate,
697 resource_size(res) + allocate);
698 action = "cur grow up";
703 if (strcmp(next->name, label_id->id) == 0) {
704 /* adjust next resource up */
705 rc = adjust_resource(next, next->start
706 - allocate, resource_size(next)
709 action = "next grow up";
710 } else if (strcmp(res->name, label_id->id) == 0) {
711 action = "grow down";
716 if (strcmp(res->name, label_id->id) == 0)
717 action = "grow down";
725 if (strcmp(action, "allocate") == 0) {
726 /* BLK allocate bottom up */
728 valid.start += available - allocate;
730 new_res = nvdimm_allocate_dpa(ndd, label_id,
731 valid.start, allocate);
734 } else if (strcmp(action, "grow down") == 0) {
735 /* adjust current resource down */
736 rc = adjust_resource(res, res->start, resource_size(res)
739 res->flags |= DPA_RESOURCE_ADJUSTED;
745 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
754 * Retry scan with newly inserted resources.
755 * For example, if we did an ALLOC_BEFORE
756 * insertion there may also have been space
757 * available for an ALLOC_AFTER insertion, so we
758 * need to check this same resource again
766 * If we allocated nothing in the BLK case it may be because we are in
767 * an initial "pmem-reserve pass". Only do an initial BLK allocation
768 * when none of the DPA space is reserved.
770 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
771 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
775 static int merge_dpa(struct nd_region *nd_region,
776 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
778 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
779 struct resource *res;
781 if (strncmp("pmem", label_id->id, 4) == 0)
784 for_each_dpa_resource(ndd, res) {
786 struct resource *next = res->sibling;
787 resource_size_t end = res->start + resource_size(res);
789 if (!next || strcmp(res->name, label_id->id) != 0
790 || strcmp(next->name, label_id->id) != 0
791 || end != next->start)
793 end += resource_size(next);
794 nvdimm_free_dpa(ndd, next);
795 rc = adjust_resource(res, res->start, end - res->start);
796 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
799 res->flags |= DPA_RESOURCE_ADJUSTED;
806 static int __reserve_free_pmem(struct device *dev, void *data)
808 struct nvdimm *nvdimm = data;
809 struct nd_region *nd_region;
810 struct nd_label_id label_id;
816 nd_region = to_nd_region(dev);
817 if (nd_region->ndr_mappings == 0)
820 memset(&label_id, 0, sizeof(label_id));
821 strcat(label_id.id, "pmem-reserve");
822 for (i = 0; i < nd_region->ndr_mappings; i++) {
823 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
824 resource_size_t n, rem = 0;
826 if (nd_mapping->nvdimm != nvdimm)
829 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
832 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
833 dev_WARN_ONCE(&nd_region->dev, rem,
834 "pmem reserve underrun: %#llx of %#llx bytes\n",
835 (unsigned long long) n - rem,
836 (unsigned long long) n);
837 return rem ? -ENXIO : 0;
843 static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
844 struct nd_mapping *nd_mapping)
846 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
847 struct resource *res, *_res;
849 for_each_dpa_resource_safe(ndd, res, _res)
850 if (strcmp(res->name, "pmem-reserve") == 0)
851 nvdimm_free_dpa(ndd, res);
854 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
855 struct nd_mapping *nd_mapping)
857 struct nvdimm *nvdimm = nd_mapping->nvdimm;
860 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
861 __reserve_free_pmem);
863 release_free_pmem(nvdimm_bus, nd_mapping);
868 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
869 * @nd_region: the set of dimms to allocate @n more bytes from
870 * @label_id: unique identifier for the namespace consuming this dpa range
871 * @n: number of bytes per-dimm to add to the existing allocation
873 * Assumes resources are ordered. For BLK regions, first consume
874 * BLK-only available DPA free space, then consume PMEM-aliased DPA
875 * space starting at the highest DPA. For PMEM regions start
876 * allocations from the start of an interleave set and end at the first
877 * BLK allocation or the end of the interleave set, whichever comes
880 static int grow_dpa_allocation(struct nd_region *nd_region,
881 struct nd_label_id *label_id, resource_size_t n)
883 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
884 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
887 for (i = 0; i < nd_region->ndr_mappings; i++) {
888 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
889 resource_size_t rem = n;
893 * In the BLK case try once with all unallocated PMEM
894 * reserved, and once without
896 for (j = is_pmem; j < 2; j++) {
897 bool blk_only = j == 0;
900 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
904 rem = scan_allocate(nd_region, nd_mapping,
907 release_free_pmem(nvdimm_bus, nd_mapping);
909 /* try again and allow encroachments into PMEM */
914 dev_WARN_ONCE(&nd_region->dev, rem,
915 "allocation underrun: %#llx of %#llx bytes\n",
916 (unsigned long long) n - rem,
917 (unsigned long long) n);
921 rc = merge_dpa(nd_region, nd_mapping, label_id);
929 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
930 struct nd_namespace_pmem *nspm, resource_size_t size)
932 struct resource *res = &nspm->nsio.res;
933 resource_size_t offset = 0;
935 if (size && !nspm->uuid) {
940 if (size && nspm->uuid) {
941 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
942 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
943 struct nd_label_id label_id;
944 struct resource *res;
951 nd_label_gen_id(&label_id, nspm->uuid, 0);
953 /* calculate a spa offset from the dpa allocation offset */
954 for_each_dpa_resource(ndd, res)
955 if (strcmp(res->name, label_id.id) == 0) {
956 offset = (res->start - nd_mapping->start)
957 * nd_region->ndr_mappings;
966 res->start = nd_region->ndr_start + offset;
967 res->end = res->start + size - 1;
970 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
973 dev_dbg(dev, "%s: uuid not set\n", where);
979 static ssize_t __size_store(struct device *dev, unsigned long long val)
981 resource_size_t allocated = 0, available = 0;
982 struct nd_region *nd_region = to_nd_region(dev->parent);
983 struct nd_namespace_common *ndns = to_ndns(dev);
984 struct nd_mapping *nd_mapping;
985 struct nvdimm_drvdata *ndd;
986 struct nd_label_id label_id;
987 u32 flags = 0, remainder;
991 if (dev->driver || ndns->claim)
994 if (is_namespace_pmem(dev)) {
995 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
999 } else if (is_namespace_blk(dev)) {
1000 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1003 flags = NSLABEL_FLAG_LOCAL;
1008 * We need a uuid for the allocation-label and dimm(s) on which
1009 * to store the label.
1011 if (uuid_not_set(uuid, dev, __func__))
1013 if (nd_region->ndr_mappings == 0) {
1014 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
1018 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
1020 dev_dbg(dev, "%llu is not %dK aligned\n", val,
1021 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
1025 nd_label_gen_id(&label_id, uuid, flags);
1026 for (i = 0; i < nd_region->ndr_mappings; i++) {
1027 nd_mapping = &nd_region->mapping[i];
1028 ndd = to_ndd(nd_mapping);
1031 * All dimms in an interleave set, or the base dimm for a blk
1032 * region, need to be enabled for the size to be changed.
1037 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1039 available = nd_region_available_dpa(nd_region);
1041 if (val > available + allocated)
1044 if (val == allocated)
1047 val = div_u64(val, nd_region->ndr_mappings);
1048 allocated = div_u64(allocated, nd_region->ndr_mappings);
1049 if (val < allocated)
1050 rc = shrink_dpa_allocation(nd_region, &label_id,
1053 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1058 if (is_namespace_pmem(dev)) {
1059 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1061 nd_namespace_pmem_set_resource(nd_region, nspm,
1062 val * nd_region->ndr_mappings);
1066 * Try to delete the namespace if we deleted all of its
1067 * allocation, this is not the seed or 0th device for the
1068 * region, and it is not actively claimed by a btt, pfn, or dax
1071 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
1072 nd_device_unregister(dev, ND_ASYNC);
1077 static ssize_t size_store(struct device *dev,
1078 struct device_attribute *attr, const char *buf, size_t len)
1080 struct nd_region *nd_region = to_nd_region(dev->parent);
1081 unsigned long long val;
1085 rc = kstrtoull(buf, 0, &val);
1090 nvdimm_bus_lock(dev);
1091 wait_nvdimm_bus_probe_idle(dev);
1092 rc = __size_store(dev, val);
1094 rc = nd_namespace_label_update(nd_region, dev);
1096 if (is_namespace_pmem(dev)) {
1097 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1100 } else if (is_namespace_blk(dev)) {
1101 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1103 uuid = &nsblk->uuid;
1106 if (rc == 0 && val == 0 && uuid) {
1107 /* setting size zero == 'delete namespace' */
1112 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
1113 ? "fail" : "success", rc);
1115 nvdimm_bus_unlock(dev);
1118 return rc < 0 ? rc : len;
1121 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1123 struct device *dev = &ndns->dev;
1125 if (is_namespace_pmem(dev)) {
1126 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1128 return resource_size(&nspm->nsio.res);
1129 } else if (is_namespace_blk(dev)) {
1130 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1131 } else if (is_namespace_io(dev)) {
1132 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1134 return resource_size(&nsio->res);
1136 WARN_ONCE(1, "unknown namespace type\n");
1140 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1142 resource_size_t size;
1144 nvdimm_bus_lock(&ndns->dev);
1145 size = __nvdimm_namespace_capacity(ndns);
1146 nvdimm_bus_unlock(&ndns->dev);
1150 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1152 static ssize_t size_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
1155 return sprintf(buf, "%llu\n", (unsigned long long)
1156 nvdimm_namespace_capacity(to_ndns(dev)));
1158 static DEVICE_ATTR(size, 0444, size_show, size_store);
1160 static u8 *namespace_to_uuid(struct device *dev)
1162 if (is_namespace_pmem(dev)) {
1163 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1166 } else if (is_namespace_blk(dev)) {
1167 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1171 return ERR_PTR(-ENXIO);
1174 static ssize_t uuid_show(struct device *dev,
1175 struct device_attribute *attr, char *buf)
1177 u8 *uuid = namespace_to_uuid(dev);
1180 return PTR_ERR(uuid);
1182 return sprintf(buf, "%pUb\n", uuid);
1183 return sprintf(buf, "\n");
1187 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1188 * @nd_region: parent region so we can updates all dimms in the set
1189 * @dev: namespace type for generating label_id
1190 * @new_uuid: incoming uuid
1191 * @old_uuid: reference to the uuid storage location in the namespace object
1193 static int namespace_update_uuid(struct nd_region *nd_region,
1194 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1196 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1197 struct nd_label_id old_label_id;
1198 struct nd_label_id new_label_id;
1201 if (!nd_is_uuid_unique(dev, new_uuid))
1204 if (*old_uuid == NULL)
1208 * If we've already written a label with this uuid, then it's
1209 * too late to rename because we can't reliably update the uuid
1210 * without losing the old namespace. Userspace must delete this
1211 * namespace to abandon the old uuid.
1213 for (i = 0; i < nd_region->ndr_mappings; i++) {
1214 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1217 * This check by itself is sufficient because old_uuid
1218 * would be NULL above if this uuid did not exist in the
1219 * currently written set.
1221 * FIXME: can we delete uuid with zero dpa allocated?
1223 if (list_empty(&nd_mapping->labels))
1227 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1228 nd_label_gen_id(&new_label_id, new_uuid, flags);
1229 for (i = 0; i < nd_region->ndr_mappings; i++) {
1230 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1231 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1232 struct nd_label_ent *label_ent;
1233 struct resource *res;
1235 for_each_dpa_resource(ndd, res)
1236 if (strcmp(res->name, old_label_id.id) == 0)
1237 sprintf((void *) res->name, "%s",
1240 mutex_lock(&nd_mapping->lock);
1241 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1242 struct nd_namespace_label *nd_label = label_ent->label;
1243 struct nd_label_id label_id;
1247 nd_label_gen_id(&label_id, nd_label->uuid,
1248 __le32_to_cpu(nd_label->flags));
1249 if (strcmp(old_label_id.id, label_id.id) == 0)
1250 set_bit(ND_LABEL_REAP, &label_ent->flags);
1252 mutex_unlock(&nd_mapping->lock);
1256 *old_uuid = new_uuid;
1260 static ssize_t uuid_store(struct device *dev,
1261 struct device_attribute *attr, const char *buf, size_t len)
1263 struct nd_region *nd_region = to_nd_region(dev->parent);
1268 if (is_namespace_pmem(dev)) {
1269 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1271 ns_uuid = &nspm->uuid;
1272 } else if (is_namespace_blk(dev)) {
1273 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1275 ns_uuid = &nsblk->uuid;
1280 nvdimm_bus_lock(dev);
1281 wait_nvdimm_bus_probe_idle(dev);
1282 if (to_ndns(dev)->claim)
1285 rc = nd_uuid_store(dev, &uuid, buf, len);
1287 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1289 rc = nd_namespace_label_update(nd_region, dev);
1292 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1293 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1294 nvdimm_bus_unlock(dev);
1297 return rc < 0 ? rc : len;
1299 static DEVICE_ATTR_RW(uuid);
1301 static ssize_t resource_show(struct device *dev,
1302 struct device_attribute *attr, char *buf)
1304 struct resource *res;
1306 if (is_namespace_pmem(dev)) {
1307 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1309 res = &nspm->nsio.res;
1310 } else if (is_namespace_io(dev)) {
1311 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1317 /* no address to convey if the namespace has no allocation */
1318 if (resource_size(res) == 0)
1320 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1322 static DEVICE_ATTR_RO(resource);
1324 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
1325 4096, 4104, 4160, 4224, 0 };
1327 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1329 static ssize_t sector_size_show(struct device *dev,
1330 struct device_attribute *attr, char *buf)
1332 if (is_namespace_blk(dev)) {
1333 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1335 return nd_size_select_show(nsblk->lbasize,
1336 blk_lbasize_supported, buf);
1339 if (is_namespace_pmem(dev)) {
1340 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1342 return nd_size_select_show(nspm->lbasize,
1343 pmem_lbasize_supported, buf);
1348 static ssize_t sector_size_store(struct device *dev,
1349 struct device_attribute *attr, const char *buf, size_t len)
1351 struct nd_region *nd_region = to_nd_region(dev->parent);
1352 const unsigned long *supported;
1353 unsigned long *lbasize;
1356 if (is_namespace_blk(dev)) {
1357 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1359 lbasize = &nsblk->lbasize;
1360 supported = blk_lbasize_supported;
1361 } else if (is_namespace_pmem(dev)) {
1362 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1364 lbasize = &nspm->lbasize;
1365 supported = pmem_lbasize_supported;
1370 nvdimm_bus_lock(dev);
1371 if (to_ndns(dev)->claim)
1374 rc = nd_size_select_store(dev, buf, lbasize, supported);
1376 rc = nd_namespace_label_update(nd_region, dev);
1377 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1378 rc, rc < 0 ? "tried" : "wrote", buf,
1379 buf[len - 1] == '\n' ? "" : "\n");
1380 nvdimm_bus_unlock(dev);
1383 return rc ? rc : len;
1385 static DEVICE_ATTR_RW(sector_size);
1387 static ssize_t dpa_extents_show(struct device *dev,
1388 struct device_attribute *attr, char *buf)
1390 struct nd_region *nd_region = to_nd_region(dev->parent);
1391 struct nd_label_id label_id;
1396 nvdimm_bus_lock(dev);
1397 if (is_namespace_pmem(dev)) {
1398 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1402 } else if (is_namespace_blk(dev)) {
1403 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1406 flags = NSLABEL_FLAG_LOCAL;
1412 nd_label_gen_id(&label_id, uuid, flags);
1413 for (i = 0; i < nd_region->ndr_mappings; i++) {
1414 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1415 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1416 struct resource *res;
1418 for_each_dpa_resource(ndd, res)
1419 if (strcmp(res->name, label_id.id) == 0)
1423 nvdimm_bus_unlock(dev);
1425 return sprintf(buf, "%d\n", count);
1427 static DEVICE_ATTR_RO(dpa_extents);
1429 static int btt_claim_class(struct device *dev)
1431 struct nd_region *nd_region = to_nd_region(dev->parent);
1432 int i, loop_bitmask = 0;
1434 for (i = 0; i < nd_region->ndr_mappings; i++) {
1435 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1436 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1437 struct nd_namespace_index *nsindex;
1440 * If any of the DIMMs do not support labels the only
1441 * possible BTT format is v1.
1448 nsindex = to_namespace_index(ndd, ndd->ns_current);
1449 if (nsindex == NULL)
1452 /* check whether existing labels are v1.1 or v1.2 */
1453 if (__le16_to_cpu(nsindex->major) == 1
1454 && __le16_to_cpu(nsindex->minor) == 1)
1461 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1462 * block is found, a v1.1 label for any mapping will set bit 1, and a
1463 * v1.2 label will set bit 2.
1465 * At the end of the loop, at most one of the three bits must be set.
1466 * If multiple bits were set, it means the different mappings disagree
1467 * about their labels, and this must be cleaned up first.
1469 * If all the label index blocks are found to agree, nsindex of NULL
1470 * implies labels haven't been initialized yet, and when they will,
1471 * they will be of the 1.2 format, so we can assume BTT2.0
1473 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1474 * found, we enforce BTT2.0
1476 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1478 switch (loop_bitmask) {
1481 return NVDIMM_CCLASS_BTT;
1484 return NVDIMM_CCLASS_BTT2;
1490 static ssize_t holder_show(struct device *dev,
1491 struct device_attribute *attr, char *buf)
1493 struct nd_namespace_common *ndns = to_ndns(dev);
1497 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1502 static DEVICE_ATTR_RO(holder);
1504 static ssize_t __holder_class_store(struct device *dev, const char *buf)
1506 struct nd_namespace_common *ndns = to_ndns(dev);
1508 if (dev->driver || ndns->claim)
1511 if (strcmp(buf, "btt") == 0 || strcmp(buf, "btt\n") == 0)
1512 ndns->claim_class = btt_claim_class(dev);
1513 else if (strcmp(buf, "pfn") == 0 || strcmp(buf, "pfn\n") == 0)
1514 ndns->claim_class = NVDIMM_CCLASS_PFN;
1515 else if (strcmp(buf, "dax") == 0 || strcmp(buf, "dax\n") == 0)
1516 ndns->claim_class = NVDIMM_CCLASS_DAX;
1517 else if (strcmp(buf, "") == 0 || strcmp(buf, "\n") == 0)
1518 ndns->claim_class = NVDIMM_CCLASS_NONE;
1522 /* btt_claim_class() could've returned an error */
1523 if (ndns->claim_class < 0)
1524 return ndns->claim_class;
1529 static ssize_t holder_class_store(struct device *dev,
1530 struct device_attribute *attr, const char *buf, size_t len)
1532 struct nd_region *nd_region = to_nd_region(dev->parent);
1536 nvdimm_bus_lock(dev);
1537 wait_nvdimm_bus_probe_idle(dev);
1538 rc = __holder_class_store(dev, buf);
1540 rc = nd_namespace_label_update(nd_region, dev);
1541 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
1542 nvdimm_bus_unlock(dev);
1545 return rc < 0 ? rc : len;
1548 static ssize_t holder_class_show(struct device *dev,
1549 struct device_attribute *attr, char *buf)
1551 struct nd_namespace_common *ndns = to_ndns(dev);
1555 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1556 rc = sprintf(buf, "\n");
1557 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1558 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1559 rc = sprintf(buf, "btt\n");
1560 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1561 rc = sprintf(buf, "pfn\n");
1562 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1563 rc = sprintf(buf, "dax\n");
1565 rc = sprintf(buf, "<unknown>\n");
1570 static DEVICE_ATTR_RW(holder_class);
1572 static ssize_t mode_show(struct device *dev,
1573 struct device_attribute *attr, char *buf)
1575 struct nd_namespace_common *ndns = to_ndns(dev);
1576 struct device *claim;
1581 claim = ndns->claim;
1582 if (claim && is_nd_btt(claim))
1584 else if (claim && is_nd_pfn(claim))
1586 else if (claim && is_nd_dax(claim))
1588 else if (!claim && pmem_should_map_pages(dev))
1592 rc = sprintf(buf, "%s\n", mode);
1597 static DEVICE_ATTR_RO(mode);
1599 static ssize_t force_raw_store(struct device *dev,
1600 struct device_attribute *attr, const char *buf, size_t len)
1603 int rc = strtobool(buf, &force_raw);
1608 to_ndns(dev)->force_raw = force_raw;
1612 static ssize_t force_raw_show(struct device *dev,
1613 struct device_attribute *attr, char *buf)
1615 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1617 static DEVICE_ATTR_RW(force_raw);
1619 static struct attribute *nd_namespace_attributes[] = {
1620 &dev_attr_nstype.attr,
1621 &dev_attr_size.attr,
1622 &dev_attr_mode.attr,
1623 &dev_attr_uuid.attr,
1624 &dev_attr_holder.attr,
1625 &dev_attr_resource.attr,
1626 &dev_attr_alt_name.attr,
1627 &dev_attr_force_raw.attr,
1628 &dev_attr_sector_size.attr,
1629 &dev_attr_dpa_extents.attr,
1630 &dev_attr_holder_class.attr,
1634 static umode_t namespace_visible(struct kobject *kobj,
1635 struct attribute *a, int n)
1637 struct device *dev = container_of(kobj, struct device, kobj);
1639 if (a == &dev_attr_resource.attr) {
1640 if (is_namespace_blk(dev))
1645 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1646 if (a == &dev_attr_size.attr)
1652 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1653 || a == &dev_attr_holder.attr
1654 || a == &dev_attr_holder_class.attr
1655 || a == &dev_attr_force_raw.attr
1656 || a == &dev_attr_mode.attr)
1662 static struct attribute_group nd_namespace_attribute_group = {
1663 .attrs = nd_namespace_attributes,
1664 .is_visible = namespace_visible,
1667 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1668 &nd_device_attribute_group,
1669 &nd_namespace_attribute_group,
1670 &nd_numa_attribute_group,
1674 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1676 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1677 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1678 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1679 struct nd_namespace_common *ndns = NULL;
1680 resource_size_t size;
1682 if (nd_btt || nd_pfn || nd_dax) {
1684 ndns = nd_btt->ndns;
1686 ndns = nd_pfn->ndns;
1688 ndns = nd_dax->nd_pfn.ndns;
1691 return ERR_PTR(-ENODEV);
1694 * Flush any in-progess probes / removals in the driver
1695 * for the raw personality of this namespace.
1697 device_lock(&ndns->dev);
1698 device_unlock(&ndns->dev);
1699 if (ndns->dev.driver) {
1700 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1702 return ERR_PTR(-EBUSY);
1704 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1705 "host (%s) vs claim (%s) mismatch\n",
1707 dev_name(ndns->claim)))
1708 return ERR_PTR(-ENXIO);
1710 ndns = to_ndns(dev);
1712 dev_dbg(dev, "claimed by %s, failing probe\n",
1713 dev_name(ndns->claim));
1715 return ERR_PTR(-ENXIO);
1719 size = nvdimm_namespace_capacity(ndns);
1720 if (size < ND_MIN_NAMESPACE_SIZE) {
1721 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1722 &size, ND_MIN_NAMESPACE_SIZE);
1723 return ERR_PTR(-ENODEV);
1726 if (is_namespace_pmem(&ndns->dev)) {
1727 struct nd_namespace_pmem *nspm;
1729 nspm = to_nd_namespace_pmem(&ndns->dev);
1730 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1731 return ERR_PTR(-ENODEV);
1732 } else if (is_namespace_blk(&ndns->dev)) {
1733 struct nd_namespace_blk *nsblk;
1735 nsblk = to_nd_namespace_blk(&ndns->dev);
1736 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1737 return ERR_PTR(-ENODEV);
1738 if (!nsblk->lbasize) {
1739 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1741 return ERR_PTR(-ENODEV);
1743 if (!nd_namespace_blk_validate(nsblk))
1744 return ERR_PTR(-ENODEV);
1749 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1751 static struct device **create_namespace_io(struct nd_region *nd_region)
1753 struct nd_namespace_io *nsio;
1754 struct device *dev, **devs;
1755 struct resource *res;
1757 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1761 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1767 dev = &nsio->common.dev;
1768 dev->type = &namespace_io_device_type;
1769 dev->parent = &nd_region->dev;
1771 res->name = dev_name(&nd_region->dev);
1772 res->flags = IORESOURCE_MEM;
1773 res->start = nd_region->ndr_start;
1774 res->end = res->start + nd_region->ndr_size - 1;
1780 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1781 u64 cookie, u16 pos)
1783 struct nd_namespace_label *found = NULL;
1786 for (i = 0; i < nd_region->ndr_mappings; i++) {
1787 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1788 struct nd_interleave_set *nd_set = nd_region->nd_set;
1789 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1790 struct nd_label_ent *label_ent;
1791 bool found_uuid = false;
1793 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1794 struct nd_namespace_label *nd_label = label_ent->label;
1795 u16 position, nlabel;
1800 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1801 position = __le16_to_cpu(nd_label->position);
1802 nlabel = __le16_to_cpu(nd_label->nlabel);
1804 if (isetcookie != cookie)
1807 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1810 if (namespace_label_has(ndd, type_guid)
1811 && !guid_equal(&nd_set->type_guid,
1812 &nd_label->type_guid)) {
1813 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1814 nd_set->type_guid.b,
1815 nd_label->type_guid.b);
1821 "%s duplicate entry for uuid\n",
1826 if (nlabel != nd_region->ndr_mappings)
1828 if (position != pos)
1836 return found != NULL;
1839 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1846 for (i = 0; i < nd_region->ndr_mappings; i++) {
1847 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1848 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1849 struct nd_namespace_label *nd_label = NULL;
1850 u64 hw_start, hw_end, pmem_start, pmem_end;
1851 struct nd_label_ent *label_ent;
1853 lockdep_assert_held(&nd_mapping->lock);
1854 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1855 nd_label = label_ent->label;
1858 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1869 * Check that this label is compliant with the dpa
1870 * range published in NFIT
1872 hw_start = nd_mapping->start;
1873 hw_end = hw_start + nd_mapping->size;
1874 pmem_start = __le64_to_cpu(nd_label->dpa);
1875 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1876 if (pmem_start >= hw_start && pmem_start < hw_end
1877 && pmem_end <= hw_end && pmem_end > hw_start)
1880 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1881 dev_name(ndd->dev), nd_label->uuid);
1885 /* move recently validated label to the front of the list */
1886 list_move(&label_ent->list, &nd_mapping->labels);
1892 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1893 * @nd_region: region with mappings to validate
1894 * @nspm: target namespace to create
1895 * @nd_label: target pmem namespace label to evaluate
1897 struct device *create_namespace_pmem(struct nd_region *nd_region,
1898 struct nd_namespace_index *nsindex,
1899 struct nd_namespace_label *nd_label)
1901 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1902 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1903 struct nd_label_ent *label_ent;
1904 struct nd_namespace_pmem *nspm;
1905 struct nd_mapping *nd_mapping;
1906 resource_size_t size = 0;
1907 struct resource *res;
1913 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1914 return ERR_PTR(-ENXIO);
1917 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1918 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1920 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1921 return ERR_PTR(-EAGAIN);
1923 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1927 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1929 return ERR_PTR(-ENOMEM);
1932 dev = &nspm->nsio.common.dev;
1933 dev->type = &namespace_pmem_device_type;
1934 dev->parent = &nd_region->dev;
1935 res = &nspm->nsio.res;
1936 res->name = dev_name(&nd_region->dev);
1937 res->flags = IORESOURCE_MEM;
1939 for (i = 0; i < nd_region->ndr_mappings; i++) {
1940 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1942 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1947 if (i < nd_region->ndr_mappings) {
1948 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1951 * Give up if we don't find an instance of a uuid at each
1952 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1953 * find a dimm with two instances of the same uuid.
1955 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1956 nvdimm_name(nvdimm), nd_label->uuid);
1962 * Fix up each mapping's 'labels' to have the validated pmem label for
1963 * that position at labels[0], and NULL at labels[1]. In the process,
1964 * check that the namespace aligns with interleave-set. We know
1965 * that it does not overlap with any blk namespaces by virtue of
1966 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1969 rc = select_pmem_id(nd_region, nd_label->uuid);
1973 /* Calculate total size and populate namespace properties from label0 */
1974 for (i = 0; i < nd_region->ndr_mappings; i++) {
1975 struct nd_namespace_label *label0;
1976 struct nvdimm_drvdata *ndd;
1978 nd_mapping = &nd_region->mapping[i];
1979 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1980 typeof(*label_ent), list);
1981 label0 = label_ent ? label_ent->label : NULL;
1988 size += __le64_to_cpu(label0->rawsize);
1989 if (__le16_to_cpu(label0->position) != 0)
1991 WARN_ON(nspm->alt_name || nspm->uuid);
1992 nspm->alt_name = kmemdup((void __force *) label0->name,
1993 NSLABEL_NAME_LEN, GFP_KERNEL);
1994 nspm->uuid = kmemdup((void __force *) label0->uuid,
1995 NSLABEL_UUID_LEN, GFP_KERNEL);
1996 nspm->lbasize = __le64_to_cpu(label0->lbasize);
1997 ndd = to_ndd(nd_mapping);
1998 if (namespace_label_has(ndd, abstraction_guid))
1999 nspm->nsio.common.claim_class
2000 = to_nvdimm_cclass(&label0->abstraction_guid);
2004 if (!nspm->alt_name || !nspm->uuid) {
2009 nd_namespace_pmem_set_resource(nd_region, nspm, size);
2013 namespace_pmem_release(dev);
2016 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
2019 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
2022 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
2029 struct resource *nsblk_add_resource(struct nd_region *nd_region,
2030 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2031 resource_size_t start)
2033 struct nd_label_id label_id;
2034 struct resource *res;
2036 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2037 res = krealloc(nsblk->res,
2038 sizeof(void *) * (nsblk->num_resources + 1),
2042 nsblk->res = (struct resource **) res;
2043 for_each_dpa_resource(ndd, res)
2044 if (strcmp(res->name, label_id.id) == 0
2045 && res->start == start) {
2046 nsblk->res[nsblk->num_resources++] = res;
2052 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2054 struct nd_namespace_blk *nsblk;
2057 if (!is_nd_blk(&nd_region->dev))
2060 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2064 dev = &nsblk->common.dev;
2065 dev->type = &namespace_blk_device_type;
2066 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2067 if (nsblk->id < 0) {
2071 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2072 dev->parent = &nd_region->dev;
2073 dev->groups = nd_namespace_attribute_groups;
2075 return &nsblk->common.dev;
2078 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2080 struct nd_namespace_pmem *nspm;
2081 struct resource *res;
2084 if (!is_memory(&nd_region->dev))
2087 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2091 dev = &nspm->nsio.common.dev;
2092 dev->type = &namespace_pmem_device_type;
2093 dev->parent = &nd_region->dev;
2094 res = &nspm->nsio.res;
2095 res->name = dev_name(&nd_region->dev);
2096 res->flags = IORESOURCE_MEM;
2098 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2103 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
2104 dev->parent = &nd_region->dev;
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 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 (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
2319 __le64_to_cpu(nd_label->dpa) > map_end)
2322 i = add_namespace_resource(nd_region, nd_label, devs, count);
2327 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2330 memcpy(__devs, devs, sizeof(dev) * count);
2334 if (is_nd_blk(&nd_region->dev))
2335 dev = create_namespace_blk(nd_region, nd_label, count);
2337 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2338 struct nd_namespace_index *nsindex;
2340 nsindex = to_namespace_index(ndd, ndd->ns_current);
2341 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
2345 switch (PTR_ERR(dev)) {
2347 /* skip invalid labels */
2350 /* fallthrough to seed creation */
2356 devs[count++] = dev;
2360 dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
2361 __func__, count, is_nd_blk(&nd_region->dev)
2362 ? "blk" : "pmem", count == 1 ? "" : "s");
2365 /* Publish a zero-sized namespace for userspace to configure. */
2366 nd_mapping_free_labels(nd_mapping);
2368 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2371 if (is_nd_blk(&nd_region->dev)) {
2372 struct nd_namespace_blk *nsblk;
2374 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2377 dev = &nsblk->common.dev;
2378 dev->type = &namespace_blk_device_type;
2380 struct nd_namespace_pmem *nspm;
2382 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2385 dev = &nspm->nsio.common.dev;
2386 dev->type = &namespace_pmem_device_type;
2387 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2389 dev->parent = &nd_region->dev;
2390 devs[count++] = dev;
2391 } else if (is_memory(&nd_region->dev)) {
2392 /* clean unselected labels */
2393 for (i = 0; i < nd_region->ndr_mappings; i++) {
2394 struct list_head *l, *e;
2398 nd_mapping = &nd_region->mapping[i];
2399 if (list_empty(&nd_mapping->labels)) {
2405 list_for_each_safe(l, e, &nd_mapping->labels) {
2408 list_move_tail(l, &list);
2410 nd_mapping_free_labels(nd_mapping);
2411 list_splice_init(&list, &nd_mapping->labels);
2416 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2422 for (i = 0; devs[i]; i++)
2423 if (is_nd_blk(&nd_region->dev))
2424 namespace_blk_release(devs[i]);
2426 namespace_pmem_release(devs[i]);
2432 static struct device **create_namespaces(struct nd_region *nd_region)
2434 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2435 struct device **devs;
2438 if (nd_region->ndr_mappings == 0)
2441 /* lock down all mappings while we scan labels */
2442 for (i = 0; i < nd_region->ndr_mappings; i++) {
2443 nd_mapping = &nd_region->mapping[i];
2444 mutex_lock_nested(&nd_mapping->lock, i);
2447 devs = scan_labels(nd_region);
2449 for (i = 0; i < nd_region->ndr_mappings; i++) {
2450 int reverse = nd_region->ndr_mappings - 1 - i;
2452 nd_mapping = &nd_region->mapping[reverse];
2453 mutex_unlock(&nd_mapping->lock);
2459 static int init_active_labels(struct nd_region *nd_region)
2463 for (i = 0; i < nd_region->ndr_mappings; i++) {
2464 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2465 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2466 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2467 struct nd_label_ent *label_ent;
2471 * If the dimm is disabled then we may need to prevent
2472 * the region from being activated.
2475 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2476 /* fail, label data may be unreadable */;
2477 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2478 /* fail, labels needed to disambiguate dpa */;
2482 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2483 dev_name(&nd_mapping->nvdimm->dev),
2484 test_bit(NDD_LOCKED, &nvdimm->flags)
2485 ? "locked" : "disabled");
2488 nd_mapping->ndd = ndd;
2489 atomic_inc(&nvdimm->busy);
2492 count = nd_label_active_count(ndd);
2493 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
2496 for (j = 0; j < count; j++) {
2497 struct nd_namespace_label *label;
2499 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2502 label = nd_label_active(ndd, j);
2503 label_ent->label = label;
2505 mutex_lock(&nd_mapping->lock);
2506 list_add_tail(&label_ent->list, &nd_mapping->labels);
2507 mutex_unlock(&nd_mapping->lock);
2513 mutex_lock(&nd_mapping->lock);
2514 nd_mapping_free_labels(nd_mapping);
2515 mutex_unlock(&nd_mapping->lock);
2522 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2524 struct device **devs = NULL;
2525 int i, rc = 0, type;
2528 nvdimm_bus_lock(&nd_region->dev);
2529 rc = init_active_labels(nd_region);
2531 nvdimm_bus_unlock(&nd_region->dev);
2535 type = nd_region_to_nstype(nd_region);
2537 case ND_DEVICE_NAMESPACE_IO:
2538 devs = create_namespace_io(nd_region);
2540 case ND_DEVICE_NAMESPACE_PMEM:
2541 case ND_DEVICE_NAMESPACE_BLK:
2542 devs = create_namespaces(nd_region);
2547 nvdimm_bus_unlock(&nd_region->dev);
2552 for (i = 0; devs[i]; i++) {
2553 struct device *dev = devs[i];
2556 if (type == ND_DEVICE_NAMESPACE_BLK) {
2557 struct nd_namespace_blk *nsblk;
2559 nsblk = to_nd_namespace_blk(dev);
2560 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2563 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2564 struct nd_namespace_pmem *nspm;
2566 nspm = to_nd_namespace_pmem(dev);
2567 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2575 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2576 dev->groups = nd_namespace_attribute_groups;
2577 nd_device_register(dev);
2580 nd_region->ns_seed = devs[0];
2585 for (j = i; devs[j]; j++) {
2586 struct device *dev = devs[j];
2588 device_initialize(dev);
2593 * All of the namespaces we tried to register failed, so
2594 * fail region activation.