Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
3d88002e DW |
2 | /* |
3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. | |
3d88002e DW |
4 | */ |
5 | #include <linux/module.h> | |
6 | #include <linux/device.h> | |
6ff3e912 | 7 | #include <linux/sort.h> |
3d88002e | 8 | #include <linux/slab.h> |
ae8219f1 | 9 | #include <linux/list.h> |
3d88002e | 10 | #include <linux/nd.h> |
bf9bccc1 | 11 | #include "nd-core.h" |
ca6a4657 | 12 | #include "pmem.h" |
6acd7d5e | 13 | #include "pfn.h" |
3d88002e DW |
14 | #include "nd.h" |
15 | ||
16 | static 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 | ||
bf9bccc1 DW |
23 | static void namespace_pmem_release(struct device *dev) |
24 | { | |
25 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
0e3b0d12 | 26 | struct nd_region *nd_region = to_nd_region(dev->parent); |
bf9bccc1 | 27 | |
0e3b0d12 DW |
28 | if (nspm->id >= 0) |
29 | ida_simple_remove(&nd_region->ns_ida, nspm->id); | |
bf9bccc1 DW |
30 | kfree(nspm->alt_name); |
31 | kfree(nspm->uuid); | |
32 | kfree(nspm); | |
33 | } | |
34 | ||
78c81cc8 | 35 | static bool is_namespace_pmem(const struct device *dev); |
78c81cc8 | 36 | static bool is_namespace_io(const struct device *dev); |
bf9bccc1 | 37 | |
e07ecd76 DW |
38 | static int is_uuid_busy(struct device *dev, void *data) |
39 | { | |
d1c6e08e | 40 | uuid_t *uuid1 = data, *uuid2 = NULL; |
e07ecd76 DW |
41 | |
42 | if (is_namespace_pmem(dev)) { | |
43 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
44 | ||
45 | uuid2 = nspm->uuid; | |
e07ecd76 DW |
46 | } else if (is_nd_btt(dev)) { |
47 | struct nd_btt *nd_btt = to_nd_btt(dev); | |
48 | ||
49 | uuid2 = nd_btt->uuid; | |
50 | } else if (is_nd_pfn(dev)) { | |
51 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); | |
52 | ||
53 | uuid2 = nd_pfn->uuid; | |
54 | } | |
55 | ||
d1c6e08e | 56 | if (uuid2 && uuid_equal(uuid1, uuid2)) |
e07ecd76 DW |
57 | return -EBUSY; |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
62 | static int is_namespace_uuid_busy(struct device *dev, void *data) | |
63 | { | |
c9e582aa | 64 | if (is_nd_region(dev)) |
e07ecd76 DW |
65 | return device_for_each_child(dev, data, is_uuid_busy); |
66 | return 0; | |
67 | } | |
68 | ||
69 | /** | |
70 | * nd_is_uuid_unique - verify that no other namespace has @uuid | |
71 | * @dev: any device on a nvdimm_bus | |
72 | * @uuid: uuid to check | |
73 | */ | |
d1c6e08e | 74 | bool nd_is_uuid_unique(struct device *dev, uuid_t *uuid) |
e07ecd76 DW |
75 | { |
76 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); | |
77 | ||
78 | if (!nvdimm_bus) | |
79 | return false; | |
80 | WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev)); | |
81 | if (device_for_each_child(&nvdimm_bus->dev, uuid, | |
82 | is_namespace_uuid_busy) != 0) | |
83 | return false; | |
84 | return true; | |
85 | } | |
86 | ||
004f1afb DW |
87 | bool pmem_should_map_pages(struct device *dev) |
88 | { | |
89 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
fa7d2e63 | 90 | struct nd_namespace_common *ndns = to_ndns(dev); |
cfe30b87 | 91 | struct nd_namespace_io *nsio; |
004f1afb DW |
92 | |
93 | if (!IS_ENABLED(CONFIG_ZONE_DEVICE)) | |
94 | return false; | |
95 | ||
96 | if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags)) | |
97 | return false; | |
98 | ||
99 | if (is_nd_pfn(dev) || is_nd_btt(dev)) | |
100 | return false; | |
101 | ||
fa7d2e63 DW |
102 | if (ndns->force_raw) |
103 | return false; | |
104 | ||
cfe30b87 DW |
105 | nsio = to_nd_namespace_io(dev); |
106 | if (region_intersects(nsio->res.start, resource_size(&nsio->res), | |
107 | IORESOURCE_SYSTEM_RAM, | |
108 | IORES_DESC_NONE) == REGION_MIXED) | |
109 | return false; | |
110 | ||
004f1afb | 111 | return ARCH_MEMREMAP_PMEM == MEMREMAP_WB; |
004f1afb DW |
112 | } |
113 | EXPORT_SYMBOL(pmem_should_map_pages); | |
114 | ||
f979b13c DW |
115 | unsigned int pmem_sector_size(struct nd_namespace_common *ndns) |
116 | { | |
117 | if (is_namespace_pmem(&ndns->dev)) { | |
118 | struct nd_namespace_pmem *nspm; | |
119 | ||
120 | nspm = to_nd_namespace_pmem(&ndns->dev); | |
121 | if (nspm->lbasize == 0 || nspm->lbasize == 512) | |
122 | /* default */; | |
123 | else if (nspm->lbasize == 4096) | |
124 | return 4096; | |
125 | else | |
126 | dev_WARN(&ndns->dev, "unsupported sector size: %ld\n", | |
127 | nspm->lbasize); | |
128 | } | |
129 | ||
130 | /* | |
131 | * There is no namespace label (is_namespace_io()), or the label | |
132 | * indicates the default sector size. | |
133 | */ | |
134 | return 512; | |
135 | } | |
136 | EXPORT_SYMBOL(pmem_sector_size); | |
137 | ||
5212e11f VV |
138 | const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, |
139 | char *name) | |
140 | { | |
141 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
004f1afb | 142 | const char *suffix = NULL; |
5212e11f | 143 | |
0731de0d DW |
144 | if (ndns->claim && is_nd_btt(ndns->claim)) |
145 | suffix = "s"; | |
5212e11f | 146 | |
004f1afb | 147 | if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) { |
01220733 DW |
148 | int nsidx = 0; |
149 | ||
150 | if (is_namespace_pmem(&ndns->dev)) { | |
151 | struct nd_namespace_pmem *nspm; | |
152 | ||
153 | nspm = to_nd_namespace_pmem(&ndns->dev); | |
154 | nsidx = nspm->id; | |
155 | } | |
156 | ||
157 | if (nsidx) | |
158 | sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx, | |
159 | suffix ? suffix : ""); | |
160 | else | |
161 | sprintf(name, "pmem%d%s", nd_region->id, | |
162 | suffix ? suffix : ""); | |
5212e11f VV |
163 | } else { |
164 | return NULL; | |
165 | } | |
166 | ||
167 | return name; | |
168 | } | |
169 | EXPORT_SYMBOL(nvdimm_namespace_disk_name); | |
170 | ||
d1c6e08e | 171 | const uuid_t *nd_dev_to_uuid(struct device *dev) |
6ec68954 | 172 | { |
6ec68954 | 173 | if (!dev) |
d1c6e08e | 174 | return &uuid_null; |
6ec68954 VV |
175 | |
176 | if (is_namespace_pmem(dev)) { | |
177 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
178 | ||
179 | return nspm->uuid; | |
6ec68954 | 180 | } else |
d1c6e08e | 181 | return &uuid_null; |
6ec68954 VV |
182 | } |
183 | EXPORT_SYMBOL(nd_dev_to_uuid); | |
184 | ||
3d88002e DW |
185 | static ssize_t nstype_show(struct device *dev, |
186 | struct device_attribute *attr, char *buf) | |
187 | { | |
188 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
189 | ||
190 | return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); | |
191 | } | |
192 | static DEVICE_ATTR_RO(nstype); | |
193 | ||
bf9bccc1 DW |
194 | static ssize_t __alt_name_store(struct device *dev, const char *buf, |
195 | const size_t len) | |
196 | { | |
197 | char *input, *pos, *alt_name, **ns_altname; | |
198 | ssize_t rc; | |
199 | ||
200 | if (is_namespace_pmem(dev)) { | |
201 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
202 | ||
203 | ns_altname = &nspm->alt_name; | |
bf9bccc1 DW |
204 | } else |
205 | return -ENXIO; | |
206 | ||
8c2f7e86 | 207 | if (dev->driver || to_ndns(dev)->claim) |
bf9bccc1 DW |
208 | return -EBUSY; |
209 | ||
3d9cbe37 | 210 | input = kstrndup(buf, len, GFP_KERNEL); |
bf9bccc1 DW |
211 | if (!input) |
212 | return -ENOMEM; | |
213 | ||
bf9bccc1 DW |
214 | pos = strim(input); |
215 | if (strlen(pos) + 1 > NSLABEL_NAME_LEN) { | |
216 | rc = -EINVAL; | |
217 | goto out; | |
218 | } | |
219 | ||
220 | alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL); | |
221 | if (!alt_name) { | |
222 | rc = -ENOMEM; | |
223 | goto out; | |
224 | } | |
225 | kfree(*ns_altname); | |
226 | *ns_altname = alt_name; | |
227 | sprintf(*ns_altname, "%s", pos); | |
228 | rc = len; | |
229 | ||
230 | out: | |
231 | kfree(input); | |
232 | return rc; | |
233 | } | |
234 | ||
f524bf27 DW |
235 | static int nd_namespace_label_update(struct nd_region *nd_region, |
236 | struct device *dev) | |
237 | { | |
8c2f7e86 | 238 | dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim, |
f524bf27 | 239 | "namespace must be idle during label update\n"); |
8c2f7e86 | 240 | if (dev->driver || to_ndns(dev)->claim) |
f524bf27 DW |
241 | return 0; |
242 | ||
243 | /* | |
244 | * Only allow label writes that will result in a valid namespace | |
245 | * or deletion of an existing namespace. | |
246 | */ | |
247 | if (is_namespace_pmem(dev)) { | |
248 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
0ba1c634 | 249 | resource_size_t size = resource_size(&nspm->nsio.res); |
f524bf27 DW |
250 | |
251 | if (size == 0 && nspm->uuid) | |
252 | /* delete allocation */; | |
253 | else if (!nspm->uuid) | |
254 | return 0; | |
255 | ||
256 | return nd_pmem_namespace_label_update(nd_region, nspm, size); | |
f524bf27 DW |
257 | } else |
258 | return -ENXIO; | |
259 | } | |
260 | ||
bf9bccc1 DW |
261 | static ssize_t alt_name_store(struct device *dev, |
262 | struct device_attribute *attr, const char *buf, size_t len) | |
263 | { | |
f524bf27 | 264 | struct nd_region *nd_region = to_nd_region(dev->parent); |
bf9bccc1 DW |
265 | ssize_t rc; |
266 | ||
81beea55 | 267 | device_lock(dev); |
bf9bccc1 DW |
268 | nvdimm_bus_lock(dev); |
269 | wait_nvdimm_bus_probe_idle(dev); | |
270 | rc = __alt_name_store(dev, buf, len); | |
f524bf27 DW |
271 | if (rc >= 0) |
272 | rc = nd_namespace_label_update(nd_region, dev); | |
426824d6 | 273 | dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc); |
bf9bccc1 | 274 | nvdimm_bus_unlock(dev); |
81beea55 | 275 | device_unlock(dev); |
bf9bccc1 | 276 | |
f524bf27 | 277 | return rc < 0 ? rc : len; |
bf9bccc1 DW |
278 | } |
279 | ||
280 | static ssize_t alt_name_show(struct device *dev, | |
281 | struct device_attribute *attr, char *buf) | |
282 | { | |
283 | char *ns_altname; | |
284 | ||
285 | if (is_namespace_pmem(dev)) { | |
286 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
287 | ||
288 | ns_altname = nspm->alt_name; | |
bf9bccc1 DW |
289 | } else |
290 | return -ENXIO; | |
291 | ||
292 | return sprintf(buf, "%s\n", ns_altname ? ns_altname : ""); | |
293 | } | |
294 | static DEVICE_ATTR_RW(alt_name); | |
295 | ||
296 | static int scan_free(struct nd_region *nd_region, | |
297 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, | |
298 | resource_size_t n) | |
299 | { | |
bf9bccc1 DW |
300 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
301 | int rc = 0; | |
302 | ||
303 | while (n) { | |
304 | struct resource *res, *last; | |
bf9bccc1 DW |
305 | |
306 | last = NULL; | |
307 | for_each_dpa_resource(ndd, res) | |
308 | if (strcmp(res->name, label_id->id) == 0) | |
309 | last = res; | |
310 | res = last; | |
311 | if (!res) | |
312 | return 0; | |
313 | ||
314 | if (n >= resource_size(res)) { | |
315 | n -= resource_size(res); | |
316 | nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc); | |
317 | nvdimm_free_dpa(ndd, res); | |
318 | /* retry with last resource deleted */ | |
319 | continue; | |
320 | } | |
321 | ||
3b6c6c03 | 322 | rc = adjust_resource(res, res->start, resource_size(res) - n); |
1b40e09a DW |
323 | if (rc == 0) |
324 | res->flags |= DPA_RESOURCE_ADJUSTED; | |
bf9bccc1 DW |
325 | nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc); |
326 | break; | |
327 | } | |
328 | ||
329 | return rc; | |
330 | } | |
331 | ||
332 | /** | |
333 | * shrink_dpa_allocation - for each dimm in region free n bytes for label_id | |
334 | * @nd_region: the set of dimms to reclaim @n bytes from | |
335 | * @label_id: unique identifier for the namespace consuming this dpa range | |
336 | * @n: number of bytes per-dimm to release | |
337 | * | |
338 | * Assumes resources are ordered. Starting from the end try to | |
339 | * adjust_resource() the allocation to @n, but if @n is larger than the | |
340 | * allocation delete it and find the 'new' last allocation in the label | |
341 | * set. | |
342 | */ | |
343 | static int shrink_dpa_allocation(struct nd_region *nd_region, | |
344 | struct nd_label_id *label_id, resource_size_t n) | |
345 | { | |
346 | int i; | |
347 | ||
348 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
349 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
350 | int rc; | |
351 | ||
352 | rc = scan_free(nd_region, nd_mapping, label_id, n); | |
353 | if (rc) | |
354 | return rc; | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | static resource_size_t init_dpa_allocation(struct nd_label_id *label_id, | |
361 | struct nd_region *nd_region, struct nd_mapping *nd_mapping, | |
362 | resource_size_t n) | |
363 | { | |
bf9bccc1 | 364 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
bf9bccc1 DW |
365 | struct resource *res; |
366 | int rc = 0; | |
367 | ||
bf9bccc1 | 368 | /* first resource allocation for this label-id or dimm */ |
3b6c6c03 | 369 | res = nvdimm_allocate_dpa(ndd, label_id, nd_mapping->start, n); |
bf9bccc1 DW |
370 | if (!res) |
371 | rc = -EBUSY; | |
372 | ||
373 | nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc); | |
374 | return rc ? n : 0; | |
375 | } | |
376 | ||
762d067d DW |
377 | |
378 | /** | |
379 | * space_valid() - validate free dpa space against constraints | |
380 | * @nd_region: hosting region of the free space | |
381 | * @ndd: dimm device data for debug | |
382 | * @label_id: namespace id to allocate space | |
383 | * @prev: potential allocation that precedes free space | |
384 | * @next: allocation that follows the given free space range | |
385 | * @exist: first allocation with same id in the mapping | |
386 | * @n: range that must satisfied for pmem allocations | |
387 | * @valid: free space range to validate | |
388 | * | |
389 | * BLK-space is valid as long as it does not precede a PMEM | |
390 | * allocation in a given region. PMEM-space must be contiguous | |
391 | * and adjacent to an existing existing allocation (if one | |
392 | * exists). If reserving PMEM any space is valid. | |
393 | */ | |
394 | static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd, | |
395 | struct nd_label_id *label_id, struct resource *prev, | |
396 | struct resource *next, struct resource *exist, | |
397 | resource_size_t n, struct resource *valid) | |
bf9bccc1 | 398 | { |
762d067d | 399 | bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0; |
2522afb8 DW |
400 | unsigned long align; |
401 | ||
402 | align = nd_region->align / nd_region->ndr_mappings; | |
403 | valid->start = ALIGN(valid->start, align); | |
404 | valid->end = ALIGN_DOWN(valid->end + 1, align) - 1; | |
762d067d DW |
405 | |
406 | if (valid->start >= valid->end) | |
407 | goto invalid; | |
408 | ||
409 | if (is_reserve) | |
410 | return; | |
411 | ||
762d067d DW |
412 | /* allocation needs to be contiguous, so this is all or nothing */ |
413 | if (resource_size(valid) < n) | |
414 | goto invalid; | |
415 | ||
416 | /* we've got all the space we need and no existing allocation */ | |
417 | if (!exist) | |
418 | return; | |
419 | ||
420 | /* allocation needs to be contiguous with the existing namespace */ | |
421 | if (valid->start == exist->end + 1 | |
422 | || valid->end == exist->start - 1) | |
423 | return; | |
424 | ||
425 | invalid: | |
426 | /* truncate @valid size to 0 */ | |
427 | valid->end = valid->start - 1; | |
bf9bccc1 DW |
428 | } |
429 | ||
430 | enum alloc_loc { | |
431 | ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER, | |
432 | }; | |
433 | ||
434 | static resource_size_t scan_allocate(struct nd_region *nd_region, | |
435 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, | |
436 | resource_size_t n) | |
437 | { | |
438 | resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1; | |
bf9bccc1 | 439 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
762d067d | 440 | struct resource *res, *exist = NULL, valid; |
bf9bccc1 | 441 | const resource_size_t to_allocate = n; |
bf9bccc1 DW |
442 | int first; |
443 | ||
762d067d DW |
444 | for_each_dpa_resource(ndd, res) |
445 | if (strcmp(label_id->id, res->name) == 0) | |
446 | exist = res; | |
447 | ||
448 | valid.start = nd_mapping->start; | |
449 | valid.end = mapping_end; | |
450 | valid.name = "free space"; | |
bf9bccc1 DW |
451 | retry: |
452 | first = 0; | |
453 | for_each_dpa_resource(ndd, res) { | |
bf9bccc1 | 454 | struct resource *next = res->sibling, *new_res = NULL; |
762d067d | 455 | resource_size_t allocate, available = 0; |
bf9bccc1 DW |
456 | enum alloc_loc loc = ALLOC_ERR; |
457 | const char *action; | |
458 | int rc = 0; | |
459 | ||
460 | /* ignore resources outside this nd_mapping */ | |
461 | if (res->start > mapping_end) | |
462 | continue; | |
463 | if (res->end < nd_mapping->start) | |
464 | continue; | |
465 | ||
466 | /* space at the beginning of the mapping */ | |
467 | if (!first++ && res->start > nd_mapping->start) { | |
762d067d DW |
468 | valid.start = nd_mapping->start; |
469 | valid.end = res->start - 1; | |
470 | space_valid(nd_region, ndd, label_id, NULL, next, exist, | |
471 | to_allocate, &valid); | |
472 | available = resource_size(&valid); | |
473 | if (available) | |
bf9bccc1 DW |
474 | loc = ALLOC_BEFORE; |
475 | } | |
476 | ||
477 | /* space between allocations */ | |
478 | if (!loc && next) { | |
762d067d DW |
479 | valid.start = res->start + resource_size(res); |
480 | valid.end = min(mapping_end, next->start - 1); | |
481 | space_valid(nd_region, ndd, label_id, res, next, exist, | |
482 | to_allocate, &valid); | |
483 | available = resource_size(&valid); | |
484 | if (available) | |
bf9bccc1 | 485 | loc = ALLOC_MID; |
bf9bccc1 DW |
486 | } |
487 | ||
488 | /* space at the end of the mapping */ | |
489 | if (!loc && !next) { | |
762d067d DW |
490 | valid.start = res->start + resource_size(res); |
491 | valid.end = mapping_end; | |
492 | space_valid(nd_region, ndd, label_id, res, next, exist, | |
493 | to_allocate, &valid); | |
494 | available = resource_size(&valid); | |
495 | if (available) | |
bf9bccc1 | 496 | loc = ALLOC_AFTER; |
bf9bccc1 DW |
497 | } |
498 | ||
499 | if (!loc || !available) | |
500 | continue; | |
501 | allocate = min(available, n); | |
502 | switch (loc) { | |
503 | case ALLOC_BEFORE: | |
504 | if (strcmp(res->name, label_id->id) == 0) { | |
505 | /* adjust current resource up */ | |
bf9bccc1 DW |
506 | rc = adjust_resource(res, res->start - allocate, |
507 | resource_size(res) + allocate); | |
508 | action = "cur grow up"; | |
509 | } else | |
510 | action = "allocate"; | |
511 | break; | |
512 | case ALLOC_MID: | |
513 | if (strcmp(next->name, label_id->id) == 0) { | |
514 | /* adjust next resource up */ | |
bf9bccc1 DW |
515 | rc = adjust_resource(next, next->start |
516 | - allocate, resource_size(next) | |
517 | + allocate); | |
518 | new_res = next; | |
519 | action = "next grow up"; | |
520 | } else if (strcmp(res->name, label_id->id) == 0) { | |
521 | action = "grow down"; | |
522 | } else | |
523 | action = "allocate"; | |
524 | break; | |
525 | case ALLOC_AFTER: | |
526 | if (strcmp(res->name, label_id->id) == 0) | |
527 | action = "grow down"; | |
528 | else | |
529 | action = "allocate"; | |
530 | break; | |
531 | default: | |
532 | return n; | |
533 | } | |
534 | ||
535 | if (strcmp(action, "allocate") == 0) { | |
bf9bccc1 | 536 | new_res = nvdimm_allocate_dpa(ndd, label_id, |
762d067d | 537 | valid.start, allocate); |
bf9bccc1 DW |
538 | if (!new_res) |
539 | rc = -EBUSY; | |
540 | } else if (strcmp(action, "grow down") == 0) { | |
541 | /* adjust current resource down */ | |
542 | rc = adjust_resource(res, res->start, resource_size(res) | |
543 | + allocate); | |
1b40e09a DW |
544 | if (rc == 0) |
545 | res->flags |= DPA_RESOURCE_ADJUSTED; | |
bf9bccc1 DW |
546 | } |
547 | ||
548 | if (!new_res) | |
549 | new_res = res; | |
550 | ||
551 | nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n", | |
552 | action, loc, rc); | |
553 | ||
554 | if (rc) | |
555 | return n; | |
556 | ||
557 | n -= allocate; | |
558 | if (n) { | |
559 | /* | |
560 | * Retry scan with newly inserted resources. | |
561 | * For example, if we did an ALLOC_BEFORE | |
562 | * insertion there may also have been space | |
563 | * available for an ALLOC_AFTER insertion, so we | |
564 | * need to check this same resource again | |
565 | */ | |
566 | goto retry; | |
567 | } else | |
568 | return 0; | |
569 | } | |
570 | ||
3b6c6c03 | 571 | if (n == to_allocate) |
bf9bccc1 DW |
572 | return init_dpa_allocation(label_id, nd_region, nd_mapping, n); |
573 | return n; | |
574 | } | |
575 | ||
1b40e09a DW |
576 | static int merge_dpa(struct nd_region *nd_region, |
577 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id) | |
578 | { | |
579 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
580 | struct resource *res; | |
581 | ||
582 | if (strncmp("pmem", label_id->id, 4) == 0) | |
583 | return 0; | |
584 | retry: | |
585 | for_each_dpa_resource(ndd, res) { | |
586 | int rc; | |
587 | struct resource *next = res->sibling; | |
588 | resource_size_t end = res->start + resource_size(res); | |
589 | ||
590 | if (!next || strcmp(res->name, label_id->id) != 0 | |
591 | || strcmp(next->name, label_id->id) != 0 | |
592 | || end != next->start) | |
593 | continue; | |
594 | end += resource_size(next); | |
595 | nvdimm_free_dpa(ndd, next); | |
596 | rc = adjust_resource(res, res->start, end - res->start); | |
597 | nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc); | |
598 | if (rc) | |
599 | return rc; | |
600 | res->flags |= DPA_RESOURCE_ADJUSTED; | |
601 | goto retry; | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
12e3129e | 607 | int __reserve_free_pmem(struct device *dev, void *data) |
1b40e09a DW |
608 | { |
609 | struct nvdimm *nvdimm = data; | |
610 | struct nd_region *nd_region; | |
611 | struct nd_label_id label_id; | |
612 | int i; | |
613 | ||
c9e582aa | 614 | if (!is_memory(dev)) |
1b40e09a DW |
615 | return 0; |
616 | ||
617 | nd_region = to_nd_region(dev); | |
618 | if (nd_region->ndr_mappings == 0) | |
619 | return 0; | |
620 | ||
621 | memset(&label_id, 0, sizeof(label_id)); | |
622 | strcat(label_id.id, "pmem-reserve"); | |
623 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
624 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
625 | resource_size_t n, rem = 0; | |
626 | ||
627 | if (nd_mapping->nvdimm != nvdimm) | |
628 | continue; | |
629 | ||
3b6c6c03 | 630 | n = nd_pmem_available_dpa(nd_region, nd_mapping); |
1b40e09a DW |
631 | if (n == 0) |
632 | return 0; | |
633 | rem = scan_allocate(nd_region, nd_mapping, &label_id, n); | |
634 | dev_WARN_ONCE(&nd_region->dev, rem, | |
635 | "pmem reserve underrun: %#llx of %#llx bytes\n", | |
636 | (unsigned long long) n - rem, | |
637 | (unsigned long long) n); | |
638 | return rem ? -ENXIO : 0; | |
639 | } | |
640 | ||
641 | return 0; | |
642 | } | |
643 | ||
12e3129e | 644 | void release_free_pmem(struct nvdimm_bus *nvdimm_bus, |
1b40e09a DW |
645 | struct nd_mapping *nd_mapping) |
646 | { | |
647 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
648 | struct resource *res, *_res; | |
649 | ||
650 | for_each_dpa_resource_safe(ndd, res, _res) | |
651 | if (strcmp(res->name, "pmem-reserve") == 0) | |
652 | nvdimm_free_dpa(ndd, res); | |
653 | } | |
654 | ||
bf9bccc1 DW |
655 | /** |
656 | * grow_dpa_allocation - for each dimm allocate n bytes for @label_id | |
657 | * @nd_region: the set of dimms to allocate @n more bytes from | |
658 | * @label_id: unique identifier for the namespace consuming this dpa range | |
659 | * @n: number of bytes per-dimm to add to the existing allocation | |
660 | * | |
661 | * Assumes resources are ordered. For BLK regions, first consume | |
662 | * BLK-only available DPA free space, then consume PMEM-aliased DPA | |
663 | * space starting at the highest DPA. For PMEM regions start | |
664 | * allocations from the start of an interleave set and end at the first | |
665 | * BLK allocation or the end of the interleave set, whichever comes | |
666 | * first. | |
667 | */ | |
668 | static int grow_dpa_allocation(struct nd_region *nd_region, | |
669 | struct nd_label_id *label_id, resource_size_t n) | |
670 | { | |
671 | int i; | |
672 | ||
673 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
674 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1b40e09a | 675 | resource_size_t rem = n; |
3b6c6c03 | 676 | int rc; |
1b40e09a | 677 | |
3b6c6c03 | 678 | rem = scan_allocate(nd_region, nd_mapping, label_id, rem); |
1b40e09a DW |
679 | dev_WARN_ONCE(&nd_region->dev, rem, |
680 | "allocation underrun: %#llx of %#llx bytes\n", | |
681 | (unsigned long long) n - rem, | |
682 | (unsigned long long) n); | |
683 | if (rem) | |
684 | return -ENXIO; | |
685 | ||
686 | rc = merge_dpa(nd_region, nd_mapping, label_id); | |
bf9bccc1 DW |
687 | if (rc) |
688 | return rc; | |
689 | } | |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
0e3b0d12 | 694 | static void nd_namespace_pmem_set_resource(struct nd_region *nd_region, |
bf9bccc1 DW |
695 | struct nd_namespace_pmem *nspm, resource_size_t size) |
696 | { | |
697 | struct resource *res = &nspm->nsio.res; | |
0e3b0d12 | 698 | resource_size_t offset = 0; |
bf9bccc1 | 699 | |
0e3b0d12 DW |
700 | if (size && !nspm->uuid) { |
701 | WARN_ON_ONCE(1); | |
702 | size = 0; | |
703 | } | |
704 | ||
705 | if (size && nspm->uuid) { | |
706 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; | |
707 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
708 | struct nd_label_id label_id; | |
709 | struct resource *res; | |
710 | ||
711 | if (!ndd) { | |
712 | size = 0; | |
713 | goto out; | |
714 | } | |
715 | ||
716 | nd_label_gen_id(&label_id, nspm->uuid, 0); | |
717 | ||
718 | /* calculate a spa offset from the dpa allocation offset */ | |
719 | for_each_dpa_resource(ndd, res) | |
720 | if (strcmp(res->name, label_id.id) == 0) { | |
721 | offset = (res->start - nd_mapping->start) | |
722 | * nd_region->ndr_mappings; | |
723 | goto out; | |
724 | } | |
725 | ||
726 | WARN_ON_ONCE(1); | |
727 | size = 0; | |
728 | } | |
729 | ||
730 | out: | |
731 | res->start = nd_region->ndr_start + offset; | |
732 | res->end = res->start + size - 1; | |
bf9bccc1 DW |
733 | } |
734 | ||
d1c6e08e DW |
735 | static bool uuid_not_set(const uuid_t *uuid, struct device *dev, |
736 | const char *where) | |
bd26d0d0 DK |
737 | { |
738 | if (!uuid) { | |
739 | dev_dbg(dev, "%s: uuid not set\n", where); | |
740 | return true; | |
741 | } | |
742 | return false; | |
743 | } | |
744 | ||
bf9bccc1 DW |
745 | static ssize_t __size_store(struct device *dev, unsigned long long val) |
746 | { | |
747 | resource_size_t allocated = 0, available = 0; | |
748 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
1f19b983 | 749 | struct nd_namespace_common *ndns = to_ndns(dev); |
bf9bccc1 DW |
750 | struct nd_mapping *nd_mapping; |
751 | struct nvdimm_drvdata *ndd; | |
752 | struct nd_label_id label_id; | |
753 | u32 flags = 0, remainder; | |
9d032f42 | 754 | int rc, i, id = -1; |
d1c6e08e | 755 | uuid_t *uuid = NULL; |
bf9bccc1 | 756 | |
1f19b983 | 757 | if (dev->driver || ndns->claim) |
bf9bccc1 DW |
758 | return -EBUSY; |
759 | ||
760 | if (is_namespace_pmem(dev)) { | |
761 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
762 | ||
763 | uuid = nspm->uuid; | |
9d032f42 | 764 | id = nspm->id; |
bf9bccc1 DW |
765 | } |
766 | ||
767 | /* | |
768 | * We need a uuid for the allocation-label and dimm(s) on which | |
769 | * to store the label. | |
770 | */ | |
bd26d0d0 | 771 | if (uuid_not_set(uuid, dev, __func__)) |
bf9bccc1 | 772 | return -ENXIO; |
bd26d0d0 | 773 | if (nd_region->ndr_mappings == 0) { |
426824d6 | 774 | dev_dbg(dev, "not associated with dimm(s)\n"); |
bd26d0d0 DK |
775 | return -ENXIO; |
776 | } | |
bf9bccc1 | 777 | |
2522afb8 | 778 | div_u64_rem(val, nd_region->align, &remainder); |
bf9bccc1 | 779 | if (remainder) { |
5b26db95 | 780 | dev_dbg(dev, "%llu is not %ldK aligned\n", val, |
2522afb8 | 781 | nd_region->align / SZ_1K); |
bf9bccc1 DW |
782 | return -EINVAL; |
783 | } | |
784 | ||
785 | nd_label_gen_id(&label_id, uuid, flags); | |
786 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
787 | nd_mapping = &nd_region->mapping[i]; | |
788 | ndd = to_ndd(nd_mapping); | |
789 | ||
790 | /* | |
3b6c6c03 DW |
791 | * All dimms in an interleave set, need to be enabled |
792 | * for the size to be changed. | |
bf9bccc1 DW |
793 | */ |
794 | if (!ndd) | |
795 | return -ENXIO; | |
796 | ||
797 | allocated += nvdimm_allocated_dpa(ndd, &label_id); | |
798 | } | |
12e3129e | 799 | available = nd_region_allocatable_dpa(nd_region); |
bf9bccc1 DW |
800 | |
801 | if (val > available + allocated) | |
802 | return -ENOSPC; | |
803 | ||
804 | if (val == allocated) | |
805 | return 0; | |
806 | ||
807 | val = div_u64(val, nd_region->ndr_mappings); | |
808 | allocated = div_u64(allocated, nd_region->ndr_mappings); | |
809 | if (val < allocated) | |
810 | rc = shrink_dpa_allocation(nd_region, &label_id, | |
811 | allocated - val); | |
812 | else | |
813 | rc = grow_dpa_allocation(nd_region, &label_id, val - allocated); | |
814 | ||
815 | if (rc) | |
816 | return rc; | |
817 | ||
818 | if (is_namespace_pmem(dev)) { | |
819 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
820 | ||
0e3b0d12 | 821 | nd_namespace_pmem_set_resource(nd_region, nspm, |
bf9bccc1 DW |
822 | val * nd_region->ndr_mappings); |
823 | } | |
824 | ||
1f19b983 DW |
825 | /* |
826 | * Try to delete the namespace if we deleted all of its | |
9d032f42 DW |
827 | * allocation, this is not the seed or 0th device for the |
828 | * region, and it is not actively claimed by a btt, pfn, or dax | |
829 | * instance. | |
1f19b983 | 830 | */ |
9d032f42 | 831 | if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim) |
1f19b983 DW |
832 | nd_device_unregister(dev, ND_ASYNC); |
833 | ||
bf9bccc1 DW |
834 | return rc; |
835 | } | |
836 | ||
837 | static ssize_t size_store(struct device *dev, | |
838 | struct device_attribute *attr, const char *buf, size_t len) | |
839 | { | |
f524bf27 | 840 | struct nd_region *nd_region = to_nd_region(dev->parent); |
bf9bccc1 | 841 | unsigned long long val; |
d1c6e08e | 842 | uuid_t **uuid = NULL; |
bf9bccc1 DW |
843 | int rc; |
844 | ||
845 | rc = kstrtoull(buf, 0, &val); | |
846 | if (rc) | |
847 | return rc; | |
848 | ||
81beea55 | 849 | device_lock(dev); |
bf9bccc1 DW |
850 | nvdimm_bus_lock(dev); |
851 | wait_nvdimm_bus_probe_idle(dev); | |
852 | rc = __size_store(dev, val); | |
f524bf27 DW |
853 | if (rc >= 0) |
854 | rc = nd_namespace_label_update(nd_region, dev); | |
bf9bccc1 DW |
855 | |
856 | if (is_namespace_pmem(dev)) { | |
857 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
858 | ||
859 | uuid = &nspm->uuid; | |
bf9bccc1 DW |
860 | } |
861 | ||
862 | if (rc == 0 && val == 0 && uuid) { | |
863 | /* setting size zero == 'delete namespace' */ | |
864 | kfree(*uuid); | |
865 | *uuid = NULL; | |
866 | } | |
867 | ||
426824d6 | 868 | dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc); |
bf9bccc1 DW |
869 | |
870 | nvdimm_bus_unlock(dev); | |
81beea55 | 871 | device_unlock(dev); |
bf9bccc1 | 872 | |
f524bf27 | 873 | return rc < 0 ? rc : len; |
bf9bccc1 DW |
874 | } |
875 | ||
8c2f7e86 | 876 | resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns) |
bf9bccc1 | 877 | { |
8c2f7e86 | 878 | struct device *dev = &ndns->dev; |
1b40e09a | 879 | |
bf9bccc1 DW |
880 | if (is_namespace_pmem(dev)) { |
881 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
882 | ||
8c2f7e86 | 883 | return resource_size(&nspm->nsio.res); |
bf9bccc1 DW |
884 | } else if (is_namespace_io(dev)) { |
885 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); | |
886 | ||
8c2f7e86 DW |
887 | return resource_size(&nsio->res); |
888 | } else | |
889 | WARN_ONCE(1, "unknown namespace type\n"); | |
890 | return 0; | |
891 | } | |
892 | ||
893 | resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) | |
894 | { | |
895 | resource_size_t size; | |
1b40e09a | 896 | |
8c2f7e86 DW |
897 | nvdimm_bus_lock(&ndns->dev); |
898 | size = __nvdimm_namespace_capacity(ndns); | |
899 | nvdimm_bus_unlock(&ndns->dev); | |
900 | ||
901 | return size; | |
902 | } | |
903 | EXPORT_SYMBOL(nvdimm_namespace_capacity); | |
904 | ||
08e6b3c6 DW |
905 | bool nvdimm_namespace_locked(struct nd_namespace_common *ndns) |
906 | { | |
907 | int i; | |
908 | bool locked = false; | |
909 | struct device *dev = &ndns->dev; | |
910 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
911 | ||
912 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
913 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
914 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
915 | ||
916 | if (test_bit(NDD_LOCKED, &nvdimm->flags)) { | |
917 | dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm)); | |
918 | locked = true; | |
919 | } | |
920 | } | |
921 | return locked; | |
922 | } | |
923 | EXPORT_SYMBOL(nvdimm_namespace_locked); | |
924 | ||
8c2f7e86 DW |
925 | static ssize_t size_show(struct device *dev, |
926 | struct device_attribute *attr, char *buf) | |
927 | { | |
928 | return sprintf(buf, "%llu\n", (unsigned long long) | |
929 | nvdimm_namespace_capacity(to_ndns(dev))); | |
bf9bccc1 | 930 | } |
b44fe760 | 931 | static DEVICE_ATTR(size, 0444, size_show, size_store); |
bf9bccc1 | 932 | |
d1c6e08e | 933 | static uuid_t *namespace_to_uuid(struct device *dev) |
bf9bccc1 | 934 | { |
bf9bccc1 DW |
935 | if (is_namespace_pmem(dev)) { |
936 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
937 | ||
f95b4bca | 938 | return nspm->uuid; |
fadc38a6 DW |
939 | } |
940 | return ERR_PTR(-ENXIO); | |
f95b4bca DW |
941 | } |
942 | ||
d1c6e08e DW |
943 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
944 | char *buf) | |
f95b4bca | 945 | { |
d1c6e08e | 946 | uuid_t *uuid = namespace_to_uuid(dev); |
bf9bccc1 | 947 | |
f95b4bca DW |
948 | if (IS_ERR(uuid)) |
949 | return PTR_ERR(uuid); | |
bf9bccc1 DW |
950 | if (uuid) |
951 | return sprintf(buf, "%pUb\n", uuid); | |
952 | return sprintf(buf, "\n"); | |
953 | } | |
954 | ||
955 | /** | |
956 | * namespace_update_uuid - check for a unique uuid and whether we're "renaming" | |
957 | * @nd_region: parent region so we can updates all dimms in the set | |
958 | * @dev: namespace type for generating label_id | |
959 | * @new_uuid: incoming uuid | |
960 | * @old_uuid: reference to the uuid storage location in the namespace object | |
961 | */ | |
962 | static int namespace_update_uuid(struct nd_region *nd_region, | |
d1c6e08e DW |
963 | struct device *dev, uuid_t *new_uuid, |
964 | uuid_t **old_uuid) | |
bf9bccc1 | 965 | { |
bf9bccc1 DW |
966 | struct nd_label_id old_label_id; |
967 | struct nd_label_id new_label_id; | |
f524bf27 | 968 | int i; |
bf9bccc1 | 969 | |
f524bf27 DW |
970 | if (!nd_is_uuid_unique(dev, new_uuid)) |
971 | return -EINVAL; | |
bf9bccc1 DW |
972 | |
973 | if (*old_uuid == NULL) | |
974 | goto out; | |
975 | ||
f524bf27 DW |
976 | /* |
977 | * If we've already written a label with this uuid, then it's | |
978 | * too late to rename because we can't reliably update the uuid | |
979 | * without losing the old namespace. Userspace must delete this | |
980 | * namespace to abandon the old uuid. | |
981 | */ | |
982 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
983 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
984 | ||
985 | /* | |
986 | * This check by itself is sufficient because old_uuid | |
987 | * would be NULL above if this uuid did not exist in the | |
988 | * currently written set. | |
989 | * | |
990 | * FIXME: can we delete uuid with zero dpa allocated? | |
991 | */ | |
ae8219f1 | 992 | if (list_empty(&nd_mapping->labels)) |
f524bf27 DW |
993 | return -EBUSY; |
994 | } | |
995 | ||
fadc38a6 DW |
996 | nd_label_gen_id(&old_label_id, *old_uuid, 0); |
997 | nd_label_gen_id(&new_label_id, new_uuid, 0); | |
bf9bccc1 DW |
998 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
999 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1000 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
c4703ce1 | 1001 | struct nd_label_ent *label_ent; |
bf9bccc1 DW |
1002 | struct resource *res; |
1003 | ||
1004 | for_each_dpa_resource(ndd, res) | |
1005 | if (strcmp(res->name, old_label_id.id) == 0) | |
1006 | sprintf((void *) res->name, "%s", | |
1007 | new_label_id.id); | |
c4703ce1 DW |
1008 | |
1009 | mutex_lock(&nd_mapping->lock); | |
1010 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { | |
1011 | struct nd_namespace_label *nd_label = label_ent->label; | |
1012 | struct nd_label_id label_id; | |
d1c6e08e | 1013 | uuid_t uuid; |
c4703ce1 DW |
1014 | |
1015 | if (!nd_label) | |
1016 | continue; | |
d1c6e08e DW |
1017 | nsl_get_uuid(ndd, nd_label, &uuid); |
1018 | nd_label_gen_id(&label_id, &uuid, | |
b4366a82 | 1019 | nsl_get_flags(ndd, nd_label)); |
c4703ce1 DW |
1020 | if (strcmp(old_label_id.id, label_id.id) == 0) |
1021 | set_bit(ND_LABEL_REAP, &label_ent->flags); | |
1022 | } | |
1023 | mutex_unlock(&nd_mapping->lock); | |
bf9bccc1 DW |
1024 | } |
1025 | kfree(*old_uuid); | |
1026 | out: | |
1027 | *old_uuid = new_uuid; | |
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | static ssize_t uuid_store(struct device *dev, | |
1032 | struct device_attribute *attr, const char *buf, size_t len) | |
1033 | { | |
1034 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
d1c6e08e DW |
1035 | uuid_t *uuid = NULL; |
1036 | uuid_t **ns_uuid; | |
8c2f7e86 | 1037 | ssize_t rc = 0; |
bf9bccc1 DW |
1038 | |
1039 | if (is_namespace_pmem(dev)) { | |
1040 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1041 | ||
1042 | ns_uuid = &nspm->uuid; | |
bf9bccc1 DW |
1043 | } else |
1044 | return -ENXIO; | |
1045 | ||
81beea55 | 1046 | device_lock(dev); |
bf9bccc1 DW |
1047 | nvdimm_bus_lock(dev); |
1048 | wait_nvdimm_bus_probe_idle(dev); | |
8c2f7e86 DW |
1049 | if (to_ndns(dev)->claim) |
1050 | rc = -EBUSY; | |
1051 | if (rc >= 0) | |
1052 | rc = nd_uuid_store(dev, &uuid, buf, len); | |
bf9bccc1 DW |
1053 | if (rc >= 0) |
1054 | rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid); | |
f524bf27 DW |
1055 | if (rc >= 0) |
1056 | rc = nd_namespace_label_update(nd_region, dev); | |
1057 | else | |
1058 | kfree(uuid); | |
426824d6 DW |
1059 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
1060 | buf[len - 1] == '\n' ? "" : "\n"); | |
bf9bccc1 | 1061 | nvdimm_bus_unlock(dev); |
81beea55 | 1062 | device_unlock(dev); |
bf9bccc1 | 1063 | |
f524bf27 | 1064 | return rc < 0 ? rc : len; |
bf9bccc1 DW |
1065 | } |
1066 | static DEVICE_ATTR_RW(uuid); | |
1067 | ||
1068 | static ssize_t resource_show(struct device *dev, | |
1069 | struct device_attribute *attr, char *buf) | |
1070 | { | |
1071 | struct resource *res; | |
1072 | ||
1073 | if (is_namespace_pmem(dev)) { | |
1074 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1075 | ||
1076 | res = &nspm->nsio.res; | |
1077 | } else if (is_namespace_io(dev)) { | |
1078 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); | |
1079 | ||
1080 | res = &nsio->res; | |
1081 | } else | |
1082 | return -ENXIO; | |
1083 | ||
1084 | /* no address to convey if the namespace has no allocation */ | |
1085 | if (resource_size(res) == 0) | |
1086 | return -ENXIO; | |
1087 | return sprintf(buf, "%#llx\n", (unsigned long long) res->start); | |
1088 | } | |
5cf81ce1 | 1089 | static DEVICE_ATTR_ADMIN_RO(resource); |
bf9bccc1 | 1090 | |
f979b13c DW |
1091 | static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 }; |
1092 | ||
1b40e09a DW |
1093 | static ssize_t sector_size_show(struct device *dev, |
1094 | struct device_attribute *attr, char *buf) | |
1095 | { | |
f979b13c DW |
1096 | if (is_namespace_pmem(dev)) { |
1097 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1098 | ||
b2c48f9f | 1099 | return nd_size_select_show(nspm->lbasize, |
f979b13c DW |
1100 | pmem_lbasize_supported, buf); |
1101 | } | |
1102 | return -ENXIO; | |
1b40e09a DW |
1103 | } |
1104 | ||
1105 | static ssize_t sector_size_store(struct device *dev, | |
1106 | struct device_attribute *attr, const char *buf, size_t len) | |
1107 | { | |
f524bf27 | 1108 | struct nd_region *nd_region = to_nd_region(dev->parent); |
f979b13c DW |
1109 | const unsigned long *supported; |
1110 | unsigned long *lbasize; | |
8c2f7e86 | 1111 | ssize_t rc = 0; |
1b40e09a | 1112 | |
fadc38a6 | 1113 | if (is_namespace_pmem(dev)) { |
f979b13c DW |
1114 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); |
1115 | ||
1116 | lbasize = &nspm->lbasize; | |
1117 | supported = pmem_lbasize_supported; | |
1118 | } else | |
1b40e09a DW |
1119 | return -ENXIO; |
1120 | ||
81beea55 | 1121 | device_lock(dev); |
1b40e09a | 1122 | nvdimm_bus_lock(dev); |
8c2f7e86 DW |
1123 | if (to_ndns(dev)->claim) |
1124 | rc = -EBUSY; | |
1125 | if (rc >= 0) | |
b2c48f9f | 1126 | rc = nd_size_select_store(dev, buf, lbasize, supported); |
f524bf27 DW |
1127 | if (rc >= 0) |
1128 | rc = nd_namespace_label_update(nd_region, dev); | |
426824d6 DW |
1129 | dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote", |
1130 | buf, buf[len - 1] == '\n' ? "" : "\n"); | |
1b40e09a | 1131 | nvdimm_bus_unlock(dev); |
81beea55 | 1132 | device_unlock(dev); |
1b40e09a DW |
1133 | |
1134 | return rc ? rc : len; | |
1135 | } | |
1136 | static DEVICE_ATTR_RW(sector_size); | |
1137 | ||
0ba1c634 DW |
1138 | static ssize_t dpa_extents_show(struct device *dev, |
1139 | struct device_attribute *attr, char *buf) | |
1140 | { | |
1141 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
1142 | struct nd_label_id label_id; | |
d1c6e08e | 1143 | uuid_t *uuid = NULL; |
0ba1c634 | 1144 | int count = 0, i; |
0ba1c634 DW |
1145 | u32 flags = 0; |
1146 | ||
1147 | nvdimm_bus_lock(dev); | |
1148 | if (is_namespace_pmem(dev)) { | |
1149 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1150 | ||
1151 | uuid = nspm->uuid; | |
1152 | flags = 0; | |
0ba1c634 DW |
1153 | } |
1154 | ||
1155 | if (!uuid) | |
1156 | goto out; | |
1157 | ||
1158 | nd_label_gen_id(&label_id, uuid, flags); | |
1159 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1160 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1161 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
1162 | struct resource *res; | |
1163 | ||
1164 | for_each_dpa_resource(ndd, res) | |
1165 | if (strcmp(res->name, label_id.id) == 0) | |
1166 | count++; | |
1167 | } | |
1168 | out: | |
1169 | nvdimm_bus_unlock(dev); | |
1170 | ||
1171 | return sprintf(buf, "%d\n", count); | |
1172 | } | |
1173 | static DEVICE_ATTR_RO(dpa_extents); | |
1174 | ||
14e49454 VV |
1175 | static int btt_claim_class(struct device *dev) |
1176 | { | |
1177 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
1178 | int i, loop_bitmask = 0; | |
1179 | ||
1180 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1181 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1182 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
1183 | struct nd_namespace_index *nsindex; | |
1184 | ||
33a56086 DW |
1185 | /* |
1186 | * If any of the DIMMs do not support labels the only | |
1187 | * possible BTT format is v1. | |
1188 | */ | |
1189 | if (!ndd) { | |
1190 | loop_bitmask = 0; | |
1191 | break; | |
1192 | } | |
1193 | ||
14e49454 VV |
1194 | nsindex = to_namespace_index(ndd, ndd->ns_current); |
1195 | if (nsindex == NULL) | |
1196 | loop_bitmask |= 1; | |
1197 | else { | |
1198 | /* check whether existing labels are v1.1 or v1.2 */ | |
1199 | if (__le16_to_cpu(nsindex->major) == 1 | |
1200 | && __le16_to_cpu(nsindex->minor) == 1) | |
1201 | loop_bitmask |= 2; | |
1202 | else | |
1203 | loop_bitmask |= 4; | |
1204 | } | |
1205 | } | |
1206 | /* | |
1207 | * If nsindex is null loop_bitmask's bit 0 will be set, and if an index | |
1208 | * block is found, a v1.1 label for any mapping will set bit 1, and a | |
1209 | * v1.2 label will set bit 2. | |
1210 | * | |
1211 | * At the end of the loop, at most one of the three bits must be set. | |
1212 | * If multiple bits were set, it means the different mappings disagree | |
1213 | * about their labels, and this must be cleaned up first. | |
1214 | * | |
1215 | * If all the label index blocks are found to agree, nsindex of NULL | |
1216 | * implies labels haven't been initialized yet, and when they will, | |
1217 | * they will be of the 1.2 format, so we can assume BTT2.0 | |
1218 | * | |
1219 | * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are | |
1220 | * found, we enforce BTT2.0 | |
1221 | * | |
1222 | * If the loop was never entered, default to BTT1.1 (legacy namespaces) | |
1223 | */ | |
1224 | switch (loop_bitmask) { | |
1225 | case 0: | |
1226 | case 2: | |
1227 | return NVDIMM_CCLASS_BTT; | |
1228 | case 1: | |
1229 | case 4: | |
1230 | return NVDIMM_CCLASS_BTT2; | |
1231 | default: | |
1232 | return -ENXIO; | |
1233 | } | |
1234 | } | |
1235 | ||
8c2f7e86 DW |
1236 | static ssize_t holder_show(struct device *dev, |
1237 | struct device_attribute *attr, char *buf) | |
1238 | { | |
1239 | struct nd_namespace_common *ndns = to_ndns(dev); | |
1240 | ssize_t rc; | |
1241 | ||
81beea55 | 1242 | device_lock(dev); |
8c2f7e86 | 1243 | rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); |
81beea55 | 1244 | device_unlock(dev); |
8c2f7e86 DW |
1245 | |
1246 | return rc; | |
1247 | } | |
1248 | static DEVICE_ATTR_RO(holder); | |
1249 | ||
ab84b77a | 1250 | static int __holder_class_store(struct device *dev, const char *buf) |
b3fde74e DW |
1251 | { |
1252 | struct nd_namespace_common *ndns = to_ndns(dev); | |
1253 | ||
1254 | if (dev->driver || ndns->claim) | |
1255 | return -EBUSY; | |
1256 | ||
ab84b77a IW |
1257 | if (sysfs_streq(buf, "btt")) { |
1258 | int rc = btt_claim_class(dev); | |
1259 | ||
1260 | if (rc < NVDIMM_CCLASS_NONE) | |
1261 | return rc; | |
1262 | ndns->claim_class = rc; | |
1263 | } else if (sysfs_streq(buf, "pfn")) | |
b3fde74e | 1264 | ndns->claim_class = NVDIMM_CCLASS_PFN; |
075c3fdd | 1265 | else if (sysfs_streq(buf, "dax")) |
b3fde74e | 1266 | ndns->claim_class = NVDIMM_CCLASS_DAX; |
075c3fdd | 1267 | else if (sysfs_streq(buf, "")) |
b3fde74e DW |
1268 | ndns->claim_class = NVDIMM_CCLASS_NONE; |
1269 | else | |
1270 | return -EINVAL; | |
1271 | ||
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | static ssize_t holder_class_store(struct device *dev, | |
1276 | struct device_attribute *attr, const char *buf, size_t len) | |
1277 | { | |
1278 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
ab84b77a | 1279 | int rc; |
b3fde74e | 1280 | |
81beea55 | 1281 | device_lock(dev); |
b3fde74e DW |
1282 | nvdimm_bus_lock(dev); |
1283 | wait_nvdimm_bus_probe_idle(dev); | |
1284 | rc = __holder_class_store(dev, buf); | |
1285 | if (rc >= 0) | |
1286 | rc = nd_namespace_label_update(nd_region, dev); | |
ab84b77a | 1287 | dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc); |
b3fde74e | 1288 | nvdimm_bus_unlock(dev); |
81beea55 | 1289 | device_unlock(dev); |
b3fde74e DW |
1290 | |
1291 | return rc < 0 ? rc : len; | |
1292 | } | |
1293 | ||
1294 | static ssize_t holder_class_show(struct device *dev, | |
1295 | struct device_attribute *attr, char *buf) | |
1296 | { | |
1297 | struct nd_namespace_common *ndns = to_ndns(dev); | |
1298 | ssize_t rc; | |
1299 | ||
81beea55 | 1300 | device_lock(dev); |
b3fde74e DW |
1301 | if (ndns->claim_class == NVDIMM_CCLASS_NONE) |
1302 | rc = sprintf(buf, "\n"); | |
14e49454 VV |
1303 | else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) || |
1304 | (ndns->claim_class == NVDIMM_CCLASS_BTT2)) | |
b3fde74e DW |
1305 | rc = sprintf(buf, "btt\n"); |
1306 | else if (ndns->claim_class == NVDIMM_CCLASS_PFN) | |
1307 | rc = sprintf(buf, "pfn\n"); | |
1308 | else if (ndns->claim_class == NVDIMM_CCLASS_DAX) | |
1309 | rc = sprintf(buf, "dax\n"); | |
1310 | else | |
1311 | rc = sprintf(buf, "<unknown>\n"); | |
81beea55 | 1312 | device_unlock(dev); |
b3fde74e DW |
1313 | |
1314 | return rc; | |
1315 | } | |
1316 | static DEVICE_ATTR_RW(holder_class); | |
1317 | ||
0731de0d DW |
1318 | static ssize_t mode_show(struct device *dev, |
1319 | struct device_attribute *attr, char *buf) | |
1320 | { | |
1321 | struct nd_namespace_common *ndns = to_ndns(dev); | |
1322 | struct device *claim; | |
1323 | char *mode; | |
1324 | ssize_t rc; | |
1325 | ||
81beea55 | 1326 | device_lock(dev); |
0731de0d | 1327 | claim = ndns->claim; |
9c412428 | 1328 | if (claim && is_nd_btt(claim)) |
0731de0d | 1329 | mode = "safe"; |
9c412428 DW |
1330 | else if (claim && is_nd_pfn(claim)) |
1331 | mode = "memory"; | |
cd03412a DW |
1332 | else if (claim && is_nd_dax(claim)) |
1333 | mode = "dax"; | |
9c412428 DW |
1334 | else if (!claim && pmem_should_map_pages(dev)) |
1335 | mode = "memory"; | |
0731de0d DW |
1336 | else |
1337 | mode = "raw"; | |
1338 | rc = sprintf(buf, "%s\n", mode); | |
81beea55 | 1339 | device_unlock(dev); |
0731de0d DW |
1340 | |
1341 | return rc; | |
1342 | } | |
1343 | static DEVICE_ATTR_RO(mode); | |
1344 | ||
8c2f7e86 DW |
1345 | static ssize_t force_raw_store(struct device *dev, |
1346 | struct device_attribute *attr, const char *buf, size_t len) | |
1347 | { | |
1348 | bool force_raw; | |
1349 | int rc = strtobool(buf, &force_raw); | |
1350 | ||
1351 | if (rc) | |
1352 | return rc; | |
1353 | ||
1354 | to_ndns(dev)->force_raw = force_raw; | |
1355 | return len; | |
1356 | } | |
1357 | ||
1358 | static ssize_t force_raw_show(struct device *dev, | |
1359 | struct device_attribute *attr, char *buf) | |
1360 | { | |
1361 | return sprintf(buf, "%d\n", to_ndns(dev)->force_raw); | |
1362 | } | |
1363 | static DEVICE_ATTR_RW(force_raw); | |
1364 | ||
3d88002e DW |
1365 | static struct attribute *nd_namespace_attributes[] = { |
1366 | &dev_attr_nstype.attr, | |
bf9bccc1 | 1367 | &dev_attr_size.attr, |
0731de0d | 1368 | &dev_attr_mode.attr, |
bf9bccc1 | 1369 | &dev_attr_uuid.attr, |
8c2f7e86 | 1370 | &dev_attr_holder.attr, |
bf9bccc1 DW |
1371 | &dev_attr_resource.attr, |
1372 | &dev_attr_alt_name.attr, | |
8c2f7e86 | 1373 | &dev_attr_force_raw.attr, |
1b40e09a | 1374 | &dev_attr_sector_size.attr, |
0ba1c634 | 1375 | &dev_attr_dpa_extents.attr, |
b3fde74e | 1376 | &dev_attr_holder_class.attr, |
3d88002e DW |
1377 | NULL, |
1378 | }; | |
1379 | ||
bf9bccc1 DW |
1380 | static umode_t namespace_visible(struct kobject *kobj, |
1381 | struct attribute *a, int n) | |
1382 | { | |
1383 | struct device *dev = container_of(kobj, struct device, kobj); | |
1384 | ||
fadc38a6 | 1385 | if (is_namespace_pmem(dev)) { |
bf9bccc1 | 1386 | if (a == &dev_attr_size.attr) |
b44fe760 | 1387 | return 0644; |
1b40e09a | 1388 | |
bf9bccc1 DW |
1389 | return a->mode; |
1390 | } | |
1391 | ||
13f445d6 DW |
1392 | /* base is_namespace_io() attributes */ |
1393 | if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr || | |
1394 | a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr || | |
1395 | a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr || | |
1396 | a == &dev_attr_resource.attr) | |
bf9bccc1 DW |
1397 | return a->mode; |
1398 | ||
1399 | return 0; | |
1400 | } | |
1401 | ||
3d88002e DW |
1402 | static struct attribute_group nd_namespace_attribute_group = { |
1403 | .attrs = nd_namespace_attributes, | |
bf9bccc1 | 1404 | .is_visible = namespace_visible, |
3d88002e DW |
1405 | }; |
1406 | ||
1407 | static const struct attribute_group *nd_namespace_attribute_groups[] = { | |
1408 | &nd_device_attribute_group, | |
1409 | &nd_namespace_attribute_group, | |
74ae66c3 | 1410 | &nd_numa_attribute_group, |
3d88002e DW |
1411 | NULL, |
1412 | }; | |
1413 | ||
78c81cc8 DW |
1414 | static const struct device_type namespace_io_device_type = { |
1415 | .name = "nd_namespace_io", | |
1416 | .release = namespace_io_release, | |
1417 | .groups = nd_namespace_attribute_groups, | |
1418 | }; | |
1419 | ||
1420 | static const struct device_type namespace_pmem_device_type = { | |
1421 | .name = "nd_namespace_pmem", | |
1422 | .release = namespace_pmem_release, | |
1423 | .groups = nd_namespace_attribute_groups, | |
1424 | }; | |
1425 | ||
78c81cc8 DW |
1426 | static bool is_namespace_pmem(const struct device *dev) |
1427 | { | |
1428 | return dev ? dev->type == &namespace_pmem_device_type : false; | |
1429 | } | |
1430 | ||
78c81cc8 DW |
1431 | static bool is_namespace_io(const struct device *dev) |
1432 | { | |
1433 | return dev ? dev->type == &namespace_io_device_type : false; | |
1434 | } | |
1435 | ||
8c2f7e86 DW |
1436 | struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) |
1437 | { | |
1438 | struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; | |
e1455744 | 1439 | struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL; |
cd03412a | 1440 | struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL; |
0bfb8dd3 | 1441 | struct nd_namespace_common *ndns = NULL; |
8c2f7e86 DW |
1442 | resource_size_t size; |
1443 | ||
cd03412a | 1444 | if (nd_btt || nd_pfn || nd_dax) { |
0bfb8dd3 | 1445 | if (nd_btt) |
e1455744 | 1446 | ndns = nd_btt->ndns; |
0bfb8dd3 | 1447 | else if (nd_pfn) |
e1455744 | 1448 | ndns = nd_pfn->ndns; |
cd03412a DW |
1449 | else if (nd_dax) |
1450 | ndns = nd_dax->nd_pfn.ndns; | |
e1455744 | 1451 | |
0bfb8dd3 | 1452 | if (!ndns) |
8c2f7e86 DW |
1453 | return ERR_PTR(-ENODEV); |
1454 | ||
1455 | /* | |
1456 | * Flush any in-progess probes / removals in the driver | |
1457 | * for the raw personality of this namespace. | |
1458 | */ | |
81beea55 DW |
1459 | device_lock(&ndns->dev); |
1460 | device_unlock(&ndns->dev); | |
8c2f7e86 DW |
1461 | if (ndns->dev.driver) { |
1462 | dev_dbg(&ndns->dev, "is active, can't bind %s\n", | |
0bfb8dd3 | 1463 | dev_name(dev)); |
8c2f7e86 DW |
1464 | return ERR_PTR(-EBUSY); |
1465 | } | |
0bfb8dd3 | 1466 | if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev, |
8c2f7e86 | 1467 | "host (%s) vs claim (%s) mismatch\n", |
0bfb8dd3 | 1468 | dev_name(dev), |
8c2f7e86 DW |
1469 | dev_name(ndns->claim))) |
1470 | return ERR_PTR(-ENXIO); | |
1471 | } else { | |
1472 | ndns = to_ndns(dev); | |
1473 | if (ndns->claim) { | |
1474 | dev_dbg(dev, "claimed by %s, failing probe\n", | |
1475 | dev_name(ndns->claim)); | |
1476 | ||
1477 | return ERR_PTR(-ENXIO); | |
1478 | } | |
1479 | } | |
1480 | ||
08e6b3c6 DW |
1481 | if (nvdimm_namespace_locked(ndns)) |
1482 | return ERR_PTR(-EACCES); | |
1483 | ||
8c2f7e86 DW |
1484 | size = nvdimm_namespace_capacity(ndns); |
1485 | if (size < ND_MIN_NAMESPACE_SIZE) { | |
1486 | dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", | |
1487 | &size, ND_MIN_NAMESPACE_SIZE); | |
1488 | return ERR_PTR(-ENODEV); | |
1489 | } | |
1490 | ||
6acd7d5e DW |
1491 | /* |
1492 | * Note, alignment validation for fsdax and devdax mode | |
1493 | * namespaces happens in nd_pfn_validate() where infoblock | |
1494 | * padding parameters can be applied. | |
1495 | */ | |
1496 | if (pmem_should_map_pages(dev)) { | |
1497 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); | |
1498 | struct resource *res = &nsio->res; | |
1499 | ||
1500 | if (!IS_ALIGNED(res->start | (res->end + 1), | |
1501 | memremap_compat_align())) { | |
1502 | dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res); | |
1503 | return ERR_PTR(-EOPNOTSUPP); | |
1504 | } | |
1505 | } | |
1506 | ||
8c2f7e86 DW |
1507 | if (is_namespace_pmem(&ndns->dev)) { |
1508 | struct nd_namespace_pmem *nspm; | |
1509 | ||
1510 | nspm = to_nd_namespace_pmem(&ndns->dev); | |
bd26d0d0 | 1511 | if (uuid_not_set(nspm->uuid, &ndns->dev, __func__)) |
8c2f7e86 | 1512 | return ERR_PTR(-ENODEV); |
8c2f7e86 DW |
1513 | } |
1514 | ||
1515 | return ndns; | |
1516 | } | |
1517 | EXPORT_SYMBOL(nvdimm_namespace_common_probe); | |
1518 | ||
8f4b01fc AK |
1519 | int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns, |
1520 | resource_size_t size) | |
1521 | { | |
8f4b01fc AK |
1522 | return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size); |
1523 | } | |
1524 | EXPORT_SYMBOL_GPL(devm_namespace_enable); | |
1525 | ||
1526 | void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns) | |
1527 | { | |
8f4b01fc AK |
1528 | devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev)); |
1529 | } | |
1530 | EXPORT_SYMBOL_GPL(devm_namespace_disable); | |
1531 | ||
3d88002e DW |
1532 | static struct device **create_namespace_io(struct nd_region *nd_region) |
1533 | { | |
1534 | struct nd_namespace_io *nsio; | |
1535 | struct device *dev, **devs; | |
1536 | struct resource *res; | |
1537 | ||
1538 | nsio = kzalloc(sizeof(*nsio), GFP_KERNEL); | |
1539 | if (!nsio) | |
1540 | return NULL; | |
1541 | ||
1542 | devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); | |
1543 | if (!devs) { | |
1544 | kfree(nsio); | |
1545 | return NULL; | |
1546 | } | |
1547 | ||
8c2f7e86 | 1548 | dev = &nsio->common.dev; |
3d88002e DW |
1549 | dev->type = &namespace_io_device_type; |
1550 | dev->parent = &nd_region->dev; | |
1551 | res = &nsio->res; | |
1552 | res->name = dev_name(&nd_region->dev); | |
1553 | res->flags = IORESOURCE_MEM; | |
1554 | res->start = nd_region->ndr_start; | |
1555 | res->end = res->start + nd_region->ndr_size - 1; | |
1556 | ||
1557 | devs[0] = dev; | |
1558 | return devs; | |
1559 | } | |
1560 | ||
d1c6e08e DW |
1561 | static bool has_uuid_at_pos(struct nd_region *nd_region, const uuid_t *uuid, |
1562 | u64 cookie, u16 pos) | |
bf9bccc1 DW |
1563 | { |
1564 | struct nd_namespace_label *found = NULL; | |
1565 | int i; | |
1566 | ||
1567 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1568 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
faec6f8a DW |
1569 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
1570 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
ae8219f1 | 1571 | struct nd_label_ent *label_ent; |
bf9bccc1 | 1572 | bool found_uuid = false; |
bf9bccc1 | 1573 | |
ae8219f1 DW |
1574 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
1575 | struct nd_namespace_label *nd_label = label_ent->label; | |
8172db92 | 1576 | u16 position; |
ae8219f1 DW |
1577 | |
1578 | if (!nd_label) | |
1579 | continue; | |
b4366a82 | 1580 | position = nsl_get_position(ndd, nd_label); |
bf9bccc1 | 1581 | |
9761b02d | 1582 | if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) |
bf9bccc1 DW |
1583 | continue; |
1584 | ||
d1c6e08e | 1585 | if (!nsl_uuid_equal(ndd, nd_label, uuid)) |
bf9bccc1 DW |
1586 | continue; |
1587 | ||
8b03aa0e DW |
1588 | if (!nsl_validate_type_guid(ndd, nd_label, |
1589 | &nd_set->type_guid)) | |
faec6f8a | 1590 | continue; |
faec6f8a | 1591 | |
bf9bccc1 | 1592 | if (found_uuid) { |
426824d6 | 1593 | dev_dbg(ndd->dev, "duplicate entry for uuid\n"); |
bf9bccc1 DW |
1594 | return false; |
1595 | } | |
1596 | found_uuid = true; | |
8172db92 | 1597 | if (!nsl_validate_nlabel(nd_region, ndd, nd_label)) |
bf9bccc1 DW |
1598 | continue; |
1599 | if (position != pos) | |
1600 | continue; | |
1601 | found = nd_label; | |
1602 | break; | |
1603 | } | |
1604 | if (found) | |
1605 | break; | |
1606 | } | |
1607 | return found != NULL; | |
1608 | } | |
1609 | ||
d1c6e08e | 1610 | static int select_pmem_id(struct nd_region *nd_region, const uuid_t *pmem_id) |
bf9bccc1 | 1611 | { |
bf9bccc1 DW |
1612 | int i; |
1613 | ||
1614 | if (!pmem_id) | |
1615 | return -ENODEV; | |
1616 | ||
1617 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1618 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
0e3b0d12 | 1619 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
ae8219f1 | 1620 | struct nd_namespace_label *nd_label = NULL; |
bf9bccc1 | 1621 | u64 hw_start, hw_end, pmem_start, pmem_end; |
ae8219f1 | 1622 | struct nd_label_ent *label_ent; |
bf9bccc1 | 1623 | |
9cf8bd52 | 1624 | lockdep_assert_held(&nd_mapping->lock); |
ae8219f1 DW |
1625 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
1626 | nd_label = label_ent->label; | |
1627 | if (!nd_label) | |
1628 | continue; | |
d1c6e08e | 1629 | if (nsl_uuid_equal(ndd, nd_label, pmem_id)) |
bf9bccc1 | 1630 | break; |
ae8219f1 DW |
1631 | nd_label = NULL; |
1632 | } | |
bf9bccc1 DW |
1633 | |
1634 | if (!nd_label) { | |
1635 | WARN_ON(1); | |
1636 | return -EINVAL; | |
1637 | } | |
1638 | ||
bf9bccc1 DW |
1639 | /* |
1640 | * Check that this label is compliant with the dpa | |
1641 | * range published in NFIT | |
1642 | */ | |
1643 | hw_start = nd_mapping->start; | |
1644 | hw_end = hw_start + nd_mapping->size; | |
b4366a82 DW |
1645 | pmem_start = nsl_get_dpa(ndd, nd_label); |
1646 | pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label); | |
0e3b0d12 DW |
1647 | if (pmem_start >= hw_start && pmem_start < hw_end |
1648 | && pmem_end <= hw_end && pmem_end > hw_start) | |
bf9bccc1 | 1649 | /* pass */; |
0e3b0d12 DW |
1650 | else { |
1651 | dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n", | |
d1c6e08e DW |
1652 | dev_name(ndd->dev), |
1653 | nsl_uuid_raw(ndd, nd_label)); | |
bf9bccc1 | 1654 | return -EINVAL; |
0e3b0d12 | 1655 | } |
bf9bccc1 | 1656 | |
8a5f50d3 DW |
1657 | /* move recently validated label to the front of the list */ |
1658 | list_move(&label_ent->list, &nd_mapping->labels); | |
bf9bccc1 DW |
1659 | } |
1660 | return 0; | |
1661 | } | |
1662 | ||
1663 | /** | |
8a5f50d3 | 1664 | * create_namespace_pmem - validate interleave set labelling, retrieve label0 |
bf9bccc1 | 1665 | * @nd_region: region with mappings to validate |
8a5f50d3 DW |
1666 | * @nspm: target namespace to create |
1667 | * @nd_label: target pmem namespace label to evaluate | |
bf9bccc1 | 1668 | */ |
65853a1d | 1669 | static struct device *create_namespace_pmem(struct nd_region *nd_region, |
b4366a82 DW |
1670 | struct nd_mapping *nd_mapping, |
1671 | struct nd_namespace_label *nd_label) | |
bf9bccc1 | 1672 | { |
b4366a82 DW |
1673 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
1674 | struct nd_namespace_index *nsindex = | |
1675 | to_namespace_index(ndd, ndd->ns_current); | |
c12c48ce | 1676 | u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex); |
86ef58a4 | 1677 | u64 altcookie = nd_region_interleave_set_altcookie(nd_region); |
ae8219f1 | 1678 | struct nd_label_ent *label_ent; |
8a5f50d3 | 1679 | struct nd_namespace_pmem *nspm; |
bf9bccc1 | 1680 | resource_size_t size = 0; |
8a5f50d3 DW |
1681 | struct resource *res; |
1682 | struct device *dev; | |
d1c6e08e | 1683 | uuid_t uuid; |
ae8219f1 | 1684 | int rc = 0; |
bf9bccc1 DW |
1685 | u16 i; |
1686 | ||
4765218d DW |
1687 | if (cookie == 0) { |
1688 | dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n"); | |
8a5f50d3 | 1689 | return ERR_PTR(-ENXIO); |
4765218d | 1690 | } |
bf9bccc1 | 1691 | |
9761b02d | 1692 | if (!nsl_validate_isetcookie(ndd, nd_label, cookie)) { |
8a5f50d3 | 1693 | dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n", |
d1c6e08e | 1694 | nsl_uuid_raw(ndd, nd_label)); |
9761b02d | 1695 | if (!nsl_validate_isetcookie(ndd, nd_label, altcookie)) |
86ef58a4 DW |
1696 | return ERR_PTR(-EAGAIN); |
1697 | ||
1698 | dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n", | |
d1c6e08e | 1699 | nsl_uuid_raw(ndd, nd_label)); |
ae8219f1 | 1700 | } |
bf9bccc1 | 1701 | |
8a5f50d3 DW |
1702 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
1703 | if (!nspm) | |
1704 | return ERR_PTR(-ENOMEM); | |
ae8219f1 | 1705 | |
0e3b0d12 | 1706 | nspm->id = -1; |
8a5f50d3 DW |
1707 | dev = &nspm->nsio.common.dev; |
1708 | dev->type = &namespace_pmem_device_type; | |
1709 | dev->parent = &nd_region->dev; | |
1710 | res = &nspm->nsio.res; | |
1711 | res->name = dev_name(&nd_region->dev); | |
1712 | res->flags = IORESOURCE_MEM; | |
ae8219f1 | 1713 | |
86ef58a4 | 1714 | for (i = 0; i < nd_region->ndr_mappings; i++) { |
d1c6e08e DW |
1715 | uuid_t uuid; |
1716 | ||
1717 | nsl_get_uuid(ndd, nd_label, &uuid); | |
1718 | if (has_uuid_at_pos(nd_region, &uuid, cookie, i)) | |
86ef58a4 | 1719 | continue; |
d1c6e08e | 1720 | if (has_uuid_at_pos(nd_region, &uuid, altcookie, i)) |
86ef58a4 DW |
1721 | continue; |
1722 | break; | |
1723 | } | |
1724 | ||
8a5f50d3 | 1725 | if (i < nd_region->ndr_mappings) { |
4f867220 | 1726 | struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm; |
0e3b0d12 | 1727 | |
8a5f50d3 DW |
1728 | /* |
1729 | * Give up if we don't find an instance of a uuid at each | |
1730 | * position (from 0 to nd_region->ndr_mappings - 1), or if we | |
1731 | * find a dimm with two instances of the same uuid. | |
1732 | */ | |
0e3b0d12 | 1733 | dev_err(&nd_region->dev, "%s missing label for %pUb\n", |
d1c6e08e | 1734 | nvdimm_name(nvdimm), nsl_uuid_raw(ndd, nd_label)); |
8a5f50d3 | 1735 | rc = -EINVAL; |
ae8219f1 | 1736 | goto err; |
8a5f50d3 | 1737 | } |
bf9bccc1 DW |
1738 | |
1739 | /* | |
1740 | * Fix up each mapping's 'labels' to have the validated pmem label for | |
1741 | * that position at labels[0], and NULL at labels[1]. In the process, | |
3b6c6c03 | 1742 | * check that the namespace aligns with interleave-set. |
bf9bccc1 | 1743 | */ |
d1c6e08e DW |
1744 | nsl_get_uuid(ndd, nd_label, &uuid); |
1745 | rc = select_pmem_id(nd_region, &uuid); | |
bf9bccc1 DW |
1746 | if (rc) |
1747 | goto err; | |
1748 | ||
1749 | /* Calculate total size and populate namespace properties from label0 */ | |
1750 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
ae8219f1 | 1751 | struct nd_namespace_label *label0; |
b3fde74e | 1752 | struct nvdimm_drvdata *ndd; |
ae8219f1 DW |
1753 | |
1754 | nd_mapping = &nd_region->mapping[i]; | |
ae8219f1 DW |
1755 | label_ent = list_first_entry_or_null(&nd_mapping->labels, |
1756 | typeof(*label_ent), list); | |
86aa6668 | 1757 | label0 = label_ent ? label_ent->label : NULL; |
ae8219f1 DW |
1758 | |
1759 | if (!label0) { | |
1760 | WARN_ON(1); | |
1761 | continue; | |
1762 | } | |
bf9bccc1 | 1763 | |
b4366a82 DW |
1764 | ndd = to_ndd(nd_mapping); |
1765 | size += nsl_get_rawsize(ndd, label0); | |
1766 | if (nsl_get_position(ndd, label0) != 0) | |
bf9bccc1 DW |
1767 | continue; |
1768 | WARN_ON(nspm->alt_name || nspm->uuid); | |
b4366a82 DW |
1769 | nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0), |
1770 | NSLABEL_NAME_LEN, GFP_KERNEL); | |
d1c6e08e DW |
1771 | nsl_get_uuid(ndd, label0, &uuid); |
1772 | nspm->uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL); | |
b4366a82 | 1773 | nspm->lbasize = nsl_get_lbasize(ndd, label0); |
a6e6d722 DW |
1774 | nspm->nsio.common.claim_class = |
1775 | nsl_get_claim_class(ndd, label0); | |
bf9bccc1 DW |
1776 | } |
1777 | ||
1778 | if (!nspm->alt_name || !nspm->uuid) { | |
1779 | rc = -ENOMEM; | |
1780 | goto err; | |
1781 | } | |
1782 | ||
0e3b0d12 | 1783 | nd_namespace_pmem_set_resource(nd_region, nspm, size); |
bf9bccc1 | 1784 | |
8a5f50d3 | 1785 | return dev; |
bf9bccc1 | 1786 | err: |
8a5f50d3 | 1787 | namespace_pmem_release(dev); |
bf9bccc1 DW |
1788 | switch (rc) { |
1789 | case -EINVAL: | |
426824d6 | 1790 | dev_dbg(&nd_region->dev, "invalid label(s)\n"); |
bf9bccc1 DW |
1791 | break; |
1792 | case -ENODEV: | |
426824d6 | 1793 | dev_dbg(&nd_region->dev, "label not found\n"); |
bf9bccc1 DW |
1794 | break; |
1795 | default: | |
426824d6 | 1796 | dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc); |
bf9bccc1 DW |
1797 | break; |
1798 | } | |
8a5f50d3 | 1799 | return ERR_PTR(rc); |
bf9bccc1 DW |
1800 | } |
1801 | ||
98a29c39 DW |
1802 | static struct device *nd_namespace_pmem_create(struct nd_region *nd_region) |
1803 | { | |
1804 | struct nd_namespace_pmem *nspm; | |
1805 | struct resource *res; | |
1806 | struct device *dev; | |
1807 | ||
c9e582aa | 1808 | if (!is_memory(&nd_region->dev)) |
98a29c39 DW |
1809 | return NULL; |
1810 | ||
1811 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); | |
1812 | if (!nspm) | |
1813 | return NULL; | |
1814 | ||
1815 | dev = &nspm->nsio.common.dev; | |
1816 | dev->type = &namespace_pmem_device_type; | |
1817 | dev->parent = &nd_region->dev; | |
1818 | res = &nspm->nsio.res; | |
1819 | res->name = dev_name(&nd_region->dev); | |
1820 | res->flags = IORESOURCE_MEM; | |
1821 | ||
1822 | nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); | |
1823 | if (nspm->id < 0) { | |
1824 | kfree(nspm); | |
1825 | return NULL; | |
1826 | } | |
1827 | dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id); | |
98a29c39 DW |
1828 | nd_namespace_pmem_set_resource(nd_region, nspm, 0); |
1829 | ||
1830 | return dev; | |
1831 | } | |
1832 | ||
4a0079bc DW |
1833 | static struct lock_class_key nvdimm_namespace_key; |
1834 | ||
98a29c39 | 1835 | void nd_region_create_ns_seed(struct nd_region *nd_region) |
1b40e09a DW |
1836 | { |
1837 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); | |
98a29c39 DW |
1838 | |
1839 | if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO) | |
1840 | return; | |
1841 | ||
84bd3690 | 1842 | nd_region->ns_seed = nd_namespace_pmem_create(nd_region); |
98a29c39 | 1843 | |
1b40e09a DW |
1844 | /* |
1845 | * Seed creation failures are not fatal, provisioning is simply | |
1846 | * disabled until memory becomes available | |
1847 | */ | |
1848 | if (!nd_region->ns_seed) | |
3b6c6c03 | 1849 | dev_err(&nd_region->dev, "failed to create namespace\n"); |
4a0079bc DW |
1850 | else { |
1851 | device_initialize(nd_region->ns_seed); | |
1852 | lockdep_set_class(&nd_region->ns_seed->mutex, | |
1853 | &nvdimm_namespace_key); | |
1b40e09a | 1854 | nd_device_register(nd_region->ns_seed); |
4a0079bc | 1855 | } |
1b40e09a DW |
1856 | } |
1857 | ||
cd03412a DW |
1858 | void nd_region_create_dax_seed(struct nd_region *nd_region) |
1859 | { | |
1860 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); | |
1861 | nd_region->dax_seed = nd_dax_create(nd_region); | |
1862 | /* | |
1863 | * Seed creation failures are not fatal, provisioning is simply | |
1864 | * disabled until memory becomes available | |
1865 | */ | |
1866 | if (!nd_region->dax_seed) | |
1867 | dev_err(&nd_region->dev, "failed to create dax namespace\n"); | |
1868 | } | |
1869 | ||
2dc43331 DW |
1870 | void nd_region_create_pfn_seed(struct nd_region *nd_region) |
1871 | { | |
1872 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); | |
1873 | nd_region->pfn_seed = nd_pfn_create(nd_region); | |
1874 | /* | |
1875 | * Seed creation failures are not fatal, provisioning is simply | |
1876 | * disabled until memory becomes available | |
1877 | */ | |
1878 | if (!nd_region->pfn_seed) | |
1879 | dev_err(&nd_region->dev, "failed to create pfn namespace\n"); | |
1880 | } | |
1881 | ||
8c2f7e86 DW |
1882 | void nd_region_create_btt_seed(struct nd_region *nd_region) |
1883 | { | |
1884 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); | |
1885 | nd_region->btt_seed = nd_btt_create(nd_region); | |
1886 | /* | |
1887 | * Seed creation failures are not fatal, provisioning is simply | |
1888 | * disabled until memory becomes available | |
1889 | */ | |
1890 | if (!nd_region->btt_seed) | |
1891 | dev_err(&nd_region->dev, "failed to create btt namespace\n"); | |
1892 | } | |
1893 | ||
8a5f50d3 DW |
1894 | static int add_namespace_resource(struct nd_region *nd_region, |
1895 | struct nd_namespace_label *nd_label, struct device **devs, | |
1896 | int count) | |
1b40e09a | 1897 | { |
8a5f50d3 DW |
1898 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
1899 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
1900 | int i; | |
1901 | ||
1902 | for (i = 0; i < count; i++) { | |
d1c6e08e | 1903 | uuid_t *uuid = namespace_to_uuid(devs[i]); |
8a5f50d3 | 1904 | |
d1c6e08e | 1905 | if (IS_ERR(uuid)) { |
8a5f50d3 DW |
1906 | WARN_ON(1); |
1907 | continue; | |
1908 | } | |
1909 | ||
d1c6e08e | 1910 | if (!nsl_uuid_equal(ndd, nd_label, uuid)) |
8a5f50d3 | 1911 | continue; |
fadc38a6 DW |
1912 | dev_err(&nd_region->dev, |
1913 | "error: conflicting extents for uuid: %pUb\n", uuid); | |
1914 | return -ENXIO; | |
8a5f50d3 DW |
1915 | } |
1916 | ||
1917 | return i; | |
1918 | } | |
1919 | ||
6ff3e912 DW |
1920 | static int cmp_dpa(const void *a, const void *b) |
1921 | { | |
1922 | const struct device *dev_a = *(const struct device **) a; | |
1923 | const struct device *dev_b = *(const struct device **) b; | |
6ff3e912 DW |
1924 | struct nd_namespace_pmem *nspm_a, *nspm_b; |
1925 | ||
1926 | if (is_namespace_io(dev_a)) | |
1927 | return 0; | |
1928 | ||
6ff3e912 DW |
1929 | nspm_a = to_nd_namespace_pmem(dev_a); |
1930 | nspm_b = to_nd_namespace_pmem(dev_b); | |
1931 | ||
1932 | return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start, | |
1933 | sizeof(resource_size_t)); | |
1934 | } | |
1935 | ||
8a5f50d3 DW |
1936 | static struct device **scan_labels(struct nd_region *nd_region) |
1937 | { | |
c969e24c | 1938 | int i, count = 0; |
8a5f50d3 DW |
1939 | struct device *dev, **devs = NULL; |
1940 | struct nd_label_ent *label_ent, *e; | |
c969e24c | 1941 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
b4366a82 | 1942 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
c969e24c | 1943 | resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1; |
1b40e09a | 1944 | |
8a5f50d3 DW |
1945 | /* "safe" because create_namespace_pmem() might list_move() label_ent */ |
1946 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { | |
ae8219f1 | 1947 | struct nd_namespace_label *nd_label = label_ent->label; |
1b40e09a DW |
1948 | struct device **__devs; |
1949 | ||
ae8219f1 DW |
1950 | if (!nd_label) |
1951 | continue; | |
1b40e09a | 1952 | |
c969e24c | 1953 | /* skip labels that describe extents outside of the region */ |
b4366a82 DW |
1954 | if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start || |
1955 | nsl_get_dpa(ndd, nd_label) > map_end) | |
1956 | continue; | |
c969e24c | 1957 | |
8a5f50d3 DW |
1958 | i = add_namespace_resource(nd_region, nd_label, devs, count); |
1959 | if (i < 0) | |
1960 | goto err; | |
1b40e09a DW |
1961 | if (i < count) |
1962 | continue; | |
1963 | __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL); | |
1964 | if (!__devs) | |
1965 | goto err; | |
1966 | memcpy(__devs, devs, sizeof(dev) * count); | |
1967 | kfree(devs); | |
1968 | devs = __devs; | |
1969 | ||
84bd3690 | 1970 | dev = create_namespace_pmem(nd_region, nd_mapping, nd_label); |
faec6f8a DW |
1971 | if (IS_ERR(dev)) { |
1972 | switch (PTR_ERR(dev)) { | |
1973 | case -EAGAIN: | |
1974 | /* skip invalid labels */ | |
1975 | continue; | |
1976 | case -ENODEV: | |
1977 | /* fallthrough to seed creation */ | |
1978 | break; | |
1979 | default: | |
1980 | goto err; | |
1981 | } | |
1982 | } else | |
1983 | devs[count++] = dev; | |
1984 | ||
1b40e09a DW |
1985 | } |
1986 | ||
3b6c6c03 DW |
1987 | dev_dbg(&nd_region->dev, "discovered %d namespace%s\n", count, |
1988 | count == 1 ? "" : "s"); | |
1b40e09a DW |
1989 | |
1990 | if (count == 0) { | |
84bd3690 DW |
1991 | struct nd_namespace_pmem *nspm; |
1992 | ||
1b40e09a | 1993 | /* Publish a zero-sized namespace for userspace to configure. */ |
ae8219f1 | 1994 | nd_mapping_free_labels(nd_mapping); |
1b40e09a DW |
1995 | |
1996 | devs = kcalloc(2, sizeof(dev), GFP_KERNEL); | |
1997 | if (!devs) | |
1998 | goto err; | |
8a5f50d3 | 1999 | |
84bd3690 DW |
2000 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); |
2001 | if (!nspm) | |
2002 | goto err; | |
2003 | dev = &nspm->nsio.common.dev; | |
2004 | dev->type = &namespace_pmem_device_type; | |
2005 | nd_namespace_pmem_set_resource(nd_region, nspm, 0); | |
1b40e09a DW |
2006 | dev->parent = &nd_region->dev; |
2007 | devs[count++] = dev; | |
c9e582aa | 2008 | } else if (is_memory(&nd_region->dev)) { |
8a5f50d3 DW |
2009 | /* clean unselected labels */ |
2010 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
0e3b0d12 DW |
2011 | struct list_head *l, *e; |
2012 | LIST_HEAD(list); | |
2013 | int j; | |
2014 | ||
8a5f50d3 DW |
2015 | nd_mapping = &nd_region->mapping[i]; |
2016 | if (list_empty(&nd_mapping->labels)) { | |
2017 | WARN_ON(1); | |
2018 | continue; | |
2019 | } | |
0e3b0d12 DW |
2020 | |
2021 | j = count; | |
2022 | list_for_each_safe(l, e, &nd_mapping->labels) { | |
2023 | if (!j--) | |
2024 | break; | |
2025 | list_move_tail(l, &list); | |
2026 | } | |
8a5f50d3 | 2027 | nd_mapping_free_labels(nd_mapping); |
0e3b0d12 | 2028 | list_splice_init(&list, &nd_mapping->labels); |
8a5f50d3 | 2029 | } |
1b40e09a DW |
2030 | } |
2031 | ||
6ff3e912 DW |
2032 | if (count > 1) |
2033 | sort(devs, count, sizeof(struct device *), cmp_dpa, NULL); | |
2034 | ||
1b40e09a DW |
2035 | return devs; |
2036 | ||
ae8219f1 | 2037 | err: |
75d29713 DC |
2038 | if (devs) { |
2039 | for (i = 0; devs[i]; i++) | |
84bd3690 | 2040 | namespace_pmem_release(devs[i]); |
75d29713 DC |
2041 | kfree(devs); |
2042 | } | |
1b40e09a DW |
2043 | return NULL; |
2044 | } | |
2045 | ||
8a5f50d3 | 2046 | static struct device **create_namespaces(struct nd_region *nd_region) |
ae8219f1 | 2047 | { |
59858d3d | 2048 | struct nd_mapping *nd_mapping; |
ae8219f1 | 2049 | struct device **devs; |
8a5f50d3 | 2050 | int i; |
ae8219f1 DW |
2051 | |
2052 | if (nd_region->ndr_mappings == 0) | |
2053 | return NULL; | |
2054 | ||
8a5f50d3 DW |
2055 | /* lock down all mappings while we scan labels */ |
2056 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
2057 | nd_mapping = &nd_region->mapping[i]; | |
2058 | mutex_lock_nested(&nd_mapping->lock, i); | |
2059 | } | |
2060 | ||
2061 | devs = scan_labels(nd_region); | |
2062 | ||
2063 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
2064 | int reverse = nd_region->ndr_mappings - 1 - i; | |
2065 | ||
2066 | nd_mapping = &nd_region->mapping[reverse]; | |
2067 | mutex_unlock(&nd_mapping->lock); | |
2068 | } | |
ae8219f1 DW |
2069 | |
2070 | return devs; | |
2071 | } | |
2072 | ||
a2d1c7a6 DW |
2073 | static void deactivate_labels(void *region) |
2074 | { | |
2075 | struct nd_region *nd_region = region; | |
2076 | int i; | |
2077 | ||
2078 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
2079 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
2080 | struct nvdimm_drvdata *ndd = nd_mapping->ndd; | |
2081 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
2082 | ||
2083 | mutex_lock(&nd_mapping->lock); | |
2084 | nd_mapping_free_labels(nd_mapping); | |
2085 | mutex_unlock(&nd_mapping->lock); | |
2086 | ||
2087 | put_ndd(ndd); | |
2088 | nd_mapping->ndd = NULL; | |
2089 | if (ndd) | |
2090 | atomic_dec(&nvdimm->busy); | |
2091 | } | |
2092 | } | |
2093 | ||
bf9bccc1 DW |
2094 | static int init_active_labels(struct nd_region *nd_region) |
2095 | { | |
d9cee9f8 | 2096 | int i, rc = 0; |
bf9bccc1 DW |
2097 | |
2098 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
2099 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
2100 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
2101 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
ae8219f1 | 2102 | struct nd_label_ent *label_ent; |
bf9bccc1 DW |
2103 | int count, j; |
2104 | ||
2105 | /* | |
9d62ed96 DW |
2106 | * If the dimm is disabled then we may need to prevent |
2107 | * the region from being activated. | |
bf9bccc1 DW |
2108 | */ |
2109 | if (!ndd) { | |
9d62ed96 DW |
2110 | if (test_bit(NDD_LOCKED, &nvdimm->flags)) |
2111 | /* fail, label data may be unreadable */; | |
a0e37452 | 2112 | else if (test_bit(NDD_LABELING, &nvdimm->flags)) |
9d62ed96 DW |
2113 | /* fail, labels needed to disambiguate dpa */; |
2114 | else | |
d9cee9f8 | 2115 | continue; |
9d62ed96 DW |
2116 | |
2117 | dev_err(&nd_region->dev, "%s: is %s, failing probe\n", | |
2118 | dev_name(&nd_mapping->nvdimm->dev), | |
2119 | test_bit(NDD_LOCKED, &nvdimm->flags) | |
2120 | ? "locked" : "disabled"); | |
d9cee9f8 DW |
2121 | rc = -ENXIO; |
2122 | goto out; | |
bf9bccc1 DW |
2123 | } |
2124 | nd_mapping->ndd = ndd; | |
2125 | atomic_inc(&nvdimm->busy); | |
2126 | get_ndd(ndd); | |
2127 | ||
2128 | count = nd_label_active_count(ndd); | |
426824d6 | 2129 | dev_dbg(ndd->dev, "count: %d\n", count); |
bf9bccc1 DW |
2130 | if (!count) |
2131 | continue; | |
bf9bccc1 DW |
2132 | for (j = 0; j < count; j++) { |
2133 | struct nd_namespace_label *label; | |
2134 | ||
ae8219f1 DW |
2135 | label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL); |
2136 | if (!label_ent) | |
2137 | break; | |
bf9bccc1 | 2138 | label = nd_label_active(ndd, j); |
ae8219f1 DW |
2139 | label_ent->label = label; |
2140 | ||
2141 | mutex_lock(&nd_mapping->lock); | |
2142 | list_add_tail(&label_ent->list, &nd_mapping->labels); | |
2143 | mutex_unlock(&nd_mapping->lock); | |
bf9bccc1 | 2144 | } |
ae8219f1 | 2145 | |
a2d1c7a6 DW |
2146 | if (j < count) |
2147 | break; | |
2148 | } | |
ae8219f1 | 2149 | |
d9cee9f8 DW |
2150 | if (i < nd_region->ndr_mappings) |
2151 | rc = -ENOMEM; | |
2152 | ||
2153 | out: | |
2154 | if (rc) { | |
a2d1c7a6 | 2155 | deactivate_labels(nd_region); |
d9cee9f8 | 2156 | return rc; |
bf9bccc1 DW |
2157 | } |
2158 | ||
a2d1c7a6 | 2159 | return devm_add_action_or_reset(&nd_region->dev, deactivate_labels, |
d9cee9f8 | 2160 | nd_region); |
bf9bccc1 DW |
2161 | } |
2162 | ||
3d88002e DW |
2163 | int nd_region_register_namespaces(struct nd_region *nd_region, int *err) |
2164 | { | |
2165 | struct device **devs = NULL; | |
bf9bccc1 | 2166 | int i, rc = 0, type; |
3d88002e DW |
2167 | |
2168 | *err = 0; | |
bf9bccc1 DW |
2169 | nvdimm_bus_lock(&nd_region->dev); |
2170 | rc = init_active_labels(nd_region); | |
2171 | if (rc) { | |
2172 | nvdimm_bus_unlock(&nd_region->dev); | |
2173 | return rc; | |
2174 | } | |
2175 | ||
2176 | type = nd_region_to_nstype(nd_region); | |
2177 | switch (type) { | |
3d88002e DW |
2178 | case ND_DEVICE_NAMESPACE_IO: |
2179 | devs = create_namespace_io(nd_region); | |
2180 | break; | |
bf9bccc1 | 2181 | case ND_DEVICE_NAMESPACE_PMEM: |
8a5f50d3 | 2182 | devs = create_namespaces(nd_region); |
1b40e09a | 2183 | break; |
3d88002e DW |
2184 | default: |
2185 | break; | |
2186 | } | |
bf9bccc1 | 2187 | nvdimm_bus_unlock(&nd_region->dev); |
3d88002e DW |
2188 | |
2189 | if (!devs) | |
2190 | return -ENODEV; | |
2191 | ||
2192 | for (i = 0; devs[i]; i++) { | |
2193 | struct device *dev = devs[i]; | |
1b40e09a | 2194 | int id; |
3d88002e | 2195 | |
84bd3690 | 2196 | if (type == ND_DEVICE_NAMESPACE_PMEM) { |
0e3b0d12 DW |
2197 | struct nd_namespace_pmem *nspm; |
2198 | ||
2199 | nspm = to_nd_namespace_pmem(dev); | |
2200 | id = ida_simple_get(&nd_region->ns_ida, 0, 0, | |
84bd3690 | 2201 | GFP_KERNEL); |
0e3b0d12 | 2202 | nspm->id = id; |
1b40e09a DW |
2203 | } else |
2204 | id = i; | |
2205 | ||
2206 | if (id < 0) | |
2207 | break; | |
2208 | dev_set_name(dev, "namespace%d.%d", nd_region->id, id); | |
4a0079bc DW |
2209 | device_initialize(dev); |
2210 | lockdep_set_class(&dev->mutex, &nvdimm_namespace_key); | |
3d88002e DW |
2211 | nd_device_register(dev); |
2212 | } | |
1b40e09a DW |
2213 | if (i) |
2214 | nd_region->ns_seed = devs[0]; | |
2215 | ||
2216 | if (devs[i]) { | |
2217 | int j; | |
2218 | ||
2219 | for (j = i; devs[j]; j++) { | |
2220 | struct device *dev = devs[j]; | |
2221 | ||
2222 | device_initialize(dev); | |
2223 | put_device(dev); | |
2224 | } | |
2225 | *err = j - i; | |
2226 | /* | |
2227 | * All of the namespaces we tried to register failed, so | |
2228 | * fail region activation. | |
2229 | */ | |
2230 | if (*err == 0) | |
2231 | rc = -ENODEV; | |
2232 | } | |
3d88002e DW |
2233 | kfree(devs); |
2234 | ||
1b40e09a DW |
2235 | if (rc == -ENODEV) |
2236 | return rc; | |
2237 | ||
3d88002e DW |
2238 | return i; |
2239 | } |