2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/sort.h>
16 #include <linux/slab.h>
17 #include <linux/pmem.h>
18 #include <linux/list.h>
23 static void namespace_io_release(struct device *dev)
25 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
30 static void namespace_pmem_release(struct device *dev)
32 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
33 struct nd_region *nd_region = to_nd_region(dev->parent);
36 ida_simple_remove(&nd_region->ns_ida, nspm->id);
37 kfree(nspm->alt_name);
42 static void namespace_blk_release(struct device *dev)
44 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
45 struct nd_region *nd_region = to_nd_region(dev->parent);
48 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
49 kfree(nsblk->alt_name);
55 static struct device_type namespace_io_device_type = {
56 .name = "nd_namespace_io",
57 .release = namespace_io_release,
60 static struct device_type namespace_pmem_device_type = {
61 .name = "nd_namespace_pmem",
62 .release = namespace_pmem_release,
65 static struct device_type namespace_blk_device_type = {
66 .name = "nd_namespace_blk",
67 .release = namespace_blk_release,
70 static bool is_namespace_pmem(const struct device *dev)
72 return dev ? dev->type == &namespace_pmem_device_type : false;
75 static bool is_namespace_blk(const struct device *dev)
77 return dev ? dev->type == &namespace_blk_device_type : false;
80 static bool is_namespace_io(const struct device *dev)
82 return dev ? dev->type == &namespace_io_device_type : false;
85 static int is_uuid_busy(struct device *dev, void *data)
87 u8 *uuid1 = data, *uuid2 = NULL;
89 if (is_namespace_pmem(dev)) {
90 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
93 } else if (is_namespace_blk(dev)) {
94 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
97 } else if (is_nd_btt(dev)) {
98 struct nd_btt *nd_btt = to_nd_btt(dev);
100 uuid2 = nd_btt->uuid;
101 } else if (is_nd_pfn(dev)) {
102 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
104 uuid2 = nd_pfn->uuid;
107 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
113 static int is_namespace_uuid_busy(struct device *dev, void *data)
115 if (is_nd_pmem(dev) || is_nd_blk(dev))
116 return device_for_each_child(dev, data, is_uuid_busy);
121 * nd_is_uuid_unique - verify that no other namespace has @uuid
122 * @dev: any device on a nvdimm_bus
123 * @uuid: uuid to check
125 bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
127 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
131 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
132 if (device_for_each_child(&nvdimm_bus->dev, uuid,
133 is_namespace_uuid_busy) != 0)
138 bool pmem_should_map_pages(struct device *dev)
140 struct nd_region *nd_region = to_nd_region(dev->parent);
141 struct nd_namespace_common *ndns = to_ndns(dev);
142 struct nd_namespace_io *nsio;
144 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
147 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
150 if (is_nd_pfn(dev) || is_nd_btt(dev))
156 nsio = to_nd_namespace_io(dev);
157 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
158 IORESOURCE_SYSTEM_RAM,
159 IORES_DESC_NONE) == REGION_MIXED)
162 #ifdef ARCH_MEMREMAP_PMEM
163 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
168 EXPORT_SYMBOL(pmem_should_map_pages);
170 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
173 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
174 const char *suffix = NULL;
176 if (ndns->claim && is_nd_btt(ndns->claim))
179 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
182 if (is_namespace_pmem(&ndns->dev)) {
183 struct nd_namespace_pmem *nspm;
185 nspm = to_nd_namespace_pmem(&ndns->dev);
190 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
191 suffix ? suffix : "");
193 sprintf(name, "pmem%d%s", nd_region->id,
194 suffix ? suffix : "");
195 } else if (is_namespace_blk(&ndns->dev)) {
196 struct nd_namespace_blk *nsblk;
198 nsblk = to_nd_namespace_blk(&ndns->dev);
199 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
200 suffix ? suffix : "");
207 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
209 const u8 *nd_dev_to_uuid(struct device *dev)
211 static const u8 null_uuid[16];
216 if (is_namespace_pmem(dev)) {
217 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
220 } else if (is_namespace_blk(dev)) {
221 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
227 EXPORT_SYMBOL(nd_dev_to_uuid);
229 static ssize_t nstype_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct nd_region *nd_region = to_nd_region(dev->parent);
234 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
236 static DEVICE_ATTR_RO(nstype);
238 static ssize_t __alt_name_store(struct device *dev, const char *buf,
241 char *input, *pos, *alt_name, **ns_altname;
244 if (is_namespace_pmem(dev)) {
245 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
247 ns_altname = &nspm->alt_name;
248 } else if (is_namespace_blk(dev)) {
249 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
251 ns_altname = &nsblk->alt_name;
255 if (dev->driver || to_ndns(dev)->claim)
258 input = kmemdup(buf, len + 1, GFP_KERNEL);
264 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
269 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
275 *ns_altname = alt_name;
276 sprintf(*ns_altname, "%s", pos);
284 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
286 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
287 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
288 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
289 struct nd_label_id label_id;
290 resource_size_t size = 0;
291 struct resource *res;
295 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
296 for_each_dpa_resource(ndd, res)
297 if (strcmp(res->name, label_id.id) == 0)
298 size += resource_size(res);
302 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
304 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
305 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
306 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
307 struct nd_label_id label_id;
308 struct resource *res;
311 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
315 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
316 for_each_dpa_resource(ndd, res) {
317 if (strcmp(res->name, label_id.id) != 0)
320 * Resources with unacknowledged adjustments indicate a
321 * failure to update labels
323 if (res->flags & DPA_RESOURCE_ADJUSTED)
328 /* These values match after a successful label update */
329 if (count != nsblk->num_resources)
332 for (i = 0; i < nsblk->num_resources; i++) {
333 struct resource *found = NULL;
335 for_each_dpa_resource(ndd, res)
336 if (res == nsblk->res[i]) {
348 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
350 resource_size_t size;
352 nvdimm_bus_lock(&nsblk->common.dev);
353 size = __nd_namespace_blk_validate(nsblk);
354 nvdimm_bus_unlock(&nsblk->common.dev);
358 EXPORT_SYMBOL(nd_namespace_blk_validate);
361 static int nd_namespace_label_update(struct nd_region *nd_region,
364 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
365 "namespace must be idle during label update\n");
366 if (dev->driver || to_ndns(dev)->claim)
370 * Only allow label writes that will result in a valid namespace
371 * or deletion of an existing namespace.
373 if (is_namespace_pmem(dev)) {
374 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
375 resource_size_t size = resource_size(&nspm->nsio.res);
377 if (size == 0 && nspm->uuid)
378 /* delete allocation */;
379 else if (!nspm->uuid)
382 return nd_pmem_namespace_label_update(nd_region, nspm, size);
383 } else if (is_namespace_blk(dev)) {
384 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
385 resource_size_t size = nd_namespace_blk_size(nsblk);
387 if (size == 0 && nsblk->uuid)
388 /* delete allocation */;
389 else if (!nsblk->uuid || !nsblk->lbasize)
392 return nd_blk_namespace_label_update(nd_region, nsblk, size);
397 static ssize_t alt_name_store(struct device *dev,
398 struct device_attribute *attr, const char *buf, size_t len)
400 struct nd_region *nd_region = to_nd_region(dev->parent);
404 nvdimm_bus_lock(dev);
405 wait_nvdimm_bus_probe_idle(dev);
406 rc = __alt_name_store(dev, buf, len);
408 rc = nd_namespace_label_update(nd_region, dev);
409 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
410 nvdimm_bus_unlock(dev);
413 return rc < 0 ? rc : len;
416 static ssize_t alt_name_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
421 if (is_namespace_pmem(dev)) {
422 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
424 ns_altname = nspm->alt_name;
425 } else if (is_namespace_blk(dev)) {
426 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
428 ns_altname = nsblk->alt_name;
432 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
434 static DEVICE_ATTR_RW(alt_name);
436 static int scan_free(struct nd_region *nd_region,
437 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
440 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
441 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
445 struct resource *res, *last;
446 resource_size_t new_start;
449 for_each_dpa_resource(ndd, res)
450 if (strcmp(res->name, label_id->id) == 0)
456 if (n >= resource_size(res)) {
457 n -= resource_size(res);
458 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
459 nvdimm_free_dpa(ndd, res);
460 /* retry with last resource deleted */
465 * Keep BLK allocations relegated to high DPA as much as
469 new_start = res->start + n;
471 new_start = res->start;
473 rc = adjust_resource(res, new_start, resource_size(res) - n);
475 res->flags |= DPA_RESOURCE_ADJUSTED;
476 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
484 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
485 * @nd_region: the set of dimms to reclaim @n bytes from
486 * @label_id: unique identifier for the namespace consuming this dpa range
487 * @n: number of bytes per-dimm to release
489 * Assumes resources are ordered. Starting from the end try to
490 * adjust_resource() the allocation to @n, but if @n is larger than the
491 * allocation delete it and find the 'new' last allocation in the label
494 static int shrink_dpa_allocation(struct nd_region *nd_region,
495 struct nd_label_id *label_id, resource_size_t n)
499 for (i = 0; i < nd_region->ndr_mappings; i++) {
500 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
503 rc = scan_free(nd_region, nd_mapping, label_id, n);
511 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
512 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
515 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
516 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
517 resource_size_t first_dpa;
518 struct resource *res;
521 /* allocate blk from highest dpa first */
523 first_dpa = nd_mapping->start + nd_mapping->size - n;
525 first_dpa = nd_mapping->start;
527 /* first resource allocation for this label-id or dimm */
528 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
532 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
538 * space_valid() - validate free dpa space against constraints
539 * @nd_region: hosting region of the free space
540 * @ndd: dimm device data for debug
541 * @label_id: namespace id to allocate space
542 * @prev: potential allocation that precedes free space
543 * @next: allocation that follows the given free space range
544 * @exist: first allocation with same id in the mapping
545 * @n: range that must satisfied for pmem allocations
546 * @valid: free space range to validate
548 * BLK-space is valid as long as it does not precede a PMEM
549 * allocation in a given region. PMEM-space must be contiguous
550 * and adjacent to an existing existing allocation (if one
551 * exists). If reserving PMEM any space is valid.
553 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
554 struct nd_label_id *label_id, struct resource *prev,
555 struct resource *next, struct resource *exist,
556 resource_size_t n, struct resource *valid)
558 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
559 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
561 if (valid->start >= valid->end)
568 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
569 struct nvdimm_bus *nvdimm_bus;
570 struct blk_alloc_info info = {
571 .nd_mapping = nd_mapping,
572 .available = nd_mapping->size,
576 WARN_ON(!is_nd_blk(&nd_region->dev));
577 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
578 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
582 /* allocation needs to be contiguous, so this is all or nothing */
583 if (resource_size(valid) < n)
586 /* we've got all the space we need and no existing allocation */
590 /* allocation needs to be contiguous with the existing namespace */
591 if (valid->start == exist->end + 1
592 || valid->end == exist->start - 1)
596 /* truncate @valid size to 0 */
597 valid->end = valid->start - 1;
601 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
604 static resource_size_t scan_allocate(struct nd_region *nd_region,
605 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
608 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
609 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
610 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
611 struct resource *res, *exist = NULL, valid;
612 const resource_size_t to_allocate = n;
615 for_each_dpa_resource(ndd, res)
616 if (strcmp(label_id->id, res->name) == 0)
619 valid.start = nd_mapping->start;
620 valid.end = mapping_end;
621 valid.name = "free space";
624 for_each_dpa_resource(ndd, res) {
625 struct resource *next = res->sibling, *new_res = NULL;
626 resource_size_t allocate, available = 0;
627 enum alloc_loc loc = ALLOC_ERR;
631 /* ignore resources outside this nd_mapping */
632 if (res->start > mapping_end)
634 if (res->end < nd_mapping->start)
637 /* space at the beginning of the mapping */
638 if (!first++ && res->start > nd_mapping->start) {
639 valid.start = nd_mapping->start;
640 valid.end = res->start - 1;
641 space_valid(nd_region, ndd, label_id, NULL, next, exist,
642 to_allocate, &valid);
643 available = resource_size(&valid);
648 /* space between allocations */
650 valid.start = res->start + resource_size(res);
651 valid.end = min(mapping_end, next->start - 1);
652 space_valid(nd_region, ndd, label_id, res, next, exist,
653 to_allocate, &valid);
654 available = resource_size(&valid);
659 /* space at the end of the mapping */
661 valid.start = res->start + resource_size(res);
662 valid.end = mapping_end;
663 space_valid(nd_region, ndd, label_id, res, next, exist,
664 to_allocate, &valid);
665 available = resource_size(&valid);
670 if (!loc || !available)
672 allocate = min(available, n);
675 if (strcmp(res->name, label_id->id) == 0) {
676 /* adjust current resource up */
677 rc = adjust_resource(res, res->start - allocate,
678 resource_size(res) + allocate);
679 action = "cur grow up";
684 if (strcmp(next->name, label_id->id) == 0) {
685 /* adjust next resource up */
686 rc = adjust_resource(next, next->start
687 - allocate, resource_size(next)
690 action = "next grow up";
691 } else if (strcmp(res->name, label_id->id) == 0) {
692 action = "grow down";
697 if (strcmp(res->name, label_id->id) == 0)
698 action = "grow down";
706 if (strcmp(action, "allocate") == 0) {
707 /* BLK allocate bottom up */
709 valid.start += available - allocate;
711 new_res = nvdimm_allocate_dpa(ndd, label_id,
712 valid.start, allocate);
715 } else if (strcmp(action, "grow down") == 0) {
716 /* adjust current resource down */
717 rc = adjust_resource(res, res->start, resource_size(res)
720 res->flags |= DPA_RESOURCE_ADJUSTED;
726 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
735 * Retry scan with newly inserted resources.
736 * For example, if we did an ALLOC_BEFORE
737 * insertion there may also have been space
738 * available for an ALLOC_AFTER insertion, so we
739 * need to check this same resource again
747 * If we allocated nothing in the BLK case it may be because we are in
748 * an initial "pmem-reserve pass". Only do an initial BLK allocation
749 * when none of the DPA space is reserved.
751 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
752 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
756 static int merge_dpa(struct nd_region *nd_region,
757 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
759 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
760 struct resource *res;
762 if (strncmp("pmem", label_id->id, 4) == 0)
765 for_each_dpa_resource(ndd, res) {
767 struct resource *next = res->sibling;
768 resource_size_t end = res->start + resource_size(res);
770 if (!next || strcmp(res->name, label_id->id) != 0
771 || strcmp(next->name, label_id->id) != 0
772 || end != next->start)
774 end += resource_size(next);
775 nvdimm_free_dpa(ndd, next);
776 rc = adjust_resource(res, res->start, end - res->start);
777 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
780 res->flags |= DPA_RESOURCE_ADJUSTED;
787 static int __reserve_free_pmem(struct device *dev, void *data)
789 struct nvdimm *nvdimm = data;
790 struct nd_region *nd_region;
791 struct nd_label_id label_id;
794 if (!is_nd_pmem(dev))
797 nd_region = to_nd_region(dev);
798 if (nd_region->ndr_mappings == 0)
801 memset(&label_id, 0, sizeof(label_id));
802 strcat(label_id.id, "pmem-reserve");
803 for (i = 0; i < nd_region->ndr_mappings; i++) {
804 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
805 resource_size_t n, rem = 0;
807 if (nd_mapping->nvdimm != nvdimm)
810 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
813 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
814 dev_WARN_ONCE(&nd_region->dev, rem,
815 "pmem reserve underrun: %#llx of %#llx bytes\n",
816 (unsigned long long) n - rem,
817 (unsigned long long) n);
818 return rem ? -ENXIO : 0;
824 static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
825 struct nd_mapping *nd_mapping)
827 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
828 struct resource *res, *_res;
830 for_each_dpa_resource_safe(ndd, res, _res)
831 if (strcmp(res->name, "pmem-reserve") == 0)
832 nvdimm_free_dpa(ndd, res);
835 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
836 struct nd_mapping *nd_mapping)
838 struct nvdimm *nvdimm = nd_mapping->nvdimm;
841 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
842 __reserve_free_pmem);
844 release_free_pmem(nvdimm_bus, nd_mapping);
849 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
850 * @nd_region: the set of dimms to allocate @n more bytes from
851 * @label_id: unique identifier for the namespace consuming this dpa range
852 * @n: number of bytes per-dimm to add to the existing allocation
854 * Assumes resources are ordered. For BLK regions, first consume
855 * BLK-only available DPA free space, then consume PMEM-aliased DPA
856 * space starting at the highest DPA. For PMEM regions start
857 * allocations from the start of an interleave set and end at the first
858 * BLK allocation or the end of the interleave set, whichever comes
861 static int grow_dpa_allocation(struct nd_region *nd_region,
862 struct nd_label_id *label_id, resource_size_t n)
864 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
865 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
868 for (i = 0; i < nd_region->ndr_mappings; i++) {
869 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
870 resource_size_t rem = n;
874 * In the BLK case try once with all unallocated PMEM
875 * reserved, and once without
877 for (j = is_pmem; j < 2; j++) {
878 bool blk_only = j == 0;
881 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
885 rem = scan_allocate(nd_region, nd_mapping,
888 release_free_pmem(nvdimm_bus, nd_mapping);
890 /* try again and allow encroachments into PMEM */
895 dev_WARN_ONCE(&nd_region->dev, rem,
896 "allocation underrun: %#llx of %#llx bytes\n",
897 (unsigned long long) n - rem,
898 (unsigned long long) n);
902 rc = merge_dpa(nd_region, nd_mapping, label_id);
910 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
911 struct nd_namespace_pmem *nspm, resource_size_t size)
913 struct resource *res = &nspm->nsio.res;
914 resource_size_t offset = 0;
916 if (size && !nspm->uuid) {
921 if (size && nspm->uuid) {
922 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
923 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
924 struct nd_label_id label_id;
925 struct resource *res;
932 nd_label_gen_id(&label_id, nspm->uuid, 0);
934 /* calculate a spa offset from the dpa allocation offset */
935 for_each_dpa_resource(ndd, res)
936 if (strcmp(res->name, label_id.id) == 0) {
937 offset = (res->start - nd_mapping->start)
938 * nd_region->ndr_mappings;
947 res->start = nd_region->ndr_start + offset;
948 res->end = res->start + size - 1;
951 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
954 dev_dbg(dev, "%s: uuid not set\n", where);
960 static ssize_t __size_store(struct device *dev, unsigned long long val)
962 resource_size_t allocated = 0, available = 0;
963 struct nd_region *nd_region = to_nd_region(dev->parent);
964 struct nd_namespace_common *ndns = to_ndns(dev);
965 struct nd_mapping *nd_mapping;
966 struct nvdimm_drvdata *ndd;
967 struct nd_label_id label_id;
968 u32 flags = 0, remainder;
972 if (dev->driver || ndns->claim)
975 if (is_namespace_pmem(dev)) {
976 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
980 } else if (is_namespace_blk(dev)) {
981 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
984 flags = NSLABEL_FLAG_LOCAL;
989 * We need a uuid for the allocation-label and dimm(s) on which
990 * to store the label.
992 if (uuid_not_set(uuid, dev, __func__))
994 if (nd_region->ndr_mappings == 0) {
995 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
999 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
1001 dev_dbg(dev, "%llu is not %dK aligned\n", val,
1002 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
1006 nd_label_gen_id(&label_id, uuid, flags);
1007 for (i = 0; i < nd_region->ndr_mappings; i++) {
1008 nd_mapping = &nd_region->mapping[i];
1009 ndd = to_ndd(nd_mapping);
1012 * All dimms in an interleave set, or the base dimm for a blk
1013 * region, need to be enabled for the size to be changed.
1018 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1020 available = nd_region_available_dpa(nd_region);
1022 if (val > available + allocated)
1025 if (val == allocated)
1028 val = div_u64(val, nd_region->ndr_mappings);
1029 allocated = div_u64(allocated, nd_region->ndr_mappings);
1030 if (val < allocated)
1031 rc = shrink_dpa_allocation(nd_region, &label_id,
1034 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1039 if (is_namespace_pmem(dev)) {
1040 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1042 nd_namespace_pmem_set_resource(nd_region, nspm,
1043 val * nd_region->ndr_mappings);
1047 * Try to delete the namespace if we deleted all of its
1048 * allocation, this is not the seed or 0th device for the
1049 * region, and it is not actively claimed by a btt, pfn, or dax
1052 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
1053 nd_device_unregister(dev, ND_ASYNC);
1058 static ssize_t size_store(struct device *dev,
1059 struct device_attribute *attr, const char *buf, size_t len)
1061 struct nd_region *nd_region = to_nd_region(dev->parent);
1062 unsigned long long val;
1066 rc = kstrtoull(buf, 0, &val);
1071 nvdimm_bus_lock(dev);
1072 wait_nvdimm_bus_probe_idle(dev);
1073 rc = __size_store(dev, val);
1075 rc = nd_namespace_label_update(nd_region, dev);
1077 if (is_namespace_pmem(dev)) {
1078 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1081 } else if (is_namespace_blk(dev)) {
1082 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1084 uuid = &nsblk->uuid;
1087 if (rc == 0 && val == 0 && uuid) {
1088 /* setting size zero == 'delete namespace' */
1093 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
1094 ? "fail" : "success", rc);
1096 nvdimm_bus_unlock(dev);
1099 return rc < 0 ? rc : len;
1102 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1104 struct device *dev = &ndns->dev;
1106 if (is_namespace_pmem(dev)) {
1107 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1109 return resource_size(&nspm->nsio.res);
1110 } else if (is_namespace_blk(dev)) {
1111 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1112 } else if (is_namespace_io(dev)) {
1113 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1115 return resource_size(&nsio->res);
1117 WARN_ONCE(1, "unknown namespace type\n");
1121 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1123 resource_size_t size;
1125 nvdimm_bus_lock(&ndns->dev);
1126 size = __nvdimm_namespace_capacity(ndns);
1127 nvdimm_bus_unlock(&ndns->dev);
1131 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1133 static ssize_t size_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
1136 return sprintf(buf, "%llu\n", (unsigned long long)
1137 nvdimm_namespace_capacity(to_ndns(dev)));
1139 static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
1141 static u8 *namespace_to_uuid(struct device *dev)
1143 if (is_namespace_pmem(dev)) {
1144 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1147 } else if (is_namespace_blk(dev)) {
1148 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1152 return ERR_PTR(-ENXIO);
1155 static ssize_t uuid_show(struct device *dev,
1156 struct device_attribute *attr, char *buf)
1158 u8 *uuid = namespace_to_uuid(dev);
1161 return PTR_ERR(uuid);
1163 return sprintf(buf, "%pUb\n", uuid);
1164 return sprintf(buf, "\n");
1168 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1169 * @nd_region: parent region so we can updates all dimms in the set
1170 * @dev: namespace type for generating label_id
1171 * @new_uuid: incoming uuid
1172 * @old_uuid: reference to the uuid storage location in the namespace object
1174 static int namespace_update_uuid(struct nd_region *nd_region,
1175 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1177 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1178 struct nd_label_id old_label_id;
1179 struct nd_label_id new_label_id;
1182 if (!nd_is_uuid_unique(dev, new_uuid))
1185 if (*old_uuid == NULL)
1189 * If we've already written a label with this uuid, then it's
1190 * too late to rename because we can't reliably update the uuid
1191 * without losing the old namespace. Userspace must delete this
1192 * namespace to abandon the old uuid.
1194 for (i = 0; i < nd_region->ndr_mappings; i++) {
1195 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1198 * This check by itself is sufficient because old_uuid
1199 * would be NULL above if this uuid did not exist in the
1200 * currently written set.
1202 * FIXME: can we delete uuid with zero dpa allocated?
1204 if (list_empty(&nd_mapping->labels))
1208 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1209 nd_label_gen_id(&new_label_id, new_uuid, flags);
1210 for (i = 0; i < nd_region->ndr_mappings; i++) {
1211 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1212 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1213 struct nd_label_ent *label_ent;
1214 struct resource *res;
1216 for_each_dpa_resource(ndd, res)
1217 if (strcmp(res->name, old_label_id.id) == 0)
1218 sprintf((void *) res->name, "%s",
1221 mutex_lock(&nd_mapping->lock);
1222 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1223 struct nd_namespace_label *nd_label = label_ent->label;
1224 struct nd_label_id label_id;
1228 nd_label_gen_id(&label_id, nd_label->uuid,
1229 __le32_to_cpu(nd_label->flags));
1230 if (strcmp(old_label_id.id, label_id.id) == 0)
1231 set_bit(ND_LABEL_REAP, &label_ent->flags);
1233 mutex_unlock(&nd_mapping->lock);
1237 *old_uuid = new_uuid;
1241 static ssize_t uuid_store(struct device *dev,
1242 struct device_attribute *attr, const char *buf, size_t len)
1244 struct nd_region *nd_region = to_nd_region(dev->parent);
1249 if (is_namespace_pmem(dev)) {
1250 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1252 ns_uuid = &nspm->uuid;
1253 } else if (is_namespace_blk(dev)) {
1254 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1256 ns_uuid = &nsblk->uuid;
1261 nvdimm_bus_lock(dev);
1262 wait_nvdimm_bus_probe_idle(dev);
1263 if (to_ndns(dev)->claim)
1266 rc = nd_uuid_store(dev, &uuid, buf, len);
1268 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1270 rc = nd_namespace_label_update(nd_region, dev);
1273 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1274 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1275 nvdimm_bus_unlock(dev);
1278 return rc < 0 ? rc : len;
1280 static DEVICE_ATTR_RW(uuid);
1282 static ssize_t resource_show(struct device *dev,
1283 struct device_attribute *attr, char *buf)
1285 struct resource *res;
1287 if (is_namespace_pmem(dev)) {
1288 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1290 res = &nspm->nsio.res;
1291 } else if (is_namespace_io(dev)) {
1292 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1298 /* no address to convey if the namespace has no allocation */
1299 if (resource_size(res) == 0)
1301 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1303 static DEVICE_ATTR_RO(resource);
1305 static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1306 4096, 4104, 4160, 4224, 0 };
1308 static ssize_t sector_size_show(struct device *dev,
1309 struct device_attribute *attr, char *buf)
1311 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1313 if (!is_namespace_blk(dev))
1316 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1319 static ssize_t sector_size_store(struct device *dev,
1320 struct device_attribute *attr, const char *buf, size_t len)
1322 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1323 struct nd_region *nd_region = to_nd_region(dev->parent);
1326 if (!is_namespace_blk(dev))
1330 nvdimm_bus_lock(dev);
1331 if (to_ndns(dev)->claim)
1334 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1335 ns_lbasize_supported);
1337 rc = nd_namespace_label_update(nd_region, dev);
1338 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1339 rc, rc < 0 ? "tried" : "wrote", buf,
1340 buf[len - 1] == '\n' ? "" : "\n");
1341 nvdimm_bus_unlock(dev);
1344 return rc ? rc : len;
1346 static DEVICE_ATTR_RW(sector_size);
1348 static ssize_t dpa_extents_show(struct device *dev,
1349 struct device_attribute *attr, char *buf)
1351 struct nd_region *nd_region = to_nd_region(dev->parent);
1352 struct nd_label_id label_id;
1357 nvdimm_bus_lock(dev);
1358 if (is_namespace_pmem(dev)) {
1359 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1363 } else if (is_namespace_blk(dev)) {
1364 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1367 flags = NSLABEL_FLAG_LOCAL;
1373 nd_label_gen_id(&label_id, uuid, flags);
1374 for (i = 0; i < nd_region->ndr_mappings; i++) {
1375 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1376 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1377 struct resource *res;
1379 for_each_dpa_resource(ndd, res)
1380 if (strcmp(res->name, label_id.id) == 0)
1384 nvdimm_bus_unlock(dev);
1386 return sprintf(buf, "%d\n", count);
1388 static DEVICE_ATTR_RO(dpa_extents);
1390 static ssize_t holder_show(struct device *dev,
1391 struct device_attribute *attr, char *buf)
1393 struct nd_namespace_common *ndns = to_ndns(dev);
1397 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1402 static DEVICE_ATTR_RO(holder);
1404 static ssize_t mode_show(struct device *dev,
1405 struct device_attribute *attr, char *buf)
1407 struct nd_namespace_common *ndns = to_ndns(dev);
1408 struct device *claim;
1413 claim = ndns->claim;
1414 if (claim && is_nd_btt(claim))
1416 else if (claim && is_nd_pfn(claim))
1418 else if (claim && is_nd_dax(claim))
1420 else if (!claim && pmem_should_map_pages(dev))
1424 rc = sprintf(buf, "%s\n", mode);
1429 static DEVICE_ATTR_RO(mode);
1431 static ssize_t force_raw_store(struct device *dev,
1432 struct device_attribute *attr, const char *buf, size_t len)
1435 int rc = strtobool(buf, &force_raw);
1440 to_ndns(dev)->force_raw = force_raw;
1444 static ssize_t force_raw_show(struct device *dev,
1445 struct device_attribute *attr, char *buf)
1447 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1449 static DEVICE_ATTR_RW(force_raw);
1451 static struct attribute *nd_namespace_attributes[] = {
1452 &dev_attr_nstype.attr,
1453 &dev_attr_size.attr,
1454 &dev_attr_mode.attr,
1455 &dev_attr_uuid.attr,
1456 &dev_attr_holder.attr,
1457 &dev_attr_resource.attr,
1458 &dev_attr_alt_name.attr,
1459 &dev_attr_force_raw.attr,
1460 &dev_attr_sector_size.attr,
1461 &dev_attr_dpa_extents.attr,
1465 static umode_t namespace_visible(struct kobject *kobj,
1466 struct attribute *a, int n)
1468 struct device *dev = container_of(kobj, struct device, kobj);
1470 if (a == &dev_attr_resource.attr) {
1471 if (is_namespace_blk(dev))
1476 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1477 if (a == &dev_attr_size.attr)
1478 return S_IWUSR | S_IRUGO;
1480 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1486 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1487 || a == &dev_attr_holder.attr
1488 || a == &dev_attr_force_raw.attr
1489 || a == &dev_attr_mode.attr)
1495 static struct attribute_group nd_namespace_attribute_group = {
1496 .attrs = nd_namespace_attributes,
1497 .is_visible = namespace_visible,
1500 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1501 &nd_device_attribute_group,
1502 &nd_namespace_attribute_group,
1503 &nd_numa_attribute_group,
1507 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1509 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1510 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1511 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1512 struct nd_namespace_common *ndns = NULL;
1513 resource_size_t size;
1515 if (nd_btt || nd_pfn || nd_dax) {
1517 ndns = nd_btt->ndns;
1519 ndns = nd_pfn->ndns;
1521 ndns = nd_dax->nd_pfn.ndns;
1524 return ERR_PTR(-ENODEV);
1527 * Flush any in-progess probes / removals in the driver
1528 * for the raw personality of this namespace.
1530 device_lock(&ndns->dev);
1531 device_unlock(&ndns->dev);
1532 if (ndns->dev.driver) {
1533 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1535 return ERR_PTR(-EBUSY);
1537 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1538 "host (%s) vs claim (%s) mismatch\n",
1540 dev_name(ndns->claim)))
1541 return ERR_PTR(-ENXIO);
1543 ndns = to_ndns(dev);
1545 dev_dbg(dev, "claimed by %s, failing probe\n",
1546 dev_name(ndns->claim));
1548 return ERR_PTR(-ENXIO);
1552 size = nvdimm_namespace_capacity(ndns);
1553 if (size < ND_MIN_NAMESPACE_SIZE) {
1554 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1555 &size, ND_MIN_NAMESPACE_SIZE);
1556 return ERR_PTR(-ENODEV);
1559 if (is_namespace_pmem(&ndns->dev)) {
1560 struct nd_namespace_pmem *nspm;
1562 nspm = to_nd_namespace_pmem(&ndns->dev);
1563 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1564 return ERR_PTR(-ENODEV);
1565 } else if (is_namespace_blk(&ndns->dev)) {
1566 struct nd_namespace_blk *nsblk;
1568 nsblk = to_nd_namespace_blk(&ndns->dev);
1569 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1570 return ERR_PTR(-ENODEV);
1571 if (!nsblk->lbasize) {
1572 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1574 return ERR_PTR(-ENODEV);
1576 if (!nd_namespace_blk_validate(nsblk))
1577 return ERR_PTR(-ENODEV);
1582 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1584 static struct device **create_namespace_io(struct nd_region *nd_region)
1586 struct nd_namespace_io *nsio;
1587 struct device *dev, **devs;
1588 struct resource *res;
1590 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1594 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1600 dev = &nsio->common.dev;
1601 dev->type = &namespace_io_device_type;
1602 dev->parent = &nd_region->dev;
1604 res->name = dev_name(&nd_region->dev);
1605 res->flags = IORESOURCE_MEM;
1606 res->start = nd_region->ndr_start;
1607 res->end = res->start + nd_region->ndr_size - 1;
1613 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1614 u64 cookie, u16 pos)
1616 struct nd_namespace_label *found = NULL;
1619 for (i = 0; i < nd_region->ndr_mappings; i++) {
1620 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1621 struct nd_label_ent *label_ent;
1622 bool found_uuid = false;
1624 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1625 struct nd_namespace_label *nd_label = label_ent->label;
1626 u16 position, nlabel;
1631 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1632 position = __le16_to_cpu(nd_label->position);
1633 nlabel = __le16_to_cpu(nd_label->nlabel);
1635 if (isetcookie != cookie)
1638 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1642 dev_dbg(to_ndd(nd_mapping)->dev,
1643 "%s duplicate entry for uuid\n",
1648 if (nlabel != nd_region->ndr_mappings)
1650 if (position != pos)
1658 return found != NULL;
1661 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1668 for (i = 0; i < nd_region->ndr_mappings; i++) {
1669 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1670 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1671 struct nd_namespace_label *nd_label = NULL;
1672 u64 hw_start, hw_end, pmem_start, pmem_end;
1673 struct nd_label_ent *label_ent;
1675 WARN_ON(!mutex_is_locked(&nd_mapping->lock));
1676 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1677 nd_label = label_ent->label;
1680 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1691 * Check that this label is compliant with the dpa
1692 * range published in NFIT
1694 hw_start = nd_mapping->start;
1695 hw_end = hw_start + nd_mapping->size;
1696 pmem_start = __le64_to_cpu(nd_label->dpa);
1697 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1698 if (pmem_start >= hw_start && pmem_start < hw_end
1699 && pmem_end <= hw_end && pmem_end > hw_start)
1702 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1703 dev_name(ndd->dev), nd_label->uuid);
1707 /* move recently validated label to the front of the list */
1708 list_move(&label_ent->list, &nd_mapping->labels);
1714 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1715 * @nd_region: region with mappings to validate
1716 * @nspm: target namespace to create
1717 * @nd_label: target pmem namespace label to evaluate
1719 struct device *create_namespace_pmem(struct nd_region *nd_region,
1720 struct nd_namespace_label *nd_label)
1722 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1723 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1724 struct nd_label_ent *label_ent;
1725 struct nd_namespace_pmem *nspm;
1726 struct nd_mapping *nd_mapping;
1727 resource_size_t size = 0;
1728 struct resource *res;
1734 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1735 return ERR_PTR(-ENXIO);
1738 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1739 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1741 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1742 return ERR_PTR(-EAGAIN);
1744 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1748 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1750 return ERR_PTR(-ENOMEM);
1753 dev = &nspm->nsio.common.dev;
1754 dev->type = &namespace_pmem_device_type;
1755 dev->parent = &nd_region->dev;
1756 res = &nspm->nsio.res;
1757 res->name = dev_name(&nd_region->dev);
1758 res->flags = IORESOURCE_MEM;
1760 for (i = 0; i < nd_region->ndr_mappings; i++) {
1761 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1763 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1768 if (i < nd_region->ndr_mappings) {
1769 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1772 * Give up if we don't find an instance of a uuid at each
1773 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1774 * find a dimm with two instances of the same uuid.
1776 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1777 nvdimm_name(nvdimm), nd_label->uuid);
1783 * Fix up each mapping's 'labels' to have the validated pmem label for
1784 * that position at labels[0], and NULL at labels[1]. In the process,
1785 * check that the namespace aligns with interleave-set. We know
1786 * that it does not overlap with any blk namespaces by virtue of
1787 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1790 rc = select_pmem_id(nd_region, nd_label->uuid);
1794 /* Calculate total size and populate namespace properties from label0 */
1795 for (i = 0; i < nd_region->ndr_mappings; i++) {
1796 struct nd_namespace_label *label0;
1798 nd_mapping = &nd_region->mapping[i];
1799 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1800 typeof(*label_ent), list);
1801 label0 = label_ent ? label_ent->label : 0;
1808 size += __le64_to_cpu(label0->rawsize);
1809 if (__le16_to_cpu(label0->position) != 0)
1811 WARN_ON(nspm->alt_name || nspm->uuid);
1812 nspm->alt_name = kmemdup((void __force *) label0->name,
1813 NSLABEL_NAME_LEN, GFP_KERNEL);
1814 nspm->uuid = kmemdup((void __force *) label0->uuid,
1815 NSLABEL_UUID_LEN, GFP_KERNEL);
1818 if (!nspm->alt_name || !nspm->uuid) {
1823 nd_namespace_pmem_set_resource(nd_region, nspm, size);
1827 namespace_pmem_release(dev);
1830 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1833 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1836 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1843 struct resource *nsblk_add_resource(struct nd_region *nd_region,
1844 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1845 resource_size_t start)
1847 struct nd_label_id label_id;
1848 struct resource *res;
1850 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1851 res = krealloc(nsblk->res,
1852 sizeof(void *) * (nsblk->num_resources + 1),
1856 nsblk->res = (struct resource **) res;
1857 for_each_dpa_resource(ndd, res)
1858 if (strcmp(res->name, label_id.id) == 0
1859 && res->start == start) {
1860 nsblk->res[nsblk->num_resources++] = res;
1866 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1868 struct nd_namespace_blk *nsblk;
1871 if (!is_nd_blk(&nd_region->dev))
1874 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1878 dev = &nsblk->common.dev;
1879 dev->type = &namespace_blk_device_type;
1880 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1881 if (nsblk->id < 0) {
1885 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1886 dev->parent = &nd_region->dev;
1887 dev->groups = nd_namespace_attribute_groups;
1889 return &nsblk->common.dev;
1892 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
1894 struct nd_namespace_pmem *nspm;
1895 struct resource *res;
1898 if (!is_nd_pmem(&nd_region->dev))
1901 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1905 dev = &nspm->nsio.common.dev;
1906 dev->type = &namespace_pmem_device_type;
1907 dev->parent = &nd_region->dev;
1908 res = &nspm->nsio.res;
1909 res->name = dev_name(&nd_region->dev);
1910 res->flags = IORESOURCE_MEM;
1912 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1917 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
1918 dev->parent = &nd_region->dev;
1919 dev->groups = nd_namespace_attribute_groups;
1920 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1925 void nd_region_create_ns_seed(struct nd_region *nd_region)
1927 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1929 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
1932 if (is_nd_blk(&nd_region->dev))
1933 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1935 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
1938 * Seed creation failures are not fatal, provisioning is simply
1939 * disabled until memory becomes available
1941 if (!nd_region->ns_seed)
1942 dev_err(&nd_region->dev, "failed to create %s namespace\n",
1943 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
1945 nd_device_register(nd_region->ns_seed);
1948 void nd_region_create_dax_seed(struct nd_region *nd_region)
1950 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1951 nd_region->dax_seed = nd_dax_create(nd_region);
1953 * Seed creation failures are not fatal, provisioning is simply
1954 * disabled until memory becomes available
1956 if (!nd_region->dax_seed)
1957 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1960 void nd_region_create_pfn_seed(struct nd_region *nd_region)
1962 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1963 nd_region->pfn_seed = nd_pfn_create(nd_region);
1965 * Seed creation failures are not fatal, provisioning is simply
1966 * disabled until memory becomes available
1968 if (!nd_region->pfn_seed)
1969 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1972 void nd_region_create_btt_seed(struct nd_region *nd_region)
1974 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1975 nd_region->btt_seed = nd_btt_create(nd_region);
1977 * Seed creation failures are not fatal, provisioning is simply
1978 * disabled until memory becomes available
1980 if (!nd_region->btt_seed)
1981 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1984 static int add_namespace_resource(struct nd_region *nd_region,
1985 struct nd_namespace_label *nd_label, struct device **devs,
1988 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1989 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1992 for (i = 0; i < count; i++) {
1993 u8 *uuid = namespace_to_uuid(devs[i]);
1994 struct resource *res;
1996 if (IS_ERR_OR_NULL(uuid)) {
2001 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2003 if (is_namespace_blk(devs[i])) {
2004 res = nsblk_add_resource(nd_region, ndd,
2005 to_nd_namespace_blk(devs[i]),
2006 __le64_to_cpu(nd_label->dpa));
2009 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2011 dev_err(&nd_region->dev,
2012 "error: conflicting extents for uuid: %pUb\n",
2022 struct device *create_namespace_blk(struct nd_region *nd_region,
2023 struct nd_namespace_label *nd_label, int count)
2026 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2027 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2028 struct nd_namespace_blk *nsblk;
2029 char *name[NSLABEL_NAME_LEN];
2030 struct device *dev = NULL;
2031 struct resource *res;
2033 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2035 return ERR_PTR(-ENOMEM);
2036 dev = &nsblk->common.dev;
2037 dev->type = &namespace_blk_device_type;
2038 dev->parent = &nd_region->dev;
2040 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2041 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2045 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2047 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2049 if (!nsblk->alt_name)
2052 res = nsblk_add_resource(nd_region, ndd, nsblk,
2053 __le64_to_cpu(nd_label->dpa));
2056 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2059 namespace_blk_release(dev);
2060 return ERR_PTR(-ENXIO);
2063 static int cmp_dpa(const void *a, const void *b)
2065 const struct device *dev_a = *(const struct device **) a;
2066 const struct device *dev_b = *(const struct device **) b;
2067 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2068 struct nd_namespace_pmem *nspm_a, *nspm_b;
2070 if (is_namespace_io(dev_a))
2073 if (is_namespace_blk(dev_a)) {
2074 nsblk_a = to_nd_namespace_blk(dev_a);
2075 nsblk_b = to_nd_namespace_blk(dev_b);
2077 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2078 sizeof(resource_size_t));
2081 nspm_a = to_nd_namespace_pmem(dev_a);
2082 nspm_b = to_nd_namespace_pmem(dev_b);
2084 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2085 sizeof(resource_size_t));
2088 static struct device **scan_labels(struct nd_region *nd_region)
2091 struct device *dev, **devs = NULL;
2092 struct nd_label_ent *label_ent, *e;
2093 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2094 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
2096 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2097 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
2098 struct nd_namespace_label *nd_label = label_ent->label;
2099 struct device **__devs;
2104 flags = __le32_to_cpu(nd_label->flags);
2105 if (is_nd_blk(&nd_region->dev)
2106 == !!(flags & NSLABEL_FLAG_LOCAL))
2107 /* pass, region matches label type */;
2111 /* skip labels that describe extents outside of the region */
2112 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
2115 i = add_namespace_resource(nd_region, nd_label, devs, count);
2120 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2123 memcpy(__devs, devs, sizeof(dev) * count);
2127 if (is_nd_blk(&nd_region->dev)) {
2128 dev = create_namespace_blk(nd_region, nd_label, count);
2131 devs[count++] = dev;
2133 dev = create_namespace_pmem(nd_region, nd_label);
2135 switch (PTR_ERR(dev)) {
2137 /* skip invalid labels */
2140 /* fallthrough to seed creation */
2146 devs[count++] = dev;
2150 dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
2151 __func__, count, is_nd_blk(&nd_region->dev)
2152 ? "blk" : "pmem", count == 1 ? "" : "s");
2155 /* Publish a zero-sized namespace for userspace to configure. */
2156 nd_mapping_free_labels(nd_mapping);
2158 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2161 if (is_nd_blk(&nd_region->dev)) {
2162 struct nd_namespace_blk *nsblk;
2164 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2167 dev = &nsblk->common.dev;
2168 dev->type = &namespace_blk_device_type;
2170 struct nd_namespace_pmem *nspm;
2172 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2175 dev = &nspm->nsio.common.dev;
2176 dev->type = &namespace_pmem_device_type;
2177 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2179 dev->parent = &nd_region->dev;
2180 devs[count++] = dev;
2181 } else if (is_nd_pmem(&nd_region->dev)) {
2182 /* clean unselected labels */
2183 for (i = 0; i < nd_region->ndr_mappings; i++) {
2184 struct list_head *l, *e;
2188 nd_mapping = &nd_region->mapping[i];
2189 if (list_empty(&nd_mapping->labels)) {
2195 list_for_each_safe(l, e, &nd_mapping->labels) {
2198 list_move_tail(l, &list);
2200 nd_mapping_free_labels(nd_mapping);
2201 list_splice_init(&list, &nd_mapping->labels);
2206 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2212 for (i = 0; devs[i]; i++)
2213 if (is_nd_blk(&nd_region->dev))
2214 namespace_blk_release(devs[i]);
2216 namespace_pmem_release(devs[i]);
2222 static struct device **create_namespaces(struct nd_region *nd_region)
2224 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2225 struct device **devs;
2228 if (nd_region->ndr_mappings == 0)
2231 /* lock down all mappings while we scan labels */
2232 for (i = 0; i < nd_region->ndr_mappings; i++) {
2233 nd_mapping = &nd_region->mapping[i];
2234 mutex_lock_nested(&nd_mapping->lock, i);
2237 devs = scan_labels(nd_region);
2239 for (i = 0; i < nd_region->ndr_mappings; i++) {
2240 int reverse = nd_region->ndr_mappings - 1 - i;
2242 nd_mapping = &nd_region->mapping[reverse];
2243 mutex_unlock(&nd_mapping->lock);
2249 static int init_active_labels(struct nd_region *nd_region)
2253 for (i = 0; i < nd_region->ndr_mappings; i++) {
2254 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2255 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2256 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2257 struct nd_label_ent *label_ent;
2261 * If the dimm is disabled then prevent the region from
2262 * being activated if it aliases DPA.
2265 if ((nvdimm->flags & NDD_ALIASING) == 0)
2267 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
2268 dev_name(&nd_mapping->nvdimm->dev));
2271 nd_mapping->ndd = ndd;
2272 atomic_inc(&nvdimm->busy);
2275 count = nd_label_active_count(ndd);
2276 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
2279 for (j = 0; j < count; j++) {
2280 struct nd_namespace_label *label;
2282 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2285 label = nd_label_active(ndd, j);
2286 label_ent->label = label;
2288 mutex_lock(&nd_mapping->lock);
2289 list_add_tail(&label_ent->list, &nd_mapping->labels);
2290 mutex_unlock(&nd_mapping->lock);
2296 mutex_lock(&nd_mapping->lock);
2297 nd_mapping_free_labels(nd_mapping);
2298 mutex_unlock(&nd_mapping->lock);
2305 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2307 struct device **devs = NULL;
2308 int i, rc = 0, type;
2311 nvdimm_bus_lock(&nd_region->dev);
2312 rc = init_active_labels(nd_region);
2314 nvdimm_bus_unlock(&nd_region->dev);
2318 type = nd_region_to_nstype(nd_region);
2320 case ND_DEVICE_NAMESPACE_IO:
2321 devs = create_namespace_io(nd_region);
2323 case ND_DEVICE_NAMESPACE_PMEM:
2324 case ND_DEVICE_NAMESPACE_BLK:
2325 devs = create_namespaces(nd_region);
2330 nvdimm_bus_unlock(&nd_region->dev);
2335 for (i = 0; devs[i]; i++) {
2336 struct device *dev = devs[i];
2339 if (type == ND_DEVICE_NAMESPACE_BLK) {
2340 struct nd_namespace_blk *nsblk;
2342 nsblk = to_nd_namespace_blk(dev);
2343 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2346 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2347 struct nd_namespace_pmem *nspm;
2349 nspm = to_nd_namespace_pmem(dev);
2350 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2358 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2359 dev->groups = nd_namespace_attribute_groups;
2360 nd_device_register(dev);
2363 nd_region->ns_seed = devs[0];
2368 for (j = i; devs[j]; j++) {
2369 struct device *dev = devs[j];
2371 device_initialize(dev);
2376 * All of the namespaces we tried to register failed, so
2377 * fail region activation.