GNU Linux-libre 4.19.286-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(%zd)\n", 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 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 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, "not associated with dimm(s)\n");
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_allocatable_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, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
1113
1114         nvdimm_bus_unlock(dev);
1115         device_unlock(dev);
1116
1117         return rc < 0 ? rc : len;
1118 }
1119
1120 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1121 {
1122         struct device *dev = &ndns->dev;
1123
1124         if (is_namespace_pmem(dev)) {
1125                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1126
1127                 return resource_size(&nspm->nsio.res);
1128         } else if (is_namespace_blk(dev)) {
1129                 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
1130         } else if (is_namespace_io(dev)) {
1131                 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1132
1133                 return resource_size(&nsio->res);
1134         } else
1135                 WARN_ONCE(1, "unknown namespace type\n");
1136         return 0;
1137 }
1138
1139 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1140 {
1141         resource_size_t size;
1142
1143         nvdimm_bus_lock(&ndns->dev);
1144         size = __nvdimm_namespace_capacity(ndns);
1145         nvdimm_bus_unlock(&ndns->dev);
1146
1147         return size;
1148 }
1149 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1150
1151 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1152 {
1153         int i;
1154         bool locked = false;
1155         struct device *dev = &ndns->dev;
1156         struct nd_region *nd_region = to_nd_region(dev->parent);
1157
1158         for (i = 0; i < nd_region->ndr_mappings; i++) {
1159                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1160                 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1161
1162                 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1163                         dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1164                         locked = true;
1165                 }
1166         }
1167         return locked;
1168 }
1169 EXPORT_SYMBOL(nvdimm_namespace_locked);
1170
1171 static ssize_t size_show(struct device *dev,
1172                 struct device_attribute *attr, char *buf)
1173 {
1174         return sprintf(buf, "%llu\n", (unsigned long long)
1175                         nvdimm_namespace_capacity(to_ndns(dev)));
1176 }
1177 static DEVICE_ATTR(size, 0444, size_show, size_store);
1178
1179 static u8 *namespace_to_uuid(struct device *dev)
1180 {
1181         if (is_namespace_pmem(dev)) {
1182                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1183
1184                 return nspm->uuid;
1185         } else if (is_namespace_blk(dev)) {
1186                 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1187
1188                 return nsblk->uuid;
1189         } else
1190                 return ERR_PTR(-ENXIO);
1191 }
1192
1193 static ssize_t uuid_show(struct device *dev,
1194                 struct device_attribute *attr, char *buf)
1195 {
1196         u8 *uuid = namespace_to_uuid(dev);
1197
1198         if (IS_ERR(uuid))
1199                 return PTR_ERR(uuid);
1200         if (uuid)
1201                 return sprintf(buf, "%pUb\n", uuid);
1202         return sprintf(buf, "\n");
1203 }
1204
1205 /**
1206  * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1207  * @nd_region: parent region so we can updates all dimms in the set
1208  * @dev: namespace type for generating label_id
1209  * @new_uuid: incoming uuid
1210  * @old_uuid: reference to the uuid storage location in the namespace object
1211  */
1212 static int namespace_update_uuid(struct nd_region *nd_region,
1213                 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1214 {
1215         u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1216         struct nd_label_id old_label_id;
1217         struct nd_label_id new_label_id;
1218         int i;
1219
1220         if (!nd_is_uuid_unique(dev, new_uuid))
1221                 return -EINVAL;
1222
1223         if (*old_uuid == NULL)
1224                 goto out;
1225
1226         /*
1227          * If we've already written a label with this uuid, then it's
1228          * too late to rename because we can't reliably update the uuid
1229          * without losing the old namespace.  Userspace must delete this
1230          * namespace to abandon the old uuid.
1231          */
1232         for (i = 0; i < nd_region->ndr_mappings; i++) {
1233                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1234
1235                 /*
1236                  * This check by itself is sufficient because old_uuid
1237                  * would be NULL above if this uuid did not exist in the
1238                  * currently written set.
1239                  *
1240                  * FIXME: can we delete uuid with zero dpa allocated?
1241                  */
1242                 if (list_empty(&nd_mapping->labels))
1243                         return -EBUSY;
1244         }
1245
1246         nd_label_gen_id(&old_label_id, *old_uuid, flags);
1247         nd_label_gen_id(&new_label_id, new_uuid, flags);
1248         for (i = 0; i < nd_region->ndr_mappings; i++) {
1249                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1250                 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1251                 struct nd_label_ent *label_ent;
1252                 struct resource *res;
1253
1254                 for_each_dpa_resource(ndd, res)
1255                         if (strcmp(res->name, old_label_id.id) == 0)
1256                                 sprintf((void *) res->name, "%s",
1257                                                 new_label_id.id);
1258
1259                 mutex_lock(&nd_mapping->lock);
1260                 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1261                         struct nd_namespace_label *nd_label = label_ent->label;
1262                         struct nd_label_id label_id;
1263
1264                         if (!nd_label)
1265                                 continue;
1266                         nd_label_gen_id(&label_id, nd_label->uuid,
1267                                         __le32_to_cpu(nd_label->flags));
1268                         if (strcmp(old_label_id.id, label_id.id) == 0)
1269                                 set_bit(ND_LABEL_REAP, &label_ent->flags);
1270                 }
1271                 mutex_unlock(&nd_mapping->lock);
1272         }
1273         kfree(*old_uuid);
1274  out:
1275         *old_uuid = new_uuid;
1276         return 0;
1277 }
1278
1279 static ssize_t uuid_store(struct device *dev,
1280                 struct device_attribute *attr, const char *buf, size_t len)
1281 {
1282         struct nd_region *nd_region = to_nd_region(dev->parent);
1283         u8 *uuid = NULL;
1284         ssize_t rc = 0;
1285         u8 **ns_uuid;
1286
1287         if (is_namespace_pmem(dev)) {
1288                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1289
1290                 ns_uuid = &nspm->uuid;
1291         } else if (is_namespace_blk(dev)) {
1292                 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1293
1294                 ns_uuid = &nsblk->uuid;
1295         } else
1296                 return -ENXIO;
1297
1298         device_lock(dev);
1299         nvdimm_bus_lock(dev);
1300         wait_nvdimm_bus_probe_idle(dev);
1301         if (to_ndns(dev)->claim)
1302                 rc = -EBUSY;
1303         if (rc >= 0)
1304                 rc = nd_uuid_store(dev, &uuid, buf, len);
1305         if (rc >= 0)
1306                 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
1307         if (rc >= 0)
1308                 rc = nd_namespace_label_update(nd_region, dev);
1309         else
1310                 kfree(uuid);
1311         dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1312                         buf[len - 1] == '\n' ? "" : "\n");
1313         nvdimm_bus_unlock(dev);
1314         device_unlock(dev);
1315
1316         return rc < 0 ? rc : len;
1317 }
1318 static DEVICE_ATTR_RW(uuid);
1319
1320 static ssize_t resource_show(struct device *dev,
1321                 struct device_attribute *attr, char *buf)
1322 {
1323         struct resource *res;
1324
1325         if (is_namespace_pmem(dev)) {
1326                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1327
1328                 res = &nspm->nsio.res;
1329         } else if (is_namespace_io(dev)) {
1330                 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1331
1332                 res = &nsio->res;
1333         } else
1334                 return -ENXIO;
1335
1336         /* no address to convey if the namespace has no allocation */
1337         if (resource_size(res) == 0)
1338                 return -ENXIO;
1339         return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1340 }
1341 static DEVICE_ATTR_RO(resource);
1342
1343 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
1344         4096, 4104, 4160, 4224, 0 };
1345
1346 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1347
1348 static ssize_t sector_size_show(struct device *dev,
1349                 struct device_attribute *attr, char *buf)
1350 {
1351         if (is_namespace_blk(dev)) {
1352                 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1353
1354                 return nd_size_select_show(nsblk->lbasize,
1355                                 blk_lbasize_supported, buf);
1356         }
1357
1358         if (is_namespace_pmem(dev)) {
1359                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1360
1361                 return nd_size_select_show(nspm->lbasize,
1362                                 pmem_lbasize_supported, buf);
1363         }
1364         return -ENXIO;
1365 }
1366
1367 static ssize_t sector_size_store(struct device *dev,
1368                 struct device_attribute *attr, const char *buf, size_t len)
1369 {
1370         struct nd_region *nd_region = to_nd_region(dev->parent);
1371         const unsigned long *supported;
1372         unsigned long *lbasize;
1373         ssize_t rc = 0;
1374
1375         if (is_namespace_blk(dev)) {
1376                 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1377
1378                 lbasize = &nsblk->lbasize;
1379                 supported = blk_lbasize_supported;
1380         } else if (is_namespace_pmem(dev)) {
1381                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1382
1383                 lbasize = &nspm->lbasize;
1384                 supported = pmem_lbasize_supported;
1385         } else
1386                 return -ENXIO;
1387
1388         device_lock(dev);
1389         nvdimm_bus_lock(dev);
1390         if (to_ndns(dev)->claim)
1391                 rc = -EBUSY;
1392         if (rc >= 0)
1393                 rc = nd_size_select_store(dev, buf, lbasize, supported);
1394         if (rc >= 0)
1395                 rc = nd_namespace_label_update(nd_region, dev);
1396         dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1397                         buf, buf[len - 1] == '\n' ? "" : "\n");
1398         nvdimm_bus_unlock(dev);
1399         device_unlock(dev);
1400
1401         return rc ? rc : len;
1402 }
1403 static DEVICE_ATTR_RW(sector_size);
1404
1405 static ssize_t dpa_extents_show(struct device *dev,
1406                 struct device_attribute *attr, char *buf)
1407 {
1408         struct nd_region *nd_region = to_nd_region(dev->parent);
1409         struct nd_label_id label_id;
1410         int count = 0, i;
1411         u8 *uuid = NULL;
1412         u32 flags = 0;
1413
1414         nvdimm_bus_lock(dev);
1415         if (is_namespace_pmem(dev)) {
1416                 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1417
1418                 uuid = nspm->uuid;
1419                 flags = 0;
1420         } else if (is_namespace_blk(dev)) {
1421                 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1422
1423                 uuid = nsblk->uuid;
1424                 flags = NSLABEL_FLAG_LOCAL;
1425         }
1426
1427         if (!uuid)
1428                 goto out;
1429
1430         nd_label_gen_id(&label_id, uuid, flags);
1431         for (i = 0; i < nd_region->ndr_mappings; i++) {
1432                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1433                 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1434                 struct resource *res;
1435
1436                 for_each_dpa_resource(ndd, res)
1437                         if (strcmp(res->name, label_id.id) == 0)
1438                                 count++;
1439         }
1440  out:
1441         nvdimm_bus_unlock(dev);
1442
1443         return sprintf(buf, "%d\n", count);
1444 }
1445 static DEVICE_ATTR_RO(dpa_extents);
1446
1447 static int btt_claim_class(struct device *dev)
1448 {
1449         struct nd_region *nd_region = to_nd_region(dev->parent);
1450         int i, loop_bitmask = 0;
1451
1452         for (i = 0; i < nd_region->ndr_mappings; i++) {
1453                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1454                 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1455                 struct nd_namespace_index *nsindex;
1456
1457                 /*
1458                  * If any of the DIMMs do not support labels the only
1459                  * possible BTT format is v1.
1460                  */
1461                 if (!ndd) {
1462                         loop_bitmask = 0;
1463                         break;
1464                 }
1465
1466                 nsindex = to_namespace_index(ndd, ndd->ns_current);
1467                 if (nsindex == NULL)
1468                         loop_bitmask |= 1;
1469                 else {
1470                         /* check whether existing labels are v1.1 or v1.2 */
1471                         if (__le16_to_cpu(nsindex->major) == 1
1472                                         && __le16_to_cpu(nsindex->minor) == 1)
1473                                 loop_bitmask |= 2;
1474                         else
1475                                 loop_bitmask |= 4;
1476                 }
1477         }
1478         /*
1479          * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1480          * block is found, a v1.1 label for any mapping will set bit 1, and a
1481          * v1.2 label will set bit 2.
1482          *
1483          * At the end of the loop, at most one of the three bits must be set.
1484          * If multiple bits were set, it means the different mappings disagree
1485          * about their labels, and this must be cleaned up first.
1486          *
1487          * If all the label index blocks are found to agree, nsindex of NULL
1488          * implies labels haven't been initialized yet, and when they will,
1489          * they will be of the 1.2 format, so we can assume BTT2.0
1490          *
1491          * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1492          * found, we enforce BTT2.0
1493          *
1494          * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1495          */
1496         switch (loop_bitmask) {
1497         case 0:
1498         case 2:
1499                 return NVDIMM_CCLASS_BTT;
1500         case 1:
1501         case 4:
1502                 return NVDIMM_CCLASS_BTT2;
1503         default:
1504                 return -ENXIO;
1505         }
1506 }
1507
1508 static ssize_t holder_show(struct device *dev,
1509                 struct device_attribute *attr, char *buf)
1510 {
1511         struct nd_namespace_common *ndns = to_ndns(dev);
1512         ssize_t rc;
1513
1514         device_lock(dev);
1515         rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1516         device_unlock(dev);
1517
1518         return rc;
1519 }
1520 static DEVICE_ATTR_RO(holder);
1521
1522 static ssize_t __holder_class_store(struct device *dev, const char *buf)
1523 {
1524         struct nd_namespace_common *ndns = to_ndns(dev);
1525
1526         if (dev->driver || ndns->claim)
1527                 return -EBUSY;
1528
1529         if (strcmp(buf, "btt") == 0 || strcmp(buf, "btt\n") == 0)
1530                 ndns->claim_class = btt_claim_class(dev);
1531         else if (strcmp(buf, "pfn") == 0 || strcmp(buf, "pfn\n") == 0)
1532                 ndns->claim_class = NVDIMM_CCLASS_PFN;
1533         else if (strcmp(buf, "dax") == 0 || strcmp(buf, "dax\n") == 0)
1534                 ndns->claim_class = NVDIMM_CCLASS_DAX;
1535         else if (strcmp(buf, "") == 0 || strcmp(buf, "\n") == 0)
1536                 ndns->claim_class = NVDIMM_CCLASS_NONE;
1537         else
1538                 return -EINVAL;
1539
1540         /* btt_claim_class() could've returned an error */
1541         if (ndns->claim_class < 0)
1542                 return ndns->claim_class;
1543
1544         return 0;
1545 }
1546
1547 static ssize_t holder_class_store(struct device *dev,
1548                 struct device_attribute *attr, const char *buf, size_t len)
1549 {
1550         struct nd_region *nd_region = to_nd_region(dev->parent);
1551         ssize_t rc;
1552
1553         device_lock(dev);
1554         nvdimm_bus_lock(dev);
1555         wait_nvdimm_bus_probe_idle(dev);
1556         rc = __holder_class_store(dev, buf);
1557         if (rc >= 0)
1558                 rc = nd_namespace_label_update(nd_region, dev);
1559         dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
1560         nvdimm_bus_unlock(dev);
1561         device_unlock(dev);
1562
1563         return rc < 0 ? rc : len;
1564 }
1565
1566 static ssize_t holder_class_show(struct device *dev,
1567                 struct device_attribute *attr, char *buf)
1568 {
1569         struct nd_namespace_common *ndns = to_ndns(dev);
1570         ssize_t rc;
1571
1572         device_lock(dev);
1573         if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1574                 rc = sprintf(buf, "\n");
1575         else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1576                         (ndns->claim_class == NVDIMM_CCLASS_BTT2))
1577                 rc = sprintf(buf, "btt\n");
1578         else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1579                 rc = sprintf(buf, "pfn\n");
1580         else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1581                 rc = sprintf(buf, "dax\n");
1582         else
1583                 rc = sprintf(buf, "<unknown>\n");
1584         device_unlock(dev);
1585
1586         return rc;
1587 }
1588 static DEVICE_ATTR_RW(holder_class);
1589
1590 static ssize_t mode_show(struct device *dev,
1591                 struct device_attribute *attr, char *buf)
1592 {
1593         struct nd_namespace_common *ndns = to_ndns(dev);
1594         struct device *claim;
1595         char *mode;
1596         ssize_t rc;
1597
1598         device_lock(dev);
1599         claim = ndns->claim;
1600         if (claim && is_nd_btt(claim))
1601                 mode = "safe";
1602         else if (claim && is_nd_pfn(claim))
1603                 mode = "memory";
1604         else if (claim && is_nd_dax(claim))
1605                 mode = "dax";
1606         else if (!claim && pmem_should_map_pages(dev))
1607                 mode = "memory";
1608         else
1609                 mode = "raw";
1610         rc = sprintf(buf, "%s\n", mode);
1611         device_unlock(dev);
1612
1613         return rc;
1614 }
1615 static DEVICE_ATTR_RO(mode);
1616
1617 static ssize_t force_raw_store(struct device *dev,
1618                 struct device_attribute *attr, const char *buf, size_t len)
1619 {
1620         bool force_raw;
1621         int rc = strtobool(buf, &force_raw);
1622
1623         if (rc)
1624                 return rc;
1625
1626         to_ndns(dev)->force_raw = force_raw;
1627         return len;
1628 }
1629
1630 static ssize_t force_raw_show(struct device *dev,
1631                 struct device_attribute *attr, char *buf)
1632 {
1633         return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1634 }
1635 static DEVICE_ATTR_RW(force_raw);
1636
1637 static struct attribute *nd_namespace_attributes[] = {
1638         &dev_attr_nstype.attr,
1639         &dev_attr_size.attr,
1640         &dev_attr_mode.attr,
1641         &dev_attr_uuid.attr,
1642         &dev_attr_holder.attr,
1643         &dev_attr_resource.attr,
1644         &dev_attr_alt_name.attr,
1645         &dev_attr_force_raw.attr,
1646         &dev_attr_sector_size.attr,
1647         &dev_attr_dpa_extents.attr,
1648         &dev_attr_holder_class.attr,
1649         NULL,
1650 };
1651
1652 static umode_t namespace_visible(struct kobject *kobj,
1653                 struct attribute *a, int n)
1654 {
1655         struct device *dev = container_of(kobj, struct device, kobj);
1656
1657         if (a == &dev_attr_resource.attr) {
1658                 if (is_namespace_blk(dev))
1659                         return 0;
1660                 return 0400;
1661         }
1662
1663         if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1664                 if (a == &dev_attr_size.attr)
1665                         return 0644;
1666
1667                 return a->mode;
1668         }
1669
1670         if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1671                         || a == &dev_attr_holder.attr
1672                         || a == &dev_attr_holder_class.attr
1673                         || a == &dev_attr_force_raw.attr
1674                         || a == &dev_attr_mode.attr)
1675                 return a->mode;
1676
1677         return 0;
1678 }
1679
1680 static struct attribute_group nd_namespace_attribute_group = {
1681         .attrs = nd_namespace_attributes,
1682         .is_visible = namespace_visible,
1683 };
1684
1685 static const struct attribute_group *nd_namespace_attribute_groups[] = {
1686         &nd_device_attribute_group,
1687         &nd_namespace_attribute_group,
1688         &nd_numa_attribute_group,
1689         NULL,
1690 };
1691
1692 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1693 {
1694         struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1695         struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
1696         struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
1697         struct nd_namespace_common *ndns = NULL;
1698         resource_size_t size;
1699
1700         if (nd_btt || nd_pfn || nd_dax) {
1701                 if (nd_btt)
1702                         ndns = nd_btt->ndns;
1703                 else if (nd_pfn)
1704                         ndns = nd_pfn->ndns;
1705                 else if (nd_dax)
1706                         ndns = nd_dax->nd_pfn.ndns;
1707
1708                 if (!ndns)
1709                         return ERR_PTR(-ENODEV);
1710
1711                 /*
1712                  * Flush any in-progess probes / removals in the driver
1713                  * for the raw personality of this namespace.
1714                  */
1715                 device_lock(&ndns->dev);
1716                 device_unlock(&ndns->dev);
1717                 if (ndns->dev.driver) {
1718                         dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1719                                         dev_name(dev));
1720                         return ERR_PTR(-EBUSY);
1721                 }
1722                 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
1723                                         "host (%s) vs claim (%s) mismatch\n",
1724                                         dev_name(dev),
1725                                         dev_name(ndns->claim)))
1726                         return ERR_PTR(-ENXIO);
1727         } else {
1728                 ndns = to_ndns(dev);
1729                 if (ndns->claim) {
1730                         dev_dbg(dev, "claimed by %s, failing probe\n",
1731                                 dev_name(ndns->claim));
1732
1733                         return ERR_PTR(-ENXIO);
1734                 }
1735         }
1736
1737         if (nvdimm_namespace_locked(ndns))
1738                 return ERR_PTR(-EACCES);
1739
1740         size = nvdimm_namespace_capacity(ndns);
1741         if (size < ND_MIN_NAMESPACE_SIZE) {
1742                 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1743                                 &size, ND_MIN_NAMESPACE_SIZE);
1744                 return ERR_PTR(-ENODEV);
1745         }
1746
1747         if (is_namespace_pmem(&ndns->dev)) {
1748                 struct nd_namespace_pmem *nspm;
1749
1750                 nspm = to_nd_namespace_pmem(&ndns->dev);
1751                 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
1752                         return ERR_PTR(-ENODEV);
1753         } else if (is_namespace_blk(&ndns->dev)) {
1754                 struct nd_namespace_blk *nsblk;
1755
1756                 nsblk = to_nd_namespace_blk(&ndns->dev);
1757                 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1758                         return ERR_PTR(-ENODEV);
1759                 if (!nsblk->lbasize) {
1760                         dev_dbg(&ndns->dev, "sector size not set\n");
1761                         return ERR_PTR(-ENODEV);
1762                 }
1763                 if (!nd_namespace_blk_validate(nsblk))
1764                         return ERR_PTR(-ENODEV);
1765         }
1766
1767         return ndns;
1768 }
1769 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1770
1771 static struct device **create_namespace_io(struct nd_region *nd_region)
1772 {
1773         struct nd_namespace_io *nsio;
1774         struct device *dev, **devs;
1775         struct resource *res;
1776
1777         nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1778         if (!nsio)
1779                 return NULL;
1780
1781         devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1782         if (!devs) {
1783                 kfree(nsio);
1784                 return NULL;
1785         }
1786
1787         dev = &nsio->common.dev;
1788         dev->type = &namespace_io_device_type;
1789         dev->parent = &nd_region->dev;
1790         res = &nsio->res;
1791         res->name = dev_name(&nd_region->dev);
1792         res->flags = IORESOURCE_MEM;
1793         res->start = nd_region->ndr_start;
1794         res->end = res->start + nd_region->ndr_size - 1;
1795
1796         devs[0] = dev;
1797         return devs;
1798 }
1799
1800 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1801                 u64 cookie, u16 pos)
1802 {
1803         struct nd_namespace_label *found = NULL;
1804         int i;
1805
1806         for (i = 0; i < nd_region->ndr_mappings; i++) {
1807                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1808                 struct nd_interleave_set *nd_set = nd_region->nd_set;
1809                 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1810                 struct nd_label_ent *label_ent;
1811                 bool found_uuid = false;
1812
1813                 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1814                         struct nd_namespace_label *nd_label = label_ent->label;
1815                         u16 position, nlabel;
1816                         u64 isetcookie;
1817
1818                         if (!nd_label)
1819                                 continue;
1820                         isetcookie = __le64_to_cpu(nd_label->isetcookie);
1821                         position = __le16_to_cpu(nd_label->position);
1822                         nlabel = __le16_to_cpu(nd_label->nlabel);
1823
1824                         if (isetcookie != cookie)
1825                                 continue;
1826
1827                         if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1828                                 continue;
1829
1830                         if (namespace_label_has(ndd, type_guid)
1831                                         && !guid_equal(&nd_set->type_guid,
1832                                                 &nd_label->type_guid)) {
1833                                 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1834                                                 nd_set->type_guid.b,
1835                                                 nd_label->type_guid.b);
1836                                 continue;
1837                         }
1838
1839                         if (found_uuid) {
1840                                 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1841                                 return false;
1842                         }
1843                         found_uuid = true;
1844                         if (nlabel != nd_region->ndr_mappings)
1845                                 continue;
1846                         if (position != pos)
1847                                 continue;
1848                         found = nd_label;
1849                         break;
1850                 }
1851                 if (found)
1852                         break;
1853         }
1854         return found != NULL;
1855 }
1856
1857 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1858 {
1859         int i;
1860
1861         if (!pmem_id)
1862                 return -ENODEV;
1863
1864         for (i = 0; i < nd_region->ndr_mappings; i++) {
1865                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1866                 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1867                 struct nd_namespace_label *nd_label = NULL;
1868                 u64 hw_start, hw_end, pmem_start, pmem_end;
1869                 struct nd_label_ent *label_ent;
1870
1871                 lockdep_assert_held(&nd_mapping->lock);
1872                 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1873                         nd_label = label_ent->label;
1874                         if (!nd_label)
1875                                 continue;
1876                         if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1877                                 break;
1878                         nd_label = NULL;
1879                 }
1880
1881                 if (!nd_label) {
1882                         WARN_ON(1);
1883                         return -EINVAL;
1884                 }
1885
1886                 /*
1887                  * Check that this label is compliant with the dpa
1888                  * range published in NFIT
1889                  */
1890                 hw_start = nd_mapping->start;
1891                 hw_end = hw_start + nd_mapping->size;
1892                 pmem_start = __le64_to_cpu(nd_label->dpa);
1893                 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1894                 if (pmem_start >= hw_start && pmem_start < hw_end
1895                                 && pmem_end <= hw_end && pmem_end > hw_start)
1896                         /* pass */;
1897                 else {
1898                         dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1899                                         dev_name(ndd->dev), nd_label->uuid);
1900                         return -EINVAL;
1901                 }
1902
1903                 /* move recently validated label to the front of the list */
1904                 list_move(&label_ent->list, &nd_mapping->labels);
1905         }
1906         return 0;
1907 }
1908
1909 /**
1910  * create_namespace_pmem - validate interleave set labelling, retrieve label0
1911  * @nd_region: region with mappings to validate
1912  * @nspm: target namespace to create
1913  * @nd_label: target pmem namespace label to evaluate
1914  */
1915 static struct device *create_namespace_pmem(struct nd_region *nd_region,
1916                 struct nd_namespace_index *nsindex,
1917                 struct nd_namespace_label *nd_label)
1918 {
1919         u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
1920         u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
1921         struct nd_label_ent *label_ent;
1922         struct nd_namespace_pmem *nspm;
1923         struct nd_mapping *nd_mapping;
1924         resource_size_t size = 0;
1925         struct resource *res;
1926         struct device *dev;
1927         int rc = 0;
1928         u16 i;
1929
1930         if (cookie == 0) {
1931                 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1932                 return ERR_PTR(-ENXIO);
1933         }
1934
1935         if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1936                 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1937                                 nd_label->uuid);
1938                 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1939                         return ERR_PTR(-EAGAIN);
1940
1941                 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1942                                 nd_label->uuid);
1943         }
1944
1945         nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1946         if (!nspm)
1947                 return ERR_PTR(-ENOMEM);
1948
1949         nspm->id = -1;
1950         dev = &nspm->nsio.common.dev;
1951         dev->type = &namespace_pmem_device_type;
1952         dev->parent = &nd_region->dev;
1953         res = &nspm->nsio.res;
1954         res->name = dev_name(&nd_region->dev);
1955         res->flags = IORESOURCE_MEM;
1956
1957         for (i = 0; i < nd_region->ndr_mappings; i++) {
1958                 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1959                         continue;
1960                 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1961                         continue;
1962                 break;
1963         }
1964
1965         if (i < nd_region->ndr_mappings) {
1966                 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
1967
1968                 /*
1969                  * Give up if we don't find an instance of a uuid at each
1970                  * position (from 0 to nd_region->ndr_mappings - 1), or if we
1971                  * find a dimm with two instances of the same uuid.
1972                  */
1973                 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1974                                 nvdimm_name(nvdimm), nd_label->uuid);
1975                 rc = -EINVAL;
1976                 goto err;
1977         }
1978
1979         /*
1980          * Fix up each mapping's 'labels' to have the validated pmem label for
1981          * that position at labels[0], and NULL at labels[1].  In the process,
1982          * check that the namespace aligns with interleave-set.  We know
1983          * that it does not overlap with any blk namespaces by virtue of
1984          * the dimm being enabled (i.e. nd_label_reserve_dpa()
1985          * succeeded).
1986          */
1987         rc = select_pmem_id(nd_region, nd_label->uuid);
1988         if (rc)
1989                 goto err;
1990
1991         /* Calculate total size and populate namespace properties from label0 */
1992         for (i = 0; i < nd_region->ndr_mappings; i++) {
1993                 struct nd_namespace_label *label0;
1994                 struct nvdimm_drvdata *ndd;
1995
1996                 nd_mapping = &nd_region->mapping[i];
1997                 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1998                                 typeof(*label_ent), list);
1999                 label0 = label_ent ? label_ent->label : NULL;
2000
2001                 if (!label0) {
2002                         WARN_ON(1);
2003                         continue;
2004                 }
2005
2006                 size += __le64_to_cpu(label0->rawsize);
2007                 if (__le16_to_cpu(label0->position) != 0)
2008                         continue;
2009                 WARN_ON(nspm->alt_name || nspm->uuid);
2010                 nspm->alt_name = kmemdup((void __force *) label0->name,
2011                                 NSLABEL_NAME_LEN, GFP_KERNEL);
2012                 nspm->uuid = kmemdup((void __force *) label0->uuid,
2013                                 NSLABEL_UUID_LEN, GFP_KERNEL);
2014                 nspm->lbasize = __le64_to_cpu(label0->lbasize);
2015                 ndd = to_ndd(nd_mapping);
2016                 if (namespace_label_has(ndd, abstraction_guid))
2017                         nspm->nsio.common.claim_class
2018                                 = to_nvdimm_cclass(&label0->abstraction_guid);
2019
2020         }
2021
2022         if (!nspm->alt_name || !nspm->uuid) {
2023                 rc = -ENOMEM;
2024                 goto err;
2025         }
2026
2027         nd_namespace_pmem_set_resource(nd_region, nspm, size);
2028
2029         return dev;
2030  err:
2031         namespace_pmem_release(dev);
2032         switch (rc) {
2033         case -EINVAL:
2034                 dev_dbg(&nd_region->dev, "invalid label(s)\n");
2035                 break;
2036         case -ENODEV:
2037                 dev_dbg(&nd_region->dev, "label not found\n");
2038                 break;
2039         default:
2040                 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
2041                 break;
2042         }
2043         return ERR_PTR(rc);
2044 }
2045
2046 struct resource *nsblk_add_resource(struct nd_region *nd_region,
2047                 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2048                 resource_size_t start)
2049 {
2050         struct nd_label_id label_id;
2051         struct resource *res;
2052
2053         nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2054         res = krealloc(nsblk->res,
2055                         sizeof(void *) * (nsblk->num_resources + 1),
2056                         GFP_KERNEL);
2057         if (!res)
2058                 return NULL;
2059         nsblk->res = (struct resource **) res;
2060         for_each_dpa_resource(ndd, res)
2061                 if (strcmp(res->name, label_id.id) == 0
2062                                 && res->start == start) {
2063                         nsblk->res[nsblk->num_resources++] = res;
2064                         return res;
2065                 }
2066         return NULL;
2067 }
2068
2069 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2070 {
2071         struct nd_namespace_blk *nsblk;
2072         struct device *dev;
2073
2074         if (!is_nd_blk(&nd_region->dev))
2075                 return NULL;
2076
2077         nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2078         if (!nsblk)
2079                 return NULL;
2080
2081         dev = &nsblk->common.dev;
2082         dev->type = &namespace_blk_device_type;
2083         nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2084         if (nsblk->id < 0) {
2085                 kfree(nsblk);
2086                 return NULL;
2087         }
2088         dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2089         dev->parent = &nd_region->dev;
2090         dev->groups = nd_namespace_attribute_groups;
2091
2092         return &nsblk->common.dev;
2093 }
2094
2095 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2096 {
2097         struct nd_namespace_pmem *nspm;
2098         struct resource *res;
2099         struct device *dev;
2100
2101         if (!is_memory(&nd_region->dev))
2102                 return NULL;
2103
2104         nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2105         if (!nspm)
2106                 return NULL;
2107
2108         dev = &nspm->nsio.common.dev;
2109         dev->type = &namespace_pmem_device_type;
2110         dev->parent = &nd_region->dev;
2111         res = &nspm->nsio.res;
2112         res->name = dev_name(&nd_region->dev);
2113         res->flags = IORESOURCE_MEM;
2114
2115         nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2116         if (nspm->id < 0) {
2117                 kfree(nspm);
2118                 return NULL;
2119         }
2120         dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
2121         dev->parent = &nd_region->dev;
2122         dev->groups = nd_namespace_attribute_groups;
2123         nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2124
2125         return dev;
2126 }
2127
2128 void nd_region_create_ns_seed(struct nd_region *nd_region)
2129 {
2130         WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2131
2132         if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2133                 return;
2134
2135         if (is_nd_blk(&nd_region->dev))
2136                 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2137         else
2138                 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2139
2140         /*
2141          * Seed creation failures are not fatal, provisioning is simply
2142          * disabled until memory becomes available
2143          */
2144         if (!nd_region->ns_seed)
2145                 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2146                                 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
2147         else
2148                 nd_device_register(nd_region->ns_seed);
2149 }
2150
2151 void nd_region_create_dax_seed(struct nd_region *nd_region)
2152 {
2153         WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2154         nd_region->dax_seed = nd_dax_create(nd_region);
2155         /*
2156          * Seed creation failures are not fatal, provisioning is simply
2157          * disabled until memory becomes available
2158          */
2159         if (!nd_region->dax_seed)
2160                 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2161 }
2162
2163 void nd_region_create_pfn_seed(struct nd_region *nd_region)
2164 {
2165         WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2166         nd_region->pfn_seed = nd_pfn_create(nd_region);
2167         /*
2168          * Seed creation failures are not fatal, provisioning is simply
2169          * disabled until memory becomes available
2170          */
2171         if (!nd_region->pfn_seed)
2172                 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2173 }
2174
2175 void nd_region_create_btt_seed(struct nd_region *nd_region)
2176 {
2177         WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2178         nd_region->btt_seed = nd_btt_create(nd_region);
2179         /*
2180          * Seed creation failures are not fatal, provisioning is simply
2181          * disabled until memory becomes available
2182          */
2183         if (!nd_region->btt_seed)
2184                 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2185 }
2186
2187 static int add_namespace_resource(struct nd_region *nd_region,
2188                 struct nd_namespace_label *nd_label, struct device **devs,
2189                 int count)
2190 {
2191         struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2192         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2193         int i;
2194
2195         for (i = 0; i < count; i++) {
2196                 u8 *uuid = namespace_to_uuid(devs[i]);
2197                 struct resource *res;
2198
2199                 if (IS_ERR_OR_NULL(uuid)) {
2200                         WARN_ON(1);
2201                         continue;
2202                 }
2203
2204                 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2205                         continue;
2206                 if (is_namespace_blk(devs[i])) {
2207                         res = nsblk_add_resource(nd_region, ndd,
2208                                         to_nd_namespace_blk(devs[i]),
2209                                         __le64_to_cpu(nd_label->dpa));
2210                         if (!res)
2211                                 return -ENXIO;
2212                         nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2213                 } else {
2214                         dev_err(&nd_region->dev,
2215                                         "error: conflicting extents for uuid: %pUb\n",
2216                                         nd_label->uuid);
2217                         return -ENXIO;
2218                 }
2219                 break;
2220         }
2221
2222         return i;
2223 }
2224
2225 static struct device *create_namespace_blk(struct nd_region *nd_region,
2226                 struct nd_namespace_label *nd_label, int count)
2227 {
2228
2229         struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2230         struct nd_interleave_set *nd_set = nd_region->nd_set;
2231         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2232         struct nd_namespace_blk *nsblk;
2233         char name[NSLABEL_NAME_LEN];
2234         struct device *dev = NULL;
2235         struct resource *res;
2236
2237         if (namespace_label_has(ndd, type_guid)) {
2238                 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
2239                         dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
2240                                         nd_set->type_guid.b,
2241                                         nd_label->type_guid.b);
2242                         return ERR_PTR(-EAGAIN);
2243                 }
2244
2245                 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
2246                         dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
2247                                         nd_set->cookie2,
2248                                         __le64_to_cpu(nd_label->isetcookie));
2249                         return ERR_PTR(-EAGAIN);
2250                 }
2251         }
2252
2253         nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2254         if (!nsblk)
2255                 return ERR_PTR(-ENOMEM);
2256         dev = &nsblk->common.dev;
2257         dev->type = &namespace_blk_device_type;
2258         dev->parent = &nd_region->dev;
2259         nsblk->id = -1;
2260         nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2261         nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2262                         GFP_KERNEL);
2263         if (namespace_label_has(ndd, abstraction_guid))
2264                 nsblk->common.claim_class
2265                         = to_nvdimm_cclass(&nd_label->abstraction_guid);
2266         if (!nsblk->uuid)
2267                 goto blk_err;
2268         memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2269         if (name[0]) {
2270                 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2271                                 GFP_KERNEL);
2272                 if (!nsblk->alt_name)
2273                         goto blk_err;
2274         }
2275         res = nsblk_add_resource(nd_region, ndd, nsblk,
2276                         __le64_to_cpu(nd_label->dpa));
2277         if (!res)
2278                 goto blk_err;
2279         nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2280         return dev;
2281  blk_err:
2282         namespace_blk_release(dev);
2283         return ERR_PTR(-ENXIO);
2284 }
2285
2286 static int cmp_dpa(const void *a, const void *b)
2287 {
2288         const struct device *dev_a = *(const struct device **) a;
2289         const struct device *dev_b = *(const struct device **) b;
2290         struct nd_namespace_blk *nsblk_a, *nsblk_b;
2291         struct nd_namespace_pmem *nspm_a, *nspm_b;
2292
2293         if (is_namespace_io(dev_a))
2294                 return 0;
2295
2296         if (is_namespace_blk(dev_a)) {
2297                 nsblk_a = to_nd_namespace_blk(dev_a);
2298                 nsblk_b = to_nd_namespace_blk(dev_b);
2299
2300                 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2301                                 sizeof(resource_size_t));
2302         }
2303
2304         nspm_a = to_nd_namespace_pmem(dev_a);
2305         nspm_b = to_nd_namespace_pmem(dev_b);
2306
2307         return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2308                         sizeof(resource_size_t));
2309 }
2310
2311 static struct device **scan_labels(struct nd_region *nd_region)
2312 {
2313         int i, count = 0;
2314         struct device *dev, **devs = NULL;
2315         struct nd_label_ent *label_ent, *e;
2316         struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2317         resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
2318
2319         /* "safe" because create_namespace_pmem() might list_move() label_ent */
2320         list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
2321                 struct nd_namespace_label *nd_label = label_ent->label;
2322                 struct device **__devs;
2323                 u32 flags;
2324
2325                 if (!nd_label)
2326                         continue;
2327                 flags = __le32_to_cpu(nd_label->flags);
2328                 if (is_nd_blk(&nd_region->dev)
2329                                 == !!(flags & NSLABEL_FLAG_LOCAL))
2330                         /* pass, region matches label type */;
2331                 else
2332                         continue;
2333
2334                 /* skip labels that describe extents outside of the region */
2335                 if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
2336                     __le64_to_cpu(nd_label->dpa) > map_end)
2337                                 continue;
2338
2339                 i = add_namespace_resource(nd_region, nd_label, devs, count);
2340                 if (i < 0)
2341                         goto err;
2342                 if (i < count)
2343                         continue;
2344                 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2345                 if (!__devs)
2346                         goto err;
2347                 memcpy(__devs, devs, sizeof(dev) * count);
2348                 kfree(devs);
2349                 devs = __devs;
2350
2351                 if (is_nd_blk(&nd_region->dev))
2352                         dev = create_namespace_blk(nd_region, nd_label, count);
2353                 else {
2354                         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2355                         struct nd_namespace_index *nsindex;
2356
2357                         nsindex = to_namespace_index(ndd, ndd->ns_current);
2358                         dev = create_namespace_pmem(nd_region, nsindex, nd_label);
2359                 }
2360
2361                 if (IS_ERR(dev)) {
2362                         switch (PTR_ERR(dev)) {
2363                         case -EAGAIN:
2364                                 /* skip invalid labels */
2365                                 continue;
2366                         case -ENODEV:
2367                                 /* fallthrough to seed creation */
2368                                 break;
2369                         default:
2370                                 goto err;
2371                         }
2372                 } else
2373                         devs[count++] = dev;
2374
2375         }
2376
2377         dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2378                         count, is_nd_blk(&nd_region->dev)
2379                         ? "blk" : "pmem", count == 1 ? "" : "s");
2380
2381         if (count == 0) {
2382                 /* Publish a zero-sized namespace for userspace to configure. */
2383                 nd_mapping_free_labels(nd_mapping);
2384
2385                 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2386                 if (!devs)
2387                         goto err;
2388                 if (is_nd_blk(&nd_region->dev)) {
2389                         struct nd_namespace_blk *nsblk;
2390
2391                         nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2392                         if (!nsblk)
2393                                 goto err;
2394                         dev = &nsblk->common.dev;
2395                         dev->type = &namespace_blk_device_type;
2396                 } else {
2397                         struct nd_namespace_pmem *nspm;
2398
2399                         nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2400                         if (!nspm)
2401                                 goto err;
2402                         dev = &nspm->nsio.common.dev;
2403                         dev->type = &namespace_pmem_device_type;
2404                         nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2405                 }
2406                 dev->parent = &nd_region->dev;
2407                 devs[count++] = dev;
2408         } else if (is_memory(&nd_region->dev)) {
2409                 /* clean unselected labels */
2410                 for (i = 0; i < nd_region->ndr_mappings; i++) {
2411                         struct list_head *l, *e;
2412                         LIST_HEAD(list);
2413                         int j;
2414
2415                         nd_mapping = &nd_region->mapping[i];
2416                         if (list_empty(&nd_mapping->labels)) {
2417                                 WARN_ON(1);
2418                                 continue;
2419                         }
2420
2421                         j = count;
2422                         list_for_each_safe(l, e, &nd_mapping->labels) {
2423                                 if (!j--)
2424                                         break;
2425                                 list_move_tail(l, &list);
2426                         }
2427                         nd_mapping_free_labels(nd_mapping);
2428                         list_splice_init(&list, &nd_mapping->labels);
2429                 }
2430         }
2431
2432         if (count > 1)
2433                 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2434
2435         return devs;
2436
2437  err:
2438         if (devs) {
2439                 for (i = 0; devs[i]; i++)
2440                         if (is_nd_blk(&nd_region->dev))
2441                                 namespace_blk_release(devs[i]);
2442                         else
2443                                 namespace_pmem_release(devs[i]);
2444                 kfree(devs);
2445         }
2446         return NULL;
2447 }
2448
2449 static struct device **create_namespaces(struct nd_region *nd_region)
2450 {
2451         struct nd_mapping *nd_mapping;
2452         struct device **devs;
2453         int i;
2454
2455         if (nd_region->ndr_mappings == 0)
2456                 return NULL;
2457
2458         /* lock down all mappings while we scan labels */
2459         for (i = 0; i < nd_region->ndr_mappings; i++) {
2460                 nd_mapping = &nd_region->mapping[i];
2461                 mutex_lock_nested(&nd_mapping->lock, i);
2462         }
2463
2464         devs = scan_labels(nd_region);
2465
2466         for (i = 0; i < nd_region->ndr_mappings; i++) {
2467                 int reverse = nd_region->ndr_mappings - 1 - i;
2468
2469                 nd_mapping = &nd_region->mapping[reverse];
2470                 mutex_unlock(&nd_mapping->lock);
2471         }
2472
2473         return devs;
2474 }
2475
2476 static int init_active_labels(struct nd_region *nd_region)
2477 {
2478         int i;
2479
2480         for (i = 0; i < nd_region->ndr_mappings; i++) {
2481                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2482                 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2483                 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2484                 struct nd_label_ent *label_ent;
2485                 int count, j;
2486
2487                 /*
2488                  * If the dimm is disabled then we may need to prevent
2489                  * the region from being activated.
2490                  */
2491                 if (!ndd) {
2492                         if (test_bit(NDD_LOCKED, &nvdimm->flags))
2493                                 /* fail, label data may be unreadable */;
2494                         else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2495                                 /* fail, labels needed to disambiguate dpa */;
2496                         else
2497                                 return 0;
2498
2499                         dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2500                                         dev_name(&nd_mapping->nvdimm->dev),
2501                                         test_bit(NDD_LOCKED, &nvdimm->flags)
2502                                         ? "locked" : "disabled");
2503                         return -ENXIO;
2504                 }
2505                 nd_mapping->ndd = ndd;
2506                 atomic_inc(&nvdimm->busy);
2507                 get_ndd(ndd);
2508
2509                 count = nd_label_active_count(ndd);
2510                 dev_dbg(ndd->dev, "count: %d\n", count);
2511                 if (!count)
2512                         continue;
2513                 for (j = 0; j < count; j++) {
2514                         struct nd_namespace_label *label;
2515
2516                         label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2517                         if (!label_ent)
2518                                 break;
2519                         label = nd_label_active(ndd, j);
2520                         label_ent->label = label;
2521
2522                         mutex_lock(&nd_mapping->lock);
2523                         list_add_tail(&label_ent->list, &nd_mapping->labels);
2524                         mutex_unlock(&nd_mapping->lock);
2525                 }
2526
2527                 if (j >= count)
2528                         continue;
2529
2530                 mutex_lock(&nd_mapping->lock);
2531                 nd_mapping_free_labels(nd_mapping);
2532                 mutex_unlock(&nd_mapping->lock);
2533                 return -ENOMEM;
2534         }
2535
2536         return 0;
2537 }
2538
2539 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2540 {
2541         struct device **devs = NULL;
2542         int i, rc = 0, type;
2543
2544         *err = 0;
2545         nvdimm_bus_lock(&nd_region->dev);
2546         rc = init_active_labels(nd_region);
2547         if (rc) {
2548                 nvdimm_bus_unlock(&nd_region->dev);
2549                 return rc;
2550         }
2551
2552         type = nd_region_to_nstype(nd_region);
2553         switch (type) {
2554         case ND_DEVICE_NAMESPACE_IO:
2555                 devs = create_namespace_io(nd_region);
2556                 break;
2557         case ND_DEVICE_NAMESPACE_PMEM:
2558         case ND_DEVICE_NAMESPACE_BLK:
2559                 devs = create_namespaces(nd_region);
2560                 break;
2561         default:
2562                 break;
2563         }
2564         nvdimm_bus_unlock(&nd_region->dev);
2565
2566         if (!devs)
2567                 return -ENODEV;
2568
2569         for (i = 0; devs[i]; i++) {
2570                 struct device *dev = devs[i];
2571                 int id;
2572
2573                 if (type == ND_DEVICE_NAMESPACE_BLK) {
2574                         struct nd_namespace_blk *nsblk;
2575
2576                         nsblk = to_nd_namespace_blk(dev);
2577                         id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2578                                         GFP_KERNEL);
2579                         nsblk->id = id;
2580                 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2581                         struct nd_namespace_pmem *nspm;
2582
2583                         nspm = to_nd_namespace_pmem(dev);
2584                         id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2585                                         GFP_KERNEL);
2586                         nspm->id = id;
2587                 } else
2588                         id = i;
2589
2590                 if (id < 0)
2591                         break;
2592                 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
2593                 dev->groups = nd_namespace_attribute_groups;
2594                 nd_device_register(dev);
2595         }
2596         if (i)
2597                 nd_region->ns_seed = devs[0];
2598
2599         if (devs[i]) {
2600                 int j;
2601
2602                 for (j = i; devs[j]; j++) {
2603                         struct device *dev = devs[j];
2604
2605                         device_initialize(dev);
2606                         put_device(dev);
2607                 }
2608                 *err = j - i;
2609                 /*
2610                  * All of the namespaces we tried to register failed, so
2611                  * fail region activation.
2612                  */
2613                 if (*err == 0)
2614                         rc = -ENODEV;
2615         }
2616         kfree(devs);
2617
2618         if (rc == -ENODEV)
2619                 return rc;
2620
2621         return i;
2622 }