]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/vdev.c
Add request size histograms (-r) to zpool iostat, minor man page fix
[mirror_zfs.git] / module / zfs / vdev.c
index 9335ba511d222bc16a2dbed4d8ed6fb8c7c39552..607be3ce3b81a0eecf6deb9e6fe3daa706c62d94 100644 (file)
@@ -22,7 +22,7 @@
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
- * Copyright (c) 2012 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
@@ -36,6 +36,7 @@
 #include <sys/metaslab.h>
 #include <sys/metaslab_impl.h>
 #include <sys/space_map.h>
+#include <sys/space_reftree.h>
 #include <sys/zio.h>
 #include <sys/zap.h>
 #include <sys/fs/zfs.h>
 #include <sys/dsl_scan.h>
 #include <sys/zvol.h>
 
+/*
+ * When a vdev is added, it will be divided into approximately (but no
+ * more than) this number of metaslabs.
+ */
+int metaslabs_per_vdev = 200;
+
 /*
  * Virtual device management.
  */
@@ -61,9 +68,6 @@ static vdev_ops_t *vdev_ops_table[] = {
        NULL
 };
 
-/* maximum scrub/resilver I/O queue per leaf vdev */
-int zfs_scrub_limit = 10;
-
 /*
  * Given a vdev type, return the appropriate ops vector.
  */
@@ -175,6 +179,27 @@ vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
        return (NULL);
 }
 
+static int
+vdev_count_leaves_impl(vdev_t *vd)
+{
+       int n = 0;
+       int c;
+
+       if (vd->vdev_ops->vdev_op_leaf)
+               return (1);
+
+       for (c = 0; c < vd->vdev_children; c++)
+               n += vdev_count_leaves_impl(vd->vdev_child[c]);
+
+       return (n);
+}
+
+int
+vdev_count_leaves(spa_t *spa)
+{
+       return (vdev_count_leaves_impl(spa->spa_root_vdev));
+}
+
 void
 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
 {
@@ -196,7 +221,7 @@ vdev_add_child(vdev_t *pvd, vdev_t *cvd)
        pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
        newsize = pvd->vdev_children * sizeof (vdev_t *);
 
-       newchild = kmem_zalloc(newsize, KM_PUSHPAGE);
+       newchild = kmem_alloc(newsize, KM_SLEEP);
        if (pvd->vdev_child != NULL) {
                bcopy(pvd->vdev_child, newchild, oldsize);
                kmem_free(pvd->vdev_child, oldsize);
@@ -266,7 +291,7 @@ vdev_compact_children(vdev_t *pvd)
                if (pvd->vdev_child[c])
                        newc++;
 
-       newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_PUSHPAGE);
+       newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
 
        for (c = newc = 0; c < oldc; c++) {
                if ((cvd = pvd->vdev_child[c]) != NULL) {
@@ -289,7 +314,7 @@ vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
        vdev_t *vd;
        int t;
 
-       vd = kmem_zalloc(sizeof (vdev_t), KM_PUSHPAGE);
+       vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
 
        if (spa->spa_root_vdev == NULL) {
                ASSERT(ops == &vdev_root_ops);
@@ -323,11 +348,11 @@ vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
 
        list_link_init(&vd->vdev_config_dirty_node);
        list_link_init(&vd->vdev_state_dirty_node);
-       mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
+       mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
        mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
        mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
        for (t = 0; t < DTL_TYPES; t++) {
-               space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
+               vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
                    &vd->vdev_dtl_lock);
        }
        txg_list_create(&vd->vdev_ms_list,
@@ -358,10 +383,10 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
        ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
 
        if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        if ((ops = vdev_getops(type)) == NULL)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        /*
         * If this is a load, get the vdev guid from the nvlist.
@@ -372,26 +397,26 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
 
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
                    label_id != id)
-                       return (EINVAL);
+                       return (SET_ERROR(EINVAL));
 
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
-                       return (EINVAL);
+                       return (SET_ERROR(EINVAL));
        } else if (alloctype == VDEV_ALLOC_SPARE) {
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
-                       return (EINVAL);
+                       return (SET_ERROR(EINVAL));
        } else if (alloctype == VDEV_ALLOC_L2CACHE) {
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
-                       return (EINVAL);
+                       return (SET_ERROR(EINVAL));
        } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
-                       return (EINVAL);
+                       return (SET_ERROR(EINVAL));
        }
 
        /*
         * The first allocated vdev must be of type 'root'.
         */
        if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        /*
         * Determine whether we're a log vdev.
@@ -399,10 +424,10 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
        islog = 0;
        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
        if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
-               return (ENOTSUP);
+               return (SET_ERROR(ENOTSUP));
 
        if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
-               return (ENOTSUP);
+               return (SET_ERROR(ENOTSUP));
 
        /*
         * Set the nparity property for RAID-Z vdevs.
@@ -412,24 +437,24 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
                    &nparity) == 0) {
                        if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
-                               return (EINVAL);
+                               return (SET_ERROR(EINVAL));
                        /*
                         * Previous versions could only support 1 or 2 parity
                         * device.
                         */
                        if (nparity > 1 &&
                            spa_version(spa) < SPA_VERSION_RAIDZ2)
-                               return (ENOTSUP);
+                               return (SET_ERROR(ENOTSUP));
                        if (nparity > 2 &&
                            spa_version(spa) < SPA_VERSION_RAIDZ3)
-                               return (ENOTSUP);
+                               return (SET_ERROR(ENOTSUP));
                } else {
                        /*
                         * We require the parity to be specified for SPAs that
                         * support multiple parity levels.
                         */
                        if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
-                               return (EINVAL);
+                               return (SET_ERROR(EINVAL));
                        /*
                         * Otherwise, we default to 1 parity device for RAID-Z.
                         */
@@ -494,6 +519,10 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
                    &vd->vdev_asize);
                (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
                    &vd->vdev_removing);
+               (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
+                   &vd->vdev_top_zap);
+       } else {
+               ASSERT0(vd->vdev_top_zap);
        }
 
        if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
@@ -505,15 +534,24 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
                    spa_log_class(spa) : spa_normal_class(spa), vd);
        }
 
+       if (vd->vdev_ops->vdev_op_leaf &&
+           (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
+               (void) nvlist_lookup_uint64(nv,
+                   ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
+       } else {
+               ASSERT0(vd->vdev_leaf_zap);
+       }
+
        /*
         * If we're a leaf vdev, try to load the DTL object and other state.
         */
+
        if (vd->vdev_ops->vdev_op_leaf &&
            (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
            alloctype == VDEV_ALLOC_ROOTPOOL)) {
                if (alloctype == VDEV_ALLOC_LOAD) {
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
-                           &vd->vdev_dtl_smo.smo_object);
+                           &vd->vdev_dtl_object);
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
                            &vd->vdev_unspare);
                }
@@ -529,8 +567,8 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
                (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
                    &vd->vdev_offline);
 
-               (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVERING,
-                   &vd->vdev_resilvering);
+               (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
+                   &vd->vdev_resilver_txg);
 
                /*
                 * When importing a pool, we want to ignore the persistent fault
@@ -601,9 +639,9 @@ vdev_free(vdev_t *vd)
                metaslab_group_destroy(vd->vdev_mg);
        }
 
-       ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
-       ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
-       ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
+       ASSERT0(vd->vdev_stat.vs_space);
+       ASSERT0(vd->vdev_stat.vs_dspace);
+       ASSERT0(vd->vdev_stat.vs_alloc);
 
        /*
         * Remove this vdev from its parent's child list.
@@ -636,9 +674,10 @@ vdev_free(vdev_t *vd)
        txg_list_destroy(&vd->vdev_dtl_list);
 
        mutex_enter(&vd->vdev_dtl_lock);
+       space_map_close(vd->vdev_dtl_sm);
        for (t = 0; t < DTL_TYPES; t++) {
-               space_map_unload(&vd->vdev_dtl[t]);
-               space_map_destroy(&vd->vdev_dtl[t]);
+               range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
+               range_tree_destroy(vd->vdev_dtl[t]);
        }
        mutex_exit(&vd->vdev_dtl_lock);
 
@@ -668,10 +707,12 @@ vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
        tvd->vdev_ms_array = svd->vdev_ms_array;
        tvd->vdev_ms_shift = svd->vdev_ms_shift;
        tvd->vdev_ms_count = svd->vdev_ms_count;
+       tvd->vdev_top_zap = svd->vdev_top_zap;
 
        svd->vdev_ms_array = 0;
        svd->vdev_ms_shift = 0;
        svd->vdev_ms_count = 0;
+       svd->vdev_top_zap = 0;
 
        if (tvd->vdev_mg)
                ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
@@ -796,6 +837,17 @@ vdev_remove_parent(vdev_t *cvd)
                cvd->vdev_orig_guid = cvd->vdev_guid;
                cvd->vdev_guid += guid_delta;
                cvd->vdev_guid_sum += guid_delta;
+
+               /*
+                * If pool not set for autoexpand, we need to also preserve
+                * mvd's asize to prevent automatic expansion of cvd.
+                * Otherwise if we are adjusting the mirror by attaching and
+                * detaching children of non-uniform sizes, the mirror could
+                * autoexpand, unexpectedly requiring larger devices to
+                * re-establish the mirror.
+                */
+               if (!cvd->vdev_spa->spa_autoexpand)
+                       cvd->vdev_asize = mvd->vdev_asize;
        }
        cvd->vdev_id = mvd->vdev_id;
        vdev_add_child(pvd, cvd);
@@ -831,16 +883,16 @@ vdev_metaslab_init(vdev_t *vd, uint64_t txg)
 
        /*
         * Compute the raidz-deflation ratio.  Note, we hard-code
-        * in 128k (1 << 17) because it is the current "typical" blocksize.
-        * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
-        * or we will inconsistently account for existing bp's.
+        * in 128k (1 << 17) because it is the "typical" blocksize.
+        * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
+        * otherwise it would inconsistently account for existing bp's.
         */
        vd->vdev_deflate_ratio = (1 << 17) /
            (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
 
        ASSERT(oldc <= newc);
 
-       mspp = kmem_zalloc(newc * sizeof (*mspp), KM_PUSHPAGE | KM_NODEBUG);
+       mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
 
        if (oldc != 0) {
                bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
@@ -851,27 +903,20 @@ vdev_metaslab_init(vdev_t *vd, uint64_t txg)
        vd->vdev_ms_count = newc;
 
        for (m = oldc; m < newc; m++) {
-               space_map_obj_t smo = { 0, 0, 0 };
+               uint64_t object = 0;
+
                if (txg == 0) {
-                       uint64_t object = 0;
                        error = dmu_read(mos, vd->vdev_ms_array,
                            m * sizeof (uint64_t), sizeof (uint64_t), &object,
                            DMU_READ_PREFETCH);
                        if (error)
                                return (error);
-                       if (object != 0) {
-                               dmu_buf_t *db;
-                               error = dmu_bonus_hold(mos, object, FTAG, &db);
-                               if (error)
-                                       return (error);
-                               ASSERT3U(db->db_size, >=, sizeof (smo));
-                               bcopy(db->db_data, &smo, sizeof (smo));
-                               ASSERT3U(smo.smo_object, ==, object);
-                               dmu_buf_rele(db, FTAG);
-                       }
                }
-               vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
-                   m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
+
+               error = metaslab_init(vd->vdev_mg, m, object, txg,
+                   &(vd->vdev_ms[m]));
+               if (error)
+                       return (error);
        }
 
        if (txg == 0)
@@ -899,9 +944,12 @@ vdev_metaslab_fini(vdev_t *vd)
 
        if (vd->vdev_ms != NULL) {
                metaslab_group_passivate(vd->vdev_mg);
-               for (m = 0; m < count; m++)
-                       if (vd->vdev_ms[m] != NULL)
-                               metaslab_fini(vd->vdev_ms[m]);
+               for (m = 0; m < count; m++) {
+                       metaslab_t *msp = vd->vdev_ms[m];
+
+                       if (msp != NULL)
+                               metaslab_fini(msp);
+               }
                kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
                vd->vdev_ms = NULL;
        }
@@ -952,7 +1000,7 @@ vdev_probe_done(zio_t *zio)
                        ASSERT(zio->io_error != 0);
                        zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
                            spa, vd, NULL, 0, 0);
-                       zio->io_error = ENXIO;
+                       zio->io_error = SET_ERROR(ENXIO);
                }
 
                mutex_enter(&vd->vdev_probe_lock);
@@ -962,16 +1010,18 @@ vdev_probe_done(zio_t *zio)
 
                while ((pio = zio_walk_parents(zio)) != NULL)
                        if (!vdev_accessible(vd, pio))
-                               pio->io_error = ENXIO;
+                               pio->io_error = SET_ERROR(ENXIO);
 
                kmem_free(vps, sizeof (*vps));
        }
 }
 
 /*
- * Determine whether this device is accessible by reading and writing
- * to several known locations: the pad regions of each vdev label
- * but the first (which we leave alone in case it contains a VTOC).
+ * Determine whether this device is accessible.
+ *
+ * Read and write to several known locations: the pad regions of each
+ * vdev label but the first, which we leave alone in case it contains
+ * a VTOC.
  */
 zio_t *
 vdev_probe(vdev_t *vd, zio_t *zio)
@@ -997,7 +1047,7 @@ vdev_probe(vdev_t *vd, zio_t *zio)
        mutex_enter(&vd->vdev_probe_lock);
 
        if ((pio = vd->vdev_probe_zio) == NULL) {
-               vps = kmem_zalloc(sizeof (*vps), KM_PUSHPAGE);
+               vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
 
                vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
                    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
@@ -1073,6 +1123,7 @@ vdev_open_child(void *arg)
        vd->vdev_open_thread = curthread;
        vd->vdev_open_error = vdev_open(vd);
        vd->vdev_open_thread = NULL;
+       vd->vdev_parent->vdev_nonrot &= vd->vdev_nonrot;
 }
 
 static boolean_t
@@ -1099,15 +1150,19 @@ vdev_open_children(vdev_t *vd)
        int children = vd->vdev_children;
        int c;
 
+       vd->vdev_nonrot = B_TRUE;
+
        /*
         * in order to handle pools on top of zvols, do the opens
         * in a single thread so that the same thread holds the
         * spa_namespace_lock
         */
        if (vdev_uses_zvols(vd)) {
-               for (c = 0; c < children; c++)
+               for (c = 0; c < children; c++) {
                        vd->vdev_child[c]->vdev_open_error =
                            vdev_open(vd->vdev_child[c]);
+                       vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
+               }
                return;
        }
        tq = taskq_create("vdev_open", children, minclsyspri,
@@ -1118,6 +1173,9 @@ vdev_open_children(vdev_t *vd)
                    TQ_SLEEP) != 0);
 
        taskq_destroy(tq);
+
+       for (c = 0; c < children; c++)
+               vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
 }
 
 /*
@@ -1155,11 +1213,11 @@ vdev_open(vdev_t *vd)
                    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
                vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
                    vd->vdev_label_aux);
-               return (ENXIO);
+               return (SET_ERROR(ENXIO));
        } else if (vd->vdev_offline) {
                ASSERT(vd->vdev_children == 0);
                vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
-               return (ENXIO);
+               return (SET_ERROR(ENXIO));
        }
 
        error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
@@ -1194,7 +1252,7 @@ vdev_open(vdev_t *vd)
                    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
                vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
                    vd->vdev_label_aux);
-               return (ENXIO);
+               return (SET_ERROR(ENXIO));
        }
 
        if (vd->vdev_degraded) {
@@ -1226,7 +1284,7 @@ vdev_open(vdev_t *vd)
                if (osize < SPA_MINDEVSIZE) {
                        vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
                            VDEV_AUX_TOO_SMALL);
-                       return (EOVERFLOW);
+                       return (SET_ERROR(EOVERFLOW));
                }
                psize = osize;
                asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
@@ -1237,7 +1295,7 @@ vdev_open(vdev_t *vd)
                    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
                        vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
                            VDEV_AUX_TOO_SMALL);
-                       return (EOVERFLOW);
+                       return (SET_ERROR(EOVERFLOW));
                }
                psize = 0;
                asize = osize;
@@ -1252,17 +1310,18 @@ vdev_open(vdev_t *vd)
        if (asize < vd->vdev_min_asize) {
                vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
                    VDEV_AUX_BAD_LABEL);
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
        }
 
        if (vd->vdev_asize == 0) {
                /*
                 * This is the first-ever open, so use the computed values.
-                * For testing purposes, a higher ashift can be requested.
+                * For compatibility, a different ashift can be requested.
                 */
                vd->vdev_asize = asize;
                vd->vdev_max_asize = max_asize;
-               vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
+               if (vd->vdev_ashift == 0)
+                       vd->vdev_ashift = ashift;
        } else {
                /*
                 * Detect if the alignment requirement has increased.
@@ -1300,6 +1359,17 @@ vdev_open(vdev_t *vd)
                return (error);
        }
 
+       /*
+        * Track the min and max ashift values for normal data devices.
+        */
+       if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
+           !vd->vdev_islog && vd->vdev_aux == NULL) {
+               if (vd->vdev_ashift > spa->spa_max_ashift)
+                       spa->spa_max_ashift = vd->vdev_ashift;
+               if (vd->vdev_ashift < spa->spa_min_ashift)
+                       spa->spa_min_ashift = vd->vdev_ashift;
+       }
+
        /*
         * If a leaf vdev has a DTL, and seems healthy, then kick off a
         * resilver.  But don't do this if we are doing a reopen for a scrub,
@@ -1338,7 +1408,7 @@ vdev_validate(vdev_t *vd, boolean_t strict)
 
        for (c = 0; c < vd->vdev_children; c++)
                if (vdev_validate(vd->vdev_child[c], strict) != 0)
-                       return (EBADF);
+                       return (SET_ERROR(EBADF));
 
        /*
         * If the device has already failed, or was marked offline, don't do
@@ -1348,9 +1418,10 @@ vdev_validate(vdev_t *vd, boolean_t strict)
        if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
                uint64_t aux_guid = 0;
                nvlist_t *nvl;
+               uint64_t txg = spa_last_synced_txg(spa) != 0 ?
+                   spa_last_synced_txg(spa) : -1ULL;
 
-               if ((label = vdev_label_read_config(vd, VDEV_BEST_LABEL)) ==
-                   NULL) {
+               if ((label = vdev_label_read_config(vd, txg)) == NULL) {
                        vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
                            VDEV_AUX_BAD_LABEL);
                        return (0);
@@ -1423,7 +1494,7 @@ vdev_validate(vdev_t *vd, boolean_t strict)
                if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
                    spa_load_state(spa) == SPA_LOAD_OPEN &&
                    state != POOL_STATE_ACTIVE)
-                       return (EBADF);
+                       return (SET_ERROR(EBADF));
 
                /*
                 * If we were able to open and validate a vdev that was
@@ -1560,9 +1631,10 @@ vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
        }
 
        /*
-        * Recursively initialize all labels.
+        * Recursively load DTLs and initialize all labels.
         */
-       if ((error = vdev_label_init(vd, txg, isreplacing ?
+       if ((error = vdev_dtl_load(vd)) != 0 ||
+           (error = vdev_label_init(vd, txg, isreplacing ?
            VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
                vdev_close(vd);
                return (error);
@@ -1575,9 +1647,9 @@ void
 vdev_metaslab_set_size(vdev_t *vd)
 {
        /*
-        * Aim for roughly 200 metaslabs per vdev.
+        * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
         */
-       vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
+       vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
        vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
 }
 
@@ -1598,6 +1670,18 @@ vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
        (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
 }
 
+void
+vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
+{
+       int c;
+
+       for (c = 0; c < vd->vdev_children; c++)
+               vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
+
+       if (vd->vdev_ops->vdev_op_leaf)
+               vdev_dirty(vd->vdev_top, flags, vd, txg);
+}
+
 /*
  * DTLs.
  *
@@ -1639,31 +1723,31 @@ vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
 void
 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
 {
-       space_map_t *sm = &vd->vdev_dtl[t];
+       range_tree_t *rt = vd->vdev_dtl[t];
 
        ASSERT(t < DTL_TYPES);
        ASSERT(vd != vd->vdev_spa->spa_root_vdev);
        ASSERT(spa_writeable(vd->vdev_spa));
 
-       mutex_enter(sm->sm_lock);
-       if (!space_map_contains(sm, txg, size))
-               space_map_add(sm, txg, size);
-       mutex_exit(sm->sm_lock);
+       mutex_enter(rt->rt_lock);
+       if (!range_tree_contains(rt, txg, size))
+               range_tree_add(rt, txg, size);
+       mutex_exit(rt->rt_lock);
 }
 
 boolean_t
 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
 {
-       space_map_t *sm = &vd->vdev_dtl[t];
+       range_tree_t *rt = vd->vdev_dtl[t];
        boolean_t dirty = B_FALSE;
 
        ASSERT(t < DTL_TYPES);
        ASSERT(vd != vd->vdev_spa->spa_root_vdev);
 
-       mutex_enter(sm->sm_lock);
-       if (sm->sm_space != 0)
-               dirty = space_map_contains(sm, txg, size);
-       mutex_exit(sm->sm_lock);
+       mutex_enter(rt->rt_lock);
+       if (range_tree_space(rt) != 0)
+               dirty = range_tree_contains(rt, txg, size);
+       mutex_exit(rt->rt_lock);
 
        return (dirty);
 }
@@ -1671,16 +1755,85 @@ vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
 boolean_t
 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
 {
-       space_map_t *sm = &vd->vdev_dtl[t];
+       range_tree_t *rt = vd->vdev_dtl[t];
        boolean_t empty;
 
-       mutex_enter(sm->sm_lock);
-       empty = (sm->sm_space == 0);
-       mutex_exit(sm->sm_lock);
+       mutex_enter(rt->rt_lock);
+       empty = (range_tree_space(rt) == 0);
+       mutex_exit(rt->rt_lock);
 
        return (empty);
 }
 
+/*
+ * Returns the lowest txg in the DTL range.
+ */
+static uint64_t
+vdev_dtl_min(vdev_t *vd)
+{
+       range_seg_t *rs;
+
+       ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
+       ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
+       ASSERT0(vd->vdev_children);
+
+       rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
+       return (rs->rs_start - 1);
+}
+
+/*
+ * Returns the highest txg in the DTL.
+ */
+static uint64_t
+vdev_dtl_max(vdev_t *vd)
+{
+       range_seg_t *rs;
+
+       ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
+       ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
+       ASSERT0(vd->vdev_children);
+
+       rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
+       return (rs->rs_end);
+}
+
+/*
+ * Determine if a resilvering vdev should remove any DTL entries from
+ * its range. If the vdev was resilvering for the entire duration of the
+ * scan then it should excise that range from its DTLs. Otherwise, this
+ * vdev is considered partially resilvered and should leave its DTL
+ * entries intact. The comment in vdev_dtl_reassess() describes how we
+ * excise the DTLs.
+ */
+static boolean_t
+vdev_dtl_should_excise(vdev_t *vd)
+{
+       spa_t *spa = vd->vdev_spa;
+       dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
+
+       ASSERT0(scn->scn_phys.scn_errors);
+       ASSERT0(vd->vdev_children);
+
+       if (vd->vdev_resilver_txg == 0 ||
+           range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
+               return (B_TRUE);
+
+       /*
+        * When a resilver is initiated the scan will assign the scn_max_txg
+        * value to the highest txg value that exists in all DTLs. If this
+        * device's max DTL is not part of this scan (i.e. it is not in
+        * the range (scn_min_txg, scn_max_txg] then it is not eligible
+        * for excision.
+        */
+       if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
+               ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
+               ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
+               ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
+               return (B_TRUE);
+       }
+       return (B_FALSE);
+}
+
 /*
  * Reassess DTLs after a config change or scrub completion.
  */
@@ -1704,9 +1857,17 @@ vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
                dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
 
                mutex_enter(&vd->vdev_dtl_lock);
+
+               /*
+                * If we've completed a scan cleanly then determine
+                * if this vdev should remove any DTLs. We only want to
+                * excise regions on vdevs that were available during
+                * the entire duration of this scan.
+                */
                if (scrub_txg != 0 &&
                    (spa->spa_scrub_started ||
-                   (scn && scn->scn_phys.scn_errors == 0))) {
+                   (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
+                   vdev_dtl_should_excise(vd)) {
                        /*
                         * We completed a scrub up to scrub_txg.  If we
                         * did it without rebooting, then the scrub dtl
@@ -1724,27 +1885,37 @@ vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
                         * positive refcnt -- either 1 or 2.  We then convert
                         * the reference tree into the new DTL_MISSING map.
                         */
-                       space_map_ref_create(&reftree);
-                       space_map_ref_add_map(&reftree,
-                           &vd->vdev_dtl[DTL_MISSING], 1);
-                       space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
-                       space_map_ref_add_map(&reftree,
-                           &vd->vdev_dtl[DTL_SCRUB], 2);
-                       space_map_ref_generate_map(&reftree,
-                           &vd->vdev_dtl[DTL_MISSING], 1);
-                       space_map_ref_destroy(&reftree);
+                       space_reftree_create(&reftree);
+                       space_reftree_add_map(&reftree,
+                           vd->vdev_dtl[DTL_MISSING], 1);
+                       space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
+                       space_reftree_add_map(&reftree,
+                           vd->vdev_dtl[DTL_SCRUB], 2);
+                       space_reftree_generate_map(&reftree,
+                           vd->vdev_dtl[DTL_MISSING], 1);
+                       space_reftree_destroy(&reftree);
                }
-               space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
-               space_map_walk(&vd->vdev_dtl[DTL_MISSING],
-                   space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
+               range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
+               range_tree_walk(vd->vdev_dtl[DTL_MISSING],
+                   range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
                if (scrub_done)
-                       space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
-               space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
+                       range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
+               range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
                if (!vdev_readable(vd))
-                       space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
+                       range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
                else
-                       space_map_walk(&vd->vdev_dtl[DTL_MISSING],
-                           space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
+                       range_tree_walk(vd->vdev_dtl[DTL_MISSING],
+                           range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
+
+               /*
+                * If the vdev was resilvering and no longer has any
+                * DTLs then reset its resilvering flag.
+                */
+               if (vd->vdev_resilver_txg != 0 &&
+                   range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
+                   range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
+                       vd->vdev_resilver_txg = 0;
+
                mutex_exit(&vd->vdev_dtl_lock);
 
                if (txg != 0)
@@ -1754,6 +1925,8 @@ vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
 
        mutex_enter(&vd->vdev_dtl_lock);
        for (t = 0; t < DTL_TYPES; t++) {
+               int c;
+
                /* account for child's outage in parent's missing map */
                int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
                if (t == DTL_SCRUB)
@@ -1764,111 +1937,189 @@ vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
                        minref = vd->vdev_nparity + 1;  /* RAID-Z */
                else
                        minref = vd->vdev_children;     /* any kind of mirror */
-               space_map_ref_create(&reftree);
+               space_reftree_create(&reftree);
                for (c = 0; c < vd->vdev_children; c++) {
                        vdev_t *cvd = vd->vdev_child[c];
                        mutex_enter(&cvd->vdev_dtl_lock);
-                       space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
+                       space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
                        mutex_exit(&cvd->vdev_dtl_lock);
                }
-               space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
-               space_map_ref_destroy(&reftree);
+               space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
+               space_reftree_destroy(&reftree);
        }
        mutex_exit(&vd->vdev_dtl_lock);
 }
 
-static int
+int
 vdev_dtl_load(vdev_t *vd)
 {
        spa_t *spa = vd->vdev_spa;
-       space_map_obj_t *smo = &vd->vdev_dtl_smo;
        objset_t *mos = spa->spa_meta_objset;
-       dmu_buf_t *db;
-       int error;
+       int error = 0;
+       int c;
 
-       ASSERT(vd->vdev_children == 0);
+       if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
+               ASSERT(!vd->vdev_ishole);
 
-       if (smo->smo_object == 0)
-               return (0);
+               error = space_map_open(&vd->vdev_dtl_sm, mos,
+                   vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
+               if (error)
+                       return (error);
+               ASSERT(vd->vdev_dtl_sm != NULL);
 
-       ASSERT(!vd->vdev_ishole);
+               mutex_enter(&vd->vdev_dtl_lock);
 
-       if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
-               return (error);
+               /*
+                * Now that we've opened the space_map we need to update
+                * the in-core DTL.
+                */
+               space_map_update(vd->vdev_dtl_sm);
 
-       ASSERT3U(db->db_size, >=, sizeof (*smo));
-       bcopy(db->db_data, smo, sizeof (*smo));
-       dmu_buf_rele(db, FTAG);
+               error = space_map_load(vd->vdev_dtl_sm,
+                   vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
+               mutex_exit(&vd->vdev_dtl_lock);
 
-       mutex_enter(&vd->vdev_dtl_lock);
-       error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
-           NULL, SM_ALLOC, smo, mos);
-       mutex_exit(&vd->vdev_dtl_lock);
+               return (error);
+       }
+
+       for (c = 0; c < vd->vdev_children; c++) {
+               error = vdev_dtl_load(vd->vdev_child[c]);
+               if (error != 0)
+                       break;
+       }
 
        return (error);
 }
 
+void
+vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
+{
+       spa_t *spa = vd->vdev_spa;
+
+       VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
+       VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
+           zapobj, tx));
+}
+
+uint64_t
+vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
+{
+       spa_t *spa = vd->vdev_spa;
+       uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
+           DMU_OT_NONE, 0, tx);
+
+       ASSERT(zap != 0);
+       VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
+           zap, tx));
+
+       return (zap);
+}
+
+void
+vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
+{
+       uint64_t i;
+
+       if (vd->vdev_ops != &vdev_hole_ops &&
+           vd->vdev_ops != &vdev_missing_ops &&
+           vd->vdev_ops != &vdev_root_ops &&
+           !vd->vdev_top->vdev_removing) {
+               if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
+                       vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
+               }
+               if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
+                       vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
+               }
+       }
+       for (i = 0; i < vd->vdev_children; i++) {
+               vdev_construct_zaps(vd->vdev_child[i], tx);
+       }
+}
+
 void
 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
 {
        spa_t *spa = vd->vdev_spa;
-       space_map_obj_t *smo = &vd->vdev_dtl_smo;
-       space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
+       range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
        objset_t *mos = spa->spa_meta_objset;
-       space_map_t smsync;
-       kmutex_t smlock;
-       dmu_buf_t *db;
+       range_tree_t *rtsync;
+       kmutex_t rtlock;
        dmu_tx_t *tx;
+       uint64_t object = space_map_object(vd->vdev_dtl_sm);
 
        ASSERT(!vd->vdev_ishole);
+       ASSERT(vd->vdev_ops->vdev_op_leaf);
 
        tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
 
-       if (vd->vdev_detached) {
-               if (smo->smo_object != 0) {
-                       VERIFY(0 == dmu_object_free(mos, smo->smo_object, tx));
-                       smo->smo_object = 0;
+       if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
+               mutex_enter(&vd->vdev_dtl_lock);
+               space_map_free(vd->vdev_dtl_sm, tx);
+               space_map_close(vd->vdev_dtl_sm);
+               vd->vdev_dtl_sm = NULL;
+               mutex_exit(&vd->vdev_dtl_lock);
+
+               /*
+                * We only destroy the leaf ZAP for detached leaves or for
+                * removed log devices. Removed data devices handle leaf ZAP
+                * cleanup later, once cancellation is no longer possible.
+                */
+               if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
+                   vd->vdev_top->vdev_islog)) {
+                       vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
+                       vd->vdev_leaf_zap = 0;
                }
+
                dmu_tx_commit(tx);
                return;
        }
 
-       if (smo->smo_object == 0) {
-               ASSERT(smo->smo_objsize == 0);
-               ASSERT(smo->smo_alloc == 0);
-               smo->smo_object = dmu_object_alloc(mos,
-                   DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
-                   DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
-               ASSERT(smo->smo_object != 0);
-               vdev_config_dirty(vd->vdev_top);
+       if (vd->vdev_dtl_sm == NULL) {
+               uint64_t new_object;
+
+               new_object = space_map_alloc(mos, tx);
+               VERIFY3U(new_object, !=, 0);
+
+               VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
+                   0, -1ULL, 0, &vd->vdev_dtl_lock));
+               ASSERT(vd->vdev_dtl_sm != NULL);
        }
 
-       mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
+       mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
 
-       space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
-           &smlock);
+       rtsync = range_tree_create(NULL, NULL, &rtlock);
 
-       mutex_enter(&smlock);
+       mutex_enter(&rtlock);
 
        mutex_enter(&vd->vdev_dtl_lock);
-       space_map_walk(sm, space_map_add, &smsync);
+       range_tree_walk(rt, range_tree_add, rtsync);
        mutex_exit(&vd->vdev_dtl_lock);
 
-       space_map_truncate(smo, mos, tx);
-       space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
+       space_map_truncate(vd->vdev_dtl_sm, tx);
+       space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
+       range_tree_vacate(rtsync, NULL, NULL);
 
-       space_map_destroy(&smsync);
+       range_tree_destroy(rtsync);
 
-       mutex_exit(&smlock);
-       mutex_destroy(&smlock);
+       mutex_exit(&rtlock);
+       mutex_destroy(&rtlock);
 
-       VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
-       dmu_buf_will_dirty(db, tx);
-       ASSERT3U(db->db_size, >=, sizeof (*smo));
-       bcopy(smo, db->db_data, sizeof (*smo));
-       dmu_buf_rele(db, FTAG);
+       /*
+        * If the object for the space map has changed then dirty
+        * the top level so that we update the config.
+        */
+       if (object != space_map_object(vd->vdev_dtl_sm)) {
+               zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
+                   "new object %llu", txg, spa_name(spa), object,
+                   space_map_object(vd->vdev_dtl_sm));
+               vdev_config_dirty(vd->vdev_top);
+       }
 
        dmu_tx_commit(tx);
+
+       mutex_enter(&vd->vdev_dtl_lock);
+       space_map_update(vd->vdev_dtl_sm);
+       mutex_exit(&vd->vdev_dtl_lock);
 }
 
 /*
@@ -1918,14 +2169,11 @@ vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
 
        if (vd->vdev_children == 0) {
                mutex_enter(&vd->vdev_dtl_lock);
-               if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
+               if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
                    vdev_writeable(vd)) {
-                       space_seg_t *ss;
 
-                       ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
-                       thismin = ss->ss_start - 1;
-                       ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
-                       thismax = ss->ss_end;
+                       thismin = vdev_dtl_min(vd);
+                       thismax = vdev_dtl_max(vd);
                        needed = B_TRUE;
                }
                mutex_exit(&vd->vdev_dtl_lock);
@@ -1994,7 +2242,7 @@ vdev_validate_aux(vdev_t *vd)
        if (!vdev_readable(vd))
                return (0);
 
-       if ((label = vdev_label_read_config(vd, VDEV_BEST_LABEL)) == NULL) {
+       if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
                vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
                    VDEV_AUX_CORRUPT_DATA);
                return (-1);
@@ -2025,33 +2273,56 @@ vdev_remove(vdev_t *vd, uint64_t txg)
        spa_t *spa = vd->vdev_spa;
        objset_t *mos = spa->spa_meta_objset;
        dmu_tx_t *tx;
-       int m;
+       int m, i;
 
        tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
-
-       if (vd->vdev_dtl_smo.smo_object) {
-               ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
-               (void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
-               vd->vdev_dtl_smo.smo_object = 0;
-       }
+       ASSERT(vd == vd->vdev_top);
+       ASSERT3U(txg, ==, spa_syncing_txg(spa));
 
        if (vd->vdev_ms != NULL) {
+               metaslab_group_t *mg = vd->vdev_mg;
+
+               metaslab_group_histogram_verify(mg);
+               metaslab_class_histogram_verify(mg->mg_class);
+
                for (m = 0; m < vd->vdev_ms_count; m++) {
                        metaslab_t *msp = vd->vdev_ms[m];
 
-                       if (msp == NULL || msp->ms_smo.smo_object == 0)
+                       if (msp == NULL || msp->ms_sm == NULL)
                                continue;
 
-                       ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
-                       (void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
-                       msp->ms_smo.smo_object = 0;
+                       mutex_enter(&msp->ms_lock);
+                       /*
+                        * If the metaslab was not loaded when the vdev
+                        * was removed then the histogram accounting may
+                        * not be accurate. Update the histogram information
+                        * here so that we ensure that the metaslab group
+                        * and metaslab class are up-to-date.
+                        */
+                       metaslab_group_histogram_remove(mg, msp);
+
+                       VERIFY0(space_map_allocated(msp->ms_sm));
+                       space_map_free(msp->ms_sm, tx);
+                       space_map_close(msp->ms_sm);
+                       msp->ms_sm = NULL;
+                       mutex_exit(&msp->ms_lock);
                }
+
+               metaslab_group_histogram_verify(mg);
+               metaslab_class_histogram_verify(mg->mg_class);
+               for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
+                       ASSERT0(mg->mg_histogram[i]);
+
        }
 
        if (vd->vdev_ms_array) {
                (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
                vd->vdev_ms_array = 0;
-               vd->vdev_ms_shift = 0;
+       }
+
+       if (vd->vdev_islog && vd->vdev_top_zap != 0) {
+               vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
+               vd->vdev_top_zap = 0;
        }
        dmu_tx_commit(tx);
 }
@@ -2202,10 +2473,12 @@ vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
 }
 
 /*
- * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
- * any attached spare device should be detached when the device finishes
- * resilvering.  Second, the online should be treated like a 'test' online case,
- * so no FMA events are generated if the device fails to open.
+ * Online the given vdev.
+ *
+ * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
+ * spare device should be detached when the device finishes resilvering.
+ * Second, the online should be treated like a 'test' online case, so no FMA
+ * events are generated if the device fails to open.
  */
 int
 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
@@ -2320,7 +2593,7 @@ top:
                                (void) spa_vdev_state_exit(spa, vd, 0);
                                goto top;
                        }
-                       ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
+                       ASSERT0(tvd->vdev_stat.vs_alloc);
                }
 
                /*
@@ -2491,45 +2764,132 @@ vdev_accessible(vdev_t *vd, zio_t *zio)
        return (B_TRUE);
 }
 
+static void
+vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
+{
+       int t;
+       for (t = 0; t < ZIO_TYPES; t++) {
+               vs->vs_ops[t] += cvs->vs_ops[t];
+               vs->vs_bytes[t] += cvs->vs_bytes[t];
+       }
+
+       cvs->vs_scan_removing = cvd->vdev_removing;
+}
+
 /*
- * Get statistics for the given vdev.
+ * Get extended stats
  */
-void
-vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
+static void
+vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
 {
-       vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
-       int c, t;
+       int t, b;
+       for (t = 0; t < ZIO_TYPES; t++) {
+               for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
+                       vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
 
-       mutex_enter(&vd->vdev_stat_lock);
-       bcopy(&vd->vdev_stat, vs, sizeof (*vs));
-       vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
-       vs->vs_state = vd->vdev_state;
-       vs->vs_rsize = vdev_get_min_asize(vd);
-       if (vd->vdev_ops->vdev_op_leaf)
-               vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
-       vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
-       mutex_exit(&vd->vdev_stat_lock);
+               for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
+                       vsx->vsx_total_histo[t][b] +=
+                           cvsx->vsx_total_histo[t][b];
+               }
+       }
+
+       for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
+               for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
+                       vsx->vsx_queue_histo[t][b] +=
+                           cvsx->vsx_queue_histo[t][b];
+               }
+               vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
+               vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
+
+               for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
+                       vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
+
+               for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
+                       vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
+       }
+
+}
 
+/*
+ * Get statistics for the given vdev.
+ */
+static void
+vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
+{
+       int c, t;
        /*
         * If we're getting stats on the root vdev, aggregate the I/O counts
         * over all top-level vdevs (i.e. the direct children of the root).
         */
-       if (vd == rvd) {
-               for (c = 0; c < rvd->vdev_children; c++) {
-                       vdev_t *cvd = rvd->vdev_child[c];
+       if (!vd->vdev_ops->vdev_op_leaf) {
+               if (vs) {
+                       memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
+                       memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
+               }
+               if (vsx)
+                       memset(vsx, 0, sizeof (*vsx));
+
+               for (c = 0; c < vd->vdev_children; c++) {
+                       vdev_t *cvd = vd->vdev_child[c];
                        vdev_stat_t *cvs = &cvd->vdev_stat;
+                       vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
+
+                       vdev_get_stats_ex_impl(cvd, cvs, cvsx);
+                       if (vs)
+                               vdev_get_child_stat(cvd, vs, cvs);
+                       if (vsx)
+                               vdev_get_child_stat_ex(cvd, vsx, cvsx);
 
-                       mutex_enter(&vd->vdev_stat_lock);
-                       for (t = 0; t < ZIO_TYPES; t++) {
-                               vs->vs_ops[t] += cvs->vs_ops[t];
-                               vs->vs_bytes[t] += cvs->vs_bytes[t];
-                       }
-                       cvs->vs_scan_removing = cvd->vdev_removing;
-                       mutex_exit(&vd->vdev_stat_lock);
+               }
+       } else {
+               /*
+                * We're a leaf.  Just copy our ZIO active queue stats in.  The
+                * other leaf stats are updated in vdev_stat_update().
+                */
+               if (!vsx)
+                       return;
+
+               memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
+
+               for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) {
+                       vsx->vsx_active_queue[t] =
+                           vd->vdev_queue.vq_class[t].vqc_active;
+                       vsx->vsx_pend_queue[t] = avl_numnodes(
+                           &vd->vdev_queue.vq_class[t].vqc_queued_tree);
                }
        }
 }
 
+void
+vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
+{
+       mutex_enter(&vd->vdev_stat_lock);
+       if (vs) {
+               bcopy(&vd->vdev_stat, vs, sizeof (*vs));
+               vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
+               vs->vs_state = vd->vdev_state;
+               vs->vs_rsize = vdev_get_min_asize(vd);
+               if (vd->vdev_ops->vdev_op_leaf)
+                       vs->vs_rsize += VDEV_LABEL_START_SIZE +
+                           VDEV_LABEL_END_SIZE;
+               vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
+               if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
+                   !vd->vdev_ishole) {
+                       vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
+               }
+       }
+
+       ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_READER) != 0);
+       vdev_get_stats_ex_impl(vd, vs, vsx);
+       mutex_exit(&vd->vdev_stat_lock);
+}
+
+void
+vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
+{
+       return (vdev_get_stats_ex(vd, vs, NULL));
+}
+
 void
 vdev_clear_stats(vdev_t *vd)
 {
@@ -2563,6 +2923,7 @@ vdev_stat_update(zio_t *zio, uint64_t psize)
        vdev_t *pvd;
        uint64_t txg = zio->io_txg;
        vdev_stat_t *vs = &vd->vdev_stat;
+       vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
        zio_type_t type = zio->io_type;
        int flags = zio->io_flags;
 
@@ -2613,8 +2974,32 @@ vdev_stat_update(zio_t *zio, uint64_t psize)
                                vs->vs_self_healed += psize;
                }
 
-               vs->vs_ops[type]++;
-               vs->vs_bytes[type] += psize;
+               /*
+                * The bytes/ops/histograms are recorded at the leaf level and
+                * aggregated into the higher level vdevs in vdev_get_stats().
+                */
+               if (vd->vdev_ops->vdev_op_leaf) {
+
+                       vs->vs_ops[type]++;
+                       vs->vs_bytes[type] += psize;
+
+                       if (flags & ZIO_FLAG_DELEGATED) {
+                               vsx->vsx_agg_histo[zio->io_priority]
+                                   [RQ_HISTO(zio->io_size)]++;
+                       } else {
+                               vsx->vsx_ind_histo[zio->io_priority]
+                                   [RQ_HISTO(zio->io_size)]++;
+                       }
+
+                       if (zio->io_delta && zio->io_delay) {
+                               vsx->vsx_queue_histo[zio->io_priority]
+                                   [L_HISTO(zio->io_delta - zio->io_delay)]++;
+                               vsx->vsx_disk_histo[type]
+                                   [L_HISTO(zio->io_delay)]++;
+                               vsx->vsx_total_histo[type]
+                                   [L_HISTO(zio->io_delta)]++;
+                       }
+               }
 
                mutex_exit(&vd->vdev_stat_lock);
                return;
@@ -3193,6 +3578,46 @@ vdev_split(vdev_t *vd)
        vdev_propagate_state(cvd);
 }
 
+void
+vdev_deadman(vdev_t *vd)
+{
+       int c;
+
+       for (c = 0; c < vd->vdev_children; c++) {
+               vdev_t *cvd = vd->vdev_child[c];
+
+               vdev_deadman(cvd);
+       }
+
+       if (vd->vdev_ops->vdev_op_leaf) {
+               vdev_queue_t *vq = &vd->vdev_queue;
+
+               mutex_enter(&vq->vq_lock);
+               if (avl_numnodes(&vq->vq_active_tree) > 0) {
+                       spa_t *spa = vd->vdev_spa;
+                       zio_t *fio;
+                       uint64_t delta;
+
+                       /*
+                        * Look at the head of all the pending queues,
+                        * if any I/O has been outstanding for longer than
+                        * the spa_deadman_synctime we log a zevent.
+                        */
+                       fio = avl_first(&vq->vq_active_tree);
+                       delta = gethrtime() - fio->io_timestamp;
+                       if (delta > spa_deadman_synctime(spa)) {
+                               zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
+                                   "delta %lluns, last io %lluns",
+                                   fio->io_timestamp, delta,
+                                   vq->vq_io_complete_ts);
+                               zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
+                                   spa, vd, fio, 0, 0);
+                       }
+               }
+               mutex_exit(&vq->vq_lock);
+       }
+}
+
 #if defined(_KERNEL) && defined(HAVE_SPL)
 EXPORT_SYMBOL(vdev_fault);
 EXPORT_SYMBOL(vdev_degrade);
@@ -3200,6 +3625,8 @@ EXPORT_SYMBOL(vdev_online);
 EXPORT_SYMBOL(vdev_offline);
 EXPORT_SYMBOL(vdev_clear);
 
-module_param(zfs_scrub_limit, int, 0644);
-MODULE_PARM_DESC(zfs_scrub_limit, "Max scrub/resilver I/O per leaf vdev");
+module_param(metaslabs_per_vdev, int, 0644);
+MODULE_PARM_DESC(metaslabs_per_vdev,
+       "Divide added vdev into approximately (but no more than) this number "
+       "of metaslabs");
 #endif