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>
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 u32 best_seq(u32 a, u32 b)
30 a &= NSINDEX_SEQ_MASK;
31 b &= NSINDEX_SEQ_MASK;
37 else if (nd_inc_seq(a) == b)
43 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
45 return ndd->nslabel_size;
48 static size_t __sizeof_namespace_index(u32 nslot)
50 return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8),
54 static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd,
57 return (ndd->nsarea.config_size - index_size * 2) /
58 sizeof_namespace_label(ndd);
61 int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
65 tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd);
66 n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN;
68 return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n);
71 size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
73 u32 nslot, space, size;
76 * Per UEFI 2.7, the minimum size of the Label Storage Area is large
77 * enough to hold 2 index blocks and 2 labels. The minimum index
78 * block size is 256 bytes. The label size is 128 for namespaces
79 * prior to version 1.2 and at minimum 256 for version 1.2 and later.
81 nslot = nvdimm_num_label_slots(ndd);
82 space = ndd->nsarea.config_size - nslot * sizeof_namespace_label(ndd);
83 size = __sizeof_namespace_index(nslot) * 2;
84 if (size <= space && nslot >= 2)
87 dev_err(ndd->dev, "label area (%d) too small to host (%d byte) labels\n",
88 ndd->nsarea.config_size, sizeof_namespace_label(ndd));
92 static int __nd_label_validate(struct nvdimm_drvdata *ndd)
95 * On media label format consists of two index blocks followed
96 * by an array of labels. None of these structures are ever
97 * updated in place. A sequence number tracks the current
98 * active index and the next one to write, while labels are
99 * written to free slots.
121 struct nd_namespace_index *nsindex[] = {
122 to_namespace_index(ndd, 0),
123 to_namespace_index(ndd, 1),
125 const int num_index = ARRAY_SIZE(nsindex);
126 struct device *dev = ndd->dev;
127 bool valid[2] = { 0 };
128 int i, num_valid = 0;
131 for (i = 0; i < num_index; i++) {
133 u8 sig[NSINDEX_SIG_LEN];
134 u64 sum_save, sum, size;
135 unsigned int version, labelsize;
137 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
138 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
139 dev_dbg(dev, "nsindex%d signature invalid\n", i);
143 /* label sizes larger than 128 arrived with v1.2 */
144 version = __le16_to_cpu(nsindex[i]->major) * 100
145 + __le16_to_cpu(nsindex[i]->minor);
147 labelsize = 1 << (7 + nsindex[i]->labelsize);
151 if (labelsize != sizeof_namespace_label(ndd)) {
152 dev_dbg(dev, "nsindex%d labelsize %d invalid\n",
153 i, nsindex[i]->labelsize);
157 sum_save = __le64_to_cpu(nsindex[i]->checksum);
158 nsindex[i]->checksum = __cpu_to_le64(0);
159 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
160 nsindex[i]->checksum = __cpu_to_le64(sum_save);
161 if (sum != sum_save) {
162 dev_dbg(dev, "nsindex%d checksum invalid\n", i);
166 seq = __le32_to_cpu(nsindex[i]->seq);
167 if ((seq & NSINDEX_SEQ_MASK) == 0) {
168 dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq);
172 /* sanity check the index against expected values */
173 if (__le64_to_cpu(nsindex[i]->myoff)
174 != i * sizeof_namespace_index(ndd)) {
175 dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n",
176 i, (unsigned long long)
177 __le64_to_cpu(nsindex[i]->myoff));
180 if (__le64_to_cpu(nsindex[i]->otheroff)
181 != (!i) * sizeof_namespace_index(ndd)) {
182 dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n",
183 i, (unsigned long long)
184 __le64_to_cpu(nsindex[i]->otheroff));
187 if (__le64_to_cpu(nsindex[i]->labeloff)
188 != 2 * sizeof_namespace_index(ndd)) {
189 dev_dbg(dev, "nsindex%d labeloff: %#llx invalid\n",
190 i, (unsigned long long)
191 __le64_to_cpu(nsindex[i]->labeloff));
195 size = __le64_to_cpu(nsindex[i]->mysize);
196 if (size > sizeof_namespace_index(ndd)
197 || size < sizeof(struct nd_namespace_index)) {
198 dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size);
202 nslot = __le32_to_cpu(nsindex[i]->nslot);
203 if (nslot * sizeof_namespace_label(ndd)
204 + 2 * sizeof_namespace_index(ndd)
205 > ndd->nsarea.config_size) {
206 dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n",
207 i, nslot, ndd->nsarea.config_size);
218 for (i = 0; i < num_index; i++)
221 /* can't have num_valid > 0 but valid[] = { false, false } */
225 /* pick the best index... */
226 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
227 __le32_to_cpu(nsindex[1]->seq));
228 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
238 static int nd_label_validate(struct nvdimm_drvdata *ndd)
241 * In order to probe for and validate namespace index blocks we
242 * need to know the size of the labels, and we can't trust the
243 * size of the labels until we validate the index blocks.
244 * Resolve this dependency loop by probing for known label
245 * sizes, but default to v1.2 256-byte namespace labels if
248 int label_size[] = { 128, 256 };
251 for (i = 0; i < ARRAY_SIZE(label_size); i++) {
252 ndd->nslabel_size = label_size[i];
253 rc = __nd_label_validate(ndd);
261 static void nd_label_copy(struct nvdimm_drvdata *ndd,
262 struct nd_namespace_index *dst,
263 struct nd_namespace_index *src)
265 /* just exit if either destination or source is NULL */
269 memcpy(dst, src, sizeof_namespace_index(ndd));
272 static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
274 void *base = to_namespace_index(ndd, 0);
276 return base + 2 * sizeof_namespace_index(ndd);
279 static int to_slot(struct nvdimm_drvdata *ndd,
280 struct nd_namespace_label *nd_label)
282 unsigned long label, base;
284 label = (unsigned long) nd_label;
285 base = (unsigned long) nd_label_base(ndd);
287 return (label - base) / sizeof_namespace_label(ndd);
290 static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
292 unsigned long label, base;
294 base = (unsigned long) nd_label_base(ndd);
295 label = base + sizeof_namespace_label(ndd) * slot;
297 return (struct nd_namespace_label *) label;
300 #define for_each_clear_bit_le(bit, addr, size) \
301 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
303 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
306 * preamble_index - common variable initialization for nd_label_* routines
307 * @ndd: dimm container for the relevant label set
308 * @idx: namespace_index index
309 * @nsindex_out: on return set to the currently active namespace index
310 * @free: on return set to the free label bitmap in the index
311 * @nslot: on return set to the number of slots in the label space
313 static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
314 struct nd_namespace_index **nsindex_out,
315 unsigned long **free, u32 *nslot)
317 struct nd_namespace_index *nsindex;
319 nsindex = to_namespace_index(ndd, idx);
323 *free = (unsigned long *) nsindex->free;
324 *nslot = __le32_to_cpu(nsindex->nslot);
325 *nsindex_out = nsindex;
330 char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
332 if (!label_id || !uuid)
334 snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
335 flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
339 static bool preamble_current(struct nvdimm_drvdata *ndd,
340 struct nd_namespace_index **nsindex,
341 unsigned long **free, u32 *nslot)
343 return preamble_index(ndd, ndd->ns_current, nsindex,
347 static bool preamble_next(struct nvdimm_drvdata *ndd,
348 struct nd_namespace_index **nsindex,
349 unsigned long **free, u32 *nslot)
351 return preamble_index(ndd, ndd->ns_next, nsindex,
355 static bool slot_valid(struct nvdimm_drvdata *ndd,
356 struct nd_namespace_label *nd_label, u32 slot)
358 /* check that we are written where we expect to be written */
359 if (slot != __le32_to_cpu(nd_label->slot))
362 /* check that DPA allocations are page aligned */
363 if ((__le64_to_cpu(nd_label->dpa)
364 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
368 if (namespace_label_has(ndd, checksum)) {
371 sum_save = __le64_to_cpu(nd_label->checksum);
372 nd_label->checksum = __cpu_to_le64(0);
373 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
374 nd_label->checksum = __cpu_to_le64(sum_save);
375 if (sum != sum_save) {
376 dev_dbg(ndd->dev, "fail checksum. slot: %d expect: %#llx\n",
385 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
387 struct nd_namespace_index *nsindex;
391 if (!preamble_current(ndd, &nsindex, &free, &nslot))
392 return 0; /* no label, nothing to reserve */
394 for_each_clear_bit_le(slot, free, nslot) {
395 struct nvdimm *nvdimm = to_nvdimm(ndd->dev);
396 struct nd_namespace_label *nd_label;
397 struct nd_region *nd_region = NULL;
398 u8 label_uuid[NSLABEL_UUID_LEN];
399 struct nd_label_id label_id;
400 struct resource *res;
403 nd_label = to_label(ndd, slot);
405 if (!slot_valid(ndd, nd_label, slot))
408 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
409 flags = __le32_to_cpu(nd_label->flags);
410 if (test_bit(NDD_NOBLK, &nvdimm->flags))
411 flags &= ~NSLABEL_FLAG_LOCAL;
412 nd_label_gen_id(&label_id, label_uuid, flags);
413 res = nvdimm_allocate_dpa(ndd, &label_id,
414 __le64_to_cpu(nd_label->dpa),
415 __le64_to_cpu(nd_label->rawsize));
416 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
424 int nd_label_data_init(struct nvdimm_drvdata *ndd)
426 size_t config_size, read_size, max_xfer, offset;
427 struct nd_namespace_index *nsindex;
435 if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0) {
436 dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n",
437 ndd->nsarea.max_xfer, ndd->nsarea.config_size);
442 * We need to determine the maximum index area as this is the section
443 * we must read and validate before we can start processing labels.
445 * If the area is too small to contain the two indexes and 2 labels
448 * Start at a label size of 128 as this should result in the largest
449 * possible namespace index size.
451 ndd->nslabel_size = 128;
452 read_size = sizeof_namespace_index(ndd) * 2;
456 /* Allocate config data */
457 config_size = ndd->nsarea.config_size;
458 ndd->data = kvzalloc(config_size, GFP_KERNEL);
463 * We want to guarantee as few reads as possible while conserving
464 * memory. To do that we figure out how much unused space will be left
465 * in the last read, divide that by the total number of reads it is
466 * going to take given our maximum transfer size, and then reduce our
467 * maximum transfer size based on that result.
469 max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size);
470 if (read_size < max_xfer) {
472 max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) /
473 DIV_ROUND_UP(config_size, max_xfer);
474 /* make certain we read indexes in exactly 1 read */
475 if (max_xfer < read_size)
476 max_xfer = read_size;
479 /* Make our initial read size a multiple of max_xfer size */
480 read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer,
483 /* Read the index data */
484 rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size);
488 /* Validate index data, if not valid assume all labels are invalid */
489 ndd->ns_current = nd_label_validate(ndd);
490 if (ndd->ns_current < 0)
493 /* Record our index values */
494 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
496 /* Copy "current" index on top of the "next" index */
497 nsindex = to_current_namespace_index(ndd);
498 nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex);
500 /* Determine starting offset for label data */
501 offset = __le64_to_cpu(nsindex->labeloff);
502 nslot = __le32_to_cpu(nsindex->nslot);
504 /* Loop through the free list pulling in any active labels */
505 for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) {
506 size_t label_read_size;
508 /* zero out the unused labels */
509 if (test_bit_le(i, nsindex->free)) {
510 memset(ndd->data + offset, 0, ndd->nslabel_size);
514 /* if we already read past here then just continue */
515 if (offset + ndd->nslabel_size <= read_size)
518 /* if we haven't read in a while reset our read_size offset */
519 if (read_size < offset)
522 /* determine how much more will be read after this next call. */
523 label_read_size = offset + ndd->nslabel_size - read_size;
524 label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) *
527 /* truncate last read if needed */
528 if (read_size + label_read_size > config_size)
529 label_read_size = config_size - read_size;
531 /* Read the label data */
532 rc = nvdimm_get_config_data(ndd, ndd->data + read_size,
533 read_size, label_read_size);
537 /* push read_size to next read offset */
538 read_size += label_read_size;
541 dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc);
546 int nd_label_active_count(struct nvdimm_drvdata *ndd)
548 struct nd_namespace_index *nsindex;
553 if (!preamble_current(ndd, &nsindex, &free, &nslot))
556 for_each_clear_bit_le(slot, free, nslot) {
557 struct nd_namespace_label *nd_label;
559 nd_label = to_label(ndd, slot);
561 if (!slot_valid(ndd, nd_label, slot)) {
562 u32 label_slot = __le32_to_cpu(nd_label->slot);
563 u64 size = __le64_to_cpu(nd_label->rawsize);
564 u64 dpa = __le64_to_cpu(nd_label->dpa);
567 "slot%d invalid slot: %d dpa: %llx size: %llx\n",
568 slot, label_slot, dpa, size);
576 struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
578 struct nd_namespace_index *nsindex;
582 if (!preamble_current(ndd, &nsindex, &free, &nslot))
585 for_each_clear_bit_le(slot, free, nslot) {
586 struct nd_namespace_label *nd_label;
588 nd_label = to_label(ndd, slot);
589 if (!slot_valid(ndd, nd_label, slot))
593 return to_label(ndd, slot);
599 u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
601 struct nd_namespace_index *nsindex;
605 if (!preamble_next(ndd, &nsindex, &free, &nslot))
608 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
610 slot = find_next_bit_le(free, nslot, 0);
614 clear_bit_le(slot, free);
619 bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
621 struct nd_namespace_index *nsindex;
625 if (!preamble_next(ndd, &nsindex, &free, &nslot))
628 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
631 return !test_and_set_bit_le(slot, free);
635 u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
637 struct nd_namespace_index *nsindex;
641 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
643 if (!preamble_next(ndd, &nsindex, &free, &nslot))
644 return nvdimm_num_label_slots(ndd);
646 return bitmap_weight(free, nslot);
649 static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
652 struct nd_namespace_index *nsindex;
653 unsigned long offset;
658 nsindex = to_namespace_index(ndd, index);
659 if (flags & ND_NSINDEX_INIT)
660 nslot = nvdimm_num_label_slots(ndd);
662 nslot = __le32_to_cpu(nsindex->nslot);
664 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
665 memset(&nsindex->flags, 0, 3);
666 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
667 nsindex->seq = __cpu_to_le32(seq);
668 offset = (unsigned long) nsindex
669 - (unsigned long) to_namespace_index(ndd, 0);
670 nsindex->myoff = __cpu_to_le64(offset);
671 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
672 offset = (unsigned long) to_namespace_index(ndd,
673 nd_label_next_nsindex(index))
674 - (unsigned long) to_namespace_index(ndd, 0);
675 nsindex->otheroff = __cpu_to_le64(offset);
676 offset = (unsigned long) nd_label_base(ndd)
677 - (unsigned long) to_namespace_index(ndd, 0);
678 nsindex->labeloff = __cpu_to_le64(offset);
679 nsindex->nslot = __cpu_to_le32(nslot);
680 nsindex->major = __cpu_to_le16(1);
681 if (sizeof_namespace_label(ndd) < 256)
682 nsindex->minor = __cpu_to_le16(1);
684 nsindex->minor = __cpu_to_le16(2);
685 nsindex->checksum = __cpu_to_le64(0);
686 if (flags & ND_NSINDEX_INIT) {
687 unsigned long *free = (unsigned long *) nsindex->free;
688 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
691 memset(nsindex->free, 0xff, nfree / 8);
692 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
693 clear_bit_le(nslot + i, free);
695 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
696 nsindex->checksum = __cpu_to_le64(checksum);
697 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
698 nsindex, sizeof_namespace_index(ndd));
702 if (flags & ND_NSINDEX_INIT)
705 /* copy the index we just wrote to the new 'next' */
706 WARN_ON(index != ndd->ns_next);
707 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
708 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
709 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
710 WARN_ON(ndd->ns_current == ndd->ns_next);
715 static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
716 struct nd_namespace_label *nd_label)
718 return (unsigned long) nd_label
719 - (unsigned long) to_namespace_index(ndd, 0);
722 enum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid)
724 if (guid_equal(guid, &nvdimm_btt_guid))
725 return NVDIMM_CCLASS_BTT;
726 else if (guid_equal(guid, &nvdimm_btt2_guid))
727 return NVDIMM_CCLASS_BTT2;
728 else if (guid_equal(guid, &nvdimm_pfn_guid))
729 return NVDIMM_CCLASS_PFN;
730 else if (guid_equal(guid, &nvdimm_dax_guid))
731 return NVDIMM_CCLASS_DAX;
732 else if (guid_equal(guid, &guid_null))
733 return NVDIMM_CCLASS_NONE;
735 return NVDIMM_CCLASS_UNKNOWN;
738 static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
741 if (claim_class == NVDIMM_CCLASS_BTT)
742 return &nvdimm_btt_guid;
743 else if (claim_class == NVDIMM_CCLASS_BTT2)
744 return &nvdimm_btt2_guid;
745 else if (claim_class == NVDIMM_CCLASS_PFN)
746 return &nvdimm_pfn_guid;
747 else if (claim_class == NVDIMM_CCLASS_DAX)
748 return &nvdimm_dax_guid;
749 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
751 * If we're modifying a namespace for which we don't
752 * know the claim_class, don't touch the existing guid.
759 static void reap_victim(struct nd_mapping *nd_mapping,
760 struct nd_label_ent *victim)
762 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
763 u32 slot = to_slot(ndd, victim->label);
765 dev_dbg(ndd->dev, "free: %d\n", slot);
766 nd_label_free_slot(ndd, slot);
767 victim->label = NULL;
770 static int __pmem_label_update(struct nd_region *nd_region,
771 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
772 int pos, unsigned long flags)
774 struct nd_namespace_common *ndns = &nspm->nsio.common;
775 struct nd_interleave_set *nd_set = nd_region->nd_set;
776 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
777 struct nd_namespace_label *nd_label;
778 struct nd_namespace_index *nsindex;
779 struct nd_label_ent *label_ent;
780 struct nd_label_id label_id;
781 struct resource *res;
788 if (!preamble_next(ndd, &nsindex, &free, &nslot))
791 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
792 nd_label_gen_id(&label_id, nspm->uuid, 0);
793 for_each_dpa_resource(ndd, res)
794 if (strcmp(res->name, label_id.id) == 0)
802 /* allocate and write the label to the staging (next) index */
803 slot = nd_label_alloc_slot(ndd);
804 if (slot == UINT_MAX)
806 dev_dbg(ndd->dev, "allocated: %d\n", slot);
808 nd_label = to_label(ndd, slot);
809 memset(nd_label, 0, sizeof_namespace_label(ndd));
810 memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
812 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
813 nd_label->flags = __cpu_to_le32(flags);
814 nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
815 nd_label->position = __cpu_to_le16(pos);
816 nd_label->isetcookie = __cpu_to_le64(cookie);
817 nd_label->rawsize = __cpu_to_le64(resource_size(res));
818 nd_label->lbasize = __cpu_to_le64(nspm->lbasize);
819 nd_label->dpa = __cpu_to_le64(res->start);
820 nd_label->slot = __cpu_to_le32(slot);
821 if (namespace_label_has(ndd, type_guid))
822 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
823 if (namespace_label_has(ndd, abstraction_guid))
824 guid_copy(&nd_label->abstraction_guid,
825 to_abstraction_guid(ndns->claim_class,
826 &nd_label->abstraction_guid));
827 if (namespace_label_has(ndd, checksum)) {
830 nd_label->checksum = __cpu_to_le64(0);
831 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
832 nd_label->checksum = __cpu_to_le64(sum);
834 nd_dbg_dpa(nd_region, ndd, res, "\n");
837 offset = nd_label_offset(ndd, nd_label);
838 rc = nvdimm_set_config_data(ndd, offset, nd_label,
839 sizeof_namespace_label(ndd));
843 /* Garbage collect the previous label */
844 mutex_lock(&nd_mapping->lock);
845 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
846 if (!label_ent->label)
848 if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags)
849 || memcmp(nspm->uuid, label_ent->label->uuid,
850 NSLABEL_UUID_LEN) == 0)
851 reap_victim(nd_mapping, label_ent);
855 rc = nd_label_write_index(ndd, ndd->ns_next,
856 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
858 list_for_each_entry(label_ent, &nd_mapping->labels, list)
859 if (!label_ent->label) {
860 label_ent->label = nd_label;
864 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
865 "failed to track label: %d\n",
866 to_slot(ndd, nd_label));
870 mutex_unlock(&nd_mapping->lock);
875 static bool is_old_resource(struct resource *res, struct resource **list, int n)
879 if (res->flags & DPA_RESOURCE_ADJUSTED)
881 for (i = 0; i < n; i++)
887 static struct resource *to_resource(struct nvdimm_drvdata *ndd,
888 struct nd_namespace_label *nd_label)
890 struct resource *res;
892 for_each_dpa_resource(ndd, res) {
893 if (res->start != __le64_to_cpu(nd_label->dpa))
895 if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
904 * 1/ Account all the labels that can be freed after this update
905 * 2/ Allocate and write the label to the staging (next) index
906 * 3/ Record the resources in the namespace device
908 static int __blk_label_update(struct nd_region *nd_region,
909 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
912 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
913 struct nd_interleave_set *nd_set = nd_region->nd_set;
914 struct nd_namespace_common *ndns = &nsblk->common;
915 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
916 struct nd_namespace_label *nd_label;
917 struct nd_label_ent *label_ent, *e;
918 struct nd_namespace_index *nsindex;
919 unsigned long *free, *victim_map = NULL;
920 struct resource *res, **old_res_list;
921 struct nd_label_id label_id;
922 u8 uuid[NSLABEL_UUID_LEN];
927 if (!preamble_next(ndd, &nsindex, &free, &nslot))
930 old_res_list = nsblk->res;
931 nfree = nd_label_nfree(ndd);
932 old_num_resources = nsblk->num_resources;
933 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
936 * We need to loop over the old resources a few times, which seems a
937 * bit inefficient, but we need to know that we have the label
938 * space before we start mutating the tracking structures.
939 * Otherwise the recovery method of last resort for userspace is
940 * disable and re-enable the parent region.
943 for_each_dpa_resource(ndd, res) {
944 if (strcmp(res->name, label_id.id) != 0)
946 if (!is_old_resource(res, old_res_list, old_num_resources))
951 if (old_num_resources) {
952 /* convert old local-label-map to dimm-slot victim-map */
953 victim_map = bitmap_zalloc(nslot, GFP_KERNEL);
957 /* mark unused labels for garbage collection */
958 for_each_clear_bit_le(slot, free, nslot) {
959 nd_label = to_label(ndd, slot);
960 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
961 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
963 res = to_resource(ndd, nd_label);
964 if (res && is_old_resource(res, old_res_list,
967 slot = to_slot(ndd, nd_label);
968 set_bit(slot, victim_map);
973 /* don't allow updates that consume the last label */
974 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
975 dev_info(&nsblk->common.dev, "insufficient label space\n");
976 bitmap_free(victim_map);
979 /* from here on we need to abort on error */
982 /* assign all resources to the namespace before writing the labels */
984 nsblk->num_resources = 0;
985 for_each_dpa_resource(ndd, res) {
986 if (strcmp(res->name, label_id.id) != 0)
988 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
995 * Find the resource associated with the first label in the set
996 * per the v1.2 namespace specification.
998 for (i = 0; i < nsblk->num_resources; i++) {
999 struct resource *min = nsblk->res[min_dpa_idx];
1001 res = nsblk->res[i];
1002 if (res->start < min->start)
1006 for (i = 0; i < nsblk->num_resources; i++) {
1009 res = nsblk->res[i];
1010 if (is_old_resource(res, old_res_list, old_num_resources))
1011 continue; /* carry-over */
1012 slot = nd_label_alloc_slot(ndd);
1013 if (slot == UINT_MAX)
1015 dev_dbg(ndd->dev, "allocated: %d\n", slot);
1017 nd_label = to_label(ndd, slot);
1018 memset(nd_label, 0, sizeof_namespace_label(ndd));
1019 memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
1020 if (nsblk->alt_name)
1021 memcpy(nd_label->name, nsblk->alt_name,
1023 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
1026 * Use the presence of the type_guid as a flag to
1027 * determine isetcookie usage and nlabel + position
1028 * policy for blk-aperture namespaces.
1030 if (namespace_label_has(ndd, type_guid)) {
1031 if (i == min_dpa_idx) {
1032 nd_label->nlabel = __cpu_to_le16(nsblk->num_resources);
1033 nd_label->position = __cpu_to_le16(0);
1035 nd_label->nlabel = __cpu_to_le16(0xffff);
1036 nd_label->position = __cpu_to_le16(0xffff);
1038 nd_label->isetcookie = __cpu_to_le64(nd_set->cookie2);
1040 nd_label->nlabel = __cpu_to_le16(0); /* N/A */
1041 nd_label->position = __cpu_to_le16(0); /* N/A */
1042 nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
1045 nd_label->dpa = __cpu_to_le64(res->start);
1046 nd_label->rawsize = __cpu_to_le64(resource_size(res));
1047 nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
1048 nd_label->slot = __cpu_to_le32(slot);
1049 if (namespace_label_has(ndd, type_guid))
1050 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
1051 if (namespace_label_has(ndd, abstraction_guid))
1052 guid_copy(&nd_label->abstraction_guid,
1053 to_abstraction_guid(ndns->claim_class,
1054 &nd_label->abstraction_guid));
1056 if (namespace_label_has(ndd, checksum)) {
1059 nd_label->checksum = __cpu_to_le64(0);
1060 sum = nd_fletcher64(nd_label,
1061 sizeof_namespace_label(ndd), 1);
1062 nd_label->checksum = __cpu_to_le64(sum);
1066 offset = nd_label_offset(ndd, nd_label);
1067 rc = nvdimm_set_config_data(ndd, offset, nd_label,
1068 sizeof_namespace_label(ndd));
1073 /* free up now unused slots in the new index */
1074 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
1075 dev_dbg(ndd->dev, "free: %d\n", slot);
1076 nd_label_free_slot(ndd, slot);
1080 rc = nd_label_write_index(ndd, ndd->ns_next,
1081 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1086 * Now that the on-dimm labels are up to date, fix up the tracking
1087 * entries in nd_mapping->labels
1090 mutex_lock(&nd_mapping->lock);
1091 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1092 nd_label = label_ent->label;
1096 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1097 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
1100 list_move(&label_ent->list, &list);
1101 label_ent->label = NULL;
1103 list_splice_tail_init(&list, &nd_mapping->labels);
1104 mutex_unlock(&nd_mapping->lock);
1106 if (nlabel + nsblk->num_resources > num_labels) {
1108 * Bug, we can't end up with more resources than
1116 mutex_lock(&nd_mapping->lock);
1117 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1118 typeof(*label_ent), list);
1121 mutex_unlock(&nd_mapping->lock);
1125 for_each_clear_bit_le(slot, free, nslot) {
1126 nd_label = to_label(ndd, slot);
1127 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1128 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
1130 res = to_resource(ndd, nd_label);
1131 res->flags &= ~DPA_RESOURCE_ADJUSTED;
1132 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
1133 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
1134 if (label_ent->label)
1136 label_ent->label = nd_label;
1141 dev_WARN(&nsblk->common.dev,
1142 "failed to track label slot%d\n", slot);
1144 mutex_unlock(&nd_mapping->lock);
1147 kfree(old_res_list);
1148 bitmap_free(victim_map);
1153 * 1/ repair the allocated label bitmap in the index
1154 * 2/ restore the resource list
1156 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
1158 nsblk->res = old_res_list;
1159 nsblk->num_resources = old_num_resources;
1160 old_res_list = NULL;
1164 static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
1166 int i, old_num_labels = 0;
1167 struct nd_label_ent *label_ent;
1168 struct nd_namespace_index *nsindex;
1169 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1171 mutex_lock(&nd_mapping->lock);
1172 list_for_each_entry(label_ent, &nd_mapping->labels, list)
1174 mutex_unlock(&nd_mapping->lock);
1177 * We need to preserve all the old labels for the mapping so
1178 * they can be garbage collected after writing the new labels.
1180 for (i = old_num_labels; i < num_labels; i++) {
1181 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
1184 mutex_lock(&nd_mapping->lock);
1185 list_add_tail(&label_ent->list, &nd_mapping->labels);
1186 mutex_unlock(&nd_mapping->lock);
1189 if (ndd->ns_current == -1 || ndd->ns_next == -1)
1192 return max(num_labels, old_num_labels);
1194 nsindex = to_namespace_index(ndd, 0);
1195 memset(nsindex, 0, ndd->nsarea.config_size);
1196 for (i = 0; i < 2; i++) {
1197 int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
1203 ndd->ns_current = 0;
1205 return max(num_labels, old_num_labels);
1208 static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
1210 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1211 struct nd_label_ent *label_ent, *e;
1212 struct nd_namespace_index *nsindex;
1213 u8 label_uuid[NSLABEL_UUID_LEN];
1214 unsigned long *free;
1222 /* no index || no labels == nothing to delete */
1223 if (!preamble_next(ndd, &nsindex, &free, &nslot))
1226 mutex_lock(&nd_mapping->lock);
1227 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1228 struct nd_namespace_label *nd_label = label_ent->label;
1233 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1234 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
1237 slot = to_slot(ndd, nd_label);
1238 nd_label_free_slot(ndd, slot);
1239 dev_dbg(ndd->dev, "free: %d\n", slot);
1240 list_move_tail(&label_ent->list, &list);
1241 label_ent->label = NULL;
1243 list_splice_tail_init(&list, &nd_mapping->labels);
1246 nd_mapping_free_labels(nd_mapping);
1247 dev_dbg(ndd->dev, "no more active labels\n");
1249 mutex_unlock(&nd_mapping->lock);
1251 return nd_label_write_index(ndd, ndd->ns_next,
1252 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1255 int nd_pmem_namespace_label_update(struct nd_region *nd_region,
1256 struct nd_namespace_pmem *nspm, resource_size_t size)
1260 for (i = 0; i < nd_region->ndr_mappings; i++) {
1261 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1262 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1263 struct resource *res;
1267 rc = del_labels(nd_mapping, nspm->uuid);
1273 for_each_dpa_resource(ndd, res)
1274 if (strncmp(res->name, "pmem", 4) == 0)
1276 WARN_ON_ONCE(!count);
1278 rc = init_labels(nd_mapping, count);
1282 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
1283 NSLABEL_FLAG_UPDATING);
1291 /* Clear the UPDATING flag per UEFI 2.7 expectations */
1292 for (i = 0; i < nd_region->ndr_mappings; i++) {
1293 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1295 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
1303 int nd_blk_namespace_label_update(struct nd_region *nd_region,
1304 struct nd_namespace_blk *nsblk, resource_size_t size)
1306 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1307 struct resource *res;
1311 return del_labels(nd_mapping, nsblk->uuid);
1313 for_each_dpa_resource(to_ndd(nd_mapping), res)
1316 count = init_labels(nd_mapping, count);
1320 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
1323 int __init nd_label_init(void)
1325 WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
1326 WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
1327 WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
1328 WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));