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