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