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