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