blob: 9108004a0d9b661d403eb2c3fd4b394e393d6ac4 [file] [log] [blame]
Dan Williams4a826c82015-06-09 16:09:36 -04001/*
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/device.h>
14#include <linux/ndctl.h>
Dan Williamsf524bf22015-05-30 12:36:02 -040015#include <linux/slab.h>
Dan Williams4a826c82015-06-09 16:09:36 -040016#include <linux/io.h>
17#include <linux/nd.h>
18#include "nd-core.h"
19#include "label.h"
20#include "nd.h"
21
22static 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
37size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
38{
39 u32 index_span;
40
41 if (ndd->nsindex_size)
42 return ndd->nsindex_size;
43
44 /*
45 * The minimum index space is 512 bytes, with that amount of
46 * index we can describe ~1400 labels which is less than a byte
47 * of overhead per label. Round up to a byte of overhead per
48 * label and determine the size of the index region. Yes, this
49 * starts to waste space at larger config_sizes, but it's
50 * unlikely we'll ever see anything but 128K.
51 */
52 index_span = ndd->nsarea.config_size / 129;
53 index_span /= NSINDEX_ALIGN * 2;
54 ndd->nsindex_size = index_span * NSINDEX_ALIGN;
55
56 return ndd->nsindex_size;
57}
58
Dan Williams0ba1c632015-05-30 12:35:36 -040059int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
Dan Williamsf524bf22015-05-30 12:36:02 -040060{
61 return ndd->nsarea.config_size / 129;
62}
63
Dan Williams4a826c82015-06-09 16:09:36 -040064int nd_label_validate(struct nvdimm_drvdata *ndd)
65{
66 /*
67 * On media label format consists of two index blocks followed
68 * by an array of labels. None of these structures are ever
69 * updated in place. A sequence number tracks the current
70 * active index and the next one to write, while labels are
71 * written to free slots.
72 *
73 * +------------+
74 * | |
75 * | nsindex0 |
76 * | |
77 * +------------+
78 * | |
79 * | nsindex1 |
80 * | |
81 * +------------+
82 * | label0 |
83 * +------------+
84 * | label1 |
85 * +------------+
86 * | |
87 * ....nslot...
88 * | |
89 * +------------+
90 * | labelN |
91 * +------------+
92 */
93 struct nd_namespace_index *nsindex[] = {
94 to_namespace_index(ndd, 0),
95 to_namespace_index(ndd, 1),
96 };
97 const int num_index = ARRAY_SIZE(nsindex);
98 struct device *dev = ndd->dev;
99 bool valid[2] = { 0 };
100 int i, num_valid = 0;
101 u32 seq;
102
103 for (i = 0; i < num_index; i++) {
104 u32 nslot;
105 u8 sig[NSINDEX_SIG_LEN];
106 u64 sum_save, sum, size;
107
108 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
109 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
110 dev_dbg(dev, "%s: nsindex%d signature invalid\n",
111 __func__, i);
112 continue;
113 }
114 sum_save = __le64_to_cpu(nsindex[i]->checksum);
115 nsindex[i]->checksum = __cpu_to_le64(0);
116 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
117 nsindex[i]->checksum = __cpu_to_le64(sum_save);
118 if (sum != sum_save) {
119 dev_dbg(dev, "%s: nsindex%d checksum invalid\n",
120 __func__, i);
121 continue;
122 }
123
124 seq = __le32_to_cpu(nsindex[i]->seq);
125 if ((seq & NSINDEX_SEQ_MASK) == 0) {
126 dev_dbg(dev, "%s: nsindex%d sequence: %#x invalid\n",
127 __func__, i, seq);
128 continue;
129 }
130
131 /* sanity check the index against expected values */
132 if (__le64_to_cpu(nsindex[i]->myoff)
133 != i * sizeof_namespace_index(ndd)) {
134 dev_dbg(dev, "%s: nsindex%d myoff: %#llx invalid\n",
135 __func__, i, (unsigned long long)
136 __le64_to_cpu(nsindex[i]->myoff));
137 continue;
138 }
139 if (__le64_to_cpu(nsindex[i]->otheroff)
140 != (!i) * sizeof_namespace_index(ndd)) {
141 dev_dbg(dev, "%s: nsindex%d otheroff: %#llx invalid\n",
142 __func__, i, (unsigned long long)
143 __le64_to_cpu(nsindex[i]->otheroff));
144 continue;
145 }
146
147 size = __le64_to_cpu(nsindex[i]->mysize);
148 if (size > sizeof_namespace_index(ndd)
149 || size < sizeof(struct nd_namespace_index)) {
150 dev_dbg(dev, "%s: nsindex%d mysize: %#llx invalid\n",
151 __func__, i, size);
152 continue;
153 }
154
155 nslot = __le32_to_cpu(nsindex[i]->nslot);
156 if (nslot * sizeof(struct nd_namespace_label)
157 + 2 * sizeof_namespace_index(ndd)
158 > ndd->nsarea.config_size) {
159 dev_dbg(dev, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
160 __func__, i, nslot,
161 ndd->nsarea.config_size);
162 continue;
163 }
164 valid[i] = true;
165 num_valid++;
166 }
167
168 switch (num_valid) {
169 case 0:
170 break;
171 case 1:
172 for (i = 0; i < num_index; i++)
173 if (valid[i])
174 return i;
175 /* can't have num_valid > 0 but valid[] = { false, false } */
176 WARN_ON(1);
177 break;
178 default:
179 /* pick the best index... */
180 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
181 __le32_to_cpu(nsindex[1]->seq));
182 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
183 return 1;
184 else
185 return 0;
186 break;
187 }
188
189 return -1;
190}
191
192void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
193 struct nd_namespace_index *src)
194{
195 if (dst && src)
196 /* pass */;
197 else
198 return;
199
200 memcpy(dst, src, sizeof_namespace_index(ndd));
201}
202
203static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
204{
205 void *base = to_namespace_index(ndd, 0);
206
207 return base + 2 * sizeof_namespace_index(ndd);
208}
209
Dan Williamsf524bf22015-05-30 12:36:02 -0400210static int to_slot(struct nvdimm_drvdata *ndd,
211 struct nd_namespace_label *nd_label)
212{
213 return nd_label - nd_label_base(ndd);
214}
215
Dan Williams4a826c82015-06-09 16:09:36 -0400216#define for_each_clear_bit_le(bit, addr, size) \
217 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
218 (bit) < (size); \
219 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
220
221/**
Dan Williamsf524bf22015-05-30 12:36:02 -0400222 * preamble_index - common variable initialization for nd_label_* routines
Dan Williams4a826c82015-06-09 16:09:36 -0400223 * @ndd: dimm container for the relevant label set
Dan Williamsf524bf22015-05-30 12:36:02 -0400224 * @idx: namespace_index index
Dan Williams4a826c82015-06-09 16:09:36 -0400225 * @nsindex_out: on return set to the currently active namespace index
226 * @free: on return set to the free label bitmap in the index
227 * @nslot: on return set to the number of slots in the label space
228 */
Dan Williamsf524bf22015-05-30 12:36:02 -0400229static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
Dan Williams4a826c82015-06-09 16:09:36 -0400230 struct nd_namespace_index **nsindex_out,
231 unsigned long **free, u32 *nslot)
232{
233 struct nd_namespace_index *nsindex;
234
Dan Williamsf524bf22015-05-30 12:36:02 -0400235 nsindex = to_namespace_index(ndd, idx);
Dan Williams4a826c82015-06-09 16:09:36 -0400236 if (nsindex == NULL)
237 return false;
238
239 *free = (unsigned long *) nsindex->free;
240 *nslot = __le32_to_cpu(nsindex->nslot);
241 *nsindex_out = nsindex;
242
243 return true;
244}
245
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400246char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
Dan Williams4a826c82015-06-09 16:09:36 -0400247{
248 if (!label_id || !uuid)
249 return NULL;
250 snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
251 flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
252 return label_id->id;
253}
254
Dan Williamsf524bf22015-05-30 12:36:02 -0400255static bool preamble_current(struct nvdimm_drvdata *ndd,
256 struct nd_namespace_index **nsindex,
257 unsigned long **free, u32 *nslot)
258{
259 return preamble_index(ndd, ndd->ns_current, nsindex,
260 free, nslot);
261}
262
263static bool preamble_next(struct nvdimm_drvdata *ndd,
264 struct nd_namespace_index **nsindex,
265 unsigned long **free, u32 *nslot)
266{
267 return preamble_index(ndd, ndd->ns_next, nsindex,
268 free, nslot);
269}
270
Dan Williams4a826c82015-06-09 16:09:36 -0400271static bool slot_valid(struct nd_namespace_label *nd_label, u32 slot)
272{
273 /* check that we are written where we expect to be written */
274 if (slot != __le32_to_cpu(nd_label->slot))
275 return false;
276
277 /* check that DPA allocations are page aligned */
278 if ((__le64_to_cpu(nd_label->dpa)
279 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
280 return false;
281
282 return true;
283}
284
285int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
286{
287 struct nd_namespace_index *nsindex;
288 unsigned long *free;
289 u32 nslot, slot;
290
291 if (!preamble_current(ndd, &nsindex, &free, &nslot))
292 return 0; /* no label, nothing to reserve */
293
294 for_each_clear_bit_le(slot, free, nslot) {
295 struct nd_namespace_label *nd_label;
296 struct nd_region *nd_region = NULL;
297 u8 label_uuid[NSLABEL_UUID_LEN];
298 struct nd_label_id label_id;
299 struct resource *res;
300 u32 flags;
301
302 nd_label = nd_label_base(ndd) + slot;
303
304 if (!slot_valid(nd_label, slot))
305 continue;
306
307 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
308 flags = __le32_to_cpu(nd_label->flags);
309 nd_label_gen_id(&label_id, label_uuid, flags);
310 res = nvdimm_allocate_dpa(ndd, &label_id,
311 __le64_to_cpu(nd_label->dpa),
312 __le64_to_cpu(nd_label->rawsize));
313 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
314 if (!res)
315 return -EBUSY;
316 }
317
318 return 0;
319}
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400320
321int nd_label_active_count(struct nvdimm_drvdata *ndd)
322{
323 struct nd_namespace_index *nsindex;
324 unsigned long *free;
325 u32 nslot, slot;
326 int count = 0;
327
328 if (!preamble_current(ndd, &nsindex, &free, &nslot))
329 return 0;
330
331 for_each_clear_bit_le(slot, free, nslot) {
332 struct nd_namespace_label *nd_label;
333
334 nd_label = nd_label_base(ndd) + slot;
335
336 if (!slot_valid(nd_label, slot)) {
337 u32 label_slot = __le32_to_cpu(nd_label->slot);
338 u64 size = __le64_to_cpu(nd_label->rawsize);
339 u64 dpa = __le64_to_cpu(nd_label->dpa);
340
341 dev_dbg(ndd->dev,
342 "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
343 __func__, slot, label_slot, dpa, size);
344 continue;
345 }
346 count++;
347 }
348 return count;
349}
350
351struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
352{
353 struct nd_namespace_index *nsindex;
354 unsigned long *free;
355 u32 nslot, slot;
356
357 if (!preamble_current(ndd, &nsindex, &free, &nslot))
358 return NULL;
359
360 for_each_clear_bit_le(slot, free, nslot) {
361 struct nd_namespace_label *nd_label;
362
363 nd_label = nd_label_base(ndd) + slot;
364 if (!slot_valid(nd_label, slot))
365 continue;
366
367 if (n-- == 0)
368 return nd_label_base(ndd) + slot;
369 }
370
371 return NULL;
372}
Dan Williamsf524bf22015-05-30 12:36:02 -0400373
Dan Williams0ba1c632015-05-30 12:35:36 -0400374u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
Dan Williamsf524bf22015-05-30 12:36:02 -0400375{
376 struct nd_namespace_index *nsindex;
377 unsigned long *free;
378 u32 nslot, slot;
379
380 if (!preamble_next(ndd, &nsindex, &free, &nslot))
381 return UINT_MAX;
382
383 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
384
385 slot = find_next_bit_le(free, nslot, 0);
386 if (slot == nslot)
387 return UINT_MAX;
388
389 clear_bit_le(slot, free);
390
391 return slot;
392}
393
Dan Williams0ba1c632015-05-30 12:35:36 -0400394bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
Dan Williamsf524bf22015-05-30 12:36:02 -0400395{
396 struct nd_namespace_index *nsindex;
397 unsigned long *free;
398 u32 nslot;
399
400 if (!preamble_next(ndd, &nsindex, &free, &nslot))
401 return false;
402
403 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
404
405 if (slot < nslot)
406 return !test_and_set_bit_le(slot, free);
407 return false;
408}
409
410u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
411{
412 struct nd_namespace_index *nsindex;
413 unsigned long *free;
414 u32 nslot;
415
416 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
417
418 if (!preamble_next(ndd, &nsindex, &free, &nslot))
Dan Williams0ba1c632015-05-30 12:35:36 -0400419 return nvdimm_num_label_slots(ndd);
Dan Williamsf524bf22015-05-30 12:36:02 -0400420
421 return bitmap_weight(free, nslot);
422}
423
424static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
425 unsigned long flags)
426{
427 struct nd_namespace_index *nsindex;
428 unsigned long offset;
429 u64 checksum;
430 u32 nslot;
431 int rc;
432
433 nsindex = to_namespace_index(ndd, index);
434 if (flags & ND_NSINDEX_INIT)
435 nslot = nvdimm_num_label_slots(ndd);
436 else
437 nslot = __le32_to_cpu(nsindex->nslot);
438
439 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
440 nsindex->flags = __cpu_to_le32(0);
441 nsindex->seq = __cpu_to_le32(seq);
442 offset = (unsigned long) nsindex
443 - (unsigned long) to_namespace_index(ndd, 0);
444 nsindex->myoff = __cpu_to_le64(offset);
445 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
446 offset = (unsigned long) to_namespace_index(ndd,
447 nd_label_next_nsindex(index))
448 - (unsigned long) to_namespace_index(ndd, 0);
449 nsindex->otheroff = __cpu_to_le64(offset);
450 offset = (unsigned long) nd_label_base(ndd)
451 - (unsigned long) to_namespace_index(ndd, 0);
452 nsindex->labeloff = __cpu_to_le64(offset);
453 nsindex->nslot = __cpu_to_le32(nslot);
454 nsindex->major = __cpu_to_le16(1);
455 nsindex->minor = __cpu_to_le16(1);
456 nsindex->checksum = __cpu_to_le64(0);
457 if (flags & ND_NSINDEX_INIT) {
458 unsigned long *free = (unsigned long *) nsindex->free;
459 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
460 int last_bits, i;
461
462 memset(nsindex->free, 0xff, nfree / 8);
463 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
464 clear_bit_le(nslot + i, free);
465 }
466 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
467 nsindex->checksum = __cpu_to_le64(checksum);
468 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
469 nsindex, sizeof_namespace_index(ndd));
470 if (rc < 0)
471 return rc;
472
473 if (flags & ND_NSINDEX_INIT)
474 return 0;
475
476 /* copy the index we just wrote to the new 'next' */
477 WARN_ON(index != ndd->ns_next);
478 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
479 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
480 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
481 WARN_ON(ndd->ns_current == ndd->ns_next);
482
483 return 0;
484}
485
486static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
487 struct nd_namespace_label *nd_label)
488{
489 return (unsigned long) nd_label
490 - (unsigned long) to_namespace_index(ndd, 0);
491}
492
Dan Williamsef274962019-04-30 21:51:21 -0700493static void reap_victim(struct nd_mapping *nd_mapping,
494 struct nd_label_ent *victim)
495{
496 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
497 u32 slot = to_slot(ndd, victim->label);
498
499 dev_dbg(ndd->dev, "free: %d\n", slot);
500 nd_label_free_slot(ndd, slot);
501 victim->label = NULL;
502}
503
Dan Williamsf524bf22015-05-30 12:36:02 -0400504static int __pmem_label_update(struct nd_region *nd_region,
505 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
Dan Williams44655322019-01-15 10:47:00 -0800506 int pos, unsigned long flags)
Dan Williamsf524bf22015-05-30 12:36:02 -0400507{
Dan Williams16660ea2016-10-05 21:13:23 -0700508 u64 cookie = nd_region_interleave_set_cookie(nd_region);
Dan Williamsf524bf22015-05-30 12:36:02 -0400509 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsf524bf22015-05-30 12:36:02 -0400510 struct nd_namespace_label *nd_label;
511 struct nd_namespace_index *nsindex;
Dan Williamsef274962019-04-30 21:51:21 -0700512 struct nd_label_ent *label_ent;
Dan Williams16660ea2016-10-05 21:13:23 -0700513 struct nd_label_id label_id;
514 struct resource *res;
Dan Williamsf524bf22015-05-30 12:36:02 -0400515 unsigned long *free;
516 u32 nslot, slot;
517 size_t offset;
518 int rc;
519
520 if (!preamble_next(ndd, &nsindex, &free, &nslot))
521 return -ENXIO;
522
Dan Williams16660ea2016-10-05 21:13:23 -0700523 nd_label_gen_id(&label_id, nspm->uuid, 0);
524 for_each_dpa_resource(ndd, res)
525 if (strcmp(res->name, label_id.id) == 0)
526 break;
527
528 if (!res) {
529 WARN_ON_ONCE(1);
530 return -ENXIO;
531 }
532
Dan Williamsf524bf22015-05-30 12:36:02 -0400533 /* allocate and write the label to the staging (next) index */
534 slot = nd_label_alloc_slot(ndd);
535 if (slot == UINT_MAX)
536 return -ENXIO;
537 dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
538
539 nd_label = nd_label_base(ndd) + slot;
540 memset(nd_label, 0, sizeof(struct nd_namespace_label));
541 memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
542 if (nspm->alt_name)
543 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
Dan Williams44655322019-01-15 10:47:00 -0800544 nd_label->flags = __cpu_to_le32(flags);
Dan Williamsf524bf22015-05-30 12:36:02 -0400545 nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
546 nd_label->position = __cpu_to_le16(pos);
547 nd_label->isetcookie = __cpu_to_le64(cookie);
Dan Williams16660ea2016-10-05 21:13:23 -0700548 nd_label->rawsize = __cpu_to_le64(resource_size(res));
549 nd_label->dpa = __cpu_to_le64(res->start);
Dan Williamsf524bf22015-05-30 12:36:02 -0400550 nd_label->slot = __cpu_to_le32(slot);
Dan Williams16660ea2016-10-05 21:13:23 -0700551 nd_dbg_dpa(nd_region, ndd, res, "%s\n", __func__);
Dan Williamsf524bf22015-05-30 12:36:02 -0400552
553 /* update label */
554 offset = nd_label_offset(ndd, nd_label);
555 rc = nvdimm_set_config_data(ndd, offset, nd_label,
556 sizeof(struct nd_namespace_label));
557 if (rc < 0)
558 return rc;
559
560 /* Garbage collect the previous label */
Dan Williamsae8219f2016-09-19 16:04:21 -0700561 mutex_lock(&nd_mapping->lock);
Dan Williams16660ea2016-10-05 21:13:23 -0700562 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
563 if (!label_ent->label)
564 continue;
Dan Williamsef274962019-04-30 21:51:21 -0700565 if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags)
566 || memcmp(nspm->uuid, label_ent->label->uuid,
567 NSLABEL_UUID_LEN) == 0)
568 reap_victim(nd_mapping, label_ent);
Dan Williamsf524bf22015-05-30 12:36:02 -0400569 }
570
571 /* update index */
572 rc = nd_label_write_index(ndd, ndd->ns_next,
573 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
Dan Williams16660ea2016-10-05 21:13:23 -0700574 if (rc == 0) {
575 list_for_each_entry(label_ent, &nd_mapping->labels, list)
576 if (!label_ent->label) {
577 label_ent->label = nd_label;
578 nd_label = NULL;
579 break;
580 }
581 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
582 "failed to track label: %d\n",
583 to_slot(ndd, nd_label));
584 if (nd_label)
585 rc = -ENXIO;
586 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700587 mutex_unlock(&nd_mapping->lock);
Dan Williamsf524bf22015-05-30 12:36:02 -0400588
Dan Williamsae8219f2016-09-19 16:04:21 -0700589 return rc;
Dan Williams0ba1c632015-05-30 12:35:36 -0400590}
591
592static bool is_old_resource(struct resource *res, struct resource **list, int n)
Dan Williamsf524bf22015-05-30 12:36:02 -0400593{
594 int i;
Dan Williams0ba1c632015-05-30 12:35:36 -0400595
596 if (res->flags & DPA_RESOURCE_ADJUSTED)
597 return false;
598 for (i = 0; i < n; i++)
599 if (res == list[i])
600 return true;
601 return false;
602}
603
604static struct resource *to_resource(struct nvdimm_drvdata *ndd,
605 struct nd_namespace_label *nd_label)
606{
607 struct resource *res;
608
609 for_each_dpa_resource(ndd, res) {
610 if (res->start != __le64_to_cpu(nd_label->dpa))
611 continue;
612 if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
613 continue;
614 return res;
615 }
616
617 return NULL;
618}
619
620/*
621 * 1/ Account all the labels that can be freed after this update
622 * 2/ Allocate and write the label to the staging (next) index
623 * 3/ Record the resources in the namespace device
624 */
625static int __blk_label_update(struct nd_region *nd_region,
626 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
627 int num_labels)
628{
Dan Williamsae8219f2016-09-19 16:04:21 -0700629 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
Dan Williamsf524bf22015-05-30 12:36:02 -0400630 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams0ba1c632015-05-30 12:35:36 -0400631 struct nd_namespace_label *nd_label;
Dan Williamsae8219f2016-09-19 16:04:21 -0700632 struct nd_label_ent *label_ent, *e;
Dan Williams0ba1c632015-05-30 12:35:36 -0400633 struct nd_namespace_index *nsindex;
634 unsigned long *free, *victim_map = NULL;
635 struct resource *res, **old_res_list;
636 struct nd_label_id label_id;
637 u8 uuid[NSLABEL_UUID_LEN];
Dan Williamsae8219f2016-09-19 16:04:21 -0700638 LIST_HEAD(list);
Dan Williams0ba1c632015-05-30 12:35:36 -0400639 u32 nslot, slot;
Dan Williamsf524bf22015-05-30 12:36:02 -0400640
Dan Williams0ba1c632015-05-30 12:35:36 -0400641 if (!preamble_next(ndd, &nsindex, &free, &nslot))
642 return -ENXIO;
Dan Williamsf524bf22015-05-30 12:36:02 -0400643
Dan Williams0ba1c632015-05-30 12:35:36 -0400644 old_res_list = nsblk->res;
645 nfree = nd_label_nfree(ndd);
646 old_num_resources = nsblk->num_resources;
647 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
648
649 /*
650 * We need to loop over the old resources a few times, which seems a
651 * bit inefficient, but we need to know that we have the label
652 * space before we start mutating the tracking structures.
653 * Otherwise the recovery method of last resort for userspace is
654 * disable and re-enable the parent region.
655 */
656 alloc = 0;
657 for_each_dpa_resource(ndd, res) {
658 if (strcmp(res->name, label_id.id) != 0)
659 continue;
660 if (!is_old_resource(res, old_res_list, old_num_resources))
661 alloc++;
662 }
663
664 victims = 0;
665 if (old_num_resources) {
666 /* convert old local-label-map to dimm-slot victim-map */
667 victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
668 GFP_KERNEL);
669 if (!victim_map)
670 return -ENOMEM;
671
672 /* mark unused labels for garbage collection */
673 for_each_clear_bit_le(slot, free, nslot) {
674 nd_label = nd_label_base(ndd) + slot;
675 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
676 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
677 continue;
678 res = to_resource(ndd, nd_label);
679 if (res && is_old_resource(res, old_res_list,
680 old_num_resources))
681 continue;
682 slot = to_slot(ndd, nd_label);
683 set_bit(slot, victim_map);
684 victims++;
685 }
686 }
687
688 /* don't allow updates that consume the last label */
689 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400690 dev_info(&nsblk->common.dev, "insufficient label space\n");
Dan Williams0ba1c632015-05-30 12:35:36 -0400691 kfree(victim_map);
692 return -ENOSPC;
693 }
694 /* from here on we need to abort on error */
695
696
697 /* assign all resources to the namespace before writing the labels */
698 nsblk->res = NULL;
699 nsblk->num_resources = 0;
700 for_each_dpa_resource(ndd, res) {
701 if (strcmp(res->name, label_id.id) != 0)
702 continue;
703 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
704 rc = -ENOMEM;
705 goto abort;
706 }
707 }
708
709 for (i = 0; i < nsblk->num_resources; i++) {
710 size_t offset;
711
712 res = nsblk->res[i];
713 if (is_old_resource(res, old_res_list, old_num_resources))
714 continue; /* carry-over */
715 slot = nd_label_alloc_slot(ndd);
716 if (slot == UINT_MAX)
717 goto abort;
718 dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
719
720 nd_label = nd_label_base(ndd) + slot;
721 memset(nd_label, 0, sizeof(struct nd_namespace_label));
722 memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
723 if (nsblk->alt_name)
724 memcpy(nd_label->name, nsblk->alt_name,
725 NSLABEL_NAME_LEN);
726 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
727 nd_label->nlabel = __cpu_to_le16(0); /* N/A */
728 nd_label->position = __cpu_to_le16(0); /* N/A */
729 nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
730 nd_label->dpa = __cpu_to_le64(res->start);
731 nd_label->rawsize = __cpu_to_le64(resource_size(res));
732 nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
733 nd_label->slot = __cpu_to_le32(slot);
734
735 /* update label */
736 offset = nd_label_offset(ndd, nd_label);
737 rc = nvdimm_set_config_data(ndd, offset, nd_label,
738 sizeof(struct nd_namespace_label));
739 if (rc < 0)
740 goto abort;
741 }
742
743 /* free up now unused slots in the new index */
744 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
745 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
746 nd_label_free_slot(ndd, slot);
747 }
748
749 /* update index */
750 rc = nd_label_write_index(ndd, ndd->ns_next,
751 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
752 if (rc)
753 goto abort;
754
755 /*
756 * Now that the on-dimm labels are up to date, fix up the tracking
757 * entries in nd_mapping->labels
758 */
759 nlabel = 0;
Dan Williamsae8219f2016-09-19 16:04:21 -0700760 mutex_lock(&nd_mapping->lock);
761 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
762 nd_label = label_ent->label;
763 if (!nd_label)
764 continue;
Dan Williams0ba1c632015-05-30 12:35:36 -0400765 nlabel++;
766 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
767 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
768 continue;
769 nlabel--;
Dan Williamsae8219f2016-09-19 16:04:21 -0700770 list_move(&label_ent->list, &list);
771 label_ent->label = NULL;
Dan Williams0ba1c632015-05-30 12:35:36 -0400772 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700773 list_splice_tail_init(&list, &nd_mapping->labels);
774 mutex_unlock(&nd_mapping->lock);
775
Dan Williams0ba1c632015-05-30 12:35:36 -0400776 if (nlabel + nsblk->num_resources > num_labels) {
777 /*
778 * Bug, we can't end up with more resources than
779 * available labels
780 */
781 WARN_ON_ONCE(1);
782 rc = -ENXIO;
783 goto out;
784 }
785
Dan Williamsae8219f2016-09-19 16:04:21 -0700786 mutex_lock(&nd_mapping->lock);
787 label_ent = list_first_entry_or_null(&nd_mapping->labels,
788 typeof(*label_ent), list);
789 if (!label_ent) {
790 WARN_ON(1);
791 mutex_unlock(&nd_mapping->lock);
792 rc = -ENXIO;
793 goto out;
794 }
Dan Williams0ba1c632015-05-30 12:35:36 -0400795 for_each_clear_bit_le(slot, free, nslot) {
796 nd_label = nd_label_base(ndd) + slot;
797 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
798 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
799 continue;
800 res = to_resource(ndd, nd_label);
801 res->flags &= ~DPA_RESOURCE_ADJUSTED;
Dan Williamsae8219f2016-09-19 16:04:21 -0700802 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
803 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
804 if (label_ent->label)
805 continue;
806 label_ent->label = nd_label;
807 nd_label = NULL;
808 break;
809 }
810 if (nd_label)
811 dev_WARN(&nsblk->common.dev,
812 "failed to track label slot%d\n", slot);
Dan Williams0ba1c632015-05-30 12:35:36 -0400813 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700814 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400815
816 out:
817 kfree(old_res_list);
818 kfree(victim_map);
819 return rc;
820
821 abort:
822 /*
823 * 1/ repair the allocated label bitmap in the index
824 * 2/ restore the resource list
825 */
826 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
827 kfree(nsblk->res);
828 nsblk->res = old_res_list;
829 nsblk->num_resources = old_num_resources;
830 old_res_list = NULL;
831 goto out;
832}
833
834static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
835{
Dan Williamsae8219f2016-09-19 16:04:21 -0700836 int i, old_num_labels = 0;
837 struct nd_label_ent *label_ent;
Dan Williams0ba1c632015-05-30 12:35:36 -0400838 struct nd_namespace_index *nsindex;
Dan Williams0ba1c632015-05-30 12:35:36 -0400839 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams0ba1c632015-05-30 12:35:36 -0400840
Dan Williamsae8219f2016-09-19 16:04:21 -0700841 mutex_lock(&nd_mapping->lock);
842 list_for_each_entry(label_ent, &nd_mapping->labels, list)
Dan Williams0ba1c632015-05-30 12:35:36 -0400843 old_num_labels++;
Dan Williamsae8219f2016-09-19 16:04:21 -0700844 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400845
846 /*
847 * We need to preserve all the old labels for the mapping so
848 * they can be garbage collected after writing the new labels.
849 */
Dan Williamsae8219f2016-09-19 16:04:21 -0700850 for (i = old_num_labels; i < num_labels; i++) {
851 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
852 if (!label_ent)
Dan Williams0ba1c632015-05-30 12:35:36 -0400853 return -ENOMEM;
Dan Williamsae8219f2016-09-19 16:04:21 -0700854 mutex_lock(&nd_mapping->lock);
855 list_add_tail(&label_ent->list, &nd_mapping->labels);
856 mutex_unlock(&nd_mapping->lock);
Dan Williams0ba1c632015-05-30 12:35:36 -0400857 }
Dan Williams0ba1c632015-05-30 12:35:36 -0400858
Dan Williamsf524bf22015-05-30 12:36:02 -0400859 if (ndd->ns_current == -1 || ndd->ns_next == -1)
860 /* pass */;
861 else
Dan Williams0ba1c632015-05-30 12:35:36 -0400862 return max(num_labels, old_num_labels);
Dan Williamsf524bf22015-05-30 12:36:02 -0400863
864 nsindex = to_namespace_index(ndd, 0);
865 memset(nsindex, 0, ndd->nsarea.config_size);
866 for (i = 0; i < 2; i++) {
Dan Williams6e83c892017-09-26 11:41:28 -0700867 int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
Dan Williamsf524bf22015-05-30 12:36:02 -0400868
869 if (rc)
870 return rc;
871 }
872 ndd->ns_next = 1;
873 ndd->ns_current = 0;
874
Dan Williams0ba1c632015-05-30 12:35:36 -0400875 return max(num_labels, old_num_labels);
Dan Williamsf524bf22015-05-30 12:36:02 -0400876}
877
878static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
879{
880 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -0700881 struct nd_label_ent *label_ent, *e;
Dan Williamsf524bf22015-05-30 12:36:02 -0400882 struct nd_namespace_index *nsindex;
883 u8 label_uuid[NSLABEL_UUID_LEN];
Dan Williamsf524bf22015-05-30 12:36:02 -0400884 unsigned long *free;
Dan Williamsae8219f2016-09-19 16:04:21 -0700885 LIST_HEAD(list);
Dan Williamsf524bf22015-05-30 12:36:02 -0400886 u32 nslot, slot;
Dan Williamsae8219f2016-09-19 16:04:21 -0700887 int active = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -0400888
889 if (!uuid)
890 return 0;
891
892 /* no index || no labels == nothing to delete */
Dan Williamsae8219f2016-09-19 16:04:21 -0700893 if (!preamble_next(ndd, &nsindex, &free, &nslot))
Dan Williamsf524bf22015-05-30 12:36:02 -0400894 return 0;
895
Dan Williamsae8219f2016-09-19 16:04:21 -0700896 mutex_lock(&nd_mapping->lock);
897 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
898 struct nd_namespace_label *nd_label = label_ent->label;
899
900 if (!nd_label)
901 continue;
902 active++;
Dan Williamsf524bf22015-05-30 12:36:02 -0400903 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
904 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
905 continue;
Dan Williamsae8219f2016-09-19 16:04:21 -0700906 active--;
Dan Williamsf524bf22015-05-30 12:36:02 -0400907 slot = to_slot(ndd, nd_label);
908 nd_label_free_slot(ndd, slot);
909 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
Dan Williamsae8219f2016-09-19 16:04:21 -0700910 list_move_tail(&label_ent->list, &list);
911 label_ent->label = NULL;
Dan Williamsf524bf22015-05-30 12:36:02 -0400912 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700913 list_splice_tail_init(&list, &nd_mapping->labels);
Dan Williamsf524bf22015-05-30 12:36:02 -0400914
Dan Williamsae8219f2016-09-19 16:04:21 -0700915 if (active == 0) {
916 nd_mapping_free_labels(nd_mapping);
917 dev_dbg(ndd->dev, "%s: no more active labels\n", __func__);
Dan Williamsf524bf22015-05-30 12:36:02 -0400918 }
Dan Williamsae8219f2016-09-19 16:04:21 -0700919 mutex_unlock(&nd_mapping->lock);
Dan Williamsf524bf22015-05-30 12:36:02 -0400920
921 return nd_label_write_index(ndd, ndd->ns_next,
922 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
923}
924
925int nd_pmem_namespace_label_update(struct nd_region *nd_region,
926 struct nd_namespace_pmem *nspm, resource_size_t size)
927{
Dan Williams44655322019-01-15 10:47:00 -0800928 int i, rc;
Dan Williamsf524bf22015-05-30 12:36:02 -0400929
930 for (i = 0; i < nd_region->ndr_mappings; i++) {
931 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams16660ea2016-10-05 21:13:23 -0700932 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
933 struct resource *res;
Dan Williams44655322019-01-15 10:47:00 -0800934 int count = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -0400935
936 if (size == 0) {
937 rc = del_labels(nd_mapping, nspm->uuid);
938 if (rc)
939 return rc;
940 continue;
941 }
942
Dan Williams16660ea2016-10-05 21:13:23 -0700943 for_each_dpa_resource(ndd, res)
944 if (strncmp(res->name, "pmem", 3) == 0)
945 count++;
946 WARN_ON_ONCE(!count);
947
948 rc = init_labels(nd_mapping, count);
Dan Williams0ba1c632015-05-30 12:35:36 -0400949 if (rc < 0)
Dan Williamsf524bf22015-05-30 12:36:02 -0400950 return rc;
951
Dan Williams44655322019-01-15 10:47:00 -0800952 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
953 NSLABEL_FLAG_UPDATING);
954 if (rc)
955 return rc;
956 }
957
958 if (size == 0)
959 return 0;
960
961 /* Clear the UPDATING flag per UEFI 2.7 expectations */
962 for (i = 0; i < nd_region->ndr_mappings; i++) {
963 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
964
965 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
Dan Williamsf524bf22015-05-30 12:36:02 -0400966 if (rc)
967 return rc;
968 }
969
970 return 0;
971}
Dan Williams0ba1c632015-05-30 12:35:36 -0400972
973int nd_blk_namespace_label_update(struct nd_region *nd_region,
974 struct nd_namespace_blk *nsblk, resource_size_t size)
975{
976 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
977 struct resource *res;
978 int count = 0;
979
980 if (size == 0)
981 return del_labels(nd_mapping, nsblk->uuid);
982
983 for_each_dpa_resource(to_ndd(nd_mapping), res)
984 count++;
985
986 count = init_labels(nd_mapping, count);
987 if (count < 0)
988 return count;
989
990 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
991}