> 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/copy-on-write.md).

# 写时复制

ZFS 几乎所有独特之处都源于一条规则： **使用中的块永远不会被覆盖。** 修改数据意味着把它写到别处，然后切换到新版本。理解了这条规则，ZFS 的其余部分——快照、`zfs send`、自愈、没有 `fsck`——就不再像是彼此独立的功能，而开始像是这条规则的必然结果。

## 树

存储池是一棵大树。叶子节点保存数据，内部节点保存*块指针*，每个指针记录其子节点的位置、大小，以及——最要紧的部分——校验和。因此，块的校验和保存在它的父节点中，而不是与块本身放在一起，这样受损的块就无法自证清白。

树的根部是 **uberblock** 。池中的一切都可以从它到达。

## 写入操作

无法就地修改树中间的块，因此 ZFS 会：

1. 把新数据写入空闲空间；
2. 写入父块的新版本，指向新块；
3. 由于每个父块的校验和都已改变，逐层向上重复；
4. 最后写入新的 uberblock。

在这条写入路径上，只有 `D4` 被修改。这迫使 `D4`、它的父块 `ind 2`、`root` 块以及最后的 uberblock 各生成一个新副本——也就是 **从被修改的块到根节点的路径** ，别无其他。`D1`、`D2`、`D3` 和 `ind 1` 被新树引用，而不是复制。

在最后一步之前，什么都没有改变：旧的 uberblock 仍然描述着一个完整、一致的存储池。切换到新树是一次单一的原子写入。这正是 ZFS 存储池在磁盘上始终保持一致的原因，无论何时断电——不存在存储池被更新到一半的窗口，因此也就没有与 RAID-5 写洞等价的问题，也不需要重放元数据日志。

因此，一次小写入的代价与树的*深度*成正比，而不是与它的大小成正比——而旧树保持完整，这正是快照所需要的。

uberblock 也不会被覆盖。每个设备标签都带有一个 **128 KiB 的 uberblock 环** ，以轮转方式写入；每个设备还带有 **四份标签副本** ——两份在开头，两份在结尾——这样即使丢失磁盘的任一端，或分区表被重写，也不会丢失树的根。导入时，ZFS 会选择事务号最高的有效 uberblock。

## 事务组

写入不会逐条提交。它们被批处理为一个 **事务组** （txg），整组原子提交，否则完全不提交。

同时有三个事务组在进行：一个 **打开** 并接受新写入，一个 **静止** （不再接受新写入，等待尚未完成的操作收尾），一个正在 **同步** 到磁盘。当 `zfs_txg_timeout`（默认 5 秒）超时后，或脏数据积累到足够多时，该组就会关闭。

实际结果是：异步写入一进入内存就会返回，最多几秒后到达磁盘。崩溃会丢失尚未完成同步的事务组，存储池会回到最后一个已提交的事务组。它是一致的，只是稍微旧了一些。

对于调用了 `fsync()` 的应用程序来说，这是不可接受的，这正是 [ZFS 意图日志](https://book.bsdcn.org/zfs/ji-chu-gai-nian/cun-chu-chi-jie-gou-pool-structure/caching.md)存在的意义：它立即记录同步写入，以便在系统于事务组提交前死亡时重放它们。除此之外，ZIL 从不被读取。

## 由此得出的结论

**快照几乎是免费的。** 旧树已经完好完整——快照只是一个引用，阻止这些块被释放。没有任何东西被复制。参见[快照、克隆与书签](https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-ji-datasets/snapshots-and-clones.md)。

**克隆与块克隆开销很低** ，原因相同：对同一个块的多次引用是正常现象，因此共享的代价是一个引用，而不是一次复制。

**增量复制是精确的。** 由于每个块都记录它诞生时所在的事务组，ZFS 无需扫描任何内容，就能精确找出两个快照之间发生变化的块。参见[发送与接收](https://book.bsdcn.org/zfs/ji-chu-gai-nian/cao-zuo-operations/send-and-receive.md)。

**损坏可检测，且通常可修复。** 校验和位于父节点中，因此读回错误的块会在读取时被发现；在有冗余的情况下，会使用完好的副本并重写损坏的那个。参见[校验和](https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-cun-chu-data-storage/checksums.md)与 [Scrub 与 resilver](https://book.bsdcn.org/zfs/ji-chu-gai-nian/cao-zuo-operations/scrub-and-resilver.md)。

**没有 fsck。** `fsck.zfs` 的存在只是为了满足接口，基本不做事——它的手册页就是这么说的。挂载时无需检查一致性，因为存储池从不处于不一致状态。验证*内容*是另一项工作，由 `zpool scrub` 在线完成。

## 代价

写时复制并非没有代价，这些代价也解释了若干调优话题：

**碎片化。** 重写文件会把它的新块散布到任何有空闲空间的地方，因此先顺序写入、再随机修改的数据不会保持顺序。这就是 ZFS 对存储池使用率敏感的原因：找到连续的空闲空间越来越难，分配器也不得不更费力。参见[工作负载调优](https://book.bsdcn.org/zfs/xing-neng-tiao-you/workloadtuning.md)。

**需要空闲空间才能写入。** 即使删除数据也需要分配，这就是 ZFS 保留余量空间的原因——参见[配额与预留](https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-ji-datasets/quotas-and-reservations.md)。

**部分写入代价高昂。** 修改记录的一部分意味着读取整条记录、修改它，再写入新记录。常见的补救办法是让 `recordsize` 匹配应用程序的 I/O 大小，这也是数据库和虚拟机镜像需要单独设置的原因。

## 延伸阅读

* [zfsconcepts(7)](https://book.bsdcn.org/zfs/shou-ce-ye/za-xiang-miscellaneous-7/zfsconcepts.7.md)、[zpoolconcepts(7)](https://book.bsdcn.org/zfs/shou-ce-ye/za-xiang-miscellaneous-7/zpoolconcepts.7.md)
* [校验和](https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-cun-chu-data-storage/checksums.md)、[快照、克隆与书签](https://book.bsdcn.org/zfs/ji-chu-gai-nian/shu-ju-ji-datasets/snapshots-and-clones.md)
* [OpenZFS 事务延迟](https://book.bsdcn.org/zfs/xing-neng-tiao-you/transaction-delay.md)、[ZFS I/O (ZIO) 调度器](https://book.bsdcn.org/zfs/xing-neng-tiao-you/zio-scheduler.md)


---

# 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/copy-on-write.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.
