From: Christoph Hellwig <hch@lst.de>
To: Andrey Albershteyn <aalbersh@kernel.org>
Cc: "Darrick J . Wong" <djwong@kernel.org>, linux-xfs@vger.kernel.org
Subject: [PATCH 03/32] libfrog: add a crc64_nvme implementation
Date: Thu, 24 Sep 2026 12:03:53 +0200 [thread overview]
Message-ID: <20260924100512.2733748-4-hch@lst.de> (raw)
In-Reply-To: <20260924100512.2733748-1-hch@lst.de>
Imported from the Linux kernel, using and older version as of commit
commit 067bc8717aee ("lib/crc64: add support for arch-optimized
implementations") which doesn't have the relatively complicated
common optimized crc implementation in the current kernel.
Compared to the kernel, libfrog drops the ECMA CRC64 and only keeps the
NVMe one that is used by XFS RT deata checksums.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
libfrog/Makefile | 14 +++++++--
libfrog/crc64.c | 23 ++++++++++++++
libfrog/crc64.h | 25 +++++++++++++++
libfrog/gen_crc64table.c | 67 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 127 insertions(+), 2 deletions(-)
create mode 100644 libfrog/crc64.c
create mode 100644 libfrog/crc64.h
create mode 100644 libfrog/gen_crc64table.c
diff --git a/libfrog/Makefile b/libfrog/Makefile
index c7bcd6a778d7..8cf07622f01f 100644
--- a/libfrog/Makefile
+++ b/libfrog/Makefile
@@ -18,6 +18,7 @@ bitmap.c \
bulkstat.c \
convert.c \
crc32.c \
+crc64.c \
file_exchange.c \
flagmap.c \
fsgeom.c \
@@ -51,6 +52,8 @@ crc32c.h \
crc32cselftest.h \
crc32defs.h \
crc32table.h \
+crc64.h \
+crc64table.h \
dahashselftest.h \
div64.h \
fakelibattr.h \
@@ -80,9 +83,10 @@ zones.h
GETTEXT_PY = \
gettext.py
-LSRCFILES += gen_crc32table.c
+LSRCFILES += gen_crc32table.c gen_crc64table.c
-LDIRT = gen_crc32table crc32table.h
+LDIRT = gen_crc32table crc32table.h \
+ gen_crc64table crc64table.h
ifeq ($(ENABLE_GETTEXT),yes)
HAVE_GETTEXT = True
@@ -120,6 +124,12 @@ crc32table.h: gen_crc32table.c crc32defs.h
@echo " [GENERATE] $@"
$(Q) ./gen_crc32table > crc32table.h
+crc64table.h: gen_crc64table.c
+ @echo " [CC] gen_crc64table"
+ $(Q) $(BUILD_CC) $(BUILD_CFLAGS) -o gen_crc64table $<
+ @echo " [GENERATE] $@"
+ $(Q) ./gen_crc64table > crc64table.h
+
$(GETTEXT_PY): $(GETTEXT_PY).in $(TOPDIR)/include/builddefs
@echo " [SED] $@"
$(Q)$(SED) -e "s|@HAVE_GETTEXT@|$(HAVE_GETTEXT)|g" \
diff --git a/libfrog/crc64.c b/libfrog/crc64.c
new file mode 100644
index 000000000000..280ac77b16cc
--- /dev/null
+++ b/libfrog/crc64.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * 64-bit CRC calculation based on the NVMe NVM Command Set specication:
+ * using least-significant-bit first bit order:
+ *
+ * x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 +
+ * x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 +
+ * x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1
+ *
+ * Copyright 2018 SUSE Linux.
+ * Author: Coly Li <colyli@suse.de>
+ */
+
+#include <stdint.h>
+#include "libfrog/crc64.h"
+#include "crc64table.h"
+
+uint64_t crc64_nvme_generic(uint64_t crc, const uint8_t *p, size_t len)
+{
+ while (len--)
+ crc = (crc >> 8) ^ crc64nvmetable[(crc & 0xff) ^ *p++];
+ return crc;
+}
diff --git a/libfrog/crc64.h b/libfrog/crc64.h
new file mode 100644
index 000000000000..283bd3949577
--- /dev/null
+++ b/libfrog/crc64.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LIBFROG_CRC64_H
+#define _LIBFROG_CRC64_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+uint64_t crc64_nvme_generic(uint64_t crc, const uint8_t *p, size_t len);
+
+/**
+ * crc64_nvme - Calculate CRC64-NVME
+ * @crc: seed value for computation. 0 for a new CRC calculation, or the
+ * previous crc64 value if computing incrementally.
+ * @p: pointer to buffer over which CRC64 is run
+ * @len: length of buffer @p
+ *
+ * This computes the CRC64 defined in the NVME NVM Command Set Specification,
+ * *including the bitwise inversion at the beginning and end*.
+ */
+static inline uint64_t crc64_nvme(uint64_t crc, const void *p, size_t len)
+{
+ return ~crc64_nvme_generic(~crc, p, len);
+}
+
+#endif /* _LIBFROG_CRC64_H */
diff --git a/libfrog/gen_crc64table.c b/libfrog/gen_crc64table.c
new file mode 100644
index 000000000000..bf88f8c6ffc8
--- /dev/null
+++ b/libfrog/gen_crc64table.c
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generate lookup table for the table-driven CRC64 calculation.
+ *
+ * gen_crc64table is executed at build time and generates crc64table.h.
+ * This header is included by crc64.c for the table-driven CRC64 calculation.
+ *
+ * See crc64.c for more information about which specification and polynomial
+ * arithmetic that gen_crc64table.c follows to generate the lookup table.
+ *
+ * Copyright 2018 SUSE Linux.
+ * Author: Coly Li <colyli@suse.de>
+ */
+#include <inttypes.h>
+#include <stdio.h>
+
+#define CRC64_NVME_POLY 0x9A6C9329AC4BC9B5ULL
+
+static uint64_t crc64_nvme_table[256] = {0};
+
+static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
+{
+ uint64_t i, j, c, crc;
+
+ for (i = 0; i < 256; i++) {
+ crc = 0ULL;
+ c = i;
+
+ for (j = 0; j < 8; j++) {
+ if ((crc ^ (c >> j)) & 1)
+ crc = (crc >> 1) ^ poly;
+ else
+ crc >>= 1;
+ }
+ table[i] = crc;
+ }
+}
+
+static void output_table(uint64_t table[256])
+{
+ int i;
+
+ for (i = 0; i < 256; i++) {
+ printf("\t0x%016" PRIx64 "ULL", table[i]);
+ if (i & 0x1)
+ printf(",\n");
+ else
+ printf(", ");
+ }
+ printf("};\n");
+}
+
+static void print_crc64_tables(void)
+{
+ printf("/* this file is generated - do not edit */\n\n");
+ printf("#include <stdint.h>\n");
+
+ printf("\nstatic const uint64_t crc64nvmetable[256] = {\n");
+ output_table(crc64_nvme_table);
+}
+
+int main(int argc, char *argv[])
+{
+ generate_reflected_crc64_table(crc64_nvme_table, CRC64_NVME_POLY);
+ print_crc64_tables();
+ return 0;
+}
--
2.53.0
next prev parent reply other threads:[~2026-09-24 10:05 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 10:03 xfsprogs support for RT data checksums Christoph Hellwig
2026-09-24 10:03 ` [PATCH 01/32] man: fix alignment of the rtstart field in ioctl_xfs_fsgeometry.2 Christoph Hellwig
2026-09-24 20:30 ` Darrick J. Wong
2026-09-26 6:15 ` Christoph Hellwig
2026-09-29 10:14 ` Andrey Albershteyn
2026-09-24 10:03 ` [PATCH 02/32] add cpu_to_le64 and le64_to_cpu_helpers Christoph Hellwig
2026-09-24 20:30 ` Darrick J. Wong
2026-09-24 10:03 ` Christoph Hellwig [this message]
2026-09-24 10:03 ` [PATCH 04/32] libxfs: add DIV_ROUND_UP_ULL Christoph Hellwig
2026-09-24 20:44 ` Darrick J. Wong
2026-09-25 6:13 ` Christoph Hellwig
2026-09-24 10:03 ` [PATCH 05/32] libxfs: remove a spurious xfs_rtbitmap.h include in xfs_sb.c Christoph Hellwig
2026-09-25 23:27 ` Darrick J. Wong
2026-09-24 10:03 ` [PATCH 06/32] libxfs: add SZ_* constants Christoph Hellwig
2026-09-25 23:27 ` Darrick J. Wong
2026-09-24 10:03 ` [PATCH 07/32] xfs: remove spurious XBF_DONE clearing on readahead validation failure Christoph Hellwig
2026-09-24 10:03 ` [PATCH 08/32] xfs: hide b_flags manipulation from code outside of xfs_buf.c Christoph Hellwig
2026-09-24 10:03 ` [PATCH 09/32] FIXUP Christoph Hellwig
2026-09-25 23:29 ` Darrick J. Wong
2026-09-26 6:17 ` Christoph Hellwig
2026-09-24 10:04 ` [PATCH 10/32] xfs: add error injection for lazy bounce buffering Christoph Hellwig
2026-09-24 10:04 ` [PATCH 11/32] xfs: add xfs_daddr_to_rgno and xfs_daddr_to_rgbno helpers Christoph Hellwig
2026-09-24 10:04 ` [PATCH 12/32] FIXUP Christoph Hellwig
2026-09-24 10:04 ` [PATCH 13/32] xfs: introduce XFS_BLI_PREALLOC Christoph Hellwig
2026-09-24 10:04 ` [PATCH 14/32] xfs: centralize setting of buf_ops/buf_type/magic for rtblocks Christoph Hellwig
2026-09-24 10:04 ` [PATCH 15/32] xfs: factor out a xfs_rtfile_initialize_buf helper Christoph Hellwig
2026-09-24 10:04 ` [PATCH 16/32] xfs: add a xfs_rtblock_payload helper Christoph Hellwig
2026-09-24 10:04 ` [PATCH 17/32] xfs: prepare xfs_rtfile_initialize_blocks for larger than FSB blocks Christoph Hellwig
2026-09-24 10:04 ` [PATCH 18/32] FIXUP Christoph Hellwig
2026-09-24 10:04 ` [PATCH 19/32] xfs: define the RT data checksum on-disk format Christoph Hellwig
2026-09-24 10:04 ` [PATCH 20/32] FIXUP Christoph Hellwig
2026-09-24 10:04 ` [PATCH 21/32] xfs: add support for per-RTG csum files Christoph Hellwig
2026-09-24 10:04 ` [PATCH 22/32] FIXUP Christoph Hellwig
2026-09-24 10:04 ` [PATCH 23/32] xfs: calculate the log reservation for logging data checksum buffers Christoph Hellwig
2026-09-24 10:04 ` [PATCH 24/32] xfs: report RT data checksum information via XFS_FSOP_GEOM Christoph Hellwig
2026-09-24 10:04 ` [PATCH 25/32] xfs: enable RT data checksums Christoph Hellwig
2026-09-24 10:04 ` [PATCH 26/32] man: document the rtcsum geom fields Christoph Hellwig
2026-09-25 23:31 ` Darrick J. Wong
2026-09-26 6:17 ` Christoph Hellwig
2026-09-24 10:04 ` [PATCH 27/32] libfrog: print csum geometry information Christoph Hellwig
2026-09-25 23:32 ` Darrick J. Wong
2026-09-24 10:04 ` [PATCH 28/32] xfs_io: report checksum information from fs geometry in statfs Christoph Hellwig
2026-09-25 23:33 ` Darrick J. Wong
2026-09-26 6:18 ` Christoph Hellwig
2026-09-24 10:04 ` [PATCH 29/32] mkfs: support RT data checksums Christoph Hellwig
2026-09-25 23:39 ` Darrick J. Wong
2026-09-26 6:19 ` Christoph Hellwig
2026-09-24 10:04 ` [PATCH 30/32] xfs_db: support RT data checksum Christoph Hellwig
2026-09-25 23:43 ` Darrick J. Wong
2026-09-26 6:21 ` Christoph Hellwig
2026-09-26 18:20 ` Darrick J. Wong
2026-09-24 10:04 ` [PATCH 31/32] repair: support RT data checksums Christoph Hellwig
2026-09-25 23:54 ` Darrick J. Wong
2026-09-26 6:23 ` Christoph Hellwig
2026-09-27 0:01 ` Darrick J. Wong
2026-09-24 10:04 ` [PATCH 32/32] xfs_scrub: don't merge over unused space when data checksums are enabled Christoph Hellwig
2026-09-25 23:51 ` Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260924100512.2733748-4-hch@lst.de \
--to=hch@lst.de \
--cc=aalbersh@kernel.org \
--cc=djwong@kernel.org \
--cc=linux-xfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox