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, and the minimum label size is 256 bytes.
80 nslot
= nvdimm_num_label_slots(ndd
);
81 space
= ndd
->nsarea
.config_size
- nslot
* sizeof_namespace_label(ndd
);
82 size
= __sizeof_namespace_index(nslot
) * 2;
83 if (size
<= space
&& nslot
>= 2)
86 dev_err(ndd
->dev
, "label area (%d) too small to host (%d byte) labels\n",
87 ndd
->nsarea
.config_size
, sizeof_namespace_label(ndd
));
91 static int __nd_label_validate(struct nvdimm_drvdata
*ndd
)
94 * On media label format consists of two index blocks followed
95 * by an array of labels. None of these structures are ever
96 * updated in place. A sequence number tracks the current
97 * active index and the next one to write, while labels are
98 * written to free slots.
120 struct nd_namespace_index
*nsindex
[] = {
121 to_namespace_index(ndd
, 0),
122 to_namespace_index(ndd
, 1),
124 const int num_index
= ARRAY_SIZE(nsindex
);
125 struct device
*dev
= ndd
->dev
;
126 bool valid
[2] = { 0 };
127 int i
, num_valid
= 0;
130 for (i
= 0; i
< num_index
; i
++) {
132 u8 sig
[NSINDEX_SIG_LEN
];
133 u64 sum_save
, sum
, size
;
134 unsigned int version
, labelsize
;
136 memcpy(sig
, nsindex
[i
]->sig
, NSINDEX_SIG_LEN
);
137 if (memcmp(sig
, NSINDEX_SIGNATURE
, NSINDEX_SIG_LEN
) != 0) {
138 dev_dbg(dev
, "nsindex%d signature invalid\n", i
);
142 /* label sizes larger than 128 arrived with v1.2 */
143 version
= __le16_to_cpu(nsindex
[i
]->major
) * 100
144 + __le16_to_cpu(nsindex
[i
]->minor
);
146 labelsize
= 1 << (7 + nsindex
[i
]->labelsize
);
150 if (labelsize
!= sizeof_namespace_label(ndd
)) {
151 dev_dbg(dev
, "nsindex%d labelsize %d invalid\n",
152 i
, nsindex
[i
]->labelsize
);
156 sum_save
= __le64_to_cpu(nsindex
[i
]->checksum
);
157 nsindex
[i
]->checksum
= __cpu_to_le64(0);
158 sum
= nd_fletcher64(nsindex
[i
], sizeof_namespace_index(ndd
), 1);
159 nsindex
[i
]->checksum
= __cpu_to_le64(sum_save
);
160 if (sum
!= sum_save
) {
161 dev_dbg(dev
, "nsindex%d checksum invalid\n", i
);
165 seq
= __le32_to_cpu(nsindex
[i
]->seq
);
166 if ((seq
& NSINDEX_SEQ_MASK
) == 0) {
167 dev_dbg(dev
, "nsindex%d sequence: %#x invalid\n", i
, seq
);
171 /* sanity check the index against expected values */
172 if (__le64_to_cpu(nsindex
[i
]->myoff
)
173 != i
* sizeof_namespace_index(ndd
)) {
174 dev_dbg(dev
, "nsindex%d myoff: %#llx invalid\n",
175 i
, (unsigned long long)
176 __le64_to_cpu(nsindex
[i
]->myoff
));
179 if (__le64_to_cpu(nsindex
[i
]->otheroff
)
180 != (!i
) * sizeof_namespace_index(ndd
)) {
181 dev_dbg(dev
, "nsindex%d otheroff: %#llx invalid\n",
182 i
, (unsigned long long)
183 __le64_to_cpu(nsindex
[i
]->otheroff
));
187 size
= __le64_to_cpu(nsindex
[i
]->mysize
);
188 if (size
> sizeof_namespace_index(ndd
)
189 || size
< sizeof(struct nd_namespace_index
)) {
190 dev_dbg(dev
, "nsindex%d mysize: %#llx invalid\n", i
, size
);
194 nslot
= __le32_to_cpu(nsindex
[i
]->nslot
);
195 if (nslot
* sizeof_namespace_label(ndd
)
196 + 2 * sizeof_namespace_index(ndd
)
197 > ndd
->nsarea
.config_size
) {
198 dev_dbg(dev
, "nsindex%d nslot: %u invalid, config_size: %#x\n",
199 i
, nslot
, ndd
->nsarea
.config_size
);
210 for (i
= 0; i
< num_index
; i
++)
213 /* can't have num_valid > 0 but valid[] = { false, false } */
217 /* pick the best index... */
218 seq
= best_seq(__le32_to_cpu(nsindex
[0]->seq
),
219 __le32_to_cpu(nsindex
[1]->seq
));
220 if (seq
== (__le32_to_cpu(nsindex
[1]->seq
) & NSINDEX_SEQ_MASK
))
230 int nd_label_validate(struct nvdimm_drvdata
*ndd
)
233 * In order to probe for and validate namespace index blocks we
234 * need to know the size of the labels, and we can't trust the
235 * size of the labels until we validate the index blocks.
236 * Resolve this dependency loop by probing for known label
237 * sizes, but default to v1.2 256-byte namespace labels if
240 int label_size
[] = { 128, 256 };
243 for (i
= 0; i
< ARRAY_SIZE(label_size
); i
++) {
244 ndd
->nslabel_size
= label_size
[i
];
245 rc
= __nd_label_validate(ndd
);
253 void nd_label_copy(struct nvdimm_drvdata
*ndd
, struct nd_namespace_index
*dst
,
254 struct nd_namespace_index
*src
)
261 memcpy(dst
, src
, sizeof_namespace_index(ndd
));
264 static struct nd_namespace_label
*nd_label_base(struct nvdimm_drvdata
*ndd
)
266 void *base
= to_namespace_index(ndd
, 0);
268 return base
+ 2 * sizeof_namespace_index(ndd
);
271 static int to_slot(struct nvdimm_drvdata
*ndd
,
272 struct nd_namespace_label
*nd_label
)
274 unsigned long label
, base
;
276 label
= (unsigned long) nd_label
;
277 base
= (unsigned long) nd_label_base(ndd
);
279 return (label
- base
) / sizeof_namespace_label(ndd
);
282 static struct nd_namespace_label
*to_label(struct nvdimm_drvdata
*ndd
, int slot
)
284 unsigned long label
, base
;
286 base
= (unsigned long) nd_label_base(ndd
);
287 label
= base
+ sizeof_namespace_label(ndd
) * slot
;
289 return (struct nd_namespace_label
*) label
;
292 #define for_each_clear_bit_le(bit, addr, size) \
293 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
295 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
298 * preamble_index - common variable initialization for nd_label_* routines
299 * @ndd: dimm container for the relevant label set
300 * @idx: namespace_index index
301 * @nsindex_out: on return set to the currently active namespace index
302 * @free: on return set to the free label bitmap in the index
303 * @nslot: on return set to the number of slots in the label space
305 static bool preamble_index(struct nvdimm_drvdata
*ndd
, int idx
,
306 struct nd_namespace_index
**nsindex_out
,
307 unsigned long **free
, u32
*nslot
)
309 struct nd_namespace_index
*nsindex
;
311 nsindex
= to_namespace_index(ndd
, idx
);
315 *free
= (unsigned long *) nsindex
->free
;
316 *nslot
= __le32_to_cpu(nsindex
->nslot
);
317 *nsindex_out
= nsindex
;
322 char *nd_label_gen_id(struct nd_label_id
*label_id
, u8
*uuid
, u32 flags
)
324 if (!label_id
|| !uuid
)
326 snprintf(label_id
->id
, ND_LABEL_ID_SIZE
, "%s-%pUb",
327 flags
& NSLABEL_FLAG_LOCAL
? "blk" : "pmem", uuid
);
331 static bool preamble_current(struct nvdimm_drvdata
*ndd
,
332 struct nd_namespace_index
**nsindex
,
333 unsigned long **free
, u32
*nslot
)
335 return preamble_index(ndd
, ndd
->ns_current
, nsindex
,
339 static bool preamble_next(struct nvdimm_drvdata
*ndd
,
340 struct nd_namespace_index
**nsindex
,
341 unsigned long **free
, u32
*nslot
)
343 return preamble_index(ndd
, ndd
->ns_next
, nsindex
,
347 static bool slot_valid(struct nvdimm_drvdata
*ndd
,
348 struct nd_namespace_label
*nd_label
, u32 slot
)
350 /* check that we are written where we expect to be written */
351 if (slot
!= __le32_to_cpu(nd_label
->slot
))
354 /* check that DPA allocations are page aligned */
355 if ((__le64_to_cpu(nd_label
->dpa
)
356 | __le64_to_cpu(nd_label
->rawsize
)) % SZ_4K
)
360 if (namespace_label_has(ndd
, checksum
)) {
363 sum_save
= __le64_to_cpu(nd_label
->checksum
);
364 nd_label
->checksum
= __cpu_to_le64(0);
365 sum
= nd_fletcher64(nd_label
, sizeof_namespace_label(ndd
), 1);
366 nd_label
->checksum
= __cpu_to_le64(sum_save
);
367 if (sum
!= sum_save
) {
368 dev_dbg(ndd
->dev
, "fail checksum. slot: %d expect: %#llx\n",
377 int nd_label_reserve_dpa(struct nvdimm_drvdata
*ndd
)
379 struct nd_namespace_index
*nsindex
;
383 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
384 return 0; /* no label, nothing to reserve */
386 for_each_clear_bit_le(slot
, free
, nslot
) {
387 struct nd_namespace_label
*nd_label
;
388 struct nd_region
*nd_region
= NULL
;
389 u8 label_uuid
[NSLABEL_UUID_LEN
];
390 struct nd_label_id label_id
;
391 struct resource
*res
;
394 nd_label
= to_label(ndd
, slot
);
396 if (!slot_valid(ndd
, nd_label
, slot
))
399 memcpy(label_uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
400 flags
= __le32_to_cpu(nd_label
->flags
);
401 nd_label_gen_id(&label_id
, label_uuid
, flags
);
402 res
= nvdimm_allocate_dpa(ndd
, &label_id
,
403 __le64_to_cpu(nd_label
->dpa
),
404 __le64_to_cpu(nd_label
->rawsize
));
405 nd_dbg_dpa(nd_region
, ndd
, res
, "reserve\n");
413 int nd_label_active_count(struct nvdimm_drvdata
*ndd
)
415 struct nd_namespace_index
*nsindex
;
420 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
423 for_each_clear_bit_le(slot
, free
, nslot
) {
424 struct nd_namespace_label
*nd_label
;
426 nd_label
= to_label(ndd
, slot
);
428 if (!slot_valid(ndd
, nd_label
, slot
)) {
429 u32 label_slot
= __le32_to_cpu(nd_label
->slot
);
430 u64 size
= __le64_to_cpu(nd_label
->rawsize
);
431 u64 dpa
= __le64_to_cpu(nd_label
->dpa
);
434 "slot%d invalid slot: %d dpa: %llx size: %llx\n",
435 slot
, label_slot
, dpa
, size
);
443 struct nd_namespace_label
*nd_label_active(struct nvdimm_drvdata
*ndd
, int n
)
445 struct nd_namespace_index
*nsindex
;
449 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
452 for_each_clear_bit_le(slot
, free
, nslot
) {
453 struct nd_namespace_label
*nd_label
;
455 nd_label
= to_label(ndd
, slot
);
456 if (!slot_valid(ndd
, nd_label
, slot
))
460 return to_label(ndd
, slot
);
466 u32
nd_label_alloc_slot(struct nvdimm_drvdata
*ndd
)
468 struct nd_namespace_index
*nsindex
;
472 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
475 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
477 slot
= find_next_bit_le(free
, nslot
, 0);
481 clear_bit_le(slot
, free
);
486 bool nd_label_free_slot(struct nvdimm_drvdata
*ndd
, u32 slot
)
488 struct nd_namespace_index
*nsindex
;
492 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
495 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
498 return !test_and_set_bit_le(slot
, free
);
502 u32
nd_label_nfree(struct nvdimm_drvdata
*ndd
)
504 struct nd_namespace_index
*nsindex
;
508 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
510 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
511 return nvdimm_num_label_slots(ndd
);
513 return bitmap_weight(free
, nslot
);
516 static int nd_label_write_index(struct nvdimm_drvdata
*ndd
, int index
, u32 seq
,
519 struct nd_namespace_index
*nsindex
;
520 unsigned long offset
;
525 nsindex
= to_namespace_index(ndd
, index
);
526 if (flags
& ND_NSINDEX_INIT
)
527 nslot
= nvdimm_num_label_slots(ndd
);
529 nslot
= __le32_to_cpu(nsindex
->nslot
);
531 memcpy(nsindex
->sig
, NSINDEX_SIGNATURE
, NSINDEX_SIG_LEN
);
532 memset(&nsindex
->flags
, 0, 3);
533 nsindex
->labelsize
= sizeof_namespace_label(ndd
) >> 8;
534 nsindex
->seq
= __cpu_to_le32(seq
);
535 offset
= (unsigned long) nsindex
536 - (unsigned long) to_namespace_index(ndd
, 0);
537 nsindex
->myoff
= __cpu_to_le64(offset
);
538 nsindex
->mysize
= __cpu_to_le64(sizeof_namespace_index(ndd
));
539 offset
= (unsigned long) to_namespace_index(ndd
,
540 nd_label_next_nsindex(index
))
541 - (unsigned long) to_namespace_index(ndd
, 0);
542 nsindex
->otheroff
= __cpu_to_le64(offset
);
543 offset
= (unsigned long) nd_label_base(ndd
)
544 - (unsigned long) to_namespace_index(ndd
, 0);
545 nsindex
->labeloff
= __cpu_to_le64(offset
);
546 nsindex
->nslot
= __cpu_to_le32(nslot
);
547 nsindex
->major
= __cpu_to_le16(1);
548 if (sizeof_namespace_label(ndd
) < 256)
549 nsindex
->minor
= __cpu_to_le16(1);
551 nsindex
->minor
= __cpu_to_le16(2);
552 nsindex
->checksum
= __cpu_to_le64(0);
553 if (flags
& ND_NSINDEX_INIT
) {
554 unsigned long *free
= (unsigned long *) nsindex
->free
;
555 u32 nfree
= ALIGN(nslot
, BITS_PER_LONG
);
558 memset(nsindex
->free
, 0xff, nfree
/ 8);
559 for (i
= 0, last_bits
= nfree
- nslot
; i
< last_bits
; i
++)
560 clear_bit_le(nslot
+ i
, free
);
562 checksum
= nd_fletcher64(nsindex
, sizeof_namespace_index(ndd
), 1);
563 nsindex
->checksum
= __cpu_to_le64(checksum
);
564 rc
= nvdimm_set_config_data(ndd
, __le64_to_cpu(nsindex
->myoff
),
565 nsindex
, sizeof_namespace_index(ndd
));
569 if (flags
& ND_NSINDEX_INIT
)
572 /* copy the index we just wrote to the new 'next' */
573 WARN_ON(index
!= ndd
->ns_next
);
574 nd_label_copy(ndd
, to_current_namespace_index(ndd
), nsindex
);
575 ndd
->ns_current
= nd_label_next_nsindex(ndd
->ns_current
);
576 ndd
->ns_next
= nd_label_next_nsindex(ndd
->ns_next
);
577 WARN_ON(ndd
->ns_current
== ndd
->ns_next
);
582 static unsigned long nd_label_offset(struct nvdimm_drvdata
*ndd
,
583 struct nd_namespace_label
*nd_label
)
585 return (unsigned long) nd_label
586 - (unsigned long) to_namespace_index(ndd
, 0);
589 enum nvdimm_claim_class
to_nvdimm_cclass(guid_t
*guid
)
591 if (guid_equal(guid
, &nvdimm_btt_guid
))
592 return NVDIMM_CCLASS_BTT
;
593 else if (guid_equal(guid
, &nvdimm_btt2_guid
))
594 return NVDIMM_CCLASS_BTT2
;
595 else if (guid_equal(guid
, &nvdimm_pfn_guid
))
596 return NVDIMM_CCLASS_PFN
;
597 else if (guid_equal(guid
, &nvdimm_dax_guid
))
598 return NVDIMM_CCLASS_DAX
;
599 else if (guid_equal(guid
, &guid_null
))
600 return NVDIMM_CCLASS_NONE
;
602 return NVDIMM_CCLASS_UNKNOWN
;
605 static const guid_t
*to_abstraction_guid(enum nvdimm_claim_class claim_class
,
608 if (claim_class
== NVDIMM_CCLASS_BTT
)
609 return &nvdimm_btt_guid
;
610 else if (claim_class
== NVDIMM_CCLASS_BTT2
)
611 return &nvdimm_btt2_guid
;
612 else if (claim_class
== NVDIMM_CCLASS_PFN
)
613 return &nvdimm_pfn_guid
;
614 else if (claim_class
== NVDIMM_CCLASS_DAX
)
615 return &nvdimm_dax_guid
;
616 else if (claim_class
== NVDIMM_CCLASS_UNKNOWN
) {
618 * If we're modifying a namespace for which we don't
619 * know the claim_class, don't touch the existing guid.
626 static int __pmem_label_update(struct nd_region
*nd_region
,
627 struct nd_mapping
*nd_mapping
, struct nd_namespace_pmem
*nspm
,
630 struct nd_namespace_common
*ndns
= &nspm
->nsio
.common
;
631 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
632 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
633 struct nd_label_ent
*label_ent
, *victim
= NULL
;
634 struct nd_namespace_label
*nd_label
;
635 struct nd_namespace_index
*nsindex
;
636 struct nd_label_id label_id
;
637 struct resource
*res
;
644 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
647 cookie
= nd_region_interleave_set_cookie(nd_region
, nsindex
);
648 nd_label_gen_id(&label_id
, nspm
->uuid
, 0);
649 for_each_dpa_resource(ndd
, res
)
650 if (strcmp(res
->name
, label_id
.id
) == 0)
658 /* allocate and write the label to the staging (next) index */
659 slot
= nd_label_alloc_slot(ndd
);
660 if (slot
== UINT_MAX
)
662 dev_dbg(ndd
->dev
, "allocated: %d\n", slot
);
664 nd_label
= to_label(ndd
, slot
);
665 memset(nd_label
, 0, sizeof_namespace_label(ndd
));
666 memcpy(nd_label
->uuid
, nspm
->uuid
, NSLABEL_UUID_LEN
);
668 memcpy(nd_label
->name
, nspm
->alt_name
, NSLABEL_NAME_LEN
);
669 nd_label
->flags
= __cpu_to_le32(NSLABEL_FLAG_UPDATING
);
670 nd_label
->nlabel
= __cpu_to_le16(nd_region
->ndr_mappings
);
671 nd_label
->position
= __cpu_to_le16(pos
);
672 nd_label
->isetcookie
= __cpu_to_le64(cookie
);
673 nd_label
->rawsize
= __cpu_to_le64(resource_size(res
));
674 nd_label
->lbasize
= __cpu_to_le64(nspm
->lbasize
);
675 nd_label
->dpa
= __cpu_to_le64(res
->start
);
676 nd_label
->slot
= __cpu_to_le32(slot
);
677 if (namespace_label_has(ndd
, type_guid
))
678 guid_copy(&nd_label
->type_guid
, &nd_set
->type_guid
);
679 if (namespace_label_has(ndd
, abstraction_guid
))
680 guid_copy(&nd_label
->abstraction_guid
,
681 to_abstraction_guid(ndns
->claim_class
,
682 &nd_label
->abstraction_guid
));
683 if (namespace_label_has(ndd
, checksum
)) {
686 nd_label
->checksum
= __cpu_to_le64(0);
687 sum
= nd_fletcher64(nd_label
, sizeof_namespace_label(ndd
), 1);
688 nd_label
->checksum
= __cpu_to_le64(sum
);
690 nd_dbg_dpa(nd_region
, ndd
, res
, "\n");
693 offset
= nd_label_offset(ndd
, nd_label
);
694 rc
= nvdimm_set_config_data(ndd
, offset
, nd_label
,
695 sizeof_namespace_label(ndd
));
699 /* Garbage collect the previous label */
700 mutex_lock(&nd_mapping
->lock
);
701 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
702 if (!label_ent
->label
)
704 if (memcmp(nspm
->uuid
, label_ent
->label
->uuid
,
705 NSLABEL_UUID_LEN
) != 0)
708 list_move_tail(&victim
->list
, &nd_mapping
->labels
);
712 dev_dbg(ndd
->dev
, "free: %d\n", slot
);
713 slot
= to_slot(ndd
, victim
->label
);
714 nd_label_free_slot(ndd
, slot
);
715 victim
->label
= NULL
;
719 rc
= nd_label_write_index(ndd
, ndd
->ns_next
,
720 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
722 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
)
723 if (!label_ent
->label
) {
724 label_ent
->label
= nd_label
;
728 dev_WARN_ONCE(&nspm
->nsio
.common
.dev
, nd_label
,
729 "failed to track label: %d\n",
730 to_slot(ndd
, nd_label
));
734 mutex_unlock(&nd_mapping
->lock
);
739 static bool is_old_resource(struct resource
*res
, struct resource
**list
, int n
)
743 if (res
->flags
& DPA_RESOURCE_ADJUSTED
)
745 for (i
= 0; i
< n
; i
++)
751 static struct resource
*to_resource(struct nvdimm_drvdata
*ndd
,
752 struct nd_namespace_label
*nd_label
)
754 struct resource
*res
;
756 for_each_dpa_resource(ndd
, res
) {
757 if (res
->start
!= __le64_to_cpu(nd_label
->dpa
))
759 if (resource_size(res
) != __le64_to_cpu(nd_label
->rawsize
))
768 * 1/ Account all the labels that can be freed after this update
769 * 2/ Allocate and write the label to the staging (next) index
770 * 3/ Record the resources in the namespace device
772 static int __blk_label_update(struct nd_region
*nd_region
,
773 struct nd_mapping
*nd_mapping
, struct nd_namespace_blk
*nsblk
,
776 int i
, alloc
, victims
, nfree
, old_num_resources
, nlabel
, rc
= -ENXIO
;
777 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
778 struct nd_namespace_common
*ndns
= &nsblk
->common
;
779 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
780 struct nd_namespace_label
*nd_label
;
781 struct nd_label_ent
*label_ent
, *e
;
782 struct nd_namespace_index
*nsindex
;
783 unsigned long *free
, *victim_map
= NULL
;
784 struct resource
*res
, **old_res_list
;
785 struct nd_label_id label_id
;
786 u8 uuid
[NSLABEL_UUID_LEN
];
791 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
794 old_res_list
= nsblk
->res
;
795 nfree
= nd_label_nfree(ndd
);
796 old_num_resources
= nsblk
->num_resources
;
797 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
800 * We need to loop over the old resources a few times, which seems a
801 * bit inefficient, but we need to know that we have the label
802 * space before we start mutating the tracking structures.
803 * Otherwise the recovery method of last resort for userspace is
804 * disable and re-enable the parent region.
807 for_each_dpa_resource(ndd
, res
) {
808 if (strcmp(res
->name
, label_id
.id
) != 0)
810 if (!is_old_resource(res
, old_res_list
, old_num_resources
))
815 if (old_num_resources
) {
816 /* convert old local-label-map to dimm-slot victim-map */
817 victim_map
= kcalloc(BITS_TO_LONGS(nslot
), sizeof(long),
822 /* mark unused labels for garbage collection */
823 for_each_clear_bit_le(slot
, free
, nslot
) {
824 nd_label
= to_label(ndd
, slot
);
825 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
826 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
828 res
= to_resource(ndd
, nd_label
);
829 if (res
&& is_old_resource(res
, old_res_list
,
832 slot
= to_slot(ndd
, nd_label
);
833 set_bit(slot
, victim_map
);
838 /* don't allow updates that consume the last label */
839 if (nfree
- alloc
< 0 || nfree
- alloc
+ victims
< 1) {
840 dev_info(&nsblk
->common
.dev
, "insufficient label space\n");
844 /* from here on we need to abort on error */
847 /* assign all resources to the namespace before writing the labels */
849 nsblk
->num_resources
= 0;
850 for_each_dpa_resource(ndd
, res
) {
851 if (strcmp(res
->name
, label_id
.id
) != 0)
853 if (!nsblk_add_resource(nd_region
, ndd
, nsblk
, res
->start
)) {
860 * Find the resource associated with the first label in the set
861 * per the v1.2 namespace specification.
863 for (i
= 0; i
< nsblk
->num_resources
; i
++) {
864 struct resource
*min
= nsblk
->res
[min_dpa_idx
];
867 if (res
->start
< min
->start
)
871 for (i
= 0; i
< nsblk
->num_resources
; i
++) {
875 if (is_old_resource(res
, old_res_list
, old_num_resources
))
876 continue; /* carry-over */
877 slot
= nd_label_alloc_slot(ndd
);
878 if (slot
== UINT_MAX
)
880 dev_dbg(ndd
->dev
, "allocated: %d\n", slot
);
882 nd_label
= to_label(ndd
, slot
);
883 memset(nd_label
, 0, sizeof_namespace_label(ndd
));
884 memcpy(nd_label
->uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
);
886 memcpy(nd_label
->name
, nsblk
->alt_name
,
888 nd_label
->flags
= __cpu_to_le32(NSLABEL_FLAG_LOCAL
);
891 * Use the presence of the type_guid as a flag to
892 * determine isetcookie usage and nlabel + position
893 * policy for blk-aperture namespaces.
895 if (namespace_label_has(ndd
, type_guid
)) {
896 if (i
== min_dpa_idx
) {
897 nd_label
->nlabel
= __cpu_to_le16(nsblk
->num_resources
);
898 nd_label
->position
= __cpu_to_le16(0);
900 nd_label
->nlabel
= __cpu_to_le16(0xffff);
901 nd_label
->position
= __cpu_to_le16(0xffff);
903 nd_label
->isetcookie
= __cpu_to_le64(nd_set
->cookie2
);
905 nd_label
->nlabel
= __cpu_to_le16(0); /* N/A */
906 nd_label
->position
= __cpu_to_le16(0); /* N/A */
907 nd_label
->isetcookie
= __cpu_to_le64(0); /* N/A */
910 nd_label
->dpa
= __cpu_to_le64(res
->start
);
911 nd_label
->rawsize
= __cpu_to_le64(resource_size(res
));
912 nd_label
->lbasize
= __cpu_to_le64(nsblk
->lbasize
);
913 nd_label
->slot
= __cpu_to_le32(slot
);
914 if (namespace_label_has(ndd
, type_guid
))
915 guid_copy(&nd_label
->type_guid
, &nd_set
->type_guid
);
916 if (namespace_label_has(ndd
, abstraction_guid
))
917 guid_copy(&nd_label
->abstraction_guid
,
918 to_abstraction_guid(ndns
->claim_class
,
919 &nd_label
->abstraction_guid
));
921 if (namespace_label_has(ndd
, checksum
)) {
924 nd_label
->checksum
= __cpu_to_le64(0);
925 sum
= nd_fletcher64(nd_label
,
926 sizeof_namespace_label(ndd
), 1);
927 nd_label
->checksum
= __cpu_to_le64(sum
);
931 offset
= nd_label_offset(ndd
, nd_label
);
932 rc
= nvdimm_set_config_data(ndd
, offset
, nd_label
,
933 sizeof_namespace_label(ndd
));
938 /* free up now unused slots in the new index */
939 for_each_set_bit(slot
, victim_map
, victim_map
? nslot
: 0) {
940 dev_dbg(ndd
->dev
, "free: %d\n", slot
);
941 nd_label_free_slot(ndd
, slot
);
945 rc
= nd_label_write_index(ndd
, ndd
->ns_next
,
946 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
951 * Now that the on-dimm labels are up to date, fix up the tracking
952 * entries in nd_mapping->labels
955 mutex_lock(&nd_mapping
->lock
);
956 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
957 nd_label
= label_ent
->label
;
961 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
962 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
965 list_move(&label_ent
->list
, &list
);
966 label_ent
->label
= NULL
;
968 list_splice_tail_init(&list
, &nd_mapping
->labels
);
969 mutex_unlock(&nd_mapping
->lock
);
971 if (nlabel
+ nsblk
->num_resources
> num_labels
) {
973 * Bug, we can't end up with more resources than
981 mutex_lock(&nd_mapping
->lock
);
982 label_ent
= list_first_entry_or_null(&nd_mapping
->labels
,
983 typeof(*label_ent
), list
);
986 mutex_unlock(&nd_mapping
->lock
);
990 for_each_clear_bit_le(slot
, free
, nslot
) {
991 nd_label
= to_label(ndd
, slot
);
992 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
993 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
995 res
= to_resource(ndd
, nd_label
);
996 res
->flags
&= ~DPA_RESOURCE_ADJUSTED
;
997 dev_vdbg(&nsblk
->common
.dev
, "assign label slot: %d\n", slot
);
998 list_for_each_entry_from(label_ent
, &nd_mapping
->labels
, list
) {
999 if (label_ent
->label
)
1001 label_ent
->label
= nd_label
;
1006 dev_WARN(&nsblk
->common
.dev
,
1007 "failed to track label slot%d\n", slot
);
1009 mutex_unlock(&nd_mapping
->lock
);
1012 kfree(old_res_list
);
1018 * 1/ repair the allocated label bitmap in the index
1019 * 2/ restore the resource list
1021 nd_label_copy(ndd
, nsindex
, to_current_namespace_index(ndd
));
1023 nsblk
->res
= old_res_list
;
1024 nsblk
->num_resources
= old_num_resources
;
1025 old_res_list
= NULL
;
1029 static int init_labels(struct nd_mapping
*nd_mapping
, int num_labels
)
1031 int i
, old_num_labels
= 0;
1032 struct nd_label_ent
*label_ent
;
1033 struct nd_namespace_index
*nsindex
;
1034 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1036 mutex_lock(&nd_mapping
->lock
);
1037 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
)
1039 mutex_unlock(&nd_mapping
->lock
);
1042 * We need to preserve all the old labels for the mapping so
1043 * they can be garbage collected after writing the new labels.
1045 for (i
= old_num_labels
; i
< num_labels
; i
++) {
1046 label_ent
= kzalloc(sizeof(*label_ent
), GFP_KERNEL
);
1049 mutex_lock(&nd_mapping
->lock
);
1050 list_add_tail(&label_ent
->list
, &nd_mapping
->labels
);
1051 mutex_unlock(&nd_mapping
->lock
);
1054 if (ndd
->ns_current
== -1 || ndd
->ns_next
== -1)
1057 return max(num_labels
, old_num_labels
);
1059 nsindex
= to_namespace_index(ndd
, 0);
1060 memset(nsindex
, 0, ndd
->nsarea
.config_size
);
1061 for (i
= 0; i
< 2; i
++) {
1062 int rc
= nd_label_write_index(ndd
, i
, 3 - i
, ND_NSINDEX_INIT
);
1068 ndd
->ns_current
= 0;
1070 return max(num_labels
, old_num_labels
);
1073 static int del_labels(struct nd_mapping
*nd_mapping
, u8
*uuid
)
1075 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1076 struct nd_label_ent
*label_ent
, *e
;
1077 struct nd_namespace_index
*nsindex
;
1078 u8 label_uuid
[NSLABEL_UUID_LEN
];
1079 unsigned long *free
;
1087 /* no index || no labels == nothing to delete */
1088 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
1091 mutex_lock(&nd_mapping
->lock
);
1092 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
1093 struct nd_namespace_label
*nd_label
= label_ent
->label
;
1098 memcpy(label_uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
1099 if (memcmp(label_uuid
, uuid
, NSLABEL_UUID_LEN
) != 0)
1102 slot
= to_slot(ndd
, nd_label
);
1103 nd_label_free_slot(ndd
, slot
);
1104 dev_dbg(ndd
->dev
, "free: %d\n", slot
);
1105 list_move_tail(&label_ent
->list
, &list
);
1106 label_ent
->label
= NULL
;
1108 list_splice_tail_init(&list
, &nd_mapping
->labels
);
1111 nd_mapping_free_labels(nd_mapping
);
1112 dev_dbg(ndd
->dev
, "no more active labels\n");
1114 mutex_unlock(&nd_mapping
->lock
);
1116 return nd_label_write_index(ndd
, ndd
->ns_next
,
1117 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
1120 int nd_pmem_namespace_label_update(struct nd_region
*nd_region
,
1121 struct nd_namespace_pmem
*nspm
, resource_size_t size
)
1125 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1126 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1127 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1128 struct resource
*res
;
1132 rc
= del_labels(nd_mapping
, nspm
->uuid
);
1138 for_each_dpa_resource(ndd
, res
)
1139 if (strncmp(res
->name
, "pmem", 4) == 0)
1141 WARN_ON_ONCE(!count
);
1143 rc
= init_labels(nd_mapping
, count
);
1147 rc
= __pmem_label_update(nd_region
, nd_mapping
, nspm
, i
);
1155 int nd_blk_namespace_label_update(struct nd_region
*nd_region
,
1156 struct nd_namespace_blk
*nsblk
, resource_size_t size
)
1158 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
1159 struct resource
*res
;
1163 return del_labels(nd_mapping
, nsblk
->uuid
);
1165 for_each_dpa_resource(to_ndd(nd_mapping
), res
)
1168 count
= init_labels(nd_mapping
, count
);
1172 return __blk_label_update(nd_region
, nd_mapping
, nsblk
, count
);
1175 int __init
nd_label_init(void)
1177 WARN_ON(guid_parse(NVDIMM_BTT_GUID
, &nvdimm_btt_guid
));
1178 WARN_ON(guid_parse(NVDIMM_BTT2_GUID
, &nvdimm_btt2_guid
));
1179 WARN_ON(guid_parse(NVDIMM_PFN_GUID
, &nvdimm_pfn_guid
));
1180 WARN_ON(guid_parse(NVDIMM_DAX_GUID
, &nvdimm_dax_guid
));