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 int nvdimm_num_label_slots(struct nvdimm_drvdata
*ndd
)
50 return ndd
->nsarea
.config_size
/ (sizeof_namespace_label(ndd
) + 1);
53 size_t sizeof_namespace_index(struct nvdimm_drvdata
*ndd
)
55 u32 nslot
, space
, size
;
58 * The minimum index space is 512 bytes, with that amount of
59 * index we can describe ~1400 labels which is less than a byte
60 * of overhead per label. Round up to a byte of overhead per
61 * label and determine the size of the index region. Yes, this
62 * starts to waste space at larger config_sizes, but it's
63 * unlikely we'll ever see anything but 128K.
65 nslot
= nvdimm_num_label_slots(ndd
);
66 space
= ndd
->nsarea
.config_size
- nslot
* sizeof_namespace_label(ndd
);
67 size
= ALIGN(sizeof(struct nd_namespace_index
) + DIV_ROUND_UP(nslot
, 8),
72 dev_err(ndd
->dev
, "label area (%d) too small to host (%d byte) labels\n",
73 ndd
->nsarea
.config_size
, sizeof_namespace_label(ndd
));
77 static int __nd_label_validate(struct nvdimm_drvdata
*ndd
)
80 * On media label format consists of two index blocks followed
81 * by an array of labels. None of these structures are ever
82 * updated in place. A sequence number tracks the current
83 * active index and the next one to write, while labels are
84 * written to free slots.
106 struct nd_namespace_index
*nsindex
[] = {
107 to_namespace_index(ndd
, 0),
108 to_namespace_index(ndd
, 1),
110 const int num_index
= ARRAY_SIZE(nsindex
);
111 struct device
*dev
= ndd
->dev
;
112 bool valid
[2] = { 0 };
113 int i
, num_valid
= 0;
116 for (i
= 0; i
< num_index
; i
++) {
118 u8 sig
[NSINDEX_SIG_LEN
];
119 u64 sum_save
, sum
, size
;
120 unsigned int version
, labelsize
;
122 memcpy(sig
, nsindex
[i
]->sig
, NSINDEX_SIG_LEN
);
123 if (memcmp(sig
, NSINDEX_SIGNATURE
, NSINDEX_SIG_LEN
) != 0) {
124 dev_dbg(dev
, "%s: nsindex%d signature invalid\n",
129 /* label sizes larger than 128 arrived with v1.2 */
130 version
= __le16_to_cpu(nsindex
[i
]->major
) * 100
131 + __le16_to_cpu(nsindex
[i
]->minor
);
133 labelsize
= 1 << (7 + nsindex
[i
]->labelsize
);
137 if (labelsize
!= sizeof_namespace_label(ndd
)) {
138 dev_dbg(dev
, "%s: nsindex%d labelsize %d invalid\n",
139 __func__
, i
, nsindex
[i
]->labelsize
);
143 sum_save
= __le64_to_cpu(nsindex
[i
]->checksum
);
144 nsindex
[i
]->checksum
= __cpu_to_le64(0);
145 sum
= nd_fletcher64(nsindex
[i
], sizeof_namespace_index(ndd
), 1);
146 nsindex
[i
]->checksum
= __cpu_to_le64(sum_save
);
147 if (sum
!= sum_save
) {
148 dev_dbg(dev
, "%s: nsindex%d checksum invalid\n",
153 seq
= __le32_to_cpu(nsindex
[i
]->seq
);
154 if ((seq
& NSINDEX_SEQ_MASK
) == 0) {
155 dev_dbg(dev
, "%s: nsindex%d sequence: %#x invalid\n",
160 /* sanity check the index against expected values */
161 if (__le64_to_cpu(nsindex
[i
]->myoff
)
162 != i
* sizeof_namespace_index(ndd
)) {
163 dev_dbg(dev
, "%s: nsindex%d myoff: %#llx invalid\n",
164 __func__
, i
, (unsigned long long)
165 __le64_to_cpu(nsindex
[i
]->myoff
));
168 if (__le64_to_cpu(nsindex
[i
]->otheroff
)
169 != (!i
) * sizeof_namespace_index(ndd
)) {
170 dev_dbg(dev
, "%s: nsindex%d otheroff: %#llx invalid\n",
171 __func__
, i
, (unsigned long long)
172 __le64_to_cpu(nsindex
[i
]->otheroff
));
176 size
= __le64_to_cpu(nsindex
[i
]->mysize
);
177 if (size
> sizeof_namespace_index(ndd
)
178 || size
< sizeof(struct nd_namespace_index
)) {
179 dev_dbg(dev
, "%s: nsindex%d mysize: %#llx invalid\n",
184 nslot
= __le32_to_cpu(nsindex
[i
]->nslot
);
185 if (nslot
* sizeof_namespace_label(ndd
)
186 + 2 * sizeof_namespace_index(ndd
)
187 > ndd
->nsarea
.config_size
) {
188 dev_dbg(dev
, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
190 ndd
->nsarea
.config_size
);
201 for (i
= 0; i
< num_index
; i
++)
204 /* can't have num_valid > 0 but valid[] = { false, false } */
208 /* pick the best index... */
209 seq
= best_seq(__le32_to_cpu(nsindex
[0]->seq
),
210 __le32_to_cpu(nsindex
[1]->seq
));
211 if (seq
== (__le32_to_cpu(nsindex
[1]->seq
) & NSINDEX_SEQ_MASK
))
221 int nd_label_validate(struct nvdimm_drvdata
*ndd
)
224 * In order to probe for and validate namespace index blocks we
225 * need to know the size of the labels, and we can't trust the
226 * size of the labels until we validate the index blocks.
227 * Resolve this dependency loop by probing for known label
228 * sizes, but default to v1.2 256-byte namespace labels if
231 int label_size
[] = { 128, 256 };
234 for (i
= 0; i
< ARRAY_SIZE(label_size
); i
++) {
235 ndd
->nslabel_size
= label_size
[i
];
236 rc
= __nd_label_validate(ndd
);
244 void nd_label_copy(struct nvdimm_drvdata
*ndd
, struct nd_namespace_index
*dst
,
245 struct nd_namespace_index
*src
)
252 memcpy(dst
, src
, sizeof_namespace_index(ndd
));
255 static struct nd_namespace_label
*nd_label_base(struct nvdimm_drvdata
*ndd
)
257 void *base
= to_namespace_index(ndd
, 0);
259 return base
+ 2 * sizeof_namespace_index(ndd
);
262 static int to_slot(struct nvdimm_drvdata
*ndd
,
263 struct nd_namespace_label
*nd_label
)
265 unsigned long label
, base
;
267 label
= (unsigned long) nd_label
;
268 base
= (unsigned long) nd_label_base(ndd
);
270 return (label
- base
) / sizeof_namespace_label(ndd
);
273 static struct nd_namespace_label
*to_label(struct nvdimm_drvdata
*ndd
, int slot
)
275 unsigned long label
, base
;
277 base
= (unsigned long) nd_label_base(ndd
);
278 label
= base
+ sizeof_namespace_label(ndd
) * slot
;
280 return (struct nd_namespace_label
*) label
;
283 #define for_each_clear_bit_le(bit, addr, size) \
284 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
286 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
289 * preamble_index - common variable initialization for nd_label_* routines
290 * @ndd: dimm container for the relevant label set
291 * @idx: namespace_index index
292 * @nsindex_out: on return set to the currently active namespace index
293 * @free: on return set to the free label bitmap in the index
294 * @nslot: on return set to the number of slots in the label space
296 static bool preamble_index(struct nvdimm_drvdata
*ndd
, int idx
,
297 struct nd_namespace_index
**nsindex_out
,
298 unsigned long **free
, u32
*nslot
)
300 struct nd_namespace_index
*nsindex
;
302 nsindex
= to_namespace_index(ndd
, idx
);
306 *free
= (unsigned long *) nsindex
->free
;
307 *nslot
= __le32_to_cpu(nsindex
->nslot
);
308 *nsindex_out
= nsindex
;
313 char *nd_label_gen_id(struct nd_label_id
*label_id
, u8
*uuid
, u32 flags
)
315 if (!label_id
|| !uuid
)
317 snprintf(label_id
->id
, ND_LABEL_ID_SIZE
, "%s-%pUb",
318 flags
& NSLABEL_FLAG_LOCAL
? "blk" : "pmem", uuid
);
322 static bool preamble_current(struct nvdimm_drvdata
*ndd
,
323 struct nd_namespace_index
**nsindex
,
324 unsigned long **free
, u32
*nslot
)
326 return preamble_index(ndd
, ndd
->ns_current
, nsindex
,
330 static bool preamble_next(struct nvdimm_drvdata
*ndd
,
331 struct nd_namespace_index
**nsindex
,
332 unsigned long **free
, u32
*nslot
)
334 return preamble_index(ndd
, ndd
->ns_next
, nsindex
,
338 static bool slot_valid(struct nvdimm_drvdata
*ndd
,
339 struct nd_namespace_label
*nd_label
, u32 slot
)
341 /* check that we are written where we expect to be written */
342 if (slot
!= __le32_to_cpu(nd_label
->slot
))
345 /* check that DPA allocations are page aligned */
346 if ((__le64_to_cpu(nd_label
->dpa
)
347 | __le64_to_cpu(nd_label
->rawsize
)) % SZ_4K
)
351 if (namespace_label_has(ndd
, checksum
)) {
354 sum_save
= __le64_to_cpu(nd_label
->checksum
);
355 nd_label
->checksum
= __cpu_to_le64(0);
356 sum
= nd_fletcher64(nd_label
, sizeof_namespace_label(ndd
), 1);
357 nd_label
->checksum
= __cpu_to_le64(sum_save
);
358 if (sum
!= sum_save
) {
359 dev_dbg(ndd
->dev
, "%s fail checksum. slot: %d expect: %#llx\n",
360 __func__
, slot
, sum
);
368 int nd_label_reserve_dpa(struct nvdimm_drvdata
*ndd
)
370 struct nd_namespace_index
*nsindex
;
374 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
375 return 0; /* no label, nothing to reserve */
377 for_each_clear_bit_le(slot
, free
, nslot
) {
378 struct nd_namespace_label
*nd_label
;
379 struct nd_region
*nd_region
= NULL
;
380 u8 label_uuid
[NSLABEL_UUID_LEN
];
381 struct nd_label_id label_id
;
382 struct resource
*res
;
385 nd_label
= to_label(ndd
, slot
);
387 if (!slot_valid(ndd
, nd_label
, slot
))
390 memcpy(label_uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
391 flags
= __le32_to_cpu(nd_label
->flags
);
392 nd_label_gen_id(&label_id
, label_uuid
, flags
);
393 res
= nvdimm_allocate_dpa(ndd
, &label_id
,
394 __le64_to_cpu(nd_label
->dpa
),
395 __le64_to_cpu(nd_label
->rawsize
));
396 nd_dbg_dpa(nd_region
, ndd
, res
, "reserve\n");
404 int nd_label_active_count(struct nvdimm_drvdata
*ndd
)
406 struct nd_namespace_index
*nsindex
;
411 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
414 for_each_clear_bit_le(slot
, free
, nslot
) {
415 struct nd_namespace_label
*nd_label
;
417 nd_label
= to_label(ndd
, slot
);
419 if (!slot_valid(ndd
, nd_label
, slot
)) {
420 u32 label_slot
= __le32_to_cpu(nd_label
->slot
);
421 u64 size
= __le64_to_cpu(nd_label
->rawsize
);
422 u64 dpa
= __le64_to_cpu(nd_label
->dpa
);
425 "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
426 __func__
, slot
, label_slot
, dpa
, size
);
434 struct nd_namespace_label
*nd_label_active(struct nvdimm_drvdata
*ndd
, int n
)
436 struct nd_namespace_index
*nsindex
;
440 if (!preamble_current(ndd
, &nsindex
, &free
, &nslot
))
443 for_each_clear_bit_le(slot
, free
, nslot
) {
444 struct nd_namespace_label
*nd_label
;
446 nd_label
= to_label(ndd
, slot
);
447 if (!slot_valid(ndd
, nd_label
, slot
))
451 return to_label(ndd
, slot
);
457 u32
nd_label_alloc_slot(struct nvdimm_drvdata
*ndd
)
459 struct nd_namespace_index
*nsindex
;
463 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
466 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
468 slot
= find_next_bit_le(free
, nslot
, 0);
472 clear_bit_le(slot
, free
);
477 bool nd_label_free_slot(struct nvdimm_drvdata
*ndd
, u32 slot
)
479 struct nd_namespace_index
*nsindex
;
483 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
486 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
489 return !test_and_set_bit_le(slot
, free
);
493 u32
nd_label_nfree(struct nvdimm_drvdata
*ndd
)
495 struct nd_namespace_index
*nsindex
;
499 WARN_ON(!is_nvdimm_bus_locked(ndd
->dev
));
501 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
502 return nvdimm_num_label_slots(ndd
);
504 return bitmap_weight(free
, nslot
);
507 static int nd_label_write_index(struct nvdimm_drvdata
*ndd
, int index
, u32 seq
,
510 struct nd_namespace_index
*nsindex
;
511 unsigned long offset
;
516 nsindex
= to_namespace_index(ndd
, index
);
517 if (flags
& ND_NSINDEX_INIT
)
518 nslot
= nvdimm_num_label_slots(ndd
);
520 nslot
= __le32_to_cpu(nsindex
->nslot
);
522 memcpy(nsindex
->sig
, NSINDEX_SIGNATURE
, NSINDEX_SIG_LEN
);
523 memset(&nsindex
->flags
, 0, 3);
524 nsindex
->labelsize
= sizeof_namespace_label(ndd
) >> 8;
525 nsindex
->seq
= __cpu_to_le32(seq
);
526 offset
= (unsigned long) nsindex
527 - (unsigned long) to_namespace_index(ndd
, 0);
528 nsindex
->myoff
= __cpu_to_le64(offset
);
529 nsindex
->mysize
= __cpu_to_le64(sizeof_namespace_index(ndd
));
530 offset
= (unsigned long) to_namespace_index(ndd
,
531 nd_label_next_nsindex(index
))
532 - (unsigned long) to_namespace_index(ndd
, 0);
533 nsindex
->otheroff
= __cpu_to_le64(offset
);
534 offset
= (unsigned long) nd_label_base(ndd
)
535 - (unsigned long) to_namespace_index(ndd
, 0);
536 nsindex
->labeloff
= __cpu_to_le64(offset
);
537 nsindex
->nslot
= __cpu_to_le32(nslot
);
538 nsindex
->major
= __cpu_to_le16(1);
539 if (sizeof_namespace_label(ndd
) < 256)
540 nsindex
->minor
= __cpu_to_le16(1);
542 nsindex
->minor
= __cpu_to_le16(2);
543 nsindex
->checksum
= __cpu_to_le64(0);
544 if (flags
& ND_NSINDEX_INIT
) {
545 unsigned long *free
= (unsigned long *) nsindex
->free
;
546 u32 nfree
= ALIGN(nslot
, BITS_PER_LONG
);
549 memset(nsindex
->free
, 0xff, nfree
/ 8);
550 for (i
= 0, last_bits
= nfree
- nslot
; i
< last_bits
; i
++)
551 clear_bit_le(nslot
+ i
, free
);
553 checksum
= nd_fletcher64(nsindex
, sizeof_namespace_index(ndd
), 1);
554 nsindex
->checksum
= __cpu_to_le64(checksum
);
555 rc
= nvdimm_set_config_data(ndd
, __le64_to_cpu(nsindex
->myoff
),
556 nsindex
, sizeof_namespace_index(ndd
));
560 if (flags
& ND_NSINDEX_INIT
)
563 /* copy the index we just wrote to the new 'next' */
564 WARN_ON(index
!= ndd
->ns_next
);
565 nd_label_copy(ndd
, to_current_namespace_index(ndd
), nsindex
);
566 ndd
->ns_current
= nd_label_next_nsindex(ndd
->ns_current
);
567 ndd
->ns_next
= nd_label_next_nsindex(ndd
->ns_next
);
568 WARN_ON(ndd
->ns_current
== ndd
->ns_next
);
573 static unsigned long nd_label_offset(struct nvdimm_drvdata
*ndd
,
574 struct nd_namespace_label
*nd_label
)
576 return (unsigned long) nd_label
577 - (unsigned long) to_namespace_index(ndd
, 0);
580 enum nvdimm_claim_class
to_nvdimm_cclass(guid_t
*guid
)
582 if (guid_equal(guid
, &nvdimm_btt_guid
))
583 return NVDIMM_CCLASS_BTT
;
584 else if (guid_equal(guid
, &nvdimm_btt2_guid
))
585 return NVDIMM_CCLASS_BTT2
;
586 else if (guid_equal(guid
, &nvdimm_pfn_guid
))
587 return NVDIMM_CCLASS_PFN
;
588 else if (guid_equal(guid
, &nvdimm_dax_guid
))
589 return NVDIMM_CCLASS_DAX
;
590 else if (guid_equal(guid
, &guid_null
))
591 return NVDIMM_CCLASS_NONE
;
593 return NVDIMM_CCLASS_UNKNOWN
;
596 static const guid_t
*to_abstraction_guid(enum nvdimm_claim_class claim_class
,
599 if (claim_class
== NVDIMM_CCLASS_BTT
)
600 return &nvdimm_btt_guid
;
601 else if (claim_class
== NVDIMM_CCLASS_BTT2
)
602 return &nvdimm_btt2_guid
;
603 else if (claim_class
== NVDIMM_CCLASS_PFN
)
604 return &nvdimm_pfn_guid
;
605 else if (claim_class
== NVDIMM_CCLASS_DAX
)
606 return &nvdimm_dax_guid
;
607 else if (claim_class
== NVDIMM_CCLASS_UNKNOWN
) {
609 * If we're modifying a namespace for which we don't
610 * know the claim_class, don't touch the existing guid.
617 static int __pmem_label_update(struct nd_region
*nd_region
,
618 struct nd_mapping
*nd_mapping
, struct nd_namespace_pmem
*nspm
,
621 struct nd_namespace_common
*ndns
= &nspm
->nsio
.common
;
622 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
623 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
624 struct nd_label_ent
*label_ent
, *victim
= NULL
;
625 struct nd_namespace_label
*nd_label
;
626 struct nd_namespace_index
*nsindex
;
627 struct nd_label_id label_id
;
628 struct resource
*res
;
635 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
638 cookie
= nd_region_interleave_set_cookie(nd_region
, nsindex
);
639 nd_label_gen_id(&label_id
, nspm
->uuid
, 0);
640 for_each_dpa_resource(ndd
, res
)
641 if (strcmp(res
->name
, label_id
.id
) == 0)
649 /* allocate and write the label to the staging (next) index */
650 slot
= nd_label_alloc_slot(ndd
);
651 if (slot
== UINT_MAX
)
653 dev_dbg(ndd
->dev
, "%s: allocated: %d\n", __func__
, slot
);
655 nd_label
= to_label(ndd
, slot
);
656 memset(nd_label
, 0, sizeof_namespace_label(ndd
));
657 memcpy(nd_label
->uuid
, nspm
->uuid
, NSLABEL_UUID_LEN
);
659 memcpy(nd_label
->name
, nspm
->alt_name
, NSLABEL_NAME_LEN
);
660 nd_label
->flags
= __cpu_to_le32(NSLABEL_FLAG_UPDATING
);
661 nd_label
->nlabel
= __cpu_to_le16(nd_region
->ndr_mappings
);
662 nd_label
->position
= __cpu_to_le16(pos
);
663 nd_label
->isetcookie
= __cpu_to_le64(cookie
);
664 nd_label
->rawsize
= __cpu_to_le64(resource_size(res
));
665 nd_label
->lbasize
= __cpu_to_le64(nspm
->lbasize
);
666 nd_label
->dpa
= __cpu_to_le64(res
->start
);
667 nd_label
->slot
= __cpu_to_le32(slot
);
668 if (namespace_label_has(ndd
, type_guid
))
669 guid_copy(&nd_label
->type_guid
, &nd_set
->type_guid
);
670 if (namespace_label_has(ndd
, abstraction_guid
))
671 guid_copy(&nd_label
->abstraction_guid
,
672 to_abstraction_guid(ndns
->claim_class
,
673 &nd_label
->abstraction_guid
));
674 if (namespace_label_has(ndd
, checksum
)) {
677 nd_label
->checksum
= __cpu_to_le64(0);
678 sum
= nd_fletcher64(nd_label
, sizeof_namespace_label(ndd
), 1);
679 nd_label
->checksum
= __cpu_to_le64(sum
);
681 nd_dbg_dpa(nd_region
, ndd
, res
, "%s\n", __func__
);
684 offset
= nd_label_offset(ndd
, nd_label
);
685 rc
= nvdimm_set_config_data(ndd
, offset
, nd_label
,
686 sizeof_namespace_label(ndd
));
690 /* Garbage collect the previous label */
691 mutex_lock(&nd_mapping
->lock
);
692 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
) {
693 if (!label_ent
->label
)
695 if (memcmp(nspm
->uuid
, label_ent
->label
->uuid
,
696 NSLABEL_UUID_LEN
) != 0)
699 list_move_tail(&victim
->list
, &nd_mapping
->labels
);
703 dev_dbg(ndd
->dev
, "%s: free: %d\n", __func__
, slot
);
704 slot
= to_slot(ndd
, victim
->label
);
705 nd_label_free_slot(ndd
, slot
);
706 victim
->label
= NULL
;
710 rc
= nd_label_write_index(ndd
, ndd
->ns_next
,
711 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
713 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
)
714 if (!label_ent
->label
) {
715 label_ent
->label
= nd_label
;
719 dev_WARN_ONCE(&nspm
->nsio
.common
.dev
, nd_label
,
720 "failed to track label: %d\n",
721 to_slot(ndd
, nd_label
));
725 mutex_unlock(&nd_mapping
->lock
);
730 static bool is_old_resource(struct resource
*res
, struct resource
**list
, int n
)
734 if (res
->flags
& DPA_RESOURCE_ADJUSTED
)
736 for (i
= 0; i
< n
; i
++)
742 static struct resource
*to_resource(struct nvdimm_drvdata
*ndd
,
743 struct nd_namespace_label
*nd_label
)
745 struct resource
*res
;
747 for_each_dpa_resource(ndd
, res
) {
748 if (res
->start
!= __le64_to_cpu(nd_label
->dpa
))
750 if (resource_size(res
) != __le64_to_cpu(nd_label
->rawsize
))
759 * 1/ Account all the labels that can be freed after this update
760 * 2/ Allocate and write the label to the staging (next) index
761 * 3/ Record the resources in the namespace device
763 static int __blk_label_update(struct nd_region
*nd_region
,
764 struct nd_mapping
*nd_mapping
, struct nd_namespace_blk
*nsblk
,
767 int i
, alloc
, victims
, nfree
, old_num_resources
, nlabel
, rc
= -ENXIO
;
768 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
769 struct nd_namespace_common
*ndns
= &nsblk
->common
;
770 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
771 struct nd_namespace_label
*nd_label
;
772 struct nd_label_ent
*label_ent
, *e
;
773 struct nd_namespace_index
*nsindex
;
774 unsigned long *free
, *victim_map
= NULL
;
775 struct resource
*res
, **old_res_list
;
776 struct nd_label_id label_id
;
777 u8 uuid
[NSLABEL_UUID_LEN
];
782 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
785 old_res_list
= nsblk
->res
;
786 nfree
= nd_label_nfree(ndd
);
787 old_num_resources
= nsblk
->num_resources
;
788 nd_label_gen_id(&label_id
, nsblk
->uuid
, NSLABEL_FLAG_LOCAL
);
791 * We need to loop over the old resources a few times, which seems a
792 * bit inefficient, but we need to know that we have the label
793 * space before we start mutating the tracking structures.
794 * Otherwise the recovery method of last resort for userspace is
795 * disable and re-enable the parent region.
798 for_each_dpa_resource(ndd
, res
) {
799 if (strcmp(res
->name
, label_id
.id
) != 0)
801 if (!is_old_resource(res
, old_res_list
, old_num_resources
))
806 if (old_num_resources
) {
807 /* convert old local-label-map to dimm-slot victim-map */
808 victim_map
= kcalloc(BITS_TO_LONGS(nslot
), sizeof(long),
813 /* mark unused labels for garbage collection */
814 for_each_clear_bit_le(slot
, free
, nslot
) {
815 nd_label
= to_label(ndd
, slot
);
816 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
817 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
819 res
= to_resource(ndd
, nd_label
);
820 if (res
&& is_old_resource(res
, old_res_list
,
823 slot
= to_slot(ndd
, nd_label
);
824 set_bit(slot
, victim_map
);
829 /* don't allow updates that consume the last label */
830 if (nfree
- alloc
< 0 || nfree
- alloc
+ victims
< 1) {
831 dev_info(&nsblk
->common
.dev
, "insufficient label space\n");
835 /* from here on we need to abort on error */
838 /* assign all resources to the namespace before writing the labels */
840 nsblk
->num_resources
= 0;
841 for_each_dpa_resource(ndd
, res
) {
842 if (strcmp(res
->name
, label_id
.id
) != 0)
844 if (!nsblk_add_resource(nd_region
, ndd
, nsblk
, res
->start
)) {
851 * Find the resource associated with the first label in the set
852 * per the v1.2 namespace specification.
854 for (i
= 0; i
< nsblk
->num_resources
; i
++) {
855 struct resource
*min
= nsblk
->res
[min_dpa_idx
];
858 if (res
->start
< min
->start
)
862 for (i
= 0; i
< nsblk
->num_resources
; i
++) {
866 if (is_old_resource(res
, old_res_list
, old_num_resources
))
867 continue; /* carry-over */
868 slot
= nd_label_alloc_slot(ndd
);
869 if (slot
== UINT_MAX
)
871 dev_dbg(ndd
->dev
, "%s: allocated: %d\n", __func__
, slot
);
873 nd_label
= to_label(ndd
, slot
);
874 memset(nd_label
, 0, sizeof_namespace_label(ndd
));
875 memcpy(nd_label
->uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
);
877 memcpy(nd_label
->name
, nsblk
->alt_name
,
879 nd_label
->flags
= __cpu_to_le32(NSLABEL_FLAG_LOCAL
);
882 * Use the presence of the type_guid as a flag to
883 * determine isetcookie usage and nlabel + position
884 * policy for blk-aperture namespaces.
886 if (namespace_label_has(ndd
, type_guid
)) {
887 if (i
== min_dpa_idx
) {
888 nd_label
->nlabel
= __cpu_to_le16(nsblk
->num_resources
);
889 nd_label
->position
= __cpu_to_le16(0);
891 nd_label
->nlabel
= __cpu_to_le16(0xffff);
892 nd_label
->position
= __cpu_to_le16(0xffff);
894 nd_label
->isetcookie
= __cpu_to_le64(nd_set
->cookie2
);
896 nd_label
->nlabel
= __cpu_to_le16(0); /* N/A */
897 nd_label
->position
= __cpu_to_le16(0); /* N/A */
898 nd_label
->isetcookie
= __cpu_to_le64(0); /* N/A */
901 nd_label
->dpa
= __cpu_to_le64(res
->start
);
902 nd_label
->rawsize
= __cpu_to_le64(resource_size(res
));
903 nd_label
->lbasize
= __cpu_to_le64(nsblk
->lbasize
);
904 nd_label
->slot
= __cpu_to_le32(slot
);
905 if (namespace_label_has(ndd
, type_guid
))
906 guid_copy(&nd_label
->type_guid
, &nd_set
->type_guid
);
907 if (namespace_label_has(ndd
, abstraction_guid
))
908 guid_copy(&nd_label
->abstraction_guid
,
909 to_abstraction_guid(ndns
->claim_class
,
910 &nd_label
->abstraction_guid
));
912 if (namespace_label_has(ndd
, checksum
)) {
915 nd_label
->checksum
= __cpu_to_le64(0);
916 sum
= nd_fletcher64(nd_label
,
917 sizeof_namespace_label(ndd
), 1);
918 nd_label
->checksum
= __cpu_to_le64(sum
);
922 offset
= nd_label_offset(ndd
, nd_label
);
923 rc
= nvdimm_set_config_data(ndd
, offset
, nd_label
,
924 sizeof_namespace_label(ndd
));
929 /* free up now unused slots in the new index */
930 for_each_set_bit(slot
, victim_map
, victim_map
? nslot
: 0) {
931 dev_dbg(ndd
->dev
, "%s: free: %d\n", __func__
, slot
);
932 nd_label_free_slot(ndd
, slot
);
936 rc
= nd_label_write_index(ndd
, ndd
->ns_next
,
937 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
942 * Now that the on-dimm labels are up to date, fix up the tracking
943 * entries in nd_mapping->labels
946 mutex_lock(&nd_mapping
->lock
);
947 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
948 nd_label
= label_ent
->label
;
952 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
953 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
956 list_move(&label_ent
->list
, &list
);
957 label_ent
->label
= NULL
;
959 list_splice_tail_init(&list
, &nd_mapping
->labels
);
960 mutex_unlock(&nd_mapping
->lock
);
962 if (nlabel
+ nsblk
->num_resources
> num_labels
) {
964 * Bug, we can't end up with more resources than
972 mutex_lock(&nd_mapping
->lock
);
973 label_ent
= list_first_entry_or_null(&nd_mapping
->labels
,
974 typeof(*label_ent
), list
);
977 mutex_unlock(&nd_mapping
->lock
);
981 for_each_clear_bit_le(slot
, free
, nslot
) {
982 nd_label
= to_label(ndd
, slot
);
983 memcpy(uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
984 if (memcmp(uuid
, nsblk
->uuid
, NSLABEL_UUID_LEN
) != 0)
986 res
= to_resource(ndd
, nd_label
);
987 res
->flags
&= ~DPA_RESOURCE_ADJUSTED
;
988 dev_vdbg(&nsblk
->common
.dev
, "assign label slot: %d\n", slot
);
989 list_for_each_entry_from(label_ent
, &nd_mapping
->labels
, list
) {
990 if (label_ent
->label
)
992 label_ent
->label
= nd_label
;
997 dev_WARN(&nsblk
->common
.dev
,
998 "failed to track label slot%d\n", slot
);
1000 mutex_unlock(&nd_mapping
->lock
);
1003 kfree(old_res_list
);
1009 * 1/ repair the allocated label bitmap in the index
1010 * 2/ restore the resource list
1012 nd_label_copy(ndd
, nsindex
, to_current_namespace_index(ndd
));
1014 nsblk
->res
= old_res_list
;
1015 nsblk
->num_resources
= old_num_resources
;
1016 old_res_list
= NULL
;
1020 static int init_labels(struct nd_mapping
*nd_mapping
, int num_labels
)
1022 int i
, old_num_labels
= 0;
1023 struct nd_label_ent
*label_ent
;
1024 struct nd_namespace_index
*nsindex
;
1025 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1027 mutex_lock(&nd_mapping
->lock
);
1028 list_for_each_entry(label_ent
, &nd_mapping
->labels
, list
)
1030 mutex_unlock(&nd_mapping
->lock
);
1033 * We need to preserve all the old labels for the mapping so
1034 * they can be garbage collected after writing the new labels.
1036 for (i
= old_num_labels
; i
< num_labels
; i
++) {
1037 label_ent
= kzalloc(sizeof(*label_ent
), GFP_KERNEL
);
1040 mutex_lock(&nd_mapping
->lock
);
1041 list_add_tail(&label_ent
->list
, &nd_mapping
->labels
);
1042 mutex_unlock(&nd_mapping
->lock
);
1045 if (ndd
->ns_current
== -1 || ndd
->ns_next
== -1)
1048 return max(num_labels
, old_num_labels
);
1050 nsindex
= to_namespace_index(ndd
, 0);
1051 memset(nsindex
, 0, ndd
->nsarea
.config_size
);
1052 for (i
= 0; i
< 2; i
++) {
1053 int rc
= nd_label_write_index(ndd
, i
, 3 - i
, ND_NSINDEX_INIT
);
1059 ndd
->ns_current
= 0;
1061 return max(num_labels
, old_num_labels
);
1064 static int del_labels(struct nd_mapping
*nd_mapping
, u8
*uuid
)
1066 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1067 struct nd_label_ent
*label_ent
, *e
;
1068 struct nd_namespace_index
*nsindex
;
1069 u8 label_uuid
[NSLABEL_UUID_LEN
];
1070 unsigned long *free
;
1078 /* no index || no labels == nothing to delete */
1079 if (!preamble_next(ndd
, &nsindex
, &free
, &nslot
))
1082 mutex_lock(&nd_mapping
->lock
);
1083 list_for_each_entry_safe(label_ent
, e
, &nd_mapping
->labels
, list
) {
1084 struct nd_namespace_label
*nd_label
= label_ent
->label
;
1089 memcpy(label_uuid
, nd_label
->uuid
, NSLABEL_UUID_LEN
);
1090 if (memcmp(label_uuid
, uuid
, NSLABEL_UUID_LEN
) != 0)
1093 slot
= to_slot(ndd
, nd_label
);
1094 nd_label_free_slot(ndd
, slot
);
1095 dev_dbg(ndd
->dev
, "%s: free: %d\n", __func__
, slot
);
1096 list_move_tail(&label_ent
->list
, &list
);
1097 label_ent
->label
= NULL
;
1099 list_splice_tail_init(&list
, &nd_mapping
->labels
);
1102 nd_mapping_free_labels(nd_mapping
);
1103 dev_dbg(ndd
->dev
, "%s: no more active labels\n", __func__
);
1105 mutex_unlock(&nd_mapping
->lock
);
1107 return nd_label_write_index(ndd
, ndd
->ns_next
,
1108 nd_inc_seq(__le32_to_cpu(nsindex
->seq
)), 0);
1111 int nd_pmem_namespace_label_update(struct nd_region
*nd_region
,
1112 struct nd_namespace_pmem
*nspm
, resource_size_t size
)
1116 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
1117 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
1118 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
1119 struct resource
*res
;
1123 rc
= del_labels(nd_mapping
, nspm
->uuid
);
1129 for_each_dpa_resource(ndd
, res
)
1130 if (strncmp(res
->name
, "pmem", 4) == 0)
1132 WARN_ON_ONCE(!count
);
1134 rc
= init_labels(nd_mapping
, count
);
1138 rc
= __pmem_label_update(nd_region
, nd_mapping
, nspm
, i
);
1146 int nd_blk_namespace_label_update(struct nd_region
*nd_region
,
1147 struct nd_namespace_blk
*nsblk
, resource_size_t size
)
1149 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
1150 struct resource
*res
;
1154 return del_labels(nd_mapping
, nsblk
->uuid
);
1156 for_each_dpa_resource(to_ndd(nd_mapping
), res
)
1159 count
= init_labels(nd_mapping
, count
);
1163 return __blk_label_update(nd_region
, nd_mapping
, nsblk
, count
);
1166 int __init
nd_label_init(void)
1168 WARN_ON(guid_parse(NVDIMM_BTT_GUID
, &nvdimm_btt_guid
));
1169 WARN_ON(guid_parse(NVDIMM_BTT2_GUID
, &nvdimm_btt2_guid
));
1170 WARN_ON(guid_parse(NVDIMM_PFN_GUID
, &nvdimm_pfn_guid
));
1171 WARN_ON(guid_parse(NVDIMM_DAX_GUID
, &nvdimm_dax_guid
));