Linux 4.19.133
[linux/fpc-iii.git] / drivers / nvdimm / label.c
blob9f1b7e3153f991866bc70667ad212e5d333097fc
1 /*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/device.h>
14 #include <linux/ndctl.h>
15 #include <linux/uuid.h>
16 #include <linux/slab.h>
17 #include <linux/io.h>
18 #include <linux/nd.h>
19 #include "nd-core.h"
20 #include "label.h"
21 #include "nd.h"
23 static guid_t nvdimm_btt_guid;
24 static guid_t nvdimm_btt2_guid;
25 static guid_t nvdimm_pfn_guid;
26 static guid_t nvdimm_dax_guid;
28 static const char NSINDEX_SIGNATURE[] = "NAMESPACE_INDEX\0";
30 static u32 best_seq(u32 a, u32 b)
32 a &= NSINDEX_SEQ_MASK;
33 b &= NSINDEX_SEQ_MASK;
35 if (a == 0 || a == b)
36 return b;
37 else if (b == 0)
38 return a;
39 else if (nd_inc_seq(a) == b)
40 return b;
41 else
42 return a;
45 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
47 return ndd->nslabel_size;
50 static size_t __sizeof_namespace_index(u32 nslot)
52 return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8),
53 NSINDEX_ALIGN);
56 static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd,
57 size_t index_size)
59 return (ndd->nsarea.config_size - index_size * 2) /
60 sizeof_namespace_label(ndd);
63 int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
65 u32 tmp_nslot, n;
67 tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd);
68 n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN;
70 return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n);
73 size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
75 u32 nslot, space, size;
78 * Per UEFI 2.7, the minimum size of the Label Storage Area is large
79 * enough to hold 2 index blocks and 2 labels. The minimum index
80 * block size is 256 bytes, and the minimum label size is 256 bytes.
82 nslot = nvdimm_num_label_slots(ndd);
83 space = ndd->nsarea.config_size - nslot * sizeof_namespace_label(ndd);
84 size = __sizeof_namespace_index(nslot) * 2;
85 if (size <= space && nslot >= 2)
86 return size / 2;
88 dev_err(ndd->dev, "label area (%d) too small to host (%d byte) labels\n",
89 ndd->nsarea.config_size, sizeof_namespace_label(ndd));
90 return 0;
93 static int __nd_label_validate(struct nvdimm_drvdata *ndd)
96 * On media label format consists of two index blocks followed
97 * by an array of labels. None of these structures are ever
98 * updated in place. A sequence number tracks the current
99 * active index and the next one to write, while labels are
100 * written to free slots.
102 * +------------+
103 * | |
104 * | nsindex0 |
105 * | |
106 * +------------+
107 * | |
108 * | nsindex1 |
109 * | |
110 * +------------+
111 * | label0 |
112 * +------------+
113 * | label1 |
114 * +------------+
115 * | |
116 * ....nslot...
117 * | |
118 * +------------+
119 * | labelN |
120 * +------------+
122 struct nd_namespace_index *nsindex[] = {
123 to_namespace_index(ndd, 0),
124 to_namespace_index(ndd, 1),
126 const int num_index = ARRAY_SIZE(nsindex);
127 struct device *dev = ndd->dev;
128 bool valid[2] = { 0 };
129 int i, num_valid = 0;
130 u32 seq;
132 for (i = 0; i < num_index; i++) {
133 u32 nslot;
134 u8 sig[NSINDEX_SIG_LEN];
135 u64 sum_save, sum, size;
136 unsigned int version, labelsize;
138 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
139 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
140 dev_dbg(dev, "nsindex%d signature invalid\n", i);
141 continue;
144 /* label sizes larger than 128 arrived with v1.2 */
145 version = __le16_to_cpu(nsindex[i]->major) * 100
146 + __le16_to_cpu(nsindex[i]->minor);
147 if (version >= 102)
148 labelsize = 1 << (7 + nsindex[i]->labelsize);
149 else
150 labelsize = 128;
152 if (labelsize != sizeof_namespace_label(ndd)) {
153 dev_dbg(dev, "nsindex%d labelsize %d invalid\n",
154 i, nsindex[i]->labelsize);
155 continue;
158 sum_save = __le64_to_cpu(nsindex[i]->checksum);
159 nsindex[i]->checksum = __cpu_to_le64(0);
160 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
161 nsindex[i]->checksum = __cpu_to_le64(sum_save);
162 if (sum != sum_save) {
163 dev_dbg(dev, "nsindex%d checksum invalid\n", i);
164 continue;
167 seq = __le32_to_cpu(nsindex[i]->seq);
168 if ((seq & NSINDEX_SEQ_MASK) == 0) {
169 dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq);
170 continue;
173 /* sanity check the index against expected values */
174 if (__le64_to_cpu(nsindex[i]->myoff)
175 != i * sizeof_namespace_index(ndd)) {
176 dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n",
177 i, (unsigned long long)
178 __le64_to_cpu(nsindex[i]->myoff));
179 continue;
181 if (__le64_to_cpu(nsindex[i]->otheroff)
182 != (!i) * sizeof_namespace_index(ndd)) {
183 dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n",
184 i, (unsigned long long)
185 __le64_to_cpu(nsindex[i]->otheroff));
186 continue;
189 size = __le64_to_cpu(nsindex[i]->mysize);
190 if (size > sizeof_namespace_index(ndd)
191 || size < sizeof(struct nd_namespace_index)) {
192 dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size);
193 continue;
196 nslot = __le32_to_cpu(nsindex[i]->nslot);
197 if (nslot * sizeof_namespace_label(ndd)
198 + 2 * sizeof_namespace_index(ndd)
199 > ndd->nsarea.config_size) {
200 dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n",
201 i, nslot, ndd->nsarea.config_size);
202 continue;
204 valid[i] = true;
205 num_valid++;
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;
229 return -1;
232 int nd_label_validate(struct nvdimm_drvdata *ndd)
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
239 * sizes, but default to v1.2 256-byte namespace labels if
240 * discovery fails.
242 int label_size[] = { 128, 256 };
243 int i, rc;
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;
252 return -1;
255 void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
256 struct nd_namespace_index *src)
258 if (dst && src)
259 /* pass */;
260 else
261 return;
263 memcpy(dst, src, sizeof_namespace_index(ndd));
266 static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
268 void *base = to_namespace_index(ndd, 0);
270 return base + 2 * sizeof_namespace_index(ndd);
273 static int to_slot(struct nvdimm_drvdata *ndd,
274 struct nd_namespace_label *nd_label)
276 unsigned long label, base;
278 label = (unsigned long) nd_label;
279 base = (unsigned long) nd_label_base(ndd);
281 return (label - base) / sizeof_namespace_label(ndd);
284 static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
286 unsigned long label, base;
288 base = (unsigned long) nd_label_base(ndd);
289 label = base + sizeof_namespace_label(ndd) * slot;
291 return (struct nd_namespace_label *) label;
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))
300 * preamble_index - common variable initialization for nd_label_* routines
301 * @ndd: dimm container for the relevant label set
302 * @idx: namespace_index index
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
307 static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
308 struct nd_namespace_index **nsindex_out,
309 unsigned long **free, u32 *nslot)
311 struct nd_namespace_index *nsindex;
313 nsindex = to_namespace_index(ndd, idx);
314 if (nsindex == NULL)
315 return false;
317 *free = (unsigned long *) nsindex->free;
318 *nslot = __le32_to_cpu(nsindex->nslot);
319 *nsindex_out = nsindex;
321 return true;
324 char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
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;
333 static bool preamble_current(struct nvdimm_drvdata *ndd,
334 struct nd_namespace_index **nsindex,
335 unsigned long **free, u32 *nslot)
337 return preamble_index(ndd, ndd->ns_current, nsindex,
338 free, nslot);
341 static bool preamble_next(struct nvdimm_drvdata *ndd,
342 struct nd_namespace_index **nsindex,
343 unsigned long **free, u32 *nslot)
345 return preamble_index(ndd, ndd->ns_next, nsindex,
346 free, nslot);
349 static bool slot_valid(struct nvdimm_drvdata *ndd,
350 struct nd_namespace_label *nd_label, u32 slot)
352 /* check that we are written where we expect to be written */
353 if (slot != __le32_to_cpu(nd_label->slot))
354 return false;
356 /* check that DPA allocations are page aligned */
357 if ((__le64_to_cpu(nd_label->dpa)
358 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
359 return false;
361 /* check checksum */
362 if (namespace_label_has(ndd, checksum)) {
363 u64 sum, sum_save;
365 sum_save = __le64_to_cpu(nd_label->checksum);
366 nd_label->checksum = __cpu_to_le64(0);
367 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
368 nd_label->checksum = __cpu_to_le64(sum_save);
369 if (sum != sum_save) {
370 dev_dbg(ndd->dev, "fail checksum. slot: %d expect: %#llx\n",
371 slot, sum);
372 return false;
376 return true;
379 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
381 struct nd_namespace_index *nsindex;
382 unsigned long *free;
383 u32 nslot, slot;
385 if (!preamble_current(ndd, &nsindex, &free, &nslot))
386 return 0; /* no label, nothing to reserve */
388 for_each_clear_bit_le(slot, free, nslot) {
389 struct nd_namespace_label *nd_label;
390 struct nd_region *nd_region = NULL;
391 u8 label_uuid[NSLABEL_UUID_LEN];
392 struct nd_label_id label_id;
393 struct resource *res;
394 u32 flags;
396 nd_label = to_label(ndd, slot);
398 if (!slot_valid(ndd, nd_label, slot))
399 continue;
401 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
402 flags = __le32_to_cpu(nd_label->flags);
403 nd_label_gen_id(&label_id, label_uuid, flags);
404 res = nvdimm_allocate_dpa(ndd, &label_id,
405 __le64_to_cpu(nd_label->dpa),
406 __le64_to_cpu(nd_label->rawsize));
407 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
408 if (!res)
409 return -EBUSY;
412 return 0;
415 int nd_label_active_count(struct nvdimm_drvdata *ndd)
417 struct nd_namespace_index *nsindex;
418 unsigned long *free;
419 u32 nslot, slot;
420 int count = 0;
422 if (!preamble_current(ndd, &nsindex, &free, &nslot))
423 return 0;
425 for_each_clear_bit_le(slot, free, nslot) {
426 struct nd_namespace_label *nd_label;
428 nd_label = to_label(ndd, slot);
430 if (!slot_valid(ndd, nd_label, slot)) {
431 u32 label_slot = __le32_to_cpu(nd_label->slot);
432 u64 size = __le64_to_cpu(nd_label->rawsize);
433 u64 dpa = __le64_to_cpu(nd_label->dpa);
435 dev_dbg(ndd->dev,
436 "slot%d invalid slot: %d dpa: %llx size: %llx\n",
437 slot, label_slot, dpa, size);
438 continue;
440 count++;
442 return count;
445 struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
447 struct nd_namespace_index *nsindex;
448 unsigned long *free;
449 u32 nslot, slot;
451 if (!preamble_current(ndd, &nsindex, &free, &nslot))
452 return NULL;
454 for_each_clear_bit_le(slot, free, nslot) {
455 struct nd_namespace_label *nd_label;
457 nd_label = to_label(ndd, slot);
458 if (!slot_valid(ndd, nd_label, slot))
459 continue;
461 if (n-- == 0)
462 return to_label(ndd, slot);
465 return NULL;
468 u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
470 struct nd_namespace_index *nsindex;
471 unsigned long *free;
472 u32 nslot, slot;
474 if (!preamble_next(ndd, &nsindex, &free, &nslot))
475 return UINT_MAX;
477 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
479 slot = find_next_bit_le(free, nslot, 0);
480 if (slot == nslot)
481 return UINT_MAX;
483 clear_bit_le(slot, free);
485 return slot;
488 bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
490 struct nd_namespace_index *nsindex;
491 unsigned long *free;
492 u32 nslot;
494 if (!preamble_next(ndd, &nsindex, &free, &nslot))
495 return false;
497 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
499 if (slot < nslot)
500 return !test_and_set_bit_le(slot, free);
501 return false;
504 u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
506 struct nd_namespace_index *nsindex;
507 unsigned long *free;
508 u32 nslot;
510 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
512 if (!preamble_next(ndd, &nsindex, &free, &nslot))
513 return nvdimm_num_label_slots(ndd);
515 return bitmap_weight(free, nslot);
518 static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
519 unsigned long flags)
521 struct nd_namespace_index *nsindex;
522 unsigned long offset;
523 u64 checksum;
524 u32 nslot;
525 int rc;
527 nsindex = to_namespace_index(ndd, index);
528 if (flags & ND_NSINDEX_INIT)
529 nslot = nvdimm_num_label_slots(ndd);
530 else
531 nslot = __le32_to_cpu(nsindex->nslot);
533 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
534 memset(&nsindex->flags, 0, 3);
535 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
536 nsindex->seq = __cpu_to_le32(seq);
537 offset = (unsigned long) nsindex
538 - (unsigned long) to_namespace_index(ndd, 0);
539 nsindex->myoff = __cpu_to_le64(offset);
540 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
541 offset = (unsigned long) to_namespace_index(ndd,
542 nd_label_next_nsindex(index))
543 - (unsigned long) to_namespace_index(ndd, 0);
544 nsindex->otheroff = __cpu_to_le64(offset);
545 offset = (unsigned long) nd_label_base(ndd)
546 - (unsigned long) to_namespace_index(ndd, 0);
547 nsindex->labeloff = __cpu_to_le64(offset);
548 nsindex->nslot = __cpu_to_le32(nslot);
549 nsindex->major = __cpu_to_le16(1);
550 if (sizeof_namespace_label(ndd) < 256)
551 nsindex->minor = __cpu_to_le16(1);
552 else
553 nsindex->minor = __cpu_to_le16(2);
554 nsindex->checksum = __cpu_to_le64(0);
555 if (flags & ND_NSINDEX_INIT) {
556 unsigned long *free = (unsigned long *) nsindex->free;
557 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
558 int last_bits, i;
560 memset(nsindex->free, 0xff, nfree / 8);
561 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
562 clear_bit_le(nslot + i, free);
564 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
565 nsindex->checksum = __cpu_to_le64(checksum);
566 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
567 nsindex, sizeof_namespace_index(ndd));
568 if (rc < 0)
569 return rc;
571 if (flags & ND_NSINDEX_INIT)
572 return 0;
574 /* copy the index we just wrote to the new 'next' */
575 WARN_ON(index != ndd->ns_next);
576 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
577 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
578 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
579 WARN_ON(ndd->ns_current == ndd->ns_next);
581 return 0;
584 static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
585 struct nd_namespace_label *nd_label)
587 return (unsigned long) nd_label
588 - (unsigned long) to_namespace_index(ndd, 0);
591 enum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid)
593 if (guid_equal(guid, &nvdimm_btt_guid))
594 return NVDIMM_CCLASS_BTT;
595 else if (guid_equal(guid, &nvdimm_btt2_guid))
596 return NVDIMM_CCLASS_BTT2;
597 else if (guid_equal(guid, &nvdimm_pfn_guid))
598 return NVDIMM_CCLASS_PFN;
599 else if (guid_equal(guid, &nvdimm_dax_guid))
600 return NVDIMM_CCLASS_DAX;
601 else if (guid_equal(guid, &guid_null))
602 return NVDIMM_CCLASS_NONE;
604 return NVDIMM_CCLASS_UNKNOWN;
607 static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
608 guid_t *target)
610 if (claim_class == NVDIMM_CCLASS_BTT)
611 return &nvdimm_btt_guid;
612 else if (claim_class == NVDIMM_CCLASS_BTT2)
613 return &nvdimm_btt2_guid;
614 else if (claim_class == NVDIMM_CCLASS_PFN)
615 return &nvdimm_pfn_guid;
616 else if (claim_class == NVDIMM_CCLASS_DAX)
617 return &nvdimm_dax_guid;
618 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
620 * If we're modifying a namespace for which we don't
621 * know the claim_class, don't touch the existing guid.
623 return target;
624 } else
625 return &guid_null;
628 static void reap_victim(struct nd_mapping *nd_mapping,
629 struct nd_label_ent *victim)
631 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
632 u32 slot = to_slot(ndd, victim->label);
634 dev_dbg(ndd->dev, "free: %d\n", slot);
635 nd_label_free_slot(ndd, slot);
636 victim->label = NULL;
639 static int __pmem_label_update(struct nd_region *nd_region,
640 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
641 int pos, unsigned long flags)
643 struct nd_namespace_common *ndns = &nspm->nsio.common;
644 struct nd_interleave_set *nd_set = nd_region->nd_set;
645 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
646 struct nd_namespace_label *nd_label;
647 struct nd_namespace_index *nsindex;
648 struct nd_label_ent *label_ent;
649 struct nd_label_id label_id;
650 struct resource *res;
651 unsigned long *free;
652 u32 nslot, slot;
653 size_t offset;
654 u64 cookie;
655 int rc;
657 if (!preamble_next(ndd, &nsindex, &free, &nslot))
658 return -ENXIO;
660 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
661 nd_label_gen_id(&label_id, nspm->uuid, 0);
662 for_each_dpa_resource(ndd, res)
663 if (strcmp(res->name, label_id.id) == 0)
664 break;
666 if (!res) {
667 WARN_ON_ONCE(1);
668 return -ENXIO;
671 /* allocate and write the label to the staging (next) index */
672 slot = nd_label_alloc_slot(ndd);
673 if (slot == UINT_MAX)
674 return -ENXIO;
675 dev_dbg(ndd->dev, "allocated: %d\n", slot);
677 nd_label = to_label(ndd, slot);
678 memset(nd_label, 0, sizeof_namespace_label(ndd));
679 memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
680 if (nspm->alt_name)
681 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
682 nd_label->flags = __cpu_to_le32(flags);
683 nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
684 nd_label->position = __cpu_to_le16(pos);
685 nd_label->isetcookie = __cpu_to_le64(cookie);
686 nd_label->rawsize = __cpu_to_le64(resource_size(res));
687 nd_label->lbasize = __cpu_to_le64(nspm->lbasize);
688 nd_label->dpa = __cpu_to_le64(res->start);
689 nd_label->slot = __cpu_to_le32(slot);
690 if (namespace_label_has(ndd, type_guid))
691 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
692 if (namespace_label_has(ndd, abstraction_guid))
693 guid_copy(&nd_label->abstraction_guid,
694 to_abstraction_guid(ndns->claim_class,
695 &nd_label->abstraction_guid));
696 if (namespace_label_has(ndd, checksum)) {
697 u64 sum;
699 nd_label->checksum = __cpu_to_le64(0);
700 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
701 nd_label->checksum = __cpu_to_le64(sum);
703 nd_dbg_dpa(nd_region, ndd, res, "\n");
705 /* update label */
706 offset = nd_label_offset(ndd, nd_label);
707 rc = nvdimm_set_config_data(ndd, offset, nd_label,
708 sizeof_namespace_label(ndd));
709 if (rc < 0)
710 return rc;
712 /* Garbage collect the previous label */
713 mutex_lock(&nd_mapping->lock);
714 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
715 if (!label_ent->label)
716 continue;
717 if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags)
718 || memcmp(nspm->uuid, label_ent->label->uuid,
719 NSLABEL_UUID_LEN) == 0)
720 reap_victim(nd_mapping, label_ent);
723 /* update index */
724 rc = nd_label_write_index(ndd, ndd->ns_next,
725 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
726 if (rc == 0) {
727 list_for_each_entry(label_ent, &nd_mapping->labels, list)
728 if (!label_ent->label) {
729 label_ent->label = nd_label;
730 nd_label = NULL;
731 break;
733 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
734 "failed to track label: %d\n",
735 to_slot(ndd, nd_label));
736 if (nd_label)
737 rc = -ENXIO;
739 mutex_unlock(&nd_mapping->lock);
741 return rc;
744 static bool is_old_resource(struct resource *res, struct resource **list, int n)
746 int i;
748 if (res->flags & DPA_RESOURCE_ADJUSTED)
749 return false;
750 for (i = 0; i < n; i++)
751 if (res == list[i])
752 return true;
753 return false;
756 static struct resource *to_resource(struct nvdimm_drvdata *ndd,
757 struct nd_namespace_label *nd_label)
759 struct resource *res;
761 for_each_dpa_resource(ndd, res) {
762 if (res->start != __le64_to_cpu(nd_label->dpa))
763 continue;
764 if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
765 continue;
766 return res;
769 return NULL;
773 * 1/ Account all the labels that can be freed after this update
774 * 2/ Allocate and write the label to the staging (next) index
775 * 3/ Record the resources in the namespace device
777 static int __blk_label_update(struct nd_region *nd_region,
778 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
779 int num_labels)
781 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
782 struct nd_interleave_set *nd_set = nd_region->nd_set;
783 struct nd_namespace_common *ndns = &nsblk->common;
784 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
785 struct nd_namespace_label *nd_label;
786 struct nd_label_ent *label_ent, *e;
787 struct nd_namespace_index *nsindex;
788 unsigned long *free, *victim_map = NULL;
789 struct resource *res, **old_res_list;
790 struct nd_label_id label_id;
791 u8 uuid[NSLABEL_UUID_LEN];
792 int min_dpa_idx = 0;
793 LIST_HEAD(list);
794 u32 nslot, slot;
796 if (!preamble_next(ndd, &nsindex, &free, &nslot))
797 return -ENXIO;
799 old_res_list = nsblk->res;
800 nfree = nd_label_nfree(ndd);
801 old_num_resources = nsblk->num_resources;
802 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
805 * We need to loop over the old resources a few times, which seems a
806 * bit inefficient, but we need to know that we have the label
807 * space before we start mutating the tracking structures.
808 * Otherwise the recovery method of last resort for userspace is
809 * disable and re-enable the parent region.
811 alloc = 0;
812 for_each_dpa_resource(ndd, res) {
813 if (strcmp(res->name, label_id.id) != 0)
814 continue;
815 if (!is_old_resource(res, old_res_list, old_num_resources))
816 alloc++;
819 victims = 0;
820 if (old_num_resources) {
821 /* convert old local-label-map to dimm-slot victim-map */
822 victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
823 GFP_KERNEL);
824 if (!victim_map)
825 return -ENOMEM;
827 /* mark unused labels for garbage collection */
828 for_each_clear_bit_le(slot, free, nslot) {
829 nd_label = to_label(ndd, slot);
830 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
831 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
832 continue;
833 res = to_resource(ndd, nd_label);
834 if (res && is_old_resource(res, old_res_list,
835 old_num_resources))
836 continue;
837 slot = to_slot(ndd, nd_label);
838 set_bit(slot, victim_map);
839 victims++;
843 /* don't allow updates that consume the last label */
844 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
845 dev_info(&nsblk->common.dev, "insufficient label space\n");
846 kfree(victim_map);
847 return -ENOSPC;
849 /* from here on we need to abort on error */
852 /* assign all resources to the namespace before writing the labels */
853 nsblk->res = NULL;
854 nsblk->num_resources = 0;
855 for_each_dpa_resource(ndd, res) {
856 if (strcmp(res->name, label_id.id) != 0)
857 continue;
858 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
859 rc = -ENOMEM;
860 goto abort;
865 * Find the resource associated with the first label in the set
866 * per the v1.2 namespace specification.
868 for (i = 0; i < nsblk->num_resources; i++) {
869 struct resource *min = nsblk->res[min_dpa_idx];
871 res = nsblk->res[i];
872 if (res->start < min->start)
873 min_dpa_idx = i;
876 for (i = 0; i < nsblk->num_resources; i++) {
877 size_t offset;
879 res = nsblk->res[i];
880 if (is_old_resource(res, old_res_list, old_num_resources))
881 continue; /* carry-over */
882 slot = nd_label_alloc_slot(ndd);
883 if (slot == UINT_MAX)
884 goto abort;
885 dev_dbg(ndd->dev, "allocated: %d\n", slot);
887 nd_label = to_label(ndd, slot);
888 memset(nd_label, 0, sizeof_namespace_label(ndd));
889 memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
890 if (nsblk->alt_name)
891 memcpy(nd_label->name, nsblk->alt_name,
892 NSLABEL_NAME_LEN);
893 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
896 * Use the presence of the type_guid as a flag to
897 * determine isetcookie usage and nlabel + position
898 * policy for blk-aperture namespaces.
900 if (namespace_label_has(ndd, type_guid)) {
901 if (i == min_dpa_idx) {
902 nd_label->nlabel = __cpu_to_le16(nsblk->num_resources);
903 nd_label->position = __cpu_to_le16(0);
904 } else {
905 nd_label->nlabel = __cpu_to_le16(0xffff);
906 nd_label->position = __cpu_to_le16(0xffff);
908 nd_label->isetcookie = __cpu_to_le64(nd_set->cookie2);
909 } else {
910 nd_label->nlabel = __cpu_to_le16(0); /* N/A */
911 nd_label->position = __cpu_to_le16(0); /* N/A */
912 nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
915 nd_label->dpa = __cpu_to_le64(res->start);
916 nd_label->rawsize = __cpu_to_le64(resource_size(res));
917 nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
918 nd_label->slot = __cpu_to_le32(slot);
919 if (namespace_label_has(ndd, type_guid))
920 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
921 if (namespace_label_has(ndd, abstraction_guid))
922 guid_copy(&nd_label->abstraction_guid,
923 to_abstraction_guid(ndns->claim_class,
924 &nd_label->abstraction_guid));
926 if (namespace_label_has(ndd, checksum)) {
927 u64 sum;
929 nd_label->checksum = __cpu_to_le64(0);
930 sum = nd_fletcher64(nd_label,
931 sizeof_namespace_label(ndd), 1);
932 nd_label->checksum = __cpu_to_le64(sum);
935 /* update label */
936 offset = nd_label_offset(ndd, nd_label);
937 rc = nvdimm_set_config_data(ndd, offset, nd_label,
938 sizeof_namespace_label(ndd));
939 if (rc < 0)
940 goto abort;
943 /* free up now unused slots in the new index */
944 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
945 dev_dbg(ndd->dev, "free: %d\n", slot);
946 nd_label_free_slot(ndd, slot);
949 /* update index */
950 rc = nd_label_write_index(ndd, ndd->ns_next,
951 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
952 if (rc)
953 goto abort;
956 * Now that the on-dimm labels are up to date, fix up the tracking
957 * entries in nd_mapping->labels
959 nlabel = 0;
960 mutex_lock(&nd_mapping->lock);
961 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
962 nd_label = label_ent->label;
963 if (!nd_label)
964 continue;
965 nlabel++;
966 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
967 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
968 continue;
969 nlabel--;
970 list_move(&label_ent->list, &list);
971 label_ent->label = NULL;
973 list_splice_tail_init(&list, &nd_mapping->labels);
974 mutex_unlock(&nd_mapping->lock);
976 if (nlabel + nsblk->num_resources > num_labels) {
978 * Bug, we can't end up with more resources than
979 * available labels
981 WARN_ON_ONCE(1);
982 rc = -ENXIO;
983 goto out;
986 mutex_lock(&nd_mapping->lock);
987 label_ent = list_first_entry_or_null(&nd_mapping->labels,
988 typeof(*label_ent), list);
989 if (!label_ent) {
990 WARN_ON(1);
991 mutex_unlock(&nd_mapping->lock);
992 rc = -ENXIO;
993 goto out;
995 for_each_clear_bit_le(slot, free, nslot) {
996 nd_label = to_label(ndd, slot);
997 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
998 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
999 continue;
1000 res = to_resource(ndd, nd_label);
1001 res->flags &= ~DPA_RESOURCE_ADJUSTED;
1002 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
1003 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
1004 if (label_ent->label)
1005 continue;
1006 label_ent->label = nd_label;
1007 nd_label = NULL;
1008 break;
1010 if (nd_label)
1011 dev_WARN(&nsblk->common.dev,
1012 "failed to track label slot%d\n", slot);
1014 mutex_unlock(&nd_mapping->lock);
1016 out:
1017 kfree(old_res_list);
1018 kfree(victim_map);
1019 return rc;
1021 abort:
1023 * 1/ repair the allocated label bitmap in the index
1024 * 2/ restore the resource list
1026 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
1027 kfree(nsblk->res);
1028 nsblk->res = old_res_list;
1029 nsblk->num_resources = old_num_resources;
1030 old_res_list = NULL;
1031 goto out;
1034 static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
1036 int i, old_num_labels = 0;
1037 struct nd_label_ent *label_ent;
1038 struct nd_namespace_index *nsindex;
1039 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1041 mutex_lock(&nd_mapping->lock);
1042 list_for_each_entry(label_ent, &nd_mapping->labels, list)
1043 old_num_labels++;
1044 mutex_unlock(&nd_mapping->lock);
1047 * We need to preserve all the old labels for the mapping so
1048 * they can be garbage collected after writing the new labels.
1050 for (i = old_num_labels; i < num_labels; i++) {
1051 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
1052 if (!label_ent)
1053 return -ENOMEM;
1054 mutex_lock(&nd_mapping->lock);
1055 list_add_tail(&label_ent->list, &nd_mapping->labels);
1056 mutex_unlock(&nd_mapping->lock);
1059 if (ndd->ns_current == -1 || ndd->ns_next == -1)
1060 /* pass */;
1061 else
1062 return max(num_labels, old_num_labels);
1064 nsindex = to_namespace_index(ndd, 0);
1065 memset(nsindex, 0, ndd->nsarea.config_size);
1066 for (i = 0; i < 2; i++) {
1067 int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
1069 if (rc)
1070 return rc;
1072 ndd->ns_next = 1;
1073 ndd->ns_current = 0;
1075 return max(num_labels, old_num_labels);
1078 static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
1080 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1081 struct nd_label_ent *label_ent, *e;
1082 struct nd_namespace_index *nsindex;
1083 u8 label_uuid[NSLABEL_UUID_LEN];
1084 unsigned long *free;
1085 LIST_HEAD(list);
1086 u32 nslot, slot;
1087 int active = 0;
1089 if (!uuid)
1090 return 0;
1092 /* no index || no labels == nothing to delete */
1093 if (!preamble_next(ndd, &nsindex, &free, &nslot))
1094 return 0;
1096 mutex_lock(&nd_mapping->lock);
1097 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1098 struct nd_namespace_label *nd_label = label_ent->label;
1100 if (!nd_label)
1101 continue;
1102 active++;
1103 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1104 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
1105 continue;
1106 active--;
1107 slot = to_slot(ndd, nd_label);
1108 nd_label_free_slot(ndd, slot);
1109 dev_dbg(ndd->dev, "free: %d\n", slot);
1110 list_move_tail(&label_ent->list, &list);
1111 label_ent->label = NULL;
1113 list_splice_tail_init(&list, &nd_mapping->labels);
1115 if (active == 0) {
1116 nd_mapping_free_labels(nd_mapping);
1117 dev_dbg(ndd->dev, "no more active labels\n");
1119 mutex_unlock(&nd_mapping->lock);
1121 return nd_label_write_index(ndd, ndd->ns_next,
1122 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1125 int nd_pmem_namespace_label_update(struct nd_region *nd_region,
1126 struct nd_namespace_pmem *nspm, resource_size_t size)
1128 int i, rc;
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 int count = 0;
1136 if (size == 0) {
1137 rc = del_labels(nd_mapping, nspm->uuid);
1138 if (rc)
1139 return rc;
1140 continue;
1143 for_each_dpa_resource(ndd, res)
1144 if (strncmp(res->name, "pmem", 4) == 0)
1145 count++;
1146 WARN_ON_ONCE(!count);
1148 rc = init_labels(nd_mapping, count);
1149 if (rc < 0)
1150 return rc;
1152 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
1153 NSLABEL_FLAG_UPDATING);
1154 if (rc)
1155 return rc;
1158 if (size == 0)
1159 return 0;
1161 /* Clear the UPDATING flag per UEFI 2.7 expectations */
1162 for (i = 0; i < nd_region->ndr_mappings; i++) {
1163 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1165 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
1166 if (rc)
1167 return rc;
1170 return 0;
1173 int nd_blk_namespace_label_update(struct nd_region *nd_region,
1174 struct nd_namespace_blk *nsblk, resource_size_t size)
1176 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1177 struct resource *res;
1178 int count = 0;
1180 if (size == 0)
1181 return del_labels(nd_mapping, nsblk->uuid);
1183 for_each_dpa_resource(to_ndd(nd_mapping), res)
1184 count++;
1186 count = init_labels(nd_mapping, count);
1187 if (count < 0)
1188 return count;
1190 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
1193 int __init nd_label_init(void)
1195 WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
1196 WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
1197 WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
1198 WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
1200 return 0;