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