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