1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/module.h>
6 #include <linux/device.h>
7 #include <linux/sort.h>
8 #include <linux/slab.h>
9 #include <linux/list.h>
15 static void namespace_io_release(struct device
*dev
)
17 struct nd_namespace_io
*nsio
= to_nd_namespace_io(dev
);
22 static void namespace_pmem_release(struct device
*dev
)
24 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
25 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
28 ida_simple_remove(&nd_region
->ns_ida
, nspm
->id
);
29 kfree(nspm
->alt_name
);
34 static void namespace_blk_release(struct device
*dev
)
36 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
37 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
40 ida_simple_remove(&nd_region
->ns_ida
, nsblk
->id
);
41 kfree(nsblk
->alt_name
);
47 static bool is_namespace_pmem(const struct device
*dev
);
48 static bool is_namespace_blk(const struct device
*dev
);
49 static bool is_namespace_io(const struct device
*dev
);
51 static int is_uuid_busy(struct device
*dev
, void *data
)
53 u8
*uuid1
= data
, *uuid2
= NULL
;
55 if (is_namespace_pmem(dev
)) {
56 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
59 } else if (is_namespace_blk(dev
)) {
60 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
63 } else if (is_nd_btt(dev
)) {
64 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
67 } else if (is_nd_pfn(dev
)) {
68 struct nd_pfn
*nd_pfn
= to_nd_pfn(dev
);
73 if (uuid2
&& memcmp(uuid1
, uuid2
, NSLABEL_UUID_LEN
) == 0)
79 static int is_namespace_uuid_busy(struct device
*dev
, void *data
)
81 if (is_nd_region(dev
))
82 return device_for_each_child(dev
, data
, is_uuid_busy
);
87 * nd_is_uuid_unique - verify that no other namespace has @uuid
88 * @dev: any device on a nvdimm_bus
89 * @uuid: uuid to check
91 bool nd_is_uuid_unique(struct device
*dev
, u8
*uuid
)
93 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
97 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus
->dev
));
98 if (device_for_each_child(&nvdimm_bus
->dev
, uuid
,
99 is_namespace_uuid_busy
) != 0)
104 bool pmem_should_map_pages(struct device
*dev
)
106 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
107 struct nd_namespace_common
*ndns
= to_ndns(dev
);
108 struct nd_namespace_io
*nsio
;
110 if (!IS_ENABLED(CONFIG_ZONE_DEVICE
))
113 if (!test_bit(ND_REGION_PAGEMAP
, &nd_region
->flags
))
116 if (is_nd_pfn(dev
) || is_nd_btt(dev
))
122 nsio
= to_nd_namespace_io(dev
);
123 if (region_intersects(nsio
->res
.start
, resource_size(&nsio
->res
),
124 IORESOURCE_SYSTEM_RAM
,
125 IORES_DESC_NONE
) == REGION_MIXED
)
128 return ARCH_MEMREMAP_PMEM
== MEMREMAP_WB
;
130 EXPORT_SYMBOL(pmem_should_map_pages
);
132 unsigned int pmem_sector_size(struct nd_namespace_common
*ndns
)
134 if (is_namespace_pmem(&ndns
->dev
)) {
135 struct nd_namespace_pmem
*nspm
;
137 nspm
= to_nd_namespace_pmem(&ndns
->dev
);
138 if (nspm
->lbasize
== 0 || nspm
->lbasize
== 512)
140 else if (nspm
->lbasize
== 4096)
143 dev_WARN(&ndns
->dev
, "unsupported sector size: %ld\n",
148 * There is no namespace label (is_namespace_io()), or the label
149 * indicates the default sector size.
153 EXPORT_SYMBOL(pmem_sector_size
);
155 const char *nvdimm_namespace_disk_name(struct nd_namespace_common
*ndns
,
158 struct nd_region
*nd_region
= to_nd_region(ndns
->dev
.parent
);
159 const char *suffix
= NULL
;
161 if (ndns
->claim
&& is_nd_btt(ndns
->claim
))
164 if (is_namespace_pmem(&ndns
->dev
) || is_namespace_io(&ndns
->dev
)) {
167 if (is_namespace_pmem(&ndns
->dev
)) {
168 struct nd_namespace_pmem
*nspm
;
170 nspm
= to_nd_namespace_pmem(&ndns
->dev
);
175 sprintf(name
, "pmem%d.%d%s", nd_region
->id
, nsidx
,
176 suffix
? suffix
: "");
178 sprintf(name
, "pmem%d%s", nd_region
->id
,
179 suffix
? suffix
: "");
180 } else if (is_namespace_blk(&ndns
->dev
)) {
181 struct nd_namespace_blk
*nsblk
;
183 nsblk
= to_nd_namespace_blk(&ndns
->dev
);
184 sprintf(name
, "ndblk%d.%d%s", nd_region
->id
, nsblk
->id
,
185 suffix
? suffix
: "");
192 EXPORT_SYMBOL(nvdimm_namespace_disk_name
);
194 const u8
*nd_dev_to_uuid(struct device
*dev
)
196 static const u8 null_uuid
[16];
201 if (is_namespace_pmem(dev
)) {
202 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
205 } else if (is_namespace_blk(dev
)) {
206 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
212 EXPORT_SYMBOL(nd_dev_to_uuid
);
214 static ssize_t
nstype_show(struct device
*dev
,
215 struct device_attribute
*attr
, char *buf
)
217 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
219 return sprintf(buf
, "%d\n", nd_region_to_nstype(nd_region
));
221 static DEVICE_ATTR_RO(nstype
);
223 static ssize_t
__alt_name_store(struct device
*dev
, const char *buf
,
226 char *input
, *pos
, *alt_name
, **ns_altname
;
229 if (is_namespace_pmem(dev
)) {
230 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
232 ns_altname
= &nspm
->alt_name
;
233 } else if (is_namespace_blk(dev
)) {
234 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
236 ns_altname
= &nsblk
->alt_name
;
240 if (dev
->driver
|| to_ndns(dev
)->claim
)
243 input
= kstrndup(buf
, len
, GFP_KERNEL
);
248 if (strlen(pos
) + 1 > NSLABEL_NAME_LEN
) {
253 alt_name
= kzalloc(NSLABEL_NAME_LEN
, GFP_KERNEL
);
259 *ns_altname
= alt_name
;
260 sprintf(*ns_altname
, "%s", pos
);
268 static resource_size_t
nd_namespace_blk_size(struct nd_namespace_blk
*nsblk
)
270 struct nd_region
*nd_region
= to_nd_region(nsblk
->common
.dev
.parent
);
271 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
272 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
273 struct nd_label_id label_id
;
274 resource_size_t size
= 0;
275 struct resource
*res
;
279 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
280 for_each_dpa_resource(ndd
, res
)
281 if (strcmp(res
->name
, label_id
.id
) == 0)
282 size
+= resource_size(res
);
286 static bool __nd_namespace_blk_validate(struct nd_namespace_blk
*nsblk
)
288 struct nd_region
*nd_region
= to_nd_region(nsblk
->common
.dev
.parent
);
289 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
290 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
291 struct nd_label_id label_id
;
292 struct resource
*res
;
295 if (!nsblk
->uuid
|| !nsblk
->lbasize
|| !ndd
)
299 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
300 for_each_dpa_resource(ndd
, res
) {
301 if (strcmp(res
->name
, label_id
.id
) != 0)
304 * Resources with unacknowledged adjustments indicate a
305 * failure to update labels
307 if (res
->flags
& DPA_RESOURCE_ADJUSTED
)
312 /* These values match after a successful label update */
313 if (count
!= nsblk
->num_resources
)
316 for (i
= 0; i
< nsblk
->num_resources
; i
++) {
317 struct resource
*found
= NULL
;
319 for_each_dpa_resource(ndd
, res
)
320 if (res
== nsblk
->res
[i
]) {
332 resource_size_t
nd_namespace_blk_validate(struct nd_namespace_blk
*nsblk
)
334 resource_size_t size
;
336 nvdimm_bus_lock(&nsblk
->common
.dev
);
337 size
= __nd_namespace_blk_validate(nsblk
);
338 nvdimm_bus_unlock(&nsblk
->common
.dev
);
342 EXPORT_SYMBOL(nd_namespace_blk_validate
);
345 static int nd_namespace_label_update(struct nd_region
*nd_region
,
348 dev_WARN_ONCE(dev
, dev
->driver
|| to_ndns(dev
)->claim
,
349 "namespace must be idle during label update\n");
350 if (dev
->driver
|| to_ndns(dev
)->claim
)
354 * Only allow label writes that will result in a valid namespace
355 * or deletion of an existing namespace.
357 if (is_namespace_pmem(dev
)) {
358 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
359 resource_size_t size
= resource_size(&nspm
->nsio
.res
);
361 if (size
== 0 && nspm
->uuid
)
362 /* delete allocation */;
363 else if (!nspm
->uuid
)
366 return nd_pmem_namespace_label_update(nd_region
, nspm
, size
);
367 } else if (is_namespace_blk(dev
)) {
368 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
369 resource_size_t size
= nd_namespace_blk_size(nsblk
);
371 if (size
== 0 && nsblk
->uuid
)
372 /* delete allocation */;
373 else if (!nsblk
->uuid
|| !nsblk
->lbasize
)
376 return nd_blk_namespace_label_update(nd_region
, nsblk
, size
);
381 static ssize_t
alt_name_store(struct device
*dev
,
382 struct device_attribute
*attr
, const char *buf
, size_t len
)
384 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
388 nvdimm_bus_lock(dev
);
389 wait_nvdimm_bus_probe_idle(dev
);
390 rc
= __alt_name_store(dev
, buf
, len
);
392 rc
= nd_namespace_label_update(nd_region
, dev
);
393 dev_dbg(dev
, "%s(%zd)\n", rc
< 0 ? "fail " : "", rc
);
394 nvdimm_bus_unlock(dev
);
395 nd_device_unlock(dev
);
397 return rc
< 0 ? rc
: len
;
400 static ssize_t
alt_name_show(struct device
*dev
,
401 struct device_attribute
*attr
, char *buf
)
405 if (is_namespace_pmem(dev
)) {
406 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
408 ns_altname
= nspm
->alt_name
;
409 } else if (is_namespace_blk(dev
)) {
410 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
412 ns_altname
= nsblk
->alt_name
;
416 return sprintf(buf
, "%s\n", ns_altname
? ns_altname
: "");
418 static DEVICE_ATTR_RW(alt_name
);
420 static int scan_free(struct nd_region
*nd_region
,
421 struct nd_mapping
*nd_mapping
, struct nd_label_id
*label_id
,
424 bool is_blk
= strncmp(label_id
->id
, "blk", 3) == 0;
425 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
429 struct resource
*res
, *last
;
430 resource_size_t new_start
;
433 for_each_dpa_resource(ndd
, res
)
434 if (strcmp(res
->name
, label_id
->id
) == 0)
440 if (n
>= resource_size(res
)) {
441 n
-= resource_size(res
);
442 nd_dbg_dpa(nd_region
, ndd
, res
, "delete %d\n", rc
);
443 nvdimm_free_dpa(ndd
, res
);
444 /* retry with last resource deleted */
449 * Keep BLK allocations relegated to high DPA as much as
453 new_start
= res
->start
+ n
;
455 new_start
= res
->start
;
457 rc
= adjust_resource(res
, new_start
, resource_size(res
) - n
);
459 res
->flags
|= DPA_RESOURCE_ADJUSTED
;
460 nd_dbg_dpa(nd_region
, ndd
, res
, "shrink %d\n", rc
);
468 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
469 * @nd_region: the set of dimms to reclaim @n bytes from
470 * @label_id: unique identifier for the namespace consuming this dpa range
471 * @n: number of bytes per-dimm to release
473 * Assumes resources are ordered. Starting from the end try to
474 * adjust_resource() the allocation to @n, but if @n is larger than the
475 * allocation delete it and find the 'new' last allocation in the label
478 static int shrink_dpa_allocation(struct nd_region
*nd_region
,
479 struct nd_label_id
*label_id
, resource_size_t n
)
483 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
484 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
487 rc
= scan_free(nd_region
, nd_mapping
, label_id
, n
);
495 static resource_size_t
init_dpa_allocation(struct nd_label_id
*label_id
,
496 struct nd_region
*nd_region
, struct nd_mapping
*nd_mapping
,
499 bool is_blk
= strncmp(label_id
->id
, "blk", 3) == 0;
500 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
501 resource_size_t first_dpa
;
502 struct resource
*res
;
505 /* allocate blk from highest dpa first */
507 first_dpa
= nd_mapping
->start
+ nd_mapping
->size
- n
;
509 first_dpa
= nd_mapping
->start
;
511 /* first resource allocation for this label-id or dimm */
512 res
= nvdimm_allocate_dpa(ndd
, label_id
, first_dpa
, n
);
516 nd_dbg_dpa(nd_region
, ndd
, res
, "init %d\n", rc
);
522 * space_valid() - validate free dpa space against constraints
523 * @nd_region: hosting region of the free space
524 * @ndd: dimm device data for debug
525 * @label_id: namespace id to allocate space
526 * @prev: potential allocation that precedes free space
527 * @next: allocation that follows the given free space range
528 * @exist: first allocation with same id in the mapping
529 * @n: range that must satisfied for pmem allocations
530 * @valid: free space range to validate
532 * BLK-space is valid as long as it does not precede a PMEM
533 * allocation in a given region. PMEM-space must be contiguous
534 * and adjacent to an existing existing allocation (if one
535 * exists). If reserving PMEM any space is valid.
537 static void space_valid(struct nd_region
*nd_region
, struct nvdimm_drvdata
*ndd
,
538 struct nd_label_id
*label_id
, struct resource
*prev
,
539 struct resource
*next
, struct resource
*exist
,
540 resource_size_t n
, struct resource
*valid
)
542 bool is_reserve
= strcmp(label_id
->id
, "pmem-reserve") == 0;
543 bool is_pmem
= strncmp(label_id
->id
, "pmem", 4) == 0;
545 if (valid
->start
>= valid
->end
)
552 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
553 struct nvdimm_bus
*nvdimm_bus
;
554 struct blk_alloc_info info
= {
555 .nd_mapping
= nd_mapping
,
556 .available
= nd_mapping
->size
,
560 WARN_ON(!is_nd_blk(&nd_region
->dev
));
561 nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
562 device_for_each_child(&nvdimm_bus
->dev
, &info
, alias_dpa_busy
);
566 /* allocation needs to be contiguous, so this is all or nothing */
567 if (resource_size(valid
) < n
)
570 /* we've got all the space we need and no existing allocation */
574 /* allocation needs to be contiguous with the existing namespace */
575 if (valid
->start
== exist
->end
+ 1
576 || valid
->end
== exist
->start
- 1)
580 /* truncate @valid size to 0 */
581 valid
->end
= valid
->start
- 1;
585 ALLOC_ERR
= 0, ALLOC_BEFORE
, ALLOC_MID
, ALLOC_AFTER
,
588 static resource_size_t
scan_allocate(struct nd_region
*nd_region
,
589 struct nd_mapping
*nd_mapping
, struct nd_label_id
*label_id
,
592 resource_size_t mapping_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
593 bool is_pmem
= strncmp(label_id
->id
, "pmem", 4) == 0;
594 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
595 struct resource
*res
, *exist
= NULL
, valid
;
596 const resource_size_t to_allocate
= n
;
599 for_each_dpa_resource(ndd
, res
)
600 if (strcmp(label_id
->id
, res
->name
) == 0)
603 valid
.start
= nd_mapping
->start
;
604 valid
.end
= mapping_end
;
605 valid
.name
= "free space";
608 for_each_dpa_resource(ndd
, res
) {
609 struct resource
*next
= res
->sibling
, *new_res
= NULL
;
610 resource_size_t allocate
, available
= 0;
611 enum alloc_loc loc
= ALLOC_ERR
;
615 /* ignore resources outside this nd_mapping */
616 if (res
->start
> mapping_end
)
618 if (res
->end
< nd_mapping
->start
)
621 /* space at the beginning of the mapping */
622 if (!first
++ && res
->start
> nd_mapping
->start
) {
623 valid
.start
= nd_mapping
->start
;
624 valid
.end
= res
->start
- 1;
625 space_valid(nd_region
, ndd
, label_id
, NULL
, next
, exist
,
626 to_allocate
, &valid
);
627 available
= resource_size(&valid
);
632 /* space between allocations */
634 valid
.start
= res
->start
+ resource_size(res
);
635 valid
.end
= min(mapping_end
, next
->start
- 1);
636 space_valid(nd_region
, ndd
, label_id
, res
, next
, exist
,
637 to_allocate
, &valid
);
638 available
= resource_size(&valid
);
643 /* space at the end of the mapping */
645 valid
.start
= res
->start
+ resource_size(res
);
646 valid
.end
= mapping_end
;
647 space_valid(nd_region
, ndd
, label_id
, res
, next
, exist
,
648 to_allocate
, &valid
);
649 available
= resource_size(&valid
);
654 if (!loc
|| !available
)
656 allocate
= min(available
, n
);
659 if (strcmp(res
->name
, label_id
->id
) == 0) {
660 /* adjust current resource up */
661 rc
= adjust_resource(res
, res
->start
- allocate
,
662 resource_size(res
) + allocate
);
663 action
= "cur grow up";
668 if (strcmp(next
->name
, label_id
->id
) == 0) {
669 /* adjust next resource up */
670 rc
= adjust_resource(next
, next
->start
671 - allocate
, resource_size(next
)
674 action
= "next grow up";
675 } else if (strcmp(res
->name
, label_id
->id
) == 0) {
676 action
= "grow down";
681 if (strcmp(res
->name
, label_id
->id
) == 0)
682 action
= "grow down";
690 if (strcmp(action
, "allocate") == 0) {
691 /* BLK allocate bottom up */
693 valid
.start
+= available
- allocate
;
695 new_res
= nvdimm_allocate_dpa(ndd
, label_id
,
696 valid
.start
, allocate
);
699 } else if (strcmp(action
, "grow down") == 0) {
700 /* adjust current resource down */
701 rc
= adjust_resource(res
, res
->start
, resource_size(res
)
704 res
->flags
|= DPA_RESOURCE_ADJUSTED
;
710 nd_dbg_dpa(nd_region
, ndd
, new_res
, "%s(%d) %d\n",
719 * Retry scan with newly inserted resources.
720 * For example, if we did an ALLOC_BEFORE
721 * insertion there may also have been space
722 * available for an ALLOC_AFTER insertion, so we
723 * need to check this same resource again
731 * If we allocated nothing in the BLK case it may be because we are in
732 * an initial "pmem-reserve pass". Only do an initial BLK allocation
733 * when none of the DPA space is reserved.
735 if ((is_pmem
|| !ndd
->dpa
.child
) && n
== to_allocate
)
736 return init_dpa_allocation(label_id
, nd_region
, nd_mapping
, n
);
740 static int merge_dpa(struct nd_region
*nd_region
,
741 struct nd_mapping
*nd_mapping
, struct nd_label_id
*label_id
)
743 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
744 struct resource
*res
;
746 if (strncmp("pmem", label_id
->id
, 4) == 0)
749 for_each_dpa_resource(ndd
, res
) {
751 struct resource
*next
= res
->sibling
;
752 resource_size_t end
= res
->start
+ resource_size(res
);
754 if (!next
|| strcmp(res
->name
, label_id
->id
) != 0
755 || strcmp(next
->name
, label_id
->id
) != 0
756 || end
!= next
->start
)
758 end
+= resource_size(next
);
759 nvdimm_free_dpa(ndd
, next
);
760 rc
= adjust_resource(res
, res
->start
, end
- res
->start
);
761 nd_dbg_dpa(nd_region
, ndd
, res
, "merge %d\n", rc
);
764 res
->flags
|= DPA_RESOURCE_ADJUSTED
;
771 int __reserve_free_pmem(struct device
*dev
, void *data
)
773 struct nvdimm
*nvdimm
= data
;
774 struct nd_region
*nd_region
;
775 struct nd_label_id label_id
;
781 nd_region
= to_nd_region(dev
);
782 if (nd_region
->ndr_mappings
== 0)
785 memset(&label_id
, 0, sizeof(label_id
));
786 strcat(label_id
.id
, "pmem-reserve");
787 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
788 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
789 resource_size_t n
, rem
= 0;
791 if (nd_mapping
->nvdimm
!= nvdimm
)
794 n
= nd_pmem_available_dpa(nd_region
, nd_mapping
, &rem
);
797 rem
= scan_allocate(nd_region
, nd_mapping
, &label_id
, n
);
798 dev_WARN_ONCE(&nd_region
->dev
, rem
,
799 "pmem reserve underrun: %#llx of %#llx bytes\n",
800 (unsigned long long) n
- rem
,
801 (unsigned long long) n
);
802 return rem
? -ENXIO
: 0;
808 void release_free_pmem(struct nvdimm_bus
*nvdimm_bus
,
809 struct nd_mapping
*nd_mapping
)
811 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
812 struct resource
*res
, *_res
;
814 for_each_dpa_resource_safe(ndd
, res
, _res
)
815 if (strcmp(res
->name
, "pmem-reserve") == 0)
816 nvdimm_free_dpa(ndd
, res
);
819 static int reserve_free_pmem(struct nvdimm_bus
*nvdimm_bus
,
820 struct nd_mapping
*nd_mapping
)
822 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
825 rc
= device_for_each_child(&nvdimm_bus
->dev
, nvdimm
,
826 __reserve_free_pmem
);
828 release_free_pmem(nvdimm_bus
, nd_mapping
);
833 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
834 * @nd_region: the set of dimms to allocate @n more bytes from
835 * @label_id: unique identifier for the namespace consuming this dpa range
836 * @n: number of bytes per-dimm to add to the existing allocation
838 * Assumes resources are ordered. For BLK regions, first consume
839 * BLK-only available DPA free space, then consume PMEM-aliased DPA
840 * space starting at the highest DPA. For PMEM regions start
841 * allocations from the start of an interleave set and end at the first
842 * BLK allocation or the end of the interleave set, whichever comes
845 static int grow_dpa_allocation(struct nd_region
*nd_region
,
846 struct nd_label_id
*label_id
, resource_size_t n
)
848 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
849 bool is_pmem
= strncmp(label_id
->id
, "pmem", 4) == 0;
852 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
853 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
854 resource_size_t rem
= n
;
858 * In the BLK case try once with all unallocated PMEM
859 * reserved, and once without
861 for (j
= is_pmem
; j
< 2; j
++) {
862 bool blk_only
= j
== 0;
865 rc
= reserve_free_pmem(nvdimm_bus
, nd_mapping
);
869 rem
= scan_allocate(nd_region
, nd_mapping
,
872 release_free_pmem(nvdimm_bus
, nd_mapping
);
874 /* try again and allow encroachments into PMEM */
879 dev_WARN_ONCE(&nd_region
->dev
, rem
,
880 "allocation underrun: %#llx of %#llx bytes\n",
881 (unsigned long long) n
- rem
,
882 (unsigned long long) n
);
886 rc
= merge_dpa(nd_region
, nd_mapping
, label_id
);
894 static void nd_namespace_pmem_set_resource(struct nd_region
*nd_region
,
895 struct nd_namespace_pmem
*nspm
, resource_size_t size
)
897 struct resource
*res
= &nspm
->nsio
.res
;
898 resource_size_t offset
= 0;
900 if (size
&& !nspm
->uuid
) {
905 if (size
&& nspm
->uuid
) {
906 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
907 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
908 struct nd_label_id label_id
;
909 struct resource
*res
;
916 nd_label_gen_id(&label_id
, nspm
->uuid
, 0);
918 /* calculate a spa offset from the dpa allocation offset */
919 for_each_dpa_resource(ndd
, res
)
920 if (strcmp(res
->name
, label_id
.id
) == 0) {
921 offset
= (res
->start
- nd_mapping
->start
)
922 * nd_region
->ndr_mappings
;
931 res
->start
= nd_region
->ndr_start
+ offset
;
932 res
->end
= res
->start
+ size
- 1;
935 static bool uuid_not_set(const u8
*uuid
, struct device
*dev
, const char *where
)
938 dev_dbg(dev
, "%s: uuid not set\n", where
);
944 static ssize_t
__size_store(struct device
*dev
, unsigned long long val
)
946 resource_size_t allocated
= 0, available
= 0;
947 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
948 struct nd_namespace_common
*ndns
= to_ndns(dev
);
949 struct nd_mapping
*nd_mapping
;
950 struct nvdimm_drvdata
*ndd
;
951 struct nd_label_id label_id
;
952 u32 flags
= 0, remainder
;
956 if (dev
->driver
|| ndns
->claim
)
959 if (is_namespace_pmem(dev
)) {
960 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
964 } else if (is_namespace_blk(dev
)) {
965 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
968 flags
= NSLABEL_FLAG_LOCAL
;
973 * We need a uuid for the allocation-label and dimm(s) on which
974 * to store the label.
976 if (uuid_not_set(uuid
, dev
, __func__
))
978 if (nd_region
->ndr_mappings
== 0) {
979 dev_dbg(dev
, "not associated with dimm(s)\n");
983 div_u64_rem(val
, PAGE_SIZE
* nd_region
->ndr_mappings
, &remainder
);
985 dev_dbg(dev
, "%llu is not %ldK aligned\n", val
,
986 (PAGE_SIZE
* nd_region
->ndr_mappings
) / SZ_1K
);
990 nd_label_gen_id(&label_id
, uuid
, flags
);
991 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
992 nd_mapping
= &nd_region
->mapping
[i
];
993 ndd
= to_ndd(nd_mapping
);
996 * All dimms in an interleave set, or the base dimm for a blk
997 * region, need to be enabled for the size to be changed.
1002 allocated
+= nvdimm_allocated_dpa(ndd
, &label_id
);
1004 available
= nd_region_allocatable_dpa(nd_region
);
1006 if (val
> available
+ allocated
)
1009 if (val
== allocated
)
1012 val
= div_u64(val
, nd_region
->ndr_mappings
);
1013 allocated
= div_u64(allocated
, nd_region
->ndr_mappings
);
1014 if (val
< allocated
)
1015 rc
= shrink_dpa_allocation(nd_region
, &label_id
,
1018 rc
= grow_dpa_allocation(nd_region
, &label_id
, val
- allocated
);
1023 if (is_namespace_pmem(dev
)) {
1024 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1026 nd_namespace_pmem_set_resource(nd_region
, nspm
,
1027 val
* nd_region
->ndr_mappings
);
1031 * Try to delete the namespace if we deleted all of its
1032 * allocation, this is not the seed or 0th device for the
1033 * region, and it is not actively claimed by a btt, pfn, or dax
1036 if (val
== 0 && id
!= 0 && nd_region
->ns_seed
!= dev
&& !ndns
->claim
)
1037 nd_device_unregister(dev
, ND_ASYNC
);
1042 static ssize_t
size_store(struct device
*dev
,
1043 struct device_attribute
*attr
, const char *buf
, size_t len
)
1045 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1046 unsigned long long val
;
1050 rc
= kstrtoull(buf
, 0, &val
);
1054 nd_device_lock(dev
);
1055 nvdimm_bus_lock(dev
);
1056 wait_nvdimm_bus_probe_idle(dev
);
1057 rc
= __size_store(dev
, val
);
1059 rc
= nd_namespace_label_update(nd_region
, dev
);
1061 if (is_namespace_pmem(dev
)) {
1062 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1065 } else if (is_namespace_blk(dev
)) {
1066 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1068 uuid
= &nsblk
->uuid
;
1071 if (rc
== 0 && val
== 0 && uuid
) {
1072 /* setting size zero == 'delete namespace' */
1077 dev_dbg(dev
, "%llx %s (%d)\n", val
, rc
< 0 ? "fail" : "success", rc
);
1079 nvdimm_bus_unlock(dev
);
1080 nd_device_unlock(dev
);
1082 return rc
< 0 ? rc
: len
;
1085 resource_size_t
__nvdimm_namespace_capacity(struct nd_namespace_common
*ndns
)
1087 struct device
*dev
= &ndns
->dev
;
1089 if (is_namespace_pmem(dev
)) {
1090 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1092 return resource_size(&nspm
->nsio
.res
);
1093 } else if (is_namespace_blk(dev
)) {
1094 return nd_namespace_blk_size(to_nd_namespace_blk(dev
));
1095 } else if (is_namespace_io(dev
)) {
1096 struct nd_namespace_io
*nsio
= to_nd_namespace_io(dev
);
1098 return resource_size(&nsio
->res
);
1100 WARN_ONCE(1, "unknown namespace type\n");
1104 resource_size_t
nvdimm_namespace_capacity(struct nd_namespace_common
*ndns
)
1106 resource_size_t size
;
1108 nvdimm_bus_lock(&ndns
->dev
);
1109 size
= __nvdimm_namespace_capacity(ndns
);
1110 nvdimm_bus_unlock(&ndns
->dev
);
1114 EXPORT_SYMBOL(nvdimm_namespace_capacity
);
1116 bool nvdimm_namespace_locked(struct nd_namespace_common
*ndns
)
1119 bool locked
= false;
1120 struct device
*dev
= &ndns
->dev
;
1121 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1123 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1124 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1125 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
1127 if (test_bit(NDD_LOCKED
, &nvdimm
->flags
)) {
1128 dev_dbg(dev
, "%s locked\n", nvdimm_name(nvdimm
));
1134 EXPORT_SYMBOL(nvdimm_namespace_locked
);
1136 static ssize_t
size_show(struct device
*dev
,
1137 struct device_attribute
*attr
, char *buf
)
1139 return sprintf(buf
, "%llu\n", (unsigned long long)
1140 nvdimm_namespace_capacity(to_ndns(dev
)));
1142 static DEVICE_ATTR(size
, 0444, size_show
, size_store
);
1144 static u8
*namespace_to_uuid(struct device
*dev
)
1146 if (is_namespace_pmem(dev
)) {
1147 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1150 } else if (is_namespace_blk(dev
)) {
1151 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1155 return ERR_PTR(-ENXIO
);
1158 static ssize_t
uuid_show(struct device
*dev
,
1159 struct device_attribute
*attr
, char *buf
)
1161 u8
*uuid
= namespace_to_uuid(dev
);
1164 return PTR_ERR(uuid
);
1166 return sprintf(buf
, "%pUb\n", uuid
);
1167 return sprintf(buf
, "\n");
1171 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1172 * @nd_region: parent region so we can updates all dimms in the set
1173 * @dev: namespace type for generating label_id
1174 * @new_uuid: incoming uuid
1175 * @old_uuid: reference to the uuid storage location in the namespace object
1177 static int namespace_update_uuid(struct nd_region
*nd_region
,
1178 struct device
*dev
, u8
*new_uuid
, u8
**old_uuid
)
1180 u32 flags
= is_namespace_blk(dev
) ? NSLABEL_FLAG_LOCAL
: 0;
1181 struct nd_label_id old_label_id
;
1182 struct nd_label_id new_label_id
;
1185 if (!nd_is_uuid_unique(dev
, new_uuid
))
1188 if (*old_uuid
== NULL
)
1192 * If we've already written a label with this uuid, then it's
1193 * too late to rename because we can't reliably update the uuid
1194 * without losing the old namespace. Userspace must delete this
1195 * namespace to abandon the old uuid.
1197 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1198 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1201 * This check by itself is sufficient because old_uuid
1202 * would be NULL above if this uuid did not exist in the
1203 * currently written set.
1205 * FIXME: can we delete uuid with zero dpa allocated?
1207 if (list_empty(&nd_mapping
->labels
))
1211 nd_label_gen_id(&old_label_id
, *old_uuid
, flags
);
1212 nd_label_gen_id(&new_label_id
, new_uuid
, flags
);
1213 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1214 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1215 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1216 struct nd_label_ent
*label_ent
;
1217 struct resource
*res
;
1219 for_each_dpa_resource(ndd
, res
)
1220 if (strcmp(res
->name
, old_label_id
.id
) == 0)
1221 sprintf((void *) res
->name
, "%s",
1224 mutex_lock(&nd_mapping
->lock
);
1225 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
1226 struct nd_namespace_label
*nd_label
= label_ent
->label
;
1227 struct nd_label_id label_id
;
1231 nd_label_gen_id(&label_id
, nd_label
->uuid
,
1232 __le32_to_cpu(nd_label
->flags
));
1233 if (strcmp(old_label_id
.id
, label_id
.id
) == 0)
1234 set_bit(ND_LABEL_REAP
, &label_ent
->flags
);
1236 mutex_unlock(&nd_mapping
->lock
);
1240 *old_uuid
= new_uuid
;
1244 static ssize_t
uuid_store(struct device
*dev
,
1245 struct device_attribute
*attr
, const char *buf
, size_t len
)
1247 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1252 if (is_namespace_pmem(dev
)) {
1253 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1255 ns_uuid
= &nspm
->uuid
;
1256 } else if (is_namespace_blk(dev
)) {
1257 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1259 ns_uuid
= &nsblk
->uuid
;
1263 nd_device_lock(dev
);
1264 nvdimm_bus_lock(dev
);
1265 wait_nvdimm_bus_probe_idle(dev
);
1266 if (to_ndns(dev
)->claim
)
1269 rc
= nd_uuid_store(dev
, &uuid
, buf
, len
);
1271 rc
= namespace_update_uuid(nd_region
, dev
, uuid
, ns_uuid
);
1273 rc
= nd_namespace_label_update(nd_region
, dev
);
1276 dev_dbg(dev
, "result: %zd wrote: %s%s", rc
, buf
,
1277 buf
[len
- 1] == '\n' ? "" : "\n");
1278 nvdimm_bus_unlock(dev
);
1279 nd_device_unlock(dev
);
1281 return rc
< 0 ? rc
: len
;
1283 static DEVICE_ATTR_RW(uuid
);
1285 static ssize_t
resource_show(struct device
*dev
,
1286 struct device_attribute
*attr
, char *buf
)
1288 struct resource
*res
;
1290 if (is_namespace_pmem(dev
)) {
1291 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1293 res
= &nspm
->nsio
.res
;
1294 } else if (is_namespace_io(dev
)) {
1295 struct nd_namespace_io
*nsio
= to_nd_namespace_io(dev
);
1301 /* no address to convey if the namespace has no allocation */
1302 if (resource_size(res
) == 0)
1304 return sprintf(buf
, "%#llx\n", (unsigned long long) res
->start
);
1306 static DEVICE_ATTR(resource
, 0400, resource_show
, NULL
);
1308 static const unsigned long blk_lbasize_supported
[] = { 512, 520, 528,
1309 4096, 4104, 4160, 4224, 0 };
1311 static const unsigned long pmem_lbasize_supported
[] = { 512, 4096, 0 };
1313 static ssize_t
sector_size_show(struct device
*dev
,
1314 struct device_attribute
*attr
, char *buf
)
1316 if (is_namespace_blk(dev
)) {
1317 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1319 return nd_size_select_show(nsblk
->lbasize
,
1320 blk_lbasize_supported
, buf
);
1323 if (is_namespace_pmem(dev
)) {
1324 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1326 return nd_size_select_show(nspm
->lbasize
,
1327 pmem_lbasize_supported
, buf
);
1332 static ssize_t
sector_size_store(struct device
*dev
,
1333 struct device_attribute
*attr
, const char *buf
, size_t len
)
1335 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1336 const unsigned long *supported
;
1337 unsigned long *lbasize
;
1340 if (is_namespace_blk(dev
)) {
1341 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1343 lbasize
= &nsblk
->lbasize
;
1344 supported
= blk_lbasize_supported
;
1345 } else if (is_namespace_pmem(dev
)) {
1346 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1348 lbasize
= &nspm
->lbasize
;
1349 supported
= pmem_lbasize_supported
;
1353 nd_device_lock(dev
);
1354 nvdimm_bus_lock(dev
);
1355 if (to_ndns(dev
)->claim
)
1358 rc
= nd_size_select_store(dev
, buf
, lbasize
, supported
);
1360 rc
= nd_namespace_label_update(nd_region
, dev
);
1361 dev_dbg(dev
, "result: %zd %s: %s%s", rc
, rc
< 0 ? "tried" : "wrote",
1362 buf
, buf
[len
- 1] == '\n' ? "" : "\n");
1363 nvdimm_bus_unlock(dev
);
1364 nd_device_unlock(dev
);
1366 return rc
? rc
: len
;
1368 static DEVICE_ATTR_RW(sector_size
);
1370 static ssize_t
dpa_extents_show(struct device
*dev
,
1371 struct device_attribute
*attr
, char *buf
)
1373 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1374 struct nd_label_id label_id
;
1379 nvdimm_bus_lock(dev
);
1380 if (is_namespace_pmem(dev
)) {
1381 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1385 } else if (is_namespace_blk(dev
)) {
1386 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1389 flags
= NSLABEL_FLAG_LOCAL
;
1395 nd_label_gen_id(&label_id
, uuid
, flags
);
1396 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1397 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1398 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1399 struct resource
*res
;
1401 for_each_dpa_resource(ndd
, res
)
1402 if (strcmp(res
->name
, label_id
.id
) == 0)
1406 nvdimm_bus_unlock(dev
);
1408 return sprintf(buf
, "%d\n", count
);
1410 static DEVICE_ATTR_RO(dpa_extents
);
1412 static int btt_claim_class(struct device
*dev
)
1414 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1415 int i
, loop_bitmask
= 0;
1417 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1418 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1419 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1420 struct nd_namespace_index
*nsindex
;
1423 * If any of the DIMMs do not support labels the only
1424 * possible BTT format is v1.
1431 nsindex
= to_namespace_index(ndd
, ndd
->ns_current
);
1432 if (nsindex
== NULL
)
1435 /* check whether existing labels are v1.1 or v1.2 */
1436 if (__le16_to_cpu(nsindex
->major
) == 1
1437 && __le16_to_cpu(nsindex
->minor
) == 1)
1444 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1445 * block is found, a v1.1 label for any mapping will set bit 1, and a
1446 * v1.2 label will set bit 2.
1448 * At the end of the loop, at most one of the three bits must be set.
1449 * If multiple bits were set, it means the different mappings disagree
1450 * about their labels, and this must be cleaned up first.
1452 * If all the label index blocks are found to agree, nsindex of NULL
1453 * implies labels haven't been initialized yet, and when they will,
1454 * they will be of the 1.2 format, so we can assume BTT2.0
1456 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1457 * found, we enforce BTT2.0
1459 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1461 switch (loop_bitmask
) {
1464 return NVDIMM_CCLASS_BTT
;
1467 return NVDIMM_CCLASS_BTT2
;
1473 static ssize_t
holder_show(struct device
*dev
,
1474 struct device_attribute
*attr
, char *buf
)
1476 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1479 nd_device_lock(dev
);
1480 rc
= sprintf(buf
, "%s\n", ndns
->claim
? dev_name(ndns
->claim
) : "");
1481 nd_device_unlock(dev
);
1485 static DEVICE_ATTR_RO(holder
);
1487 static int __holder_class_store(struct device
*dev
, const char *buf
)
1489 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1491 if (dev
->driver
|| ndns
->claim
)
1494 if (sysfs_streq(buf
, "btt")) {
1495 int rc
= btt_claim_class(dev
);
1497 if (rc
< NVDIMM_CCLASS_NONE
)
1499 ndns
->claim_class
= rc
;
1500 } else if (sysfs_streq(buf
, "pfn"))
1501 ndns
->claim_class
= NVDIMM_CCLASS_PFN
;
1502 else if (sysfs_streq(buf
, "dax"))
1503 ndns
->claim_class
= NVDIMM_CCLASS_DAX
;
1504 else if (sysfs_streq(buf
, ""))
1505 ndns
->claim_class
= NVDIMM_CCLASS_NONE
;
1512 static ssize_t
holder_class_store(struct device
*dev
,
1513 struct device_attribute
*attr
, const char *buf
, size_t len
)
1515 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1518 nd_device_lock(dev
);
1519 nvdimm_bus_lock(dev
);
1520 wait_nvdimm_bus_probe_idle(dev
);
1521 rc
= __holder_class_store(dev
, buf
);
1523 rc
= nd_namespace_label_update(nd_region
, dev
);
1524 dev_dbg(dev
, "%s(%d)\n", rc
< 0 ? "fail " : "", rc
);
1525 nvdimm_bus_unlock(dev
);
1526 nd_device_unlock(dev
);
1528 return rc
< 0 ? rc
: len
;
1531 static ssize_t
holder_class_show(struct device
*dev
,
1532 struct device_attribute
*attr
, char *buf
)
1534 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1537 nd_device_lock(dev
);
1538 if (ndns
->claim_class
== NVDIMM_CCLASS_NONE
)
1539 rc
= sprintf(buf
, "\n");
1540 else if ((ndns
->claim_class
== NVDIMM_CCLASS_BTT
) ||
1541 (ndns
->claim_class
== NVDIMM_CCLASS_BTT2
))
1542 rc
= sprintf(buf
, "btt\n");
1543 else if (ndns
->claim_class
== NVDIMM_CCLASS_PFN
)
1544 rc
= sprintf(buf
, "pfn\n");
1545 else if (ndns
->claim_class
== NVDIMM_CCLASS_DAX
)
1546 rc
= sprintf(buf
, "dax\n");
1548 rc
= sprintf(buf
, "<unknown>\n");
1549 nd_device_unlock(dev
);
1553 static DEVICE_ATTR_RW(holder_class
);
1555 static ssize_t
mode_show(struct device
*dev
,
1556 struct device_attribute
*attr
, char *buf
)
1558 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1559 struct device
*claim
;
1563 nd_device_lock(dev
);
1564 claim
= ndns
->claim
;
1565 if (claim
&& is_nd_btt(claim
))
1567 else if (claim
&& is_nd_pfn(claim
))
1569 else if (claim
&& is_nd_dax(claim
))
1571 else if (!claim
&& pmem_should_map_pages(dev
))
1575 rc
= sprintf(buf
, "%s\n", mode
);
1576 nd_device_unlock(dev
);
1580 static DEVICE_ATTR_RO(mode
);
1582 static ssize_t
force_raw_store(struct device
*dev
,
1583 struct device_attribute
*attr
, const char *buf
, size_t len
)
1586 int rc
= strtobool(buf
, &force_raw
);
1591 to_ndns(dev
)->force_raw
= force_raw
;
1595 static ssize_t
force_raw_show(struct device
*dev
,
1596 struct device_attribute
*attr
, char *buf
)
1598 return sprintf(buf
, "%d\n", to_ndns(dev
)->force_raw
);
1600 static DEVICE_ATTR_RW(force_raw
);
1602 static struct attribute
*nd_namespace_attributes
[] = {
1603 &dev_attr_nstype
.attr
,
1604 &dev_attr_size
.attr
,
1605 &dev_attr_mode
.attr
,
1606 &dev_attr_uuid
.attr
,
1607 &dev_attr_holder
.attr
,
1608 &dev_attr_resource
.attr
,
1609 &dev_attr_alt_name
.attr
,
1610 &dev_attr_force_raw
.attr
,
1611 &dev_attr_sector_size
.attr
,
1612 &dev_attr_dpa_extents
.attr
,
1613 &dev_attr_holder_class
.attr
,
1617 static umode_t
namespace_visible(struct kobject
*kobj
,
1618 struct attribute
*a
, int n
)
1620 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1622 if (a
== &dev_attr_resource
.attr
&& is_namespace_blk(dev
))
1625 if (is_namespace_pmem(dev
) || is_namespace_blk(dev
)) {
1626 if (a
== &dev_attr_size
.attr
)
1632 if (a
== &dev_attr_nstype
.attr
|| a
== &dev_attr_size
.attr
1633 || a
== &dev_attr_holder
.attr
1634 || a
== &dev_attr_holder_class
.attr
1635 || a
== &dev_attr_force_raw
.attr
1636 || a
== &dev_attr_mode
.attr
)
1642 static struct attribute_group nd_namespace_attribute_group
= {
1643 .attrs
= nd_namespace_attributes
,
1644 .is_visible
= namespace_visible
,
1647 static const struct attribute_group
*nd_namespace_attribute_groups
[] = {
1648 &nd_device_attribute_group
,
1649 &nd_namespace_attribute_group
,
1650 &nd_numa_attribute_group
,
1654 static const struct device_type namespace_io_device_type
= {
1655 .name
= "nd_namespace_io",
1656 .release
= namespace_io_release
,
1657 .groups
= nd_namespace_attribute_groups
,
1660 static const struct device_type namespace_pmem_device_type
= {
1661 .name
= "nd_namespace_pmem",
1662 .release
= namespace_pmem_release
,
1663 .groups
= nd_namespace_attribute_groups
,
1666 static const struct device_type namespace_blk_device_type
= {
1667 .name
= "nd_namespace_blk",
1668 .release
= namespace_blk_release
,
1669 .groups
= nd_namespace_attribute_groups
,
1672 static bool is_namespace_pmem(const struct device
*dev
)
1674 return dev
? dev
->type
== &namespace_pmem_device_type
: false;
1677 static bool is_namespace_blk(const struct device
*dev
)
1679 return dev
? dev
->type
== &namespace_blk_device_type
: false;
1682 static bool is_namespace_io(const struct device
*dev
)
1684 return dev
? dev
->type
== &namespace_io_device_type
: false;
1687 struct nd_namespace_common
*nvdimm_namespace_common_probe(struct device
*dev
)
1689 struct nd_btt
*nd_btt
= is_nd_btt(dev
) ? to_nd_btt(dev
) : NULL
;
1690 struct nd_pfn
*nd_pfn
= is_nd_pfn(dev
) ? to_nd_pfn(dev
) : NULL
;
1691 struct nd_dax
*nd_dax
= is_nd_dax(dev
) ? to_nd_dax(dev
) : NULL
;
1692 struct nd_namespace_common
*ndns
= NULL
;
1693 resource_size_t size
;
1695 if (nd_btt
|| nd_pfn
|| nd_dax
) {
1697 ndns
= nd_btt
->ndns
;
1699 ndns
= nd_pfn
->ndns
;
1701 ndns
= nd_dax
->nd_pfn
.ndns
;
1704 return ERR_PTR(-ENODEV
);
1707 * Flush any in-progess probes / removals in the driver
1708 * for the raw personality of this namespace.
1710 nd_device_lock(&ndns
->dev
);
1711 nd_device_unlock(&ndns
->dev
);
1712 if (ndns
->dev
.driver
) {
1713 dev_dbg(&ndns
->dev
, "is active, can't bind %s\n",
1715 return ERR_PTR(-EBUSY
);
1717 if (dev_WARN_ONCE(&ndns
->dev
, ndns
->claim
!= dev
,
1718 "host (%s) vs claim (%s) mismatch\n",
1720 dev_name(ndns
->claim
)))
1721 return ERR_PTR(-ENXIO
);
1723 ndns
= to_ndns(dev
);
1725 dev_dbg(dev
, "claimed by %s, failing probe\n",
1726 dev_name(ndns
->claim
));
1728 return ERR_PTR(-ENXIO
);
1732 if (nvdimm_namespace_locked(ndns
))
1733 return ERR_PTR(-EACCES
);
1735 size
= nvdimm_namespace_capacity(ndns
);
1736 if (size
< ND_MIN_NAMESPACE_SIZE
) {
1737 dev_dbg(&ndns
->dev
, "%pa, too small must be at least %#x\n",
1738 &size
, ND_MIN_NAMESPACE_SIZE
);
1739 return ERR_PTR(-ENODEV
);
1742 if (is_namespace_pmem(&ndns
->dev
)) {
1743 struct nd_namespace_pmem
*nspm
;
1745 nspm
= to_nd_namespace_pmem(&ndns
->dev
);
1746 if (uuid_not_set(nspm
->uuid
, &ndns
->dev
, __func__
))
1747 return ERR_PTR(-ENODEV
);
1748 } else if (is_namespace_blk(&ndns
->dev
)) {
1749 struct nd_namespace_blk
*nsblk
;
1751 nsblk
= to_nd_namespace_blk(&ndns
->dev
);
1752 if (uuid_not_set(nsblk
->uuid
, &ndns
->dev
, __func__
))
1753 return ERR_PTR(-ENODEV
);
1754 if (!nsblk
->lbasize
) {
1755 dev_dbg(&ndns
->dev
, "sector size not set\n");
1756 return ERR_PTR(-ENODEV
);
1758 if (!nd_namespace_blk_validate(nsblk
))
1759 return ERR_PTR(-ENODEV
);
1764 EXPORT_SYMBOL(nvdimm_namespace_common_probe
);
1766 int devm_namespace_enable(struct device
*dev
, struct nd_namespace_common
*ndns
,
1767 resource_size_t size
)
1769 if (is_namespace_blk(&ndns
->dev
))
1771 return devm_nsio_enable(dev
, to_nd_namespace_io(&ndns
->dev
), size
);
1773 EXPORT_SYMBOL_GPL(devm_namespace_enable
);
1775 void devm_namespace_disable(struct device
*dev
, struct nd_namespace_common
*ndns
)
1777 if (is_namespace_blk(&ndns
->dev
))
1779 devm_nsio_disable(dev
, to_nd_namespace_io(&ndns
->dev
));
1781 EXPORT_SYMBOL_GPL(devm_namespace_disable
);
1783 static struct device
**create_namespace_io(struct nd_region
*nd_region
)
1785 struct nd_namespace_io
*nsio
;
1786 struct device
*dev
, **devs
;
1787 struct resource
*res
;
1789 nsio
= kzalloc(sizeof(*nsio
), GFP_KERNEL
);
1793 devs
= kcalloc(2, sizeof(struct device
*), GFP_KERNEL
);
1799 dev
= &nsio
->common
.dev
;
1800 dev
->type
= &namespace_io_device_type
;
1801 dev
->parent
= &nd_region
->dev
;
1803 res
->name
= dev_name(&nd_region
->dev
);
1804 res
->flags
= IORESOURCE_MEM
;
1805 res
->start
= nd_region
->ndr_start
;
1806 res
->end
= res
->start
+ nd_region
->ndr_size
- 1;
1812 static bool has_uuid_at_pos(struct nd_region
*nd_region
, u8
*uuid
,
1813 u64 cookie
, u16 pos
)
1815 struct nd_namespace_label
*found
= NULL
;
1818 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1819 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1820 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
1821 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1822 struct nd_label_ent
*label_ent
;
1823 bool found_uuid
= false;
1825 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
1826 struct nd_namespace_label
*nd_label
= label_ent
->label
;
1827 u16 position
, nlabel
;
1832 isetcookie
= __le64_to_cpu(nd_label
->isetcookie
);
1833 position
= __le16_to_cpu(nd_label
->position
);
1834 nlabel
= __le16_to_cpu(nd_label
->nlabel
);
1836 if (isetcookie
!= cookie
)
1839 if (memcmp(nd_label
->uuid
, uuid
, NSLABEL_UUID_LEN
) != 0)
1842 if (namespace_label_has(ndd
, type_guid
)
1843 && !guid_equal(&nd_set
->type_guid
,
1844 &nd_label
->type_guid
)) {
1845 dev_dbg(ndd
->dev
, "expect type_guid %pUb got %pUb\n",
1847 &nd_label
->type_guid
);
1852 dev_dbg(ndd
->dev
, "duplicate entry for uuid\n");
1856 if (nlabel
!= nd_region
->ndr_mappings
)
1858 if (position
!= pos
)
1866 return found
!= NULL
;
1869 static int select_pmem_id(struct nd_region
*nd_region
, u8
*pmem_id
)
1876 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1877 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1878 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1879 struct nd_namespace_label
*nd_label
= NULL
;
1880 u64 hw_start
, hw_end
, pmem_start
, pmem_end
;
1881 struct nd_label_ent
*label_ent
;
1883 lockdep_assert_held(&nd_mapping
->lock
);
1884 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
1885 nd_label
= label_ent
->label
;
1888 if (memcmp(nd_label
->uuid
, pmem_id
, NSLABEL_UUID_LEN
) == 0)
1899 * Check that this label is compliant with the dpa
1900 * range published in NFIT
1902 hw_start
= nd_mapping
->start
;
1903 hw_end
= hw_start
+ nd_mapping
->size
;
1904 pmem_start
= __le64_to_cpu(nd_label
->dpa
);
1905 pmem_end
= pmem_start
+ __le64_to_cpu(nd_label
->rawsize
);
1906 if (pmem_start
>= hw_start
&& pmem_start
< hw_end
1907 && pmem_end
<= hw_end
&& pmem_end
> hw_start
)
1910 dev_dbg(&nd_region
->dev
, "%s invalid label for %pUb\n",
1911 dev_name(ndd
->dev
), nd_label
->uuid
);
1915 /* move recently validated label to the front of the list */
1916 list_move(&label_ent
->list
, &nd_mapping
->labels
);
1922 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1923 * @nd_region: region with mappings to validate
1924 * @nspm: target namespace to create
1925 * @nd_label: target pmem namespace label to evaluate
1927 static struct device
*create_namespace_pmem(struct nd_region
*nd_region
,
1928 struct nd_namespace_index
*nsindex
,
1929 struct nd_namespace_label
*nd_label
)
1931 u64 cookie
= nd_region_interleave_set_cookie(nd_region
, nsindex
);
1932 u64 altcookie
= nd_region_interleave_set_altcookie(nd_region
);
1933 struct nd_label_ent
*label_ent
;
1934 struct nd_namespace_pmem
*nspm
;
1935 struct nd_mapping
*nd_mapping
;
1936 resource_size_t size
= 0;
1937 struct resource
*res
;
1943 dev_dbg(&nd_region
->dev
, "invalid interleave-set-cookie\n");
1944 return ERR_PTR(-ENXIO
);
1947 if (__le64_to_cpu(nd_label
->isetcookie
) != cookie
) {
1948 dev_dbg(&nd_region
->dev
, "invalid cookie in label: %pUb\n",
1950 if (__le64_to_cpu(nd_label
->isetcookie
) != altcookie
)
1951 return ERR_PTR(-EAGAIN
);
1953 dev_dbg(&nd_region
->dev
, "valid altcookie in label: %pUb\n",
1957 nspm
= kzalloc(sizeof(*nspm
), GFP_KERNEL
);
1959 return ERR_PTR(-ENOMEM
);
1962 dev
= &nspm
->nsio
.common
.dev
;
1963 dev
->type
= &namespace_pmem_device_type
;
1964 dev
->parent
= &nd_region
->dev
;
1965 res
= &nspm
->nsio
.res
;
1966 res
->name
= dev_name(&nd_region
->dev
);
1967 res
->flags
= IORESOURCE_MEM
;
1969 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1970 if (has_uuid_at_pos(nd_region
, nd_label
->uuid
, cookie
, i
))
1972 if (has_uuid_at_pos(nd_region
, nd_label
->uuid
, altcookie
, i
))
1977 if (i
< nd_region
->ndr_mappings
) {
1978 struct nvdimm
*nvdimm
= nd_region
->mapping
[i
].nvdimm
;
1981 * Give up if we don't find an instance of a uuid at each
1982 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1983 * find a dimm with two instances of the same uuid.
1985 dev_err(&nd_region
->dev
, "%s missing label for %pUb\n",
1986 nvdimm_name(nvdimm
), nd_label
->uuid
);
1992 * Fix up each mapping's 'labels' to have the validated pmem label for
1993 * that position at labels[0], and NULL at labels[1]. In the process,
1994 * check that the namespace aligns with interleave-set. We know
1995 * that it does not overlap with any blk namespaces by virtue of
1996 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1999 rc
= select_pmem_id(nd_region
, nd_label
->uuid
);
2003 /* Calculate total size and populate namespace properties from label0 */
2004 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2005 struct nd_namespace_label
*label0
;
2006 struct nvdimm_drvdata
*ndd
;
2008 nd_mapping
= &nd_region
->mapping
[i
];
2009 label_ent
= list_first_entry_or_null(&nd_mapping
->labels
,
2010 typeof(*label_ent
), list
);
2011 label0
= label_ent
? label_ent
->label
: NULL
;
2018 size
+= __le64_to_cpu(label0
->rawsize
);
2019 if (__le16_to_cpu(label0
->position
) != 0)
2021 WARN_ON(nspm
->alt_name
|| nspm
->uuid
);
2022 nspm
->alt_name
= kmemdup((void __force
*) label0
->name
,
2023 NSLABEL_NAME_LEN
, GFP_KERNEL
);
2024 nspm
->uuid
= kmemdup((void __force
*) label0
->uuid
,
2025 NSLABEL_UUID_LEN
, GFP_KERNEL
);
2026 nspm
->lbasize
= __le64_to_cpu(label0
->lbasize
);
2027 ndd
= to_ndd(nd_mapping
);
2028 if (namespace_label_has(ndd
, abstraction_guid
))
2029 nspm
->nsio
.common
.claim_class
2030 = to_nvdimm_cclass(&label0
->abstraction_guid
);
2034 if (!nspm
->alt_name
|| !nspm
->uuid
) {
2039 nd_namespace_pmem_set_resource(nd_region
, nspm
, size
);
2043 namespace_pmem_release(dev
);
2046 dev_dbg(&nd_region
->dev
, "invalid label(s)\n");
2049 dev_dbg(&nd_region
->dev
, "label not found\n");
2052 dev_dbg(&nd_region
->dev
, "unexpected err: %d\n", rc
);
2058 struct resource
*nsblk_add_resource(struct nd_region
*nd_region
,
2059 struct nvdimm_drvdata
*ndd
, struct nd_namespace_blk
*nsblk
,
2060 resource_size_t start
)
2062 struct nd_label_id label_id
;
2063 struct resource
*res
;
2065 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
2066 res
= krealloc(nsblk
->res
,
2067 sizeof(void *) * (nsblk
->num_resources
+ 1),
2071 nsblk
->res
= (struct resource
**) res
;
2072 for_each_dpa_resource(ndd
, res
)
2073 if (strcmp(res
->name
, label_id
.id
) == 0
2074 && res
->start
== start
) {
2075 nsblk
->res
[nsblk
->num_resources
++] = res
;
2081 static struct device
*nd_namespace_blk_create(struct nd_region
*nd_region
)
2083 struct nd_namespace_blk
*nsblk
;
2086 if (!is_nd_blk(&nd_region
->dev
))
2089 nsblk
= kzalloc(sizeof(*nsblk
), GFP_KERNEL
);
2093 dev
= &nsblk
->common
.dev
;
2094 dev
->type
= &namespace_blk_device_type
;
2095 nsblk
->id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0, GFP_KERNEL
);
2096 if (nsblk
->id
< 0) {
2100 dev_set_name(dev
, "namespace%d.%d", nd_region
->id
, nsblk
->id
);
2101 dev
->parent
= &nd_region
->dev
;
2103 return &nsblk
->common
.dev
;
2106 static struct device
*nd_namespace_pmem_create(struct nd_region
*nd_region
)
2108 struct nd_namespace_pmem
*nspm
;
2109 struct resource
*res
;
2112 if (!is_memory(&nd_region
->dev
))
2115 nspm
= kzalloc(sizeof(*nspm
), GFP_KERNEL
);
2119 dev
= &nspm
->nsio
.common
.dev
;
2120 dev
->type
= &namespace_pmem_device_type
;
2121 dev
->parent
= &nd_region
->dev
;
2122 res
= &nspm
->nsio
.res
;
2123 res
->name
= dev_name(&nd_region
->dev
);
2124 res
->flags
= IORESOURCE_MEM
;
2126 nspm
->id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0, GFP_KERNEL
);
2131 dev_set_name(dev
, "namespace%d.%d", nd_region
->id
, nspm
->id
);
2132 nd_namespace_pmem_set_resource(nd_region
, nspm
, 0);
2137 void nd_region_create_ns_seed(struct nd_region
*nd_region
)
2139 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2141 if (nd_region_to_nstype(nd_region
) == ND_DEVICE_NAMESPACE_IO
)
2144 if (is_nd_blk(&nd_region
->dev
))
2145 nd_region
->ns_seed
= nd_namespace_blk_create(nd_region
);
2147 nd_region
->ns_seed
= nd_namespace_pmem_create(nd_region
);
2150 * Seed creation failures are not fatal, provisioning is simply
2151 * disabled until memory becomes available
2153 if (!nd_region
->ns_seed
)
2154 dev_err(&nd_region
->dev
, "failed to create %s namespace\n",
2155 is_nd_blk(&nd_region
->dev
) ? "blk" : "pmem");
2157 nd_device_register(nd_region
->ns_seed
);
2160 void nd_region_create_dax_seed(struct nd_region
*nd_region
)
2162 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2163 nd_region
->dax_seed
= nd_dax_create(nd_region
);
2165 * Seed creation failures are not fatal, provisioning is simply
2166 * disabled until memory becomes available
2168 if (!nd_region
->dax_seed
)
2169 dev_err(&nd_region
->dev
, "failed to create dax namespace\n");
2172 void nd_region_create_pfn_seed(struct nd_region
*nd_region
)
2174 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2175 nd_region
->pfn_seed
= nd_pfn_create(nd_region
);
2177 * Seed creation failures are not fatal, provisioning is simply
2178 * disabled until memory becomes available
2180 if (!nd_region
->pfn_seed
)
2181 dev_err(&nd_region
->dev
, "failed to create pfn namespace\n");
2184 void nd_region_create_btt_seed(struct nd_region
*nd_region
)
2186 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2187 nd_region
->btt_seed
= nd_btt_create(nd_region
);
2189 * Seed creation failures are not fatal, provisioning is simply
2190 * disabled until memory becomes available
2192 if (!nd_region
->btt_seed
)
2193 dev_err(&nd_region
->dev
, "failed to create btt namespace\n");
2196 static int add_namespace_resource(struct nd_region
*nd_region
,
2197 struct nd_namespace_label
*nd_label
, struct device
**devs
,
2200 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
2201 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2204 for (i
= 0; i
< count
; i
++) {
2205 u8
*uuid
= namespace_to_uuid(devs
[i
]);
2206 struct resource
*res
;
2208 if (IS_ERR_OR_NULL(uuid
)) {
2213 if (memcmp(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
) != 0)
2215 if (is_namespace_blk(devs
[i
])) {
2216 res
= nsblk_add_resource(nd_region
, ndd
,
2217 to_nd_namespace_blk(devs
[i
]),
2218 __le64_to_cpu(nd_label
->dpa
));
2221 nd_dbg_dpa(nd_region
, ndd
, res
, "%d assign\n", count
);
2223 dev_err(&nd_region
->dev
,
2224 "error: conflicting extents for uuid: %pUb\n",
2234 static struct device
*create_namespace_blk(struct nd_region
*nd_region
,
2235 struct nd_namespace_label
*nd_label
, int count
)
2238 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
2239 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
2240 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2241 struct nd_namespace_blk
*nsblk
;
2242 char name
[NSLABEL_NAME_LEN
];
2243 struct device
*dev
= NULL
;
2244 struct resource
*res
;
2246 if (namespace_label_has(ndd
, type_guid
)) {
2247 if (!guid_equal(&nd_set
->type_guid
, &nd_label
->type_guid
)) {
2248 dev_dbg(ndd
->dev
, "expect type_guid %pUb got %pUb\n",
2250 &nd_label
->type_guid
);
2251 return ERR_PTR(-EAGAIN
);
2254 if (nd_label
->isetcookie
!= __cpu_to_le64(nd_set
->cookie2
)) {
2255 dev_dbg(ndd
->dev
, "expect cookie %#llx got %#llx\n",
2257 __le64_to_cpu(nd_label
->isetcookie
));
2258 return ERR_PTR(-EAGAIN
);
2262 nsblk
= kzalloc(sizeof(*nsblk
), GFP_KERNEL
);
2264 return ERR_PTR(-ENOMEM
);
2265 dev
= &nsblk
->common
.dev
;
2266 dev
->type
= &namespace_blk_device_type
;
2267 dev
->parent
= &nd_region
->dev
;
2269 nsblk
->lbasize
= __le64_to_cpu(nd_label
->lbasize
);
2270 nsblk
->uuid
= kmemdup(nd_label
->uuid
, NSLABEL_UUID_LEN
,
2272 if (namespace_label_has(ndd
, abstraction_guid
))
2273 nsblk
->common
.claim_class
2274 = to_nvdimm_cclass(&nd_label
->abstraction_guid
);
2277 memcpy(name
, nd_label
->name
, NSLABEL_NAME_LEN
);
2279 nsblk
->alt_name
= kmemdup(name
, NSLABEL_NAME_LEN
,
2281 if (!nsblk
->alt_name
)
2284 res
= nsblk_add_resource(nd_region
, ndd
, nsblk
,
2285 __le64_to_cpu(nd_label
->dpa
));
2288 nd_dbg_dpa(nd_region
, ndd
, res
, "%d: assign\n", count
);
2291 namespace_blk_release(dev
);
2292 return ERR_PTR(-ENXIO
);
2295 static int cmp_dpa(const void *a
, const void *b
)
2297 const struct device
*dev_a
= *(const struct device
**) a
;
2298 const struct device
*dev_b
= *(const struct device
**) b
;
2299 struct nd_namespace_blk
*nsblk_a
, *nsblk_b
;
2300 struct nd_namespace_pmem
*nspm_a
, *nspm_b
;
2302 if (is_namespace_io(dev_a
))
2305 if (is_namespace_blk(dev_a
)) {
2306 nsblk_a
= to_nd_namespace_blk(dev_a
);
2307 nsblk_b
= to_nd_namespace_blk(dev_b
);
2309 return memcmp(&nsblk_a
->res
[0]->start
, &nsblk_b
->res
[0]->start
,
2310 sizeof(resource_size_t
));
2313 nspm_a
= to_nd_namespace_pmem(dev_a
);
2314 nspm_b
= to_nd_namespace_pmem(dev_b
);
2316 return memcmp(&nspm_a
->nsio
.res
.start
, &nspm_b
->nsio
.res
.start
,
2317 sizeof(resource_size_t
));
2320 static struct device
**scan_labels(struct nd_region
*nd_region
)
2323 struct device
*dev
, **devs
= NULL
;
2324 struct nd_label_ent
*label_ent
, *e
;
2325 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
2326 resource_size_t map_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
2328 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2329 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
2330 struct nd_namespace_label
*nd_label
= label_ent
->label
;
2331 struct device
**__devs
;
2336 flags
= __le32_to_cpu(nd_label
->flags
);
2337 if (is_nd_blk(&nd_region
->dev
)
2338 == !!(flags
& NSLABEL_FLAG_LOCAL
))
2339 /* pass, region matches label type */;
2343 /* skip labels that describe extents outside of the region */
2344 if (__le64_to_cpu(nd_label
->dpa
) < nd_mapping
->start
||
2345 __le64_to_cpu(nd_label
->dpa
) > map_end
)
2348 i
= add_namespace_resource(nd_region
, nd_label
, devs
, count
);
2353 __devs
= kcalloc(count
+ 2, sizeof(dev
), GFP_KERNEL
);
2356 memcpy(__devs
, devs
, sizeof(dev
) * count
);
2360 if (is_nd_blk(&nd_region
->dev
))
2361 dev
= create_namespace_blk(nd_region
, nd_label
, count
);
2363 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2364 struct nd_namespace_index
*nsindex
;
2366 nsindex
= to_namespace_index(ndd
, ndd
->ns_current
);
2367 dev
= create_namespace_pmem(nd_region
, nsindex
, nd_label
);
2371 switch (PTR_ERR(dev
)) {
2373 /* skip invalid labels */
2376 /* fallthrough to seed creation */
2382 devs
[count
++] = dev
;
2386 dev_dbg(&nd_region
->dev
, "discovered %d %s namespace%s\n",
2387 count
, is_nd_blk(&nd_region
->dev
)
2388 ? "blk" : "pmem", count
== 1 ? "" : "s");
2391 /* Publish a zero-sized namespace for userspace to configure. */
2392 nd_mapping_free_labels(nd_mapping
);
2394 devs
= kcalloc(2, sizeof(dev
), GFP_KERNEL
);
2397 if (is_nd_blk(&nd_region
->dev
)) {
2398 struct nd_namespace_blk
*nsblk
;
2400 nsblk
= kzalloc(sizeof(*nsblk
), GFP_KERNEL
);
2403 dev
= &nsblk
->common
.dev
;
2404 dev
->type
= &namespace_blk_device_type
;
2406 struct nd_namespace_pmem
*nspm
;
2408 nspm
= kzalloc(sizeof(*nspm
), GFP_KERNEL
);
2411 dev
= &nspm
->nsio
.common
.dev
;
2412 dev
->type
= &namespace_pmem_device_type
;
2413 nd_namespace_pmem_set_resource(nd_region
, nspm
, 0);
2415 dev
->parent
= &nd_region
->dev
;
2416 devs
[count
++] = dev
;
2417 } else if (is_memory(&nd_region
->dev
)) {
2418 /* clean unselected labels */
2419 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2420 struct list_head
*l
, *e
;
2424 nd_mapping
= &nd_region
->mapping
[i
];
2425 if (list_empty(&nd_mapping
->labels
)) {
2431 list_for_each_safe(l
, e
, &nd_mapping
->labels
) {
2434 list_move_tail(l
, &list
);
2436 nd_mapping_free_labels(nd_mapping
);
2437 list_splice_init(&list
, &nd_mapping
->labels
);
2442 sort(devs
, count
, sizeof(struct device
*), cmp_dpa
, NULL
);
2448 for (i
= 0; devs
[i
]; i
++)
2449 if (is_nd_blk(&nd_region
->dev
))
2450 namespace_blk_release(devs
[i
]);
2452 namespace_pmem_release(devs
[i
]);
2458 static struct device
**create_namespaces(struct nd_region
*nd_region
)
2460 struct nd_mapping
*nd_mapping
;
2461 struct device
**devs
;
2464 if (nd_region
->ndr_mappings
== 0)
2467 /* lock down all mappings while we scan labels */
2468 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2469 nd_mapping
= &nd_region
->mapping
[i
];
2470 mutex_lock_nested(&nd_mapping
->lock
, i
);
2473 devs
= scan_labels(nd_region
);
2475 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2476 int reverse
= nd_region
->ndr_mappings
- 1 - i
;
2478 nd_mapping
= &nd_region
->mapping
[reverse
];
2479 mutex_unlock(&nd_mapping
->lock
);
2485 static void deactivate_labels(void *region
)
2487 struct nd_region
*nd_region
= region
;
2490 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2491 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
2492 struct nvdimm_drvdata
*ndd
= nd_mapping
->ndd
;
2493 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
2495 mutex_lock(&nd_mapping
->lock
);
2496 nd_mapping_free_labels(nd_mapping
);
2497 mutex_unlock(&nd_mapping
->lock
);
2500 nd_mapping
->ndd
= NULL
;
2502 atomic_dec(&nvdimm
->busy
);
2506 static int init_active_labels(struct nd_region
*nd_region
)
2510 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2511 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
2512 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2513 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
2514 struct nd_label_ent
*label_ent
;
2518 * If the dimm is disabled then we may need to prevent
2519 * the region from being activated.
2522 if (test_bit(NDD_LOCKED
, &nvdimm
->flags
))
2523 /* fail, label data may be unreadable */;
2524 else if (test_bit(NDD_ALIASING
, &nvdimm
->flags
))
2525 /* fail, labels needed to disambiguate dpa */;
2529 dev_err(&nd_region
->dev
, "%s: is %s, failing probe\n",
2530 dev_name(&nd_mapping
->nvdimm
->dev
),
2531 test_bit(NDD_LOCKED
, &nvdimm
->flags
)
2532 ? "locked" : "disabled");
2535 nd_mapping
->ndd
= ndd
;
2536 atomic_inc(&nvdimm
->busy
);
2539 count
= nd_label_active_count(ndd
);
2540 dev_dbg(ndd
->dev
, "count: %d\n", count
);
2543 for (j
= 0; j
< count
; j
++) {
2544 struct nd_namespace_label
*label
;
2546 label_ent
= kzalloc(sizeof(*label_ent
), GFP_KERNEL
);
2549 label
= nd_label_active(ndd
, j
);
2550 if (test_bit(NDD_NOBLK
, &nvdimm
->flags
)) {
2551 u32 flags
= __le32_to_cpu(label
->flags
);
2553 flags
&= ~NSLABEL_FLAG_LOCAL
;
2554 label
->flags
= __cpu_to_le32(flags
);
2556 label_ent
->label
= label
;
2558 mutex_lock(&nd_mapping
->lock
);
2559 list_add_tail(&label_ent
->list
, &nd_mapping
->labels
);
2560 mutex_unlock(&nd_mapping
->lock
);
2567 if (i
< nd_region
->ndr_mappings
) {
2568 deactivate_labels(nd_region
);
2572 return devm_add_action_or_reset(&nd_region
->dev
, deactivate_labels
,
2576 int nd_region_register_namespaces(struct nd_region
*nd_region
, int *err
)
2578 struct device
**devs
= NULL
;
2579 int i
, rc
= 0, type
;
2582 nvdimm_bus_lock(&nd_region
->dev
);
2583 rc
= init_active_labels(nd_region
);
2585 nvdimm_bus_unlock(&nd_region
->dev
);
2589 type
= nd_region_to_nstype(nd_region
);
2591 case ND_DEVICE_NAMESPACE_IO
:
2592 devs
= create_namespace_io(nd_region
);
2594 case ND_DEVICE_NAMESPACE_PMEM
:
2595 case ND_DEVICE_NAMESPACE_BLK
:
2596 devs
= create_namespaces(nd_region
);
2601 nvdimm_bus_unlock(&nd_region
->dev
);
2606 for (i
= 0; devs
[i
]; i
++) {
2607 struct device
*dev
= devs
[i
];
2610 if (type
== ND_DEVICE_NAMESPACE_BLK
) {
2611 struct nd_namespace_blk
*nsblk
;
2613 nsblk
= to_nd_namespace_blk(dev
);
2614 id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0,
2617 } else if (type
== ND_DEVICE_NAMESPACE_PMEM
) {
2618 struct nd_namespace_pmem
*nspm
;
2620 nspm
= to_nd_namespace_pmem(dev
);
2621 id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0,
2629 dev_set_name(dev
, "namespace%d.%d", nd_region
->id
, id
);
2630 nd_device_register(dev
);
2633 nd_region
->ns_seed
= devs
[0];
2638 for (j
= i
; devs
[j
]; j
++) {
2639 struct device
*dev
= devs
[j
];
2641 device_initialize(dev
);
2646 * All of the namespaces we tried to register failed, so
2647 * fail region activation.