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