1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/kstrtox.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/sort.h>
9 #include <linux/slab.h>
10 #include <linux/list.h>
17 static void namespace_io_release(struct device *dev)
19 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
24 static void namespace_pmem_release(struct device *dev)
26 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
27 struct nd_region *nd_region = to_nd_region(dev->parent);
30 ida_simple_remove(&nd_region->ns_ida, nspm->id);
31 kfree(nspm->alt_name);
36 static bool is_namespace_pmem(const struct device *dev);
37 static bool is_namespace_io(const struct device *dev);
39 static int is_uuid_busy(struct device *dev, void *data)
41 uuid_t *uuid1 = data, *uuid2 = NULL;
43 if (is_namespace_pmem(dev)) {
44 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
47 } else if (is_nd_btt(dev)) {
48 struct nd_btt *nd_btt = to_nd_btt(dev);
51 } else if (is_nd_pfn(dev)) {
52 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
57 if (uuid2 && uuid_equal(uuid1, uuid2))
63 static int is_namespace_uuid_busy(struct device *dev, void *data)
65 if (is_nd_region(dev))
66 return device_for_each_child(dev, data, is_uuid_busy);
71 * nd_is_uuid_unique - verify that no other namespace has @uuid
72 * @dev: any device on a nvdimm_bus
73 * @uuid: uuid to check
75 bool nd_is_uuid_unique(struct device *dev, uuid_t *uuid)
77 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
81 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
82 if (device_for_each_child(&nvdimm_bus->dev, uuid,
83 is_namespace_uuid_busy) != 0)
88 bool pmem_should_map_pages(struct device *dev)
90 struct nd_region *nd_region = to_nd_region(dev->parent);
91 struct nd_namespace_common *ndns = to_ndns(dev);
92 struct nd_namespace_io *nsio;
94 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
97 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
100 if (is_nd_pfn(dev) || is_nd_btt(dev))
106 nsio = to_nd_namespace_io(dev);
107 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
108 IORESOURCE_SYSTEM_RAM,
109 IORES_DESC_NONE) == REGION_MIXED)
112 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
114 EXPORT_SYMBOL(pmem_should_map_pages);
116 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
118 if (is_namespace_pmem(&ndns->dev)) {
119 struct nd_namespace_pmem *nspm;
121 nspm = to_nd_namespace_pmem(&ndns->dev);
122 if (nspm->lbasize == 0 || nspm->lbasize == 512)
124 else if (nspm->lbasize == 4096)
127 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
132 * There is no namespace label (is_namespace_io()), or the label
133 * indicates the default sector size.
137 EXPORT_SYMBOL(pmem_sector_size);
139 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
142 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
143 const char *suffix = NULL;
145 if (ndns->claim && is_nd_btt(ndns->claim))
148 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
151 if (is_namespace_pmem(&ndns->dev)) {
152 struct nd_namespace_pmem *nspm;
154 nspm = to_nd_namespace_pmem(&ndns->dev);
159 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
160 suffix ? suffix : "");
162 sprintf(name, "pmem%d%s", nd_region->id,
163 suffix ? suffix : "");
170 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
172 const uuid_t *nd_dev_to_uuid(struct device *dev)
174 if (dev && is_namespace_pmem(dev)) {
175 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
181 EXPORT_SYMBOL(nd_dev_to_uuid);
183 static ssize_t nstype_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct nd_region *nd_region = to_nd_region(dev->parent);
188 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
190 static DEVICE_ATTR_RO(nstype);
192 static ssize_t __alt_name_store(struct device *dev, const char *buf,
195 char *input, *pos, *alt_name, **ns_altname;
198 if (is_namespace_pmem(dev)) {
199 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
201 ns_altname = &nspm->alt_name;
205 if (dev->driver || to_ndns(dev)->claim)
208 input = kstrndup(buf, len, GFP_KERNEL);
213 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
218 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
224 *ns_altname = alt_name;
225 sprintf(*ns_altname, "%s", pos);
233 static int nd_namespace_label_update(struct nd_region *nd_region,
236 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
237 "namespace must be idle during label update\n");
238 if (dev->driver || to_ndns(dev)->claim)
242 * Only allow label writes that will result in a valid namespace
243 * or deletion of an existing namespace.
245 if (is_namespace_pmem(dev)) {
246 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
247 resource_size_t size = resource_size(&nspm->nsio.res);
249 if (size == 0 && nspm->uuid)
250 /* delete allocation */;
251 else if (!nspm->uuid)
254 return nd_pmem_namespace_label_update(nd_region, nspm, size);
259 static ssize_t alt_name_store(struct device *dev,
260 struct device_attribute *attr, const char *buf, size_t len)
262 struct nd_region *nd_region = to_nd_region(dev->parent);
266 nvdimm_bus_lock(dev);
267 wait_nvdimm_bus_probe_idle(dev);
268 rc = __alt_name_store(dev, buf, len);
270 rc = nd_namespace_label_update(nd_region, dev);
271 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
272 nvdimm_bus_unlock(dev);
275 return rc < 0 ? rc : len;
278 static ssize_t alt_name_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
283 if (is_namespace_pmem(dev)) {
284 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
286 ns_altname = nspm->alt_name;
290 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
292 static DEVICE_ATTR_RW(alt_name);
294 static int scan_free(struct nd_region *nd_region,
295 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
298 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
302 struct resource *res, *last;
305 for_each_dpa_resource(ndd, res)
306 if (strcmp(res->name, label_id->id) == 0)
312 if (n >= resource_size(res)) {
313 n -= resource_size(res);
314 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
315 nvdimm_free_dpa(ndd, res);
316 /* retry with last resource deleted */
320 rc = adjust_resource(res, res->start, resource_size(res) - n);
322 res->flags |= DPA_RESOURCE_ADJUSTED;
323 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
331 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
332 * @nd_region: the set of dimms to reclaim @n bytes from
333 * @label_id: unique identifier for the namespace consuming this dpa range
334 * @n: number of bytes per-dimm to release
336 * Assumes resources are ordered. Starting from the end try to
337 * adjust_resource() the allocation to @n, but if @n is larger than the
338 * allocation delete it and find the 'new' last allocation in the label
341 static int shrink_dpa_allocation(struct nd_region *nd_region,
342 struct nd_label_id *label_id, resource_size_t n)
346 for (i = 0; i < nd_region->ndr_mappings; i++) {
347 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
350 rc = scan_free(nd_region, nd_mapping, label_id, n);
358 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
359 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
362 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
363 struct resource *res;
366 /* first resource allocation for this label-id or dimm */
367 res = nvdimm_allocate_dpa(ndd, label_id, nd_mapping->start, n);
371 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
377 * space_valid() - validate free dpa space against constraints
378 * @nd_region: hosting region of the free space
379 * @ndd: dimm device data for debug
380 * @label_id: namespace id to allocate space
381 * @prev: potential allocation that precedes free space
382 * @next: allocation that follows the given free space range
383 * @exist: first allocation with same id in the mapping
384 * @n: range that must satisfied for pmem allocations
385 * @valid: free space range to validate
387 * BLK-space is valid as long as it does not precede a PMEM
388 * allocation in a given region. PMEM-space must be contiguous
389 * and adjacent to an existing allocation (if one
390 * exists). If reserving PMEM any space is valid.
392 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
393 struct nd_label_id *label_id, struct resource *prev,
394 struct resource *next, struct resource *exist,
395 resource_size_t n, struct resource *valid)
397 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
400 align = nd_region->align / nd_region->ndr_mappings;
401 valid->start = ALIGN(valid->start, align);
402 valid->end = ALIGN_DOWN(valid->end + 1, align) - 1;
404 if (valid->start >= valid->end)
410 /* allocation needs to be contiguous, so this is all or nothing */
411 if (resource_size(valid) < n)
414 /* we've got all the space we need and no existing allocation */
418 /* allocation needs to be contiguous with the existing namespace */
419 if (valid->start == exist->end + 1
420 || valid->end == exist->start - 1)
424 /* truncate @valid size to 0 */
425 valid->end = valid->start - 1;
429 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
432 static resource_size_t scan_allocate(struct nd_region *nd_region,
433 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
436 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
437 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
438 struct resource *res, *exist = NULL, valid;
439 const resource_size_t to_allocate = n;
442 for_each_dpa_resource(ndd, res)
443 if (strcmp(label_id->id, res->name) == 0)
446 valid.start = nd_mapping->start;
447 valid.end = mapping_end;
448 valid.name = "free space";
451 for_each_dpa_resource(ndd, res) {
452 struct resource *next = res->sibling, *new_res = NULL;
453 resource_size_t allocate, available = 0;
454 enum alloc_loc loc = ALLOC_ERR;
458 /* ignore resources outside this nd_mapping */
459 if (res->start > mapping_end)
461 if (res->end < nd_mapping->start)
464 /* space at the beginning of the mapping */
465 if (!first++ && res->start > nd_mapping->start) {
466 valid.start = nd_mapping->start;
467 valid.end = res->start - 1;
468 space_valid(nd_region, ndd, label_id, NULL, next, exist,
469 to_allocate, &valid);
470 available = resource_size(&valid);
475 /* space between allocations */
477 valid.start = res->start + resource_size(res);
478 valid.end = min(mapping_end, next->start - 1);
479 space_valid(nd_region, ndd, label_id, res, next, exist,
480 to_allocate, &valid);
481 available = resource_size(&valid);
486 /* space at the end of the mapping */
488 valid.start = res->start + resource_size(res);
489 valid.end = mapping_end;
490 space_valid(nd_region, ndd, label_id, res, next, exist,
491 to_allocate, &valid);
492 available = resource_size(&valid);
497 if (!loc || !available)
499 allocate = min(available, n);
502 if (strcmp(res->name, label_id->id) == 0) {
503 /* adjust current resource up */
504 rc = adjust_resource(res, res->start - allocate,
505 resource_size(res) + allocate);
506 action = "cur grow up";
511 if (strcmp(next->name, label_id->id) == 0) {
512 /* adjust next resource up */
513 rc = adjust_resource(next, next->start
514 - allocate, resource_size(next)
517 action = "next grow up";
518 } else if (strcmp(res->name, label_id->id) == 0) {
519 action = "grow down";
524 if (strcmp(res->name, label_id->id) == 0)
525 action = "grow down";
533 if (strcmp(action, "allocate") == 0) {
534 new_res = nvdimm_allocate_dpa(ndd, label_id,
535 valid.start, allocate);
538 } else if (strcmp(action, "grow down") == 0) {
539 /* adjust current resource down */
540 rc = adjust_resource(res, res->start, resource_size(res)
543 res->flags |= DPA_RESOURCE_ADJUSTED;
549 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
558 * Retry scan with newly inserted resources.
559 * For example, if we did an ALLOC_BEFORE
560 * insertion there may also have been space
561 * available for an ALLOC_AFTER insertion, so we
562 * need to check this same resource again
569 if (n == to_allocate)
570 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
574 static int merge_dpa(struct nd_region *nd_region,
575 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
577 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
578 struct resource *res;
580 if (strncmp("pmem", label_id->id, 4) == 0)
583 for_each_dpa_resource(ndd, res) {
585 struct resource *next = res->sibling;
586 resource_size_t end = res->start + resource_size(res);
588 if (!next || strcmp(res->name, label_id->id) != 0
589 || strcmp(next->name, label_id->id) != 0
590 || end != next->start)
592 end += resource_size(next);
593 nvdimm_free_dpa(ndd, next);
594 rc = adjust_resource(res, res->start, end - res->start);
595 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
598 res->flags |= DPA_RESOURCE_ADJUSTED;
605 int __reserve_free_pmem(struct device *dev, void *data)
607 struct nvdimm *nvdimm = data;
608 struct nd_region *nd_region;
609 struct nd_label_id label_id;
615 nd_region = to_nd_region(dev);
616 if (nd_region->ndr_mappings == 0)
619 memset(&label_id, 0, sizeof(label_id));
620 strcat(label_id.id, "pmem-reserve");
621 for (i = 0; i < nd_region->ndr_mappings; i++) {
622 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
623 resource_size_t n, rem = 0;
625 if (nd_mapping->nvdimm != nvdimm)
628 n = nd_pmem_available_dpa(nd_region, nd_mapping);
631 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
632 dev_WARN_ONCE(&nd_region->dev, rem,
633 "pmem reserve underrun: %#llx of %#llx bytes\n",
634 (unsigned long long) n - rem,
635 (unsigned long long) n);
636 return rem ? -ENXIO : 0;
642 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
643 struct nd_mapping *nd_mapping)
645 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
646 struct resource *res, *_res;
648 for_each_dpa_resource_safe(ndd, res, _res)
649 if (strcmp(res->name, "pmem-reserve") == 0)
650 nvdimm_free_dpa(ndd, res);
654 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
655 * @nd_region: the set of dimms to allocate @n more bytes from
656 * @label_id: unique identifier for the namespace consuming this dpa range
657 * @n: number of bytes per-dimm to add to the existing allocation
659 * Assumes resources are ordered. For BLK regions, first consume
660 * BLK-only available DPA free space, then consume PMEM-aliased DPA
661 * space starting at the highest DPA. For PMEM regions start
662 * allocations from the start of an interleave set and end at the first
663 * BLK allocation or the end of the interleave set, whichever comes
666 static int grow_dpa_allocation(struct nd_region *nd_region,
667 struct nd_label_id *label_id, resource_size_t n)
671 for (i = 0; i < nd_region->ndr_mappings; i++) {
672 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
673 resource_size_t rem = n;
676 rem = scan_allocate(nd_region, nd_mapping, label_id, rem);
677 dev_WARN_ONCE(&nd_region->dev, rem,
678 "allocation underrun: %#llx of %#llx bytes\n",
679 (unsigned long long) n - rem,
680 (unsigned long long) n);
684 rc = merge_dpa(nd_region, nd_mapping, label_id);
692 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
693 struct nd_namespace_pmem *nspm, resource_size_t size)
695 struct resource *res = &nspm->nsio.res;
696 resource_size_t offset = 0;
698 if (size && !nspm->uuid) {
703 if (size && nspm->uuid) {
704 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
705 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
706 struct nd_label_id label_id;
707 struct resource *res;
714 nd_label_gen_id(&label_id, nspm->uuid, 0);
716 /* calculate a spa offset from the dpa allocation offset */
717 for_each_dpa_resource(ndd, res)
718 if (strcmp(res->name, label_id.id) == 0) {
719 offset = (res->start - nd_mapping->start)
720 * nd_region->ndr_mappings;
729 res->start = nd_region->ndr_start + offset;
730 res->end = res->start + size - 1;
733 static bool uuid_not_set(const uuid_t *uuid, struct device *dev,
737 dev_dbg(dev, "%s: uuid not set\n", where);
743 static ssize_t __size_store(struct device *dev, unsigned long long val)
745 resource_size_t allocated = 0, available = 0;
746 struct nd_region *nd_region = to_nd_region(dev->parent);
747 struct nd_namespace_common *ndns = to_ndns(dev);
748 struct nd_mapping *nd_mapping;
749 struct nvdimm_drvdata *ndd;
750 struct nd_label_id label_id;
751 u32 flags = 0, remainder;
755 if (dev->driver || ndns->claim)
758 if (is_namespace_pmem(dev)) {
759 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
766 * We need a uuid for the allocation-label and dimm(s) on which
767 * to store the label.
769 if (uuid_not_set(uuid, dev, __func__))
771 if (nd_region->ndr_mappings == 0) {
772 dev_dbg(dev, "not associated with dimm(s)\n");
776 div_u64_rem(val, nd_region->align, &remainder);
778 dev_dbg(dev, "%llu is not %ldK aligned\n", val,
779 nd_region->align / SZ_1K);
783 nd_label_gen_id(&label_id, uuid, flags);
784 for (i = 0; i < nd_region->ndr_mappings; i++) {
785 nd_mapping = &nd_region->mapping[i];
786 ndd = to_ndd(nd_mapping);
789 * All dimms in an interleave set, need to be enabled
790 * for the size to be changed.
795 allocated += nvdimm_allocated_dpa(ndd, &label_id);
797 available = nd_region_allocatable_dpa(nd_region);
799 if (val > available + allocated)
802 if (val == allocated)
805 val = div_u64(val, nd_region->ndr_mappings);
806 allocated = div_u64(allocated, nd_region->ndr_mappings);
808 rc = shrink_dpa_allocation(nd_region, &label_id,
811 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
816 if (is_namespace_pmem(dev)) {
817 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
819 nd_namespace_pmem_set_resource(nd_region, nspm,
820 val * nd_region->ndr_mappings);
824 * Try to delete the namespace if we deleted all of its
825 * allocation, this is not the seed or 0th device for the
826 * region, and it is not actively claimed by a btt, pfn, or dax
829 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
830 nd_device_unregister(dev, ND_ASYNC);
835 static ssize_t size_store(struct device *dev,
836 struct device_attribute *attr, const char *buf, size_t len)
838 struct nd_region *nd_region = to_nd_region(dev->parent);
839 unsigned long long val;
842 rc = kstrtoull(buf, 0, &val);
847 nvdimm_bus_lock(dev);
848 wait_nvdimm_bus_probe_idle(dev);
849 rc = __size_store(dev, val);
851 rc = nd_namespace_label_update(nd_region, dev);
853 /* setting size zero == 'delete namespace' */
854 if (rc == 0 && val == 0 && is_namespace_pmem(dev)) {
855 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
861 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
863 nvdimm_bus_unlock(dev);
866 return rc < 0 ? rc : len;
869 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
871 struct device *dev = &ndns->dev;
873 if (is_namespace_pmem(dev)) {
874 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
876 return resource_size(&nspm->nsio.res);
877 } else if (is_namespace_io(dev)) {
878 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
880 return resource_size(&nsio->res);
882 WARN_ONCE(1, "unknown namespace type\n");
886 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
888 resource_size_t size;
890 nvdimm_bus_lock(&ndns->dev);
891 size = __nvdimm_namespace_capacity(ndns);
892 nvdimm_bus_unlock(&ndns->dev);
896 EXPORT_SYMBOL(nvdimm_namespace_capacity);
898 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
902 struct device *dev = &ndns->dev;
903 struct nd_region *nd_region = to_nd_region(dev->parent);
905 for (i = 0; i < nd_region->ndr_mappings; i++) {
906 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
907 struct nvdimm *nvdimm = nd_mapping->nvdimm;
909 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
910 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
916 EXPORT_SYMBOL(nvdimm_namespace_locked);
918 static ssize_t size_show(struct device *dev,
919 struct device_attribute *attr, char *buf)
921 return sprintf(buf, "%llu\n", (unsigned long long)
922 nvdimm_namespace_capacity(to_ndns(dev)));
924 static DEVICE_ATTR(size, 0444, size_show, size_store);
926 static uuid_t *namespace_to_uuid(struct device *dev)
928 if (is_namespace_pmem(dev)) {
929 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
933 return ERR_PTR(-ENXIO);
936 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
939 uuid_t *uuid = namespace_to_uuid(dev);
942 return PTR_ERR(uuid);
944 return sprintf(buf, "%pUb\n", uuid);
945 return sprintf(buf, "\n");
949 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
950 * @nd_region: parent region so we can updates all dimms in the set
951 * @dev: namespace type for generating label_id
952 * @new_uuid: incoming uuid
953 * @old_uuid: reference to the uuid storage location in the namespace object
955 static int namespace_update_uuid(struct nd_region *nd_region,
956 struct device *dev, uuid_t *new_uuid,
959 struct nd_label_id old_label_id;
960 struct nd_label_id new_label_id;
963 if (!nd_is_uuid_unique(dev, new_uuid))
966 if (*old_uuid == NULL)
970 * If we've already written a label with this uuid, then it's
971 * too late to rename because we can't reliably update the uuid
972 * without losing the old namespace. Userspace must delete this
973 * namespace to abandon the old uuid.
975 for (i = 0; i < nd_region->ndr_mappings; i++) {
976 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
979 * This check by itself is sufficient because old_uuid
980 * would be NULL above if this uuid did not exist in the
981 * currently written set.
983 * FIXME: can we delete uuid with zero dpa allocated?
985 if (list_empty(&nd_mapping->labels))
989 nd_label_gen_id(&old_label_id, *old_uuid, 0);
990 nd_label_gen_id(&new_label_id, new_uuid, 0);
991 for (i = 0; i < nd_region->ndr_mappings; i++) {
992 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
993 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
994 struct nd_label_ent *label_ent;
995 struct resource *res;
997 for_each_dpa_resource(ndd, res)
998 if (strcmp(res->name, old_label_id.id) == 0)
999 sprintf((void *) res->name, "%s",
1002 mutex_lock(&nd_mapping->lock);
1003 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1004 struct nd_namespace_label *nd_label = label_ent->label;
1005 struct nd_label_id label_id;
1010 nsl_get_uuid(ndd, nd_label, &uuid);
1011 nd_label_gen_id(&label_id, &uuid,
1012 nsl_get_flags(ndd, nd_label));
1013 if (strcmp(old_label_id.id, label_id.id) == 0)
1014 set_bit(ND_LABEL_REAP, &label_ent->flags);
1016 mutex_unlock(&nd_mapping->lock);
1020 *old_uuid = new_uuid;
1024 static ssize_t uuid_store(struct device *dev,
1025 struct device_attribute *attr, const char *buf, size_t len)
1027 struct nd_region *nd_region = to_nd_region(dev->parent);
1028 uuid_t *uuid = NULL;
1032 if (is_namespace_pmem(dev)) {
1033 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1035 ns_uuid = &nspm->uuid;
1040 nvdimm_bus_lock(dev);
1041 wait_nvdimm_bus_probe_idle(dev);
1042 if (to_ndns(dev)->claim)
1045 rc = nd_uuid_store(dev, &uuid, buf, len);
1047 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1049 rc = nd_namespace_label_update(nd_region, dev);
1052 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1053 buf[len - 1] == '\n' ? "" : "\n");
1054 nvdimm_bus_unlock(dev);
1057 return rc < 0 ? rc : len;
1059 static DEVICE_ATTR_RW(uuid);
1061 static ssize_t resource_show(struct device *dev,
1062 struct device_attribute *attr, char *buf)
1064 struct resource *res;
1066 if (is_namespace_pmem(dev)) {
1067 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1069 res = &nspm->nsio.res;
1070 } else if (is_namespace_io(dev)) {
1071 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1077 /* no address to convey if the namespace has no allocation */
1078 if (resource_size(res) == 0)
1080 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1082 static DEVICE_ATTR_ADMIN_RO(resource);
1084 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1086 static ssize_t sector_size_show(struct device *dev,
1087 struct device_attribute *attr, char *buf)
1089 if (is_namespace_pmem(dev)) {
1090 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1092 return nd_size_select_show(nspm->lbasize,
1093 pmem_lbasize_supported, buf);
1098 static ssize_t sector_size_store(struct device *dev,
1099 struct device_attribute *attr, const char *buf, size_t len)
1101 struct nd_region *nd_region = to_nd_region(dev->parent);
1102 const unsigned long *supported;
1103 unsigned long *lbasize;
1106 if (is_namespace_pmem(dev)) {
1107 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1109 lbasize = &nspm->lbasize;
1110 supported = pmem_lbasize_supported;
1115 nvdimm_bus_lock(dev);
1116 if (to_ndns(dev)->claim)
1119 rc = nd_size_select_store(dev, buf, lbasize, supported);
1121 rc = nd_namespace_label_update(nd_region, dev);
1122 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1123 buf, buf[len - 1] == '\n' ? "" : "\n");
1124 nvdimm_bus_unlock(dev);
1127 return rc ? rc : len;
1129 static DEVICE_ATTR_RW(sector_size);
1131 static ssize_t dpa_extents_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1134 struct nd_region *nd_region = to_nd_region(dev->parent);
1135 struct nd_label_id label_id;
1136 uuid_t *uuid = NULL;
1140 nvdimm_bus_lock(dev);
1141 if (is_namespace_pmem(dev)) {
1142 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1151 nd_label_gen_id(&label_id, uuid, flags);
1152 for (i = 0; i < nd_region->ndr_mappings; i++) {
1153 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1154 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1155 struct resource *res;
1157 for_each_dpa_resource(ndd, res)
1158 if (strcmp(res->name, label_id.id) == 0)
1162 nvdimm_bus_unlock(dev);
1164 return sprintf(buf, "%d\n", count);
1166 static DEVICE_ATTR_RO(dpa_extents);
1168 static int btt_claim_class(struct device *dev)
1170 struct nd_region *nd_region = to_nd_region(dev->parent);
1171 int i, loop_bitmask = 0;
1173 for (i = 0; i < nd_region->ndr_mappings; i++) {
1174 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1175 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1176 struct nd_namespace_index *nsindex;
1179 * If any of the DIMMs do not support labels the only
1180 * possible BTT format is v1.
1187 nsindex = to_namespace_index(ndd, ndd->ns_current);
1188 if (nsindex == NULL)
1191 /* check whether existing labels are v1.1 or v1.2 */
1192 if (__le16_to_cpu(nsindex->major) == 1
1193 && __le16_to_cpu(nsindex->minor) == 1)
1200 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1201 * block is found, a v1.1 label for any mapping will set bit 1, and a
1202 * v1.2 label will set bit 2.
1204 * At the end of the loop, at most one of the three bits must be set.
1205 * If multiple bits were set, it means the different mappings disagree
1206 * about their labels, and this must be cleaned up first.
1208 * If all the label index blocks are found to agree, nsindex of NULL
1209 * implies labels haven't been initialized yet, and when they will,
1210 * they will be of the 1.2 format, so we can assume BTT2.0
1212 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1213 * found, we enforce BTT2.0
1215 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1217 switch (loop_bitmask) {
1220 return NVDIMM_CCLASS_BTT;
1223 return NVDIMM_CCLASS_BTT2;
1229 static ssize_t holder_show(struct device *dev,
1230 struct device_attribute *attr, char *buf)
1232 struct nd_namespace_common *ndns = to_ndns(dev);
1236 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1241 static DEVICE_ATTR_RO(holder);
1243 static int __holder_class_store(struct device *dev, const char *buf)
1245 struct nd_namespace_common *ndns = to_ndns(dev);
1247 if (dev->driver || ndns->claim)
1250 if (sysfs_streq(buf, "btt")) {
1251 int rc = btt_claim_class(dev);
1253 if (rc < NVDIMM_CCLASS_NONE)
1255 ndns->claim_class = rc;
1256 } else if (sysfs_streq(buf, "pfn"))
1257 ndns->claim_class = NVDIMM_CCLASS_PFN;
1258 else if (sysfs_streq(buf, "dax"))
1259 ndns->claim_class = NVDIMM_CCLASS_DAX;
1260 else if (sysfs_streq(buf, ""))
1261 ndns->claim_class = NVDIMM_CCLASS_NONE;
1268 static ssize_t holder_class_store(struct device *dev,
1269 struct device_attribute *attr, const char *buf, size_t len)
1271 struct nd_region *nd_region = to_nd_region(dev->parent);
1275 nvdimm_bus_lock(dev);
1276 wait_nvdimm_bus_probe_idle(dev);
1277 rc = __holder_class_store(dev, buf);
1279 rc = nd_namespace_label_update(nd_region, dev);
1280 dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
1281 nvdimm_bus_unlock(dev);
1284 return rc < 0 ? rc : len;
1287 static ssize_t holder_class_show(struct device *dev,
1288 struct device_attribute *attr, char *buf)
1290 struct nd_namespace_common *ndns = to_ndns(dev);
1294 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1295 rc = sprintf(buf, "\n");
1296 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1297 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1298 rc = sprintf(buf, "btt\n");
1299 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1300 rc = sprintf(buf, "pfn\n");
1301 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1302 rc = sprintf(buf, "dax\n");
1304 rc = sprintf(buf, "<unknown>\n");
1309 static DEVICE_ATTR_RW(holder_class);
1311 static ssize_t mode_show(struct device *dev,
1312 struct device_attribute *attr, char *buf)
1314 struct nd_namespace_common *ndns = to_ndns(dev);
1315 struct device *claim;
1320 claim = ndns->claim;
1321 if (claim && is_nd_btt(claim))
1323 else if (claim && is_nd_pfn(claim))
1325 else if (claim && is_nd_dax(claim))
1327 else if (!claim && pmem_should_map_pages(dev))
1331 rc = sprintf(buf, "%s\n", mode);
1336 static DEVICE_ATTR_RO(mode);
1338 static ssize_t force_raw_store(struct device *dev,
1339 struct device_attribute *attr, const char *buf, size_t len)
1342 int rc = kstrtobool(buf, &force_raw);
1347 to_ndns(dev)->force_raw = force_raw;
1351 static ssize_t force_raw_show(struct device *dev,
1352 struct device_attribute *attr, char *buf)
1354 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1356 static DEVICE_ATTR_RW(force_raw);
1358 static struct attribute *nd_namespace_attributes[] = {
1359 &dev_attr_nstype.attr,
1360 &dev_attr_size.attr,
1361 &dev_attr_mode.attr,
1362 &dev_attr_uuid.attr,
1363 &dev_attr_holder.attr,
1364 &dev_attr_resource.attr,
1365 &dev_attr_alt_name.attr,
1366 &dev_attr_force_raw.attr,
1367 &dev_attr_sector_size.attr,
1368 &dev_attr_dpa_extents.attr,
1369 &dev_attr_holder_class.attr,
1373 static umode_t namespace_visible(struct kobject *kobj,
1374 struct attribute *a, int n)
1376 struct device *dev = container_of(kobj, struct device, kobj);
1378 if (is_namespace_pmem(dev)) {
1379 if (a == &dev_attr_size.attr)
1385 /* base is_namespace_io() attributes */
1386 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr ||
1387 a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr ||
1388 a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr ||
1389 a == &dev_attr_resource.attr)
1395 static struct attribute_group nd_namespace_attribute_group = {
1396 .attrs = nd_namespace_attributes,
1397 .is_visible = namespace_visible,
1400 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1401 &nd_device_attribute_group,
1402 &nd_namespace_attribute_group,
1403 &nd_numa_attribute_group,
1407 static const struct device_type namespace_io_device_type = {
1408 .name = "nd_namespace_io",
1409 .release = namespace_io_release,
1410 .groups = nd_namespace_attribute_groups,
1413 static const struct device_type namespace_pmem_device_type = {
1414 .name = "nd_namespace_pmem",
1415 .release = namespace_pmem_release,
1416 .groups = nd_namespace_attribute_groups,
1419 static bool is_namespace_pmem(const struct device *dev)
1421 return dev ? dev->type == &namespace_pmem_device_type : false;
1424 static bool is_namespace_io(const struct device *dev)
1426 return dev ? dev->type == &namespace_io_device_type : false;
1429 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1431 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1432 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1433 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1434 struct nd_namespace_common *ndns = NULL;
1435 resource_size_t size;
1437 if (nd_btt || nd_pfn || nd_dax) {
1439 ndns = nd_btt->ndns;
1441 ndns = nd_pfn->ndns;
1443 ndns = nd_dax->nd_pfn.ndns;
1446 return ERR_PTR(-ENODEV);
1449 * Flush any in-progess probes / removals in the driver
1450 * for the raw personality of this namespace.
1452 device_lock(&ndns->dev);
1453 device_unlock(&ndns->dev);
1454 if (ndns->dev.driver) {
1455 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1457 return ERR_PTR(-EBUSY);
1459 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1460 "host (%s) vs claim (%s) mismatch\n",
1462 dev_name(ndns->claim)))
1463 return ERR_PTR(-ENXIO);
1465 ndns = to_ndns(dev);
1467 dev_dbg(dev, "claimed by %s, failing probe\n",
1468 dev_name(ndns->claim));
1470 return ERR_PTR(-ENXIO);
1474 if (nvdimm_namespace_locked(ndns))
1475 return ERR_PTR(-EACCES);
1477 size = nvdimm_namespace_capacity(ndns);
1478 if (size < ND_MIN_NAMESPACE_SIZE) {
1479 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1480 &size, ND_MIN_NAMESPACE_SIZE);
1481 return ERR_PTR(-ENODEV);
1485 * Note, alignment validation for fsdax and devdax mode
1486 * namespaces happens in nd_pfn_validate() where infoblock
1487 * padding parameters can be applied.
1489 if (pmem_should_map_pages(dev)) {
1490 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
1491 struct resource *res = &nsio->res;
1493 if (!IS_ALIGNED(res->start | (res->end + 1),
1494 memremap_compat_align())) {
1495 dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
1496 return ERR_PTR(-EOPNOTSUPP);
1500 if (is_namespace_pmem(&ndns->dev)) {
1501 struct nd_namespace_pmem *nspm;
1503 nspm = to_nd_namespace_pmem(&ndns->dev);
1504 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1505 return ERR_PTR(-ENODEV);
1510 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1512 int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1513 resource_size_t size)
1515 return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1517 EXPORT_SYMBOL_GPL(devm_namespace_enable);
1519 void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1521 devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1523 EXPORT_SYMBOL_GPL(devm_namespace_disable);
1525 static struct device **create_namespace_io(struct nd_region *nd_region)
1527 struct nd_namespace_io *nsio;
1528 struct device *dev, **devs;
1529 struct resource *res;
1531 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1535 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1541 dev = &nsio->common.dev;
1542 dev->type = &namespace_io_device_type;
1543 dev->parent = &nd_region->dev;
1545 res->name = dev_name(&nd_region->dev);
1546 res->flags = IORESOURCE_MEM;
1547 res->start = nd_region->ndr_start;
1548 res->end = res->start + nd_region->ndr_size - 1;
1554 static bool has_uuid_at_pos(struct nd_region *nd_region, const uuid_t *uuid,
1555 u64 cookie, u16 pos)
1557 struct nd_namespace_label *found = NULL;
1560 for (i = 0; i < nd_region->ndr_mappings; i++) {
1561 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1562 struct nd_interleave_set *nd_set = nd_region->nd_set;
1563 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1564 struct nd_label_ent *label_ent;
1565 bool found_uuid = false;
1567 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1568 struct nd_namespace_label *nd_label = label_ent->label;
1573 position = nsl_get_position(ndd, nd_label);
1575 if (!nsl_validate_isetcookie(ndd, nd_label, cookie))
1578 if (!nsl_uuid_equal(ndd, nd_label, uuid))
1581 if (!nsl_validate_type_guid(ndd, nd_label,
1582 &nd_set->type_guid))
1586 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1590 if (!nsl_validate_nlabel(nd_region, ndd, nd_label))
1592 if (position != pos)
1600 return found != NULL;
1603 static int select_pmem_id(struct nd_region *nd_region, const uuid_t *pmem_id)
1610 for (i = 0; i < nd_region->ndr_mappings; i++) {
1611 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1612 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1613 struct nd_namespace_label *nd_label = NULL;
1614 u64 hw_start, hw_end, pmem_start, pmem_end;
1615 struct nd_label_ent *label_ent;
1617 lockdep_assert_held(&nd_mapping->lock);
1618 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1619 nd_label = label_ent->label;
1622 if (nsl_uuid_equal(ndd, nd_label, pmem_id))
1633 * Check that this label is compliant with the dpa
1634 * range published in NFIT
1636 hw_start = nd_mapping->start;
1637 hw_end = hw_start + nd_mapping->size;
1638 pmem_start = nsl_get_dpa(ndd, nd_label);
1639 pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label);
1640 if (pmem_start >= hw_start && pmem_start < hw_end
1641 && pmem_end <= hw_end && pmem_end > hw_start)
1644 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1646 nsl_uuid_raw(ndd, nd_label));
1650 /* move recently validated label to the front of the list */
1651 list_move(&label_ent->list, &nd_mapping->labels);
1657 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1658 * @nd_region: region with mappings to validate
1659 * @nspm: target namespace to create
1660 * @nd_label: target pmem namespace label to evaluate
1662 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1663 struct nd_mapping *nd_mapping,
1664 struct nd_namespace_label *nd_label)
1666 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1667 struct nd_namespace_index *nsindex =
1668 to_namespace_index(ndd, ndd->ns_current);
1669 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1670 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1671 struct nd_label_ent *label_ent;
1672 struct nd_namespace_pmem *nspm;
1673 resource_size_t size = 0;
1674 struct resource *res;
1681 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1682 return ERR_PTR(-ENXIO);
1685 if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) {
1686 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1687 nsl_uuid_raw(ndd, nd_label));
1688 if (!nsl_validate_isetcookie(ndd, nd_label, altcookie))
1689 return ERR_PTR(-EAGAIN);
1691 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1692 nsl_uuid_raw(ndd, nd_label));
1695 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1697 return ERR_PTR(-ENOMEM);
1700 dev = &nspm->nsio.common.dev;
1701 dev->type = &namespace_pmem_device_type;
1702 dev->parent = &nd_region->dev;
1703 res = &nspm->nsio.res;
1704 res->name = dev_name(&nd_region->dev);
1705 res->flags = IORESOURCE_MEM;
1707 for (i = 0; i < nd_region->ndr_mappings; i++) {
1708 nsl_get_uuid(ndd, nd_label, &uuid);
1709 if (has_uuid_at_pos(nd_region, &uuid, cookie, i))
1711 if (has_uuid_at_pos(nd_region, &uuid, altcookie, i))
1716 if (i < nd_region->ndr_mappings) {
1717 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1720 * Give up if we don't find an instance of a uuid at each
1721 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1722 * find a dimm with two instances of the same uuid.
1724 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1725 nvdimm_name(nvdimm), nsl_uuid_raw(ndd, nd_label));
1731 * Fix up each mapping's 'labels' to have the validated pmem label for
1732 * that position at labels[0], and NULL at labels[1]. In the process,
1733 * check that the namespace aligns with interleave-set.
1735 nsl_get_uuid(ndd, nd_label, &uuid);
1736 rc = select_pmem_id(nd_region, &uuid);
1740 /* Calculate total size and populate namespace properties from label0 */
1741 for (i = 0; i < nd_region->ndr_mappings; i++) {
1742 struct nd_namespace_label *label0;
1743 struct nvdimm_drvdata *ndd;
1745 nd_mapping = &nd_region->mapping[i];
1746 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1747 typeof(*label_ent), list);
1748 label0 = label_ent ? label_ent->label : NULL;
1755 ndd = to_ndd(nd_mapping);
1756 size += nsl_get_rawsize(ndd, label0);
1757 if (nsl_get_position(ndd, label0) != 0)
1759 WARN_ON(nspm->alt_name || nspm->uuid);
1760 nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0),
1761 NSLABEL_NAME_LEN, GFP_KERNEL);
1762 nsl_get_uuid(ndd, label0, &uuid);
1763 nspm->uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
1764 nspm->lbasize = nsl_get_lbasize(ndd, label0);
1765 nspm->nsio.common.claim_class =
1766 nsl_get_claim_class(ndd, label0);
1769 if (!nspm->alt_name || !nspm->uuid) {
1774 nd_namespace_pmem_set_resource(nd_region, nspm, size);
1778 namespace_pmem_release(dev);
1781 dev_dbg(&nd_region->dev, "invalid label(s)\n");
1784 dev_dbg(&nd_region->dev, "label not found\n");
1787 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
1793 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
1795 struct nd_namespace_pmem *nspm;
1796 struct resource *res;
1799 if (!is_memory(&nd_region->dev))
1802 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1806 dev = &nspm->nsio.common.dev;
1807 dev->type = &namespace_pmem_device_type;
1808 dev->parent = &nd_region->dev;
1809 res = &nspm->nsio.res;
1810 res->name = dev_name(&nd_region->dev);
1811 res->flags = IORESOURCE_MEM;
1813 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1818 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
1819 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1824 static struct lock_class_key nvdimm_namespace_key;
1826 void nd_region_create_ns_seed(struct nd_region *nd_region)
1828 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1830 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
1833 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
1836 * Seed creation failures are not fatal, provisioning is simply
1837 * disabled until memory becomes available
1839 if (!nd_region->ns_seed)
1840 dev_err(&nd_region->dev, "failed to create namespace\n");
1842 device_initialize(nd_region->ns_seed);
1843 lockdep_set_class(&nd_region->ns_seed->mutex,
1844 &nvdimm_namespace_key);
1845 nd_device_register(nd_region->ns_seed);
1849 void nd_region_create_dax_seed(struct nd_region *nd_region)
1851 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1852 nd_region->dax_seed = nd_dax_create(nd_region);
1854 * Seed creation failures are not fatal, provisioning is simply
1855 * disabled until memory becomes available
1857 if (!nd_region->dax_seed)
1858 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1861 void nd_region_create_pfn_seed(struct nd_region *nd_region)
1863 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1864 nd_region->pfn_seed = nd_pfn_create(nd_region);
1866 * Seed creation failures are not fatal, provisioning is simply
1867 * disabled until memory becomes available
1869 if (!nd_region->pfn_seed)
1870 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1873 void nd_region_create_btt_seed(struct nd_region *nd_region)
1875 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1876 nd_region->btt_seed = nd_btt_create(nd_region);
1878 * Seed creation failures are not fatal, provisioning is simply
1879 * disabled until memory becomes available
1881 if (!nd_region->btt_seed)
1882 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1885 static int add_namespace_resource(struct nd_region *nd_region,
1886 struct nd_namespace_label *nd_label, struct device **devs,
1889 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1890 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1893 for (i = 0; i < count; i++) {
1894 uuid_t *uuid = namespace_to_uuid(devs[i]);
1901 if (!nsl_uuid_equal(ndd, nd_label, uuid))
1903 dev_err(&nd_region->dev,
1904 "error: conflicting extents for uuid: %pUb\n", uuid);
1911 static int cmp_dpa(const void *a, const void *b)
1913 const struct device *dev_a = *(const struct device **) a;
1914 const struct device *dev_b = *(const struct device **) b;
1915 struct nd_namespace_pmem *nspm_a, *nspm_b;
1917 if (is_namespace_io(dev_a))
1920 nspm_a = to_nd_namespace_pmem(dev_a);
1921 nspm_b = to_nd_namespace_pmem(dev_b);
1923 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
1924 sizeof(resource_size_t));
1927 static struct device **scan_labels(struct nd_region *nd_region)
1930 struct device *dev, **devs = NULL;
1931 struct nd_label_ent *label_ent, *e;
1932 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1933 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1934 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
1936 /* "safe" because create_namespace_pmem() might list_move() label_ent */
1937 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1938 struct nd_namespace_label *nd_label = label_ent->label;
1939 struct device **__devs;
1944 /* skip labels that describe extents outside of the region */
1945 if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start ||
1946 nsl_get_dpa(ndd, nd_label) > map_end)
1949 i = add_namespace_resource(nd_region, nd_label, devs, count);
1954 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1957 memcpy(__devs, devs, sizeof(dev) * count);
1961 dev = create_namespace_pmem(nd_region, nd_mapping, nd_label);
1963 switch (PTR_ERR(dev)) {
1965 /* skip invalid labels */
1968 /* fallthrough to seed creation */
1974 devs[count++] = dev;
1978 dev_dbg(&nd_region->dev, "discovered %d namespace%s\n", count,
1979 count == 1 ? "" : "s");
1982 struct nd_namespace_pmem *nspm;
1984 /* Publish a zero-sized namespace for userspace to configure. */
1985 nd_mapping_free_labels(nd_mapping);
1987 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1991 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1994 dev = &nspm->nsio.common.dev;
1995 dev->type = &namespace_pmem_device_type;
1996 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1997 dev->parent = &nd_region->dev;
1998 devs[count++] = dev;
1999 } else if (is_memory(&nd_region->dev)) {
2000 /* clean unselected labels */
2001 for (i = 0; i < nd_region->ndr_mappings; i++) {
2002 struct list_head *l, *e;
2006 nd_mapping = &nd_region->mapping[i];
2007 if (list_empty(&nd_mapping->labels)) {
2013 list_for_each_safe(l, e, &nd_mapping->labels) {
2016 list_move_tail(l, &list);
2018 nd_mapping_free_labels(nd_mapping);
2019 list_splice_init(&list, &nd_mapping->labels);
2024 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2030 for (i = 0; devs[i]; i++)
2031 namespace_pmem_release(devs[i]);
2037 static struct device **create_namespaces(struct nd_region *nd_region)
2039 struct nd_mapping *nd_mapping;
2040 struct device **devs;
2043 if (nd_region->ndr_mappings == 0)
2046 /* lock down all mappings while we scan labels */
2047 for (i = 0; i < nd_region->ndr_mappings; i++) {
2048 nd_mapping = &nd_region->mapping[i];
2049 mutex_lock_nested(&nd_mapping->lock, i);
2052 devs = scan_labels(nd_region);
2054 for (i = 0; i < nd_region->ndr_mappings; i++) {
2055 int reverse = nd_region->ndr_mappings - 1 - i;
2057 nd_mapping = &nd_region->mapping[reverse];
2058 mutex_unlock(&nd_mapping->lock);
2064 static void deactivate_labels(void *region)
2066 struct nd_region *nd_region = region;
2069 for (i = 0; i < nd_region->ndr_mappings; i++) {
2070 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2071 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2072 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2074 mutex_lock(&nd_mapping->lock);
2075 nd_mapping_free_labels(nd_mapping);
2076 mutex_unlock(&nd_mapping->lock);
2079 nd_mapping->ndd = NULL;
2081 atomic_dec(&nvdimm->busy);
2085 static int init_active_labels(struct nd_region *nd_region)
2089 for (i = 0; i < nd_region->ndr_mappings; i++) {
2090 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2091 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2092 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2093 struct nd_label_ent *label_ent;
2097 * If the dimm is disabled then we may need to prevent
2098 * the region from being activated.
2101 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2102 /* fail, label data may be unreadable */;
2103 else if (test_bit(NDD_LABELING, &nvdimm->flags))
2104 /* fail, labels needed to disambiguate dpa */;
2108 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2109 dev_name(&nd_mapping->nvdimm->dev),
2110 test_bit(NDD_LOCKED, &nvdimm->flags)
2111 ? "locked" : "disabled");
2115 nd_mapping->ndd = ndd;
2116 atomic_inc(&nvdimm->busy);
2119 count = nd_label_active_count(ndd);
2120 dev_dbg(ndd->dev, "count: %d\n", count);
2123 for (j = 0; j < count; j++) {
2124 struct nd_namespace_label *label;
2126 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2129 label = nd_label_active(ndd, j);
2130 label_ent->label = label;
2132 mutex_lock(&nd_mapping->lock);
2133 list_add_tail(&label_ent->list, &nd_mapping->labels);
2134 mutex_unlock(&nd_mapping->lock);
2141 if (i < nd_region->ndr_mappings)
2146 deactivate_labels(nd_region);
2150 return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
2154 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2156 struct device **devs = NULL;
2157 int i, rc = 0, type;
2160 nvdimm_bus_lock(&nd_region->dev);
2161 rc = init_active_labels(nd_region);
2163 nvdimm_bus_unlock(&nd_region->dev);
2167 type = nd_region_to_nstype(nd_region);
2169 case ND_DEVICE_NAMESPACE_IO:
2170 devs = create_namespace_io(nd_region);
2172 case ND_DEVICE_NAMESPACE_PMEM:
2173 devs = create_namespaces(nd_region);
2178 nvdimm_bus_unlock(&nd_region->dev);
2183 for (i = 0; devs[i]; i++) {
2184 struct device *dev = devs[i];
2187 if (type == ND_DEVICE_NAMESPACE_PMEM) {
2188 struct nd_namespace_pmem *nspm;
2190 nspm = to_nd_namespace_pmem(dev);
2191 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2199 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2200 device_initialize(dev);
2201 lockdep_set_class(&dev->mutex, &nvdimm_namespace_key);
2202 nd_device_register(dev);
2205 nd_region->ns_seed = devs[0];
2210 for (j = i; devs[j]; j++) {
2211 struct device *dev = devs[j];
2213 device_initialize(dev);
2218 * All of the namespaces we tried to register failed, so
2219 * fail region activation.