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