blob: 2c50a0719f8d5a4153ff64e3a75b11d3e87c03c0 [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
Dan Williams8c2f7e82015-06-25 04:20:04 -0400105 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400106 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{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400136 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400137 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 Williamsf524bf22015-05-30 12:36:02 -0400152static int nd_namespace_label_update(struct nd_region *nd_region,
153 struct device *dev)
154{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400155 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400156 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400157 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400158 return 0;
159
160 /*
161 * Only allow label writes that will result in a valid namespace
162 * or deletion of an existing namespace.
163 */
164 if (is_namespace_pmem(dev)) {
165 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400166 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400167
168 if (size == 0 && nspm->uuid)
169 /* delete allocation */;
170 else if (!nspm->uuid)
171 return 0;
172
173 return nd_pmem_namespace_label_update(nd_region, nspm, size);
174 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400175 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
176 resource_size_t size = nd_namespace_blk_size(nsblk);
177
178 if (size == 0 && nsblk->uuid)
179 /* delete allocation */;
180 else if (!nsblk->uuid || !nsblk->lbasize)
181 return 0;
182
183 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400184 } else
185 return -ENXIO;
186}
187
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400188static ssize_t alt_name_store(struct device *dev,
189 struct device_attribute *attr, const char *buf, size_t len)
190{
Dan Williamsf524bf22015-05-30 12:36:02 -0400191 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400192 ssize_t rc;
193
194 device_lock(dev);
195 nvdimm_bus_lock(dev);
196 wait_nvdimm_bus_probe_idle(dev);
197 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400198 if (rc >= 0)
199 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400200 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
201 nvdimm_bus_unlock(dev);
202 device_unlock(dev);
203
Dan Williamsf524bf22015-05-30 12:36:02 -0400204 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400205}
206
207static ssize_t alt_name_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 char *ns_altname;
211
212 if (is_namespace_pmem(dev)) {
213 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
214
215 ns_altname = nspm->alt_name;
216 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400217 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
218
219 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400220 } else
221 return -ENXIO;
222
223 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
224}
225static DEVICE_ATTR_RW(alt_name);
226
227static int scan_free(struct nd_region *nd_region,
228 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
229 resource_size_t n)
230{
231 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
232 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
233 int rc = 0;
234
235 while (n) {
236 struct resource *res, *last;
237 resource_size_t new_start;
238
239 last = NULL;
240 for_each_dpa_resource(ndd, res)
241 if (strcmp(res->name, label_id->id) == 0)
242 last = res;
243 res = last;
244 if (!res)
245 return 0;
246
247 if (n >= resource_size(res)) {
248 n -= resource_size(res);
249 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
250 nvdimm_free_dpa(ndd, res);
251 /* retry with last resource deleted */
252 continue;
253 }
254
255 /*
256 * Keep BLK allocations relegated to high DPA as much as
257 * possible
258 */
259 if (is_blk)
260 new_start = res->start + n;
261 else
262 new_start = res->start;
263
264 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400265 if (rc == 0)
266 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400267 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
268 break;
269 }
270
271 return rc;
272}
273
274/**
275 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
276 * @nd_region: the set of dimms to reclaim @n bytes from
277 * @label_id: unique identifier for the namespace consuming this dpa range
278 * @n: number of bytes per-dimm to release
279 *
280 * Assumes resources are ordered. Starting from the end try to
281 * adjust_resource() the allocation to @n, but if @n is larger than the
282 * allocation delete it and find the 'new' last allocation in the label
283 * set.
284 */
285static int shrink_dpa_allocation(struct nd_region *nd_region,
286 struct nd_label_id *label_id, resource_size_t n)
287{
288 int i;
289
290 for (i = 0; i < nd_region->ndr_mappings; i++) {
291 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
292 int rc;
293
294 rc = scan_free(nd_region, nd_mapping, label_id, n);
295 if (rc)
296 return rc;
297 }
298
299 return 0;
300}
301
302static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
303 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
304 resource_size_t n)
305{
306 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
307 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
308 resource_size_t first_dpa;
309 struct resource *res;
310 int rc = 0;
311
312 /* allocate blk from highest dpa first */
313 if (is_blk)
314 first_dpa = nd_mapping->start + nd_mapping->size - n;
315 else
316 first_dpa = nd_mapping->start;
317
318 /* first resource allocation for this label-id or dimm */
319 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
320 if (!res)
321 rc = -EBUSY;
322
323 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
324 return rc ? n : 0;
325}
326
Dan Williams1b40e092015-05-01 13:34:01 -0400327static bool space_valid(bool is_pmem, bool is_reserve,
328 struct nd_label_id *label_id, struct resource *res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400329{
330 /*
331 * For BLK-space any space is valid, for PMEM-space, it must be
Dan Williams1b40e092015-05-01 13:34:01 -0400332 * contiguous with an existing allocation unless we are
333 * reserving pmem.
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400334 */
Dan Williams1b40e092015-05-01 13:34:01 -0400335 if (is_reserve || !is_pmem)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400336 return true;
337 if (!res || strcmp(res->name, label_id->id) == 0)
338 return true;
339 return false;
340}
341
342enum alloc_loc {
343 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
344};
345
346static resource_size_t scan_allocate(struct nd_region *nd_region,
347 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
348 resource_size_t n)
349{
350 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -0400351 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400352 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
353 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
354 const resource_size_t to_allocate = n;
355 struct resource *res;
356 int first;
357
358 retry:
359 first = 0;
360 for_each_dpa_resource(ndd, res) {
361 resource_size_t allocate, available = 0, free_start, free_end;
362 struct resource *next = res->sibling, *new_res = NULL;
363 enum alloc_loc loc = ALLOC_ERR;
364 const char *action;
365 int rc = 0;
366
367 /* ignore resources outside this nd_mapping */
368 if (res->start > mapping_end)
369 continue;
370 if (res->end < nd_mapping->start)
371 continue;
372
373 /* space at the beginning of the mapping */
374 if (!first++ && res->start > nd_mapping->start) {
375 free_start = nd_mapping->start;
376 available = res->start - free_start;
Dan Williams1b40e092015-05-01 13:34:01 -0400377 if (space_valid(is_pmem, is_reserve, label_id, NULL))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400378 loc = ALLOC_BEFORE;
379 }
380
381 /* space between allocations */
382 if (!loc && next) {
383 free_start = res->start + resource_size(res);
384 free_end = min(mapping_end, next->start - 1);
Dan Williams1b40e092015-05-01 13:34:01 -0400385 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400386 && free_start < free_end) {
387 available = free_end + 1 - free_start;
388 loc = ALLOC_MID;
389 }
390 }
391
392 /* space at the end of the mapping */
393 if (!loc && !next) {
394 free_start = res->start + resource_size(res);
395 free_end = mapping_end;
Dan Williams1b40e092015-05-01 13:34:01 -0400396 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400397 && free_start < free_end) {
398 available = free_end + 1 - free_start;
399 loc = ALLOC_AFTER;
400 }
401 }
402
403 if (!loc || !available)
404 continue;
405 allocate = min(available, n);
406 switch (loc) {
407 case ALLOC_BEFORE:
408 if (strcmp(res->name, label_id->id) == 0) {
409 /* adjust current resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400410 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400411 return n;
412 rc = adjust_resource(res, res->start - allocate,
413 resource_size(res) + allocate);
414 action = "cur grow up";
415 } else
416 action = "allocate";
417 break;
418 case ALLOC_MID:
419 if (strcmp(next->name, label_id->id) == 0) {
420 /* adjust next resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400421 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400422 return n;
423 rc = adjust_resource(next, next->start
424 - allocate, resource_size(next)
425 + allocate);
426 new_res = next;
427 action = "next grow up";
428 } else if (strcmp(res->name, label_id->id) == 0) {
429 action = "grow down";
430 } else
431 action = "allocate";
432 break;
433 case ALLOC_AFTER:
434 if (strcmp(res->name, label_id->id) == 0)
435 action = "grow down";
436 else
437 action = "allocate";
438 break;
439 default:
440 return n;
441 }
442
443 if (strcmp(action, "allocate") == 0) {
444 /* BLK allocate bottom up */
445 if (!is_pmem)
446 free_start += available - allocate;
Dan Williams1b40e092015-05-01 13:34:01 -0400447 else if (!is_reserve && free_start != nd_mapping->start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400448 return n;
449
450 new_res = nvdimm_allocate_dpa(ndd, label_id,
451 free_start, allocate);
452 if (!new_res)
453 rc = -EBUSY;
454 } else if (strcmp(action, "grow down") == 0) {
455 /* adjust current resource down */
456 rc = adjust_resource(res, res->start, resource_size(res)
457 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400458 if (rc == 0)
459 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400460 }
461
462 if (!new_res)
463 new_res = res;
464
465 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
466 action, loc, rc);
467
468 if (rc)
469 return n;
470
471 n -= allocate;
472 if (n) {
473 /*
474 * Retry scan with newly inserted resources.
475 * For example, if we did an ALLOC_BEFORE
476 * insertion there may also have been space
477 * available for an ALLOC_AFTER insertion, so we
478 * need to check this same resource again
479 */
480 goto retry;
481 } else
482 return 0;
483 }
484
Dan Williams1b40e092015-05-01 13:34:01 -0400485 /*
486 * If we allocated nothing in the BLK case it may be because we are in
487 * an initial "pmem-reserve pass". Only do an initial BLK allocation
488 * when none of the DPA space is reserved.
489 */
490 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400491 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
492 return n;
493}
494
Dan Williams1b40e092015-05-01 13:34:01 -0400495static int merge_dpa(struct nd_region *nd_region,
496 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
497{
498 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
499 struct resource *res;
500
501 if (strncmp("pmem", label_id->id, 4) == 0)
502 return 0;
503 retry:
504 for_each_dpa_resource(ndd, res) {
505 int rc;
506 struct resource *next = res->sibling;
507 resource_size_t end = res->start + resource_size(res);
508
509 if (!next || strcmp(res->name, label_id->id) != 0
510 || strcmp(next->name, label_id->id) != 0
511 || end != next->start)
512 continue;
513 end += resource_size(next);
514 nvdimm_free_dpa(ndd, next);
515 rc = adjust_resource(res, res->start, end - res->start);
516 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
517 if (rc)
518 return rc;
519 res->flags |= DPA_RESOURCE_ADJUSTED;
520 goto retry;
521 }
522
523 return 0;
524}
525
526static int __reserve_free_pmem(struct device *dev, void *data)
527{
528 struct nvdimm *nvdimm = data;
529 struct nd_region *nd_region;
530 struct nd_label_id label_id;
531 int i;
532
533 if (!is_nd_pmem(dev))
534 return 0;
535
536 nd_region = to_nd_region(dev);
537 if (nd_region->ndr_mappings == 0)
538 return 0;
539
540 memset(&label_id, 0, sizeof(label_id));
541 strcat(label_id.id, "pmem-reserve");
542 for (i = 0; i < nd_region->ndr_mappings; i++) {
543 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
544 resource_size_t n, rem = 0;
545
546 if (nd_mapping->nvdimm != nvdimm)
547 continue;
548
549 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
550 if (n == 0)
551 return 0;
552 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
553 dev_WARN_ONCE(&nd_region->dev, rem,
554 "pmem reserve underrun: %#llx of %#llx bytes\n",
555 (unsigned long long) n - rem,
556 (unsigned long long) n);
557 return rem ? -ENXIO : 0;
558 }
559
560 return 0;
561}
562
563static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
564 struct nd_mapping *nd_mapping)
565{
566 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
567 struct resource *res, *_res;
568
569 for_each_dpa_resource_safe(ndd, res, _res)
570 if (strcmp(res->name, "pmem-reserve") == 0)
571 nvdimm_free_dpa(ndd, res);
572}
573
574static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
575 struct nd_mapping *nd_mapping)
576{
577 struct nvdimm *nvdimm = nd_mapping->nvdimm;
578 int rc;
579
580 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
581 __reserve_free_pmem);
582 if (rc)
583 release_free_pmem(nvdimm_bus, nd_mapping);
584 return rc;
585}
586
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400587/**
588 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
589 * @nd_region: the set of dimms to allocate @n more bytes from
590 * @label_id: unique identifier for the namespace consuming this dpa range
591 * @n: number of bytes per-dimm to add to the existing allocation
592 *
593 * Assumes resources are ordered. For BLK regions, first consume
594 * BLK-only available DPA free space, then consume PMEM-aliased DPA
595 * space starting at the highest DPA. For PMEM regions start
596 * allocations from the start of an interleave set and end at the first
597 * BLK allocation or the end of the interleave set, whichever comes
598 * first.
599 */
600static int grow_dpa_allocation(struct nd_region *nd_region,
601 struct nd_label_id *label_id, resource_size_t n)
602{
Dan Williams1b40e092015-05-01 13:34:01 -0400603 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
604 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400605 int i;
606
607 for (i = 0; i < nd_region->ndr_mappings; i++) {
608 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400609 resource_size_t rem = n;
610 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400611
Dan Williams1b40e092015-05-01 13:34:01 -0400612 /*
613 * In the BLK case try once with all unallocated PMEM
614 * reserved, and once without
615 */
616 for (j = is_pmem; j < 2; j++) {
617 bool blk_only = j == 0;
618
619 if (blk_only) {
620 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
621 if (rc)
622 return rc;
623 }
624 rem = scan_allocate(nd_region, nd_mapping,
625 label_id, rem);
626 if (blk_only)
627 release_free_pmem(nvdimm_bus, nd_mapping);
628
629 /* try again and allow encroachments into PMEM */
630 if (rem == 0)
631 break;
632 }
633
634 dev_WARN_ONCE(&nd_region->dev, rem,
635 "allocation underrun: %#llx of %#llx bytes\n",
636 (unsigned long long) n - rem,
637 (unsigned long long) n);
638 if (rem)
639 return -ENXIO;
640
641 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400642 if (rc)
643 return rc;
644 }
645
646 return 0;
647}
648
649static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
650 struct nd_namespace_pmem *nspm, resource_size_t size)
651{
652 struct resource *res = &nspm->nsio.res;
653
654 res->start = nd_region->ndr_start;
655 res->end = nd_region->ndr_start + size - 1;
656}
657
658static ssize_t __size_store(struct device *dev, unsigned long long val)
659{
660 resource_size_t allocated = 0, available = 0;
661 struct nd_region *nd_region = to_nd_region(dev->parent);
662 struct nd_mapping *nd_mapping;
663 struct nvdimm_drvdata *ndd;
664 struct nd_label_id label_id;
665 u32 flags = 0, remainder;
666 u8 *uuid = NULL;
667 int rc, i;
668
Dan Williams8c2f7e82015-06-25 04:20:04 -0400669 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400670 return -EBUSY;
671
672 if (is_namespace_pmem(dev)) {
673 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
674
675 uuid = nspm->uuid;
676 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400677 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
678
679 uuid = nsblk->uuid;
680 flags = NSLABEL_FLAG_LOCAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400681 }
682
683 /*
684 * We need a uuid for the allocation-label and dimm(s) on which
685 * to store the label.
686 */
687 if (!uuid || nd_region->ndr_mappings == 0)
688 return -ENXIO;
689
690 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
691 if (remainder) {
692 dev_dbg(dev, "%llu is not %dK aligned\n", val,
693 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
694 return -EINVAL;
695 }
696
697 nd_label_gen_id(&label_id, uuid, flags);
698 for (i = 0; i < nd_region->ndr_mappings; i++) {
699 nd_mapping = &nd_region->mapping[i];
700 ndd = to_ndd(nd_mapping);
701
702 /*
703 * All dimms in an interleave set, or the base dimm for a blk
704 * region, need to be enabled for the size to be changed.
705 */
706 if (!ndd)
707 return -ENXIO;
708
709 allocated += nvdimm_allocated_dpa(ndd, &label_id);
710 }
711 available = nd_region_available_dpa(nd_region);
712
713 if (val > available + allocated)
714 return -ENOSPC;
715
716 if (val == allocated)
717 return 0;
718
719 val = div_u64(val, nd_region->ndr_mappings);
720 allocated = div_u64(allocated, nd_region->ndr_mappings);
721 if (val < allocated)
722 rc = shrink_dpa_allocation(nd_region, &label_id,
723 allocated - val);
724 else
725 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
726
727 if (rc)
728 return rc;
729
730 if (is_namespace_pmem(dev)) {
731 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
732
733 nd_namespace_pmem_set_size(nd_region, nspm,
734 val * nd_region->ndr_mappings);
Dan Williams1b40e092015-05-01 13:34:01 -0400735 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400736 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
737
Dan Williams1b40e092015-05-01 13:34:01 -0400738 /*
739 * Try to delete the namespace if we deleted all of its
Dan Williams8c2f7e82015-06-25 04:20:04 -0400740 * allocation, this is not the seed device for the
741 * region, and it is not actively claimed by a btt
742 * instance.
Dan Williams1b40e092015-05-01 13:34:01 -0400743 */
Dan Williams8c2f7e82015-06-25 04:20:04 -0400744 if (val == 0 && nd_region->ns_seed != dev
745 && !nsblk->common.claim)
Dan Williams1b40e092015-05-01 13:34:01 -0400746 nd_device_unregister(dev, ND_ASYNC);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400747 }
748
749 return rc;
750}
751
752static ssize_t size_store(struct device *dev,
753 struct device_attribute *attr, const char *buf, size_t len)
754{
Dan Williamsf524bf22015-05-30 12:36:02 -0400755 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400756 unsigned long long val;
757 u8 **uuid = NULL;
758 int rc;
759
760 rc = kstrtoull(buf, 0, &val);
761 if (rc)
762 return rc;
763
764 device_lock(dev);
765 nvdimm_bus_lock(dev);
766 wait_nvdimm_bus_probe_idle(dev);
767 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -0400768 if (rc >= 0)
769 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400770
771 if (is_namespace_pmem(dev)) {
772 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
773
774 uuid = &nspm->uuid;
775 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400776 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
777
778 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400779 }
780
781 if (rc == 0 && val == 0 && uuid) {
782 /* setting size zero == 'delete namespace' */
783 kfree(*uuid);
784 *uuid = NULL;
785 }
786
787 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
788 ? "fail" : "success", rc);
789
790 nvdimm_bus_unlock(dev);
791 device_unlock(dev);
792
Dan Williamsf524bf22015-05-30 12:36:02 -0400793 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400794}
795
Dan Williams8c2f7e82015-06-25 04:20:04 -0400796resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400797{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400798 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -0400799
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400800 if (is_namespace_pmem(dev)) {
801 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
802
Dan Williams8c2f7e82015-06-25 04:20:04 -0400803 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400804 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400805 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400806 } else if (is_namespace_io(dev)) {
807 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
808
Dan Williams8c2f7e82015-06-25 04:20:04 -0400809 return resource_size(&nsio->res);
810 } else
811 WARN_ONCE(1, "unknown namespace type\n");
812 return 0;
813}
Dan Williams1b40e092015-05-01 13:34:01 -0400814
Dan Williams8c2f7e82015-06-25 04:20:04 -0400815resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
816{
817 resource_size_t size;
818
819 nvdimm_bus_lock(&ndns->dev);
820 size = __nvdimm_namespace_capacity(ndns);
821 nvdimm_bus_unlock(&ndns->dev);
822
823 return size;
824}
825EXPORT_SYMBOL(nvdimm_namespace_capacity);
826
827static ssize_t size_show(struct device *dev,
828 struct device_attribute *attr, char *buf)
829{
830 return sprintf(buf, "%llu\n", (unsigned long long)
831 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400832}
833static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
834
835static ssize_t uuid_show(struct device *dev,
836 struct device_attribute *attr, char *buf)
837{
838 u8 *uuid;
839
840 if (is_namespace_pmem(dev)) {
841 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
842
843 uuid = nspm->uuid;
844 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400845 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
846
847 uuid = nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400848 } else
849 return -ENXIO;
850
851 if (uuid)
852 return sprintf(buf, "%pUb\n", uuid);
853 return sprintf(buf, "\n");
854}
855
856/**
857 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
858 * @nd_region: parent region so we can updates all dimms in the set
859 * @dev: namespace type for generating label_id
860 * @new_uuid: incoming uuid
861 * @old_uuid: reference to the uuid storage location in the namespace object
862 */
863static int namespace_update_uuid(struct nd_region *nd_region,
864 struct device *dev, u8 *new_uuid, u8 **old_uuid)
865{
866 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
867 struct nd_label_id old_label_id;
868 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -0400869 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400870
Dan Williamsf524bf22015-05-30 12:36:02 -0400871 if (!nd_is_uuid_unique(dev, new_uuid))
872 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400873
874 if (*old_uuid == NULL)
875 goto out;
876
Dan Williamsf524bf22015-05-30 12:36:02 -0400877 /*
878 * If we've already written a label with this uuid, then it's
879 * too late to rename because we can't reliably update the uuid
880 * without losing the old namespace. Userspace must delete this
881 * namespace to abandon the old uuid.
882 */
883 for (i = 0; i < nd_region->ndr_mappings; i++) {
884 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
885
886 /*
887 * This check by itself is sufficient because old_uuid
888 * would be NULL above if this uuid did not exist in the
889 * currently written set.
890 *
891 * FIXME: can we delete uuid with zero dpa allocated?
892 */
893 if (nd_mapping->labels)
894 return -EBUSY;
895 }
896
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400897 nd_label_gen_id(&old_label_id, *old_uuid, flags);
898 nd_label_gen_id(&new_label_id, new_uuid, flags);
899 for (i = 0; i < nd_region->ndr_mappings; i++) {
900 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
901 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
902 struct resource *res;
903
904 for_each_dpa_resource(ndd, res)
905 if (strcmp(res->name, old_label_id.id) == 0)
906 sprintf((void *) res->name, "%s",
907 new_label_id.id);
908 }
909 kfree(*old_uuid);
910 out:
911 *old_uuid = new_uuid;
912 return 0;
913}
914
915static ssize_t uuid_store(struct device *dev,
916 struct device_attribute *attr, const char *buf, size_t len)
917{
918 struct nd_region *nd_region = to_nd_region(dev->parent);
919 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400920 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400921 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400922
923 if (is_namespace_pmem(dev)) {
924 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
925
926 ns_uuid = &nspm->uuid;
927 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400928 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
929
930 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400931 } else
932 return -ENXIO;
933
934 device_lock(dev);
935 nvdimm_bus_lock(dev);
936 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400937 if (to_ndns(dev)->claim)
938 rc = -EBUSY;
939 if (rc >= 0)
940 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400941 if (rc >= 0)
942 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -0400943 if (rc >= 0)
944 rc = nd_namespace_label_update(nd_region, dev);
945 else
946 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400947 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
948 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
949 nvdimm_bus_unlock(dev);
950 device_unlock(dev);
951
Dan Williamsf524bf22015-05-30 12:36:02 -0400952 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400953}
954static DEVICE_ATTR_RW(uuid);
955
956static ssize_t resource_show(struct device *dev,
957 struct device_attribute *attr, char *buf)
958{
959 struct resource *res;
960
961 if (is_namespace_pmem(dev)) {
962 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
963
964 res = &nspm->nsio.res;
965 } else if (is_namespace_io(dev)) {
966 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
967
968 res = &nsio->res;
969 } else
970 return -ENXIO;
971
972 /* no address to convey if the namespace has no allocation */
973 if (resource_size(res) == 0)
974 return -ENXIO;
975 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
976}
977static DEVICE_ATTR_RO(resource);
978
Dan Williams1b40e092015-05-01 13:34:01 -0400979static const unsigned long ns_lbasize_supported[] = { 512, 0 };
980
981static ssize_t sector_size_show(struct device *dev,
982 struct device_attribute *attr, char *buf)
983{
984 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
985
986 if (!is_namespace_blk(dev))
987 return -ENXIO;
988
989 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
990}
991
992static ssize_t sector_size_store(struct device *dev,
993 struct device_attribute *attr, const char *buf, size_t len)
994{
995 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williamsf524bf22015-05-30 12:36:02 -0400996 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400997 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -0400998
999 if (!is_namespace_blk(dev))
1000 return -ENXIO;
1001
1002 device_lock(dev);
1003 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001004 if (to_ndns(dev)->claim)
1005 rc = -EBUSY;
1006 if (rc >= 0)
1007 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1008 ns_lbasize_supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001009 if (rc >= 0)
1010 rc = nd_namespace_label_update(nd_region, dev);
1011 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1012 rc, rc < 0 ? "tried" : "wrote", buf,
1013 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001014 nvdimm_bus_unlock(dev);
1015 device_unlock(dev);
1016
1017 return rc ? rc : len;
1018}
1019static DEVICE_ATTR_RW(sector_size);
1020
Dan Williams0ba1c632015-05-30 12:35:36 -04001021static ssize_t dpa_extents_show(struct device *dev,
1022 struct device_attribute *attr, char *buf)
1023{
1024 struct nd_region *nd_region = to_nd_region(dev->parent);
1025 struct nd_label_id label_id;
1026 int count = 0, i;
1027 u8 *uuid = NULL;
1028 u32 flags = 0;
1029
1030 nvdimm_bus_lock(dev);
1031 if (is_namespace_pmem(dev)) {
1032 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1033
1034 uuid = nspm->uuid;
1035 flags = 0;
1036 } else if (is_namespace_blk(dev)) {
1037 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1038
1039 uuid = nsblk->uuid;
1040 flags = NSLABEL_FLAG_LOCAL;
1041 }
1042
1043 if (!uuid)
1044 goto out;
1045
1046 nd_label_gen_id(&label_id, uuid, flags);
1047 for (i = 0; i < nd_region->ndr_mappings; i++) {
1048 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1049 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1050 struct resource *res;
1051
1052 for_each_dpa_resource(ndd, res)
1053 if (strcmp(res->name, label_id.id) == 0)
1054 count++;
1055 }
1056 out:
1057 nvdimm_bus_unlock(dev);
1058
1059 return sprintf(buf, "%d\n", count);
1060}
1061static DEVICE_ATTR_RO(dpa_extents);
1062
Dan Williams8c2f7e82015-06-25 04:20:04 -04001063static ssize_t holder_show(struct device *dev,
1064 struct device_attribute *attr, char *buf)
1065{
1066 struct nd_namespace_common *ndns = to_ndns(dev);
1067 ssize_t rc;
1068
1069 device_lock(dev);
1070 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1071 device_unlock(dev);
1072
1073 return rc;
1074}
1075static DEVICE_ATTR_RO(holder);
1076
1077static ssize_t force_raw_store(struct device *dev,
1078 struct device_attribute *attr, const char *buf, size_t len)
1079{
1080 bool force_raw;
1081 int rc = strtobool(buf, &force_raw);
1082
1083 if (rc)
1084 return rc;
1085
1086 to_ndns(dev)->force_raw = force_raw;
1087 return len;
1088}
1089
1090static ssize_t force_raw_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1092{
1093 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1094}
1095static DEVICE_ATTR_RW(force_raw);
1096
Dan Williams3d880022015-05-31 15:02:11 -04001097static struct attribute *nd_namespace_attributes[] = {
1098 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001099 &dev_attr_size.attr,
1100 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001101 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001102 &dev_attr_resource.attr,
1103 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001104 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001105 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001106 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001107 NULL,
1108};
1109
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001110static umode_t namespace_visible(struct kobject *kobj,
1111 struct attribute *a, int n)
1112{
1113 struct device *dev = container_of(kobj, struct device, kobj);
1114
1115 if (a == &dev_attr_resource.attr) {
1116 if (is_namespace_blk(dev))
1117 return 0;
1118 return a->mode;
1119 }
1120
1121 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1122 if (a == &dev_attr_size.attr)
1123 return S_IWUSR | S_IRUGO;
Dan Williams1b40e092015-05-01 13:34:01 -04001124
1125 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1126 return 0;
1127
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001128 return a->mode;
1129 }
1130
Dan Williams8c2f7e82015-06-25 04:20:04 -04001131 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1132 || a == &dev_attr_holder.attr
1133 || a == &dev_attr_force_raw.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001134 return a->mode;
1135
1136 return 0;
1137}
1138
Dan Williams3d880022015-05-31 15:02:11 -04001139static struct attribute_group nd_namespace_attribute_group = {
1140 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001141 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001142};
1143
1144static const struct attribute_group *nd_namespace_attribute_groups[] = {
1145 &nd_device_attribute_group,
1146 &nd_namespace_attribute_group,
1147 NULL,
1148};
1149
Dan Williams8c2f7e82015-06-25 04:20:04 -04001150struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1151{
1152 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
1153 struct nd_namespace_common *ndns;
1154 resource_size_t size;
1155
1156 if (nd_btt) {
1157 ndns = nd_btt->ndns;
1158 if (!ndns)
1159 return ERR_PTR(-ENODEV);
1160
1161 /*
1162 * Flush any in-progess probes / removals in the driver
1163 * for the raw personality of this namespace.
1164 */
1165 device_lock(&ndns->dev);
1166 device_unlock(&ndns->dev);
1167 if (ndns->dev.driver) {
1168 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
1169 dev_name(&nd_btt->dev));
1170 return ERR_PTR(-EBUSY);
1171 }
1172 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != &nd_btt->dev,
1173 "host (%s) vs claim (%s) mismatch\n",
1174 dev_name(&nd_btt->dev),
1175 dev_name(ndns->claim)))
1176 return ERR_PTR(-ENXIO);
1177 } else {
1178 ndns = to_ndns(dev);
1179 if (ndns->claim) {
1180 dev_dbg(dev, "claimed by %s, failing probe\n",
1181 dev_name(ndns->claim));
1182
1183 return ERR_PTR(-ENXIO);
1184 }
1185 }
1186
1187 size = nvdimm_namespace_capacity(ndns);
1188 if (size < ND_MIN_NAMESPACE_SIZE) {
1189 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1190 &size, ND_MIN_NAMESPACE_SIZE);
1191 return ERR_PTR(-ENODEV);
1192 }
1193
1194 if (is_namespace_pmem(&ndns->dev)) {
1195 struct nd_namespace_pmem *nspm;
1196
1197 nspm = to_nd_namespace_pmem(&ndns->dev);
1198 if (!nspm->uuid) {
1199 dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__);
1200 return ERR_PTR(-ENODEV);
1201 }
1202 } else if (is_namespace_blk(&ndns->dev)) {
1203 return ERR_PTR(-ENODEV); /* TODO */
1204 }
1205
1206 return ndns;
1207}
1208EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1209
Dan Williams3d880022015-05-31 15:02:11 -04001210static struct device **create_namespace_io(struct nd_region *nd_region)
1211{
1212 struct nd_namespace_io *nsio;
1213 struct device *dev, **devs;
1214 struct resource *res;
1215
1216 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1217 if (!nsio)
1218 return NULL;
1219
1220 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1221 if (!devs) {
1222 kfree(nsio);
1223 return NULL;
1224 }
1225
Dan Williams8c2f7e82015-06-25 04:20:04 -04001226 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001227 dev->type = &namespace_io_device_type;
1228 dev->parent = &nd_region->dev;
1229 res = &nsio->res;
1230 res->name = dev_name(&nd_region->dev);
1231 res->flags = IORESOURCE_MEM;
1232 res->start = nd_region->ndr_start;
1233 res->end = res->start + nd_region->ndr_size - 1;
1234
1235 devs[0] = dev;
1236 return devs;
1237}
1238
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001239static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1240 u64 cookie, u16 pos)
1241{
1242 struct nd_namespace_label *found = NULL;
1243 int i;
1244
1245 for (i = 0; i < nd_region->ndr_mappings; i++) {
1246 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1247 struct nd_namespace_label *nd_label;
1248 bool found_uuid = false;
1249 int l;
1250
1251 for_each_label(l, nd_label, nd_mapping->labels) {
1252 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1253 u16 position = __le16_to_cpu(nd_label->position);
1254 u16 nlabel = __le16_to_cpu(nd_label->nlabel);
1255
1256 if (isetcookie != cookie)
1257 continue;
1258
1259 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1260 continue;
1261
1262 if (found_uuid) {
1263 dev_dbg(to_ndd(nd_mapping)->dev,
1264 "%s duplicate entry for uuid\n",
1265 __func__);
1266 return false;
1267 }
1268 found_uuid = true;
1269 if (nlabel != nd_region->ndr_mappings)
1270 continue;
1271 if (position != pos)
1272 continue;
1273 found = nd_label;
1274 break;
1275 }
1276 if (found)
1277 break;
1278 }
1279 return found != NULL;
1280}
1281
1282static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1283{
1284 struct nd_namespace_label *select = NULL;
1285 int i;
1286
1287 if (!pmem_id)
1288 return -ENODEV;
1289
1290 for (i = 0; i < nd_region->ndr_mappings; i++) {
1291 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1292 struct nd_namespace_label *nd_label;
1293 u64 hw_start, hw_end, pmem_start, pmem_end;
1294 int l;
1295
1296 for_each_label(l, nd_label, nd_mapping->labels)
1297 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1298 break;
1299
1300 if (!nd_label) {
1301 WARN_ON(1);
1302 return -EINVAL;
1303 }
1304
1305 select = nd_label;
1306 /*
1307 * Check that this label is compliant with the dpa
1308 * range published in NFIT
1309 */
1310 hw_start = nd_mapping->start;
1311 hw_end = hw_start + nd_mapping->size;
1312 pmem_start = __le64_to_cpu(select->dpa);
1313 pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
1314 if (pmem_start == hw_start && pmem_end <= hw_end)
1315 /* pass */;
1316 else
1317 return -EINVAL;
1318
1319 nd_mapping->labels[0] = select;
1320 nd_mapping->labels[1] = NULL;
1321 }
1322 return 0;
1323}
1324
1325/**
1326 * find_pmem_label_set - validate interleave set labelling, retrieve label0
1327 * @nd_region: region with mappings to validate
1328 */
1329static int find_pmem_label_set(struct nd_region *nd_region,
1330 struct nd_namespace_pmem *nspm)
1331{
1332 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1333 struct nd_namespace_label *nd_label;
1334 u8 select_id[NSLABEL_UUID_LEN];
1335 resource_size_t size = 0;
1336 u8 *pmem_id = NULL;
1337 int rc = -ENODEV, l;
1338 u16 i;
1339
1340 if (cookie == 0)
1341 return -ENXIO;
1342
1343 /*
1344 * Find a complete set of labels by uuid. By definition we can start
1345 * with any mapping as the reference label
1346 */
1347 for_each_label(l, nd_label, nd_region->mapping[0].labels) {
1348 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1349
1350 if (isetcookie != cookie)
1351 continue;
1352
1353 for (i = 0; nd_region->ndr_mappings; i++)
1354 if (!has_uuid_at_pos(nd_region, nd_label->uuid,
1355 cookie, i))
1356 break;
1357 if (i < nd_region->ndr_mappings) {
1358 /*
1359 * Give up if we don't find an instance of a
1360 * uuid at each position (from 0 to
1361 * nd_region->ndr_mappings - 1), or if we find a
1362 * dimm with two instances of the same uuid.
1363 */
1364 rc = -EINVAL;
1365 goto err;
1366 } else if (pmem_id) {
1367 /*
1368 * If there is more than one valid uuid set, we
1369 * need userspace to clean this up.
1370 */
1371 rc = -EBUSY;
1372 goto err;
1373 }
1374 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
1375 pmem_id = select_id;
1376 }
1377
1378 /*
1379 * Fix up each mapping's 'labels' to have the validated pmem label for
1380 * that position at labels[0], and NULL at labels[1]. In the process,
1381 * check that the namespace aligns with interleave-set. We know
1382 * that it does not overlap with any blk namespaces by virtue of
1383 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1384 * succeeded).
1385 */
1386 rc = select_pmem_id(nd_region, pmem_id);
1387 if (rc)
1388 goto err;
1389
1390 /* Calculate total size and populate namespace properties from label0 */
1391 for (i = 0; i < nd_region->ndr_mappings; i++) {
1392 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1393 struct nd_namespace_label *label0 = nd_mapping->labels[0];
1394
1395 size += __le64_to_cpu(label0->rawsize);
1396 if (__le16_to_cpu(label0->position) != 0)
1397 continue;
1398 WARN_ON(nspm->alt_name || nspm->uuid);
1399 nspm->alt_name = kmemdup((void __force *) label0->name,
1400 NSLABEL_NAME_LEN, GFP_KERNEL);
1401 nspm->uuid = kmemdup((void __force *) label0->uuid,
1402 NSLABEL_UUID_LEN, GFP_KERNEL);
1403 }
1404
1405 if (!nspm->alt_name || !nspm->uuid) {
1406 rc = -ENOMEM;
1407 goto err;
1408 }
1409
1410 nd_namespace_pmem_set_size(nd_region, nspm, size);
1411
1412 return 0;
1413 err:
1414 switch (rc) {
1415 case -EINVAL:
1416 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1417 break;
1418 case -ENODEV:
1419 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1420 break;
1421 default:
1422 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1423 __func__, rc);
1424 break;
1425 }
1426 return rc;
1427}
1428
1429static struct device **create_namespace_pmem(struct nd_region *nd_region)
1430{
1431 struct nd_namespace_pmem *nspm;
1432 struct device *dev, **devs;
1433 struct resource *res;
1434 int rc;
1435
1436 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1437 if (!nspm)
1438 return NULL;
1439
Dan Williams8c2f7e82015-06-25 04:20:04 -04001440 dev = &nspm->nsio.common.dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001441 dev->type = &namespace_pmem_device_type;
1442 dev->parent = &nd_region->dev;
1443 res = &nspm->nsio.res;
1444 res->name = dev_name(&nd_region->dev);
1445 res->flags = IORESOURCE_MEM;
1446 rc = find_pmem_label_set(nd_region, nspm);
1447 if (rc == -ENODEV) {
1448 int i;
1449
1450 /* Pass, try to permit namespace creation... */
1451 for (i = 0; i < nd_region->ndr_mappings; i++) {
1452 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1453
1454 kfree(nd_mapping->labels);
1455 nd_mapping->labels = NULL;
1456 }
1457
1458 /* Publish a zero-sized namespace for userspace to configure. */
1459 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1460
1461 rc = 0;
1462 } else if (rc)
1463 goto err;
1464
1465 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1466 if (!devs)
1467 goto err;
1468
1469 devs[0] = dev;
1470 return devs;
1471
1472 err:
Dan Williams8c2f7e82015-06-25 04:20:04 -04001473 namespace_pmem_release(&nspm->nsio.common.dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001474 return NULL;
1475}
1476
Dan Williams1b40e092015-05-01 13:34:01 -04001477struct resource *nsblk_add_resource(struct nd_region *nd_region,
1478 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1479 resource_size_t start)
1480{
1481 struct nd_label_id label_id;
1482 struct resource *res;
1483
1484 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1485 res = krealloc(nsblk->res,
1486 sizeof(void *) * (nsblk->num_resources + 1),
1487 GFP_KERNEL);
1488 if (!res)
1489 return NULL;
1490 nsblk->res = (struct resource **) res;
1491 for_each_dpa_resource(ndd, res)
1492 if (strcmp(res->name, label_id.id) == 0
1493 && res->start == start) {
1494 nsblk->res[nsblk->num_resources++] = res;
1495 return res;
1496 }
1497 return NULL;
1498}
1499
1500static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1501{
1502 struct nd_namespace_blk *nsblk;
1503 struct device *dev;
1504
1505 if (!is_nd_blk(&nd_region->dev))
1506 return NULL;
1507
1508 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1509 if (!nsblk)
1510 return NULL;
1511
Dan Williams8c2f7e82015-06-25 04:20:04 -04001512 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001513 dev->type = &namespace_blk_device_type;
1514 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1515 if (nsblk->id < 0) {
1516 kfree(nsblk);
1517 return NULL;
1518 }
1519 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1520 dev->parent = &nd_region->dev;
1521 dev->groups = nd_namespace_attribute_groups;
1522
Dan Williams8c2f7e82015-06-25 04:20:04 -04001523 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001524}
1525
1526void nd_region_create_blk_seed(struct nd_region *nd_region)
1527{
1528 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1529 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1530 /*
1531 * Seed creation failures are not fatal, provisioning is simply
1532 * disabled until memory becomes available
1533 */
1534 if (!nd_region->ns_seed)
1535 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1536 else
1537 nd_device_register(nd_region->ns_seed);
1538}
1539
Dan Williams8c2f7e82015-06-25 04:20:04 -04001540void nd_region_create_btt_seed(struct nd_region *nd_region)
1541{
1542 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1543 nd_region->btt_seed = nd_btt_create(nd_region);
1544 /*
1545 * Seed creation failures are not fatal, provisioning is simply
1546 * disabled until memory becomes available
1547 */
1548 if (!nd_region->btt_seed)
1549 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1550}
1551
Dan Williams1b40e092015-05-01 13:34:01 -04001552static struct device **create_namespace_blk(struct nd_region *nd_region)
1553{
1554 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1555 struct nd_namespace_label *nd_label;
1556 struct device *dev, **devs = NULL;
1557 struct nd_namespace_blk *nsblk;
1558 struct nvdimm_drvdata *ndd;
1559 int i, l, count = 0;
1560 struct resource *res;
1561
1562 if (nd_region->ndr_mappings == 0)
1563 return NULL;
1564
1565 ndd = to_ndd(nd_mapping);
1566 for_each_label(l, nd_label, nd_mapping->labels) {
1567 u32 flags = __le32_to_cpu(nd_label->flags);
1568 char *name[NSLABEL_NAME_LEN];
1569 struct device **__devs;
1570
1571 if (flags & NSLABEL_FLAG_LOCAL)
1572 /* pass */;
1573 else
1574 continue;
1575
1576 for (i = 0; i < count; i++) {
1577 nsblk = to_nd_namespace_blk(devs[i]);
1578 if (memcmp(nsblk->uuid, nd_label->uuid,
1579 NSLABEL_UUID_LEN) == 0) {
1580 res = nsblk_add_resource(nd_region, ndd, nsblk,
1581 __le64_to_cpu(nd_label->dpa));
1582 if (!res)
1583 goto err;
1584 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001585 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001586 break;
1587 }
1588 }
1589 if (i < count)
1590 continue;
1591 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1592 if (!__devs)
1593 goto err;
1594 memcpy(__devs, devs, sizeof(dev) * count);
1595 kfree(devs);
1596 devs = __devs;
1597
1598 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1599 if (!nsblk)
1600 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001601 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001602 dev->type = &namespace_blk_device_type;
1603 dev->parent = &nd_region->dev;
1604 dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
1605 devs[count++] = dev;
1606 nsblk->id = -1;
1607 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1608 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1609 GFP_KERNEL);
1610 if (!nsblk->uuid)
1611 goto err;
1612 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1613 if (name[0])
1614 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1615 GFP_KERNEL);
1616 res = nsblk_add_resource(nd_region, ndd, nsblk,
1617 __le64_to_cpu(nd_label->dpa));
1618 if (!res)
1619 goto err;
1620 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001621 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001622 }
1623
1624 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
1625 __func__, count, count == 1 ? "" : "s");
1626
1627 if (count == 0) {
1628 /* Publish a zero-sized namespace for userspace to configure. */
1629 for (i = 0; i < nd_region->ndr_mappings; i++) {
1630 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1631
1632 kfree(nd_mapping->labels);
1633 nd_mapping->labels = NULL;
1634 }
1635
1636 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1637 if (!devs)
1638 goto err;
1639 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1640 if (!nsblk)
1641 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001642 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001643 dev->type = &namespace_blk_device_type;
1644 dev->parent = &nd_region->dev;
1645 devs[count++] = dev;
1646 }
1647
1648 return devs;
1649
1650err:
1651 for (i = 0; i < count; i++) {
1652 nsblk = to_nd_namespace_blk(devs[i]);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001653 namespace_blk_release(&nsblk->common.dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001654 }
1655 kfree(devs);
1656 return NULL;
1657}
1658
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001659static int init_active_labels(struct nd_region *nd_region)
1660{
1661 int i;
1662
1663 for (i = 0; i < nd_region->ndr_mappings; i++) {
1664 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1665 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1666 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1667 int count, j;
1668
1669 /*
1670 * If the dimm is disabled then prevent the region from
1671 * being activated if it aliases DPA.
1672 */
1673 if (!ndd) {
1674 if ((nvdimm->flags & NDD_ALIASING) == 0)
1675 return 0;
1676 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
1677 dev_name(&nd_mapping->nvdimm->dev));
1678 return -ENXIO;
1679 }
1680 nd_mapping->ndd = ndd;
1681 atomic_inc(&nvdimm->busy);
1682 get_ndd(ndd);
1683
1684 count = nd_label_active_count(ndd);
1685 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
1686 if (!count)
1687 continue;
1688 nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
1689 GFP_KERNEL);
1690 if (!nd_mapping->labels)
1691 return -ENOMEM;
1692 for (j = 0; j < count; j++) {
1693 struct nd_namespace_label *label;
1694
1695 label = nd_label_active(ndd, j);
1696 nd_mapping->labels[j] = label;
1697 }
1698 }
1699
1700 return 0;
1701}
1702
Dan Williams3d880022015-05-31 15:02:11 -04001703int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
1704{
1705 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001706 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04001707
1708 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001709 nvdimm_bus_lock(&nd_region->dev);
1710 rc = init_active_labels(nd_region);
1711 if (rc) {
1712 nvdimm_bus_unlock(&nd_region->dev);
1713 return rc;
1714 }
1715
1716 type = nd_region_to_nstype(nd_region);
1717 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04001718 case ND_DEVICE_NAMESPACE_IO:
1719 devs = create_namespace_io(nd_region);
1720 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001721 case ND_DEVICE_NAMESPACE_PMEM:
1722 devs = create_namespace_pmem(nd_region);
1723 break;
Dan Williams1b40e092015-05-01 13:34:01 -04001724 case ND_DEVICE_NAMESPACE_BLK:
1725 devs = create_namespace_blk(nd_region);
1726 break;
Dan Williams3d880022015-05-31 15:02:11 -04001727 default:
1728 break;
1729 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001730 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04001731
1732 if (!devs)
1733 return -ENODEV;
1734
Dan Williams3d880022015-05-31 15:02:11 -04001735 for (i = 0; devs[i]; i++) {
1736 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04001737 int id;
Dan Williams3d880022015-05-31 15:02:11 -04001738
Dan Williams1b40e092015-05-01 13:34:01 -04001739 if (type == ND_DEVICE_NAMESPACE_BLK) {
1740 struct nd_namespace_blk *nsblk;
1741
1742 nsblk = to_nd_namespace_blk(dev);
1743 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
1744 GFP_KERNEL);
1745 nsblk->id = id;
1746 } else
1747 id = i;
1748
1749 if (id < 0)
1750 break;
1751 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04001752 dev->groups = nd_namespace_attribute_groups;
1753 nd_device_register(dev);
1754 }
Dan Williams1b40e092015-05-01 13:34:01 -04001755 if (i)
1756 nd_region->ns_seed = devs[0];
1757
1758 if (devs[i]) {
1759 int j;
1760
1761 for (j = i; devs[j]; j++) {
1762 struct device *dev = devs[j];
1763
1764 device_initialize(dev);
1765 put_device(dev);
1766 }
1767 *err = j - i;
1768 /*
1769 * All of the namespaces we tried to register failed, so
1770 * fail region activation.
1771 */
1772 if (*err == 0)
1773 rc = -ENODEV;
1774 }
Dan Williams3d880022015-05-31 15:02:11 -04001775 kfree(devs);
1776
Dan Williams1b40e092015-05-01 13:34:01 -04001777 if (rc == -ENODEV)
1778 return rc;
1779
Dan Williams3d880022015-05-31 15:02:11 -04001780 return i;
1781}