Skip to content

Commit 7cd35b2

Browse files
committed
libnvdimm/labels: Add a checksum calculation helper
In preparation for LIBNVDIMM to manage labels on CXL devices deploy helpers that abstract the label type from the implementation. The CXL label format is mostly similar to the EFI label format with concepts / fields added, like dynamic region creation and label type guids, and other concepts removed like BLK-mode and interleave-set-cookie ids. CXL labels support checksums by default, but early versions of the EFI labels did not. Add a validate function that can return true in the case the label format does not implement a checksum. Reviewed-by: Jonathan Cameron <[email protected]> Link: https://lore.kernel.org/r/162982114637.1124374.6966639787307077105.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Dan Williams <[email protected]>
1 parent 8176f14 commit 7cd35b2

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

drivers/nvdimm/label.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -346,29 +346,45 @@ static bool preamble_next(struct nvdimm_drvdata *ndd,
346346
free, nslot);
347347
}
348348

349+
static bool nsl_validate_checksum(struct nvdimm_drvdata *ndd,
350+
struct nd_namespace_label *nd_label)
351+
{
352+
u64 sum, sum_save;
353+
354+
if (!namespace_label_has(ndd, checksum))
355+
return true;
356+
357+
sum_save = nsl_get_checksum(ndd, nd_label);
358+
nsl_set_checksum(ndd, nd_label, 0);
359+
sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
360+
nsl_set_checksum(ndd, nd_label, sum_save);
361+
return sum == sum_save;
362+
}
363+
364+
static void nsl_calculate_checksum(struct nvdimm_drvdata *ndd,
365+
struct nd_namespace_label *nd_label)
366+
{
367+
u64 sum;
368+
369+
if (!namespace_label_has(ndd, checksum))
370+
return;
371+
nsl_set_checksum(ndd, nd_label, 0);
372+
sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
373+
nsl_set_checksum(ndd, nd_label, sum);
374+
}
375+
349376
static bool slot_valid(struct nvdimm_drvdata *ndd,
350377
struct nd_namespace_label *nd_label, u32 slot)
351378
{
379+
bool valid;
380+
352381
/* check that we are written where we expect to be written */
353382
if (slot != nsl_get_slot(ndd, nd_label))
354383
return false;
355-
356-
/* check checksum */
357-
if (namespace_label_has(ndd, checksum)) {
358-
u64 sum, sum_save;
359-
360-
sum_save = nsl_get_checksum(ndd, nd_label);
361-
nsl_set_checksum(ndd, nd_label, 0);
362-
sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
363-
nsl_set_checksum(ndd, nd_label, sum_save);
364-
if (sum != sum_save) {
365-
dev_dbg(ndd->dev, "fail checksum. slot: %d expect: %#llx\n",
366-
slot, sum);
367-
return false;
368-
}
369-
}
370-
371-
return true;
384+
valid = nsl_validate_checksum(ndd, nd_label);
385+
if (!valid)
386+
dev_dbg(ndd->dev, "fail checksum. slot: %d\n", slot);
387+
return valid;
372388
}
373389

374390
int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
@@ -812,13 +828,7 @@ static int __pmem_label_update(struct nd_region *nd_region,
812828
guid_copy(&nd_label->abstraction_guid,
813829
to_abstraction_guid(ndns->claim_class,
814830
&nd_label->abstraction_guid));
815-
if (namespace_label_has(ndd, checksum)) {
816-
u64 sum;
817-
818-
nsl_set_checksum(ndd, nd_label, 0);
819-
sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
820-
nsl_set_checksum(ndd, nd_label, sum);
821-
}
831+
nsl_calculate_checksum(ndd, nd_label);
822832
nd_dbg_dpa(nd_region, ndd, res, "\n");
823833

824834
/* update label */
@@ -1049,15 +1059,7 @@ static int __blk_label_update(struct nd_region *nd_region,
10491059
guid_copy(&nd_label->abstraction_guid,
10501060
to_abstraction_guid(ndns->claim_class,
10511061
&nd_label->abstraction_guid));
1052-
1053-
if (namespace_label_has(ndd, checksum)) {
1054-
u64 sum;
1055-
1056-
nsl_set_checksum(ndd, nd_label, 0);
1057-
sum = nd_fletcher64(nd_label,
1058-
sizeof_namespace_label(ndd), 1);
1059-
nsl_set_checksum(ndd, nd_label, sum);
1060-
}
1062+
nsl_calculate_checksum(ndd, nd_label);
10611063

10621064
/* update label */
10631065
offset = nd_label_offset(ndd, nd_label);

0 commit comments

Comments
 (0)