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> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/nd.h> | |
bf9bccc1 | 17 | #include "nd-core.h" |
3d88002e DW |
18 | #include "nd.h" |
19 | ||
20 | static void namespace_io_release(struct device *dev) | |
21 | { | |
22 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); | |
23 | ||
24 | kfree(nsio); | |
25 | } | |
26 | ||
bf9bccc1 DW |
27 | static void namespace_pmem_release(struct device *dev) |
28 | { | |
29 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
30 | ||
31 | kfree(nspm->alt_name); | |
32 | kfree(nspm->uuid); | |
33 | kfree(nspm); | |
34 | } | |
35 | ||
36 | static void namespace_blk_release(struct device *dev) | |
37 | { | |
1b40e09a DW |
38 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
39 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
40 | ||
41 | if (nsblk->id >= 0) | |
42 | ida_simple_remove(&nd_region->ns_ida, nsblk->id); | |
43 | kfree(nsblk->alt_name); | |
44 | kfree(nsblk->uuid); | |
45 | kfree(nsblk->res); | |
46 | kfree(nsblk); | |
bf9bccc1 DW |
47 | } |
48 | ||
3d88002e DW |
49 | static struct device_type namespace_io_device_type = { |
50 | .name = "nd_namespace_io", | |
51 | .release = namespace_io_release, | |
52 | }; | |
53 | ||
bf9bccc1 DW |
54 | static struct device_type namespace_pmem_device_type = { |
55 | .name = "nd_namespace_pmem", | |
56 | .release = namespace_pmem_release, | |
57 | }; | |
58 | ||
59 | static struct device_type namespace_blk_device_type = { | |
60 | .name = "nd_namespace_blk", | |
61 | .release = namespace_blk_release, | |
62 | }; | |
63 | ||
64 | static bool is_namespace_pmem(struct device *dev) | |
65 | { | |
66 | return dev ? dev->type == &namespace_pmem_device_type : false; | |
67 | } | |
68 | ||
69 | static bool is_namespace_blk(struct device *dev) | |
70 | { | |
71 | return dev ? dev->type == &namespace_blk_device_type : false; | |
72 | } | |
73 | ||
74 | static bool is_namespace_io(struct device *dev) | |
75 | { | |
76 | return dev ? dev->type == &namespace_io_device_type : false; | |
77 | } | |
78 | ||
5212e11f VV |
79 | const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, |
80 | char *name) | |
81 | { | |
82 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); | |
83 | const char *suffix = ""; | |
84 | ||
85 | if (ndns->claim && is_nd_btt(ndns->claim)) | |
86 | suffix = "s"; | |
87 | ||
88 | if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) | |
89 | sprintf(name, "pmem%d%s", nd_region->id, suffix); | |
90 | else if (is_namespace_blk(&ndns->dev)) { | |
91 | struct nd_namespace_blk *nsblk; | |
92 | ||
93 | nsblk = to_nd_namespace_blk(&ndns->dev); | |
94 | sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, suffix); | |
95 | } else { | |
96 | return NULL; | |
97 | } | |
98 | ||
99 | return name; | |
100 | } | |
101 | EXPORT_SYMBOL(nvdimm_namespace_disk_name); | |
102 | ||
3d88002e DW |
103 | static ssize_t nstype_show(struct device *dev, |
104 | struct device_attribute *attr, char *buf) | |
105 | { | |
106 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
107 | ||
108 | return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region)); | |
109 | } | |
110 | static DEVICE_ATTR_RO(nstype); | |
111 | ||
bf9bccc1 DW |
112 | static ssize_t __alt_name_store(struct device *dev, const char *buf, |
113 | const size_t len) | |
114 | { | |
115 | char *input, *pos, *alt_name, **ns_altname; | |
116 | ssize_t rc; | |
117 | ||
118 | if (is_namespace_pmem(dev)) { | |
119 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
120 | ||
121 | ns_altname = &nspm->alt_name; | |
122 | } else if (is_namespace_blk(dev)) { | |
1b40e09a DW |
123 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
124 | ||
125 | ns_altname = &nsblk->alt_name; | |
bf9bccc1 DW |
126 | } else |
127 | return -ENXIO; | |
128 | ||
8c2f7e86 | 129 | if (dev->driver || to_ndns(dev)->claim) |
bf9bccc1 DW |
130 | return -EBUSY; |
131 | ||
132 | input = kmemdup(buf, len + 1, GFP_KERNEL); | |
133 | if (!input) | |
134 | return -ENOMEM; | |
135 | ||
136 | input[len] = '\0'; | |
137 | pos = strim(input); | |
138 | if (strlen(pos) + 1 > NSLABEL_NAME_LEN) { | |
139 | rc = -EINVAL; | |
140 | goto out; | |
141 | } | |
142 | ||
143 | alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL); | |
144 | if (!alt_name) { | |
145 | rc = -ENOMEM; | |
146 | goto out; | |
147 | } | |
148 | kfree(*ns_altname); | |
149 | *ns_altname = alt_name; | |
150 | sprintf(*ns_altname, "%s", pos); | |
151 | rc = len; | |
152 | ||
153 | out: | |
154 | kfree(input); | |
155 | return rc; | |
156 | } | |
157 | ||
1b40e09a DW |
158 | static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk) |
159 | { | |
8c2f7e86 | 160 | struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); |
1b40e09a DW |
161 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; |
162 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
163 | struct nd_label_id label_id; | |
164 | resource_size_t size = 0; | |
165 | struct resource *res; | |
166 | ||
167 | if (!nsblk->uuid) | |
168 | return 0; | |
169 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); | |
170 | for_each_dpa_resource(ndd, res) | |
171 | if (strcmp(res->name, label_id.id) == 0) | |
172 | size += resource_size(res); | |
173 | return size; | |
174 | } | |
175 | ||
047fc8a1 RZ |
176 | static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) |
177 | { | |
178 | struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent); | |
179 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; | |
180 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
181 | struct nd_label_id label_id; | |
182 | struct resource *res; | |
183 | int count, i; | |
184 | ||
185 | if (!nsblk->uuid || !nsblk->lbasize || !ndd) | |
186 | return false; | |
187 | ||
188 | count = 0; | |
189 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); | |
190 | for_each_dpa_resource(ndd, res) { | |
191 | if (strcmp(res->name, label_id.id) != 0) | |
192 | continue; | |
193 | /* | |
194 | * Resources with unacknoweldged adjustments indicate a | |
195 | * failure to update labels | |
196 | */ | |
197 | if (res->flags & DPA_RESOURCE_ADJUSTED) | |
198 | return false; | |
199 | count++; | |
200 | } | |
201 | ||
202 | /* These values match after a successful label update */ | |
203 | if (count != nsblk->num_resources) | |
204 | return false; | |
205 | ||
206 | for (i = 0; i < nsblk->num_resources; i++) { | |
207 | struct resource *found = NULL; | |
208 | ||
209 | for_each_dpa_resource(ndd, res) | |
210 | if (res == nsblk->res[i]) { | |
211 | found = res; | |
212 | break; | |
213 | } | |
214 | /* stale resource */ | |
215 | if (!found) | |
216 | return false; | |
217 | } | |
218 | ||
219 | return true; | |
220 | } | |
221 | ||
222 | resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk) | |
223 | { | |
224 | resource_size_t size; | |
225 | ||
226 | nvdimm_bus_lock(&nsblk->common.dev); | |
227 | size = __nd_namespace_blk_validate(nsblk); | |
228 | nvdimm_bus_unlock(&nsblk->common.dev); | |
229 | ||
230 | return size; | |
231 | } | |
232 | EXPORT_SYMBOL(nd_namespace_blk_validate); | |
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); | |
257 | } else if (is_namespace_blk(dev)) { | |
0ba1c634 DW |
258 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
259 | resource_size_t size = nd_namespace_blk_size(nsblk); | |
260 | ||
261 | if (size == 0 && nsblk->uuid) | |
262 | /* delete allocation */; | |
263 | else if (!nsblk->uuid || !nsblk->lbasize) | |
264 | return 0; | |
265 | ||
266 | return nd_blk_namespace_label_update(nd_region, nsblk, size); | |
f524bf27 DW |
267 | } else |
268 | return -ENXIO; | |
269 | } | |
270 | ||
bf9bccc1 DW |
271 | static ssize_t alt_name_store(struct device *dev, |
272 | struct device_attribute *attr, const char *buf, size_t len) | |
273 | { | |
f524bf27 | 274 | struct nd_region *nd_region = to_nd_region(dev->parent); |
bf9bccc1 DW |
275 | ssize_t rc; |
276 | ||
277 | device_lock(dev); | |
278 | nvdimm_bus_lock(dev); | |
279 | wait_nvdimm_bus_probe_idle(dev); | |
280 | rc = __alt_name_store(dev, buf, len); | |
f524bf27 DW |
281 | if (rc >= 0) |
282 | rc = nd_namespace_label_update(nd_region, dev); | |
bf9bccc1 DW |
283 | dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc); |
284 | nvdimm_bus_unlock(dev); | |
285 | device_unlock(dev); | |
286 | ||
f524bf27 | 287 | return rc < 0 ? rc : len; |
bf9bccc1 DW |
288 | } |
289 | ||
290 | static ssize_t alt_name_show(struct device *dev, | |
291 | struct device_attribute *attr, char *buf) | |
292 | { | |
293 | char *ns_altname; | |
294 | ||
295 | if (is_namespace_pmem(dev)) { | |
296 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
297 | ||
298 | ns_altname = nspm->alt_name; | |
299 | } else if (is_namespace_blk(dev)) { | |
1b40e09a DW |
300 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
301 | ||
302 | ns_altname = nsblk->alt_name; | |
bf9bccc1 DW |
303 | } else |
304 | return -ENXIO; | |
305 | ||
306 | return sprintf(buf, "%s\n", ns_altname ? ns_altname : ""); | |
307 | } | |
308 | static DEVICE_ATTR_RW(alt_name); | |
309 | ||
310 | static int scan_free(struct nd_region *nd_region, | |
311 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, | |
312 | resource_size_t n) | |
313 | { | |
314 | bool is_blk = strncmp(label_id->id, "blk", 3) == 0; | |
315 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
316 | int rc = 0; | |
317 | ||
318 | while (n) { | |
319 | struct resource *res, *last; | |
320 | resource_size_t new_start; | |
321 | ||
322 | last = NULL; | |
323 | for_each_dpa_resource(ndd, res) | |
324 | if (strcmp(res->name, label_id->id) == 0) | |
325 | last = res; | |
326 | res = last; | |
327 | if (!res) | |
328 | return 0; | |
329 | ||
330 | if (n >= resource_size(res)) { | |
331 | n -= resource_size(res); | |
332 | nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc); | |
333 | nvdimm_free_dpa(ndd, res); | |
334 | /* retry with last resource deleted */ | |
335 | continue; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Keep BLK allocations relegated to high DPA as much as | |
340 | * possible | |
341 | */ | |
342 | if (is_blk) | |
343 | new_start = res->start + n; | |
344 | else | |
345 | new_start = res->start; | |
346 | ||
347 | rc = adjust_resource(res, new_start, resource_size(res) - n); | |
1b40e09a DW |
348 | if (rc == 0) |
349 | res->flags |= DPA_RESOURCE_ADJUSTED; | |
bf9bccc1 DW |
350 | nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc); |
351 | break; | |
352 | } | |
353 | ||
354 | return rc; | |
355 | } | |
356 | ||
357 | /** | |
358 | * shrink_dpa_allocation - for each dimm in region free n bytes for label_id | |
359 | * @nd_region: the set of dimms to reclaim @n bytes from | |
360 | * @label_id: unique identifier for the namespace consuming this dpa range | |
361 | * @n: number of bytes per-dimm to release | |
362 | * | |
363 | * Assumes resources are ordered. Starting from the end try to | |
364 | * adjust_resource() the allocation to @n, but if @n is larger than the | |
365 | * allocation delete it and find the 'new' last allocation in the label | |
366 | * set. | |
367 | */ | |
368 | static int shrink_dpa_allocation(struct nd_region *nd_region, | |
369 | struct nd_label_id *label_id, resource_size_t n) | |
370 | { | |
371 | int i; | |
372 | ||
373 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
374 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
375 | int rc; | |
376 | ||
377 | rc = scan_free(nd_region, nd_mapping, label_id, n); | |
378 | if (rc) | |
379 | return rc; | |
380 | } | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
385 | static resource_size_t init_dpa_allocation(struct nd_label_id *label_id, | |
386 | struct nd_region *nd_region, struct nd_mapping *nd_mapping, | |
387 | resource_size_t n) | |
388 | { | |
389 | bool is_blk = strncmp(label_id->id, "blk", 3) == 0; | |
390 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
391 | resource_size_t first_dpa; | |
392 | struct resource *res; | |
393 | int rc = 0; | |
394 | ||
395 | /* allocate blk from highest dpa first */ | |
396 | if (is_blk) | |
397 | first_dpa = nd_mapping->start + nd_mapping->size - n; | |
398 | else | |
399 | first_dpa = nd_mapping->start; | |
400 | ||
401 | /* first resource allocation for this label-id or dimm */ | |
402 | res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n); | |
403 | if (!res) | |
404 | rc = -EBUSY; | |
405 | ||
406 | nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc); | |
407 | return rc ? n : 0; | |
408 | } | |
409 | ||
1b40e09a DW |
410 | static bool space_valid(bool is_pmem, bool is_reserve, |
411 | struct nd_label_id *label_id, struct resource *res) | |
bf9bccc1 DW |
412 | { |
413 | /* | |
414 | * For BLK-space any space is valid, for PMEM-space, it must be | |
1b40e09a DW |
415 | * contiguous with an existing allocation unless we are |
416 | * reserving pmem. | |
bf9bccc1 | 417 | */ |
1b40e09a | 418 | if (is_reserve || !is_pmem) |
bf9bccc1 DW |
419 | return true; |
420 | if (!res || strcmp(res->name, label_id->id) == 0) | |
421 | return true; | |
422 | return false; | |
423 | } | |
424 | ||
425 | enum alloc_loc { | |
426 | ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER, | |
427 | }; | |
428 | ||
429 | static resource_size_t scan_allocate(struct nd_region *nd_region, | |
430 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id, | |
431 | resource_size_t n) | |
432 | { | |
433 | resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1; | |
1b40e09a | 434 | bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0; |
bf9bccc1 DW |
435 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; |
436 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
437 | const resource_size_t to_allocate = n; | |
438 | struct resource *res; | |
439 | int first; | |
440 | ||
441 | retry: | |
442 | first = 0; | |
443 | for_each_dpa_resource(ndd, res) { | |
444 | resource_size_t allocate, available = 0, free_start, free_end; | |
445 | struct resource *next = res->sibling, *new_res = NULL; | |
446 | enum alloc_loc loc = ALLOC_ERR; | |
447 | const char *action; | |
448 | int rc = 0; | |
449 | ||
450 | /* ignore resources outside this nd_mapping */ | |
451 | if (res->start > mapping_end) | |
452 | continue; | |
453 | if (res->end < nd_mapping->start) | |
454 | continue; | |
455 | ||
456 | /* space at the beginning of the mapping */ | |
457 | if (!first++ && res->start > nd_mapping->start) { | |
458 | free_start = nd_mapping->start; | |
459 | available = res->start - free_start; | |
1b40e09a | 460 | if (space_valid(is_pmem, is_reserve, label_id, NULL)) |
bf9bccc1 DW |
461 | loc = ALLOC_BEFORE; |
462 | } | |
463 | ||
464 | /* space between allocations */ | |
465 | if (!loc && next) { | |
466 | free_start = res->start + resource_size(res); | |
467 | free_end = min(mapping_end, next->start - 1); | |
1b40e09a | 468 | if (space_valid(is_pmem, is_reserve, label_id, res) |
bf9bccc1 DW |
469 | && free_start < free_end) { |
470 | available = free_end + 1 - free_start; | |
471 | loc = ALLOC_MID; | |
472 | } | |
473 | } | |
474 | ||
475 | /* space at the end of the mapping */ | |
476 | if (!loc && !next) { | |
477 | free_start = res->start + resource_size(res); | |
478 | free_end = mapping_end; | |
1b40e09a | 479 | if (space_valid(is_pmem, is_reserve, label_id, res) |
bf9bccc1 DW |
480 | && free_start < free_end) { |
481 | available = free_end + 1 - free_start; | |
482 | loc = ALLOC_AFTER; | |
483 | } | |
484 | } | |
485 | ||
486 | if (!loc || !available) | |
487 | continue; | |
488 | allocate = min(available, n); | |
489 | switch (loc) { | |
490 | case ALLOC_BEFORE: | |
491 | if (strcmp(res->name, label_id->id) == 0) { | |
492 | /* adjust current resource up */ | |
1b40e09a | 493 | if (is_pmem && !is_reserve) |
bf9bccc1 DW |
494 | return n; |
495 | rc = adjust_resource(res, res->start - allocate, | |
496 | resource_size(res) + allocate); | |
497 | action = "cur grow up"; | |
498 | } else | |
499 | action = "allocate"; | |
500 | break; | |
501 | case ALLOC_MID: | |
502 | if (strcmp(next->name, label_id->id) == 0) { | |
503 | /* adjust next resource up */ | |
1b40e09a | 504 | if (is_pmem && !is_reserve) |
bf9bccc1 DW |
505 | return n; |
506 | rc = adjust_resource(next, next->start | |
507 | - allocate, resource_size(next) | |
508 | + allocate); | |
509 | new_res = next; | |
510 | action = "next grow up"; | |
511 | } else if (strcmp(res->name, label_id->id) == 0) { | |
512 | action = "grow down"; | |
513 | } else | |
514 | action = "allocate"; | |
515 | break; | |
516 | case ALLOC_AFTER: | |
517 | if (strcmp(res->name, label_id->id) == 0) | |
518 | action = "grow down"; | |
519 | else | |
520 | action = "allocate"; | |
521 | break; | |
522 | default: | |
523 | return n; | |
524 | } | |
525 | ||
526 | if (strcmp(action, "allocate") == 0) { | |
527 | /* BLK allocate bottom up */ | |
528 | if (!is_pmem) | |
529 | free_start += available - allocate; | |
1b40e09a | 530 | else if (!is_reserve && free_start != nd_mapping->start) |
bf9bccc1 DW |
531 | return n; |
532 | ||
533 | new_res = nvdimm_allocate_dpa(ndd, label_id, | |
534 | free_start, allocate); | |
535 | if (!new_res) | |
536 | rc = -EBUSY; | |
537 | } else if (strcmp(action, "grow down") == 0) { | |
538 | /* adjust current resource down */ | |
539 | rc = adjust_resource(res, res->start, resource_size(res) | |
540 | + allocate); | |
1b40e09a DW |
541 | if (rc == 0) |
542 | res->flags |= DPA_RESOURCE_ADJUSTED; | |
bf9bccc1 DW |
543 | } |
544 | ||
545 | if (!new_res) | |
546 | new_res = res; | |
547 | ||
548 | nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n", | |
549 | action, loc, rc); | |
550 | ||
551 | if (rc) | |
552 | return n; | |
553 | ||
554 | n -= allocate; | |
555 | if (n) { | |
556 | /* | |
557 | * Retry scan with newly inserted resources. | |
558 | * For example, if we did an ALLOC_BEFORE | |
559 | * insertion there may also have been space | |
560 | * available for an ALLOC_AFTER insertion, so we | |
561 | * need to check this same resource again | |
562 | */ | |
563 | goto retry; | |
564 | } else | |
565 | return 0; | |
566 | } | |
567 | ||
1b40e09a DW |
568 | /* |
569 | * If we allocated nothing in the BLK case it may be because we are in | |
570 | * an initial "pmem-reserve pass". Only do an initial BLK allocation | |
571 | * when none of the DPA space is reserved. | |
572 | */ | |
573 | if ((is_pmem || !ndd->dpa.child) && n == to_allocate) | |
bf9bccc1 DW |
574 | return init_dpa_allocation(label_id, nd_region, nd_mapping, n); |
575 | return n; | |
576 | } | |
577 | ||
1b40e09a DW |
578 | static int merge_dpa(struct nd_region *nd_region, |
579 | struct nd_mapping *nd_mapping, struct nd_label_id *label_id) | |
580 | { | |
581 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
582 | struct resource *res; | |
583 | ||
584 | if (strncmp("pmem", label_id->id, 4) == 0) | |
585 | return 0; | |
586 | retry: | |
587 | for_each_dpa_resource(ndd, res) { | |
588 | int rc; | |
589 | struct resource *next = res->sibling; | |
590 | resource_size_t end = res->start + resource_size(res); | |
591 | ||
592 | if (!next || strcmp(res->name, label_id->id) != 0 | |
593 | || strcmp(next->name, label_id->id) != 0 | |
594 | || end != next->start) | |
595 | continue; | |
596 | end += resource_size(next); | |
597 | nvdimm_free_dpa(ndd, next); | |
598 | rc = adjust_resource(res, res->start, end - res->start); | |
599 | nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc); | |
600 | if (rc) | |
601 | return rc; | |
602 | res->flags |= DPA_RESOURCE_ADJUSTED; | |
603 | goto retry; | |
604 | } | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
609 | static int __reserve_free_pmem(struct device *dev, void *data) | |
610 | { | |
611 | struct nvdimm *nvdimm = data; | |
612 | struct nd_region *nd_region; | |
613 | struct nd_label_id label_id; | |
614 | int i; | |
615 | ||
616 | if (!is_nd_pmem(dev)) | |
617 | return 0; | |
618 | ||
619 | nd_region = to_nd_region(dev); | |
620 | if (nd_region->ndr_mappings == 0) | |
621 | return 0; | |
622 | ||
623 | memset(&label_id, 0, sizeof(label_id)); | |
624 | strcat(label_id.id, "pmem-reserve"); | |
625 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
626 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
627 | resource_size_t n, rem = 0; | |
628 | ||
629 | if (nd_mapping->nvdimm != nvdimm) | |
630 | continue; | |
631 | ||
632 | n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem); | |
633 | if (n == 0) | |
634 | return 0; | |
635 | rem = scan_allocate(nd_region, nd_mapping, &label_id, n); | |
636 | dev_WARN_ONCE(&nd_region->dev, rem, | |
637 | "pmem reserve underrun: %#llx of %#llx bytes\n", | |
638 | (unsigned long long) n - rem, | |
639 | (unsigned long long) n); | |
640 | return rem ? -ENXIO : 0; | |
641 | } | |
642 | ||
643 | return 0; | |
644 | } | |
645 | ||
646 | static void release_free_pmem(struct nvdimm_bus *nvdimm_bus, | |
647 | struct nd_mapping *nd_mapping) | |
648 | { | |
649 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
650 | struct resource *res, *_res; | |
651 | ||
652 | for_each_dpa_resource_safe(ndd, res, _res) | |
653 | if (strcmp(res->name, "pmem-reserve") == 0) | |
654 | nvdimm_free_dpa(ndd, res); | |
655 | } | |
656 | ||
657 | static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus, | |
658 | struct nd_mapping *nd_mapping) | |
659 | { | |
660 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
661 | int rc; | |
662 | ||
663 | rc = device_for_each_child(&nvdimm_bus->dev, nvdimm, | |
664 | __reserve_free_pmem); | |
665 | if (rc) | |
666 | release_free_pmem(nvdimm_bus, nd_mapping); | |
667 | return rc; | |
668 | } | |
669 | ||
bf9bccc1 DW |
670 | /** |
671 | * grow_dpa_allocation - for each dimm allocate n bytes for @label_id | |
672 | * @nd_region: the set of dimms to allocate @n more bytes from | |
673 | * @label_id: unique identifier for the namespace consuming this dpa range | |
674 | * @n: number of bytes per-dimm to add to the existing allocation | |
675 | * | |
676 | * Assumes resources are ordered. For BLK regions, first consume | |
677 | * BLK-only available DPA free space, then consume PMEM-aliased DPA | |
678 | * space starting at the highest DPA. For PMEM regions start | |
679 | * allocations from the start of an interleave set and end at the first | |
680 | * BLK allocation or the end of the interleave set, whichever comes | |
681 | * first. | |
682 | */ | |
683 | static int grow_dpa_allocation(struct nd_region *nd_region, | |
684 | struct nd_label_id *label_id, resource_size_t n) | |
685 | { | |
1b40e09a DW |
686 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev); |
687 | bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0; | |
bf9bccc1 DW |
688 | int i; |
689 | ||
690 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
691 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1b40e09a DW |
692 | resource_size_t rem = n; |
693 | int rc, j; | |
694 | ||
695 | /* | |
696 | * In the BLK case try once with all unallocated PMEM | |
697 | * reserved, and once without | |
698 | */ | |
699 | for (j = is_pmem; j < 2; j++) { | |
700 | bool blk_only = j == 0; | |
701 | ||
702 | if (blk_only) { | |
703 | rc = reserve_free_pmem(nvdimm_bus, nd_mapping); | |
704 | if (rc) | |
705 | return rc; | |
706 | } | |
707 | rem = scan_allocate(nd_region, nd_mapping, | |
708 | label_id, rem); | |
709 | if (blk_only) | |
710 | release_free_pmem(nvdimm_bus, nd_mapping); | |
bf9bccc1 | 711 | |
1b40e09a DW |
712 | /* try again and allow encroachments into PMEM */ |
713 | if (rem == 0) | |
714 | break; | |
715 | } | |
716 | ||
717 | dev_WARN_ONCE(&nd_region->dev, rem, | |
718 | "allocation underrun: %#llx of %#llx bytes\n", | |
719 | (unsigned long long) n - rem, | |
720 | (unsigned long long) n); | |
721 | if (rem) | |
722 | return -ENXIO; | |
723 | ||
724 | rc = merge_dpa(nd_region, nd_mapping, label_id); | |
bf9bccc1 DW |
725 | if (rc) |
726 | return rc; | |
727 | } | |
728 | ||
729 | return 0; | |
730 | } | |
731 | ||
732 | static void nd_namespace_pmem_set_size(struct nd_region *nd_region, | |
733 | struct nd_namespace_pmem *nspm, resource_size_t size) | |
734 | { | |
735 | struct resource *res = &nspm->nsio.res; | |
736 | ||
737 | res->start = nd_region->ndr_start; | |
738 | res->end = nd_region->ndr_start + size - 1; | |
739 | } | |
740 | ||
741 | static ssize_t __size_store(struct device *dev, unsigned long long val) | |
742 | { | |
743 | resource_size_t allocated = 0, available = 0; | |
744 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
745 | struct nd_mapping *nd_mapping; | |
746 | struct nvdimm_drvdata *ndd; | |
747 | struct nd_label_id label_id; | |
748 | u32 flags = 0, remainder; | |
749 | u8 *uuid = NULL; | |
750 | int rc, i; | |
751 | ||
8c2f7e86 | 752 | if (dev->driver || to_ndns(dev)->claim) |
bf9bccc1 DW |
753 | return -EBUSY; |
754 | ||
755 | if (is_namespace_pmem(dev)) { | |
756 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
757 | ||
758 | uuid = nspm->uuid; | |
759 | } else if (is_namespace_blk(dev)) { | |
1b40e09a DW |
760 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
761 | ||
762 | uuid = nsblk->uuid; | |
763 | flags = NSLABEL_FLAG_LOCAL; | |
bf9bccc1 DW |
764 | } |
765 | ||
766 | /* | |
767 | * We need a uuid for the allocation-label and dimm(s) on which | |
768 | * to store the label. | |
769 | */ | |
770 | if (!uuid || nd_region->ndr_mappings == 0) | |
771 | return -ENXIO; | |
772 | ||
773 | div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder); | |
774 | if (remainder) { | |
775 | dev_dbg(dev, "%llu is not %dK aligned\n", val, | |
776 | (SZ_4K * nd_region->ndr_mappings) / SZ_1K); | |
777 | return -EINVAL; | |
778 | } | |
779 | ||
780 | nd_label_gen_id(&label_id, uuid, flags); | |
781 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
782 | nd_mapping = &nd_region->mapping[i]; | |
783 | ndd = to_ndd(nd_mapping); | |
784 | ||
785 | /* | |
786 | * All dimms in an interleave set, or the base dimm for a blk | |
787 | * region, need to be enabled for the size to be changed. | |
788 | */ | |
789 | if (!ndd) | |
790 | return -ENXIO; | |
791 | ||
792 | allocated += nvdimm_allocated_dpa(ndd, &label_id); | |
793 | } | |
794 | available = nd_region_available_dpa(nd_region); | |
795 | ||
796 | if (val > available + allocated) | |
797 | return -ENOSPC; | |
798 | ||
799 | if (val == allocated) | |
800 | return 0; | |
801 | ||
802 | val = div_u64(val, nd_region->ndr_mappings); | |
803 | allocated = div_u64(allocated, nd_region->ndr_mappings); | |
804 | if (val < allocated) | |
805 | rc = shrink_dpa_allocation(nd_region, &label_id, | |
806 | allocated - val); | |
807 | else | |
808 | rc = grow_dpa_allocation(nd_region, &label_id, val - allocated); | |
809 | ||
810 | if (rc) | |
811 | return rc; | |
812 | ||
813 | if (is_namespace_pmem(dev)) { | |
814 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
815 | ||
816 | nd_namespace_pmem_set_size(nd_region, nspm, | |
817 | val * nd_region->ndr_mappings); | |
1b40e09a | 818 | } else if (is_namespace_blk(dev)) { |
8c2f7e86 DW |
819 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
820 | ||
1b40e09a DW |
821 | /* |
822 | * Try to delete the namespace if we deleted all of its | |
8c2f7e86 DW |
823 | * allocation, this is not the seed device for the |
824 | * region, and it is not actively claimed by a btt | |
825 | * instance. | |
1b40e09a | 826 | */ |
8c2f7e86 DW |
827 | if (val == 0 && nd_region->ns_seed != dev |
828 | && !nsblk->common.claim) | |
1b40e09a | 829 | nd_device_unregister(dev, ND_ASYNC); |
bf9bccc1 DW |
830 | } |
831 | ||
832 | return rc; | |
833 | } | |
834 | ||
835 | static ssize_t size_store(struct device *dev, | |
836 | struct device_attribute *attr, const char *buf, size_t len) | |
837 | { | |
f524bf27 | 838 | struct nd_region *nd_region = to_nd_region(dev->parent); |
bf9bccc1 DW |
839 | unsigned long long val; |
840 | u8 **uuid = NULL; | |
841 | int rc; | |
842 | ||
843 | rc = kstrtoull(buf, 0, &val); | |
844 | if (rc) | |
845 | return rc; | |
846 | ||
847 | device_lock(dev); | |
848 | nvdimm_bus_lock(dev); | |
849 | wait_nvdimm_bus_probe_idle(dev); | |
850 | rc = __size_store(dev, val); | |
f524bf27 DW |
851 | if (rc >= 0) |
852 | rc = nd_namespace_label_update(nd_region, dev); | |
bf9bccc1 DW |
853 | |
854 | if (is_namespace_pmem(dev)) { | |
855 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
856 | ||
857 | uuid = &nspm->uuid; | |
858 | } else if (is_namespace_blk(dev)) { | |
1b40e09a DW |
859 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
860 | ||
861 | uuid = &nsblk->uuid; | |
bf9bccc1 DW |
862 | } |
863 | ||
864 | if (rc == 0 && val == 0 && uuid) { | |
865 | /* setting size zero == 'delete namespace' */ | |
866 | kfree(*uuid); | |
867 | *uuid = NULL; | |
868 | } | |
869 | ||
870 | dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0 | |
871 | ? "fail" : "success", rc); | |
872 | ||
873 | nvdimm_bus_unlock(dev); | |
874 | device_unlock(dev); | |
875 | ||
f524bf27 | 876 | return rc < 0 ? rc : len; |
bf9bccc1 DW |
877 | } |
878 | ||
8c2f7e86 | 879 | resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns) |
bf9bccc1 | 880 | { |
8c2f7e86 | 881 | struct device *dev = &ndns->dev; |
1b40e09a | 882 | |
bf9bccc1 DW |
883 | if (is_namespace_pmem(dev)) { |
884 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
885 | ||
8c2f7e86 | 886 | return resource_size(&nspm->nsio.res); |
bf9bccc1 | 887 | } else if (is_namespace_blk(dev)) { |
8c2f7e86 | 888 | return nd_namespace_blk_size(to_nd_namespace_blk(dev)); |
bf9bccc1 DW |
889 | } else if (is_namespace_io(dev)) { |
890 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); | |
891 | ||
8c2f7e86 DW |
892 | return resource_size(&nsio->res); |
893 | } else | |
894 | WARN_ONCE(1, "unknown namespace type\n"); | |
895 | return 0; | |
896 | } | |
897 | ||
898 | resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) | |
899 | { | |
900 | resource_size_t size; | |
1b40e09a | 901 | |
8c2f7e86 DW |
902 | nvdimm_bus_lock(&ndns->dev); |
903 | size = __nvdimm_namespace_capacity(ndns); | |
904 | nvdimm_bus_unlock(&ndns->dev); | |
905 | ||
906 | return size; | |
907 | } | |
908 | EXPORT_SYMBOL(nvdimm_namespace_capacity); | |
909 | ||
910 | static ssize_t size_show(struct device *dev, | |
911 | struct device_attribute *attr, char *buf) | |
912 | { | |
913 | return sprintf(buf, "%llu\n", (unsigned long long) | |
914 | nvdimm_namespace_capacity(to_ndns(dev))); | |
bf9bccc1 DW |
915 | } |
916 | static DEVICE_ATTR(size, S_IRUGO, size_show, size_store); | |
917 | ||
918 | static ssize_t uuid_show(struct device *dev, | |
919 | struct device_attribute *attr, char *buf) | |
920 | { | |
921 | u8 *uuid; | |
922 | ||
923 | if (is_namespace_pmem(dev)) { | |
924 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
925 | ||
926 | uuid = nspm->uuid; | |
927 | } else if (is_namespace_blk(dev)) { | |
1b40e09a DW |
928 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
929 | ||
930 | uuid = nsblk->uuid; | |
bf9bccc1 DW |
931 | } else |
932 | return -ENXIO; | |
933 | ||
934 | if (uuid) | |
935 | return sprintf(buf, "%pUb\n", uuid); | |
936 | return sprintf(buf, "\n"); | |
937 | } | |
938 | ||
939 | /** | |
940 | * namespace_update_uuid - check for a unique uuid and whether we're "renaming" | |
941 | * @nd_region: parent region so we can updates all dimms in the set | |
942 | * @dev: namespace type for generating label_id | |
943 | * @new_uuid: incoming uuid | |
944 | * @old_uuid: reference to the uuid storage location in the namespace object | |
945 | */ | |
946 | static int namespace_update_uuid(struct nd_region *nd_region, | |
947 | struct device *dev, u8 *new_uuid, u8 **old_uuid) | |
948 | { | |
949 | u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0; | |
950 | struct nd_label_id old_label_id; | |
951 | struct nd_label_id new_label_id; | |
f524bf27 | 952 | int i; |
bf9bccc1 | 953 | |
f524bf27 DW |
954 | if (!nd_is_uuid_unique(dev, new_uuid)) |
955 | return -EINVAL; | |
bf9bccc1 DW |
956 | |
957 | if (*old_uuid == NULL) | |
958 | goto out; | |
959 | ||
f524bf27 DW |
960 | /* |
961 | * If we've already written a label with this uuid, then it's | |
962 | * too late to rename because we can't reliably update the uuid | |
963 | * without losing the old namespace. Userspace must delete this | |
964 | * namespace to abandon the old uuid. | |
965 | */ | |
966 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
967 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
968 | ||
969 | /* | |
970 | * This check by itself is sufficient because old_uuid | |
971 | * would be NULL above if this uuid did not exist in the | |
972 | * currently written set. | |
973 | * | |
974 | * FIXME: can we delete uuid with zero dpa allocated? | |
975 | */ | |
976 | if (nd_mapping->labels) | |
977 | return -EBUSY; | |
978 | } | |
979 | ||
bf9bccc1 DW |
980 | nd_label_gen_id(&old_label_id, *old_uuid, flags); |
981 | nd_label_gen_id(&new_label_id, new_uuid, flags); | |
982 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
983 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
984 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
985 | struct resource *res; | |
986 | ||
987 | for_each_dpa_resource(ndd, res) | |
988 | if (strcmp(res->name, old_label_id.id) == 0) | |
989 | sprintf((void *) res->name, "%s", | |
990 | new_label_id.id); | |
991 | } | |
992 | kfree(*old_uuid); | |
993 | out: | |
994 | *old_uuid = new_uuid; | |
995 | return 0; | |
996 | } | |
997 | ||
998 | static ssize_t uuid_store(struct device *dev, | |
999 | struct device_attribute *attr, const char *buf, size_t len) | |
1000 | { | |
1001 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
1002 | u8 *uuid = NULL; | |
8c2f7e86 | 1003 | ssize_t rc = 0; |
bf9bccc1 | 1004 | u8 **ns_uuid; |
bf9bccc1 DW |
1005 | |
1006 | if (is_namespace_pmem(dev)) { | |
1007 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1008 | ||
1009 | ns_uuid = &nspm->uuid; | |
1010 | } else if (is_namespace_blk(dev)) { | |
1b40e09a DW |
1011 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); |
1012 | ||
1013 | ns_uuid = &nsblk->uuid; | |
bf9bccc1 DW |
1014 | } else |
1015 | return -ENXIO; | |
1016 | ||
1017 | device_lock(dev); | |
1018 | nvdimm_bus_lock(dev); | |
1019 | wait_nvdimm_bus_probe_idle(dev); | |
8c2f7e86 DW |
1020 | if (to_ndns(dev)->claim) |
1021 | rc = -EBUSY; | |
1022 | if (rc >= 0) | |
1023 | rc = nd_uuid_store(dev, &uuid, buf, len); | |
bf9bccc1 DW |
1024 | if (rc >= 0) |
1025 | rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid); | |
f524bf27 DW |
1026 | if (rc >= 0) |
1027 | rc = nd_namespace_label_update(nd_region, dev); | |
1028 | else | |
1029 | kfree(uuid); | |
bf9bccc1 DW |
1030 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
1031 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); | |
1032 | nvdimm_bus_unlock(dev); | |
1033 | device_unlock(dev); | |
1034 | ||
f524bf27 | 1035 | return rc < 0 ? rc : len; |
bf9bccc1 DW |
1036 | } |
1037 | static DEVICE_ATTR_RW(uuid); | |
1038 | ||
1039 | static ssize_t resource_show(struct device *dev, | |
1040 | struct device_attribute *attr, char *buf) | |
1041 | { | |
1042 | struct resource *res; | |
1043 | ||
1044 | if (is_namespace_pmem(dev)) { | |
1045 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1046 | ||
1047 | res = &nspm->nsio.res; | |
1048 | } else if (is_namespace_io(dev)) { | |
1049 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); | |
1050 | ||
1051 | res = &nsio->res; | |
1052 | } else | |
1053 | return -ENXIO; | |
1054 | ||
1055 | /* no address to convey if the namespace has no allocation */ | |
1056 | if (resource_size(res) == 0) | |
1057 | return -ENXIO; | |
1058 | return sprintf(buf, "%#llx\n", (unsigned long long) res->start); | |
1059 | } | |
1060 | static DEVICE_ATTR_RO(resource); | |
1061 | ||
1b40e09a DW |
1062 | static const unsigned long ns_lbasize_supported[] = { 512, 0 }; |
1063 | ||
1064 | static ssize_t sector_size_show(struct device *dev, | |
1065 | struct device_attribute *attr, char *buf) | |
1066 | { | |
1067 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); | |
1068 | ||
1069 | if (!is_namespace_blk(dev)) | |
1070 | return -ENXIO; | |
1071 | ||
1072 | return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf); | |
1073 | } | |
1074 | ||
1075 | static ssize_t sector_size_store(struct device *dev, | |
1076 | struct device_attribute *attr, const char *buf, size_t len) | |
1077 | { | |
1078 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); | |
f524bf27 | 1079 | struct nd_region *nd_region = to_nd_region(dev->parent); |
8c2f7e86 | 1080 | ssize_t rc = 0; |
1b40e09a DW |
1081 | |
1082 | if (!is_namespace_blk(dev)) | |
1083 | return -ENXIO; | |
1084 | ||
1085 | device_lock(dev); | |
1086 | nvdimm_bus_lock(dev); | |
8c2f7e86 DW |
1087 | if (to_ndns(dev)->claim) |
1088 | rc = -EBUSY; | |
1089 | if (rc >= 0) | |
1090 | rc = nd_sector_size_store(dev, buf, &nsblk->lbasize, | |
1091 | ns_lbasize_supported); | |
f524bf27 DW |
1092 | if (rc >= 0) |
1093 | rc = nd_namespace_label_update(nd_region, dev); | |
1094 | dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__, | |
1095 | rc, rc < 0 ? "tried" : "wrote", buf, | |
1096 | buf[len - 1] == '\n' ? "" : "\n"); | |
1b40e09a DW |
1097 | nvdimm_bus_unlock(dev); |
1098 | device_unlock(dev); | |
1099 | ||
1100 | return rc ? rc : len; | |
1101 | } | |
1102 | static DEVICE_ATTR_RW(sector_size); | |
1103 | ||
0ba1c634 DW |
1104 | static ssize_t dpa_extents_show(struct device *dev, |
1105 | struct device_attribute *attr, char *buf) | |
1106 | { | |
1107 | struct nd_region *nd_region = to_nd_region(dev->parent); | |
1108 | struct nd_label_id label_id; | |
1109 | int count = 0, i; | |
1110 | u8 *uuid = NULL; | |
1111 | u32 flags = 0; | |
1112 | ||
1113 | nvdimm_bus_lock(dev); | |
1114 | if (is_namespace_pmem(dev)) { | |
1115 | struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev); | |
1116 | ||
1117 | uuid = nspm->uuid; | |
1118 | flags = 0; | |
1119 | } else if (is_namespace_blk(dev)) { | |
1120 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev); | |
1121 | ||
1122 | uuid = nsblk->uuid; | |
1123 | flags = NSLABEL_FLAG_LOCAL; | |
1124 | } | |
1125 | ||
1126 | if (!uuid) | |
1127 | goto out; | |
1128 | ||
1129 | nd_label_gen_id(&label_id, uuid, flags); | |
1130 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1131 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1132 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
1133 | struct resource *res; | |
1134 | ||
1135 | for_each_dpa_resource(ndd, res) | |
1136 | if (strcmp(res->name, label_id.id) == 0) | |
1137 | count++; | |
1138 | } | |
1139 | out: | |
1140 | nvdimm_bus_unlock(dev); | |
1141 | ||
1142 | return sprintf(buf, "%d\n", count); | |
1143 | } | |
1144 | static DEVICE_ATTR_RO(dpa_extents); | |
1145 | ||
8c2f7e86 DW |
1146 | static ssize_t holder_show(struct device *dev, |
1147 | struct device_attribute *attr, char *buf) | |
1148 | { | |
1149 | struct nd_namespace_common *ndns = to_ndns(dev); | |
1150 | ssize_t rc; | |
1151 | ||
1152 | device_lock(dev); | |
1153 | rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); | |
1154 | device_unlock(dev); | |
1155 | ||
1156 | return rc; | |
1157 | } | |
1158 | static DEVICE_ATTR_RO(holder); | |
1159 | ||
1160 | static ssize_t force_raw_store(struct device *dev, | |
1161 | struct device_attribute *attr, const char *buf, size_t len) | |
1162 | { | |
1163 | bool force_raw; | |
1164 | int rc = strtobool(buf, &force_raw); | |
1165 | ||
1166 | if (rc) | |
1167 | return rc; | |
1168 | ||
1169 | to_ndns(dev)->force_raw = force_raw; | |
1170 | return len; | |
1171 | } | |
1172 | ||
1173 | static ssize_t force_raw_show(struct device *dev, | |
1174 | struct device_attribute *attr, char *buf) | |
1175 | { | |
1176 | return sprintf(buf, "%d\n", to_ndns(dev)->force_raw); | |
1177 | } | |
1178 | static DEVICE_ATTR_RW(force_raw); | |
1179 | ||
3d88002e DW |
1180 | static struct attribute *nd_namespace_attributes[] = { |
1181 | &dev_attr_nstype.attr, | |
bf9bccc1 DW |
1182 | &dev_attr_size.attr, |
1183 | &dev_attr_uuid.attr, | |
8c2f7e86 | 1184 | &dev_attr_holder.attr, |
bf9bccc1 DW |
1185 | &dev_attr_resource.attr, |
1186 | &dev_attr_alt_name.attr, | |
8c2f7e86 | 1187 | &dev_attr_force_raw.attr, |
1b40e09a | 1188 | &dev_attr_sector_size.attr, |
0ba1c634 | 1189 | &dev_attr_dpa_extents.attr, |
3d88002e DW |
1190 | NULL, |
1191 | }; | |
1192 | ||
bf9bccc1 DW |
1193 | static umode_t namespace_visible(struct kobject *kobj, |
1194 | struct attribute *a, int n) | |
1195 | { | |
1196 | struct device *dev = container_of(kobj, struct device, kobj); | |
1197 | ||
1198 | if (a == &dev_attr_resource.attr) { | |
1199 | if (is_namespace_blk(dev)) | |
1200 | return 0; | |
1201 | return a->mode; | |
1202 | } | |
1203 | ||
1204 | if (is_namespace_pmem(dev) || is_namespace_blk(dev)) { | |
1205 | if (a == &dev_attr_size.attr) | |
1206 | return S_IWUSR | S_IRUGO; | |
1b40e09a DW |
1207 | |
1208 | if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr) | |
1209 | return 0; | |
1210 | ||
bf9bccc1 DW |
1211 | return a->mode; |
1212 | } | |
1213 | ||
8c2f7e86 DW |
1214 | if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr |
1215 | || a == &dev_attr_holder.attr | |
1216 | || a == &dev_attr_force_raw.attr) | |
bf9bccc1 DW |
1217 | return a->mode; |
1218 | ||
1219 | return 0; | |
1220 | } | |
1221 | ||
3d88002e DW |
1222 | static struct attribute_group nd_namespace_attribute_group = { |
1223 | .attrs = nd_namespace_attributes, | |
bf9bccc1 | 1224 | .is_visible = namespace_visible, |
3d88002e DW |
1225 | }; |
1226 | ||
1227 | static const struct attribute_group *nd_namespace_attribute_groups[] = { | |
1228 | &nd_device_attribute_group, | |
1229 | &nd_namespace_attribute_group, | |
1230 | NULL, | |
1231 | }; | |
1232 | ||
8c2f7e86 DW |
1233 | struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) |
1234 | { | |
1235 | struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL; | |
1236 | struct nd_namespace_common *ndns; | |
1237 | resource_size_t size; | |
1238 | ||
1239 | if (nd_btt) { | |
1240 | ndns = nd_btt->ndns; | |
1241 | if (!ndns) | |
1242 | return ERR_PTR(-ENODEV); | |
1243 | ||
1244 | /* | |
1245 | * Flush any in-progess probes / removals in the driver | |
1246 | * for the raw personality of this namespace. | |
1247 | */ | |
1248 | device_lock(&ndns->dev); | |
1249 | device_unlock(&ndns->dev); | |
1250 | if (ndns->dev.driver) { | |
1251 | dev_dbg(&ndns->dev, "is active, can't bind %s\n", | |
1252 | dev_name(&nd_btt->dev)); | |
1253 | return ERR_PTR(-EBUSY); | |
1254 | } | |
1255 | if (dev_WARN_ONCE(&ndns->dev, ndns->claim != &nd_btt->dev, | |
1256 | "host (%s) vs claim (%s) mismatch\n", | |
1257 | dev_name(&nd_btt->dev), | |
1258 | dev_name(ndns->claim))) | |
1259 | return ERR_PTR(-ENXIO); | |
1260 | } else { | |
1261 | ndns = to_ndns(dev); | |
1262 | if (ndns->claim) { | |
1263 | dev_dbg(dev, "claimed by %s, failing probe\n", | |
1264 | dev_name(ndns->claim)); | |
1265 | ||
1266 | return ERR_PTR(-ENXIO); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | size = nvdimm_namespace_capacity(ndns); | |
1271 | if (size < ND_MIN_NAMESPACE_SIZE) { | |
1272 | dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", | |
1273 | &size, ND_MIN_NAMESPACE_SIZE); | |
1274 | return ERR_PTR(-ENODEV); | |
1275 | } | |
1276 | ||
1277 | if (is_namespace_pmem(&ndns->dev)) { | |
1278 | struct nd_namespace_pmem *nspm; | |
1279 | ||
1280 | nspm = to_nd_namespace_pmem(&ndns->dev); | |
1281 | if (!nspm->uuid) { | |
1282 | dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__); | |
1283 | return ERR_PTR(-ENODEV); | |
1284 | } | |
1285 | } else if (is_namespace_blk(&ndns->dev)) { | |
047fc8a1 RZ |
1286 | struct nd_namespace_blk *nsblk; |
1287 | ||
1288 | nsblk = to_nd_namespace_blk(&ndns->dev); | |
1289 | if (!nd_namespace_blk_validate(nsblk)) | |
1290 | return ERR_PTR(-ENODEV); | |
8c2f7e86 DW |
1291 | } |
1292 | ||
1293 | return ndns; | |
1294 | } | |
1295 | EXPORT_SYMBOL(nvdimm_namespace_common_probe); | |
1296 | ||
3d88002e DW |
1297 | static struct device **create_namespace_io(struct nd_region *nd_region) |
1298 | { | |
1299 | struct nd_namespace_io *nsio; | |
1300 | struct device *dev, **devs; | |
1301 | struct resource *res; | |
1302 | ||
1303 | nsio = kzalloc(sizeof(*nsio), GFP_KERNEL); | |
1304 | if (!nsio) | |
1305 | return NULL; | |
1306 | ||
1307 | devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); | |
1308 | if (!devs) { | |
1309 | kfree(nsio); | |
1310 | return NULL; | |
1311 | } | |
1312 | ||
8c2f7e86 | 1313 | dev = &nsio->common.dev; |
3d88002e DW |
1314 | dev->type = &namespace_io_device_type; |
1315 | dev->parent = &nd_region->dev; | |
1316 | res = &nsio->res; | |
1317 | res->name = dev_name(&nd_region->dev); | |
1318 | res->flags = IORESOURCE_MEM; | |
1319 | res->start = nd_region->ndr_start; | |
1320 | res->end = res->start + nd_region->ndr_size - 1; | |
1321 | ||
1322 | devs[0] = dev; | |
1323 | return devs; | |
1324 | } | |
1325 | ||
bf9bccc1 DW |
1326 | static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid, |
1327 | u64 cookie, u16 pos) | |
1328 | { | |
1329 | struct nd_namespace_label *found = NULL; | |
1330 | int i; | |
1331 | ||
1332 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1333 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1334 | struct nd_namespace_label *nd_label; | |
1335 | bool found_uuid = false; | |
1336 | int l; | |
1337 | ||
1338 | for_each_label(l, nd_label, nd_mapping->labels) { | |
1339 | u64 isetcookie = __le64_to_cpu(nd_label->isetcookie); | |
1340 | u16 position = __le16_to_cpu(nd_label->position); | |
1341 | u16 nlabel = __le16_to_cpu(nd_label->nlabel); | |
1342 | ||
1343 | if (isetcookie != cookie) | |
1344 | continue; | |
1345 | ||
1346 | if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0) | |
1347 | continue; | |
1348 | ||
1349 | if (found_uuid) { | |
1350 | dev_dbg(to_ndd(nd_mapping)->dev, | |
1351 | "%s duplicate entry for uuid\n", | |
1352 | __func__); | |
1353 | return false; | |
1354 | } | |
1355 | found_uuid = true; | |
1356 | if (nlabel != nd_region->ndr_mappings) | |
1357 | continue; | |
1358 | if (position != pos) | |
1359 | continue; | |
1360 | found = nd_label; | |
1361 | break; | |
1362 | } | |
1363 | if (found) | |
1364 | break; | |
1365 | } | |
1366 | return found != NULL; | |
1367 | } | |
1368 | ||
1369 | static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id) | |
1370 | { | |
1371 | struct nd_namespace_label *select = NULL; | |
1372 | int i; | |
1373 | ||
1374 | if (!pmem_id) | |
1375 | return -ENODEV; | |
1376 | ||
1377 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1378 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1379 | struct nd_namespace_label *nd_label; | |
1380 | u64 hw_start, hw_end, pmem_start, pmem_end; | |
1381 | int l; | |
1382 | ||
1383 | for_each_label(l, nd_label, nd_mapping->labels) | |
1384 | if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0) | |
1385 | break; | |
1386 | ||
1387 | if (!nd_label) { | |
1388 | WARN_ON(1); | |
1389 | return -EINVAL; | |
1390 | } | |
1391 | ||
1392 | select = nd_label; | |
1393 | /* | |
1394 | * Check that this label is compliant with the dpa | |
1395 | * range published in NFIT | |
1396 | */ | |
1397 | hw_start = nd_mapping->start; | |
1398 | hw_end = hw_start + nd_mapping->size; | |
1399 | pmem_start = __le64_to_cpu(select->dpa); | |
1400 | pmem_end = pmem_start + __le64_to_cpu(select->rawsize); | |
1401 | if (pmem_start == hw_start && pmem_end <= hw_end) | |
1402 | /* pass */; | |
1403 | else | |
1404 | return -EINVAL; | |
1405 | ||
1406 | nd_mapping->labels[0] = select; | |
1407 | nd_mapping->labels[1] = NULL; | |
1408 | } | |
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | /** | |
1413 | * find_pmem_label_set - validate interleave set labelling, retrieve label0 | |
1414 | * @nd_region: region with mappings to validate | |
1415 | */ | |
1416 | static int find_pmem_label_set(struct nd_region *nd_region, | |
1417 | struct nd_namespace_pmem *nspm) | |
1418 | { | |
1419 | u64 cookie = nd_region_interleave_set_cookie(nd_region); | |
1420 | struct nd_namespace_label *nd_label; | |
1421 | u8 select_id[NSLABEL_UUID_LEN]; | |
1422 | resource_size_t size = 0; | |
1423 | u8 *pmem_id = NULL; | |
1424 | int rc = -ENODEV, l; | |
1425 | u16 i; | |
1426 | ||
1427 | if (cookie == 0) | |
1428 | return -ENXIO; | |
1429 | ||
1430 | /* | |
1431 | * Find a complete set of labels by uuid. By definition we can start | |
1432 | * with any mapping as the reference label | |
1433 | */ | |
1434 | for_each_label(l, nd_label, nd_region->mapping[0].labels) { | |
1435 | u64 isetcookie = __le64_to_cpu(nd_label->isetcookie); | |
1436 | ||
1437 | if (isetcookie != cookie) | |
1438 | continue; | |
1439 | ||
1440 | for (i = 0; nd_region->ndr_mappings; i++) | |
1441 | if (!has_uuid_at_pos(nd_region, nd_label->uuid, | |
1442 | cookie, i)) | |
1443 | break; | |
1444 | if (i < nd_region->ndr_mappings) { | |
1445 | /* | |
1446 | * Give up if we don't find an instance of a | |
1447 | * uuid at each position (from 0 to | |
1448 | * nd_region->ndr_mappings - 1), or if we find a | |
1449 | * dimm with two instances of the same uuid. | |
1450 | */ | |
1451 | rc = -EINVAL; | |
1452 | goto err; | |
1453 | } else if (pmem_id) { | |
1454 | /* | |
1455 | * If there is more than one valid uuid set, we | |
1456 | * need userspace to clean this up. | |
1457 | */ | |
1458 | rc = -EBUSY; | |
1459 | goto err; | |
1460 | } | |
1461 | memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN); | |
1462 | pmem_id = select_id; | |
1463 | } | |
1464 | ||
1465 | /* | |
1466 | * Fix up each mapping's 'labels' to have the validated pmem label for | |
1467 | * that position at labels[0], and NULL at labels[1]. In the process, | |
1468 | * check that the namespace aligns with interleave-set. We know | |
1469 | * that it does not overlap with any blk namespaces by virtue of | |
1470 | * the dimm being enabled (i.e. nd_label_reserve_dpa() | |
1471 | * succeeded). | |
1472 | */ | |
1473 | rc = select_pmem_id(nd_region, pmem_id); | |
1474 | if (rc) | |
1475 | goto err; | |
1476 | ||
1477 | /* Calculate total size and populate namespace properties from label0 */ | |
1478 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1479 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1480 | struct nd_namespace_label *label0 = nd_mapping->labels[0]; | |
1481 | ||
1482 | size += __le64_to_cpu(label0->rawsize); | |
1483 | if (__le16_to_cpu(label0->position) != 0) | |
1484 | continue; | |
1485 | WARN_ON(nspm->alt_name || nspm->uuid); | |
1486 | nspm->alt_name = kmemdup((void __force *) label0->name, | |
1487 | NSLABEL_NAME_LEN, GFP_KERNEL); | |
1488 | nspm->uuid = kmemdup((void __force *) label0->uuid, | |
1489 | NSLABEL_UUID_LEN, GFP_KERNEL); | |
1490 | } | |
1491 | ||
1492 | if (!nspm->alt_name || !nspm->uuid) { | |
1493 | rc = -ENOMEM; | |
1494 | goto err; | |
1495 | } | |
1496 | ||
1497 | nd_namespace_pmem_set_size(nd_region, nspm, size); | |
1498 | ||
1499 | return 0; | |
1500 | err: | |
1501 | switch (rc) { | |
1502 | case -EINVAL: | |
1503 | dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__); | |
1504 | break; | |
1505 | case -ENODEV: | |
1506 | dev_dbg(&nd_region->dev, "%s: label not found\n", __func__); | |
1507 | break; | |
1508 | default: | |
1509 | dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n", | |
1510 | __func__, rc); | |
1511 | break; | |
1512 | } | |
1513 | return rc; | |
1514 | } | |
1515 | ||
1516 | static struct device **create_namespace_pmem(struct nd_region *nd_region) | |
1517 | { | |
1518 | struct nd_namespace_pmem *nspm; | |
1519 | struct device *dev, **devs; | |
1520 | struct resource *res; | |
1521 | int rc; | |
1522 | ||
1523 | nspm = kzalloc(sizeof(*nspm), GFP_KERNEL); | |
1524 | if (!nspm) | |
1525 | return NULL; | |
1526 | ||
8c2f7e86 | 1527 | dev = &nspm->nsio.common.dev; |
bf9bccc1 DW |
1528 | dev->type = &namespace_pmem_device_type; |
1529 | dev->parent = &nd_region->dev; | |
1530 | res = &nspm->nsio.res; | |
1531 | res->name = dev_name(&nd_region->dev); | |
1532 | res->flags = IORESOURCE_MEM; | |
1533 | rc = find_pmem_label_set(nd_region, nspm); | |
1534 | if (rc == -ENODEV) { | |
1535 | int i; | |
1536 | ||
1537 | /* Pass, try to permit namespace creation... */ | |
1538 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1539 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1540 | ||
1541 | kfree(nd_mapping->labels); | |
1542 | nd_mapping->labels = NULL; | |
1543 | } | |
1544 | ||
1545 | /* Publish a zero-sized namespace for userspace to configure. */ | |
1546 | nd_namespace_pmem_set_size(nd_region, nspm, 0); | |
1547 | ||
1548 | rc = 0; | |
1549 | } else if (rc) | |
1550 | goto err; | |
1551 | ||
1552 | devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL); | |
1553 | if (!devs) | |
1554 | goto err; | |
1555 | ||
1556 | devs[0] = dev; | |
1557 | return devs; | |
1558 | ||
1559 | err: | |
8c2f7e86 | 1560 | namespace_pmem_release(&nspm->nsio.common.dev); |
bf9bccc1 DW |
1561 | return NULL; |
1562 | } | |
1563 | ||
1b40e09a DW |
1564 | struct resource *nsblk_add_resource(struct nd_region *nd_region, |
1565 | struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk, | |
1566 | resource_size_t start) | |
1567 | { | |
1568 | struct nd_label_id label_id; | |
1569 | struct resource *res; | |
1570 | ||
1571 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); | |
1572 | res = krealloc(nsblk->res, | |
1573 | sizeof(void *) * (nsblk->num_resources + 1), | |
1574 | GFP_KERNEL); | |
1575 | if (!res) | |
1576 | return NULL; | |
1577 | nsblk->res = (struct resource **) res; | |
1578 | for_each_dpa_resource(ndd, res) | |
1579 | if (strcmp(res->name, label_id.id) == 0 | |
1580 | && res->start == start) { | |
1581 | nsblk->res[nsblk->num_resources++] = res; | |
1582 | return res; | |
1583 | } | |
1584 | return NULL; | |
1585 | } | |
1586 | ||
1587 | static struct device *nd_namespace_blk_create(struct nd_region *nd_region) | |
1588 | { | |
1589 | struct nd_namespace_blk *nsblk; | |
1590 | struct device *dev; | |
1591 | ||
1592 | if (!is_nd_blk(&nd_region->dev)) | |
1593 | return NULL; | |
1594 | ||
1595 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); | |
1596 | if (!nsblk) | |
1597 | return NULL; | |
1598 | ||
8c2f7e86 | 1599 | dev = &nsblk->common.dev; |
1b40e09a DW |
1600 | dev->type = &namespace_blk_device_type; |
1601 | nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL); | |
1602 | if (nsblk->id < 0) { | |
1603 | kfree(nsblk); | |
1604 | return NULL; | |
1605 | } | |
1606 | dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id); | |
1607 | dev->parent = &nd_region->dev; | |
1608 | dev->groups = nd_namespace_attribute_groups; | |
1609 | ||
8c2f7e86 | 1610 | return &nsblk->common.dev; |
1b40e09a DW |
1611 | } |
1612 | ||
1613 | void nd_region_create_blk_seed(struct nd_region *nd_region) | |
1614 | { | |
1615 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); | |
1616 | nd_region->ns_seed = nd_namespace_blk_create(nd_region); | |
1617 | /* | |
1618 | * Seed creation failures are not fatal, provisioning is simply | |
1619 | * disabled until memory becomes available | |
1620 | */ | |
1621 | if (!nd_region->ns_seed) | |
1622 | dev_err(&nd_region->dev, "failed to create blk namespace\n"); | |
1623 | else | |
1624 | nd_device_register(nd_region->ns_seed); | |
1625 | } | |
1626 | ||
8c2f7e86 DW |
1627 | void nd_region_create_btt_seed(struct nd_region *nd_region) |
1628 | { | |
1629 | WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev)); | |
1630 | nd_region->btt_seed = nd_btt_create(nd_region); | |
1631 | /* | |
1632 | * Seed creation failures are not fatal, provisioning is simply | |
1633 | * disabled until memory becomes available | |
1634 | */ | |
1635 | if (!nd_region->btt_seed) | |
1636 | dev_err(&nd_region->dev, "failed to create btt namespace\n"); | |
1637 | } | |
1638 | ||
1b40e09a DW |
1639 | static struct device **create_namespace_blk(struct nd_region *nd_region) |
1640 | { | |
1641 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; | |
1642 | struct nd_namespace_label *nd_label; | |
1643 | struct device *dev, **devs = NULL; | |
1644 | struct nd_namespace_blk *nsblk; | |
1645 | struct nvdimm_drvdata *ndd; | |
1646 | int i, l, count = 0; | |
1647 | struct resource *res; | |
1648 | ||
1649 | if (nd_region->ndr_mappings == 0) | |
1650 | return NULL; | |
1651 | ||
1652 | ndd = to_ndd(nd_mapping); | |
1653 | for_each_label(l, nd_label, nd_mapping->labels) { | |
1654 | u32 flags = __le32_to_cpu(nd_label->flags); | |
1655 | char *name[NSLABEL_NAME_LEN]; | |
1656 | struct device **__devs; | |
1657 | ||
1658 | if (flags & NSLABEL_FLAG_LOCAL) | |
1659 | /* pass */; | |
1660 | else | |
1661 | continue; | |
1662 | ||
1663 | for (i = 0; i < count; i++) { | |
1664 | nsblk = to_nd_namespace_blk(devs[i]); | |
1665 | if (memcmp(nsblk->uuid, nd_label->uuid, | |
1666 | NSLABEL_UUID_LEN) == 0) { | |
1667 | res = nsblk_add_resource(nd_region, ndd, nsblk, | |
1668 | __le64_to_cpu(nd_label->dpa)); | |
1669 | if (!res) | |
1670 | goto err; | |
1671 | nd_dbg_dpa(nd_region, ndd, res, "%s assign\n", | |
8c2f7e86 | 1672 | dev_name(&nsblk->common.dev)); |
1b40e09a DW |
1673 | break; |
1674 | } | |
1675 | } | |
1676 | if (i < count) | |
1677 | continue; | |
1678 | __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL); | |
1679 | if (!__devs) | |
1680 | goto err; | |
1681 | memcpy(__devs, devs, sizeof(dev) * count); | |
1682 | kfree(devs); | |
1683 | devs = __devs; | |
1684 | ||
1685 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); | |
1686 | if (!nsblk) | |
1687 | goto err; | |
8c2f7e86 | 1688 | dev = &nsblk->common.dev; |
1b40e09a DW |
1689 | dev->type = &namespace_blk_device_type; |
1690 | dev->parent = &nd_region->dev; | |
1691 | dev_set_name(dev, "namespace%d.%d", nd_region->id, count); | |
1692 | devs[count++] = dev; | |
1693 | nsblk->id = -1; | |
1694 | nsblk->lbasize = __le64_to_cpu(nd_label->lbasize); | |
1695 | nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN, | |
1696 | GFP_KERNEL); | |
1697 | if (!nsblk->uuid) | |
1698 | goto err; | |
1699 | memcpy(name, nd_label->name, NSLABEL_NAME_LEN); | |
1700 | if (name[0]) | |
1701 | nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, | |
1702 | GFP_KERNEL); | |
1703 | res = nsblk_add_resource(nd_region, ndd, nsblk, | |
1704 | __le64_to_cpu(nd_label->dpa)); | |
1705 | if (!res) | |
1706 | goto err; | |
1707 | nd_dbg_dpa(nd_region, ndd, res, "%s assign\n", | |
8c2f7e86 | 1708 | dev_name(&nsblk->common.dev)); |
1b40e09a DW |
1709 | } |
1710 | ||
1711 | dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n", | |
1712 | __func__, count, count == 1 ? "" : "s"); | |
1713 | ||
1714 | if (count == 0) { | |
1715 | /* Publish a zero-sized namespace for userspace to configure. */ | |
1716 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1717 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1718 | ||
1719 | kfree(nd_mapping->labels); | |
1720 | nd_mapping->labels = NULL; | |
1721 | } | |
1722 | ||
1723 | devs = kcalloc(2, sizeof(dev), GFP_KERNEL); | |
1724 | if (!devs) | |
1725 | goto err; | |
1726 | nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL); | |
1727 | if (!nsblk) | |
1728 | goto err; | |
8c2f7e86 | 1729 | dev = &nsblk->common.dev; |
1b40e09a DW |
1730 | dev->type = &namespace_blk_device_type; |
1731 | dev->parent = &nd_region->dev; | |
1732 | devs[count++] = dev; | |
1733 | } | |
1734 | ||
1735 | return devs; | |
1736 | ||
1737 | err: | |
1738 | for (i = 0; i < count; i++) { | |
1739 | nsblk = to_nd_namespace_blk(devs[i]); | |
8c2f7e86 | 1740 | namespace_blk_release(&nsblk->common.dev); |
1b40e09a DW |
1741 | } |
1742 | kfree(devs); | |
1743 | return NULL; | |
1744 | } | |
1745 | ||
bf9bccc1 DW |
1746 | static int init_active_labels(struct nd_region *nd_region) |
1747 | { | |
1748 | int i; | |
1749 | ||
1750 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1751 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1752 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
1753 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
1754 | int count, j; | |
1755 | ||
1756 | /* | |
1757 | * If the dimm is disabled then prevent the region from | |
1758 | * being activated if it aliases DPA. | |
1759 | */ | |
1760 | if (!ndd) { | |
1761 | if ((nvdimm->flags & NDD_ALIASING) == 0) | |
1762 | return 0; | |
1763 | dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n", | |
1764 | dev_name(&nd_mapping->nvdimm->dev)); | |
1765 | return -ENXIO; | |
1766 | } | |
1767 | nd_mapping->ndd = ndd; | |
1768 | atomic_inc(&nvdimm->busy); | |
1769 | get_ndd(ndd); | |
1770 | ||
1771 | count = nd_label_active_count(ndd); | |
1772 | dev_dbg(ndd->dev, "%s: %d\n", __func__, count); | |
1773 | if (!count) | |
1774 | continue; | |
1775 | nd_mapping->labels = kcalloc(count + 1, sizeof(void *), | |
1776 | GFP_KERNEL); | |
1777 | if (!nd_mapping->labels) | |
1778 | return -ENOMEM; | |
1779 | for (j = 0; j < count; j++) { | |
1780 | struct nd_namespace_label *label; | |
1781 | ||
1782 | label = nd_label_active(ndd, j); | |
1783 | nd_mapping->labels[j] = label; | |
1784 | } | |
1785 | } | |
1786 | ||
1787 | return 0; | |
1788 | } | |
1789 | ||
3d88002e DW |
1790 | int nd_region_register_namespaces(struct nd_region *nd_region, int *err) |
1791 | { | |
1792 | struct device **devs = NULL; | |
bf9bccc1 | 1793 | int i, rc = 0, type; |
3d88002e DW |
1794 | |
1795 | *err = 0; | |
bf9bccc1 DW |
1796 | nvdimm_bus_lock(&nd_region->dev); |
1797 | rc = init_active_labels(nd_region); | |
1798 | if (rc) { | |
1799 | nvdimm_bus_unlock(&nd_region->dev); | |
1800 | return rc; | |
1801 | } | |
1802 | ||
1803 | type = nd_region_to_nstype(nd_region); | |
1804 | switch (type) { | |
3d88002e DW |
1805 | case ND_DEVICE_NAMESPACE_IO: |
1806 | devs = create_namespace_io(nd_region); | |
1807 | break; | |
bf9bccc1 DW |
1808 | case ND_DEVICE_NAMESPACE_PMEM: |
1809 | devs = create_namespace_pmem(nd_region); | |
1810 | break; | |
1b40e09a DW |
1811 | case ND_DEVICE_NAMESPACE_BLK: |
1812 | devs = create_namespace_blk(nd_region); | |
1813 | break; | |
3d88002e DW |
1814 | default: |
1815 | break; | |
1816 | } | |
bf9bccc1 | 1817 | nvdimm_bus_unlock(&nd_region->dev); |
3d88002e DW |
1818 | |
1819 | if (!devs) | |
1820 | return -ENODEV; | |
1821 | ||
1822 | for (i = 0; devs[i]; i++) { | |
1823 | struct device *dev = devs[i]; | |
1b40e09a | 1824 | int id; |
3d88002e | 1825 | |
1b40e09a DW |
1826 | if (type == ND_DEVICE_NAMESPACE_BLK) { |
1827 | struct nd_namespace_blk *nsblk; | |
1828 | ||
1829 | nsblk = to_nd_namespace_blk(dev); | |
1830 | id = ida_simple_get(&nd_region->ns_ida, 0, 0, | |
1831 | GFP_KERNEL); | |
1832 | nsblk->id = id; | |
1833 | } else | |
1834 | id = i; | |
1835 | ||
1836 | if (id < 0) | |
1837 | break; | |
1838 | dev_set_name(dev, "namespace%d.%d", nd_region->id, id); | |
3d88002e DW |
1839 | dev->groups = nd_namespace_attribute_groups; |
1840 | nd_device_register(dev); | |
1841 | } | |
1b40e09a DW |
1842 | if (i) |
1843 | nd_region->ns_seed = devs[0]; | |
1844 | ||
1845 | if (devs[i]) { | |
1846 | int j; | |
1847 | ||
1848 | for (j = i; devs[j]; j++) { | |
1849 | struct device *dev = devs[j]; | |
1850 | ||
1851 | device_initialize(dev); | |
1852 | put_device(dev); | |
1853 | } | |
1854 | *err = j - i; | |
1855 | /* | |
1856 | * All of the namespaces we tried to register failed, so | |
1857 | * fail region activation. | |
1858 | */ | |
1859 | if (*err == 0) | |
1860 | rc = -ENODEV; | |
1861 | } | |
3d88002e DW |
1862 | kfree(devs); |
1863 | ||
1b40e09a DW |
1864 | if (rc == -ENODEV) |
1865 | return rc; | |
1866 | ||
3d88002e DW |
1867 | return i; |
1868 | } |