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