Linux 4.19.133
[linux/fpc-iii.git] / drivers / nvdimm / namespace_devs.c
blob63640c315d939648470eab86dba19eb8403e7865
1 /*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
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"
23 static void namespace_io_release(struct device *dev)
25 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
27 kfree(nsio);
30 static void namespace_pmem_release(struct device *dev)
32 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
33 struct nd_region *nd_region = to_nd_region(dev->parent);
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);
42 static void namespace_blk_release(struct device *dev)
44 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
45 struct nd_region *nd_region = to_nd_region(dev->parent);
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);
55 static const struct device_type namespace_io_device_type = {
56 .name = "nd_namespace_io",
57 .release = namespace_io_release,
60 static const struct device_type namespace_pmem_device_type = {
61 .name = "nd_namespace_pmem",
62 .release = namespace_pmem_release,
65 static const struct device_type namespace_blk_device_type = {
66 .name = "nd_namespace_blk",
67 .release = namespace_blk_release,
70 static bool is_namespace_pmem(const struct device *dev)
72 return dev ? dev->type == &namespace_pmem_device_type : false;
75 static bool is_namespace_blk(const struct device *dev)
77 return dev ? dev->type == &namespace_blk_device_type : false;
80 static bool is_namespace_io(const struct device *dev)
82 return dev ? dev->type == &namespace_io_device_type : false;
85 static int is_uuid_busy(struct device *dev, void *data)
87 u8 *uuid1 = data, *uuid2 = NULL;
89 if (is_namespace_pmem(dev)) {
90 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
92 uuid2 = nspm->uuid;
93 } else if (is_namespace_blk(dev)) {
94 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
96 uuid2 = nsblk->uuid;
97 } else if (is_nd_btt(dev)) {
98 struct nd_btt *nd_btt = to_nd_btt(dev);
100 uuid2 = nd_btt->uuid;
101 } else if (is_nd_pfn(dev)) {
102 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
104 uuid2 = nd_pfn->uuid;
107 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
108 return -EBUSY;
110 return 0;
113 static int is_namespace_uuid_busy(struct device *dev, void *data)
115 if (is_nd_region(dev))
116 return device_for_each_child(dev, data, is_uuid_busy);
117 return 0;
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
125 bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
127 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
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;
138 bool pmem_should_map_pages(struct device *dev)
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;
144 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
145 return false;
147 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
148 return false;
150 if (is_nd_pfn(dev) || is_nd_btt(dev))
151 return false;
153 if (ndns->force_raw)
154 return false;
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;
162 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
164 EXPORT_SYMBOL(pmem_should_map_pages);
166 unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
168 if (is_namespace_pmem(&ndns->dev)) {
169 struct nd_namespace_pmem *nspm;
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);
182 * There is no namespace label (is_namespace_io()), or the label
183 * indicates the default sector size.
185 return 512;
187 EXPORT_SYMBOL(pmem_sector_size);
189 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
190 char *name)
192 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
193 const char *suffix = NULL;
195 if (ndns->claim && is_nd_btt(ndns->claim))
196 suffix = "s";
198 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
199 int nsidx = 0;
201 if (is_namespace_pmem(&ndns->dev)) {
202 struct nd_namespace_pmem *nspm;
204 nspm = to_nd_namespace_pmem(&ndns->dev);
205 nsidx = nspm->id;
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;
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;
224 return name;
226 EXPORT_SYMBOL(nvdimm_namespace_disk_name);
228 const u8 *nd_dev_to_uuid(struct device *dev)
230 static const u8 null_uuid[16];
232 if (!dev)
233 return null_uuid;
235 if (is_namespace_pmem(dev)) {
236 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
238 return nspm->uuid;
239 } else if (is_namespace_blk(dev)) {
240 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
242 return nsblk->uuid;
243 } else
244 return null_uuid;
246 EXPORT_SYMBOL(nd_dev_to_uuid);
248 static ssize_t nstype_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct nd_region *nd_region = to_nd_region(dev->parent);
253 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
255 static DEVICE_ATTR_RO(nstype);
257 static ssize_t __alt_name_store(struct device *dev, const char *buf,
258 const size_t len)
260 char *input, *pos, *alt_name, **ns_altname;
261 ssize_t rc;
263 if (is_namespace_pmem(dev)) {
264 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
266 ns_altname = &nspm->alt_name;
267 } else if (is_namespace_blk(dev)) {
268 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
270 ns_altname = &nsblk->alt_name;
271 } else
272 return -ENXIO;
274 if (dev->driver || to_ndns(dev)->claim)
275 return -EBUSY;
277 input = kmemdup(buf, len + 1, GFP_KERNEL);
278 if (!input)
279 return -ENOMEM;
281 input[len] = '\0';
282 pos = strim(input);
283 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
284 rc = -EINVAL;
285 goto out;
288 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
289 if (!alt_name) {
290 rc = -ENOMEM;
291 goto out;
293 kfree(*ns_altname);
294 *ns_altname = alt_name;
295 sprintf(*ns_altname, "%s", pos);
296 rc = len;
298 out:
299 kfree(input);
300 return rc;
303 static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
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;
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;
321 static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
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;
330 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
331 return false;
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;
339 * Resources with unacknowledged adjustments indicate a
340 * failure to update labels
342 if (res->flags & DPA_RESOURCE_ADJUSTED)
343 return false;
344 count++;
347 /* These values match after a successful label update */
348 if (count != nsblk->num_resources)
349 return false;
351 for (i = 0; i < nsblk->num_resources; i++) {
352 struct resource *found = NULL;
354 for_each_dpa_resource(ndd, res)
355 if (res == nsblk->res[i]) {
356 found = res;
357 break;
359 /* stale resource */
360 if (!found)
361 return false;
364 return true;
367 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
369 resource_size_t size;
371 nvdimm_bus_lock(&nsblk->common.dev);
372 size = __nd_namespace_blk_validate(nsblk);
373 nvdimm_bus_unlock(&nsblk->common.dev);
375 return size;
377 EXPORT_SYMBOL(nd_namespace_blk_validate);
380 static int nd_namespace_label_update(struct nd_region *nd_region,
381 struct device *dev)
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;
389 * Only allow label writes that will result in a valid namespace
390 * or deletion of an existing namespace.
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);
396 if (size == 0 && nspm->uuid)
397 /* delete allocation */;
398 else if (!nspm->uuid)
399 return 0;
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);
406 if (size == 0 && nsblk->uuid)
407 /* delete allocation */;
408 else if (!nsblk->uuid || !nsblk->lbasize)
409 return 0;
411 return nd_blk_namespace_label_update(nd_region, nsblk, size);
412 } else
413 return -ENXIO;
416 static ssize_t alt_name_store(struct device *dev,
417 struct device_attribute *attr, const char *buf, size_t len)
419 struct nd_region *nd_region = to_nd_region(dev->parent);
420 ssize_t rc;
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);
432 return rc < 0 ? rc : len;
435 static ssize_t alt_name_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 char *ns_altname;
440 if (is_namespace_pmem(dev)) {
441 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
443 ns_altname = nspm->alt_name;
444 } else if (is_namespace_blk(dev)) {
445 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
447 ns_altname = nsblk->alt_name;
448 } else
449 return -ENXIO;
451 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
453 static DEVICE_ATTR_RW(alt_name);
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)
459 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
460 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
461 int rc = 0;
463 while (n) {
464 struct resource *res, *last;
465 resource_size_t new_start;
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;
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;
484 * Keep BLK allocations relegated to high DPA as much as
485 * possible
487 if (is_blk)
488 new_start = res->start + n;
489 else
490 new_start = res->start;
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;
499 return rc;
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
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.
513 static int shrink_dpa_allocation(struct nd_region *nd_region,
514 struct nd_label_id *label_id, resource_size_t n)
516 int i;
518 for (i = 0; i < nd_region->ndr_mappings; i++) {
519 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
520 int rc;
522 rc = scan_free(nd_region, nd_mapping, label_id, n);
523 if (rc)
524 return rc;
527 return 0;
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)
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;
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;
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;
551 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
552 return rc ? n : 0;
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
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.
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)
577 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
578 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
580 if (valid->start >= valid->end)
581 goto invalid;
583 if (is_reserve)
584 return;
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,
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;
601 /* allocation needs to be contiguous, so this is all or nothing */
602 if (resource_size(valid) < n)
603 goto invalid;
605 /* we've got all the space we need and no existing allocation */
606 if (!exist)
607 return;
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;
614 invalid:
615 /* truncate @valid size to 0 */
616 valid->end = valid->start - 1;
619 enum alloc_loc {
620 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
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)
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;
634 for_each_dpa_resource(ndd, res)
635 if (strcmp(label_id->id, res->name) == 0)
636 exist = res;
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;
650 /* ignore resources outside this nd_mapping */
651 if (res->start > mapping_end)
652 continue;
653 if (res->end < nd_mapping->start)
654 continue;
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;
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;
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;
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;
725 if (strcmp(action, "allocate") == 0) {
726 /* BLK allocate bottom up */
727 if (!is_pmem)
728 valid.start += available - allocate;
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;
742 if (!new_res)
743 new_res = res;
745 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
746 action, loc, rc);
748 if (rc)
749 return n;
751 n -= allocate;
752 if (n) {
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
760 goto retry;
761 } else
762 return 0;
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.
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;
775 static int merge_dpa(struct nd_region *nd_region,
776 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
778 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
779 struct resource *res;
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);
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;
803 return 0;
806 int __reserve_free_pmem(struct device *dev, void *data)
808 struct nvdimm *nvdimm = data;
809 struct nd_region *nd_region;
810 struct nd_label_id label_id;
811 int i;
813 if (!is_memory(dev))
814 return 0;
816 nd_region = to_nd_region(dev);
817 if (nd_region->ndr_mappings == 0)
818 return 0;
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;
826 if (nd_mapping->nvdimm != nvdimm)
827 continue;
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;
840 return 0;
843 void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
844 struct nd_mapping *nd_mapping)
846 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
847 struct resource *res, *_res;
849 for_each_dpa_resource_safe(ndd, res, _res)
850 if (strcmp(res->name, "pmem-reserve") == 0)
851 nvdimm_free_dpa(ndd, res);
854 static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
855 struct nd_mapping *nd_mapping)
857 struct nvdimm *nvdimm = nd_mapping->nvdimm;
858 int rc;
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;
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
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.
880 static int grow_dpa_allocation(struct nd_region *nd_region,
881 struct nd_label_id *label_id, resource_size_t n)
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;
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;
893 * In the BLK case try once with all unallocated PMEM
894 * reserved, and once without
896 for (j = is_pmem; j < 2; j++) {
897 bool blk_only = j == 0;
899 if (blk_only) {
900 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
901 if (rc)
902 return rc;
904 rem = scan_allocate(nd_region, nd_mapping,
905 label_id, rem);
906 if (blk_only)
907 release_free_pmem(nvdimm_bus, nd_mapping);
909 /* try again and allow encroachments into PMEM */
910 if (rem == 0)
911 break;
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;
921 rc = merge_dpa(nd_region, nd_mapping, label_id);
922 if (rc)
923 return rc;
926 return 0;
929 static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
930 struct nd_namespace_pmem *nspm, resource_size_t size)
932 struct resource *res = &nspm->nsio.res;
933 resource_size_t offset = 0;
935 if (size && !nspm->uuid) {
936 WARN_ON_ONCE(1);
937 size = 0;
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;
946 if (!ndd) {
947 size = 0;
948 goto out;
951 nd_label_gen_id(&label_id, nspm->uuid, 0);
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;
961 WARN_ON_ONCE(1);
962 size = 0;
965 out:
966 res->start = nd_region->ndr_start + offset;
967 res->end = res->start + size - 1;
970 static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
972 if (!uuid) {
973 dev_dbg(dev, "%s: uuid not set\n", where);
974 return true;
976 return false;
979 static ssize_t __size_store(struct device *dev, unsigned long long val)
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;
991 if (dev->driver || ndns->claim)
992 return -EBUSY;
994 if (is_namespace_pmem(dev)) {
995 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
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);
1002 uuid = nsblk->uuid;
1003 flags = NSLABEL_FLAG_LOCAL;
1004 id = nsblk->id;
1008 * We need a uuid for the allocation-label and dimm(s) on which
1009 * to store the label.
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;
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;
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);
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.
1034 if (!ndd)
1035 return -ENXIO;
1037 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1039 available = nd_region_allocatable_dpa(nd_region);
1041 if (val > available + allocated)
1042 return -ENOSPC;
1044 if (val == allocated)
1045 return 0;
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);
1055 if (rc)
1056 return rc;
1058 if (is_namespace_pmem(dev)) {
1059 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1061 nd_namespace_pmem_set_resource(nd_region, nspm,
1062 val * nd_region->ndr_mappings);
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.
1071 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
1072 nd_device_unregister(dev, ND_ASYNC);
1074 return rc;
1077 static ssize_t size_store(struct device *dev,
1078 struct device_attribute *attr, const char *buf, size_t len)
1080 struct nd_region *nd_region = to_nd_region(dev->parent);
1081 unsigned long long val;
1082 u8 **uuid = NULL;
1083 int rc;
1085 rc = kstrtoull(buf, 0, &val);
1086 if (rc)
1087 return rc;
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);
1096 if (is_namespace_pmem(dev)) {
1097 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1099 uuid = &nspm->uuid;
1100 } else if (is_namespace_blk(dev)) {
1101 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1103 uuid = &nsblk->uuid;
1106 if (rc == 0 && val == 0 && uuid) {
1107 /* setting size zero == 'delete namespace' */
1108 kfree(*uuid);
1109 *uuid = NULL;
1112 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
1114 nvdimm_bus_unlock(dev);
1115 device_unlock(dev);
1117 return rc < 0 ? rc : len;
1120 resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1122 struct device *dev = &ndns->dev;
1124 if (is_namespace_pmem(dev)) {
1125 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
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);
1133 return resource_size(&nsio->res);
1134 } else
1135 WARN_ONCE(1, "unknown namespace type\n");
1136 return 0;
1139 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1141 resource_size_t size;
1143 nvdimm_bus_lock(&ndns->dev);
1144 size = __nvdimm_namespace_capacity(ndns);
1145 nvdimm_bus_unlock(&ndns->dev);
1147 return size;
1149 EXPORT_SYMBOL(nvdimm_namespace_capacity);
1151 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1153 int i;
1154 bool locked = false;
1155 struct device *dev = &ndns->dev;
1156 struct nd_region *nd_region = to_nd_region(dev->parent);
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;
1162 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1163 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1164 locked = true;
1167 return locked;
1169 EXPORT_SYMBOL(nvdimm_namespace_locked);
1171 static ssize_t size_show(struct device *dev,
1172 struct device_attribute *attr, char *buf)
1174 return sprintf(buf, "%llu\n", (unsigned long long)
1175 nvdimm_namespace_capacity(to_ndns(dev)));
1177 static DEVICE_ATTR(size, 0444, size_show, size_store);
1179 static u8 *namespace_to_uuid(struct device *dev)
1181 if (is_namespace_pmem(dev)) {
1182 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1184 return nspm->uuid;
1185 } else if (is_namespace_blk(dev)) {
1186 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1188 return nsblk->uuid;
1189 } else
1190 return ERR_PTR(-ENXIO);
1193 static ssize_t uuid_show(struct device *dev,
1194 struct device_attribute *attr, char *buf)
1196 u8 *uuid = namespace_to_uuid(dev);
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");
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
1212 static int namespace_update_uuid(struct nd_region *nd_region,
1213 struct device *dev, u8 *new_uuid, u8 **old_uuid)
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;
1220 if (!nd_is_uuid_unique(dev, new_uuid))
1221 return -EINVAL;
1223 if (*old_uuid == NULL)
1224 goto out;
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.
1232 for (i = 0; i < nd_region->ndr_mappings; i++) {
1233 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
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.
1240 * FIXME: can we delete uuid with zero dpa allocated?
1242 if (list_empty(&nd_mapping->labels))
1243 return -EBUSY;
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;
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);
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;
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);
1271 mutex_unlock(&nd_mapping->lock);
1273 kfree(*old_uuid);
1274 out:
1275 *old_uuid = new_uuid;
1276 return 0;
1279 static ssize_t uuid_store(struct device *dev,
1280 struct device_attribute *attr, const char *buf, size_t len)
1282 struct nd_region *nd_region = to_nd_region(dev->parent);
1283 u8 *uuid = NULL;
1284 ssize_t rc = 0;
1285 u8 **ns_uuid;
1287 if (is_namespace_pmem(dev)) {
1288 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1290 ns_uuid = &nspm->uuid;
1291 } else if (is_namespace_blk(dev)) {
1292 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1294 ns_uuid = &nsblk->uuid;
1295 } else
1296 return -ENXIO;
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);
1316 return rc < 0 ? rc : len;
1318 static DEVICE_ATTR_RW(uuid);
1320 static ssize_t resource_show(struct device *dev,
1321 struct device_attribute *attr, char *buf)
1323 struct resource *res;
1325 if (is_namespace_pmem(dev)) {
1326 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1328 res = &nspm->nsio.res;
1329 } else if (is_namespace_io(dev)) {
1330 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1332 res = &nsio->res;
1333 } else
1334 return -ENXIO;
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);
1341 static DEVICE_ATTR_RO(resource);
1343 static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
1344 4096, 4104, 4160, 4224, 0 };
1346 static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1348 static ssize_t sector_size_show(struct device *dev,
1349 struct device_attribute *attr, char *buf)
1351 if (is_namespace_blk(dev)) {
1352 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1354 return nd_size_select_show(nsblk->lbasize,
1355 blk_lbasize_supported, buf);
1358 if (is_namespace_pmem(dev)) {
1359 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1361 return nd_size_select_show(nspm->lbasize,
1362 pmem_lbasize_supported, buf);
1364 return -ENXIO;
1367 static ssize_t sector_size_store(struct device *dev,
1368 struct device_attribute *attr, const char *buf, size_t len)
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;
1375 if (is_namespace_blk(dev)) {
1376 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
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);
1383 lbasize = &nspm->lbasize;
1384 supported = pmem_lbasize_supported;
1385 } else
1386 return -ENXIO;
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);
1401 return rc ? rc : len;
1403 static DEVICE_ATTR_RW(sector_size);
1405 static ssize_t dpa_extents_show(struct device *dev,
1406 struct device_attribute *attr, char *buf)
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;
1414 nvdimm_bus_lock(dev);
1415 if (is_namespace_pmem(dev)) {
1416 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
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);
1423 uuid = nsblk->uuid;
1424 flags = NSLABEL_FLAG_LOCAL;
1427 if (!uuid)
1428 goto out;
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;
1436 for_each_dpa_resource(ndd, res)
1437 if (strcmp(res->name, label_id.id) == 0)
1438 count++;
1440 out:
1441 nvdimm_bus_unlock(dev);
1443 return sprintf(buf, "%d\n", count);
1445 static DEVICE_ATTR_RO(dpa_extents);
1447 static int btt_claim_class(struct device *dev)
1449 struct nd_region *nd_region = to_nd_region(dev->parent);
1450 int i, loop_bitmask = 0;
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;
1458 * If any of the DIMMs do not support labels the only
1459 * possible BTT format is v1.
1461 if (!ndd) {
1462 loop_bitmask = 0;
1463 break;
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;
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.
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.
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
1491 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1492 * found, we enforce BTT2.0
1494 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
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;
1508 static ssize_t holder_show(struct device *dev,
1509 struct device_attribute *attr, char *buf)
1511 struct nd_namespace_common *ndns = to_ndns(dev);
1512 ssize_t rc;
1514 device_lock(dev);
1515 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1516 device_unlock(dev);
1518 return rc;
1520 static DEVICE_ATTR_RO(holder);
1522 static ssize_t __holder_class_store(struct device *dev, const char *buf)
1524 struct nd_namespace_common *ndns = to_ndns(dev);
1526 if (dev->driver || ndns->claim)
1527 return -EBUSY;
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;
1540 /* btt_claim_class() could've returned an error */
1541 if (ndns->claim_class < 0)
1542 return ndns->claim_class;
1544 return 0;
1547 static ssize_t holder_class_store(struct device *dev,
1548 struct device_attribute *attr, const char *buf, size_t len)
1550 struct nd_region *nd_region = to_nd_region(dev->parent);
1551 ssize_t rc;
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);
1563 return rc < 0 ? rc : len;
1566 static ssize_t holder_class_show(struct device *dev,
1567 struct device_attribute *attr, char *buf)
1569 struct nd_namespace_common *ndns = to_ndns(dev);
1570 ssize_t rc;
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);
1586 return rc;
1588 static DEVICE_ATTR_RW(holder_class);
1590 static ssize_t mode_show(struct device *dev,
1591 struct device_attribute *attr, char *buf)
1593 struct nd_namespace_common *ndns = to_ndns(dev);
1594 struct device *claim;
1595 char *mode;
1596 ssize_t rc;
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);
1613 return rc;
1615 static DEVICE_ATTR_RO(mode);
1617 static ssize_t force_raw_store(struct device *dev,
1618 struct device_attribute *attr, const char *buf, size_t len)
1620 bool force_raw;
1621 int rc = strtobool(buf, &force_raw);
1623 if (rc)
1624 return rc;
1626 to_ndns(dev)->force_raw = force_raw;
1627 return len;
1630 static ssize_t force_raw_show(struct device *dev,
1631 struct device_attribute *attr, char *buf)
1633 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1635 static DEVICE_ATTR_RW(force_raw);
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,
1652 static umode_t namespace_visible(struct kobject *kobj,
1653 struct attribute *a, int n)
1655 struct device *dev = container_of(kobj, struct device, kobj);
1657 if (a == &dev_attr_resource.attr) {
1658 if (is_namespace_blk(dev))
1659 return 0;
1660 return 0400;
1663 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1664 if (a == &dev_attr_size.attr)
1665 return 0644;
1667 return a->mode;
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;
1677 return 0;
1680 static struct attribute_group nd_namespace_attribute_group = {
1681 .attrs = nd_namespace_attributes,
1682 .is_visible = namespace_visible,
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,
1692 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
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;
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;
1708 if (!ndns)
1709 return ERR_PTR(-ENODEV);
1712 * Flush any in-progess probes / removals in the driver
1713 * for the raw personality of this namespace.
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);
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));
1733 return ERR_PTR(-ENXIO);
1737 if (nvdimm_namespace_locked(ndns))
1738 return ERR_PTR(-EACCES);
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);
1747 if (is_namespace_pmem(&ndns->dev)) {
1748 struct nd_namespace_pmem *nspm;
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;
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);
1763 if (!nd_namespace_blk_validate(nsblk))
1764 return ERR_PTR(-ENODEV);
1767 return ndns;
1769 EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1771 static struct device **create_namespace_io(struct nd_region *nd_region)
1773 struct nd_namespace_io *nsio;
1774 struct device *dev, **devs;
1775 struct resource *res;
1777 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1778 if (!nsio)
1779 return NULL;
1781 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1782 if (!devs) {
1783 kfree(nsio);
1784 return NULL;
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;
1796 devs[0] = dev;
1797 return devs;
1800 static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1801 u64 cookie, u16 pos)
1803 struct nd_namespace_label *found = NULL;
1804 int i;
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;
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;
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);
1824 if (isetcookie != cookie)
1825 continue;
1827 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1828 continue;
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;
1839 if (found_uuid) {
1840 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
1841 return false;
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;
1851 if (found)
1852 break;
1854 return found != NULL;
1857 static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1859 int i;
1861 if (!pmem_id)
1862 return -ENODEV;
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;
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;
1881 if (!nd_label) {
1882 WARN_ON(1);
1883 return -EINVAL;
1887 * Check that this label is compliant with the dpa
1888 * range published in NFIT
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;
1903 /* move recently validated label to the front of the list */
1904 list_move(&label_ent->list, &nd_mapping->labels);
1906 return 0;
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
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)
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;
1930 if (cookie == 0) {
1931 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
1932 return ERR_PTR(-ENXIO);
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);
1941 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1942 nd_label->uuid);
1945 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1946 if (!nspm)
1947 return ERR_PTR(-ENOMEM);
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;
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;
1965 if (i < nd_region->ndr_mappings) {
1966 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
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.
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;
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).
1987 rc = select_pmem_id(nd_region, nd_label->uuid);
1988 if (rc)
1989 goto err;
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;
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;
2001 if (!label0) {
2002 WARN_ON(1);
2003 continue;
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);
2022 if (!nspm->alt_name || !nspm->uuid) {
2023 rc = -ENOMEM;
2024 goto err;
2027 nd_namespace_pmem_set_resource(nd_region, nspm, size);
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;
2043 return ERR_PTR(rc);
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)
2050 struct nd_label_id label_id;
2051 struct resource *res;
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;
2066 return NULL;
2069 static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2071 struct nd_namespace_blk *nsblk;
2072 struct device *dev;
2074 if (!is_nd_blk(&nd_region->dev))
2075 return NULL;
2077 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2078 if (!nsblk)
2079 return NULL;
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;
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;
2092 return &nsblk->common.dev;
2095 static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2097 struct nd_namespace_pmem *nspm;
2098 struct resource *res;
2099 struct device *dev;
2101 if (!is_memory(&nd_region->dev))
2102 return NULL;
2104 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2105 if (!nspm)
2106 return NULL;
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;
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;
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);
2125 return dev;
2128 void nd_region_create_ns_seed(struct nd_region *nd_region)
2130 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2132 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2133 return;
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);
2141 * Seed creation failures are not fatal, provisioning is simply
2142 * disabled until memory becomes available
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);
2151 void nd_region_create_dax_seed(struct nd_region *nd_region)
2153 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2154 nd_region->dax_seed = nd_dax_create(nd_region);
2156 * Seed creation failures are not fatal, provisioning is simply
2157 * disabled until memory becomes available
2159 if (!nd_region->dax_seed)
2160 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2163 void nd_region_create_pfn_seed(struct nd_region *nd_region)
2165 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2166 nd_region->pfn_seed = nd_pfn_create(nd_region);
2168 * Seed creation failures are not fatal, provisioning is simply
2169 * disabled until memory becomes available
2171 if (!nd_region->pfn_seed)
2172 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2175 void nd_region_create_btt_seed(struct nd_region *nd_region)
2177 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2178 nd_region->btt_seed = nd_btt_create(nd_region);
2180 * Seed creation failures are not fatal, provisioning is simply
2181 * disabled until memory becomes available
2183 if (!nd_region->btt_seed)
2184 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2187 static int add_namespace_resource(struct nd_region *nd_region,
2188 struct nd_namespace_label *nd_label, struct device **devs,
2189 int count)
2191 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2192 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2193 int i;
2195 for (i = 0; i < count; i++) {
2196 u8 *uuid = namespace_to_uuid(devs[i]);
2197 struct resource *res;
2199 if (IS_ERR_OR_NULL(uuid)) {
2200 WARN_ON(1);
2201 continue;
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;
2219 break;
2222 return i;
2225 static struct device *create_namespace_blk(struct nd_region *nd_region,
2226 struct nd_namespace_label *nd_label, int count)
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;
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);
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);
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;
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);
2286 static int cmp_dpa(const void *a, const void *b)
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;
2293 if (is_namespace_io(dev_a))
2294 return 0;
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);
2300 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2301 sizeof(resource_size_t));
2304 nspm_a = to_nd_namespace_pmem(dev_a);
2305 nspm_b = to_nd_namespace_pmem(dev_b);
2307 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2308 sizeof(resource_size_t));
2311 static struct device **scan_labels(struct nd_region *nd_region)
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;
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;
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;
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;
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;
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;
2357 nsindex = to_namespace_index(ndd, ndd->ns_current);
2358 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
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;
2372 } else
2373 devs[count++] = dev;
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");
2381 if (count == 0) {
2382 /* Publish a zero-sized namespace for userspace to configure. */
2383 nd_mapping_free_labels(nd_mapping);
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;
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;
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);
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;
2415 nd_mapping = &nd_region->mapping[i];
2416 if (list_empty(&nd_mapping->labels)) {
2417 WARN_ON(1);
2418 continue;
2421 j = count;
2422 list_for_each_safe(l, e, &nd_mapping->labels) {
2423 if (!j--)
2424 break;
2425 list_move_tail(l, &list);
2427 nd_mapping_free_labels(nd_mapping);
2428 list_splice_init(&list, &nd_mapping->labels);
2432 if (count > 1)
2433 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2435 return devs;
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);
2446 return NULL;
2449 static struct device **create_namespaces(struct nd_region *nd_region)
2451 struct nd_mapping *nd_mapping;
2452 struct device **devs;
2453 int i;
2455 if (nd_region->ndr_mappings == 0)
2456 return NULL;
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);
2464 devs = scan_labels(nd_region);
2466 for (i = 0; i < nd_region->ndr_mappings; i++) {
2467 int reverse = nd_region->ndr_mappings - 1 - i;
2469 nd_mapping = &nd_region->mapping[reverse];
2470 mutex_unlock(&nd_mapping->lock);
2473 return devs;
2476 static int init_active_labels(struct nd_region *nd_region)
2478 int i;
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;
2488 * If the dimm is disabled then we may need to prevent
2489 * the region from being activated.
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;
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;
2505 nd_mapping->ndd = ndd;
2506 atomic_inc(&nvdimm->busy);
2507 get_ndd(ndd);
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;
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;
2522 mutex_lock(&nd_mapping->lock);
2523 list_add_tail(&label_ent->list, &nd_mapping->labels);
2524 mutex_unlock(&nd_mapping->lock);
2527 if (j >= count)
2528 continue;
2530 mutex_lock(&nd_mapping->lock);
2531 nd_mapping_free_labels(nd_mapping);
2532 mutex_unlock(&nd_mapping->lock);
2533 return -ENOMEM;
2536 return 0;
2539 int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2541 struct device **devs = NULL;
2542 int i, rc = 0, type;
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;
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;
2564 nvdimm_bus_unlock(&nd_region->dev);
2566 if (!devs)
2567 return -ENODEV;
2569 for (i = 0; devs[i]; i++) {
2570 struct device *dev = devs[i];
2571 int id;
2573 if (type == ND_DEVICE_NAMESPACE_BLK) {
2574 struct nd_namespace_blk *nsblk;
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;
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;
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);
2596 if (i)
2597 nd_region->ns_seed = devs[0];
2599 if (devs[i]) {
2600 int j;
2602 for (j = i; devs[j]; j++) {
2603 struct device *dev = devs[j];
2605 device_initialize(dev);
2606 put_device(dev);
2608 *err = j - i;
2610 * All of the namespaces we tried to register failed, so
2611 * fail region activation.
2613 if (*err == 0)
2614 rc = -ENODEV;
2616 kfree(devs);
2618 if (rc == -ENODEV)
2619 return rc;
2621 return i;