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