blob: f981177524a19047ed08c8400a7e81f315f2785e [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 Williams3d880022015-05-31 15:02:11 -040017#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040018#include "nd-core.h"
Dan Williams3d880022015-05-31 15:02:11 -040019#include "nd.h"
20
21static void namespace_io_release(struct device *dev)
22{
23 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
24
25 kfree(nsio);
26}
27
Dan Williamsbf9bccc2015-06-17 17:14:46 -040028static void namespace_pmem_release(struct device *dev)
29{
30 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
31
32 kfree(nspm->alt_name);
33 kfree(nspm->uuid);
34 kfree(nspm);
35}
36
37static void namespace_blk_release(struct device *dev)
38{
Dan Williams1b40e092015-05-01 13:34:01 -040039 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
40 struct nd_region *nd_region = to_nd_region(dev->parent);
41
42 if (nsblk->id >= 0)
43 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
44 kfree(nsblk->alt_name);
45 kfree(nsblk->uuid);
46 kfree(nsblk->res);
47 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040048}
49
Dan Williams3d880022015-05-31 15:02:11 -040050static struct device_type namespace_io_device_type = {
51 .name = "nd_namespace_io",
52 .release = namespace_io_release,
53};
54
Dan Williamsbf9bccc2015-06-17 17:14:46 -040055static struct device_type namespace_pmem_device_type = {
56 .name = "nd_namespace_pmem",
57 .release = namespace_pmem_release,
58};
59
60static struct device_type namespace_blk_device_type = {
61 .name = "nd_namespace_blk",
62 .release = namespace_blk_release,
63};
64
65static bool is_namespace_pmem(struct device *dev)
66{
67 return dev ? dev->type == &namespace_pmem_device_type : false;
68}
69
70static bool is_namespace_blk(struct device *dev)
71{
72 return dev ? dev->type == &namespace_blk_device_type : false;
73}
74
75static bool is_namespace_io(struct device *dev)
76{
77 return dev ? dev->type == &namespace_io_device_type : false;
78}
79
Dan Williams004f1af2015-08-24 19:20:23 -040080bool pmem_should_map_pages(struct device *dev)
81{
82 struct nd_region *nd_region = to_nd_region(dev->parent);
83
84 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
85 return false;
86
87 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
88 return false;
89
90 if (is_nd_pfn(dev) || is_nd_btt(dev))
91 return false;
92
93#ifdef ARCH_MEMREMAP_PMEM
94 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
95#else
96 return false;
97#endif
98}
99EXPORT_SYMBOL(pmem_should_map_pages);
100
Vishal Verma5212e112015-06-25 04:20:32 -0400101const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
102 char *name)
103{
104 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400105 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400106
Dan Williamse1455742015-07-30 17:57:47 -0400107 if (ndns->claim) {
108 if (is_nd_btt(ndns->claim))
109 suffix = "s";
110 else if (is_nd_pfn(ndns->claim))
111 suffix = "m";
112 else
113 dev_WARN_ONCE(&ndns->dev, 1,
114 "unknown claim type by %s\n",
115 dev_name(ndns->claim));
116 }
Vishal Verma5212e112015-06-25 04:20:32 -0400117
Dan Williams004f1af2015-08-24 19:20:23 -0400118 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
119 if (!suffix && pmem_should_map_pages(&ndns->dev))
120 suffix = "m";
121 sprintf(name, "pmem%d%s", nd_region->id, suffix ? suffix : "");
122 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400123 struct nd_namespace_blk *nsblk;
124
125 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400126 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
127 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400128 } else {
129 return NULL;
130 }
131
132 return name;
133}
134EXPORT_SYMBOL(nvdimm_namespace_disk_name);
135
Vishal Verma6ec68952015-07-29 14:58:09 -0600136const u8 *nd_dev_to_uuid(struct device *dev)
137{
138 static const u8 null_uuid[16];
139
140 if (!dev)
141 return null_uuid;
142
143 if (is_namespace_pmem(dev)) {
144 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
145
146 return nspm->uuid;
147 } else if (is_namespace_blk(dev)) {
148 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
149
150 return nsblk->uuid;
151 } else
152 return null_uuid;
153}
154EXPORT_SYMBOL(nd_dev_to_uuid);
155
Dan Williams3d880022015-05-31 15:02:11 -0400156static ssize_t nstype_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 struct nd_region *nd_region = to_nd_region(dev->parent);
160
161 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
162}
163static DEVICE_ATTR_RO(nstype);
164
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400165static ssize_t __alt_name_store(struct device *dev, const char *buf,
166 const size_t len)
167{
168 char *input, *pos, *alt_name, **ns_altname;
169 ssize_t rc;
170
171 if (is_namespace_pmem(dev)) {
172 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
173
174 ns_altname = &nspm->alt_name;
175 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400176 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
177
178 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400179 } else
180 return -ENXIO;
181
Dan Williams8c2f7e82015-06-25 04:20:04 -0400182 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400183 return -EBUSY;
184
185 input = kmemdup(buf, len + 1, GFP_KERNEL);
186 if (!input)
187 return -ENOMEM;
188
189 input[len] = '\0';
190 pos = strim(input);
191 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
192 rc = -EINVAL;
193 goto out;
194 }
195
196 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
197 if (!alt_name) {
198 rc = -ENOMEM;
199 goto out;
200 }
201 kfree(*ns_altname);
202 *ns_altname = alt_name;
203 sprintf(*ns_altname, "%s", pos);
204 rc = len;
205
206out:
207 kfree(input);
208 return rc;
209}
210
Dan Williams1b40e092015-05-01 13:34:01 -0400211static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
212{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400213 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400214 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
215 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
216 struct nd_label_id label_id;
217 resource_size_t size = 0;
218 struct resource *res;
219
220 if (!nsblk->uuid)
221 return 0;
222 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
223 for_each_dpa_resource(ndd, res)
224 if (strcmp(res->name, label_id.id) == 0)
225 size += resource_size(res);
226 return size;
227}
228
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400229static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
230{
231 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
232 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
233 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
234 struct nd_label_id label_id;
235 struct resource *res;
236 int count, i;
237
238 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
239 return false;
240
241 count = 0;
242 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
243 for_each_dpa_resource(ndd, res) {
244 if (strcmp(res->name, label_id.id) != 0)
245 continue;
246 /*
247 * Resources with unacknoweldged adjustments indicate a
248 * failure to update labels
249 */
250 if (res->flags & DPA_RESOURCE_ADJUSTED)
251 return false;
252 count++;
253 }
254
255 /* These values match after a successful label update */
256 if (count != nsblk->num_resources)
257 return false;
258
259 for (i = 0; i < nsblk->num_resources; i++) {
260 struct resource *found = NULL;
261
262 for_each_dpa_resource(ndd, res)
263 if (res == nsblk->res[i]) {
264 found = res;
265 break;
266 }
267 /* stale resource */
268 if (!found)
269 return false;
270 }
271
272 return true;
273}
274
275resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
276{
277 resource_size_t size;
278
279 nvdimm_bus_lock(&nsblk->common.dev);
280 size = __nd_namespace_blk_validate(nsblk);
281 nvdimm_bus_unlock(&nsblk->common.dev);
282
283 return size;
284}
285EXPORT_SYMBOL(nd_namespace_blk_validate);
286
287
Dan Williamsf524bf22015-05-30 12:36:02 -0400288static int nd_namespace_label_update(struct nd_region *nd_region,
289 struct device *dev)
290{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400291 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400292 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400293 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400294 return 0;
295
296 /*
297 * Only allow label writes that will result in a valid namespace
298 * or deletion of an existing namespace.
299 */
300 if (is_namespace_pmem(dev)) {
301 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400302 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400303
304 if (size == 0 && nspm->uuid)
305 /* delete allocation */;
306 else if (!nspm->uuid)
307 return 0;
308
309 return nd_pmem_namespace_label_update(nd_region, nspm, size);
310 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400311 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
312 resource_size_t size = nd_namespace_blk_size(nsblk);
313
314 if (size == 0 && nsblk->uuid)
315 /* delete allocation */;
316 else if (!nsblk->uuid || !nsblk->lbasize)
317 return 0;
318
319 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400320 } else
321 return -ENXIO;
322}
323
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400324static ssize_t alt_name_store(struct device *dev,
325 struct device_attribute *attr, const char *buf, size_t len)
326{
Dan Williamsf524bf22015-05-30 12:36:02 -0400327 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400328 ssize_t rc;
329
330 device_lock(dev);
331 nvdimm_bus_lock(dev);
332 wait_nvdimm_bus_probe_idle(dev);
333 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400334 if (rc >= 0)
335 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400336 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
337 nvdimm_bus_unlock(dev);
338 device_unlock(dev);
339
Dan Williamsf524bf22015-05-30 12:36:02 -0400340 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400341}
342
343static ssize_t alt_name_show(struct device *dev,
344 struct device_attribute *attr, char *buf)
345{
346 char *ns_altname;
347
348 if (is_namespace_pmem(dev)) {
349 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
350
351 ns_altname = nspm->alt_name;
352 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400353 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
354
355 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400356 } else
357 return -ENXIO;
358
359 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
360}
361static DEVICE_ATTR_RW(alt_name);
362
363static int scan_free(struct nd_region *nd_region,
364 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
365 resource_size_t n)
366{
367 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
368 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
369 int rc = 0;
370
371 while (n) {
372 struct resource *res, *last;
373 resource_size_t new_start;
374
375 last = NULL;
376 for_each_dpa_resource(ndd, res)
377 if (strcmp(res->name, label_id->id) == 0)
378 last = res;
379 res = last;
380 if (!res)
381 return 0;
382
383 if (n >= resource_size(res)) {
384 n -= resource_size(res);
385 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
386 nvdimm_free_dpa(ndd, res);
387 /* retry with last resource deleted */
388 continue;
389 }
390
391 /*
392 * Keep BLK allocations relegated to high DPA as much as
393 * possible
394 */
395 if (is_blk)
396 new_start = res->start + n;
397 else
398 new_start = res->start;
399
400 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400401 if (rc == 0)
402 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400403 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
404 break;
405 }
406
407 return rc;
408}
409
410/**
411 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
412 * @nd_region: the set of dimms to reclaim @n bytes from
413 * @label_id: unique identifier for the namespace consuming this dpa range
414 * @n: number of bytes per-dimm to release
415 *
416 * Assumes resources are ordered. Starting from the end try to
417 * adjust_resource() the allocation to @n, but if @n is larger than the
418 * allocation delete it and find the 'new' last allocation in the label
419 * set.
420 */
421static int shrink_dpa_allocation(struct nd_region *nd_region,
422 struct nd_label_id *label_id, resource_size_t n)
423{
424 int i;
425
426 for (i = 0; i < nd_region->ndr_mappings; i++) {
427 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
428 int rc;
429
430 rc = scan_free(nd_region, nd_mapping, label_id, n);
431 if (rc)
432 return rc;
433 }
434
435 return 0;
436}
437
438static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
439 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
440 resource_size_t n)
441{
442 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
443 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
444 resource_size_t first_dpa;
445 struct resource *res;
446 int rc = 0;
447
448 /* allocate blk from highest dpa first */
449 if (is_blk)
450 first_dpa = nd_mapping->start + nd_mapping->size - n;
451 else
452 first_dpa = nd_mapping->start;
453
454 /* first resource allocation for this label-id or dimm */
455 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
456 if (!res)
457 rc = -EBUSY;
458
459 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
460 return rc ? n : 0;
461}
462
Dan Williams1b40e092015-05-01 13:34:01 -0400463static bool space_valid(bool is_pmem, bool is_reserve,
464 struct nd_label_id *label_id, struct resource *res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400465{
466 /*
467 * For BLK-space any space is valid, for PMEM-space, it must be
Dan Williams1b40e092015-05-01 13:34:01 -0400468 * contiguous with an existing allocation unless we are
469 * reserving pmem.
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400470 */
Dan Williams1b40e092015-05-01 13:34:01 -0400471 if (is_reserve || !is_pmem)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400472 return true;
473 if (!res || strcmp(res->name, label_id->id) == 0)
474 return true;
475 return false;
476}
477
478enum alloc_loc {
479 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
480};
481
482static resource_size_t scan_allocate(struct nd_region *nd_region,
483 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
484 resource_size_t n)
485{
486 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -0400487 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400488 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
489 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
490 const resource_size_t to_allocate = n;
491 struct resource *res;
492 int first;
493
494 retry:
495 first = 0;
496 for_each_dpa_resource(ndd, res) {
497 resource_size_t allocate, available = 0, free_start, free_end;
498 struct resource *next = res->sibling, *new_res = NULL;
499 enum alloc_loc loc = ALLOC_ERR;
500 const char *action;
501 int rc = 0;
502
503 /* ignore resources outside this nd_mapping */
504 if (res->start > mapping_end)
505 continue;
506 if (res->end < nd_mapping->start)
507 continue;
508
509 /* space at the beginning of the mapping */
510 if (!first++ && res->start > nd_mapping->start) {
511 free_start = nd_mapping->start;
512 available = res->start - free_start;
Dan Williams1b40e092015-05-01 13:34:01 -0400513 if (space_valid(is_pmem, is_reserve, label_id, NULL))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400514 loc = ALLOC_BEFORE;
515 }
516
517 /* space between allocations */
518 if (!loc && next) {
519 free_start = res->start + resource_size(res);
520 free_end = min(mapping_end, next->start - 1);
Dan Williams1b40e092015-05-01 13:34:01 -0400521 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400522 && free_start < free_end) {
523 available = free_end + 1 - free_start;
524 loc = ALLOC_MID;
525 }
526 }
527
528 /* space at the end of the mapping */
529 if (!loc && !next) {
530 free_start = res->start + resource_size(res);
531 free_end = mapping_end;
Dan Williams1b40e092015-05-01 13:34:01 -0400532 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400533 && free_start < free_end) {
534 available = free_end + 1 - free_start;
535 loc = ALLOC_AFTER;
536 }
537 }
538
539 if (!loc || !available)
540 continue;
541 allocate = min(available, n);
542 switch (loc) {
543 case ALLOC_BEFORE:
544 if (strcmp(res->name, label_id->id) == 0) {
545 /* adjust current resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400546 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400547 return n;
548 rc = adjust_resource(res, res->start - allocate,
549 resource_size(res) + allocate);
550 action = "cur grow up";
551 } else
552 action = "allocate";
553 break;
554 case ALLOC_MID:
555 if (strcmp(next->name, label_id->id) == 0) {
556 /* adjust next resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400557 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400558 return n;
559 rc = adjust_resource(next, next->start
560 - allocate, resource_size(next)
561 + allocate);
562 new_res = next;
563 action = "next grow up";
564 } else if (strcmp(res->name, label_id->id) == 0) {
565 action = "grow down";
566 } else
567 action = "allocate";
568 break;
569 case ALLOC_AFTER:
570 if (strcmp(res->name, label_id->id) == 0)
571 action = "grow down";
572 else
573 action = "allocate";
574 break;
575 default:
576 return n;
577 }
578
579 if (strcmp(action, "allocate") == 0) {
580 /* BLK allocate bottom up */
581 if (!is_pmem)
582 free_start += available - allocate;
Dan Williams1b40e092015-05-01 13:34:01 -0400583 else if (!is_reserve && free_start != nd_mapping->start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400584 return n;
585
586 new_res = nvdimm_allocate_dpa(ndd, label_id,
587 free_start, allocate);
588 if (!new_res)
589 rc = -EBUSY;
590 } else if (strcmp(action, "grow down") == 0) {
591 /* adjust current resource down */
592 rc = adjust_resource(res, res->start, resource_size(res)
593 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400594 if (rc == 0)
595 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400596 }
597
598 if (!new_res)
599 new_res = res;
600
601 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
602 action, loc, rc);
603
604 if (rc)
605 return n;
606
607 n -= allocate;
608 if (n) {
609 /*
610 * Retry scan with newly inserted resources.
611 * For example, if we did an ALLOC_BEFORE
612 * insertion there may also have been space
613 * available for an ALLOC_AFTER insertion, so we
614 * need to check this same resource again
615 */
616 goto retry;
617 } else
618 return 0;
619 }
620
Dan Williams1b40e092015-05-01 13:34:01 -0400621 /*
622 * If we allocated nothing in the BLK case it may be because we are in
623 * an initial "pmem-reserve pass". Only do an initial BLK allocation
624 * when none of the DPA space is reserved.
625 */
626 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400627 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
628 return n;
629}
630
Dan Williams1b40e092015-05-01 13:34:01 -0400631static int merge_dpa(struct nd_region *nd_region,
632 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
633{
634 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
635 struct resource *res;
636
637 if (strncmp("pmem", label_id->id, 4) == 0)
638 return 0;
639 retry:
640 for_each_dpa_resource(ndd, res) {
641 int rc;
642 struct resource *next = res->sibling;
643 resource_size_t end = res->start + resource_size(res);
644
645 if (!next || strcmp(res->name, label_id->id) != 0
646 || strcmp(next->name, label_id->id) != 0
647 || end != next->start)
648 continue;
649 end += resource_size(next);
650 nvdimm_free_dpa(ndd, next);
651 rc = adjust_resource(res, res->start, end - res->start);
652 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
653 if (rc)
654 return rc;
655 res->flags |= DPA_RESOURCE_ADJUSTED;
656 goto retry;
657 }
658
659 return 0;
660}
661
662static int __reserve_free_pmem(struct device *dev, void *data)
663{
664 struct nvdimm *nvdimm = data;
665 struct nd_region *nd_region;
666 struct nd_label_id label_id;
667 int i;
668
669 if (!is_nd_pmem(dev))
670 return 0;
671
672 nd_region = to_nd_region(dev);
673 if (nd_region->ndr_mappings == 0)
674 return 0;
675
676 memset(&label_id, 0, sizeof(label_id));
677 strcat(label_id.id, "pmem-reserve");
678 for (i = 0; i < nd_region->ndr_mappings; i++) {
679 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
680 resource_size_t n, rem = 0;
681
682 if (nd_mapping->nvdimm != nvdimm)
683 continue;
684
685 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
686 if (n == 0)
687 return 0;
688 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
689 dev_WARN_ONCE(&nd_region->dev, rem,
690 "pmem reserve underrun: %#llx of %#llx bytes\n",
691 (unsigned long long) n - rem,
692 (unsigned long long) n);
693 return rem ? -ENXIO : 0;
694 }
695
696 return 0;
697}
698
699static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
700 struct nd_mapping *nd_mapping)
701{
702 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
703 struct resource *res, *_res;
704
705 for_each_dpa_resource_safe(ndd, res, _res)
706 if (strcmp(res->name, "pmem-reserve") == 0)
707 nvdimm_free_dpa(ndd, res);
708}
709
710static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
711 struct nd_mapping *nd_mapping)
712{
713 struct nvdimm *nvdimm = nd_mapping->nvdimm;
714 int rc;
715
716 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
717 __reserve_free_pmem);
718 if (rc)
719 release_free_pmem(nvdimm_bus, nd_mapping);
720 return rc;
721}
722
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400723/**
724 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
725 * @nd_region: the set of dimms to allocate @n more bytes from
726 * @label_id: unique identifier for the namespace consuming this dpa range
727 * @n: number of bytes per-dimm to add to the existing allocation
728 *
729 * Assumes resources are ordered. For BLK regions, first consume
730 * BLK-only available DPA free space, then consume PMEM-aliased DPA
731 * space starting at the highest DPA. For PMEM regions start
732 * allocations from the start of an interleave set and end at the first
733 * BLK allocation or the end of the interleave set, whichever comes
734 * first.
735 */
736static int grow_dpa_allocation(struct nd_region *nd_region,
737 struct nd_label_id *label_id, resource_size_t n)
738{
Dan Williams1b40e092015-05-01 13:34:01 -0400739 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
740 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400741 int i;
742
743 for (i = 0; i < nd_region->ndr_mappings; i++) {
744 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400745 resource_size_t rem = n;
746 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400747
Dan Williams1b40e092015-05-01 13:34:01 -0400748 /*
749 * In the BLK case try once with all unallocated PMEM
750 * reserved, and once without
751 */
752 for (j = is_pmem; j < 2; j++) {
753 bool blk_only = j == 0;
754
755 if (blk_only) {
756 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
757 if (rc)
758 return rc;
759 }
760 rem = scan_allocate(nd_region, nd_mapping,
761 label_id, rem);
762 if (blk_only)
763 release_free_pmem(nvdimm_bus, nd_mapping);
764
765 /* try again and allow encroachments into PMEM */
766 if (rem == 0)
767 break;
768 }
769
770 dev_WARN_ONCE(&nd_region->dev, rem,
771 "allocation underrun: %#llx of %#llx bytes\n",
772 (unsigned long long) n - rem,
773 (unsigned long long) n);
774 if (rem)
775 return -ENXIO;
776
777 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400778 if (rc)
779 return rc;
780 }
781
782 return 0;
783}
784
785static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
786 struct nd_namespace_pmem *nspm, resource_size_t size)
787{
788 struct resource *res = &nspm->nsio.res;
789
790 res->start = nd_region->ndr_start;
791 res->end = nd_region->ndr_start + size - 1;
792}
793
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300794static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
795{
796 if (!uuid) {
797 dev_dbg(dev, "%s: uuid not set\n", where);
798 return true;
799 }
800 return false;
801}
802
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400803static ssize_t __size_store(struct device *dev, unsigned long long val)
804{
805 resource_size_t allocated = 0, available = 0;
806 struct nd_region *nd_region = to_nd_region(dev->parent);
807 struct nd_mapping *nd_mapping;
808 struct nvdimm_drvdata *ndd;
809 struct nd_label_id label_id;
810 u32 flags = 0, remainder;
811 u8 *uuid = NULL;
812 int rc, i;
813
Dan Williams8c2f7e82015-06-25 04:20:04 -0400814 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400815 return -EBUSY;
816
817 if (is_namespace_pmem(dev)) {
818 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
819
820 uuid = nspm->uuid;
821 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400822 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
823
824 uuid = nsblk->uuid;
825 flags = NSLABEL_FLAG_LOCAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400826 }
827
828 /*
829 * We need a uuid for the allocation-label and dimm(s) on which
830 * to store the label.
831 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300832 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400833 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300834 if (nd_region->ndr_mappings == 0) {
835 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
836 return -ENXIO;
837 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400838
839 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
840 if (remainder) {
841 dev_dbg(dev, "%llu is not %dK aligned\n", val,
842 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
843 return -EINVAL;
844 }
845
846 nd_label_gen_id(&label_id, uuid, flags);
847 for (i = 0; i < nd_region->ndr_mappings; i++) {
848 nd_mapping = &nd_region->mapping[i];
849 ndd = to_ndd(nd_mapping);
850
851 /*
852 * All dimms in an interleave set, or the base dimm for a blk
853 * region, need to be enabled for the size to be changed.
854 */
855 if (!ndd)
856 return -ENXIO;
857
858 allocated += nvdimm_allocated_dpa(ndd, &label_id);
859 }
860 available = nd_region_available_dpa(nd_region);
861
862 if (val > available + allocated)
863 return -ENOSPC;
864
865 if (val == allocated)
866 return 0;
867
868 val = div_u64(val, nd_region->ndr_mappings);
869 allocated = div_u64(allocated, nd_region->ndr_mappings);
870 if (val < allocated)
871 rc = shrink_dpa_allocation(nd_region, &label_id,
872 allocated - val);
873 else
874 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
875
876 if (rc)
877 return rc;
878
879 if (is_namespace_pmem(dev)) {
880 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
881
882 nd_namespace_pmem_set_size(nd_region, nspm,
883 val * nd_region->ndr_mappings);
Dan Williams1b40e092015-05-01 13:34:01 -0400884 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400885 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
886
Dan Williams1b40e092015-05-01 13:34:01 -0400887 /*
888 * Try to delete the namespace if we deleted all of its
Dan Williams8c2f7e82015-06-25 04:20:04 -0400889 * allocation, this is not the seed device for the
890 * region, and it is not actively claimed by a btt
891 * instance.
Dan Williams1b40e092015-05-01 13:34:01 -0400892 */
Dan Williams8c2f7e82015-06-25 04:20:04 -0400893 if (val == 0 && nd_region->ns_seed != dev
894 && !nsblk->common.claim)
Dan Williams1b40e092015-05-01 13:34:01 -0400895 nd_device_unregister(dev, ND_ASYNC);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400896 }
897
898 return rc;
899}
900
901static ssize_t size_store(struct device *dev,
902 struct device_attribute *attr, const char *buf, size_t len)
903{
Dan Williamsf524bf22015-05-30 12:36:02 -0400904 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400905 unsigned long long val;
906 u8 **uuid = NULL;
907 int rc;
908
909 rc = kstrtoull(buf, 0, &val);
910 if (rc)
911 return rc;
912
913 device_lock(dev);
914 nvdimm_bus_lock(dev);
915 wait_nvdimm_bus_probe_idle(dev);
916 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -0400917 if (rc >= 0)
918 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400919
920 if (is_namespace_pmem(dev)) {
921 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
922
923 uuid = &nspm->uuid;
924 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400925 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
926
927 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400928 }
929
930 if (rc == 0 && val == 0 && uuid) {
931 /* setting size zero == 'delete namespace' */
932 kfree(*uuid);
933 *uuid = NULL;
934 }
935
936 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
937 ? "fail" : "success", rc);
938
939 nvdimm_bus_unlock(dev);
940 device_unlock(dev);
941
Dan Williamsf524bf22015-05-30 12:36:02 -0400942 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400943}
944
Dan Williams8c2f7e82015-06-25 04:20:04 -0400945resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400946{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400947 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -0400948
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400949 if (is_namespace_pmem(dev)) {
950 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
951
Dan Williams8c2f7e82015-06-25 04:20:04 -0400952 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400953 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400954 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400955 } else if (is_namespace_io(dev)) {
956 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
957
Dan Williams8c2f7e82015-06-25 04:20:04 -0400958 return resource_size(&nsio->res);
959 } else
960 WARN_ONCE(1, "unknown namespace type\n");
961 return 0;
962}
Dan Williams1b40e092015-05-01 13:34:01 -0400963
Dan Williams8c2f7e82015-06-25 04:20:04 -0400964resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
965{
966 resource_size_t size;
967
968 nvdimm_bus_lock(&ndns->dev);
969 size = __nvdimm_namespace_capacity(ndns);
970 nvdimm_bus_unlock(&ndns->dev);
971
972 return size;
973}
974EXPORT_SYMBOL(nvdimm_namespace_capacity);
975
976static ssize_t size_show(struct device *dev,
977 struct device_attribute *attr, char *buf)
978{
979 return sprintf(buf, "%llu\n", (unsigned long long)
980 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400981}
982static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
983
984static ssize_t uuid_show(struct device *dev,
985 struct device_attribute *attr, char *buf)
986{
987 u8 *uuid;
988
989 if (is_namespace_pmem(dev)) {
990 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
991
992 uuid = nspm->uuid;
993 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400994 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
995
996 uuid = nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400997 } else
998 return -ENXIO;
999
1000 if (uuid)
1001 return sprintf(buf, "%pUb\n", uuid);
1002 return sprintf(buf, "\n");
1003}
1004
1005/**
1006 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1007 * @nd_region: parent region so we can updates all dimms in the set
1008 * @dev: namespace type for generating label_id
1009 * @new_uuid: incoming uuid
1010 * @old_uuid: reference to the uuid storage location in the namespace object
1011 */
1012static int namespace_update_uuid(struct nd_region *nd_region,
1013 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1014{
1015 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1016 struct nd_label_id old_label_id;
1017 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001018 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001019
Dan Williamsf524bf22015-05-30 12:36:02 -04001020 if (!nd_is_uuid_unique(dev, new_uuid))
1021 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001022
1023 if (*old_uuid == NULL)
1024 goto out;
1025
Dan Williamsf524bf22015-05-30 12:36:02 -04001026 /*
1027 * If we've already written a label with this uuid, then it's
1028 * too late to rename because we can't reliably update the uuid
1029 * without losing the old namespace. Userspace must delete this
1030 * namespace to abandon the old uuid.
1031 */
1032 for (i = 0; i < nd_region->ndr_mappings; i++) {
1033 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1034
1035 /*
1036 * This check by itself is sufficient because old_uuid
1037 * would be NULL above if this uuid did not exist in the
1038 * currently written set.
1039 *
1040 * FIXME: can we delete uuid with zero dpa allocated?
1041 */
1042 if (nd_mapping->labels)
1043 return -EBUSY;
1044 }
1045
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001046 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1047 nd_label_gen_id(&new_label_id, new_uuid, flags);
1048 for (i = 0; i < nd_region->ndr_mappings; i++) {
1049 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1050 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1051 struct resource *res;
1052
1053 for_each_dpa_resource(ndd, res)
1054 if (strcmp(res->name, old_label_id.id) == 0)
1055 sprintf((void *) res->name, "%s",
1056 new_label_id.id);
1057 }
1058 kfree(*old_uuid);
1059 out:
1060 *old_uuid = new_uuid;
1061 return 0;
1062}
1063
1064static ssize_t uuid_store(struct device *dev,
1065 struct device_attribute *attr, const char *buf, size_t len)
1066{
1067 struct nd_region *nd_region = to_nd_region(dev->parent);
1068 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001069 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001070 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001071
1072 if (is_namespace_pmem(dev)) {
1073 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1074
1075 ns_uuid = &nspm->uuid;
1076 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001077 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1078
1079 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001080 } else
1081 return -ENXIO;
1082
1083 device_lock(dev);
1084 nvdimm_bus_lock(dev);
1085 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001086 if (to_ndns(dev)->claim)
1087 rc = -EBUSY;
1088 if (rc >= 0)
1089 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001090 if (rc >= 0)
1091 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001092 if (rc >= 0)
1093 rc = nd_namespace_label_update(nd_region, dev);
1094 else
1095 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001096 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1097 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1098 nvdimm_bus_unlock(dev);
1099 device_unlock(dev);
1100
Dan Williamsf524bf22015-05-30 12:36:02 -04001101 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001102}
1103static DEVICE_ATTR_RW(uuid);
1104
1105static ssize_t resource_show(struct device *dev,
1106 struct device_attribute *attr, char *buf)
1107{
1108 struct resource *res;
1109
1110 if (is_namespace_pmem(dev)) {
1111 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1112
1113 res = &nspm->nsio.res;
1114 } else if (is_namespace_io(dev)) {
1115 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1116
1117 res = &nsio->res;
1118 } else
1119 return -ENXIO;
1120
1121 /* no address to convey if the namespace has no allocation */
1122 if (resource_size(res) == 0)
1123 return -ENXIO;
1124 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1125}
1126static DEVICE_ATTR_RO(resource);
1127
Vishal Vermafcae6952015-06-25 04:22:39 -04001128static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1129 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001130
1131static ssize_t sector_size_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1135
1136 if (!is_namespace_blk(dev))
1137 return -ENXIO;
1138
1139 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1140}
1141
1142static ssize_t sector_size_store(struct device *dev,
1143 struct device_attribute *attr, const char *buf, size_t len)
1144{
1145 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williamsf524bf22015-05-30 12:36:02 -04001146 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001147 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001148
1149 if (!is_namespace_blk(dev))
1150 return -ENXIO;
1151
1152 device_lock(dev);
1153 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001154 if (to_ndns(dev)->claim)
1155 rc = -EBUSY;
1156 if (rc >= 0)
1157 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1158 ns_lbasize_supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001159 if (rc >= 0)
1160 rc = nd_namespace_label_update(nd_region, dev);
1161 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1162 rc, rc < 0 ? "tried" : "wrote", buf,
1163 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001164 nvdimm_bus_unlock(dev);
1165 device_unlock(dev);
1166
1167 return rc ? rc : len;
1168}
1169static DEVICE_ATTR_RW(sector_size);
1170
Dan Williams0ba1c632015-05-30 12:35:36 -04001171static ssize_t dpa_extents_show(struct device *dev,
1172 struct device_attribute *attr, char *buf)
1173{
1174 struct nd_region *nd_region = to_nd_region(dev->parent);
1175 struct nd_label_id label_id;
1176 int count = 0, i;
1177 u8 *uuid = NULL;
1178 u32 flags = 0;
1179
1180 nvdimm_bus_lock(dev);
1181 if (is_namespace_pmem(dev)) {
1182 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1183
1184 uuid = nspm->uuid;
1185 flags = 0;
1186 } else if (is_namespace_blk(dev)) {
1187 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1188
1189 uuid = nsblk->uuid;
1190 flags = NSLABEL_FLAG_LOCAL;
1191 }
1192
1193 if (!uuid)
1194 goto out;
1195
1196 nd_label_gen_id(&label_id, uuid, flags);
1197 for (i = 0; i < nd_region->ndr_mappings; i++) {
1198 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1199 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1200 struct resource *res;
1201
1202 for_each_dpa_resource(ndd, res)
1203 if (strcmp(res->name, label_id.id) == 0)
1204 count++;
1205 }
1206 out:
1207 nvdimm_bus_unlock(dev);
1208
1209 return sprintf(buf, "%d\n", count);
1210}
1211static DEVICE_ATTR_RO(dpa_extents);
1212
Dan Williams8c2f7e82015-06-25 04:20:04 -04001213static ssize_t holder_show(struct device *dev,
1214 struct device_attribute *attr, char *buf)
1215{
1216 struct nd_namespace_common *ndns = to_ndns(dev);
1217 ssize_t rc;
1218
1219 device_lock(dev);
1220 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1221 device_unlock(dev);
1222
1223 return rc;
1224}
1225static DEVICE_ATTR_RO(holder);
1226
1227static ssize_t force_raw_store(struct device *dev,
1228 struct device_attribute *attr, const char *buf, size_t len)
1229{
1230 bool force_raw;
1231 int rc = strtobool(buf, &force_raw);
1232
1233 if (rc)
1234 return rc;
1235
1236 to_ndns(dev)->force_raw = force_raw;
1237 return len;
1238}
1239
1240static ssize_t force_raw_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1242{
1243 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1244}
1245static DEVICE_ATTR_RW(force_raw);
1246
Dan Williams3d880022015-05-31 15:02:11 -04001247static struct attribute *nd_namespace_attributes[] = {
1248 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001249 &dev_attr_size.attr,
1250 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001251 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001252 &dev_attr_resource.attr,
1253 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001254 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001255 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001256 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001257 NULL,
1258};
1259
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001260static umode_t namespace_visible(struct kobject *kobj,
1261 struct attribute *a, int n)
1262{
1263 struct device *dev = container_of(kobj, struct device, kobj);
1264
1265 if (a == &dev_attr_resource.attr) {
1266 if (is_namespace_blk(dev))
1267 return 0;
1268 return a->mode;
1269 }
1270
1271 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1272 if (a == &dev_attr_size.attr)
1273 return S_IWUSR | S_IRUGO;
Dan Williams1b40e092015-05-01 13:34:01 -04001274
1275 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1276 return 0;
1277
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001278 return a->mode;
1279 }
1280
Dan Williams8c2f7e82015-06-25 04:20:04 -04001281 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1282 || a == &dev_attr_holder.attr
1283 || a == &dev_attr_force_raw.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001284 return a->mode;
1285
1286 return 0;
1287}
1288
Dan Williams3d880022015-05-31 15:02:11 -04001289static struct attribute_group nd_namespace_attribute_group = {
1290 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001291 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001292};
1293
1294static const struct attribute_group *nd_namespace_attribute_groups[] = {
1295 &nd_device_attribute_group,
1296 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001297 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001298 NULL,
1299};
1300
Dan Williams8c2f7e82015-06-25 04:20:04 -04001301struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1302{
1303 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001304 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001305 struct nd_namespace_common *ndns;
1306 resource_size_t size;
1307
Dan Williamse1455742015-07-30 17:57:47 -04001308 if (nd_btt || nd_pfn) {
1309 struct device *host = NULL;
1310
1311 if (nd_btt) {
1312 host = &nd_btt->dev;
1313 ndns = nd_btt->ndns;
1314 } else if (nd_pfn) {
1315 host = &nd_pfn->dev;
1316 ndns = nd_pfn->ndns;
1317 }
1318
1319 if (!ndns || !host)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001320 return ERR_PTR(-ENODEV);
1321
1322 /*
1323 * Flush any in-progess probes / removals in the driver
1324 * for the raw personality of this namespace.
1325 */
1326 device_lock(&ndns->dev);
1327 device_unlock(&ndns->dev);
1328 if (ndns->dev.driver) {
1329 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williamse1455742015-07-30 17:57:47 -04001330 dev_name(host));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001331 return ERR_PTR(-EBUSY);
1332 }
Dan Williamse1455742015-07-30 17:57:47 -04001333 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != host,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001334 "host (%s) vs claim (%s) mismatch\n",
Dan Williamse1455742015-07-30 17:57:47 -04001335 dev_name(host),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001336 dev_name(ndns->claim)))
1337 return ERR_PTR(-ENXIO);
1338 } else {
1339 ndns = to_ndns(dev);
1340 if (ndns->claim) {
1341 dev_dbg(dev, "claimed by %s, failing probe\n",
1342 dev_name(ndns->claim));
1343
1344 return ERR_PTR(-ENXIO);
1345 }
1346 }
1347
1348 size = nvdimm_namespace_capacity(ndns);
1349 if (size < ND_MIN_NAMESPACE_SIZE) {
1350 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1351 &size, ND_MIN_NAMESPACE_SIZE);
1352 return ERR_PTR(-ENODEV);
1353 }
1354
1355 if (is_namespace_pmem(&ndns->dev)) {
1356 struct nd_namespace_pmem *nspm;
1357
1358 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001359 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001360 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001361 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001362 struct nd_namespace_blk *nsblk;
1363
1364 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001365 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1366 return ERR_PTR(-ENODEV);
1367 if (!nsblk->lbasize) {
1368 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1369 __func__);
1370 return ERR_PTR(-ENODEV);
1371 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001372 if (!nd_namespace_blk_validate(nsblk))
1373 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001374 }
1375
1376 return ndns;
1377}
1378EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1379
Dan Williams3d880022015-05-31 15:02:11 -04001380static struct device **create_namespace_io(struct nd_region *nd_region)
1381{
1382 struct nd_namespace_io *nsio;
1383 struct device *dev, **devs;
1384 struct resource *res;
1385
1386 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1387 if (!nsio)
1388 return NULL;
1389
1390 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1391 if (!devs) {
1392 kfree(nsio);
1393 return NULL;
1394 }
1395
Dan Williams8c2f7e82015-06-25 04:20:04 -04001396 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001397 dev->type = &namespace_io_device_type;
1398 dev->parent = &nd_region->dev;
1399 res = &nsio->res;
1400 res->name = dev_name(&nd_region->dev);
1401 res->flags = IORESOURCE_MEM;
1402 res->start = nd_region->ndr_start;
1403 res->end = res->start + nd_region->ndr_size - 1;
1404
1405 devs[0] = dev;
1406 return devs;
1407}
1408
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001409static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1410 u64 cookie, u16 pos)
1411{
1412 struct nd_namespace_label *found = NULL;
1413 int i;
1414
1415 for (i = 0; i < nd_region->ndr_mappings; i++) {
1416 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1417 struct nd_namespace_label *nd_label;
1418 bool found_uuid = false;
1419 int l;
1420
1421 for_each_label(l, nd_label, nd_mapping->labels) {
1422 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1423 u16 position = __le16_to_cpu(nd_label->position);
1424 u16 nlabel = __le16_to_cpu(nd_label->nlabel);
1425
1426 if (isetcookie != cookie)
1427 continue;
1428
1429 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1430 continue;
1431
1432 if (found_uuid) {
1433 dev_dbg(to_ndd(nd_mapping)->dev,
1434 "%s duplicate entry for uuid\n",
1435 __func__);
1436 return false;
1437 }
1438 found_uuid = true;
1439 if (nlabel != nd_region->ndr_mappings)
1440 continue;
1441 if (position != pos)
1442 continue;
1443 found = nd_label;
1444 break;
1445 }
1446 if (found)
1447 break;
1448 }
1449 return found != NULL;
1450}
1451
1452static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1453{
1454 struct nd_namespace_label *select = NULL;
1455 int i;
1456
1457 if (!pmem_id)
1458 return -ENODEV;
1459
1460 for (i = 0; i < nd_region->ndr_mappings; i++) {
1461 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1462 struct nd_namespace_label *nd_label;
1463 u64 hw_start, hw_end, pmem_start, pmem_end;
1464 int l;
1465
1466 for_each_label(l, nd_label, nd_mapping->labels)
1467 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1468 break;
1469
1470 if (!nd_label) {
1471 WARN_ON(1);
1472 return -EINVAL;
1473 }
1474
1475 select = nd_label;
1476 /*
1477 * Check that this label is compliant with the dpa
1478 * range published in NFIT
1479 */
1480 hw_start = nd_mapping->start;
1481 hw_end = hw_start + nd_mapping->size;
1482 pmem_start = __le64_to_cpu(select->dpa);
1483 pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
1484 if (pmem_start == hw_start && pmem_end <= hw_end)
1485 /* pass */;
1486 else
1487 return -EINVAL;
1488
1489 nd_mapping->labels[0] = select;
1490 nd_mapping->labels[1] = NULL;
1491 }
1492 return 0;
1493}
1494
1495/**
1496 * find_pmem_label_set - validate interleave set labelling, retrieve label0
1497 * @nd_region: region with mappings to validate
1498 */
1499static int find_pmem_label_set(struct nd_region *nd_region,
1500 struct nd_namespace_pmem *nspm)
1501{
1502 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1503 struct nd_namespace_label *nd_label;
1504 u8 select_id[NSLABEL_UUID_LEN];
1505 resource_size_t size = 0;
1506 u8 *pmem_id = NULL;
1507 int rc = -ENODEV, l;
1508 u16 i;
1509
1510 if (cookie == 0)
1511 return -ENXIO;
1512
1513 /*
1514 * Find a complete set of labels by uuid. By definition we can start
1515 * with any mapping as the reference label
1516 */
1517 for_each_label(l, nd_label, nd_region->mapping[0].labels) {
1518 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1519
1520 if (isetcookie != cookie)
1521 continue;
1522
1523 for (i = 0; nd_region->ndr_mappings; i++)
1524 if (!has_uuid_at_pos(nd_region, nd_label->uuid,
1525 cookie, i))
1526 break;
1527 if (i < nd_region->ndr_mappings) {
1528 /*
1529 * Give up if we don't find an instance of a
1530 * uuid at each position (from 0 to
1531 * nd_region->ndr_mappings - 1), or if we find a
1532 * dimm with two instances of the same uuid.
1533 */
1534 rc = -EINVAL;
1535 goto err;
1536 } else if (pmem_id) {
1537 /*
1538 * If there is more than one valid uuid set, we
1539 * need userspace to clean this up.
1540 */
1541 rc = -EBUSY;
1542 goto err;
1543 }
1544 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
1545 pmem_id = select_id;
1546 }
1547
1548 /*
1549 * Fix up each mapping's 'labels' to have the validated pmem label for
1550 * that position at labels[0], and NULL at labels[1]. In the process,
1551 * check that the namespace aligns with interleave-set. We know
1552 * that it does not overlap with any blk namespaces by virtue of
1553 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1554 * succeeded).
1555 */
1556 rc = select_pmem_id(nd_region, pmem_id);
1557 if (rc)
1558 goto err;
1559
1560 /* Calculate total size and populate namespace properties from label0 */
1561 for (i = 0; i < nd_region->ndr_mappings; i++) {
1562 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1563 struct nd_namespace_label *label0 = nd_mapping->labels[0];
1564
1565 size += __le64_to_cpu(label0->rawsize);
1566 if (__le16_to_cpu(label0->position) != 0)
1567 continue;
1568 WARN_ON(nspm->alt_name || nspm->uuid);
1569 nspm->alt_name = kmemdup((void __force *) label0->name,
1570 NSLABEL_NAME_LEN, GFP_KERNEL);
1571 nspm->uuid = kmemdup((void __force *) label0->uuid,
1572 NSLABEL_UUID_LEN, GFP_KERNEL);
1573 }
1574
1575 if (!nspm->alt_name || !nspm->uuid) {
1576 rc = -ENOMEM;
1577 goto err;
1578 }
1579
1580 nd_namespace_pmem_set_size(nd_region, nspm, size);
1581
1582 return 0;
1583 err:
1584 switch (rc) {
1585 case -EINVAL:
1586 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1587 break;
1588 case -ENODEV:
1589 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1590 break;
1591 default:
1592 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1593 __func__, rc);
1594 break;
1595 }
1596 return rc;
1597}
1598
1599static struct device **create_namespace_pmem(struct nd_region *nd_region)
1600{
1601 struct nd_namespace_pmem *nspm;
1602 struct device *dev, **devs;
1603 struct resource *res;
1604 int rc;
1605
1606 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1607 if (!nspm)
1608 return NULL;
1609
Dan Williams8c2f7e82015-06-25 04:20:04 -04001610 dev = &nspm->nsio.common.dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001611 dev->type = &namespace_pmem_device_type;
1612 dev->parent = &nd_region->dev;
1613 res = &nspm->nsio.res;
1614 res->name = dev_name(&nd_region->dev);
1615 res->flags = IORESOURCE_MEM;
1616 rc = find_pmem_label_set(nd_region, nspm);
1617 if (rc == -ENODEV) {
1618 int i;
1619
1620 /* Pass, try to permit namespace creation... */
1621 for (i = 0; i < nd_region->ndr_mappings; i++) {
1622 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1623
1624 kfree(nd_mapping->labels);
1625 nd_mapping->labels = NULL;
1626 }
1627
1628 /* Publish a zero-sized namespace for userspace to configure. */
1629 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1630
1631 rc = 0;
1632 } else if (rc)
1633 goto err;
1634
1635 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1636 if (!devs)
1637 goto err;
1638
1639 devs[0] = dev;
1640 return devs;
1641
1642 err:
Dan Williams8c2f7e82015-06-25 04:20:04 -04001643 namespace_pmem_release(&nspm->nsio.common.dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001644 return NULL;
1645}
1646
Dan Williams1b40e092015-05-01 13:34:01 -04001647struct resource *nsblk_add_resource(struct nd_region *nd_region,
1648 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1649 resource_size_t start)
1650{
1651 struct nd_label_id label_id;
1652 struct resource *res;
1653
1654 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1655 res = krealloc(nsblk->res,
1656 sizeof(void *) * (nsblk->num_resources + 1),
1657 GFP_KERNEL);
1658 if (!res)
1659 return NULL;
1660 nsblk->res = (struct resource **) res;
1661 for_each_dpa_resource(ndd, res)
1662 if (strcmp(res->name, label_id.id) == 0
1663 && res->start == start) {
1664 nsblk->res[nsblk->num_resources++] = res;
1665 return res;
1666 }
1667 return NULL;
1668}
1669
1670static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1671{
1672 struct nd_namespace_blk *nsblk;
1673 struct device *dev;
1674
1675 if (!is_nd_blk(&nd_region->dev))
1676 return NULL;
1677
1678 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1679 if (!nsblk)
1680 return NULL;
1681
Dan Williams8c2f7e82015-06-25 04:20:04 -04001682 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001683 dev->type = &namespace_blk_device_type;
1684 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1685 if (nsblk->id < 0) {
1686 kfree(nsblk);
1687 return NULL;
1688 }
1689 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1690 dev->parent = &nd_region->dev;
1691 dev->groups = nd_namespace_attribute_groups;
1692
Dan Williams8c2f7e82015-06-25 04:20:04 -04001693 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001694}
1695
1696void nd_region_create_blk_seed(struct nd_region *nd_region)
1697{
1698 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1699 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1700 /*
1701 * Seed creation failures are not fatal, provisioning is simply
1702 * disabled until memory becomes available
1703 */
1704 if (!nd_region->ns_seed)
1705 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1706 else
1707 nd_device_register(nd_region->ns_seed);
1708}
1709
Dan Williams8c2f7e82015-06-25 04:20:04 -04001710void nd_region_create_btt_seed(struct nd_region *nd_region)
1711{
1712 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1713 nd_region->btt_seed = nd_btt_create(nd_region);
1714 /*
1715 * Seed creation failures are not fatal, provisioning is simply
1716 * disabled until memory becomes available
1717 */
1718 if (!nd_region->btt_seed)
1719 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1720}
1721
Dan Williams1b40e092015-05-01 13:34:01 -04001722static struct device **create_namespace_blk(struct nd_region *nd_region)
1723{
1724 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1725 struct nd_namespace_label *nd_label;
1726 struct device *dev, **devs = NULL;
1727 struct nd_namespace_blk *nsblk;
1728 struct nvdimm_drvdata *ndd;
1729 int i, l, count = 0;
1730 struct resource *res;
1731
1732 if (nd_region->ndr_mappings == 0)
1733 return NULL;
1734
1735 ndd = to_ndd(nd_mapping);
1736 for_each_label(l, nd_label, nd_mapping->labels) {
1737 u32 flags = __le32_to_cpu(nd_label->flags);
1738 char *name[NSLABEL_NAME_LEN];
1739 struct device **__devs;
1740
1741 if (flags & NSLABEL_FLAG_LOCAL)
1742 /* pass */;
1743 else
1744 continue;
1745
1746 for (i = 0; i < count; i++) {
1747 nsblk = to_nd_namespace_blk(devs[i]);
1748 if (memcmp(nsblk->uuid, nd_label->uuid,
1749 NSLABEL_UUID_LEN) == 0) {
1750 res = nsblk_add_resource(nd_region, ndd, nsblk,
1751 __le64_to_cpu(nd_label->dpa));
1752 if (!res)
1753 goto err;
1754 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001755 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001756 break;
1757 }
1758 }
1759 if (i < count)
1760 continue;
1761 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1762 if (!__devs)
1763 goto err;
1764 memcpy(__devs, devs, sizeof(dev) * count);
1765 kfree(devs);
1766 devs = __devs;
1767
1768 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1769 if (!nsblk)
1770 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001771 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001772 dev->type = &namespace_blk_device_type;
1773 dev->parent = &nd_region->dev;
1774 dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
1775 devs[count++] = dev;
1776 nsblk->id = -1;
1777 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1778 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1779 GFP_KERNEL);
1780 if (!nsblk->uuid)
1781 goto err;
1782 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1783 if (name[0])
1784 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1785 GFP_KERNEL);
1786 res = nsblk_add_resource(nd_region, ndd, nsblk,
1787 __le64_to_cpu(nd_label->dpa));
1788 if (!res)
1789 goto err;
1790 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001791 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001792 }
1793
1794 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
1795 __func__, count, count == 1 ? "" : "s");
1796
1797 if (count == 0) {
1798 /* Publish a zero-sized namespace for userspace to configure. */
1799 for (i = 0; i < nd_region->ndr_mappings; i++) {
1800 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1801
1802 kfree(nd_mapping->labels);
1803 nd_mapping->labels = NULL;
1804 }
1805
1806 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1807 if (!devs)
1808 goto err;
1809 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1810 if (!nsblk)
1811 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001812 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001813 dev->type = &namespace_blk_device_type;
1814 dev->parent = &nd_region->dev;
1815 devs[count++] = dev;
1816 }
1817
1818 return devs;
1819
1820err:
1821 for (i = 0; i < count; i++) {
1822 nsblk = to_nd_namespace_blk(devs[i]);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001823 namespace_blk_release(&nsblk->common.dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001824 }
1825 kfree(devs);
1826 return NULL;
1827}
1828
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001829static int init_active_labels(struct nd_region *nd_region)
1830{
1831 int i;
1832
1833 for (i = 0; i < nd_region->ndr_mappings; i++) {
1834 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1835 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1836 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1837 int count, j;
1838
1839 /*
1840 * If the dimm is disabled then prevent the region from
1841 * being activated if it aliases DPA.
1842 */
1843 if (!ndd) {
1844 if ((nvdimm->flags & NDD_ALIASING) == 0)
1845 return 0;
1846 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
1847 dev_name(&nd_mapping->nvdimm->dev));
1848 return -ENXIO;
1849 }
1850 nd_mapping->ndd = ndd;
1851 atomic_inc(&nvdimm->busy);
1852 get_ndd(ndd);
1853
1854 count = nd_label_active_count(ndd);
1855 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
1856 if (!count)
1857 continue;
1858 nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
1859 GFP_KERNEL);
1860 if (!nd_mapping->labels)
1861 return -ENOMEM;
1862 for (j = 0; j < count; j++) {
1863 struct nd_namespace_label *label;
1864
1865 label = nd_label_active(ndd, j);
1866 nd_mapping->labels[j] = label;
1867 }
1868 }
1869
1870 return 0;
1871}
1872
Dan Williams3d880022015-05-31 15:02:11 -04001873int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
1874{
1875 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001876 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04001877
1878 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001879 nvdimm_bus_lock(&nd_region->dev);
1880 rc = init_active_labels(nd_region);
1881 if (rc) {
1882 nvdimm_bus_unlock(&nd_region->dev);
1883 return rc;
1884 }
1885
1886 type = nd_region_to_nstype(nd_region);
1887 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04001888 case ND_DEVICE_NAMESPACE_IO:
1889 devs = create_namespace_io(nd_region);
1890 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001891 case ND_DEVICE_NAMESPACE_PMEM:
1892 devs = create_namespace_pmem(nd_region);
1893 break;
Dan Williams1b40e092015-05-01 13:34:01 -04001894 case ND_DEVICE_NAMESPACE_BLK:
1895 devs = create_namespace_blk(nd_region);
1896 break;
Dan Williams3d880022015-05-31 15:02:11 -04001897 default:
1898 break;
1899 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001900 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04001901
1902 if (!devs)
1903 return -ENODEV;
1904
Dan Williams3d880022015-05-31 15:02:11 -04001905 for (i = 0; devs[i]; i++) {
1906 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04001907 int id;
Dan Williams3d880022015-05-31 15:02:11 -04001908
Dan Williams1b40e092015-05-01 13:34:01 -04001909 if (type == ND_DEVICE_NAMESPACE_BLK) {
1910 struct nd_namespace_blk *nsblk;
1911
1912 nsblk = to_nd_namespace_blk(dev);
1913 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
1914 GFP_KERNEL);
1915 nsblk->id = id;
1916 } else
1917 id = i;
1918
1919 if (id < 0)
1920 break;
1921 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04001922 dev->groups = nd_namespace_attribute_groups;
1923 nd_device_register(dev);
1924 }
Dan Williams1b40e092015-05-01 13:34:01 -04001925 if (i)
1926 nd_region->ns_seed = devs[0];
1927
1928 if (devs[i]) {
1929 int j;
1930
1931 for (j = i; devs[j]; j++) {
1932 struct device *dev = devs[j];
1933
1934 device_initialize(dev);
1935 put_device(dev);
1936 }
1937 *err = j - i;
1938 /*
1939 * All of the namespaces we tried to register failed, so
1940 * fail region activation.
1941 */
1942 if (*err == 0)
1943 rc = -ENODEV;
1944 }
Dan Williams3d880022015-05-31 15:02:11 -04001945 kfree(devs);
1946
Dan Williams1b40e092015-05-01 13:34:01 -04001947 if (rc == -ENODEV)
1948 return rc;
1949
Dan Williams3d880022015-05-31 15:02:11 -04001950 return i;
1951}