Room 7 · Snapshots and clones
Why doesn’t deleting a file give me my space back?
A snapshot keeps every block that existed when it was taken. If the file was already there at your last snapshot, deleting it only takes it out of the live filesystem: its blocks stay on disk for the snapshot, and the space comes back when the last snapshot that holds them is destroyed. Taking a snapshot costs almost nothing, because ZFS never changes a block in place.
ZFS writes in batches called transaction groups, or txgs, numbered in order, and every block records the txg it was written in, its birth txg. When the filesystem lets go of a block, ZFS compares that birth with the txg of its latest snapshot. A block born after it is in no snapshot and is freed. Any other block goes on the filesystem's deadlist, the list of blocks it no longer uses but a snapshot still holds.
The deadlist is split by the txgs of the snapshots, so destroying one frees whole the parts only it held, without checking the blocks one by one. A clone, a writable filesystem made from a snapshot, shares every block with that snapshot at first and uses the same comparison for what it writes later. The snapshot cannot be destroyed while the clone exists.
1 Take a snapshot, change blocks, destroy it
Select B in fs, overwrite it and sync. The line under the buttons says which old blocks were freed
and which were kept for a snapshot, and a ring marks them on the board. Take a snapshot and overwrite B again to
see the same comparison, this time with the new snapshot. Each snapshot says what destroying it would free: destroy
one, sync, and those blocks go. Clone a snapshot and overwrite a block in the clone: the old block stays,
because the snapshot still holds it.
fs has room for four data blocks, A to D, under one root block. The small number on each block is
its birth txg: A8 was born in txg 8. Select any block to see which snapshots hold it. Under the
figure, each sync is listed step by step, with the two numbers it compared.
fs@s1 was taken in txg 9, then A was overwritten in txg 10. The old A8 and the old root R8 are hatched: fs no longer uses them, but fs@s1 does, so they are kept.
Open txg 11. Snapshots: fs@s1. 6 blocks on disk.
fs
head, writableds_bp →R10A10B8C8D: emptydeadlist- > 0empty
- > 1A8R8
fs@s1
snapshot of txg 9ds_bp →R8A8B8C8D: emptydeadlist- > 0empty
Destroying it frees these blocks (2): A8R8
Which snapshots hold this block?
Select a block to see when it was born and who holds it.
What the sync did
Nothing synced yet: press Sync txg 11.
Text version of this figure
The open txg is 11. The datasets, in order:
fs, head, writable: latest snapshot:fs@s1, txg 9; it points to the R10, the root, born in txg 10; data blocks: A10, born in txg 10, B8, born in txg 8, C8, born in txg 8, D: empty. Deadlist: entry > 0: empty; entry > 1: A8, born in txg 8 and R8, the root, born in txg 8.fs@s1, snapshot of txg 9: snapshot before it: the pool’s start, txg 1; it points to the R8, the root, born in txg 8; data blocks: A8, born in txg 8, B8, born in txg 8, C8, born in txg 8, D: empty. Deadlist: entry > 0: empty. Destroying it frees these blocks (2): A8, born in txg 8 and R8, the root, born in txg 8.
Freed so far: R7, the root, born in txg 7 (in txg 8).
In the code
The figure follows five places in the code of datasets and deadlists. Open a Source note to see the lines, taken from release zfs-2.4.4.
Free it, or keep it for a snapshot
Every block a filesystem lets go of goes through dsl_dataset_block_kill, in the txg being
written. One test makes the whole decision: the block's birth against ds_prev_snap_txg, the
txg of the filesystem's latest snapshot.
Source
dsl_dataset_block_kill, dsl_dataset.c
dsl_dataset_block_kill
Excerpt of module/zfs/dsl_dataset.c, lines
294–294, OpenZFS zfs-2.4.4.
View on GitHub
if (BP_GET_BIRTH(bp) > dsl_dataset_phys(ds)->ds_prev_snap_txg) {-
L294
Born after the latest snapshot: freed in this txg. Otherwise the
elseputs it on the deadlist. The birth was set when the block was allocated. The test is strict: a block born in the txg of a snapshot is in that snapshot, because a txg writes its data before it takes its snapshots.
A snapshot copies one block pointer
dsl_dataset_snapshot_sync_impl creates the snapshot at the end of a txg. It writes one new record
for it, and the only thing it takes from the tree of blocks is the pointer to its root.
Source
dsl_dataset_snapshot_sync_impl, dsl_dataset.c
dsl_dataset_snapshot_sync_impl
Excerpt of module/zfs/dsl_dataset.c, lines
1782–1784, OpenZFS zfs-2.4.4.
View on GitHub
rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
rrw_exit(&ds->ds_bp_rwlock, FTAG);-
L1783
The snapshot's
ds_bp, the pointer to the root of the tree, is the filesystem's. Nothing below it is read or written. The snapshot also gets this txg as its creation txg and the filesystem's deadlist as it is. The filesystem starts a new, empty deadlist with one more key and records this txg as its latest snapshot. A second snapshot in the same txg is refused.
Where a kept block goes
A deadlist is split into entries, each keyed by the txg of a snapshot. dsl_deadlist_insert puts a
block in the entry with the largest key below its birth, so the entry says between which two snapshots the
block was born.
Source
dsl_deadlist_insert, dsl_deadlist.c
dsl_deadlist_insert
Excerpt of module/zfs/dsl_deadlist.c, lines
487–492, OpenZFS zfs-2.4.4.
View on GitHub
dle_tofind.dle_mintxg = BP_GET_BIRTH(bp);
dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
if (dle == NULL)
dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
else
dle = AVL_PREV(&dl->dl_tree, dle);- L487–L488 The block's birth is looked up among the keys, which are stored on disk. A key “is not inclusive”: the entry > 9 holds blocks born after txg 9.
- L489–L492 With no exact match, it takes the nearest key below. With an exact match, it takes the key before that one. Either way, it is the largest key strictly below the birth.
Destroying a snapshot
dsl_destroy_snapshot_sync_impl uses those keys. The blocks that only the destroyed snapshot held
were born after the snapshot before it and let go of before the next snapshot was taken, so they fill whole entries of the
next dataset's deadlist.
Source
dsl_destroy_snapshot_sync_impl, dsl_destroy.c
dsl_destroy_snapshot_sync_impl
Excerpt of module/zfs/dsl_destroy.c, lines
415–424, OpenZFS zfs-2.4.4.
View on GitHub
/* Move blocks to be freed to pool's free list. */
dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
&dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
tx);
dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
DD_USED_HEAD, used, comp, uncomp, tx);
/* Merge our deadlist into next's and free it. */
dsl_deadlist_merge(&ds_next->ds_deadlist,
dsl_dataset_phys(ds)->ds_deadlist_obj, tx);-
L415–L418
Every entry of the next deadlist at or above the snapshot's
ds_prev_snap_txgmoves to the pool's free list, which is freed in later txgs. No block pointer is read to decide it. The figure frees those blocks at once. - L419–L420 Space accounting, not drawn.
- L422–L424 The snapshot's own deadlist, blocks the snapshot before it still holds, joins the next one, entry by entry. Then its creation txg is removed as a key from the filesystem's deadlist, when the next dataset is a snapshot.
A clone starts from a snapshot
dsl_dataset_create_sync_dd, given an origin snapshot, makes a clone the way a snapshot is made: by
copying one block pointer. It also gives the clone the origin as its latest snapshot, so the same test as above
keeps what the clone shares and frees what it wrote.
Source
dsl_dataset_create_sync_dd, dsl_dataset.c
dsl_dataset_create_sync_dd
Excerpt of module/zfs/dsl_dataset.c, lines
1213–1227, OpenZFS zfs-2.4.4.
View on GitHub
} else {
dsl_dataset_t *ohds; /* head of the origin snapshot */
dsphys->ds_prev_snap_obj = origin->ds_object;
dsphys->ds_prev_snap_txg =
dsl_dataset_phys(origin)->ds_creation_txg;
dsphys->ds_referenced_bytes =
dsl_dataset_phys(origin)->ds_referenced_bytes;
dsphys->ds_compressed_bytes =
dsl_dataset_phys(origin)->ds_compressed_bytes;
dsphys->ds_uncompressed_bytes =
dsl_dataset_phys(origin)->ds_uncompressed_bytes;
rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
rrw_exit(&origin->ds_bp_rwlock, FTAG);- L1216–L1218 The clone's latest snapshot is its origin, with the origin's creation txg. A block born at or before it is shared with the origin and goes on the clone's deadlist; a block the clone wrote is freed.
-
L1225–L1227
Its
ds_bpis the origin's. The origin gains a child. With the dataset after it, that makes two, and a snapshot with more than one child cannot be destroyed,EEXIST.
Source
What the figure leaves out
The figure runs the functions above over one filesystem with room for four data blocks and up to four snapshots, and one
clone that cannot itself be snapshotted or destroyed. The root block stands for the objset, the dnodes and the
indirect blocks, which are let go of by the same rule. Gang blocks, dedup blocks and blocks shared by block cloning, which ZFS frees later
rather than at once, are freed at once here, and so are the blocks of a destroyed snapshot. Space accounting
(the used and referenced sizes) is not drawn, nor is the clone's livelist, a list of the blocks it wrote that
makes destroying a clone fast; neither changes whether a block is freed or kept. Holds, deferred destroy, pools
older than keyed deadlists, bookmarks and zfs promote, which swaps a clone with the filesystem of
its origin, are left out.