1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/module.h>
6 #include <linux/device.h>
7 #include <linux/sort.h>
8 #include <linux/slab.h>
9 #include <linux/list.h>
16 static void namespace_io_release(struct device *dev)
18 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
23 static void namespace_pmem_release(struct device *dev)
25 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
26 struct nd_region *nd_region = to_nd_region(dev->parent);
29 ida_simple_remove(&nd_region->ns_ida, nspm->id);
30 kfree(nspm->alt_name);
35 static void namespace_blk_release(struct device *dev)
37 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
38 struct nd_region *nd_region = to_nd_region(dev->parent);
41 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
42 kfree(nsblk->alt_name);
48 static bool is_namespace_pmem(const struct device *dev);
49 static bool is_namespace_blk(const struct device *dev);
50 static bool is_namespace_io(const struct device *dev);
52 static int is_uuid_busy(struct device *dev, void *data)
54 u8 *uuid1 = data, *uuid2 = NULL;
56 if (is_namespace_pmem(dev)) {
57 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
60 } else if (is_namespace_blk(dev)) {
61 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
64 } else if (is_nd_btt(dev)) {
65 struct nd_btt *nd_btt = to_nd_btt(dev);
68 } else if (is_nd_pfn(dev)) {
69 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
74 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
80 static int is_namespace_uuid_busy(struct device *dev, void *data)
82 if (is_nd_region(dev))
83 return device_for_each_child(dev, data, is_uuid_busy);
88 * nd_is_uuid_unique - verify that no other namespace has @uuid
89 * @dev: any device on a nvdimm_bus
90 * @uuid: uuid to check
92 bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
94 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
98 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
99 if (device_for_each_child(&nvdimm_bus->dev, uuid,
100 is_namespace_uuid_busy) != 0)
105 bool pmem_should_map_pages(struct device *dev)
107 struct nd_region *nd_region = to_nd_region(dev->parent);
108 struct nd_namespace_common *ndns = to_ndns(dev);
109 struct nd_namespace_io *nsio;
111 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
114 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
117 if (is_nd_pfn(dev) || is_nd_btt(dev))
123 nsio = to_nd_namespace_io(dev);
124 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
125 IORESOURCE_SYSTEM_RAM,
126 IORES_DESC_NONE) == REGION_MIXED)
129 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
131 EXPORT_SYMBOL(pmem_should_map_pages);
133 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
135 if (is_namespace_pmem(&ndns->dev)) {
136 struct nd_namespace_pmem *nspm;
138 nspm = to_nd_namespace_pmem(&ndns->dev);
139 if (nspm->lbasize == 0 || nspm->lbasize == 512)
141 else if (nspm->lbasize == 4096)
144 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
149 * There is no namespace label (is_namespace_io()), or the label
150 * indicates the default sector size.
154 EXPORT_SYMBOL(pmem_sector_size);
156 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
159 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
160 const char *suffix = NULL;
162 if (ndns->claim && is_nd_btt(ndns->claim))
165 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
168 if (is_namespace_pmem(&ndns->dev)) {
169 struct nd_namespace_pmem *nspm;
171 nspm = to_nd_namespace_pmem(&ndns->dev);
176 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
177 suffix ? suffix : "");
179 sprintf(name, "pmem%d%s", nd_region->id,
180 suffix ? suffix : "");
181 } else if (is_namespace_blk(&ndns->dev)) {
182 struct nd_namespace_blk *nsblk;
184 nsblk = to_nd_namespace_blk(&ndns->dev);
185 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
186 suffix ? suffix : "");
193 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
195 const u8 *nd_dev_to_uuid(struct device *dev)
197 static const u8 null_uuid[16];
202 if (is_namespace_pmem(dev)) {
203 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
206 } else if (is_namespace_blk(dev)) {
207 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
213 EXPORT_SYMBOL(nd_dev_to_uuid);
215 static ssize_t nstype_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
218 struct nd_region *nd_region = to_nd_region(dev->parent);
220 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
222 static DEVICE_ATTR_RO(nstype);
224 static ssize_t __alt_name_store(struct device *dev, const char *buf,
227 char *input, *pos, *alt_name, **ns_altname;
230 if (is_namespace_pmem(dev)) {
231 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
233 ns_altname = &nspm->alt_name;
234 } else if (is_namespace_blk(dev)) {
235 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
237 ns_altname = &nsblk->alt_name;
241 if (dev->driver || to_ndns(dev)->claim)
244 input = kstrndup(buf, len, GFP_KERNEL);
249 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
254 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
260 *ns_altname = alt_name;
261 sprintf(*ns_altname, "%s", pos);
269 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
271 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
272 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
273 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
274 struct nd_label_id label_id;
275 resource_size_t size = 0;
276 struct resource *res;
280 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
281 for_each_dpa_resource(ndd, res)
282 if (strcmp(res->name, label_id.id) == 0)
283 size += resource_size(res);
287 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
289 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
290 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
291 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
292 struct nd_label_id label_id;
293 struct resource *res;
296 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
300 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
301 for_each_dpa_resource(ndd, res) {
302 if (strcmp(res->name, label_id.id) != 0)
305 * Resources with unacknowledged adjustments indicate a
306 * failure to update labels
308 if (res->flags & DPA_RESOURCE_ADJUSTED)
313 /* These values match after a successful label update */
314 if (count != nsblk->num_resources)
317 for (i = 0; i < nsblk->num_resources; i++) {
318 struct resource *found = NULL;
320 for_each_dpa_resource(ndd, res)
321 if (res == nsblk->res[i]) {
333 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
335 resource_size_t size;
337 nvdimm_bus_lock(&nsblk->common.dev);
338 size = __nd_namespace_blk_validate(nsblk);
339 nvdimm_bus_unlock(&nsblk->common.dev);
343 EXPORT_SYMBOL(nd_namespace_blk_validate);
346 static int nd_namespace_label_update(struct nd_region *nd_region,
349 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
350 "namespace must be idle during label update\n");
351 if (dev->driver || to_ndns(dev)->claim)
355 * Only allow label writes that will result in a valid namespace
356 * or deletion of an existing namespace.
358 if (is_namespace_pmem(dev)) {
359 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
360 resource_size_t size = resource_size(&nspm->nsio.res);
362 if (size == 0 && nspm->uuid)
363 /* delete allocation */;
364 else if (!nspm->uuid)
367 return nd_pmem_namespace_label_update(nd_region, nspm, size);
368 } else if (is_namespace_blk(dev)) {
369 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
370 resource_size_t size = nd_namespace_blk_size(nsblk);
372 if (size == 0 && nsblk->uuid)
373 /* delete allocation */;
374 else if (!nsblk->uuid || !nsblk->lbasize)
377 return nd_blk_namespace_label_update(nd_region, nsblk, size);
382 static ssize_t alt_name_store(struct device *dev,
383 struct device_attribute *attr, const char *buf, size_t len)
385 struct nd_region *nd_region = to_nd_region(dev->parent);
389 nvdimm_bus_lock(dev);
390 wait_nvdimm_bus_probe_idle(dev);
391 rc = __alt_name_store(dev, buf, len);
393 rc = nd_namespace_label_update(nd_region, dev);
394 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
395 nvdimm_bus_unlock(dev);
396 nd_device_unlock(dev);
398 return rc < 0 ? rc : len;
401 static ssize_t alt_name_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
406 if (is_namespace_pmem(dev)) {
407 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
409 ns_altname = nspm->alt_name;
410 } else if (is_namespace_blk(dev)) {
411 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
413 ns_altname = nsblk->alt_name;
417 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
419 static DEVICE_ATTR_RW(alt_name);
421 static int scan_free(struct nd_region *nd_region,
422 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
425 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
426 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
430 struct resource *res, *last;
431 resource_size_t new_start;
434 for_each_dpa_resource(ndd, res)
435 if (strcmp(res->name, label_id->id) == 0)
441 if (n >= resource_size(res)) {
442 n -= resource_size(res);
443 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
444 nvdimm_free_dpa(ndd, res);
445 /* retry with last resource deleted */
450 * Keep BLK allocations relegated to high DPA as much as
454 new_start = res->start + n;
456 new_start = res->start;
458 rc = adjust_resource(res, new_start, resource_size(res) - n);
460 res->flags |= DPA_RESOURCE_ADJUSTED;
461 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
469 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
470 * @nd_region: the set of dimms to reclaim @n bytes from
471 * @label_id: unique identifier for the namespace consuming this dpa range
472 * @n: number of bytes per-dimm to release
474 * Assumes resources are ordered. Starting from the end try to
475 * adjust_resource() the allocation to @n, but if @n is larger than the
476 * allocation delete it and find the 'new' last allocation in the label
479 static int shrink_dpa_allocation(struct nd_region *nd_region,
480 struct nd_label_id *label_id, resource_size_t n)
484 for (i = 0; i < nd_region->ndr_mappings; i++) {
485 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
488 rc = scan_free(nd_region, nd_mapping, label_id, n);
496 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
497 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
500 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
501 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
502 resource_size_t first_dpa;
503 struct resource *res;
506 /* allocate blk from highest dpa first */
508 first_dpa = nd_mapping->start + nd_mapping->size - n;
510 first_dpa = nd_mapping->start;
512 /* first resource allocation for this label-id or dimm */
513 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
517 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
523 * space_valid() - validate free dpa space against constraints
524 * @nd_region: hosting region of the free space
525 * @ndd: dimm device data for debug
526 * @label_id: namespace id to allocate space
527 * @prev: potential allocation that precedes free space
528 * @next: allocation that follows the given free space range
529 * @exist: first allocation with same id in the mapping
530 * @n: range that must satisfied for pmem allocations
531 * @valid: free space range to validate
533 * BLK-space is valid as long as it does not precede a PMEM
534 * allocation in a given region. PMEM-space must be contiguous
535 * and adjacent to an existing existing allocation (if one
536 * exists). If reserving PMEM any space is valid.
538 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
539 struct nd_label_id *label_id, struct resource *prev,
540 struct resource *next, struct resource *exist,
541 resource_size_t n, struct resource *valid)
543 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
544 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
546 if (valid->start >= valid->end)
553 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
554 struct nvdimm_bus *nvdimm_bus;
555 struct blk_alloc_info info = {
556 .nd_mapping = nd_mapping,
557 .available = nd_mapping->size,
561 WARN_ON(!is_nd_blk(&nd_region->dev));
562 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
563 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
567 /* allocation needs to be contiguous, so this is all or nothing */
568 if (resource_size(valid) < n)
571 /* we've got all the space we need and no existing allocation */
575 /* allocation needs to be contiguous with the existing namespace */
576 if (valid->start == exist->end + 1
577 || valid->end == exist->start - 1)
581 /* truncate @valid size to 0 */
582 valid->end = valid->start - 1;
586 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
589 static resource_size_t scan_allocate(struct nd_region *nd_region,
590 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
593 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
594 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
595 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
596 struct resource *res, *exist = NULL, valid;
597 const resource_size_t to_allocate = n;
600 for_each_dpa_resource(ndd, res)
601 if (strcmp(label_id->id, res->name) == 0)
604 valid.start = nd_mapping->start;
605 valid.end = mapping_end;
606 valid.name = "free space";
609 for_each_dpa_resource(ndd, res) {
610 struct resource *next = res->sibling, *new_res = NULL;
611 resource_size_t allocate, available = 0;
612 enum alloc_loc loc = ALLOC_ERR;
616 /* ignore resources outside this nd_mapping */
617 if (res->start > mapping_end)
619 if (res->end < nd_mapping->start)
622 /* space at the beginning of the mapping */
623 if (!first++ && res->start > nd_mapping->start) {
624 valid.start = nd_mapping->start;
625 valid.end = res->start - 1;
626 space_valid(nd_region, ndd, label_id, NULL, next, exist,
627 to_allocate, &valid);
628 available = resource_size(&valid);
633 /* space between allocations */
635 valid.start = res->start + resource_size(res);
636 valid.end = min(mapping_end, next->start - 1);
637 space_valid(nd_region, ndd, label_id, res, next, exist,
638 to_allocate, &valid);
639 available = resource_size(&valid);
644 /* space at the end of the mapping */
646 valid.start = res->start + resource_size(res);
647 valid.end = mapping_end;
648 space_valid(nd_region, ndd, label_id, res, next, exist,
649 to_allocate, &valid);
650 available = resource_size(&valid);
655 if (!loc || !available)
657 allocate = min(available, n);
660 if (strcmp(res->name, label_id->id) == 0) {
661 /* adjust current resource up */
662 rc = adjust_resource(res, res->start - allocate,
663 resource_size(res) + allocate);
664 action = "cur grow up";
669 if (strcmp(next->name, label_id->id) == 0) {
670 /* adjust next resource up */
671 rc = adjust_resource(next, next->start
672 - allocate, resource_size(next)
675 action = "next grow up";
676 } else if (strcmp(res->name, label_id->id) == 0) {
677 action = "grow down";
682 if (strcmp(res->name, label_id->id) == 0)
683 action = "grow down";
691 if (strcmp(action, "allocate") == 0) {
692 /* BLK allocate bottom up */
694 valid.start += available - allocate;
696 new_res = nvdimm_allocate_dpa(ndd, label_id,
697 valid.start, allocate);
700 } else if (strcmp(action, "grow down") == 0) {
701 /* adjust current resource down */
702 rc = adjust_resource(res, res->start, resource_size(res)
705 res->flags |= DPA_RESOURCE_ADJUSTED;
711 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
720 * Retry scan with newly inserted resources.
721 * For example, if we did an ALLOC_BEFORE
722 * insertion there may also have been space
723 * available for an ALLOC_AFTER insertion, so we
724 * need to check this same resource again
732 * If we allocated nothing in the BLK case it may be because we are in
733 * an initial "pmem-reserve pass". Only do an initial BLK allocation
734 * when none of the DPA space is reserved.
736 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
737 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
741 static int merge_dpa(struct nd_region *nd_region,
742 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
744 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
745 struct resource *res;
747 if (strncmp("pmem", label_id->id, 4) == 0)
750 for_each_dpa_resource(ndd, res) {
752 struct resource *next = res->sibling;
753 resource_size_t end = res->start + resource_size(res);
755 if (!next || strcmp(res->name, label_id->id) != 0
756 || strcmp(next->name, label_id->id) != 0
757 || end != next->start)
759 end += resource_size(next);
760 nvdimm_free_dpa(ndd, next);
761 rc = adjust_resource(res, res->start, end - res->start);
762 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
765 res->flags |= DPA_RESOURCE_ADJUSTED;
772 int __reserve_free_pmem(struct device *dev, void *data)
774 struct nvdimm *nvdimm = data;
775 struct nd_region *nd_region;
776 struct nd_label_id label_id;
782 nd_region = to_nd_region(dev);
783 if (nd_region->ndr_mappings == 0)
786 memset(&label_id, 0, sizeof(label_id));
787 strcat(label_id.id, "pmem-reserve");
788 for (i = 0; i < nd_region->ndr_mappings; i++) {
789 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
790 resource_size_t n, rem = 0;
792 if (nd_mapping->nvdimm != nvdimm)
795 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
798 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
799 dev_WARN_ONCE(&nd_region->dev, rem,
800 "pmem reserve underrun: %#llx of %#llx bytes\n",
801 (unsigned long long) n - rem,
802 (unsigned long long) n);
803 return rem ? -ENXIO : 0;
809 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
810 struct nd_mapping *nd_mapping)
812 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
813 struct resource *res, *_res;
815 for_each_dpa_resource_safe(ndd, res, _res)
816 if (strcmp(res->name, "pmem-reserve") == 0)
817 nvdimm_free_dpa(ndd, res);
820 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
821 struct nd_mapping *nd_mapping)
823 struct nvdimm *nvdimm = nd_mapping->nvdimm;
826 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
827 __reserve_free_pmem);
829 release_free_pmem(nvdimm_bus, nd_mapping);
834 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
835 * @nd_region: the set of dimms to allocate @n more bytes from
836 * @label_id: unique identifier for the namespace consuming this dpa range
837 * @n: number of bytes per-dimm to add to the existing allocation
839 * Assumes resources are ordered. For BLK regions, first consume
840 * BLK-only available DPA free space, then consume PMEM-aliased DPA
841 * space starting at the highest DPA. For PMEM regions start
842 * allocations from the start of an interleave set and end at the first
843 * BLK allocation or the end of the interleave set, whichever comes
846 static int grow_dpa_allocation(struct nd_region *nd_region,
847 struct nd_label_id *label_id, resource_size_t n)
849 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
850 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
853 for (i = 0; i < nd_region->ndr_mappings; i++) {
854 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
855 resource_size_t rem = n;
859 * In the BLK case try once with all unallocated PMEM
860 * reserved, and once without
862 for (j = is_pmem; j < 2; j++) {
863 bool blk_only = j == 0;
866 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
870 rem = scan_allocate(nd_region, nd_mapping,
873 release_free_pmem(nvdimm_bus, nd_mapping);
875 /* try again and allow encroachments into PMEM */
880 dev_WARN_ONCE(&nd_region->dev, rem,
881 "allocation underrun: %#llx of %#llx bytes\n",
882 (unsigned long long) n - rem,
883 (unsigned long long) n);
887 rc = merge_dpa(nd_region, nd_mapping, label_id);
895 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
896 struct nd_namespace_pmem *nspm, resource_size_t size)
898 struct resource *res = &nspm->nsio.res;
899 resource_size_t offset = 0;
901 if (size && !nspm->uuid) {
906 if (size && nspm->uuid) {
907 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
908 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
909 struct nd_label_id label_id;
910 struct resource *res;
917 nd_label_gen_id(&label_id, nspm->uuid, 0);
919 /* calculate a spa offset from the dpa allocation offset */
920 for_each_dpa_resource(ndd, res)
921 if (strcmp(res->name, label_id.id) == 0) {
922 offset = (res->start - nd_mapping->start)
923 * nd_region->ndr_mappings;
932 res->start = nd_region->ndr_start + offset;
933 res->end = res->start + size - 1;
936 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
939 dev_dbg(dev, "%s: uuid not set\n", where);
945 static ssize_t __size_store(struct device *dev, unsigned long long val)
947 resource_size_t allocated = 0, available = 0;
948 struct nd_region *nd_region = to_nd_region(dev->parent);
949 struct nd_namespace_common *ndns = to_ndns(dev);
950 struct nd_mapping *nd_mapping;
951 struct nvdimm_drvdata *ndd;
952 struct nd_label_id label_id;
953 u32 flags = 0, remainder;
957 if (dev->driver || ndns->claim)
960 if (is_namespace_pmem(dev)) {
961 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
965 } else if (is_namespace_blk(dev)) {
966 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
969 flags = NSLABEL_FLAG_LOCAL;
974 * We need a uuid for the allocation-label and dimm(s) on which
975 * to store the label.
977 if (uuid_not_set(uuid, dev, __func__))
979 if (nd_region->ndr_mappings == 0) {
980 dev_dbg(dev, "not associated with dimm(s)\n");
984 div_u64_rem(val, PAGE_SIZE * nd_region->ndr_mappings, &remainder);
986 dev_dbg(dev, "%llu is not %ldK aligned\n", val,
987 (PAGE_SIZE * nd_region->ndr_mappings) / SZ_1K);
991 nd_label_gen_id(&label_id, uuid, flags);
992 for (i = 0; i < nd_region->ndr_mappings; i++) {
993 nd_mapping = &nd_region->mapping[i];
994 ndd = to_ndd(nd_mapping);
997 * All dimms in an interleave set, or the base dimm for a blk
998 * region, need to be enabled for the size to be changed.
1003 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1005 available = nd_region_allocatable_dpa(nd_region);
1007 if (val > available + allocated)
1010 if (val == allocated)
1013 val = div_u64(val, nd_region->ndr_mappings);
1014 allocated = div_u64(allocated, nd_region->ndr_mappings);
1015 if (val < allocated)
1016 rc = shrink_dpa_allocation(nd_region, &label_id,
1019 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1024 if (is_namespace_pmem(dev)) {
1025 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1027 nd_namespace_pmem_set_resource(nd_region, nspm,
1028 val * nd_region->ndr_mappings);
1032 * Try to delete the namespace if we deleted all of its
1033 * allocation, this is not the seed or 0th device for the
1034 * region, and it is not actively claimed by a btt, pfn, or dax
1037 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
1038 nd_device_unregister(dev, ND_ASYNC);
1043 static ssize_t size_store(struct device *dev,
1044 struct device_attribute *attr, const char *buf, size_t len)
1046 struct nd_region *nd_region = to_nd_region(dev->parent);
1047 unsigned long long val;
1051 rc = kstrtoull(buf, 0, &val);
1055 nd_device_lock(dev);
1056 nvdimm_bus_lock(dev);
1057 wait_nvdimm_bus_probe_idle(dev);
1058 rc = __size_store(dev, val);
1060 rc = nd_namespace_label_update(nd_region, dev);
1062 if (is_namespace_pmem(dev)) {
1063 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1066 } else if (is_namespace_blk(dev)) {
1067 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1069 uuid = &nsblk->uuid;
1072 if (rc == 0 && val == 0 && uuid) {
1073 /* setting size zero == 'delete namespace' */
1078 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
1080 nvdimm_bus_unlock(dev);
1081 nd_device_unlock(dev);
1083 return rc < 0 ? rc : len;
1086 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1088 struct device *dev = &ndns->dev;
1090 if (is_namespace_pmem(dev)) {
1091 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1093 return resource_size(&nspm->nsio.res);
1094 } else if (is_namespace_blk(dev)) {
1095 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1096 } else if (is_namespace_io(dev)) {
1097 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1099 return resource_size(&nsio->res);
1101 WARN_ONCE(1, "unknown namespace type\n");
1105 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1107 resource_size_t size;
1109 nvdimm_bus_lock(&ndns->dev);
1110 size = __nvdimm_namespace_capacity(ndns);
1111 nvdimm_bus_unlock(&ndns->dev);
1115 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1117 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1120 bool locked = false;
1121 struct device *dev = &ndns->dev;
1122 struct nd_region *nd_region = to_nd_region(dev->parent);
1124 for (i = 0; i < nd_region->ndr_mappings; i++) {
1125 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1126 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1128 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1129 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1135 EXPORT_SYMBOL(nvdimm_namespace_locked);
1137 static ssize_t size_show(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1140 return sprintf(buf, "%llu\n", (unsigned long long)
1141 nvdimm_namespace_capacity(to_ndns(dev)));
1143 static DEVICE_ATTR(size, 0444, size_show, size_store);
1145 static u8 *namespace_to_uuid(struct device *dev)
1147 if (is_namespace_pmem(dev)) {
1148 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1151 } else if (is_namespace_blk(dev)) {
1152 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1156 return ERR_PTR(-ENXIO);
1159 static ssize_t uuid_show(struct device *dev,
1160 struct device_attribute *attr, char *buf)
1162 u8 *uuid = namespace_to_uuid(dev);
1165 return PTR_ERR(uuid);
1167 return sprintf(buf, "%pUb\n", uuid);
1168 return sprintf(buf, "\n");
1172 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1173 * @nd_region: parent region so we can updates all dimms in the set
1174 * @dev: namespace type for generating label_id
1175 * @new_uuid: incoming uuid
1176 * @old_uuid: reference to the uuid storage location in the namespace object
1178 static int namespace_update_uuid(struct nd_region *nd_region,
1179 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1181 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1182 struct nd_label_id old_label_id;
1183 struct nd_label_id new_label_id;
1186 if (!nd_is_uuid_unique(dev, new_uuid))
1189 if (*old_uuid == NULL)
1193 * If we've already written a label with this uuid, then it's
1194 * too late to rename because we can't reliably update the uuid
1195 * without losing the old namespace. Userspace must delete this
1196 * namespace to abandon the old uuid.
1198 for (i = 0; i < nd_region->ndr_mappings; i++) {
1199 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1202 * This check by itself is sufficient because old_uuid
1203 * would be NULL above if this uuid did not exist in the
1204 * currently written set.
1206 * FIXME: can we delete uuid with zero dpa allocated?
1208 if (list_empty(&nd_mapping->labels))
1212 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1213 nd_label_gen_id(&new_label_id, new_uuid, flags);
1214 for (i = 0; i < nd_region->ndr_mappings; i++) {
1215 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1216 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1217 struct nd_label_ent *label_ent;
1218 struct resource *res;
1220 for_each_dpa_resource(ndd, res)
1221 if (strcmp(res->name, old_label_id.id) == 0)
1222 sprintf((void *) res->name, "%s",
1225 mutex_lock(&nd_mapping->lock);
1226 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1227 struct nd_namespace_label *nd_label = label_ent->label;
1228 struct nd_label_id label_id;
1232 nd_label_gen_id(&label_id, nd_label->uuid,
1233 __le32_to_cpu(nd_label->flags));
1234 if (strcmp(old_label_id.id, label_id.id) == 0)
1235 set_bit(ND_LABEL_REAP, &label_ent->flags);
1237 mutex_unlock(&nd_mapping->lock);
1241 *old_uuid = new_uuid;
1245 static ssize_t uuid_store(struct device *dev,
1246 struct device_attribute *attr, const char *buf, size_t len)
1248 struct nd_region *nd_region = to_nd_region(dev->parent);
1253 if (is_namespace_pmem(dev)) {
1254 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1256 ns_uuid = &nspm->uuid;
1257 } else if (is_namespace_blk(dev)) {
1258 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1260 ns_uuid = &nsblk->uuid;
1264 nd_device_lock(dev);
1265 nvdimm_bus_lock(dev);
1266 wait_nvdimm_bus_probe_idle(dev);
1267 if (to_ndns(dev)->claim)
1270 rc = nd_uuid_store(dev, &uuid, buf, len);
1272 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1274 rc = nd_namespace_label_update(nd_region, dev);
1277 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1278 buf[len - 1] == '\n' ? "" : "\n");
1279 nvdimm_bus_unlock(dev);
1280 nd_device_unlock(dev);
1282 return rc < 0 ? rc : len;
1284 static DEVICE_ATTR_RW(uuid);
1286 static ssize_t resource_show(struct device *dev,
1287 struct device_attribute *attr, char *buf)
1289 struct resource *res;
1291 if (is_namespace_pmem(dev)) {
1292 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1294 res = &nspm->nsio.res;
1295 } else if (is_namespace_io(dev)) {
1296 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1302 /* no address to convey if the namespace has no allocation */
1303 if (resource_size(res) == 0)
1305 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1307 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1309 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
1310 4096, 4104, 4160, 4224, 0 };
1312 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1314 static ssize_t sector_size_show(struct device *dev,
1315 struct device_attribute *attr, char *buf)
1317 if (is_namespace_blk(dev)) {
1318 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1320 return nd_size_select_show(nsblk->lbasize,
1321 blk_lbasize_supported, buf);
1324 if (is_namespace_pmem(dev)) {
1325 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1327 return nd_size_select_show(nspm->lbasize,
1328 pmem_lbasize_supported, buf);
1333 static ssize_t sector_size_store(struct device *dev,
1334 struct device_attribute *attr, const char *buf, size_t len)
1336 struct nd_region *nd_region = to_nd_region(dev->parent);
1337 const unsigned long *supported;
1338 unsigned long *lbasize;
1341 if (is_namespace_blk(dev)) {
1342 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1344 lbasize = &nsblk->lbasize;
1345 supported = blk_lbasize_supported;
1346 } else if (is_namespace_pmem(dev)) {
1347 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1349 lbasize = &nspm->lbasize;
1350 supported = pmem_lbasize_supported;
1354 nd_device_lock(dev);
1355 nvdimm_bus_lock(dev);
1356 if (to_ndns(dev)->claim)
1359 rc = nd_size_select_store(dev, buf, lbasize, supported);
1361 rc = nd_namespace_label_update(nd_region, dev);
1362 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1363 buf, buf[len - 1] == '\n' ? "" : "\n");
1364 nvdimm_bus_unlock(dev);
1365 nd_device_unlock(dev);
1367 return rc ? rc : len;
1369 static DEVICE_ATTR_RW(sector_size);
1371 static ssize_t dpa_extents_show(struct device *dev,
1372 struct device_attribute *attr, char *buf)
1374 struct nd_region *nd_region = to_nd_region(dev->parent);
1375 struct nd_label_id label_id;
1380 nvdimm_bus_lock(dev);
1381 if (is_namespace_pmem(dev)) {
1382 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1386 } else if (is_namespace_blk(dev)) {
1387 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1390 flags = NSLABEL_FLAG_LOCAL;
1396 nd_label_gen_id(&label_id, uuid, flags);
1397 for (i = 0; i < nd_region->ndr_mappings; i++) {
1398 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1399 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1400 struct resource *res;
1402 for_each_dpa_resource(ndd, res)
1403 if (strcmp(res->name, label_id.id) == 0)
1407 nvdimm_bus_unlock(dev);
1409 return sprintf(buf, "%d\n", count);
1411 static DEVICE_ATTR_RO(dpa_extents);
1413 static int btt_claim_class(struct device *dev)
1415 struct nd_region *nd_region = to_nd_region(dev->parent);
1416 int i, loop_bitmask = 0;
1418 for (i = 0; i < nd_region->ndr_mappings; i++) {
1419 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1420 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1421 struct nd_namespace_index *nsindex;
1424 * If any of the DIMMs do not support labels the only
1425 * possible BTT format is v1.
1432 nsindex = to_namespace_index(ndd, ndd->ns_current);
1433 if (nsindex == NULL)
1436 /* check whether existing labels are v1.1 or v1.2 */
1437 if (__le16_to_cpu(nsindex->major) == 1
1438 && __le16_to_cpu(nsindex->minor) == 1)
1445 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1446 * block is found, a v1.1 label for any mapping will set bit 1, and a
1447 * v1.2 label will set bit 2.
1449 * At the end of the loop, at most one of the three bits must be set.
1450 * If multiple bits were set, it means the different mappings disagree
1451 * about their labels, and this must be cleaned up first.
1453 * If all the label index blocks are found to agree, nsindex of NULL
1454 * implies labels haven't been initialized yet, and when they will,
1455 * they will be of the 1.2 format, so we can assume BTT2.0
1457 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1458 * found, we enforce BTT2.0
1460 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1462 switch (loop_bitmask) {
1465 return NVDIMM_CCLASS_BTT;
1468 return NVDIMM_CCLASS_BTT2;
1474 static ssize_t holder_show(struct device *dev,
1475 struct device_attribute *attr, char *buf)
1477 struct nd_namespace_common *ndns = to_ndns(dev);
1480 nd_device_lock(dev);
1481 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1482 nd_device_unlock(dev);
1486 static DEVICE_ATTR_RO(holder);
1488 static int __holder_class_store(struct device *dev, const char *buf)
1490 struct nd_namespace_common *ndns = to_ndns(dev);
1492 if (dev->driver || ndns->claim)
1495 if (sysfs_streq(buf, "btt")) {
1496 int rc = btt_claim_class(dev);
1498 if (rc < NVDIMM_CCLASS_NONE)
1500 ndns->claim_class = rc;
1501 } else if (sysfs_streq(buf, "pfn"))
1502 ndns->claim_class = NVDIMM_CCLASS_PFN;
1503 else if (sysfs_streq(buf, "dax"))
1504 ndns->claim_class = NVDIMM_CCLASS_DAX;
1505 else if (sysfs_streq(buf, ""))
1506 ndns->claim_class = NVDIMM_CCLASS_NONE;
1513 static ssize_t holder_class_store(struct device *dev,
1514 struct device_attribute *attr, const char *buf, size_t len)
1516 struct nd_region *nd_region = to_nd_region(dev->parent);
1519 nd_device_lock(dev);
1520 nvdimm_bus_lock(dev);
1521 wait_nvdimm_bus_probe_idle(dev);
1522 rc = __holder_class_store(dev, buf);
1524 rc = nd_namespace_label_update(nd_region, dev);
1525 dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
1526 nvdimm_bus_unlock(dev);
1527 nd_device_unlock(dev);
1529 return rc < 0 ? rc : len;
1532 static ssize_t holder_class_show(struct device *dev,
1533 struct device_attribute *attr, char *buf)
1535 struct nd_namespace_common *ndns = to_ndns(dev);
1538 nd_device_lock(dev);
1539 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1540 rc = sprintf(buf, "\n");
1541 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1542 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1543 rc = sprintf(buf, "btt\n");
1544 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1545 rc = sprintf(buf, "pfn\n");
1546 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1547 rc = sprintf(buf, "dax\n");
1549 rc = sprintf(buf, "<unknown>\n");
1550 nd_device_unlock(dev);
1554 static DEVICE_ATTR_RW(holder_class);
1556 static ssize_t mode_show(struct device *dev,
1557 struct device_attribute *attr, char *buf)
1559 struct nd_namespace_common *ndns = to_ndns(dev);
1560 struct device *claim;
1564 nd_device_lock(dev);
1565 claim = ndns->claim;
1566 if (claim && is_nd_btt(claim))
1568 else if (claim && is_nd_pfn(claim))
1570 else if (claim && is_nd_dax(claim))
1572 else if (!claim && pmem_should_map_pages(dev))
1576 rc = sprintf(buf, "%s\n", mode);
1577 nd_device_unlock(dev);
1581 static DEVICE_ATTR_RO(mode);
1583 static ssize_t force_raw_store(struct device *dev,
1584 struct device_attribute *attr, const char *buf, size_t len)
1587 int rc = strtobool(buf, &force_raw);
1592 to_ndns(dev)->force_raw = force_raw;
1596 static ssize_t force_raw_show(struct device *dev,
1597 struct device_attribute *attr, char *buf)
1599 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1601 static DEVICE_ATTR_RW(force_raw);
1603 static struct attribute *nd_namespace_attributes[] = {
1604 &dev_attr_nstype.attr,
1605 &dev_attr_size.attr,
1606 &dev_attr_mode.attr,
1607 &dev_attr_uuid.attr,
1608 &dev_attr_holder.attr,
1609 &dev_attr_resource.attr,
1610 &dev_attr_alt_name.attr,
1611 &dev_attr_force_raw.attr,
1612 &dev_attr_sector_size.attr,
1613 &dev_attr_dpa_extents.attr,
1614 &dev_attr_holder_class.attr,
1618 static umode_t namespace_visible(struct kobject *kobj,
1619 struct attribute *a, int n)
1621 struct device *dev = container_of(kobj, struct device, kobj);
1623 if (a == &dev_attr_resource.attr && is_namespace_blk(dev))
1626 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1627 if (a == &dev_attr_size.attr)
1633 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1634 || a == &dev_attr_holder.attr
1635 || a == &dev_attr_holder_class.attr
1636 || a == &dev_attr_force_raw.attr
1637 || a == &dev_attr_mode.attr)
1643 static struct attribute_group nd_namespace_attribute_group = {
1644 .attrs = nd_namespace_attributes,
1645 .is_visible = namespace_visible,
1648 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1649 &nd_device_attribute_group,
1650 &nd_namespace_attribute_group,
1651 &nd_numa_attribute_group,
1655 static const struct device_type namespace_io_device_type = {
1656 .name = "nd_namespace_io",
1657 .release = namespace_io_release,
1658 .groups = nd_namespace_attribute_groups,
1661 static const struct device_type namespace_pmem_device_type = {
1662 .name = "nd_namespace_pmem",
1663 .release = namespace_pmem_release,
1664 .groups = nd_namespace_attribute_groups,
1667 static const struct device_type namespace_blk_device_type = {
1668 .name = "nd_namespace_blk",
1669 .release = namespace_blk_release,
1670 .groups = nd_namespace_attribute_groups,
1673 static bool is_namespace_pmem(const struct device *dev)
1675 return dev ? dev->type == &namespace_pmem_device_type : false;
1678 static bool is_namespace_blk(const struct device *dev)
1680 return dev ? dev->type == &namespace_blk_device_type : false;
1683 static bool is_namespace_io(const struct device *dev)
1685 return dev ? dev->type == &namespace_io_device_type : false;
1688 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1690 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1691 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1692 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1693 struct nd_namespace_common *ndns = NULL;
1694 resource_size_t size;
1696 if (nd_btt || nd_pfn || nd_dax) {
1698 ndns = nd_btt->ndns;
1700 ndns = nd_pfn->ndns;
1702 ndns = nd_dax->nd_pfn.ndns;
1705 return ERR_PTR(-ENODEV);
1708 * Flush any in-progess probes / removals in the driver
1709 * for the raw personality of this namespace.
1711 nd_device_lock(&ndns->dev);
1712 nd_device_unlock(&ndns->dev);
1713 if (ndns->dev.driver) {
1714 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1716 return ERR_PTR(-EBUSY);
1718 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1719 "host (%s) vs claim (%s) mismatch\n",
1721 dev_name(ndns->claim)))
1722 return ERR_PTR(-ENXIO);
1724 ndns = to_ndns(dev);
1726 dev_dbg(dev, "claimed by %s, failing probe\n",
1727 dev_name(ndns->claim));
1729 return ERR_PTR(-ENXIO);
1733 if (nvdimm_namespace_locked(ndns))
1734 return ERR_PTR(-EACCES);
1736 size = nvdimm_namespace_capacity(ndns);
1737 if (size < ND_MIN_NAMESPACE_SIZE) {
1738 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1739 &size, ND_MIN_NAMESPACE_SIZE);
1740 return ERR_PTR(-ENODEV);
1744 * Note, alignment validation for fsdax and devdax mode
1745 * namespaces happens in nd_pfn_validate() where infoblock
1746 * padding parameters can be applied.
1748 if (pmem_should_map_pages(dev)) {
1749 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
1750 struct resource *res = &nsio->res;
1752 if (!IS_ALIGNED(res->start | (res->end + 1),
1753 memremap_compat_align())) {
1754 dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
1755 return ERR_PTR(-EOPNOTSUPP);
1759 if (is_namespace_pmem(&ndns->dev)) {
1760 struct nd_namespace_pmem *nspm;
1762 nspm = to_nd_namespace_pmem(&ndns->dev);
1763 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1764 return ERR_PTR(-ENODEV);
1765 } else if (is_namespace_blk(&ndns->dev)) {
1766 struct nd_namespace_blk *nsblk;
1768 nsblk = to_nd_namespace_blk(&ndns->dev);
1769 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1770 return ERR_PTR(-ENODEV);
1771 if (!nsblk->lbasize) {
1772 dev_dbg(&ndns->dev, "sector size not set\n");
1773 return ERR_PTR(-ENODEV);
1775 if (!nd_namespace_blk_validate(nsblk))
1776 return ERR_PTR(-ENODEV);
1781 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1783 int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1784 resource_size_t size)
1786 if (is_namespace_blk(&ndns->dev))
1788 return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1790 EXPORT_SYMBOL_GPL(devm_namespace_enable);
1792 void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1794 if (is_namespace_blk(&ndns->dev))
1796 devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1798 EXPORT_SYMBOL_GPL(devm_namespace_disable);
1800 static struct device **create_namespace_io(struct nd_region *nd_region)
1802 struct nd_namespace_io *nsio;
1803 struct device *dev, **devs;
1804 struct resource *res;
1806 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1810 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1816 dev = &nsio->common.dev;
1817 dev->type = &namespace_io_device_type;
1818 dev->parent = &nd_region->dev;
1820 res->name = dev_name(&nd_region->dev);
1821 res->flags = IORESOURCE_MEM;
1822 res->start = nd_region->ndr_start;
1823 res->end = res->start + nd_region->ndr_size - 1;
1829 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1830 u64 cookie, u16 pos)
1832 struct nd_namespace_label *found = NULL;
1835 for (i = 0; i < nd_region->ndr_mappings; i++) {
1836 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1837 struct nd_interleave_set *nd_set = nd_region->nd_set;
1838 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1839 struct nd_label_ent *label_ent;
1840 bool found_uuid = false;
1842 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1843 struct nd_namespace_label *nd_label = label_ent->label;
1844 u16 position, nlabel;
1849 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1850 position = __le16_to_cpu(nd_label->position);
1851 nlabel = __le16_to_cpu(nd_label->nlabel);
1853 if (isetcookie != cookie)
1856 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1859 if (namespace_label_has(ndd, type_guid)
1860 && !guid_equal(&nd_set->type_guid,
1861 &nd_label->type_guid)) {
1862 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1864 &nd_label->type_guid);
1869 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1873 if (nlabel != nd_region->ndr_mappings)
1875 if (position != pos)
1883 return found != NULL;
1886 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1893 for (i = 0; i < nd_region->ndr_mappings; i++) {
1894 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1895 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1896 struct nd_namespace_label *nd_label = NULL;
1897 u64 hw_start, hw_end, pmem_start, pmem_end;
1898 struct nd_label_ent *label_ent;
1900 lockdep_assert_held(&nd_mapping->lock);
1901 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1902 nd_label = label_ent->label;
1905 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1916 * Check that this label is compliant with the dpa
1917 * range published in NFIT
1919 hw_start = nd_mapping->start;
1920 hw_end = hw_start + nd_mapping->size;
1921 pmem_start = __le64_to_cpu(nd_label->dpa);
1922 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1923 if (pmem_start >= hw_start && pmem_start < hw_end
1924 && pmem_end <= hw_end && pmem_end > hw_start)
1927 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1928 dev_name(ndd->dev), nd_label->uuid);
1932 /* move recently validated label to the front of the list */
1933 list_move(&label_ent->list, &nd_mapping->labels);
1939 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1940 * @nd_region: region with mappings to validate
1941 * @nspm: target namespace to create
1942 * @nd_label: target pmem namespace label to evaluate
1944 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1945 struct nd_namespace_index *nsindex,
1946 struct nd_namespace_label *nd_label)
1948 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1949 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1950 struct nd_label_ent *label_ent;
1951 struct nd_namespace_pmem *nspm;
1952 struct nd_mapping *nd_mapping;
1953 resource_size_t size = 0;
1954 struct resource *res;
1960 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1961 return ERR_PTR(-ENXIO);
1964 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1965 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1967 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1968 return ERR_PTR(-EAGAIN);
1970 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1974 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1976 return ERR_PTR(-ENOMEM);
1979 dev = &nspm->nsio.common.dev;
1980 dev->type = &namespace_pmem_device_type;
1981 dev->parent = &nd_region->dev;
1982 res = &nspm->nsio.res;
1983 res->name = dev_name(&nd_region->dev);
1984 res->flags = IORESOURCE_MEM;
1986 for (i = 0; i < nd_region->ndr_mappings; i++) {
1987 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1989 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1994 if (i < nd_region->ndr_mappings) {
1995 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1998 * Give up if we don't find an instance of a uuid at each
1999 * position (from 0 to nd_region->ndr_mappings - 1), or if we
2000 * find a dimm with two instances of the same uuid.
2002 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
2003 nvdimm_name(nvdimm), nd_label->uuid);
2009 * Fix up each mapping's 'labels' to have the validated pmem label for
2010 * that position at labels[0], and NULL at labels[1]. In the process,
2011 * check that the namespace aligns with interleave-set. We know
2012 * that it does not overlap with any blk namespaces by virtue of
2013 * the dimm being enabled (i.e. nd_label_reserve_dpa()
2016 rc = select_pmem_id(nd_region, nd_label->uuid);
2020 /* Calculate total size and populate namespace properties from label0 */
2021 for (i = 0; i < nd_region->ndr_mappings; i++) {
2022 struct nd_namespace_label *label0;
2023 struct nvdimm_drvdata *ndd;
2025 nd_mapping = &nd_region->mapping[i];
2026 label_ent = list_first_entry_or_null(&nd_mapping->labels,
2027 typeof(*label_ent), list);
2028 label0 = label_ent ? label_ent->label : NULL;
2035 size += __le64_to_cpu(label0->rawsize);
2036 if (__le16_to_cpu(label0->position) != 0)
2038 WARN_ON(nspm->alt_name || nspm->uuid);
2039 nspm->alt_name = kmemdup((void __force *) label0->name,
2040 NSLABEL_NAME_LEN, GFP_KERNEL);
2041 nspm->uuid = kmemdup((void __force *) label0->uuid,
2042 NSLABEL_UUID_LEN, GFP_KERNEL);
2043 nspm->lbasize = __le64_to_cpu(label0->lbasize);
2044 ndd = to_ndd(nd_mapping);
2045 if (namespace_label_has(ndd, abstraction_guid))
2046 nspm->nsio.common.claim_class
2047 = to_nvdimm_cclass(&label0->abstraction_guid);
2051 if (!nspm->alt_name || !nspm->uuid) {
2056 nd_namespace_pmem_set_resource(nd_region, nspm, size);
2060 namespace_pmem_release(dev);
2063 dev_dbg(&nd_region->dev, "invalid label(s)\n");
2066 dev_dbg(&nd_region->dev, "label not found\n");
2069 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
2075 struct resource *nsblk_add_resource(struct nd_region *nd_region,
2076 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2077 resource_size_t start)
2079 struct nd_label_id label_id;
2080 struct resource *res;
2082 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2083 res = krealloc(nsblk->res,
2084 sizeof(void *) * (nsblk->num_resources + 1),
2088 nsblk->res = (struct resource **) res;
2089 for_each_dpa_resource(ndd, res)
2090 if (strcmp(res->name, label_id.id) == 0
2091 && res->start == start) {
2092 nsblk->res[nsblk->num_resources++] = res;
2098 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2100 struct nd_namespace_blk *nsblk;
2103 if (!is_nd_blk(&nd_region->dev))
2106 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2110 dev = &nsblk->common.dev;
2111 dev->type = &namespace_blk_device_type;
2112 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2113 if (nsblk->id < 0) {
2117 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2118 dev->parent = &nd_region->dev;
2120 return &nsblk->common.dev;
2123 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2125 struct nd_namespace_pmem *nspm;
2126 struct resource *res;
2129 if (!is_memory(&nd_region->dev))
2132 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2136 dev = &nspm->nsio.common.dev;
2137 dev->type = &namespace_pmem_device_type;
2138 dev->parent = &nd_region->dev;
2139 res = &nspm->nsio.res;
2140 res->name = dev_name(&nd_region->dev);
2141 res->flags = IORESOURCE_MEM;
2143 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2148 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
2149 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2154 void nd_region_create_ns_seed(struct nd_region *nd_region)
2156 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2158 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2161 if (is_nd_blk(&nd_region->dev))
2162 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2164 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2167 * Seed creation failures are not fatal, provisioning is simply
2168 * disabled until memory becomes available
2170 if (!nd_region->ns_seed)
2171 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2172 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
2174 nd_device_register(nd_region->ns_seed);
2177 void nd_region_create_dax_seed(struct nd_region *nd_region)
2179 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2180 nd_region->dax_seed = nd_dax_create(nd_region);
2182 * Seed creation failures are not fatal, provisioning is simply
2183 * disabled until memory becomes available
2185 if (!nd_region->dax_seed)
2186 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2189 void nd_region_create_pfn_seed(struct nd_region *nd_region)
2191 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2192 nd_region->pfn_seed = nd_pfn_create(nd_region);
2194 * Seed creation failures are not fatal, provisioning is simply
2195 * disabled until memory becomes available
2197 if (!nd_region->pfn_seed)
2198 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2201 void nd_region_create_btt_seed(struct nd_region *nd_region)
2203 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2204 nd_region->btt_seed = nd_btt_create(nd_region);
2206 * Seed creation failures are not fatal, provisioning is simply
2207 * disabled until memory becomes available
2209 if (!nd_region->btt_seed)
2210 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2213 static int add_namespace_resource(struct nd_region *nd_region,
2214 struct nd_namespace_label *nd_label, struct device **devs,
2217 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2218 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2221 for (i = 0; i < count; i++) {
2222 u8 *uuid = namespace_to_uuid(devs[i]);
2223 struct resource *res;
2225 if (IS_ERR_OR_NULL(uuid)) {
2230 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2232 if (is_namespace_blk(devs[i])) {
2233 res = nsblk_add_resource(nd_region, ndd,
2234 to_nd_namespace_blk(devs[i]),
2235 __le64_to_cpu(nd_label->dpa));
2238 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2240 dev_err(&nd_region->dev,
2241 "error: conflicting extents for uuid: %pUb\n",
2251 static struct device *create_namespace_blk(struct nd_region *nd_region,
2252 struct nd_namespace_label *nd_label, int count)
2255 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2256 struct nd_interleave_set *nd_set = nd_region->nd_set;
2257 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2258 struct nd_namespace_blk *nsblk;
2259 char name[NSLABEL_NAME_LEN];
2260 struct device *dev = NULL;
2261 struct resource *res;
2263 if (namespace_label_has(ndd, type_guid)) {
2264 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
2265 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
2267 &nd_label->type_guid);
2268 return ERR_PTR(-EAGAIN);
2271 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
2272 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
2274 __le64_to_cpu(nd_label->isetcookie));
2275 return ERR_PTR(-EAGAIN);
2279 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2281 return ERR_PTR(-ENOMEM);
2282 dev = &nsblk->common.dev;
2283 dev->type = &namespace_blk_device_type;
2284 dev->parent = &nd_region->dev;
2286 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2287 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2289 if (namespace_label_has(ndd, abstraction_guid))
2290 nsblk->common.claim_class
2291 = to_nvdimm_cclass(&nd_label->abstraction_guid);
2294 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2296 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2298 if (!nsblk->alt_name)
2301 res = nsblk_add_resource(nd_region, ndd, nsblk,
2302 __le64_to_cpu(nd_label->dpa));
2305 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2308 namespace_blk_release(dev);
2309 return ERR_PTR(-ENXIO);
2312 static int cmp_dpa(const void *a, const void *b)
2314 const struct device *dev_a = *(const struct device **) a;
2315 const struct device *dev_b = *(const struct device **) b;
2316 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2317 struct nd_namespace_pmem *nspm_a, *nspm_b;
2319 if (is_namespace_io(dev_a))
2322 if (is_namespace_blk(dev_a)) {
2323 nsblk_a = to_nd_namespace_blk(dev_a);
2324 nsblk_b = to_nd_namespace_blk(dev_b);
2326 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2327 sizeof(resource_size_t));
2330 nspm_a = to_nd_namespace_pmem(dev_a);
2331 nspm_b = to_nd_namespace_pmem(dev_b);
2333 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2334 sizeof(resource_size_t));
2337 static struct device **scan_labels(struct nd_region *nd_region)
2340 struct device *dev, **devs = NULL;
2341 struct nd_label_ent *label_ent, *e;
2342 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2343 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
2345 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2346 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
2347 struct nd_namespace_label *nd_label = label_ent->label;
2348 struct device **__devs;
2353 flags = __le32_to_cpu(nd_label->flags);
2354 if (is_nd_blk(&nd_region->dev)
2355 == !!(flags & NSLABEL_FLAG_LOCAL))
2356 /* pass, region matches label type */;
2360 /* skip labels that describe extents outside of the region */
2361 if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
2362 __le64_to_cpu(nd_label->dpa) > map_end)
2365 i = add_namespace_resource(nd_region, nd_label, devs, count);
2370 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2373 memcpy(__devs, devs, sizeof(dev) * count);
2377 if (is_nd_blk(&nd_region->dev))
2378 dev = create_namespace_blk(nd_region, nd_label, count);
2380 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2381 struct nd_namespace_index *nsindex;
2383 nsindex = to_namespace_index(ndd, ndd->ns_current);
2384 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
2388 switch (PTR_ERR(dev)) {
2390 /* skip invalid labels */
2393 /* fallthrough to seed creation */
2399 devs[count++] = dev;
2403 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2404 count, is_nd_blk(&nd_region->dev)
2405 ? "blk" : "pmem", count == 1 ? "" : "s");
2408 /* Publish a zero-sized namespace for userspace to configure. */
2409 nd_mapping_free_labels(nd_mapping);
2411 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2414 if (is_nd_blk(&nd_region->dev)) {
2415 struct nd_namespace_blk *nsblk;
2417 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2420 dev = &nsblk->common.dev;
2421 dev->type = &namespace_blk_device_type;
2423 struct nd_namespace_pmem *nspm;
2425 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2428 dev = &nspm->nsio.common.dev;
2429 dev->type = &namespace_pmem_device_type;
2430 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2432 dev->parent = &nd_region->dev;
2433 devs[count++] = dev;
2434 } else if (is_memory(&nd_region->dev)) {
2435 /* clean unselected labels */
2436 for (i = 0; i < nd_region->ndr_mappings; i++) {
2437 struct list_head *l, *e;
2441 nd_mapping = &nd_region->mapping[i];
2442 if (list_empty(&nd_mapping->labels)) {
2448 list_for_each_safe(l, e, &nd_mapping->labels) {
2451 list_move_tail(l, &list);
2453 nd_mapping_free_labels(nd_mapping);
2454 list_splice_init(&list, &nd_mapping->labels);
2459 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2465 for (i = 0; devs[i]; i++)
2466 if (is_nd_blk(&nd_region->dev))
2467 namespace_blk_release(devs[i]);
2469 namespace_pmem_release(devs[i]);
2475 static struct device **create_namespaces(struct nd_region *nd_region)
2477 struct nd_mapping *nd_mapping;
2478 struct device **devs;
2481 if (nd_region->ndr_mappings == 0)
2484 /* lock down all mappings while we scan labels */
2485 for (i = 0; i < nd_region->ndr_mappings; i++) {
2486 nd_mapping = &nd_region->mapping[i];
2487 mutex_lock_nested(&nd_mapping->lock, i);
2490 devs = scan_labels(nd_region);
2492 for (i = 0; i < nd_region->ndr_mappings; i++) {
2493 int reverse = nd_region->ndr_mappings - 1 - i;
2495 nd_mapping = &nd_region->mapping[reverse];
2496 mutex_unlock(&nd_mapping->lock);
2502 static void deactivate_labels(void *region)
2504 struct nd_region *nd_region = region;
2507 for (i = 0; i < nd_region->ndr_mappings; i++) {
2508 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2509 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2510 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2512 mutex_lock(&nd_mapping->lock);
2513 nd_mapping_free_labels(nd_mapping);
2514 mutex_unlock(&nd_mapping->lock);
2517 nd_mapping->ndd = NULL;
2519 atomic_dec(&nvdimm->busy);
2523 static int init_active_labels(struct nd_region *nd_region)
2527 for (i = 0; i < nd_region->ndr_mappings; i++) {
2528 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2529 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2530 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2531 struct nd_label_ent *label_ent;
2535 * If the dimm is disabled then we may need to prevent
2536 * the region from being activated.
2539 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2540 /* fail, label data may be unreadable */;
2541 else if (test_bit(NDD_LABELING, &nvdimm->flags))
2542 /* fail, labels needed to disambiguate dpa */;
2546 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2547 dev_name(&nd_mapping->nvdimm->dev),
2548 test_bit(NDD_LOCKED, &nvdimm->flags)
2549 ? "locked" : "disabled");
2552 nd_mapping->ndd = ndd;
2553 atomic_inc(&nvdimm->busy);
2556 count = nd_label_active_count(ndd);
2557 dev_dbg(ndd->dev, "count: %d\n", count);
2560 for (j = 0; j < count; j++) {
2561 struct nd_namespace_label *label;
2563 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2566 label = nd_label_active(ndd, j);
2567 if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2568 u32 flags = __le32_to_cpu(label->flags);
2570 flags &= ~NSLABEL_FLAG_LOCAL;
2571 label->flags = __cpu_to_le32(flags);
2573 label_ent->label = label;
2575 mutex_lock(&nd_mapping->lock);
2576 list_add_tail(&label_ent->list, &nd_mapping->labels);
2577 mutex_unlock(&nd_mapping->lock);
2584 if (i < nd_region->ndr_mappings) {
2585 deactivate_labels(nd_region);
2589 return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
2593 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2595 struct device **devs = NULL;
2596 int i, rc = 0, type;
2599 nvdimm_bus_lock(&nd_region->dev);
2600 rc = init_active_labels(nd_region);
2602 nvdimm_bus_unlock(&nd_region->dev);
2606 type = nd_region_to_nstype(nd_region);
2608 case ND_DEVICE_NAMESPACE_IO:
2609 devs = create_namespace_io(nd_region);
2611 case ND_DEVICE_NAMESPACE_PMEM:
2612 case ND_DEVICE_NAMESPACE_BLK:
2613 devs = create_namespaces(nd_region);
2618 nvdimm_bus_unlock(&nd_region->dev);
2623 for (i = 0; devs[i]; i++) {
2624 struct device *dev = devs[i];
2627 if (type == ND_DEVICE_NAMESPACE_BLK) {
2628 struct nd_namespace_blk *nsblk;
2630 nsblk = to_nd_namespace_blk(dev);
2631 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2634 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2635 struct nd_namespace_pmem *nspm;
2637 nspm = to_nd_namespace_pmem(dev);
2638 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2646 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2647 nd_device_register(dev);
2650 nd_region->ns_seed = devs[0];
2655 for (j = i; devs[j]; j++) {
2656 struct device *dev = devs[j];
2658 device_initialize(dev);
2663 * All of the namespaces we tried to register failed, so
2664 * fail region activation.