Room 6 · RAID-Z

What happens when a disk in my RAID-Z fails?

A RAID-5 array cuts its disks into fixed stripes, rows that run across all the disks. Each stripe holds some data and its parity, extra blocks computed from that data, so that any one missing piece can be rebuilt from the others. To change part of a stripe, the array writes the new data and then the new parity, in place. If the power fails between those two writes, the parity no longer matches the data, and nothing records that. The damage shows much later, when a disk dies and its contents are rebuilt from that parity: the rebuilt data is wrong, and no error is reported. This is called the write hole.

When data changes, RAID-Z never updates a stripe in place. Each block ZFS writes gets a stripe of its own, cut across as many disks as the block needs, with parity computed from that block alone. The smallest block takes one disk for its data and one more for each level of parity. A large block takes them all. Like every write in ZFS, the stripe goes to free space, and the old version of the block stays where it was until the pool has switched to the new one. A power failure can leave a half-written stripe, but nothing points to it.

When a disk dies, ZFS rebuilds each block that had data on it: it reads that block's parity along with the rest and recomputes the missing part. A disk can also return wrong data without reporting an error. Parity alone cannot say which disk that is, but the checksum that ZFS stores with the pointer to each block can. ZFS assumes one disk is wrong, then another, rebuilds the block each time, and keeps the version that matches the checksum. Then it writes the good data back to the disk that returned the wrong data.

1 Each block gets its own stripe

The map shows the disks of a raidz2 group side by side, six of them, which the code calls children. Each row is one sector, the smallest unit a disk writes (4 KiB here). Block A is 24 KiB: its six sectors of data sit in four columns, with two parity columns, P and Q, beside them. Write a 4 KiB block after it and see it take three disks, one for data and two for parity. A 128 KiB block takes all six, over several rows. Change the number of disks or the parity, and every block is laid out again.

The hatched sectors are padding. RAID-Z rounds each block up to a multiple of parity + 1 sectors (3 on a raidz2), so that no gap too small to use is left between two blocks. That is why an 8 KiB block on this group takes 24 KiB on disk, as much as a 16 KiB block.

Then damage the disks beside the map and read the last block back, one step per press. If the missing disk holds a data column, the read asks for parity and rebuilds that column. With a disk that returns bad data, the first checksum fails, ZFS reads every column, and the figure shows each guess in turn until one verifies.

Block A, 24 KiB: 6 data sectors and 4 parity sectors on 6 of the 6 children, plus 2 skip sectors. asize = 48 KiB, 2.00 times psize.

The counters, as vdev_raidz_map_alloc computes them
b
io_offset / 2ashift = 0 / 4096 = 0
s
⌈psize / 2ashift⌉ = ⌈24576 / 4096⌉ = 6
f
b % dcols = 0 % 6 = 0
o
b / dcols = 0 / 6 = 0
q
s / (dcols − nparity) = 6 / 4 = 1
r
s − q × 4 = 6 − 4 = 2
bc
r + nparity = 2 + 2 = 4
tot
s + nparity × (q + 1) = 6 + 2 × 2 = 10
acols
dcols = 6
scols
dcols = 6
rm_nskip
roundup(tot, 3) − tot = 12 − 10 = 2
asize
roundup(6 + 2 × ⌈6 / 4⌉, 3) = 12 × 4096 = 49152 (48 KiB)
Text version of this figure

Block A, 24 KiB: 6 data sectors and 4 parity sectors on 6 of the 6 children, plus 2 skip sectors. asize = 48 KiB, 2.00 times psize.

  1. Column 0, P, on child 0 from sector 0: 2 parity sectors.
  2. Column 1, Q, on child 1 from sector 0: 2 parity sectors.
  3. Column 2, D0, on child 2 from sector 0: sectors 0 and 1 of the block.
  4. Column 3, D1, on child 3 from sector 0: sectors 2 and 3 of the block.
  5. Column 4, D2, on child 4 at sector 0: sector 4 of the block. Then one skip sector, sector 1 of that child.
  6. Column 5, D3, on child 5 at sector 0: sector 5 of the block. Then one skip sector, sector 1 of that child.

2 No write hole

Step through both sides at once. On the left, RAID-5 writes D1′ over D1 and the power fails before the new parity is written. Nothing looks wrong until disk 0 dies and D0 is rebuilt from the stale parity. On the right, RAID-Z writes the new version to free space. After the reboot the old block is whole, and when a disk dies its part is rebuilt correctly.

The RAID-Z code only does half of this. When a block changes, it writes the new version as a whole new stripe and never reads or modifies the old one. The other half is copy-on-write and the transaction group commit, which make sure no pointer reaches a stripe before all of it is on disk.

RAID-5: the stripe is updated in place

  1. Before. D1 shares a stripe with D0 and D2. P is the parity of all three.

  2. The power fails. D1′ is written over D1. P′ has to be written too, and the power fails before it is.

  3. After the reboot. P still holds the parity of the old D1, so it no longer matches D0, D1′ and D2. Nothing on disk records that.

  4. A disk dies. Disk 0 dies. D0 is rebuilt from P, D1′ and D2, and comes out wrong. No error is reported.

RAID-Z: the new version gets a new stripe

  1. Before. The block, A0 and A1 with their parity PA, has a stripe of its own.

  2. The power fails. The new version, B0 and B1 with PB, goes to free space, with parity computed from it alone. The power fails before B1 is written.

  3. After the reboot. The last committed uberblock still points to the old block, which nothing has touched. Nothing points to the half-written stripe, and its space was never recorded as used, so it is still free.

  4. A disk dies. Disk 2 dies. A1 is rebuilt from PA and A0, and it is right, because PA was written with A0 and A1 and has not changed since.

In the code

The figures follow five places in vdev_raidz.c. Open a Source note to see the lines, taken from release zfs-2.4.4.

Cutting a block into columns

vdev_raidz_map_alloc builds the map of one block. It counts the block's sectors, divides them among the data columns, and makes the parity columns as long as the longest data column. Nothing in the arithmetic looks at any other block.

Source vdev_raidz_map_alloc, vdev_raidz.c
vdev_raidz_map_alloc Excerpt of module/zfs/vdev_raidz.c, lines 634–649, OpenZFS zfs-2.4.4. View on GitHub
	uint64_t q = s / (dcols - nparity);

	/*
	 * "Remainder": The number of partial stripe data sectors in this I/O.
	 * This will add a sector to some, but not all, child vdevs.
	 */
	uint64_t r = s - q * (dcols - nparity);

	/* The number of "big columns" - those which contain remainder data. */
	uint64_t bc = (r == 0 ? 0 : r + nparity);

	/*
	 * The total number of data and parity sectors associated with
	 * this I/O.
	 */
	uint64_t tot = s + nparity * (q + (r == 0 ? 0 : 1));
  1. L634–L640 s is the block's size in sectors, already padded to a whole sector. q full rows of data, and r sectors left over.
  2. L643 The bc "big columns" get one sector more: the r columns that hold the remainder, and the parity columns, which come first (P, Q, R).
  3. L649 tot, data plus parity. vdev_raidz_io_start_write computes the parity of this map and sends every column to its disk. Nothing old is read.

Rounding up the space

Before the map exists, the allocator reserves space with vdev_raidz_psize_to_asize. It adds one set of parity sectors for each row of data, then rounds up to a multiple of parity + 1 sectors. The extra sectors are the padding in the figure.

Source vdev_raidz_psize_to_asize, vdev_raidz.c
vdev_raidz_psize_to_asize Excerpt of module/zfs/vdev_raidz.c, lines 2315–2317, OpenZFS zfs-2.4.4. View on GitHub
	asize = ((psize - 1) >> ashift) + 1;
	asize += nparity * ((asize + cols - nparity - 1) / (cols - nparity));
	asize = roundup(asize, nparity + 1) << ashift;
  1. L2315–L2316 Data sectors, then nparity parity sectors for each row of cols - nparity data sectors that is started.
  2. L2317 Rounded up to a multiple of nparity + 1. The map calls the difference skip sectors and writes them as zeros. A one-sector block costs nparity + 1 sectors whatever the width.

Reading the data columns only

A normal read of a healthy block asks for its data columns and nothing else. Parity is read only when a data column is missing, or by a scrub (a check of every block in the pool) or a resilver (the rebuild of a disk that missed writes). So a disk that holds bad parity goes unnoticed until one of those reads it.

Source vdev_raidz_io_start_read_row, vdev_raidz.c
vdev_raidz_io_start_read_row Excerpt of module/zfs/vdev_raidz.c, lines 2573–2588, OpenZFS zfs-2.4.4. View on GitHub
	for (int c = rr->rr_cols - 1; c >= 0; c--) {
		raidz_col_t *rc = &rr->rr_col[c];
		vdev_t *cvd = vd->vdev_child[rc->rc_devidx];

		if (rc->rc_error || rc->rc_size == 0)
			continue;

		if (forceparity ||
		    c >= rr->rr_firstdatacol || rr->rr_missingdata > 0 ||
		    (zio->io_flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) {
			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
			    rc->rc_offset, rc->rc_abd, rc->rc_size,
			    zio->io_type, zio->io_priority, 0,
			    vdev_raidz_child_done, rc));
		}
	}
  1. L2577–L2578 A column on a child that cannot be read is skipped. It was marked just above.
  2. L2580–L2582 Parity columns are below rr_firstdatacol. They are read only with a data column missing (rr_missingdata), for a scrub or resilver, or when parity is forced. The missing data is then rebuilt from the first valid parity, P before Q before R (the choice).

Finding the disk that lies

Every read ends by checking the block against the checksum stored in its parent's block pointer. When it fails and no disk reported an error, ZFS reads every column and calls vdev_raidz_combrec. The function assumes one child is wrong, then two, up to the parity count, and tries each combination in turn.

Source vdev_raidz_combrec, vdev_raidz.c
vdev_raidz_combrec Excerpt of module/zfs/vdev_raidz.c, lines 3542–3558, OpenZFS zfs-2.4.4. View on GitHub
		ltgts[-1] = -1;
		for (int i = 0; i < num_failures; i++) {
			ltgts[i] = i;
		}
		ltgts[num_failures] = n;

		for (;;) {
			int err = raidz_reconstruct(zio, ltgts, num_failures,
			    nparity);
			if (err == EINVAL) {
				/*
				 * Reconstruction not possible with this #
				 * failures; try more failures.
				 */
				break;
			} else if (err == 0)
				return (0);
  1. L3542–L3546 Reached after every column has been read. The first combination: children 0 to num_failures - 1, inside a loop from 1 failure to nparity. The next ones follow the order the comment above lists.
  2. L3549–L3556 raidz_reconstruct rebuilds those children's data from parity and checks the checksum again. EINVAL means more dead columns than parity can rebuild (the test), so it tries one more failure.
  3. L3557–L3558 The checksum verifies. The columns found wrong are counted as checksum errors and written back with the good data.

Resilvering only what is needed

When a disk comes back after missing some writes, a resilver does not copy the whole disk. It walks the tree of blocks, skips those written while every disk was present, and asks the vdev whether each remaining block touches a disk that missed writes. The figure answers that question for the last block when a disk is set to missing.

Source vdev_raidz_need_resilver, vdev_raidz.c
vdev_raidz_need_resilver Excerpt of module/zfs/vdev_raidz.c, lines 4018–4035, OpenZFS zfs-2.4.4. View on GitHub
	if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1))
		return (B_FALSE);

	if (s + nparity >= dcols)
		return (B_TRUE);

	for (uint64_t c = 0; c < s + nparity; c++) {
		uint64_t devidx = (f + c) % dcols;
		vdev_t *cvd = vd->vdev_child[devidx];

		/*
		 * dsl_scan_need_resilver() already checked vd with
		 * vdev_dtl_contains(). So here just check cvd with
		 * vdev_dtl_empty(), cheaper and a good approximation.
		 */
		if (!vdev_dtl_empty(cvd, DTL_PARTIAL))
			return (B_TRUE);
	}
  1. L4018–L4019 The block was not written while a child was missing writes: nothing to do. The scan has already skipped blocks born outside that window, then asked this question for the rest.
  2. L4021–L4022 A block with at least as many columns as children touches every child.
  3. L4024–L4035 Otherwise, only if one of the s + nparity children from the first column on missed writes.

What the figures leave out

The map runs the arithmetic of vdev_raidz_map_alloc and vdev_raidz_psize_to_asize as the code writes it, so every layout and every number is the code's. It places each block right after the previous one. The allocator puts blocks wherever a metaslab has room. In raidz1, parity and the first data column swap disks in every other megabyte of the vdev. Set the first sector to 256 with 4 KiB sectors to see it. The read follows the code's decisions (which columns are read, which parity rebuilds what, the order of the guesses, what is written back), but tracks only whether each column is right or wrong, not its bytes. It leaves out disks that missed writes (read as stale, not missing), slow disks skipped on purpose, gang blocks, the scrub path and dRAID. Blocks stop at 128 KiB and groups at 16 disks, so the map stays legible. RAID-Z expansion, which adds a disk to an existing group and gives older blocks maps of several rows, is left for another time.