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_io *nsio;
143 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
146 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
149 if (is_nd_pfn(dev) || is_nd_btt(dev))
152 nsio = to_nd_namespace_io(dev);
153 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
154 IORESOURCE_SYSTEM_RAM,
155 IORES_DESC_NONE) == REGION_MIXED)
158 #ifdef ARCH_MEMREMAP_PMEM
159 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
164 EXPORT_SYMBOL(pmem_should_map_pages);
166 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
169 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
170 const char *suffix = NULL;
172 if (ndns->claim && is_nd_btt(ndns->claim))
175 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
178 if (is_namespace_pmem(&ndns->dev)) {
179 struct nd_namespace_pmem *nspm;
181 nspm = to_nd_namespace_pmem(&ndns->dev);
186 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
187 suffix ? suffix : "");
189 sprintf(name, "pmem%d%s", nd_region->id,
190 suffix ? suffix : "");
191 } else if (is_namespace_blk(&ndns->dev)) {
192 struct nd_namespace_blk *nsblk;
194 nsblk = to_nd_namespace_blk(&ndns->dev);
195 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
196 suffix ? suffix : "");
203 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
205 const u8 *nd_dev_to_uuid(struct device *dev)
207 static const u8 null_uuid[16];
212 if (is_namespace_pmem(dev)) {
213 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
216 } else if (is_namespace_blk(dev)) {
217 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
223 EXPORT_SYMBOL(nd_dev_to_uuid);
225 static ssize_t nstype_show(struct device *dev,
226 struct device_attribute *attr, char *buf)
228 struct nd_region *nd_region = to_nd_region(dev->parent);
230 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
232 static DEVICE_ATTR_RO(nstype);
234 static ssize_t __alt_name_store(struct device *dev, const char *buf,
237 char *input, *pos, *alt_name, **ns_altname;
240 if (is_namespace_pmem(dev)) {
241 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
243 ns_altname = &nspm->alt_name;
244 } else if (is_namespace_blk(dev)) {
245 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
247 ns_altname = &nsblk->alt_name;
251 if (dev->driver || to_ndns(dev)->claim)
254 input = kmemdup(buf, len + 1, GFP_KERNEL);
260 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
265 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
271 *ns_altname = alt_name;
272 sprintf(*ns_altname, "%s", pos);
280 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
282 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
283 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
284 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
285 struct nd_label_id label_id;
286 resource_size_t size = 0;
287 struct resource *res;
291 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
292 for_each_dpa_resource(ndd, res)
293 if (strcmp(res->name, label_id.id) == 0)
294 size += resource_size(res);
298 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
300 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
301 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
302 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
303 struct nd_label_id label_id;
304 struct resource *res;
307 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
311 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
312 for_each_dpa_resource(ndd, res) {
313 if (strcmp(res->name, label_id.id) != 0)
316 * Resources with unacknowledged adjustments indicate a
317 * failure to update labels
319 if (res->flags & DPA_RESOURCE_ADJUSTED)
324 /* These values match after a successful label update */
325 if (count != nsblk->num_resources)
328 for (i = 0; i < nsblk->num_resources; i++) {
329 struct resource *found = NULL;
331 for_each_dpa_resource(ndd, res)
332 if (res == nsblk->res[i]) {
344 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
346 resource_size_t size;
348 nvdimm_bus_lock(&nsblk->common.dev);
349 size = __nd_namespace_blk_validate(nsblk);
350 nvdimm_bus_unlock(&nsblk->common.dev);
354 EXPORT_SYMBOL(nd_namespace_blk_validate);
357 static int nd_namespace_label_update(struct nd_region *nd_region,
360 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
361 "namespace must be idle during label update\n");
362 if (dev->driver || to_ndns(dev)->claim)
366 * Only allow label writes that will result in a valid namespace
367 * or deletion of an existing namespace.
369 if (is_namespace_pmem(dev)) {
370 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
371 resource_size_t size = resource_size(&nspm->nsio.res);
373 if (size == 0 && nspm->uuid)
374 /* delete allocation */;
375 else if (!nspm->uuid)
378 return nd_pmem_namespace_label_update(nd_region, nspm, size);
379 } else if (is_namespace_blk(dev)) {
380 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
381 resource_size_t size = nd_namespace_blk_size(nsblk);
383 if (size == 0 && nsblk->uuid)
384 /* delete allocation */;
385 else if (!nsblk->uuid || !nsblk->lbasize)
388 return nd_blk_namespace_label_update(nd_region, nsblk, size);
393 static ssize_t alt_name_store(struct device *dev,
394 struct device_attribute *attr, const char *buf, size_t len)
396 struct nd_region *nd_region = to_nd_region(dev->parent);
400 nvdimm_bus_lock(dev);
401 wait_nvdimm_bus_probe_idle(dev);
402 rc = __alt_name_store(dev, buf, len);
404 rc = nd_namespace_label_update(nd_region, dev);
405 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
406 nvdimm_bus_unlock(dev);
409 return rc < 0 ? rc : len;
412 static ssize_t alt_name_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
417 if (is_namespace_pmem(dev)) {
418 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
420 ns_altname = nspm->alt_name;
421 } else if (is_namespace_blk(dev)) {
422 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
424 ns_altname = nsblk->alt_name;
428 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
430 static DEVICE_ATTR_RW(alt_name);
432 static int scan_free(struct nd_region *nd_region,
433 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
436 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
437 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
441 struct resource *res, *last;
442 resource_size_t new_start;
445 for_each_dpa_resource(ndd, res)
446 if (strcmp(res->name, label_id->id) == 0)
452 if (n >= resource_size(res)) {
453 n -= resource_size(res);
454 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
455 nvdimm_free_dpa(ndd, res);
456 /* retry with last resource deleted */
461 * Keep BLK allocations relegated to high DPA as much as
465 new_start = res->start + n;
467 new_start = res->start;
469 rc = adjust_resource(res, new_start, resource_size(res) - n);
471 res->flags |= DPA_RESOURCE_ADJUSTED;
472 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
480 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
481 * @nd_region: the set of dimms to reclaim @n bytes from
482 * @label_id: unique identifier for the namespace consuming this dpa range
483 * @n: number of bytes per-dimm to release
485 * Assumes resources are ordered. Starting from the end try to
486 * adjust_resource() the allocation to @n, but if @n is larger than the
487 * allocation delete it and find the 'new' last allocation in the label
490 static int shrink_dpa_allocation(struct nd_region *nd_region,
491 struct nd_label_id *label_id, resource_size_t n)
495 for (i = 0; i < nd_region->ndr_mappings; i++) {
496 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
499 rc = scan_free(nd_region, nd_mapping, label_id, n);
507 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
508 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
511 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
512 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
513 resource_size_t first_dpa;
514 struct resource *res;
517 /* allocate blk from highest dpa first */
519 first_dpa = nd_mapping->start + nd_mapping->size - n;
521 first_dpa = nd_mapping->start;
523 /* first resource allocation for this label-id or dimm */
524 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
528 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
532 static bool space_valid(bool is_pmem, bool is_reserve,
533 struct nd_label_id *label_id, struct resource *res)
536 * For BLK-space any space is valid, for PMEM-space, it must be
537 * contiguous with an existing allocation unless we are
540 if (is_reserve || !is_pmem)
542 if (!res || strcmp(res->name, label_id->id) == 0)
548 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
551 static resource_size_t scan_allocate(struct nd_region *nd_region,
552 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
555 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
556 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
557 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
558 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
559 const resource_size_t to_allocate = n;
560 struct resource *res;
565 for_each_dpa_resource(ndd, res) {
566 resource_size_t allocate, available = 0, free_start, free_end;
567 struct resource *next = res->sibling, *new_res = NULL;
568 enum alloc_loc loc = ALLOC_ERR;
572 /* ignore resources outside this nd_mapping */
573 if (res->start > mapping_end)
575 if (res->end < nd_mapping->start)
578 /* space at the beginning of the mapping */
579 if (!first++ && res->start > nd_mapping->start) {
580 free_start = nd_mapping->start;
581 available = res->start - free_start;
582 if (space_valid(is_pmem, is_reserve, label_id, NULL))
586 /* space between allocations */
588 free_start = res->start + resource_size(res);
589 free_end = min(mapping_end, next->start - 1);
590 if (space_valid(is_pmem, is_reserve, label_id, res)
591 && free_start < free_end) {
592 available = free_end + 1 - free_start;
597 /* space at the end of the mapping */
599 free_start = res->start + resource_size(res);
600 free_end = mapping_end;
601 if (space_valid(is_pmem, is_reserve, label_id, res)
602 && free_start < free_end) {
603 available = free_end + 1 - free_start;
608 if (!loc || !available)
610 allocate = min(available, n);
613 if (strcmp(res->name, label_id->id) == 0) {
614 /* adjust current resource up */
615 if (is_pmem && !is_reserve)
617 rc = adjust_resource(res, res->start - allocate,
618 resource_size(res) + allocate);
619 action = "cur grow up";
624 if (strcmp(next->name, label_id->id) == 0) {
625 /* adjust next resource up */
626 if (is_pmem && !is_reserve)
628 rc = adjust_resource(next, next->start
629 - allocate, resource_size(next)
632 action = "next grow up";
633 } else if (strcmp(res->name, label_id->id) == 0) {
634 action = "grow down";
639 if (strcmp(res->name, label_id->id) == 0)
640 action = "grow down";
648 if (strcmp(action, "allocate") == 0) {
649 /* BLK allocate bottom up */
651 free_start += available - allocate;
652 else if (!is_reserve && free_start != nd_mapping->start)
655 new_res = nvdimm_allocate_dpa(ndd, label_id,
656 free_start, allocate);
659 } else if (strcmp(action, "grow down") == 0) {
660 /* adjust current resource down */
661 rc = adjust_resource(res, res->start, resource_size(res)
664 res->flags |= DPA_RESOURCE_ADJUSTED;
670 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
679 * Retry scan with newly inserted resources.
680 * For example, if we did an ALLOC_BEFORE
681 * insertion there may also have been space
682 * available for an ALLOC_AFTER insertion, so we
683 * need to check this same resource again
691 * If we allocated nothing in the BLK case it may be because we are in
692 * an initial "pmem-reserve pass". Only do an initial BLK allocation
693 * when none of the DPA space is reserved.
695 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
696 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
700 static int merge_dpa(struct nd_region *nd_region,
701 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
703 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
704 struct resource *res;
706 if (strncmp("pmem", label_id->id, 4) == 0)
709 for_each_dpa_resource(ndd, res) {
711 struct resource *next = res->sibling;
712 resource_size_t end = res->start + resource_size(res);
714 if (!next || strcmp(res->name, label_id->id) != 0
715 || strcmp(next->name, label_id->id) != 0
716 || end != next->start)
718 end += resource_size(next);
719 nvdimm_free_dpa(ndd, next);
720 rc = adjust_resource(res, res->start, end - res->start);
721 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
724 res->flags |= DPA_RESOURCE_ADJUSTED;
731 static int __reserve_free_pmem(struct device *dev, void *data)
733 struct nvdimm *nvdimm = data;
734 struct nd_region *nd_region;
735 struct nd_label_id label_id;
738 if (!is_nd_pmem(dev))
741 nd_region = to_nd_region(dev);
742 if (nd_region->ndr_mappings == 0)
745 memset(&label_id, 0, sizeof(label_id));
746 strcat(label_id.id, "pmem-reserve");
747 for (i = 0; i < nd_region->ndr_mappings; i++) {
748 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
749 resource_size_t n, rem = 0;
751 if (nd_mapping->nvdimm != nvdimm)
754 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
757 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
758 dev_WARN_ONCE(&nd_region->dev, rem,
759 "pmem reserve underrun: %#llx of %#llx bytes\n",
760 (unsigned long long) n - rem,
761 (unsigned long long) n);
762 return rem ? -ENXIO : 0;
768 static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
769 struct nd_mapping *nd_mapping)
771 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
772 struct resource *res, *_res;
774 for_each_dpa_resource_safe(ndd, res, _res)
775 if (strcmp(res->name, "pmem-reserve") == 0)
776 nvdimm_free_dpa(ndd, res);
779 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
780 struct nd_mapping *nd_mapping)
782 struct nvdimm *nvdimm = nd_mapping->nvdimm;
785 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
786 __reserve_free_pmem);
788 release_free_pmem(nvdimm_bus, nd_mapping);
793 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
794 * @nd_region: the set of dimms to allocate @n more bytes from
795 * @label_id: unique identifier for the namespace consuming this dpa range
796 * @n: number of bytes per-dimm to add to the existing allocation
798 * Assumes resources are ordered. For BLK regions, first consume
799 * BLK-only available DPA free space, then consume PMEM-aliased DPA
800 * space starting at the highest DPA. For PMEM regions start
801 * allocations from the start of an interleave set and end at the first
802 * BLK allocation or the end of the interleave set, whichever comes
805 static int grow_dpa_allocation(struct nd_region *nd_region,
806 struct nd_label_id *label_id, resource_size_t n)
808 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
809 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
812 for (i = 0; i < nd_region->ndr_mappings; i++) {
813 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
814 resource_size_t rem = n;
818 * In the BLK case try once with all unallocated PMEM
819 * reserved, and once without
821 for (j = is_pmem; j < 2; j++) {
822 bool blk_only = j == 0;
825 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
829 rem = scan_allocate(nd_region, nd_mapping,
832 release_free_pmem(nvdimm_bus, nd_mapping);
834 /* try again and allow encroachments into PMEM */
839 dev_WARN_ONCE(&nd_region->dev, rem,
840 "allocation underrun: %#llx of %#llx bytes\n",
841 (unsigned long long) n - rem,
842 (unsigned long long) n);
846 rc = merge_dpa(nd_region, nd_mapping, label_id);
854 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
855 struct nd_namespace_pmem *nspm, resource_size_t size)
857 struct resource *res = &nspm->nsio.res;
858 resource_size_t offset = 0;
860 if (size && !nspm->uuid) {
865 if (size && nspm->uuid) {
866 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
867 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
868 struct nd_label_id label_id;
869 struct resource *res;
876 nd_label_gen_id(&label_id, nspm->uuid, 0);
878 /* calculate a spa offset from the dpa allocation offset */
879 for_each_dpa_resource(ndd, res)
880 if (strcmp(res->name, label_id.id) == 0) {
881 offset = (res->start - nd_mapping->start)
882 * nd_region->ndr_mappings;
891 res->start = nd_region->ndr_start + offset;
892 res->end = res->start + size - 1;
895 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
898 dev_dbg(dev, "%s: uuid not set\n", where);
904 static ssize_t __size_store(struct device *dev, unsigned long long val)
906 resource_size_t allocated = 0, available = 0;
907 struct nd_region *nd_region = to_nd_region(dev->parent);
908 struct nd_mapping *nd_mapping;
909 struct nvdimm_drvdata *ndd;
910 struct nd_label_id label_id;
911 u32 flags = 0, remainder;
915 if (dev->driver || to_ndns(dev)->claim)
918 if (is_namespace_pmem(dev)) {
919 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
922 } else if (is_namespace_blk(dev)) {
923 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
926 flags = NSLABEL_FLAG_LOCAL;
930 * We need a uuid for the allocation-label and dimm(s) on which
931 * to store the label.
933 if (uuid_not_set(uuid, dev, __func__))
935 if (nd_region->ndr_mappings == 0) {
936 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
940 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
942 dev_dbg(dev, "%llu is not %dK aligned\n", val,
943 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
947 nd_label_gen_id(&label_id, uuid, flags);
948 for (i = 0; i < nd_region->ndr_mappings; i++) {
949 nd_mapping = &nd_region->mapping[i];
950 ndd = to_ndd(nd_mapping);
953 * All dimms in an interleave set, or the base dimm for a blk
954 * region, need to be enabled for the size to be changed.
959 allocated += nvdimm_allocated_dpa(ndd, &label_id);
961 available = nd_region_available_dpa(nd_region);
963 if (val > available + allocated)
966 if (val == allocated)
969 val = div_u64(val, nd_region->ndr_mappings);
970 allocated = div_u64(allocated, nd_region->ndr_mappings);
972 rc = shrink_dpa_allocation(nd_region, &label_id,
975 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
980 if (is_namespace_pmem(dev)) {
981 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
983 nd_namespace_pmem_set_resource(nd_region, nspm,
984 val * nd_region->ndr_mappings);
985 } else if (is_namespace_blk(dev)) {
986 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
989 * Try to delete the namespace if we deleted all of its
990 * allocation, this is not the seed device for the
991 * region, and it is not actively claimed by a btt
994 if (val == 0 && nd_region->ns_seed != dev
995 && !nsblk->common.claim)
996 nd_device_unregister(dev, ND_ASYNC);
1002 static ssize_t size_store(struct device *dev,
1003 struct device_attribute *attr, const char *buf, size_t len)
1005 struct nd_region *nd_region = to_nd_region(dev->parent);
1006 unsigned long long val;
1010 rc = kstrtoull(buf, 0, &val);
1015 nvdimm_bus_lock(dev);
1016 wait_nvdimm_bus_probe_idle(dev);
1017 rc = __size_store(dev, val);
1019 rc = nd_namespace_label_update(nd_region, dev);
1021 if (is_namespace_pmem(dev)) {
1022 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1025 } else if (is_namespace_blk(dev)) {
1026 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1028 uuid = &nsblk->uuid;
1031 if (rc == 0 && val == 0 && uuid) {
1032 /* setting size zero == 'delete namespace' */
1037 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
1038 ? "fail" : "success", rc);
1040 nvdimm_bus_unlock(dev);
1043 return rc < 0 ? rc : len;
1046 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1048 struct device *dev = &ndns->dev;
1050 if (is_namespace_pmem(dev)) {
1051 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1053 return resource_size(&nspm->nsio.res);
1054 } else if (is_namespace_blk(dev)) {
1055 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1056 } else if (is_namespace_io(dev)) {
1057 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1059 return resource_size(&nsio->res);
1061 WARN_ONCE(1, "unknown namespace type\n");
1065 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1067 resource_size_t size;
1069 nvdimm_bus_lock(&ndns->dev);
1070 size = __nvdimm_namespace_capacity(ndns);
1071 nvdimm_bus_unlock(&ndns->dev);
1075 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1077 static ssize_t size_show(struct device *dev,
1078 struct device_attribute *attr, char *buf)
1080 return sprintf(buf, "%llu\n", (unsigned long long)
1081 nvdimm_namespace_capacity(to_ndns(dev)));
1083 static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
1085 static u8 *namespace_to_uuid(struct device *dev)
1087 if (is_namespace_pmem(dev)) {
1088 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1091 } else if (is_namespace_blk(dev)) {
1092 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1096 return ERR_PTR(-ENXIO);
1099 static ssize_t uuid_show(struct device *dev,
1100 struct device_attribute *attr, char *buf)
1102 u8 *uuid = namespace_to_uuid(dev);
1105 return PTR_ERR(uuid);
1107 return sprintf(buf, "%pUb\n", uuid);
1108 return sprintf(buf, "\n");
1112 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1113 * @nd_region: parent region so we can updates all dimms in the set
1114 * @dev: namespace type for generating label_id
1115 * @new_uuid: incoming uuid
1116 * @old_uuid: reference to the uuid storage location in the namespace object
1118 static int namespace_update_uuid(struct nd_region *nd_region,
1119 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1121 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1122 struct nd_label_id old_label_id;
1123 struct nd_label_id new_label_id;
1126 if (!nd_is_uuid_unique(dev, new_uuid))
1129 if (*old_uuid == NULL)
1133 * If we've already written a label with this uuid, then it's
1134 * too late to rename because we can't reliably update the uuid
1135 * without losing the old namespace. Userspace must delete this
1136 * namespace to abandon the old uuid.
1138 for (i = 0; i < nd_region->ndr_mappings; i++) {
1139 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1142 * This check by itself is sufficient because old_uuid
1143 * would be NULL above if this uuid did not exist in the
1144 * currently written set.
1146 * FIXME: can we delete uuid with zero dpa allocated?
1148 if (list_empty(&nd_mapping->labels))
1152 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1153 nd_label_gen_id(&new_label_id, new_uuid, flags);
1154 for (i = 0; i < nd_region->ndr_mappings; i++) {
1155 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1156 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1157 struct resource *res;
1159 for_each_dpa_resource(ndd, res)
1160 if (strcmp(res->name, old_label_id.id) == 0)
1161 sprintf((void *) res->name, "%s",
1166 *old_uuid = new_uuid;
1170 static ssize_t uuid_store(struct device *dev,
1171 struct device_attribute *attr, const char *buf, size_t len)
1173 struct nd_region *nd_region = to_nd_region(dev->parent);
1178 if (is_namespace_pmem(dev)) {
1179 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1181 ns_uuid = &nspm->uuid;
1182 } else if (is_namespace_blk(dev)) {
1183 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1185 ns_uuid = &nsblk->uuid;
1190 nvdimm_bus_lock(dev);
1191 wait_nvdimm_bus_probe_idle(dev);
1192 if (to_ndns(dev)->claim)
1195 rc = nd_uuid_store(dev, &uuid, buf, len);
1197 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1199 rc = nd_namespace_label_update(nd_region, dev);
1202 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1203 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1204 nvdimm_bus_unlock(dev);
1207 return rc < 0 ? rc : len;
1209 static DEVICE_ATTR_RW(uuid);
1211 static ssize_t resource_show(struct device *dev,
1212 struct device_attribute *attr, char *buf)
1214 struct resource *res;
1216 if (is_namespace_pmem(dev)) {
1217 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1219 res = &nspm->nsio.res;
1220 } else if (is_namespace_io(dev)) {
1221 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1227 /* no address to convey if the namespace has no allocation */
1228 if (resource_size(res) == 0)
1230 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1232 static DEVICE_ATTR_RO(resource);
1234 static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1235 4096, 4104, 4160, 4224, 0 };
1237 static ssize_t sector_size_show(struct device *dev,
1238 struct device_attribute *attr, char *buf)
1240 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1242 if (!is_namespace_blk(dev))
1245 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1248 static ssize_t sector_size_store(struct device *dev,
1249 struct device_attribute *attr, const char *buf, size_t len)
1251 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1252 struct nd_region *nd_region = to_nd_region(dev->parent);
1255 if (!is_namespace_blk(dev))
1259 nvdimm_bus_lock(dev);
1260 if (to_ndns(dev)->claim)
1263 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1264 ns_lbasize_supported);
1266 rc = nd_namespace_label_update(nd_region, dev);
1267 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1268 rc, rc < 0 ? "tried" : "wrote", buf,
1269 buf[len - 1] == '\n' ? "" : "\n");
1270 nvdimm_bus_unlock(dev);
1273 return rc ? rc : len;
1275 static DEVICE_ATTR_RW(sector_size);
1277 static ssize_t dpa_extents_show(struct device *dev,
1278 struct device_attribute *attr, char *buf)
1280 struct nd_region *nd_region = to_nd_region(dev->parent);
1281 struct nd_label_id label_id;
1286 nvdimm_bus_lock(dev);
1287 if (is_namespace_pmem(dev)) {
1288 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1292 } else if (is_namespace_blk(dev)) {
1293 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1296 flags = NSLABEL_FLAG_LOCAL;
1302 nd_label_gen_id(&label_id, uuid, flags);
1303 for (i = 0; i < nd_region->ndr_mappings; i++) {
1304 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1305 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1306 struct resource *res;
1308 for_each_dpa_resource(ndd, res)
1309 if (strcmp(res->name, label_id.id) == 0)
1313 nvdimm_bus_unlock(dev);
1315 return sprintf(buf, "%d\n", count);
1317 static DEVICE_ATTR_RO(dpa_extents);
1319 static ssize_t holder_show(struct device *dev,
1320 struct device_attribute *attr, char *buf)
1322 struct nd_namespace_common *ndns = to_ndns(dev);
1326 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1331 static DEVICE_ATTR_RO(holder);
1333 static ssize_t mode_show(struct device *dev,
1334 struct device_attribute *attr, char *buf)
1336 struct nd_namespace_common *ndns = to_ndns(dev);
1337 struct device *claim;
1342 claim = ndns->claim;
1343 if (claim && is_nd_btt(claim))
1345 else if (claim && is_nd_pfn(claim))
1347 else if (claim && is_nd_dax(claim))
1349 else if (!claim && pmem_should_map_pages(dev))
1353 rc = sprintf(buf, "%s\n", mode);
1358 static DEVICE_ATTR_RO(mode);
1360 static ssize_t force_raw_store(struct device *dev,
1361 struct device_attribute *attr, const char *buf, size_t len)
1364 int rc = strtobool(buf, &force_raw);
1369 to_ndns(dev)->force_raw = force_raw;
1373 static ssize_t force_raw_show(struct device *dev,
1374 struct device_attribute *attr, char *buf)
1376 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1378 static DEVICE_ATTR_RW(force_raw);
1380 static struct attribute *nd_namespace_attributes[] = {
1381 &dev_attr_nstype.attr,
1382 &dev_attr_size.attr,
1383 &dev_attr_mode.attr,
1384 &dev_attr_uuid.attr,
1385 &dev_attr_holder.attr,
1386 &dev_attr_resource.attr,
1387 &dev_attr_alt_name.attr,
1388 &dev_attr_force_raw.attr,
1389 &dev_attr_sector_size.attr,
1390 &dev_attr_dpa_extents.attr,
1394 static umode_t namespace_visible(struct kobject *kobj,
1395 struct attribute *a, int n)
1397 struct device *dev = container_of(kobj, struct device, kobj);
1399 if (a == &dev_attr_resource.attr) {
1400 if (is_namespace_blk(dev))
1405 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1406 if (a == &dev_attr_size.attr)
1407 return S_IWUSR | S_IRUGO;
1409 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1415 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1416 || a == &dev_attr_holder.attr
1417 || a == &dev_attr_force_raw.attr
1418 || a == &dev_attr_mode.attr)
1424 static struct attribute_group nd_namespace_attribute_group = {
1425 .attrs = nd_namespace_attributes,
1426 .is_visible = namespace_visible,
1429 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1430 &nd_device_attribute_group,
1431 &nd_namespace_attribute_group,
1432 &nd_numa_attribute_group,
1436 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1438 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1439 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1440 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1441 struct nd_namespace_common *ndns = NULL;
1442 resource_size_t size;
1444 if (nd_btt || nd_pfn || nd_dax) {
1446 ndns = nd_btt->ndns;
1448 ndns = nd_pfn->ndns;
1450 ndns = nd_dax->nd_pfn.ndns;
1453 return ERR_PTR(-ENODEV);
1456 * Flush any in-progess probes / removals in the driver
1457 * for the raw personality of this namespace.
1459 device_lock(&ndns->dev);
1460 device_unlock(&ndns->dev);
1461 if (ndns->dev.driver) {
1462 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1464 return ERR_PTR(-EBUSY);
1466 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1467 "host (%s) vs claim (%s) mismatch\n",
1469 dev_name(ndns->claim)))
1470 return ERR_PTR(-ENXIO);
1472 ndns = to_ndns(dev);
1474 dev_dbg(dev, "claimed by %s, failing probe\n",
1475 dev_name(ndns->claim));
1477 return ERR_PTR(-ENXIO);
1481 size = nvdimm_namespace_capacity(ndns);
1482 if (size < ND_MIN_NAMESPACE_SIZE) {
1483 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1484 &size, ND_MIN_NAMESPACE_SIZE);
1485 return ERR_PTR(-ENODEV);
1488 if (is_namespace_pmem(&ndns->dev)) {
1489 struct nd_namespace_pmem *nspm;
1491 nspm = to_nd_namespace_pmem(&ndns->dev);
1492 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1493 return ERR_PTR(-ENODEV);
1494 } else if (is_namespace_blk(&ndns->dev)) {
1495 struct nd_namespace_blk *nsblk;
1497 nsblk = to_nd_namespace_blk(&ndns->dev);
1498 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1499 return ERR_PTR(-ENODEV);
1500 if (!nsblk->lbasize) {
1501 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1503 return ERR_PTR(-ENODEV);
1505 if (!nd_namespace_blk_validate(nsblk))
1506 return ERR_PTR(-ENODEV);
1511 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1513 static struct device **create_namespace_io(struct nd_region *nd_region)
1515 struct nd_namespace_io *nsio;
1516 struct device *dev, **devs;
1517 struct resource *res;
1519 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1523 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1529 dev = &nsio->common.dev;
1530 dev->type = &namespace_io_device_type;
1531 dev->parent = &nd_region->dev;
1533 res->name = dev_name(&nd_region->dev);
1534 res->flags = IORESOURCE_MEM;
1535 res->start = nd_region->ndr_start;
1536 res->end = res->start + nd_region->ndr_size - 1;
1542 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1543 u64 cookie, u16 pos)
1545 struct nd_namespace_label *found = NULL;
1548 for (i = 0; i < nd_region->ndr_mappings; i++) {
1549 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1550 struct nd_label_ent *label_ent;
1551 bool found_uuid = false;
1553 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1554 struct nd_namespace_label *nd_label = label_ent->label;
1555 u16 position, nlabel;
1560 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1561 position = __le16_to_cpu(nd_label->position);
1562 nlabel = __le16_to_cpu(nd_label->nlabel);
1564 if (isetcookie != cookie)
1567 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1571 dev_dbg(to_ndd(nd_mapping)->dev,
1572 "%s duplicate entry for uuid\n",
1577 if (nlabel != nd_region->ndr_mappings)
1579 if (position != pos)
1587 return found != NULL;
1590 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1597 for (i = 0; i < nd_region->ndr_mappings; i++) {
1598 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1599 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1600 struct nd_namespace_label *nd_label = NULL;
1601 u64 hw_start, hw_end, pmem_start, pmem_end;
1602 struct nd_label_ent *label_ent;
1604 WARN_ON(!mutex_is_locked(&nd_mapping->lock));
1605 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1606 nd_label = label_ent->label;
1609 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1620 * Check that this label is compliant with the dpa
1621 * range published in NFIT
1623 hw_start = nd_mapping->start;
1624 hw_end = hw_start + nd_mapping->size;
1625 pmem_start = __le64_to_cpu(nd_label->dpa);
1626 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1627 if (pmem_start >= hw_start && pmem_start < hw_end
1628 && pmem_end <= hw_end && pmem_end > hw_start)
1631 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1632 dev_name(ndd->dev), nd_label->uuid);
1636 /* move recently validated label to the front of the list */
1637 list_move(&label_ent->list, &nd_mapping->labels);
1643 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1644 * @nd_region: region with mappings to validate
1645 * @nspm: target namespace to create
1646 * @nd_label: target pmem namespace label to evaluate
1648 struct device *create_namespace_pmem(struct nd_region *nd_region,
1649 struct nd_namespace_label *nd_label)
1651 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1652 struct nd_label_ent *label_ent;
1653 struct nd_namespace_pmem *nspm;
1654 struct nd_mapping *nd_mapping;
1655 resource_size_t size = 0;
1656 struct resource *res;
1662 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1663 return ERR_PTR(-ENXIO);
1666 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1667 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1669 return ERR_PTR(-EAGAIN);
1672 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1674 return ERR_PTR(-ENOMEM);
1677 dev = &nspm->nsio.common.dev;
1678 dev->type = &namespace_pmem_device_type;
1679 dev->parent = &nd_region->dev;
1680 res = &nspm->nsio.res;
1681 res->name = dev_name(&nd_region->dev);
1682 res->flags = IORESOURCE_MEM;
1684 for (i = 0; i < nd_region->ndr_mappings; i++)
1685 if (!has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1687 if (i < nd_region->ndr_mappings) {
1688 struct nvdimm_drvdata *ndd = to_ndd(&nd_region->mapping[i]);
1691 * Give up if we don't find an instance of a uuid at each
1692 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1693 * find a dimm with two instances of the same uuid.
1695 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1696 dev_name(ndd->dev), nd_label->uuid);
1702 * Fix up each mapping's 'labels' to have the validated pmem label for
1703 * that position at labels[0], and NULL at labels[1]. In the process,
1704 * check that the namespace aligns with interleave-set. We know
1705 * that it does not overlap with any blk namespaces by virtue of
1706 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1709 rc = select_pmem_id(nd_region, nd_label->uuid);
1713 /* Calculate total size and populate namespace properties from label0 */
1714 for (i = 0; i < nd_region->ndr_mappings; i++) {
1715 struct nd_namespace_label *label0;
1717 nd_mapping = &nd_region->mapping[i];
1718 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1719 typeof(*label_ent), list);
1720 label0 = label_ent ? label_ent->label : 0;
1727 size += __le64_to_cpu(label0->rawsize);
1728 if (__le16_to_cpu(label0->position) != 0)
1730 WARN_ON(nspm->alt_name || nspm->uuid);
1731 nspm->alt_name = kmemdup((void __force *) label0->name,
1732 NSLABEL_NAME_LEN, GFP_KERNEL);
1733 nspm->uuid = kmemdup((void __force *) label0->uuid,
1734 NSLABEL_UUID_LEN, GFP_KERNEL);
1737 if (!nspm->alt_name || !nspm->uuid) {
1742 nd_namespace_pmem_set_resource(nd_region, nspm, size);
1746 namespace_pmem_release(dev);
1749 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1752 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1755 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1762 struct resource *nsblk_add_resource(struct nd_region *nd_region,
1763 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1764 resource_size_t start)
1766 struct nd_label_id label_id;
1767 struct resource *res;
1769 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1770 res = krealloc(nsblk->res,
1771 sizeof(void *) * (nsblk->num_resources + 1),
1775 nsblk->res = (struct resource **) res;
1776 for_each_dpa_resource(ndd, res)
1777 if (strcmp(res->name, label_id.id) == 0
1778 && res->start == start) {
1779 nsblk->res[nsblk->num_resources++] = res;
1785 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1787 struct nd_namespace_blk *nsblk;
1790 if (!is_nd_blk(&nd_region->dev))
1793 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1797 dev = &nsblk->common.dev;
1798 dev->type = &namespace_blk_device_type;
1799 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1800 if (nsblk->id < 0) {
1804 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1805 dev->parent = &nd_region->dev;
1806 dev->groups = nd_namespace_attribute_groups;
1808 return &nsblk->common.dev;
1811 void nd_region_create_blk_seed(struct nd_region *nd_region)
1813 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1814 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1816 * Seed creation failures are not fatal, provisioning is simply
1817 * disabled until memory becomes available
1819 if (!nd_region->ns_seed)
1820 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1822 nd_device_register(nd_region->ns_seed);
1825 void nd_region_create_dax_seed(struct nd_region *nd_region)
1827 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1828 nd_region->dax_seed = nd_dax_create(nd_region);
1830 * Seed creation failures are not fatal, provisioning is simply
1831 * disabled until memory becomes available
1833 if (!nd_region->dax_seed)
1834 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1837 void nd_region_create_pfn_seed(struct nd_region *nd_region)
1839 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1840 nd_region->pfn_seed = nd_pfn_create(nd_region);
1842 * Seed creation failures are not fatal, provisioning is simply
1843 * disabled until memory becomes available
1845 if (!nd_region->pfn_seed)
1846 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1849 void nd_region_create_btt_seed(struct nd_region *nd_region)
1851 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1852 nd_region->btt_seed = nd_btt_create(nd_region);
1854 * Seed creation failures are not fatal, provisioning is simply
1855 * disabled until memory becomes available
1857 if (!nd_region->btt_seed)
1858 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1861 static int add_namespace_resource(struct nd_region *nd_region,
1862 struct nd_namespace_label *nd_label, struct device **devs,
1865 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1866 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1869 for (i = 0; i < count; i++) {
1870 u8 *uuid = namespace_to_uuid(devs[i]);
1871 struct resource *res;
1873 if (IS_ERR_OR_NULL(uuid)) {
1878 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
1880 if (is_namespace_blk(devs[i])) {
1881 res = nsblk_add_resource(nd_region, ndd,
1882 to_nd_namespace_blk(devs[i]),
1883 __le64_to_cpu(nd_label->dpa));
1886 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
1888 dev_err(&nd_region->dev,
1889 "error: conflicting extents for uuid: %pUb\n",
1899 struct device *create_namespace_blk(struct nd_region *nd_region,
1900 struct nd_namespace_label *nd_label, int count)
1903 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1904 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1905 struct nd_namespace_blk *nsblk;
1906 char *name[NSLABEL_NAME_LEN];
1907 struct device *dev = NULL;
1908 struct resource *res;
1910 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1912 return ERR_PTR(-ENOMEM);
1913 dev = &nsblk->common.dev;
1914 dev->type = &namespace_blk_device_type;
1915 dev->parent = &nd_region->dev;
1917 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1918 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1922 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1924 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1926 res = nsblk_add_resource(nd_region, ndd, nsblk,
1927 __le64_to_cpu(nd_label->dpa));
1930 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
1933 namespace_blk_release(dev);
1934 return ERR_PTR(-ENXIO);
1937 static int cmp_dpa(const void *a, const void *b)
1939 const struct device *dev_a = *(const struct device **) a;
1940 const struct device *dev_b = *(const struct device **) b;
1941 struct nd_namespace_blk *nsblk_a, *nsblk_b;
1942 struct nd_namespace_pmem *nspm_a, *nspm_b;
1944 if (is_namespace_io(dev_a))
1947 if (is_namespace_blk(dev_a)) {
1948 nsblk_a = to_nd_namespace_blk(dev_a);
1949 nsblk_b = to_nd_namespace_blk(dev_b);
1951 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
1952 sizeof(resource_size_t));
1955 nspm_a = to_nd_namespace_pmem(dev_a);
1956 nspm_b = to_nd_namespace_pmem(dev_b);
1958 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
1959 sizeof(resource_size_t));
1962 static struct device **scan_labels(struct nd_region *nd_region)
1964 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1965 struct device *dev, **devs = NULL;
1966 struct nd_label_ent *label_ent, *e;
1969 /* "safe" because create_namespace_pmem() might list_move() label_ent */
1970 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1971 struct nd_namespace_label *nd_label = label_ent->label;
1972 struct device **__devs;
1977 flags = __le32_to_cpu(nd_label->flags);
1978 if (is_nd_blk(&nd_region->dev)
1979 == !!(flags & NSLABEL_FLAG_LOCAL))
1980 /* pass, region matches label type */;
1984 i = add_namespace_resource(nd_region, nd_label, devs, count);
1989 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1992 memcpy(__devs, devs, sizeof(dev) * count);
1996 if (is_nd_blk(&nd_region->dev)) {
1997 dev = create_namespace_blk(nd_region, nd_label, count);
2000 devs[count++] = dev;
2002 dev = create_namespace_pmem(nd_region, nd_label);
2004 switch (PTR_ERR(dev)) {
2006 /* skip invalid labels */
2009 /* fallthrough to seed creation */
2015 devs[count++] = dev;
2017 /* we only expect one valid pmem label set per region */
2022 dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
2023 __func__, count, is_nd_blk(&nd_region->dev)
2024 ? "blk" : "pmem", count == 1 ? "" : "s");
2027 /* Publish a zero-sized namespace for userspace to configure. */
2028 nd_mapping_free_labels(nd_mapping);
2030 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2033 if (is_nd_blk(&nd_region->dev)) {
2034 struct nd_namespace_blk *nsblk;
2036 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2039 dev = &nsblk->common.dev;
2040 dev->type = &namespace_blk_device_type;
2042 struct nd_namespace_pmem *nspm;
2044 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2047 dev = &nspm->nsio.common.dev;
2048 dev->type = &namespace_pmem_device_type;
2049 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2051 dev->parent = &nd_region->dev;
2052 devs[count++] = dev;
2053 } else if (is_nd_pmem(&nd_region->dev)) {
2054 /* clean unselected labels */
2055 for (i = 0; i < nd_region->ndr_mappings; i++) {
2056 struct list_head *l, *e;
2060 nd_mapping = &nd_region->mapping[i];
2061 if (list_empty(&nd_mapping->labels)) {
2067 list_for_each_safe(l, e, &nd_mapping->labels) {
2070 list_move_tail(l, &list);
2072 nd_mapping_free_labels(nd_mapping);
2073 list_splice_init(&list, &nd_mapping->labels);
2078 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2083 for (i = 0; devs[i]; i++)
2084 if (is_nd_blk(&nd_region->dev))
2085 namespace_blk_release(devs[i]);
2087 namespace_pmem_release(devs[i]);
2092 static struct device **create_namespaces(struct nd_region *nd_region)
2094 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2095 struct device **devs;
2098 if (nd_region->ndr_mappings == 0)
2101 /* lock down all mappings while we scan labels */
2102 for (i = 0; i < nd_region->ndr_mappings; i++) {
2103 nd_mapping = &nd_region->mapping[i];
2104 mutex_lock_nested(&nd_mapping->lock, i);
2107 devs = scan_labels(nd_region);
2109 for (i = 0; i < nd_region->ndr_mappings; i++) {
2110 int reverse = nd_region->ndr_mappings - 1 - i;
2112 nd_mapping = &nd_region->mapping[reverse];
2113 mutex_unlock(&nd_mapping->lock);
2119 static int init_active_labels(struct nd_region *nd_region)
2123 for (i = 0; i < nd_region->ndr_mappings; i++) {
2124 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2125 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2126 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2127 struct nd_label_ent *label_ent;
2131 * If the dimm is disabled then prevent the region from
2132 * being activated if it aliases DPA.
2135 if ((nvdimm->flags & NDD_ALIASING) == 0)
2137 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
2138 dev_name(&nd_mapping->nvdimm->dev));
2141 nd_mapping->ndd = ndd;
2142 atomic_inc(&nvdimm->busy);
2145 count = nd_label_active_count(ndd);
2146 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
2149 for (j = 0; j < count; j++) {
2150 struct nd_namespace_label *label;
2152 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2155 label = nd_label_active(ndd, j);
2156 label_ent->label = label;
2158 mutex_lock(&nd_mapping->lock);
2159 list_add_tail(&label_ent->list, &nd_mapping->labels);
2160 mutex_unlock(&nd_mapping->lock);
2166 mutex_lock(&nd_mapping->lock);
2167 nd_mapping_free_labels(nd_mapping);
2168 mutex_unlock(&nd_mapping->lock);
2175 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2177 struct device **devs = NULL;
2178 int i, rc = 0, type;
2181 nvdimm_bus_lock(&nd_region->dev);
2182 rc = init_active_labels(nd_region);
2184 nvdimm_bus_unlock(&nd_region->dev);
2188 type = nd_region_to_nstype(nd_region);
2190 case ND_DEVICE_NAMESPACE_IO:
2191 devs = create_namespace_io(nd_region);
2193 case ND_DEVICE_NAMESPACE_PMEM:
2194 case ND_DEVICE_NAMESPACE_BLK:
2195 devs = create_namespaces(nd_region);
2200 nvdimm_bus_unlock(&nd_region->dev);
2205 for (i = 0; devs[i]; i++) {
2206 struct device *dev = devs[i];
2209 if (type == ND_DEVICE_NAMESPACE_BLK) {
2210 struct nd_namespace_blk *nsblk;
2212 nsblk = to_nd_namespace_blk(dev);
2213 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2216 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2217 struct nd_namespace_pmem *nspm;
2219 nspm = to_nd_namespace_pmem(dev);
2220 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2228 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2229 dev->groups = nd_namespace_attribute_groups;
2230 nd_device_register(dev);
2233 nd_region->ns_seed = devs[0];
2238 for (j = i; devs[j]; j++) {
2239 struct device *dev = devs[j];
2241 device_initialize(dev);
2246 * All of the namespaces we tried to register failed, so
2247 * fail region activation.