1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #include <linux/kstrtox.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/sort.h>
9 #include <linux/slab.h>
10 #include <linux/list.h>
11 #include <linux/nd.h>
12 #include "nd-core.h"
13 #include "pmem.h"
14 #include "pfn.h"
15 #include "nd.h"
16 
17 static void namespace_io_release(struct device *dev)
18 {
19 	struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
20 
21 	kfree(nsio);
22 }
23 
24 static void namespace_pmem_release(struct device *dev)
25 {
26 	struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
27 	struct nd_region *nd_region = to_nd_region(dev->parent);
28 
29 	if (nspm->id >= 0)
30 		ida_simple_remove(&nd_region->ns_ida, nspm->id);
31 	kfree(nspm->alt_name);
32 	kfree(nspm->uuid);
33 	kfree(nspm);
34 }
35 
36 static bool is_namespace_pmem(const struct device *dev);
37 static bool is_namespace_io(const struct device *dev);
38 
39 static int is_uuid_busy(struct device *dev, void *data)
40 {
41 	uuid_t *uuid1 = data, *uuid2 = NULL;
42 
43 	if (is_namespace_pmem(dev)) {
44 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
45 
46 		uuid2 = nspm->uuid;
47 	} else if (is_nd_btt(dev)) {
48 		struct nd_btt *nd_btt = to_nd_btt(dev);
49 
50 		uuid2 = nd_btt->uuid;
51 	} else if (is_nd_pfn(dev)) {
52 		struct nd_pfn *nd_pfn = to_nd_pfn(dev);
53 
54 		uuid2 = nd_pfn->uuid;
55 	}
56 
57 	if (uuid2 && uuid_equal(uuid1, uuid2))
58 		return -EBUSY;
59 
60 	return 0;
61 }
62 
63 static int is_namespace_uuid_busy(struct device *dev, void *data)
64 {
65 	if (is_nd_region(dev))
66 		return device_for_each_child(dev, data, is_uuid_busy);
67 	return 0;
68 }
69 
70 /**
71  * nd_is_uuid_unique - verify that no other namespace has @uuid
72  * @dev: any device on a nvdimm_bus
73  * @uuid: uuid to check
74  */
75 bool nd_is_uuid_unique(struct device *dev, uuid_t *uuid)
76 {
77 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
78 
79 	if (!nvdimm_bus)
80 		return false;
81 	WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
82 	if (device_for_each_child(&nvdimm_bus->dev, uuid,
83 				is_namespace_uuid_busy) != 0)
84 		return false;
85 	return true;
86 }
87 
88 bool pmem_should_map_pages(struct device *dev)
89 {
90 	struct nd_region *nd_region = to_nd_region(dev->parent);
91 	struct nd_namespace_common *ndns = to_ndns(dev);
92 	struct nd_namespace_io *nsio;
93 
94 	if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
95 		return false;
96 
97 	if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
98 		return false;
99 
100 	if (is_nd_pfn(dev) || is_nd_btt(dev))
101 		return false;
102 
103 	if (ndns->force_raw)
104 		return false;
105 
106 	nsio = to_nd_namespace_io(dev);
107 	if (region_intersects(nsio->res.start, resource_size(&nsio->res),
108 				IORESOURCE_SYSTEM_RAM,
109 				IORES_DESC_NONE) == REGION_MIXED)
110 		return false;
111 
112 	return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
113 }
114 EXPORT_SYMBOL(pmem_should_map_pages);
115 
116 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
117 {
118 	if (is_namespace_pmem(&ndns->dev)) {
119 		struct nd_namespace_pmem *nspm;
120 
121 		nspm = to_nd_namespace_pmem(&ndns->dev);
122 		if (nspm->lbasize == 0 || nspm->lbasize == 512)
123 			/* default */;
124 		else if (nspm->lbasize == 4096)
125 			return 4096;
126 		else
127 			dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
128 					nspm->lbasize);
129 	}
130 
131 	/*
132 	 * There is no namespace label (is_namespace_io()), or the label
133 	 * indicates the default sector size.
134 	 */
135 	return 512;
136 }
137 EXPORT_SYMBOL(pmem_sector_size);
138 
139 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
140 		char *name)
141 {
142 	struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
143 	const char *suffix = NULL;
144 
145 	if (ndns->claim && is_nd_btt(ndns->claim))
146 		suffix = "s";
147 
148 	if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
149 		int nsidx = 0;
150 
151 		if (is_namespace_pmem(&ndns->dev)) {
152 			struct nd_namespace_pmem *nspm;
153 
154 			nspm = to_nd_namespace_pmem(&ndns->dev);
155 			nsidx = nspm->id;
156 		}
157 
158 		if (nsidx)
159 			sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
160 					suffix ? suffix : "");
161 		else
162 			sprintf(name, "pmem%d%s", nd_region->id,
163 					suffix ? suffix : "");
164 	} else {
165 		return NULL;
166 	}
167 
168 	return name;
169 }
170 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
171 
172 const uuid_t *nd_dev_to_uuid(struct device *dev)
173 {
174 	if (dev && is_namespace_pmem(dev)) {
175 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
176 
177 		return nspm->uuid;
178 	}
179 	return &uuid_null;
180 }
181 EXPORT_SYMBOL(nd_dev_to_uuid);
182 
183 static ssize_t nstype_show(struct device *dev,
184 		struct device_attribute *attr, char *buf)
185 {
186 	struct nd_region *nd_region = to_nd_region(dev->parent);
187 
188 	return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
189 }
190 static DEVICE_ATTR_RO(nstype);
191 
192 static ssize_t __alt_name_store(struct device *dev, const char *buf,
193 		const size_t len)
194 {
195 	char *input, *pos, *alt_name, **ns_altname;
196 	ssize_t rc;
197 
198 	if (is_namespace_pmem(dev)) {
199 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
200 
201 		ns_altname = &nspm->alt_name;
202 	} else
203 		return -ENXIO;
204 
205 	if (dev->driver || to_ndns(dev)->claim)
206 		return -EBUSY;
207 
208 	input = kstrndup(buf, len, GFP_KERNEL);
209 	if (!input)
210 		return -ENOMEM;
211 
212 	pos = strim(input);
213 	if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
214 		rc = -EINVAL;
215 		goto out;
216 	}
217 
218 	alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
219 	if (!alt_name) {
220 		rc = -ENOMEM;
221 		goto out;
222 	}
223 	kfree(*ns_altname);
224 	*ns_altname = alt_name;
225 	sprintf(*ns_altname, "%s", pos);
226 	rc = len;
227 
228 out:
229 	kfree(input);
230 	return rc;
231 }
232 
233 static int nd_namespace_label_update(struct nd_region *nd_region,
234 		struct device *dev)
235 {
236 	dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
237 			"namespace must be idle during label update\n");
238 	if (dev->driver || to_ndns(dev)->claim)
239 		return 0;
240 
241 	/*
242 	 * Only allow label writes that will result in a valid namespace
243 	 * or deletion of an existing namespace.
244 	 */
245 	if (is_namespace_pmem(dev)) {
246 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
247 		resource_size_t size = resource_size(&nspm->nsio.res);
248 
249 		if (size == 0 && nspm->uuid)
250 			/* delete allocation */;
251 		else if (!nspm->uuid)
252 			return 0;
253 
254 		return nd_pmem_namespace_label_update(nd_region, nspm, size);
255 	} else
256 		return -ENXIO;
257 }
258 
259 static ssize_t alt_name_store(struct device *dev,
260 		struct device_attribute *attr, const char *buf, size_t len)
261 {
262 	struct nd_region *nd_region = to_nd_region(dev->parent);
263 	ssize_t rc;
264 
265 	device_lock(dev);
266 	nvdimm_bus_lock(dev);
267 	wait_nvdimm_bus_probe_idle(dev);
268 	rc = __alt_name_store(dev, buf, len);
269 	if (rc >= 0)
270 		rc = nd_namespace_label_update(nd_region, dev);
271 	dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
272 	nvdimm_bus_unlock(dev);
273 	device_unlock(dev);
274 
275 	return rc < 0 ? rc : len;
276 }
277 
278 static ssize_t alt_name_show(struct device *dev,
279 		struct device_attribute *attr, char *buf)
280 {
281 	char *ns_altname;
282 
283 	if (is_namespace_pmem(dev)) {
284 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
285 
286 		ns_altname = nspm->alt_name;
287 	} else
288 		return -ENXIO;
289 
290 	return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
291 }
292 static DEVICE_ATTR_RW(alt_name);
293 
294 static int scan_free(struct nd_region *nd_region,
295 		struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
296 		resource_size_t n)
297 {
298 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
299 	int rc = 0;
300 
301 	while (n) {
302 		struct resource *res, *last;
303 
304 		last = NULL;
305 		for_each_dpa_resource(ndd, res)
306 			if (strcmp(res->name, label_id->id) == 0)
307 				last = res;
308 		res = last;
309 		if (!res)
310 			return 0;
311 
312 		if (n >= resource_size(res)) {
313 			n -= resource_size(res);
314 			nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
315 			nvdimm_free_dpa(ndd, res);
316 			/* retry with last resource deleted */
317 			continue;
318 		}
319 
320 		rc = adjust_resource(res, res->start, resource_size(res) - n);
321 		if (rc == 0)
322 			res->flags |= DPA_RESOURCE_ADJUSTED;
323 		nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
324 		break;
325 	}
326 
327 	return rc;
328 }
329 
330 /**
331  * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
332  * @nd_region: the set of dimms to reclaim @n bytes from
333  * @label_id: unique identifier for the namespace consuming this dpa range
334  * @n: number of bytes per-dimm to release
335  *
336  * Assumes resources are ordered.  Starting from the end try to
337  * adjust_resource() the allocation to @n, but if @n is larger than the
338  * allocation delete it and find the 'new' last allocation in the label
339  * set.
340  */
341 static int shrink_dpa_allocation(struct nd_region *nd_region,
342 		struct nd_label_id *label_id, resource_size_t n)
343 {
344 	int i;
345 
346 	for (i = 0; i < nd_region->ndr_mappings; i++) {
347 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
348 		int rc;
349 
350 		rc = scan_free(nd_region, nd_mapping, label_id, n);
351 		if (rc)
352 			return rc;
353 	}
354 
355 	return 0;
356 }
357 
358 static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
359 		struct nd_region *nd_region, struct nd_mapping *nd_mapping,
360 		resource_size_t n)
361 {
362 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
363 	struct resource *res;
364 	int rc = 0;
365 
366 	/* first resource allocation for this label-id or dimm */
367 	res = nvdimm_allocate_dpa(ndd, label_id, nd_mapping->start, n);
368 	if (!res)
369 		rc = -EBUSY;
370 
371 	nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
372 	return rc ? n : 0;
373 }
374 
375 
376 /**
377  * space_valid() - validate free dpa space against constraints
378  * @nd_region: hosting region of the free space
379  * @ndd: dimm device data for debug
380  * @label_id: namespace id to allocate space
381  * @prev: potential allocation that precedes free space
382  * @next: allocation that follows the given free space range
383  * @exist: first allocation with same id in the mapping
384  * @n: range that must satisfied for pmem allocations
385  * @valid: free space range to validate
386  *
387  * BLK-space is valid as long as it does not precede a PMEM
388  * allocation in a given region. PMEM-space must be contiguous
389  * and adjacent to an existing allocation (if one
390  * exists).  If reserving PMEM any space is valid.
391  */
392 static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
393 		struct nd_label_id *label_id, struct resource *prev,
394 		struct resource *next, struct resource *exist,
395 		resource_size_t n, struct resource *valid)
396 {
397 	bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
398 	unsigned long align;
399 
400 	align = nd_region->align / nd_region->ndr_mappings;
401 	valid->start = ALIGN(valid->start, align);
402 	valid->end = ALIGN_DOWN(valid->end + 1, align) - 1;
403 
404 	if (valid->start >= valid->end)
405 		goto invalid;
406 
407 	if (is_reserve)
408 		return;
409 
410 	/* allocation needs to be contiguous, so this is all or nothing */
411 	if (resource_size(valid) < n)
412 		goto invalid;
413 
414 	/* we've got all the space we need and no existing allocation */
415 	if (!exist)
416 		return;
417 
418 	/* allocation needs to be contiguous with the existing namespace */
419 	if (valid->start == exist->end + 1
420 			|| valid->end == exist->start - 1)
421 		return;
422 
423  invalid:
424 	/* truncate @valid size to 0 */
425 	valid->end = valid->start - 1;
426 }
427 
428 enum alloc_loc {
429 	ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
430 };
431 
432 static resource_size_t scan_allocate(struct nd_region *nd_region,
433 		struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
434 		resource_size_t n)
435 {
436 	resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
437 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
438 	struct resource *res, *exist = NULL, valid;
439 	const resource_size_t to_allocate = n;
440 	int first;
441 
442 	for_each_dpa_resource(ndd, res)
443 		if (strcmp(label_id->id, res->name) == 0)
444 			exist = res;
445 
446 	valid.start = nd_mapping->start;
447 	valid.end = mapping_end;
448 	valid.name = "free space";
449  retry:
450 	first = 0;
451 	for_each_dpa_resource(ndd, res) {
452 		struct resource *next = res->sibling, *new_res = NULL;
453 		resource_size_t allocate, available = 0;
454 		enum alloc_loc loc = ALLOC_ERR;
455 		const char *action;
456 		int rc = 0;
457 
458 		/* ignore resources outside this nd_mapping */
459 		if (res->start > mapping_end)
460 			continue;
461 		if (res->end < nd_mapping->start)
462 			continue;
463 
464 		/* space at the beginning of the mapping */
465 		if (!first++ && res->start > nd_mapping->start) {
466 			valid.start = nd_mapping->start;
467 			valid.end = res->start - 1;
468 			space_valid(nd_region, ndd, label_id, NULL, next, exist,
469 					to_allocate, &valid);
470 			available = resource_size(&valid);
471 			if (available)
472 				loc = ALLOC_BEFORE;
473 		}
474 
475 		/* space between allocations */
476 		if (!loc && next) {
477 			valid.start = res->start + resource_size(res);
478 			valid.end = min(mapping_end, next->start - 1);
479 			space_valid(nd_region, ndd, label_id, res, next, exist,
480 					to_allocate, &valid);
481 			available = resource_size(&valid);
482 			if (available)
483 				loc = ALLOC_MID;
484 		}
485 
486 		/* space at the end of the mapping */
487 		if (!loc && !next) {
488 			valid.start = res->start + resource_size(res);
489 			valid.end = mapping_end;
490 			space_valid(nd_region, ndd, label_id, res, next, exist,
491 					to_allocate, &valid);
492 			available = resource_size(&valid);
493 			if (available)
494 				loc = ALLOC_AFTER;
495 		}
496 
497 		if (!loc || !available)
498 			continue;
499 		allocate = min(available, n);
500 		switch (loc) {
501 		case ALLOC_BEFORE:
502 			if (strcmp(res->name, label_id->id) == 0) {
503 				/* adjust current resource up */
504 				rc = adjust_resource(res, res->start - allocate,
505 						resource_size(res) + allocate);
506 				action = "cur grow up";
507 			} else
508 				action = "allocate";
509 			break;
510 		case ALLOC_MID:
511 			if (strcmp(next->name, label_id->id) == 0) {
512 				/* adjust next resource up */
513 				rc = adjust_resource(next, next->start
514 						- allocate, resource_size(next)
515 						+ allocate);
516 				new_res = next;
517 				action = "next grow up";
518 			} else if (strcmp(res->name, label_id->id) == 0) {
519 				action = "grow down";
520 			} else
521 				action = "allocate";
522 			break;
523 		case ALLOC_AFTER:
524 			if (strcmp(res->name, label_id->id) == 0)
525 				action = "grow down";
526 			else
527 				action = "allocate";
528 			break;
529 		default:
530 			return n;
531 		}
532 
533 		if (strcmp(action, "allocate") == 0) {
534 			new_res = nvdimm_allocate_dpa(ndd, label_id,
535 					valid.start, allocate);
536 			if (!new_res)
537 				rc = -EBUSY;
538 		} else if (strcmp(action, "grow down") == 0) {
539 			/* adjust current resource down */
540 			rc = adjust_resource(res, res->start, resource_size(res)
541 					+ allocate);
542 			if (rc == 0)
543 				res->flags |= DPA_RESOURCE_ADJUSTED;
544 		}
545 
546 		if (!new_res)
547 			new_res = res;
548 
549 		nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
550 				action, loc, rc);
551 
552 		if (rc)
553 			return n;
554 
555 		n -= allocate;
556 		if (n) {
557 			/*
558 			 * Retry scan with newly inserted resources.
559 			 * For example, if we did an ALLOC_BEFORE
560 			 * insertion there may also have been space
561 			 * available for an ALLOC_AFTER insertion, so we
562 			 * need to check this same resource again
563 			 */
564 			goto retry;
565 		} else
566 			return 0;
567 	}
568 
569 	if (n == to_allocate)
570 		return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
571 	return n;
572 }
573 
574 static int merge_dpa(struct nd_region *nd_region,
575 		struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
576 {
577 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
578 	struct resource *res;
579 
580 	if (strncmp("pmem", label_id->id, 4) == 0)
581 		return 0;
582  retry:
583 	for_each_dpa_resource(ndd, res) {
584 		int rc;
585 		struct resource *next = res->sibling;
586 		resource_size_t end = res->start + resource_size(res);
587 
588 		if (!next || strcmp(res->name, label_id->id) != 0
589 				|| strcmp(next->name, label_id->id) != 0
590 				|| end != next->start)
591 			continue;
592 		end += resource_size(next);
593 		nvdimm_free_dpa(ndd, next);
594 		rc = adjust_resource(res, res->start, end - res->start);
595 		nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
596 		if (rc)
597 			return rc;
598 		res->flags |= DPA_RESOURCE_ADJUSTED;
599 		goto retry;
600 	}
601 
602 	return 0;
603 }
604 
605 int __reserve_free_pmem(struct device *dev, void *data)
606 {
607 	struct nvdimm *nvdimm = data;
608 	struct nd_region *nd_region;
609 	struct nd_label_id label_id;
610 	int i;
611 
612 	if (!is_memory(dev))
613 		return 0;
614 
615 	nd_region = to_nd_region(dev);
616 	if (nd_region->ndr_mappings == 0)
617 		return 0;
618 
619 	memset(&label_id, 0, sizeof(label_id));
620 	strcat(label_id.id, "pmem-reserve");
621 	for (i = 0; i < nd_region->ndr_mappings; i++) {
622 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
623 		resource_size_t n, rem = 0;
624 
625 		if (nd_mapping->nvdimm != nvdimm)
626 			continue;
627 
628 		n = nd_pmem_available_dpa(nd_region, nd_mapping);
629 		if (n == 0)
630 			return 0;
631 		rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
632 		dev_WARN_ONCE(&nd_region->dev, rem,
633 				"pmem reserve underrun: %#llx of %#llx bytes\n",
634 				(unsigned long long) n - rem,
635 				(unsigned long long) n);
636 		return rem ? -ENXIO : 0;
637 	}
638 
639 	return 0;
640 }
641 
642 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
643 		struct nd_mapping *nd_mapping)
644 {
645 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
646 	struct resource *res, *_res;
647 
648 	for_each_dpa_resource_safe(ndd, res, _res)
649 		if (strcmp(res->name, "pmem-reserve") == 0)
650 			nvdimm_free_dpa(ndd, res);
651 }
652 
653 /**
654  * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
655  * @nd_region: the set of dimms to allocate @n more bytes from
656  * @label_id: unique identifier for the namespace consuming this dpa range
657  * @n: number of bytes per-dimm to add to the existing allocation
658  *
659  * Assumes resources are ordered.  For BLK regions, first consume
660  * BLK-only available DPA free space, then consume PMEM-aliased DPA
661  * space starting at the highest DPA.  For PMEM regions start
662  * allocations from the start of an interleave set and end at the first
663  * BLK allocation or the end of the interleave set, whichever comes
664  * first.
665  */
666 static int grow_dpa_allocation(struct nd_region *nd_region,
667 		struct nd_label_id *label_id, resource_size_t n)
668 {
669 	int i;
670 
671 	for (i = 0; i < nd_region->ndr_mappings; i++) {
672 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
673 		resource_size_t rem = n;
674 		int rc;
675 
676 		rem = scan_allocate(nd_region, nd_mapping, label_id, rem);
677 		dev_WARN_ONCE(&nd_region->dev, rem,
678 				"allocation underrun: %#llx of %#llx bytes\n",
679 				(unsigned long long) n - rem,
680 				(unsigned long long) n);
681 		if (rem)
682 			return -ENXIO;
683 
684 		rc = merge_dpa(nd_region, nd_mapping, label_id);
685 		if (rc)
686 			return rc;
687 	}
688 
689 	return 0;
690 }
691 
692 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
693 		struct nd_namespace_pmem *nspm, resource_size_t size)
694 {
695 	struct resource *res = &nspm->nsio.res;
696 	resource_size_t offset = 0;
697 
698 	if (size && !nspm->uuid) {
699 		WARN_ON_ONCE(1);
700 		size = 0;
701 	}
702 
703 	if (size && nspm->uuid) {
704 		struct nd_mapping *nd_mapping = &nd_region->mapping[0];
705 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
706 		struct nd_label_id label_id;
707 		struct resource *res;
708 
709 		if (!ndd) {
710 			size = 0;
711 			goto out;
712 		}
713 
714 		nd_label_gen_id(&label_id, nspm->uuid, 0);
715 
716 		/* calculate a spa offset from the dpa allocation offset */
717 		for_each_dpa_resource(ndd, res)
718 			if (strcmp(res->name, label_id.id) == 0) {
719 				offset = (res->start - nd_mapping->start)
720 					* nd_region->ndr_mappings;
721 				goto out;
722 			}
723 
724 		WARN_ON_ONCE(1);
725 		size = 0;
726 	}
727 
728  out:
729 	res->start = nd_region->ndr_start + offset;
730 	res->end = res->start + size - 1;
731 }
732 
733 static bool uuid_not_set(const uuid_t *uuid, struct device *dev,
734 			 const char *where)
735 {
736 	if (!uuid) {
737 		dev_dbg(dev, "%s: uuid not set\n", where);
738 		return true;
739 	}
740 	return false;
741 }
742 
743 static ssize_t __size_store(struct device *dev, unsigned long long val)
744 {
745 	resource_size_t allocated = 0, available = 0;
746 	struct nd_region *nd_region = to_nd_region(dev->parent);
747 	struct nd_namespace_common *ndns = to_ndns(dev);
748 	struct nd_mapping *nd_mapping;
749 	struct nvdimm_drvdata *ndd;
750 	struct nd_label_id label_id;
751 	u32 flags = 0, remainder;
752 	int rc, i, id = -1;
753 	uuid_t *uuid = NULL;
754 
755 	if (dev->driver || ndns->claim)
756 		return -EBUSY;
757 
758 	if (is_namespace_pmem(dev)) {
759 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
760 
761 		uuid = nspm->uuid;
762 		id = nspm->id;
763 	}
764 
765 	/*
766 	 * We need a uuid for the allocation-label and dimm(s) on which
767 	 * to store the label.
768 	 */
769 	if (uuid_not_set(uuid, dev, __func__))
770 		return -ENXIO;
771 	if (nd_region->ndr_mappings == 0) {
772 		dev_dbg(dev, "not associated with dimm(s)\n");
773 		return -ENXIO;
774 	}
775 
776 	div_u64_rem(val, nd_region->align, &remainder);
777 	if (remainder) {
778 		dev_dbg(dev, "%llu is not %ldK aligned\n", val,
779 				nd_region->align / SZ_1K);
780 		return -EINVAL;
781 	}
782 
783 	nd_label_gen_id(&label_id, uuid, flags);
784 	for (i = 0; i < nd_region->ndr_mappings; i++) {
785 		nd_mapping = &nd_region->mapping[i];
786 		ndd = to_ndd(nd_mapping);
787 
788 		/*
789 		 * All dimms in an interleave set, need to be enabled
790 		 * for the size to be changed.
791 		 */
792 		if (!ndd)
793 			return -ENXIO;
794 
795 		allocated += nvdimm_allocated_dpa(ndd, &label_id);
796 	}
797 	available = nd_region_allocatable_dpa(nd_region);
798 
799 	if (val > available + allocated)
800 		return -ENOSPC;
801 
802 	if (val == allocated)
803 		return 0;
804 
805 	val = div_u64(val, nd_region->ndr_mappings);
806 	allocated = div_u64(allocated, nd_region->ndr_mappings);
807 	if (val < allocated)
808 		rc = shrink_dpa_allocation(nd_region, &label_id,
809 				allocated - val);
810 	else
811 		rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
812 
813 	if (rc)
814 		return rc;
815 
816 	if (is_namespace_pmem(dev)) {
817 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
818 
819 		nd_namespace_pmem_set_resource(nd_region, nspm,
820 				val * nd_region->ndr_mappings);
821 	}
822 
823 	/*
824 	 * Try to delete the namespace if we deleted all of its
825 	 * allocation, this is not the seed or 0th device for the
826 	 * region, and it is not actively claimed by a btt, pfn, or dax
827 	 * instance.
828 	 */
829 	if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
830 		nd_device_unregister(dev, ND_ASYNC);
831 
832 	return rc;
833 }
834 
835 static ssize_t size_store(struct device *dev,
836 		struct device_attribute *attr, const char *buf, size_t len)
837 {
838 	struct nd_region *nd_region = to_nd_region(dev->parent);
839 	unsigned long long val;
840 	int rc;
841 
842 	rc = kstrtoull(buf, 0, &val);
843 	if (rc)
844 		return rc;
845 
846 	device_lock(dev);
847 	nvdimm_bus_lock(dev);
848 	wait_nvdimm_bus_probe_idle(dev);
849 	rc = __size_store(dev, val);
850 	if (rc >= 0)
851 		rc = nd_namespace_label_update(nd_region, dev);
852 
853 	/* setting size zero == 'delete namespace' */
854 	if (rc == 0 && val == 0 && is_namespace_pmem(dev)) {
855 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
856 
857 		kfree(nspm->uuid);
858 		nspm->uuid = NULL;
859 	}
860 
861 	dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
862 
863 	nvdimm_bus_unlock(dev);
864 	device_unlock(dev);
865 
866 	return rc < 0 ? rc : len;
867 }
868 
869 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
870 {
871 	struct device *dev = &ndns->dev;
872 
873 	if (is_namespace_pmem(dev)) {
874 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
875 
876 		return resource_size(&nspm->nsio.res);
877 	} else if (is_namespace_io(dev)) {
878 		struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
879 
880 		return resource_size(&nsio->res);
881 	} else
882 		WARN_ONCE(1, "unknown namespace type\n");
883 	return 0;
884 }
885 
886 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
887 {
888 	resource_size_t size;
889 
890 	nvdimm_bus_lock(&ndns->dev);
891 	size = __nvdimm_namespace_capacity(ndns);
892 	nvdimm_bus_unlock(&ndns->dev);
893 
894 	return size;
895 }
896 EXPORT_SYMBOL(nvdimm_namespace_capacity);
897 
898 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
899 {
900 	int i;
901 	bool locked = false;
902 	struct device *dev = &ndns->dev;
903 	struct nd_region *nd_region = to_nd_region(dev->parent);
904 
905 	for (i = 0; i < nd_region->ndr_mappings; i++) {
906 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
907 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
908 
909 		if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
910 			dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
911 			locked = true;
912 		}
913 	}
914 	return locked;
915 }
916 EXPORT_SYMBOL(nvdimm_namespace_locked);
917 
918 static ssize_t size_show(struct device *dev,
919 		struct device_attribute *attr, char *buf)
920 {
921 	return sprintf(buf, "%llu\n", (unsigned long long)
922 			nvdimm_namespace_capacity(to_ndns(dev)));
923 }
924 static DEVICE_ATTR(size, 0444, size_show, size_store);
925 
926 static uuid_t *namespace_to_uuid(struct device *dev)
927 {
928 	if (is_namespace_pmem(dev)) {
929 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
930 
931 		return nspm->uuid;
932 	}
933 	return ERR_PTR(-ENXIO);
934 }
935 
936 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
937 			 char *buf)
938 {
939 	uuid_t *uuid = namespace_to_uuid(dev);
940 
941 	if (IS_ERR(uuid))
942 		return PTR_ERR(uuid);
943 	if (uuid)
944 		return sprintf(buf, "%pUb\n", uuid);
945 	return sprintf(buf, "\n");
946 }
947 
948 /**
949  * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
950  * @nd_region: parent region so we can updates all dimms in the set
951  * @dev: namespace type for generating label_id
952  * @new_uuid: incoming uuid
953  * @old_uuid: reference to the uuid storage location in the namespace object
954  */
955 static int namespace_update_uuid(struct nd_region *nd_region,
956 				 struct device *dev, uuid_t *new_uuid,
957 				 uuid_t **old_uuid)
958 {
959 	struct nd_label_id old_label_id;
960 	struct nd_label_id new_label_id;
961 	int i;
962 
963 	if (!nd_is_uuid_unique(dev, new_uuid))
964 		return -EINVAL;
965 
966 	if (*old_uuid == NULL)
967 		goto out;
968 
969 	/*
970 	 * If we've already written a label with this uuid, then it's
971 	 * too late to rename because we can't reliably update the uuid
972 	 * without losing the old namespace.  Userspace must delete this
973 	 * namespace to abandon the old uuid.
974 	 */
975 	for (i = 0; i < nd_region->ndr_mappings; i++) {
976 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
977 
978 		/*
979 		 * This check by itself is sufficient because old_uuid
980 		 * would be NULL above if this uuid did not exist in the
981 		 * currently written set.
982 		 *
983 		 * FIXME: can we delete uuid with zero dpa allocated?
984 		 */
985 		if (list_empty(&nd_mapping->labels))
986 			return -EBUSY;
987 	}
988 
989 	nd_label_gen_id(&old_label_id, *old_uuid, 0);
990 	nd_label_gen_id(&new_label_id, new_uuid, 0);
991 	for (i = 0; i < nd_region->ndr_mappings; i++) {
992 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
993 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
994 		struct nd_label_ent *label_ent;
995 		struct resource *res;
996 
997 		for_each_dpa_resource(ndd, res)
998 			if (strcmp(res->name, old_label_id.id) == 0)
999 				sprintf((void *) res->name, "%s",
1000 						new_label_id.id);
1001 
1002 		mutex_lock(&nd_mapping->lock);
1003 		list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1004 			struct nd_namespace_label *nd_label = label_ent->label;
1005 			struct nd_label_id label_id;
1006 			uuid_t uuid;
1007 
1008 			if (!nd_label)
1009 				continue;
1010 			nsl_get_uuid(ndd, nd_label, &uuid);
1011 			nd_label_gen_id(&label_id, &uuid,
1012 					nsl_get_flags(ndd, nd_label));
1013 			if (strcmp(old_label_id.id, label_id.id) == 0)
1014 				set_bit(ND_LABEL_REAP, &label_ent->flags);
1015 		}
1016 		mutex_unlock(&nd_mapping->lock);
1017 	}
1018 	kfree(*old_uuid);
1019  out:
1020 	*old_uuid = new_uuid;
1021 	return 0;
1022 }
1023 
1024 static ssize_t uuid_store(struct device *dev,
1025 		struct device_attribute *attr, const char *buf, size_t len)
1026 {
1027 	struct nd_region *nd_region = to_nd_region(dev->parent);
1028 	uuid_t *uuid = NULL;
1029 	uuid_t **ns_uuid;
1030 	ssize_t rc = 0;
1031 
1032 	if (is_namespace_pmem(dev)) {
1033 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1034 
1035 		ns_uuid = &nspm->uuid;
1036 	} else
1037 		return -ENXIO;
1038 
1039 	device_lock(dev);
1040 	nvdimm_bus_lock(dev);
1041 	wait_nvdimm_bus_probe_idle(dev);
1042 	if (to_ndns(dev)->claim)
1043 		rc = -EBUSY;
1044 	if (rc >= 0)
1045 		rc = nd_uuid_store(dev, &uuid, buf, len);
1046 	if (rc >= 0)
1047 		rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1048 	if (rc >= 0)
1049 		rc = nd_namespace_label_update(nd_region, dev);
1050 	else
1051 		kfree(uuid);
1052 	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1053 			buf[len - 1] == '\n' ? "" : "\n");
1054 	nvdimm_bus_unlock(dev);
1055 	device_unlock(dev);
1056 
1057 	return rc < 0 ? rc : len;
1058 }
1059 static DEVICE_ATTR_RW(uuid);
1060 
1061 static ssize_t resource_show(struct device *dev,
1062 		struct device_attribute *attr, char *buf)
1063 {
1064 	struct resource *res;
1065 
1066 	if (is_namespace_pmem(dev)) {
1067 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1068 
1069 		res = &nspm->nsio.res;
1070 	} else if (is_namespace_io(dev)) {
1071 		struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1072 
1073 		res = &nsio->res;
1074 	} else
1075 		return -ENXIO;
1076 
1077 	/* no address to convey if the namespace has no allocation */
1078 	if (resource_size(res) == 0)
1079 		return -ENXIO;
1080 	return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1081 }
1082 static DEVICE_ATTR_ADMIN_RO(resource);
1083 
1084 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1085 
1086 static ssize_t sector_size_show(struct device *dev,
1087 		struct device_attribute *attr, char *buf)
1088 {
1089 	if (is_namespace_pmem(dev)) {
1090 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1091 
1092 		return nd_size_select_show(nspm->lbasize,
1093 				pmem_lbasize_supported, buf);
1094 	}
1095 	return -ENXIO;
1096 }
1097 
1098 static ssize_t sector_size_store(struct device *dev,
1099 		struct device_attribute *attr, const char *buf, size_t len)
1100 {
1101 	struct nd_region *nd_region = to_nd_region(dev->parent);
1102 	const unsigned long *supported;
1103 	unsigned long *lbasize;
1104 	ssize_t rc = 0;
1105 
1106 	if (is_namespace_pmem(dev)) {
1107 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1108 
1109 		lbasize = &nspm->lbasize;
1110 		supported = pmem_lbasize_supported;
1111 	} else
1112 		return -ENXIO;
1113 
1114 	device_lock(dev);
1115 	nvdimm_bus_lock(dev);
1116 	if (to_ndns(dev)->claim)
1117 		rc = -EBUSY;
1118 	if (rc >= 0)
1119 		rc = nd_size_select_store(dev, buf, lbasize, supported);
1120 	if (rc >= 0)
1121 		rc = nd_namespace_label_update(nd_region, dev);
1122 	dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1123 			buf, buf[len - 1] == '\n' ? "" : "\n");
1124 	nvdimm_bus_unlock(dev);
1125 	device_unlock(dev);
1126 
1127 	return rc ? rc : len;
1128 }
1129 static DEVICE_ATTR_RW(sector_size);
1130 
1131 static ssize_t dpa_extents_show(struct device *dev,
1132 		struct device_attribute *attr, char *buf)
1133 {
1134 	struct nd_region *nd_region = to_nd_region(dev->parent);
1135 	struct nd_label_id label_id;
1136 	uuid_t *uuid = NULL;
1137 	int count = 0, i;
1138 	u32 flags = 0;
1139 
1140 	nvdimm_bus_lock(dev);
1141 	if (is_namespace_pmem(dev)) {
1142 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1143 
1144 		uuid = nspm->uuid;
1145 		flags = 0;
1146 	}
1147 
1148 	if (!uuid)
1149 		goto out;
1150 
1151 	nd_label_gen_id(&label_id, uuid, flags);
1152 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1153 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1154 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1155 		struct resource *res;
1156 
1157 		for_each_dpa_resource(ndd, res)
1158 			if (strcmp(res->name, label_id.id) == 0)
1159 				count++;
1160 	}
1161  out:
1162 	nvdimm_bus_unlock(dev);
1163 
1164 	return sprintf(buf, "%d\n", count);
1165 }
1166 static DEVICE_ATTR_RO(dpa_extents);
1167 
1168 static int btt_claim_class(struct device *dev)
1169 {
1170 	struct nd_region *nd_region = to_nd_region(dev->parent);
1171 	int i, loop_bitmask = 0;
1172 
1173 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1174 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1175 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1176 		struct nd_namespace_index *nsindex;
1177 
1178 		/*
1179 		 * If any of the DIMMs do not support labels the only
1180 		 * possible BTT format is v1.
1181 		 */
1182 		if (!ndd) {
1183 			loop_bitmask = 0;
1184 			break;
1185 		}
1186 
1187 		nsindex = to_namespace_index(ndd, ndd->ns_current);
1188 		if (nsindex == NULL)
1189 			loop_bitmask |= 1;
1190 		else {
1191 			/* check whether existing labels are v1.1 or v1.2 */
1192 			if (__le16_to_cpu(nsindex->major) == 1
1193 					&& __le16_to_cpu(nsindex->minor) == 1)
1194 				loop_bitmask |= 2;
1195 			else
1196 				loop_bitmask |= 4;
1197 		}
1198 	}
1199 	/*
1200 	 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1201 	 * block is found, a v1.1 label for any mapping will set bit 1, and a
1202 	 * v1.2 label will set bit 2.
1203 	 *
1204 	 * At the end of the loop, at most one of the three bits must be set.
1205 	 * If multiple bits were set, it means the different mappings disagree
1206 	 * about their labels, and this must be cleaned up first.
1207 	 *
1208 	 * If all the label index blocks are found to agree, nsindex of NULL
1209 	 * implies labels haven't been initialized yet, and when they will,
1210 	 * they will be of the 1.2 format, so we can assume BTT2.0
1211 	 *
1212 	 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1213 	 * found, we enforce BTT2.0
1214 	 *
1215 	 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1216 	 */
1217 	switch (loop_bitmask) {
1218 	case 0:
1219 	case 2:
1220 		return NVDIMM_CCLASS_BTT;
1221 	case 1:
1222 	case 4:
1223 		return NVDIMM_CCLASS_BTT2;
1224 	default:
1225 		return -ENXIO;
1226 	}
1227 }
1228 
1229 static ssize_t holder_show(struct device *dev,
1230 		struct device_attribute *attr, char *buf)
1231 {
1232 	struct nd_namespace_common *ndns = to_ndns(dev);
1233 	ssize_t rc;
1234 
1235 	device_lock(dev);
1236 	rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1237 	device_unlock(dev);
1238 
1239 	return rc;
1240 }
1241 static DEVICE_ATTR_RO(holder);
1242 
1243 static int __holder_class_store(struct device *dev, const char *buf)
1244 {
1245 	struct nd_namespace_common *ndns = to_ndns(dev);
1246 
1247 	if (dev->driver || ndns->claim)
1248 		return -EBUSY;
1249 
1250 	if (sysfs_streq(buf, "btt")) {
1251 		int rc = btt_claim_class(dev);
1252 
1253 		if (rc < NVDIMM_CCLASS_NONE)
1254 			return rc;
1255 		ndns->claim_class = rc;
1256 	} else if (sysfs_streq(buf, "pfn"))
1257 		ndns->claim_class = NVDIMM_CCLASS_PFN;
1258 	else if (sysfs_streq(buf, "dax"))
1259 		ndns->claim_class = NVDIMM_CCLASS_DAX;
1260 	else if (sysfs_streq(buf, ""))
1261 		ndns->claim_class = NVDIMM_CCLASS_NONE;
1262 	else
1263 		return -EINVAL;
1264 
1265 	return 0;
1266 }
1267 
1268 static ssize_t holder_class_store(struct device *dev,
1269 		struct device_attribute *attr, const char *buf, size_t len)
1270 {
1271 	struct nd_region *nd_region = to_nd_region(dev->parent);
1272 	int rc;
1273 
1274 	device_lock(dev);
1275 	nvdimm_bus_lock(dev);
1276 	wait_nvdimm_bus_probe_idle(dev);
1277 	rc = __holder_class_store(dev, buf);
1278 	if (rc >= 0)
1279 		rc = nd_namespace_label_update(nd_region, dev);
1280 	dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
1281 	nvdimm_bus_unlock(dev);
1282 	device_unlock(dev);
1283 
1284 	return rc < 0 ? rc : len;
1285 }
1286 
1287 static ssize_t holder_class_show(struct device *dev,
1288 		struct device_attribute *attr, char *buf)
1289 {
1290 	struct nd_namespace_common *ndns = to_ndns(dev);
1291 	ssize_t rc;
1292 
1293 	device_lock(dev);
1294 	if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1295 		rc = sprintf(buf, "\n");
1296 	else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1297 			(ndns->claim_class == NVDIMM_CCLASS_BTT2))
1298 		rc = sprintf(buf, "btt\n");
1299 	else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1300 		rc = sprintf(buf, "pfn\n");
1301 	else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1302 		rc = sprintf(buf, "dax\n");
1303 	else
1304 		rc = sprintf(buf, "<unknown>\n");
1305 	device_unlock(dev);
1306 
1307 	return rc;
1308 }
1309 static DEVICE_ATTR_RW(holder_class);
1310 
1311 static ssize_t mode_show(struct device *dev,
1312 		struct device_attribute *attr, char *buf)
1313 {
1314 	struct nd_namespace_common *ndns = to_ndns(dev);
1315 	struct device *claim;
1316 	char *mode;
1317 	ssize_t rc;
1318 
1319 	device_lock(dev);
1320 	claim = ndns->claim;
1321 	if (claim && is_nd_btt(claim))
1322 		mode = "safe";
1323 	else if (claim && is_nd_pfn(claim))
1324 		mode = "memory";
1325 	else if (claim && is_nd_dax(claim))
1326 		mode = "dax";
1327 	else if (!claim && pmem_should_map_pages(dev))
1328 		mode = "memory";
1329 	else
1330 		mode = "raw";
1331 	rc = sprintf(buf, "%s\n", mode);
1332 	device_unlock(dev);
1333 
1334 	return rc;
1335 }
1336 static DEVICE_ATTR_RO(mode);
1337 
1338 static ssize_t force_raw_store(struct device *dev,
1339 		struct device_attribute *attr, const char *buf, size_t len)
1340 {
1341 	bool force_raw;
1342 	int rc = kstrtobool(buf, &force_raw);
1343 
1344 	if (rc)
1345 		return rc;
1346 
1347 	to_ndns(dev)->force_raw = force_raw;
1348 	return len;
1349 }
1350 
1351 static ssize_t force_raw_show(struct device *dev,
1352 		struct device_attribute *attr, char *buf)
1353 {
1354 	return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1355 }
1356 static DEVICE_ATTR_RW(force_raw);
1357 
1358 static struct attribute *nd_namespace_attributes[] = {
1359 	&dev_attr_nstype.attr,
1360 	&dev_attr_size.attr,
1361 	&dev_attr_mode.attr,
1362 	&dev_attr_uuid.attr,
1363 	&dev_attr_holder.attr,
1364 	&dev_attr_resource.attr,
1365 	&dev_attr_alt_name.attr,
1366 	&dev_attr_force_raw.attr,
1367 	&dev_attr_sector_size.attr,
1368 	&dev_attr_dpa_extents.attr,
1369 	&dev_attr_holder_class.attr,
1370 	NULL,
1371 };
1372 
1373 static umode_t namespace_visible(struct kobject *kobj,
1374 		struct attribute *a, int n)
1375 {
1376 	struct device *dev = container_of(kobj, struct device, kobj);
1377 
1378 	if (is_namespace_pmem(dev)) {
1379 		if (a == &dev_attr_size.attr)
1380 			return 0644;
1381 
1382 		return a->mode;
1383 	}
1384 
1385 	/* base is_namespace_io() attributes */
1386 	if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr ||
1387 	    a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr ||
1388 	    a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr ||
1389 	    a == &dev_attr_resource.attr)
1390 		return a->mode;
1391 
1392 	return 0;
1393 }
1394 
1395 static struct attribute_group nd_namespace_attribute_group = {
1396 	.attrs = nd_namespace_attributes,
1397 	.is_visible = namespace_visible,
1398 };
1399 
1400 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1401 	&nd_device_attribute_group,
1402 	&nd_namespace_attribute_group,
1403 	&nd_numa_attribute_group,
1404 	NULL,
1405 };
1406 
1407 static const struct device_type namespace_io_device_type = {
1408 	.name = "nd_namespace_io",
1409 	.release = namespace_io_release,
1410 	.groups = nd_namespace_attribute_groups,
1411 };
1412 
1413 static const struct device_type namespace_pmem_device_type = {
1414 	.name = "nd_namespace_pmem",
1415 	.release = namespace_pmem_release,
1416 	.groups = nd_namespace_attribute_groups,
1417 };
1418 
1419 static bool is_namespace_pmem(const struct device *dev)
1420 {
1421 	return dev ? dev->type == &namespace_pmem_device_type : false;
1422 }
1423 
1424 static bool is_namespace_io(const struct device *dev)
1425 {
1426 	return dev ? dev->type == &namespace_io_device_type : false;
1427 }
1428 
1429 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1430 {
1431 	struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1432 	struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1433 	struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1434 	struct nd_namespace_common *ndns = NULL;
1435 	resource_size_t size;
1436 
1437 	if (nd_btt || nd_pfn || nd_dax) {
1438 		if (nd_btt)
1439 			ndns = nd_btt->ndns;
1440 		else if (nd_pfn)
1441 			ndns = nd_pfn->ndns;
1442 		else if (nd_dax)
1443 			ndns = nd_dax->nd_pfn.ndns;
1444 
1445 		if (!ndns)
1446 			return ERR_PTR(-ENODEV);
1447 
1448 		/*
1449 		 * Flush any in-progess probes / removals in the driver
1450 		 * for the raw personality of this namespace.
1451 		 */
1452 		device_lock(&ndns->dev);
1453 		device_unlock(&ndns->dev);
1454 		if (ndns->dev.driver) {
1455 			dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1456 					dev_name(dev));
1457 			return ERR_PTR(-EBUSY);
1458 		}
1459 		if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1460 					"host (%s) vs claim (%s) mismatch\n",
1461 					dev_name(dev),
1462 					dev_name(ndns->claim)))
1463 			return ERR_PTR(-ENXIO);
1464 	} else {
1465 		ndns = to_ndns(dev);
1466 		if (ndns->claim) {
1467 			dev_dbg(dev, "claimed by %s, failing probe\n",
1468 				dev_name(ndns->claim));
1469 
1470 			return ERR_PTR(-ENXIO);
1471 		}
1472 	}
1473 
1474 	if (nvdimm_namespace_locked(ndns))
1475 		return ERR_PTR(-EACCES);
1476 
1477 	size = nvdimm_namespace_capacity(ndns);
1478 	if (size < ND_MIN_NAMESPACE_SIZE) {
1479 		dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1480 				&size, ND_MIN_NAMESPACE_SIZE);
1481 		return ERR_PTR(-ENODEV);
1482 	}
1483 
1484 	/*
1485 	 * Note, alignment validation for fsdax and devdax mode
1486 	 * namespaces happens in nd_pfn_validate() where infoblock
1487 	 * padding parameters can be applied.
1488 	 */
1489 	if (pmem_should_map_pages(dev)) {
1490 		struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
1491 		struct resource *res = &nsio->res;
1492 
1493 		if (!IS_ALIGNED(res->start | (res->end + 1),
1494 					memremap_compat_align())) {
1495 			dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
1496 			return ERR_PTR(-EOPNOTSUPP);
1497 		}
1498 	}
1499 
1500 	if (is_namespace_pmem(&ndns->dev)) {
1501 		struct nd_namespace_pmem *nspm;
1502 
1503 		nspm = to_nd_namespace_pmem(&ndns->dev);
1504 		if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1505 			return ERR_PTR(-ENODEV);
1506 	}
1507 
1508 	return ndns;
1509 }
1510 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1511 
1512 int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1513 		resource_size_t size)
1514 {
1515 	return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1516 }
1517 EXPORT_SYMBOL_GPL(devm_namespace_enable);
1518 
1519 void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1520 {
1521 	devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1522 }
1523 EXPORT_SYMBOL_GPL(devm_namespace_disable);
1524 
1525 static struct device **create_namespace_io(struct nd_region *nd_region)
1526 {
1527 	struct nd_namespace_io *nsio;
1528 	struct device *dev, **devs;
1529 	struct resource *res;
1530 
1531 	nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1532 	if (!nsio)
1533 		return NULL;
1534 
1535 	devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1536 	if (!devs) {
1537 		kfree(nsio);
1538 		return NULL;
1539 	}
1540 
1541 	dev = &nsio->common.dev;
1542 	dev->type = &namespace_io_device_type;
1543 	dev->parent = &nd_region->dev;
1544 	res = &nsio->res;
1545 	res->name = dev_name(&nd_region->dev);
1546 	res->flags = IORESOURCE_MEM;
1547 	res->start = nd_region->ndr_start;
1548 	res->end = res->start + nd_region->ndr_size - 1;
1549 
1550 	devs[0] = dev;
1551 	return devs;
1552 }
1553 
1554 static bool has_uuid_at_pos(struct nd_region *nd_region, const uuid_t *uuid,
1555 			    u64 cookie, u16 pos)
1556 {
1557 	struct nd_namespace_label *found = NULL;
1558 	int i;
1559 
1560 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1561 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1562 		struct nd_interleave_set *nd_set = nd_region->nd_set;
1563 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1564 		struct nd_label_ent *label_ent;
1565 		bool found_uuid = false;
1566 
1567 		list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1568 			struct nd_namespace_label *nd_label = label_ent->label;
1569 			u16 position;
1570 
1571 			if (!nd_label)
1572 				continue;
1573 			position = nsl_get_position(ndd, nd_label);
1574 
1575 			if (!nsl_validate_isetcookie(ndd, nd_label, cookie))
1576 				continue;
1577 
1578 			if (!nsl_uuid_equal(ndd, nd_label, uuid))
1579 				continue;
1580 
1581 			if (!nsl_validate_type_guid(ndd, nd_label,
1582 						    &nd_set->type_guid))
1583 				continue;
1584 
1585 			if (found_uuid) {
1586 				dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1587 				return false;
1588 			}
1589 			found_uuid = true;
1590 			if (!nsl_validate_nlabel(nd_region, ndd, nd_label))
1591 				continue;
1592 			if (position != pos)
1593 				continue;
1594 			found = nd_label;
1595 			break;
1596 		}
1597 		if (found)
1598 			break;
1599 	}
1600 	return found != NULL;
1601 }
1602 
1603 static int select_pmem_id(struct nd_region *nd_region, const uuid_t *pmem_id)
1604 {
1605 	int i;
1606 
1607 	if (!pmem_id)
1608 		return -ENODEV;
1609 
1610 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1611 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1612 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1613 		struct nd_namespace_label *nd_label = NULL;
1614 		u64 hw_start, hw_end, pmem_start, pmem_end;
1615 		struct nd_label_ent *label_ent;
1616 
1617 		lockdep_assert_held(&nd_mapping->lock);
1618 		list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1619 			nd_label = label_ent->label;
1620 			if (!nd_label)
1621 				continue;
1622 			if (nsl_uuid_equal(ndd, nd_label, pmem_id))
1623 				break;
1624 			nd_label = NULL;
1625 		}
1626 
1627 		if (!nd_label) {
1628 			WARN_ON(1);
1629 			return -EINVAL;
1630 		}
1631 
1632 		/*
1633 		 * Check that this label is compliant with the dpa
1634 		 * range published in NFIT
1635 		 */
1636 		hw_start = nd_mapping->start;
1637 		hw_end = hw_start + nd_mapping->size;
1638 		pmem_start = nsl_get_dpa(ndd, nd_label);
1639 		pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label);
1640 		if (pmem_start >= hw_start && pmem_start < hw_end
1641 				&& pmem_end <= hw_end && pmem_end > hw_start)
1642 			/* pass */;
1643 		else {
1644 			dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1645 				dev_name(ndd->dev),
1646 				nsl_uuid_raw(ndd, nd_label));
1647 			return -EINVAL;
1648 		}
1649 
1650 		/* move recently validated label to the front of the list */
1651 		list_move(&label_ent->list, &nd_mapping->labels);
1652 	}
1653 	return 0;
1654 }
1655 
1656 /**
1657  * create_namespace_pmem - validate interleave set labelling, retrieve label0
1658  * @nd_region: region with mappings to validate
1659  * @nspm: target namespace to create
1660  * @nd_label: target pmem namespace label to evaluate
1661  */
1662 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1663 					    struct nd_mapping *nd_mapping,
1664 					    struct nd_namespace_label *nd_label)
1665 {
1666 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1667 	struct nd_namespace_index *nsindex =
1668 		to_namespace_index(ndd, ndd->ns_current);
1669 	u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1670 	u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1671 	struct nd_label_ent *label_ent;
1672 	struct nd_namespace_pmem *nspm;
1673 	resource_size_t size = 0;
1674 	struct resource *res;
1675 	struct device *dev;
1676 	uuid_t uuid;
1677 	int rc = 0;
1678 	u16 i;
1679 
1680 	if (cookie == 0) {
1681 		dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1682 		return ERR_PTR(-ENXIO);
1683 	}
1684 
1685 	if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) {
1686 		dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1687 			nsl_uuid_raw(ndd, nd_label));
1688 		if (!nsl_validate_isetcookie(ndd, nd_label, altcookie))
1689 			return ERR_PTR(-EAGAIN);
1690 
1691 		dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1692 			nsl_uuid_raw(ndd, nd_label));
1693 	}
1694 
1695 	nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1696 	if (!nspm)
1697 		return ERR_PTR(-ENOMEM);
1698 
1699 	nspm->id = -1;
1700 	dev = &nspm->nsio.common.dev;
1701 	dev->type = &namespace_pmem_device_type;
1702 	dev->parent = &nd_region->dev;
1703 	res = &nspm->nsio.res;
1704 	res->name = dev_name(&nd_region->dev);
1705 	res->flags = IORESOURCE_MEM;
1706 
1707 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1708 		nsl_get_uuid(ndd, nd_label, &uuid);
1709 		if (has_uuid_at_pos(nd_region, &uuid, cookie, i))
1710 			continue;
1711 		if (has_uuid_at_pos(nd_region, &uuid, altcookie, i))
1712 			continue;
1713 		break;
1714 	}
1715 
1716 	if (i < nd_region->ndr_mappings) {
1717 		struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1718 
1719 		/*
1720 		 * Give up if we don't find an instance of a uuid at each
1721 		 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1722 		 * find a dimm with two instances of the same uuid.
1723 		 */
1724 		dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1725 			nvdimm_name(nvdimm), nsl_uuid_raw(ndd, nd_label));
1726 		rc = -EINVAL;
1727 		goto err;
1728 	}
1729 
1730 	/*
1731 	 * Fix up each mapping's 'labels' to have the validated pmem label for
1732 	 * that position at labels[0], and NULL at labels[1].  In the process,
1733 	 * check that the namespace aligns with interleave-set.
1734 	 */
1735 	nsl_get_uuid(ndd, nd_label, &uuid);
1736 	rc = select_pmem_id(nd_region, &uuid);
1737 	if (rc)
1738 		goto err;
1739 
1740 	/* Calculate total size and populate namespace properties from label0 */
1741 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1742 		struct nd_namespace_label *label0;
1743 		struct nvdimm_drvdata *ndd;
1744 
1745 		nd_mapping = &nd_region->mapping[i];
1746 		label_ent = list_first_entry_or_null(&nd_mapping->labels,
1747 				typeof(*label_ent), list);
1748 		label0 = label_ent ? label_ent->label : NULL;
1749 
1750 		if (!label0) {
1751 			WARN_ON(1);
1752 			continue;
1753 		}
1754 
1755 		ndd = to_ndd(nd_mapping);
1756 		size += nsl_get_rawsize(ndd, label0);
1757 		if (nsl_get_position(ndd, label0) != 0)
1758 			continue;
1759 		WARN_ON(nspm->alt_name || nspm->uuid);
1760 		nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0),
1761 					 NSLABEL_NAME_LEN, GFP_KERNEL);
1762 		nsl_get_uuid(ndd, label0, &uuid);
1763 		nspm->uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
1764 		nspm->lbasize = nsl_get_lbasize(ndd, label0);
1765 		nspm->nsio.common.claim_class =
1766 			nsl_get_claim_class(ndd, label0);
1767 	}
1768 
1769 	if (!nspm->alt_name || !nspm->uuid) {
1770 		rc = -ENOMEM;
1771 		goto err;
1772 	}
1773 
1774 	nd_namespace_pmem_set_resource(nd_region, nspm, size);
1775 
1776 	return dev;
1777  err:
1778 	namespace_pmem_release(dev);
1779 	switch (rc) {
1780 	case -EINVAL:
1781 		dev_dbg(&nd_region->dev, "invalid label(s)\n");
1782 		break;
1783 	case -ENODEV:
1784 		dev_dbg(&nd_region->dev, "label not found\n");
1785 		break;
1786 	default:
1787 		dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
1788 		break;
1789 	}
1790 	return ERR_PTR(rc);
1791 }
1792 
1793 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
1794 {
1795 	struct nd_namespace_pmem *nspm;
1796 	struct resource *res;
1797 	struct device *dev;
1798 
1799 	if (!is_memory(&nd_region->dev))
1800 		return NULL;
1801 
1802 	nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1803 	if (!nspm)
1804 		return NULL;
1805 
1806 	dev = &nspm->nsio.common.dev;
1807 	dev->type = &namespace_pmem_device_type;
1808 	dev->parent = &nd_region->dev;
1809 	res = &nspm->nsio.res;
1810 	res->name = dev_name(&nd_region->dev);
1811 	res->flags = IORESOURCE_MEM;
1812 
1813 	nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1814 	if (nspm->id < 0) {
1815 		kfree(nspm);
1816 		return NULL;
1817 	}
1818 	dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
1819 	nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1820 
1821 	return dev;
1822 }
1823 
1824 static struct lock_class_key nvdimm_namespace_key;
1825 
1826 void nd_region_create_ns_seed(struct nd_region *nd_region)
1827 {
1828 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1829 
1830 	if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
1831 		return;
1832 
1833 	nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
1834 
1835 	/*
1836 	 * Seed creation failures are not fatal, provisioning is simply
1837 	 * disabled until memory becomes available
1838 	 */
1839 	if (!nd_region->ns_seed)
1840 		dev_err(&nd_region->dev, "failed to create namespace\n");
1841 	else {
1842 		device_initialize(nd_region->ns_seed);
1843 		lockdep_set_class(&nd_region->ns_seed->mutex,
1844 				  &nvdimm_namespace_key);
1845 		nd_device_register(nd_region->ns_seed);
1846 	}
1847 }
1848 
1849 void nd_region_create_dax_seed(struct nd_region *nd_region)
1850 {
1851 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1852 	nd_region->dax_seed = nd_dax_create(nd_region);
1853 	/*
1854 	 * Seed creation failures are not fatal, provisioning is simply
1855 	 * disabled until memory becomes available
1856 	 */
1857 	if (!nd_region->dax_seed)
1858 		dev_err(&nd_region->dev, "failed to create dax namespace\n");
1859 }
1860 
1861 void nd_region_create_pfn_seed(struct nd_region *nd_region)
1862 {
1863 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1864 	nd_region->pfn_seed = nd_pfn_create(nd_region);
1865 	/*
1866 	 * Seed creation failures are not fatal, provisioning is simply
1867 	 * disabled until memory becomes available
1868 	 */
1869 	if (!nd_region->pfn_seed)
1870 		dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1871 }
1872 
1873 void nd_region_create_btt_seed(struct nd_region *nd_region)
1874 {
1875 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1876 	nd_region->btt_seed = nd_btt_create(nd_region);
1877 	/*
1878 	 * Seed creation failures are not fatal, provisioning is simply
1879 	 * disabled until memory becomes available
1880 	 */
1881 	if (!nd_region->btt_seed)
1882 		dev_err(&nd_region->dev, "failed to create btt namespace\n");
1883 }
1884 
1885 static int add_namespace_resource(struct nd_region *nd_region,
1886 		struct nd_namespace_label *nd_label, struct device **devs,
1887 		int count)
1888 {
1889 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1890 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1891 	int i;
1892 
1893 	for (i = 0; i < count; i++) {
1894 		uuid_t *uuid = namespace_to_uuid(devs[i]);
1895 
1896 		if (IS_ERR(uuid)) {
1897 			WARN_ON(1);
1898 			continue;
1899 		}
1900 
1901 		if (!nsl_uuid_equal(ndd, nd_label, uuid))
1902 			continue;
1903 		dev_err(&nd_region->dev,
1904 			"error: conflicting extents for uuid: %pUb\n", uuid);
1905 		return -ENXIO;
1906 	}
1907 
1908 	return i;
1909 }
1910 
1911 static int cmp_dpa(const void *a, const void *b)
1912 {
1913 	const struct device *dev_a = *(const struct device **) a;
1914 	const struct device *dev_b = *(const struct device **) b;
1915 	struct nd_namespace_pmem *nspm_a, *nspm_b;
1916 
1917 	if (is_namespace_io(dev_a))
1918 		return 0;
1919 
1920 	nspm_a = to_nd_namespace_pmem(dev_a);
1921 	nspm_b = to_nd_namespace_pmem(dev_b);
1922 
1923 	return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
1924 			sizeof(resource_size_t));
1925 }
1926 
1927 static struct device **scan_labels(struct nd_region *nd_region)
1928 {
1929 	int i, count = 0;
1930 	struct device *dev, **devs = NULL;
1931 	struct nd_label_ent *label_ent, *e;
1932 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1933 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1934 	resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
1935 
1936 	/* "safe" because create_namespace_pmem() might list_move() label_ent */
1937 	list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1938 		struct nd_namespace_label *nd_label = label_ent->label;
1939 		struct device **__devs;
1940 
1941 		if (!nd_label)
1942 			continue;
1943 
1944 		/* skip labels that describe extents outside of the region */
1945 		if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start ||
1946 		    nsl_get_dpa(ndd, nd_label) > map_end)
1947 			continue;
1948 
1949 		i = add_namespace_resource(nd_region, nd_label, devs, count);
1950 		if (i < 0)
1951 			goto err;
1952 		if (i < count)
1953 			continue;
1954 		__devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1955 		if (!__devs)
1956 			goto err;
1957 		memcpy(__devs, devs, sizeof(dev) * count);
1958 		kfree(devs);
1959 		devs = __devs;
1960 
1961 		dev = create_namespace_pmem(nd_region, nd_mapping, nd_label);
1962 		if (IS_ERR(dev)) {
1963 			switch (PTR_ERR(dev)) {
1964 			case -EAGAIN:
1965 				/* skip invalid labels */
1966 				continue;
1967 			case -ENODEV:
1968 				/* fallthrough to seed creation */
1969 				break;
1970 			default:
1971 				goto err;
1972 			}
1973 		} else
1974 			devs[count++] = dev;
1975 
1976 	}
1977 
1978 	dev_dbg(&nd_region->dev, "discovered %d namespace%s\n", count,
1979 		count == 1 ? "" : "s");
1980 
1981 	if (count == 0) {
1982 		struct nd_namespace_pmem *nspm;
1983 
1984 		/* Publish a zero-sized namespace for userspace to configure. */
1985 		nd_mapping_free_labels(nd_mapping);
1986 
1987 		devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1988 		if (!devs)
1989 			goto err;
1990 
1991 		nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1992 		if (!nspm)
1993 			goto err;
1994 		dev = &nspm->nsio.common.dev;
1995 		dev->type = &namespace_pmem_device_type;
1996 		nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1997 		dev->parent = &nd_region->dev;
1998 		devs[count++] = dev;
1999 	} else if (is_memory(&nd_region->dev)) {
2000 		/* clean unselected labels */
2001 		for (i = 0; i < nd_region->ndr_mappings; i++) {
2002 			struct list_head *l, *e;
2003 			LIST_HEAD(list);
2004 			int j;
2005 
2006 			nd_mapping = &nd_region->mapping[i];
2007 			if (list_empty(&nd_mapping->labels)) {
2008 				WARN_ON(1);
2009 				continue;
2010 			}
2011 
2012 			j = count;
2013 			list_for_each_safe(l, e, &nd_mapping->labels) {
2014 				if (!j--)
2015 					break;
2016 				list_move_tail(l, &list);
2017 			}
2018 			nd_mapping_free_labels(nd_mapping);
2019 			list_splice_init(&list, &nd_mapping->labels);
2020 		}
2021 	}
2022 
2023 	if (count > 1)
2024 		sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2025 
2026 	return devs;
2027 
2028  err:
2029 	if (devs) {
2030 		for (i = 0; devs[i]; i++)
2031 			namespace_pmem_release(devs[i]);
2032 		kfree(devs);
2033 	}
2034 	return NULL;
2035 }
2036 
2037 static struct device **create_namespaces(struct nd_region *nd_region)
2038 {
2039 	struct nd_mapping *nd_mapping;
2040 	struct device **devs;
2041 	int i;
2042 
2043 	if (nd_region->ndr_mappings == 0)
2044 		return NULL;
2045 
2046 	/* lock down all mappings while we scan labels */
2047 	for (i = 0; i < nd_region->ndr_mappings; i++) {
2048 		nd_mapping = &nd_region->mapping[i];
2049 		mutex_lock_nested(&nd_mapping->lock, i);
2050 	}
2051 
2052 	devs = scan_labels(nd_region);
2053 
2054 	for (i = 0; i < nd_region->ndr_mappings; i++) {
2055 		int reverse = nd_region->ndr_mappings - 1 - i;
2056 
2057 		nd_mapping = &nd_region->mapping[reverse];
2058 		mutex_unlock(&nd_mapping->lock);
2059 	}
2060 
2061 	return devs;
2062 }
2063 
2064 static void deactivate_labels(void *region)
2065 {
2066 	struct nd_region *nd_region = region;
2067 	int i;
2068 
2069 	for (i = 0; i < nd_region->ndr_mappings; i++) {
2070 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2071 		struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2072 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
2073 
2074 		mutex_lock(&nd_mapping->lock);
2075 		nd_mapping_free_labels(nd_mapping);
2076 		mutex_unlock(&nd_mapping->lock);
2077 
2078 		put_ndd(ndd);
2079 		nd_mapping->ndd = NULL;
2080 		if (ndd)
2081 			atomic_dec(&nvdimm->busy);
2082 	}
2083 }
2084 
2085 static int init_active_labels(struct nd_region *nd_region)
2086 {
2087 	int i, rc = 0;
2088 
2089 	for (i = 0; i < nd_region->ndr_mappings; i++) {
2090 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2091 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2092 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
2093 		struct nd_label_ent *label_ent;
2094 		int count, j;
2095 
2096 		/*
2097 		 * If the dimm is disabled then we may need to prevent
2098 		 * the region from being activated.
2099 		 */
2100 		if (!ndd) {
2101 			if (test_bit(NDD_LOCKED, &nvdimm->flags))
2102 				/* fail, label data may be unreadable */;
2103 			else if (test_bit(NDD_LABELING, &nvdimm->flags))
2104 				/* fail, labels needed to disambiguate dpa */;
2105 			else
2106 				continue;
2107 
2108 			dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2109 					dev_name(&nd_mapping->nvdimm->dev),
2110 					test_bit(NDD_LOCKED, &nvdimm->flags)
2111 					? "locked" : "disabled");
2112 			rc = -ENXIO;
2113 			goto out;
2114 		}
2115 		nd_mapping->ndd = ndd;
2116 		atomic_inc(&nvdimm->busy);
2117 		get_ndd(ndd);
2118 
2119 		count = nd_label_active_count(ndd);
2120 		dev_dbg(ndd->dev, "count: %d\n", count);
2121 		if (!count)
2122 			continue;
2123 		for (j = 0; j < count; j++) {
2124 			struct nd_namespace_label *label;
2125 
2126 			label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2127 			if (!label_ent)
2128 				break;
2129 			label = nd_label_active(ndd, j);
2130 			label_ent->label = label;
2131 
2132 			mutex_lock(&nd_mapping->lock);
2133 			list_add_tail(&label_ent->list, &nd_mapping->labels);
2134 			mutex_unlock(&nd_mapping->lock);
2135 		}
2136 
2137 		if (j < count)
2138 			break;
2139 	}
2140 
2141 	if (i < nd_region->ndr_mappings)
2142 		rc = -ENOMEM;
2143 
2144 out:
2145 	if (rc) {
2146 		deactivate_labels(nd_region);
2147 		return rc;
2148 	}
2149 
2150 	return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
2151 					nd_region);
2152 }
2153 
2154 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2155 {
2156 	struct device **devs = NULL;
2157 	int i, rc = 0, type;
2158 
2159 	*err = 0;
2160 	nvdimm_bus_lock(&nd_region->dev);
2161 	rc = init_active_labels(nd_region);
2162 	if (rc) {
2163 		nvdimm_bus_unlock(&nd_region->dev);
2164 		return rc;
2165 	}
2166 
2167 	type = nd_region_to_nstype(nd_region);
2168 	switch (type) {
2169 	case ND_DEVICE_NAMESPACE_IO:
2170 		devs = create_namespace_io(nd_region);
2171 		break;
2172 	case ND_DEVICE_NAMESPACE_PMEM:
2173 		devs = create_namespaces(nd_region);
2174 		break;
2175 	default:
2176 		break;
2177 	}
2178 	nvdimm_bus_unlock(&nd_region->dev);
2179 
2180 	if (!devs)
2181 		return -ENODEV;
2182 
2183 	for (i = 0; devs[i]; i++) {
2184 		struct device *dev = devs[i];
2185 		int id;
2186 
2187 		if (type == ND_DEVICE_NAMESPACE_PMEM) {
2188 			struct nd_namespace_pmem *nspm;
2189 
2190 			nspm = to_nd_namespace_pmem(dev);
2191 			id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2192 					    GFP_KERNEL);
2193 			nspm->id = id;
2194 		} else
2195 			id = i;
2196 
2197 		if (id < 0)
2198 			break;
2199 		dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2200 		device_initialize(dev);
2201 		lockdep_set_class(&dev->mutex, &nvdimm_namespace_key);
2202 		nd_device_register(dev);
2203 	}
2204 	if (i)
2205 		nd_region->ns_seed = devs[0];
2206 
2207 	if (devs[i]) {
2208 		int j;
2209 
2210 		for (j = i; devs[j]; j++) {
2211 			struct device *dev = devs[j];
2212 
2213 			device_initialize(dev);
2214 			put_device(dev);
2215 		}
2216 		*err = j - i;
2217 		/*
2218 		 * All of the namespaces we tried to register failed, so
2219 		 * fail region activation.
2220 		 */
2221 		if (*err == 0)
2222 			rc = -ENODEV;
2223 	}
2224 	kfree(devs);
2225 
2226 	if (rc == -ENODEV)
2227 		return rc;
2228 
2229 	return i;
2230 }
2231