> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-cun-chu-data-storage/checksums.md).

# ZFS 的校验和及其用途

端到端校验和是 ZFS 的核心特性，也是 ZFS 有别于其他 RAID 实现和文件系统的重要特性。端到端校验和的优势包括：

* 在从介质读取数据时检测数据损坏
* 借助经适当配置的池中的 RAID 保护或冗余副本，在可能的情况下自动修复检测为损坏的块（参见 ZFS 属性 `copies`）
* 定期 scrub 可检查数据，以检测并修复潜在的介质退化（bit rot）以及来自其他来源的损坏
* ZFS 复制流、`zfs send` 和 `zfs receive` 上的校验和确保接收的数据未被中间存储或传输机制损坏

## 校验和算法

可以更改 ZFS 中数据集（文件系统或卷）的校验和算法。每个块使用的校验和算法都存储在块指针（元数据）中。块的校验和在写入时计算，因此更改算法仅影响修改后发生的写入操作。

可以通过设置属性 `checksum` 更改数据集的校验和算法：

```sh
zfs set checksum=sha256 pool_name/dataset_name
```

| 校验和       | 是否适用于去重和 nopwrite？ | 是否兼容其他 ZFS 实现？             | 备注                                                                           |
| --------- | ------------------ | -------------------------- | ---------------------------------------------------------------------------- |
| on        | 见备注                | 是                          | `on` 对非去重数据集为 `fletcher4` 的简写，对去重数据集为 `sha256` 的简写                           |
| off       | 否                  | 是                          | 不要使用 `off`                                                                   |
| fletcher2 | 否                  | 是                          | Fletcher 校验和的已弃用实现，请使用 `fletcher4`                                           |
| fletcher4 | 否                  | 是                          | Fletcher 算法，也用于 `zfs send` 流                                                 |
| sha256    | 是                  | 是                          | 去重数据集的默认值                                                                    |
| noparity  | 否                  | 是                          | 不要使用 `noparity`                                                              |
| sha512    | 是                  | 需要池特性 `org.illumos:sha512` | 带盐的 `sha512` 目前不支持用于启动池上的任何文件系统                                              |
| skein     | 是                  | 需要池特性 `org.illumos:skein`  | 带盐的 `skein` 目前不支持用于启动池上的任何文件系统                                               |
| edonr     | 见备注                | 需要池特性 `org.illumos:edonr`  | 带盐的 `edonr` 目前不支持用于启动池上的任何文件系统。为了谨慎起见，Edon-R 在与去重一起使用时需要验证，因此会自动使用 `verify`。 |
| blake3    | 是                  | 需要池特性 `org.openzfs:blake3` | 带盐的 `blake3` 目前不支持用于启动池上的任何文件系统                                              |

## 校验和加速器

ZFS 能够将校验和操作卸载到 Intel QuickAssist Technology (QAT) 适配器上。

## 校验和微基准测试

某些 ZFS 功能在加载 `zfs.ko` 内核模块时会使用微基准测试来确定校验和的最优算法。微基准测试的结果可以在 `/proc/spl/kstat/zfs` 目录中观察到。获胜算法会被报告为“最快”，并成为默认值。可以通过设置 ZFS 模块参数来覆盖默认值。

| 校验和       | 结果文件名                                  | `zfs` 模块参数                                            |
| --------- | -------------------------------------- | ----------------------------------------------------- |
| Fletcher4 | `/proc/spl/kstat/zfs/fletcher_4_bench` | `zfs_fletcher_4_impl`                                 |
| 其他全部      | `/proc/spl/kstat/zfs/chksum_bench`     | `zfs_blake3_impl`、`zfs_sha256_impl`、`zfs_sha512_impl` |

## 禁用校验和

虽然为了提升 CPU 性能而禁用校验和可能很有诱惑力，但 ZFS 社区普遍认为这是极其不明智的做法。请不要禁用校验和。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-cun-chu-data-storage/checksums.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
