Room 3 · Copy-on-write, checksums and self-healing

What happens when I change a block?

Two things can go wrong under a filesystem. The power can fail in the middle of a write and leave a file part old and part new. A disk can also return wrong data without reporting an error: a bit flips, or a write is lost or lands in the wrong place.

When a block changes, ZFS does not write the new version over the old one. The new version goes to free space, and the old one stays where it was. The blocks that point to it are copied the same way, up to the top of the pool. Then a small root record called the uberblock is written last, and it switches the pool to the new version. If the power fails before that write, the pool comes back at the last finished version, with nothing half written. Writes that an application forced to disk with fsync are also kept in a log, the ZIL, and replayed when the pool comes back.

Each pointer also stores a checksum of the block it points to, and every block read from disk is checked against it. So wrong data is caught when it is read. If the pool has redundancy, such as a mirror, ZFS takes the good data from another copy and writes it over the bad one.

1 The new version goes somewhere else

Pick a data block in the figure and step through the change. The new block gets a new address. Its parent holds the pointer to it, so the parent changes as well and moves to a new address. The same happens to the top of the file’s tree. Each version of the pool is numbered by its transaction group (txg), the batch of changes ZFS writes out together every few seconds.

Until the last step, the pool still points to the old tree, and nothing in that tree has been touched. The last step writes the uberblock, and only then does the pool use the new tree. Cut the power before that step to see what the pool finds when it comes back. Take a snapshot first, and the old blocks stay on disk.

Text version of this figure
  1. The tree has an uberblock at the top, a root indirect block R, two indirect blocks I0 and I1, and four data blocks D0 to D3. Each block pointer holds the child’s address, its birth txg and its checksum.
  2. Rewriting a data block writes a new copy at a new address; the old copy stays where it is.
  3. Its parent now needs a new pointer (new address, new checksum), so the parent is copied too, and so on up to R.
  4. Writing the new uberblock to the next slot of the ring is the commit. Before it, a power cut leaves the old tree intact and current; after it, the pool uses the new tree.
  5. A snapshot keeps the tree of its txg: every block it reaches stays on disk. Replaced blocks are freed, unless the snapshot still uses them: then they stay, on the dataset’s deadlist, the list of blocks that only snapshots still hold.

2 The parent holds the checksum

A checksum kept next to the block it protects misses some faults. Say a write never reaches the disk. The place it was meant for still holds older data, and that data still matches the checksum stored beside it. The read looks fine and returns the wrong thing.

ZFS keeps the checksum in the parent, inside the pointer. The parent is checked by its own parent, and so on up to the uberblock. When a block is read from disk, ZFS computes its checksum again and compares it with the one in the pointer. Damage one side of the mirror below, then read the file. ZFS catches the bad copy, reads the other side, and writes the good data back over the bad copy.

Pointers in the parent block: the expected checksums

      Text version of this figure
      1. A file of four blocks, D0 to D3, sits on a two-way mirror: disk A and disk B each hold a full copy. The parent block holds, in each block pointer, the fletcher4 checksum of the block it points to.
      2. Bit rot: one bit of D1 flips on disk A. The disk returns the bytes without error. Recomputing fletcher4 over them gives a different value from the one in the pointer: ECKSUM.
      3. Lost write: disk A acknowledged the write of the new D2 but never performed it. Under copy-on-write that write went to a new address, not over the old D2, so the address still holds whatever was there before: stale bytes of an earlier block, since freed. Those bytes can be consistent with themselves; only the checksum in the parent, which describes the new D2, reveals them.
      4. On a mismatch the mirror reads the other disk. If that copy matches, it is returned to the application and written back over the bad copy: the pool heals itself on a normal read.
      5. If both copies are damaged, the read fails with an error rather than return wrong data. Each mismatch is counted against the disk that returned it.

      In the code

      The figures follow four places in the OpenZFS source. Open a Source note to see the lines, taken from release zfs-2.4.4.

      Writing a block

      zio_write_compress decides whether a write may go over the old block. It may do so only for a block created in the txg being written: no committed uberblock points to such a block yet. Otherwise the pointer is cleared, and the block normally gets a new address.

      Source zio_write_compress, zio.c
      zio_write_compress Excerpt of module/zfs/zio.c, lines 2094–2105, OpenZFS zfs-2.4.4. View on GitHub
      	if (!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) == zio->io_txg &&
      	    BP_GET_PSIZE(bp) == psize &&
      	    pass >= zfs_sync_pass_rewrite) {
      		VERIFY3U(psize, !=, 0);
      		enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
      
      		zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
      		zio->io_flags |= ZIO_FLAG_IO_REWRITE;
      	} else {
      		BP_ZERO(bp);
      		zio->io_pipeline = ZIO_WRITE_PIPELINE;
      	}
      1. L2094–L2101 Writing over the old block is allowed only for a block born in the transaction group being synced (BP_GET_BIRTH(bp) == zio->io_txg), of the same size, from sync pass 2 on (zfs_sync_pass_rewrite). No committed uberblock points to such a block yet.
      2. L2102–L2104 Every other write clears the pointer and takes the full write pipeline. zio_dva_allocate checks that the pointer is empty before it takes a new address. When the write is done, dbuf_write_done frees the old block, or leaves it to a snapshot that still uses it.

      Storing the checksum

      A block pointer, blkptr_t, takes 128 bytes. It holds up to three addresses, for copies of the same block, and one 256-bit checksum. The copies hold the same bytes, so the one checksum checks any of them. The pointer sits inside the parent block, so the parent checks the child.

      Source blkptr_t, spa.h
      blkptr_t Excerpt of include/sys/spa.h, lines 374–382, OpenZFS zfs-2.4.4. View on GitHub
      typedef struct blkptr {
      	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
      	uint64_t	blk_prop;	/* size, compression, type, etc	    */
      	uint64_t	blk_prop2;	/* additional properties	    */
      	uint64_t	blk_pad;	/* Extra space for the future	    */
      	uint64_t	blk_birth_word[2];
      	uint64_t	blk_fill;	/* fill count			    */
      	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
      } blkptr_t;
      1. L375 Up to three addresses for the same block (SPA_DVAS_PER_BP), all covered by the one checksum. For an encrypted block, the third slot holds the encryption salt and part of the IV instead.
      2. L381 The checksum of the block this pointer leads to. The uberblock has no parent, so it carries its own checksum instead.

      Reading a block

      On every read from disk, zio_checksum_error_impl takes the expected checksum from the pointer the read came through. It computes the actual checksum over the bytes the disk returned. If the two differ, that copy is rejected with ECKSUM, and the error counts against that disk in the CKSUM column of zpool status.

      Source zio_checksum_error_impl, zio_checksum.c
      zio_checksum_error_impl Excerpt of module/zfs/zio_checksum.c, lines 501–506, OpenZFS zfs-2.4.4. View on GitHub
      	} else {
      		byteswap = BP_SHOULD_BYTESWAP(bp);
      		expected_cksum = bp->blk_cksum;
      		ci->ci_func[byteswap](abd, size,
      		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
      	}
      1. L503 The expected value comes from the block pointer the reader followed, in the parent.
      2. L504–L505 The actual value is computed over the bytes just read. With checksum=on, the default, the function is fletcher4. The two are compared further down, and each mismatch is counted on the disk.

      Repairing a mirror

      When one side of a mirror fails the check, ZFS reads another side. If that copy is good and the pool is writable, it writes the good data to each side that failed. If no side is good, the read returns an error, and the application never gets the wrong bytes.

      Source vdev_mirror_io_done, vdev_mirror.c
      vdev_mirror_io_done Excerpt of module/zfs/vdev_mirror.c, lines 927–933, OpenZFS zfs-2.4.4. View on GitHub
      			zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
      			    mc->mc_vd, mc->mc_offset,
      			    zio->io_abd, zio->io_size, ZIO_TYPE_WRITE,
      			    zio->io_priority == ZIO_PRIORITY_REBUILD ?
      			    ZIO_PRIORITY_REBUILD : ZIO_PRIORITY_ASYNC_WRITE,
      			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
      			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
      1. L927–L933 The good buffer, zio->io_abd, is written to each damaged side as a repair write, marked ZIO_FLAG_SELF_HEAL when the cause was an unexpected error. This runs only with a good copy and a writable pool (the condition). With no good copy, the read fails.

      What the figures leave out

      The figures show one file, so the tree is short. In a real pool it runs from the uberblock through the MOS (the pool’s own metadata), the dataset and the file’s dnode, the record that points to its block tree. They leave out sync passes, gang blocks, dedup, nopwrite (a write skipped when the new data matches the block it replaces) and encryption. Writes an application forced to disk with fsync also go to the ZIL, the intent log, and are replayed after a crash. The ZIL room shows that. The mirror here always reads disk A first. ZFS picks the side by load. The uberblock ring has 32 slots, as on disks with 4K sectors. The checksums are real fletcher4 values over the demo blocks. Addresses and txg numbers are made up.