Room 5 · The vdevs

Where does ZFS put a block in my pool?

A pool is built from vdevs, short for virtual devices: mirrors, RAID-Z groups, dRAID groups or single disks. Beside the vdevs that hold your data, a pool can have a log device for synchronous writes, a special device for metadata and a dedup device for the deduplication table. Any disk can be replaced while the pool runs. For each block it writes, ZFS has to choose one of these vdevs, then get the block onto the disks under it, whatever the layout looks like that day.

ZFS first chooses a class from the kind of block. The vdevs that hold data form the normal class, and each kind of special-purpose device forms a class of its own. Metadata goes to the special device when the pool has one. File data goes there only if the block is no larger than a size you set per dataset, zero by default, and only while the special device is less than three-quarters full. The deduplication table goes to the dedup device, or to the special device when there is no dedup device. The intent log, the ZIL, goes to the log device, or without one to space set aside on the special or normal vdevs. Everything else goes to the normal class.

Then the write goes down a tree. The pool is the root, the vdevs are its children and the disks are theirs. Each node has its own small table of functions that says how an I/O passes on to its children: a mirror sends a write to all its disks and a read to one, and a RAID-Z group cuts the block into data and parity columns, each on a different disk. Replacing a disk adds a node to that tree. The old disk and the new one become the two sides of a temporary mirror, so writes reach both, and reads skip the new disk for blocks it does not have yet. When the resilver, the copy onto the new disk, is done, the old disk and the temporary node go away, and the code that moves reads and writes needed no special case for it.

1 Build a pool and write blocks

The tree is a pool with three vdevs for data (a mirror, a RAID-Z group of three disks and a dRAID of five) and, to start with, a log mirror and a special mirror. Write each kind of block and see where it lands. The line under the buttons says where it went, the vdevs that got a copy are ringed, and its class is marked in the table. Then switch the special mirror off, which rebuilds the pool without it, and write metadata again, set special_small_blocks to 16 KiB and write a small file block, or fill the special class and see the block go to the normal class: when the special or dedup class is full, ZFS chooses again without it, and in the end uses the normal class.

The last block panel lists each step of the choice and the path of the write down the tree. Select a node to see its table of functions. Within a class, ZFS spreads blocks over the vdevs by free space and load. Metadata is stored twice by default, and ZFS starts the second copy on the next vdev of the class: with the special mirror off, a metadata block rings two normal vdevs. A mirror writes to every child and reads from one. A RAID-Z group writes one column per child. A dRAID group writes three columns, each on a different child of the five. The dRAID spreads its groups over all five children through a fixed table of permutations, so two large blocks in a row land on different sets of disks.

Then select a disk and replace it. It becomes the first child of a replacing node, with the new disk beside it. Read the last block back to see the new disk skipped, then press Resilver done. A hot spare makes a spare node instead, and that node stays after the resilver, until the original disk is back or one of the two is detached. On a dRAID child you can use the distributed spare, which is space set aside on every child of the dRAID.

rootrootmirror-0mirror1sdadisk32 sectorssdbdisk32 sectorsraidz1-1raidzsdcdisksdddisksdediskdraid1:2d:5c:1s-2draidsdfdisksdgdisksdhdisksdidisksdjdiskmirror-3mirrorsdkdisksdldiskmirror-4mirror1sdmdisksdndisk

Key

  • The class of a top-level vdev, by its coloured edge: normal log special dedup
  • The path of the last write or read
  • Where the last block landed: a ring, and its count filled
  • Blocks with a copy on this vdev
  • The selected node
  • A replacing or spare node

Allocation classes

ClassTop-level vdevsBlocks
normalmirror-0, raidz1-1, draid1:2d:5c:1s-21
logmirror-30
specialmirror-41
dedupnone0
embedded logmirror-0, raidz1-1, draid1:2d:5c:1s-20
special embedded logmirror-40

The last block

Block 2, a large file block, txg 101: the normal class.

  1. A data block of 128 KiB, larger than special_small_blocks (0): the normal class.
  2. 1 copy: a file’s data block with copies=1. DVA 0 on mirror-0.

The I/O, down the tree

One write per copy (1 copy). Each goes from its top-level vdev down to the disks, and each node passes it on with its own function:

  • DVA 0 mirror-0 mirror vdev_mirror_io_start: to every child
    • sda disk vdev_disk_io_start: 32 sectors
    • sdb disk vdev_disk_io_start: 32 sectors

The selected node

sdd

FieldValue
vdev_ops&vdev_disk_ops
vdev_op_type"disk"
vdev_op_io_startvdev_disk_io_start
vdev_op_io_donevdev_disk_io_done
vdev_op_psize_to_asizevdev_default_asize
vdev_op_need_resilverNULL
vdev_op_leafB_TRUE
vdev_parentraidz1-1
vdev_topraidz1-1
vdev_children0
Metaslab groupsnone: only top-level vdevs are allocated from

Beside the tree

spa_spares, hot spares

  • sdp disk, available
  • draid1-2-0 dspare, available, distributed: space on every child of the dRAID

spa_l2cache, cache devices (L2ARC)

  • sdq disk

Text version of this figure

The tree, from the root:

  • root root
  • mirror-0 mirror, normal
  • sda disk
  • sdb disk
  • raidz1-1 raidz, normal
  • sdc disk
  • sdd disk
  • sde disk
  • draid1:2d:5c:1s-2 draid, normal
  • sdf disk
  • sdg disk
  • sdh disk
  • sdi disk
  • sdj disk
  • mirror-3 mirror, log
  • sdk disk
  • sdl disk
  • mirror-4 mirror, special
  • sdm disk
  • sdn disk

Beside the tree: hot spares sdp and draid1-2-0; cache sdq.

The last block:

Block 2, a large file block, txg 101: the normal class.

  1. A data block of 128 KiB, larger than special_small_blocks (0): the normal class.
  2. 1 copy: a file’s data block with copies=1. DVA 0 on mirror-0.

The I/O, down the tree

One write per copy (1 copy). Each goes from its top-level vdev down to the disks, and each node passes it on with its own function:

  • DVA 0 mirror-0 mirror vdev_mirror_io_start: to every child
    • sda disk vdev_disk_io_start: 32 sectors
    • sdb disk vdev_disk_io_start: 32 sectors

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.

One table of functions per node

Every node of the tree is a vdev_t, and its type is nothing but the table of functions it points to, a vdev_ops_t. The I/O path does not look at the type. It calls the node's vdev_op_io_start and lets it decide how the I/O goes on.

Source vdev_ops_t, vdev_impl.h
vdev_ops_t Excerpt of include/sys/vdev_impl.h, lines 104–113, OpenZFS zfs-2.4.4. View on GitHub
	vdev_open_func_t		*vdev_op_open;
	vdev_close_func_t		*vdev_op_close;
	vdev_asize_func_t		*vdev_op_psize_to_asize;
	vdev_asize_func_t		*vdev_op_asize_to_psize;
	vdev_min_asize_func_t		*vdev_op_min_asize;
	vdev_min_alloc_func_t		*vdev_op_min_alloc;
	vdev_io_start_func_t		*vdev_op_io_start;
	vdev_io_done_func_t		*vdev_op_io_done;
	vdev_state_change_func_t	*vdev_op_state_change;
	vdev_need_resilver_func_t	*vdev_op_need_resilver;
  1. L104–L105 Open and close the device. A mirror's open opens every child.
  2. L106–L109 Size translation: a raidz turns a block's size into a larger allocated size, to leave room for parity.
  3. L110–L111 How an I/O goes on to the children, and how their results come back. The I/O path only calls this pointer. The root has neither: an I/O never starts at the root.
  4. L113 Whether a block touches a child that missed writes. A resilver asks this before reading the block.

A mirror: one child for a read, all of them for a write

vdev_mirror_io_start is the mirror's function. A normal read picks one child and a write takes them all. The replacing and spare nodes use this same function, which is why a replacement needs no code of its own in the I/O path.

Source vdev_mirror_io_start, vdev_mirror.c
vdev_mirror_io_start Excerpt of module/zfs/vdev_mirror.c, lines 656–669, OpenZFS zfs-2.4.4. View on GitHub
		/*
		 * For normal reads just pick one child.
		 */
		c = vdev_mirror_child_select(zio);
		children = (c >= 0);
	} else {
		ASSERT(zio->io_type == ZIO_TYPE_WRITE);

		/*
		 * Writes go to all children.
		 */
		c = 0;
		children = mm->mm_children;
	}
  1. L656–L660 A normal read picks one child: not one that is missing the block's txg, and among the others the least loaded.
  2. L662–L668 A write goes to every child. The loop that follows issues one child I/O per child, at the same offset and size.

Choosing a class

spa_preferred_class chooses the class of every block except the ZIL's. It looks at the kind of block, at which classes the pool has, and at the class it may already have tried.

Source spa_preferred_class, spa_misc.c
spa_preferred_class Excerpt of module/zfs/spa_misc.c, lines 2149–2178, OpenZFS zfs-2.4.4. View on GitHub
	if (DMU_OT_IS_DDT(objtype)) {
		if (spa_has_dedup(spa) && !tried_dedup && !tried_special)
			return (spa_dedup_class(spa));
		else if (spa_special_has_ddt(spa) && !tried_special)
			return (spa_special_class(spa));
		else
			return (spa_normal_class(spa));
	}

	if (!spa_has_special(spa) || tried_special)
		return (spa_normal_class(spa));

	if (DMU_OT_IS_METADATA(objtype) ||
	    (zfs_user_indirect_is_special && zp->zp_level > 0))
		return (spa_special_class(spa));

	/*
	 * Allow small blocks in special class.  However, leave a reserve of
	 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
	 */
	if (zio->io_size <= zp->zp_zpl_smallblk) {
		metaslab_class_t *special = spa_special_class(spa);
		uint64_t limit = metaslab_class_get_space(special) *
		    (100 - zfs_special_class_metadata_reserve_pct) / 100;

		if (metaslab_class_get_alloc(special) < limit)
			return (special);
	}

	return (spa_normal_class(spa));
  1. L2149–L2156 A block of the deduplication table goes to dedup if the pool has a dedup vdev, else to special if the pool keeps the table there, else to normal. A class already tried is skipped.
  2. L2158–L2159 No special vdev, or special already tried: normal.
  3. L2161–L2163 Metadata (an object type marked so, dnodes among them) and the indirect blocks of files: special.
  4. L2169–L2176 A block no larger than special_small_blocks (0 by default) goes to special while special has allocated less than 75% of its space. The last 25% is kept for metadata.
  5. L2178 Everything else: normal. A ZIL block never comes here: zio_alloc_zil tries its own order, log, special's embedded log, special, the embedded log of normal vdevs, then normal.

When a class is full

When the chosen class has no room left, zio_dva_allocate asks for a class again. The second answer is different, because the class that failed is now marked as tried.

Source zio_dva_allocate, zio.c
zio_dva_allocate Excerpt of module/zfs/zio.c, lines 4404–4407, OpenZFS zfs-2.4.4. View on GitHub
	/*
	 * Fall back to some other class when this one is full.
	 */
	if (error == ENOSPC && (newmc = spa_preferred_class(spa, zio)) != mc) {
  1. L4407 On ENOSPC, spa_preferred_class is called again and returns the next class. The I/O records it and starts the allocation over there.

The next copy on the next vdev

A block with several copies has one address per copy, a DVA. metaslab_alloc_dva_range starts the search for each copy after the first on the vdev that follows the previous copy's vdev in the class.

Source metaslab_alloc_dva_range, metaslab.c
metaslab_alloc_dva_range Excerpt of module/zfs/metaslab.c, lines 5420–5427, OpenZFS zfs-2.4.4. View on GitHub
	if (mg == NULL && d != 0) {
		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
		mg = vdev_get_mg(vd, mc)->mg_next;
	}
	if (mg == NULL || mg->mg_class != mc || mg->mg_activation_count <= 0) {
		ASSERT(mca->mca_rotor != NULL);
		mg = mca->mca_rotor;
	}
  1. L5420–L5423 Copy d starts at the group after the one of copy d - 1: the next vdev of the class, round the class. The comment above gives the reason: only two adjacent vdevs failing can then lose such a block. Metadata has one more copy than copies with redundant_metadata=all, the default.
  2. L5424–L5427 The first copy starts at the class's rotor, which moves on by block size, queue depth and free space (the metaslabs room). A class of one vdev puts every copy on it.

A replacement uses the mirror's functions

A replacing node has a table of its own, vdev_replacing_ops, but the slots that move I/O hold the mirror's functions. Only the name in the table differs.

Source vdev_replacing_ops, vdev_mirror.c
vdev_replacing_ops Excerpt of module/zfs/vdev_mirror.c, lines 1000–1007, OpenZFS zfs-2.4.4. View on GitHub
	.vdev_op_open = vdev_mirror_open,
	.vdev_op_close = vdev_mirror_close,
	.vdev_op_psize_to_asize = vdev_default_asize,
	.vdev_op_asize_to_psize = vdev_default_psize,
	.vdev_op_min_asize = vdev_default_min_asize,
	.vdev_op_min_alloc = NULL,
	.vdev_op_io_start = vdev_mirror_io_start,
	.vdev_op_io_done = vdev_mirror_io_done,
  1. L1000–L1001 The mirror's open and close.
  2. L1006–L1007 The mirror's io_start and io_done, as in vdev_mirror_ops. The hot spare's vdev_spare_ops fills the same slots.

The new node takes the old disk's place

vdev_add_parent does the change to the tree. The old disk leaves its parent, the new node takes its place, and the old disk becomes the new node's first child.

Source vdev_add_parent, vdev.c
vdev_add_parent Excerpt of module/zfs/vdev.c, lines 1439–1446, OpenZFS zfs-2.4.4. View on GitHub
	vdev_remove_child(pvd, cvd);
	vdev_add_child(pvd, mvd);
	cvd->vdev_id = mvd->vdev_children;
	vdev_add_child(mvd, cvd);
	vdev_top_update(cvd->vdev_top, cvd->vdev_top);

	if (mvd == mvd->vdev_top)
		vdev_top_transfer(cvd, mvd);
  1. L1439–L1440 The new node replaces the old disk among its parent's children, with the same vdev_id.
  2. L1441–L1442 The old disk becomes child 0. spa_vdev_attach then adds the new disk as the last child and marks it missing every txg up to now, so a read of an older block skips it until the resilver is done.
  3. L1445–L1446 If the old disk was itself a top-level vdev, the new node becomes the top-level vdev and takes over its metaslabs and its class.

The end of the resilver

After a resilver, spa_vdev_resilver_done_hunt looks for a replacing node whose new disk now has everything. It hands back the old disk to be detached.

Source spa_vdev_resilver_done_hunt, spa.c
spa_vdev_resilver_done_hunt Excerpt of module/zfs/spa.c, lines 9446–9455, OpenZFS zfs-2.4.4. View on GitHub
	if (vd->vdev_ops == &vdev_replacing_ops) {
		ASSERT(vd->vdev_children > 1);

		newvd = vd->vdev_child[vd->vdev_children - 1];
		oldvd = vd->vdev_child[0];

		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
		    !vdev_dtl_required(oldvd))
			return (oldvd);
  1. L9449–L9450 The newest disk is the last child, the oldest the first.
  2. L9452–L9455 The new disk misses nothing and the old one is not needed: the old disk is detached, and the replacing node, left with one child, is removed. A spare node is handled further down, and stays until the original disk is brought back or replaced, or one of the two is detached.

dRAID: which child holds each column

A RAID-Z group always writes to the same disks. A dRAID group of the same width can land on any of the children: vdev_draid_map_alloc_row looks each column up in a row of a table of permutations.

Source vdev_draid_map_alloc_row, vdev_draid.c
vdev_draid_map_alloc_row Excerpt of module/zfs/vdev_draid.c, lines 1047–1053, OpenZFS zfs-2.4.4. View on GitHub
		uint64_t c = (groupstart + i) % ndisks;

		/* increment the offset if we wrap to the next row */
		if (i == wrap)
			physical_offset += VDEV_DRAID_ROWHEIGHT;

		rc->rc_devidx = vdev_draid_permute_id(vdc, base, iter, c);
  1. L1047 Column i of the group sits at position (groupstart + i) mod ndisks in the row. ndisks is the children minus the distributed spares.
  2. L1049–L1051 A group that runs past the end of a row goes on in the next one, 16 MiB further.
  3. L1053 The child is the row's entry at that position, plus an offset that grows by one from each row to the next, so each permutation serves as many rows as the dRAID has children before the next one takes over. The table is generated from a fixed seed and checked against a stored checksum, since existing pools depend on it. The figure computes the same table.

What the figure leaves out

Which vdev of a class gets a block's first copy is decided by the allocator's rotor, by load and free space. The figure moves the rotor on by one vdev per block, and places each dRAID copy at the start of the next group and each raidz copy after the last. Block sizes are fixed: 4 KiB for the ZIL and small file blocks, 16 KiB for metadata, 8 KiB for the deduplication table and 128 KiB for a large file block, after compression. A mirror's read takes the first eligible child, where the code takes the least loaded. Only the special class fills up, and its embedded log space stays free. Normal and special vdevs are taken to be large enough to hold an embedded log metaslab. The figure allows one replacement per disk at a time. A real pool with RAID-Z or dRAID vdevs cannot remove a special vdev, which the figure does by building the pool again. The resilver itself, its order and the sequential rebuild are not drawn, only its end. Removed (indirect) vdevs, holes, RAID-Z expansion, gang blocks and errors are left out. The 3D view draws the same state as the 2D one, and neither shows disk offsets.