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