Room 11 · send / receive
How does ZFS send only what changed?
To keep a copy of a filesystem on another machine, a file-by-file tool such as rsync has to find what changed since the last copy. It lists or reads every file on both sides, every time, and it sees a change only through the dates, sizes or contents it compares. On a large filesystem where little has changed, almost all of that work finds nothing.
ZFS does not compare files. Each pointer to a block records the transaction group, or txg, in which the block was written. A txg is a batch of changes committed to disk together, and its number only grows. A snapshot taken in txg F holds only blocks written in F or before. When ZFS changes a block, it also writes new copies of every block above it, up to the root. So a block written in F or before has nothing new below it. An incremental send walks the newer snapshot from the top and skips each such block, with everything below it, without reading it.
What the walk finds becomes a stream of records: a WRITE for each new block of data, a FREE for each range that was emptied, an OBJECT that describes a file. Each record carries a checksum of all the stream up to it, its own header included, and the receiving side checks that checksum before it applies the record. A stream damaged on the way stops at the first bad record, and the receive is undone. An incremental stream also names the snapshot it starts from, and the receiver refuses it unless it holds that snapshot.
1 Send @s4, from the snapshot you choose
The filesystem tank/fs has four snapshots, taken in txgs 10, 20, 30 and 40 (the history above
the tree says what each txg changed). The figure sends the last one, @s4. On the left is its tree,
each block pointer with the txg its block was born in. In the middle is the stream, and on the right the
receiving side.
Choose None first, for a full stream, and press Next step a few times. F is 0, so every pointer is visited and
every block travels: 21 records, 11,672 bytes. Then choose @s3, which the receiver already holds,
and step again. A pointer to a block born at or before txg 30 turns dashed, the blocks below it fade, and they are never
read: 7 records, 2,184 bytes. Each step outlines what it did, first the pointer it tested and the records it
wrote, then the record the receiver checked and the blocks that record changed. Flip a bit to damage the
stream on the way, and the receiver stops at the next header.
Incremental stream from @s2 to @s4 (F = 20): 12 block pointers visited out of 19, 15 records, 6,728 bytes.
History of tank/fs
- txg 10 creates files a, b and c (objects 1 to 3), four 512-byte blocks each; snapshot
@s1 - txg 20 rewrites block 2 of a; snapshot
@s2 - txg 30 punches a hole in blocks 1 and 2 of b, creates d (object 4); snapshot
@s3 - txg 40 deletes c; snapshot
@s4
Sending side: the tree of @s4
12 block pointers visited3 skipped4 below them, never read15 records6,728 bytes
- objset40> 20
- dnode block 040> 200meta-dnode1a2b3free
- a: L120≤ 20a010a110a2′20a310
- b: L130> 20b010≤ 20hole30> 20hole30> 20b310≤ 20
- dnode block 130> 204d5free6free7free
- d: L130> 20d030> 20d130> 20d230> 20d330> 20
The stream
- 0BEGIN@s2 → @s4 (checked and applied)no checksum
- 1OBJECTobject 1, a (checked and applied)50788f0f
- 2FREEa 2,048–end (checked and applied)43af0187
- 3OBJECTobject 2, b (checked and applied)abe05a34
- 4FREEb 2,048–end (checked and applied)e8b7bb43
- 5FREEb 512–1,536 (checked and applied)8c224c03
- 6FREEOBJECTSobject 3 (checked and applied)356ecbe7
- 7OBJECTobject 4, d (checked and applied)21559e25
- 8FREEd 2,048–end (checked and applied)7331f09e
- 9WRITEd @0 “d0” (checked and applied)753d4dba
- 10WRITEd @512 “d1” (checked and applied)c439bd12
- 11WRITEd @1,024 “d2” (checked and applied)a695a488
- 12WRITEd @1,536 “d3” (checked and applied)cc0bcf92
- 13FREEOBJECTSobjects 5–7 (checked and applied)d3567f4c
- 14ENDchecksum 58c87268 (checked and applied)b31db7bb
Receiving side
The receiver holds @s2 (txg 20).
- aobject 1a0a1a2′a3
- bobject 2b0holeholeb3
- dobject 4d0d1d2d3
END checked (58c87268). The received files are those of @s4.
| From | F | Visited | Skipped + below | Records | Bytes |
|---|---|---|---|---|---|
| none (full) | 0 | 19 / 19 | 0 + 0 | 21 | 11,672 |
| @s1 | 10 | 14 / 19 | 5 + 0 | 16 | 7,552 |
| @s2 | 20 | 12 / 19 | 3 + 4 | 15 | 6,728 |
| @s3 | 30 | 3 / 19 | 3 + 13 | 7 | 2,184 |
Text version of this figure
Incremental stream from @s2 to @s4 (F = 20): 12 block pointers visited out of 19, 15 records, 6,728 bytes.
The traversal
- objset, born in txg 40: 40 ≤ 20 is false, visited. No record: the block is read only to reach what is below it.
- meta-dnode L1, born in txg 40: 40 ≤ 20 is false, visited. No record: the block is read only to reach what is below it.
- dnode block 0, born in txg 40: 40 ≤ 20 is false, visited. No record: a dnode block makes one only in a raw (encrypted) stream.
- Dnode 0, the meta-dnode’s number: No record for the meta-dnode.
- Dnode 1, file a: The file travels as an OBJECT, then a FREE from its end, so that a longer copy on the other side is cut. Written: OBJECT 1 (a), FREE a from 2,048 to the end.
- a: L1, born in txg 20: 20 ≤ 20, skipped. Never read, nor what is below it (4 block pointers).
- Dnode 2, file b: The file travels as an OBJECT, then a FREE from its end, so that a longer copy on the other side is cut. Written: OBJECT 2 (b), FREE b from 2,048 to the end.
- b: L1, born in txg 30: 30 ≤ 20 is false, visited. No record: the block is read only to reach what is below it.
- b, block 0, born in txg 10: 10 ≤ 20, skipped. Never read.
- b, block 1 (hole), born in txg 30: 30 ≤ 20 is false, visited. A hole: it travels as a FREE of its range. Held back for now: a FREE or FREEOBJECTS that continues it is merged into it.
- b, block 2 (hole), born in txg 30: 30 ≤ 20 is false, visited. A hole: it travels as a FREE of its range. Held back for now: a FREE or FREEOBJECTS that continues it is merged into it.
- b, block 3, born in txg 10: 10 ≤ 20, skipped. Never read.
- Dnode 3, free: An empty slot travels as FREEOBJECTS, so that an object in that slot on the other side is deleted. Written: FREE b from 512, 1,024 bytes.
- dnode block 1, born in txg 30: 30 ≤ 20 is false, visited. No record: a dnode block makes one only in a raw (encrypted) stream.
- Dnode 4, file d: The file travels as an OBJECT, then a FREE from its end, so that a longer copy on the other side is cut. Written: FREEOBJECTS 3, OBJECT 4 (d), FREE d from 2,048 to the end.
- d: L1, born in txg 30: 30 ≤ 20 is false, visited. No record: the block is read only to reach what is below it.
- d, block 0, born in txg 30: 30 ≤ 20 is false, visited. A data block: it travels as a WRITE with its 512 bytes. Written: WRITE d at 0 (d0).
- d, block 1, born in txg 30: 30 ≤ 20 is false, visited. A data block: it travels as a WRITE with its 512 bytes. Written: WRITE d at 512 (d1).
- d, block 2, born in txg 30: 30 ≤ 20 is false, visited. A data block: it travels as a WRITE with its 512 bytes. Written: WRITE d at 1,024 (d2).
- d, block 3, born in txg 30: 30 ≤ 20 is false, visited. A data block: it travels as a WRITE with its 512 bytes. Written: WRITE d at 1,536 (d3).
- Dnode 5, free: An empty slot travels as FREEOBJECTS, so that an object in that slot on the other side is deleted. Held back for now: a FREE or FREEOBJECTS that continues it is merged into it.
- Dnode 6, free: An empty slot travels as FREEOBJECTS, so that an object in that slot on the other side is deleted. Held back for now: a FREE or FREEOBJECTS that continues it is merged into it.
- Dnode 7, free: An empty slot travels as FREEOBJECTS, so that an object in that slot on the other side is deleted. Held back for now: a FREE or FREEOBJECTS that continues it is merged into it.
The records
- BEGIN
- OBJECT 1 (a), checksum 50788f0f
- FREE a from 2,048 to the end, checksum 43af0187
- OBJECT 2 (b), checksum abe05a34
- FREE b from 2,048 to the end, checksum e8b7bb43
- FREE b from 512, 1,024 bytes, checksum 8c224c03
- FREEOBJECTS 3, checksum 356ecbe7
- OBJECT 4 (d), checksum 21559e25
- FREE d from 2,048 to the end, checksum 7331f09e
- WRITE d at 0 (d0), checksum 753d4dba
- WRITE d at 512 (d1), checksum c439bd12
- WRITE d at 1,024 (d2), checksum a695a488
- WRITE d at 1,536 (d3), checksum cc0bcf92
- FREEOBJECTS 5 to 7, checksum d3567f4c
- END, checksum b31db7bb
END checked (58c87268). The received files are those of @s4.
In the code
The figure follows six places in dmu_send.c, dmu_traverse.c and
dmu_recv.c. Open a Source note to see the lines, taken from release zfs-2.4.4.
Where the walk starts
send_traverse_thread starts the walk of the snapshot being sent. Its lower bound is
fromtxg, the txg in which the from-snapshot was taken, or 0 for a full stream. The figure calls
it F.
Source
send_traverse_thread, dmu_send.c
send_traverse_thread
Excerpt of module/zfs/dmu_send.c, lines
1211–1213, OpenZFS zfs-2.4.4.
View on GitHub
err = traverse_dataset_resume(st_arg->os->os_dsl_dataset,
st_arg->fromtxg, &st_arg->resume,
st_arg->flags | TRAVERSE_LOGICAL, send_cb, st_arg);-
L1211–L1212
fromtxgis the from-snapshot's creation txg, 0 for a full stream. Inside the traversal it becomestd_min_txg. -
L1213
TRAVERSE_LOGICALjoins the send's flags, and every block the traversal visits goes tosend_cb.
The test that skips a subtree
traverse_visitbp runs for every block pointer the walk reaches, and one comparison decides. A
block born at or before the bound was already in the from-snapshot. The function returns without reading it,
so nothing below it is reached either. That is safe because copy-on-write
gives a new birth to every block above a change.
Source
traverse_visitbp, dmu_traverse.c
traverse_visitbp
Excerpt of module/zfs/dmu_traverse.c, lines
277–279, OpenZFS zfs-2.4.4.
View on GitHub
} else if (get_birth_time(td, bp) <= td->td_min_txg) {
return (0);
}-
L277
With
TRAVERSE_LOGICAL,get_birth_timereturns the logical birth, the txg in which the data was written. The comment that defines the two births says the physical one differs for dedup, block cloning and rewritten blocks, and that the logical one is the one for incremental replication. -
L278
Returning here skips the block and its whole subtree: in the figure the pointer turns dashed and what is below it fades. A hole of birth 0 takes the branch above and is visited, as
send_holes_without_birth_timeis 1 by default.
From blocks to records
send_cb gets every block the walk visits. A block of data becomes a WRITE and a hole becomes a
FREE. In a visited dnode block, each file's dnode (the structure on disk that describes the file) becomes an OBJECT,
changed or not, and each empty slot becomes FREEOBJECTS. That is why sending @s4 from @s3 still sends OBJECT records for files a and b: deleting file c rewrote the dnode block the three files share.
Source
send_cb, dmu_send.c
send_cb
Excerpt of module/zfs/dmu_send.c, lines
1140–1146, OpenZFS zfs-2.4.4.
View on GitHub
enum type record_type = DATA;
if (BP_IS_HOLE(bp))
record_type = HOLE;
else if (BP_IS_REDACTED(bp))
record_type = REDACT;
else
record_type = DATA;-
L1140–L1142
A hole is written as a FREE of its range, or, among the dnode blocks, as FREEOBJECTS.
dump_freemerges a FREE into the one before it when it continues it, which is why the two holes of b travel as one record. - L1143–L1146 A data block becomes a WRITE with its data; REDACT belongs to redacted sends. A dnode becomes an OBJECT a few lines above, and every dnode of a visited dnode block reaches this callback.
A checksum that runs through the stream
dump_record writes each record's header. Before it does, it carries a running fletcher-4
checksum over the header and stores the value in the header's last 32 bytes. So the value in each record covers every byte of the stream before it, and its own header up to that value.
Source
dump_record, dmu_send.c
dump_record
Excerpt of module/zfs/dmu_send.c, lines
268–283, OpenZFS zfs-2.4.4.
View on GitHub
(void) fletcher_4_incremental_native(dscp->dsc_drr,
offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
&dscp->dsc_zc);
if (dscp->dsc_drr->drr_type == DRR_BEGIN) {
dscp->dsc_sent_begin = B_TRUE;
} else {
ASSERT(ZIO_CHECKSUM_IS_ZERO(&dscp->dsc_drr->drr_u.
drr_checksum.drr_checksum));
dscp->dsc_drr->drr_u.drr_checksum.drr_checksum = dscp->dsc_zc;
}
if (dscp->dsc_drr->drr_type == DRR_END) {
dscp->dsc_sent_end = B_TRUE;
}
(void) fletcher_4_incremental_native(&dscp->dsc_drr->
drr_u.drr_checksum.drr_checksum,
sizeof (zio_cksum_t), &dscp->dsc_zc);- L268–L270 The running sum is carried on over the header, up to its last 32 bytes.
-
L271–L277
Except for BEGIN, the sum goes into those 32 bytes,
drr_checksum, which every record type has at the same place. - L281–L283 Then the 32 bytes are summed, and the payload after them. END carries the sum of the whole stream. The figure shows the low 32 bits of each value.
The receiver checks before it applies
receive_read_payload_and_next_header keeps the same running sum on the receiving side and
compares it with each header as it arrives. A record goes to the writer only once the header after it has
been checked, so a record whose bytes were damaged is never applied.
Source
receive_read_payload_and_next_header, dmu_recv.c
receive_read_payload_and_next_header
Excerpt of module/zfs/dmu_recv.c, lines
2861–2868, OpenZFS zfs-2.4.4.
View on GitHub
if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
!ZIO_CHECKSUM_EQUAL(drc->drc_cksum, *cksump)) {
kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
drc->drc_next_rrd = NULL;
return (SET_ERROR(ECKSUM));
}
receive_cksum(drc, sizeof (cksum_orig), &cksum_orig);-
L2861–L2866
drc_cksumis the receiver's own sum: it starts over BEGIN and has just been carried over this header. A mismatch stops the receive withECKSUM. A checksum of all zeros is not checked. - L2868 The stored value is summed, as the sender did. Only then is the record before it queued for the writer. END is checked once more, and on an error what the receive created is destroyed.
A stream for a snapshot the receiver does not have
An incremental stream only makes sense on top of the snapshot it starts from. Its first record names that
snapshot by its GUID, a number unique to each snapshot. recv_begin_check_existing_impl looks for
it among the receiving filesystem's snapshots before anything is written.
Source
recv_begin_check_existing_impl, dmu_recv.c
recv_begin_check_existing_impl
Excerpt of module/zfs/dmu_recv.c, lines
473–479, OpenZFS zfs-2.4.4.
View on GitHub
if (dsl_dataset_phys(snap)->ds_guid == fromguid)
break;
obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
dsl_dataset_rele(snap, FTAG);
}
if (obj == 0)
return (SET_ERROR(ENODEV));-
L473–L477
The loop goes from the newest snapshot to older ones until one has the stream's
fromguid, which the sender wrote in BEGIN. -
L478–L479
None has it:
ENODEV, and nothing is received.
Source
What the figure leaves out
The figure is small: files of four 512-byte blocks under one indirect block, dnode blocks of four dnodes (a real
16 KiB dnode block holds 32), a meta-dnode drawn with its two dnode blocks, no bonus buffer. The send runs as one
sequence where the code joins several threads (traversal, merge, reader, the main thread) by queues that keep the
order, and the receiver starts once the stream is complete, where the code receives through a pipe as the stream
is written. Record headers are laid out as dmu_replay_record_t, with only the fields the figure
uses filled in, and the receiver applies each record as receive_process_record dispatches it, by
type. Redacted, raw (encrypted), compressed and resumable streams, embedded data, WRITE_BYREF and spill blocks
are not shown; neither are throughput figures, which depend on the disks and the network, not on this code.