blob: 9edf7eb7d17cc39713e67e4dd62d4e16c0d93e07 [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 Williamse07ecd72016-01-05 18:37:23 -080080static int is_uuid_busy(struct device *dev, void *data)
81{
82 u8 *uuid1 = data, *uuid2 = NULL;
83
84 if (is_namespace_pmem(dev)) {
85 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
86
87 uuid2 = nspm->uuid;
88 } else if (is_namespace_blk(dev)) {
89 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
90
91 uuid2 = nsblk->uuid;
92 } else if (is_nd_btt(dev)) {
93 struct nd_btt *nd_btt = to_nd_btt(dev);
94
95 uuid2 = nd_btt->uuid;
96 } else if (is_nd_pfn(dev)) {
97 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
98
99 uuid2 = nd_pfn->uuid;
100 }
101
102 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
103 return -EBUSY;
104
105 return 0;
106}
107
108static int is_namespace_uuid_busy(struct device *dev, void *data)
109{
110 if (is_nd_pmem(dev) || is_nd_blk(dev))
111 return device_for_each_child(dev, data, is_uuid_busy);
112 return 0;
113}
114
115/**
116 * nd_is_uuid_unique - verify that no other namespace has @uuid
117 * @dev: any device on a nvdimm_bus
118 * @uuid: uuid to check
119 */
120bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
121{
122 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
123
124 if (!nvdimm_bus)
125 return false;
126 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
127 if (device_for_each_child(&nvdimm_bus->dev, uuid,
128 is_namespace_uuid_busy) != 0)
129 return false;
130 return true;
131}
132
Dan Williams004f1af2015-08-24 19:20:23 -0400133bool pmem_should_map_pages(struct device *dev)
134{
135 struct nd_region *nd_region = to_nd_region(dev->parent);
136
137 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
138 return false;
139
140 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
141 return false;
142
143 if (is_nd_pfn(dev) || is_nd_btt(dev))
144 return false;
145
146#ifdef ARCH_MEMREMAP_PMEM
147 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
148#else
149 return false;
150#endif
151}
152EXPORT_SYMBOL(pmem_should_map_pages);
153
Vishal Verma5212e112015-06-25 04:20:32 -0400154const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
155 char *name)
156{
157 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400158 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400159
Dan Williams0731de02015-12-14 15:34:15 -0800160 if (ndns->claim && is_nd_btt(ndns->claim))
161 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400162
Dan Williams004f1af2015-08-24 19:20:23 -0400163 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams004f1af2015-08-24 19:20:23 -0400164 sprintf(name, "pmem%d%s", nd_region->id, suffix ? suffix : "");
165 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400166 struct nd_namespace_blk *nsblk;
167
168 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400169 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
170 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400171 } else {
172 return NULL;
173 }
174
175 return name;
176}
177EXPORT_SYMBOL(nvdimm_namespace_disk_name);
178
Vishal Verma6ec68952015-07-29 14:58:09 -0600179const u8 *nd_dev_to_uuid(struct device *dev)
180{
181 static const u8 null_uuid[16];
182
183 if (!dev)
184 return null_uuid;
185
186 if (is_namespace_pmem(dev)) {
187 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
188
189 return nspm->uuid;
190 } else if (is_namespace_blk(dev)) {
191 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
192
193 return nsblk->uuid;
194 } else
195 return null_uuid;
196}
197EXPORT_SYMBOL(nd_dev_to_uuid);
198
Dan Williams3d880022015-05-31 15:02:11 -0400199static ssize_t nstype_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 struct nd_region *nd_region = to_nd_region(dev->parent);
203
204 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
205}
206static DEVICE_ATTR_RO(nstype);
207
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400208static ssize_t __alt_name_store(struct device *dev, const char *buf,
209 const size_t len)
210{
211 char *input, *pos, *alt_name, **ns_altname;
212 ssize_t rc;
213
214 if (is_namespace_pmem(dev)) {
215 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
216
217 ns_altname = &nspm->alt_name;
218 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400219 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
220
221 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400222 } else
223 return -ENXIO;
224
Dan Williams8c2f7e82015-06-25 04:20:04 -0400225 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400226 return -EBUSY;
227
228 input = kmemdup(buf, len + 1, GFP_KERNEL);
229 if (!input)
230 return -ENOMEM;
231
232 input[len] = '\0';
233 pos = strim(input);
234 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
235 rc = -EINVAL;
236 goto out;
237 }
238
239 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
240 if (!alt_name) {
241 rc = -ENOMEM;
242 goto out;
243 }
244 kfree(*ns_altname);
245 *ns_altname = alt_name;
246 sprintf(*ns_altname, "%s", pos);
247 rc = len;
248
249out:
250 kfree(input);
251 return rc;
252}
253
Dan Williams1b40e092015-05-01 13:34:01 -0400254static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
255{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400256 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400257 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
258 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
259 struct nd_label_id label_id;
260 resource_size_t size = 0;
261 struct resource *res;
262
263 if (!nsblk->uuid)
264 return 0;
265 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
266 for_each_dpa_resource(ndd, res)
267 if (strcmp(res->name, label_id.id) == 0)
268 size += resource_size(res);
269 return size;
270}
271
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400272static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
273{
274 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
275 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
276 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
277 struct nd_label_id label_id;
278 struct resource *res;
279 int count, i;
280
281 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
282 return false;
283
284 count = 0;
285 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
286 for_each_dpa_resource(ndd, res) {
287 if (strcmp(res->name, label_id.id) != 0)
288 continue;
289 /*
290 * Resources with unacknoweldged adjustments indicate a
291 * failure to update labels
292 */
293 if (res->flags & DPA_RESOURCE_ADJUSTED)
294 return false;
295 count++;
296 }
297
298 /* These values match after a successful label update */
299 if (count != nsblk->num_resources)
300 return false;
301
302 for (i = 0; i < nsblk->num_resources; i++) {
303 struct resource *found = NULL;
304
305 for_each_dpa_resource(ndd, res)
306 if (res == nsblk->res[i]) {
307 found = res;
308 break;
309 }
310 /* stale resource */
311 if (!found)
312 return false;
313 }
314
315 return true;
316}
317
318resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
319{
320 resource_size_t size;
321
322 nvdimm_bus_lock(&nsblk->common.dev);
323 size = __nd_namespace_blk_validate(nsblk);
324 nvdimm_bus_unlock(&nsblk->common.dev);
325
326 return size;
327}
328EXPORT_SYMBOL(nd_namespace_blk_validate);
329
330
Dan Williamsf524bf22015-05-30 12:36:02 -0400331static int nd_namespace_label_update(struct nd_region *nd_region,
332 struct device *dev)
333{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400334 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400335 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400336 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400337 return 0;
338
339 /*
340 * Only allow label writes that will result in a valid namespace
341 * or deletion of an existing namespace.
342 */
343 if (is_namespace_pmem(dev)) {
344 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400345 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400346
347 if (size == 0 && nspm->uuid)
348 /* delete allocation */;
349 else if (!nspm->uuid)
350 return 0;
351
352 return nd_pmem_namespace_label_update(nd_region, nspm, size);
353 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400354 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
355 resource_size_t size = nd_namespace_blk_size(nsblk);
356
357 if (size == 0 && nsblk->uuid)
358 /* delete allocation */;
359 else if (!nsblk->uuid || !nsblk->lbasize)
360 return 0;
361
362 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400363 } else
364 return -ENXIO;
365}
366
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400367static ssize_t alt_name_store(struct device *dev,
368 struct device_attribute *attr, const char *buf, size_t len)
369{
Dan Williamsf524bf22015-05-30 12:36:02 -0400370 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400371 ssize_t rc;
372
373 device_lock(dev);
374 nvdimm_bus_lock(dev);
375 wait_nvdimm_bus_probe_idle(dev);
376 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400377 if (rc >= 0)
378 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400379 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
380 nvdimm_bus_unlock(dev);
381 device_unlock(dev);
382
Dan Williamsf524bf22015-05-30 12:36:02 -0400383 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400384}
385
386static ssize_t alt_name_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 char *ns_altname;
390
391 if (is_namespace_pmem(dev)) {
392 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
393
394 ns_altname = nspm->alt_name;
395 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400396 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
397
398 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400399 } else
400 return -ENXIO;
401
402 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
403}
404static DEVICE_ATTR_RW(alt_name);
405
406static int scan_free(struct nd_region *nd_region,
407 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
408 resource_size_t n)
409{
410 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
411 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
412 int rc = 0;
413
414 while (n) {
415 struct resource *res, *last;
416 resource_size_t new_start;
417
418 last = NULL;
419 for_each_dpa_resource(ndd, res)
420 if (strcmp(res->name, label_id->id) == 0)
421 last = res;
422 res = last;
423 if (!res)
424 return 0;
425
426 if (n >= resource_size(res)) {
427 n -= resource_size(res);
428 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
429 nvdimm_free_dpa(ndd, res);
430 /* retry with last resource deleted */
431 continue;
432 }
433
434 /*
435 * Keep BLK allocations relegated to high DPA as much as
436 * possible
437 */
438 if (is_blk)
439 new_start = res->start + n;
440 else
441 new_start = res->start;
442
443 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400444 if (rc == 0)
445 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400446 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
447 break;
448 }
449
450 return rc;
451}
452
453/**
454 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
455 * @nd_region: the set of dimms to reclaim @n bytes from
456 * @label_id: unique identifier for the namespace consuming this dpa range
457 * @n: number of bytes per-dimm to release
458 *
459 * Assumes resources are ordered. Starting from the end try to
460 * adjust_resource() the allocation to @n, but if @n is larger than the
461 * allocation delete it and find the 'new' last allocation in the label
462 * set.
463 */
464static int shrink_dpa_allocation(struct nd_region *nd_region,
465 struct nd_label_id *label_id, resource_size_t n)
466{
467 int i;
468
469 for (i = 0; i < nd_region->ndr_mappings; i++) {
470 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
471 int rc;
472
473 rc = scan_free(nd_region, nd_mapping, label_id, n);
474 if (rc)
475 return rc;
476 }
477
478 return 0;
479}
480
481static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
482 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
483 resource_size_t n)
484{
485 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
486 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
487 resource_size_t first_dpa;
488 struct resource *res;
489 int rc = 0;
490
491 /* allocate blk from highest dpa first */
492 if (is_blk)
493 first_dpa = nd_mapping->start + nd_mapping->size - n;
494 else
495 first_dpa = nd_mapping->start;
496
497 /* first resource allocation for this label-id or dimm */
498 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
499 if (!res)
500 rc = -EBUSY;
501
502 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
503 return rc ? n : 0;
504}
505
Dan Williams1b40e092015-05-01 13:34:01 -0400506static bool space_valid(bool is_pmem, bool is_reserve,
507 struct nd_label_id *label_id, struct resource *res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400508{
509 /*
510 * For BLK-space any space is valid, for PMEM-space, it must be
Dan Williams1b40e092015-05-01 13:34:01 -0400511 * contiguous with an existing allocation unless we are
512 * reserving pmem.
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400513 */
Dan Williams1b40e092015-05-01 13:34:01 -0400514 if (is_reserve || !is_pmem)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400515 return true;
516 if (!res || strcmp(res->name, label_id->id) == 0)
517 return true;
518 return false;
519}
520
521enum alloc_loc {
522 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
523};
524
525static resource_size_t scan_allocate(struct nd_region *nd_region,
526 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
527 resource_size_t n)
528{
529 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -0400530 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400531 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
532 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
533 const resource_size_t to_allocate = n;
534 struct resource *res;
535 int first;
536
537 retry:
538 first = 0;
539 for_each_dpa_resource(ndd, res) {
540 resource_size_t allocate, available = 0, free_start, free_end;
541 struct resource *next = res->sibling, *new_res = NULL;
542 enum alloc_loc loc = ALLOC_ERR;
543 const char *action;
544 int rc = 0;
545
546 /* ignore resources outside this nd_mapping */
547 if (res->start > mapping_end)
548 continue;
549 if (res->end < nd_mapping->start)
550 continue;
551
552 /* space at the beginning of the mapping */
553 if (!first++ && res->start > nd_mapping->start) {
554 free_start = nd_mapping->start;
555 available = res->start - free_start;
Dan Williams1b40e092015-05-01 13:34:01 -0400556 if (space_valid(is_pmem, is_reserve, label_id, NULL))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400557 loc = ALLOC_BEFORE;
558 }
559
560 /* space between allocations */
561 if (!loc && next) {
562 free_start = res->start + resource_size(res);
563 free_end = min(mapping_end, next->start - 1);
Dan Williams1b40e092015-05-01 13:34:01 -0400564 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400565 && free_start < free_end) {
566 available = free_end + 1 - free_start;
567 loc = ALLOC_MID;
568 }
569 }
570
571 /* space at the end of the mapping */
572 if (!loc && !next) {
573 free_start = res->start + resource_size(res);
574 free_end = mapping_end;
Dan Williams1b40e092015-05-01 13:34:01 -0400575 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400576 && free_start < free_end) {
577 available = free_end + 1 - free_start;
578 loc = ALLOC_AFTER;
579 }
580 }
581
582 if (!loc || !available)
583 continue;
584 allocate = min(available, n);
585 switch (loc) {
586 case ALLOC_BEFORE:
587 if (strcmp(res->name, label_id->id) == 0) {
588 /* adjust current resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400589 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400590 return n;
591 rc = adjust_resource(res, res->start - allocate,
592 resource_size(res) + allocate);
593 action = "cur grow up";
594 } else
595 action = "allocate";
596 break;
597 case ALLOC_MID:
598 if (strcmp(next->name, label_id->id) == 0) {
599 /* adjust next resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400600 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400601 return n;
602 rc = adjust_resource(next, next->start
603 - allocate, resource_size(next)
604 + allocate);
605 new_res = next;
606 action = "next grow up";
607 } else if (strcmp(res->name, label_id->id) == 0) {
608 action = "grow down";
609 } else
610 action = "allocate";
611 break;
612 case ALLOC_AFTER:
613 if (strcmp(res->name, label_id->id) == 0)
614 action = "grow down";
615 else
616 action = "allocate";
617 break;
618 default:
619 return n;
620 }
621
622 if (strcmp(action, "allocate") == 0) {
623 /* BLK allocate bottom up */
624 if (!is_pmem)
625 free_start += available - allocate;
Dan Williams1b40e092015-05-01 13:34:01 -0400626 else if (!is_reserve && free_start != nd_mapping->start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400627 return n;
628
629 new_res = nvdimm_allocate_dpa(ndd, label_id,
630 free_start, allocate);
631 if (!new_res)
632 rc = -EBUSY;
633 } else if (strcmp(action, "grow down") == 0) {
634 /* adjust current resource down */
635 rc = adjust_resource(res, res->start, resource_size(res)
636 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400637 if (rc == 0)
638 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400639 }
640
641 if (!new_res)
642 new_res = res;
643
644 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
645 action, loc, rc);
646
647 if (rc)
648 return n;
649
650 n -= allocate;
651 if (n) {
652 /*
653 * Retry scan with newly inserted resources.
654 * For example, if we did an ALLOC_BEFORE
655 * insertion there may also have been space
656 * available for an ALLOC_AFTER insertion, so we
657 * need to check this same resource again
658 */
659 goto retry;
660 } else
661 return 0;
662 }
663
Dan Williams1b40e092015-05-01 13:34:01 -0400664 /*
665 * If we allocated nothing in the BLK case it may be because we are in
666 * an initial "pmem-reserve pass". Only do an initial BLK allocation
667 * when none of the DPA space is reserved.
668 */
669 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400670 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
671 return n;
672}
673
Dan Williams1b40e092015-05-01 13:34:01 -0400674static int merge_dpa(struct nd_region *nd_region,
675 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
676{
677 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
678 struct resource *res;
679
680 if (strncmp("pmem", label_id->id, 4) == 0)
681 return 0;
682 retry:
683 for_each_dpa_resource(ndd, res) {
684 int rc;
685 struct resource *next = res->sibling;
686 resource_size_t end = res->start + resource_size(res);
687
688 if (!next || strcmp(res->name, label_id->id) != 0
689 || strcmp(next->name, label_id->id) != 0
690 || end != next->start)
691 continue;
692 end += resource_size(next);
693 nvdimm_free_dpa(ndd, next);
694 rc = adjust_resource(res, res->start, end - res->start);
695 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
696 if (rc)
697 return rc;
698 res->flags |= DPA_RESOURCE_ADJUSTED;
699 goto retry;
700 }
701
702 return 0;
703}
704
705static int __reserve_free_pmem(struct device *dev, void *data)
706{
707 struct nvdimm *nvdimm = data;
708 struct nd_region *nd_region;
709 struct nd_label_id label_id;
710 int i;
711
712 if (!is_nd_pmem(dev))
713 return 0;
714
715 nd_region = to_nd_region(dev);
716 if (nd_region->ndr_mappings == 0)
717 return 0;
718
719 memset(&label_id, 0, sizeof(label_id));
720 strcat(label_id.id, "pmem-reserve");
721 for (i = 0; i < nd_region->ndr_mappings; i++) {
722 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
723 resource_size_t n, rem = 0;
724
725 if (nd_mapping->nvdimm != nvdimm)
726 continue;
727
728 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
729 if (n == 0)
730 return 0;
731 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
732 dev_WARN_ONCE(&nd_region->dev, rem,
733 "pmem reserve underrun: %#llx of %#llx bytes\n",
734 (unsigned long long) n - rem,
735 (unsigned long long) n);
736 return rem ? -ENXIO : 0;
737 }
738
739 return 0;
740}
741
742static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
743 struct nd_mapping *nd_mapping)
744{
745 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
746 struct resource *res, *_res;
747
748 for_each_dpa_resource_safe(ndd, res, _res)
749 if (strcmp(res->name, "pmem-reserve") == 0)
750 nvdimm_free_dpa(ndd, res);
751}
752
753static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
754 struct nd_mapping *nd_mapping)
755{
756 struct nvdimm *nvdimm = nd_mapping->nvdimm;
757 int rc;
758
759 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
760 __reserve_free_pmem);
761 if (rc)
762 release_free_pmem(nvdimm_bus, nd_mapping);
763 return rc;
764}
765
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400766/**
767 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
768 * @nd_region: the set of dimms to allocate @n more bytes from
769 * @label_id: unique identifier for the namespace consuming this dpa range
770 * @n: number of bytes per-dimm to add to the existing allocation
771 *
772 * Assumes resources are ordered. For BLK regions, first consume
773 * BLK-only available DPA free space, then consume PMEM-aliased DPA
774 * space starting at the highest DPA. For PMEM regions start
775 * allocations from the start of an interleave set and end at the first
776 * BLK allocation or the end of the interleave set, whichever comes
777 * first.
778 */
779static int grow_dpa_allocation(struct nd_region *nd_region,
780 struct nd_label_id *label_id, resource_size_t n)
781{
Dan Williams1b40e092015-05-01 13:34:01 -0400782 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
783 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400784 int i;
785
786 for (i = 0; i < nd_region->ndr_mappings; i++) {
787 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400788 resource_size_t rem = n;
789 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400790
Dan Williams1b40e092015-05-01 13:34:01 -0400791 /*
792 * In the BLK case try once with all unallocated PMEM
793 * reserved, and once without
794 */
795 for (j = is_pmem; j < 2; j++) {
796 bool blk_only = j == 0;
797
798 if (blk_only) {
799 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
800 if (rc)
801 return rc;
802 }
803 rem = scan_allocate(nd_region, nd_mapping,
804 label_id, rem);
805 if (blk_only)
806 release_free_pmem(nvdimm_bus, nd_mapping);
807
808 /* try again and allow encroachments into PMEM */
809 if (rem == 0)
810 break;
811 }
812
813 dev_WARN_ONCE(&nd_region->dev, rem,
814 "allocation underrun: %#llx of %#llx bytes\n",
815 (unsigned long long) n - rem,
816 (unsigned long long) n);
817 if (rem)
818 return -ENXIO;
819
820 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400821 if (rc)
822 return rc;
823 }
824
825 return 0;
826}
827
828static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
829 struct nd_namespace_pmem *nspm, resource_size_t size)
830{
831 struct resource *res = &nspm->nsio.res;
832
833 res->start = nd_region->ndr_start;
834 res->end = nd_region->ndr_start + size - 1;
835}
836
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300837static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
838{
839 if (!uuid) {
840 dev_dbg(dev, "%s: uuid not set\n", where);
841 return true;
842 }
843 return false;
844}
845
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400846static ssize_t __size_store(struct device *dev, unsigned long long val)
847{
848 resource_size_t allocated = 0, available = 0;
849 struct nd_region *nd_region = to_nd_region(dev->parent);
850 struct nd_mapping *nd_mapping;
851 struct nvdimm_drvdata *ndd;
852 struct nd_label_id label_id;
853 u32 flags = 0, remainder;
854 u8 *uuid = NULL;
855 int rc, i;
856
Dan Williams8c2f7e82015-06-25 04:20:04 -0400857 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400858 return -EBUSY;
859
860 if (is_namespace_pmem(dev)) {
861 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
862
863 uuid = nspm->uuid;
864 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400865 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
866
867 uuid = nsblk->uuid;
868 flags = NSLABEL_FLAG_LOCAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400869 }
870
871 /*
872 * We need a uuid for the allocation-label and dimm(s) on which
873 * to store the label.
874 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300875 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400876 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300877 if (nd_region->ndr_mappings == 0) {
878 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
879 return -ENXIO;
880 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400881
882 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
883 if (remainder) {
884 dev_dbg(dev, "%llu is not %dK aligned\n", val,
885 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
886 return -EINVAL;
887 }
888
889 nd_label_gen_id(&label_id, uuid, flags);
890 for (i = 0; i < nd_region->ndr_mappings; i++) {
891 nd_mapping = &nd_region->mapping[i];
892 ndd = to_ndd(nd_mapping);
893
894 /*
895 * All dimms in an interleave set, or the base dimm for a blk
896 * region, need to be enabled for the size to be changed.
897 */
898 if (!ndd)
899 return -ENXIO;
900
901 allocated += nvdimm_allocated_dpa(ndd, &label_id);
902 }
903 available = nd_region_available_dpa(nd_region);
904
905 if (val > available + allocated)
906 return -ENOSPC;
907
908 if (val == allocated)
909 return 0;
910
911 val = div_u64(val, nd_region->ndr_mappings);
912 allocated = div_u64(allocated, nd_region->ndr_mappings);
913 if (val < allocated)
914 rc = shrink_dpa_allocation(nd_region, &label_id,
915 allocated - val);
916 else
917 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
918
919 if (rc)
920 return rc;
921
922 if (is_namespace_pmem(dev)) {
923 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
924
925 nd_namespace_pmem_set_size(nd_region, nspm,
926 val * nd_region->ndr_mappings);
Dan Williams1b40e092015-05-01 13:34:01 -0400927 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400928 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
929
Dan Williams1b40e092015-05-01 13:34:01 -0400930 /*
931 * Try to delete the namespace if we deleted all of its
Dan Williams8c2f7e82015-06-25 04:20:04 -0400932 * allocation, this is not the seed device for the
933 * region, and it is not actively claimed by a btt
934 * instance.
Dan Williams1b40e092015-05-01 13:34:01 -0400935 */
Dan Williams8c2f7e82015-06-25 04:20:04 -0400936 if (val == 0 && nd_region->ns_seed != dev
937 && !nsblk->common.claim)
Dan Williams1b40e092015-05-01 13:34:01 -0400938 nd_device_unregister(dev, ND_ASYNC);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400939 }
940
941 return rc;
942}
943
944static ssize_t size_store(struct device *dev,
945 struct device_attribute *attr, const char *buf, size_t len)
946{
Dan Williamsf524bf22015-05-30 12:36:02 -0400947 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400948 unsigned long long val;
949 u8 **uuid = NULL;
950 int rc;
951
952 rc = kstrtoull(buf, 0, &val);
953 if (rc)
954 return rc;
955
956 device_lock(dev);
957 nvdimm_bus_lock(dev);
958 wait_nvdimm_bus_probe_idle(dev);
959 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -0400960 if (rc >= 0)
961 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400962
963 if (is_namespace_pmem(dev)) {
964 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
965
966 uuid = &nspm->uuid;
967 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400968 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
969
970 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400971 }
972
973 if (rc == 0 && val == 0 && uuid) {
974 /* setting size zero == 'delete namespace' */
975 kfree(*uuid);
976 *uuid = NULL;
977 }
978
979 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
980 ? "fail" : "success", rc);
981
982 nvdimm_bus_unlock(dev);
983 device_unlock(dev);
984
Dan Williamsf524bf22015-05-30 12:36:02 -0400985 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400986}
987
Dan Williams8c2f7e82015-06-25 04:20:04 -0400988resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400989{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400990 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -0400991
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400992 if (is_namespace_pmem(dev)) {
993 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
994
Dan Williams8c2f7e82015-06-25 04:20:04 -0400995 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400996 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400997 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400998 } else if (is_namespace_io(dev)) {
999 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1000
Dan Williams8c2f7e82015-06-25 04:20:04 -04001001 return resource_size(&nsio->res);
1002 } else
1003 WARN_ONCE(1, "unknown namespace type\n");
1004 return 0;
1005}
Dan Williams1b40e092015-05-01 13:34:01 -04001006
Dan Williams8c2f7e82015-06-25 04:20:04 -04001007resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1008{
1009 resource_size_t size;
1010
1011 nvdimm_bus_lock(&ndns->dev);
1012 size = __nvdimm_namespace_capacity(ndns);
1013 nvdimm_bus_unlock(&ndns->dev);
1014
1015 return size;
1016}
1017EXPORT_SYMBOL(nvdimm_namespace_capacity);
1018
1019static ssize_t size_show(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1021{
1022 return sprintf(buf, "%llu\n", (unsigned long long)
1023 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001024}
1025static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
1026
1027static ssize_t uuid_show(struct device *dev,
1028 struct device_attribute *attr, char *buf)
1029{
1030 u8 *uuid;
1031
1032 if (is_namespace_pmem(dev)) {
1033 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1034
1035 uuid = nspm->uuid;
1036 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001037 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1038
1039 uuid = nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001040 } else
1041 return -ENXIO;
1042
1043 if (uuid)
1044 return sprintf(buf, "%pUb\n", uuid);
1045 return sprintf(buf, "\n");
1046}
1047
1048/**
1049 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1050 * @nd_region: parent region so we can updates all dimms in the set
1051 * @dev: namespace type for generating label_id
1052 * @new_uuid: incoming uuid
1053 * @old_uuid: reference to the uuid storage location in the namespace object
1054 */
1055static int namespace_update_uuid(struct nd_region *nd_region,
1056 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1057{
1058 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1059 struct nd_label_id old_label_id;
1060 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001061 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001062
Dan Williamsf524bf22015-05-30 12:36:02 -04001063 if (!nd_is_uuid_unique(dev, new_uuid))
1064 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001065
1066 if (*old_uuid == NULL)
1067 goto out;
1068
Dan Williamsf524bf22015-05-30 12:36:02 -04001069 /*
1070 * If we've already written a label with this uuid, then it's
1071 * too late to rename because we can't reliably update the uuid
1072 * without losing the old namespace. Userspace must delete this
1073 * namespace to abandon the old uuid.
1074 */
1075 for (i = 0; i < nd_region->ndr_mappings; i++) {
1076 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1077
1078 /*
1079 * This check by itself is sufficient because old_uuid
1080 * would be NULL above if this uuid did not exist in the
1081 * currently written set.
1082 *
1083 * FIXME: can we delete uuid with zero dpa allocated?
1084 */
1085 if (nd_mapping->labels)
1086 return -EBUSY;
1087 }
1088
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001089 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1090 nd_label_gen_id(&new_label_id, new_uuid, flags);
1091 for (i = 0; i < nd_region->ndr_mappings; i++) {
1092 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1093 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1094 struct resource *res;
1095
1096 for_each_dpa_resource(ndd, res)
1097 if (strcmp(res->name, old_label_id.id) == 0)
1098 sprintf((void *) res->name, "%s",
1099 new_label_id.id);
1100 }
1101 kfree(*old_uuid);
1102 out:
1103 *old_uuid = new_uuid;
1104 return 0;
1105}
1106
1107static ssize_t uuid_store(struct device *dev,
1108 struct device_attribute *attr, const char *buf, size_t len)
1109{
1110 struct nd_region *nd_region = to_nd_region(dev->parent);
1111 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001112 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001113 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001114
1115 if (is_namespace_pmem(dev)) {
1116 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1117
1118 ns_uuid = &nspm->uuid;
1119 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001120 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1121
1122 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001123 } else
1124 return -ENXIO;
1125
1126 device_lock(dev);
1127 nvdimm_bus_lock(dev);
1128 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001129 if (to_ndns(dev)->claim)
1130 rc = -EBUSY;
1131 if (rc >= 0)
1132 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001133 if (rc >= 0)
1134 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001135 if (rc >= 0)
1136 rc = nd_namespace_label_update(nd_region, dev);
1137 else
1138 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001139 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1140 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1141 nvdimm_bus_unlock(dev);
1142 device_unlock(dev);
1143
Dan Williamsf524bf22015-05-30 12:36:02 -04001144 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001145}
1146static DEVICE_ATTR_RW(uuid);
1147
1148static ssize_t resource_show(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1150{
1151 struct resource *res;
1152
1153 if (is_namespace_pmem(dev)) {
1154 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1155
1156 res = &nspm->nsio.res;
1157 } else if (is_namespace_io(dev)) {
1158 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1159
1160 res = &nsio->res;
1161 } else
1162 return -ENXIO;
1163
1164 /* no address to convey if the namespace has no allocation */
1165 if (resource_size(res) == 0)
1166 return -ENXIO;
1167 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1168}
1169static DEVICE_ATTR_RO(resource);
1170
Vishal Vermafcae6952015-06-25 04:22:39 -04001171static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1172 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001173
1174static ssize_t sector_size_show(struct device *dev,
1175 struct device_attribute *attr, char *buf)
1176{
1177 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1178
1179 if (!is_namespace_blk(dev))
1180 return -ENXIO;
1181
1182 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1183}
1184
1185static ssize_t sector_size_store(struct device *dev,
1186 struct device_attribute *attr, const char *buf, size_t len)
1187{
1188 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williamsf524bf22015-05-30 12:36:02 -04001189 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001190 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001191
1192 if (!is_namespace_blk(dev))
1193 return -ENXIO;
1194
1195 device_lock(dev);
1196 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001197 if (to_ndns(dev)->claim)
1198 rc = -EBUSY;
1199 if (rc >= 0)
1200 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1201 ns_lbasize_supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001202 if (rc >= 0)
1203 rc = nd_namespace_label_update(nd_region, dev);
1204 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1205 rc, rc < 0 ? "tried" : "wrote", buf,
1206 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001207 nvdimm_bus_unlock(dev);
1208 device_unlock(dev);
1209
1210 return rc ? rc : len;
1211}
1212static DEVICE_ATTR_RW(sector_size);
1213
Dan Williams0ba1c632015-05-30 12:35:36 -04001214static ssize_t dpa_extents_show(struct device *dev,
1215 struct device_attribute *attr, char *buf)
1216{
1217 struct nd_region *nd_region = to_nd_region(dev->parent);
1218 struct nd_label_id label_id;
1219 int count = 0, i;
1220 u8 *uuid = NULL;
1221 u32 flags = 0;
1222
1223 nvdimm_bus_lock(dev);
1224 if (is_namespace_pmem(dev)) {
1225 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1226
1227 uuid = nspm->uuid;
1228 flags = 0;
1229 } else if (is_namespace_blk(dev)) {
1230 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1231
1232 uuid = nsblk->uuid;
1233 flags = NSLABEL_FLAG_LOCAL;
1234 }
1235
1236 if (!uuid)
1237 goto out;
1238
1239 nd_label_gen_id(&label_id, uuid, flags);
1240 for (i = 0; i < nd_region->ndr_mappings; i++) {
1241 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1242 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1243 struct resource *res;
1244
1245 for_each_dpa_resource(ndd, res)
1246 if (strcmp(res->name, label_id.id) == 0)
1247 count++;
1248 }
1249 out:
1250 nvdimm_bus_unlock(dev);
1251
1252 return sprintf(buf, "%d\n", count);
1253}
1254static DEVICE_ATTR_RO(dpa_extents);
1255
Dan Williams8c2f7e82015-06-25 04:20:04 -04001256static ssize_t holder_show(struct device *dev,
1257 struct device_attribute *attr, char *buf)
1258{
1259 struct nd_namespace_common *ndns = to_ndns(dev);
1260 ssize_t rc;
1261
1262 device_lock(dev);
1263 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1264 device_unlock(dev);
1265
1266 return rc;
1267}
1268static DEVICE_ATTR_RO(holder);
1269
Dan Williams0731de02015-12-14 15:34:15 -08001270static ssize_t mode_show(struct device *dev,
1271 struct device_attribute *attr, char *buf)
1272{
1273 struct nd_namespace_common *ndns = to_ndns(dev);
1274 struct device *claim;
1275 char *mode;
1276 ssize_t rc;
1277
1278 device_lock(dev);
1279 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001280 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001281 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001282 else if (claim && is_nd_pfn(claim))
1283 mode = "memory";
1284 else if (!claim && pmem_should_map_pages(dev))
1285 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001286 else
1287 mode = "raw";
1288 rc = sprintf(buf, "%s\n", mode);
1289 device_unlock(dev);
1290
1291 return rc;
1292}
1293static DEVICE_ATTR_RO(mode);
1294
Dan Williams8c2f7e82015-06-25 04:20:04 -04001295static ssize_t force_raw_store(struct device *dev,
1296 struct device_attribute *attr, const char *buf, size_t len)
1297{
1298 bool force_raw;
1299 int rc = strtobool(buf, &force_raw);
1300
1301 if (rc)
1302 return rc;
1303
1304 to_ndns(dev)->force_raw = force_raw;
1305 return len;
1306}
1307
1308static ssize_t force_raw_show(struct device *dev,
1309 struct device_attribute *attr, char *buf)
1310{
1311 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1312}
1313static DEVICE_ATTR_RW(force_raw);
1314
Dan Williams3d880022015-05-31 15:02:11 -04001315static struct attribute *nd_namespace_attributes[] = {
1316 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001317 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001318 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001319 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001320 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001321 &dev_attr_resource.attr,
1322 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001323 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001324 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001325 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001326 NULL,
1327};
1328
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001329static umode_t namespace_visible(struct kobject *kobj,
1330 struct attribute *a, int n)
1331{
1332 struct device *dev = container_of(kobj, struct device, kobj);
1333
1334 if (a == &dev_attr_resource.attr) {
1335 if (is_namespace_blk(dev))
1336 return 0;
1337 return a->mode;
1338 }
1339
1340 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1341 if (a == &dev_attr_size.attr)
1342 return S_IWUSR | S_IRUGO;
Dan Williams1b40e092015-05-01 13:34:01 -04001343
1344 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1345 return 0;
1346
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001347 return a->mode;
1348 }
1349
Dan Williams8c2f7e82015-06-25 04:20:04 -04001350 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1351 || a == &dev_attr_holder.attr
Dan Williams0731de02015-12-14 15:34:15 -08001352 || a == &dev_attr_force_raw.attr
1353 || a == &dev_attr_mode.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001354 return a->mode;
1355
1356 return 0;
1357}
1358
Dan Williams3d880022015-05-31 15:02:11 -04001359static struct attribute_group nd_namespace_attribute_group = {
1360 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001361 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001362};
1363
1364static const struct attribute_group *nd_namespace_attribute_groups[] = {
1365 &nd_device_attribute_group,
1366 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001367 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001368 NULL,
1369};
1370
Dan Williams8c2f7e82015-06-25 04:20:04 -04001371struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1372{
1373 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001374 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001375 struct nd_namespace_common *ndns;
1376 resource_size_t size;
1377
Dan Williamse1455742015-07-30 17:57:47 -04001378 if (nd_btt || nd_pfn) {
1379 struct device *host = NULL;
1380
1381 if (nd_btt) {
1382 host = &nd_btt->dev;
1383 ndns = nd_btt->ndns;
1384 } else if (nd_pfn) {
1385 host = &nd_pfn->dev;
1386 ndns = nd_pfn->ndns;
1387 }
1388
1389 if (!ndns || !host)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001390 return ERR_PTR(-ENODEV);
1391
1392 /*
1393 * Flush any in-progess probes / removals in the driver
1394 * for the raw personality of this namespace.
1395 */
1396 device_lock(&ndns->dev);
1397 device_unlock(&ndns->dev);
1398 if (ndns->dev.driver) {
1399 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williamse1455742015-07-30 17:57:47 -04001400 dev_name(host));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001401 return ERR_PTR(-EBUSY);
1402 }
Dan Williamse1455742015-07-30 17:57:47 -04001403 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != host,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001404 "host (%s) vs claim (%s) mismatch\n",
Dan Williamse1455742015-07-30 17:57:47 -04001405 dev_name(host),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001406 dev_name(ndns->claim)))
1407 return ERR_PTR(-ENXIO);
1408 } else {
1409 ndns = to_ndns(dev);
1410 if (ndns->claim) {
1411 dev_dbg(dev, "claimed by %s, failing probe\n",
1412 dev_name(ndns->claim));
1413
1414 return ERR_PTR(-ENXIO);
1415 }
1416 }
1417
1418 size = nvdimm_namespace_capacity(ndns);
1419 if (size < ND_MIN_NAMESPACE_SIZE) {
1420 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1421 &size, ND_MIN_NAMESPACE_SIZE);
1422 return ERR_PTR(-ENODEV);
1423 }
1424
1425 if (is_namespace_pmem(&ndns->dev)) {
1426 struct nd_namespace_pmem *nspm;
1427
1428 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001429 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001430 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001431 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001432 struct nd_namespace_blk *nsblk;
1433
1434 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001435 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1436 return ERR_PTR(-ENODEV);
1437 if (!nsblk->lbasize) {
1438 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1439 __func__);
1440 return ERR_PTR(-ENODEV);
1441 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001442 if (!nd_namespace_blk_validate(nsblk))
1443 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001444 }
1445
1446 return ndns;
1447}
1448EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1449
Dan Williams3d880022015-05-31 15:02:11 -04001450static struct device **create_namespace_io(struct nd_region *nd_region)
1451{
1452 struct nd_namespace_io *nsio;
1453 struct device *dev, **devs;
1454 struct resource *res;
1455
1456 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1457 if (!nsio)
1458 return NULL;
1459
1460 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1461 if (!devs) {
1462 kfree(nsio);
1463 return NULL;
1464 }
1465
Dan Williams8c2f7e82015-06-25 04:20:04 -04001466 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001467 dev->type = &namespace_io_device_type;
1468 dev->parent = &nd_region->dev;
1469 res = &nsio->res;
1470 res->name = dev_name(&nd_region->dev);
1471 res->flags = IORESOURCE_MEM;
1472 res->start = nd_region->ndr_start;
1473 res->end = res->start + nd_region->ndr_size - 1;
1474
1475 devs[0] = dev;
1476 return devs;
1477}
1478
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001479static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1480 u64 cookie, u16 pos)
1481{
1482 struct nd_namespace_label *found = NULL;
1483 int i;
1484
1485 for (i = 0; i < nd_region->ndr_mappings; i++) {
1486 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1487 struct nd_namespace_label *nd_label;
1488 bool found_uuid = false;
1489 int l;
1490
1491 for_each_label(l, nd_label, nd_mapping->labels) {
1492 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1493 u16 position = __le16_to_cpu(nd_label->position);
1494 u16 nlabel = __le16_to_cpu(nd_label->nlabel);
1495
1496 if (isetcookie != cookie)
1497 continue;
1498
1499 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1500 continue;
1501
1502 if (found_uuid) {
1503 dev_dbg(to_ndd(nd_mapping)->dev,
1504 "%s duplicate entry for uuid\n",
1505 __func__);
1506 return false;
1507 }
1508 found_uuid = true;
1509 if (nlabel != nd_region->ndr_mappings)
1510 continue;
1511 if (position != pos)
1512 continue;
1513 found = nd_label;
1514 break;
1515 }
1516 if (found)
1517 break;
1518 }
1519 return found != NULL;
1520}
1521
1522static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1523{
1524 struct nd_namespace_label *select = NULL;
1525 int i;
1526
1527 if (!pmem_id)
1528 return -ENODEV;
1529
1530 for (i = 0; i < nd_region->ndr_mappings; i++) {
1531 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1532 struct nd_namespace_label *nd_label;
1533 u64 hw_start, hw_end, pmem_start, pmem_end;
1534 int l;
1535
1536 for_each_label(l, nd_label, nd_mapping->labels)
1537 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1538 break;
1539
1540 if (!nd_label) {
1541 WARN_ON(1);
1542 return -EINVAL;
1543 }
1544
1545 select = nd_label;
1546 /*
1547 * Check that this label is compliant with the dpa
1548 * range published in NFIT
1549 */
1550 hw_start = nd_mapping->start;
1551 hw_end = hw_start + nd_mapping->size;
1552 pmem_start = __le64_to_cpu(select->dpa);
1553 pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
1554 if (pmem_start == hw_start && pmem_end <= hw_end)
1555 /* pass */;
1556 else
1557 return -EINVAL;
1558
1559 nd_mapping->labels[0] = select;
1560 nd_mapping->labels[1] = NULL;
1561 }
1562 return 0;
1563}
1564
1565/**
1566 * find_pmem_label_set - validate interleave set labelling, retrieve label0
1567 * @nd_region: region with mappings to validate
1568 */
1569static int find_pmem_label_set(struct nd_region *nd_region,
1570 struct nd_namespace_pmem *nspm)
1571{
1572 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1573 struct nd_namespace_label *nd_label;
1574 u8 select_id[NSLABEL_UUID_LEN];
1575 resource_size_t size = 0;
1576 u8 *pmem_id = NULL;
1577 int rc = -ENODEV, l;
1578 u16 i;
1579
1580 if (cookie == 0)
1581 return -ENXIO;
1582
1583 /*
1584 * Find a complete set of labels by uuid. By definition we can start
1585 * with any mapping as the reference label
1586 */
1587 for_each_label(l, nd_label, nd_region->mapping[0].labels) {
1588 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1589
1590 if (isetcookie != cookie)
1591 continue;
1592
1593 for (i = 0; nd_region->ndr_mappings; i++)
1594 if (!has_uuid_at_pos(nd_region, nd_label->uuid,
1595 cookie, i))
1596 break;
1597 if (i < nd_region->ndr_mappings) {
1598 /*
1599 * Give up if we don't find an instance of a
1600 * uuid at each position (from 0 to
1601 * nd_region->ndr_mappings - 1), or if we find a
1602 * dimm with two instances of the same uuid.
1603 */
1604 rc = -EINVAL;
1605 goto err;
1606 } else if (pmem_id) {
1607 /*
1608 * If there is more than one valid uuid set, we
1609 * need userspace to clean this up.
1610 */
1611 rc = -EBUSY;
1612 goto err;
1613 }
1614 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
1615 pmem_id = select_id;
1616 }
1617
1618 /*
1619 * Fix up each mapping's 'labels' to have the validated pmem label for
1620 * that position at labels[0], and NULL at labels[1]. In the process,
1621 * check that the namespace aligns with interleave-set. We know
1622 * that it does not overlap with any blk namespaces by virtue of
1623 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1624 * succeeded).
1625 */
1626 rc = select_pmem_id(nd_region, pmem_id);
1627 if (rc)
1628 goto err;
1629
1630 /* Calculate total size and populate namespace properties from label0 */
1631 for (i = 0; i < nd_region->ndr_mappings; i++) {
1632 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1633 struct nd_namespace_label *label0 = nd_mapping->labels[0];
1634
1635 size += __le64_to_cpu(label0->rawsize);
1636 if (__le16_to_cpu(label0->position) != 0)
1637 continue;
1638 WARN_ON(nspm->alt_name || nspm->uuid);
1639 nspm->alt_name = kmemdup((void __force *) label0->name,
1640 NSLABEL_NAME_LEN, GFP_KERNEL);
1641 nspm->uuid = kmemdup((void __force *) label0->uuid,
1642 NSLABEL_UUID_LEN, GFP_KERNEL);
1643 }
1644
1645 if (!nspm->alt_name || !nspm->uuid) {
1646 rc = -ENOMEM;
1647 goto err;
1648 }
1649
1650 nd_namespace_pmem_set_size(nd_region, nspm, size);
1651
1652 return 0;
1653 err:
1654 switch (rc) {
1655 case -EINVAL:
1656 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1657 break;
1658 case -ENODEV:
1659 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1660 break;
1661 default:
1662 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1663 __func__, rc);
1664 break;
1665 }
1666 return rc;
1667}
1668
1669static struct device **create_namespace_pmem(struct nd_region *nd_region)
1670{
1671 struct nd_namespace_pmem *nspm;
1672 struct device *dev, **devs;
1673 struct resource *res;
1674 int rc;
1675
1676 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1677 if (!nspm)
1678 return NULL;
1679
Dan Williams8c2f7e82015-06-25 04:20:04 -04001680 dev = &nspm->nsio.common.dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001681 dev->type = &namespace_pmem_device_type;
1682 dev->parent = &nd_region->dev;
1683 res = &nspm->nsio.res;
1684 res->name = dev_name(&nd_region->dev);
1685 res->flags = IORESOURCE_MEM;
1686 rc = find_pmem_label_set(nd_region, nspm);
1687 if (rc == -ENODEV) {
1688 int i;
1689
1690 /* Pass, try to permit namespace creation... */
1691 for (i = 0; i < nd_region->ndr_mappings; i++) {
1692 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1693
1694 kfree(nd_mapping->labels);
1695 nd_mapping->labels = NULL;
1696 }
1697
1698 /* Publish a zero-sized namespace for userspace to configure. */
1699 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1700
1701 rc = 0;
1702 } else if (rc)
1703 goto err;
1704
1705 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1706 if (!devs)
1707 goto err;
1708
1709 devs[0] = dev;
1710 return devs;
1711
1712 err:
Dan Williams8c2f7e82015-06-25 04:20:04 -04001713 namespace_pmem_release(&nspm->nsio.common.dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001714 return NULL;
1715}
1716
Dan Williams1b40e092015-05-01 13:34:01 -04001717struct resource *nsblk_add_resource(struct nd_region *nd_region,
1718 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1719 resource_size_t start)
1720{
1721 struct nd_label_id label_id;
1722 struct resource *res;
1723
1724 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1725 res = krealloc(nsblk->res,
1726 sizeof(void *) * (nsblk->num_resources + 1),
1727 GFP_KERNEL);
1728 if (!res)
1729 return NULL;
1730 nsblk->res = (struct resource **) res;
1731 for_each_dpa_resource(ndd, res)
1732 if (strcmp(res->name, label_id.id) == 0
1733 && res->start == start) {
1734 nsblk->res[nsblk->num_resources++] = res;
1735 return res;
1736 }
1737 return NULL;
1738}
1739
1740static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1741{
1742 struct nd_namespace_blk *nsblk;
1743 struct device *dev;
1744
1745 if (!is_nd_blk(&nd_region->dev))
1746 return NULL;
1747
1748 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1749 if (!nsblk)
1750 return NULL;
1751
Dan Williams8c2f7e82015-06-25 04:20:04 -04001752 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001753 dev->type = &namespace_blk_device_type;
1754 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1755 if (nsblk->id < 0) {
1756 kfree(nsblk);
1757 return NULL;
1758 }
1759 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1760 dev->parent = &nd_region->dev;
1761 dev->groups = nd_namespace_attribute_groups;
1762
Dan Williams8c2f7e82015-06-25 04:20:04 -04001763 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001764}
1765
1766void nd_region_create_blk_seed(struct nd_region *nd_region)
1767{
1768 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1769 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1770 /*
1771 * Seed creation failures are not fatal, provisioning is simply
1772 * disabled until memory becomes available
1773 */
1774 if (!nd_region->ns_seed)
1775 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1776 else
1777 nd_device_register(nd_region->ns_seed);
1778}
1779
Dan Williams2dc43332015-12-13 11:41:36 -08001780void nd_region_create_pfn_seed(struct nd_region *nd_region)
1781{
1782 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1783 nd_region->pfn_seed = nd_pfn_create(nd_region);
1784 /*
1785 * Seed creation failures are not fatal, provisioning is simply
1786 * disabled until memory becomes available
1787 */
1788 if (!nd_region->pfn_seed)
1789 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1790}
1791
Dan Williams8c2f7e82015-06-25 04:20:04 -04001792void nd_region_create_btt_seed(struct nd_region *nd_region)
1793{
1794 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1795 nd_region->btt_seed = nd_btt_create(nd_region);
1796 /*
1797 * Seed creation failures are not fatal, provisioning is simply
1798 * disabled until memory becomes available
1799 */
1800 if (!nd_region->btt_seed)
1801 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1802}
1803
Dan Williams1b40e092015-05-01 13:34:01 -04001804static struct device **create_namespace_blk(struct nd_region *nd_region)
1805{
1806 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1807 struct nd_namespace_label *nd_label;
1808 struct device *dev, **devs = NULL;
1809 struct nd_namespace_blk *nsblk;
1810 struct nvdimm_drvdata *ndd;
1811 int i, l, count = 0;
1812 struct resource *res;
1813
1814 if (nd_region->ndr_mappings == 0)
1815 return NULL;
1816
1817 ndd = to_ndd(nd_mapping);
1818 for_each_label(l, nd_label, nd_mapping->labels) {
1819 u32 flags = __le32_to_cpu(nd_label->flags);
1820 char *name[NSLABEL_NAME_LEN];
1821 struct device **__devs;
1822
1823 if (flags & NSLABEL_FLAG_LOCAL)
1824 /* pass */;
1825 else
1826 continue;
1827
1828 for (i = 0; i < count; i++) {
1829 nsblk = to_nd_namespace_blk(devs[i]);
1830 if (memcmp(nsblk->uuid, nd_label->uuid,
1831 NSLABEL_UUID_LEN) == 0) {
1832 res = nsblk_add_resource(nd_region, ndd, nsblk,
1833 __le64_to_cpu(nd_label->dpa));
1834 if (!res)
1835 goto err;
1836 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001837 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001838 break;
1839 }
1840 }
1841 if (i < count)
1842 continue;
1843 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1844 if (!__devs)
1845 goto err;
1846 memcpy(__devs, devs, sizeof(dev) * count);
1847 kfree(devs);
1848 devs = __devs;
1849
1850 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1851 if (!nsblk)
1852 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001853 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001854 dev->type = &namespace_blk_device_type;
1855 dev->parent = &nd_region->dev;
1856 dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
1857 devs[count++] = dev;
1858 nsblk->id = -1;
1859 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1860 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1861 GFP_KERNEL);
1862 if (!nsblk->uuid)
1863 goto err;
1864 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1865 if (name[0])
1866 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1867 GFP_KERNEL);
1868 res = nsblk_add_resource(nd_region, ndd, nsblk,
1869 __le64_to_cpu(nd_label->dpa));
1870 if (!res)
1871 goto err;
1872 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001873 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001874 }
1875
1876 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
1877 __func__, count, count == 1 ? "" : "s");
1878
1879 if (count == 0) {
1880 /* Publish a zero-sized namespace for userspace to configure. */
1881 for (i = 0; i < nd_region->ndr_mappings; i++) {
1882 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1883
1884 kfree(nd_mapping->labels);
1885 nd_mapping->labels = NULL;
1886 }
1887
1888 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1889 if (!devs)
1890 goto err;
1891 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1892 if (!nsblk)
1893 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001894 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001895 dev->type = &namespace_blk_device_type;
1896 dev->parent = &nd_region->dev;
1897 devs[count++] = dev;
1898 }
1899
1900 return devs;
1901
1902err:
1903 for (i = 0; i < count; i++) {
1904 nsblk = to_nd_namespace_blk(devs[i]);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001905 namespace_blk_release(&nsblk->common.dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001906 }
1907 kfree(devs);
1908 return NULL;
1909}
1910
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001911static int init_active_labels(struct nd_region *nd_region)
1912{
1913 int i;
1914
1915 for (i = 0; i < nd_region->ndr_mappings; i++) {
1916 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1917 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1918 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1919 int count, j;
1920
1921 /*
1922 * If the dimm is disabled then prevent the region from
1923 * being activated if it aliases DPA.
1924 */
1925 if (!ndd) {
1926 if ((nvdimm->flags & NDD_ALIASING) == 0)
1927 return 0;
1928 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
1929 dev_name(&nd_mapping->nvdimm->dev));
1930 return -ENXIO;
1931 }
1932 nd_mapping->ndd = ndd;
1933 atomic_inc(&nvdimm->busy);
1934 get_ndd(ndd);
1935
1936 count = nd_label_active_count(ndd);
1937 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
1938 if (!count)
1939 continue;
1940 nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
1941 GFP_KERNEL);
1942 if (!nd_mapping->labels)
1943 return -ENOMEM;
1944 for (j = 0; j < count; j++) {
1945 struct nd_namespace_label *label;
1946
1947 label = nd_label_active(ndd, j);
1948 nd_mapping->labels[j] = label;
1949 }
1950 }
1951
1952 return 0;
1953}
1954
Dan Williams3d880022015-05-31 15:02:11 -04001955int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
1956{
1957 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001958 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04001959
1960 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001961 nvdimm_bus_lock(&nd_region->dev);
1962 rc = init_active_labels(nd_region);
1963 if (rc) {
1964 nvdimm_bus_unlock(&nd_region->dev);
1965 return rc;
1966 }
1967
1968 type = nd_region_to_nstype(nd_region);
1969 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04001970 case ND_DEVICE_NAMESPACE_IO:
1971 devs = create_namespace_io(nd_region);
1972 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001973 case ND_DEVICE_NAMESPACE_PMEM:
1974 devs = create_namespace_pmem(nd_region);
1975 break;
Dan Williams1b40e092015-05-01 13:34:01 -04001976 case ND_DEVICE_NAMESPACE_BLK:
1977 devs = create_namespace_blk(nd_region);
1978 break;
Dan Williams3d880022015-05-31 15:02:11 -04001979 default:
1980 break;
1981 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001982 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04001983
1984 if (!devs)
1985 return -ENODEV;
1986
Dan Williams3d880022015-05-31 15:02:11 -04001987 for (i = 0; devs[i]; i++) {
1988 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04001989 int id;
Dan Williams3d880022015-05-31 15:02:11 -04001990
Dan Williams1b40e092015-05-01 13:34:01 -04001991 if (type == ND_DEVICE_NAMESPACE_BLK) {
1992 struct nd_namespace_blk *nsblk;
1993
1994 nsblk = to_nd_namespace_blk(dev);
1995 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
1996 GFP_KERNEL);
1997 nsblk->id = id;
1998 } else
1999 id = i;
2000
2001 if (id < 0)
2002 break;
2003 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002004 dev->groups = nd_namespace_attribute_groups;
2005 nd_device_register(dev);
2006 }
Dan Williams1b40e092015-05-01 13:34:01 -04002007 if (i)
2008 nd_region->ns_seed = devs[0];
2009
2010 if (devs[i]) {
2011 int j;
2012
2013 for (j = i; devs[j]; j++) {
2014 struct device *dev = devs[j];
2015
2016 device_initialize(dev);
2017 put_device(dev);
2018 }
2019 *err = j - i;
2020 /*
2021 * All of the namespaces we tried to register failed, so
2022 * fail region activation.
2023 */
2024 if (*err == 0)
2025 rc = -ENODEV;
2026 }
Dan Williams3d880022015-05-31 15:02:11 -04002027 kfree(devs);
2028
Dan Williams1b40e092015-05-01 13:34:01 -04002029 if (rc == -ENODEV)
2030 return rc;
2031
Dan Williams3d880022015-05-31 15:02:11 -04002032 return i;
2033}