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