]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/nvdimm/namespace_devs.c
libnvdimm: write pmem label set
[mirror_ubuntu-focal-kernel.git] / drivers / nvdimm / namespace_devs.c
CommitLineData
3d88002e
DW
1/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/slab.h>
16#include <linux/nd.h>
bf9bccc1 17#include "nd-core.h"
3d88002e
DW
18#include "nd.h"
19
20static void namespace_io_release(struct device *dev)
21{
22 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
23
24 kfree(nsio);
25}
26
bf9bccc1
DW
27static void namespace_pmem_release(struct device *dev)
28{
29 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
30
31 kfree(nspm->alt_name);
32 kfree(nspm->uuid);
33 kfree(nspm);
34}
35
36static void namespace_blk_release(struct device *dev)
37{
1b40e09a
DW
38 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
39 struct nd_region *nd_region = to_nd_region(dev->parent);
40
41 if (nsblk->id >= 0)
42 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
43 kfree(nsblk->alt_name);
44 kfree(nsblk->uuid);
45 kfree(nsblk->res);
46 kfree(nsblk);
bf9bccc1
DW
47}
48
3d88002e
DW
49static struct device_type namespace_io_device_type = {
50 .name = "nd_namespace_io",
51 .release = namespace_io_release,
52};
53
bf9bccc1
DW
54static struct device_type namespace_pmem_device_type = {
55 .name = "nd_namespace_pmem",
56 .release = namespace_pmem_release,
57};
58
59static struct device_type namespace_blk_device_type = {
60 .name = "nd_namespace_blk",
61 .release = namespace_blk_release,
62};
63
64static bool is_namespace_pmem(struct device *dev)
65{
66 return dev ? dev->type == &namespace_pmem_device_type : false;
67}
68
69static bool is_namespace_blk(struct device *dev)
70{
71 return dev ? dev->type == &namespace_blk_device_type : false;
72}
73
74static bool is_namespace_io(struct device *dev)
75{
76 return dev ? dev->type == &namespace_io_device_type : false;
77}
78
3d88002e
DW
79static ssize_t nstype_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
81{
82 struct nd_region *nd_region = to_nd_region(dev->parent);
83
84 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
85}
86static DEVICE_ATTR_RO(nstype);
87
bf9bccc1
DW
88static ssize_t __alt_name_store(struct device *dev, const char *buf,
89 const size_t len)
90{
91 char *input, *pos, *alt_name, **ns_altname;
92 ssize_t rc;
93
94 if (is_namespace_pmem(dev)) {
95 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
96
97 ns_altname = &nspm->alt_name;
98 } else if (is_namespace_blk(dev)) {
1b40e09a
DW
99 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
100
101 ns_altname = &nsblk->alt_name;
bf9bccc1
DW
102 } else
103 return -ENXIO;
104
105 if (dev->driver)
106 return -EBUSY;
107
108 input = kmemdup(buf, len + 1, GFP_KERNEL);
109 if (!input)
110 return -ENOMEM;
111
112 input[len] = '\0';
113 pos = strim(input);
114 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
115 rc = -EINVAL;
116 goto out;
117 }
118
119 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
120 if (!alt_name) {
121 rc = -ENOMEM;
122 goto out;
123 }
124 kfree(*ns_altname);
125 *ns_altname = alt_name;
126 sprintf(*ns_altname, "%s", pos);
127 rc = len;
128
129out:
130 kfree(input);
131 return rc;
132}
133
1b40e09a
DW
134static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
135{
136 struct nd_region *nd_region = to_nd_region(nsblk->dev.parent);
137 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
138 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
139 struct nd_label_id label_id;
140 resource_size_t size = 0;
141 struct resource *res;
142
143 if (!nsblk->uuid)
144 return 0;
145 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
146 for_each_dpa_resource(ndd, res)
147 if (strcmp(res->name, label_id.id) == 0)
148 size += resource_size(res);
149 return size;
150}
151
f524bf27
DW
152static int nd_namespace_label_update(struct nd_region *nd_region,
153 struct device *dev)
154{
155 dev_WARN_ONCE(dev, dev->driver,
156 "namespace must be idle during label update\n");
157 if (dev->driver)
158 return 0;
159
160 /*
161 * Only allow label writes that will result in a valid namespace
162 * or deletion of an existing namespace.
163 */
164 if (is_namespace_pmem(dev)) {
165 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
166 struct resource *res = &nspm->nsio.res;
167 resource_size_t size = resource_size(res);
168
169 if (size == 0 && nspm->uuid)
170 /* delete allocation */;
171 else if (!nspm->uuid)
172 return 0;
173
174 return nd_pmem_namespace_label_update(nd_region, nspm, size);
175 } else if (is_namespace_blk(dev)) {
176 /* TODO: implement blk labels */
177 return 0;
178 } else
179 return -ENXIO;
180}
181
bf9bccc1
DW
182static ssize_t alt_name_store(struct device *dev,
183 struct device_attribute *attr, const char *buf, size_t len)
184{
f524bf27 185 struct nd_region *nd_region = to_nd_region(dev->parent);
bf9bccc1
DW
186 ssize_t rc;
187
188 device_lock(dev);
189 nvdimm_bus_lock(dev);
190 wait_nvdimm_bus_probe_idle(dev);
191 rc = __alt_name_store(dev, buf, len);
f524bf27
DW
192 if (rc >= 0)
193 rc = nd_namespace_label_update(nd_region, dev);
bf9bccc1
DW
194 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
195 nvdimm_bus_unlock(dev);
196 device_unlock(dev);
197
f524bf27 198 return rc < 0 ? rc : len;
bf9bccc1
DW
199}
200
201static ssize_t alt_name_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
203{
204 char *ns_altname;
205
206 if (is_namespace_pmem(dev)) {
207 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
208
209 ns_altname = nspm->alt_name;
210 } else if (is_namespace_blk(dev)) {
1b40e09a
DW
211 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
212
213 ns_altname = nsblk->alt_name;
bf9bccc1
DW
214 } else
215 return -ENXIO;
216
217 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
218}
219static DEVICE_ATTR_RW(alt_name);
220
221static int scan_free(struct nd_region *nd_region,
222 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
223 resource_size_t n)
224{
225 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
226 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
227 int rc = 0;
228
229 while (n) {
230 struct resource *res, *last;
231 resource_size_t new_start;
232
233 last = NULL;
234 for_each_dpa_resource(ndd, res)
235 if (strcmp(res->name, label_id->id) == 0)
236 last = res;
237 res = last;
238 if (!res)
239 return 0;
240
241 if (n >= resource_size(res)) {
242 n -= resource_size(res);
243 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
244 nvdimm_free_dpa(ndd, res);
245 /* retry with last resource deleted */
246 continue;
247 }
248
249 /*
250 * Keep BLK allocations relegated to high DPA as much as
251 * possible
252 */
253 if (is_blk)
254 new_start = res->start + n;
255 else
256 new_start = res->start;
257
258 rc = adjust_resource(res, new_start, resource_size(res) - n);
1b40e09a
DW
259 if (rc == 0)
260 res->flags |= DPA_RESOURCE_ADJUSTED;
bf9bccc1
DW
261 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
262 break;
263 }
264
265 return rc;
266}
267
268/**
269 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
270 * @nd_region: the set of dimms to reclaim @n bytes from
271 * @label_id: unique identifier for the namespace consuming this dpa range
272 * @n: number of bytes per-dimm to release
273 *
274 * Assumes resources are ordered. Starting from the end try to
275 * adjust_resource() the allocation to @n, but if @n is larger than the
276 * allocation delete it and find the 'new' last allocation in the label
277 * set.
278 */
279static int shrink_dpa_allocation(struct nd_region *nd_region,
280 struct nd_label_id *label_id, resource_size_t n)
281{
282 int i;
283
284 for (i = 0; i < nd_region->ndr_mappings; i++) {
285 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
286 int rc;
287
288 rc = scan_free(nd_region, nd_mapping, label_id, n);
289 if (rc)
290 return rc;
291 }
292
293 return 0;
294}
295
296static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
297 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
298 resource_size_t n)
299{
300 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
301 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
302 resource_size_t first_dpa;
303 struct resource *res;
304 int rc = 0;
305
306 /* allocate blk from highest dpa first */
307 if (is_blk)
308 first_dpa = nd_mapping->start + nd_mapping->size - n;
309 else
310 first_dpa = nd_mapping->start;
311
312 /* first resource allocation for this label-id or dimm */
313 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
314 if (!res)
315 rc = -EBUSY;
316
317 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
318 return rc ? n : 0;
319}
320
1b40e09a
DW
321static bool space_valid(bool is_pmem, bool is_reserve,
322 struct nd_label_id *label_id, struct resource *res)
bf9bccc1
DW
323{
324 /*
325 * For BLK-space any space is valid, for PMEM-space, it must be
1b40e09a
DW
326 * contiguous with an existing allocation unless we are
327 * reserving pmem.
bf9bccc1 328 */
1b40e09a 329 if (is_reserve || !is_pmem)
bf9bccc1
DW
330 return true;
331 if (!res || strcmp(res->name, label_id->id) == 0)
332 return true;
333 return false;
334}
335
336enum alloc_loc {
337 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
338};
339
340static resource_size_t scan_allocate(struct nd_region *nd_region,
341 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
342 resource_size_t n)
343{
344 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
1b40e09a 345 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
bf9bccc1
DW
346 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
347 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
348 const resource_size_t to_allocate = n;
349 struct resource *res;
350 int first;
351
352 retry:
353 first = 0;
354 for_each_dpa_resource(ndd, res) {
355 resource_size_t allocate, available = 0, free_start, free_end;
356 struct resource *next = res->sibling, *new_res = NULL;
357 enum alloc_loc loc = ALLOC_ERR;
358 const char *action;
359 int rc = 0;
360
361 /* ignore resources outside this nd_mapping */
362 if (res->start > mapping_end)
363 continue;
364 if (res->end < nd_mapping->start)
365 continue;
366
367 /* space at the beginning of the mapping */
368 if (!first++ && res->start > nd_mapping->start) {
369 free_start = nd_mapping->start;
370 available = res->start - free_start;
1b40e09a 371 if (space_valid(is_pmem, is_reserve, label_id, NULL))
bf9bccc1
DW
372 loc = ALLOC_BEFORE;
373 }
374
375 /* space between allocations */
376 if (!loc && next) {
377 free_start = res->start + resource_size(res);
378 free_end = min(mapping_end, next->start - 1);
1b40e09a 379 if (space_valid(is_pmem, is_reserve, label_id, res)
bf9bccc1
DW
380 && free_start < free_end) {
381 available = free_end + 1 - free_start;
382 loc = ALLOC_MID;
383 }
384 }
385
386 /* space at the end of the mapping */
387 if (!loc && !next) {
388 free_start = res->start + resource_size(res);
389 free_end = mapping_end;
1b40e09a 390 if (space_valid(is_pmem, is_reserve, label_id, res)
bf9bccc1
DW
391 && free_start < free_end) {
392 available = free_end + 1 - free_start;
393 loc = ALLOC_AFTER;
394 }
395 }
396
397 if (!loc || !available)
398 continue;
399 allocate = min(available, n);
400 switch (loc) {
401 case ALLOC_BEFORE:
402 if (strcmp(res->name, label_id->id) == 0) {
403 /* adjust current resource up */
1b40e09a 404 if (is_pmem && !is_reserve)
bf9bccc1
DW
405 return n;
406 rc = adjust_resource(res, res->start - allocate,
407 resource_size(res) + allocate);
408 action = "cur grow up";
409 } else
410 action = "allocate";
411 break;
412 case ALLOC_MID:
413 if (strcmp(next->name, label_id->id) == 0) {
414 /* adjust next resource up */
1b40e09a 415 if (is_pmem && !is_reserve)
bf9bccc1
DW
416 return n;
417 rc = adjust_resource(next, next->start
418 - allocate, resource_size(next)
419 + allocate);
420 new_res = next;
421 action = "next grow up";
422 } else if (strcmp(res->name, label_id->id) == 0) {
423 action = "grow down";
424 } else
425 action = "allocate";
426 break;
427 case ALLOC_AFTER:
428 if (strcmp(res->name, label_id->id) == 0)
429 action = "grow down";
430 else
431 action = "allocate";
432 break;
433 default:
434 return n;
435 }
436
437 if (strcmp(action, "allocate") == 0) {
438 /* BLK allocate bottom up */
439 if (!is_pmem)
440 free_start += available - allocate;
1b40e09a 441 else if (!is_reserve && free_start != nd_mapping->start)
bf9bccc1
DW
442 return n;
443
444 new_res = nvdimm_allocate_dpa(ndd, label_id,
445 free_start, allocate);
446 if (!new_res)
447 rc = -EBUSY;
448 } else if (strcmp(action, "grow down") == 0) {
449 /* adjust current resource down */
450 rc = adjust_resource(res, res->start, resource_size(res)
451 + allocate);
1b40e09a
DW
452 if (rc == 0)
453 res->flags |= DPA_RESOURCE_ADJUSTED;
bf9bccc1
DW
454 }
455
456 if (!new_res)
457 new_res = res;
458
459 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
460 action, loc, rc);
461
462 if (rc)
463 return n;
464
465 n -= allocate;
466 if (n) {
467 /*
468 * Retry scan with newly inserted resources.
469 * For example, if we did an ALLOC_BEFORE
470 * insertion there may also have been space
471 * available for an ALLOC_AFTER insertion, so we
472 * need to check this same resource again
473 */
474 goto retry;
475 } else
476 return 0;
477 }
478
1b40e09a
DW
479 /*
480 * If we allocated nothing in the BLK case it may be because we are in
481 * an initial "pmem-reserve pass". Only do an initial BLK allocation
482 * when none of the DPA space is reserved.
483 */
484 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
bf9bccc1
DW
485 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
486 return n;
487}
488
1b40e09a
DW
489static int merge_dpa(struct nd_region *nd_region,
490 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
491{
492 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
493 struct resource *res;
494
495 if (strncmp("pmem", label_id->id, 4) == 0)
496 return 0;
497 retry:
498 for_each_dpa_resource(ndd, res) {
499 int rc;
500 struct resource *next = res->sibling;
501 resource_size_t end = res->start + resource_size(res);
502
503 if (!next || strcmp(res->name, label_id->id) != 0
504 || strcmp(next->name, label_id->id) != 0
505 || end != next->start)
506 continue;
507 end += resource_size(next);
508 nvdimm_free_dpa(ndd, next);
509 rc = adjust_resource(res, res->start, end - res->start);
510 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
511 if (rc)
512 return rc;
513 res->flags |= DPA_RESOURCE_ADJUSTED;
514 goto retry;
515 }
516
517 return 0;
518}
519
520static int __reserve_free_pmem(struct device *dev, void *data)
521{
522 struct nvdimm *nvdimm = data;
523 struct nd_region *nd_region;
524 struct nd_label_id label_id;
525 int i;
526
527 if (!is_nd_pmem(dev))
528 return 0;
529
530 nd_region = to_nd_region(dev);
531 if (nd_region->ndr_mappings == 0)
532 return 0;
533
534 memset(&label_id, 0, sizeof(label_id));
535 strcat(label_id.id, "pmem-reserve");
536 for (i = 0; i < nd_region->ndr_mappings; i++) {
537 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
538 resource_size_t n, rem = 0;
539
540 if (nd_mapping->nvdimm != nvdimm)
541 continue;
542
543 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
544 if (n == 0)
545 return 0;
546 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
547 dev_WARN_ONCE(&nd_region->dev, rem,
548 "pmem reserve underrun: %#llx of %#llx bytes\n",
549 (unsigned long long) n - rem,
550 (unsigned long long) n);
551 return rem ? -ENXIO : 0;
552 }
553
554 return 0;
555}
556
557static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
558 struct nd_mapping *nd_mapping)
559{
560 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
561 struct resource *res, *_res;
562
563 for_each_dpa_resource_safe(ndd, res, _res)
564 if (strcmp(res->name, "pmem-reserve") == 0)
565 nvdimm_free_dpa(ndd, res);
566}
567
568static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
569 struct nd_mapping *nd_mapping)
570{
571 struct nvdimm *nvdimm = nd_mapping->nvdimm;
572 int rc;
573
574 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
575 __reserve_free_pmem);
576 if (rc)
577 release_free_pmem(nvdimm_bus, nd_mapping);
578 return rc;
579}
580
bf9bccc1
DW
581/**
582 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
583 * @nd_region: the set of dimms to allocate @n more bytes from
584 * @label_id: unique identifier for the namespace consuming this dpa range
585 * @n: number of bytes per-dimm to add to the existing allocation
586 *
587 * Assumes resources are ordered. For BLK regions, first consume
588 * BLK-only available DPA free space, then consume PMEM-aliased DPA
589 * space starting at the highest DPA. For PMEM regions start
590 * allocations from the start of an interleave set and end at the first
591 * BLK allocation or the end of the interleave set, whichever comes
592 * first.
593 */
594static int grow_dpa_allocation(struct nd_region *nd_region,
595 struct nd_label_id *label_id, resource_size_t n)
596{
1b40e09a
DW
597 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
598 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
bf9bccc1
DW
599 int i;
600
601 for (i = 0; i < nd_region->ndr_mappings; i++) {
602 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1b40e09a
DW
603 resource_size_t rem = n;
604 int rc, j;
605
606 /*
607 * In the BLK case try once with all unallocated PMEM
608 * reserved, and once without
609 */
610 for (j = is_pmem; j < 2; j++) {
611 bool blk_only = j == 0;
612
613 if (blk_only) {
614 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
615 if (rc)
616 return rc;
617 }
618 rem = scan_allocate(nd_region, nd_mapping,
619 label_id, rem);
620 if (blk_only)
621 release_free_pmem(nvdimm_bus, nd_mapping);
bf9bccc1 622
1b40e09a
DW
623 /* try again and allow encroachments into PMEM */
624 if (rem == 0)
625 break;
626 }
627
628 dev_WARN_ONCE(&nd_region->dev, rem,
629 "allocation underrun: %#llx of %#llx bytes\n",
630 (unsigned long long) n - rem,
631 (unsigned long long) n);
632 if (rem)
633 return -ENXIO;
634
635 rc = merge_dpa(nd_region, nd_mapping, label_id);
bf9bccc1
DW
636 if (rc)
637 return rc;
638 }
639
640 return 0;
641}
642
643static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
644 struct nd_namespace_pmem *nspm, resource_size_t size)
645{
646 struct resource *res = &nspm->nsio.res;
647
648 res->start = nd_region->ndr_start;
649 res->end = nd_region->ndr_start + size - 1;
650}
651
652static ssize_t __size_store(struct device *dev, unsigned long long val)
653{
654 resource_size_t allocated = 0, available = 0;
655 struct nd_region *nd_region = to_nd_region(dev->parent);
656 struct nd_mapping *nd_mapping;
657 struct nvdimm_drvdata *ndd;
658 struct nd_label_id label_id;
659 u32 flags = 0, remainder;
660 u8 *uuid = NULL;
661 int rc, i;
662
663 if (dev->driver)
664 return -EBUSY;
665
666 if (is_namespace_pmem(dev)) {
667 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
668
669 uuid = nspm->uuid;
670 } else if (is_namespace_blk(dev)) {
1b40e09a
DW
671 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
672
673 uuid = nsblk->uuid;
674 flags = NSLABEL_FLAG_LOCAL;
bf9bccc1
DW
675 }
676
677 /*
678 * We need a uuid for the allocation-label and dimm(s) on which
679 * to store the label.
680 */
681 if (!uuid || nd_region->ndr_mappings == 0)
682 return -ENXIO;
683
684 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
685 if (remainder) {
686 dev_dbg(dev, "%llu is not %dK aligned\n", val,
687 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
688 return -EINVAL;
689 }
690
691 nd_label_gen_id(&label_id, uuid, flags);
692 for (i = 0; i < nd_region->ndr_mappings; i++) {
693 nd_mapping = &nd_region->mapping[i];
694 ndd = to_ndd(nd_mapping);
695
696 /*
697 * All dimms in an interleave set, or the base dimm for a blk
698 * region, need to be enabled for the size to be changed.
699 */
700 if (!ndd)
701 return -ENXIO;
702
703 allocated += nvdimm_allocated_dpa(ndd, &label_id);
704 }
705 available = nd_region_available_dpa(nd_region);
706
707 if (val > available + allocated)
708 return -ENOSPC;
709
710 if (val == allocated)
711 return 0;
712
713 val = div_u64(val, nd_region->ndr_mappings);
714 allocated = div_u64(allocated, nd_region->ndr_mappings);
715 if (val < allocated)
716 rc = shrink_dpa_allocation(nd_region, &label_id,
717 allocated - val);
718 else
719 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
720
721 if (rc)
722 return rc;
723
724 if (is_namespace_pmem(dev)) {
725 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
726
727 nd_namespace_pmem_set_size(nd_region, nspm,
728 val * nd_region->ndr_mappings);
1b40e09a
DW
729 } else if (is_namespace_blk(dev)) {
730 /*
731 * Try to delete the namespace if we deleted all of its
732 * allocation and this is not the seed device for the
733 * region.
734 */
735 if (val == 0 && nd_region->ns_seed != dev)
736 nd_device_unregister(dev, ND_ASYNC);
bf9bccc1
DW
737 }
738
739 return rc;
740}
741
742static ssize_t size_store(struct device *dev,
743 struct device_attribute *attr, const char *buf, size_t len)
744{
f524bf27 745 struct nd_region *nd_region = to_nd_region(dev->parent);
bf9bccc1
DW
746 unsigned long long val;
747 u8 **uuid = NULL;
748 int rc;
749
750 rc = kstrtoull(buf, 0, &val);
751 if (rc)
752 return rc;
753
754 device_lock(dev);
755 nvdimm_bus_lock(dev);
756 wait_nvdimm_bus_probe_idle(dev);
757 rc = __size_store(dev, val);
f524bf27
DW
758 if (rc >= 0)
759 rc = nd_namespace_label_update(nd_region, dev);
bf9bccc1
DW
760
761 if (is_namespace_pmem(dev)) {
762 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
763
764 uuid = &nspm->uuid;
765 } else if (is_namespace_blk(dev)) {
1b40e09a
DW
766 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
767
768 uuid = &nsblk->uuid;
bf9bccc1
DW
769 }
770
771 if (rc == 0 && val == 0 && uuid) {
772 /* setting size zero == 'delete namespace' */
773 kfree(*uuid);
774 *uuid = NULL;
775 }
776
777 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
778 ? "fail" : "success", rc);
779
780 nvdimm_bus_unlock(dev);
781 device_unlock(dev);
782
f524bf27 783 return rc < 0 ? rc : len;
bf9bccc1
DW
784}
785
786static ssize_t size_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
788{
1b40e09a
DW
789 unsigned long long size = 0;
790
791 nvdimm_bus_lock(dev);
bf9bccc1
DW
792 if (is_namespace_pmem(dev)) {
793 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
794
1b40e09a 795 size = resource_size(&nspm->nsio.res);
bf9bccc1 796 } else if (is_namespace_blk(dev)) {
1b40e09a 797 size = nd_namespace_blk_size(to_nd_namespace_blk(dev));
bf9bccc1
DW
798 } else if (is_namespace_io(dev)) {
799 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
800
1b40e09a
DW
801 size = resource_size(&nsio->res);
802 }
803 nvdimm_bus_unlock(dev);
804
805 return sprintf(buf, "%llu\n", size);
bf9bccc1
DW
806}
807static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
808
809static ssize_t uuid_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
811{
812 u8 *uuid;
813
814 if (is_namespace_pmem(dev)) {
815 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
816
817 uuid = nspm->uuid;
818 } else if (is_namespace_blk(dev)) {
1b40e09a
DW
819 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
820
821 uuid = nsblk->uuid;
bf9bccc1
DW
822 } else
823 return -ENXIO;
824
825 if (uuid)
826 return sprintf(buf, "%pUb\n", uuid);
827 return sprintf(buf, "\n");
828}
829
830/**
831 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
832 * @nd_region: parent region so we can updates all dimms in the set
833 * @dev: namespace type for generating label_id
834 * @new_uuid: incoming uuid
835 * @old_uuid: reference to the uuid storage location in the namespace object
836 */
837static int namespace_update_uuid(struct nd_region *nd_region,
838 struct device *dev, u8 *new_uuid, u8 **old_uuid)
839{
840 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
841 struct nd_label_id old_label_id;
842 struct nd_label_id new_label_id;
f524bf27 843 int i;
bf9bccc1 844
f524bf27
DW
845 if (!nd_is_uuid_unique(dev, new_uuid))
846 return -EINVAL;
bf9bccc1
DW
847
848 if (*old_uuid == NULL)
849 goto out;
850
f524bf27
DW
851 /*
852 * If we've already written a label with this uuid, then it's
853 * too late to rename because we can't reliably update the uuid
854 * without losing the old namespace. Userspace must delete this
855 * namespace to abandon the old uuid.
856 */
857 for (i = 0; i < nd_region->ndr_mappings; i++) {
858 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
859
860 /*
861 * This check by itself is sufficient because old_uuid
862 * would be NULL above if this uuid did not exist in the
863 * currently written set.
864 *
865 * FIXME: can we delete uuid with zero dpa allocated?
866 */
867 if (nd_mapping->labels)
868 return -EBUSY;
869 }
870
bf9bccc1
DW
871 nd_label_gen_id(&old_label_id, *old_uuid, flags);
872 nd_label_gen_id(&new_label_id, new_uuid, flags);
873 for (i = 0; i < nd_region->ndr_mappings; i++) {
874 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
875 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
876 struct resource *res;
877
878 for_each_dpa_resource(ndd, res)
879 if (strcmp(res->name, old_label_id.id) == 0)
880 sprintf((void *) res->name, "%s",
881 new_label_id.id);
882 }
883 kfree(*old_uuid);
884 out:
885 *old_uuid = new_uuid;
886 return 0;
887}
888
889static ssize_t uuid_store(struct device *dev,
890 struct device_attribute *attr, const char *buf, size_t len)
891{
892 struct nd_region *nd_region = to_nd_region(dev->parent);
893 u8 *uuid = NULL;
894 u8 **ns_uuid;
895 ssize_t rc;
896
897 if (is_namespace_pmem(dev)) {
898 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
899
900 ns_uuid = &nspm->uuid;
901 } else if (is_namespace_blk(dev)) {
1b40e09a
DW
902 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
903
904 ns_uuid = &nsblk->uuid;
bf9bccc1
DW
905 } else
906 return -ENXIO;
907
908 device_lock(dev);
909 nvdimm_bus_lock(dev);
910 wait_nvdimm_bus_probe_idle(dev);
911 rc = nd_uuid_store(dev, &uuid, buf, len);
912 if (rc >= 0)
913 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
f524bf27
DW
914 if (rc >= 0)
915 rc = nd_namespace_label_update(nd_region, dev);
916 else
917 kfree(uuid);
bf9bccc1
DW
918 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
919 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
920 nvdimm_bus_unlock(dev);
921 device_unlock(dev);
922
f524bf27 923 return rc < 0 ? rc : len;
bf9bccc1
DW
924}
925static DEVICE_ATTR_RW(uuid);
926
927static ssize_t resource_show(struct device *dev,
928 struct device_attribute *attr, char *buf)
929{
930 struct resource *res;
931
932 if (is_namespace_pmem(dev)) {
933 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
934
935 res = &nspm->nsio.res;
936 } else if (is_namespace_io(dev)) {
937 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
938
939 res = &nsio->res;
940 } else
941 return -ENXIO;
942
943 /* no address to convey if the namespace has no allocation */
944 if (resource_size(res) == 0)
945 return -ENXIO;
946 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
947}
948static DEVICE_ATTR_RO(resource);
949
1b40e09a
DW
950static const unsigned long ns_lbasize_supported[] = { 512, 0 };
951
952static ssize_t sector_size_show(struct device *dev,
953 struct device_attribute *attr, char *buf)
954{
955 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
956
957 if (!is_namespace_blk(dev))
958 return -ENXIO;
959
960 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
961}
962
963static ssize_t sector_size_store(struct device *dev,
964 struct device_attribute *attr, const char *buf, size_t len)
965{
966 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
f524bf27 967 struct nd_region *nd_region = to_nd_region(dev->parent);
1b40e09a
DW
968 ssize_t rc;
969
970 if (!is_namespace_blk(dev))
971 return -ENXIO;
972
973 device_lock(dev);
974 nvdimm_bus_lock(dev);
975 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
976 ns_lbasize_supported);
f524bf27
DW
977 if (rc >= 0)
978 rc = nd_namespace_label_update(nd_region, dev);
979 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
980 rc, rc < 0 ? "tried" : "wrote", buf,
981 buf[len - 1] == '\n' ? "" : "\n");
1b40e09a
DW
982 nvdimm_bus_unlock(dev);
983 device_unlock(dev);
984
985 return rc ? rc : len;
986}
987static DEVICE_ATTR_RW(sector_size);
988
3d88002e
DW
989static struct attribute *nd_namespace_attributes[] = {
990 &dev_attr_nstype.attr,
bf9bccc1
DW
991 &dev_attr_size.attr,
992 &dev_attr_uuid.attr,
993 &dev_attr_resource.attr,
994 &dev_attr_alt_name.attr,
1b40e09a 995 &dev_attr_sector_size.attr,
3d88002e
DW
996 NULL,
997};
998
bf9bccc1
DW
999static umode_t namespace_visible(struct kobject *kobj,
1000 struct attribute *a, int n)
1001{
1002 struct device *dev = container_of(kobj, struct device, kobj);
1003
1004 if (a == &dev_attr_resource.attr) {
1005 if (is_namespace_blk(dev))
1006 return 0;
1007 return a->mode;
1008 }
1009
1010 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1011 if (a == &dev_attr_size.attr)
1012 return S_IWUSR | S_IRUGO;
1b40e09a
DW
1013
1014 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1015 return 0;
1016
bf9bccc1
DW
1017 return a->mode;
1018 }
1019
1020 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr)
1021 return a->mode;
1022
1023 return 0;
1024}
1025
3d88002e
DW
1026static struct attribute_group nd_namespace_attribute_group = {
1027 .attrs = nd_namespace_attributes,
bf9bccc1 1028 .is_visible = namespace_visible,
3d88002e
DW
1029};
1030
1031static const struct attribute_group *nd_namespace_attribute_groups[] = {
1032 &nd_device_attribute_group,
1033 &nd_namespace_attribute_group,
1034 NULL,
1035};
1036
1037static struct device **create_namespace_io(struct nd_region *nd_region)
1038{
1039 struct nd_namespace_io *nsio;
1040 struct device *dev, **devs;
1041 struct resource *res;
1042
1043 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1044 if (!nsio)
1045 return NULL;
1046
1047 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1048 if (!devs) {
1049 kfree(nsio);
1050 return NULL;
1051 }
1052
1053 dev = &nsio->dev;
1054 dev->type = &namespace_io_device_type;
1055 dev->parent = &nd_region->dev;
1056 res = &nsio->res;
1057 res->name = dev_name(&nd_region->dev);
1058 res->flags = IORESOURCE_MEM;
1059 res->start = nd_region->ndr_start;
1060 res->end = res->start + nd_region->ndr_size - 1;
1061
1062 devs[0] = dev;
1063 return devs;
1064}
1065
bf9bccc1
DW
1066static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1067 u64 cookie, u16 pos)
1068{
1069 struct nd_namespace_label *found = NULL;
1070 int i;
1071
1072 for (i = 0; i < nd_region->ndr_mappings; i++) {
1073 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1074 struct nd_namespace_label *nd_label;
1075 bool found_uuid = false;
1076 int l;
1077
1078 for_each_label(l, nd_label, nd_mapping->labels) {
1079 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1080 u16 position = __le16_to_cpu(nd_label->position);
1081 u16 nlabel = __le16_to_cpu(nd_label->nlabel);
1082
1083 if (isetcookie != cookie)
1084 continue;
1085
1086 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1087 continue;
1088
1089 if (found_uuid) {
1090 dev_dbg(to_ndd(nd_mapping)->dev,
1091 "%s duplicate entry for uuid\n",
1092 __func__);
1093 return false;
1094 }
1095 found_uuid = true;
1096 if (nlabel != nd_region->ndr_mappings)
1097 continue;
1098 if (position != pos)
1099 continue;
1100 found = nd_label;
1101 break;
1102 }
1103 if (found)
1104 break;
1105 }
1106 return found != NULL;
1107}
1108
1109static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1110{
1111 struct nd_namespace_label *select = NULL;
1112 int i;
1113
1114 if (!pmem_id)
1115 return -ENODEV;
1116
1117 for (i = 0; i < nd_region->ndr_mappings; i++) {
1118 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1119 struct nd_namespace_label *nd_label;
1120 u64 hw_start, hw_end, pmem_start, pmem_end;
1121 int l;
1122
1123 for_each_label(l, nd_label, nd_mapping->labels)
1124 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1125 break;
1126
1127 if (!nd_label) {
1128 WARN_ON(1);
1129 return -EINVAL;
1130 }
1131
1132 select = nd_label;
1133 /*
1134 * Check that this label is compliant with the dpa
1135 * range published in NFIT
1136 */
1137 hw_start = nd_mapping->start;
1138 hw_end = hw_start + nd_mapping->size;
1139 pmem_start = __le64_to_cpu(select->dpa);
1140 pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
1141 if (pmem_start == hw_start && pmem_end <= hw_end)
1142 /* pass */;
1143 else
1144 return -EINVAL;
1145
1146 nd_mapping->labels[0] = select;
1147 nd_mapping->labels[1] = NULL;
1148 }
1149 return 0;
1150}
1151
1152/**
1153 * find_pmem_label_set - validate interleave set labelling, retrieve label0
1154 * @nd_region: region with mappings to validate
1155 */
1156static int find_pmem_label_set(struct nd_region *nd_region,
1157 struct nd_namespace_pmem *nspm)
1158{
1159 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1160 struct nd_namespace_label *nd_label;
1161 u8 select_id[NSLABEL_UUID_LEN];
1162 resource_size_t size = 0;
1163 u8 *pmem_id = NULL;
1164 int rc = -ENODEV, l;
1165 u16 i;
1166
1167 if (cookie == 0)
1168 return -ENXIO;
1169
1170 /*
1171 * Find a complete set of labels by uuid. By definition we can start
1172 * with any mapping as the reference label
1173 */
1174 for_each_label(l, nd_label, nd_region->mapping[0].labels) {
1175 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1176
1177 if (isetcookie != cookie)
1178 continue;
1179
1180 for (i = 0; nd_region->ndr_mappings; i++)
1181 if (!has_uuid_at_pos(nd_region, nd_label->uuid,
1182 cookie, i))
1183 break;
1184 if (i < nd_region->ndr_mappings) {
1185 /*
1186 * Give up if we don't find an instance of a
1187 * uuid at each position (from 0 to
1188 * nd_region->ndr_mappings - 1), or if we find a
1189 * dimm with two instances of the same uuid.
1190 */
1191 rc = -EINVAL;
1192 goto err;
1193 } else if (pmem_id) {
1194 /*
1195 * If there is more than one valid uuid set, we
1196 * need userspace to clean this up.
1197 */
1198 rc = -EBUSY;
1199 goto err;
1200 }
1201 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
1202 pmem_id = select_id;
1203 }
1204
1205 /*
1206 * Fix up each mapping's 'labels' to have the validated pmem label for
1207 * that position at labels[0], and NULL at labels[1]. In the process,
1208 * check that the namespace aligns with interleave-set. We know
1209 * that it does not overlap with any blk namespaces by virtue of
1210 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1211 * succeeded).
1212 */
1213 rc = select_pmem_id(nd_region, pmem_id);
1214 if (rc)
1215 goto err;
1216
1217 /* Calculate total size and populate namespace properties from label0 */
1218 for (i = 0; i < nd_region->ndr_mappings; i++) {
1219 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1220 struct nd_namespace_label *label0 = nd_mapping->labels[0];
1221
1222 size += __le64_to_cpu(label0->rawsize);
1223 if (__le16_to_cpu(label0->position) != 0)
1224 continue;
1225 WARN_ON(nspm->alt_name || nspm->uuid);
1226 nspm->alt_name = kmemdup((void __force *) label0->name,
1227 NSLABEL_NAME_LEN, GFP_KERNEL);
1228 nspm->uuid = kmemdup((void __force *) label0->uuid,
1229 NSLABEL_UUID_LEN, GFP_KERNEL);
1230 }
1231
1232 if (!nspm->alt_name || !nspm->uuid) {
1233 rc = -ENOMEM;
1234 goto err;
1235 }
1236
1237 nd_namespace_pmem_set_size(nd_region, nspm, size);
1238
1239 return 0;
1240 err:
1241 switch (rc) {
1242 case -EINVAL:
1243 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1244 break;
1245 case -ENODEV:
1246 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1247 break;
1248 default:
1249 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1250 __func__, rc);
1251 break;
1252 }
1253 return rc;
1254}
1255
1256static struct device **create_namespace_pmem(struct nd_region *nd_region)
1257{
1258 struct nd_namespace_pmem *nspm;
1259 struct device *dev, **devs;
1260 struct resource *res;
1261 int rc;
1262
1263 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1264 if (!nspm)
1265 return NULL;
1266
1267 dev = &nspm->nsio.dev;
1268 dev->type = &namespace_pmem_device_type;
1269 dev->parent = &nd_region->dev;
1270 res = &nspm->nsio.res;
1271 res->name = dev_name(&nd_region->dev);
1272 res->flags = IORESOURCE_MEM;
1273 rc = find_pmem_label_set(nd_region, nspm);
1274 if (rc == -ENODEV) {
1275 int i;
1276
1277 /* Pass, try to permit namespace creation... */
1278 for (i = 0; i < nd_region->ndr_mappings; i++) {
1279 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1280
1281 kfree(nd_mapping->labels);
1282 nd_mapping->labels = NULL;
1283 }
1284
1285 /* Publish a zero-sized namespace for userspace to configure. */
1286 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1287
1288 rc = 0;
1289 } else if (rc)
1290 goto err;
1291
1292 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1293 if (!devs)
1294 goto err;
1295
1296 devs[0] = dev;
1297 return devs;
1298
1299 err:
1300 namespace_pmem_release(&nspm->nsio.dev);
1301 return NULL;
1302}
1303
1b40e09a
DW
1304struct resource *nsblk_add_resource(struct nd_region *nd_region,
1305 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1306 resource_size_t start)
1307{
1308 struct nd_label_id label_id;
1309 struct resource *res;
1310
1311 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1312 res = krealloc(nsblk->res,
1313 sizeof(void *) * (nsblk->num_resources + 1),
1314 GFP_KERNEL);
1315 if (!res)
1316 return NULL;
1317 nsblk->res = (struct resource **) res;
1318 for_each_dpa_resource(ndd, res)
1319 if (strcmp(res->name, label_id.id) == 0
1320 && res->start == start) {
1321 nsblk->res[nsblk->num_resources++] = res;
1322 return res;
1323 }
1324 return NULL;
1325}
1326
1327static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1328{
1329 struct nd_namespace_blk *nsblk;
1330 struct device *dev;
1331
1332 if (!is_nd_blk(&nd_region->dev))
1333 return NULL;
1334
1335 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1336 if (!nsblk)
1337 return NULL;
1338
1339 dev = &nsblk->dev;
1340 dev->type = &namespace_blk_device_type;
1341 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1342 if (nsblk->id < 0) {
1343 kfree(nsblk);
1344 return NULL;
1345 }
1346 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1347 dev->parent = &nd_region->dev;
1348 dev->groups = nd_namespace_attribute_groups;
1349
1350 return &nsblk->dev;
1351}
1352
1353void nd_region_create_blk_seed(struct nd_region *nd_region)
1354{
1355 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1356 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1357 /*
1358 * Seed creation failures are not fatal, provisioning is simply
1359 * disabled until memory becomes available
1360 */
1361 if (!nd_region->ns_seed)
1362 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1363 else
1364 nd_device_register(nd_region->ns_seed);
1365}
1366
1367static struct device **create_namespace_blk(struct nd_region *nd_region)
1368{
1369 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1370 struct nd_namespace_label *nd_label;
1371 struct device *dev, **devs = NULL;
1372 struct nd_namespace_blk *nsblk;
1373 struct nvdimm_drvdata *ndd;
1374 int i, l, count = 0;
1375 struct resource *res;
1376
1377 if (nd_region->ndr_mappings == 0)
1378 return NULL;
1379
1380 ndd = to_ndd(nd_mapping);
1381 for_each_label(l, nd_label, nd_mapping->labels) {
1382 u32 flags = __le32_to_cpu(nd_label->flags);
1383 char *name[NSLABEL_NAME_LEN];
1384 struct device **__devs;
1385
1386 if (flags & NSLABEL_FLAG_LOCAL)
1387 /* pass */;
1388 else
1389 continue;
1390
1391 for (i = 0; i < count; i++) {
1392 nsblk = to_nd_namespace_blk(devs[i]);
1393 if (memcmp(nsblk->uuid, nd_label->uuid,
1394 NSLABEL_UUID_LEN) == 0) {
1395 res = nsblk_add_resource(nd_region, ndd, nsblk,
1396 __le64_to_cpu(nd_label->dpa));
1397 if (!res)
1398 goto err;
1399 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
1400 dev_name(&nsblk->dev));
1401 break;
1402 }
1403 }
1404 if (i < count)
1405 continue;
1406 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1407 if (!__devs)
1408 goto err;
1409 memcpy(__devs, devs, sizeof(dev) * count);
1410 kfree(devs);
1411 devs = __devs;
1412
1413 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1414 if (!nsblk)
1415 goto err;
1416 dev = &nsblk->dev;
1417 dev->type = &namespace_blk_device_type;
1418 dev->parent = &nd_region->dev;
1419 dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
1420 devs[count++] = dev;
1421 nsblk->id = -1;
1422 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1423 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1424 GFP_KERNEL);
1425 if (!nsblk->uuid)
1426 goto err;
1427 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1428 if (name[0])
1429 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1430 GFP_KERNEL);
1431 res = nsblk_add_resource(nd_region, ndd, nsblk,
1432 __le64_to_cpu(nd_label->dpa));
1433 if (!res)
1434 goto err;
1435 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
1436 dev_name(&nsblk->dev));
1437 }
1438
1439 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
1440 __func__, count, count == 1 ? "" : "s");
1441
1442 if (count == 0) {
1443 /* Publish a zero-sized namespace for userspace to configure. */
1444 for (i = 0; i < nd_region->ndr_mappings; i++) {
1445 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1446
1447 kfree(nd_mapping->labels);
1448 nd_mapping->labels = NULL;
1449 }
1450
1451 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1452 if (!devs)
1453 goto err;
1454 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1455 if (!nsblk)
1456 goto err;
1457 dev = &nsblk->dev;
1458 dev->type = &namespace_blk_device_type;
1459 dev->parent = &nd_region->dev;
1460 devs[count++] = dev;
1461 }
1462
1463 return devs;
1464
1465err:
1466 for (i = 0; i < count; i++) {
1467 nsblk = to_nd_namespace_blk(devs[i]);
1468 namespace_blk_release(&nsblk->dev);
1469 }
1470 kfree(devs);
1471 return NULL;
1472}
1473
bf9bccc1
DW
1474static int init_active_labels(struct nd_region *nd_region)
1475{
1476 int i;
1477
1478 for (i = 0; i < nd_region->ndr_mappings; i++) {
1479 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1480 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1481 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1482 int count, j;
1483
1484 /*
1485 * If the dimm is disabled then prevent the region from
1486 * being activated if it aliases DPA.
1487 */
1488 if (!ndd) {
1489 if ((nvdimm->flags & NDD_ALIASING) == 0)
1490 return 0;
1491 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
1492 dev_name(&nd_mapping->nvdimm->dev));
1493 return -ENXIO;
1494 }
1495 nd_mapping->ndd = ndd;
1496 atomic_inc(&nvdimm->busy);
1497 get_ndd(ndd);
1498
1499 count = nd_label_active_count(ndd);
1500 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
1501 if (!count)
1502 continue;
1503 nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
1504 GFP_KERNEL);
1505 if (!nd_mapping->labels)
1506 return -ENOMEM;
1507 for (j = 0; j < count; j++) {
1508 struct nd_namespace_label *label;
1509
1510 label = nd_label_active(ndd, j);
1511 nd_mapping->labels[j] = label;
1512 }
1513 }
1514
1515 return 0;
1516}
1517
3d88002e
DW
1518int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
1519{
1520 struct device **devs = NULL;
bf9bccc1 1521 int i, rc = 0, type;
3d88002e
DW
1522
1523 *err = 0;
bf9bccc1
DW
1524 nvdimm_bus_lock(&nd_region->dev);
1525 rc = init_active_labels(nd_region);
1526 if (rc) {
1527 nvdimm_bus_unlock(&nd_region->dev);
1528 return rc;
1529 }
1530
1531 type = nd_region_to_nstype(nd_region);
1532 switch (type) {
3d88002e
DW
1533 case ND_DEVICE_NAMESPACE_IO:
1534 devs = create_namespace_io(nd_region);
1535 break;
bf9bccc1
DW
1536 case ND_DEVICE_NAMESPACE_PMEM:
1537 devs = create_namespace_pmem(nd_region);
1538 break;
1b40e09a
DW
1539 case ND_DEVICE_NAMESPACE_BLK:
1540 devs = create_namespace_blk(nd_region);
1541 break;
3d88002e
DW
1542 default:
1543 break;
1544 }
bf9bccc1 1545 nvdimm_bus_unlock(&nd_region->dev);
3d88002e
DW
1546
1547 if (!devs)
1548 return -ENODEV;
1549
1550 for (i = 0; devs[i]; i++) {
1551 struct device *dev = devs[i];
1b40e09a 1552 int id;
3d88002e 1553
1b40e09a
DW
1554 if (type == ND_DEVICE_NAMESPACE_BLK) {
1555 struct nd_namespace_blk *nsblk;
1556
1557 nsblk = to_nd_namespace_blk(dev);
1558 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
1559 GFP_KERNEL);
1560 nsblk->id = id;
1561 } else
1562 id = i;
1563
1564 if (id < 0)
1565 break;
1566 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
3d88002e
DW
1567 dev->groups = nd_namespace_attribute_groups;
1568 nd_device_register(dev);
1569 }
1b40e09a
DW
1570 if (i)
1571 nd_region->ns_seed = devs[0];
1572
1573 if (devs[i]) {
1574 int j;
1575
1576 for (j = i; devs[j]; j++) {
1577 struct device *dev = devs[j];
1578
1579 device_initialize(dev);
1580 put_device(dev);
1581 }
1582 *err = j - i;
1583 /*
1584 * All of the namespaces we tried to register failed, so
1585 * fail region activation.
1586 */
1587 if (*err == 0)
1588 rc = -ENODEV;
1589 }
3d88002e
DW
1590 kfree(devs);
1591
1b40e09a
DW
1592 if (rc == -ENODEV)
1593 return rc;
1594
3d88002e
DW
1595 return i;
1596}