2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/sort.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
23 static void namespace_io_release(struct device
*dev
)
25 struct nd_namespace_io
*nsio
= to_nd_namespace_io(dev
);
30 static void namespace_pmem_release(struct device
*dev
)
32 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
33 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
36 ida_simple_remove(&nd_region
->ns_ida
, nspm
->id
);
37 kfree(nspm
->alt_name
);
42 static void namespace_blk_release(struct device
*dev
)
44 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
45 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
48 ida_simple_remove(&nd_region
->ns_ida
, nsblk
->id
);
49 kfree(nsblk
->alt_name
);
55 static const struct device_type namespace_io_device_type
= {
56 .name
= "nd_namespace_io",
57 .release
= namespace_io_release
,
60 static const struct device_type namespace_pmem_device_type
= {
61 .name
= "nd_namespace_pmem",
62 .release
= namespace_pmem_release
,
65 static const struct device_type namespace_blk_device_type
= {
66 .name
= "nd_namespace_blk",
67 .release
= namespace_blk_release
,
70 static bool is_namespace_pmem(const struct device
*dev
)
72 return dev
? dev
->type
== &namespace_pmem_device_type
: false;
75 static bool is_namespace_blk(const struct device
*dev
)
77 return dev
? dev
->type
== &namespace_blk_device_type
: false;
80 static bool is_namespace_io(const struct device
*dev
)
82 return dev
? dev
->type
== &namespace_io_device_type
: false;
85 static int is_uuid_busy(struct device
*dev
, void *data
)
87 u8
*uuid1
= data
, *uuid2
= NULL
;
89 if (is_namespace_pmem(dev
)) {
90 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
93 } else if (is_namespace_blk(dev
)) {
94 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
97 } else if (is_nd_btt(dev
)) {
98 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
100 uuid2
= nd_btt
->uuid
;
101 } else if (is_nd_pfn(dev
)) {
102 struct nd_pfn
*nd_pfn
= to_nd_pfn(dev
);
104 uuid2
= nd_pfn
->uuid
;
107 if (uuid2
&& memcmp(uuid1
, uuid2
, NSLABEL_UUID_LEN
) == 0)
113 static int is_namespace_uuid_busy(struct device
*dev
, void *data
)
115 if (is_nd_region(dev
))
116 return device_for_each_child(dev
, data
, is_uuid_busy
);
121 * nd_is_uuid_unique - verify that no other namespace has @uuid
122 * @dev: any device on a nvdimm_bus
123 * @uuid: uuid to check
125 bool nd_is_uuid_unique(struct device
*dev
, u8
*uuid
)
127 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
131 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus
->dev
));
132 if (device_for_each_child(&nvdimm_bus
->dev
, uuid
,
133 is_namespace_uuid_busy
) != 0)
138 bool pmem_should_map_pages(struct device
*dev
)
140 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
141 struct nd_namespace_common
*ndns
= to_ndns(dev
);
142 struct nd_namespace_io
*nsio
;
144 if (!IS_ENABLED(CONFIG_ZONE_DEVICE
))
147 if (!test_bit(ND_REGION_PAGEMAP
, &nd_region
->flags
))
150 if (is_nd_pfn(dev
) || is_nd_btt(dev
))
156 nsio
= to_nd_namespace_io(dev
);
157 if (region_intersects(nsio
->res
.start
, resource_size(&nsio
->res
),
158 IORESOURCE_SYSTEM_RAM
,
159 IORES_DESC_NONE
) == REGION_MIXED
)
162 return ARCH_MEMREMAP_PMEM
== MEMREMAP_WB
;
164 EXPORT_SYMBOL(pmem_should_map_pages
);
166 unsigned int pmem_sector_size(struct nd_namespace_common
*ndns
)
168 if (is_namespace_pmem(&ndns
->dev
)) {
169 struct nd_namespace_pmem
*nspm
;
171 nspm
= to_nd_namespace_pmem(&ndns
->dev
);
172 if (nspm
->lbasize
== 0 || nspm
->lbasize
== 512)
174 else if (nspm
->lbasize
== 4096)
177 dev_WARN(&ndns
->dev
, "unsupported sector size: %ld\n",
182 * There is no namespace label (is_namespace_io()), or the label
183 * indicates the default sector size.
187 EXPORT_SYMBOL(pmem_sector_size
);
189 const char *nvdimm_namespace_disk_name(struct nd_namespace_common
*ndns
,
192 struct nd_region
*nd_region
= to_nd_region(ndns
->dev
.parent
);
193 const char *suffix
= NULL
;
195 if (ndns
->claim
&& is_nd_btt(ndns
->claim
))
198 if (is_namespace_pmem(&ndns
->dev
) || is_namespace_io(&ndns
->dev
)) {
201 if (is_namespace_pmem(&ndns
->dev
)) {
202 struct nd_namespace_pmem
*nspm
;
204 nspm
= to_nd_namespace_pmem(&ndns
->dev
);
209 sprintf(name
, "pmem%d.%d%s", nd_region
->id
, nsidx
,
210 suffix
? suffix
: "");
212 sprintf(name
, "pmem%d%s", nd_region
->id
,
213 suffix
? suffix
: "");
214 } else if (is_namespace_blk(&ndns
->dev
)) {
215 struct nd_namespace_blk
*nsblk
;
217 nsblk
= to_nd_namespace_blk(&ndns
->dev
);
218 sprintf(name
, "ndblk%d.%d%s", nd_region
->id
, nsblk
->id
,
219 suffix
? suffix
: "");
226 EXPORT_SYMBOL(nvdimm_namespace_disk_name
);
228 const u8
*nd_dev_to_uuid(struct device
*dev
)
230 static const u8 null_uuid
[16];
235 if (is_namespace_pmem(dev
)) {
236 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
239 } else if (is_namespace_blk(dev
)) {
240 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
246 EXPORT_SYMBOL(nd_dev_to_uuid
);
248 static ssize_t
nstype_show(struct device
*dev
,
249 struct device_attribute
*attr
, char *buf
)
251 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
253 return sprintf(buf
, "%d\n", nd_region_to_nstype(nd_region
));
255 static DEVICE_ATTR_RO(nstype
);
257 static ssize_t
__alt_name_store(struct device
*dev
, const char *buf
,
260 char *input
, *pos
, *alt_name
, **ns_altname
;
263 if (is_namespace_pmem(dev
)) {
264 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
266 ns_altname
= &nspm
->alt_name
;
267 } else if (is_namespace_blk(dev
)) {
268 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
270 ns_altname
= &nsblk
->alt_name
;
274 if (dev
->driver
|| to_ndns(dev
)->claim
)
277 input
= kmemdup(buf
, len
+ 1, GFP_KERNEL
);
283 if (strlen(pos
) + 1 > NSLABEL_NAME_LEN
) {
288 alt_name
= kzalloc(NSLABEL_NAME_LEN
, GFP_KERNEL
);
294 *ns_altname
= alt_name
;
295 sprintf(*ns_altname
, "%s", pos
);
303 static resource_size_t
nd_namespace_blk_size(struct nd_namespace_blk
*nsblk
)
305 struct nd_region
*nd_region
= to_nd_region(nsblk
->common
.dev
.parent
);
306 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
307 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
308 struct nd_label_id label_id
;
309 resource_size_t size
= 0;
310 struct resource
*res
;
314 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
315 for_each_dpa_resource(ndd
, res
)
316 if (strcmp(res
->name
, label_id
.id
) == 0)
317 size
+= resource_size(res
);
321 static bool __nd_namespace_blk_validate(struct nd_namespace_blk
*nsblk
)
323 struct nd_region
*nd_region
= to_nd_region(nsblk
->common
.dev
.parent
);
324 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
325 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
326 struct nd_label_id label_id
;
327 struct resource
*res
;
330 if (!nsblk
->uuid
|| !nsblk
->lbasize
|| !ndd
)
334 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
335 for_each_dpa_resource(ndd
, res
) {
336 if (strcmp(res
->name
, label_id
.id
) != 0)
339 * Resources with unacknowledged adjustments indicate a
340 * failure to update labels
342 if (res
->flags
& DPA_RESOURCE_ADJUSTED
)
347 /* These values match after a successful label update */
348 if (count
!= nsblk
->num_resources
)
351 for (i
= 0; i
< nsblk
->num_resources
; i
++) {
352 struct resource
*found
= NULL
;
354 for_each_dpa_resource(ndd
, res
)
355 if (res
== nsblk
->res
[i
]) {
367 resource_size_t
nd_namespace_blk_validate(struct nd_namespace_blk
*nsblk
)
369 resource_size_t size
;
371 nvdimm_bus_lock(&nsblk
->common
.dev
);
372 size
= __nd_namespace_blk_validate(nsblk
);
373 nvdimm_bus_unlock(&nsblk
->common
.dev
);
377 EXPORT_SYMBOL(nd_namespace_blk_validate
);
380 static int nd_namespace_label_update(struct nd_region
*nd_region
,
383 dev_WARN_ONCE(dev
, dev
->driver
|| to_ndns(dev
)->claim
,
384 "namespace must be idle during label update\n");
385 if (dev
->driver
|| to_ndns(dev
)->claim
)
389 * Only allow label writes that will result in a valid namespace
390 * or deletion of an existing namespace.
392 if (is_namespace_pmem(dev
)) {
393 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
394 resource_size_t size
= resource_size(&nspm
->nsio
.res
);
396 if (size
== 0 && nspm
->uuid
)
397 /* delete allocation */;
398 else if (!nspm
->uuid
)
401 return nd_pmem_namespace_label_update(nd_region
, nspm
, size
);
402 } else if (is_namespace_blk(dev
)) {
403 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
404 resource_size_t size
= nd_namespace_blk_size(nsblk
);
406 if (size
== 0 && nsblk
->uuid
)
407 /* delete allocation */;
408 else if (!nsblk
->uuid
|| !nsblk
->lbasize
)
411 return nd_blk_namespace_label_update(nd_region
, nsblk
, size
);
416 static ssize_t
alt_name_store(struct device
*dev
,
417 struct device_attribute
*attr
, const char *buf
, size_t len
)
419 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
423 nvdimm_bus_lock(dev
);
424 wait_nvdimm_bus_probe_idle(dev
);
425 rc
= __alt_name_store(dev
, buf
, len
);
427 rc
= nd_namespace_label_update(nd_region
, dev
);
428 dev_dbg(dev
, "%s(%zd)\n", rc
< 0 ? "fail " : "", rc
);
429 nvdimm_bus_unlock(dev
);
432 return rc
< 0 ? rc
: len
;
435 static ssize_t
alt_name_show(struct device
*dev
,
436 struct device_attribute
*attr
, char *buf
)
440 if (is_namespace_pmem(dev
)) {
441 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
443 ns_altname
= nspm
->alt_name
;
444 } else if (is_namespace_blk(dev
)) {
445 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
447 ns_altname
= nsblk
->alt_name
;
451 return sprintf(buf
, "%s\n", ns_altname
? ns_altname
: "");
453 static DEVICE_ATTR_RW(alt_name
);
455 static int scan_free(struct nd_region
*nd_region
,
456 struct nd_mapping
*nd_mapping
, struct nd_label_id
*label_id
,
459 bool is_blk
= strncmp(label_id
->id
, "blk", 3) == 0;
460 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
464 struct resource
*res
, *last
;
465 resource_size_t new_start
;
468 for_each_dpa_resource(ndd
, res
)
469 if (strcmp(res
->name
, label_id
->id
) == 0)
475 if (n
>= resource_size(res
)) {
476 n
-= resource_size(res
);
477 nd_dbg_dpa(nd_region
, ndd
, res
, "delete %d\n", rc
);
478 nvdimm_free_dpa(ndd
, res
);
479 /* retry with last resource deleted */
484 * Keep BLK allocations relegated to high DPA as much as
488 new_start
= res
->start
+ n
;
490 new_start
= res
->start
;
492 rc
= adjust_resource(res
, new_start
, resource_size(res
) - n
);
494 res
->flags
|= DPA_RESOURCE_ADJUSTED
;
495 nd_dbg_dpa(nd_region
, ndd
, res
, "shrink %d\n", rc
);
503 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
504 * @nd_region: the set of dimms to reclaim @n bytes from
505 * @label_id: unique identifier for the namespace consuming this dpa range
506 * @n: number of bytes per-dimm to release
508 * Assumes resources are ordered. Starting from the end try to
509 * adjust_resource() the allocation to @n, but if @n is larger than the
510 * allocation delete it and find the 'new' last allocation in the label
513 static int shrink_dpa_allocation(struct nd_region
*nd_region
,
514 struct nd_label_id
*label_id
, resource_size_t n
)
518 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
519 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
522 rc
= scan_free(nd_region
, nd_mapping
, label_id
, n
);
530 static resource_size_t
init_dpa_allocation(struct nd_label_id
*label_id
,
531 struct nd_region
*nd_region
, struct nd_mapping
*nd_mapping
,
534 bool is_blk
= strncmp(label_id
->id
, "blk", 3) == 0;
535 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
536 resource_size_t first_dpa
;
537 struct resource
*res
;
540 /* allocate blk from highest dpa first */
542 first_dpa
= nd_mapping
->start
+ nd_mapping
->size
- n
;
544 first_dpa
= nd_mapping
->start
;
546 /* first resource allocation for this label-id or dimm */
547 res
= nvdimm_allocate_dpa(ndd
, label_id
, first_dpa
, n
);
551 nd_dbg_dpa(nd_region
, ndd
, res
, "init %d\n", rc
);
557 * space_valid() - validate free dpa space against constraints
558 * @nd_region: hosting region of the free space
559 * @ndd: dimm device data for debug
560 * @label_id: namespace id to allocate space
561 * @prev: potential allocation that precedes free space
562 * @next: allocation that follows the given free space range
563 * @exist: first allocation with same id in the mapping
564 * @n: range that must satisfied for pmem allocations
565 * @valid: free space range to validate
567 * BLK-space is valid as long as it does not precede a PMEM
568 * allocation in a given region. PMEM-space must be contiguous
569 * and adjacent to an existing existing allocation (if one
570 * exists). If reserving PMEM any space is valid.
572 static void space_valid(struct nd_region
*nd_region
, struct nvdimm_drvdata
*ndd
,
573 struct nd_label_id
*label_id
, struct resource
*prev
,
574 struct resource
*next
, struct resource
*exist
,
575 resource_size_t n
, struct resource
*valid
)
577 bool is_reserve
= strcmp(label_id
->id
, "pmem-reserve") == 0;
578 bool is_pmem
= strncmp(label_id
->id
, "pmem", 4) == 0;
580 if (valid
->start
>= valid
->end
)
587 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
588 struct nvdimm_bus
*nvdimm_bus
;
589 struct blk_alloc_info info
= {
590 .nd_mapping
= nd_mapping
,
591 .available
= nd_mapping
->size
,
595 WARN_ON(!is_nd_blk(&nd_region
->dev
));
596 nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
597 device_for_each_child(&nvdimm_bus
->dev
, &info
, alias_dpa_busy
);
601 /* allocation needs to be contiguous, so this is all or nothing */
602 if (resource_size(valid
) < n
)
605 /* we've got all the space we need and no existing allocation */
609 /* allocation needs to be contiguous with the existing namespace */
610 if (valid
->start
== exist
->end
+ 1
611 || valid
->end
== exist
->start
- 1)
615 /* truncate @valid size to 0 */
616 valid
->end
= valid
->start
- 1;
620 ALLOC_ERR
= 0, ALLOC_BEFORE
, ALLOC_MID
, ALLOC_AFTER
,
623 static resource_size_t
scan_allocate(struct nd_region
*nd_region
,
624 struct nd_mapping
*nd_mapping
, struct nd_label_id
*label_id
,
627 resource_size_t mapping_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
628 bool is_pmem
= strncmp(label_id
->id
, "pmem", 4) == 0;
629 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
630 struct resource
*res
, *exist
= NULL
, valid
;
631 const resource_size_t to_allocate
= n
;
634 for_each_dpa_resource(ndd
, res
)
635 if (strcmp(label_id
->id
, res
->name
) == 0)
638 valid
.start
= nd_mapping
->start
;
639 valid
.end
= mapping_end
;
640 valid
.name
= "free space";
643 for_each_dpa_resource(ndd
, res
) {
644 struct resource
*next
= res
->sibling
, *new_res
= NULL
;
645 resource_size_t allocate
, available
= 0;
646 enum alloc_loc loc
= ALLOC_ERR
;
650 /* ignore resources outside this nd_mapping */
651 if (res
->start
> mapping_end
)
653 if (res
->end
< nd_mapping
->start
)
656 /* space at the beginning of the mapping */
657 if (!first
++ && res
->start
> nd_mapping
->start
) {
658 valid
.start
= nd_mapping
->start
;
659 valid
.end
= res
->start
- 1;
660 space_valid(nd_region
, ndd
, label_id
, NULL
, next
, exist
,
661 to_allocate
, &valid
);
662 available
= resource_size(&valid
);
667 /* space between allocations */
669 valid
.start
= res
->start
+ resource_size(res
);
670 valid
.end
= min(mapping_end
, next
->start
- 1);
671 space_valid(nd_region
, ndd
, label_id
, res
, next
, exist
,
672 to_allocate
, &valid
);
673 available
= resource_size(&valid
);
678 /* space at the end of the mapping */
680 valid
.start
= res
->start
+ resource_size(res
);
681 valid
.end
= mapping_end
;
682 space_valid(nd_region
, ndd
, label_id
, res
, next
, exist
,
683 to_allocate
, &valid
);
684 available
= resource_size(&valid
);
689 if (!loc
|| !available
)
691 allocate
= min(available
, n
);
694 if (strcmp(res
->name
, label_id
->id
) == 0) {
695 /* adjust current resource up */
696 rc
= adjust_resource(res
, res
->start
- allocate
,
697 resource_size(res
) + allocate
);
698 action
= "cur grow up";
703 if (strcmp(next
->name
, label_id
->id
) == 0) {
704 /* adjust next resource up */
705 rc
= adjust_resource(next
, next
->start
706 - allocate
, resource_size(next
)
709 action
= "next grow up";
710 } else if (strcmp(res
->name
, label_id
->id
) == 0) {
711 action
= "grow down";
716 if (strcmp(res
->name
, label_id
->id
) == 0)
717 action
= "grow down";
725 if (strcmp(action
, "allocate") == 0) {
726 /* BLK allocate bottom up */
728 valid
.start
+= available
- allocate
;
730 new_res
= nvdimm_allocate_dpa(ndd
, label_id
,
731 valid
.start
, allocate
);
734 } else if (strcmp(action
, "grow down") == 0) {
735 /* adjust current resource down */
736 rc
= adjust_resource(res
, res
->start
, resource_size(res
)
739 res
->flags
|= DPA_RESOURCE_ADJUSTED
;
745 nd_dbg_dpa(nd_region
, ndd
, new_res
, "%s(%d) %d\n",
754 * Retry scan with newly inserted resources.
755 * For example, if we did an ALLOC_BEFORE
756 * insertion there may also have been space
757 * available for an ALLOC_AFTER insertion, so we
758 * need to check this same resource again
766 * If we allocated nothing in the BLK case it may be because we are in
767 * an initial "pmem-reserve pass". Only do an initial BLK allocation
768 * when none of the DPA space is reserved.
770 if ((is_pmem
|| !ndd
->dpa
.child
) && n
== to_allocate
)
771 return init_dpa_allocation(label_id
, nd_region
, nd_mapping
, n
);
775 static int merge_dpa(struct nd_region
*nd_region
,
776 struct nd_mapping
*nd_mapping
, struct nd_label_id
*label_id
)
778 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
779 struct resource
*res
;
781 if (strncmp("pmem", label_id
->id
, 4) == 0)
784 for_each_dpa_resource(ndd
, res
) {
786 struct resource
*next
= res
->sibling
;
787 resource_size_t end
= res
->start
+ resource_size(res
);
789 if (!next
|| strcmp(res
->name
, label_id
->id
) != 0
790 || strcmp(next
->name
, label_id
->id
) != 0
791 || end
!= next
->start
)
793 end
+= resource_size(next
);
794 nvdimm_free_dpa(ndd
, next
);
795 rc
= adjust_resource(res
, res
->start
, end
- res
->start
);
796 nd_dbg_dpa(nd_region
, ndd
, res
, "merge %d\n", rc
);
799 res
->flags
|= DPA_RESOURCE_ADJUSTED
;
806 int __reserve_free_pmem(struct device
*dev
, void *data
)
808 struct nvdimm
*nvdimm
= data
;
809 struct nd_region
*nd_region
;
810 struct nd_label_id label_id
;
816 nd_region
= to_nd_region(dev
);
817 if (nd_region
->ndr_mappings
== 0)
820 memset(&label_id
, 0, sizeof(label_id
));
821 strcat(label_id
.id
, "pmem-reserve");
822 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
823 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
824 resource_size_t n
, rem
= 0;
826 if (nd_mapping
->nvdimm
!= nvdimm
)
829 n
= nd_pmem_available_dpa(nd_region
, nd_mapping
, &rem
);
832 rem
= scan_allocate(nd_region
, nd_mapping
, &label_id
, n
);
833 dev_WARN_ONCE(&nd_region
->dev
, rem
,
834 "pmem reserve underrun: %#llx of %#llx bytes\n",
835 (unsigned long long) n
- rem
,
836 (unsigned long long) n
);
837 return rem
? -ENXIO
: 0;
843 void release_free_pmem(struct nvdimm_bus
*nvdimm_bus
,
844 struct nd_mapping
*nd_mapping
)
846 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
847 struct resource
*res
, *_res
;
849 for_each_dpa_resource_safe(ndd
, res
, _res
)
850 if (strcmp(res
->name
, "pmem-reserve") == 0)
851 nvdimm_free_dpa(ndd
, res
);
854 static int reserve_free_pmem(struct nvdimm_bus
*nvdimm_bus
,
855 struct nd_mapping
*nd_mapping
)
857 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
860 rc
= device_for_each_child(&nvdimm_bus
->dev
, nvdimm
,
861 __reserve_free_pmem
);
863 release_free_pmem(nvdimm_bus
, nd_mapping
);
868 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
869 * @nd_region: the set of dimms to allocate @n more bytes from
870 * @label_id: unique identifier for the namespace consuming this dpa range
871 * @n: number of bytes per-dimm to add to the existing allocation
873 * Assumes resources are ordered. For BLK regions, first consume
874 * BLK-only available DPA free space, then consume PMEM-aliased DPA
875 * space starting at the highest DPA. For PMEM regions start
876 * allocations from the start of an interleave set and end at the first
877 * BLK allocation or the end of the interleave set, whichever comes
880 static int grow_dpa_allocation(struct nd_region
*nd_region
,
881 struct nd_label_id
*label_id
, resource_size_t n
)
883 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
884 bool is_pmem
= strncmp(label_id
->id
, "pmem", 4) == 0;
887 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
888 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
889 resource_size_t rem
= n
;
893 * In the BLK case try once with all unallocated PMEM
894 * reserved, and once without
896 for (j
= is_pmem
; j
< 2; j
++) {
897 bool blk_only
= j
== 0;
900 rc
= reserve_free_pmem(nvdimm_bus
, nd_mapping
);
904 rem
= scan_allocate(nd_region
, nd_mapping
,
907 release_free_pmem(nvdimm_bus
, nd_mapping
);
909 /* try again and allow encroachments into PMEM */
914 dev_WARN_ONCE(&nd_region
->dev
, rem
,
915 "allocation underrun: %#llx of %#llx bytes\n",
916 (unsigned long long) n
- rem
,
917 (unsigned long long) n
);
921 rc
= merge_dpa(nd_region
, nd_mapping
, label_id
);
929 static void nd_namespace_pmem_set_resource(struct nd_region
*nd_region
,
930 struct nd_namespace_pmem
*nspm
, resource_size_t size
)
932 struct resource
*res
= &nspm
->nsio
.res
;
933 resource_size_t offset
= 0;
935 if (size
&& !nspm
->uuid
) {
940 if (size
&& nspm
->uuid
) {
941 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
942 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
943 struct nd_label_id label_id
;
944 struct resource
*res
;
951 nd_label_gen_id(&label_id
, nspm
->uuid
, 0);
953 /* calculate a spa offset from the dpa allocation offset */
954 for_each_dpa_resource(ndd
, res
)
955 if (strcmp(res
->name
, label_id
.id
) == 0) {
956 offset
= (res
->start
- nd_mapping
->start
)
957 * nd_region
->ndr_mappings
;
966 res
->start
= nd_region
->ndr_start
+ offset
;
967 res
->end
= res
->start
+ size
- 1;
970 static bool uuid_not_set(const u8
*uuid
, struct device
*dev
, const char *where
)
973 dev_dbg(dev
, "%s: uuid not set\n", where
);
979 static ssize_t
__size_store(struct device
*dev
, unsigned long long val
)
981 resource_size_t allocated
= 0, available
= 0;
982 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
983 struct nd_namespace_common
*ndns
= to_ndns(dev
);
984 struct nd_mapping
*nd_mapping
;
985 struct nvdimm_drvdata
*ndd
;
986 struct nd_label_id label_id
;
987 u32 flags
= 0, remainder
;
991 if (dev
->driver
|| ndns
->claim
)
994 if (is_namespace_pmem(dev
)) {
995 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
999 } else if (is_namespace_blk(dev
)) {
1000 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1003 flags
= NSLABEL_FLAG_LOCAL
;
1008 * We need a uuid for the allocation-label and dimm(s) on which
1009 * to store the label.
1011 if (uuid_not_set(uuid
, dev
, __func__
))
1013 if (nd_region
->ndr_mappings
== 0) {
1014 dev_dbg(dev
, "not associated with dimm(s)\n");
1018 div_u64_rem(val
, SZ_4K
* nd_region
->ndr_mappings
, &remainder
);
1020 dev_dbg(dev
, "%llu is not %dK aligned\n", val
,
1021 (SZ_4K
* nd_region
->ndr_mappings
) / SZ_1K
);
1025 nd_label_gen_id(&label_id
, uuid
, flags
);
1026 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1027 nd_mapping
= &nd_region
->mapping
[i
];
1028 ndd
= to_ndd(nd_mapping
);
1031 * All dimms in an interleave set, or the base dimm for a blk
1032 * region, need to be enabled for the size to be changed.
1037 allocated
+= nvdimm_allocated_dpa(ndd
, &label_id
);
1039 available
= nd_region_allocatable_dpa(nd_region
);
1041 if (val
> available
+ allocated
)
1044 if (val
== allocated
)
1047 val
= div_u64(val
, nd_region
->ndr_mappings
);
1048 allocated
= div_u64(allocated
, nd_region
->ndr_mappings
);
1049 if (val
< allocated
)
1050 rc
= shrink_dpa_allocation(nd_region
, &label_id
,
1053 rc
= grow_dpa_allocation(nd_region
, &label_id
, val
- allocated
);
1058 if (is_namespace_pmem(dev
)) {
1059 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1061 nd_namespace_pmem_set_resource(nd_region
, nspm
,
1062 val
* nd_region
->ndr_mappings
);
1066 * Try to delete the namespace if we deleted all of its
1067 * allocation, this is not the seed or 0th device for the
1068 * region, and it is not actively claimed by a btt, pfn, or dax
1071 if (val
== 0 && id
!= 0 && nd_region
->ns_seed
!= dev
&& !ndns
->claim
)
1072 nd_device_unregister(dev
, ND_ASYNC
);
1077 static ssize_t
size_store(struct device
*dev
,
1078 struct device_attribute
*attr
, const char *buf
, size_t len
)
1080 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1081 unsigned long long val
;
1085 rc
= kstrtoull(buf
, 0, &val
);
1090 nvdimm_bus_lock(dev
);
1091 wait_nvdimm_bus_probe_idle(dev
);
1092 rc
= __size_store(dev
, val
);
1094 rc
= nd_namespace_label_update(nd_region
, dev
);
1096 if (is_namespace_pmem(dev
)) {
1097 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1100 } else if (is_namespace_blk(dev
)) {
1101 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1103 uuid
= &nsblk
->uuid
;
1106 if (rc
== 0 && val
== 0 && uuid
) {
1107 /* setting size zero == 'delete namespace' */
1112 dev_dbg(dev
, "%llx %s (%d)\n", val
, rc
< 0 ? "fail" : "success", rc
);
1114 nvdimm_bus_unlock(dev
);
1117 return rc
< 0 ? rc
: len
;
1120 resource_size_t
__nvdimm_namespace_capacity(struct nd_namespace_common
*ndns
)
1122 struct device
*dev
= &ndns
->dev
;
1124 if (is_namespace_pmem(dev
)) {
1125 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1127 return resource_size(&nspm
->nsio
.res
);
1128 } else if (is_namespace_blk(dev
)) {
1129 return nd_namespace_blk_size(to_nd_namespace_blk(dev
));
1130 } else if (is_namespace_io(dev
)) {
1131 struct nd_namespace_io
*nsio
= to_nd_namespace_io(dev
);
1133 return resource_size(&nsio
->res
);
1135 WARN_ONCE(1, "unknown namespace type\n");
1139 resource_size_t
nvdimm_namespace_capacity(struct nd_namespace_common
*ndns
)
1141 resource_size_t size
;
1143 nvdimm_bus_lock(&ndns
->dev
);
1144 size
= __nvdimm_namespace_capacity(ndns
);
1145 nvdimm_bus_unlock(&ndns
->dev
);
1149 EXPORT_SYMBOL(nvdimm_namespace_capacity
);
1151 bool nvdimm_namespace_locked(struct nd_namespace_common
*ndns
)
1154 bool locked
= false;
1155 struct device
*dev
= &ndns
->dev
;
1156 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1158 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1159 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1160 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
1162 if (test_bit(NDD_LOCKED
, &nvdimm
->flags
)) {
1163 dev_dbg(dev
, "%s locked\n", nvdimm_name(nvdimm
));
1169 EXPORT_SYMBOL(nvdimm_namespace_locked
);
1171 static ssize_t
size_show(struct device
*dev
,
1172 struct device_attribute
*attr
, char *buf
)
1174 return sprintf(buf
, "%llu\n", (unsigned long long)
1175 nvdimm_namespace_capacity(to_ndns(dev
)));
1177 static DEVICE_ATTR(size
, 0444, size_show
, size_store
);
1179 static u8
*namespace_to_uuid(struct device
*dev
)
1181 if (is_namespace_pmem(dev
)) {
1182 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1185 } else if (is_namespace_blk(dev
)) {
1186 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1190 return ERR_PTR(-ENXIO
);
1193 static ssize_t
uuid_show(struct device
*dev
,
1194 struct device_attribute
*attr
, char *buf
)
1196 u8
*uuid
= namespace_to_uuid(dev
);
1199 return PTR_ERR(uuid
);
1201 return sprintf(buf
, "%pUb\n", uuid
);
1202 return sprintf(buf
, "\n");
1206 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1207 * @nd_region: parent region so we can updates all dimms in the set
1208 * @dev: namespace type for generating label_id
1209 * @new_uuid: incoming uuid
1210 * @old_uuid: reference to the uuid storage location in the namespace object
1212 static int namespace_update_uuid(struct nd_region
*nd_region
,
1213 struct device
*dev
, u8
*new_uuid
, u8
**old_uuid
)
1215 u32 flags
= is_namespace_blk(dev
) ? NSLABEL_FLAG_LOCAL
: 0;
1216 struct nd_label_id old_label_id
;
1217 struct nd_label_id new_label_id
;
1220 if (!nd_is_uuid_unique(dev
, new_uuid
))
1223 if (*old_uuid
== NULL
)
1227 * If we've already written a label with this uuid, then it's
1228 * too late to rename because we can't reliably update the uuid
1229 * without losing the old namespace. Userspace must delete this
1230 * namespace to abandon the old uuid.
1232 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1233 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1236 * This check by itself is sufficient because old_uuid
1237 * would be NULL above if this uuid did not exist in the
1238 * currently written set.
1240 * FIXME: can we delete uuid with zero dpa allocated?
1242 if (list_empty(&nd_mapping
->labels
))
1246 nd_label_gen_id(&old_label_id
, *old_uuid
, flags
);
1247 nd_label_gen_id(&new_label_id
, new_uuid
, flags
);
1248 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1249 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1250 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1251 struct nd_label_ent
*label_ent
;
1252 struct resource
*res
;
1254 for_each_dpa_resource(ndd
, res
)
1255 if (strcmp(res
->name
, old_label_id
.id
) == 0)
1256 sprintf((void *) res
->name
, "%s",
1259 mutex_lock(&nd_mapping
->lock
);
1260 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
1261 struct nd_namespace_label
*nd_label
= label_ent
->label
;
1262 struct nd_label_id label_id
;
1266 nd_label_gen_id(&label_id
, nd_label
->uuid
,
1267 __le32_to_cpu(nd_label
->flags
));
1268 if (strcmp(old_label_id
.id
, label_id
.id
) == 0)
1269 set_bit(ND_LABEL_REAP
, &label_ent
->flags
);
1271 mutex_unlock(&nd_mapping
->lock
);
1275 *old_uuid
= new_uuid
;
1279 static ssize_t
uuid_store(struct device
*dev
,
1280 struct device_attribute
*attr
, const char *buf
, size_t len
)
1282 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1287 if (is_namespace_pmem(dev
)) {
1288 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1290 ns_uuid
= &nspm
->uuid
;
1291 } else if (is_namespace_blk(dev
)) {
1292 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1294 ns_uuid
= &nsblk
->uuid
;
1299 nvdimm_bus_lock(dev
);
1300 wait_nvdimm_bus_probe_idle(dev
);
1301 if (to_ndns(dev
)->claim
)
1304 rc
= nd_uuid_store(dev
, &uuid
, buf
, len
);
1306 rc
= namespace_update_uuid(nd_region
, dev
, uuid
, ns_uuid
);
1308 rc
= nd_namespace_label_update(nd_region
, dev
);
1311 dev_dbg(dev
, "result: %zd wrote: %s%s", rc
, buf
,
1312 buf
[len
- 1] == '\n' ? "" : "\n");
1313 nvdimm_bus_unlock(dev
);
1316 return rc
< 0 ? rc
: len
;
1318 static DEVICE_ATTR_RW(uuid
);
1320 static ssize_t
resource_show(struct device
*dev
,
1321 struct device_attribute
*attr
, char *buf
)
1323 struct resource
*res
;
1325 if (is_namespace_pmem(dev
)) {
1326 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1328 res
= &nspm
->nsio
.res
;
1329 } else if (is_namespace_io(dev
)) {
1330 struct nd_namespace_io
*nsio
= to_nd_namespace_io(dev
);
1336 /* no address to convey if the namespace has no allocation */
1337 if (resource_size(res
) == 0)
1339 return sprintf(buf
, "%#llx\n", (unsigned long long) res
->start
);
1341 static DEVICE_ATTR_RO(resource
);
1343 static const unsigned long blk_lbasize_supported
[] = { 512, 520, 528,
1344 4096, 4104, 4160, 4224, 0 };
1346 static const unsigned long pmem_lbasize_supported
[] = { 512, 4096, 0 };
1348 static ssize_t
sector_size_show(struct device
*dev
,
1349 struct device_attribute
*attr
, char *buf
)
1351 if (is_namespace_blk(dev
)) {
1352 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1354 return nd_size_select_show(nsblk
->lbasize
,
1355 blk_lbasize_supported
, buf
);
1358 if (is_namespace_pmem(dev
)) {
1359 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1361 return nd_size_select_show(nspm
->lbasize
,
1362 pmem_lbasize_supported
, buf
);
1367 static ssize_t
sector_size_store(struct device
*dev
,
1368 struct device_attribute
*attr
, const char *buf
, size_t len
)
1370 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1371 const unsigned long *supported
;
1372 unsigned long *lbasize
;
1375 if (is_namespace_blk(dev
)) {
1376 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1378 lbasize
= &nsblk
->lbasize
;
1379 supported
= blk_lbasize_supported
;
1380 } else if (is_namespace_pmem(dev
)) {
1381 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1383 lbasize
= &nspm
->lbasize
;
1384 supported
= pmem_lbasize_supported
;
1389 nvdimm_bus_lock(dev
);
1390 if (to_ndns(dev
)->claim
)
1393 rc
= nd_size_select_store(dev
, buf
, lbasize
, supported
);
1395 rc
= nd_namespace_label_update(nd_region
, dev
);
1396 dev_dbg(dev
, "result: %zd %s: %s%s", rc
, rc
< 0 ? "tried" : "wrote",
1397 buf
, buf
[len
- 1] == '\n' ? "" : "\n");
1398 nvdimm_bus_unlock(dev
);
1401 return rc
? rc
: len
;
1403 static DEVICE_ATTR_RW(sector_size
);
1405 static ssize_t
dpa_extents_show(struct device
*dev
,
1406 struct device_attribute
*attr
, char *buf
)
1408 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1409 struct nd_label_id label_id
;
1414 nvdimm_bus_lock(dev
);
1415 if (is_namespace_pmem(dev
)) {
1416 struct nd_namespace_pmem
*nspm
= to_nd_namespace_pmem(dev
);
1420 } else if (is_namespace_blk(dev
)) {
1421 struct nd_namespace_blk
*nsblk
= to_nd_namespace_blk(dev
);
1424 flags
= NSLABEL_FLAG_LOCAL
;
1430 nd_label_gen_id(&label_id
, uuid
, flags
);
1431 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1432 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1433 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1434 struct resource
*res
;
1436 for_each_dpa_resource(ndd
, res
)
1437 if (strcmp(res
->name
, label_id
.id
) == 0)
1441 nvdimm_bus_unlock(dev
);
1443 return sprintf(buf
, "%d\n", count
);
1445 static DEVICE_ATTR_RO(dpa_extents
);
1447 static int btt_claim_class(struct device
*dev
)
1449 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1450 int i
, loop_bitmask
= 0;
1452 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1453 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1454 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1455 struct nd_namespace_index
*nsindex
;
1458 * If any of the DIMMs do not support labels the only
1459 * possible BTT format is v1.
1466 nsindex
= to_namespace_index(ndd
, ndd
->ns_current
);
1467 if (nsindex
== NULL
)
1470 /* check whether existing labels are v1.1 or v1.2 */
1471 if (__le16_to_cpu(nsindex
->major
) == 1
1472 && __le16_to_cpu(nsindex
->minor
) == 1)
1479 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1480 * block is found, a v1.1 label for any mapping will set bit 1, and a
1481 * v1.2 label will set bit 2.
1483 * At the end of the loop, at most one of the three bits must be set.
1484 * If multiple bits were set, it means the different mappings disagree
1485 * about their labels, and this must be cleaned up first.
1487 * If all the label index blocks are found to agree, nsindex of NULL
1488 * implies labels haven't been initialized yet, and when they will,
1489 * they will be of the 1.2 format, so we can assume BTT2.0
1491 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1492 * found, we enforce BTT2.0
1494 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1496 switch (loop_bitmask
) {
1499 return NVDIMM_CCLASS_BTT
;
1502 return NVDIMM_CCLASS_BTT2
;
1508 static ssize_t
holder_show(struct device
*dev
,
1509 struct device_attribute
*attr
, char *buf
)
1511 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1515 rc
= sprintf(buf
, "%s\n", ndns
->claim
? dev_name(ndns
->claim
) : "");
1520 static DEVICE_ATTR_RO(holder
);
1522 static ssize_t
__holder_class_store(struct device
*dev
, const char *buf
)
1524 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1526 if (dev
->driver
|| ndns
->claim
)
1529 if (strcmp(buf
, "btt") == 0 || strcmp(buf
, "btt\n") == 0)
1530 ndns
->claim_class
= btt_claim_class(dev
);
1531 else if (strcmp(buf
, "pfn") == 0 || strcmp(buf
, "pfn\n") == 0)
1532 ndns
->claim_class
= NVDIMM_CCLASS_PFN
;
1533 else if (strcmp(buf
, "dax") == 0 || strcmp(buf
, "dax\n") == 0)
1534 ndns
->claim_class
= NVDIMM_CCLASS_DAX
;
1535 else if (strcmp(buf
, "") == 0 || strcmp(buf
, "\n") == 0)
1536 ndns
->claim_class
= NVDIMM_CCLASS_NONE
;
1540 /* btt_claim_class() could've returned an error */
1541 if (ndns
->claim_class
< 0)
1542 return ndns
->claim_class
;
1547 static ssize_t
holder_class_store(struct device
*dev
,
1548 struct device_attribute
*attr
, const char *buf
, size_t len
)
1550 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
1554 nvdimm_bus_lock(dev
);
1555 wait_nvdimm_bus_probe_idle(dev
);
1556 rc
= __holder_class_store(dev
, buf
);
1558 rc
= nd_namespace_label_update(nd_region
, dev
);
1559 dev_dbg(dev
, "%s(%zd)\n", rc
< 0 ? "fail " : "", rc
);
1560 nvdimm_bus_unlock(dev
);
1563 return rc
< 0 ? rc
: len
;
1566 static ssize_t
holder_class_show(struct device
*dev
,
1567 struct device_attribute
*attr
, char *buf
)
1569 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1573 if (ndns
->claim_class
== NVDIMM_CCLASS_NONE
)
1574 rc
= sprintf(buf
, "\n");
1575 else if ((ndns
->claim_class
== NVDIMM_CCLASS_BTT
) ||
1576 (ndns
->claim_class
== NVDIMM_CCLASS_BTT2
))
1577 rc
= sprintf(buf
, "btt\n");
1578 else if (ndns
->claim_class
== NVDIMM_CCLASS_PFN
)
1579 rc
= sprintf(buf
, "pfn\n");
1580 else if (ndns
->claim_class
== NVDIMM_CCLASS_DAX
)
1581 rc
= sprintf(buf
, "dax\n");
1583 rc
= sprintf(buf
, "<unknown>\n");
1588 static DEVICE_ATTR_RW(holder_class
);
1590 static ssize_t
mode_show(struct device
*dev
,
1591 struct device_attribute
*attr
, char *buf
)
1593 struct nd_namespace_common
*ndns
= to_ndns(dev
);
1594 struct device
*claim
;
1599 claim
= ndns
->claim
;
1600 if (claim
&& is_nd_btt(claim
))
1602 else if (claim
&& is_nd_pfn(claim
))
1604 else if (claim
&& is_nd_dax(claim
))
1606 else if (!claim
&& pmem_should_map_pages(dev
))
1610 rc
= sprintf(buf
, "%s\n", mode
);
1615 static DEVICE_ATTR_RO(mode
);
1617 static ssize_t
force_raw_store(struct device
*dev
,
1618 struct device_attribute
*attr
, const char *buf
, size_t len
)
1621 int rc
= strtobool(buf
, &force_raw
);
1626 to_ndns(dev
)->force_raw
= force_raw
;
1630 static ssize_t
force_raw_show(struct device
*dev
,
1631 struct device_attribute
*attr
, char *buf
)
1633 return sprintf(buf
, "%d\n", to_ndns(dev
)->force_raw
);
1635 static DEVICE_ATTR_RW(force_raw
);
1637 static struct attribute
*nd_namespace_attributes
[] = {
1638 &dev_attr_nstype
.attr
,
1639 &dev_attr_size
.attr
,
1640 &dev_attr_mode
.attr
,
1641 &dev_attr_uuid
.attr
,
1642 &dev_attr_holder
.attr
,
1643 &dev_attr_resource
.attr
,
1644 &dev_attr_alt_name
.attr
,
1645 &dev_attr_force_raw
.attr
,
1646 &dev_attr_sector_size
.attr
,
1647 &dev_attr_dpa_extents
.attr
,
1648 &dev_attr_holder_class
.attr
,
1652 static umode_t
namespace_visible(struct kobject
*kobj
,
1653 struct attribute
*a
, int n
)
1655 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1657 if (a
== &dev_attr_resource
.attr
) {
1658 if (is_namespace_blk(dev
))
1663 if (is_namespace_pmem(dev
) || is_namespace_blk(dev
)) {
1664 if (a
== &dev_attr_size
.attr
)
1670 if (a
== &dev_attr_nstype
.attr
|| a
== &dev_attr_size
.attr
1671 || a
== &dev_attr_holder
.attr
1672 || a
== &dev_attr_holder_class
.attr
1673 || a
== &dev_attr_force_raw
.attr
1674 || a
== &dev_attr_mode
.attr
)
1680 static struct attribute_group nd_namespace_attribute_group
= {
1681 .attrs
= nd_namespace_attributes
,
1682 .is_visible
= namespace_visible
,
1685 static const struct attribute_group
*nd_namespace_attribute_groups
[] = {
1686 &nd_device_attribute_group
,
1687 &nd_namespace_attribute_group
,
1688 &nd_numa_attribute_group
,
1692 struct nd_namespace_common
*nvdimm_namespace_common_probe(struct device
*dev
)
1694 struct nd_btt
*nd_btt
= is_nd_btt(dev
) ? to_nd_btt(dev
) : NULL
;
1695 struct nd_pfn
*nd_pfn
= is_nd_pfn(dev
) ? to_nd_pfn(dev
) : NULL
;
1696 struct nd_dax
*nd_dax
= is_nd_dax(dev
) ? to_nd_dax(dev
) : NULL
;
1697 struct nd_namespace_common
*ndns
= NULL
;
1698 resource_size_t size
;
1700 if (nd_btt
|| nd_pfn
|| nd_dax
) {
1702 ndns
= nd_btt
->ndns
;
1704 ndns
= nd_pfn
->ndns
;
1706 ndns
= nd_dax
->nd_pfn
.ndns
;
1709 return ERR_PTR(-ENODEV
);
1712 * Flush any in-progess probes / removals in the driver
1713 * for the raw personality of this namespace.
1715 device_lock(&ndns
->dev
);
1716 device_unlock(&ndns
->dev
);
1717 if (ndns
->dev
.driver
) {
1718 dev_dbg(&ndns
->dev
, "is active, can't bind %s\n",
1720 return ERR_PTR(-EBUSY
);
1722 if (dev_WARN_ONCE(&ndns
->dev
, ndns
->claim
!= dev
,
1723 "host (%s) vs claim (%s) mismatch\n",
1725 dev_name(ndns
->claim
)))
1726 return ERR_PTR(-ENXIO
);
1728 ndns
= to_ndns(dev
);
1730 dev_dbg(dev
, "claimed by %s, failing probe\n",
1731 dev_name(ndns
->claim
));
1733 return ERR_PTR(-ENXIO
);
1737 if (nvdimm_namespace_locked(ndns
))
1738 return ERR_PTR(-EACCES
);
1740 size
= nvdimm_namespace_capacity(ndns
);
1741 if (size
< ND_MIN_NAMESPACE_SIZE
) {
1742 dev_dbg(&ndns
->dev
, "%pa, too small must be at least %#x\n",
1743 &size
, ND_MIN_NAMESPACE_SIZE
);
1744 return ERR_PTR(-ENODEV
);
1747 if (is_namespace_pmem(&ndns
->dev
)) {
1748 struct nd_namespace_pmem
*nspm
;
1750 nspm
= to_nd_namespace_pmem(&ndns
->dev
);
1751 if (uuid_not_set(nspm
->uuid
, &ndns
->dev
, __func__
))
1752 return ERR_PTR(-ENODEV
);
1753 } else if (is_namespace_blk(&ndns
->dev
)) {
1754 struct nd_namespace_blk
*nsblk
;
1756 nsblk
= to_nd_namespace_blk(&ndns
->dev
);
1757 if (uuid_not_set(nsblk
->uuid
, &ndns
->dev
, __func__
))
1758 return ERR_PTR(-ENODEV
);
1759 if (!nsblk
->lbasize
) {
1760 dev_dbg(&ndns
->dev
, "sector size not set\n");
1761 return ERR_PTR(-ENODEV
);
1763 if (!nd_namespace_blk_validate(nsblk
))
1764 return ERR_PTR(-ENODEV
);
1769 EXPORT_SYMBOL(nvdimm_namespace_common_probe
);
1771 static struct device
**create_namespace_io(struct nd_region
*nd_region
)
1773 struct nd_namespace_io
*nsio
;
1774 struct device
*dev
, **devs
;
1775 struct resource
*res
;
1777 nsio
= kzalloc(sizeof(*nsio
), GFP_KERNEL
);
1781 devs
= kcalloc(2, sizeof(struct device
*), GFP_KERNEL
);
1787 dev
= &nsio
->common
.dev
;
1788 dev
->type
= &namespace_io_device_type
;
1789 dev
->parent
= &nd_region
->dev
;
1791 res
->name
= dev_name(&nd_region
->dev
);
1792 res
->flags
= IORESOURCE_MEM
;
1793 res
->start
= nd_region
->ndr_start
;
1794 res
->end
= res
->start
+ nd_region
->ndr_size
- 1;
1800 static bool has_uuid_at_pos(struct nd_region
*nd_region
, u8
*uuid
,
1801 u64 cookie
, u16 pos
)
1803 struct nd_namespace_label
*found
= NULL
;
1806 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1807 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1808 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
1809 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1810 struct nd_label_ent
*label_ent
;
1811 bool found_uuid
= false;
1813 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
1814 struct nd_namespace_label
*nd_label
= label_ent
->label
;
1815 u16 position
, nlabel
;
1820 isetcookie
= __le64_to_cpu(nd_label
->isetcookie
);
1821 position
= __le16_to_cpu(nd_label
->position
);
1822 nlabel
= __le16_to_cpu(nd_label
->nlabel
);
1824 if (isetcookie
!= cookie
)
1827 if (memcmp(nd_label
->uuid
, uuid
, NSLABEL_UUID_LEN
) != 0)
1830 if (namespace_label_has(ndd
, type_guid
)
1831 && !guid_equal(&nd_set
->type_guid
,
1832 &nd_label
->type_guid
)) {
1833 dev_dbg(ndd
->dev
, "expect type_guid %pUb got %pUb\n",
1834 nd_set
->type_guid
.b
,
1835 nd_label
->type_guid
.b
);
1840 dev_dbg(ndd
->dev
, "duplicate entry for uuid\n");
1844 if (nlabel
!= nd_region
->ndr_mappings
)
1846 if (position
!= pos
)
1854 return found
!= NULL
;
1857 static int select_pmem_id(struct nd_region
*nd_region
, u8
*pmem_id
)
1864 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1865 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1866 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1867 struct nd_namespace_label
*nd_label
= NULL
;
1868 u64 hw_start
, hw_end
, pmem_start
, pmem_end
;
1869 struct nd_label_ent
*label_ent
;
1871 lockdep_assert_held(&nd_mapping
->lock
);
1872 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
1873 nd_label
= label_ent
->label
;
1876 if (memcmp(nd_label
->uuid
, pmem_id
, NSLABEL_UUID_LEN
) == 0)
1887 * Check that this label is compliant with the dpa
1888 * range published in NFIT
1890 hw_start
= nd_mapping
->start
;
1891 hw_end
= hw_start
+ nd_mapping
->size
;
1892 pmem_start
= __le64_to_cpu(nd_label
->dpa
);
1893 pmem_end
= pmem_start
+ __le64_to_cpu(nd_label
->rawsize
);
1894 if (pmem_start
>= hw_start
&& pmem_start
< hw_end
1895 && pmem_end
<= hw_end
&& pmem_end
> hw_start
)
1898 dev_dbg(&nd_region
->dev
, "%s invalid label for %pUb\n",
1899 dev_name(ndd
->dev
), nd_label
->uuid
);
1903 /* move recently validated label to the front of the list */
1904 list_move(&label_ent
->list
, &nd_mapping
->labels
);
1910 * create_namespace_pmem - validate interleave set labelling, retrieve label0
1911 * @nd_region: region with mappings to validate
1912 * @nspm: target namespace to create
1913 * @nd_label: target pmem namespace label to evaluate
1915 static struct device
*create_namespace_pmem(struct nd_region
*nd_region
,
1916 struct nd_namespace_index
*nsindex
,
1917 struct nd_namespace_label
*nd_label
)
1919 u64 cookie
= nd_region_interleave_set_cookie(nd_region
, nsindex
);
1920 u64 altcookie
= nd_region_interleave_set_altcookie(nd_region
);
1921 struct nd_label_ent
*label_ent
;
1922 struct nd_namespace_pmem
*nspm
;
1923 struct nd_mapping
*nd_mapping
;
1924 resource_size_t size
= 0;
1925 struct resource
*res
;
1931 dev_dbg(&nd_region
->dev
, "invalid interleave-set-cookie\n");
1932 return ERR_PTR(-ENXIO
);
1935 if (__le64_to_cpu(nd_label
->isetcookie
) != cookie
) {
1936 dev_dbg(&nd_region
->dev
, "invalid cookie in label: %pUb\n",
1938 if (__le64_to_cpu(nd_label
->isetcookie
) != altcookie
)
1939 return ERR_PTR(-EAGAIN
);
1941 dev_dbg(&nd_region
->dev
, "valid altcookie in label: %pUb\n",
1945 nspm
= kzalloc(sizeof(*nspm
), GFP_KERNEL
);
1947 return ERR_PTR(-ENOMEM
);
1950 dev
= &nspm
->nsio
.common
.dev
;
1951 dev
->type
= &namespace_pmem_device_type
;
1952 dev
->parent
= &nd_region
->dev
;
1953 res
= &nspm
->nsio
.res
;
1954 res
->name
= dev_name(&nd_region
->dev
);
1955 res
->flags
= IORESOURCE_MEM
;
1957 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1958 if (has_uuid_at_pos(nd_region
, nd_label
->uuid
, cookie
, i
))
1960 if (has_uuid_at_pos(nd_region
, nd_label
->uuid
, altcookie
, i
))
1965 if (i
< nd_region
->ndr_mappings
) {
1966 struct nvdimm
*nvdimm
= nd_region
->mapping
[i
].nvdimm
;
1969 * Give up if we don't find an instance of a uuid at each
1970 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1971 * find a dimm with two instances of the same uuid.
1973 dev_err(&nd_region
->dev
, "%s missing label for %pUb\n",
1974 nvdimm_name(nvdimm
), nd_label
->uuid
);
1980 * Fix up each mapping's 'labels' to have the validated pmem label for
1981 * that position at labels[0], and NULL at labels[1]. In the process,
1982 * check that the namespace aligns with interleave-set. We know
1983 * that it does not overlap with any blk namespaces by virtue of
1984 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1987 rc
= select_pmem_id(nd_region
, nd_label
->uuid
);
1991 /* Calculate total size and populate namespace properties from label0 */
1992 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1993 struct nd_namespace_label
*label0
;
1994 struct nvdimm_drvdata
*ndd
;
1996 nd_mapping
= &nd_region
->mapping
[i
];
1997 label_ent
= list_first_entry_or_null(&nd_mapping
->labels
,
1998 typeof(*label_ent
), list
);
1999 label0
= label_ent
? label_ent
->label
: NULL
;
2006 size
+= __le64_to_cpu(label0
->rawsize
);
2007 if (__le16_to_cpu(label0
->position
) != 0)
2009 WARN_ON(nspm
->alt_name
|| nspm
->uuid
);
2010 nspm
->alt_name
= kmemdup((void __force
*) label0
->name
,
2011 NSLABEL_NAME_LEN
, GFP_KERNEL
);
2012 nspm
->uuid
= kmemdup((void __force
*) label0
->uuid
,
2013 NSLABEL_UUID_LEN
, GFP_KERNEL
);
2014 nspm
->lbasize
= __le64_to_cpu(label0
->lbasize
);
2015 ndd
= to_ndd(nd_mapping
);
2016 if (namespace_label_has(ndd
, abstraction_guid
))
2017 nspm
->nsio
.common
.claim_class
2018 = to_nvdimm_cclass(&label0
->abstraction_guid
);
2022 if (!nspm
->alt_name
|| !nspm
->uuid
) {
2027 nd_namespace_pmem_set_resource(nd_region
, nspm
, size
);
2031 namespace_pmem_release(dev
);
2034 dev_dbg(&nd_region
->dev
, "invalid label(s)\n");
2037 dev_dbg(&nd_region
->dev
, "label not found\n");
2040 dev_dbg(&nd_region
->dev
, "unexpected err: %d\n", rc
);
2046 struct resource
*nsblk_add_resource(struct nd_region
*nd_region
,
2047 struct nvdimm_drvdata
*ndd
, struct nd_namespace_blk
*nsblk
,
2048 resource_size_t start
)
2050 struct nd_label_id label_id
;
2051 struct resource
*res
;
2053 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
2054 res
= krealloc(nsblk
->res
,
2055 sizeof(void *) * (nsblk
->num_resources
+ 1),
2059 nsblk
->res
= (struct resource
**) res
;
2060 for_each_dpa_resource(ndd
, res
)
2061 if (strcmp(res
->name
, label_id
.id
) == 0
2062 && res
->start
== start
) {
2063 nsblk
->res
[nsblk
->num_resources
++] = res
;
2069 static struct device
*nd_namespace_blk_create(struct nd_region
*nd_region
)
2071 struct nd_namespace_blk
*nsblk
;
2074 if (!is_nd_blk(&nd_region
->dev
))
2077 nsblk
= kzalloc(sizeof(*nsblk
), GFP_KERNEL
);
2081 dev
= &nsblk
->common
.dev
;
2082 dev
->type
= &namespace_blk_device_type
;
2083 nsblk
->id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0, GFP_KERNEL
);
2084 if (nsblk
->id
< 0) {
2088 dev_set_name(dev
, "namespace%d.%d", nd_region
->id
, nsblk
->id
);
2089 dev
->parent
= &nd_region
->dev
;
2090 dev
->groups
= nd_namespace_attribute_groups
;
2092 return &nsblk
->common
.dev
;
2095 static struct device
*nd_namespace_pmem_create(struct nd_region
*nd_region
)
2097 struct nd_namespace_pmem
*nspm
;
2098 struct resource
*res
;
2101 if (!is_memory(&nd_region
->dev
))
2104 nspm
= kzalloc(sizeof(*nspm
), GFP_KERNEL
);
2108 dev
= &nspm
->nsio
.common
.dev
;
2109 dev
->type
= &namespace_pmem_device_type
;
2110 dev
->parent
= &nd_region
->dev
;
2111 res
= &nspm
->nsio
.res
;
2112 res
->name
= dev_name(&nd_region
->dev
);
2113 res
->flags
= IORESOURCE_MEM
;
2115 nspm
->id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0, GFP_KERNEL
);
2120 dev_set_name(dev
, "namespace%d.%d", nd_region
->id
, nspm
->id
);
2121 dev
->parent
= &nd_region
->dev
;
2122 dev
->groups
= nd_namespace_attribute_groups
;
2123 nd_namespace_pmem_set_resource(nd_region
, nspm
, 0);
2128 void nd_region_create_ns_seed(struct nd_region
*nd_region
)
2130 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2132 if (nd_region_to_nstype(nd_region
) == ND_DEVICE_NAMESPACE_IO
)
2135 if (is_nd_blk(&nd_region
->dev
))
2136 nd_region
->ns_seed
= nd_namespace_blk_create(nd_region
);
2138 nd_region
->ns_seed
= nd_namespace_pmem_create(nd_region
);
2141 * Seed creation failures are not fatal, provisioning is simply
2142 * disabled until memory becomes available
2144 if (!nd_region
->ns_seed
)
2145 dev_err(&nd_region
->dev
, "failed to create %s namespace\n",
2146 is_nd_blk(&nd_region
->dev
) ? "blk" : "pmem");
2148 nd_device_register(nd_region
->ns_seed
);
2151 void nd_region_create_dax_seed(struct nd_region
*nd_region
)
2153 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2154 nd_region
->dax_seed
= nd_dax_create(nd_region
);
2156 * Seed creation failures are not fatal, provisioning is simply
2157 * disabled until memory becomes available
2159 if (!nd_region
->dax_seed
)
2160 dev_err(&nd_region
->dev
, "failed to create dax namespace\n");
2163 void nd_region_create_pfn_seed(struct nd_region
*nd_region
)
2165 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2166 nd_region
->pfn_seed
= nd_pfn_create(nd_region
);
2168 * Seed creation failures are not fatal, provisioning is simply
2169 * disabled until memory becomes available
2171 if (!nd_region
->pfn_seed
)
2172 dev_err(&nd_region
->dev
, "failed to create pfn namespace\n");
2175 void nd_region_create_btt_seed(struct nd_region
*nd_region
)
2177 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
2178 nd_region
->btt_seed
= nd_btt_create(nd_region
);
2180 * Seed creation failures are not fatal, provisioning is simply
2181 * disabled until memory becomes available
2183 if (!nd_region
->btt_seed
)
2184 dev_err(&nd_region
->dev
, "failed to create btt namespace\n");
2187 static int add_namespace_resource(struct nd_region
*nd_region
,
2188 struct nd_namespace_label
*nd_label
, struct device
**devs
,
2191 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
2192 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2195 for (i
= 0; i
< count
; i
++) {
2196 u8
*uuid
= namespace_to_uuid(devs
[i
]);
2197 struct resource
*res
;
2199 if (IS_ERR_OR_NULL(uuid
)) {
2204 if (memcmp(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
) != 0)
2206 if (is_namespace_blk(devs
[i
])) {
2207 res
= nsblk_add_resource(nd_region
, ndd
,
2208 to_nd_namespace_blk(devs
[i
]),
2209 __le64_to_cpu(nd_label
->dpa
));
2212 nd_dbg_dpa(nd_region
, ndd
, res
, "%d assign\n", count
);
2214 dev_err(&nd_region
->dev
,
2215 "error: conflicting extents for uuid: %pUb\n",
2225 static struct device
*create_namespace_blk(struct nd_region
*nd_region
,
2226 struct nd_namespace_label
*nd_label
, int count
)
2229 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
2230 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
2231 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2232 struct nd_namespace_blk
*nsblk
;
2233 char name
[NSLABEL_NAME_LEN
];
2234 struct device
*dev
= NULL
;
2235 struct resource
*res
;
2237 if (namespace_label_has(ndd
, type_guid
)) {
2238 if (!guid_equal(&nd_set
->type_guid
, &nd_label
->type_guid
)) {
2239 dev_dbg(ndd
->dev
, "expect type_guid %pUb got %pUb\n",
2240 nd_set
->type_guid
.b
,
2241 nd_label
->type_guid
.b
);
2242 return ERR_PTR(-EAGAIN
);
2245 if (nd_label
->isetcookie
!= __cpu_to_le64(nd_set
->cookie2
)) {
2246 dev_dbg(ndd
->dev
, "expect cookie %#llx got %#llx\n",
2248 __le64_to_cpu(nd_label
->isetcookie
));
2249 return ERR_PTR(-EAGAIN
);
2253 nsblk
= kzalloc(sizeof(*nsblk
), GFP_KERNEL
);
2255 return ERR_PTR(-ENOMEM
);
2256 dev
= &nsblk
->common
.dev
;
2257 dev
->type
= &namespace_blk_device_type
;
2258 dev
->parent
= &nd_region
->dev
;
2260 nsblk
->lbasize
= __le64_to_cpu(nd_label
->lbasize
);
2261 nsblk
->uuid
= kmemdup(nd_label
->uuid
, NSLABEL_UUID_LEN
,
2263 if (namespace_label_has(ndd
, abstraction_guid
))
2264 nsblk
->common
.claim_class
2265 = to_nvdimm_cclass(&nd_label
->abstraction_guid
);
2268 memcpy(name
, nd_label
->name
, NSLABEL_NAME_LEN
);
2270 nsblk
->alt_name
= kmemdup(name
, NSLABEL_NAME_LEN
,
2272 if (!nsblk
->alt_name
)
2275 res
= nsblk_add_resource(nd_region
, ndd
, nsblk
,
2276 __le64_to_cpu(nd_label
->dpa
));
2279 nd_dbg_dpa(nd_region
, ndd
, res
, "%d: assign\n", count
);
2282 namespace_blk_release(dev
);
2283 return ERR_PTR(-ENXIO
);
2286 static int cmp_dpa(const void *a
, const void *b
)
2288 const struct device
*dev_a
= *(const struct device
**) a
;
2289 const struct device
*dev_b
= *(const struct device
**) b
;
2290 struct nd_namespace_blk
*nsblk_a
, *nsblk_b
;
2291 struct nd_namespace_pmem
*nspm_a
, *nspm_b
;
2293 if (is_namespace_io(dev_a
))
2296 if (is_namespace_blk(dev_a
)) {
2297 nsblk_a
= to_nd_namespace_blk(dev_a
);
2298 nsblk_b
= to_nd_namespace_blk(dev_b
);
2300 return memcmp(&nsblk_a
->res
[0]->start
, &nsblk_b
->res
[0]->start
,
2301 sizeof(resource_size_t
));
2304 nspm_a
= to_nd_namespace_pmem(dev_a
);
2305 nspm_b
= to_nd_namespace_pmem(dev_b
);
2307 return memcmp(&nspm_a
->nsio
.res
.start
, &nspm_b
->nsio
.res
.start
,
2308 sizeof(resource_size_t
));
2311 static struct device
**scan_labels(struct nd_region
*nd_region
)
2314 struct device
*dev
, **devs
= NULL
;
2315 struct nd_label_ent
*label_ent
, *e
;
2316 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
2317 resource_size_t map_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
2319 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2320 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
2321 struct nd_namespace_label
*nd_label
= label_ent
->label
;
2322 struct device
**__devs
;
2327 flags
= __le32_to_cpu(nd_label
->flags
);
2328 if (is_nd_blk(&nd_region
->dev
)
2329 == !!(flags
& NSLABEL_FLAG_LOCAL
))
2330 /* pass, region matches label type */;
2334 /* skip labels that describe extents outside of the region */
2335 if (__le64_to_cpu(nd_label
->dpa
) < nd_mapping
->start
||
2336 __le64_to_cpu(nd_label
->dpa
) > map_end
)
2339 i
= add_namespace_resource(nd_region
, nd_label
, devs
, count
);
2344 __devs
= kcalloc(count
+ 2, sizeof(dev
), GFP_KERNEL
);
2347 memcpy(__devs
, devs
, sizeof(dev
) * count
);
2351 if (is_nd_blk(&nd_region
->dev
))
2352 dev
= create_namespace_blk(nd_region
, nd_label
, count
);
2354 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2355 struct nd_namespace_index
*nsindex
;
2357 nsindex
= to_namespace_index(ndd
, ndd
->ns_current
);
2358 dev
= create_namespace_pmem(nd_region
, nsindex
, nd_label
);
2362 switch (PTR_ERR(dev
)) {
2364 /* skip invalid labels */
2367 /* fallthrough to seed creation */
2373 devs
[count
++] = dev
;
2377 dev_dbg(&nd_region
->dev
, "discovered %d %s namespace%s\n",
2378 count
, is_nd_blk(&nd_region
->dev
)
2379 ? "blk" : "pmem", count
== 1 ? "" : "s");
2382 /* Publish a zero-sized namespace for userspace to configure. */
2383 nd_mapping_free_labels(nd_mapping
);
2385 devs
= kcalloc(2, sizeof(dev
), GFP_KERNEL
);
2388 if (is_nd_blk(&nd_region
->dev
)) {
2389 struct nd_namespace_blk
*nsblk
;
2391 nsblk
= kzalloc(sizeof(*nsblk
), GFP_KERNEL
);
2394 dev
= &nsblk
->common
.dev
;
2395 dev
->type
= &namespace_blk_device_type
;
2397 struct nd_namespace_pmem
*nspm
;
2399 nspm
= kzalloc(sizeof(*nspm
), GFP_KERNEL
);
2402 dev
= &nspm
->nsio
.common
.dev
;
2403 dev
->type
= &namespace_pmem_device_type
;
2404 nd_namespace_pmem_set_resource(nd_region
, nspm
, 0);
2406 dev
->parent
= &nd_region
->dev
;
2407 devs
[count
++] = dev
;
2408 } else if (is_memory(&nd_region
->dev
)) {
2409 /* clean unselected labels */
2410 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2411 struct list_head
*l
, *e
;
2415 nd_mapping
= &nd_region
->mapping
[i
];
2416 if (list_empty(&nd_mapping
->labels
)) {
2422 list_for_each_safe(l
, e
, &nd_mapping
->labels
) {
2425 list_move_tail(l
, &list
);
2427 nd_mapping_free_labels(nd_mapping
);
2428 list_splice_init(&list
, &nd_mapping
->labels
);
2433 sort(devs
, count
, sizeof(struct device
*), cmp_dpa
, NULL
);
2439 for (i
= 0; devs
[i
]; i
++)
2440 if (is_nd_blk(&nd_region
->dev
))
2441 namespace_blk_release(devs
[i
]);
2443 namespace_pmem_release(devs
[i
]);
2449 static struct device
**create_namespaces(struct nd_region
*nd_region
)
2451 struct nd_mapping
*nd_mapping
;
2452 struct device
**devs
;
2455 if (nd_region
->ndr_mappings
== 0)
2458 /* lock down all mappings while we scan labels */
2459 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2460 nd_mapping
= &nd_region
->mapping
[i
];
2461 mutex_lock_nested(&nd_mapping
->lock
, i
);
2464 devs
= scan_labels(nd_region
);
2466 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2467 int reverse
= nd_region
->ndr_mappings
- 1 - i
;
2469 nd_mapping
= &nd_region
->mapping
[reverse
];
2470 mutex_unlock(&nd_mapping
->lock
);
2476 static int init_active_labels(struct nd_region
*nd_region
)
2480 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
2481 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
2482 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
2483 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
2484 struct nd_label_ent
*label_ent
;
2488 * If the dimm is disabled then we may need to prevent
2489 * the region from being activated.
2492 if (test_bit(NDD_LOCKED
, &nvdimm
->flags
))
2493 /* fail, label data may be unreadable */;
2494 else if (test_bit(NDD_ALIASING
, &nvdimm
->flags
))
2495 /* fail, labels needed to disambiguate dpa */;
2499 dev_err(&nd_region
->dev
, "%s: is %s, failing probe\n",
2500 dev_name(&nd_mapping
->nvdimm
->dev
),
2501 test_bit(NDD_LOCKED
, &nvdimm
->flags
)
2502 ? "locked" : "disabled");
2505 nd_mapping
->ndd
= ndd
;
2506 atomic_inc(&nvdimm
->busy
);
2509 count
= nd_label_active_count(ndd
);
2510 dev_dbg(ndd
->dev
, "count: %d\n", count
);
2513 for (j
= 0; j
< count
; j
++) {
2514 struct nd_namespace_label
*label
;
2516 label_ent
= kzalloc(sizeof(*label_ent
), GFP_KERNEL
);
2519 label
= nd_label_active(ndd
, j
);
2520 label_ent
->label
= label
;
2522 mutex_lock(&nd_mapping
->lock
);
2523 list_add_tail(&label_ent
->list
, &nd_mapping
->labels
);
2524 mutex_unlock(&nd_mapping
->lock
);
2530 mutex_lock(&nd_mapping
->lock
);
2531 nd_mapping_free_labels(nd_mapping
);
2532 mutex_unlock(&nd_mapping
->lock
);
2539 int nd_region_register_namespaces(struct nd_region
*nd_region
, int *err
)
2541 struct device
**devs
= NULL
;
2542 int i
, rc
= 0, type
;
2545 nvdimm_bus_lock(&nd_region
->dev
);
2546 rc
= init_active_labels(nd_region
);
2548 nvdimm_bus_unlock(&nd_region
->dev
);
2552 type
= nd_region_to_nstype(nd_region
);
2554 case ND_DEVICE_NAMESPACE_IO
:
2555 devs
= create_namespace_io(nd_region
);
2557 case ND_DEVICE_NAMESPACE_PMEM
:
2558 case ND_DEVICE_NAMESPACE_BLK
:
2559 devs
= create_namespaces(nd_region
);
2564 nvdimm_bus_unlock(&nd_region
->dev
);
2569 for (i
= 0; devs
[i
]; i
++) {
2570 struct device
*dev
= devs
[i
];
2573 if (type
== ND_DEVICE_NAMESPACE_BLK
) {
2574 struct nd_namespace_blk
*nsblk
;
2576 nsblk
= to_nd_namespace_blk(dev
);
2577 id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0,
2580 } else if (type
== ND_DEVICE_NAMESPACE_PMEM
) {
2581 struct nd_namespace_pmem
*nspm
;
2583 nspm
= to_nd_namespace_pmem(dev
);
2584 id
= ida_simple_get(&nd_region
->ns_ida
, 0, 0,
2592 dev_set_name(dev
, "namespace%d.%d", nd_region
->id
, id
);
2593 dev
->groups
= nd_namespace_attribute_groups
;
2594 nd_device_register(dev
);
2597 nd_region
->ns_seed
= devs
[0];
2602 for (j
= i
; devs
[j
]; j
++) {
2603 struct device
*dev
= devs
[j
];
2605 device_initialize(dev
);
2610 * All of the namespaces we tried to register failed, so
2611 * fail region activation.