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/slab.h>
20 static void namespace_io_release(struct device *dev)
22 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
27 static void namespace_pmem_release(struct device *dev)
29 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
31 kfree(nspm->alt_name);
36 static void namespace_blk_release(struct device *dev)
38 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
39 struct nd_region *nd_region = to_nd_region(dev->parent);
42 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
43 kfree(nsblk->alt_name);
49 static struct device_type namespace_io_device_type = {
50 .name = "nd_namespace_io",
51 .release = namespace_io_release,
54 static struct device_type namespace_pmem_device_type = {
55 .name = "nd_namespace_pmem",
56 .release = namespace_pmem_release,
59 static struct device_type namespace_blk_device_type = {
60 .name = "nd_namespace_blk",
61 .release = namespace_blk_release,
64 static bool is_namespace_pmem(struct device *dev)
66 return dev ? dev->type == &namespace_pmem_device_type : false;
69 static bool is_namespace_blk(struct device *dev)
71 return dev ? dev->type == &namespace_blk_device_type : false;
74 static bool is_namespace_io(struct device *dev)
76 return dev ? dev->type == &namespace_io_device_type : false;
79 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
82 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
83 const char *suffix = "";
85 if (ndns->claim && is_nd_btt(ndns->claim))
88 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev))
89 sprintf(name, "pmem%d%s", nd_region->id, suffix);
90 else if (is_namespace_blk(&ndns->dev)) {
91 struct nd_namespace_blk *nsblk;
93 nsblk = to_nd_namespace_blk(&ndns->dev);
94 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, suffix);
101 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
103 const u8 *nd_dev_to_uuid(struct device *dev)
105 static const u8 null_uuid[16];
110 if (is_namespace_pmem(dev)) {
111 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
114 } else if (is_namespace_blk(dev)) {
115 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
121 EXPORT_SYMBOL(nd_dev_to_uuid);
123 static ssize_t nstype_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
126 struct nd_region *nd_region = to_nd_region(dev->parent);
128 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
130 static DEVICE_ATTR_RO(nstype);
132 static ssize_t __alt_name_store(struct device *dev, const char *buf,
135 char *input, *pos, *alt_name, **ns_altname;
138 if (is_namespace_pmem(dev)) {
139 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
141 ns_altname = &nspm->alt_name;
142 } else if (is_namespace_blk(dev)) {
143 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
145 ns_altname = &nsblk->alt_name;
149 if (dev->driver || to_ndns(dev)->claim)
152 input = kmemdup(buf, len + 1, GFP_KERNEL);
158 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
163 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
169 *ns_altname = alt_name;
170 sprintf(*ns_altname, "%s", pos);
178 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
180 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
181 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
182 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
183 struct nd_label_id label_id;
184 resource_size_t size = 0;
185 struct resource *res;
189 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
190 for_each_dpa_resource(ndd, res)
191 if (strcmp(res->name, label_id.id) == 0)
192 size += resource_size(res);
196 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
198 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
199 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
200 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
201 struct nd_label_id label_id;
202 struct resource *res;
205 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
209 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
210 for_each_dpa_resource(ndd, res) {
211 if (strcmp(res->name, label_id.id) != 0)
214 * Resources with unacknoweldged adjustments indicate a
215 * failure to update labels
217 if (res->flags & DPA_RESOURCE_ADJUSTED)
222 /* These values match after a successful label update */
223 if (count != nsblk->num_resources)
226 for (i = 0; i < nsblk->num_resources; i++) {
227 struct resource *found = NULL;
229 for_each_dpa_resource(ndd, res)
230 if (res == nsblk->res[i]) {
242 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
244 resource_size_t size;
246 nvdimm_bus_lock(&nsblk->common.dev);
247 size = __nd_namespace_blk_validate(nsblk);
248 nvdimm_bus_unlock(&nsblk->common.dev);
252 EXPORT_SYMBOL(nd_namespace_blk_validate);
255 static int nd_namespace_label_update(struct nd_region *nd_region,
258 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
259 "namespace must be idle during label update\n");
260 if (dev->driver || to_ndns(dev)->claim)
264 * Only allow label writes that will result in a valid namespace
265 * or deletion of an existing namespace.
267 if (is_namespace_pmem(dev)) {
268 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
269 resource_size_t size = resource_size(&nspm->nsio.res);
271 if (size == 0 && nspm->uuid)
272 /* delete allocation */;
273 else if (!nspm->uuid)
276 return nd_pmem_namespace_label_update(nd_region, nspm, size);
277 } else if (is_namespace_blk(dev)) {
278 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
279 resource_size_t size = nd_namespace_blk_size(nsblk);
281 if (size == 0 && nsblk->uuid)
282 /* delete allocation */;
283 else if (!nsblk->uuid || !nsblk->lbasize)
286 return nd_blk_namespace_label_update(nd_region, nsblk, size);
291 static ssize_t alt_name_store(struct device *dev,
292 struct device_attribute *attr, const char *buf, size_t len)
294 struct nd_region *nd_region = to_nd_region(dev->parent);
298 nvdimm_bus_lock(dev);
299 wait_nvdimm_bus_probe_idle(dev);
300 rc = __alt_name_store(dev, buf, len);
302 rc = nd_namespace_label_update(nd_region, dev);
303 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
304 nvdimm_bus_unlock(dev);
307 return rc < 0 ? rc : len;
310 static ssize_t alt_name_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
315 if (is_namespace_pmem(dev)) {
316 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
318 ns_altname = nspm->alt_name;
319 } else if (is_namespace_blk(dev)) {
320 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
322 ns_altname = nsblk->alt_name;
326 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
328 static DEVICE_ATTR_RW(alt_name);
330 static int scan_free(struct nd_region *nd_region,
331 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
334 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
335 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
339 struct resource *res, *last;
340 resource_size_t new_start;
343 for_each_dpa_resource(ndd, res)
344 if (strcmp(res->name, label_id->id) == 0)
350 if (n >= resource_size(res)) {
351 n -= resource_size(res);
352 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
353 nvdimm_free_dpa(ndd, res);
354 /* retry with last resource deleted */
359 * Keep BLK allocations relegated to high DPA as much as
363 new_start = res->start + n;
365 new_start = res->start;
367 rc = adjust_resource(res, new_start, resource_size(res) - n);
369 res->flags |= DPA_RESOURCE_ADJUSTED;
370 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
378 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
379 * @nd_region: the set of dimms to reclaim @n bytes from
380 * @label_id: unique identifier for the namespace consuming this dpa range
381 * @n: number of bytes per-dimm to release
383 * Assumes resources are ordered. Starting from the end try to
384 * adjust_resource() the allocation to @n, but if @n is larger than the
385 * allocation delete it and find the 'new' last allocation in the label
388 static int shrink_dpa_allocation(struct nd_region *nd_region,
389 struct nd_label_id *label_id, resource_size_t n)
393 for (i = 0; i < nd_region->ndr_mappings; i++) {
394 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
397 rc = scan_free(nd_region, nd_mapping, label_id, n);
405 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
406 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
409 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
410 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
411 resource_size_t first_dpa;
412 struct resource *res;
415 /* allocate blk from highest dpa first */
417 first_dpa = nd_mapping->start + nd_mapping->size - n;
419 first_dpa = nd_mapping->start;
421 /* first resource allocation for this label-id or dimm */
422 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
426 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
430 static bool space_valid(bool is_pmem, bool is_reserve,
431 struct nd_label_id *label_id, struct resource *res)
434 * For BLK-space any space is valid, for PMEM-space, it must be
435 * contiguous with an existing allocation unless we are
438 if (is_reserve || !is_pmem)
440 if (!res || strcmp(res->name, label_id->id) == 0)
446 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
449 static resource_size_t scan_allocate(struct nd_region *nd_region,
450 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
453 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
454 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
455 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
456 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
457 const resource_size_t to_allocate = n;
458 struct resource *res;
463 for_each_dpa_resource(ndd, res) {
464 resource_size_t allocate, available = 0, free_start, free_end;
465 struct resource *next = res->sibling, *new_res = NULL;
466 enum alloc_loc loc = ALLOC_ERR;
470 /* ignore resources outside this nd_mapping */
471 if (res->start > mapping_end)
473 if (res->end < nd_mapping->start)
476 /* space at the beginning of the mapping */
477 if (!first++ && res->start > nd_mapping->start) {
478 free_start = nd_mapping->start;
479 available = res->start - free_start;
480 if (space_valid(is_pmem, is_reserve, label_id, NULL))
484 /* space between allocations */
486 free_start = res->start + resource_size(res);
487 free_end = min(mapping_end, next->start - 1);
488 if (space_valid(is_pmem, is_reserve, label_id, res)
489 && free_start < free_end) {
490 available = free_end + 1 - free_start;
495 /* space at the end of the mapping */
497 free_start = res->start + resource_size(res);
498 free_end = mapping_end;
499 if (space_valid(is_pmem, is_reserve, label_id, res)
500 && free_start < free_end) {
501 available = free_end + 1 - free_start;
506 if (!loc || !available)
508 allocate = min(available, n);
511 if (strcmp(res->name, label_id->id) == 0) {
512 /* adjust current resource up */
513 if (is_pmem && !is_reserve)
515 rc = adjust_resource(res, res->start - allocate,
516 resource_size(res) + allocate);
517 action = "cur grow up";
522 if (strcmp(next->name, label_id->id) == 0) {
523 /* adjust next resource up */
524 if (is_pmem && !is_reserve)
526 rc = adjust_resource(next, next->start
527 - allocate, resource_size(next)
530 action = "next grow up";
531 } else if (strcmp(res->name, label_id->id) == 0) {
532 action = "grow down";
537 if (strcmp(res->name, label_id->id) == 0)
538 action = "grow down";
546 if (strcmp(action, "allocate") == 0) {
547 /* BLK allocate bottom up */
549 free_start += available - allocate;
550 else if (!is_reserve && free_start != nd_mapping->start)
553 new_res = nvdimm_allocate_dpa(ndd, label_id,
554 free_start, allocate);
557 } else if (strcmp(action, "grow down") == 0) {
558 /* adjust current resource down */
559 rc = adjust_resource(res, res->start, resource_size(res)
562 res->flags |= DPA_RESOURCE_ADJUSTED;
568 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
577 * Retry scan with newly inserted resources.
578 * For example, if we did an ALLOC_BEFORE
579 * insertion there may also have been space
580 * available for an ALLOC_AFTER insertion, so we
581 * need to check this same resource again
589 * If we allocated nothing in the BLK case it may be because we are in
590 * an initial "pmem-reserve pass". Only do an initial BLK allocation
591 * when none of the DPA space is reserved.
593 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
594 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
598 static int merge_dpa(struct nd_region *nd_region,
599 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
601 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
602 struct resource *res;
604 if (strncmp("pmem", label_id->id, 4) == 0)
607 for_each_dpa_resource(ndd, res) {
609 struct resource *next = res->sibling;
610 resource_size_t end = res->start + resource_size(res);
612 if (!next || strcmp(res->name, label_id->id) != 0
613 || strcmp(next->name, label_id->id) != 0
614 || end != next->start)
616 end += resource_size(next);
617 nvdimm_free_dpa(ndd, next);
618 rc = adjust_resource(res, res->start, end - res->start);
619 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
622 res->flags |= DPA_RESOURCE_ADJUSTED;
629 static int __reserve_free_pmem(struct device *dev, void *data)
631 struct nvdimm *nvdimm = data;
632 struct nd_region *nd_region;
633 struct nd_label_id label_id;
636 if (!is_nd_pmem(dev))
639 nd_region = to_nd_region(dev);
640 if (nd_region->ndr_mappings == 0)
643 memset(&label_id, 0, sizeof(label_id));
644 strcat(label_id.id, "pmem-reserve");
645 for (i = 0; i < nd_region->ndr_mappings; i++) {
646 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
647 resource_size_t n, rem = 0;
649 if (nd_mapping->nvdimm != nvdimm)
652 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
655 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
656 dev_WARN_ONCE(&nd_region->dev, rem,
657 "pmem reserve underrun: %#llx of %#llx bytes\n",
658 (unsigned long long) n - rem,
659 (unsigned long long) n);
660 return rem ? -ENXIO : 0;
666 static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
667 struct nd_mapping *nd_mapping)
669 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
670 struct resource *res, *_res;
672 for_each_dpa_resource_safe(ndd, res, _res)
673 if (strcmp(res->name, "pmem-reserve") == 0)
674 nvdimm_free_dpa(ndd, res);
677 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
678 struct nd_mapping *nd_mapping)
680 struct nvdimm *nvdimm = nd_mapping->nvdimm;
683 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
684 __reserve_free_pmem);
686 release_free_pmem(nvdimm_bus, nd_mapping);
691 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
692 * @nd_region: the set of dimms to allocate @n more bytes from
693 * @label_id: unique identifier for the namespace consuming this dpa range
694 * @n: number of bytes per-dimm to add to the existing allocation
696 * Assumes resources are ordered. For BLK regions, first consume
697 * BLK-only available DPA free space, then consume PMEM-aliased DPA
698 * space starting at the highest DPA. For PMEM regions start
699 * allocations from the start of an interleave set and end at the first
700 * BLK allocation or the end of the interleave set, whichever comes
703 static int grow_dpa_allocation(struct nd_region *nd_region,
704 struct nd_label_id *label_id, resource_size_t n)
706 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
707 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
710 for (i = 0; i < nd_region->ndr_mappings; i++) {
711 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
712 resource_size_t rem = n;
716 * In the BLK case try once with all unallocated PMEM
717 * reserved, and once without
719 for (j = is_pmem; j < 2; j++) {
720 bool blk_only = j == 0;
723 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
727 rem = scan_allocate(nd_region, nd_mapping,
730 release_free_pmem(nvdimm_bus, nd_mapping);
732 /* try again and allow encroachments into PMEM */
737 dev_WARN_ONCE(&nd_region->dev, rem,
738 "allocation underrun: %#llx of %#llx bytes\n",
739 (unsigned long long) n - rem,
740 (unsigned long long) n);
744 rc = merge_dpa(nd_region, nd_mapping, label_id);
752 static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
753 struct nd_namespace_pmem *nspm, resource_size_t size)
755 struct resource *res = &nspm->nsio.res;
757 res->start = nd_region->ndr_start;
758 res->end = nd_region->ndr_start + size - 1;
761 static ssize_t __size_store(struct device *dev, unsigned long long val)
763 resource_size_t allocated = 0, available = 0;
764 struct nd_region *nd_region = to_nd_region(dev->parent);
765 struct nd_mapping *nd_mapping;
766 struct nvdimm_drvdata *ndd;
767 struct nd_label_id label_id;
768 u32 flags = 0, remainder;
772 if (dev->driver || to_ndns(dev)->claim)
775 if (is_namespace_pmem(dev)) {
776 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
779 } else if (is_namespace_blk(dev)) {
780 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
783 flags = NSLABEL_FLAG_LOCAL;
787 * We need a uuid for the allocation-label and dimm(s) on which
788 * to store the label.
790 if (!uuid || nd_region->ndr_mappings == 0)
793 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
795 dev_dbg(dev, "%llu is not %dK aligned\n", val,
796 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
800 nd_label_gen_id(&label_id, uuid, flags);
801 for (i = 0; i < nd_region->ndr_mappings; i++) {
802 nd_mapping = &nd_region->mapping[i];
803 ndd = to_ndd(nd_mapping);
806 * All dimms in an interleave set, or the base dimm for a blk
807 * region, need to be enabled for the size to be changed.
812 allocated += nvdimm_allocated_dpa(ndd, &label_id);
814 available = nd_region_available_dpa(nd_region);
816 if (val > available + allocated)
819 if (val == allocated)
822 val = div_u64(val, nd_region->ndr_mappings);
823 allocated = div_u64(allocated, nd_region->ndr_mappings);
825 rc = shrink_dpa_allocation(nd_region, &label_id,
828 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
833 if (is_namespace_pmem(dev)) {
834 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
836 nd_namespace_pmem_set_size(nd_region, nspm,
837 val * nd_region->ndr_mappings);
838 } else if (is_namespace_blk(dev)) {
839 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
842 * Try to delete the namespace if we deleted all of its
843 * allocation, this is not the seed device for the
844 * region, and it is not actively claimed by a btt
847 if (val == 0 && nd_region->ns_seed != dev
848 && !nsblk->common.claim)
849 nd_device_unregister(dev, ND_ASYNC);
855 static ssize_t size_store(struct device *dev,
856 struct device_attribute *attr, const char *buf, size_t len)
858 struct nd_region *nd_region = to_nd_region(dev->parent);
859 unsigned long long val;
863 rc = kstrtoull(buf, 0, &val);
868 nvdimm_bus_lock(dev);
869 wait_nvdimm_bus_probe_idle(dev);
870 rc = __size_store(dev, val);
872 rc = nd_namespace_label_update(nd_region, dev);
874 if (is_namespace_pmem(dev)) {
875 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
878 } else if (is_namespace_blk(dev)) {
879 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
884 if (rc == 0 && val == 0 && uuid) {
885 /* setting size zero == 'delete namespace' */
890 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
891 ? "fail" : "success", rc);
893 nvdimm_bus_unlock(dev);
896 return rc < 0 ? rc : len;
899 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
901 struct device *dev = &ndns->dev;
903 if (is_namespace_pmem(dev)) {
904 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
906 return resource_size(&nspm->nsio.res);
907 } else if (is_namespace_blk(dev)) {
908 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
909 } else if (is_namespace_io(dev)) {
910 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
912 return resource_size(&nsio->res);
914 WARN_ONCE(1, "unknown namespace type\n");
918 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
920 resource_size_t size;
922 nvdimm_bus_lock(&ndns->dev);
923 size = __nvdimm_namespace_capacity(ndns);
924 nvdimm_bus_unlock(&ndns->dev);
928 EXPORT_SYMBOL(nvdimm_namespace_capacity);
930 static ssize_t size_show(struct device *dev,
931 struct device_attribute *attr, char *buf)
933 return sprintf(buf, "%llu\n", (unsigned long long)
934 nvdimm_namespace_capacity(to_ndns(dev)));
936 static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
938 static ssize_t uuid_show(struct device *dev,
939 struct device_attribute *attr, char *buf)
943 if (is_namespace_pmem(dev)) {
944 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
947 } else if (is_namespace_blk(dev)) {
948 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
955 return sprintf(buf, "%pUb\n", uuid);
956 return sprintf(buf, "\n");
960 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
961 * @nd_region: parent region so we can updates all dimms in the set
962 * @dev: namespace type for generating label_id
963 * @new_uuid: incoming uuid
964 * @old_uuid: reference to the uuid storage location in the namespace object
966 static int namespace_update_uuid(struct nd_region *nd_region,
967 struct device *dev, u8 *new_uuid, u8 **old_uuid)
969 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
970 struct nd_label_id old_label_id;
971 struct nd_label_id new_label_id;
974 if (!nd_is_uuid_unique(dev, new_uuid))
977 if (*old_uuid == NULL)
981 * If we've already written a label with this uuid, then it's
982 * too late to rename because we can't reliably update the uuid
983 * without losing the old namespace. Userspace must delete this
984 * namespace to abandon the old uuid.
986 for (i = 0; i < nd_region->ndr_mappings; i++) {
987 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
990 * This check by itself is sufficient because old_uuid
991 * would be NULL above if this uuid did not exist in the
992 * currently written set.
994 * FIXME: can we delete uuid with zero dpa allocated?
996 if (nd_mapping->labels)
1000 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1001 nd_label_gen_id(&new_label_id, new_uuid, flags);
1002 for (i = 0; i < nd_region->ndr_mappings; i++) {
1003 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1004 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1005 struct resource *res;
1007 for_each_dpa_resource(ndd, res)
1008 if (strcmp(res->name, old_label_id.id) == 0)
1009 sprintf((void *) res->name, "%s",
1014 *old_uuid = new_uuid;
1018 static ssize_t uuid_store(struct device *dev,
1019 struct device_attribute *attr, const char *buf, size_t len)
1021 struct nd_region *nd_region = to_nd_region(dev->parent);
1026 if (is_namespace_pmem(dev)) {
1027 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1029 ns_uuid = &nspm->uuid;
1030 } else if (is_namespace_blk(dev)) {
1031 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1033 ns_uuid = &nsblk->uuid;
1038 nvdimm_bus_lock(dev);
1039 wait_nvdimm_bus_probe_idle(dev);
1040 if (to_ndns(dev)->claim)
1043 rc = nd_uuid_store(dev, &uuid, buf, len);
1045 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1047 rc = nd_namespace_label_update(nd_region, dev);
1050 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1051 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1052 nvdimm_bus_unlock(dev);
1055 return rc < 0 ? rc : len;
1057 static DEVICE_ATTR_RW(uuid);
1059 static ssize_t resource_show(struct device *dev,
1060 struct device_attribute *attr, char *buf)
1062 struct resource *res;
1064 if (is_namespace_pmem(dev)) {
1065 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1067 res = &nspm->nsio.res;
1068 } else if (is_namespace_io(dev)) {
1069 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1075 /* no address to convey if the namespace has no allocation */
1076 if (resource_size(res) == 0)
1078 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1080 static DEVICE_ATTR_RO(resource);
1082 static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1083 4096, 4104, 4160, 4224, 0 };
1085 static ssize_t sector_size_show(struct device *dev,
1086 struct device_attribute *attr, char *buf)
1088 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1090 if (!is_namespace_blk(dev))
1093 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1096 static ssize_t sector_size_store(struct device *dev,
1097 struct device_attribute *attr, const char *buf, size_t len)
1099 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1100 struct nd_region *nd_region = to_nd_region(dev->parent);
1103 if (!is_namespace_blk(dev))
1107 nvdimm_bus_lock(dev);
1108 if (to_ndns(dev)->claim)
1111 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1112 ns_lbasize_supported);
1114 rc = nd_namespace_label_update(nd_region, dev);
1115 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1116 rc, rc < 0 ? "tried" : "wrote", buf,
1117 buf[len - 1] == '\n' ? "" : "\n");
1118 nvdimm_bus_unlock(dev);
1121 return rc ? rc : len;
1123 static DEVICE_ATTR_RW(sector_size);
1125 static ssize_t dpa_extents_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1128 struct nd_region *nd_region = to_nd_region(dev->parent);
1129 struct nd_label_id label_id;
1134 nvdimm_bus_lock(dev);
1135 if (is_namespace_pmem(dev)) {
1136 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1140 } else if (is_namespace_blk(dev)) {
1141 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1144 flags = NSLABEL_FLAG_LOCAL;
1150 nd_label_gen_id(&label_id, uuid, flags);
1151 for (i = 0; i < nd_region->ndr_mappings; i++) {
1152 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1153 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1154 struct resource *res;
1156 for_each_dpa_resource(ndd, res)
1157 if (strcmp(res->name, label_id.id) == 0)
1161 nvdimm_bus_unlock(dev);
1163 return sprintf(buf, "%d\n", count);
1165 static DEVICE_ATTR_RO(dpa_extents);
1167 static ssize_t holder_show(struct device *dev,
1168 struct device_attribute *attr, char *buf)
1170 struct nd_namespace_common *ndns = to_ndns(dev);
1174 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1179 static DEVICE_ATTR_RO(holder);
1181 static ssize_t force_raw_store(struct device *dev,
1182 struct device_attribute *attr, const char *buf, size_t len)
1185 int rc = strtobool(buf, &force_raw);
1190 to_ndns(dev)->force_raw = force_raw;
1194 static ssize_t force_raw_show(struct device *dev,
1195 struct device_attribute *attr, char *buf)
1197 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1199 static DEVICE_ATTR_RW(force_raw);
1201 static struct attribute *nd_namespace_attributes[] = {
1202 &dev_attr_nstype.attr,
1203 &dev_attr_size.attr,
1204 &dev_attr_uuid.attr,
1205 &dev_attr_holder.attr,
1206 &dev_attr_resource.attr,
1207 &dev_attr_alt_name.attr,
1208 &dev_attr_force_raw.attr,
1209 &dev_attr_sector_size.attr,
1210 &dev_attr_dpa_extents.attr,
1214 static umode_t namespace_visible(struct kobject *kobj,
1215 struct attribute *a, int n)
1217 struct device *dev = container_of(kobj, struct device, kobj);
1219 if (a == &dev_attr_resource.attr) {
1220 if (is_namespace_blk(dev))
1225 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1226 if (a == &dev_attr_size.attr)
1227 return S_IWUSR | S_IRUGO;
1229 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1235 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1236 || a == &dev_attr_holder.attr
1237 || a == &dev_attr_force_raw.attr)
1243 static struct attribute_group nd_namespace_attribute_group = {
1244 .attrs = nd_namespace_attributes,
1245 .is_visible = namespace_visible,
1248 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1249 &nd_device_attribute_group,
1250 &nd_namespace_attribute_group,
1251 &nd_numa_attribute_group,
1255 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1257 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1258 struct nd_namespace_common *ndns;
1259 resource_size_t size;
1262 ndns = nd_btt->ndns;
1264 return ERR_PTR(-ENODEV);
1267 * Flush any in-progess probes / removals in the driver
1268 * for the raw personality of this namespace.
1270 device_lock(&ndns->dev);
1271 device_unlock(&ndns->dev);
1272 if (ndns->dev.driver) {
1273 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1274 dev_name(&nd_btt->dev));
1275 return ERR_PTR(-EBUSY);
1277 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != &nd_btt->dev,
1278 "host (%s) vs claim (%s) mismatch\n",
1279 dev_name(&nd_btt->dev),
1280 dev_name(ndns->claim)))
1281 return ERR_PTR(-ENXIO);
1283 ndns = to_ndns(dev);
1285 dev_dbg(dev, "claimed by %s, failing probe\n",
1286 dev_name(ndns->claim));
1288 return ERR_PTR(-ENXIO);
1292 size = nvdimm_namespace_capacity(ndns);
1293 if (size < ND_MIN_NAMESPACE_SIZE) {
1294 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1295 &size, ND_MIN_NAMESPACE_SIZE);
1296 return ERR_PTR(-ENODEV);
1299 if (is_namespace_pmem(&ndns->dev)) {
1300 struct nd_namespace_pmem *nspm;
1302 nspm = to_nd_namespace_pmem(&ndns->dev);
1304 dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__);
1305 return ERR_PTR(-ENODEV);
1307 } else if (is_namespace_blk(&ndns->dev)) {
1308 struct nd_namespace_blk *nsblk;
1310 nsblk = to_nd_namespace_blk(&ndns->dev);
1311 if (!nd_namespace_blk_validate(nsblk))
1312 return ERR_PTR(-ENODEV);
1317 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1319 static struct device **create_namespace_io(struct nd_region *nd_region)
1321 struct nd_namespace_io *nsio;
1322 struct device *dev, **devs;
1323 struct resource *res;
1325 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1329 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1335 dev = &nsio->common.dev;
1336 dev->type = &namespace_io_device_type;
1337 dev->parent = &nd_region->dev;
1339 res->name = dev_name(&nd_region->dev);
1340 res->flags = IORESOURCE_MEM;
1341 res->start = nd_region->ndr_start;
1342 res->end = res->start + nd_region->ndr_size - 1;
1348 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1349 u64 cookie, u16 pos)
1351 struct nd_namespace_label *found = NULL;
1354 for (i = 0; i < nd_region->ndr_mappings; i++) {
1355 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1356 struct nd_namespace_label *nd_label;
1357 bool found_uuid = false;
1360 for_each_label(l, nd_label, nd_mapping->labels) {
1361 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1362 u16 position = __le16_to_cpu(nd_label->position);
1363 u16 nlabel = __le16_to_cpu(nd_label->nlabel);
1365 if (isetcookie != cookie)
1368 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1372 dev_dbg(to_ndd(nd_mapping)->dev,
1373 "%s duplicate entry for uuid\n",
1378 if (nlabel != nd_region->ndr_mappings)
1380 if (position != pos)
1388 return found != NULL;
1391 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1393 struct nd_namespace_label *select = NULL;
1399 for (i = 0; i < nd_region->ndr_mappings; i++) {
1400 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1401 struct nd_namespace_label *nd_label;
1402 u64 hw_start, hw_end, pmem_start, pmem_end;
1405 for_each_label(l, nd_label, nd_mapping->labels)
1406 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1416 * Check that this label is compliant with the dpa
1417 * range published in NFIT
1419 hw_start = nd_mapping->start;
1420 hw_end = hw_start + nd_mapping->size;
1421 pmem_start = __le64_to_cpu(select->dpa);
1422 pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
1423 if (pmem_start == hw_start && pmem_end <= hw_end)
1428 nd_mapping->labels[0] = select;
1429 nd_mapping->labels[1] = NULL;
1435 * find_pmem_label_set - validate interleave set labelling, retrieve label0
1436 * @nd_region: region with mappings to validate
1438 static int find_pmem_label_set(struct nd_region *nd_region,
1439 struct nd_namespace_pmem *nspm)
1441 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1442 struct nd_namespace_label *nd_label;
1443 u8 select_id[NSLABEL_UUID_LEN];
1444 resource_size_t size = 0;
1446 int rc = -ENODEV, l;
1453 * Find a complete set of labels by uuid. By definition we can start
1454 * with any mapping as the reference label
1456 for_each_label(l, nd_label, nd_region->mapping[0].labels) {
1457 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1459 if (isetcookie != cookie)
1462 for (i = 0; nd_region->ndr_mappings; i++)
1463 if (!has_uuid_at_pos(nd_region, nd_label->uuid,
1466 if (i < nd_region->ndr_mappings) {
1468 * Give up if we don't find an instance of a
1469 * uuid at each position (from 0 to
1470 * nd_region->ndr_mappings - 1), or if we find a
1471 * dimm with two instances of the same uuid.
1475 } else if (pmem_id) {
1477 * If there is more than one valid uuid set, we
1478 * need userspace to clean this up.
1483 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
1484 pmem_id = select_id;
1488 * Fix up each mapping's 'labels' to have the validated pmem label for
1489 * that position at labels[0], and NULL at labels[1]. In the process,
1490 * check that the namespace aligns with interleave-set. We know
1491 * that it does not overlap with any blk namespaces by virtue of
1492 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1495 rc = select_pmem_id(nd_region, pmem_id);
1499 /* Calculate total size and populate namespace properties from label0 */
1500 for (i = 0; i < nd_region->ndr_mappings; i++) {
1501 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1502 struct nd_namespace_label *label0 = nd_mapping->labels[0];
1504 size += __le64_to_cpu(label0->rawsize);
1505 if (__le16_to_cpu(label0->position) != 0)
1507 WARN_ON(nspm->alt_name || nspm->uuid);
1508 nspm->alt_name = kmemdup((void __force *) label0->name,
1509 NSLABEL_NAME_LEN, GFP_KERNEL);
1510 nspm->uuid = kmemdup((void __force *) label0->uuid,
1511 NSLABEL_UUID_LEN, GFP_KERNEL);
1514 if (!nspm->alt_name || !nspm->uuid) {
1519 nd_namespace_pmem_set_size(nd_region, nspm, size);
1525 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1528 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1531 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1538 static struct device **create_namespace_pmem(struct nd_region *nd_region)
1540 struct nd_namespace_pmem *nspm;
1541 struct device *dev, **devs;
1542 struct resource *res;
1545 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1549 dev = &nspm->nsio.common.dev;
1550 dev->type = &namespace_pmem_device_type;
1551 dev->parent = &nd_region->dev;
1552 res = &nspm->nsio.res;
1553 res->name = dev_name(&nd_region->dev);
1554 res->flags = IORESOURCE_MEM;
1555 rc = find_pmem_label_set(nd_region, nspm);
1556 if (rc == -ENODEV) {
1559 /* Pass, try to permit namespace creation... */
1560 for (i = 0; i < nd_region->ndr_mappings; i++) {
1561 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1563 kfree(nd_mapping->labels);
1564 nd_mapping->labels = NULL;
1567 /* Publish a zero-sized namespace for userspace to configure. */
1568 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1574 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1582 namespace_pmem_release(&nspm->nsio.common.dev);
1586 struct resource *nsblk_add_resource(struct nd_region *nd_region,
1587 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1588 resource_size_t start)
1590 struct nd_label_id label_id;
1591 struct resource *res;
1593 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1594 res = krealloc(nsblk->res,
1595 sizeof(void *) * (nsblk->num_resources + 1),
1599 nsblk->res = (struct resource **) res;
1600 for_each_dpa_resource(ndd, res)
1601 if (strcmp(res->name, label_id.id) == 0
1602 && res->start == start) {
1603 nsblk->res[nsblk->num_resources++] = res;
1609 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1611 struct nd_namespace_blk *nsblk;
1614 if (!is_nd_blk(&nd_region->dev))
1617 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1621 dev = &nsblk->common.dev;
1622 dev->type = &namespace_blk_device_type;
1623 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1624 if (nsblk->id < 0) {
1628 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1629 dev->parent = &nd_region->dev;
1630 dev->groups = nd_namespace_attribute_groups;
1632 return &nsblk->common.dev;
1635 void nd_region_create_blk_seed(struct nd_region *nd_region)
1637 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1638 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1640 * Seed creation failures are not fatal, provisioning is simply
1641 * disabled until memory becomes available
1643 if (!nd_region->ns_seed)
1644 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1646 nd_device_register(nd_region->ns_seed);
1649 void nd_region_create_btt_seed(struct nd_region *nd_region)
1651 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1652 nd_region->btt_seed = nd_btt_create(nd_region);
1654 * Seed creation failures are not fatal, provisioning is simply
1655 * disabled until memory becomes available
1657 if (!nd_region->btt_seed)
1658 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1661 static struct device **create_namespace_blk(struct nd_region *nd_region)
1663 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1664 struct nd_namespace_label *nd_label;
1665 struct device *dev, **devs = NULL;
1666 struct nd_namespace_blk *nsblk;
1667 struct nvdimm_drvdata *ndd;
1668 int i, l, count = 0;
1669 struct resource *res;
1671 if (nd_region->ndr_mappings == 0)
1674 ndd = to_ndd(nd_mapping);
1675 for_each_label(l, nd_label, nd_mapping->labels) {
1676 u32 flags = __le32_to_cpu(nd_label->flags);
1677 char *name[NSLABEL_NAME_LEN];
1678 struct device **__devs;
1680 if (flags & NSLABEL_FLAG_LOCAL)
1685 for (i = 0; i < count; i++) {
1686 nsblk = to_nd_namespace_blk(devs[i]);
1687 if (memcmp(nsblk->uuid, nd_label->uuid,
1688 NSLABEL_UUID_LEN) == 0) {
1689 res = nsblk_add_resource(nd_region, ndd, nsblk,
1690 __le64_to_cpu(nd_label->dpa));
1693 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
1694 dev_name(&nsblk->common.dev));
1700 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1703 memcpy(__devs, devs, sizeof(dev) * count);
1707 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1710 dev = &nsblk->common.dev;
1711 dev->type = &namespace_blk_device_type;
1712 dev->parent = &nd_region->dev;
1713 dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
1714 devs[count++] = dev;
1716 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1717 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1721 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1723 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1725 res = nsblk_add_resource(nd_region, ndd, nsblk,
1726 __le64_to_cpu(nd_label->dpa));
1729 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
1730 dev_name(&nsblk->common.dev));
1733 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
1734 __func__, count, count == 1 ? "" : "s");
1737 /* Publish a zero-sized namespace for userspace to configure. */
1738 for (i = 0; i < nd_region->ndr_mappings; i++) {
1739 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1741 kfree(nd_mapping->labels);
1742 nd_mapping->labels = NULL;
1745 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1748 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1751 dev = &nsblk->common.dev;
1752 dev->type = &namespace_blk_device_type;
1753 dev->parent = &nd_region->dev;
1754 devs[count++] = dev;
1760 for (i = 0; i < count; i++) {
1761 nsblk = to_nd_namespace_blk(devs[i]);
1762 namespace_blk_release(&nsblk->common.dev);
1768 static int init_active_labels(struct nd_region *nd_region)
1772 for (i = 0; i < nd_region->ndr_mappings; i++) {
1773 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1774 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1775 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1779 * If the dimm is disabled then prevent the region from
1780 * being activated if it aliases DPA.
1783 if ((nvdimm->flags & NDD_ALIASING) == 0)
1785 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
1786 dev_name(&nd_mapping->nvdimm->dev));
1789 nd_mapping->ndd = ndd;
1790 atomic_inc(&nvdimm->busy);
1793 count = nd_label_active_count(ndd);
1794 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
1797 nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
1799 if (!nd_mapping->labels)
1801 for (j = 0; j < count; j++) {
1802 struct nd_namespace_label *label;
1804 label = nd_label_active(ndd, j);
1805 nd_mapping->labels[j] = label;
1812 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
1814 struct device **devs = NULL;
1815 int i, rc = 0, type;
1818 nvdimm_bus_lock(&nd_region->dev);
1819 rc = init_active_labels(nd_region);
1821 nvdimm_bus_unlock(&nd_region->dev);
1825 type = nd_region_to_nstype(nd_region);
1827 case ND_DEVICE_NAMESPACE_IO:
1828 devs = create_namespace_io(nd_region);
1830 case ND_DEVICE_NAMESPACE_PMEM:
1831 devs = create_namespace_pmem(nd_region);
1833 case ND_DEVICE_NAMESPACE_BLK:
1834 devs = create_namespace_blk(nd_region);
1839 nvdimm_bus_unlock(&nd_region->dev);
1844 for (i = 0; devs[i]; i++) {
1845 struct device *dev = devs[i];
1848 if (type == ND_DEVICE_NAMESPACE_BLK) {
1849 struct nd_namespace_blk *nsblk;
1851 nsblk = to_nd_namespace_blk(dev);
1852 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
1860 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
1861 dev->groups = nd_namespace_attribute_groups;
1862 nd_device_register(dev);
1865 nd_region->ns_seed = devs[0];
1870 for (j = i; devs[j]; j++) {
1871 struct device *dev = devs[j];
1873 device_initialize(dev);
1878 * All of the namespaces we tried to register failed, so
1879 * fail region activation.