Room 2 · The tree and the uberblock
What if the power fails while ZFS is writing?
ZFS gathers changes in memory and writes them out together every few seconds, as a transaction group (txg). One txg can write thousands of blocks to several disks, and the power can fail at any moment while it does. A filesystem that updates blocks in place can be left half old and half new, and a repair tool such as fsck has to walk the disk to fix it. ZFS has no fsck, so after a crash the pool must come back as it was before the txg or as it is after it, never as a mix of the two.
Every block of a pool hangs from one small record, the uberblock. It holds a single pointer to the top of a tree of block pointers, and the tree leads down to every block of data. When a block changes, ZFS writes the new version somewhere else and leaves the old one in place. The block above it holds its address, so that block gets a new version too, and so on up to the top. Until the end of the txg, the uberblock still points to the old tree, which nothing has touched.
Once the new tree is written, ZFS makes the disks it wrote to empty their write caches, the memory where a disk holds writes before they reach stable storage. Then it writes a new uberblock that points to the new tree, and writing that uberblock switches the pool over. The new uberblock goes to a different slot from the one in use, so a write cut halfway cannot damage the current one. Each disk keeps four copies of this ring of slots. On import, ZFS reads every slot, skips the ones that fail their checksum, and takes the newest. It points to a complete tree, old or new.
1 Change a block, follow the pointers up
The figure draws one file under its uberblock: four data blocks, D0 to D3, two levels of indirect blocks above them, the block that holds the file’s dnode (the record that describes the file), and the objset at the top. The number under each name is the txg that wrote that version. D2 has just been changed. Its new copy and new copies of every block above it stand beside the old ones, still in memory, and the uberblock still points to the old tree.
Press Next step. ZFS writes the new blocks from the bottom up, one row of the figure per step. A block stores the new addresses and checksums of the blocks below it, and they are known only once those blocks are placed. The dotted pointers fill in one row at a time, up to the objset. Write D3 before the first step to add it to the same txg. Only D3 itself is new, because the blocks above it were already copied for D2.
- in memory, not written
- pointer not filled in yet
- written, not flushed
- just written
- new copy
- old copy
- no longer reached
- lost in the cut
3D view of the same state. Drag, or use the arrow keys on the focused view, to turn it; + and − to zoom. Every control above works the same in 3D.
No WebGL in this browser: the figure stays in 2D, which shows the same state.
The commit
Txg 4211 is open: 5 blocks to write. Next step: spa_sync writes the bottom row.
- next spa_sync writes the new blocks of txg 4211, each to a new place, one row per step from the data up.
- to do Flush: every disk written in this txg commits its write cache.
- to do Even labels: L0 and L2 get the config of txg 4211.
- to do Uberblocks: txg 4211 goes to slot 115 of the four rings.
- to do Odd labels: L1 and L3 get the config of txg 4211.
Last import
No import yet. Pull the plug at any step, then import the pool.
On the disk
| Label | Starts at | Config | Slot 114 (+242 KiB) | Slot 115 (+243 KiB) |
|---|---|---|---|---|
| L0 | 0 MiB | txg 4200 | txg 4210 | txg 4083 |
| L1 | 0.25 MiB | txg 4200 | txg 4210 | txg 4083 |
| L2 | 1023.5 MiB | txg 4200 | txg 4210 | txg 4083 |
| L3 | 1023.75 MiB | txg 4200 | txg 4210 | txg 4083 |
One disk of 1 GiB, ashift 9: each ring has 128 slots of 1 KiB. Not to scale: a label is 256 KiB, the boot area 3.5 MiB, the rest holds the blocks.
Text version of this figure
The newest uberblock on the disk is that of txg 4210, in slot 114. It points to the objset of txg 4210. The tree it reaches: objset (txg 4210), dnodes (txg 4210), L2 #0 (txg 4210), L1 #0 (txg 4190), L1 #1 (txg 4210), D0 (txg 4190), D1 (txg 4120), D2 (txg 4180), D3 (txg 4210).
Txg 4211 has new copies of objset, dnodes, L2 #0, L1 #1 and D2; Txg 4211 is open: 5 blocks to write. Next step: spa_sync writes the bottom row.
Labels: L0 config txg 4200, slot 115 txg 4083; L1 config txg 4200, slot 115 txg 4083; L2 config txg 4200, slot 115 txg 4083; L3 config txg 4200, slot 115 txg 4083.
2 The commit, in four beats
Each disk carries four labels, two at its start and two at its end. A label holds the pool’s configuration and a ring of uberblock slots. After the objset, keep pressing Next step: the next four steps are the commit, and the figure numbers them as beats 1 to 4. First every disk written in the txg empties its write cache, so the new tree is on stable storage before anything points to it. Then the even labels, L0 and L2, get the new configuration. Then the uberblock goes into the next slot of all four rings, and the arrow at the top of the tree moves to the new objset. Last, the odd labels, L1 and L3, get the new configuration.
The labels are rewritten only in a txg that changes the configuration, such as one that adds a disk. Turn off “The vdev configuration changes in this txg” before the first step to see an ordinary txg, where only the uberblock is new. After an import the switch stays on for the next txg, because ZFS rewrites every label once it has opened the pool.
3 Pull the plug, then import
Pull the plug at any step, then import the pool. The cut falls during the step that would have come next. Before beat 3, the old uberblock is still the newest valid one, so the pool comes back on the old tree, and nothing points to the blocks written for the lost txg. If the configuration changed in this txg, the even labels written at beat 2 are newer than that uberblock, so the import passes over them and reads an odd label. Once one copy of the new uberblock has landed, it wins, and the pool comes back on the new tree. A cut in the middle of an uberblock write leaves a torn slot, which fails its checksum and is skipped. To see both cases, set “Uberblock writes that land before a cut in beat 3” and pull the plug once the even labels are written. At 0 the pool comes back on the old tree, and at 1 or more on the new one.
In the code
The figure follows seven places in the OpenZFS source. Open a Source note to see the lines, taken from release zfs-2.4.4.
The root
The uberblock is a small structure. Among other fields, it holds the txg it commits, a timestamp, and one block
pointer, ub_rootbp, to the top of the tree.
Source
struct uberblock, uberblock_impl.h
struct uberblock
Excerpt of include/sys/uberblock_impl.h, lines
122–128, OpenZFS zfs-2.4.4.
View on GitHub
struct uberblock {
uint64_t ub_magic; /* UBERBLOCK_MAGIC */
uint64_t ub_version; /* SPA_VERSION */
uint64_t ub_txg; /* txg of last sync */
uint64_t ub_guid_sum; /* sum of all vdev guids */
uint64_t ub_timestamp; /* UTC time of last sync */
blkptr_t ub_rootbp; /* MOS objset_phys_t */-
L125
ub_txg: the txg this uberblock commits. On import, the valid uberblock with the highest txg wins. -
L127
ub_timestamp: decides between two uberblocks of the same txg. -
L128
ub_rootbp: the only pointer, to the pool’s meta objset (MOS).dsl_pool_sync_mossets it once the MOS is written.
A change marks every parent
dbuf_dirty marks a block as changed in the open txg. If the block has a parent, it finds the parent
one level up and calls itself on it. That is why one changed block gives a new copy of every block on its path
to the top.
Source
dbuf_dirty, dbuf.c
dbuf_dirty
Excerpt of module/zfs/dbuf.c, lines
2537–2552, OpenZFS zfs-2.4.4.
View on GitHub
if (db->db_level+1 < dn->dn_nlevels) {
dmu_buf_impl_t *parent = db->db_parent;
dbuf_dirty_record_t *di;
int parent_held = FALSE;
if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
parent = dbuf_hold_level(dn, db->db_level + 1,
db->db_blkid >> epbs, FTAG);
ASSERT(parent != NULL);
parent_held = TRUE;
}
if (drop_struct_rwlock)
rw_exit(&dn->dn_struct_rwlock);
ASSERT3U(db->db_level + 1, ==, parent->db_level);
di = dbuf_dirty(parent, tx);- L2537 A block below the top level of its object has a parent indirect block.
-
L2542–L2545
The parent is at level + 1, block
db_blkid >> epbs, with 2epbs pointers per indirect block (1024 for 128 KiB). The figure uses 2, to stay small. - L2552 The parent is marked too, and so on up. The top level is held by the dnode, which is marked dirty in its own block of dnodes. At sync, every marked block is written to a new place.
Children before parents
The write of a block is an I/O whose children are the writes of the blocks it points to.
zio_write_compress holds the parent back until every child is ready, which means the child has its
new address and checksum. The parent is then checksummed with those new pointers in it.
Source
zio_write_compress, zio.c
zio_write_compress
Excerpt of module/zfs/zio.c, lines
1932–1939, OpenZFS zfs-2.4.4.
View on GitHub
/*
* If our children haven't all reached the ready stage,
* wait for them and then repeat this pipeline stage.
*/
if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
return (NULL);
}-
L1932–L1939
The parent’s write stops at this stage until its children are ready, then runs this stage again.
dbuf_writemakes each block’s write a child of its parent’s, and when a child is ready,dbuf_write_readycopies its new block pointer into the parent. The tree is written from the data up.
Flush before the uberblock
vdev_config_sync runs the commit. It starts by flushing the write cache of every disk written in the
txg, and its comment says why.
Source
vdev_config_sync, vdev_label.c
vdev_config_sync
Excerpt of module/zfs/vdev_label.c, lines
2085–2098, OpenZFS zfs-2.4.4.
View on GitHub
/*
* Flush the write cache of every disk that's been written to
* in this transaction group. This ensures that all blocks
* written in this txg will be committed to stable storage
* before any uberblock that references them.
*/
zio_t *zio = zio_root(spa, NULL, NULL, flags);
for (vdev_t *vd =
txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
zio_flush(zio, vd);
(void) zio_wait(zio);- L2085–L2090 The blocks of this txg must be on stable storage “before any uberblock that references them”.
- L2093–L2098 Every disk written in this txg is flushed, and the wait covers them all. Then come the even labels, the uberblocks and the odd labels. The labels are written only for vdevs whose configuration changed.
A new slot every txg
vdev_uberblock_sync picks the slot from the txg number, so the new uberblock goes to a different slot
from the one the pool is using.
Source
vdev_uberblock_sync, vdev_label.c
vdev_uberblock_sync
Excerpt of module/zfs/vdev_label.c, lines
1782–1784, OpenZFS zfs-2.4.4.
View on GitHub
int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
int n = (ub->ub_txg - (RRSS_GET_STATE(ub) == RRSS_SCRATCH_VALID)) %
(VDEV_UBERBLOCK_COUNT(vd) - m);-
L1782–L1784
The slot is
ub_txgmodulo the number of slots: 128 on disks with 512-byte sectors, 32 on 4K sectors. Multihost keeps one slot back. An uberblock that updates the current txg during a RAID-Z expansion takes the slot before, still not the one in use. The same slot is written in all four labels.
The two cases of a cut
The comment on the uberblock step names the two cases that the figure’s import replays.
Source
vdev_config_sync, the uberblock step
vdev_config_sync
Excerpt of module/zfs/vdev_label.c, lines
2118–2131, OpenZFS zfs-2.4.4.
View on GitHub
/*
* Sync the uberblocks to all vdevs in svd[].
* If the system dies in the middle of this step, there are two cases
* to consider, and the on-disk state is consistent either way:
*
* (1) If none of the new uberblocks made it to disk, then the
* previous uberblock will be the newest, and the odd labels
* (which had not yet been touched) will be valid with respect
* to that uberblock.
*
* (2) If one or more new uberblocks made it to disk, then they
* will be the newest, and the even labels (which had all
* been successfully committed) will be valid with respect
* to the new uberblocks.- L2123–L2126 No new uberblock landed: the previous one wins. The even labels are newer than it, and the import reads the newest label not newer than the uberblock, an odd one.
- L2128–L2131 One or more landed: the new uberblock wins, and the even labels written just before match it.
The import keeps the newest valid uberblock
On import, vdev_uberblock_load_done runs once for every slot of every label. A slot that fails its
checksum or has the wrong magic number is skipped. Among the others, the highest txg wins, then the latest
timestamp.
Source
vdev_uberblock_load_done, vdev_label.c
vdev_uberblock_load_done
Excerpt of module/zfs/vdev_label.c, lines
1547–1553, OpenZFS zfs-2.4.4.
View on GitHub
if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
mutex_enter(&rio->io_lock);
if (vdev_uberblock_compare(ub, &cbp->ubl_latest) > 0) {
cbp->ubl_latest = *ub;
}
if (ub->ub_txg <= spa->spa_load_max_txg &&
vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {-
L1547
A slot counts only if its read succeeded, and the read checks the slot’s own checksum, so a torn write is skipped.
uberblock_verifychecks the magic number. The function runs for every slot of every label. -
L1552–L1553
The best uberblock so far is replaced when
vdev_uberblock_compareranks the new one higher.spa_load_max_txgis lowered only when an import rewinds to an older txg.
Source
What the figure leaves out
The figure shows one file and very few blocks: two pointers per indirect block instead of up to 1024. It folds the path from the MOS to a filesystem’s own objset into one block of dnodes. In the code, the writes of one level overlap, and a child counts as ready once it has its address and checksum, before its data reaches the disk. The figure finishes one row per step, and the flush is what puts every block on stable storage. All of this assumes that a disk really writes its cache to stable storage when ZFS asks it to. There is one disk, so every uberblock and every label lands on it, and its rings have the 128 slots of 512-byte sectors. In a pool with more than three top-level vdevs, an ordinary txg writes the uberblock to three of them, chosen at random. The writes of one commit step are issued together in the code. The figure lands them in order and tears the one in flight when the plug is pulled. It leaves out the content of the configuration, retries, multihost, RAID-Z expansion, several sync passes in one txg, and the freeing of old blocks, which the snapshots room shows. The 3D view draws the same state as the 2D one.