]>
Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
4a826c83 DW |
2 | /* |
3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. | |
4a826c83 DW |
4 | */ |
5 | #include <linux/device.h> | |
6 | #include <linux/ndctl.h> | |
b3fde74e | 7 | #include <linux/uuid.h> |
f524bf27 | 8 | #include <linux/slab.h> |
4a826c83 DW |
9 | #include <linux/io.h> |
10 | #include <linux/nd.h> | |
11 | #include "nd-core.h" | |
12 | #include "label.h" | |
13 | #include "nd.h" | |
14 | ||
b3fde74e | 15 | static guid_t nvdimm_btt_guid; |
14e49454 | 16 | static guid_t nvdimm_btt2_guid; |
b3fde74e DW |
17 | static guid_t nvdimm_pfn_guid; |
18 | static guid_t nvdimm_dax_guid; | |
19 | ||
c01dafad QC |
20 | static const char NSINDEX_SIGNATURE[] = "NAMESPACE_INDEX\0"; |
21 | ||
4a826c83 DW |
22 | static u32 best_seq(u32 a, u32 b) |
23 | { | |
24 | a &= NSINDEX_SEQ_MASK; | |
25 | b &= NSINDEX_SEQ_MASK; | |
26 | ||
27 | if (a == 0 || a == b) | |
28 | return b; | |
29 | else if (b == 0) | |
30 | return a; | |
31 | else if (nd_inc_seq(a) == b) | |
32 | return b; | |
33 | else | |
34 | return a; | |
35 | } | |
36 | ||
564e871a DW |
37 | unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd) |
38 | { | |
39 | return ndd->nslabel_size; | |
40 | } | |
41 | ||
9e694d9c TK |
42 | static size_t __sizeof_namespace_index(u32 nslot) |
43 | { | |
44 | return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8), | |
45 | NSINDEX_ALIGN); | |
46 | } | |
47 | ||
48 | static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd, | |
49 | size_t index_size) | |
50 | { | |
51 | return (ndd->nsarea.config_size - index_size * 2) / | |
52 | sizeof_namespace_label(ndd); | |
53 | } | |
54 | ||
02881768 | 55 | int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd) |
4a826c83 | 56 | { |
9e694d9c TK |
57 | u32 tmp_nslot, n; |
58 | ||
59 | tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd); | |
60 | n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN; | |
61 | ||
62 | return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n); | |
02881768 | 63 | } |
4a826c83 | 64 | |
02881768 DW |
65 | size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd) |
66 | { | |
67 | u32 nslot, space, size; | |
4a826c83 DW |
68 | |
69 | /* | |
9e694d9c TK |
70 | * Per UEFI 2.7, the minimum size of the Label Storage Area is large |
71 | * enough to hold 2 index blocks and 2 labels. The minimum index | |
1cfeb66e AD |
72 | * block size is 256 bytes. The label size is 128 for namespaces |
73 | * prior to version 1.2 and at minimum 256 for version 1.2 and later. | |
4a826c83 | 74 | */ |
02881768 DW |
75 | nslot = nvdimm_num_label_slots(ndd); |
76 | space = ndd->nsarea.config_size - nslot * sizeof_namespace_label(ndd); | |
9e694d9c TK |
77 | size = __sizeof_namespace_index(nslot) * 2; |
78 | if (size <= space && nslot >= 2) | |
02881768 | 79 | return size / 2; |
4a826c83 | 80 | |
02881768 DW |
81 | dev_err(ndd->dev, "label area (%d) too small to host (%d byte) labels\n", |
82 | ndd->nsarea.config_size, sizeof_namespace_label(ndd)); | |
83 | return 0; | |
f524bf27 DW |
84 | } |
85 | ||
564e871a | 86 | static int __nd_label_validate(struct nvdimm_drvdata *ndd) |
4a826c83 DW |
87 | { |
88 | /* | |
89 | * On media label format consists of two index blocks followed | |
90 | * by an array of labels. None of these structures are ever | |
91 | * updated in place. A sequence number tracks the current | |
92 | * active index and the next one to write, while labels are | |
93 | * written to free slots. | |
94 | * | |
95 | * +------------+ | |
96 | * | | | |
97 | * | nsindex0 | | |
98 | * | | | |
99 | * +------------+ | |
100 | * | | | |
101 | * | nsindex1 | | |
102 | * | | | |
103 | * +------------+ | |
104 | * | label0 | | |
105 | * +------------+ | |
106 | * | label1 | | |
107 | * +------------+ | |
108 | * | | | |
109 | * ....nslot... | |
110 | * | | | |
111 | * +------------+ | |
112 | * | labelN | | |
113 | * +------------+ | |
114 | */ | |
115 | struct nd_namespace_index *nsindex[] = { | |
116 | to_namespace_index(ndd, 0), | |
117 | to_namespace_index(ndd, 1), | |
118 | }; | |
119 | const int num_index = ARRAY_SIZE(nsindex); | |
120 | struct device *dev = ndd->dev; | |
121 | bool valid[2] = { 0 }; | |
122 | int i, num_valid = 0; | |
123 | u32 seq; | |
124 | ||
125 | for (i = 0; i < num_index; i++) { | |
126 | u32 nslot; | |
127 | u8 sig[NSINDEX_SIG_LEN]; | |
128 | u64 sum_save, sum, size; | |
564e871a | 129 | unsigned int version, labelsize; |
4a826c83 DW |
130 | |
131 | memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN); | |
132 | if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) { | |
426824d6 | 133 | dev_dbg(dev, "nsindex%d signature invalid\n", i); |
4a826c83 DW |
134 | continue; |
135 | } | |
564e871a DW |
136 | |
137 | /* label sizes larger than 128 arrived with v1.2 */ | |
138 | version = __le16_to_cpu(nsindex[i]->major) * 100 | |
139 | + __le16_to_cpu(nsindex[i]->minor); | |
140 | if (version >= 102) | |
141 | labelsize = 1 << (7 + nsindex[i]->labelsize); | |
142 | else | |
143 | labelsize = 128; | |
144 | ||
145 | if (labelsize != sizeof_namespace_label(ndd)) { | |
426824d6 DW |
146 | dev_dbg(dev, "nsindex%d labelsize %d invalid\n", |
147 | i, nsindex[i]->labelsize); | |
564e871a DW |
148 | continue; |
149 | } | |
150 | ||
4a826c83 DW |
151 | sum_save = __le64_to_cpu(nsindex[i]->checksum); |
152 | nsindex[i]->checksum = __cpu_to_le64(0); | |
153 | sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1); | |
154 | nsindex[i]->checksum = __cpu_to_le64(sum_save); | |
155 | if (sum != sum_save) { | |
426824d6 | 156 | dev_dbg(dev, "nsindex%d checksum invalid\n", i); |
4a826c83 DW |
157 | continue; |
158 | } | |
159 | ||
160 | seq = __le32_to_cpu(nsindex[i]->seq); | |
161 | if ((seq & NSINDEX_SEQ_MASK) == 0) { | |
426824d6 | 162 | dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq); |
4a826c83 DW |
163 | continue; |
164 | } | |
165 | ||
166 | /* sanity check the index against expected values */ | |
167 | if (__le64_to_cpu(nsindex[i]->myoff) | |
168 | != i * sizeof_namespace_index(ndd)) { | |
426824d6 DW |
169 | dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n", |
170 | i, (unsigned long long) | |
4a826c83 DW |
171 | __le64_to_cpu(nsindex[i]->myoff)); |
172 | continue; | |
173 | } | |
174 | if (__le64_to_cpu(nsindex[i]->otheroff) | |
175 | != (!i) * sizeof_namespace_index(ndd)) { | |
426824d6 DW |
176 | dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n", |
177 | i, (unsigned long long) | |
4a826c83 DW |
178 | __le64_to_cpu(nsindex[i]->otheroff)); |
179 | continue; | |
180 | } | |
d86d4d63 AD |
181 | if (__le64_to_cpu(nsindex[i]->labeloff) |
182 | != 2 * sizeof_namespace_index(ndd)) { | |
183 | dev_dbg(dev, "nsindex%d labeloff: %#llx invalid\n", | |
184 | i, (unsigned long long) | |
185 | __le64_to_cpu(nsindex[i]->labeloff)); | |
186 | continue; | |
187 | } | |
4a826c83 DW |
188 | |
189 | size = __le64_to_cpu(nsindex[i]->mysize); | |
190 | if (size > sizeof_namespace_index(ndd) | |
191 | || size < sizeof(struct nd_namespace_index)) { | |
426824d6 | 192 | dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size); |
4a826c83 DW |
193 | continue; |
194 | } | |
195 | ||
196 | nslot = __le32_to_cpu(nsindex[i]->nslot); | |
564e871a | 197 | if (nslot * sizeof_namespace_label(ndd) |
4a826c83 DW |
198 | + 2 * sizeof_namespace_index(ndd) |
199 | > ndd->nsarea.config_size) { | |
426824d6 DW |
200 | dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n", |
201 | i, nslot, ndd->nsarea.config_size); | |
4a826c83 DW |
202 | continue; |
203 | } | |
204 | valid[i] = true; | |
205 | num_valid++; | |
206 | } | |
207 | ||
208 | switch (num_valid) { | |
209 | case 0: | |
210 | break; | |
211 | case 1: | |
212 | for (i = 0; i < num_index; i++) | |
213 | if (valid[i]) | |
214 | return i; | |
215 | /* can't have num_valid > 0 but valid[] = { false, false } */ | |
216 | WARN_ON(1); | |
217 | break; | |
218 | default: | |
219 | /* pick the best index... */ | |
220 | seq = best_seq(__le32_to_cpu(nsindex[0]->seq), | |
221 | __le32_to_cpu(nsindex[1]->seq)); | |
222 | if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK)) | |
223 | return 1; | |
224 | else | |
225 | return 0; | |
226 | break; | |
227 | } | |
228 | ||
229 | return -1; | |
230 | } | |
231 | ||
7d47aad4 | 232 | static int nd_label_validate(struct nvdimm_drvdata *ndd) |
564e871a DW |
233 | { |
234 | /* | |
235 | * In order to probe for and validate namespace index blocks we | |
236 | * need to know the size of the labels, and we can't trust the | |
237 | * size of the labels until we validate the index blocks. | |
238 | * Resolve this dependency loop by probing for known label | |
8990cdf1 DW |
239 | * sizes, but default to v1.2 256-byte namespace labels if |
240 | * discovery fails. | |
564e871a | 241 | */ |
8990cdf1 | 242 | int label_size[] = { 128, 256 }; |
564e871a DW |
243 | int i, rc; |
244 | ||
245 | for (i = 0; i < ARRAY_SIZE(label_size); i++) { | |
246 | ndd->nslabel_size = label_size[i]; | |
247 | rc = __nd_label_validate(ndd); | |
248 | if (rc >= 0) | |
249 | return rc; | |
250 | } | |
251 | ||
252 | return -1; | |
253 | } | |
254 | ||
7d47aad4 AD |
255 | static void nd_label_copy(struct nvdimm_drvdata *ndd, |
256 | struct nd_namespace_index *dst, | |
257 | struct nd_namespace_index *src) | |
4a826c83 | 258 | { |
19418b02 AD |
259 | /* just exit if either destination or source is NULL */ |
260 | if (!dst || !src) | |
4a826c83 DW |
261 | return; |
262 | ||
263 | memcpy(dst, src, sizeof_namespace_index(ndd)); | |
264 | } | |
265 | ||
266 | static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd) | |
267 | { | |
268 | void *base = to_namespace_index(ndd, 0); | |
269 | ||
270 | return base + 2 * sizeof_namespace_index(ndd); | |
271 | } | |
272 | ||
f524bf27 DW |
273 | static int to_slot(struct nvdimm_drvdata *ndd, |
274 | struct nd_namespace_label *nd_label) | |
275 | { | |
564e871a DW |
276 | unsigned long label, base; |
277 | ||
278 | label = (unsigned long) nd_label; | |
279 | base = (unsigned long) nd_label_base(ndd); | |
280 | ||
281 | return (label - base) / sizeof_namespace_label(ndd); | |
282 | } | |
283 | ||
284 | static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot) | |
285 | { | |
286 | unsigned long label, base; | |
287 | ||
288 | base = (unsigned long) nd_label_base(ndd); | |
289 | label = base + sizeof_namespace_label(ndd) * slot; | |
290 | ||
291 | return (struct nd_namespace_label *) label; | |
f524bf27 DW |
292 | } |
293 | ||
4a826c83 DW |
294 | #define for_each_clear_bit_le(bit, addr, size) \ |
295 | for ((bit) = find_next_zero_bit_le((addr), (size), 0); \ | |
296 | (bit) < (size); \ | |
297 | (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1)) | |
298 | ||
299 | /** | |
f524bf27 | 300 | * preamble_index - common variable initialization for nd_label_* routines |
4a826c83 | 301 | * @ndd: dimm container for the relevant label set |
f524bf27 | 302 | * @idx: namespace_index index |
4a826c83 DW |
303 | * @nsindex_out: on return set to the currently active namespace index |
304 | * @free: on return set to the free label bitmap in the index | |
305 | * @nslot: on return set to the number of slots in the label space | |
306 | */ | |
f524bf27 | 307 | static bool preamble_index(struct nvdimm_drvdata *ndd, int idx, |
4a826c83 DW |
308 | struct nd_namespace_index **nsindex_out, |
309 | unsigned long **free, u32 *nslot) | |
310 | { | |
311 | struct nd_namespace_index *nsindex; | |
312 | ||
f524bf27 | 313 | nsindex = to_namespace_index(ndd, idx); |
4a826c83 DW |
314 | if (nsindex == NULL) |
315 | return false; | |
316 | ||
317 | *free = (unsigned long *) nsindex->free; | |
318 | *nslot = __le32_to_cpu(nsindex->nslot); | |
319 | *nsindex_out = nsindex; | |
320 | ||
321 | return true; | |
322 | } | |
323 | ||
bf9bccc1 | 324 | char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags) |
4a826c83 DW |
325 | { |
326 | if (!label_id || !uuid) | |
327 | return NULL; | |
328 | snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb", | |
329 | flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid); | |
330 | return label_id->id; | |
331 | } | |
332 | ||
f524bf27 DW |
333 | static bool preamble_current(struct nvdimm_drvdata *ndd, |
334 | struct nd_namespace_index **nsindex, | |
335 | unsigned long **free, u32 *nslot) | |
336 | { | |
337 | return preamble_index(ndd, ndd->ns_current, nsindex, | |
338 | free, nslot); | |
339 | } | |
340 | ||
341 | static bool preamble_next(struct nvdimm_drvdata *ndd, | |
342 | struct nd_namespace_index **nsindex, | |
343 | unsigned long **free, u32 *nslot) | |
344 | { | |
345 | return preamble_index(ndd, ndd->ns_next, nsindex, | |
346 | free, nslot); | |
347 | } | |
348 | ||
7cd35b29 DW |
349 | static bool nsl_validate_checksum(struct nvdimm_drvdata *ndd, |
350 | struct nd_namespace_label *nd_label) | |
351 | { | |
352 | u64 sum, sum_save; | |
353 | ||
354 | if (!namespace_label_has(ndd, checksum)) | |
355 | return true; | |
356 | ||
357 | sum_save = nsl_get_checksum(ndd, nd_label); | |
358 | nsl_set_checksum(ndd, nd_label, 0); | |
359 | sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1); | |
360 | nsl_set_checksum(ndd, nd_label, sum_save); | |
361 | return sum == sum_save; | |
362 | } | |
363 | ||
364 | static void nsl_calculate_checksum(struct nvdimm_drvdata *ndd, | |
365 | struct nd_namespace_label *nd_label) | |
366 | { | |
367 | u64 sum; | |
368 | ||
369 | if (!namespace_label_has(ndd, checksum)) | |
370 | return; | |
371 | nsl_set_checksum(ndd, nd_label, 0); | |
372 | sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1); | |
373 | nsl_set_checksum(ndd, nd_label, sum); | |
374 | } | |
375 | ||
355d8388 DW |
376 | static bool slot_valid(struct nvdimm_drvdata *ndd, |
377 | struct nd_namespace_label *nd_label, u32 slot) | |
4a826c83 | 378 | { |
7cd35b29 DW |
379 | bool valid; |
380 | ||
4a826c83 | 381 | /* check that we are written where we expect to be written */ |
b4366a82 | 382 | if (slot != nsl_get_slot(ndd, nd_label)) |
4a826c83 | 383 | return false; |
7cd35b29 DW |
384 | valid = nsl_validate_checksum(ndd, nd_label); |
385 | if (!valid) | |
386 | dev_dbg(ndd->dev, "fail checksum. slot: %d\n", slot); | |
387 | return valid; | |
4a826c83 DW |
388 | } |
389 | ||
390 | int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd) | |
391 | { | |
392 | struct nd_namespace_index *nsindex; | |
393 | unsigned long *free; | |
394 | u32 nslot, slot; | |
395 | ||
396 | if (!preamble_current(ndd, &nsindex, &free, &nslot)) | |
397 | return 0; /* no label, nothing to reserve */ | |
398 | ||
399 | for_each_clear_bit_le(slot, free, nslot) { | |
d5d30d5a | 400 | struct nvdimm *nvdimm = to_nvdimm(ndd->dev); |
4a826c83 DW |
401 | struct nd_namespace_label *nd_label; |
402 | struct nd_region *nd_region = NULL; | |
403 | u8 label_uuid[NSLABEL_UUID_LEN]; | |
404 | struct nd_label_id label_id; | |
405 | struct resource *res; | |
406 | u32 flags; | |
407 | ||
564e871a | 408 | nd_label = to_label(ndd, slot); |
4a826c83 | 409 | |
355d8388 | 410 | if (!slot_valid(ndd, nd_label, slot)) |
4a826c83 DW |
411 | continue; |
412 | ||
413 | memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN); | |
b4366a82 | 414 | flags = nsl_get_flags(ndd, nd_label); |
d5d30d5a DW |
415 | if (test_bit(NDD_NOBLK, &nvdimm->flags)) |
416 | flags &= ~NSLABEL_FLAG_LOCAL; | |
4a826c83 DW |
417 | nd_label_gen_id(&label_id, label_uuid, flags); |
418 | res = nvdimm_allocate_dpa(ndd, &label_id, | |
b4366a82 DW |
419 | nsl_get_dpa(ndd, nd_label), |
420 | nsl_get_rawsize(ndd, nd_label)); | |
4a826c83 DW |
421 | nd_dbg_dpa(nd_region, ndd, res, "reserve\n"); |
422 | if (!res) | |
423 | return -EBUSY; | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } | |
bf9bccc1 | 428 | |
2d657d17 AD |
429 | int nd_label_data_init(struct nvdimm_drvdata *ndd) |
430 | { | |
7d47aad4 AD |
431 | size_t config_size, read_size, max_xfer, offset; |
432 | struct nd_namespace_index *nsindex; | |
433 | unsigned int i; | |
2d657d17 | 434 | int rc = 0; |
97052c1c | 435 | u32 nslot; |
2d657d17 AD |
436 | |
437 | if (ndd->data) | |
438 | return 0; | |
439 | ||
440 | if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0) { | |
441 | dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n", | |
442 | ndd->nsarea.max_xfer, ndd->nsarea.config_size); | |
443 | return -ENXIO; | |
444 | } | |
445 | ||
446 | /* | |
447 | * We need to determine the maximum index area as this is the section | |
448 | * we must read and validate before we can start processing labels. | |
449 | * | |
450 | * If the area is too small to contain the two indexes and 2 labels | |
451 | * then we abort. | |
452 | * | |
453 | * Start at a label size of 128 as this should result in the largest | |
454 | * possible namespace index size. | |
455 | */ | |
456 | ndd->nslabel_size = 128; | |
457 | read_size = sizeof_namespace_index(ndd) * 2; | |
458 | if (!read_size) | |
459 | return -ENXIO; | |
460 | ||
461 | /* Allocate config data */ | |
462 | config_size = ndd->nsarea.config_size; | |
463 | ndd->data = kvzalloc(config_size, GFP_KERNEL); | |
464 | if (!ndd->data) | |
465 | return -ENOMEM; | |
466 | ||
7d47aad4 AD |
467 | /* |
468 | * We want to guarantee as few reads as possible while conserving | |
469 | * memory. To do that we figure out how much unused space will be left | |
470 | * in the last read, divide that by the total number of reads it is | |
471 | * going to take given our maximum transfer size, and then reduce our | |
472 | * maximum transfer size based on that result. | |
473 | */ | |
474 | max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size); | |
475 | if (read_size < max_xfer) { | |
476 | /* trim waste */ | |
477 | max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) / | |
478 | DIV_ROUND_UP(config_size, max_xfer); | |
479 | /* make certain we read indexes in exactly 1 read */ | |
480 | if (max_xfer < read_size) | |
481 | max_xfer = read_size; | |
482 | } | |
483 | ||
484 | /* Make our initial read size a multiple of max_xfer size */ | |
485 | read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer, | |
486 | config_size); | |
487 | ||
488 | /* Read the index data */ | |
489 | rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size); | |
490 | if (rc) | |
491 | goto out_err; | |
492 | ||
493 | /* Validate index data, if not valid assume all labels are invalid */ | |
494 | ndd->ns_current = nd_label_validate(ndd); | |
495 | if (ndd->ns_current < 0) | |
496 | return 0; | |
497 | ||
498 | /* Record our index values */ | |
499 | ndd->ns_next = nd_label_next_nsindex(ndd->ns_current); | |
500 | ||
501 | /* Copy "current" index on top of the "next" index */ | |
502 | nsindex = to_current_namespace_index(ndd); | |
503 | nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex); | |
504 | ||
505 | /* Determine starting offset for label data */ | |
506 | offset = __le64_to_cpu(nsindex->labeloff); | |
97052c1c | 507 | nslot = __le32_to_cpu(nsindex->nslot); |
7d47aad4 AD |
508 | |
509 | /* Loop through the free list pulling in any active labels */ | |
97052c1c | 510 | for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) { |
7d47aad4 AD |
511 | size_t label_read_size; |
512 | ||
513 | /* zero out the unused labels */ | |
514 | if (test_bit_le(i, nsindex->free)) { | |
515 | memset(ndd->data + offset, 0, ndd->nslabel_size); | |
516 | continue; | |
517 | } | |
518 | ||
519 | /* if we already read past here then just continue */ | |
520 | if (offset + ndd->nslabel_size <= read_size) | |
521 | continue; | |
522 | ||
523 | /* if we haven't read in a while reset our read_size offset */ | |
524 | if (read_size < offset) | |
525 | read_size = offset; | |
526 | ||
527 | /* determine how much more will be read after this next call. */ | |
528 | label_read_size = offset + ndd->nslabel_size - read_size; | |
529 | label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) * | |
530 | max_xfer; | |
531 | ||
532 | /* truncate last read if needed */ | |
533 | if (read_size + label_read_size > config_size) | |
534 | label_read_size = config_size - read_size; | |
535 | ||
536 | /* Read the label data */ | |
537 | rc = nvdimm_get_config_data(ndd, ndd->data + read_size, | |
538 | read_size, label_read_size); | |
539 | if (rc) | |
540 | goto out_err; | |
541 | ||
542 | /* push read_size to next read offset */ | |
543 | read_size += label_read_size; | |
544 | } | |
545 | ||
546 | dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc); | |
547 | out_err: | |
548 | return rc; | |
2d657d17 AD |
549 | } |
550 | ||
bf9bccc1 DW |
551 | int nd_label_active_count(struct nvdimm_drvdata *ndd) |
552 | { | |
553 | struct nd_namespace_index *nsindex; | |
554 | unsigned long *free; | |
555 | u32 nslot, slot; | |
556 | int count = 0; | |
557 | ||
558 | if (!preamble_current(ndd, &nsindex, &free, &nslot)) | |
559 | return 0; | |
560 | ||
561 | for_each_clear_bit_le(slot, free, nslot) { | |
562 | struct nd_namespace_label *nd_label; | |
563 | ||
564e871a | 564 | nd_label = to_label(ndd, slot); |
bf9bccc1 | 565 | |
355d8388 | 566 | if (!slot_valid(ndd, nd_label, slot)) { |
b4366a82 DW |
567 | u32 label_slot = nsl_get_slot(ndd, nd_label); |
568 | u64 size = nsl_get_rawsize(ndd, nd_label); | |
569 | u64 dpa = nsl_get_dpa(ndd, nd_label); | |
bf9bccc1 DW |
570 | |
571 | dev_dbg(ndd->dev, | |
426824d6 DW |
572 | "slot%d invalid slot: %d dpa: %llx size: %llx\n", |
573 | slot, label_slot, dpa, size); | |
bf9bccc1 DW |
574 | continue; |
575 | } | |
576 | count++; | |
577 | } | |
578 | return count; | |
579 | } | |
580 | ||
581 | struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n) | |
582 | { | |
583 | struct nd_namespace_index *nsindex; | |
584 | unsigned long *free; | |
585 | u32 nslot, slot; | |
586 | ||
587 | if (!preamble_current(ndd, &nsindex, &free, &nslot)) | |
588 | return NULL; | |
589 | ||
590 | for_each_clear_bit_le(slot, free, nslot) { | |
591 | struct nd_namespace_label *nd_label; | |
592 | ||
564e871a | 593 | nd_label = to_label(ndd, slot); |
355d8388 | 594 | if (!slot_valid(ndd, nd_label, slot)) |
bf9bccc1 DW |
595 | continue; |
596 | ||
597 | if (n-- == 0) | |
564e871a | 598 | return to_label(ndd, slot); |
bf9bccc1 DW |
599 | } |
600 | ||
601 | return NULL; | |
602 | } | |
f524bf27 | 603 | |
0ba1c634 | 604 | u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd) |
f524bf27 DW |
605 | { |
606 | struct nd_namespace_index *nsindex; | |
607 | unsigned long *free; | |
608 | u32 nslot, slot; | |
609 | ||
610 | if (!preamble_next(ndd, &nsindex, &free, &nslot)) | |
611 | return UINT_MAX; | |
612 | ||
613 | WARN_ON(!is_nvdimm_bus_locked(ndd->dev)); | |
614 | ||
615 | slot = find_next_bit_le(free, nslot, 0); | |
616 | if (slot == nslot) | |
617 | return UINT_MAX; | |
618 | ||
619 | clear_bit_le(slot, free); | |
620 | ||
621 | return slot; | |
622 | } | |
623 | ||
0ba1c634 | 624 | bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot) |
f524bf27 DW |
625 | { |
626 | struct nd_namespace_index *nsindex; | |
627 | unsigned long *free; | |
628 | u32 nslot; | |
629 | ||
630 | if (!preamble_next(ndd, &nsindex, &free, &nslot)) | |
631 | return false; | |
632 | ||
633 | WARN_ON(!is_nvdimm_bus_locked(ndd->dev)); | |
634 | ||
635 | if (slot < nslot) | |
636 | return !test_and_set_bit_le(slot, free); | |
637 | return false; | |
638 | } | |
639 | ||
640 | u32 nd_label_nfree(struct nvdimm_drvdata *ndd) | |
641 | { | |
642 | struct nd_namespace_index *nsindex; | |
643 | unsigned long *free; | |
644 | u32 nslot; | |
645 | ||
646 | WARN_ON(!is_nvdimm_bus_locked(ndd->dev)); | |
647 | ||
648 | if (!preamble_next(ndd, &nsindex, &free, &nslot)) | |
0ba1c634 | 649 | return nvdimm_num_label_slots(ndd); |
f524bf27 DW |
650 | |
651 | return bitmap_weight(free, nslot); | |
652 | } | |
653 | ||
654 | static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq, | |
655 | unsigned long flags) | |
656 | { | |
657 | struct nd_namespace_index *nsindex; | |
658 | unsigned long offset; | |
659 | u64 checksum; | |
660 | u32 nslot; | |
661 | int rc; | |
662 | ||
663 | nsindex = to_namespace_index(ndd, index); | |
664 | if (flags & ND_NSINDEX_INIT) | |
665 | nslot = nvdimm_num_label_slots(ndd); | |
666 | else | |
667 | nslot = __le32_to_cpu(nsindex->nslot); | |
668 | ||
669 | memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN); | |
564e871a DW |
670 | memset(&nsindex->flags, 0, 3); |
671 | nsindex->labelsize = sizeof_namespace_label(ndd) >> 8; | |
f524bf27 DW |
672 | nsindex->seq = __cpu_to_le32(seq); |
673 | offset = (unsigned long) nsindex | |
674 | - (unsigned long) to_namespace_index(ndd, 0); | |
675 | nsindex->myoff = __cpu_to_le64(offset); | |
676 | nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd)); | |
677 | offset = (unsigned long) to_namespace_index(ndd, | |
678 | nd_label_next_nsindex(index)) | |
679 | - (unsigned long) to_namespace_index(ndd, 0); | |
680 | nsindex->otheroff = __cpu_to_le64(offset); | |
681 | offset = (unsigned long) nd_label_base(ndd) | |
682 | - (unsigned long) to_namespace_index(ndd, 0); | |
683 | nsindex->labeloff = __cpu_to_le64(offset); | |
684 | nsindex->nslot = __cpu_to_le32(nslot); | |
685 | nsindex->major = __cpu_to_le16(1); | |
8990cdf1 DW |
686 | if (sizeof_namespace_label(ndd) < 256) |
687 | nsindex->minor = __cpu_to_le16(1); | |
688 | else | |
689 | nsindex->minor = __cpu_to_le16(2); | |
f524bf27 DW |
690 | nsindex->checksum = __cpu_to_le64(0); |
691 | if (flags & ND_NSINDEX_INIT) { | |
692 | unsigned long *free = (unsigned long *) nsindex->free; | |
693 | u32 nfree = ALIGN(nslot, BITS_PER_LONG); | |
694 | int last_bits, i; | |
695 | ||
696 | memset(nsindex->free, 0xff, nfree / 8); | |
697 | for (i = 0, last_bits = nfree - nslot; i < last_bits; i++) | |
698 | clear_bit_le(nslot + i, free); | |
699 | } | |
700 | checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1); | |
701 | nsindex->checksum = __cpu_to_le64(checksum); | |
702 | rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff), | |
703 | nsindex, sizeof_namespace_index(ndd)); | |
704 | if (rc < 0) | |
705 | return rc; | |
706 | ||
707 | if (flags & ND_NSINDEX_INIT) | |
708 | return 0; | |
709 | ||
710 | /* copy the index we just wrote to the new 'next' */ | |
711 | WARN_ON(index != ndd->ns_next); | |
712 | nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex); | |
713 | ndd->ns_current = nd_label_next_nsindex(ndd->ns_current); | |
714 | ndd->ns_next = nd_label_next_nsindex(ndd->ns_next); | |
715 | WARN_ON(ndd->ns_current == ndd->ns_next); | |
716 | ||
717 | return 0; | |
718 | } | |
719 | ||
720 | static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd, | |
721 | struct nd_namespace_label *nd_label) | |
722 | { | |
723 | return (unsigned long) nd_label | |
724 | - (unsigned long) to_namespace_index(ndd, 0); | |
725 | } | |
726 | ||
a6e6d722 | 727 | static enum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid) |
b3fde74e DW |
728 | { |
729 | if (guid_equal(guid, &nvdimm_btt_guid)) | |
730 | return NVDIMM_CCLASS_BTT; | |
14e49454 VV |
731 | else if (guid_equal(guid, &nvdimm_btt2_guid)) |
732 | return NVDIMM_CCLASS_BTT2; | |
b3fde74e DW |
733 | else if (guid_equal(guid, &nvdimm_pfn_guid)) |
734 | return NVDIMM_CCLASS_PFN; | |
735 | else if (guid_equal(guid, &nvdimm_dax_guid)) | |
736 | return NVDIMM_CCLASS_DAX; | |
737 | else if (guid_equal(guid, &guid_null)) | |
738 | return NVDIMM_CCLASS_NONE; | |
739 | ||
740 | return NVDIMM_CCLASS_UNKNOWN; | |
741 | } | |
742 | ||
743 | static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class, | |
744 | guid_t *target) | |
745 | { | |
746 | if (claim_class == NVDIMM_CCLASS_BTT) | |
747 | return &nvdimm_btt_guid; | |
14e49454 VV |
748 | else if (claim_class == NVDIMM_CCLASS_BTT2) |
749 | return &nvdimm_btt2_guid; | |
b3fde74e DW |
750 | else if (claim_class == NVDIMM_CCLASS_PFN) |
751 | return &nvdimm_pfn_guid; | |
752 | else if (claim_class == NVDIMM_CCLASS_DAX) | |
753 | return &nvdimm_dax_guid; | |
754 | else if (claim_class == NVDIMM_CCLASS_UNKNOWN) { | |
755 | /* | |
756 | * If we're modifying a namespace for which we don't | |
757 | * know the claim_class, don't touch the existing guid. | |
758 | */ | |
759 | return target; | |
760 | } else | |
761 | return &guid_null; | |
762 | } | |
763 | ||
c4703ce1 DW |
764 | static void reap_victim(struct nd_mapping *nd_mapping, |
765 | struct nd_label_ent *victim) | |
766 | { | |
767 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
768 | u32 slot = to_slot(ndd, victim->label); | |
769 | ||
770 | dev_dbg(ndd->dev, "free: %d\n", slot); | |
771 | nd_label_free_slot(ndd, slot); | |
772 | victim->label = NULL; | |
773 | } | |
774 | ||
8b03aa0e DW |
775 | static void nsl_set_type_guid(struct nvdimm_drvdata *ndd, |
776 | struct nd_namespace_label *nd_label, guid_t *guid) | |
777 | { | |
778 | if (namespace_label_has(ndd, type_guid)) | |
779 | guid_copy(&nd_label->type_guid, guid); | |
780 | } | |
781 | ||
782 | bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd, | |
783 | struct nd_namespace_label *nd_label, guid_t *guid) | |
784 | { | |
785 | if (!namespace_label_has(ndd, type_guid)) | |
786 | return true; | |
787 | if (!guid_equal(&nd_label->type_guid, guid)) { | |
788 | dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", guid, | |
789 | &nd_label->type_guid); | |
790 | return false; | |
791 | } | |
792 | return true; | |
793 | } | |
794 | ||
a6e6d722 DW |
795 | static void nsl_set_claim_class(struct nvdimm_drvdata *ndd, |
796 | struct nd_namespace_label *nd_label, | |
797 | enum nvdimm_claim_class claim_class) | |
798 | { | |
799 | if (!namespace_label_has(ndd, abstraction_guid)) | |
800 | return; | |
801 | guid_copy(&nd_label->abstraction_guid, | |
802 | to_abstraction_guid(claim_class, | |
803 | &nd_label->abstraction_guid)); | |
804 | } | |
805 | ||
806 | enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd, | |
807 | struct nd_namespace_label *nd_label) | |
808 | { | |
809 | if (!namespace_label_has(ndd, abstraction_guid)) | |
810 | return NVDIMM_CCLASS_NONE; | |
811 | return to_nvdimm_cclass(&nd_label->abstraction_guid); | |
812 | } | |
813 | ||
f524bf27 DW |
814 | static int __pmem_label_update(struct nd_region *nd_region, |
815 | struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm, | |
966d23a0 | 816 | int pos, unsigned long flags) |
f524bf27 | 817 | { |
b3fde74e | 818 | struct nd_namespace_common *ndns = &nspm->nsio.common; |
faec6f8a | 819 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
f524bf27 | 820 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
f524bf27 DW |
821 | struct nd_namespace_label *nd_label; |
822 | struct nd_namespace_index *nsindex; | |
c4703ce1 | 823 | struct nd_label_ent *label_ent; |
16660eae DW |
824 | struct nd_label_id label_id; |
825 | struct resource *res; | |
f524bf27 DW |
826 | unsigned long *free; |
827 | u32 nslot, slot; | |
828 | size_t offset; | |
c12c48ce | 829 | u64 cookie; |
f524bf27 DW |
830 | int rc; |
831 | ||
832 | if (!preamble_next(ndd, &nsindex, &free, &nslot)) | |
833 | return -ENXIO; | |
834 | ||
c12c48ce | 835 | cookie = nd_region_interleave_set_cookie(nd_region, nsindex); |
16660eae DW |
836 | nd_label_gen_id(&label_id, nspm->uuid, 0); |
837 | for_each_dpa_resource(ndd, res) | |
838 | if (strcmp(res->name, label_id.id) == 0) | |
839 | break; | |
840 | ||
841 | if (!res) { | |
842 | WARN_ON_ONCE(1); | |
843 | return -ENXIO; | |
844 | } | |
845 | ||
f524bf27 DW |
846 | /* allocate and write the label to the staging (next) index */ |
847 | slot = nd_label_alloc_slot(ndd); | |
848 | if (slot == UINT_MAX) | |
849 | return -ENXIO; | |
426824d6 | 850 | dev_dbg(ndd->dev, "allocated: %d\n", slot); |
f524bf27 | 851 | |
564e871a DW |
852 | nd_label = to_label(ndd, slot); |
853 | memset(nd_label, 0, sizeof_namespace_label(ndd)); | |
f524bf27 | 854 | memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN); |
8176f147 DW |
855 | nsl_set_name(ndd, nd_label, nspm->alt_name); |
856 | nsl_set_flags(ndd, nd_label, flags); | |
857 | nsl_set_nlabel(ndd, nd_label, nd_region->ndr_mappings); | |
858 | nsl_set_position(ndd, nd_label, pos); | |
859 | nsl_set_isetcookie(ndd, nd_label, cookie); | |
860 | nsl_set_rawsize(ndd, nd_label, resource_size(res)); | |
861 | nsl_set_lbasize(ndd, nd_label, nspm->lbasize); | |
862 | nsl_set_dpa(ndd, nd_label, res->start); | |
863 | nsl_set_slot(ndd, nd_label, slot); | |
8b03aa0e | 864 | nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid); |
a6e6d722 | 865 | nsl_set_claim_class(ndd, nd_label, ndns->claim_class); |
7cd35b29 | 866 | nsl_calculate_checksum(ndd, nd_label); |
426824d6 | 867 | nd_dbg_dpa(nd_region, ndd, res, "\n"); |
f524bf27 DW |
868 | |
869 | /* update label */ | |
870 | offset = nd_label_offset(ndd, nd_label); | |
871 | rc = nvdimm_set_config_data(ndd, offset, nd_label, | |
564e871a | 872 | sizeof_namespace_label(ndd)); |
f524bf27 DW |
873 | if (rc < 0) |
874 | return rc; | |
875 | ||
876 | /* Garbage collect the previous label */ | |
ae8219f1 | 877 | mutex_lock(&nd_mapping->lock); |
16660eae DW |
878 | list_for_each_entry(label_ent, &nd_mapping->labels, list) { |
879 | if (!label_ent->label) | |
880 | continue; | |
c4703ce1 DW |
881 | if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags) |
882 | || memcmp(nspm->uuid, label_ent->label->uuid, | |
883 | NSLABEL_UUID_LEN) == 0) | |
884 | reap_victim(nd_mapping, label_ent); | |
f524bf27 DW |
885 | } |
886 | ||
887 | /* update index */ | |
888 | rc = nd_label_write_index(ndd, ndd->ns_next, | |
889 | nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0); | |
16660eae DW |
890 | if (rc == 0) { |
891 | list_for_each_entry(label_ent, &nd_mapping->labels, list) | |
892 | if (!label_ent->label) { | |
893 | label_ent->label = nd_label; | |
894 | nd_label = NULL; | |
895 | break; | |
896 | } | |
897 | dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label, | |
898 | "failed to track label: %d\n", | |
899 | to_slot(ndd, nd_label)); | |
900 | if (nd_label) | |
901 | rc = -ENXIO; | |
902 | } | |
ae8219f1 | 903 | mutex_unlock(&nd_mapping->lock); |
0ba1c634 | 904 | |
ae8219f1 | 905 | return rc; |
0ba1c634 DW |
906 | } |
907 | ||
908 | static bool is_old_resource(struct resource *res, struct resource **list, int n) | |
f524bf27 DW |
909 | { |
910 | int i; | |
0ba1c634 DW |
911 | |
912 | if (res->flags & DPA_RESOURCE_ADJUSTED) | |
913 | return false; | |
914 | for (i = 0; i < n; i++) | |
915 | if (res == list[i]) | |
916 | return true; | |
917 | return false; | |
918 | } | |
919 | ||
920 | static struct resource *to_resource(struct nvdimm_drvdata *ndd, | |
921 | struct nd_namespace_label *nd_label) | |
922 | { | |
923 | struct resource *res; | |
924 | ||
925 | for_each_dpa_resource(ndd, res) { | |
b4366a82 | 926 | if (res->start != nsl_get_dpa(ndd, nd_label)) |
0ba1c634 | 927 | continue; |
b4366a82 | 928 | if (resource_size(res) != nsl_get_rawsize(ndd, nd_label)) |
0ba1c634 DW |
929 | continue; |
930 | return res; | |
931 | } | |
932 | ||
933 | return NULL; | |
934 | } | |
935 | ||
de8fa48b DW |
936 | /* |
937 | * Use the presence of the type_guid as a flag to determine isetcookie | |
938 | * usage and nlabel + position policy for blk-aperture namespaces. | |
939 | */ | |
f56541a7 DW |
940 | static void nsl_set_blk_isetcookie(struct nvdimm_drvdata *ndd, |
941 | struct nd_namespace_label *nd_label, | |
942 | u64 isetcookie) | |
943 | { | |
944 | if (namespace_label_has(ndd, type_guid)) { | |
945 | nsl_set_isetcookie(ndd, nd_label, isetcookie); | |
946 | return; | |
947 | } | |
948 | nsl_set_isetcookie(ndd, nd_label, 0); /* N/A */ | |
949 | } | |
950 | ||
951 | bool nsl_validate_blk_isetcookie(struct nvdimm_drvdata *ndd, | |
952 | struct nd_namespace_label *nd_label, | |
953 | u64 isetcookie) | |
954 | { | |
955 | if (!namespace_label_has(ndd, type_guid)) | |
956 | return true; | |
957 | ||
958 | if (nsl_get_isetcookie(ndd, nd_label) != isetcookie) { | |
959 | dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n", isetcookie, | |
960 | nsl_get_isetcookie(ndd, nd_label)); | |
961 | return false; | |
962 | } | |
963 | ||
964 | return true; | |
965 | } | |
966 | ||
de8fa48b DW |
967 | static void nsl_set_blk_nlabel(struct nvdimm_drvdata *ndd, |
968 | struct nd_namespace_label *nd_label, int nlabel, | |
969 | bool first) | |
970 | { | |
971 | if (!namespace_label_has(ndd, type_guid)) { | |
972 | nsl_set_nlabel(ndd, nd_label, 0); /* N/A */ | |
973 | return; | |
974 | } | |
975 | nsl_set_nlabel(ndd, nd_label, first ? nlabel : 0xffff); | |
976 | } | |
977 | ||
978 | static void nsl_set_blk_position(struct nvdimm_drvdata *ndd, | |
979 | struct nd_namespace_label *nd_label, | |
980 | bool first) | |
981 | { | |
982 | if (!namespace_label_has(ndd, type_guid)) { | |
983 | nsl_set_position(ndd, nd_label, 0); | |
984 | return; | |
985 | } | |
986 | nsl_set_position(ndd, nd_label, first ? 0 : 0xffff); | |
987 | } | |
988 | ||
0ba1c634 DW |
989 | /* |
990 | * 1/ Account all the labels that can be freed after this update | |
991 | * 2/ Allocate and write the label to the staging (next) index | |
992 | * 3/ Record the resources in the namespace device | |
993 | */ | |
994 | static int __blk_label_update(struct nd_region *nd_region, | |
995 | struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk, | |
996 | int num_labels) | |
997 | { | |
ae8219f1 | 998 | int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO; |
faec6f8a | 999 | struct nd_interleave_set *nd_set = nd_region->nd_set; |
b3fde74e | 1000 | struct nd_namespace_common *ndns = &nsblk->common; |
0ba1c634 DW |
1001 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
1002 | struct nd_namespace_label *nd_label; | |
ae8219f1 | 1003 | struct nd_label_ent *label_ent, *e; |
0ba1c634 DW |
1004 | struct nd_namespace_index *nsindex; |
1005 | unsigned long *free, *victim_map = NULL; | |
1006 | struct resource *res, **old_res_list; | |
1007 | struct nd_label_id label_id; | |
1008 | u8 uuid[NSLABEL_UUID_LEN]; | |
3934d841 | 1009 | int min_dpa_idx = 0; |
ae8219f1 | 1010 | LIST_HEAD(list); |
0ba1c634 DW |
1011 | u32 nslot, slot; |
1012 | ||
1013 | if (!preamble_next(ndd, &nsindex, &free, &nslot)) | |
1014 | return -ENXIO; | |
1015 | ||
1016 | old_res_list = nsblk->res; | |
1017 | nfree = nd_label_nfree(ndd); | |
1018 | old_num_resources = nsblk->num_resources; | |
1019 | nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL); | |
1020 | ||
1021 | /* | |
1022 | * We need to loop over the old resources a few times, which seems a | |
1023 | * bit inefficient, but we need to know that we have the label | |
1024 | * space before we start mutating the tracking structures. | |
1025 | * Otherwise the recovery method of last resort for userspace is | |
1026 | * disable and re-enable the parent region. | |
1027 | */ | |
1028 | alloc = 0; | |
1029 | for_each_dpa_resource(ndd, res) { | |
1030 | if (strcmp(res->name, label_id.id) != 0) | |
1031 | continue; | |
1032 | if (!is_old_resource(res, old_res_list, old_num_resources)) | |
1033 | alloc++; | |
1034 | } | |
1035 | ||
1036 | victims = 0; | |
1037 | if (old_num_resources) { | |
1038 | /* convert old local-label-map to dimm-slot victim-map */ | |
9065ed12 | 1039 | victim_map = bitmap_zalloc(nslot, GFP_KERNEL); |
0ba1c634 DW |
1040 | if (!victim_map) |
1041 | return -ENOMEM; | |
1042 | ||
1043 | /* mark unused labels for garbage collection */ | |
1044 | for_each_clear_bit_le(slot, free, nslot) { | |
564e871a | 1045 | nd_label = to_label(ndd, slot); |
0ba1c634 DW |
1046 | memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN); |
1047 | if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0) | |
1048 | continue; | |
1049 | res = to_resource(ndd, nd_label); | |
1050 | if (res && is_old_resource(res, old_res_list, | |
1051 | old_num_resources)) | |
1052 | continue; | |
1053 | slot = to_slot(ndd, nd_label); | |
1054 | set_bit(slot, victim_map); | |
1055 | victims++; | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | /* don't allow updates that consume the last label */ | |
1060 | if (nfree - alloc < 0 || nfree - alloc + victims < 1) { | |
8c2f7e86 | 1061 | dev_info(&nsblk->common.dev, "insufficient label space\n"); |
9065ed12 | 1062 | bitmap_free(victim_map); |
0ba1c634 DW |
1063 | return -ENOSPC; |
1064 | } | |
1065 | /* from here on we need to abort on error */ | |
1066 | ||
1067 | ||
1068 | /* assign all resources to the namespace before writing the labels */ | |
1069 | nsblk->res = NULL; | |
1070 | nsblk->num_resources = 0; | |
1071 | for_each_dpa_resource(ndd, res) { | |
1072 | if (strcmp(res->name, label_id.id) != 0) | |
1073 | continue; | |
1074 | if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) { | |
1075 | rc = -ENOMEM; | |
1076 | goto abort; | |
1077 | } | |
1078 | } | |
1079 | ||
2dd2a174 DW |
1080 | /* release slots associated with any invalidated UUIDs */ |
1081 | mutex_lock(&nd_mapping->lock); | |
1082 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) | |
1083 | if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags)) { | |
1084 | reap_victim(nd_mapping, label_ent); | |
1085 | list_move(&label_ent->list, &list); | |
1086 | } | |
1087 | mutex_unlock(&nd_mapping->lock); | |
1088 | ||
3934d841 DW |
1089 | /* |
1090 | * Find the resource associated with the first label in the set | |
1091 | * per the v1.2 namespace specification. | |
1092 | */ | |
1093 | for (i = 0; i < nsblk->num_resources; i++) { | |
1094 | struct resource *min = nsblk->res[min_dpa_idx]; | |
1095 | ||
1096 | res = nsblk->res[i]; | |
1097 | if (res->start < min->start) | |
1098 | min_dpa_idx = i; | |
1099 | } | |
1100 | ||
0ba1c634 DW |
1101 | for (i = 0; i < nsblk->num_resources; i++) { |
1102 | size_t offset; | |
1103 | ||
1104 | res = nsblk->res[i]; | |
1105 | if (is_old_resource(res, old_res_list, old_num_resources)) | |
1106 | continue; /* carry-over */ | |
1107 | slot = nd_label_alloc_slot(ndd); | |
4c467647 ZQ |
1108 | if (slot == UINT_MAX) { |
1109 | rc = -ENXIO; | |
0ba1c634 | 1110 | goto abort; |
4c467647 | 1111 | } |
426824d6 | 1112 | dev_dbg(ndd->dev, "allocated: %d\n", slot); |
0ba1c634 | 1113 | |
564e871a DW |
1114 | nd_label = to_label(ndd, slot); |
1115 | memset(nd_label, 0, sizeof_namespace_label(ndd)); | |
0ba1c634 | 1116 | memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN); |
8176f147 DW |
1117 | nsl_set_name(ndd, nd_label, nsblk->alt_name); |
1118 | nsl_set_flags(ndd, nd_label, NSLABEL_FLAG_LOCAL); | |
8f2bc243 | 1119 | |
de8fa48b DW |
1120 | nsl_set_blk_nlabel(ndd, nd_label, nsblk->num_resources, |
1121 | i == min_dpa_idx); | |
1122 | nsl_set_blk_position(ndd, nd_label, i == min_dpa_idx); | |
f56541a7 | 1123 | nsl_set_blk_isetcookie(ndd, nd_label, nd_set->cookie2); |
8f2bc243 | 1124 | |
8176f147 DW |
1125 | nsl_set_dpa(ndd, nd_label, res->start); |
1126 | nsl_set_rawsize(ndd, nd_label, resource_size(res)); | |
1127 | nsl_set_lbasize(ndd, nd_label, nsblk->lbasize); | |
1128 | nsl_set_slot(ndd, nd_label, slot); | |
8b03aa0e | 1129 | nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid); |
a6e6d722 | 1130 | nsl_set_claim_class(ndd, nd_label, ndns->claim_class); |
7cd35b29 | 1131 | nsl_calculate_checksum(ndd, nd_label); |
0ba1c634 DW |
1132 | |
1133 | /* update label */ | |
1134 | offset = nd_label_offset(ndd, nd_label); | |
1135 | rc = nvdimm_set_config_data(ndd, offset, nd_label, | |
564e871a | 1136 | sizeof_namespace_label(ndd)); |
0ba1c634 DW |
1137 | if (rc < 0) |
1138 | goto abort; | |
1139 | } | |
1140 | ||
1141 | /* free up now unused slots in the new index */ | |
1142 | for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) { | |
426824d6 | 1143 | dev_dbg(ndd->dev, "free: %d\n", slot); |
0ba1c634 DW |
1144 | nd_label_free_slot(ndd, slot); |
1145 | } | |
1146 | ||
1147 | /* update index */ | |
1148 | rc = nd_label_write_index(ndd, ndd->ns_next, | |
1149 | nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0); | |
1150 | if (rc) | |
1151 | goto abort; | |
1152 | ||
1153 | /* | |
1154 | * Now that the on-dimm labels are up to date, fix up the tracking | |
1155 | * entries in nd_mapping->labels | |
1156 | */ | |
1157 | nlabel = 0; | |
ae8219f1 DW |
1158 | mutex_lock(&nd_mapping->lock); |
1159 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { | |
1160 | nd_label = label_ent->label; | |
1161 | if (!nd_label) | |
1162 | continue; | |
0ba1c634 DW |
1163 | nlabel++; |
1164 | memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN); | |
1165 | if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0) | |
1166 | continue; | |
1167 | nlabel--; | |
ae8219f1 DW |
1168 | list_move(&label_ent->list, &list); |
1169 | label_ent->label = NULL; | |
0ba1c634 | 1170 | } |
ae8219f1 DW |
1171 | list_splice_tail_init(&list, &nd_mapping->labels); |
1172 | mutex_unlock(&nd_mapping->lock); | |
1173 | ||
0ba1c634 DW |
1174 | if (nlabel + nsblk->num_resources > num_labels) { |
1175 | /* | |
1176 | * Bug, we can't end up with more resources than | |
1177 | * available labels | |
1178 | */ | |
1179 | WARN_ON_ONCE(1); | |
1180 | rc = -ENXIO; | |
1181 | goto out; | |
1182 | } | |
1183 | ||
ae8219f1 DW |
1184 | mutex_lock(&nd_mapping->lock); |
1185 | label_ent = list_first_entry_or_null(&nd_mapping->labels, | |
1186 | typeof(*label_ent), list); | |
1187 | if (!label_ent) { | |
1188 | WARN_ON(1); | |
1189 | mutex_unlock(&nd_mapping->lock); | |
1190 | rc = -ENXIO; | |
1191 | goto out; | |
1192 | } | |
0ba1c634 | 1193 | for_each_clear_bit_le(slot, free, nslot) { |
564e871a | 1194 | nd_label = to_label(ndd, slot); |
0ba1c634 DW |
1195 | memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN); |
1196 | if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0) | |
1197 | continue; | |
1198 | res = to_resource(ndd, nd_label); | |
1199 | res->flags &= ~DPA_RESOURCE_ADJUSTED; | |
ae8219f1 DW |
1200 | dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot); |
1201 | list_for_each_entry_from(label_ent, &nd_mapping->labels, list) { | |
1202 | if (label_ent->label) | |
1203 | continue; | |
1204 | label_ent->label = nd_label; | |
1205 | nd_label = NULL; | |
1206 | break; | |
1207 | } | |
1208 | if (nd_label) | |
1209 | dev_WARN(&nsblk->common.dev, | |
1210 | "failed to track label slot%d\n", slot); | |
0ba1c634 | 1211 | } |
ae8219f1 | 1212 | mutex_unlock(&nd_mapping->lock); |
0ba1c634 DW |
1213 | |
1214 | out: | |
1215 | kfree(old_res_list); | |
9065ed12 | 1216 | bitmap_free(victim_map); |
0ba1c634 DW |
1217 | return rc; |
1218 | ||
1219 | abort: | |
1220 | /* | |
1221 | * 1/ repair the allocated label bitmap in the index | |
1222 | * 2/ restore the resource list | |
1223 | */ | |
1224 | nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd)); | |
1225 | kfree(nsblk->res); | |
1226 | nsblk->res = old_res_list; | |
1227 | nsblk->num_resources = old_num_resources; | |
1228 | old_res_list = NULL; | |
1229 | goto out; | |
1230 | } | |
1231 | ||
1232 | static int init_labels(struct nd_mapping *nd_mapping, int num_labels) | |
1233 | { | |
ae8219f1 DW |
1234 | int i, old_num_labels = 0; |
1235 | struct nd_label_ent *label_ent; | |
f524bf27 DW |
1236 | struct nd_namespace_index *nsindex; |
1237 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
1238 | ||
ae8219f1 DW |
1239 | mutex_lock(&nd_mapping->lock); |
1240 | list_for_each_entry(label_ent, &nd_mapping->labels, list) | |
0ba1c634 | 1241 | old_num_labels++; |
ae8219f1 | 1242 | mutex_unlock(&nd_mapping->lock); |
f524bf27 | 1243 | |
0ba1c634 DW |
1244 | /* |
1245 | * We need to preserve all the old labels for the mapping so | |
1246 | * they can be garbage collected after writing the new labels. | |
1247 | */ | |
ae8219f1 DW |
1248 | for (i = old_num_labels; i < num_labels; i++) { |
1249 | label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL); | |
1250 | if (!label_ent) | |
0ba1c634 | 1251 | return -ENOMEM; |
ae8219f1 DW |
1252 | mutex_lock(&nd_mapping->lock); |
1253 | list_add_tail(&label_ent->list, &nd_mapping->labels); | |
1254 | mutex_unlock(&nd_mapping->lock); | |
0ba1c634 | 1255 | } |
0ba1c634 | 1256 | |
f524bf27 DW |
1257 | if (ndd->ns_current == -1 || ndd->ns_next == -1) |
1258 | /* pass */; | |
1259 | else | |
0ba1c634 | 1260 | return max(num_labels, old_num_labels); |
f524bf27 DW |
1261 | |
1262 | nsindex = to_namespace_index(ndd, 0); | |
1263 | memset(nsindex, 0, ndd->nsarea.config_size); | |
1264 | for (i = 0; i < 2; i++) { | |
b18d4b8a | 1265 | int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT); |
f524bf27 DW |
1266 | |
1267 | if (rc) | |
1268 | return rc; | |
1269 | } | |
1270 | ndd->ns_next = 1; | |
1271 | ndd->ns_current = 0; | |
1272 | ||
0ba1c634 | 1273 | return max(num_labels, old_num_labels); |
f524bf27 DW |
1274 | } |
1275 | ||
1276 | static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid) | |
1277 | { | |
1278 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); | |
ae8219f1 | 1279 | struct nd_label_ent *label_ent, *e; |
f524bf27 DW |
1280 | struct nd_namespace_index *nsindex; |
1281 | u8 label_uuid[NSLABEL_UUID_LEN]; | |
f524bf27 | 1282 | unsigned long *free; |
ae8219f1 | 1283 | LIST_HEAD(list); |
f524bf27 | 1284 | u32 nslot, slot; |
ae8219f1 | 1285 | int active = 0; |
f524bf27 DW |
1286 | |
1287 | if (!uuid) | |
1288 | return 0; | |
1289 | ||
1290 | /* no index || no labels == nothing to delete */ | |
ae8219f1 | 1291 | if (!preamble_next(ndd, &nsindex, &free, &nslot)) |
f524bf27 DW |
1292 | return 0; |
1293 | ||
ae8219f1 DW |
1294 | mutex_lock(&nd_mapping->lock); |
1295 | list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) { | |
1296 | struct nd_namespace_label *nd_label = label_ent->label; | |
1297 | ||
1298 | if (!nd_label) | |
1299 | continue; | |
1300 | active++; | |
f524bf27 DW |
1301 | memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN); |
1302 | if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0) | |
1303 | continue; | |
ae8219f1 | 1304 | active--; |
f524bf27 DW |
1305 | slot = to_slot(ndd, nd_label); |
1306 | nd_label_free_slot(ndd, slot); | |
426824d6 | 1307 | dev_dbg(ndd->dev, "free: %d\n", slot); |
ae8219f1 DW |
1308 | list_move_tail(&label_ent->list, &list); |
1309 | label_ent->label = NULL; | |
f524bf27 | 1310 | } |
ae8219f1 | 1311 | list_splice_tail_init(&list, &nd_mapping->labels); |
f524bf27 | 1312 | |
ae8219f1 DW |
1313 | if (active == 0) { |
1314 | nd_mapping_free_labels(nd_mapping); | |
426824d6 | 1315 | dev_dbg(ndd->dev, "no more active labels\n"); |
f524bf27 | 1316 | } |
ae8219f1 | 1317 | mutex_unlock(&nd_mapping->lock); |
f524bf27 DW |
1318 | |
1319 | return nd_label_write_index(ndd, ndd->ns_next, | |
1320 | nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0); | |
1321 | } | |
1322 | ||
1323 | int nd_pmem_namespace_label_update(struct nd_region *nd_region, | |
1324 | struct nd_namespace_pmem *nspm, resource_size_t size) | |
1325 | { | |
966d23a0 | 1326 | int i, rc; |
f524bf27 DW |
1327 | |
1328 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1329 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
16660eae DW |
1330 | struct nvdimm_drvdata *ndd = to_ndd(nd_mapping); |
1331 | struct resource *res; | |
966d23a0 | 1332 | int count = 0; |
f524bf27 DW |
1333 | |
1334 | if (size == 0) { | |
1335 | rc = del_labels(nd_mapping, nspm->uuid); | |
1336 | if (rc) | |
1337 | return rc; | |
1338 | continue; | |
1339 | } | |
1340 | ||
16660eae | 1341 | for_each_dpa_resource(ndd, res) |
2d9a0274 | 1342 | if (strncmp(res->name, "pmem", 4) == 0) |
16660eae DW |
1343 | count++; |
1344 | WARN_ON_ONCE(!count); | |
1345 | ||
1346 | rc = init_labels(nd_mapping, count); | |
0ba1c634 | 1347 | if (rc < 0) |
f524bf27 DW |
1348 | return rc; |
1349 | ||
966d23a0 DW |
1350 | rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, |
1351 | NSLABEL_FLAG_UPDATING); | |
1352 | if (rc) | |
1353 | return rc; | |
1354 | } | |
1355 | ||
1356 | if (size == 0) | |
1357 | return 0; | |
1358 | ||
1359 | /* Clear the UPDATING flag per UEFI 2.7 expectations */ | |
1360 | for (i = 0; i < nd_region->ndr_mappings; i++) { | |
1361 | struct nd_mapping *nd_mapping = &nd_region->mapping[i]; | |
1362 | ||
1363 | rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0); | |
f524bf27 DW |
1364 | if (rc) |
1365 | return rc; | |
1366 | } | |
1367 | ||
1368 | return 0; | |
1369 | } | |
0ba1c634 DW |
1370 | |
1371 | int nd_blk_namespace_label_update(struct nd_region *nd_region, | |
1372 | struct nd_namespace_blk *nsblk, resource_size_t size) | |
1373 | { | |
1374 | struct nd_mapping *nd_mapping = &nd_region->mapping[0]; | |
1375 | struct resource *res; | |
1376 | int count = 0; | |
1377 | ||
1378 | if (size == 0) | |
1379 | return del_labels(nd_mapping, nsblk->uuid); | |
1380 | ||
1381 | for_each_dpa_resource(to_ndd(nd_mapping), res) | |
1382 | count++; | |
1383 | ||
1384 | count = init_labels(nd_mapping, count); | |
1385 | if (count < 0) | |
1386 | return count; | |
1387 | ||
1388 | return __blk_label_update(nd_region, nd_mapping, nsblk, count); | |
1389 | } | |
b3fde74e DW |
1390 | |
1391 | int __init nd_label_init(void) | |
1392 | { | |
1393 | WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid)); | |
14e49454 | 1394 | WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid)); |
b3fde74e DW |
1395 | WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid)); |
1396 | WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid)); | |
1397 | ||
1398 | return 0; | |
1399 | } |