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 bool is_namespace_pmem(const struct device *dev);
36 static bool is_namespace_io(const struct device *dev);
38 static int is_uuid_busy(struct device *dev, void *data)
40 uuid_t *uuid1 = data, *uuid2 = NULL;
42 if (is_namespace_pmem(dev)) {
43 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
46 } else if (is_nd_btt(dev)) {
47 struct nd_btt *nd_btt = to_nd_btt(dev);
50 } else if (is_nd_pfn(dev)) {
51 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
56 if (uuid2 && uuid_equal(uuid1, uuid2))
62 static int is_namespace_uuid_busy(struct device *dev, void *data)
64 if (is_nd_region(dev))
65 return device_for_each_child(dev, data, is_uuid_busy);
70 * nd_is_uuid_unique - verify that no other namespace has @uuid
71 * @dev: any device on a nvdimm_bus
72 * @uuid: uuid to check
74 bool nd_is_uuid_unique(struct device *dev, uuid_t *uuid)
76 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
80 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
81 if (device_for_each_child(&nvdimm_bus->dev, uuid,
82 is_namespace_uuid_busy) != 0)
87 bool pmem_should_map_pages(struct device *dev)
89 struct nd_region *nd_region = to_nd_region(dev->parent);
90 struct nd_namespace_common *ndns = to_ndns(dev);
91 struct nd_namespace_io *nsio;
93 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
96 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
99 if (is_nd_pfn(dev) || is_nd_btt(dev))
105 nsio = to_nd_namespace_io(dev);
106 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
107 IORESOURCE_SYSTEM_RAM,
108 IORES_DESC_NONE) == REGION_MIXED)
111 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
113 EXPORT_SYMBOL(pmem_should_map_pages);
115 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
117 if (is_namespace_pmem(&ndns->dev)) {
118 struct nd_namespace_pmem *nspm;
120 nspm = to_nd_namespace_pmem(&ndns->dev);
121 if (nspm->lbasize == 0 || nspm->lbasize == 512)
123 else if (nspm->lbasize == 4096)
126 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
131 * There is no namespace label (is_namespace_io()), or the label
132 * indicates the default sector size.
136 EXPORT_SYMBOL(pmem_sector_size);
138 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
141 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
142 const char *suffix = NULL;
144 if (ndns->claim && is_nd_btt(ndns->claim))
147 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
150 if (is_namespace_pmem(&ndns->dev)) {
151 struct nd_namespace_pmem *nspm;
153 nspm = to_nd_namespace_pmem(&ndns->dev);
158 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
159 suffix ? suffix : "");
161 sprintf(name, "pmem%d%s", nd_region->id,
162 suffix ? suffix : "");
169 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
171 const uuid_t *nd_dev_to_uuid(struct device *dev)
176 if (is_namespace_pmem(dev)) {
177 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
183 EXPORT_SYMBOL(nd_dev_to_uuid);
185 static ssize_t nstype_show(struct device *dev,
186 struct device_attribute *attr, char *buf)
188 struct nd_region *nd_region = to_nd_region(dev->parent);
190 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
192 static DEVICE_ATTR_RO(nstype);
194 static ssize_t __alt_name_store(struct device *dev, const char *buf,
197 char *input, *pos, *alt_name, **ns_altname;
200 if (is_namespace_pmem(dev)) {
201 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
203 ns_altname = &nspm->alt_name;
207 if (dev->driver || to_ndns(dev)->claim)
210 input = kstrndup(buf, len, GFP_KERNEL);
215 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
220 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
226 *ns_altname = alt_name;
227 sprintf(*ns_altname, "%s", pos);
235 static int nd_namespace_label_update(struct nd_region *nd_region,
238 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
239 "namespace must be idle during label update\n");
240 if (dev->driver || to_ndns(dev)->claim)
244 * Only allow label writes that will result in a valid namespace
245 * or deletion of an existing namespace.
247 if (is_namespace_pmem(dev)) {
248 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
249 resource_size_t size = resource_size(&nspm->nsio.res);
251 if (size == 0 && nspm->uuid)
252 /* delete allocation */;
253 else if (!nspm->uuid)
256 return nd_pmem_namespace_label_update(nd_region, nspm, size);
261 static ssize_t alt_name_store(struct device *dev,
262 struct device_attribute *attr, const char *buf, size_t len)
264 struct nd_region *nd_region = to_nd_region(dev->parent);
268 nvdimm_bus_lock(dev);
269 wait_nvdimm_bus_probe_idle(dev);
270 rc = __alt_name_store(dev, buf, len);
272 rc = nd_namespace_label_update(nd_region, dev);
273 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
274 nvdimm_bus_unlock(dev);
275 nd_device_unlock(dev);
277 return rc < 0 ? rc : len;
280 static ssize_t alt_name_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
285 if (is_namespace_pmem(dev)) {
286 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
288 ns_altname = nspm->alt_name;
292 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
294 static DEVICE_ATTR_RW(alt_name);
296 static int scan_free(struct nd_region *nd_region,
297 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
300 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
304 struct resource *res, *last;
307 for_each_dpa_resource(ndd, res)
308 if (strcmp(res->name, label_id->id) == 0)
314 if (n >= resource_size(res)) {
315 n -= resource_size(res);
316 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
317 nvdimm_free_dpa(ndd, res);
318 /* retry with last resource deleted */
322 rc = adjust_resource(res, res->start, resource_size(res) - n);
324 res->flags |= DPA_RESOURCE_ADJUSTED;
325 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
333 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
334 * @nd_region: the set of dimms to reclaim @n bytes from
335 * @label_id: unique identifier for the namespace consuming this dpa range
336 * @n: number of bytes per-dimm to release
338 * Assumes resources are ordered. Starting from the end try to
339 * adjust_resource() the allocation to @n, but if @n is larger than the
340 * allocation delete it and find the 'new' last allocation in the label
343 static int shrink_dpa_allocation(struct nd_region *nd_region,
344 struct nd_label_id *label_id, resource_size_t n)
348 for (i = 0; i < nd_region->ndr_mappings; i++) {
349 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
352 rc = scan_free(nd_region, nd_mapping, label_id, n);
360 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
361 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
364 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
365 struct resource *res;
368 /* first resource allocation for this label-id or dimm */
369 res = nvdimm_allocate_dpa(ndd, label_id, nd_mapping->start, n);
373 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
379 * space_valid() - validate free dpa space against constraints
380 * @nd_region: hosting region of the free space
381 * @ndd: dimm device data for debug
382 * @label_id: namespace id to allocate space
383 * @prev: potential allocation that precedes free space
384 * @next: allocation that follows the given free space range
385 * @exist: first allocation with same id in the mapping
386 * @n: range that must satisfied for pmem allocations
387 * @valid: free space range to validate
389 * BLK-space is valid as long as it does not precede a PMEM
390 * allocation in a given region. PMEM-space must be contiguous
391 * and adjacent to an existing existing allocation (if one
392 * exists). If reserving PMEM any space is valid.
394 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
395 struct nd_label_id *label_id, struct resource *prev,
396 struct resource *next, struct resource *exist,
397 resource_size_t n, struct resource *valid)
399 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
402 align = nd_region->align / nd_region->ndr_mappings;
403 valid->start = ALIGN(valid->start, align);
404 valid->end = ALIGN_DOWN(valid->end + 1, align) - 1;
406 if (valid->start >= valid->end)
412 /* allocation needs to be contiguous, so this is all or nothing */
413 if (resource_size(valid) < n)
416 /* we've got all the space we need and no existing allocation */
420 /* allocation needs to be contiguous with the existing namespace */
421 if (valid->start == exist->end + 1
422 || valid->end == exist->start - 1)
426 /* truncate @valid size to 0 */
427 valid->end = valid->start - 1;
431 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
434 static resource_size_t scan_allocate(struct nd_region *nd_region,
435 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
438 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
439 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
440 struct resource *res, *exist = NULL, valid;
441 const resource_size_t to_allocate = n;
444 for_each_dpa_resource(ndd, res)
445 if (strcmp(label_id->id, res->name) == 0)
448 valid.start = nd_mapping->start;
449 valid.end = mapping_end;
450 valid.name = "free space";
453 for_each_dpa_resource(ndd, res) {
454 struct resource *next = res->sibling, *new_res = NULL;
455 resource_size_t allocate, available = 0;
456 enum alloc_loc loc = ALLOC_ERR;
460 /* ignore resources outside this nd_mapping */
461 if (res->start > mapping_end)
463 if (res->end < nd_mapping->start)
466 /* space at the beginning of the mapping */
467 if (!first++ && res->start > nd_mapping->start) {
468 valid.start = nd_mapping->start;
469 valid.end = res->start - 1;
470 space_valid(nd_region, ndd, label_id, NULL, next, exist,
471 to_allocate, &valid);
472 available = resource_size(&valid);
477 /* space between allocations */
479 valid.start = res->start + resource_size(res);
480 valid.end = min(mapping_end, next->start - 1);
481 space_valid(nd_region, ndd, label_id, res, next, exist,
482 to_allocate, &valid);
483 available = resource_size(&valid);
488 /* space at the end of the mapping */
490 valid.start = res->start + resource_size(res);
491 valid.end = mapping_end;
492 space_valid(nd_region, ndd, label_id, res, next, exist,
493 to_allocate, &valid);
494 available = resource_size(&valid);
499 if (!loc || !available)
501 allocate = min(available, n);
504 if (strcmp(res->name, label_id->id) == 0) {
505 /* adjust current resource up */
506 rc = adjust_resource(res, res->start - allocate,
507 resource_size(res) + allocate);
508 action = "cur grow up";
513 if (strcmp(next->name, label_id->id) == 0) {
514 /* adjust next resource up */
515 rc = adjust_resource(next, next->start
516 - allocate, resource_size(next)
519 action = "next grow up";
520 } else if (strcmp(res->name, label_id->id) == 0) {
521 action = "grow down";
526 if (strcmp(res->name, label_id->id) == 0)
527 action = "grow down";
535 if (strcmp(action, "allocate") == 0) {
536 new_res = nvdimm_allocate_dpa(ndd, label_id,
537 valid.start, allocate);
540 } else if (strcmp(action, "grow down") == 0) {
541 /* adjust current resource down */
542 rc = adjust_resource(res, res->start, resource_size(res)
545 res->flags |= DPA_RESOURCE_ADJUSTED;
551 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
560 * Retry scan with newly inserted resources.
561 * For example, if we did an ALLOC_BEFORE
562 * insertion there may also have been space
563 * available for an ALLOC_AFTER insertion, so we
564 * need to check this same resource again
571 if (n == to_allocate)
572 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
576 static int merge_dpa(struct nd_region *nd_region,
577 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
579 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
580 struct resource *res;
582 if (strncmp("pmem", label_id->id, 4) == 0)
585 for_each_dpa_resource(ndd, res) {
587 struct resource *next = res->sibling;
588 resource_size_t end = res->start + resource_size(res);
590 if (!next || strcmp(res->name, label_id->id) != 0
591 || strcmp(next->name, label_id->id) != 0
592 || end != next->start)
594 end += resource_size(next);
595 nvdimm_free_dpa(ndd, next);
596 rc = adjust_resource(res, res->start, end - res->start);
597 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
600 res->flags |= DPA_RESOURCE_ADJUSTED;
607 int __reserve_free_pmem(struct device *dev, void *data)
609 struct nvdimm *nvdimm = data;
610 struct nd_region *nd_region;
611 struct nd_label_id label_id;
617 nd_region = to_nd_region(dev);
618 if (nd_region->ndr_mappings == 0)
621 memset(&label_id, 0, sizeof(label_id));
622 strcat(label_id.id, "pmem-reserve");
623 for (i = 0; i < nd_region->ndr_mappings; i++) {
624 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
625 resource_size_t n, rem = 0;
627 if (nd_mapping->nvdimm != nvdimm)
630 n = nd_pmem_available_dpa(nd_region, nd_mapping);
633 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
634 dev_WARN_ONCE(&nd_region->dev, rem,
635 "pmem reserve underrun: %#llx of %#llx bytes\n",
636 (unsigned long long) n - rem,
637 (unsigned long long) n);
638 return rem ? -ENXIO : 0;
644 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
645 struct nd_mapping *nd_mapping)
647 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
648 struct resource *res, *_res;
650 for_each_dpa_resource_safe(ndd, res, _res)
651 if (strcmp(res->name, "pmem-reserve") == 0)
652 nvdimm_free_dpa(ndd, res);
656 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
657 * @nd_region: the set of dimms to allocate @n more bytes from
658 * @label_id: unique identifier for the namespace consuming this dpa range
659 * @n: number of bytes per-dimm to add to the existing allocation
661 * Assumes resources are ordered. For BLK regions, first consume
662 * BLK-only available DPA free space, then consume PMEM-aliased DPA
663 * space starting at the highest DPA. For PMEM regions start
664 * allocations from the start of an interleave set and end at the first
665 * BLK allocation or the end of the interleave set, whichever comes
668 static int grow_dpa_allocation(struct nd_region *nd_region,
669 struct nd_label_id *label_id, resource_size_t n)
673 for (i = 0; i < nd_region->ndr_mappings; i++) {
674 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
675 resource_size_t rem = n;
678 rem = scan_allocate(nd_region, nd_mapping, label_id, rem);
679 dev_WARN_ONCE(&nd_region->dev, rem,
680 "allocation underrun: %#llx of %#llx bytes\n",
681 (unsigned long long) n - rem,
682 (unsigned long long) n);
686 rc = merge_dpa(nd_region, nd_mapping, label_id);
694 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
695 struct nd_namespace_pmem *nspm, resource_size_t size)
697 struct resource *res = &nspm->nsio.res;
698 resource_size_t offset = 0;
700 if (size && !nspm->uuid) {
705 if (size && nspm->uuid) {
706 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
707 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
708 struct nd_label_id label_id;
709 struct resource *res;
716 nd_label_gen_id(&label_id, nspm->uuid, 0);
718 /* calculate a spa offset from the dpa allocation offset */
719 for_each_dpa_resource(ndd, res)
720 if (strcmp(res->name, label_id.id) == 0) {
721 offset = (res->start - nd_mapping->start)
722 * nd_region->ndr_mappings;
731 res->start = nd_region->ndr_start + offset;
732 res->end = res->start + size - 1;
735 static bool uuid_not_set(const uuid_t *uuid, struct device *dev,
739 dev_dbg(dev, "%s: uuid not set\n", where);
745 static ssize_t __size_store(struct device *dev, unsigned long long val)
747 resource_size_t allocated = 0, available = 0;
748 struct nd_region *nd_region = to_nd_region(dev->parent);
749 struct nd_namespace_common *ndns = to_ndns(dev);
750 struct nd_mapping *nd_mapping;
751 struct nvdimm_drvdata *ndd;
752 struct nd_label_id label_id;
753 u32 flags = 0, remainder;
757 if (dev->driver || ndns->claim)
760 if (is_namespace_pmem(dev)) {
761 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
768 * We need a uuid for the allocation-label and dimm(s) on which
769 * to store the label.
771 if (uuid_not_set(uuid, dev, __func__))
773 if (nd_region->ndr_mappings == 0) {
774 dev_dbg(dev, "not associated with dimm(s)\n");
778 div_u64_rem(val, nd_region->align, &remainder);
780 dev_dbg(dev, "%llu is not %ldK aligned\n", val,
781 nd_region->align / SZ_1K);
785 nd_label_gen_id(&label_id, uuid, flags);
786 for (i = 0; i < nd_region->ndr_mappings; i++) {
787 nd_mapping = &nd_region->mapping[i];
788 ndd = to_ndd(nd_mapping);
791 * All dimms in an interleave set, need to be enabled
792 * for the size to be changed.
797 allocated += nvdimm_allocated_dpa(ndd, &label_id);
799 available = nd_region_allocatable_dpa(nd_region);
801 if (val > available + allocated)
804 if (val == allocated)
807 val = div_u64(val, nd_region->ndr_mappings);
808 allocated = div_u64(allocated, nd_region->ndr_mappings);
810 rc = shrink_dpa_allocation(nd_region, &label_id,
813 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
818 if (is_namespace_pmem(dev)) {
819 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
821 nd_namespace_pmem_set_resource(nd_region, nspm,
822 val * nd_region->ndr_mappings);
826 * Try to delete the namespace if we deleted all of its
827 * allocation, this is not the seed or 0th device for the
828 * region, and it is not actively claimed by a btt, pfn, or dax
831 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
832 nd_device_unregister(dev, ND_ASYNC);
837 static ssize_t size_store(struct device *dev,
838 struct device_attribute *attr, const char *buf, size_t len)
840 struct nd_region *nd_region = to_nd_region(dev->parent);
841 unsigned long long val;
842 uuid_t **uuid = NULL;
845 rc = kstrtoull(buf, 0, &val);
850 nvdimm_bus_lock(dev);
851 wait_nvdimm_bus_probe_idle(dev);
852 rc = __size_store(dev, val);
854 rc = nd_namespace_label_update(nd_region, dev);
856 if (is_namespace_pmem(dev)) {
857 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
862 if (rc == 0 && val == 0 && uuid) {
863 /* setting size zero == 'delete namespace' */
868 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
870 nvdimm_bus_unlock(dev);
871 nd_device_unlock(dev);
873 return rc < 0 ? rc : len;
876 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
878 struct device *dev = &ndns->dev;
880 if (is_namespace_pmem(dev)) {
881 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
883 return resource_size(&nspm->nsio.res);
884 } else if (is_namespace_io(dev)) {
885 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
887 return resource_size(&nsio->res);
889 WARN_ONCE(1, "unknown namespace type\n");
893 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
895 resource_size_t size;
897 nvdimm_bus_lock(&ndns->dev);
898 size = __nvdimm_namespace_capacity(ndns);
899 nvdimm_bus_unlock(&ndns->dev);
903 EXPORT_SYMBOL(nvdimm_namespace_capacity);
905 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
909 struct device *dev = &ndns->dev;
910 struct nd_region *nd_region = to_nd_region(dev->parent);
912 for (i = 0; i < nd_region->ndr_mappings; i++) {
913 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
914 struct nvdimm *nvdimm = nd_mapping->nvdimm;
916 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
917 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
923 EXPORT_SYMBOL(nvdimm_namespace_locked);
925 static ssize_t size_show(struct device *dev,
926 struct device_attribute *attr, char *buf)
928 return sprintf(buf, "%llu\n", (unsigned long long)
929 nvdimm_namespace_capacity(to_ndns(dev)));
931 static DEVICE_ATTR(size, 0444, size_show, size_store);
933 static uuid_t *namespace_to_uuid(struct device *dev)
935 if (is_namespace_pmem(dev)) {
936 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
940 return ERR_PTR(-ENXIO);
943 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
946 uuid_t *uuid = namespace_to_uuid(dev);
949 return PTR_ERR(uuid);
951 return sprintf(buf, "%pUb\n", uuid);
952 return sprintf(buf, "\n");
956 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
957 * @nd_region: parent region so we can updates all dimms in the set
958 * @dev: namespace type for generating label_id
959 * @new_uuid: incoming uuid
960 * @old_uuid: reference to the uuid storage location in the namespace object
962 static int namespace_update_uuid(struct nd_region *nd_region,
963 struct device *dev, uuid_t *new_uuid,
966 struct nd_label_id old_label_id;
967 struct nd_label_id new_label_id;
970 if (!nd_is_uuid_unique(dev, new_uuid))
973 if (*old_uuid == NULL)
977 * If we've already written a label with this uuid, then it's
978 * too late to rename because we can't reliably update the uuid
979 * without losing the old namespace. Userspace must delete this
980 * namespace to abandon the old uuid.
982 for (i = 0; i < nd_region->ndr_mappings; i++) {
983 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
986 * This check by itself is sufficient because old_uuid
987 * would be NULL above if this uuid did not exist in the
988 * currently written set.
990 * FIXME: can we delete uuid with zero dpa allocated?
992 if (list_empty(&nd_mapping->labels))
996 nd_label_gen_id(&old_label_id, *old_uuid, 0);
997 nd_label_gen_id(&new_label_id, new_uuid, 0);
998 for (i = 0; i < nd_region->ndr_mappings; i++) {
999 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1000 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1001 struct nd_label_ent *label_ent;
1002 struct resource *res;
1004 for_each_dpa_resource(ndd, res)
1005 if (strcmp(res->name, old_label_id.id) == 0)
1006 sprintf((void *) res->name, "%s",
1009 mutex_lock(&nd_mapping->lock);
1010 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1011 struct nd_namespace_label *nd_label = label_ent->label;
1012 struct nd_label_id label_id;
1017 nsl_get_uuid(ndd, nd_label, &uuid);
1018 nd_label_gen_id(&label_id, &uuid,
1019 nsl_get_flags(ndd, nd_label));
1020 if (strcmp(old_label_id.id, label_id.id) == 0)
1021 set_bit(ND_LABEL_REAP, &label_ent->flags);
1023 mutex_unlock(&nd_mapping->lock);
1027 *old_uuid = new_uuid;
1031 static ssize_t uuid_store(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t len)
1034 struct nd_region *nd_region = to_nd_region(dev->parent);
1035 uuid_t *uuid = NULL;
1039 if (is_namespace_pmem(dev)) {
1040 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1042 ns_uuid = &nspm->uuid;
1046 nd_device_lock(dev);
1047 nvdimm_bus_lock(dev);
1048 wait_nvdimm_bus_probe_idle(dev);
1049 if (to_ndns(dev)->claim)
1052 rc = nd_uuid_store(dev, &uuid, buf, len);
1054 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1056 rc = nd_namespace_label_update(nd_region, dev);
1059 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1060 buf[len - 1] == '\n' ? "" : "\n");
1061 nvdimm_bus_unlock(dev);
1062 nd_device_unlock(dev);
1064 return rc < 0 ? rc : len;
1066 static DEVICE_ATTR_RW(uuid);
1068 static ssize_t resource_show(struct device *dev,
1069 struct device_attribute *attr, char *buf)
1071 struct resource *res;
1073 if (is_namespace_pmem(dev)) {
1074 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1076 res = &nspm->nsio.res;
1077 } else if (is_namespace_io(dev)) {
1078 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1084 /* no address to convey if the namespace has no allocation */
1085 if (resource_size(res) == 0)
1087 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1089 static DEVICE_ATTR_ADMIN_RO(resource);
1091 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1093 static ssize_t sector_size_show(struct device *dev,
1094 struct device_attribute *attr, char *buf)
1096 if (is_namespace_pmem(dev)) {
1097 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1099 return nd_size_select_show(nspm->lbasize,
1100 pmem_lbasize_supported, buf);
1105 static ssize_t sector_size_store(struct device *dev,
1106 struct device_attribute *attr, const char *buf, size_t len)
1108 struct nd_region *nd_region = to_nd_region(dev->parent);
1109 const unsigned long *supported;
1110 unsigned long *lbasize;
1113 if (is_namespace_pmem(dev)) {
1114 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1116 lbasize = &nspm->lbasize;
1117 supported = pmem_lbasize_supported;
1121 nd_device_lock(dev);
1122 nvdimm_bus_lock(dev);
1123 if (to_ndns(dev)->claim)
1126 rc = nd_size_select_store(dev, buf, lbasize, supported);
1128 rc = nd_namespace_label_update(nd_region, dev);
1129 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1130 buf, buf[len - 1] == '\n' ? "" : "\n");
1131 nvdimm_bus_unlock(dev);
1132 nd_device_unlock(dev);
1134 return rc ? rc : len;
1136 static DEVICE_ATTR_RW(sector_size);
1138 static ssize_t dpa_extents_show(struct device *dev,
1139 struct device_attribute *attr, char *buf)
1141 struct nd_region *nd_region = to_nd_region(dev->parent);
1142 struct nd_label_id label_id;
1143 uuid_t *uuid = NULL;
1147 nvdimm_bus_lock(dev);
1148 if (is_namespace_pmem(dev)) {
1149 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1158 nd_label_gen_id(&label_id, uuid, flags);
1159 for (i = 0; i < nd_region->ndr_mappings; i++) {
1160 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1161 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1162 struct resource *res;
1164 for_each_dpa_resource(ndd, res)
1165 if (strcmp(res->name, label_id.id) == 0)
1169 nvdimm_bus_unlock(dev);
1171 return sprintf(buf, "%d\n", count);
1173 static DEVICE_ATTR_RO(dpa_extents);
1175 static int btt_claim_class(struct device *dev)
1177 struct nd_region *nd_region = to_nd_region(dev->parent);
1178 int i, loop_bitmask = 0;
1180 for (i = 0; i < nd_region->ndr_mappings; i++) {
1181 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1182 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1183 struct nd_namespace_index *nsindex;
1186 * If any of the DIMMs do not support labels the only
1187 * possible BTT format is v1.
1194 nsindex = to_namespace_index(ndd, ndd->ns_current);
1195 if (nsindex == NULL)
1198 /* check whether existing labels are v1.1 or v1.2 */
1199 if (__le16_to_cpu(nsindex->major) == 1
1200 && __le16_to_cpu(nsindex->minor) == 1)
1207 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1208 * block is found, a v1.1 label for any mapping will set bit 1, and a
1209 * v1.2 label will set bit 2.
1211 * At the end of the loop, at most one of the three bits must be set.
1212 * If multiple bits were set, it means the different mappings disagree
1213 * about their labels, and this must be cleaned up first.
1215 * If all the label index blocks are found to agree, nsindex of NULL
1216 * implies labels haven't been initialized yet, and when they will,
1217 * they will be of the 1.2 format, so we can assume BTT2.0
1219 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1220 * found, we enforce BTT2.0
1222 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1224 switch (loop_bitmask) {
1227 return NVDIMM_CCLASS_BTT;
1230 return NVDIMM_CCLASS_BTT2;
1236 static ssize_t holder_show(struct device *dev,
1237 struct device_attribute *attr, char *buf)
1239 struct nd_namespace_common *ndns = to_ndns(dev);
1242 nd_device_lock(dev);
1243 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1244 nd_device_unlock(dev);
1248 static DEVICE_ATTR_RO(holder);
1250 static int __holder_class_store(struct device *dev, const char *buf)
1252 struct nd_namespace_common *ndns = to_ndns(dev);
1254 if (dev->driver || ndns->claim)
1257 if (sysfs_streq(buf, "btt")) {
1258 int rc = btt_claim_class(dev);
1260 if (rc < NVDIMM_CCLASS_NONE)
1262 ndns->claim_class = rc;
1263 } else if (sysfs_streq(buf, "pfn"))
1264 ndns->claim_class = NVDIMM_CCLASS_PFN;
1265 else if (sysfs_streq(buf, "dax"))
1266 ndns->claim_class = NVDIMM_CCLASS_DAX;
1267 else if (sysfs_streq(buf, ""))
1268 ndns->claim_class = NVDIMM_CCLASS_NONE;
1275 static ssize_t holder_class_store(struct device *dev,
1276 struct device_attribute *attr, const char *buf, size_t len)
1278 struct nd_region *nd_region = to_nd_region(dev->parent);
1281 nd_device_lock(dev);
1282 nvdimm_bus_lock(dev);
1283 wait_nvdimm_bus_probe_idle(dev);
1284 rc = __holder_class_store(dev, buf);
1286 rc = nd_namespace_label_update(nd_region, dev);
1287 dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
1288 nvdimm_bus_unlock(dev);
1289 nd_device_unlock(dev);
1291 return rc < 0 ? rc : len;
1294 static ssize_t holder_class_show(struct device *dev,
1295 struct device_attribute *attr, char *buf)
1297 struct nd_namespace_common *ndns = to_ndns(dev);
1300 nd_device_lock(dev);
1301 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1302 rc = sprintf(buf, "\n");
1303 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1304 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1305 rc = sprintf(buf, "btt\n");
1306 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1307 rc = sprintf(buf, "pfn\n");
1308 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1309 rc = sprintf(buf, "dax\n");
1311 rc = sprintf(buf, "<unknown>\n");
1312 nd_device_unlock(dev);
1316 static DEVICE_ATTR_RW(holder_class);
1318 static ssize_t mode_show(struct device *dev,
1319 struct device_attribute *attr, char *buf)
1321 struct nd_namespace_common *ndns = to_ndns(dev);
1322 struct device *claim;
1326 nd_device_lock(dev);
1327 claim = ndns->claim;
1328 if (claim && is_nd_btt(claim))
1330 else if (claim && is_nd_pfn(claim))
1332 else if (claim && is_nd_dax(claim))
1334 else if (!claim && pmem_should_map_pages(dev))
1338 rc = sprintf(buf, "%s\n", mode);
1339 nd_device_unlock(dev);
1343 static DEVICE_ATTR_RO(mode);
1345 static ssize_t force_raw_store(struct device *dev,
1346 struct device_attribute *attr, const char *buf, size_t len)
1349 int rc = strtobool(buf, &force_raw);
1354 to_ndns(dev)->force_raw = force_raw;
1358 static ssize_t force_raw_show(struct device *dev,
1359 struct device_attribute *attr, char *buf)
1361 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1363 static DEVICE_ATTR_RW(force_raw);
1365 static struct attribute *nd_namespace_attributes[] = {
1366 &dev_attr_nstype.attr,
1367 &dev_attr_size.attr,
1368 &dev_attr_mode.attr,
1369 &dev_attr_uuid.attr,
1370 &dev_attr_holder.attr,
1371 &dev_attr_resource.attr,
1372 &dev_attr_alt_name.attr,
1373 &dev_attr_force_raw.attr,
1374 &dev_attr_sector_size.attr,
1375 &dev_attr_dpa_extents.attr,
1376 &dev_attr_holder_class.attr,
1380 static umode_t namespace_visible(struct kobject *kobj,
1381 struct attribute *a, int n)
1383 struct device *dev = container_of(kobj, struct device, kobj);
1385 if (is_namespace_pmem(dev)) {
1386 if (a == &dev_attr_size.attr)
1392 /* base is_namespace_io() attributes */
1393 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr ||
1394 a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr ||
1395 a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr ||
1396 a == &dev_attr_resource.attr)
1402 static struct attribute_group nd_namespace_attribute_group = {
1403 .attrs = nd_namespace_attributes,
1404 .is_visible = namespace_visible,
1407 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1408 &nd_device_attribute_group,
1409 &nd_namespace_attribute_group,
1410 &nd_numa_attribute_group,
1414 static const struct device_type namespace_io_device_type = {
1415 .name = "nd_namespace_io",
1416 .release = namespace_io_release,
1417 .groups = nd_namespace_attribute_groups,
1420 static const struct device_type namespace_pmem_device_type = {
1421 .name = "nd_namespace_pmem",
1422 .release = namespace_pmem_release,
1423 .groups = nd_namespace_attribute_groups,
1426 static bool is_namespace_pmem(const struct device *dev)
1428 return dev ? dev->type == &namespace_pmem_device_type : false;
1431 static bool is_namespace_io(const struct device *dev)
1433 return dev ? dev->type == &namespace_io_device_type : false;
1436 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1438 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1439 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1440 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1441 struct nd_namespace_common *ndns = NULL;
1442 resource_size_t size;
1444 if (nd_btt || nd_pfn || nd_dax) {
1446 ndns = nd_btt->ndns;
1448 ndns = nd_pfn->ndns;
1450 ndns = nd_dax->nd_pfn.ndns;
1453 return ERR_PTR(-ENODEV);
1456 * Flush any in-progess probes / removals in the driver
1457 * for the raw personality of this namespace.
1459 nd_device_lock(&ndns->dev);
1460 nd_device_unlock(&ndns->dev);
1461 if (ndns->dev.driver) {
1462 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1464 return ERR_PTR(-EBUSY);
1466 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1467 "host (%s) vs claim (%s) mismatch\n",
1469 dev_name(ndns->claim)))
1470 return ERR_PTR(-ENXIO);
1472 ndns = to_ndns(dev);
1474 dev_dbg(dev, "claimed by %s, failing probe\n",
1475 dev_name(ndns->claim));
1477 return ERR_PTR(-ENXIO);
1481 if (nvdimm_namespace_locked(ndns))
1482 return ERR_PTR(-EACCES);
1484 size = nvdimm_namespace_capacity(ndns);
1485 if (size < ND_MIN_NAMESPACE_SIZE) {
1486 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1487 &size, ND_MIN_NAMESPACE_SIZE);
1488 return ERR_PTR(-ENODEV);
1492 * Note, alignment validation for fsdax and devdax mode
1493 * namespaces happens in nd_pfn_validate() where infoblock
1494 * padding parameters can be applied.
1496 if (pmem_should_map_pages(dev)) {
1497 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
1498 struct resource *res = &nsio->res;
1500 if (!IS_ALIGNED(res->start | (res->end + 1),
1501 memremap_compat_align())) {
1502 dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
1503 return ERR_PTR(-EOPNOTSUPP);
1507 if (is_namespace_pmem(&ndns->dev)) {
1508 struct nd_namespace_pmem *nspm;
1510 nspm = to_nd_namespace_pmem(&ndns->dev);
1511 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1512 return ERR_PTR(-ENODEV);
1517 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1519 int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1520 resource_size_t size)
1522 return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1524 EXPORT_SYMBOL_GPL(devm_namespace_enable);
1526 void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1528 devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1530 EXPORT_SYMBOL_GPL(devm_namespace_disable);
1532 static struct device **create_namespace_io(struct nd_region *nd_region)
1534 struct nd_namespace_io *nsio;
1535 struct device *dev, **devs;
1536 struct resource *res;
1538 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1542 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1548 dev = &nsio->common.dev;
1549 dev->type = &namespace_io_device_type;
1550 dev->parent = &nd_region->dev;
1552 res->name = dev_name(&nd_region->dev);
1553 res->flags = IORESOURCE_MEM;
1554 res->start = nd_region->ndr_start;
1555 res->end = res->start + nd_region->ndr_size - 1;
1561 static bool has_uuid_at_pos(struct nd_region *nd_region, const uuid_t *uuid,
1562 u64 cookie, u16 pos)
1564 struct nd_namespace_label *found = NULL;
1567 for (i = 0; i < nd_region->ndr_mappings; i++) {
1568 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1569 struct nd_interleave_set *nd_set = nd_region->nd_set;
1570 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1571 struct nd_label_ent *label_ent;
1572 bool found_uuid = false;
1574 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1575 struct nd_namespace_label *nd_label = label_ent->label;
1580 position = nsl_get_position(ndd, nd_label);
1582 if (!nsl_validate_isetcookie(ndd, nd_label, cookie))
1585 if (!nsl_uuid_equal(ndd, nd_label, uuid))
1588 if (!nsl_validate_type_guid(ndd, nd_label,
1589 &nd_set->type_guid))
1593 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1597 if (!nsl_validate_nlabel(nd_region, ndd, nd_label))
1599 if (position != pos)
1607 return found != NULL;
1610 static int select_pmem_id(struct nd_region *nd_region, const uuid_t *pmem_id)
1617 for (i = 0; i < nd_region->ndr_mappings; i++) {
1618 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1619 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1620 struct nd_namespace_label *nd_label = NULL;
1621 u64 hw_start, hw_end, pmem_start, pmem_end;
1622 struct nd_label_ent *label_ent;
1624 lockdep_assert_held(&nd_mapping->lock);
1625 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1626 nd_label = label_ent->label;
1629 if (nsl_uuid_equal(ndd, nd_label, pmem_id))
1640 * Check that this label is compliant with the dpa
1641 * range published in NFIT
1643 hw_start = nd_mapping->start;
1644 hw_end = hw_start + nd_mapping->size;
1645 pmem_start = nsl_get_dpa(ndd, nd_label);
1646 pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label);
1647 if (pmem_start >= hw_start && pmem_start < hw_end
1648 && pmem_end <= hw_end && pmem_end > hw_start)
1651 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1653 nsl_uuid_raw(ndd, nd_label));
1657 /* move recently validated label to the front of the list */
1658 list_move(&label_ent->list, &nd_mapping->labels);
1664 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1665 * @nd_region: region with mappings to validate
1666 * @nspm: target namespace to create
1667 * @nd_label: target pmem namespace label to evaluate
1669 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1670 struct nd_mapping *nd_mapping,
1671 struct nd_namespace_label *nd_label)
1673 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1674 struct nd_namespace_index *nsindex =
1675 to_namespace_index(ndd, ndd->ns_current);
1676 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1677 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1678 struct nd_label_ent *label_ent;
1679 struct nd_namespace_pmem *nspm;
1680 resource_size_t size = 0;
1681 struct resource *res;
1688 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1689 return ERR_PTR(-ENXIO);
1692 if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) {
1693 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1694 nsl_uuid_raw(ndd, nd_label));
1695 if (!nsl_validate_isetcookie(ndd, nd_label, altcookie))
1696 return ERR_PTR(-EAGAIN);
1698 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1699 nsl_uuid_raw(ndd, nd_label));
1702 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1704 return ERR_PTR(-ENOMEM);
1707 dev = &nspm->nsio.common.dev;
1708 dev->type = &namespace_pmem_device_type;
1709 dev->parent = &nd_region->dev;
1710 res = &nspm->nsio.res;
1711 res->name = dev_name(&nd_region->dev);
1712 res->flags = IORESOURCE_MEM;
1714 for (i = 0; i < nd_region->ndr_mappings; i++) {
1717 nsl_get_uuid(ndd, nd_label, &uuid);
1718 if (has_uuid_at_pos(nd_region, &uuid, cookie, i))
1720 if (has_uuid_at_pos(nd_region, &uuid, altcookie, i))
1725 if (i < nd_region->ndr_mappings) {
1726 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1729 * Give up if we don't find an instance of a uuid at each
1730 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1731 * find a dimm with two instances of the same uuid.
1733 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1734 nvdimm_name(nvdimm), nsl_uuid_raw(ndd, nd_label));
1740 * Fix up each mapping's 'labels' to have the validated pmem label for
1741 * that position at labels[0], and NULL at labels[1]. In the process,
1742 * check that the namespace aligns with interleave-set.
1744 nsl_get_uuid(ndd, nd_label, &uuid);
1745 rc = select_pmem_id(nd_region, &uuid);
1749 /* Calculate total size and populate namespace properties from label0 */
1750 for (i = 0; i < nd_region->ndr_mappings; i++) {
1751 struct nd_namespace_label *label0;
1752 struct nvdimm_drvdata *ndd;
1754 nd_mapping = &nd_region->mapping[i];
1755 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1756 typeof(*label_ent), list);
1757 label0 = label_ent ? label_ent->label : NULL;
1764 ndd = to_ndd(nd_mapping);
1765 size += nsl_get_rawsize(ndd, label0);
1766 if (nsl_get_position(ndd, label0) != 0)
1768 WARN_ON(nspm->alt_name || nspm->uuid);
1769 nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0),
1770 NSLABEL_NAME_LEN, GFP_KERNEL);
1771 nsl_get_uuid(ndd, label0, &uuid);
1772 nspm->uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
1773 nspm->lbasize = nsl_get_lbasize(ndd, label0);
1774 nspm->nsio.common.claim_class =
1775 nsl_get_claim_class(ndd, label0);
1778 if (!nspm->alt_name || !nspm->uuid) {
1783 nd_namespace_pmem_set_resource(nd_region, nspm, size);
1787 namespace_pmem_release(dev);
1790 dev_dbg(&nd_region->dev, "invalid label(s)\n");
1793 dev_dbg(&nd_region->dev, "label not found\n");
1796 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
1802 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
1804 struct nd_namespace_pmem *nspm;
1805 struct resource *res;
1808 if (!is_memory(&nd_region->dev))
1811 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1815 dev = &nspm->nsio.common.dev;
1816 dev->type = &namespace_pmem_device_type;
1817 dev->parent = &nd_region->dev;
1818 res = &nspm->nsio.res;
1819 res->name = dev_name(&nd_region->dev);
1820 res->flags = IORESOURCE_MEM;
1822 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1827 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
1828 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1833 void nd_region_create_ns_seed(struct nd_region *nd_region)
1835 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1837 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
1840 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
1843 * Seed creation failures are not fatal, provisioning is simply
1844 * disabled until memory becomes available
1846 if (!nd_region->ns_seed)
1847 dev_err(&nd_region->dev, "failed to create namespace\n");
1849 nd_device_register(nd_region->ns_seed);
1852 void nd_region_create_dax_seed(struct nd_region *nd_region)
1854 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1855 nd_region->dax_seed = nd_dax_create(nd_region);
1857 * Seed creation failures are not fatal, provisioning is simply
1858 * disabled until memory becomes available
1860 if (!nd_region->dax_seed)
1861 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1864 void nd_region_create_pfn_seed(struct nd_region *nd_region)
1866 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1867 nd_region->pfn_seed = nd_pfn_create(nd_region);
1869 * Seed creation failures are not fatal, provisioning is simply
1870 * disabled until memory becomes available
1872 if (!nd_region->pfn_seed)
1873 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1876 void nd_region_create_btt_seed(struct nd_region *nd_region)
1878 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1879 nd_region->btt_seed = nd_btt_create(nd_region);
1881 * Seed creation failures are not fatal, provisioning is simply
1882 * disabled until memory becomes available
1884 if (!nd_region->btt_seed)
1885 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1888 static int add_namespace_resource(struct nd_region *nd_region,
1889 struct nd_namespace_label *nd_label, struct device **devs,
1892 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1893 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1896 for (i = 0; i < count; i++) {
1897 uuid_t *uuid = namespace_to_uuid(devs[i]);
1904 if (!nsl_uuid_equal(ndd, nd_label, uuid))
1906 dev_err(&nd_region->dev,
1907 "error: conflicting extents for uuid: %pUb\n", uuid);
1914 static int cmp_dpa(const void *a, const void *b)
1916 const struct device *dev_a = *(const struct device **) a;
1917 const struct device *dev_b = *(const struct device **) b;
1918 struct nd_namespace_pmem *nspm_a, *nspm_b;
1920 if (is_namespace_io(dev_a))
1923 nspm_a = to_nd_namespace_pmem(dev_a);
1924 nspm_b = to_nd_namespace_pmem(dev_b);
1926 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
1927 sizeof(resource_size_t));
1930 static struct device **scan_labels(struct nd_region *nd_region)
1933 struct device *dev, **devs = NULL;
1934 struct nd_label_ent *label_ent, *e;
1935 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1936 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1937 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
1939 /* "safe" because create_namespace_pmem() might list_move() label_ent */
1940 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1941 struct nd_namespace_label *nd_label = label_ent->label;
1942 struct device **__devs;
1947 /* skip labels that describe extents outside of the region */
1948 if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start ||
1949 nsl_get_dpa(ndd, nd_label) > map_end)
1952 i = add_namespace_resource(nd_region, nd_label, devs, count);
1957 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1960 memcpy(__devs, devs, sizeof(dev) * count);
1964 dev = create_namespace_pmem(nd_region, nd_mapping, nd_label);
1966 switch (PTR_ERR(dev)) {
1968 /* skip invalid labels */
1971 /* fallthrough to seed creation */
1977 devs[count++] = dev;
1981 dev_dbg(&nd_region->dev, "discovered %d namespace%s\n", count,
1982 count == 1 ? "" : "s");
1985 struct nd_namespace_pmem *nspm;
1987 /* Publish a zero-sized namespace for userspace to configure. */
1988 nd_mapping_free_labels(nd_mapping);
1990 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1994 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1997 dev = &nspm->nsio.common.dev;
1998 dev->type = &namespace_pmem_device_type;
1999 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2000 dev->parent = &nd_region->dev;
2001 devs[count++] = dev;
2002 } else if (is_memory(&nd_region->dev)) {
2003 /* clean unselected labels */
2004 for (i = 0; i < nd_region->ndr_mappings; i++) {
2005 struct list_head *l, *e;
2009 nd_mapping = &nd_region->mapping[i];
2010 if (list_empty(&nd_mapping->labels)) {
2016 list_for_each_safe(l, e, &nd_mapping->labels) {
2019 list_move_tail(l, &list);
2021 nd_mapping_free_labels(nd_mapping);
2022 list_splice_init(&list, &nd_mapping->labels);
2027 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2033 for (i = 0; devs[i]; i++)
2034 namespace_pmem_release(devs[i]);
2040 static struct device **create_namespaces(struct nd_region *nd_region)
2042 struct nd_mapping *nd_mapping;
2043 struct device **devs;
2046 if (nd_region->ndr_mappings == 0)
2049 /* lock down all mappings while we scan labels */
2050 for (i = 0; i < nd_region->ndr_mappings; i++) {
2051 nd_mapping = &nd_region->mapping[i];
2052 mutex_lock_nested(&nd_mapping->lock, i);
2055 devs = scan_labels(nd_region);
2057 for (i = 0; i < nd_region->ndr_mappings; i++) {
2058 int reverse = nd_region->ndr_mappings - 1 - i;
2060 nd_mapping = &nd_region->mapping[reverse];
2061 mutex_unlock(&nd_mapping->lock);
2067 static void deactivate_labels(void *region)
2069 struct nd_region *nd_region = region;
2072 for (i = 0; i < nd_region->ndr_mappings; i++) {
2073 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2074 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2075 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2077 mutex_lock(&nd_mapping->lock);
2078 nd_mapping_free_labels(nd_mapping);
2079 mutex_unlock(&nd_mapping->lock);
2082 nd_mapping->ndd = NULL;
2084 atomic_dec(&nvdimm->busy);
2088 static int init_active_labels(struct nd_region *nd_region)
2092 for (i = 0; i < nd_region->ndr_mappings; i++) {
2093 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2094 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2095 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2096 struct nd_label_ent *label_ent;
2100 * If the dimm is disabled then we may need to prevent
2101 * the region from being activated.
2104 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2105 /* fail, label data may be unreadable */;
2106 else if (test_bit(NDD_LABELING, &nvdimm->flags))
2107 /* fail, labels needed to disambiguate dpa */;
2111 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2112 dev_name(&nd_mapping->nvdimm->dev),
2113 test_bit(NDD_LOCKED, &nvdimm->flags)
2114 ? "locked" : "disabled");
2118 nd_mapping->ndd = ndd;
2119 atomic_inc(&nvdimm->busy);
2122 count = nd_label_active_count(ndd);
2123 dev_dbg(ndd->dev, "count: %d\n", count);
2126 for (j = 0; j < count; j++) {
2127 struct nd_namespace_label *label;
2129 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2132 label = nd_label_active(ndd, j);
2133 label_ent->label = label;
2135 mutex_lock(&nd_mapping->lock);
2136 list_add_tail(&label_ent->list, &nd_mapping->labels);
2137 mutex_unlock(&nd_mapping->lock);
2144 if (i < nd_region->ndr_mappings)
2149 deactivate_labels(nd_region);
2153 return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
2157 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2159 struct device **devs = NULL;
2160 int i, rc = 0, type;
2163 nvdimm_bus_lock(&nd_region->dev);
2164 rc = init_active_labels(nd_region);
2166 nvdimm_bus_unlock(&nd_region->dev);
2170 type = nd_region_to_nstype(nd_region);
2172 case ND_DEVICE_NAMESPACE_IO:
2173 devs = create_namespace_io(nd_region);
2175 case ND_DEVICE_NAMESPACE_PMEM:
2176 devs = create_namespaces(nd_region);
2181 nvdimm_bus_unlock(&nd_region->dev);
2186 for (i = 0; devs[i]; i++) {
2187 struct device *dev = devs[i];
2190 if (type == ND_DEVICE_NAMESPACE_PMEM) {
2191 struct nd_namespace_pmem *nspm;
2193 nspm = to_nd_namespace_pmem(dev);
2194 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2202 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2203 nd_device_register(dev);
2206 nd_region->ns_seed = devs[0];
2211 for (j = i; devs[j]; j++) {
2212 struct device *dev = devs[j];
2214 device_initialize(dev);
2219 * All of the namespaces we tried to register failed, so
2220 * fail region activation.