Room 10 · ZIL and SLOG

What does fsync() wait for?

A program that calls fsync on a file asks for one promise: when the call returns, the data it wrote survives a power cut. A database asks for it on every commit. ZFS writes changes to the pool in batches called transaction groups, or txgs, and commits a txg only every few seconds, after rewriting every block on the path from the change up to the root of the pool. Waiting for that on every fsync would be slow, and would write far more than the program changed.

So each dataset also keeps an intent log, the ZIL. Every write leaves a small record of itself in memory: which file, where, how long, and often the data. When the program calls fsync, ZFS writes that file's records at once to a log block on disk, waits for the disk to flush its cache, and returns. The txg still writes the change to the pool later, as usual, and once it has, the log block is no longer needed and is freed. On a pool without a separate log device, a large write is not copied into the log: its data is written once, in place in the pool, and the record only points to it.

The log is read only after a crash. When the pool is imported again, ZFS walks the chain of log blocks and keeps the pool from reusing them. When the dataset is mounted, it applies again every record whose txg never reached the pool, so every write that fsync returned for comes back. A write that was never synced or logged is lost, which is what its program accepted when it did not call fsync. A separate log device, called a SLOG, gives the log blocks a device of their own; what it gains depends on how fast that device is.

1 Write, fsync, cut the power

The figure is a new pool with one file whose blocks are 128 KiB. Txg 100 is open and the dataset has no log yet. Press write(), then fsync(), then Next step until fsync() returns. Each press shows one step: the line at the bottom of the figure says what it did, and a ring marks the part that changed. This first fsync() creates the log, which takes a txg sync, so it logs nothing: the write is already in the pool when the call returns.

Then press write() and fsync() again. This time the record goes into a log block, and fsync() returns while txg 101 is still open. Cut the power, import the pool and mount the dataset, and the record is replayed. Start again, repeat both pairs of write() and fsync(), and press Sync txg 101 before the power cut: the log has nothing left to replay. A write() made after the last fsync() and before the cut is lost.

The panel at the top says what the next write() will put in its record. Try 128 KiB without a SLOG and then with one, on a dataset that already has a log: after Start again or a replay, the next fsync() creates the log again and logs nothing. Without a SLOG, the data is written in place and the record only points to it. With one, all the data goes into the log, split across two log blocks. With logbias=throughput the data is always written in place.

Txg 100 open, txg 99 synced. 0 writes: none. Log: 0 blocks in the chain, the header points to nothing.

What the next write() puts in its record

WR_NEED_COPY 4 KiB

  1. 4 KiB is under 32 KiB, too small to be worth writing in place.
  2. Not committed at once: WR_NEED_COPY, the data is copied into the log block only when the record is written.

This write() returns at once; fsync() commits it.

The application’s file

No write yet.

In memory

Records waiting for a commit (itxs)

None.

Log blocks being built (lwbs)

No log yet: the first commit creates it.

On disk

The log’s header, in the dataset

Chain starts at
nothing
Claimed in txg
not claimed
Replay needed
no
Replayed up to record
0

The chain of log blocks

Empty: there is no log.

Pool vdevs: space set aside for the log, and file data

  • nothing
  • WR_COPIED data copied into the record
  • WR_NEED_COPY data copied into the log block when it is filled
  • WR_INDIRECT data written in place, the record points to it
  • log block written
  • allocated, not written yet
  • freed
Text version of this figure

A pool without a SLOG, logbias=latency. A file with 128 KiB blocks. Open txg 100, last synced 99.

  • Writes: none.
  • Records in memory, waiting for a commit: none.
  • Log blocks being built: none.
  • On disk, the chain: empty.
  • Header: the chain starts at nothing, claimed in txg 0, replay needed: no.

Txg 100 open, txg 99 synced. 0 writes: none. Log: 0 blocks in the chain, the header points to nothing.

In the code

The figure follows nine places in the source. Open a Source note to see the lines, taken from release zfs-2.4.4.

A record for each write

zfs_log_write runs on every write. It fills a record, an itx, with what is needed to apply the write again: which file, where, how long, and how the data travels. Nothing is written to disk yet.

Source zfs_log_write, zfs_log.c
zfs_log_write Excerpt of module/zfs/zfs_log.c, lines 677–682, OpenZFS zfs-2.4.4. View on GitHub
		itx->itx_wr_state = wr_state;
		lr->lr_foid = zp->z_id;
		lr->lr_offset = off;
		lr->lr_length = len;
		lr->lr_blkoff = 0;
		BP_ZERO(&lr->lr_blkptr);
  1. L677 How the data travels: copied into the record, copied into the log block later, or written in place, as the next note decides.
  2. L678–L680 The file, the offset and the length: enough to apply the write again after a crash.
  3. L681–L682 No block pointer yet: a record whose data is written in place gets one when that block is written. The record is then queued under the write's txg.

In the record, or in place

zil_write_state decides how much of the data the record carries. A large block written once, in place, costs less than the same data written to the log and then to the pool, except when the pool has a SLOG.

Source zil_write_state, zil.c
zil_write_state Excerpt of module/zfs/zil.c, lines 2227–2245, OpenZFS zfs-2.4.4. View on GitHub
	boolean_t indirect = (size >= zfs_immediate_write_sz &&
	    (size >= blocksize / 2 || !commit));

	if (spa_has_slogs(zilog->zl_spa)) {
		/* Dedicated slogs: never use indirect */
		indirect = B_FALSE;
	} else if (spa_has_special(zilog->zl_spa)) {
		/* Special vdevs: only when beneficial */
		boolean_t on_special = (blocksize <=
		    zilog->zl_os->os_zpl_special_smallblock);
		indirect &= (on_special || !zil_special_is_slog);
	}

	if (indirect)
		return (WR_INDIRECT);
	else if (commit)
		return (WR_COPIED);
	else
		return (WR_NEED_COPY);
  1. L2227–L2228 In place only from 32 KiB and, for a write committed at once (O_DSYNC), only from half a block, since the next write might rewrite the same block. Before this test, logbias=throughput or Direct I/O always returns WR_INDIRECT.
  2. L2230–L2232 A pool with a SLOG never writes in place. spa_has_slogs asks whether the log class has a device, not whether it has room: a full SLOG still counts.
  3. L2233–L2238 Special vdevs without a SLOG. The figure's pool has none.
  4. L2240–L2245 WR_INDIRECT, written in place; WR_COPIED, copied into the record now, for a write committed at once; otherwise WR_NEED_COPY, copied into the log block when it is filled. A WR_COPIED write over 7,680 bytes becomes WR_NEED_COPY, since a copied record must fit in one log block.

fsync() waits for one log block

fsync() calls zil_commit, and zil_commit_impl does the work: it writes the file's records and sleeps until the block that holds the last of them is on stable storage.

Source zil_commit_impl, zil.c
zil_commit_impl Excerpt of module/zfs/zil.c, lines 4097–4101, OpenZFS zfs-2.4.4. View on GitHub
	zil_commit_waiter_t *zcw = zil_alloc_commit_waiter();
	zil_commit_itx_assign(zilog, zcw);

	uint64_t wtxg = zil_commit_writer(zilog, zcw);
	zil_commit_waiter(zilog, zcw);
  1. L4097–L4098 A commit waiter is queued behind every record of the file.
  2. L4100 The records are packed into lwbs, log write blocks, and the lwbs are issued. A dataset with no log creates one first, and waits for a txg to sync so that the header can name its first block: the first commit on a new log costs a txg.
  3. L4101 The caller sleeps until the lwb that holds its waiter is written and the disk caches are flushed.

The seven states of an lwb

An lwb is a log block being built in memory. The figure marks its state on each lwb, in the order of lwb_state_t.

Source lwb_state_t, zil_impl.h
lwb_state_t Excerpt of include/sys/zil_impl.h, lines 83–92, OpenZFS zfs-2.4.4. View on GitHub
typedef enum {
    LWB_STATE_NEW,
    LWB_STATE_OPENED,
    LWB_STATE_CLOSED,
    LWB_STATE_READY,
    LWB_STATE_ISSUED,
    LWB_STATE_WRITE_DONE,
    LWB_STATE_FLUSH_DONE,
    LWB_NUM_STATES
} lwb_state_t;
  1. L84–L85 New, then opened by its first record.
  2. L86 Closed when full, or at the end of a commit, which also creates the next lwb, still empty.
  3. L87–L88 Ready once its records are filled in; issued once the next block is allocated.
  4. L89–L90 Write done, then flush done once the disk caches are flushed: the commits waiting on it return.

Each block names the next

Before an lwb is written, zil_lwb_write_issue allocates the block of the next lwb and puts its address in this block's trailer. The log is a chain that starts at the header.

Source zil_lwb_write_issue, zil.c
zil_lwb_write_issue Excerpt of module/zfs/zil.c, lines 2086–2092, OpenZFS zfs-2.4.4. View on GitHub
	if (error == 0) {
		ASSERT3U(BP_GET_BIRTH(bp), ==, txg);
		BP_SET_CHECKSUM(bp, (nlwb->lwb_flags & LWB_FLAG_SLIM) ?
		    ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
		bp->blk_cksum = lwb->lwb_blk.blk_cksum;
		bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
	}
  1. L2087 The next block, whose pointer goes into this block's trailer, is born in an open txg.
  2. L2088–L2091 It takes this block's checksum seed, with the sequence word incremented. After a crash, the next block must match that checksum. The block that was allocated but never written does not, and ends the chain.

The SLOG first

zio_alloc_zil allocates every log block, the first one of a chain included. It tries the SLOG first.

Source zio_alloc_zil, zio.c
zio_alloc_zil Excerpt of module/zfs/zio.c, lines 4561–4565, OpenZFS zfs-2.4.4. View on GitHub
	/* Try log class (dedicated slog devices) first */
	error = metaslab_alloc_range(spa, spa_log_class(spa), min_size,
	    max_size, new_bp, 1, txg, NULL, flags, &io_alloc_list, allocator,
	    NULL, &alloc_size);
	*slog = (error == 0);
  1. L4561–L4564 The log class, the SLOG devices, first.
  2. L4565 slog records whether that worked; it becomes LWB_FLAG_SLOG on the lwb. Otherwise the function tries, in order, the log space on special vdevs, the special vdevs, the log space every normal vdev sets aside for the ZIL, then the normal vdevs. logbias does not change this order: with throughput the data is written in place, but the log block still comes from the SLOG.

When the txg syncs, the log is freed

Once a txg has synced, the records of its writes are no longer needed. zil_sync runs during that sync and frees log blocks from the head of the chain.

Source zil_sync, zil.c
zil_sync Excerpt of module/zfs/zil.c, lines 4206–4214, OpenZFS zfs-2.4.4. View on GitHub
	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
		zh->zh_log = lwb->lwb_blk;
		if (lwb->lwb_state != LWB_STATE_FLUSH_DONE ||
		    lwb->lwb_alloc_txg > txg || lwb->lwb_max_txg > txg)
			break;
		list_remove(&zilog->zl_lwb_list, lwb);
		if (!BP_IS_HOLE(&lwb->lwb_blk))
			zio_free(spa, txg, &lwb->lwb_blk);
		zil_free_lwb(zilog, lwb);
  1. L4207 The header's zh_log, where the chain starts, follows the head of the list.
  2. L4208–L4210 An lwb stays if it is not flushed yet, or if its block or one of its records belongs to a later txg. The first one that stays stops the loop.
  3. L4211–L4214 Otherwise its block is freed. Records of the synced txg that never reached a log block are dropped from memory.

After a power cut, the import claims the chain

At import, in the pool's first txg, zil_claim walks each dataset's chain. It only marks the blocks and counts the records; nothing is replayed yet.

Source zil_claim, zil.c
zil_claim Excerpt of module/zfs/zil.c, lines 1239–1246, OpenZFS zfs-2.4.4. View on GitHub
	if (zh->zh_claim_txg == 0) {
		(void) zil_parse(zilog, zil_claim_log_block,
		    zil_claim_log_record, tx, first_txg, B_FALSE);
		zh->zh_claim_txg = first_txg;
		zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
		zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
		if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
			zh->zh_flags |= ZIL_REPLAY_NEEDED;
  1. L1239–L1241 zil_parse walks the chain from zh_log and claims each block, so that the allocator does not hand it out again, and the blocks that WR_INDIRECT records point to.
  2. L1242 The claim txg: the pool's first txg, the last synced one plus one.
  3. L1243–L1244 The highest block and record numbers claimed.
  4. L1245–L1246 ZIL_REPLAY_NEEDED if a record was found, or more than one block followed. The unwritten last block counts, so a log with nothing in it counts one.

The mount replays what the txg did not keep

When the dataset is mounted, zil_replay walks the chain again and passes each record to zil_replay_log_record, which skips what is already in the pool.

Source zil_replay_log_record, zil.c
zil_replay_log_record Excerpt of module/zfs/zil.c, lines 4708–4712, OpenZFS zfs-2.4.4. View on GitHub
	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
		return (0);

	if (lr->lrc_txg < claim_txg)		/* already committed */
		return (0);
  1. L4708–L4709 A record already replayed by an earlier, interrupted replay is skipped. The mount calls zil_replay.
  2. L4711–L4712 A record of a txg older than the claim txg is skipped: that txg synced, so its change is in the pool. Every other record is applied again, then the log is destroyed. With nothing to replay, zil_replay only destroys the log and keeps its first block, which the next commit writes into without waiting for a txg.

What the figure leaves out

The figure runs the functions above on one file of one dataset, with one writer and one txg in flight, synced at once in one pass. The power is cut between two actions, never in the middle of one. The file already spans several records, so its blocks are 128 KiB; a file of a single block smaller than recordsize has a smaller block, and the half-block rule uses it. The first block of a new chain is 4 KiB, as in the code; every later log block is 128 KiB, where the code sizes each block from the burst of records (zil_lwb_plan). So in a real chain records split at other places, and the byte counts in the figure are those of 128 KiB blocks. Each write() starts at the beginning of a file block. There is no I/O error, so the commit never falls back to waiting for a txg sync; the flush is one step for the whole lwb, without the per-vdev flush tree; the commit timeout never fires, because one writer closes its lwb at once. Writes of more than 64 MiB per commit (zil_slog_bulk) go to a SLOG at a lower priority; the Slim ZIL layout and encryption are not drawn. None of these changes how a write is logged, where a log block goes, or which writes are replayed. Transaction groups explains when a txg syncs, and Metaslabs and space maps how the classes share the pool's space.