blob: b57a2d36c517a2cba62b8d0ba3d2e311d4ab1d59 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams3d880022015-05-31 15:02:11 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams3d880022015-05-31 15:02:11 -04004 */
5#include <linux/module.h>
6#include <linux/device.h>
Dan Williams6ff3e912016-10-05 14:04:15 -07007#include <linux/sort.h>
Dan Williams3d880022015-05-31 15:02:11 -04008#include <linux/slab.h>
Dan Williamsae8219f2016-09-19 16:04:21 -07009#include <linux/list.h>
Dan Williams3d880022015-05-31 15:02:11 -040010#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040011#include "nd-core.h"
Dan Williamsca6a4652017-01-13 20:36:58 -080012#include "pmem.h"
Dan Williams6acd7d52020-02-27 21:39:23 -080013#include "pfn.h"
Dan Williams3d880022015-05-31 15:02:11 -040014#include "nd.h"
15
16static void namespace_io_release(struct device *dev)
17{
18 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
19
20 kfree(nsio);
21}
22
Dan Williamsbf9bccc2015-06-17 17:14:46 -040023static void namespace_pmem_release(struct device *dev)
24{
25 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0e3b0d12016-10-06 23:13:15 -070026 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040027
Dan Williams0e3b0d12016-10-06 23:13:15 -070028 if (nspm->id >= 0)
29 ida_simple_remove(&nd_region->ns_ida, nspm->id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040030 kfree(nspm->alt_name);
31 kfree(nspm->uuid);
32 kfree(nspm);
33}
34
35static void namespace_blk_release(struct device *dev)
36{
Dan Williams1b40e092015-05-01 13:34:01 -040037 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
38 struct nd_region *nd_region = to_nd_region(dev->parent);
39
40 if (nsblk->id >= 0)
41 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
42 kfree(nsblk->alt_name);
43 kfree(nsblk->uuid);
44 kfree(nsblk->res);
45 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040046}
47
Dan Williams78c81cc2019-11-06 19:56:41 -080048static bool is_namespace_pmem(const struct device *dev);
49static bool is_namespace_blk(const struct device *dev);
50static bool is_namespace_io(const struct device *dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040051
Dan Williamse07ecd72016-01-05 18:37:23 -080052static int is_uuid_busy(struct device *dev, void *data)
53{
Dan Williamsd1c6e082021-09-08 22:11:37 -070054 uuid_t *uuid1 = data, *uuid2 = NULL;
Dan Williamse07ecd72016-01-05 18:37:23 -080055
56 if (is_namespace_pmem(dev)) {
57 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
58
59 uuid2 = nspm->uuid;
60 } else if (is_namespace_blk(dev)) {
61 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
62
63 uuid2 = nsblk->uuid;
64 } else if (is_nd_btt(dev)) {
65 struct nd_btt *nd_btt = to_nd_btt(dev);
66
67 uuid2 = nd_btt->uuid;
68 } else if (is_nd_pfn(dev)) {
69 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
70
71 uuid2 = nd_pfn->uuid;
72 }
73
Dan Williamsd1c6e082021-09-08 22:11:37 -070074 if (uuid2 && uuid_equal(uuid1, uuid2))
Dan Williamse07ecd72016-01-05 18:37:23 -080075 return -EBUSY;
76
77 return 0;
78}
79
80static int is_namespace_uuid_busy(struct device *dev, void *data)
81{
Dan Williamsc9e582a2017-05-29 23:12:19 -070082 if (is_nd_region(dev))
Dan Williamse07ecd72016-01-05 18:37:23 -080083 return device_for_each_child(dev, data, is_uuid_busy);
84 return 0;
85}
86
87/**
88 * nd_is_uuid_unique - verify that no other namespace has @uuid
89 * @dev: any device on a nvdimm_bus
90 * @uuid: uuid to check
91 */
Dan Williamsd1c6e082021-09-08 22:11:37 -070092bool nd_is_uuid_unique(struct device *dev, uuid_t *uuid)
Dan Williamse07ecd72016-01-05 18:37:23 -080093{
94 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
95
96 if (!nvdimm_bus)
97 return false;
98 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
99 if (device_for_each_child(&nvdimm_bus->dev, uuid,
100 is_namespace_uuid_busy) != 0)
101 return false;
102 return true;
103}
104
Dan Williams004f1af2015-08-24 19:20:23 -0400105bool pmem_should_map_pages(struct device *dev)
106{
107 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsfa7d2e62019-01-24 17:33:06 -0800108 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamscfe30b82016-03-03 09:38:00 -0800109 struct nd_namespace_io *nsio;
Dan Williams004f1af2015-08-24 19:20:23 -0400110
111 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
112 return false;
113
114 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
115 return false;
116
117 if (is_nd_pfn(dev) || is_nd_btt(dev))
118 return false;
119
Dan Williamsfa7d2e62019-01-24 17:33:06 -0800120 if (ndns->force_raw)
121 return false;
122
Dan Williamscfe30b82016-03-03 09:38:00 -0800123 nsio = to_nd_namespace_io(dev);
124 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
125 IORESOURCE_SYSTEM_RAM,
126 IORES_DESC_NONE) == REGION_MIXED)
127 return false;
128
Dan Williams004f1af2015-08-24 19:20:23 -0400129 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
Dan Williams004f1af2015-08-24 19:20:23 -0400130}
131EXPORT_SYMBOL(pmem_should_map_pages);
132
Dan Williamsf979b132017-06-04 12:12:07 +0900133unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
134{
135 if (is_namespace_pmem(&ndns->dev)) {
136 struct nd_namespace_pmem *nspm;
137
138 nspm = to_nd_namespace_pmem(&ndns->dev);
139 if (nspm->lbasize == 0 || nspm->lbasize == 512)
140 /* default */;
141 else if (nspm->lbasize == 4096)
142 return 4096;
143 else
144 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
145 nspm->lbasize);
146 }
147
148 /*
149 * There is no namespace label (is_namespace_io()), or the label
150 * indicates the default sector size.
151 */
152 return 512;
153}
154EXPORT_SYMBOL(pmem_sector_size);
155
Vishal Verma5212e112015-06-25 04:20:32 -0400156const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
157 char *name)
158{
159 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400160 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400161
Dan Williams0731de02015-12-14 15:34:15 -0800162 if (ndns->claim && is_nd_btt(ndns->claim))
163 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400164
Dan Williams004f1af2015-08-24 19:20:23 -0400165 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams01220732016-10-05 09:09:44 -0700166 int nsidx = 0;
167
168 if (is_namespace_pmem(&ndns->dev)) {
169 struct nd_namespace_pmem *nspm;
170
171 nspm = to_nd_namespace_pmem(&ndns->dev);
172 nsidx = nspm->id;
173 }
174
175 if (nsidx)
176 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
177 suffix ? suffix : "");
178 else
179 sprintf(name, "pmem%d%s", nd_region->id,
180 suffix ? suffix : "");
Dan Williams004f1af2015-08-24 19:20:23 -0400181 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400182 struct nd_namespace_blk *nsblk;
183
184 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400185 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
186 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400187 } else {
188 return NULL;
189 }
190
191 return name;
192}
193EXPORT_SYMBOL(nvdimm_namespace_disk_name);
194
Dan Williamsd1c6e082021-09-08 22:11:37 -0700195const uuid_t *nd_dev_to_uuid(struct device *dev)
Vishal Verma6ec68952015-07-29 14:58:09 -0600196{
Vishal Verma6ec68952015-07-29 14:58:09 -0600197 if (!dev)
Dan Williamsd1c6e082021-09-08 22:11:37 -0700198 return &uuid_null;
Vishal Verma6ec68952015-07-29 14:58:09 -0600199
200 if (is_namespace_pmem(dev)) {
201 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
202
203 return nspm->uuid;
204 } else if (is_namespace_blk(dev)) {
205 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
206
207 return nsblk->uuid;
208 } else
Dan Williamsd1c6e082021-09-08 22:11:37 -0700209 return &uuid_null;
Vishal Verma6ec68952015-07-29 14:58:09 -0600210}
211EXPORT_SYMBOL(nd_dev_to_uuid);
212
Dan Williams3d880022015-05-31 15:02:11 -0400213static ssize_t nstype_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct nd_region *nd_region = to_nd_region(dev->parent);
217
218 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
219}
220static DEVICE_ATTR_RO(nstype);
221
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400222static ssize_t __alt_name_store(struct device *dev, const char *buf,
223 const size_t len)
224{
225 char *input, *pos, *alt_name, **ns_altname;
226 ssize_t rc;
227
228 if (is_namespace_pmem(dev)) {
229 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
230
231 ns_altname = &nspm->alt_name;
232 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400233 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
234
235 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400236 } else
237 return -ENXIO;
238
Dan Williams8c2f7e82015-06-25 04:20:04 -0400239 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400240 return -EBUSY;
241
Andy Shevchenko3d9cbe32018-06-11 16:47:21 +0300242 input = kstrndup(buf, len, GFP_KERNEL);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400243 if (!input)
244 return -ENOMEM;
245
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400246 pos = strim(input);
247 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
248 rc = -EINVAL;
249 goto out;
250 }
251
252 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
253 if (!alt_name) {
254 rc = -ENOMEM;
255 goto out;
256 }
257 kfree(*ns_altname);
258 *ns_altname = alt_name;
259 sprintf(*ns_altname, "%s", pos);
260 rc = len;
261
262out:
263 kfree(input);
264 return rc;
265}
266
Dan Williams1b40e092015-05-01 13:34:01 -0400267static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
268{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400269 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400270 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
271 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
272 struct nd_label_id label_id;
273 resource_size_t size = 0;
274 struct resource *res;
275
276 if (!nsblk->uuid)
277 return 0;
278 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
279 for_each_dpa_resource(ndd, res)
280 if (strcmp(res->name, label_id.id) == 0)
281 size += resource_size(res);
282 return size;
283}
284
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400285static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
286{
287 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
288 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
289 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
290 struct nd_label_id label_id;
291 struct resource *res;
292 int count, i;
293
294 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
295 return false;
296
297 count = 0;
298 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
299 for_each_dpa_resource(ndd, res) {
300 if (strcmp(res->name, label_id.id) != 0)
301 continue;
302 /*
Geert Uytterhoevenae551e92016-08-31 11:45:25 +0200303 * Resources with unacknowledged adjustments indicate a
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400304 * failure to update labels
305 */
306 if (res->flags & DPA_RESOURCE_ADJUSTED)
307 return false;
308 count++;
309 }
310
311 /* These values match after a successful label update */
312 if (count != nsblk->num_resources)
313 return false;
314
315 for (i = 0; i < nsblk->num_resources; i++) {
316 struct resource *found = NULL;
317
318 for_each_dpa_resource(ndd, res)
319 if (res == nsblk->res[i]) {
320 found = res;
321 break;
322 }
323 /* stale resource */
324 if (!found)
325 return false;
326 }
327
328 return true;
329}
330
331resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
332{
333 resource_size_t size;
334
335 nvdimm_bus_lock(&nsblk->common.dev);
336 size = __nd_namespace_blk_validate(nsblk);
337 nvdimm_bus_unlock(&nsblk->common.dev);
338
339 return size;
340}
341EXPORT_SYMBOL(nd_namespace_blk_validate);
342
343
Dan Williamsf524bf22015-05-30 12:36:02 -0400344static int nd_namespace_label_update(struct nd_region *nd_region,
345 struct device *dev)
346{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400347 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400348 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400349 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400350 return 0;
351
352 /*
353 * Only allow label writes that will result in a valid namespace
354 * or deletion of an existing namespace.
355 */
356 if (is_namespace_pmem(dev)) {
357 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400358 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400359
360 if (size == 0 && nspm->uuid)
361 /* delete allocation */;
362 else if (!nspm->uuid)
363 return 0;
364
365 return nd_pmem_namespace_label_update(nd_region, nspm, size);
366 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400367 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
368 resource_size_t size = nd_namespace_blk_size(nsblk);
369
370 if (size == 0 && nsblk->uuid)
371 /* delete allocation */;
372 else if (!nsblk->uuid || !nsblk->lbasize)
373 return 0;
374
375 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400376 } else
377 return -ENXIO;
378}
379
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400380static ssize_t alt_name_store(struct device *dev,
381 struct device_attribute *attr, const char *buf, size_t len)
382{
Dan Williamsf524bf22015-05-30 12:36:02 -0400383 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400384 ssize_t rc;
385
Dan Williams87a30e12019-07-17 18:08:26 -0700386 nd_device_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400387 nvdimm_bus_lock(dev);
388 wait_nvdimm_bus_probe_idle(dev);
389 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400390 if (rc >= 0)
391 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -0800392 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400393 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700394 nd_device_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400395
Dan Williamsf524bf22015-05-30 12:36:02 -0400396 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400397}
398
399static ssize_t alt_name_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
402 char *ns_altname;
403
404 if (is_namespace_pmem(dev)) {
405 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
406
407 ns_altname = nspm->alt_name;
408 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400409 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
410
411 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400412 } else
413 return -ENXIO;
414
415 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
416}
417static DEVICE_ATTR_RW(alt_name);
418
419static int scan_free(struct nd_region *nd_region,
420 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
421 resource_size_t n)
422{
423 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
424 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
425 int rc = 0;
426
427 while (n) {
428 struct resource *res, *last;
429 resource_size_t new_start;
430
431 last = NULL;
432 for_each_dpa_resource(ndd, res)
433 if (strcmp(res->name, label_id->id) == 0)
434 last = res;
435 res = last;
436 if (!res)
437 return 0;
438
439 if (n >= resource_size(res)) {
440 n -= resource_size(res);
441 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
442 nvdimm_free_dpa(ndd, res);
443 /* retry with last resource deleted */
444 continue;
445 }
446
447 /*
448 * Keep BLK allocations relegated to high DPA as much as
449 * possible
450 */
451 if (is_blk)
452 new_start = res->start + n;
453 else
454 new_start = res->start;
455
456 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400457 if (rc == 0)
458 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400459 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
460 break;
461 }
462
463 return rc;
464}
465
466/**
467 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
468 * @nd_region: the set of dimms to reclaim @n bytes from
469 * @label_id: unique identifier for the namespace consuming this dpa range
470 * @n: number of bytes per-dimm to release
471 *
472 * Assumes resources are ordered. Starting from the end try to
473 * adjust_resource() the allocation to @n, but if @n is larger than the
474 * allocation delete it and find the 'new' last allocation in the label
475 * set.
476 */
477static int shrink_dpa_allocation(struct nd_region *nd_region,
478 struct nd_label_id *label_id, resource_size_t n)
479{
480 int i;
481
482 for (i = 0; i < nd_region->ndr_mappings; i++) {
483 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
484 int rc;
485
486 rc = scan_free(nd_region, nd_mapping, label_id, n);
487 if (rc)
488 return rc;
489 }
490
491 return 0;
492}
493
494static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
495 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
496 resource_size_t n)
497{
498 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
499 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
500 resource_size_t first_dpa;
501 struct resource *res;
502 int rc = 0;
503
504 /* allocate blk from highest dpa first */
505 if (is_blk)
506 first_dpa = nd_mapping->start + nd_mapping->size - n;
507 else
508 first_dpa = nd_mapping->start;
509
510 /* first resource allocation for this label-id or dimm */
511 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
512 if (!res)
513 rc = -EBUSY;
514
515 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
516 return rc ? n : 0;
517}
518
Dan Williams762d0672016-10-04 16:09:59 -0700519
520/**
521 * space_valid() - validate free dpa space against constraints
522 * @nd_region: hosting region of the free space
523 * @ndd: dimm device data for debug
524 * @label_id: namespace id to allocate space
525 * @prev: potential allocation that precedes free space
526 * @next: allocation that follows the given free space range
527 * @exist: first allocation with same id in the mapping
528 * @n: range that must satisfied for pmem allocations
529 * @valid: free space range to validate
530 *
531 * BLK-space is valid as long as it does not precede a PMEM
532 * allocation in a given region. PMEM-space must be contiguous
533 * and adjacent to an existing existing allocation (if one
534 * exists). If reserving PMEM any space is valid.
535 */
536static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
537 struct nd_label_id *label_id, struct resource *prev,
538 struct resource *next, struct resource *exist,
539 resource_size_t n, struct resource *valid)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400540{
Dan Williams762d0672016-10-04 16:09:59 -0700541 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
542 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williams2522afb2020-01-30 12:06:23 -0800543 unsigned long align;
544
545 align = nd_region->align / nd_region->ndr_mappings;
546 valid->start = ALIGN(valid->start, align);
547 valid->end = ALIGN_DOWN(valid->end + 1, align) - 1;
Dan Williams762d0672016-10-04 16:09:59 -0700548
549 if (valid->start >= valid->end)
550 goto invalid;
551
552 if (is_reserve)
553 return;
554
555 if (!is_pmem) {
556 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
557 struct nvdimm_bus *nvdimm_bus;
558 struct blk_alloc_info info = {
559 .nd_mapping = nd_mapping,
560 .available = nd_mapping->size,
561 .res = valid,
562 };
563
564 WARN_ON(!is_nd_blk(&nd_region->dev));
565 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
566 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
567 return;
568 }
569
570 /* allocation needs to be contiguous, so this is all or nothing */
571 if (resource_size(valid) < n)
572 goto invalid;
573
574 /* we've got all the space we need and no existing allocation */
575 if (!exist)
576 return;
577
578 /* allocation needs to be contiguous with the existing namespace */
579 if (valid->start == exist->end + 1
580 || valid->end == exist->start - 1)
581 return;
582
583 invalid:
584 /* truncate @valid size to 0 */
585 valid->end = valid->start - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400586}
587
588enum alloc_loc {
589 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
590};
591
592static resource_size_t scan_allocate(struct nd_region *nd_region,
593 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
594 resource_size_t n)
595{
596 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
597 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
598 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams762d0672016-10-04 16:09:59 -0700599 struct resource *res, *exist = NULL, valid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400600 const resource_size_t to_allocate = n;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400601 int first;
602
Dan Williams762d0672016-10-04 16:09:59 -0700603 for_each_dpa_resource(ndd, res)
604 if (strcmp(label_id->id, res->name) == 0)
605 exist = res;
606
607 valid.start = nd_mapping->start;
608 valid.end = mapping_end;
609 valid.name = "free space";
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400610 retry:
611 first = 0;
612 for_each_dpa_resource(ndd, res) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400613 struct resource *next = res->sibling, *new_res = NULL;
Dan Williams762d0672016-10-04 16:09:59 -0700614 resource_size_t allocate, available = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400615 enum alloc_loc loc = ALLOC_ERR;
616 const char *action;
617 int rc = 0;
618
619 /* ignore resources outside this nd_mapping */
620 if (res->start > mapping_end)
621 continue;
622 if (res->end < nd_mapping->start)
623 continue;
624
625 /* space at the beginning of the mapping */
626 if (!first++ && res->start > nd_mapping->start) {
Dan Williams762d0672016-10-04 16:09:59 -0700627 valid.start = nd_mapping->start;
628 valid.end = res->start - 1;
629 space_valid(nd_region, ndd, label_id, NULL, next, exist,
630 to_allocate, &valid);
631 available = resource_size(&valid);
632 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400633 loc = ALLOC_BEFORE;
634 }
635
636 /* space between allocations */
637 if (!loc && next) {
Dan Williams762d0672016-10-04 16:09:59 -0700638 valid.start = res->start + resource_size(res);
639 valid.end = min(mapping_end, next->start - 1);
640 space_valid(nd_region, ndd, label_id, res, next, exist,
641 to_allocate, &valid);
642 available = resource_size(&valid);
643 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400644 loc = ALLOC_MID;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400645 }
646
647 /* space at the end of the mapping */
648 if (!loc && !next) {
Dan Williams762d0672016-10-04 16:09:59 -0700649 valid.start = res->start + resource_size(res);
650 valid.end = mapping_end;
651 space_valid(nd_region, ndd, label_id, res, next, exist,
652 to_allocate, &valid);
653 available = resource_size(&valid);
654 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400655 loc = ALLOC_AFTER;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400656 }
657
658 if (!loc || !available)
659 continue;
660 allocate = min(available, n);
661 switch (loc) {
662 case ALLOC_BEFORE:
663 if (strcmp(res->name, label_id->id) == 0) {
664 /* adjust current resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400665 rc = adjust_resource(res, res->start - allocate,
666 resource_size(res) + allocate);
667 action = "cur grow up";
668 } else
669 action = "allocate";
670 break;
671 case ALLOC_MID:
672 if (strcmp(next->name, label_id->id) == 0) {
673 /* adjust next resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400674 rc = adjust_resource(next, next->start
675 - allocate, resource_size(next)
676 + allocate);
677 new_res = next;
678 action = "next grow up";
679 } else if (strcmp(res->name, label_id->id) == 0) {
680 action = "grow down";
681 } else
682 action = "allocate";
683 break;
684 case ALLOC_AFTER:
685 if (strcmp(res->name, label_id->id) == 0)
686 action = "grow down";
687 else
688 action = "allocate";
689 break;
690 default:
691 return n;
692 }
693
694 if (strcmp(action, "allocate") == 0) {
695 /* BLK allocate bottom up */
696 if (!is_pmem)
Dan Williams762d0672016-10-04 16:09:59 -0700697 valid.start += available - allocate;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400698
699 new_res = nvdimm_allocate_dpa(ndd, label_id,
Dan Williams762d0672016-10-04 16:09:59 -0700700 valid.start, allocate);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400701 if (!new_res)
702 rc = -EBUSY;
703 } else if (strcmp(action, "grow down") == 0) {
704 /* adjust current resource down */
705 rc = adjust_resource(res, res->start, resource_size(res)
706 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400707 if (rc == 0)
708 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400709 }
710
711 if (!new_res)
712 new_res = res;
713
714 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
715 action, loc, rc);
716
717 if (rc)
718 return n;
719
720 n -= allocate;
721 if (n) {
722 /*
723 * Retry scan with newly inserted resources.
724 * For example, if we did an ALLOC_BEFORE
725 * insertion there may also have been space
726 * available for an ALLOC_AFTER insertion, so we
727 * need to check this same resource again
728 */
729 goto retry;
730 } else
731 return 0;
732 }
733
Dan Williams1b40e092015-05-01 13:34:01 -0400734 /*
735 * If we allocated nothing in the BLK case it may be because we are in
736 * an initial "pmem-reserve pass". Only do an initial BLK allocation
737 * when none of the DPA space is reserved.
738 */
739 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400740 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
741 return n;
742}
743
Dan Williams1b40e092015-05-01 13:34:01 -0400744static int merge_dpa(struct nd_region *nd_region,
745 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
746{
747 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
748 struct resource *res;
749
750 if (strncmp("pmem", label_id->id, 4) == 0)
751 return 0;
752 retry:
753 for_each_dpa_resource(ndd, res) {
754 int rc;
755 struct resource *next = res->sibling;
756 resource_size_t end = res->start + resource_size(res);
757
758 if (!next || strcmp(res->name, label_id->id) != 0
759 || strcmp(next->name, label_id->id) != 0
760 || end != next->start)
761 continue;
762 end += resource_size(next);
763 nvdimm_free_dpa(ndd, next);
764 rc = adjust_resource(res, res->start, end - res->start);
765 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
766 if (rc)
767 return rc;
768 res->flags |= DPA_RESOURCE_ADJUSTED;
769 goto retry;
770 }
771
772 return 0;
773}
774
Keith Busch12e31292018-07-24 15:07:57 -0600775int __reserve_free_pmem(struct device *dev, void *data)
Dan Williams1b40e092015-05-01 13:34:01 -0400776{
777 struct nvdimm *nvdimm = data;
778 struct nd_region *nd_region;
779 struct nd_label_id label_id;
780 int i;
781
Dan Williamsc9e582a2017-05-29 23:12:19 -0700782 if (!is_memory(dev))
Dan Williams1b40e092015-05-01 13:34:01 -0400783 return 0;
784
785 nd_region = to_nd_region(dev);
786 if (nd_region->ndr_mappings == 0)
787 return 0;
788
789 memset(&label_id, 0, sizeof(label_id));
790 strcat(label_id.id, "pmem-reserve");
791 for (i = 0; i < nd_region->ndr_mappings; i++) {
792 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
793 resource_size_t n, rem = 0;
794
795 if (nd_mapping->nvdimm != nvdimm)
796 continue;
797
798 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
799 if (n == 0)
800 return 0;
801 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
802 dev_WARN_ONCE(&nd_region->dev, rem,
803 "pmem reserve underrun: %#llx of %#llx bytes\n",
804 (unsigned long long) n - rem,
805 (unsigned long long) n);
806 return rem ? -ENXIO : 0;
807 }
808
809 return 0;
810}
811
Keith Busch12e31292018-07-24 15:07:57 -0600812void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
Dan Williams1b40e092015-05-01 13:34:01 -0400813 struct nd_mapping *nd_mapping)
814{
815 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
816 struct resource *res, *_res;
817
818 for_each_dpa_resource_safe(ndd, res, _res)
819 if (strcmp(res->name, "pmem-reserve") == 0)
820 nvdimm_free_dpa(ndd, res);
821}
822
823static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
824 struct nd_mapping *nd_mapping)
825{
826 struct nvdimm *nvdimm = nd_mapping->nvdimm;
827 int rc;
828
829 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
830 __reserve_free_pmem);
831 if (rc)
832 release_free_pmem(nvdimm_bus, nd_mapping);
833 return rc;
834}
835
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400836/**
837 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
838 * @nd_region: the set of dimms to allocate @n more bytes from
839 * @label_id: unique identifier for the namespace consuming this dpa range
840 * @n: number of bytes per-dimm to add to the existing allocation
841 *
842 * Assumes resources are ordered. For BLK regions, first consume
843 * BLK-only available DPA free space, then consume PMEM-aliased DPA
844 * space starting at the highest DPA. For PMEM regions start
845 * allocations from the start of an interleave set and end at the first
846 * BLK allocation or the end of the interleave set, whichever comes
847 * first.
848 */
849static int grow_dpa_allocation(struct nd_region *nd_region,
850 struct nd_label_id *label_id, resource_size_t n)
851{
Dan Williams1b40e092015-05-01 13:34:01 -0400852 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
853 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400854 int i;
855
856 for (i = 0; i < nd_region->ndr_mappings; i++) {
857 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400858 resource_size_t rem = n;
859 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400860
Dan Williams1b40e092015-05-01 13:34:01 -0400861 /*
862 * In the BLK case try once with all unallocated PMEM
863 * reserved, and once without
864 */
865 for (j = is_pmem; j < 2; j++) {
866 bool blk_only = j == 0;
867
868 if (blk_only) {
869 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
870 if (rc)
871 return rc;
872 }
873 rem = scan_allocate(nd_region, nd_mapping,
874 label_id, rem);
875 if (blk_only)
876 release_free_pmem(nvdimm_bus, nd_mapping);
877
878 /* try again and allow encroachments into PMEM */
879 if (rem == 0)
880 break;
881 }
882
883 dev_WARN_ONCE(&nd_region->dev, rem,
884 "allocation underrun: %#llx of %#llx bytes\n",
885 (unsigned long long) n - rem,
886 (unsigned long long) n);
887 if (rem)
888 return -ENXIO;
889
890 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400891 if (rc)
892 return rc;
893 }
894
895 return 0;
896}
897
Dan Williams0e3b0d12016-10-06 23:13:15 -0700898static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400899 struct nd_namespace_pmem *nspm, resource_size_t size)
900{
901 struct resource *res = &nspm->nsio.res;
Dan Williams0e3b0d12016-10-06 23:13:15 -0700902 resource_size_t offset = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400903
Dan Williams0e3b0d12016-10-06 23:13:15 -0700904 if (size && !nspm->uuid) {
905 WARN_ON_ONCE(1);
906 size = 0;
907 }
908
909 if (size && nspm->uuid) {
910 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
911 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
912 struct nd_label_id label_id;
913 struct resource *res;
914
915 if (!ndd) {
916 size = 0;
917 goto out;
918 }
919
920 nd_label_gen_id(&label_id, nspm->uuid, 0);
921
922 /* calculate a spa offset from the dpa allocation offset */
923 for_each_dpa_resource(ndd, res)
924 if (strcmp(res->name, label_id.id) == 0) {
925 offset = (res->start - nd_mapping->start)
926 * nd_region->ndr_mappings;
927 goto out;
928 }
929
930 WARN_ON_ONCE(1);
931 size = 0;
932 }
933
934 out:
935 res->start = nd_region->ndr_start + offset;
936 res->end = res->start + size - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400937}
938
Dan Williamsd1c6e082021-09-08 22:11:37 -0700939static bool uuid_not_set(const uuid_t *uuid, struct device *dev,
940 const char *where)
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300941{
942 if (!uuid) {
943 dev_dbg(dev, "%s: uuid not set\n", where);
944 return true;
945 }
946 return false;
947}
948
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400949static ssize_t __size_store(struct device *dev, unsigned long long val)
950{
951 resource_size_t allocated = 0, available = 0;
952 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams1f19b982017-01-09 17:30:49 -0800953 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400954 struct nd_mapping *nd_mapping;
955 struct nvdimm_drvdata *ndd;
956 struct nd_label_id label_id;
957 u32 flags = 0, remainder;
Dan Williams9d032f42017-01-25 00:54:07 +0530958 int rc, i, id = -1;
Dan Williamsd1c6e082021-09-08 22:11:37 -0700959 uuid_t *uuid = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400960
Dan Williams1f19b982017-01-09 17:30:49 -0800961 if (dev->driver || ndns->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400962 return -EBUSY;
963
964 if (is_namespace_pmem(dev)) {
965 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
966
967 uuid = nspm->uuid;
Dan Williams9d032f42017-01-25 00:54:07 +0530968 id = nspm->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400969 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400970 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
971
972 uuid = nsblk->uuid;
973 flags = NSLABEL_FLAG_LOCAL;
Dan Williams9d032f42017-01-25 00:54:07 +0530974 id = nsblk->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400975 }
976
977 /*
978 * We need a uuid for the allocation-label and dimm(s) on which
979 * to store the label.
980 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300981 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400982 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300983 if (nd_region->ndr_mappings == 0) {
Dan Williams426824d2018-03-05 16:39:31 -0800984 dev_dbg(dev, "not associated with dimm(s)\n");
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300985 return -ENXIO;
986 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400987
Dan Williams2522afb2020-01-30 12:06:23 -0800988 div_u64_rem(val, nd_region->align, &remainder);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400989 if (remainder) {
Aneesh Kumar K.V5b26db92019-09-05 21:16:02 +0530990 dev_dbg(dev, "%llu is not %ldK aligned\n", val,
Dan Williams2522afb2020-01-30 12:06:23 -0800991 nd_region->align / SZ_1K);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400992 return -EINVAL;
993 }
994
995 nd_label_gen_id(&label_id, uuid, flags);
996 for (i = 0; i < nd_region->ndr_mappings; i++) {
997 nd_mapping = &nd_region->mapping[i];
998 ndd = to_ndd(nd_mapping);
999
1000 /*
1001 * All dimms in an interleave set, or the base dimm for a blk
1002 * region, need to be enabled for the size to be changed.
1003 */
1004 if (!ndd)
1005 return -ENXIO;
1006
1007 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1008 }
Keith Busch12e31292018-07-24 15:07:57 -06001009 available = nd_region_allocatable_dpa(nd_region);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001010
1011 if (val > available + allocated)
1012 return -ENOSPC;
1013
1014 if (val == allocated)
1015 return 0;
1016
1017 val = div_u64(val, nd_region->ndr_mappings);
1018 allocated = div_u64(allocated, nd_region->ndr_mappings);
1019 if (val < allocated)
1020 rc = shrink_dpa_allocation(nd_region, &label_id,
1021 allocated - val);
1022 else
1023 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1024
1025 if (rc)
1026 return rc;
1027
1028 if (is_namespace_pmem(dev)) {
1029 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1030
Dan Williams0e3b0d12016-10-06 23:13:15 -07001031 nd_namespace_pmem_set_resource(nd_region, nspm,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001032 val * nd_region->ndr_mappings);
1033 }
1034
Dan Williams1f19b982017-01-09 17:30:49 -08001035 /*
1036 * Try to delete the namespace if we deleted all of its
Dan Williams9d032f42017-01-25 00:54:07 +05301037 * allocation, this is not the seed or 0th device for the
1038 * region, and it is not actively claimed by a btt, pfn, or dax
1039 * instance.
Dan Williams1f19b982017-01-09 17:30:49 -08001040 */
Dan Williams9d032f42017-01-25 00:54:07 +05301041 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
Dan Williams1f19b982017-01-09 17:30:49 -08001042 nd_device_unregister(dev, ND_ASYNC);
1043
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001044 return rc;
1045}
1046
1047static ssize_t size_store(struct device *dev,
1048 struct device_attribute *attr, const char *buf, size_t len)
1049{
Dan Williamsf524bf22015-05-30 12:36:02 -04001050 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001051 unsigned long long val;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001052 uuid_t **uuid = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001053 int rc;
1054
1055 rc = kstrtoull(buf, 0, &val);
1056 if (rc)
1057 return rc;
1058
Dan Williams87a30e12019-07-17 18:08:26 -07001059 nd_device_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001060 nvdimm_bus_lock(dev);
1061 wait_nvdimm_bus_probe_idle(dev);
1062 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -04001063 if (rc >= 0)
1064 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001065
1066 if (is_namespace_pmem(dev)) {
1067 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1068
1069 uuid = &nspm->uuid;
1070 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001071 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1072
1073 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001074 }
1075
1076 if (rc == 0 && val == 0 && uuid) {
1077 /* setting size zero == 'delete namespace' */
1078 kfree(*uuid);
1079 *uuid = NULL;
1080 }
1081
Dan Williams426824d2018-03-05 16:39:31 -08001082 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001083
1084 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001085 nd_device_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001086
Dan Williamsf524bf22015-05-30 12:36:02 -04001087 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001088}
1089
Dan Williams8c2f7e82015-06-25 04:20:04 -04001090resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001091{
Dan Williams8c2f7e82015-06-25 04:20:04 -04001092 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001093
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001094 if (is_namespace_pmem(dev)) {
1095 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1096
Dan Williams8c2f7e82015-06-25 04:20:04 -04001097 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001098 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -04001099 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001100 } else if (is_namespace_io(dev)) {
1101 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1102
Dan Williams8c2f7e82015-06-25 04:20:04 -04001103 return resource_size(&nsio->res);
1104 } else
1105 WARN_ONCE(1, "unknown namespace type\n");
1106 return 0;
1107}
Dan Williams1b40e092015-05-01 13:34:01 -04001108
Dan Williams8c2f7e82015-06-25 04:20:04 -04001109resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1110{
1111 resource_size_t size;
1112
1113 nvdimm_bus_lock(&ndns->dev);
1114 size = __nvdimm_namespace_capacity(ndns);
1115 nvdimm_bus_unlock(&ndns->dev);
1116
1117 return size;
1118}
1119EXPORT_SYMBOL(nvdimm_namespace_capacity);
1120
Dan Williams08e6b3c2018-06-13 09:08:36 -07001121bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1122{
1123 int i;
1124 bool locked = false;
1125 struct device *dev = &ndns->dev;
1126 struct nd_region *nd_region = to_nd_region(dev->parent);
1127
1128 for (i = 0; i < nd_region->ndr_mappings; i++) {
1129 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1130 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1131
1132 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1133 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1134 locked = true;
1135 }
1136 }
1137 return locked;
1138}
1139EXPORT_SYMBOL(nvdimm_namespace_locked);
1140
Dan Williams8c2f7e82015-06-25 04:20:04 -04001141static ssize_t size_show(struct device *dev,
1142 struct device_attribute *attr, char *buf)
1143{
1144 return sprintf(buf, "%llu\n", (unsigned long long)
1145 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001146}
Fabian Frederickb44fe762016-12-04 10:54:08 -08001147static DEVICE_ATTR(size, 0444, size_show, size_store);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001148
Dan Williamsd1c6e082021-09-08 22:11:37 -07001149static uuid_t *namespace_to_uuid(struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001150{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001151 if (is_namespace_pmem(dev)) {
1152 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1153
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001154 return nspm->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001155 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001156 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1157
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001158 return nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001159 } else
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001160 return ERR_PTR(-ENXIO);
1161}
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001162
Dan Williamsd1c6e082021-09-08 22:11:37 -07001163static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1164 char *buf)
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001165{
Dan Williamsd1c6e082021-09-08 22:11:37 -07001166 uuid_t *uuid = namespace_to_uuid(dev);
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001167
1168 if (IS_ERR(uuid))
1169 return PTR_ERR(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001170 if (uuid)
1171 return sprintf(buf, "%pUb\n", uuid);
1172 return sprintf(buf, "\n");
1173}
1174
1175/**
1176 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1177 * @nd_region: parent region so we can updates all dimms in the set
1178 * @dev: namespace type for generating label_id
1179 * @new_uuid: incoming uuid
1180 * @old_uuid: reference to the uuid storage location in the namespace object
1181 */
1182static int namespace_update_uuid(struct nd_region *nd_region,
Dan Williamsd1c6e082021-09-08 22:11:37 -07001183 struct device *dev, uuid_t *new_uuid,
1184 uuid_t **old_uuid)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001185{
1186 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1187 struct nd_label_id old_label_id;
1188 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001189 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001190
Dan Williamsf524bf22015-05-30 12:36:02 -04001191 if (!nd_is_uuid_unique(dev, new_uuid))
1192 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001193
1194 if (*old_uuid == NULL)
1195 goto out;
1196
Dan Williamsf524bf22015-05-30 12:36:02 -04001197 /*
1198 * If we've already written a label with this uuid, then it's
1199 * too late to rename because we can't reliably update the uuid
1200 * without losing the old namespace. Userspace must delete this
1201 * namespace to abandon the old uuid.
1202 */
1203 for (i = 0; i < nd_region->ndr_mappings; i++) {
1204 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1205
1206 /*
1207 * This check by itself is sufficient because old_uuid
1208 * would be NULL above if this uuid did not exist in the
1209 * currently written set.
1210 *
1211 * FIXME: can we delete uuid with zero dpa allocated?
1212 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001213 if (list_empty(&nd_mapping->labels))
Dan Williamsf524bf22015-05-30 12:36:02 -04001214 return -EBUSY;
1215 }
1216
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001217 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1218 nd_label_gen_id(&new_label_id, new_uuid, flags);
1219 for (i = 0; i < nd_region->ndr_mappings; i++) {
1220 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1221 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsc4703ce2019-04-30 21:51:21 -07001222 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001223 struct resource *res;
1224
1225 for_each_dpa_resource(ndd, res)
1226 if (strcmp(res->name, old_label_id.id) == 0)
1227 sprintf((void *) res->name, "%s",
1228 new_label_id.id);
Dan Williamsc4703ce2019-04-30 21:51:21 -07001229
1230 mutex_lock(&nd_mapping->lock);
1231 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1232 struct nd_namespace_label *nd_label = label_ent->label;
1233 struct nd_label_id label_id;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001234 uuid_t uuid;
Dan Williamsc4703ce2019-04-30 21:51:21 -07001235
1236 if (!nd_label)
1237 continue;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001238 nsl_get_uuid(ndd, nd_label, &uuid);
1239 nd_label_gen_id(&label_id, &uuid,
Dan Williamsb4366a82021-08-24 09:05:30 -07001240 nsl_get_flags(ndd, nd_label));
Dan Williamsc4703ce2019-04-30 21:51:21 -07001241 if (strcmp(old_label_id.id, label_id.id) == 0)
1242 set_bit(ND_LABEL_REAP, &label_ent->flags);
1243 }
1244 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001245 }
1246 kfree(*old_uuid);
1247 out:
1248 *old_uuid = new_uuid;
1249 return 0;
1250}
1251
1252static ssize_t uuid_store(struct device *dev,
1253 struct device_attribute *attr, const char *buf, size_t len)
1254{
1255 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsd1c6e082021-09-08 22:11:37 -07001256 uuid_t *uuid = NULL;
1257 uuid_t **ns_uuid;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001258 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001259
1260 if (is_namespace_pmem(dev)) {
1261 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1262
1263 ns_uuid = &nspm->uuid;
1264 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001265 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1266
1267 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001268 } else
1269 return -ENXIO;
1270
Dan Williams87a30e12019-07-17 18:08:26 -07001271 nd_device_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001272 nvdimm_bus_lock(dev);
1273 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001274 if (to_ndns(dev)->claim)
1275 rc = -EBUSY;
1276 if (rc >= 0)
1277 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001278 if (rc >= 0)
1279 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001280 if (rc >= 0)
1281 rc = nd_namespace_label_update(nd_region, dev);
1282 else
1283 kfree(uuid);
Dan Williams426824d2018-03-05 16:39:31 -08001284 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1285 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001286 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001287 nd_device_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001288
Dan Williamsf524bf22015-05-30 12:36:02 -04001289 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001290}
1291static DEVICE_ATTR_RW(uuid);
1292
1293static ssize_t resource_show(struct device *dev,
1294 struct device_attribute *attr, char *buf)
1295{
1296 struct resource *res;
1297
1298 if (is_namespace_pmem(dev)) {
1299 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1300
1301 res = &nspm->nsio.res;
1302 } else if (is_namespace_io(dev)) {
1303 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1304
1305 res = &nsio->res;
1306 } else
1307 return -ENXIO;
1308
1309 /* no address to convey if the namespace has no allocation */
1310 if (resource_size(res) == 0)
1311 return -ENXIO;
1312 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1313}
Dan Williams5cf81ce2020-07-20 15:08:13 -07001314static DEVICE_ATTR_ADMIN_RO(resource);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001315
Dan Williamsf979b132017-06-04 12:12:07 +09001316static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
Vishal Vermafcae6952015-06-25 04:22:39 -04001317 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001318
Dan Williamsf979b132017-06-04 12:12:07 +09001319static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1320
Dan Williams1b40e092015-05-01 13:34:01 -04001321static ssize_t sector_size_show(struct device *dev,
1322 struct device_attribute *attr, char *buf)
1323{
Dan Williamsf979b132017-06-04 12:12:07 +09001324 if (is_namespace_blk(dev)) {
1325 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001326
Dan Williamsb2c48f92017-08-11 17:36:54 -07001327 return nd_size_select_show(nsblk->lbasize,
Dan Williamsf979b132017-06-04 12:12:07 +09001328 blk_lbasize_supported, buf);
1329 }
Dan Williams1b40e092015-05-01 13:34:01 -04001330
Dan Williamsf979b132017-06-04 12:12:07 +09001331 if (is_namespace_pmem(dev)) {
1332 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1333
Dan Williamsb2c48f92017-08-11 17:36:54 -07001334 return nd_size_select_show(nspm->lbasize,
Dan Williamsf979b132017-06-04 12:12:07 +09001335 pmem_lbasize_supported, buf);
1336 }
1337 return -ENXIO;
Dan Williams1b40e092015-05-01 13:34:01 -04001338}
1339
1340static ssize_t sector_size_store(struct device *dev,
1341 struct device_attribute *attr, const char *buf, size_t len)
1342{
Dan Williamsf524bf22015-05-30 12:36:02 -04001343 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsf979b132017-06-04 12:12:07 +09001344 const unsigned long *supported;
1345 unsigned long *lbasize;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001346 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001347
Dan Williamsf979b132017-06-04 12:12:07 +09001348 if (is_namespace_blk(dev)) {
1349 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1350
1351 lbasize = &nsblk->lbasize;
1352 supported = blk_lbasize_supported;
1353 } else if (is_namespace_pmem(dev)) {
1354 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1355
1356 lbasize = &nspm->lbasize;
1357 supported = pmem_lbasize_supported;
1358 } else
Dan Williams1b40e092015-05-01 13:34:01 -04001359 return -ENXIO;
1360
Dan Williams87a30e12019-07-17 18:08:26 -07001361 nd_device_lock(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001362 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001363 if (to_ndns(dev)->claim)
1364 rc = -EBUSY;
1365 if (rc >= 0)
Dan Williamsb2c48f92017-08-11 17:36:54 -07001366 rc = nd_size_select_store(dev, buf, lbasize, supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001367 if (rc >= 0)
1368 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -08001369 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1370 buf, buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001371 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001372 nd_device_unlock(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001373
1374 return rc ? rc : len;
1375}
1376static DEVICE_ATTR_RW(sector_size);
1377
Dan Williams0ba1c632015-05-30 12:35:36 -04001378static ssize_t dpa_extents_show(struct device *dev,
1379 struct device_attribute *attr, char *buf)
1380{
1381 struct nd_region *nd_region = to_nd_region(dev->parent);
1382 struct nd_label_id label_id;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001383 uuid_t *uuid = NULL;
Dan Williams0ba1c632015-05-30 12:35:36 -04001384 int count = 0, i;
Dan Williams0ba1c632015-05-30 12:35:36 -04001385 u32 flags = 0;
1386
1387 nvdimm_bus_lock(dev);
1388 if (is_namespace_pmem(dev)) {
1389 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1390
1391 uuid = nspm->uuid;
1392 flags = 0;
1393 } else if (is_namespace_blk(dev)) {
1394 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1395
1396 uuid = nsblk->uuid;
1397 flags = NSLABEL_FLAG_LOCAL;
1398 }
1399
1400 if (!uuid)
1401 goto out;
1402
1403 nd_label_gen_id(&label_id, uuid, flags);
1404 for (i = 0; i < nd_region->ndr_mappings; i++) {
1405 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1406 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1407 struct resource *res;
1408
1409 for_each_dpa_resource(ndd, res)
1410 if (strcmp(res->name, label_id.id) == 0)
1411 count++;
1412 }
1413 out:
1414 nvdimm_bus_unlock(dev);
1415
1416 return sprintf(buf, "%d\n", count);
1417}
1418static DEVICE_ATTR_RO(dpa_extents);
1419
Vishal Verma14e49452017-06-28 14:25:00 -06001420static int btt_claim_class(struct device *dev)
1421{
1422 struct nd_region *nd_region = to_nd_region(dev->parent);
1423 int i, loop_bitmask = 0;
1424
1425 for (i = 0; i < nd_region->ndr_mappings; i++) {
1426 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1427 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1428 struct nd_namespace_index *nsindex;
1429
Dan Williams33a56082017-09-18 14:48:58 -07001430 /*
1431 * If any of the DIMMs do not support labels the only
1432 * possible BTT format is v1.
1433 */
1434 if (!ndd) {
1435 loop_bitmask = 0;
1436 break;
1437 }
1438
Vishal Verma14e49452017-06-28 14:25:00 -06001439 nsindex = to_namespace_index(ndd, ndd->ns_current);
1440 if (nsindex == NULL)
1441 loop_bitmask |= 1;
1442 else {
1443 /* check whether existing labels are v1.1 or v1.2 */
1444 if (__le16_to_cpu(nsindex->major) == 1
1445 && __le16_to_cpu(nsindex->minor) == 1)
1446 loop_bitmask |= 2;
1447 else
1448 loop_bitmask |= 4;
1449 }
1450 }
1451 /*
1452 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1453 * block is found, a v1.1 label for any mapping will set bit 1, and a
1454 * v1.2 label will set bit 2.
1455 *
1456 * At the end of the loop, at most one of the three bits must be set.
1457 * If multiple bits were set, it means the different mappings disagree
1458 * about their labels, and this must be cleaned up first.
1459 *
1460 * If all the label index blocks are found to agree, nsindex of NULL
1461 * implies labels haven't been initialized yet, and when they will,
1462 * they will be of the 1.2 format, so we can assume BTT2.0
1463 *
1464 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1465 * found, we enforce BTT2.0
1466 *
1467 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1468 */
1469 switch (loop_bitmask) {
1470 case 0:
1471 case 2:
1472 return NVDIMM_CCLASS_BTT;
1473 case 1:
1474 case 4:
1475 return NVDIMM_CCLASS_BTT2;
1476 default:
1477 return -ENXIO;
1478 }
1479}
1480
Dan Williams8c2f7e82015-06-25 04:20:04 -04001481static ssize_t holder_show(struct device *dev,
1482 struct device_attribute *attr, char *buf)
1483{
1484 struct nd_namespace_common *ndns = to_ndns(dev);
1485 ssize_t rc;
1486
Dan Williams87a30e12019-07-17 18:08:26 -07001487 nd_device_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001488 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
Dan Williams87a30e12019-07-17 18:08:26 -07001489 nd_device_unlock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001490
1491 return rc;
1492}
1493static DEVICE_ATTR_RO(holder);
1494
Ira Weinyab84b772019-11-14 19:06:34 -08001495static int __holder_class_store(struct device *dev, const char *buf)
Dan Williamsb3fde742017-06-04 10:18:39 +09001496{
1497 struct nd_namespace_common *ndns = to_ndns(dev);
1498
1499 if (dev->driver || ndns->claim)
1500 return -EBUSY;
1501
Ira Weinyab84b772019-11-14 19:06:34 -08001502 if (sysfs_streq(buf, "btt")) {
1503 int rc = btt_claim_class(dev);
1504
1505 if (rc < NVDIMM_CCLASS_NONE)
1506 return rc;
1507 ndns->claim_class = rc;
1508 } else if (sysfs_streq(buf, "pfn"))
Dan Williamsb3fde742017-06-04 10:18:39 +09001509 ndns->claim_class = NVDIMM_CCLASS_PFN;
Dan Williams075c3fd2019-03-04 12:14:04 -08001510 else if (sysfs_streq(buf, "dax"))
Dan Williamsb3fde742017-06-04 10:18:39 +09001511 ndns->claim_class = NVDIMM_CCLASS_DAX;
Dan Williams075c3fd2019-03-04 12:14:04 -08001512 else if (sysfs_streq(buf, ""))
Dan Williamsb3fde742017-06-04 10:18:39 +09001513 ndns->claim_class = NVDIMM_CCLASS_NONE;
1514 else
1515 return -EINVAL;
1516
1517 return 0;
1518}
1519
1520static ssize_t holder_class_store(struct device *dev,
1521 struct device_attribute *attr, const char *buf, size_t len)
1522{
1523 struct nd_region *nd_region = to_nd_region(dev->parent);
Ira Weinyab84b772019-11-14 19:06:34 -08001524 int rc;
Dan Williamsb3fde742017-06-04 10:18:39 +09001525
Dan Williams87a30e12019-07-17 18:08:26 -07001526 nd_device_lock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001527 nvdimm_bus_lock(dev);
1528 wait_nvdimm_bus_probe_idle(dev);
1529 rc = __holder_class_store(dev, buf);
1530 if (rc >= 0)
1531 rc = nd_namespace_label_update(nd_region, dev);
Ira Weinyab84b772019-11-14 19:06:34 -08001532 dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
Dan Williamsb3fde742017-06-04 10:18:39 +09001533 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001534 nd_device_unlock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001535
1536 return rc < 0 ? rc : len;
1537}
1538
1539static ssize_t holder_class_show(struct device *dev,
1540 struct device_attribute *attr, char *buf)
1541{
1542 struct nd_namespace_common *ndns = to_ndns(dev);
1543 ssize_t rc;
1544
Dan Williams87a30e12019-07-17 18:08:26 -07001545 nd_device_lock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001546 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1547 rc = sprintf(buf, "\n");
Vishal Verma14e49452017-06-28 14:25:00 -06001548 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1549 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
Dan Williamsb3fde742017-06-04 10:18:39 +09001550 rc = sprintf(buf, "btt\n");
1551 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1552 rc = sprintf(buf, "pfn\n");
1553 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1554 rc = sprintf(buf, "dax\n");
1555 else
1556 rc = sprintf(buf, "<unknown>\n");
Dan Williams87a30e12019-07-17 18:08:26 -07001557 nd_device_unlock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001558
1559 return rc;
1560}
1561static DEVICE_ATTR_RW(holder_class);
1562
Dan Williams0731de02015-12-14 15:34:15 -08001563static ssize_t mode_show(struct device *dev,
1564 struct device_attribute *attr, char *buf)
1565{
1566 struct nd_namespace_common *ndns = to_ndns(dev);
1567 struct device *claim;
1568 char *mode;
1569 ssize_t rc;
1570
Dan Williams87a30e12019-07-17 18:08:26 -07001571 nd_device_lock(dev);
Dan Williams0731de02015-12-14 15:34:15 -08001572 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001573 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001574 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001575 else if (claim && is_nd_pfn(claim))
1576 mode = "memory";
Dan Williamscd034122016-03-11 10:15:36 -08001577 else if (claim && is_nd_dax(claim))
1578 mode = "dax";
Dan Williams9c412422016-01-23 15:34:10 -08001579 else if (!claim && pmem_should_map_pages(dev))
1580 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001581 else
1582 mode = "raw";
1583 rc = sprintf(buf, "%s\n", mode);
Dan Williams87a30e12019-07-17 18:08:26 -07001584 nd_device_unlock(dev);
Dan Williams0731de02015-12-14 15:34:15 -08001585
1586 return rc;
1587}
1588static DEVICE_ATTR_RO(mode);
1589
Dan Williams8c2f7e82015-06-25 04:20:04 -04001590static ssize_t force_raw_store(struct device *dev,
1591 struct device_attribute *attr, const char *buf, size_t len)
1592{
1593 bool force_raw;
1594 int rc = strtobool(buf, &force_raw);
1595
1596 if (rc)
1597 return rc;
1598
1599 to_ndns(dev)->force_raw = force_raw;
1600 return len;
1601}
1602
1603static ssize_t force_raw_show(struct device *dev,
1604 struct device_attribute *attr, char *buf)
1605{
1606 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1607}
1608static DEVICE_ATTR_RW(force_raw);
1609
Dan Williams3d880022015-05-31 15:02:11 -04001610static struct attribute *nd_namespace_attributes[] = {
1611 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001612 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001613 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001614 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001615 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001616 &dev_attr_resource.attr,
1617 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001618 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001619 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001620 &dev_attr_dpa_extents.attr,
Dan Williamsb3fde742017-06-04 10:18:39 +09001621 &dev_attr_holder_class.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001622 NULL,
1623};
1624
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001625static umode_t namespace_visible(struct kobject *kobj,
1626 struct attribute *a, int n)
1627{
1628 struct device *dev = container_of(kobj, struct device, kobj);
1629
Dan Williamsbfd2e912019-11-12 17:13:14 -08001630 if (a == &dev_attr_resource.attr && is_namespace_blk(dev))
1631 return 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001632
1633 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1634 if (a == &dev_attr_size.attr)
Fabian Frederickb44fe762016-12-04 10:54:08 -08001635 return 0644;
Dan Williams1b40e092015-05-01 13:34:01 -04001636
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001637 return a->mode;
1638 }
1639
Dan Williams13f445d2021-01-12 23:35:50 -08001640 /* base is_namespace_io() attributes */
1641 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr ||
1642 a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr ||
1643 a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr ||
1644 a == &dev_attr_resource.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001645 return a->mode;
1646
1647 return 0;
1648}
1649
Dan Williams3d880022015-05-31 15:02:11 -04001650static struct attribute_group nd_namespace_attribute_group = {
1651 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001652 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001653};
1654
1655static const struct attribute_group *nd_namespace_attribute_groups[] = {
1656 &nd_device_attribute_group,
1657 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001658 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001659 NULL,
1660};
1661
Dan Williams78c81cc2019-11-06 19:56:41 -08001662static const struct device_type namespace_io_device_type = {
1663 .name = "nd_namespace_io",
1664 .release = namespace_io_release,
1665 .groups = nd_namespace_attribute_groups,
1666};
1667
1668static const struct device_type namespace_pmem_device_type = {
1669 .name = "nd_namespace_pmem",
1670 .release = namespace_pmem_release,
1671 .groups = nd_namespace_attribute_groups,
1672};
1673
1674static const struct device_type namespace_blk_device_type = {
1675 .name = "nd_namespace_blk",
1676 .release = namespace_blk_release,
1677 .groups = nd_namespace_attribute_groups,
1678};
1679
1680static bool is_namespace_pmem(const struct device *dev)
1681{
1682 return dev ? dev->type == &namespace_pmem_device_type : false;
1683}
1684
1685static bool is_namespace_blk(const struct device *dev)
1686{
1687 return dev ? dev->type == &namespace_blk_device_type : false;
1688}
1689
1690static bool is_namespace_io(const struct device *dev)
1691{
1692 return dev ? dev->type == &namespace_io_device_type : false;
1693}
1694
Dan Williams8c2f7e82015-06-25 04:20:04 -04001695struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1696{
1697 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001698 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williamscd034122016-03-11 10:15:36 -08001699 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001700 struct nd_namespace_common *ndns = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001701 resource_size_t size;
1702
Dan Williamscd034122016-03-11 10:15:36 -08001703 if (nd_btt || nd_pfn || nd_dax) {
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001704 if (nd_btt)
Dan Williamse1455742015-07-30 17:57:47 -04001705 ndns = nd_btt->ndns;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001706 else if (nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -04001707 ndns = nd_pfn->ndns;
Dan Williamscd034122016-03-11 10:15:36 -08001708 else if (nd_dax)
1709 ndns = nd_dax->nd_pfn.ndns;
Dan Williamse1455742015-07-30 17:57:47 -04001710
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001711 if (!ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001712 return ERR_PTR(-ENODEV);
1713
1714 /*
1715 * Flush any in-progess probes / removals in the driver
1716 * for the raw personality of this namespace.
1717 */
Dan Williams87a30e12019-07-17 18:08:26 -07001718 nd_device_lock(&ndns->dev);
1719 nd_device_unlock(&ndns->dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001720 if (ndns->dev.driver) {
1721 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001722 dev_name(dev));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001723 return ERR_PTR(-EBUSY);
1724 }
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001725 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001726 "host (%s) vs claim (%s) mismatch\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001727 dev_name(dev),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001728 dev_name(ndns->claim)))
1729 return ERR_PTR(-ENXIO);
1730 } else {
1731 ndns = to_ndns(dev);
1732 if (ndns->claim) {
1733 dev_dbg(dev, "claimed by %s, failing probe\n",
1734 dev_name(ndns->claim));
1735
1736 return ERR_PTR(-ENXIO);
1737 }
1738 }
1739
Dan Williams08e6b3c2018-06-13 09:08:36 -07001740 if (nvdimm_namespace_locked(ndns))
1741 return ERR_PTR(-EACCES);
1742
Dan Williams8c2f7e82015-06-25 04:20:04 -04001743 size = nvdimm_namespace_capacity(ndns);
1744 if (size < ND_MIN_NAMESPACE_SIZE) {
1745 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1746 &size, ND_MIN_NAMESPACE_SIZE);
1747 return ERR_PTR(-ENODEV);
1748 }
1749
Dan Williams6acd7d52020-02-27 21:39:23 -08001750 /*
1751 * Note, alignment validation for fsdax and devdax mode
1752 * namespaces happens in nd_pfn_validate() where infoblock
1753 * padding parameters can be applied.
1754 */
1755 if (pmem_should_map_pages(dev)) {
1756 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
1757 struct resource *res = &nsio->res;
1758
1759 if (!IS_ALIGNED(res->start | (res->end + 1),
1760 memremap_compat_align())) {
1761 dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
1762 return ERR_PTR(-EOPNOTSUPP);
1763 }
1764 }
1765
Dan Williams8c2f7e82015-06-25 04:20:04 -04001766 if (is_namespace_pmem(&ndns->dev)) {
1767 struct nd_namespace_pmem *nspm;
1768
1769 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001770 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001771 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001772 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001773 struct nd_namespace_blk *nsblk;
1774
1775 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001776 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1777 return ERR_PTR(-ENODEV);
1778 if (!nsblk->lbasize) {
Dan Williams426824d2018-03-05 16:39:31 -08001779 dev_dbg(&ndns->dev, "sector size not set\n");
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001780 return ERR_PTR(-ENODEV);
1781 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001782 if (!nd_namespace_blk_validate(nsblk))
1783 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001784 }
1785
1786 return ndns;
1787}
1788EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1789
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +05301790int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1791 resource_size_t size)
1792{
1793 if (is_namespace_blk(&ndns->dev))
1794 return 0;
1795 return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1796}
1797EXPORT_SYMBOL_GPL(devm_namespace_enable);
1798
1799void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1800{
1801 if (is_namespace_blk(&ndns->dev))
1802 return;
1803 devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1804}
1805EXPORT_SYMBOL_GPL(devm_namespace_disable);
1806
Dan Williams3d880022015-05-31 15:02:11 -04001807static struct device **create_namespace_io(struct nd_region *nd_region)
1808{
1809 struct nd_namespace_io *nsio;
1810 struct device *dev, **devs;
1811 struct resource *res;
1812
1813 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1814 if (!nsio)
1815 return NULL;
1816
1817 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1818 if (!devs) {
1819 kfree(nsio);
1820 return NULL;
1821 }
1822
Dan Williams8c2f7e82015-06-25 04:20:04 -04001823 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001824 dev->type = &namespace_io_device_type;
1825 dev->parent = &nd_region->dev;
1826 res = &nsio->res;
1827 res->name = dev_name(&nd_region->dev);
1828 res->flags = IORESOURCE_MEM;
1829 res->start = nd_region->ndr_start;
1830 res->end = res->start + nd_region->ndr_size - 1;
1831
1832 devs[0] = dev;
1833 return devs;
1834}
1835
Dan Williamsd1c6e082021-09-08 22:11:37 -07001836static bool has_uuid_at_pos(struct nd_region *nd_region, const uuid_t *uuid,
1837 u64 cookie, u16 pos)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001838{
1839 struct nd_namespace_label *found = NULL;
1840 int i;
1841
1842 for (i = 0; i < nd_region->ndr_mappings; i++) {
1843 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsfaec6f82017-06-06 11:10:51 -07001844 struct nd_interleave_set *nd_set = nd_region->nd_set;
1845 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001846 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001847 bool found_uuid = false;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001848
Dan Williamsae8219f2016-09-19 16:04:21 -07001849 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1850 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams8172db92021-09-08 22:11:42 -07001851 u16 position;
Dan Williamsae8219f2016-09-19 16:04:21 -07001852
1853 if (!nd_label)
1854 continue;
Dan Williamsb4366a82021-08-24 09:05:30 -07001855 position = nsl_get_position(ndd, nd_label);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001856
Dan Williams9761b022021-08-24 09:05:35 -07001857 if (!nsl_validate_isetcookie(ndd, nd_label, cookie))
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001858 continue;
1859
Dan Williamsd1c6e082021-09-08 22:11:37 -07001860 if (!nsl_uuid_equal(ndd, nd_label, uuid))
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001861 continue;
1862
Dan Williams8b03aa02021-08-24 09:06:02 -07001863 if (!nsl_validate_type_guid(ndd, nd_label,
1864 &nd_set->type_guid))
Dan Williamsfaec6f82017-06-06 11:10:51 -07001865 continue;
Dan Williamsfaec6f82017-06-06 11:10:51 -07001866
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001867 if (found_uuid) {
Dan Williams426824d2018-03-05 16:39:31 -08001868 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001869 return false;
1870 }
1871 found_uuid = true;
Dan Williams8172db92021-09-08 22:11:42 -07001872 if (!nsl_validate_nlabel(nd_region, ndd, nd_label))
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001873 continue;
1874 if (position != pos)
1875 continue;
1876 found = nd_label;
1877 break;
1878 }
1879 if (found)
1880 break;
1881 }
1882 return found != NULL;
1883}
1884
Dan Williamsd1c6e082021-09-08 22:11:37 -07001885static int select_pmem_id(struct nd_region *nd_region, const uuid_t *pmem_id)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001886{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001887 int i;
1888
1889 if (!pmem_id)
1890 return -ENODEV;
1891
1892 for (i = 0; i < nd_region->ndr_mappings; i++) {
1893 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams0e3b0d12016-10-06 23:13:15 -07001894 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001895 struct nd_namespace_label *nd_label = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001896 u64 hw_start, hw_end, pmem_start, pmem_end;
Dan Williamsae8219f2016-09-19 16:04:21 -07001897 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001898
Dan Williams9cf8bd52016-12-15 20:04:31 -08001899 lockdep_assert_held(&nd_mapping->lock);
Dan Williamsae8219f2016-09-19 16:04:21 -07001900 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1901 nd_label = label_ent->label;
1902 if (!nd_label)
1903 continue;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001904 if (nsl_uuid_equal(ndd, nd_label, pmem_id))
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001905 break;
Dan Williamsae8219f2016-09-19 16:04:21 -07001906 nd_label = NULL;
1907 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001908
1909 if (!nd_label) {
1910 WARN_ON(1);
1911 return -EINVAL;
1912 }
1913
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001914 /*
1915 * Check that this label is compliant with the dpa
1916 * range published in NFIT
1917 */
1918 hw_start = nd_mapping->start;
1919 hw_end = hw_start + nd_mapping->size;
Dan Williamsb4366a82021-08-24 09:05:30 -07001920 pmem_start = nsl_get_dpa(ndd, nd_label);
1921 pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label);
Dan Williams0e3b0d12016-10-06 23:13:15 -07001922 if (pmem_start >= hw_start && pmem_start < hw_end
1923 && pmem_end <= hw_end && pmem_end > hw_start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001924 /* pass */;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001925 else {
1926 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
Dan Williamsd1c6e082021-09-08 22:11:37 -07001927 dev_name(ndd->dev),
1928 nsl_uuid_raw(ndd, nd_label));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001929 return -EINVAL;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001930 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001931
Dan Williams8a5f50d2016-09-22 15:42:59 -07001932 /* move recently validated label to the front of the list */
1933 list_move(&label_ent->list, &nd_mapping->labels);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001934 }
1935 return 0;
1936}
1937
1938/**
Dan Williams8a5f50d2016-09-22 15:42:59 -07001939 * create_namespace_pmem - validate interleave set labelling, retrieve label0
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001940 * @nd_region: region with mappings to validate
Dan Williams8a5f50d2016-09-22 15:42:59 -07001941 * @nspm: target namespace to create
1942 * @nd_label: target pmem namespace label to evaluate
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001943 */
Colin Ian King65853a12017-10-05 10:55:57 +01001944static struct device *create_namespace_pmem(struct nd_region *nd_region,
Dan Williamsb4366a82021-08-24 09:05:30 -07001945 struct nd_mapping *nd_mapping,
1946 struct nd_namespace_label *nd_label)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001947{
Dan Williamsb4366a82021-08-24 09:05:30 -07001948 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1949 struct nd_namespace_index *nsindex =
1950 to_namespace_index(ndd, ndd->ns_current);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001951 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
Dan Williams86ef58a2017-02-28 18:32:48 -08001952 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07001953 struct nd_label_ent *label_ent;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001954 struct nd_namespace_pmem *nspm;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001955 resource_size_t size = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001956 struct resource *res;
1957 struct device *dev;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001958 uuid_t uuid;
Dan Williamsae8219f2016-09-19 16:04:21 -07001959 int rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001960 u16 i;
1961
Dan Williams47652182016-09-15 18:08:05 -07001962 if (cookie == 0) {
1963 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
Dan Williams8a5f50d2016-09-22 15:42:59 -07001964 return ERR_PTR(-ENXIO);
Dan Williams47652182016-09-15 18:08:05 -07001965 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001966
Dan Williams9761b022021-08-24 09:05:35 -07001967 if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07001968 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
Dan Williamsd1c6e082021-09-08 22:11:37 -07001969 nsl_uuid_raw(ndd, nd_label));
Dan Williams9761b022021-08-24 09:05:35 -07001970 if (!nsl_validate_isetcookie(ndd, nd_label, altcookie))
Dan Williams86ef58a2017-02-28 18:32:48 -08001971 return ERR_PTR(-EAGAIN);
1972
1973 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
Dan Williamsd1c6e082021-09-08 22:11:37 -07001974 nsl_uuid_raw(ndd, nd_label));
Dan Williamsae8219f2016-09-19 16:04:21 -07001975 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001976
Dan Williams8a5f50d2016-09-22 15:42:59 -07001977 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1978 if (!nspm)
1979 return ERR_PTR(-ENOMEM);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001980
Dan Williams0e3b0d12016-10-06 23:13:15 -07001981 nspm->id = -1;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001982 dev = &nspm->nsio.common.dev;
1983 dev->type = &namespace_pmem_device_type;
1984 dev->parent = &nd_region->dev;
1985 res = &nspm->nsio.res;
1986 res->name = dev_name(&nd_region->dev);
1987 res->flags = IORESOURCE_MEM;
1988
Dan Williams86ef58a2017-02-28 18:32:48 -08001989 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsd1c6e082021-09-08 22:11:37 -07001990 uuid_t uuid;
1991
1992 nsl_get_uuid(ndd, nd_label, &uuid);
1993 if (has_uuid_at_pos(nd_region, &uuid, cookie, i))
Dan Williams86ef58a2017-02-28 18:32:48 -08001994 continue;
Dan Williamsd1c6e082021-09-08 22:11:37 -07001995 if (has_uuid_at_pos(nd_region, &uuid, altcookie, i))
Dan Williams86ef58a2017-02-28 18:32:48 -08001996 continue;
1997 break;
1998 }
1999
Dan Williams8a5f50d2016-09-22 15:42:59 -07002000 if (i < nd_region->ndr_mappings) {
Dan Williams4f867222018-04-06 16:37:21 -07002001 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002002
Dan Williams8a5f50d2016-09-22 15:42:59 -07002003 /*
2004 * Give up if we don't find an instance of a uuid at each
2005 * position (from 0 to nd_region->ndr_mappings - 1), or if we
2006 * find a dimm with two instances of the same uuid.
2007 */
Dan Williams0e3b0d12016-10-06 23:13:15 -07002008 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
Dan Williamsd1c6e082021-09-08 22:11:37 -07002009 nvdimm_name(nvdimm), nsl_uuid_raw(ndd, nd_label));
Dan Williams8a5f50d2016-09-22 15:42:59 -07002010 rc = -EINVAL;
Dan Williamsae8219f2016-09-19 16:04:21 -07002011 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002012 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002013
2014 /*
2015 * Fix up each mapping's 'labels' to have the validated pmem label for
2016 * that position at labels[0], and NULL at labels[1]. In the process,
2017 * check that the namespace aligns with interleave-set. We know
2018 * that it does not overlap with any blk namespaces by virtue of
2019 * the dimm being enabled (i.e. nd_label_reserve_dpa()
2020 * succeeded).
2021 */
Dan Williamsd1c6e082021-09-08 22:11:37 -07002022 nsl_get_uuid(ndd, nd_label, &uuid);
2023 rc = select_pmem_id(nd_region, &uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002024 if (rc)
2025 goto err;
2026
2027 /* Calculate total size and populate namespace properties from label0 */
2028 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002029 struct nd_namespace_label *label0;
Dan Williamsb3fde742017-06-04 10:18:39 +09002030 struct nvdimm_drvdata *ndd;
Dan Williamsae8219f2016-09-19 16:04:21 -07002031
2032 nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07002033 label_ent = list_first_entry_or_null(&nd_mapping->labels,
2034 typeof(*label_ent), list);
Aneesh Kumar K.V86aa6662019-08-09 13:17:26 +05302035 label0 = label_ent ? label_ent->label : NULL;
Dan Williamsae8219f2016-09-19 16:04:21 -07002036
2037 if (!label0) {
2038 WARN_ON(1);
2039 continue;
2040 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002041
Dan Williamsb4366a82021-08-24 09:05:30 -07002042 ndd = to_ndd(nd_mapping);
2043 size += nsl_get_rawsize(ndd, label0);
2044 if (nsl_get_position(ndd, label0) != 0)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002045 continue;
2046 WARN_ON(nspm->alt_name || nspm->uuid);
Dan Williamsb4366a82021-08-24 09:05:30 -07002047 nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0),
2048 NSLABEL_NAME_LEN, GFP_KERNEL);
Dan Williamsd1c6e082021-09-08 22:11:37 -07002049 nsl_get_uuid(ndd, label0, &uuid);
2050 nspm->uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
Dan Williamsb4366a82021-08-24 09:05:30 -07002051 nspm->lbasize = nsl_get_lbasize(ndd, label0);
Dan Williamsa6e6d722021-08-24 09:06:07 -07002052 nspm->nsio.common.claim_class =
2053 nsl_get_claim_class(ndd, label0);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002054 }
2055
2056 if (!nspm->alt_name || !nspm->uuid) {
2057 rc = -ENOMEM;
2058 goto err;
2059 }
2060
Dan Williams0e3b0d12016-10-06 23:13:15 -07002061 nd_namespace_pmem_set_resource(nd_region, nspm, size);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002062
Dan Williams8a5f50d2016-09-22 15:42:59 -07002063 return dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002064 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002065 namespace_pmem_release(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002066 switch (rc) {
2067 case -EINVAL:
Dan Williams426824d2018-03-05 16:39:31 -08002068 dev_dbg(&nd_region->dev, "invalid label(s)\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002069 break;
2070 case -ENODEV:
Dan Williams426824d2018-03-05 16:39:31 -08002071 dev_dbg(&nd_region->dev, "label not found\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002072 break;
2073 default:
Dan Williams426824d2018-03-05 16:39:31 -08002074 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002075 break;
2076 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002077 return ERR_PTR(rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002078}
2079
Dan Williams1b40e092015-05-01 13:34:01 -04002080struct resource *nsblk_add_resource(struct nd_region *nd_region,
2081 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2082 resource_size_t start)
2083{
2084 struct nd_label_id label_id;
2085 struct resource *res;
2086
2087 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2088 res = krealloc(nsblk->res,
2089 sizeof(void *) * (nsblk->num_resources + 1),
2090 GFP_KERNEL);
2091 if (!res)
2092 return NULL;
2093 nsblk->res = (struct resource **) res;
2094 for_each_dpa_resource(ndd, res)
2095 if (strcmp(res->name, label_id.id) == 0
2096 && res->start == start) {
2097 nsblk->res[nsblk->num_resources++] = res;
2098 return res;
2099 }
2100 return NULL;
2101}
2102
2103static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2104{
2105 struct nd_namespace_blk *nsblk;
2106 struct device *dev;
2107
2108 if (!is_nd_blk(&nd_region->dev))
2109 return NULL;
2110
2111 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2112 if (!nsblk)
2113 return NULL;
2114
Dan Williams8c2f7e82015-06-25 04:20:04 -04002115 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002116 dev->type = &namespace_blk_device_type;
2117 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2118 if (nsblk->id < 0) {
2119 kfree(nsblk);
2120 return NULL;
2121 }
2122 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2123 dev->parent = &nd_region->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002124
Dan Williams8c2f7e82015-06-25 04:20:04 -04002125 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002126}
2127
Dan Williams98a29c32016-09-30 15:28:27 -07002128static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2129{
2130 struct nd_namespace_pmem *nspm;
2131 struct resource *res;
2132 struct device *dev;
2133
Dan Williamsc9e582a2017-05-29 23:12:19 -07002134 if (!is_memory(&nd_region->dev))
Dan Williams98a29c32016-09-30 15:28:27 -07002135 return NULL;
2136
2137 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2138 if (!nspm)
2139 return NULL;
2140
2141 dev = &nspm->nsio.common.dev;
2142 dev->type = &namespace_pmem_device_type;
2143 dev->parent = &nd_region->dev;
2144 res = &nspm->nsio.res;
2145 res->name = dev_name(&nd_region->dev);
2146 res->flags = IORESOURCE_MEM;
2147
2148 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2149 if (nspm->id < 0) {
2150 kfree(nspm);
2151 return NULL;
2152 }
2153 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
Dan Williams98a29c32016-09-30 15:28:27 -07002154 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2155
2156 return dev;
2157}
2158
2159void nd_region_create_ns_seed(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -04002160{
2161 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
Dan Williams98a29c32016-09-30 15:28:27 -07002162
2163 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2164 return;
2165
2166 if (is_nd_blk(&nd_region->dev))
2167 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2168 else
2169 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2170
Dan Williams1b40e092015-05-01 13:34:01 -04002171 /*
2172 * Seed creation failures are not fatal, provisioning is simply
2173 * disabled until memory becomes available
2174 */
2175 if (!nd_region->ns_seed)
Dan Williams98a29c32016-09-30 15:28:27 -07002176 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2177 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
Dan Williams1b40e092015-05-01 13:34:01 -04002178 else
2179 nd_device_register(nd_region->ns_seed);
2180}
2181
Dan Williamscd034122016-03-11 10:15:36 -08002182void nd_region_create_dax_seed(struct nd_region *nd_region)
2183{
2184 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2185 nd_region->dax_seed = nd_dax_create(nd_region);
2186 /*
2187 * Seed creation failures are not fatal, provisioning is simply
2188 * disabled until memory becomes available
2189 */
2190 if (!nd_region->dax_seed)
2191 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2192}
2193
Dan Williams2dc43332015-12-13 11:41:36 -08002194void nd_region_create_pfn_seed(struct nd_region *nd_region)
2195{
2196 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2197 nd_region->pfn_seed = nd_pfn_create(nd_region);
2198 /*
2199 * Seed creation failures are not fatal, provisioning is simply
2200 * disabled until memory becomes available
2201 */
2202 if (!nd_region->pfn_seed)
2203 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2204}
2205
Dan Williams8c2f7e82015-06-25 04:20:04 -04002206void nd_region_create_btt_seed(struct nd_region *nd_region)
2207{
2208 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2209 nd_region->btt_seed = nd_btt_create(nd_region);
2210 /*
2211 * Seed creation failures are not fatal, provisioning is simply
2212 * disabled until memory becomes available
2213 */
2214 if (!nd_region->btt_seed)
2215 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2216}
2217
Dan Williams8a5f50d2016-09-22 15:42:59 -07002218static int add_namespace_resource(struct nd_region *nd_region,
2219 struct nd_namespace_label *nd_label, struct device **devs,
2220 int count)
Dan Williams1b40e092015-05-01 13:34:01 -04002221{
Dan Williams8a5f50d2016-09-22 15:42:59 -07002222 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsae8219f2016-09-19 16:04:21 -07002223 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002224 int i;
2225
2226 for (i = 0; i < count; i++) {
Dan Williamsd1c6e082021-09-08 22:11:37 -07002227 uuid_t *uuid = namespace_to_uuid(devs[i]);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002228 struct resource *res;
2229
Dan Williamsd1c6e082021-09-08 22:11:37 -07002230 if (IS_ERR(uuid)) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07002231 WARN_ON(1);
2232 continue;
2233 }
2234
Dan Williamsd1c6e082021-09-08 22:11:37 -07002235 if (!nsl_uuid_equal(ndd, nd_label, uuid))
Dan Williams8a5f50d2016-09-22 15:42:59 -07002236 continue;
2237 if (is_namespace_blk(devs[i])) {
2238 res = nsblk_add_resource(nd_region, ndd,
2239 to_nd_namespace_blk(devs[i]),
Dan Williamsb4366a82021-08-24 09:05:30 -07002240 nsl_get_dpa(ndd, nd_label));
Dan Williams8a5f50d2016-09-22 15:42:59 -07002241 if (!res)
2242 return -ENXIO;
2243 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2244 } else {
2245 dev_err(&nd_region->dev,
Dan Williamsd1c6e082021-09-08 22:11:37 -07002246 "error: conflicting extents for uuid: %pUb\n",
2247 uuid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002248 return -ENXIO;
2249 }
2250 break;
2251 }
2252
2253 return i;
2254}
2255
Colin Ian King65853a12017-10-05 10:55:57 +01002256static struct device *create_namespace_blk(struct nd_region *nd_region,
Dan Williams8a5f50d2016-09-22 15:42:59 -07002257 struct nd_namespace_label *nd_label, int count)
2258{
2259
2260 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsfaec6f82017-06-06 11:10:51 -07002261 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002262 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002263 struct nd_namespace_blk *nsblk;
Nicolas Iooss238b3232016-11-26 20:18:04 +01002264 char name[NSLABEL_NAME_LEN];
Dan Williams8a5f50d2016-09-22 15:42:59 -07002265 struct device *dev = NULL;
2266 struct resource *res;
Dan Williamsd1c6e082021-09-08 22:11:37 -07002267 uuid_t uuid;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002268
Dan Williams8b03aa02021-08-24 09:06:02 -07002269 if (!nsl_validate_type_guid(ndd, nd_label, &nd_set->type_guid))
2270 return ERR_PTR(-EAGAIN);
Dan Williamsf56541a2021-08-24 09:05:51 -07002271 if (!nsl_validate_blk_isetcookie(ndd, nd_label, nd_set->cookie2))
2272 return ERR_PTR(-EAGAIN);
Dan Williamsfaec6f82017-06-06 11:10:51 -07002273
Dan Williams8a5f50d2016-09-22 15:42:59 -07002274 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2275 if (!nsblk)
2276 return ERR_PTR(-ENOMEM);
2277 dev = &nsblk->common.dev;
2278 dev->type = &namespace_blk_device_type;
2279 dev->parent = &nd_region->dev;
2280 nsblk->id = -1;
Dan Williamsb4366a82021-08-24 09:05:30 -07002281 nsblk->lbasize = nsl_get_lbasize(ndd, nd_label);
Dan Williamsd1c6e082021-09-08 22:11:37 -07002282 nsl_get_uuid(ndd, nd_label, &uuid);
2283 nsblk->uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
Dan Williamsa6e6d722021-08-24 09:06:07 -07002284 nsblk->common.claim_class = nsl_get_claim_class(ndd, nd_label);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002285 if (!nsblk->uuid)
2286 goto blk_err;
Dan Williamsb4366a82021-08-24 09:05:30 -07002287 nsl_get_name(ndd, nd_label, name);
Kangjie Lu55c1fc02019-03-12 03:20:34 -05002288 if (name[0]) {
Dan Williamsb4366a82021-08-24 09:05:30 -07002289 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, GFP_KERNEL);
Kangjie Lu55c1fc02019-03-12 03:20:34 -05002290 if (!nsblk->alt_name)
2291 goto blk_err;
2292 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002293 res = nsblk_add_resource(nd_region, ndd, nsblk,
Dan Williamsb4366a82021-08-24 09:05:30 -07002294 nsl_get_dpa(ndd, nd_label));
Dan Williams8a5f50d2016-09-22 15:42:59 -07002295 if (!res)
2296 goto blk_err;
2297 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2298 return dev;
2299 blk_err:
2300 namespace_blk_release(dev);
2301 return ERR_PTR(-ENXIO);
2302}
2303
Dan Williams6ff3e912016-10-05 14:04:15 -07002304static int cmp_dpa(const void *a, const void *b)
2305{
2306 const struct device *dev_a = *(const struct device **) a;
2307 const struct device *dev_b = *(const struct device **) b;
2308 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2309 struct nd_namespace_pmem *nspm_a, *nspm_b;
2310
2311 if (is_namespace_io(dev_a))
2312 return 0;
2313
2314 if (is_namespace_blk(dev_a)) {
2315 nsblk_a = to_nd_namespace_blk(dev_a);
2316 nsblk_b = to_nd_namespace_blk(dev_b);
2317
2318 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2319 sizeof(resource_size_t));
2320 }
2321
2322 nspm_a = to_nd_namespace_pmem(dev_a);
2323 nspm_b = to_nd_namespace_pmem(dev_b);
2324
2325 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2326 sizeof(resource_size_t));
2327}
2328
Dan Williams8a5f50d2016-09-22 15:42:59 -07002329static struct device **scan_labels(struct nd_region *nd_region)
2330{
Dan Williamsc969e242016-10-05 15:54:46 -07002331 int i, count = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002332 struct device *dev, **devs = NULL;
2333 struct nd_label_ent *label_ent, *e;
Dan Williamsc969e242016-10-05 15:54:46 -07002334 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsb4366a82021-08-24 09:05:30 -07002335 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsc969e242016-10-05 15:54:46 -07002336 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -04002337
Dan Williams8a5f50d2016-09-22 15:42:59 -07002338 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2339 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002340 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams1b40e092015-05-01 13:34:01 -04002341 struct device **__devs;
Dan Williamsae8219f2016-09-19 16:04:21 -07002342 u32 flags;
Dan Williams1b40e092015-05-01 13:34:01 -04002343
Dan Williamsae8219f2016-09-19 16:04:21 -07002344 if (!nd_label)
2345 continue;
Dan Williamsb4366a82021-08-24 09:05:30 -07002346 flags = nsl_get_flags(ndd, nd_label);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002347 if (is_nd_blk(&nd_region->dev)
2348 == !!(flags & NSLABEL_FLAG_LOCAL))
2349 /* pass, region matches label type */;
Dan Williams1b40e092015-05-01 13:34:01 -04002350 else
2351 continue;
2352
Dan Williamsc969e242016-10-05 15:54:46 -07002353 /* skip labels that describe extents outside of the region */
Dan Williamsb4366a82021-08-24 09:05:30 -07002354 if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start ||
2355 nsl_get_dpa(ndd, nd_label) > map_end)
2356 continue;
Dan Williamsc969e242016-10-05 15:54:46 -07002357
Dan Williams8a5f50d2016-09-22 15:42:59 -07002358 i = add_namespace_resource(nd_region, nd_label, devs, count);
2359 if (i < 0)
2360 goto err;
Dan Williams1b40e092015-05-01 13:34:01 -04002361 if (i < count)
2362 continue;
2363 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2364 if (!__devs)
2365 goto err;
2366 memcpy(__devs, devs, sizeof(dev) * count);
2367 kfree(devs);
2368 devs = __devs;
2369
Dan Williamsfaec6f82017-06-06 11:10:51 -07002370 if (is_nd_blk(&nd_region->dev))
Dan Williams8a5f50d2016-09-22 15:42:59 -07002371 dev = create_namespace_blk(nd_region, nd_label, count);
Dan Williamsb4366a82021-08-24 09:05:30 -07002372 else
2373 dev = create_namespace_pmem(nd_region, nd_mapping,
2374 nd_label);
Dan Williamsfaec6f82017-06-06 11:10:51 -07002375
2376 if (IS_ERR(dev)) {
2377 switch (PTR_ERR(dev)) {
2378 case -EAGAIN:
2379 /* skip invalid labels */
2380 continue;
2381 case -ENODEV:
2382 /* fallthrough to seed creation */
2383 break;
2384 default:
2385 goto err;
2386 }
2387 } else
2388 devs[count++] = dev;
2389
Dan Williams1b40e092015-05-01 13:34:01 -04002390 }
2391
Dan Williams426824d2018-03-05 16:39:31 -08002392 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2393 count, is_nd_blk(&nd_region->dev)
Dan Williams8a5f50d2016-09-22 15:42:59 -07002394 ? "blk" : "pmem", count == 1 ? "" : "s");
Dan Williams1b40e092015-05-01 13:34:01 -04002395
2396 if (count == 0) {
2397 /* Publish a zero-sized namespace for userspace to configure. */
Dan Williamsae8219f2016-09-19 16:04:21 -07002398 nd_mapping_free_labels(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002399
2400 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2401 if (!devs)
2402 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002403 if (is_nd_blk(&nd_region->dev)) {
2404 struct nd_namespace_blk *nsblk;
2405
2406 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2407 if (!nsblk)
2408 goto err;
2409 dev = &nsblk->common.dev;
2410 dev->type = &namespace_blk_device_type;
2411 } else {
2412 struct nd_namespace_pmem *nspm;
2413
2414 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2415 if (!nspm)
2416 goto err;
2417 dev = &nspm->nsio.common.dev;
2418 dev->type = &namespace_pmem_device_type;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002419 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002420 }
Dan Williams1b40e092015-05-01 13:34:01 -04002421 dev->parent = &nd_region->dev;
2422 devs[count++] = dev;
Dan Williamsc9e582a2017-05-29 23:12:19 -07002423 } else if (is_memory(&nd_region->dev)) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07002424 /* clean unselected labels */
2425 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07002426 struct list_head *l, *e;
2427 LIST_HEAD(list);
2428 int j;
2429
Dan Williams8a5f50d2016-09-22 15:42:59 -07002430 nd_mapping = &nd_region->mapping[i];
2431 if (list_empty(&nd_mapping->labels)) {
2432 WARN_ON(1);
2433 continue;
2434 }
Dan Williams0e3b0d12016-10-06 23:13:15 -07002435
2436 j = count;
2437 list_for_each_safe(l, e, &nd_mapping->labels) {
2438 if (!j--)
2439 break;
2440 list_move_tail(l, &list);
2441 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002442 nd_mapping_free_labels(nd_mapping);
Dan Williams0e3b0d12016-10-06 23:13:15 -07002443 list_splice_init(&list, &nd_mapping->labels);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002444 }
Dan Williams1b40e092015-05-01 13:34:01 -04002445 }
2446
Dan Williams6ff3e912016-10-05 14:04:15 -07002447 if (count > 1)
2448 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2449
Dan Williams1b40e092015-05-01 13:34:01 -04002450 return devs;
2451
Dan Williamsae8219f2016-09-19 16:04:21 -07002452 err:
Dan Carpenter75d29712016-10-12 09:34:29 +03002453 if (devs) {
2454 for (i = 0; devs[i]; i++)
2455 if (is_nd_blk(&nd_region->dev))
2456 namespace_blk_release(devs[i]);
2457 else
2458 namespace_pmem_release(devs[i]);
2459 kfree(devs);
2460 }
Dan Williams1b40e092015-05-01 13:34:01 -04002461 return NULL;
2462}
2463
Dan Williams8a5f50d2016-09-22 15:42:59 -07002464static struct device **create_namespaces(struct nd_region *nd_region)
Dan Williamsae8219f2016-09-19 16:04:21 -07002465{
Colin Ian King59858d32018-01-30 17:47:07 +00002466 struct nd_mapping *nd_mapping;
Dan Williamsae8219f2016-09-19 16:04:21 -07002467 struct device **devs;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002468 int i;
Dan Williamsae8219f2016-09-19 16:04:21 -07002469
2470 if (nd_region->ndr_mappings == 0)
2471 return NULL;
2472
Dan Williams8a5f50d2016-09-22 15:42:59 -07002473 /* lock down all mappings while we scan labels */
2474 for (i = 0; i < nd_region->ndr_mappings; i++) {
2475 nd_mapping = &nd_region->mapping[i];
2476 mutex_lock_nested(&nd_mapping->lock, i);
2477 }
2478
2479 devs = scan_labels(nd_region);
2480
2481 for (i = 0; i < nd_region->ndr_mappings; i++) {
2482 int reverse = nd_region->ndr_mappings - 1 - i;
2483
2484 nd_mapping = &nd_region->mapping[reverse];
2485 mutex_unlock(&nd_mapping->lock);
2486 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002487
2488 return devs;
2489}
2490
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302491static void deactivate_labels(void *region)
2492{
2493 struct nd_region *nd_region = region;
2494 int i;
2495
2496 for (i = 0; i < nd_region->ndr_mappings; i++) {
2497 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2498 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2499 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2500
2501 mutex_lock(&nd_mapping->lock);
2502 nd_mapping_free_labels(nd_mapping);
2503 mutex_unlock(&nd_mapping->lock);
2504
2505 put_ndd(ndd);
2506 nd_mapping->ndd = NULL;
2507 if (ndd)
2508 atomic_dec(&nvdimm->busy);
2509 }
2510}
2511
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002512static int init_active_labels(struct nd_region *nd_region)
2513{
Dan Williamsd9cee9f2021-07-30 09:46:04 -07002514 int i, rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002515
2516 for (i = 0; i < nd_region->ndr_mappings; i++) {
2517 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2518 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2519 struct nvdimm *nvdimm = nd_mapping->nvdimm;
Dan Williamsae8219f2016-09-19 16:04:21 -07002520 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002521 int count, j;
2522
2523 /*
Dan Williams9d62ed92017-05-04 11:47:22 -07002524 * If the dimm is disabled then we may need to prevent
2525 * the region from being activated.
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002526 */
2527 if (!ndd) {
Dan Williams9d62ed92017-05-04 11:47:22 -07002528 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2529 /* fail, label data may be unreadable */;
Dan Williamsa0e37452020-01-30 12:06:18 -08002530 else if (test_bit(NDD_LABELING, &nvdimm->flags))
Dan Williams9d62ed92017-05-04 11:47:22 -07002531 /* fail, labels needed to disambiguate dpa */;
2532 else
Dan Williamsd9cee9f2021-07-30 09:46:04 -07002533 continue;
Dan Williams9d62ed92017-05-04 11:47:22 -07002534
2535 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2536 dev_name(&nd_mapping->nvdimm->dev),
2537 test_bit(NDD_LOCKED, &nvdimm->flags)
2538 ? "locked" : "disabled");
Dan Williamsd9cee9f2021-07-30 09:46:04 -07002539 rc = -ENXIO;
2540 goto out;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002541 }
2542 nd_mapping->ndd = ndd;
2543 atomic_inc(&nvdimm->busy);
2544 get_ndd(ndd);
2545
2546 count = nd_label_active_count(ndd);
Dan Williams426824d2018-03-05 16:39:31 -08002547 dev_dbg(ndd->dev, "count: %d\n", count);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002548 if (!count)
2549 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002550 for (j = 0; j < count; j++) {
2551 struct nd_namespace_label *label;
2552
Dan Williamsae8219f2016-09-19 16:04:21 -07002553 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2554 if (!label_ent)
2555 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002556 label = nd_label_active(ndd, j);
Dan Williamsd5d30d52019-02-02 16:35:26 -08002557 if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
Dan Williamsb4366a82021-08-24 09:05:30 -07002558 u32 flags = nsl_get_flags(ndd, label);
Dan Williamsd5d30d52019-02-02 16:35:26 -08002559
2560 flags &= ~NSLABEL_FLAG_LOCAL;
Dan Williams8176f142021-08-24 09:05:41 -07002561 nsl_set_flags(ndd, label, flags);
Dan Williamsd5d30d52019-02-02 16:35:26 -08002562 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002563 label_ent->label = label;
2564
2565 mutex_lock(&nd_mapping->lock);
2566 list_add_tail(&label_ent->list, &nd_mapping->labels);
2567 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002568 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002569
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302570 if (j < count)
2571 break;
2572 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002573
Dan Williamsd9cee9f2021-07-30 09:46:04 -07002574 if (i < nd_region->ndr_mappings)
2575 rc = -ENOMEM;
2576
2577out:
2578 if (rc) {
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302579 deactivate_labels(nd_region);
Dan Williamsd9cee9f2021-07-30 09:46:04 -07002580 return rc;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002581 }
2582
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302583 return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
Dan Williamsd9cee9f2021-07-30 09:46:04 -07002584 nd_region);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002585}
2586
Dan Williams3d880022015-05-31 15:02:11 -04002587int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2588{
2589 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002590 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04002591
2592 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002593 nvdimm_bus_lock(&nd_region->dev);
2594 rc = init_active_labels(nd_region);
2595 if (rc) {
2596 nvdimm_bus_unlock(&nd_region->dev);
2597 return rc;
2598 }
2599
2600 type = nd_region_to_nstype(nd_region);
2601 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04002602 case ND_DEVICE_NAMESPACE_IO:
2603 devs = create_namespace_io(nd_region);
2604 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002605 case ND_DEVICE_NAMESPACE_PMEM:
Dan Williams1b40e092015-05-01 13:34:01 -04002606 case ND_DEVICE_NAMESPACE_BLK:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002607 devs = create_namespaces(nd_region);
Dan Williams1b40e092015-05-01 13:34:01 -04002608 break;
Dan Williams3d880022015-05-31 15:02:11 -04002609 default:
2610 break;
2611 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002612 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04002613
2614 if (!devs)
2615 return -ENODEV;
2616
Dan Williams3d880022015-05-31 15:02:11 -04002617 for (i = 0; devs[i]; i++) {
2618 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04002619 int id;
Dan Williams3d880022015-05-31 15:02:11 -04002620
Dan Williams1b40e092015-05-01 13:34:01 -04002621 if (type == ND_DEVICE_NAMESPACE_BLK) {
2622 struct nd_namespace_blk *nsblk;
2623
2624 nsblk = to_nd_namespace_blk(dev);
2625 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2626 GFP_KERNEL);
2627 nsblk->id = id;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002628 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2629 struct nd_namespace_pmem *nspm;
2630
2631 nspm = to_nd_namespace_pmem(dev);
2632 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2633 GFP_KERNEL);
2634 nspm->id = id;
Dan Williams1b40e092015-05-01 13:34:01 -04002635 } else
2636 id = i;
2637
2638 if (id < 0)
2639 break;
2640 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002641 nd_device_register(dev);
2642 }
Dan Williams1b40e092015-05-01 13:34:01 -04002643 if (i)
2644 nd_region->ns_seed = devs[0];
2645
2646 if (devs[i]) {
2647 int j;
2648
2649 for (j = i; devs[j]; j++) {
2650 struct device *dev = devs[j];
2651
2652 device_initialize(dev);
2653 put_device(dev);
2654 }
2655 *err = j - i;
2656 /*
2657 * All of the namespaces we tried to register failed, so
2658 * fail region activation.
2659 */
2660 if (*err == 0)
2661 rc = -ENODEV;
2662 }
Dan Williams3d880022015-05-31 15:02:11 -04002663 kfree(devs);
2664
Dan Williams1b40e092015-05-01 13:34:01 -04002665 if (rc == -ENODEV)
2666 return rc;
2667
Dan Williams3d880022015-05-31 15:02:11 -04002668 return i;
2669}