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