--- old/usr/src/uts/common/fs/zfs/vdev_mirror.c	Mon Aug  5 17:27:07 2019
+++ new/usr/src/uts/common/fs/zfs/vdev_mirror.c	Mon Aug  5 17:27:07 2019
@@ -24,6 +24,7 @@
  */
 
 /*
+ * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
  */
 
@@ -35,6 +36,7 @@
 #include <sys/vdev_impl.h>
 #include <sys/zio.h>
 #include <sys/abd.h>
+#include <sys/wbc.h>
 #include <sys/fs/zfs.h>
 
 /*
@@ -48,6 +50,9 @@
 	uint8_t		mc_tried;
 	uint8_t		mc_skipped;
 	uint8_t		mc_speculative;
+	int		mc_index;	/* index in mirror_map_t */
+	avl_node_t	mc_node;	/* used for sorting based on weight */
+	int64_t		mc_weight;	/* thread-local copy of vdev_weight */
 } mirror_child_t;
 
 typedef struct mirror_map {
@@ -84,32 +89,9 @@
 	if (vd == NULL) {
 		dva_t *dva = zio->io_bp->blk_dva;
 		spa_t *spa = zio->io_spa;
-		dva_t dva_copy[SPA_DVAS_PER_BP];
 
 		c = BP_GET_NDVAS(zio->io_bp);
 
-		/*
-		 * If we do not trust the pool config, some DVAs might be
-		 * invalid or point to vdevs that do not exist. We skip them.
-		 */
-		if (!spa_trust_config(spa)) {
-			ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
-			int j = 0;
-			for (int i = 0; i < c; i++) {
-				if (zfs_dva_valid(spa, &dva[i], zio->io_bp))
-					dva_copy[j++] = dva[i];
-			}
-			if (j == 0) {
-				zio->io_vsd = NULL;
-				zio->io_error = ENXIO;
-				return (NULL);
-			}
-			if (j < c) {
-				dva = dva_copy;
-				c = j;
-			}
-		}
-
 		mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
 		mm->mm_children = c;
 		mm->mm_resilvering = B_FALSE;
@@ -133,6 +115,9 @@
 
 			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
 			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
+			mc->mc_index = c;
+			mc->mc_weight = (mc->mc_vd != NULL ?
+			    mc->mc_vd->vdev_weight : 0);
 		}
 	} else {
 		int replacing;
@@ -187,6 +172,9 @@
 			mc = &mm->mm_child[c];
 			mc->mc_vd = vd->vdev_child[c];
 			mc->mc_offset = zio->io_offset;
+			mc->mc_index = c;
+			mc->mc_weight = (mc->mc_vd != NULL ?
+			    mc->mc_vd->vdev_weight : 0);
 		}
 	}
 
@@ -224,10 +212,7 @@
 	}
 
 	if (numerrors == vd->vdev_children) {
-		if (vdev_children_are_offline(vd))
-			vd->vdev_stat.vs_aux = VDEV_AUX_CHILDREN_OFFLINE;
-		else
-			vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
+		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
 		return (lasterror);
 	}
 
@@ -276,6 +261,59 @@
 	mc->mc_skipped = 0;
 }
 
+static int
+vdev_weight_compar(const void *mc_a, const void *mc_b)
+{
+	const mirror_child_t *a = mc_a, *b = mc_b;
+
+	/*
+	 * 1) if a's weight is less than b's, a goes right in the tree
+	 * 2) if a's weight is greater than b's, a goes left
+	 * 3) if a's and b's weights are equal, lower map index goes left
+	 * 4) if weight and map index are equal, it's the same object
+	 */
+	if (a->mc_weight < b->mc_weight)
+		return (1);
+	if (a->mc_weight > b->mc_weight)
+		return (-1);
+	if (a->mc_index > b->mc_index)
+		return (1);
+	if (a->mc_index < b->mc_index)
+		return (-1);
+	ASSERT3P(a->mc_vd, ==, b->mc_vd);
+	return (0);
+}
+
+static boolean_t
+child_select_mc(mirror_child_t *mc, uint64_t txg)
+{
+	if (mc->mc_tried || mc->mc_skipped)
+		return (B_FALSE);
+	if (!vdev_readable(mc->mc_vd)) {
+		mc->mc_error = SET_ERROR(ENXIO);
+		mc->mc_tried = 1;	/* don't even try */
+		mc->mc_skipped = 1;
+		return (B_FALSE);
+	}
+	if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
+		mc->mc_weight--;
+		mc->mc_vd->vdev_weight = mc->mc_weight;
+		return (B_TRUE);
+	}
+	mc->mc_error = SET_ERROR(ESTALE);
+	mc->mc_skipped = 1;
+	mc->mc_speculative = 1;
+	return (B_FALSE);
+}
+
+static void
+child_select_cleanup(mirror_map_t *mm, avl_tree_t *vdevs_by_weight)
+{
+	for (int i = 0; i < mm->mm_children; i++)
+		avl_remove(vdevs_by_weight, &mm->mm_child[i]);
+	avl_destroy(vdevs_by_weight);
+}
+
 /*
  * Try to find a child whose DTL doesn't contain the block we want to read.
  * If we can't, try the read on any vdev we haven't already tried.
@@ -284,42 +322,76 @@
 vdev_mirror_child_select(zio_t *zio)
 {
 	mirror_map_t *mm = zio->io_vsd;
-	mirror_child_t *mc;
 	uint64_t txg = zio->io_txg;
-	int i, c;
+	/*
+	 * Look at the weights of the vdevs in the mirror; the weights help
+	 * decide which vdev to read from; the highest-weight suitable child
+	 * index is returned, and its weight is decremented in order to avoid
+	 * creating "hot" devices; once all the vdevs' weights are zero, the
+	 * weights are set back to the ones configured in vdev props
+	 */
+	int64_t max_weight = 0;
 
 	ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
 
+	for (int c = 0; c < mm->mm_children; c++) {
+		mirror_child_t *mc = &mm->mm_child[c];
+		if (mc->mc_vd == NULL)
+			continue;
+		max_weight = MAX(max_weight, mc->mc_weight);
+	}
+
 	/*
-	 * Try to find a child whose DTL doesn't contain the block to read.
-	 * If a child is known to be completely inaccessible (indicated by
-	 * vdev_readable() returning B_FALSE), don't even try.
+	 * Recalculate weights
 	 */
-	for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
-		if (c >= mm->mm_children)
-			c = 0;
-		mc = &mm->mm_child[c];
-		if (mc->mc_tried || mc->mc_skipped)
-			continue;
-		if (!vdev_readable(mc->mc_vd)) {
-			mc->mc_error = SET_ERROR(ENXIO);
-			mc->mc_tried = 1;	/* don't even try */
-			mc->mc_skipped = 1;
-			continue;
+	if (max_weight == 0) {
+		for (int c = 0; c < mm->mm_children; c++) {
+			mirror_child_t *mc = &mm->mm_child[c];
+			if (mc->mc_vd == NULL)
+				continue;
+			mc->mc_weight =
+			    vdev_queue_get_prop_uint64(&mc->mc_vd->vdev_queue,
+			    VDEV_PROP_PREFERRED_READ) + 1;
+			mc->mc_vd->vdev_weight = mc->mc_weight;
 		}
-		if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
-			return (c);
-		mc->mc_error = SET_ERROR(ESTALE);
-		mc->mc_skipped = 1;
-		mc->mc_speculative = 1;
 	}
 
+	if (mm->mm_children > 1) {
+		avl_tree_t vdevs_by_weight;
+
+		avl_create(&vdevs_by_weight, vdev_weight_compar,
+		    sizeof (mirror_child_t), offsetof(mirror_child_t, mc_node));
+
+		/*
+		 * Sort the weighted list
+		 */
+		for (int i = 0; i < mm->mm_children; i++)
+			avl_add(&vdevs_by_weight, &mm->mm_child[i]);
+
+		/*
+		 * Try to find a child whose DTL doesn't contain the block to
+		 * read. If a child is known to be completely inaccessible
+		 * (vdev_readable() returning B_FALSE), don't even try.
+		 */
+		for (mirror_child_t *mc = avl_first(&vdevs_by_weight);
+		    mc != NULL; mc = AVL_NEXT(&vdevs_by_weight, mc)) {
+			if (child_select_mc(mc, txg)) {
+				child_select_cleanup(mm, &vdevs_by_weight);
+				return (mc->mc_index);
+			}
+		}
+		child_select_cleanup(mm, &vdevs_by_weight);
+	} else {
+		if (child_select_mc(&mm->mm_child[0], txg))
+			return (0);
+	}
+
 	/*
 	 * Every device is either missing or has this txg in its DTL.
 	 * Look for any child we haven't already tried before giving up.
 	 */
-	for (c = 0; c < mm->mm_children; c++)
-		if (!mm->mm_child[c].mc_tried)
+	for (int c = 0; c < mm->mm_children; c++)
+		if (!mm->mm_child[c].mc_tried && mm->mm_child[c].mc_vd != NULL)
 			return (c);
 
 	/*
@@ -334,19 +406,29 @@
 	mirror_map_t *mm;
 	mirror_child_t *mc;
 	int c, children;
+	boolean_t spec_case = B_FALSE;
+	spa_t *spa = zio->io_spa;
 
 	mm = vdev_mirror_map_alloc(zio);
 
-	if (mm == NULL) {
-		ASSERT(!spa_trust_config(zio->io_spa));
-		ASSERT(zio->io_type == ZIO_TYPE_READ);
-		zio_execute(zio);
-		return;
-	}
+	if (zio->io_child_type != ZIO_CHILD_VDEV &&
+	    BP_IS_SPECIAL(zio->io_bp))
+		spec_case = B_TRUE;
 
 	if (zio->io_type == ZIO_TYPE_READ) {
 		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
+			int target = 0;
+
 			/*
+			 * Scrub of special BPs should take into
+			 * account the state of WBC-Window
+			 */
+			if (spec_case) {
+				target = wbc_select_dva(
+				    spa_get_wbc_data(spa), zio);
+			}
+
+			/*
 			 * For scrubbing reads we need to allocate a read
 			 * buffer for each child and issue reads to all
 			 * children.  If any child succeeds, it will copy its
@@ -354,6 +436,18 @@
 			 */
 			for (c = 0; c < mm->mm_children; c++) {
 				mc = &mm->mm_child[c];
+				if (mc->mc_vd == NULL) {
+					/*
+					 * Invalid vdev id in blkptr caused
+					 * mc_vd to be NULL here.
+					 * Just skip this vdev.
+					 */
+					continue;
+				}
+
+				if (spec_case && c != target)
+					continue;
+
 				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
 				    mc->mc_vd, mc->mc_offset,
 				    abd_alloc_sametype(zio->io_abd,
@@ -367,7 +461,12 @@
 		/*
 		 * For normal reads just pick one child.
 		 */
-		c = vdev_mirror_child_select(zio);
+
+		if (spec_case)
+			c = wbc_select_dva(spa_get_wbc_data(spa), zio);
+		else
+			c = vdev_mirror_child_select(zio);
+
 		children = (c >= 0);
 	} else {
 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
@@ -379,13 +478,29 @@
 		children = mm->mm_children;
 	}
 
-	while (children--) {
+	for (; children--; c++) {
 		mc = &mm->mm_child[c];
+		if (mc->mc_vd == NULL) {
+			/*
+			 * Invalid vdev in blkptr caused mc_vd to be NULL here.
+			 * Just skip this vdev.
+			 */
+			continue;
+		}
+
+		if (spec_case) {
+			if (zio->io_type == ZIO_TYPE_WRITE &&
+			    !vdev_is_special(mc->mc_vd))
+				continue;
+		}
+
 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
 		    mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
 		    zio->io_type, zio->io_priority, 0,
 		    vdev_mirror_child_done, mc));
-		c++;
+
+		if (spec_case)
+			break;
 	}
 
 	zio_execute(zio);
@@ -414,9 +529,6 @@
 	int good_copies = 0;
 	int unexpected_errors = 0;
 
-	if (mm == NULL)
-		return;
-
 	for (c = 0; c < mm->mm_children; c++) {
 		mc = &mm->mm_child[c];
 
@@ -522,19 +634,13 @@
 static void
 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
 {
-	if (faulted == vd->vdev_children) {
-		if (vdev_children_are_offline(vd)) {
-			vdev_set_state(vd, B_FALSE, VDEV_STATE_OFFLINE,
-			    VDEV_AUX_CHILDREN_OFFLINE);
-		} else {
-			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
-			    VDEV_AUX_NO_REPLICAS);
-		}
-	} else if (degraded + faulted != 0) {
+	if (faulted == vd->vdev_children)
+		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
+		    VDEV_AUX_NO_REPLICAS);
+	else if (degraded + faulted != 0)
 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
-	} else {
+	else
 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
-	}
 }
 
 vdev_ops_t vdev_mirror_ops = {
