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 nd_namespace_label *nd_label;
396 struct nd_region *nd_region = NULL;
397 u8 label_uuid[NSLABEL_UUID_LEN];
398 struct nd_label_id label_id;
399 struct resource *res;
402 nd_label = to_label(ndd, slot);
404 if (!slot_valid(ndd, nd_label, slot))
407 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
408 flags = __le32_to_cpu(nd_label->flags);
409 nd_label_gen_id(&label_id, label_uuid, flags);
410 res = nvdimm_allocate_dpa(ndd, &label_id,
411 __le64_to_cpu(nd_label->dpa),
412 __le64_to_cpu(nd_label->rawsize));
413 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
421 int nd_label_data_init(struct nvdimm_drvdata *ndd)
423 size_t config_size, read_size, max_xfer, offset;
424 struct nd_namespace_index *nsindex;
432 if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0) {
433 dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n",
434 ndd->nsarea.max_xfer, ndd->nsarea.config_size);
439 * We need to determine the maximum index area as this is the section
440 * we must read and validate before we can start processing labels.
442 * If the area is too small to contain the two indexes and 2 labels
445 * Start at a label size of 128 as this should result in the largest
446 * possible namespace index size.
448 ndd->nslabel_size = 128;
449 read_size = sizeof_namespace_index(ndd) * 2;
453 /* Allocate config data */
454 config_size = ndd->nsarea.config_size;
455 ndd->data = kvzalloc(config_size, GFP_KERNEL);
460 * We want to guarantee as few reads as possible while conserving
461 * memory. To do that we figure out how much unused space will be left
462 * in the last read, divide that by the total number of reads it is
463 * going to take given our maximum transfer size, and then reduce our
464 * maximum transfer size based on that result.
466 max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size);
467 if (read_size < max_xfer) {
469 max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) /
470 DIV_ROUND_UP(config_size, max_xfer);
471 /* make certain we read indexes in exactly 1 read */
472 if (max_xfer < read_size)
473 max_xfer = read_size;
476 /* Make our initial read size a multiple of max_xfer size */
477 read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer,
480 /* Read the index data */
481 rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size);
485 /* Validate index data, if not valid assume all labels are invalid */
486 ndd->ns_current = nd_label_validate(ndd);
487 if (ndd->ns_current < 0)
490 /* Record our index values */
491 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
493 /* Copy "current" index on top of the "next" index */
494 nsindex = to_current_namespace_index(ndd);
495 nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex);
497 /* Determine starting offset for label data */
498 offset = __le64_to_cpu(nsindex->labeloff);
499 nslot = __le32_to_cpu(nsindex->nslot);
501 /* Loop through the free list pulling in any active labels */
502 for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) {
503 size_t label_read_size;
505 /* zero out the unused labels */
506 if (test_bit_le(i, nsindex->free)) {
507 memset(ndd->data + offset, 0, ndd->nslabel_size);
511 /* if we already read past here then just continue */
512 if (offset + ndd->nslabel_size <= read_size)
515 /* if we haven't read in a while reset our read_size offset */
516 if (read_size < offset)
519 /* determine how much more will be read after this next call. */
520 label_read_size = offset + ndd->nslabel_size - read_size;
521 label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) *
524 /* truncate last read if needed */
525 if (read_size + label_read_size > config_size)
526 label_read_size = config_size - read_size;
528 /* Read the label data */
529 rc = nvdimm_get_config_data(ndd, ndd->data + read_size,
530 read_size, label_read_size);
534 /* push read_size to next read offset */
535 read_size += label_read_size;
538 dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc);
543 int nd_label_active_count(struct nvdimm_drvdata *ndd)
545 struct nd_namespace_index *nsindex;
550 if (!preamble_current(ndd, &nsindex, &free, &nslot))
553 for_each_clear_bit_le(slot, free, nslot) {
554 struct nd_namespace_label *nd_label;
556 nd_label = to_label(ndd, slot);
558 if (!slot_valid(ndd, nd_label, slot)) {
559 u32 label_slot = __le32_to_cpu(nd_label->slot);
560 u64 size = __le64_to_cpu(nd_label->rawsize);
561 u64 dpa = __le64_to_cpu(nd_label->dpa);
564 "slot%d invalid slot: %d dpa: %llx size: %llx\n",
565 slot, label_slot, dpa, size);
573 struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
575 struct nd_namespace_index *nsindex;
579 if (!preamble_current(ndd, &nsindex, &free, &nslot))
582 for_each_clear_bit_le(slot, free, nslot) {
583 struct nd_namespace_label *nd_label;
585 nd_label = to_label(ndd, slot);
586 if (!slot_valid(ndd, nd_label, slot))
590 return to_label(ndd, slot);
596 u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
598 struct nd_namespace_index *nsindex;
602 if (!preamble_next(ndd, &nsindex, &free, &nslot))
605 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
607 slot = find_next_bit_le(free, nslot, 0);
611 clear_bit_le(slot, free);
616 bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
618 struct nd_namespace_index *nsindex;
622 if (!preamble_next(ndd, &nsindex, &free, &nslot))
625 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
628 return !test_and_set_bit_le(slot, free);
632 u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
634 struct nd_namespace_index *nsindex;
638 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
640 if (!preamble_next(ndd, &nsindex, &free, &nslot))
641 return nvdimm_num_label_slots(ndd);
643 return bitmap_weight(free, nslot);
646 static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
649 struct nd_namespace_index *nsindex;
650 unsigned long offset;
655 nsindex = to_namespace_index(ndd, index);
656 if (flags & ND_NSINDEX_INIT)
657 nslot = nvdimm_num_label_slots(ndd);
659 nslot = __le32_to_cpu(nsindex->nslot);
661 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
662 memset(&nsindex->flags, 0, 3);
663 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
664 nsindex->seq = __cpu_to_le32(seq);
665 offset = (unsigned long) nsindex
666 - (unsigned long) to_namespace_index(ndd, 0);
667 nsindex->myoff = __cpu_to_le64(offset);
668 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
669 offset = (unsigned long) to_namespace_index(ndd,
670 nd_label_next_nsindex(index))
671 - (unsigned long) to_namespace_index(ndd, 0);
672 nsindex->otheroff = __cpu_to_le64(offset);
673 offset = (unsigned long) nd_label_base(ndd)
674 - (unsigned long) to_namespace_index(ndd, 0);
675 nsindex->labeloff = __cpu_to_le64(offset);
676 nsindex->nslot = __cpu_to_le32(nslot);
677 nsindex->major = __cpu_to_le16(1);
678 if (sizeof_namespace_label(ndd) < 256)
679 nsindex->minor = __cpu_to_le16(1);
681 nsindex->minor = __cpu_to_le16(2);
682 nsindex->checksum = __cpu_to_le64(0);
683 if (flags & ND_NSINDEX_INIT) {
684 unsigned long *free = (unsigned long *) nsindex->free;
685 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
688 memset(nsindex->free, 0xff, nfree / 8);
689 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
690 clear_bit_le(nslot + i, free);
692 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
693 nsindex->checksum = __cpu_to_le64(checksum);
694 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
695 nsindex, sizeof_namespace_index(ndd));
699 if (flags & ND_NSINDEX_INIT)
702 /* copy the index we just wrote to the new 'next' */
703 WARN_ON(index != ndd->ns_next);
704 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
705 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
706 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
707 WARN_ON(ndd->ns_current == ndd->ns_next);
712 static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
713 struct nd_namespace_label *nd_label)
715 return (unsigned long) nd_label
716 - (unsigned long) to_namespace_index(ndd, 0);
719 enum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid)
721 if (guid_equal(guid, &nvdimm_btt_guid))
722 return NVDIMM_CCLASS_BTT;
723 else if (guid_equal(guid, &nvdimm_btt2_guid))
724 return NVDIMM_CCLASS_BTT2;
725 else if (guid_equal(guid, &nvdimm_pfn_guid))
726 return NVDIMM_CCLASS_PFN;
727 else if (guid_equal(guid, &nvdimm_dax_guid))
728 return NVDIMM_CCLASS_DAX;
729 else if (guid_equal(guid, &guid_null))
730 return NVDIMM_CCLASS_NONE;
732 return NVDIMM_CCLASS_UNKNOWN;
735 static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
738 if (claim_class == NVDIMM_CCLASS_BTT)
739 return &nvdimm_btt_guid;
740 else if (claim_class == NVDIMM_CCLASS_BTT2)
741 return &nvdimm_btt2_guid;
742 else if (claim_class == NVDIMM_CCLASS_PFN)
743 return &nvdimm_pfn_guid;
744 else if (claim_class == NVDIMM_CCLASS_DAX)
745 return &nvdimm_dax_guid;
746 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
748 * If we're modifying a namespace for which we don't
749 * know the claim_class, don't touch the existing guid.
756 static int __pmem_label_update(struct nd_region *nd_region,
757 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
760 struct nd_namespace_common *ndns = &nspm->nsio.common;
761 struct nd_interleave_set *nd_set = nd_region->nd_set;
762 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
763 struct nd_label_ent *label_ent, *victim = NULL;
764 struct nd_namespace_label *nd_label;
765 struct nd_namespace_index *nsindex;
766 struct nd_label_id label_id;
767 struct resource *res;
774 if (!preamble_next(ndd, &nsindex, &free, &nslot))
777 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
778 nd_label_gen_id(&label_id, nspm->uuid, 0);
779 for_each_dpa_resource(ndd, res)
780 if (strcmp(res->name, label_id.id) == 0)
788 /* allocate and write the label to the staging (next) index */
789 slot = nd_label_alloc_slot(ndd);
790 if (slot == UINT_MAX)
792 dev_dbg(ndd->dev, "allocated: %d\n", slot);
794 nd_label = to_label(ndd, slot);
795 memset(nd_label, 0, sizeof_namespace_label(ndd));
796 memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
798 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
799 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
800 nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
801 nd_label->position = __cpu_to_le16(pos);
802 nd_label->isetcookie = __cpu_to_le64(cookie);
803 nd_label->rawsize = __cpu_to_le64(resource_size(res));
804 nd_label->lbasize = __cpu_to_le64(nspm->lbasize);
805 nd_label->dpa = __cpu_to_le64(res->start);
806 nd_label->slot = __cpu_to_le32(slot);
807 if (namespace_label_has(ndd, type_guid))
808 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
809 if (namespace_label_has(ndd, abstraction_guid))
810 guid_copy(&nd_label->abstraction_guid,
811 to_abstraction_guid(ndns->claim_class,
812 &nd_label->abstraction_guid));
813 if (namespace_label_has(ndd, checksum)) {
816 nd_label->checksum = __cpu_to_le64(0);
817 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
818 nd_label->checksum = __cpu_to_le64(sum);
820 nd_dbg_dpa(nd_region, ndd, res, "\n");
823 offset = nd_label_offset(ndd, nd_label);
824 rc = nvdimm_set_config_data(ndd, offset, nd_label,
825 sizeof_namespace_label(ndd));
829 /* Garbage collect the previous label */
830 mutex_lock(&nd_mapping->lock);
831 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
832 if (!label_ent->label)
834 if (memcmp(nspm->uuid, label_ent->label->uuid,
835 NSLABEL_UUID_LEN) != 0)
838 list_move_tail(&victim->list, &nd_mapping->labels);
842 dev_dbg(ndd->dev, "free: %d\n", slot);
843 slot = to_slot(ndd, victim->label);
844 nd_label_free_slot(ndd, slot);
845 victim->label = NULL;
849 rc = nd_label_write_index(ndd, ndd->ns_next,
850 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
852 list_for_each_entry(label_ent, &nd_mapping->labels, list)
853 if (!label_ent->label) {
854 label_ent->label = nd_label;
858 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
859 "failed to track label: %d\n",
860 to_slot(ndd, nd_label));
864 mutex_unlock(&nd_mapping->lock);
869 static bool is_old_resource(struct resource *res, struct resource **list, int n)
873 if (res->flags & DPA_RESOURCE_ADJUSTED)
875 for (i = 0; i < n; i++)
881 static struct resource *to_resource(struct nvdimm_drvdata *ndd,
882 struct nd_namespace_label *nd_label)
884 struct resource *res;
886 for_each_dpa_resource(ndd, res) {
887 if (res->start != __le64_to_cpu(nd_label->dpa))
889 if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
898 * 1/ Account all the labels that can be freed after this update
899 * 2/ Allocate and write the label to the staging (next) index
900 * 3/ Record the resources in the namespace device
902 static int __blk_label_update(struct nd_region *nd_region,
903 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
906 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
907 struct nd_interleave_set *nd_set = nd_region->nd_set;
908 struct nd_namespace_common *ndns = &nsblk->common;
909 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
910 struct nd_namespace_label *nd_label;
911 struct nd_label_ent *label_ent, *e;
912 struct nd_namespace_index *nsindex;
913 unsigned long *free, *victim_map = NULL;
914 struct resource *res, **old_res_list;
915 struct nd_label_id label_id;
916 u8 uuid[NSLABEL_UUID_LEN];
921 if (!preamble_next(ndd, &nsindex, &free, &nslot))
924 old_res_list = nsblk->res;
925 nfree = nd_label_nfree(ndd);
926 old_num_resources = nsblk->num_resources;
927 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
930 * We need to loop over the old resources a few times, which seems a
931 * bit inefficient, but we need to know that we have the label
932 * space before we start mutating the tracking structures.
933 * Otherwise the recovery method of last resort for userspace is
934 * disable and re-enable the parent region.
937 for_each_dpa_resource(ndd, res) {
938 if (strcmp(res->name, label_id.id) != 0)
940 if (!is_old_resource(res, old_res_list, old_num_resources))
945 if (old_num_resources) {
946 /* convert old local-label-map to dimm-slot victim-map */
947 victim_map = kcalloc(BITS_TO_LONGS(nslot), sizeof(long),
952 /* mark unused labels for garbage collection */
953 for_each_clear_bit_le(slot, free, nslot) {
954 nd_label = to_label(ndd, slot);
955 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
956 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
958 res = to_resource(ndd, nd_label);
959 if (res && is_old_resource(res, old_res_list,
962 slot = to_slot(ndd, nd_label);
963 set_bit(slot, victim_map);
968 /* don't allow updates that consume the last label */
969 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
970 dev_info(&nsblk->common.dev, "insufficient label space\n");
974 /* from here on we need to abort on error */
977 /* assign all resources to the namespace before writing the labels */
979 nsblk->num_resources = 0;
980 for_each_dpa_resource(ndd, res) {
981 if (strcmp(res->name, label_id.id) != 0)
983 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
990 * Find the resource associated with the first label in the set
991 * per the v1.2 namespace specification.
993 for (i = 0; i < nsblk->num_resources; i++) {
994 struct resource *min = nsblk->res[min_dpa_idx];
997 if (res->start < min->start)
1001 for (i = 0; i < nsblk->num_resources; i++) {
1004 res = nsblk->res[i];
1005 if (is_old_resource(res, old_res_list, old_num_resources))
1006 continue; /* carry-over */
1007 slot = nd_label_alloc_slot(ndd);
1008 if (slot == UINT_MAX)
1010 dev_dbg(ndd->dev, "allocated: %d\n", slot);
1012 nd_label = to_label(ndd, slot);
1013 memset(nd_label, 0, sizeof_namespace_label(ndd));
1014 memcpy(nd_label->uuid, nsblk->uuid, NSLABEL_UUID_LEN);
1015 if (nsblk->alt_name)
1016 memcpy(nd_label->name, nsblk->alt_name,
1018 nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_LOCAL);
1021 * Use the presence of the type_guid as a flag to
1022 * determine isetcookie usage and nlabel + position
1023 * policy for blk-aperture namespaces.
1025 if (namespace_label_has(ndd, type_guid)) {
1026 if (i == min_dpa_idx) {
1027 nd_label->nlabel = __cpu_to_le16(nsblk->num_resources);
1028 nd_label->position = __cpu_to_le16(0);
1030 nd_label->nlabel = __cpu_to_le16(0xffff);
1031 nd_label->position = __cpu_to_le16(0xffff);
1033 nd_label->isetcookie = __cpu_to_le64(nd_set->cookie2);
1035 nd_label->nlabel = __cpu_to_le16(0); /* N/A */
1036 nd_label->position = __cpu_to_le16(0); /* N/A */
1037 nd_label->isetcookie = __cpu_to_le64(0); /* N/A */
1040 nd_label->dpa = __cpu_to_le64(res->start);
1041 nd_label->rawsize = __cpu_to_le64(resource_size(res));
1042 nd_label->lbasize = __cpu_to_le64(nsblk->lbasize);
1043 nd_label->slot = __cpu_to_le32(slot);
1044 if (namespace_label_has(ndd, type_guid))
1045 guid_copy(&nd_label->type_guid, &nd_set->type_guid);
1046 if (namespace_label_has(ndd, abstraction_guid))
1047 guid_copy(&nd_label->abstraction_guid,
1048 to_abstraction_guid(ndns->claim_class,
1049 &nd_label->abstraction_guid));
1051 if (namespace_label_has(ndd, checksum)) {
1054 nd_label->checksum = __cpu_to_le64(0);
1055 sum = nd_fletcher64(nd_label,
1056 sizeof_namespace_label(ndd), 1);
1057 nd_label->checksum = __cpu_to_le64(sum);
1061 offset = nd_label_offset(ndd, nd_label);
1062 rc = nvdimm_set_config_data(ndd, offset, nd_label,
1063 sizeof_namespace_label(ndd));
1068 /* free up now unused slots in the new index */
1069 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
1070 dev_dbg(ndd->dev, "free: %d\n", slot);
1071 nd_label_free_slot(ndd, slot);
1075 rc = nd_label_write_index(ndd, ndd->ns_next,
1076 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1081 * Now that the on-dimm labels are up to date, fix up the tracking
1082 * entries in nd_mapping->labels
1085 mutex_lock(&nd_mapping->lock);
1086 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1087 nd_label = label_ent->label;
1091 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1092 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
1095 list_move(&label_ent->list, &list);
1096 label_ent->label = NULL;
1098 list_splice_tail_init(&list, &nd_mapping->labels);
1099 mutex_unlock(&nd_mapping->lock);
1101 if (nlabel + nsblk->num_resources > num_labels) {
1103 * Bug, we can't end up with more resources than
1111 mutex_lock(&nd_mapping->lock);
1112 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1113 typeof(*label_ent), list);
1116 mutex_unlock(&nd_mapping->lock);
1120 for_each_clear_bit_le(slot, free, nslot) {
1121 nd_label = to_label(ndd, slot);
1122 memcpy(uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1123 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) != 0)
1125 res = to_resource(ndd, nd_label);
1126 res->flags &= ~DPA_RESOURCE_ADJUSTED;
1127 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
1128 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
1129 if (label_ent->label)
1131 label_ent->label = nd_label;
1136 dev_WARN(&nsblk->common.dev,
1137 "failed to track label slot%d\n", slot);
1139 mutex_unlock(&nd_mapping->lock);
1142 kfree(old_res_list);
1148 * 1/ repair the allocated label bitmap in the index
1149 * 2/ restore the resource list
1151 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
1153 nsblk->res = old_res_list;
1154 nsblk->num_resources = old_num_resources;
1155 old_res_list = NULL;
1159 static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
1161 int i, old_num_labels = 0;
1162 struct nd_label_ent *label_ent;
1163 struct nd_namespace_index *nsindex;
1164 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1166 mutex_lock(&nd_mapping->lock);
1167 list_for_each_entry(label_ent, &nd_mapping->labels, list)
1169 mutex_unlock(&nd_mapping->lock);
1172 * We need to preserve all the old labels for the mapping so
1173 * they can be garbage collected after writing the new labels.
1175 for (i = old_num_labels; i < num_labels; i++) {
1176 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
1179 mutex_lock(&nd_mapping->lock);
1180 list_add_tail(&label_ent->list, &nd_mapping->labels);
1181 mutex_unlock(&nd_mapping->lock);
1184 if (ndd->ns_current == -1 || ndd->ns_next == -1)
1187 return max(num_labels, old_num_labels);
1189 nsindex = to_namespace_index(ndd, 0);
1190 memset(nsindex, 0, ndd->nsarea.config_size);
1191 for (i = 0; i < 2; i++) {
1192 int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
1198 ndd->ns_current = 0;
1200 return max(num_labels, old_num_labels);
1203 static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
1205 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1206 struct nd_label_ent *label_ent, *e;
1207 struct nd_namespace_index *nsindex;
1208 u8 label_uuid[NSLABEL_UUID_LEN];
1209 unsigned long *free;
1217 /* no index || no labels == nothing to delete */
1218 if (!preamble_next(ndd, &nsindex, &free, &nslot))
1221 mutex_lock(&nd_mapping->lock);
1222 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1223 struct nd_namespace_label *nd_label = label_ent->label;
1228 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
1229 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
1232 slot = to_slot(ndd, nd_label);
1233 nd_label_free_slot(ndd, slot);
1234 dev_dbg(ndd->dev, "free: %d\n", slot);
1235 list_move_tail(&label_ent->list, &list);
1236 label_ent->label = NULL;
1238 list_splice_tail_init(&list, &nd_mapping->labels);
1241 nd_mapping_free_labels(nd_mapping);
1242 dev_dbg(ndd->dev, "no more active labels\n");
1244 mutex_unlock(&nd_mapping->lock);
1246 return nd_label_write_index(ndd, ndd->ns_next,
1247 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1250 int nd_pmem_namespace_label_update(struct nd_region *nd_region,
1251 struct nd_namespace_pmem *nspm, resource_size_t size)
1255 for (i = 0; i < nd_region->ndr_mappings; i++) {
1256 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1257 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1258 struct resource *res;
1262 rc = del_labels(nd_mapping, nspm->uuid);
1268 for_each_dpa_resource(ndd, res)
1269 if (strncmp(res->name, "pmem", 4) == 0)
1271 WARN_ON_ONCE(!count);
1273 rc = init_labels(nd_mapping, count);
1277 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
1285 int nd_blk_namespace_label_update(struct nd_region *nd_region,
1286 struct nd_namespace_blk *nsblk, resource_size_t size)
1288 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1289 struct resource *res;
1293 return del_labels(nd_mapping, nsblk->uuid);
1295 for_each_dpa_resource(to_ndd(nd_mapping), res)
1298 count = init_labels(nd_mapping, count);
1302 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
1305 int __init nd_label_init(void)
1307 WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
1308 WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
1309 WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
1310 WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));