blob: 9303ca29be9b6e337340faf7bcd4e57b2414fb3c [file] [log] [blame]
Dan Williams3d880022015-05-31 15:02:11 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/slab.h>
16#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040017#include "nd-core.h"
Dan Williams3d880022015-05-31 15:02:11 -040018#include "nd.h"
19
20static void namespace_io_release(struct device *dev)
21{
22 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
23
24 kfree(nsio);
25}
26
Dan Williamsbf9bccc2015-06-17 17:14:46 -040027static void namespace_pmem_release(struct device *dev)
28{
29 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
30
31 kfree(nspm->alt_name);
32 kfree(nspm->uuid);
33 kfree(nspm);
34}
35
36static void namespace_blk_release(struct device *dev)
37{
Dan Williams1b40e092015-05-01 13:34:01 -040038 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
39 struct nd_region *nd_region = to_nd_region(dev->parent);
40
41 if (nsblk->id >= 0)
42 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
43 kfree(nsblk->alt_name);
44 kfree(nsblk->uuid);
45 kfree(nsblk->res);
46 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040047}
48
Dan Williams3d880022015-05-31 15:02:11 -040049static struct device_type namespace_io_device_type = {
50 .name = "nd_namespace_io",
51 .release = namespace_io_release,
52};
53
Dan Williamsbf9bccc2015-06-17 17:14:46 -040054static struct device_type namespace_pmem_device_type = {
55 .name = "nd_namespace_pmem",
56 .release = namespace_pmem_release,
57};
58
59static struct device_type namespace_blk_device_type = {
60 .name = "nd_namespace_blk",
61 .release = namespace_blk_release,
62};
63
64static bool is_namespace_pmem(struct device *dev)
65{
66 return dev ? dev->type == &namespace_pmem_device_type : false;
67}
68
69static bool is_namespace_blk(struct device *dev)
70{
71 return dev ? dev->type == &namespace_blk_device_type : false;
72}
73
74static bool is_namespace_io(struct device *dev)
75{
76 return dev ? dev->type == &namespace_io_device_type : false;
77}
78
Vishal Verma5212e112015-06-25 04:20:32 -040079const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
80 char *name)
81{
82 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
83 const char *suffix = "";
84
Dan Williamse1455742015-07-30 17:57:47 -040085 if (ndns->claim) {
86 if (is_nd_btt(ndns->claim))
87 suffix = "s";
88 else if (is_nd_pfn(ndns->claim))
89 suffix = "m";
90 else
91 dev_WARN_ONCE(&ndns->dev, 1,
92 "unknown claim type by %s\n",
93 dev_name(ndns->claim));
94 }
Vishal Verma5212e112015-06-25 04:20:32 -040095
96 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev))
97 sprintf(name, "pmem%d%s", nd_region->id, suffix);
98 else if (is_namespace_blk(&ndns->dev)) {
99 struct nd_namespace_blk *nsblk;
100
101 nsblk = to_nd_namespace_blk(&ndns->dev);
102 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, suffix);
103 } else {
104 return NULL;
105 }
106
107 return name;
108}
109EXPORT_SYMBOL(nvdimm_namespace_disk_name);
110
Vishal Verma6ec68952015-07-29 14:58:09 -0600111const u8 *nd_dev_to_uuid(struct device *dev)
112{
113 static const u8 null_uuid[16];
114
115 if (!dev)
116 return null_uuid;
117
118 if (is_namespace_pmem(dev)) {
119 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
120
121 return nspm->uuid;
122 } else if (is_namespace_blk(dev)) {
123 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
124
125 return nsblk->uuid;
126 } else
127 return null_uuid;
128}
129EXPORT_SYMBOL(nd_dev_to_uuid);
130
Dan Williams3d880022015-05-31 15:02:11 -0400131static ssize_t nstype_show(struct device *dev,
132 struct device_attribute *attr, char *buf)
133{
134 struct nd_region *nd_region = to_nd_region(dev->parent);
135
136 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
137}
138static DEVICE_ATTR_RO(nstype);
139
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400140static ssize_t __alt_name_store(struct device *dev, const char *buf,
141 const size_t len)
142{
143 char *input, *pos, *alt_name, **ns_altname;
144 ssize_t rc;
145
146 if (is_namespace_pmem(dev)) {
147 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
148
149 ns_altname = &nspm->alt_name;
150 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400151 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
152
153 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400154 } else
155 return -ENXIO;
156
Dan Williams8c2f7e82015-06-25 04:20:04 -0400157 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400158 return -EBUSY;
159
160 input = kmemdup(buf, len + 1, GFP_KERNEL);
161 if (!input)
162 return -ENOMEM;
163
164 input[len] = '\0';
165 pos = strim(input);
166 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
167 rc = -EINVAL;
168 goto out;
169 }
170
171 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
172 if (!alt_name) {
173 rc = -ENOMEM;
174 goto out;
175 }
176 kfree(*ns_altname);
177 *ns_altname = alt_name;
178 sprintf(*ns_altname, "%s", pos);
179 rc = len;
180
181out:
182 kfree(input);
183 return rc;
184}
185
Dan Williams1b40e092015-05-01 13:34:01 -0400186static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
187{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400188 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400189 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
190 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
191 struct nd_label_id label_id;
192 resource_size_t size = 0;
193 struct resource *res;
194
195 if (!nsblk->uuid)
196 return 0;
197 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
198 for_each_dpa_resource(ndd, res)
199 if (strcmp(res->name, label_id.id) == 0)
200 size += resource_size(res);
201 return size;
202}
203
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400204static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
205{
206 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
207 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
208 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
209 struct nd_label_id label_id;
210 struct resource *res;
211 int count, i;
212
213 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
214 return false;
215
216 count = 0;
217 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
218 for_each_dpa_resource(ndd, res) {
219 if (strcmp(res->name, label_id.id) != 0)
220 continue;
221 /*
222 * Resources with unacknoweldged adjustments indicate a
223 * failure to update labels
224 */
225 if (res->flags & DPA_RESOURCE_ADJUSTED)
226 return false;
227 count++;
228 }
229
230 /* These values match after a successful label update */
231 if (count != nsblk->num_resources)
232 return false;
233
234 for (i = 0; i < nsblk->num_resources; i++) {
235 struct resource *found = NULL;
236
237 for_each_dpa_resource(ndd, res)
238 if (res == nsblk->res[i]) {
239 found = res;
240 break;
241 }
242 /* stale resource */
243 if (!found)
244 return false;
245 }
246
247 return true;
248}
249
250resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
251{
252 resource_size_t size;
253
254 nvdimm_bus_lock(&nsblk->common.dev);
255 size = __nd_namespace_blk_validate(nsblk);
256 nvdimm_bus_unlock(&nsblk->common.dev);
257
258 return size;
259}
260EXPORT_SYMBOL(nd_namespace_blk_validate);
261
262
Dan Williamsf524bf22015-05-30 12:36:02 -0400263static int nd_namespace_label_update(struct nd_region *nd_region,
264 struct device *dev)
265{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400266 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400267 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400268 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400269 return 0;
270
271 /*
272 * Only allow label writes that will result in a valid namespace
273 * or deletion of an existing namespace.
274 */
275 if (is_namespace_pmem(dev)) {
276 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400277 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400278
279 if (size == 0 && nspm->uuid)
280 /* delete allocation */;
281 else if (!nspm->uuid)
282 return 0;
283
284 return nd_pmem_namespace_label_update(nd_region, nspm, size);
285 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400286 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
287 resource_size_t size = nd_namespace_blk_size(nsblk);
288
289 if (size == 0 && nsblk->uuid)
290 /* delete allocation */;
291 else if (!nsblk->uuid || !nsblk->lbasize)
292 return 0;
293
294 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400295 } else
296 return -ENXIO;
297}
298
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400299static ssize_t alt_name_store(struct device *dev,
300 struct device_attribute *attr, const char *buf, size_t len)
301{
Dan Williamsf524bf22015-05-30 12:36:02 -0400302 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400303 ssize_t rc;
304
305 device_lock(dev);
306 nvdimm_bus_lock(dev);
307 wait_nvdimm_bus_probe_idle(dev);
308 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400309 if (rc >= 0)
310 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400311 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
312 nvdimm_bus_unlock(dev);
313 device_unlock(dev);
314
Dan Williamsf524bf22015-05-30 12:36:02 -0400315 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400316}
317
318static ssize_t alt_name_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
320{
321 char *ns_altname;
322
323 if (is_namespace_pmem(dev)) {
324 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
325
326 ns_altname = nspm->alt_name;
327 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400328 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
329
330 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400331 } else
332 return -ENXIO;
333
334 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
335}
336static DEVICE_ATTR_RW(alt_name);
337
338static int scan_free(struct nd_region *nd_region,
339 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
340 resource_size_t n)
341{
342 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
343 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
344 int rc = 0;
345
346 while (n) {
347 struct resource *res, *last;
348 resource_size_t new_start;
349
350 last = NULL;
351 for_each_dpa_resource(ndd, res)
352 if (strcmp(res->name, label_id->id) == 0)
353 last = res;
354 res = last;
355 if (!res)
356 return 0;
357
358 if (n >= resource_size(res)) {
359 n -= resource_size(res);
360 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
361 nvdimm_free_dpa(ndd, res);
362 /* retry with last resource deleted */
363 continue;
364 }
365
366 /*
367 * Keep BLK allocations relegated to high DPA as much as
368 * possible
369 */
370 if (is_blk)
371 new_start = res->start + n;
372 else
373 new_start = res->start;
374
375 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400376 if (rc == 0)
377 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400378 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
379 break;
380 }
381
382 return rc;
383}
384
385/**
386 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
387 * @nd_region: the set of dimms to reclaim @n bytes from
388 * @label_id: unique identifier for the namespace consuming this dpa range
389 * @n: number of bytes per-dimm to release
390 *
391 * Assumes resources are ordered. Starting from the end try to
392 * adjust_resource() the allocation to @n, but if @n is larger than the
393 * allocation delete it and find the 'new' last allocation in the label
394 * set.
395 */
396static int shrink_dpa_allocation(struct nd_region *nd_region,
397 struct nd_label_id *label_id, resource_size_t n)
398{
399 int i;
400
401 for (i = 0; i < nd_region->ndr_mappings; i++) {
402 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
403 int rc;
404
405 rc = scan_free(nd_region, nd_mapping, label_id, n);
406 if (rc)
407 return rc;
408 }
409
410 return 0;
411}
412
413static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
414 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
415 resource_size_t n)
416{
417 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
418 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
419 resource_size_t first_dpa;
420 struct resource *res;
421 int rc = 0;
422
423 /* allocate blk from highest dpa first */
424 if (is_blk)
425 first_dpa = nd_mapping->start + nd_mapping->size - n;
426 else
427 first_dpa = nd_mapping->start;
428
429 /* first resource allocation for this label-id or dimm */
430 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
431 if (!res)
432 rc = -EBUSY;
433
434 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
435 return rc ? n : 0;
436}
437
Dan Williams1b40e092015-05-01 13:34:01 -0400438static bool space_valid(bool is_pmem, bool is_reserve,
439 struct nd_label_id *label_id, struct resource *res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400440{
441 /*
442 * For BLK-space any space is valid, for PMEM-space, it must be
Dan Williams1b40e092015-05-01 13:34:01 -0400443 * contiguous with an existing allocation unless we are
444 * reserving pmem.
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400445 */
Dan Williams1b40e092015-05-01 13:34:01 -0400446 if (is_reserve || !is_pmem)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400447 return true;
448 if (!res || strcmp(res->name, label_id->id) == 0)
449 return true;
450 return false;
451}
452
453enum alloc_loc {
454 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
455};
456
457static resource_size_t scan_allocate(struct nd_region *nd_region,
458 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
459 resource_size_t n)
460{
461 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -0400462 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400463 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
464 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
465 const resource_size_t to_allocate = n;
466 struct resource *res;
467 int first;
468
469 retry:
470 first = 0;
471 for_each_dpa_resource(ndd, res) {
472 resource_size_t allocate, available = 0, free_start, free_end;
473 struct resource *next = res->sibling, *new_res = NULL;
474 enum alloc_loc loc = ALLOC_ERR;
475 const char *action;
476 int rc = 0;
477
478 /* ignore resources outside this nd_mapping */
479 if (res->start > mapping_end)
480 continue;
481 if (res->end < nd_mapping->start)
482 continue;
483
484 /* space at the beginning of the mapping */
485 if (!first++ && res->start > nd_mapping->start) {
486 free_start = nd_mapping->start;
487 available = res->start - free_start;
Dan Williams1b40e092015-05-01 13:34:01 -0400488 if (space_valid(is_pmem, is_reserve, label_id, NULL))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400489 loc = ALLOC_BEFORE;
490 }
491
492 /* space between allocations */
493 if (!loc && next) {
494 free_start = res->start + resource_size(res);
495 free_end = min(mapping_end, next->start - 1);
Dan Williams1b40e092015-05-01 13:34:01 -0400496 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400497 && free_start < free_end) {
498 available = free_end + 1 - free_start;
499 loc = ALLOC_MID;
500 }
501 }
502
503 /* space at the end of the mapping */
504 if (!loc && !next) {
505 free_start = res->start + resource_size(res);
506 free_end = mapping_end;
Dan Williams1b40e092015-05-01 13:34:01 -0400507 if (space_valid(is_pmem, is_reserve, label_id, res)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400508 && free_start < free_end) {
509 available = free_end + 1 - free_start;
510 loc = ALLOC_AFTER;
511 }
512 }
513
514 if (!loc || !available)
515 continue;
516 allocate = min(available, n);
517 switch (loc) {
518 case ALLOC_BEFORE:
519 if (strcmp(res->name, label_id->id) == 0) {
520 /* adjust current resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400521 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400522 return n;
523 rc = adjust_resource(res, res->start - allocate,
524 resource_size(res) + allocate);
525 action = "cur grow up";
526 } else
527 action = "allocate";
528 break;
529 case ALLOC_MID:
530 if (strcmp(next->name, label_id->id) == 0) {
531 /* adjust next resource up */
Dan Williams1b40e092015-05-01 13:34:01 -0400532 if (is_pmem && !is_reserve)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400533 return n;
534 rc = adjust_resource(next, next->start
535 - allocate, resource_size(next)
536 + allocate);
537 new_res = next;
538 action = "next grow up";
539 } else if (strcmp(res->name, label_id->id) == 0) {
540 action = "grow down";
541 } else
542 action = "allocate";
543 break;
544 case ALLOC_AFTER:
545 if (strcmp(res->name, label_id->id) == 0)
546 action = "grow down";
547 else
548 action = "allocate";
549 break;
550 default:
551 return n;
552 }
553
554 if (strcmp(action, "allocate") == 0) {
555 /* BLK allocate bottom up */
556 if (!is_pmem)
557 free_start += available - allocate;
Dan Williams1b40e092015-05-01 13:34:01 -0400558 else if (!is_reserve && free_start != nd_mapping->start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400559 return n;
560
561 new_res = nvdimm_allocate_dpa(ndd, label_id,
562 free_start, allocate);
563 if (!new_res)
564 rc = -EBUSY;
565 } else if (strcmp(action, "grow down") == 0) {
566 /* adjust current resource down */
567 rc = adjust_resource(res, res->start, resource_size(res)
568 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400569 if (rc == 0)
570 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400571 }
572
573 if (!new_res)
574 new_res = res;
575
576 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
577 action, loc, rc);
578
579 if (rc)
580 return n;
581
582 n -= allocate;
583 if (n) {
584 /*
585 * Retry scan with newly inserted resources.
586 * For example, if we did an ALLOC_BEFORE
587 * insertion there may also have been space
588 * available for an ALLOC_AFTER insertion, so we
589 * need to check this same resource again
590 */
591 goto retry;
592 } else
593 return 0;
594 }
595
Dan Williams1b40e092015-05-01 13:34:01 -0400596 /*
597 * If we allocated nothing in the BLK case it may be because we are in
598 * an initial "pmem-reserve pass". Only do an initial BLK allocation
599 * when none of the DPA space is reserved.
600 */
601 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400602 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
603 return n;
604}
605
Dan Williams1b40e092015-05-01 13:34:01 -0400606static int merge_dpa(struct nd_region *nd_region,
607 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
608{
609 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
610 struct resource *res;
611
612 if (strncmp("pmem", label_id->id, 4) == 0)
613 return 0;
614 retry:
615 for_each_dpa_resource(ndd, res) {
616 int rc;
617 struct resource *next = res->sibling;
618 resource_size_t end = res->start + resource_size(res);
619
620 if (!next || strcmp(res->name, label_id->id) != 0
621 || strcmp(next->name, label_id->id) != 0
622 || end != next->start)
623 continue;
624 end += resource_size(next);
625 nvdimm_free_dpa(ndd, next);
626 rc = adjust_resource(res, res->start, end - res->start);
627 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
628 if (rc)
629 return rc;
630 res->flags |= DPA_RESOURCE_ADJUSTED;
631 goto retry;
632 }
633
634 return 0;
635}
636
637static int __reserve_free_pmem(struct device *dev, void *data)
638{
639 struct nvdimm *nvdimm = data;
640 struct nd_region *nd_region;
641 struct nd_label_id label_id;
642 int i;
643
644 if (!is_nd_pmem(dev))
645 return 0;
646
647 nd_region = to_nd_region(dev);
648 if (nd_region->ndr_mappings == 0)
649 return 0;
650
651 memset(&label_id, 0, sizeof(label_id));
652 strcat(label_id.id, "pmem-reserve");
653 for (i = 0; i < nd_region->ndr_mappings; i++) {
654 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
655 resource_size_t n, rem = 0;
656
657 if (nd_mapping->nvdimm != nvdimm)
658 continue;
659
660 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
661 if (n == 0)
662 return 0;
663 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
664 dev_WARN_ONCE(&nd_region->dev, rem,
665 "pmem reserve underrun: %#llx of %#llx bytes\n",
666 (unsigned long long) n - rem,
667 (unsigned long long) n);
668 return rem ? -ENXIO : 0;
669 }
670
671 return 0;
672}
673
674static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
675 struct nd_mapping *nd_mapping)
676{
677 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
678 struct resource *res, *_res;
679
680 for_each_dpa_resource_safe(ndd, res, _res)
681 if (strcmp(res->name, "pmem-reserve") == 0)
682 nvdimm_free_dpa(ndd, res);
683}
684
685static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
686 struct nd_mapping *nd_mapping)
687{
688 struct nvdimm *nvdimm = nd_mapping->nvdimm;
689 int rc;
690
691 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
692 __reserve_free_pmem);
693 if (rc)
694 release_free_pmem(nvdimm_bus, nd_mapping);
695 return rc;
696}
697
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400698/**
699 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
700 * @nd_region: the set of dimms to allocate @n more bytes from
701 * @label_id: unique identifier for the namespace consuming this dpa range
702 * @n: number of bytes per-dimm to add to the existing allocation
703 *
704 * Assumes resources are ordered. For BLK regions, first consume
705 * BLK-only available DPA free space, then consume PMEM-aliased DPA
706 * space starting at the highest DPA. For PMEM regions start
707 * allocations from the start of an interleave set and end at the first
708 * BLK allocation or the end of the interleave set, whichever comes
709 * first.
710 */
711static int grow_dpa_allocation(struct nd_region *nd_region,
712 struct nd_label_id *label_id, resource_size_t n)
713{
Dan Williams1b40e092015-05-01 13:34:01 -0400714 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
715 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400716 int i;
717
718 for (i = 0; i < nd_region->ndr_mappings; i++) {
719 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400720 resource_size_t rem = n;
721 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400722
Dan Williams1b40e092015-05-01 13:34:01 -0400723 /*
724 * In the BLK case try once with all unallocated PMEM
725 * reserved, and once without
726 */
727 for (j = is_pmem; j < 2; j++) {
728 bool blk_only = j == 0;
729
730 if (blk_only) {
731 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
732 if (rc)
733 return rc;
734 }
735 rem = scan_allocate(nd_region, nd_mapping,
736 label_id, rem);
737 if (blk_only)
738 release_free_pmem(nvdimm_bus, nd_mapping);
739
740 /* try again and allow encroachments into PMEM */
741 if (rem == 0)
742 break;
743 }
744
745 dev_WARN_ONCE(&nd_region->dev, rem,
746 "allocation underrun: %#llx of %#llx bytes\n",
747 (unsigned long long) n - rem,
748 (unsigned long long) n);
749 if (rem)
750 return -ENXIO;
751
752 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400753 if (rc)
754 return rc;
755 }
756
757 return 0;
758}
759
760static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
761 struct nd_namespace_pmem *nspm, resource_size_t size)
762{
763 struct resource *res = &nspm->nsio.res;
764
765 res->start = nd_region->ndr_start;
766 res->end = nd_region->ndr_start + size - 1;
767}
768
769static ssize_t __size_store(struct device *dev, unsigned long long val)
770{
771 resource_size_t allocated = 0, available = 0;
772 struct nd_region *nd_region = to_nd_region(dev->parent);
773 struct nd_mapping *nd_mapping;
774 struct nvdimm_drvdata *ndd;
775 struct nd_label_id label_id;
776 u32 flags = 0, remainder;
777 u8 *uuid = NULL;
778 int rc, i;
779
Dan Williams8c2f7e82015-06-25 04:20:04 -0400780 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400781 return -EBUSY;
782
783 if (is_namespace_pmem(dev)) {
784 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
785
786 uuid = nspm->uuid;
787 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400788 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
789
790 uuid = nsblk->uuid;
791 flags = NSLABEL_FLAG_LOCAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400792 }
793
794 /*
795 * We need a uuid for the allocation-label and dimm(s) on which
796 * to store the label.
797 */
798 if (!uuid || nd_region->ndr_mappings == 0)
799 return -ENXIO;
800
801 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
802 if (remainder) {
803 dev_dbg(dev, "%llu is not %dK aligned\n", val,
804 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
805 return -EINVAL;
806 }
807
808 nd_label_gen_id(&label_id, uuid, flags);
809 for (i = 0; i < nd_region->ndr_mappings; i++) {
810 nd_mapping = &nd_region->mapping[i];
811 ndd = to_ndd(nd_mapping);
812
813 /*
814 * All dimms in an interleave set, or the base dimm for a blk
815 * region, need to be enabled for the size to be changed.
816 */
817 if (!ndd)
818 return -ENXIO;
819
820 allocated += nvdimm_allocated_dpa(ndd, &label_id);
821 }
822 available = nd_region_available_dpa(nd_region);
823
824 if (val > available + allocated)
825 return -ENOSPC;
826
827 if (val == allocated)
828 return 0;
829
830 val = div_u64(val, nd_region->ndr_mappings);
831 allocated = div_u64(allocated, nd_region->ndr_mappings);
832 if (val < allocated)
833 rc = shrink_dpa_allocation(nd_region, &label_id,
834 allocated - val);
835 else
836 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
837
838 if (rc)
839 return rc;
840
841 if (is_namespace_pmem(dev)) {
842 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
843
844 nd_namespace_pmem_set_size(nd_region, nspm,
845 val * nd_region->ndr_mappings);
Dan Williams1b40e092015-05-01 13:34:01 -0400846 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400847 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
848
Dan Williams1b40e092015-05-01 13:34:01 -0400849 /*
850 * Try to delete the namespace if we deleted all of its
Dan Williams8c2f7e82015-06-25 04:20:04 -0400851 * allocation, this is not the seed device for the
852 * region, and it is not actively claimed by a btt
853 * instance.
Dan Williams1b40e092015-05-01 13:34:01 -0400854 */
Dan Williams8c2f7e82015-06-25 04:20:04 -0400855 if (val == 0 && nd_region->ns_seed != dev
856 && !nsblk->common.claim)
Dan Williams1b40e092015-05-01 13:34:01 -0400857 nd_device_unregister(dev, ND_ASYNC);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400858 }
859
860 return rc;
861}
862
863static ssize_t size_store(struct device *dev,
864 struct device_attribute *attr, const char *buf, size_t len)
865{
Dan Williamsf524bf22015-05-30 12:36:02 -0400866 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400867 unsigned long long val;
868 u8 **uuid = NULL;
869 int rc;
870
871 rc = kstrtoull(buf, 0, &val);
872 if (rc)
873 return rc;
874
875 device_lock(dev);
876 nvdimm_bus_lock(dev);
877 wait_nvdimm_bus_probe_idle(dev);
878 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -0400879 if (rc >= 0)
880 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400881
882 if (is_namespace_pmem(dev)) {
883 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
884
885 uuid = &nspm->uuid;
886 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400887 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
888
889 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400890 }
891
892 if (rc == 0 && val == 0 && uuid) {
893 /* setting size zero == 'delete namespace' */
894 kfree(*uuid);
895 *uuid = NULL;
896 }
897
898 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
899 ? "fail" : "success", rc);
900
901 nvdimm_bus_unlock(dev);
902 device_unlock(dev);
903
Dan Williamsf524bf22015-05-30 12:36:02 -0400904 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400905}
906
Dan Williams8c2f7e82015-06-25 04:20:04 -0400907resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400908{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400909 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -0400910
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400911 if (is_namespace_pmem(dev)) {
912 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
913
Dan Williams8c2f7e82015-06-25 04:20:04 -0400914 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400915 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400916 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400917 } else if (is_namespace_io(dev)) {
918 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
919
Dan Williams8c2f7e82015-06-25 04:20:04 -0400920 return resource_size(&nsio->res);
921 } else
922 WARN_ONCE(1, "unknown namespace type\n");
923 return 0;
924}
Dan Williams1b40e092015-05-01 13:34:01 -0400925
Dan Williams8c2f7e82015-06-25 04:20:04 -0400926resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
927{
928 resource_size_t size;
929
930 nvdimm_bus_lock(&ndns->dev);
931 size = __nvdimm_namespace_capacity(ndns);
932 nvdimm_bus_unlock(&ndns->dev);
933
934 return size;
935}
936EXPORT_SYMBOL(nvdimm_namespace_capacity);
937
938static ssize_t size_show(struct device *dev,
939 struct device_attribute *attr, char *buf)
940{
941 return sprintf(buf, "%llu\n", (unsigned long long)
942 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400943}
944static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
945
946static ssize_t uuid_show(struct device *dev,
947 struct device_attribute *attr, char *buf)
948{
949 u8 *uuid;
950
951 if (is_namespace_pmem(dev)) {
952 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
953
954 uuid = nspm->uuid;
955 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400956 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
957
958 uuid = nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400959 } else
960 return -ENXIO;
961
962 if (uuid)
963 return sprintf(buf, "%pUb\n", uuid);
964 return sprintf(buf, "\n");
965}
966
967/**
968 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
969 * @nd_region: parent region so we can updates all dimms in the set
970 * @dev: namespace type for generating label_id
971 * @new_uuid: incoming uuid
972 * @old_uuid: reference to the uuid storage location in the namespace object
973 */
974static int namespace_update_uuid(struct nd_region *nd_region,
975 struct device *dev, u8 *new_uuid, u8 **old_uuid)
976{
977 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
978 struct nd_label_id old_label_id;
979 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -0400980 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400981
Dan Williamsf524bf22015-05-30 12:36:02 -0400982 if (!nd_is_uuid_unique(dev, new_uuid))
983 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400984
985 if (*old_uuid == NULL)
986 goto out;
987
Dan Williamsf524bf22015-05-30 12:36:02 -0400988 /*
989 * If we've already written a label with this uuid, then it's
990 * too late to rename because we can't reliably update the uuid
991 * without losing the old namespace. Userspace must delete this
992 * namespace to abandon the old uuid.
993 */
994 for (i = 0; i < nd_region->ndr_mappings; i++) {
995 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
996
997 /*
998 * This check by itself is sufficient because old_uuid
999 * would be NULL above if this uuid did not exist in the
1000 * currently written set.
1001 *
1002 * FIXME: can we delete uuid with zero dpa allocated?
1003 */
1004 if (nd_mapping->labels)
1005 return -EBUSY;
1006 }
1007
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001008 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1009 nd_label_gen_id(&new_label_id, new_uuid, flags);
1010 for (i = 0; i < nd_region->ndr_mappings; i++) {
1011 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1012 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1013 struct resource *res;
1014
1015 for_each_dpa_resource(ndd, res)
1016 if (strcmp(res->name, old_label_id.id) == 0)
1017 sprintf((void *) res->name, "%s",
1018 new_label_id.id);
1019 }
1020 kfree(*old_uuid);
1021 out:
1022 *old_uuid = new_uuid;
1023 return 0;
1024}
1025
1026static ssize_t uuid_store(struct device *dev,
1027 struct device_attribute *attr, const char *buf, size_t len)
1028{
1029 struct nd_region *nd_region = to_nd_region(dev->parent);
1030 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001031 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001032 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001033
1034 if (is_namespace_pmem(dev)) {
1035 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1036
1037 ns_uuid = &nspm->uuid;
1038 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001039 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1040
1041 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001042 } else
1043 return -ENXIO;
1044
1045 device_lock(dev);
1046 nvdimm_bus_lock(dev);
1047 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001048 if (to_ndns(dev)->claim)
1049 rc = -EBUSY;
1050 if (rc >= 0)
1051 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001052 if (rc >= 0)
1053 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001054 if (rc >= 0)
1055 rc = nd_namespace_label_update(nd_region, dev);
1056 else
1057 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001058 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1059 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1060 nvdimm_bus_unlock(dev);
1061 device_unlock(dev);
1062
Dan Williamsf524bf22015-05-30 12:36:02 -04001063 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001064}
1065static DEVICE_ATTR_RW(uuid);
1066
1067static ssize_t resource_show(struct device *dev,
1068 struct device_attribute *attr, char *buf)
1069{
1070 struct resource *res;
1071
1072 if (is_namespace_pmem(dev)) {
1073 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1074
1075 res = &nspm->nsio.res;
1076 } else if (is_namespace_io(dev)) {
1077 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1078
1079 res = &nsio->res;
1080 } else
1081 return -ENXIO;
1082
1083 /* no address to convey if the namespace has no allocation */
1084 if (resource_size(res) == 0)
1085 return -ENXIO;
1086 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1087}
1088static DEVICE_ATTR_RO(resource);
1089
Vishal Vermafcae6952015-06-25 04:22:39 -04001090static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1091 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001092
1093static ssize_t sector_size_show(struct device *dev,
1094 struct device_attribute *attr, char *buf)
1095{
1096 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1097
1098 if (!is_namespace_blk(dev))
1099 return -ENXIO;
1100
1101 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1102}
1103
1104static ssize_t sector_size_store(struct device *dev,
1105 struct device_attribute *attr, const char *buf, size_t len)
1106{
1107 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williamsf524bf22015-05-30 12:36:02 -04001108 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001109 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001110
1111 if (!is_namespace_blk(dev))
1112 return -ENXIO;
1113
1114 device_lock(dev);
1115 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001116 if (to_ndns(dev)->claim)
1117 rc = -EBUSY;
1118 if (rc >= 0)
1119 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1120 ns_lbasize_supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001121 if (rc >= 0)
1122 rc = nd_namespace_label_update(nd_region, dev);
1123 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1124 rc, rc < 0 ? "tried" : "wrote", buf,
1125 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001126 nvdimm_bus_unlock(dev);
1127 device_unlock(dev);
1128
1129 return rc ? rc : len;
1130}
1131static DEVICE_ATTR_RW(sector_size);
1132
Dan Williams0ba1c632015-05-30 12:35:36 -04001133static ssize_t dpa_extents_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
1135{
1136 struct nd_region *nd_region = to_nd_region(dev->parent);
1137 struct nd_label_id label_id;
1138 int count = 0, i;
1139 u8 *uuid = NULL;
1140 u32 flags = 0;
1141
1142 nvdimm_bus_lock(dev);
1143 if (is_namespace_pmem(dev)) {
1144 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1145
1146 uuid = nspm->uuid;
1147 flags = 0;
1148 } else if (is_namespace_blk(dev)) {
1149 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1150
1151 uuid = nsblk->uuid;
1152 flags = NSLABEL_FLAG_LOCAL;
1153 }
1154
1155 if (!uuid)
1156 goto out;
1157
1158 nd_label_gen_id(&label_id, uuid, flags);
1159 for (i = 0; i < nd_region->ndr_mappings; i++) {
1160 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1161 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1162 struct resource *res;
1163
1164 for_each_dpa_resource(ndd, res)
1165 if (strcmp(res->name, label_id.id) == 0)
1166 count++;
1167 }
1168 out:
1169 nvdimm_bus_unlock(dev);
1170
1171 return sprintf(buf, "%d\n", count);
1172}
1173static DEVICE_ATTR_RO(dpa_extents);
1174
Dan Williams8c2f7e82015-06-25 04:20:04 -04001175static ssize_t holder_show(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1177{
1178 struct nd_namespace_common *ndns = to_ndns(dev);
1179 ssize_t rc;
1180
1181 device_lock(dev);
1182 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1183 device_unlock(dev);
1184
1185 return rc;
1186}
1187static DEVICE_ATTR_RO(holder);
1188
1189static ssize_t force_raw_store(struct device *dev,
1190 struct device_attribute *attr, const char *buf, size_t len)
1191{
1192 bool force_raw;
1193 int rc = strtobool(buf, &force_raw);
1194
1195 if (rc)
1196 return rc;
1197
1198 to_ndns(dev)->force_raw = force_raw;
1199 return len;
1200}
1201
1202static ssize_t force_raw_show(struct device *dev,
1203 struct device_attribute *attr, char *buf)
1204{
1205 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1206}
1207static DEVICE_ATTR_RW(force_raw);
1208
Dan Williams3d880022015-05-31 15:02:11 -04001209static struct attribute *nd_namespace_attributes[] = {
1210 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001211 &dev_attr_size.attr,
1212 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001213 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001214 &dev_attr_resource.attr,
1215 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001216 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001217 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001218 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001219 NULL,
1220};
1221
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001222static umode_t namespace_visible(struct kobject *kobj,
1223 struct attribute *a, int n)
1224{
1225 struct device *dev = container_of(kobj, struct device, kobj);
1226
1227 if (a == &dev_attr_resource.attr) {
1228 if (is_namespace_blk(dev))
1229 return 0;
1230 return a->mode;
1231 }
1232
1233 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1234 if (a == &dev_attr_size.attr)
1235 return S_IWUSR | S_IRUGO;
Dan Williams1b40e092015-05-01 13:34:01 -04001236
1237 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1238 return 0;
1239
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001240 return a->mode;
1241 }
1242
Dan Williams8c2f7e82015-06-25 04:20:04 -04001243 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1244 || a == &dev_attr_holder.attr
1245 || a == &dev_attr_force_raw.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001246 return a->mode;
1247
1248 return 0;
1249}
1250
Dan Williams3d880022015-05-31 15:02:11 -04001251static struct attribute_group nd_namespace_attribute_group = {
1252 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001253 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001254};
1255
1256static const struct attribute_group *nd_namespace_attribute_groups[] = {
1257 &nd_device_attribute_group,
1258 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001259 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001260 NULL,
1261};
1262
Dan Williams8c2f7e82015-06-25 04:20:04 -04001263struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1264{
1265 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001266 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001267 struct nd_namespace_common *ndns;
1268 resource_size_t size;
1269
Dan Williamse1455742015-07-30 17:57:47 -04001270 if (nd_btt || nd_pfn) {
1271 struct device *host = NULL;
1272
1273 if (nd_btt) {
1274 host = &nd_btt->dev;
1275 ndns = nd_btt->ndns;
1276 } else if (nd_pfn) {
1277 host = &nd_pfn->dev;
1278 ndns = nd_pfn->ndns;
1279 }
1280
1281 if (!ndns || !host)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001282 return ERR_PTR(-ENODEV);
1283
1284 /*
1285 * Flush any in-progess probes / removals in the driver
1286 * for the raw personality of this namespace.
1287 */
1288 device_lock(&ndns->dev);
1289 device_unlock(&ndns->dev);
1290 if (ndns->dev.driver) {
1291 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williamse1455742015-07-30 17:57:47 -04001292 dev_name(host));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001293 return ERR_PTR(-EBUSY);
1294 }
Dan Williamse1455742015-07-30 17:57:47 -04001295 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != host,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001296 "host (%s) vs claim (%s) mismatch\n",
Dan Williamse1455742015-07-30 17:57:47 -04001297 dev_name(host),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001298 dev_name(ndns->claim)))
1299 return ERR_PTR(-ENXIO);
1300 } else {
1301 ndns = to_ndns(dev);
1302 if (ndns->claim) {
1303 dev_dbg(dev, "claimed by %s, failing probe\n",
1304 dev_name(ndns->claim));
1305
1306 return ERR_PTR(-ENXIO);
1307 }
1308 }
1309
1310 size = nvdimm_namespace_capacity(ndns);
1311 if (size < ND_MIN_NAMESPACE_SIZE) {
1312 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1313 &size, ND_MIN_NAMESPACE_SIZE);
1314 return ERR_PTR(-ENODEV);
1315 }
1316
1317 if (is_namespace_pmem(&ndns->dev)) {
1318 struct nd_namespace_pmem *nspm;
1319
1320 nspm = to_nd_namespace_pmem(&ndns->dev);
1321 if (!nspm->uuid) {
1322 dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__);
1323 return ERR_PTR(-ENODEV);
1324 }
1325 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001326 struct nd_namespace_blk *nsblk;
1327
1328 nsblk = to_nd_namespace_blk(&ndns->dev);
1329 if (!nd_namespace_blk_validate(nsblk))
1330 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001331 }
1332
1333 return ndns;
1334}
1335EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1336
Dan Williams3d880022015-05-31 15:02:11 -04001337static struct device **create_namespace_io(struct nd_region *nd_region)
1338{
1339 struct nd_namespace_io *nsio;
1340 struct device *dev, **devs;
1341 struct resource *res;
1342
1343 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1344 if (!nsio)
1345 return NULL;
1346
1347 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1348 if (!devs) {
1349 kfree(nsio);
1350 return NULL;
1351 }
1352
Dan Williams8c2f7e82015-06-25 04:20:04 -04001353 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001354 dev->type = &namespace_io_device_type;
1355 dev->parent = &nd_region->dev;
1356 res = &nsio->res;
1357 res->name = dev_name(&nd_region->dev);
1358 res->flags = IORESOURCE_MEM;
1359 res->start = nd_region->ndr_start;
1360 res->end = res->start + nd_region->ndr_size - 1;
1361
1362 devs[0] = dev;
1363 return devs;
1364}
1365
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001366static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1367 u64 cookie, u16 pos)
1368{
1369 struct nd_namespace_label *found = NULL;
1370 int i;
1371
1372 for (i = 0; i < nd_region->ndr_mappings; i++) {
1373 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1374 struct nd_namespace_label *nd_label;
1375 bool found_uuid = false;
1376 int l;
1377
1378 for_each_label(l, nd_label, nd_mapping->labels) {
1379 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1380 u16 position = __le16_to_cpu(nd_label->position);
1381 u16 nlabel = __le16_to_cpu(nd_label->nlabel);
1382
1383 if (isetcookie != cookie)
1384 continue;
1385
1386 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1387 continue;
1388
1389 if (found_uuid) {
1390 dev_dbg(to_ndd(nd_mapping)->dev,
1391 "%s duplicate entry for uuid\n",
1392 __func__);
1393 return false;
1394 }
1395 found_uuid = true;
1396 if (nlabel != nd_region->ndr_mappings)
1397 continue;
1398 if (position != pos)
1399 continue;
1400 found = nd_label;
1401 break;
1402 }
1403 if (found)
1404 break;
1405 }
1406 return found != NULL;
1407}
1408
1409static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1410{
1411 struct nd_namespace_label *select = NULL;
1412 int i;
1413
1414 if (!pmem_id)
1415 return -ENODEV;
1416
1417 for (i = 0; i < nd_region->ndr_mappings; i++) {
1418 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1419 struct nd_namespace_label *nd_label;
1420 u64 hw_start, hw_end, pmem_start, pmem_end;
1421 int l;
1422
1423 for_each_label(l, nd_label, nd_mapping->labels)
1424 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1425 break;
1426
1427 if (!nd_label) {
1428 WARN_ON(1);
1429 return -EINVAL;
1430 }
1431
1432 select = nd_label;
1433 /*
1434 * Check that this label is compliant with the dpa
1435 * range published in NFIT
1436 */
1437 hw_start = nd_mapping->start;
1438 hw_end = hw_start + nd_mapping->size;
1439 pmem_start = __le64_to_cpu(select->dpa);
1440 pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
1441 if (pmem_start == hw_start && pmem_end <= hw_end)
1442 /* pass */;
1443 else
1444 return -EINVAL;
1445
1446 nd_mapping->labels[0] = select;
1447 nd_mapping->labels[1] = NULL;
1448 }
1449 return 0;
1450}
1451
1452/**
1453 * find_pmem_label_set - validate interleave set labelling, retrieve label0
1454 * @nd_region: region with mappings to validate
1455 */
1456static int find_pmem_label_set(struct nd_region *nd_region,
1457 struct nd_namespace_pmem *nspm)
1458{
1459 u64 cookie = nd_region_interleave_set_cookie(nd_region);
1460 struct nd_namespace_label *nd_label;
1461 u8 select_id[NSLABEL_UUID_LEN];
1462 resource_size_t size = 0;
1463 u8 *pmem_id = NULL;
1464 int rc = -ENODEV, l;
1465 u16 i;
1466
1467 if (cookie == 0)
1468 return -ENXIO;
1469
1470 /*
1471 * Find a complete set of labels by uuid. By definition we can start
1472 * with any mapping as the reference label
1473 */
1474 for_each_label(l, nd_label, nd_region->mapping[0].labels) {
1475 u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1476
1477 if (isetcookie != cookie)
1478 continue;
1479
1480 for (i = 0; nd_region->ndr_mappings; i++)
1481 if (!has_uuid_at_pos(nd_region, nd_label->uuid,
1482 cookie, i))
1483 break;
1484 if (i < nd_region->ndr_mappings) {
1485 /*
1486 * Give up if we don't find an instance of a
1487 * uuid at each position (from 0 to
1488 * nd_region->ndr_mappings - 1), or if we find a
1489 * dimm with two instances of the same uuid.
1490 */
1491 rc = -EINVAL;
1492 goto err;
1493 } else if (pmem_id) {
1494 /*
1495 * If there is more than one valid uuid set, we
1496 * need userspace to clean this up.
1497 */
1498 rc = -EBUSY;
1499 goto err;
1500 }
1501 memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
1502 pmem_id = select_id;
1503 }
1504
1505 /*
1506 * Fix up each mapping's 'labels' to have the validated pmem label for
1507 * that position at labels[0], and NULL at labels[1]. In the process,
1508 * check that the namespace aligns with interleave-set. We know
1509 * that it does not overlap with any blk namespaces by virtue of
1510 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1511 * succeeded).
1512 */
1513 rc = select_pmem_id(nd_region, pmem_id);
1514 if (rc)
1515 goto err;
1516
1517 /* Calculate total size and populate namespace properties from label0 */
1518 for (i = 0; i < nd_region->ndr_mappings; i++) {
1519 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1520 struct nd_namespace_label *label0 = nd_mapping->labels[0];
1521
1522 size += __le64_to_cpu(label0->rawsize);
1523 if (__le16_to_cpu(label0->position) != 0)
1524 continue;
1525 WARN_ON(nspm->alt_name || nspm->uuid);
1526 nspm->alt_name = kmemdup((void __force *) label0->name,
1527 NSLABEL_NAME_LEN, GFP_KERNEL);
1528 nspm->uuid = kmemdup((void __force *) label0->uuid,
1529 NSLABEL_UUID_LEN, GFP_KERNEL);
1530 }
1531
1532 if (!nspm->alt_name || !nspm->uuid) {
1533 rc = -ENOMEM;
1534 goto err;
1535 }
1536
1537 nd_namespace_pmem_set_size(nd_region, nspm, size);
1538
1539 return 0;
1540 err:
1541 switch (rc) {
1542 case -EINVAL:
1543 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1544 break;
1545 case -ENODEV:
1546 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1547 break;
1548 default:
1549 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1550 __func__, rc);
1551 break;
1552 }
1553 return rc;
1554}
1555
1556static struct device **create_namespace_pmem(struct nd_region *nd_region)
1557{
1558 struct nd_namespace_pmem *nspm;
1559 struct device *dev, **devs;
1560 struct resource *res;
1561 int rc;
1562
1563 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1564 if (!nspm)
1565 return NULL;
1566
Dan Williams8c2f7e82015-06-25 04:20:04 -04001567 dev = &nspm->nsio.common.dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001568 dev->type = &namespace_pmem_device_type;
1569 dev->parent = &nd_region->dev;
1570 res = &nspm->nsio.res;
1571 res->name = dev_name(&nd_region->dev);
1572 res->flags = IORESOURCE_MEM;
1573 rc = find_pmem_label_set(nd_region, nspm);
1574 if (rc == -ENODEV) {
1575 int i;
1576
1577 /* Pass, try to permit namespace creation... */
1578 for (i = 0; i < nd_region->ndr_mappings; i++) {
1579 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1580
1581 kfree(nd_mapping->labels);
1582 nd_mapping->labels = NULL;
1583 }
1584
1585 /* Publish a zero-sized namespace for userspace to configure. */
1586 nd_namespace_pmem_set_size(nd_region, nspm, 0);
1587
1588 rc = 0;
1589 } else if (rc)
1590 goto err;
1591
1592 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1593 if (!devs)
1594 goto err;
1595
1596 devs[0] = dev;
1597 return devs;
1598
1599 err:
Dan Williams8c2f7e82015-06-25 04:20:04 -04001600 namespace_pmem_release(&nspm->nsio.common.dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001601 return NULL;
1602}
1603
Dan Williams1b40e092015-05-01 13:34:01 -04001604struct resource *nsblk_add_resource(struct nd_region *nd_region,
1605 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1606 resource_size_t start)
1607{
1608 struct nd_label_id label_id;
1609 struct resource *res;
1610
1611 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1612 res = krealloc(nsblk->res,
1613 sizeof(void *) * (nsblk->num_resources + 1),
1614 GFP_KERNEL);
1615 if (!res)
1616 return NULL;
1617 nsblk->res = (struct resource **) res;
1618 for_each_dpa_resource(ndd, res)
1619 if (strcmp(res->name, label_id.id) == 0
1620 && res->start == start) {
1621 nsblk->res[nsblk->num_resources++] = res;
1622 return res;
1623 }
1624 return NULL;
1625}
1626
1627static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1628{
1629 struct nd_namespace_blk *nsblk;
1630 struct device *dev;
1631
1632 if (!is_nd_blk(&nd_region->dev))
1633 return NULL;
1634
1635 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1636 if (!nsblk)
1637 return NULL;
1638
Dan Williams8c2f7e82015-06-25 04:20:04 -04001639 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001640 dev->type = &namespace_blk_device_type;
1641 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1642 if (nsblk->id < 0) {
1643 kfree(nsblk);
1644 return NULL;
1645 }
1646 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1647 dev->parent = &nd_region->dev;
1648 dev->groups = nd_namespace_attribute_groups;
1649
Dan Williams8c2f7e82015-06-25 04:20:04 -04001650 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001651}
1652
1653void nd_region_create_blk_seed(struct nd_region *nd_region)
1654{
1655 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1656 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1657 /*
1658 * Seed creation failures are not fatal, provisioning is simply
1659 * disabled until memory becomes available
1660 */
1661 if (!nd_region->ns_seed)
1662 dev_err(&nd_region->dev, "failed to create blk namespace\n");
1663 else
1664 nd_device_register(nd_region->ns_seed);
1665}
1666
Dan Williams8c2f7e82015-06-25 04:20:04 -04001667void nd_region_create_btt_seed(struct nd_region *nd_region)
1668{
1669 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1670 nd_region->btt_seed = nd_btt_create(nd_region);
1671 /*
1672 * Seed creation failures are not fatal, provisioning is simply
1673 * disabled until memory becomes available
1674 */
1675 if (!nd_region->btt_seed)
1676 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1677}
1678
Dan Williams1b40e092015-05-01 13:34:01 -04001679static struct device **create_namespace_blk(struct nd_region *nd_region)
1680{
1681 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1682 struct nd_namespace_label *nd_label;
1683 struct device *dev, **devs = NULL;
1684 struct nd_namespace_blk *nsblk;
1685 struct nvdimm_drvdata *ndd;
1686 int i, l, count = 0;
1687 struct resource *res;
1688
1689 if (nd_region->ndr_mappings == 0)
1690 return NULL;
1691
1692 ndd = to_ndd(nd_mapping);
1693 for_each_label(l, nd_label, nd_mapping->labels) {
1694 u32 flags = __le32_to_cpu(nd_label->flags);
1695 char *name[NSLABEL_NAME_LEN];
1696 struct device **__devs;
1697
1698 if (flags & NSLABEL_FLAG_LOCAL)
1699 /* pass */;
1700 else
1701 continue;
1702
1703 for (i = 0; i < count; i++) {
1704 nsblk = to_nd_namespace_blk(devs[i]);
1705 if (memcmp(nsblk->uuid, nd_label->uuid,
1706 NSLABEL_UUID_LEN) == 0) {
1707 res = nsblk_add_resource(nd_region, ndd, nsblk,
1708 __le64_to_cpu(nd_label->dpa));
1709 if (!res)
1710 goto err;
1711 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001712 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001713 break;
1714 }
1715 }
1716 if (i < count)
1717 continue;
1718 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
1719 if (!__devs)
1720 goto err;
1721 memcpy(__devs, devs, sizeof(dev) * count);
1722 kfree(devs);
1723 devs = __devs;
1724
1725 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1726 if (!nsblk)
1727 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001728 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001729 dev->type = &namespace_blk_device_type;
1730 dev->parent = &nd_region->dev;
1731 dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
1732 devs[count++] = dev;
1733 nsblk->id = -1;
1734 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
1735 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
1736 GFP_KERNEL);
1737 if (!nsblk->uuid)
1738 goto err;
1739 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
1740 if (name[0])
1741 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
1742 GFP_KERNEL);
1743 res = nsblk_add_resource(nd_region, ndd, nsblk,
1744 __le64_to_cpu(nd_label->dpa));
1745 if (!res)
1746 goto err;
1747 nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
Dan Williams8c2f7e82015-06-25 04:20:04 -04001748 dev_name(&nsblk->common.dev));
Dan Williams1b40e092015-05-01 13:34:01 -04001749 }
1750
1751 dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
1752 __func__, count, count == 1 ? "" : "s");
1753
1754 if (count == 0) {
1755 /* Publish a zero-sized namespace for userspace to configure. */
1756 for (i = 0; i < nd_region->ndr_mappings; i++) {
1757 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1758
1759 kfree(nd_mapping->labels);
1760 nd_mapping->labels = NULL;
1761 }
1762
1763 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
1764 if (!devs)
1765 goto err;
1766 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1767 if (!nsblk)
1768 goto err;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001769 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001770 dev->type = &namespace_blk_device_type;
1771 dev->parent = &nd_region->dev;
1772 devs[count++] = dev;
1773 }
1774
1775 return devs;
1776
1777err:
1778 for (i = 0; i < count; i++) {
1779 nsblk = to_nd_namespace_blk(devs[i]);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001780 namespace_blk_release(&nsblk->common.dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001781 }
1782 kfree(devs);
1783 return NULL;
1784}
1785
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001786static int init_active_labels(struct nd_region *nd_region)
1787{
1788 int i;
1789
1790 for (i = 0; i < nd_region->ndr_mappings; i++) {
1791 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1792 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1793 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1794 int count, j;
1795
1796 /*
1797 * If the dimm is disabled then prevent the region from
1798 * being activated if it aliases DPA.
1799 */
1800 if (!ndd) {
1801 if ((nvdimm->flags & NDD_ALIASING) == 0)
1802 return 0;
1803 dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
1804 dev_name(&nd_mapping->nvdimm->dev));
1805 return -ENXIO;
1806 }
1807 nd_mapping->ndd = ndd;
1808 atomic_inc(&nvdimm->busy);
1809 get_ndd(ndd);
1810
1811 count = nd_label_active_count(ndd);
1812 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
1813 if (!count)
1814 continue;
1815 nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
1816 GFP_KERNEL);
1817 if (!nd_mapping->labels)
1818 return -ENOMEM;
1819 for (j = 0; j < count; j++) {
1820 struct nd_namespace_label *label;
1821
1822 label = nd_label_active(ndd, j);
1823 nd_mapping->labels[j] = label;
1824 }
1825 }
1826
1827 return 0;
1828}
1829
Dan Williams3d880022015-05-31 15:02:11 -04001830int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
1831{
1832 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001833 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04001834
1835 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001836 nvdimm_bus_lock(&nd_region->dev);
1837 rc = init_active_labels(nd_region);
1838 if (rc) {
1839 nvdimm_bus_unlock(&nd_region->dev);
1840 return rc;
1841 }
1842
1843 type = nd_region_to_nstype(nd_region);
1844 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04001845 case ND_DEVICE_NAMESPACE_IO:
1846 devs = create_namespace_io(nd_region);
1847 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001848 case ND_DEVICE_NAMESPACE_PMEM:
1849 devs = create_namespace_pmem(nd_region);
1850 break;
Dan Williams1b40e092015-05-01 13:34:01 -04001851 case ND_DEVICE_NAMESPACE_BLK:
1852 devs = create_namespace_blk(nd_region);
1853 break;
Dan Williams3d880022015-05-31 15:02:11 -04001854 default:
1855 break;
1856 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001857 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04001858
1859 if (!devs)
1860 return -ENODEV;
1861
Dan Williams3d880022015-05-31 15:02:11 -04001862 for (i = 0; devs[i]; i++) {
1863 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04001864 int id;
Dan Williams3d880022015-05-31 15:02:11 -04001865
Dan Williams1b40e092015-05-01 13:34:01 -04001866 if (type == ND_DEVICE_NAMESPACE_BLK) {
1867 struct nd_namespace_blk *nsblk;
1868
1869 nsblk = to_nd_namespace_blk(dev);
1870 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
1871 GFP_KERNEL);
1872 nsblk->id = id;
1873 } else
1874 id = i;
1875
1876 if (id < 0)
1877 break;
1878 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04001879 dev->groups = nd_namespace_attribute_groups;
1880 nd_device_register(dev);
1881 }
Dan Williams1b40e092015-05-01 13:34:01 -04001882 if (i)
1883 nd_region->ns_seed = devs[0];
1884
1885 if (devs[i]) {
1886 int j;
1887
1888 for (j = i; devs[j]; j++) {
1889 struct device *dev = devs[j];
1890
1891 device_initialize(dev);
1892 put_device(dev);
1893 }
1894 *err = j - i;
1895 /*
1896 * All of the namespaces we tried to register failed, so
1897 * fail region activation.
1898 */
1899 if (*err == 0)
1900 rc = -ENODEV;
1901 }
Dan Williams3d880022015-05-31 15:02:11 -04001902 kfree(devs);
1903
Dan Williams1b40e092015-05-01 13:34:01 -04001904 if (rc == -ENODEV)
1905 return rc;
1906
Dan Williams3d880022015-05-31 15:02:11 -04001907 return i;
1908}