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