blob: fbcadc7cb8fd1f2ef7ac63c164fa8320c1aa6c23 [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>
Dan Williams004f1af2015-08-24 19:20:23 -040016#include <linux/pmem.h>
Dan Williamsae8219f2016-09-19 16:04:21 -070017#include <linux/list.h>
Dan Williams3d880022015-05-31 15:02:11 -040018#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040019#include "nd-core.h"
Dan Williams3d880022015-05-31 15:02:11 -040020#include "nd.h"
21
22static void namespace_io_release(struct device *dev)
23{
24 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
25
26 kfree(nsio);
27}
28
Dan Williamsbf9bccc2015-06-17 17:14:46 -040029static void namespace_pmem_release(struct device *dev)
30{
31 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
32
33 kfree(nspm->alt_name);
34 kfree(nspm->uuid);
35 kfree(nspm);
36}
37
38static void namespace_blk_release(struct device *dev)
39{
Dan Williams1b40e092015-05-01 13:34:01 -040040 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
41 struct nd_region *nd_region = to_nd_region(dev->parent);
42
43 if (nsblk->id >= 0)
44 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
45 kfree(nsblk->alt_name);
46 kfree(nsblk->uuid);
47 kfree(nsblk->res);
48 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040049}
50
Dan Williams3d880022015-05-31 15:02:11 -040051static struct device_type namespace_io_device_type = {
52 .name = "nd_namespace_io",
53 .release = namespace_io_release,
54};
55
Dan Williamsbf9bccc2015-06-17 17:14:46 -040056static struct device_type namespace_pmem_device_type = {
57 .name = "nd_namespace_pmem",
58 .release = namespace_pmem_release,
59};
60
61static struct device_type namespace_blk_device_type = {
62 .name = "nd_namespace_blk",
63 .release = namespace_blk_release,
64};
65
66static bool is_namespace_pmem(struct device *dev)
67{
68 return dev ? dev->type == &namespace_pmem_device_type : false;
69}
70
71static bool is_namespace_blk(struct device *dev)
72{
73 return dev ? dev->type == &namespace_blk_device_type : false;
74}
75
76static bool is_namespace_io(struct device *dev)
77{
78 return dev ? dev->type == &namespace_io_device_type : false;
79}
80
Dan Williamse07ecd72016-01-05 18:37:23 -080081static int is_uuid_busy(struct device *dev, void *data)
82{
83 u8 *uuid1 = data, *uuid2 = NULL;
84
85 if (is_namespace_pmem(dev)) {
86 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
87
88 uuid2 = nspm->uuid;
89 } else if (is_namespace_blk(dev)) {
90 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
91
92 uuid2 = nsblk->uuid;
93 } else if (is_nd_btt(dev)) {
94 struct nd_btt *nd_btt = to_nd_btt(dev);
95
96 uuid2 = nd_btt->uuid;
97 } else if (is_nd_pfn(dev)) {
98 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
99
100 uuid2 = nd_pfn->uuid;
101 }
102
103 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
104 return -EBUSY;
105
106 return 0;
107}
108
109static int is_namespace_uuid_busy(struct device *dev, void *data)
110{
111 if (is_nd_pmem(dev) || is_nd_blk(dev))
112 return device_for_each_child(dev, data, is_uuid_busy);
113 return 0;
114}
115
116/**
117 * nd_is_uuid_unique - verify that no other namespace has @uuid
118 * @dev: any device on a nvdimm_bus
119 * @uuid: uuid to check
120 */
121bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
122{
123 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
124
125 if (!nvdimm_bus)
126 return false;
127 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
128 if (device_for_each_child(&nvdimm_bus->dev, uuid,
129 is_namespace_uuid_busy) != 0)
130 return false;
131 return true;
132}
133
Dan Williams004f1af2015-08-24 19:20:23 -0400134bool pmem_should_map_pages(struct device *dev)
135{
136 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamscfe30b82016-03-03 09:38:00 -0800137 struct nd_namespace_io *nsio;
Dan Williams004f1af2015-08-24 19:20:23 -0400138
139 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
140 return false;
141
142 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
143 return false;
144
145 if (is_nd_pfn(dev) || is_nd_btt(dev))
146 return false;
147
Dan Williamscfe30b82016-03-03 09:38:00 -0800148 nsio = to_nd_namespace_io(dev);
149 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
150 IORESOURCE_SYSTEM_RAM,
151 IORES_DESC_NONE) == REGION_MIXED)
152 return false;
153
Dan Williams004f1af2015-08-24 19:20:23 -0400154#ifdef ARCH_MEMREMAP_PMEM
155 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
156#else
157 return false;
158#endif
159}
160EXPORT_SYMBOL(pmem_should_map_pages);
161
Vishal Verma5212e112015-06-25 04:20:32 -0400162const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
163 char *name)
164{
165 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400166 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400167
Dan Williams0731de02015-12-14 15:34:15 -0800168 if (ndns->claim && is_nd_btt(ndns->claim))
169 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400170
Dan Williams004f1af2015-08-24 19:20:23 -0400171 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams004f1af2015-08-24 19:20:23 -0400172 sprintf(name, "pmem%d%s", nd_region->id, suffix ? suffix : "");
173 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400174 struct nd_namespace_blk *nsblk;
175
176 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400177 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
178 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400179 } else {
180 return NULL;
181 }
182
183 return name;
184}
185EXPORT_SYMBOL(nvdimm_namespace_disk_name);
186
Vishal Verma6ec68952015-07-29 14:58:09 -0600187const u8 *nd_dev_to_uuid(struct device *dev)
188{
189 static const u8 null_uuid[16];
190
191 if (!dev)
192 return null_uuid;
193
194 if (is_namespace_pmem(dev)) {
195 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
196
197 return nspm->uuid;
198 } else if (is_namespace_blk(dev)) {
199 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
200
201 return nsblk->uuid;
202 } else
203 return null_uuid;
204}
205EXPORT_SYMBOL(nd_dev_to_uuid);
206
Dan Williams3d880022015-05-31 15:02:11 -0400207static ssize_t nstype_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 struct nd_region *nd_region = to_nd_region(dev->parent);
211
212 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
213}
214static DEVICE_ATTR_RO(nstype);
215
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400216static ssize_t __alt_name_store(struct device *dev, const char *buf,
217 const size_t len)
218{
219 char *input, *pos, *alt_name, **ns_altname;
220 ssize_t rc;
221
222 if (is_namespace_pmem(dev)) {
223 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
224
225 ns_altname = &nspm->alt_name;
226 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400227 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
228
229 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400230 } else
231 return -ENXIO;
232
Dan Williams8c2f7e82015-06-25 04:20:04 -0400233 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400234 return -EBUSY;
235
236 input = kmemdup(buf, len + 1, GFP_KERNEL);
237 if (!input)
238 return -ENOMEM;
239
240 input[len] = '\0';
241 pos = strim(input);
242 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
243 rc = -EINVAL;
244 goto out;
245 }
246
247 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
248 if (!alt_name) {
249 rc = -ENOMEM;
250 goto out;
251 }
252 kfree(*ns_altname);
253 *ns_altname = alt_name;
254 sprintf(*ns_altname, "%s", pos);
255 rc = len;
256
257out:
258 kfree(input);
259 return rc;
260}
261
Dan Williams1b40e092015-05-01 13:34:01 -0400262static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
263{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400264 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400265 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
266 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
267 struct nd_label_id label_id;
268 resource_size_t size = 0;
269 struct resource *res;
270
271 if (!nsblk->uuid)
272 return 0;
273 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
274 for_each_dpa_resource(ndd, res)
275 if (strcmp(res->name, label_id.id) == 0)
276 size += resource_size(res);
277 return size;
278}
279
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400280static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
281{
282 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
283 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
284 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
285 struct nd_label_id label_id;
286 struct resource *res;
287 int count, i;
288
289 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
290 return false;
291
292 count = 0;
293 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
294 for_each_dpa_resource(ndd, res) {
295 if (strcmp(res->name, label_id.id) != 0)
296 continue;
297 /*
Geert Uytterhoevenae551e92016-08-31 11:45:25 +0200298 * Resources with unacknowledged adjustments indicate a
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400299 * failure to update labels
300 */
301 if (res->flags & DPA_RESOURCE_ADJUSTED)
302 return false;
303 count++;
304 }
305
306 /* These values match after a successful label update */
307 if (count != nsblk->num_resources)
308 return false;
309
310 for (i = 0; i < nsblk->num_resources; i++) {
311 struct resource *found = NULL;
312
313 for_each_dpa_resource(ndd, res)
314 if (res == nsblk->res[i]) {
315 found = res;
316 break;
317 }
318 /* stale resource */
319 if (!found)
320 return false;
321 }
322
323 return true;
324}
325
326resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
327{
328 resource_size_t size;
329
330 nvdimm_bus_lock(&nsblk->common.dev);
331 size = __nd_namespace_blk_validate(nsblk);
332 nvdimm_bus_unlock(&nsblk->common.dev);
333
334 return size;
335}
336EXPORT_SYMBOL(nd_namespace_blk_validate);
337
338
Dan Williamsf524bf22015-05-30 12:36:02 -0400339static int nd_namespace_label_update(struct nd_region *nd_region,
340 struct device *dev)
341{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400342 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400343 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400344 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400345 return 0;
346
347 /*
348 * Only allow label writes that will result in a valid namespace
349 * or deletion of an existing namespace.
350 */
351 if (is_namespace_pmem(dev)) {
352 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400353 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400354
355 if (size == 0 && nspm->uuid)
356 /* delete allocation */;
357 else if (!nspm->uuid)
358 return 0;
359
360 return nd_pmem_namespace_label_update(nd_region, nspm, size);
361 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400362 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
363 resource_size_t size = nd_namespace_blk_size(nsblk);
364
365 if (size == 0 && nsblk->uuid)
366 /* delete allocation */;
367 else if (!nsblk->uuid || !nsblk->lbasize)
368 return 0;
369
370 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400371 } else
372 return -ENXIO;
373}
374
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400375static ssize_t alt_name_store(struct device *dev,
376 struct device_attribute *attr, const char *buf, size_t len)
377{
Dan Williamsf524bf22015-05-30 12:36:02 -0400378 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400379 ssize_t rc;
380
381 device_lock(dev);
382 nvdimm_bus_lock(dev);
383 wait_nvdimm_bus_probe_idle(dev);
384 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400385 if (rc >= 0)
386 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400387 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
388 nvdimm_bus_unlock(dev);
389 device_unlock(dev);
390
Dan Williamsf524bf22015-05-30 12:36:02 -0400391 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400392}
393
394static ssize_t alt_name_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
397 char *ns_altname;
398
399 if (is_namespace_pmem(dev)) {
400 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
401
402 ns_altname = nspm->alt_name;
403 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400404 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
405
406 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400407 } else
408 return -ENXIO;
409
410 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
411}
412static DEVICE_ATTR_RW(alt_name);
413
414static int scan_free(struct nd_region *nd_region,
415 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
416 resource_size_t n)
417{
418 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
419 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
420 int rc = 0;
421
422 while (n) {
423 struct resource *res, *last;
424 resource_size_t new_start;
425
426 last = NULL;
427 for_each_dpa_resource(ndd, res)
428 if (strcmp(res->name, label_id->id) == 0)
429 last = res;
430 res = last;
431 if (!res)
432 return 0;
433
434 if (n >= resource_size(res)) {
435 n -= resource_size(res);
436 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
437 nvdimm_free_dpa(ndd, res);
438 /* retry with last resource deleted */
439 continue;
440 }
441
442 /*
443 * Keep BLK allocations relegated to high DPA as much as
444 * possible
445 */
446 if (is_blk)
447 new_start = res->start + n;
448 else
449 new_start = res->start;
450
451 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400452 if (rc == 0)
453 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400454 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
455 break;
456 }
457
458 return rc;
459}
460
461/**
462 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
463 * @nd_region: the set of dimms to reclaim @n bytes from
464 * @label_id: unique identifier for the namespace consuming this dpa range
465 * @n: number of bytes per-dimm to release
466 *
467 * Assumes resources are ordered. Starting from the end try to
468 * adjust_resource() the allocation to @n, but if @n is larger than the
469 * allocation delete it and find the 'new' last allocation in the label
470 * set.
471 */
472static int shrink_dpa_allocation(struct nd_region *nd_region,
473 struct nd_label_id *label_id, resource_size_t n)
474{
475 int i;
476
477 for (i = 0; i < nd_region->ndr_mappings; i++) {
478 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
479 int rc;
480
481 rc = scan_free(nd_region, nd_mapping, label_id, n);
482 if (rc)
483 return rc;
484 }
485
486 return 0;
487}
488
489static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
490 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
491 resource_size_t n)
492{
493 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
494 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
495 resource_size_t first_dpa;
496 struct resource *res;
497 int rc = 0;
498
499 /* allocate blk from highest dpa first */
500 if (is_blk)
501 first_dpa = nd_mapping->start + nd_mapping->size - n;
502 else
503 first_dpa = nd_mapping->start;
504
505 /* first resource allocation for this label-id or dimm */
506 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
507 if (!res)
508 rc = -EBUSY;
509
510 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
511 return rc ? n : 0;
512}
513
Dan Williams1b40e092015-05-01 13:34:01 -0400514static bool space_valid(bool is_pmem, bool is_reserve,
515 struct nd_label_id *label_id, struct resource *res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400516{
517 /*
518 * For BLK-space any space is valid, for PMEM-space, it must be
Dan Williams1b40e092015-05-01 13:34:01 -0400519 * contiguous with an existing allocation unless we are
520 * reserving pmem.
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400521 */
Dan Williams1b40e092015-05-01 13:34:01 -0400522 if (is_reserve || !is_pmem)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400523 return true;
524 if (!res || strcmp(res->name, label_id->id) == 0)
525 return true;
526 return false;
527}
528
529enum alloc_loc {
530 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
531};
532
533static resource_size_t scan_allocate(struct nd_region *nd_region,
534 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
535 resource_size_t n)
536{
537 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -0400538 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400539 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
540 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
541 const resource_size_t to_allocate = n;
542 struct resource *res;
543 int first;
544
545 retry:
546 first = 0;
547 for_each_dpa_resource(ndd, res) {
548 resource_size_t allocate, available = 0, free_start, free_end;
549 struct resource *next = res->sibling, *new_res = NULL;
550 enum alloc_loc loc = ALLOC_ERR;
551 const char *action;
552 int rc = 0;
553
554 /* ignore resources outside this nd_mapping */
555 if (res->start > mapping_end)
556 continue;
557 if (res->end < nd_mapping->start)
558 continue;
559
560 /* space at the beginning of the mapping */
561 if (!first++ && res->start > nd_mapping->start) {
562 free_start = nd_mapping->start;
563 available = res->start - free_start;
Dan Williams1b40e092015-05-01 13:34:01 -0400564 if (space_valid(is_pmem, is_reserve, label_id, NULL))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400565 loc = ALLOC_BEFORE;
566 }
567
568 /* space between allocations */
569 if (!loc && next) {
570 free_start = res->start + resource_size(res);
571 free_end = min(mapping_end, next->start - 1);
Dan Williams1b40e092015-05-01 13:34:01 -0400572 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400573 && free_start < free_end) {
574 available = free_end + 1 - free_start;
575 loc = ALLOC_MID;
576 }
577 }
578
579 /* space at the end of the mapping */
580 if (!loc && !next) {
581 free_start = res->start + resource_size(res);
582 free_end = mapping_end;
Dan Williams1b40e092015-05-01 13:34:01 -0400583 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400584 && free_start < free_end) {
585 available = free_end + 1 - free_start;
586 loc = ALLOC_AFTER;
587 }
588 }
589
590 if (!loc || !available)
591 continue;
592 allocate = min(available, n);
593 switch (loc) {
594 case ALLOC_BEFORE:
595 if (strcmp(res->name, label_id->id) == 0) {
596 /* adjust current resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400597 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400598 return n;
599 rc = adjust_resource(res, res->start - allocate,
600 resource_size(res) + allocate);
601 action = "cur grow up";
602 } else
603 action = "allocate";
604 break;
605 case ALLOC_MID:
606 if (strcmp(next->name, label_id->id) == 0) {
607 /* adjust next resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400608 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400609 return n;
610 rc = adjust_resource(next, next->start
611 - allocate, resource_size(next)
612 + allocate);
613 new_res = next;
614 action = "next grow up";
615 } else if (strcmp(res->name, label_id->id) == 0) {
616 action = "grow down";
617 } else
618 action = "allocate";
619 break;
620 case ALLOC_AFTER:
621 if (strcmp(res->name, label_id->id) == 0)
622 action = "grow down";
623 else
624 action = "allocate";
625 break;
626 default:
627 return n;
628 }
629
630 if (strcmp(action, "allocate") == 0) {
631 /* BLK allocate bottom up */
632 if (!is_pmem)
633 free_start += available - allocate;
Dan Williams1b40e092015-05-01 13:34:01 -0400634 else if (!is_reserve && free_start != nd_mapping->start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400635 return n;
636
637 new_res = nvdimm_allocate_dpa(ndd, label_id,
638 free_start, allocate);
639 if (!new_res)
640 rc = -EBUSY;
641 } else if (strcmp(action, "grow down") == 0) {
642 /* adjust current resource down */
643 rc = adjust_resource(res, res->start, resource_size(res)
644 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400645 if (rc == 0)
646 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400647 }
648
649 if (!new_res)
650 new_res = res;
651
652 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
653 action, loc, rc);
654
655 if (rc)
656 return n;
657
658 n -= allocate;
659 if (n) {
660 /*
661 * Retry scan with newly inserted resources.
662 * For example, if we did an ALLOC_BEFORE
663 * insertion there may also have been space
664 * available for an ALLOC_AFTER insertion, so we
665 * need to check this same resource again
666 */
667 goto retry;
668 } else
669 return 0;
670 }
671
Dan Williams1b40e092015-05-01 13:34:01 -0400672 /*
673 * If we allocated nothing in the BLK case it may be because we are in
674 * an initial "pmem-reserve pass". Only do an initial BLK allocation
675 * when none of the DPA space is reserved.
676 */
677 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400678 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
679 return n;
680}
681
Dan Williams1b40e092015-05-01 13:34:01 -0400682static int merge_dpa(struct nd_region *nd_region,
683 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
684{
685 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
686 struct resource *res;
687
688 if (strncmp("pmem", label_id->id, 4) == 0)
689 return 0;
690 retry:
691 for_each_dpa_resource(ndd, res) {
692 int rc;
693 struct resource *next = res->sibling;
694 resource_size_t end = res->start + resource_size(res);
695
696 if (!next || strcmp(res->name, label_id->id) != 0
697 || strcmp(next->name, label_id->id) != 0
698 || end != next->start)
699 continue;
700 end += resource_size(next);
701 nvdimm_free_dpa(ndd, next);
702 rc = adjust_resource(res, res->start, end - res->start);
703 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
704 if (rc)
705 return rc;
706 res->flags |= DPA_RESOURCE_ADJUSTED;
707 goto retry;
708 }
709
710 return 0;
711}
712
713static int __reserve_free_pmem(struct device *dev, void *data)
714{
715 struct nvdimm *nvdimm = data;
716 struct nd_region *nd_region;
717 struct nd_label_id label_id;
718 int i;
719
720 if (!is_nd_pmem(dev))
721 return 0;
722
723 nd_region = to_nd_region(dev);
724 if (nd_region->ndr_mappings == 0)
725 return 0;
726
727 memset(&label_id, 0, sizeof(label_id));
728 strcat(label_id.id, "pmem-reserve");
729 for (i = 0; i < nd_region->ndr_mappings; i++) {
730 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
731 resource_size_t n, rem = 0;
732
733 if (nd_mapping->nvdimm != nvdimm)
734 continue;
735
736 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
737 if (n == 0)
738 return 0;
739 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
740 dev_WARN_ONCE(&nd_region->dev, rem,
741 "pmem reserve underrun: %#llx of %#llx bytes\n",
742 (unsigned long long) n - rem,
743 (unsigned long long) n);
744 return rem ? -ENXIO : 0;
745 }
746
747 return 0;
748}
749
750static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
751 struct nd_mapping *nd_mapping)
752{
753 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
754 struct resource *res, *_res;
755
756 for_each_dpa_resource_safe(ndd, res, _res)
757 if (strcmp(res->name, "pmem-reserve") == 0)
758 nvdimm_free_dpa(ndd, res);
759}
760
761static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
762 struct nd_mapping *nd_mapping)
763{
764 struct nvdimm *nvdimm = nd_mapping->nvdimm;
765 int rc;
766
767 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
768 __reserve_free_pmem);
769 if (rc)
770 release_free_pmem(nvdimm_bus, nd_mapping);
771 return rc;
772}
773
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400774/**
775 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
776 * @nd_region: the set of dimms to allocate @n more bytes from
777 * @label_id: unique identifier for the namespace consuming this dpa range
778 * @n: number of bytes per-dimm to add to the existing allocation
779 *
780 * Assumes resources are ordered. For BLK regions, first consume
781 * BLK-only available DPA free space, then consume PMEM-aliased DPA
782 * space starting at the highest DPA. For PMEM regions start
783 * allocations from the start of an interleave set and end at the first
784 * BLK allocation or the end of the interleave set, whichever comes
785 * first.
786 */
787static int grow_dpa_allocation(struct nd_region *nd_region,
788 struct nd_label_id *label_id, resource_size_t n)
789{
Dan Williams1b40e092015-05-01 13:34:01 -0400790 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
791 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400792 int i;
793
794 for (i = 0; i < nd_region->ndr_mappings; i++) {
795 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400796 resource_size_t rem = n;
797 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400798
Dan Williams1b40e092015-05-01 13:34:01 -0400799 /*
800 * In the BLK case try once with all unallocated PMEM
801 * reserved, and once without
802 */
803 for (j = is_pmem; j < 2; j++) {
804 bool blk_only = j == 0;
805
806 if (blk_only) {
807 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
808 if (rc)
809 return rc;
810 }
811 rem = scan_allocate(nd_region, nd_mapping,
812 label_id, rem);
813 if (blk_only)
814 release_free_pmem(nvdimm_bus, nd_mapping);
815
816 /* try again and allow encroachments into PMEM */
817 if (rem == 0)
818 break;
819 }
820
821 dev_WARN_ONCE(&nd_region->dev, rem,
822 "allocation underrun: %#llx of %#llx bytes\n",
823 (unsigned long long) n - rem,
824 (unsigned long long) n);
825 if (rem)
826 return -ENXIO;
827
828 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400829 if (rc)
830 return rc;
831 }
832
833 return 0;
834}
835
836static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
837 struct nd_namespace_pmem *nspm, resource_size_t size)
838{
839 struct resource *res = &nspm->nsio.res;
840
841 res->start = nd_region->ndr_start;
842 res->end = nd_region->ndr_start + size - 1;
843}
844
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300845static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
846{
847 if (!uuid) {
848 dev_dbg(dev, "%s: uuid not set\n", where);
849 return true;
850 }
851 return false;
852}
853
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400854static ssize_t __size_store(struct device *dev, unsigned long long val)
855{
856 resource_size_t allocated = 0, available = 0;
857 struct nd_region *nd_region = to_nd_region(dev->parent);
858 struct nd_mapping *nd_mapping;
859 struct nvdimm_drvdata *ndd;
860 struct nd_label_id label_id;
861 u32 flags = 0, remainder;
862 u8 *uuid = NULL;
863 int rc, i;
864
Dan Williams8c2f7e82015-06-25 04:20:04 -0400865 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400866 return -EBUSY;
867
868 if (is_namespace_pmem(dev)) {
869 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
870
871 uuid = nspm->uuid;
872 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400873 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
874
875 uuid = nsblk->uuid;
876 flags = NSLABEL_FLAG_LOCAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400877 }
878
879 /*
880 * We need a uuid for the allocation-label and dimm(s) on which
881 * to store the label.
882 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300883 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400884 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300885 if (nd_region->ndr_mappings == 0) {
886 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
887 return -ENXIO;
888 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400889
890 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
891 if (remainder) {
892 dev_dbg(dev, "%llu is not %dK aligned\n", val,
893 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
894 return -EINVAL;
895 }
896
897 nd_label_gen_id(&label_id, uuid, flags);
898 for (i = 0; i < nd_region->ndr_mappings; i++) {
899 nd_mapping = &nd_region->mapping[i];
900 ndd = to_ndd(nd_mapping);
901
902 /*
903 * All dimms in an interleave set, or the base dimm for a blk
904 * region, need to be enabled for the size to be changed.
905 */
906 if (!ndd)
907 return -ENXIO;
908
909 allocated += nvdimm_allocated_dpa(ndd, &label_id);
910 }
911 available = nd_region_available_dpa(nd_region);
912
913 if (val > available + allocated)
914 return -ENOSPC;
915
916 if (val == allocated)
917 return 0;
918
919 val = div_u64(val, nd_region->ndr_mappings);
920 allocated = div_u64(allocated, nd_region->ndr_mappings);
921 if (val < allocated)
922 rc = shrink_dpa_allocation(nd_region, &label_id,
923 allocated - val);
924 else
925 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
926
927 if (rc)
928 return rc;
929
930 if (is_namespace_pmem(dev)) {
931 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
932
933 nd_namespace_pmem_set_size(nd_region, nspm,
934 val * nd_region->ndr_mappings);
Dan Williams1b40e092015-05-01 13:34:01 -0400935 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400936 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
937
Dan Williams1b40e092015-05-01 13:34:01 -0400938 /*
939 * Try to delete the namespace if we deleted all of its
Dan Williams8c2f7e82015-06-25 04:20:04 -0400940 * allocation, this is not the seed device for the
941 * region, and it is not actively claimed by a btt
942 * instance.
Dan Williams1b40e092015-05-01 13:34:01 -0400943 */
Dan Williams8c2f7e82015-06-25 04:20:04 -0400944 if (val == 0 && nd_region->ns_seed != dev
945 && !nsblk->common.claim)
Dan Williams1b40e092015-05-01 13:34:01 -0400946 nd_device_unregister(dev, ND_ASYNC);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400947 }
948
949 return rc;
950}
951
952static ssize_t size_store(struct device *dev,
953 struct device_attribute *attr, const char *buf, size_t len)
954{
Dan Williamsf524bf22015-05-30 12:36:02 -0400955 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400956 unsigned long long val;
957 u8 **uuid = NULL;
958 int rc;
959
960 rc = kstrtoull(buf, 0, &val);
961 if (rc)
962 return rc;
963
964 device_lock(dev);
965 nvdimm_bus_lock(dev);
966 wait_nvdimm_bus_probe_idle(dev);
967 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -0400968 if (rc >= 0)
969 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400970
971 if (is_namespace_pmem(dev)) {
972 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
973
974 uuid = &nspm->uuid;
975 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400976 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
977
978 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400979 }
980
981 if (rc == 0 && val == 0 && uuid) {
982 /* setting size zero == 'delete namespace' */
983 kfree(*uuid);
984 *uuid = NULL;
985 }
986
987 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
988 ? "fail" : "success", rc);
989
990 nvdimm_bus_unlock(dev);
991 device_unlock(dev);
992
Dan Williamsf524bf22015-05-30 12:36:02 -0400993 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400994}
995
Dan Williams8c2f7e82015-06-25 04:20:04 -0400996resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400997{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400998 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -0400999
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001000 if (is_namespace_pmem(dev)) {
1001 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1002
Dan Williams8c2f7e82015-06-25 04:20:04 -04001003 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001004 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -04001005 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001006 } else if (is_namespace_io(dev)) {
1007 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1008
Dan Williams8c2f7e82015-06-25 04:20:04 -04001009 return resource_size(&nsio->res);
1010 } else
1011 WARN_ONCE(1, "unknown namespace type\n");
1012 return 0;
1013}
Dan Williams1b40e092015-05-01 13:34:01 -04001014
Dan Williams8c2f7e82015-06-25 04:20:04 -04001015resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1016{
1017 resource_size_t size;
1018
1019 nvdimm_bus_lock(&ndns->dev);
1020 size = __nvdimm_namespace_capacity(ndns);
1021 nvdimm_bus_unlock(&ndns->dev);
1022
1023 return size;
1024}
1025EXPORT_SYMBOL(nvdimm_namespace_capacity);
1026
1027static ssize_t size_show(struct device *dev,
1028 struct device_attribute *attr, char *buf)
1029{
1030 return sprintf(buf, "%llu\n", (unsigned long long)
1031 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001032}
1033static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
1034
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001035static u8 *namespace_to_uuid(struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001036{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001037 if (is_namespace_pmem(dev)) {
1038 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1039
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001040 return nspm->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001041 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001042 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1043
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001044 return nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001045 } else
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001046 return ERR_PTR(-ENXIO);
1047}
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001048
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001049static ssize_t uuid_show(struct device *dev,
1050 struct device_attribute *attr, char *buf)
1051{
1052 u8 *uuid = namespace_to_uuid(dev);
1053
1054 if (IS_ERR(uuid))
1055 return PTR_ERR(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001056 if (uuid)
1057 return sprintf(buf, "%pUb\n", uuid);
1058 return sprintf(buf, "\n");
1059}
1060
1061/**
1062 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1063 * @nd_region: parent region so we can updates all dimms in the set
1064 * @dev: namespace type for generating label_id
1065 * @new_uuid: incoming uuid
1066 * @old_uuid: reference to the uuid storage location in the namespace object
1067 */
1068static int namespace_update_uuid(struct nd_region *nd_region,
1069 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1070{
1071 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1072 struct nd_label_id old_label_id;
1073 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001074 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001075
Dan Williamsf524bf22015-05-30 12:36:02 -04001076 if (!nd_is_uuid_unique(dev, new_uuid))
1077 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001078
1079 if (*old_uuid == NULL)
1080 goto out;
1081
Dan Williamsf524bf22015-05-30 12:36:02 -04001082 /*
1083 * If we've already written a label with this uuid, then it's
1084 * too late to rename because we can't reliably update the uuid
1085 * without losing the old namespace. Userspace must delete this
1086 * namespace to abandon the old uuid.
1087 */
1088 for (i = 0; i < nd_region->ndr_mappings; i++) {
1089 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1090
1091 /*
1092 * This check by itself is sufficient because old_uuid
1093 * would be NULL above if this uuid did not exist in the
1094 * currently written set.
1095 *
1096 * FIXME: can we delete uuid with zero dpa allocated?
1097 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001098 if (list_empty(&nd_mapping->labels))
Dan Williamsf524bf22015-05-30 12:36:02 -04001099 return -EBUSY;
1100 }
1101
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001102 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1103 nd_label_gen_id(&new_label_id, new_uuid, flags);
1104 for (i = 0; i < nd_region->ndr_mappings; i++) {
1105 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1106 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1107 struct resource *res;
1108
1109 for_each_dpa_resource(ndd, res)
1110 if (strcmp(res->name, old_label_id.id) == 0)
1111 sprintf((void *) res->name, "%s",
1112 new_label_id.id);
1113 }
1114 kfree(*old_uuid);
1115 out:
1116 *old_uuid = new_uuid;
1117 return 0;
1118}
1119
1120static ssize_t uuid_store(struct device *dev,
1121 struct device_attribute *attr, const char *buf, size_t len)
1122{
1123 struct nd_region *nd_region = to_nd_region(dev->parent);
1124 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001125 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001126 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001127
1128 if (is_namespace_pmem(dev)) {
1129 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1130
1131 ns_uuid = &nspm->uuid;
1132 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001133 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1134
1135 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001136 } else
1137 return -ENXIO;
1138
1139 device_lock(dev);
1140 nvdimm_bus_lock(dev);
1141 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001142 if (to_ndns(dev)->claim)
1143 rc = -EBUSY;
1144 if (rc >= 0)
1145 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001146 if (rc >= 0)
1147 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001148 if (rc >= 0)
1149 rc = nd_namespace_label_update(nd_region, dev);
1150 else
1151 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001152 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1153 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1154 nvdimm_bus_unlock(dev);
1155 device_unlock(dev);
1156
Dan Williamsf524bf22015-05-30 12:36:02 -04001157 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001158}
1159static DEVICE_ATTR_RW(uuid);
1160
1161static ssize_t resource_show(struct device *dev,
1162 struct device_attribute *attr, char *buf)
1163{
1164 struct resource *res;
1165
1166 if (is_namespace_pmem(dev)) {
1167 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1168
1169 res = &nspm->nsio.res;
1170 } else if (is_namespace_io(dev)) {
1171 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1172
1173 res = &nsio->res;
1174 } else
1175 return -ENXIO;
1176
1177 /* no address to convey if the namespace has no allocation */
1178 if (resource_size(res) == 0)
1179 return -ENXIO;
1180 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1181}
1182static DEVICE_ATTR_RO(resource);
1183
Vishal Vermafcae6952015-06-25 04:22:39 -04001184static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1185 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001186
1187static ssize_t sector_size_show(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1189{
1190 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1191
1192 if (!is_namespace_blk(dev))
1193 return -ENXIO;
1194
1195 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1196}
1197
1198static ssize_t sector_size_store(struct device *dev,
1199 struct device_attribute *attr, const char *buf, size_t len)
1200{
1201 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williamsf524bf22015-05-30 12:36:02 -04001202 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001203 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001204
1205 if (!is_namespace_blk(dev))
1206 return -ENXIO;
1207
1208 device_lock(dev);
1209 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001210 if (to_ndns(dev)->claim)
1211 rc = -EBUSY;
1212 if (rc >= 0)
1213 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1214 ns_lbasize_supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001215 if (rc >= 0)
1216 rc = nd_namespace_label_update(nd_region, dev);
1217 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1218 rc, rc < 0 ? "tried" : "wrote", buf,
1219 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001220 nvdimm_bus_unlock(dev);
1221 device_unlock(dev);
1222
1223 return rc ? rc : len;
1224}
1225static DEVICE_ATTR_RW(sector_size);
1226
Dan Williams0ba1c632015-05-30 12:35:36 -04001227static ssize_t dpa_extents_show(struct device *dev,
1228 struct device_attribute *attr, char *buf)
1229{
1230 struct nd_region *nd_region = to_nd_region(dev->parent);
1231 struct nd_label_id label_id;
1232 int count = 0, i;
1233 u8 *uuid = NULL;
1234 u32 flags = 0;
1235
1236 nvdimm_bus_lock(dev);
1237 if (is_namespace_pmem(dev)) {
1238 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1239
1240 uuid = nspm->uuid;
1241 flags = 0;
1242 } else if (is_namespace_blk(dev)) {
1243 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1244
1245 uuid = nsblk->uuid;
1246 flags = NSLABEL_FLAG_LOCAL;
1247 }
1248
1249 if (!uuid)
1250 goto out;
1251
1252 nd_label_gen_id(&label_id, uuid, flags);
1253 for (i = 0; i < nd_region->ndr_mappings; i++) {
1254 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1255 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1256 struct resource *res;
1257
1258 for_each_dpa_resource(ndd, res)
1259 if (strcmp(res->name, label_id.id) == 0)
1260 count++;
1261 }
1262 out:
1263 nvdimm_bus_unlock(dev);
1264
1265 return sprintf(buf, "%d\n", count);
1266}
1267static DEVICE_ATTR_RO(dpa_extents);
1268
Dan Williams8c2f7e82015-06-25 04:20:04 -04001269static ssize_t holder_show(struct device *dev,
1270 struct device_attribute *attr, char *buf)
1271{
1272 struct nd_namespace_common *ndns = to_ndns(dev);
1273 ssize_t rc;
1274
1275 device_lock(dev);
1276 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1277 device_unlock(dev);
1278
1279 return rc;
1280}
1281static DEVICE_ATTR_RO(holder);
1282
Dan Williams0731de02015-12-14 15:34:15 -08001283static ssize_t mode_show(struct device *dev,
1284 struct device_attribute *attr, char *buf)
1285{
1286 struct nd_namespace_common *ndns = to_ndns(dev);
1287 struct device *claim;
1288 char *mode;
1289 ssize_t rc;
1290
1291 device_lock(dev);
1292 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001293 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001294 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001295 else if (claim && is_nd_pfn(claim))
1296 mode = "memory";
Dan Williamscd034122016-03-11 10:15:36 -08001297 else if (claim && is_nd_dax(claim))
1298 mode = "dax";
Dan Williams9c412422016-01-23 15:34:10 -08001299 else if (!claim && pmem_should_map_pages(dev))
1300 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001301 else
1302 mode = "raw";
1303 rc = sprintf(buf, "%s\n", mode);
1304 device_unlock(dev);
1305
1306 return rc;
1307}
1308static DEVICE_ATTR_RO(mode);
1309
Dan Williams8c2f7e82015-06-25 04:20:04 -04001310static ssize_t force_raw_store(struct device *dev,
1311 struct device_attribute *attr, const char *buf, size_t len)
1312{
1313 bool force_raw;
1314 int rc = strtobool(buf, &force_raw);
1315
1316 if (rc)
1317 return rc;
1318
1319 to_ndns(dev)->force_raw = force_raw;
1320 return len;
1321}
1322
1323static ssize_t force_raw_show(struct device *dev,
1324 struct device_attribute *attr, char *buf)
1325{
1326 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1327}
1328static DEVICE_ATTR_RW(force_raw);
1329
Dan Williams3d880022015-05-31 15:02:11 -04001330static struct attribute *nd_namespace_attributes[] = {
1331 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001332 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001333 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001334 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001335 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001336 &dev_attr_resource.attr,
1337 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001338 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001339 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001340 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001341 NULL,
1342};
1343
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001344static umode_t namespace_visible(struct kobject *kobj,
1345 struct attribute *a, int n)
1346{
1347 struct device *dev = container_of(kobj, struct device, kobj);
1348
1349 if (a == &dev_attr_resource.attr) {
1350 if (is_namespace_blk(dev))
1351 return 0;
1352 return a->mode;
1353 }
1354
1355 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1356 if (a == &dev_attr_size.attr)
1357 return S_IWUSR | S_IRUGO;
Dan Williams1b40e092015-05-01 13:34:01 -04001358
1359 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1360 return 0;
1361
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001362 return a->mode;
1363 }
1364
Dan Williams8c2f7e82015-06-25 04:20:04 -04001365 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1366 || a == &dev_attr_holder.attr
Dan Williams0731de02015-12-14 15:34:15 -08001367 || a == &dev_attr_force_raw.attr
1368 || a == &dev_attr_mode.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001369 return a->mode;
1370
1371 return 0;
1372}
1373
Dan Williams3d880022015-05-31 15:02:11 -04001374static struct attribute_group nd_namespace_attribute_group = {
1375 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001376 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001377};
1378
1379static const struct attribute_group *nd_namespace_attribute_groups[] = {
1380 &nd_device_attribute_group,
1381 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001382 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001383 NULL,
1384};
1385
Dan Williams8c2f7e82015-06-25 04:20:04 -04001386struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1387{
1388 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001389 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williamscd034122016-03-11 10:15:36 -08001390 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001391 struct nd_namespace_common *ndns = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001392 resource_size_t size;
1393
Dan Williamscd034122016-03-11 10:15:36 -08001394 if (nd_btt || nd_pfn || nd_dax) {
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001395 if (nd_btt)
Dan Williamse1455742015-07-30 17:57:47 -04001396 ndns = nd_btt->ndns;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001397 else if (nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -04001398 ndns = nd_pfn->ndns;
Dan Williamscd034122016-03-11 10:15:36 -08001399 else if (nd_dax)
1400 ndns = nd_dax->nd_pfn.ndns;
Dan Williamse1455742015-07-30 17:57:47 -04001401
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001402 if (!ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001403 return ERR_PTR(-ENODEV);
1404
1405 /*
1406 * Flush any in-progess probes / removals in the driver
1407 * for the raw personality of this namespace.
1408 */
1409 device_lock(&ndns->dev);
1410 device_unlock(&ndns->dev);
1411 if (ndns->dev.driver) {
1412 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001413 dev_name(dev));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001414 return ERR_PTR(-EBUSY);
1415 }
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001416 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001417 "host (%s) vs claim (%s) mismatch\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001418 dev_name(dev),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001419 dev_name(ndns->claim)))
1420 return ERR_PTR(-ENXIO);
1421 } else {
1422 ndns = to_ndns(dev);
1423 if (ndns->claim) {
1424 dev_dbg(dev, "claimed by %s, failing probe\n",
1425 dev_name(ndns->claim));
1426
1427 return ERR_PTR(-ENXIO);
1428 }
1429 }
1430
1431 size = nvdimm_namespace_capacity(ndns);
1432 if (size < ND_MIN_NAMESPACE_SIZE) {
1433 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1434 &size, ND_MIN_NAMESPACE_SIZE);
1435 return ERR_PTR(-ENODEV);
1436 }
1437
1438 if (is_namespace_pmem(&ndns->dev)) {
1439 struct nd_namespace_pmem *nspm;
1440
1441 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001442 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001443 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001444 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001445 struct nd_namespace_blk *nsblk;
1446
1447 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001448 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1449 return ERR_PTR(-ENODEV);
1450 if (!nsblk->lbasize) {
1451 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1452 __func__);
1453 return ERR_PTR(-ENODEV);
1454 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001455 if (!nd_namespace_blk_validate(nsblk))
1456 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001457 }
1458
1459 return ndns;
1460}
1461EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1462
Dan Williams3d880022015-05-31 15:02:11 -04001463static struct device **create_namespace_io(struct nd_region *nd_region)
1464{
1465 struct nd_namespace_io *nsio;
1466 struct device *dev, **devs;
1467 struct resource *res;
1468
1469 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1470 if (!nsio)
1471 return NULL;
1472
1473 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1474 if (!devs) {
1475 kfree(nsio);
1476 return NULL;
1477 }
1478
Dan Williams8c2f7e82015-06-25 04:20:04 -04001479 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001480 dev->type = &namespace_io_device_type;
1481 dev->parent = &nd_region->dev;
1482 res = &nsio->res;
1483 res->name = dev_name(&nd_region->dev);
1484 res->flags = IORESOURCE_MEM;
1485 res->start = nd_region->ndr_start;
1486 res->end = res->start + nd_region->ndr_size - 1;
1487
1488 devs[0] = dev;
1489 return devs;
1490}
1491
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001492static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1493 u64 cookie, u16 pos)
1494{
1495 struct nd_namespace_label *found = NULL;
1496 int i;
1497
1498 for (i = 0; i < nd_region->ndr_mappings; i++) {
1499 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001500 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001501 bool found_uuid = false;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001502
Dan Williamsae8219f2016-09-19 16:04:21 -07001503 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1504 struct nd_namespace_label *nd_label = label_ent->label;
1505 u16 position, nlabel;
1506 u64 isetcookie;
1507
1508 if (!nd_label)
1509 continue;
1510 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1511 position = __le16_to_cpu(nd_label->position);
1512 nlabel = __le16_to_cpu(nd_label->nlabel);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001513
1514 if (isetcookie != cookie)
1515 continue;
1516
1517 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1518 continue;
1519
1520 if (found_uuid) {
1521 dev_dbg(to_ndd(nd_mapping)->dev,
1522 "%s duplicate entry for uuid\n",
1523 __func__);
1524 return false;
1525 }
1526 found_uuid = true;
1527 if (nlabel != nd_region->ndr_mappings)
1528 continue;
1529 if (position != pos)
1530 continue;
1531 found = nd_label;
1532 break;
1533 }
1534 if (found)
1535 break;
1536 }
1537 return found != NULL;
1538}
1539
1540static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1541{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001542 int i;
1543
1544 if (!pmem_id)
1545 return -ENODEV;
1546
1547 for (i = 0; i < nd_region->ndr_mappings; i++) {
1548 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001549 struct nd_namespace_label *nd_label = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001550 u64 hw_start, hw_end, pmem_start, pmem_end;
Dan Williamsae8219f2016-09-19 16:04:21 -07001551 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001552
Dan Williams8a5f50d2016-09-22 15:42:59 -07001553 WARN_ON(!mutex_is_locked(&nd_mapping->lock));
Dan Williamsae8219f2016-09-19 16:04:21 -07001554 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1555 nd_label = label_ent->label;
1556 if (!nd_label)
1557 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001558 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1559 break;
Dan Williamsae8219f2016-09-19 16:04:21 -07001560 nd_label = NULL;
1561 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001562
1563 if (!nd_label) {
1564 WARN_ON(1);
1565 return -EINVAL;
1566 }
1567
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001568 /*
1569 * Check that this label is compliant with the dpa
1570 * range published in NFIT
1571 */
1572 hw_start = nd_mapping->start;
1573 hw_end = hw_start + nd_mapping->size;
Dan Williamsae8219f2016-09-19 16:04:21 -07001574 pmem_start = __le64_to_cpu(nd_label->dpa);
1575 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001576 if (pmem_start == hw_start && pmem_end <= hw_end)
1577 /* pass */;
1578 else
1579 return -EINVAL;
1580
Dan Williams8a5f50d2016-09-22 15:42:59 -07001581 /* move recently validated label to the front of the list */
1582 list_move(&label_ent->list, &nd_mapping->labels);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001583 }
1584 return 0;
1585}
1586
1587/**
Dan Williams8a5f50d2016-09-22 15:42:59 -07001588 * create_namespace_pmem - validate interleave set labelling, retrieve label0
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001589 * @nd_region: region with mappings to validate
Dan Williams8a5f50d2016-09-22 15:42:59 -07001590 * @nspm: target namespace to create
1591 * @nd_label: target pmem namespace label to evaluate
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001592 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001593struct device *create_namespace_pmem(struct nd_region *nd_region,
1594 struct nd_namespace_label *nd_label)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001595{
1596 u64 cookie = nd_region_interleave_set_cookie(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07001597 struct nd_label_ent *label_ent;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001598 struct nd_namespace_pmem *nspm;
Dan Williamsae8219f2016-09-19 16:04:21 -07001599 struct nd_mapping *nd_mapping;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001600 resource_size_t size = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001601 struct resource *res;
1602 struct device *dev;
Dan Williamsae8219f2016-09-19 16:04:21 -07001603 int rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001604 u16 i;
1605
Dan Williams47652182016-09-15 18:08:05 -07001606 if (cookie == 0) {
1607 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
Dan Williams8a5f50d2016-09-22 15:42:59 -07001608 return ERR_PTR(-ENXIO);
Dan Williams47652182016-09-15 18:08:05 -07001609 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001610
Dan Williams8a5f50d2016-09-22 15:42:59 -07001611 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1612 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1613 nd_label->uuid);
1614 return ERR_PTR(-EAGAIN);
Dan Williamsae8219f2016-09-19 16:04:21 -07001615 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001616
Dan Williams8a5f50d2016-09-22 15:42:59 -07001617 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1618 if (!nspm)
1619 return ERR_PTR(-ENOMEM);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001620
Dan Williams8a5f50d2016-09-22 15:42:59 -07001621 dev = &nspm->nsio.common.dev;
1622 dev->type = &namespace_pmem_device_type;
1623 dev->parent = &nd_region->dev;
1624 res = &nspm->nsio.res;
1625 res->name = dev_name(&nd_region->dev);
1626 res->flags = IORESOURCE_MEM;
1627
1628 for (i = 0; i < nd_region->ndr_mappings; i++)
1629 if (!has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
Dan Williamsae8219f2016-09-19 16:04:21 -07001630 break;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001631 if (i < nd_region->ndr_mappings) {
1632 /*
1633 * Give up if we don't find an instance of a uuid at each
1634 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1635 * find a dimm with two instances of the same uuid.
1636 */
1637 rc = -EINVAL;
Dan Williamsae8219f2016-09-19 16:04:21 -07001638 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001639 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001640
1641 /*
1642 * Fix up each mapping's 'labels' to have the validated pmem label for
1643 * that position at labels[0], and NULL at labels[1]. In the process,
1644 * check that the namespace aligns with interleave-set. We know
1645 * that it does not overlap with any blk namespaces by virtue of
1646 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1647 * succeeded).
1648 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001649 rc = select_pmem_id(nd_region, nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001650 if (rc)
1651 goto err;
1652
1653 /* Calculate total size and populate namespace properties from label0 */
1654 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsae8219f2016-09-19 16:04:21 -07001655 struct nd_namespace_label *label0;
1656
1657 nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001658 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1659 typeof(*label_ent), list);
1660 label0 = label_ent ? label_ent->label : 0;
Dan Williamsae8219f2016-09-19 16:04:21 -07001661
1662 if (!label0) {
1663 WARN_ON(1);
1664 continue;
1665 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001666
1667 size += __le64_to_cpu(label0->rawsize);
1668 if (__le16_to_cpu(label0->position) != 0)
1669 continue;
1670 WARN_ON(nspm->alt_name || nspm->uuid);
1671 nspm->alt_name = kmemdup((void __force *) label0->name,
1672 NSLABEL_NAME_LEN, GFP_KERNEL);
1673 nspm->uuid = kmemdup((void __force *) label0->uuid,
1674 NSLABEL_UUID_LEN, GFP_KERNEL);
1675 }
1676
1677 if (!nspm->alt_name || !nspm->uuid) {
1678 rc = -ENOMEM;
1679 goto err;
1680 }
1681
1682 nd_namespace_pmem_set_size(nd_region, nspm, size);
1683
Dan Williams8a5f50d2016-09-22 15:42:59 -07001684 return dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001685 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07001686 namespace_pmem_release(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001687 switch (rc) {
1688 case -EINVAL:
1689 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1690 break;
1691 case -ENODEV:
1692 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1693 break;
1694 default:
1695 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1696 __func__, rc);
1697 break;
1698 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07001699 return ERR_PTR(rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001700}
1701
Dan Williams1b40e092015-05-01 13:34:01 -04001702struct resource *nsblk_add_resource(struct nd_region *nd_region,
1703 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1704 resource_size_t start)
1705{
1706 struct nd_label_id label_id;
1707 struct resource *res;
1708
1709 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1710 res = krealloc(nsblk->res,
1711 sizeof(void *) * (nsblk->num_resources + 1),
1712 GFP_KERNEL);
1713 if (!res)
1714 return NULL;
1715 nsblk->res = (struct resource **) res;
1716 for_each_dpa_resource(ndd, res)
1717 if (strcmp(res->name, label_id.id) == 0
1718 && res->start == start) {
1719 nsblk->res[nsblk->num_resources++] = res;
1720 return res;
1721 }
1722 return NULL;
1723}
1724
1725static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1726{
1727 struct nd_namespace_blk *nsblk;
1728 struct device *dev;
1729
1730 if (!is_nd_blk(&nd_region->dev))
1731 return NULL;
1732
1733 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1734 if (!nsblk)
1735 return NULL;
1736
Dan Williams8c2f7e82015-06-25 04:20:04 -04001737 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001738 dev->type = &namespace_blk_device_type;
1739 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1740 if (nsblk->id < 0) {
1741 kfree(nsblk);
1742 return NULL;
1743 }
1744 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1745 dev->parent = &nd_region->dev;
1746 dev->groups = nd_namespace_attribute_groups;
1747
Dan Williams8c2f7e82015-06-25 04:20:04 -04001748 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001749}
1750
1751void nd_region_create_blk_seed(struct nd_region *nd_region)
1752{
1753 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1754 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1755 /*
1756 * Seed creation failures are not fatal, provisioning is simply
1757 * disabled until memory becomes available
1758 */
1759 if (!nd_region->ns_seed)
1760 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1761 else
1762 nd_device_register(nd_region->ns_seed);
1763}
1764
Dan Williamscd034122016-03-11 10:15:36 -08001765void nd_region_create_dax_seed(struct nd_region *nd_region)
1766{
1767 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1768 nd_region->dax_seed = nd_dax_create(nd_region);
1769 /*
1770 * Seed creation failures are not fatal, provisioning is simply
1771 * disabled until memory becomes available
1772 */
1773 if (!nd_region->dax_seed)
1774 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1775}
1776
Dan Williams2dc43332015-12-13 11:41:36 -08001777void nd_region_create_pfn_seed(struct nd_region *nd_region)
1778{
1779 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1780 nd_region->pfn_seed = nd_pfn_create(nd_region);
1781 /*
1782 * Seed creation failures are not fatal, provisioning is simply
1783 * disabled until memory becomes available
1784 */
1785 if (!nd_region->pfn_seed)
1786 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1787}
1788
Dan Williams8c2f7e82015-06-25 04:20:04 -04001789void nd_region_create_btt_seed(struct nd_region *nd_region)
1790{
1791 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1792 nd_region->btt_seed = nd_btt_create(nd_region);
1793 /*
1794 * Seed creation failures are not fatal, provisioning is simply
1795 * disabled until memory becomes available
1796 */
1797 if (!nd_region->btt_seed)
1798 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1799}
1800
Dan Williams8a5f50d2016-09-22 15:42:59 -07001801static int add_namespace_resource(struct nd_region *nd_region,
1802 struct nd_namespace_label *nd_label, struct device **devs,
1803 int count)
Dan Williams1b40e092015-05-01 13:34:01 -04001804{
Dan Williams8a5f50d2016-09-22 15:42:59 -07001805 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsae8219f2016-09-19 16:04:21 -07001806 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001807 int i;
1808
1809 for (i = 0; i < count; i++) {
1810 u8 *uuid = namespace_to_uuid(devs[i]);
1811 struct resource *res;
1812
1813 if (IS_ERR_OR_NULL(uuid)) {
1814 WARN_ON(1);
1815 continue;
1816 }
1817
1818 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
1819 continue;
1820 if (is_namespace_blk(devs[i])) {
1821 res = nsblk_add_resource(nd_region, ndd,
1822 to_nd_namespace_blk(devs[i]),
1823 __le64_to_cpu(nd_label->dpa));
1824 if (!res)
1825 return -ENXIO;
1826 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
1827 } else {
1828 dev_err(&nd_region->dev,
1829 "error: conflicting extents for uuid: %pUb\n",
1830 nd_label->uuid);
1831 return -ENXIO;
1832 }
1833 break;
1834 }
1835
1836 return i;
1837}
1838
1839struct device *create_namespace_blk(struct nd_region *nd_region,
1840 struct nd_namespace_label *nd_label, int count)
1841{
1842
1843 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1844 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04001845 struct nd_namespace_blk *nsblk;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001846 char *name[NSLABEL_NAME_LEN];
1847 struct device *dev = NULL;
1848 struct resource *res;
1849
1850 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1851 if (!nsblk)
1852 return ERR_PTR(-ENOMEM);
1853 dev = &nsblk->common.dev;
1854 dev->type = &namespace_blk_device_type;
1855 dev->parent = &nd_region->dev;
1856 nsblk->id = -1;
1857 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1858 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1859 GFP_KERNEL);
1860 if (!nsblk->uuid)
1861 goto blk_err;
1862 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1863 if (name[0])
1864 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1865 GFP_KERNEL);
1866 res = nsblk_add_resource(nd_region, ndd, nsblk,
1867 __le64_to_cpu(nd_label->dpa));
1868 if (!res)
1869 goto blk_err;
1870 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
1871 return dev;
1872 blk_err:
1873 namespace_blk_release(dev);
1874 return ERR_PTR(-ENXIO);
1875}
1876
1877static struct device **scan_labels(struct nd_region *nd_region)
1878{
1879 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1880 struct device *dev, **devs = NULL;
1881 struct nd_label_ent *label_ent, *e;
Dan Williamsae8219f2016-09-19 16:04:21 -07001882 int i, count = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001883
Dan Williams8a5f50d2016-09-22 15:42:59 -07001884 /* "safe" because create_namespace_pmem() might list_move() label_ent */
1885 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
Dan Williamsae8219f2016-09-19 16:04:21 -07001886 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams1b40e092015-05-01 13:34:01 -04001887 struct device **__devs;
Dan Williamsae8219f2016-09-19 16:04:21 -07001888 u32 flags;
Dan Williams1b40e092015-05-01 13:34:01 -04001889
Dan Williamsae8219f2016-09-19 16:04:21 -07001890 if (!nd_label)
1891 continue;
1892 flags = __le32_to_cpu(nd_label->flags);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001893 if (is_nd_blk(&nd_region->dev)
1894 == !!(flags & NSLABEL_FLAG_LOCAL))
1895 /* pass, region matches label type */;
Dan Williams1b40e092015-05-01 13:34:01 -04001896 else
1897 continue;
1898
Dan Williams8a5f50d2016-09-22 15:42:59 -07001899 i = add_namespace_resource(nd_region, nd_label, devs, count);
1900 if (i < 0)
1901 goto err;
Dan Williams1b40e092015-05-01 13:34:01 -04001902 if (i < count)
1903 continue;
1904 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1905 if (!__devs)
1906 goto err;
1907 memcpy(__devs, devs, sizeof(dev) * count);
1908 kfree(devs);
1909 devs = __devs;
1910
Dan Williams8a5f50d2016-09-22 15:42:59 -07001911 if (is_nd_blk(&nd_region->dev)) {
1912 dev = create_namespace_blk(nd_region, nd_label, count);
1913 if (IS_ERR(dev))
1914 goto err;
1915 devs[count++] = dev;
1916 } else {
1917 dev = create_namespace_pmem(nd_region, nd_label);
1918 if (IS_ERR(dev)) {
1919 switch (PTR_ERR(dev)) {
1920 case -EAGAIN:
1921 /* skip invalid labels */
1922 continue;
1923 case -ENODEV:
1924 /* fallthrough to seed creation */
1925 break;
1926 default:
1927 goto err;
1928 }
1929 } else
1930 devs[count++] = dev;
1931
1932 /* we only expect one valid pmem label set per region */
1933 break;
1934 }
Dan Williams1b40e092015-05-01 13:34:01 -04001935 }
1936
Dan Williams8a5f50d2016-09-22 15:42:59 -07001937 dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
1938 __func__, count, is_nd_blk(&nd_region->dev)
1939 ? "blk" : "pmem", count == 1 ? "" : "s");
Dan Williams1b40e092015-05-01 13:34:01 -04001940
1941 if (count == 0) {
1942 /* Publish a zero-sized namespace for userspace to configure. */
Dan Williamsae8219f2016-09-19 16:04:21 -07001943 nd_mapping_free_labels(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04001944
1945 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1946 if (!devs)
1947 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001948 if (is_nd_blk(&nd_region->dev)) {
1949 struct nd_namespace_blk *nsblk;
1950
1951 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1952 if (!nsblk)
1953 goto err;
1954 dev = &nsblk->common.dev;
1955 dev->type = &namespace_blk_device_type;
1956 } else {
1957 struct nd_namespace_pmem *nspm;
1958
1959 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1960 if (!nspm)
1961 goto err;
1962 dev = &nspm->nsio.common.dev;
1963 dev->type = &namespace_pmem_device_type;
1964 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1965 }
Dan Williams1b40e092015-05-01 13:34:01 -04001966 dev->parent = &nd_region->dev;
1967 devs[count++] = dev;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001968 } else if (is_nd_pmem(&nd_region->dev)) {
1969 /* clean unselected labels */
1970 for (i = 0; i < nd_region->ndr_mappings; i++) {
1971 nd_mapping = &nd_region->mapping[i];
1972 if (list_empty(&nd_mapping->labels)) {
1973 WARN_ON(1);
1974 continue;
1975 }
1976 label_ent = list_first_entry(&nd_mapping->labels,
1977 typeof(*label_ent), list);
1978 list_del(&label_ent->list);
1979 nd_mapping_free_labels(nd_mapping);
1980 list_add(&label_ent->list, &nd_mapping->labels);
1981 }
Dan Williams1b40e092015-05-01 13:34:01 -04001982 }
1983
1984 return devs;
1985
Dan Williamsae8219f2016-09-19 16:04:21 -07001986 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07001987 for (i = 0; devs[i]; i++)
1988 if (is_nd_blk(&nd_region->dev))
1989 namespace_blk_release(devs[i]);
1990 else
1991 namespace_pmem_release(devs[i]);
Dan Williams1b40e092015-05-01 13:34:01 -04001992 kfree(devs);
1993 return NULL;
1994}
1995
Dan Williams8a5f50d2016-09-22 15:42:59 -07001996static struct device **create_namespaces(struct nd_region *nd_region)
Dan Williamsae8219f2016-09-19 16:04:21 -07001997{
1998 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1999 struct device **devs;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002000 int i;
Dan Williamsae8219f2016-09-19 16:04:21 -07002001
2002 if (nd_region->ndr_mappings == 0)
2003 return NULL;
2004
Dan Williams8a5f50d2016-09-22 15:42:59 -07002005 /* lock down all mappings while we scan labels */
2006 for (i = 0; i < nd_region->ndr_mappings; i++) {
2007 nd_mapping = &nd_region->mapping[i];
2008 mutex_lock_nested(&nd_mapping->lock, i);
2009 }
2010
2011 devs = scan_labels(nd_region);
2012
2013 for (i = 0; i < nd_region->ndr_mappings; i++) {
2014 int reverse = nd_region->ndr_mappings - 1 - i;
2015
2016 nd_mapping = &nd_region->mapping[reverse];
2017 mutex_unlock(&nd_mapping->lock);
2018 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002019
2020 return devs;
2021}
2022
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002023static int init_active_labels(struct nd_region *nd_region)
2024{
2025 int i;
2026
2027 for (i = 0; i < nd_region->ndr_mappings; i++) {
2028 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2029 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2030 struct nvdimm *nvdimm = nd_mapping->nvdimm;
Dan Williamsae8219f2016-09-19 16:04:21 -07002031 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002032 int count, j;
2033
2034 /*
2035 * If the dimm is disabled then prevent the region from
2036 * being activated if it aliases DPA.
2037 */
2038 if (!ndd) {
2039 if ((nvdimm->flags & NDD_ALIASING) == 0)
2040 return 0;
2041 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
2042 dev_name(&nd_mapping->nvdimm->dev));
2043 return -ENXIO;
2044 }
2045 nd_mapping->ndd = ndd;
2046 atomic_inc(&nvdimm->busy);
2047 get_ndd(ndd);
2048
2049 count = nd_label_active_count(ndd);
2050 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
2051 if (!count)
2052 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002053 for (j = 0; j < count; j++) {
2054 struct nd_namespace_label *label;
2055
Dan Williamsae8219f2016-09-19 16:04:21 -07002056 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2057 if (!label_ent)
2058 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002059 label = nd_label_active(ndd, j);
Dan Williamsae8219f2016-09-19 16:04:21 -07002060 label_ent->label = label;
2061
2062 mutex_lock(&nd_mapping->lock);
2063 list_add_tail(&label_ent->list, &nd_mapping->labels);
2064 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002065 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002066
2067 if (j >= count)
2068 continue;
2069
2070 mutex_lock(&nd_mapping->lock);
2071 nd_mapping_free_labels(nd_mapping);
2072 mutex_unlock(&nd_mapping->lock);
2073 return -ENOMEM;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002074 }
2075
2076 return 0;
2077}
2078
Dan Williams3d880022015-05-31 15:02:11 -04002079int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2080{
2081 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002082 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04002083
2084 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002085 nvdimm_bus_lock(&nd_region->dev);
2086 rc = init_active_labels(nd_region);
2087 if (rc) {
2088 nvdimm_bus_unlock(&nd_region->dev);
2089 return rc;
2090 }
2091
2092 type = nd_region_to_nstype(nd_region);
2093 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04002094 case ND_DEVICE_NAMESPACE_IO:
2095 devs = create_namespace_io(nd_region);
2096 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002097 case ND_DEVICE_NAMESPACE_PMEM:
Dan Williams1b40e092015-05-01 13:34:01 -04002098 case ND_DEVICE_NAMESPACE_BLK:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002099 devs = create_namespaces(nd_region);
Dan Williams1b40e092015-05-01 13:34:01 -04002100 break;
Dan Williams3d880022015-05-31 15:02:11 -04002101 default:
2102 break;
2103 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002104 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04002105
2106 if (!devs)
2107 return -ENODEV;
2108
Dan Williams3d880022015-05-31 15:02:11 -04002109 for (i = 0; devs[i]; i++) {
2110 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04002111 int id;
Dan Williams3d880022015-05-31 15:02:11 -04002112
Dan Williams1b40e092015-05-01 13:34:01 -04002113 if (type == ND_DEVICE_NAMESPACE_BLK) {
2114 struct nd_namespace_blk *nsblk;
2115
2116 nsblk = to_nd_namespace_blk(dev);
2117 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2118 GFP_KERNEL);
2119 nsblk->id = id;
2120 } else
2121 id = i;
2122
2123 if (id < 0)
2124 break;
2125 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002126 dev->groups = nd_namespace_attribute_groups;
2127 nd_device_register(dev);
2128 }
Dan Williams1b40e092015-05-01 13:34:01 -04002129 if (i)
2130 nd_region->ns_seed = devs[0];
2131
2132 if (devs[i]) {
2133 int j;
2134
2135 for (j = i; devs[j]; j++) {
2136 struct device *dev = devs[j];
2137
2138 device_initialize(dev);
2139 put_device(dev);
2140 }
2141 *err = j - i;
2142 /*
2143 * All of the namespaces we tried to register failed, so
2144 * fail region activation.
2145 */
2146 if (*err == 0)
2147 rc = -ENODEV;
2148 }
Dan Williams3d880022015-05-31 15:02:11 -04002149 kfree(devs);
2150
Dan Williams1b40e092015-05-01 13:34:01 -04002151 if (rc == -ENODEV)
2152 return rc;
2153
Dan Williams3d880022015-05-31 15:02:11 -04002154 return i;
2155}