]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/vdev.c
OpenZFS 7786 - zfs`vdev_online() needs better notification about state changes
[mirror_zfs.git] / module / zfs / vdev.c
index fe66319256627925ed02aaaa08368b7963980968..da06391029fdf4d9092cb699371ba4e8fa26e0f0 100644 (file)
 
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
- * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
+ * Copyright 2017 Nexenta Systems, Inc.
+ * Copyright (c) 2014 Integros [integros.com]
+ * Copyright 2016 Toomas Soome <tsoome@me.com>
  */
 
 #include <sys/zfs_context.h>
@@ -43,7 +45,9 @@
 #include <sys/arc.h>
 #include <sys/zil.h>
 #include <sys/dsl_scan.h>
+#include <sys/abd.h>
 #include <sys/zvol.h>
+#include <sys/zfs_ratelimit.h>
 
 /*
  * When a vdev is added, it will be divided into approximately (but no
@@ -132,7 +136,8 @@ vdev_get_min_asize(vdev_t *vd)
         * so each child must provide at least 1/Nth of its asize.
         */
        if (pvd->vdev_ops == &vdev_raidz_ops)
-               return (pvd->vdev_min_asize / pvd->vdev_children);
+               return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
+                   pvd->vdev_children);
 
        return (pvd->vdev_min_asize);
 }
@@ -179,6 +184,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)
 {
@@ -325,11 +351,21 @@ vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
        vd->vdev_state = VDEV_STATE_CLOSED;
        vd->vdev_ishole = (ops == &vdev_hole_ops);
 
+       /*
+        * Initialize rate limit structs for events.  We rate limit ZIO delay
+        * and checksum events so that we don't overwhelm ZED with thousands
+        * of events when a disk is acting up.
+        */
+       zfs_ratelimit_init(&vd->vdev_delay_rl, DELAYS_PER_SECOND, 1);
+       zfs_ratelimit_init(&vd->vdev_checksum_rl, CHECKSUMS_PER_SECOND, 1);
+
        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);
+       mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
+
        for (t = 0; t < DTL_TYPES; t++) {
                vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
                    &vd->vdev_dtl_lock);
@@ -456,6 +492,11 @@ vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
        if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
            &vd->vdev_physpath) == 0)
                vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
+
+       if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
+           &vd->vdev_enc_sysfs_path) == 0)
+               vd->vdev_enc_sysfs_path = spa_strdup(vd->vdev_enc_sysfs_path);
+
        if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
                vd->vdev_fru = spa_strdup(vd->vdev_fru);
 
@@ -498,6 +539,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) {
@@ -509,9 +554,18 @@ 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)) {
@@ -628,6 +682,10 @@ vdev_free(vdev_t *vd)
                spa_strfree(vd->vdev_devid);
        if (vd->vdev_physpath)
                spa_strfree(vd->vdev_physpath);
+
+       if (vd->vdev_enc_sysfs_path)
+               spa_strfree(vd->vdev_enc_sysfs_path);
+
        if (vd->vdev_fru)
                spa_strfree(vd->vdev_fru);
 
@@ -647,6 +705,7 @@ vdev_free(vdev_t *vd)
        }
        mutex_exit(&vd->vdev_dtl_lock);
 
+       mutex_destroy(&vd->vdev_queue_lock);
        mutex_destroy(&vd->vdev_dtl_lock);
        mutex_destroy(&vd->vdev_stat_lock);
        mutex_destroy(&vd->vdev_probe_lock);
@@ -670,13 +729,16 @@ vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
 
        ASSERT(tvd == tvd->vdev_top);
 
+       tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite;
        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);
@@ -856,11 +918,11 @@ vdev_metaslab_init(vdev_t *vd, uint64_t txg)
 
        ASSERT(oldc <= newc);
 
-       mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
+       mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
 
        if (oldc != 0) {
                bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
-               kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
+               vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
        }
 
        vd->vdev_ms = mspp;
@@ -914,7 +976,7 @@ vdev_metaslab_fini(vdev_t *vd)
                        if (msp != NULL)
                                metaslab_fini(msp);
                }
-               kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
+               vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
                vd->vdev_ms = NULL;
        }
 
@@ -941,18 +1003,19 @@ vdev_probe_done(zio_t *zio)
                        vps->vps_readable = 1;
                if (zio->io_error == 0 && spa_writeable(spa)) {
                        zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
-                           zio->io_offset, zio->io_size, zio->io_data,
+                           zio->io_offset, zio->io_size, zio->io_abd,
                            ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
                            ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
                } else {
-                       zio_buf_free(zio->io_data, zio->io_size);
+                       abd_free(zio->io_abd);
                }
        } else if (zio->io_type == ZIO_TYPE_WRITE) {
                if (zio->io_error == 0)
                        vps->vps_writeable = 1;
-               zio_buf_free(zio->io_data, zio->io_size);
+               abd_free(zio->io_abd);
        } else if (zio->io_type == ZIO_TYPE_NULL) {
                zio_t *pio;
+               zio_link_t *zl;
 
                vd->vdev_cant_read |= !vps->vps_readable;
                vd->vdev_cant_write |= !vps->vps_writeable;
@@ -972,7 +1035,8 @@ vdev_probe_done(zio_t *zio)
                vd->vdev_probe_zio = NULL;
                mutex_exit(&vd->vdev_probe_lock);
 
-               while ((pio = zio_walk_parents(zio)) != NULL)
+               zl = NULL;
+               while ((pio = zio_walk_parents(zio, &zl)) != NULL)
                        if (!vdev_accessible(vd, pio))
                                pio->io_error = SET_ERROR(ENXIO);
 
@@ -1066,8 +1130,8 @@ vdev_probe(vdev_t *vd, zio_t *zio)
        for (l = 1; l < VDEV_LABELS; l++) {
                zio_nowait(zio_read_phys(pio, vd,
                    vdev_label_offset(vd->vdev_psize, l,
-                   offsetof(vdev_label_t, vl_pad2)),
-                   VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
+                   offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
+                   abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
                    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
                    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
        }
@@ -1119,19 +1183,27 @@ vdev_open_children(vdev_t *vd)
         * spa_namespace_lock
         */
        if (vdev_uses_zvols(vd)) {
+retry_sync:
                for (c = 0; c < children; c++)
                        vd->vdev_child[c]->vdev_open_error =
                            vdev_open(vd->vdev_child[c]);
-               return;
+       } else {
+               tq = taskq_create("vdev_open", children, minclsyspri,
+                   children, children, TASKQ_PREPOPULATE);
+               if (tq == NULL)
+                       goto retry_sync;
+
+               for (c = 0; c < children; c++)
+                       VERIFY(taskq_dispatch(tq, vdev_open_child,
+                           vd->vdev_child[c], TQ_SLEEP) != TASKQID_INVALID);
+
+               taskq_destroy(tq);
        }
-       tq = taskq_create("vdev_open", children, minclsyspri,
-           children, children, TASKQ_PREPOPULATE);
 
-       for (c = 0; c < children; c++)
-               VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
-                   TQ_SLEEP) != 0);
+       vd->vdev_nonrot = B_TRUE;
 
-       taskq_destroy(tq);
+       for (c = 0; c < children; c++)
+               vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
 }
 
 /*
@@ -1261,7 +1333,7 @@ vdev_open(vdev_t *vd)
        vd->vdev_psize = psize;
 
        /*
-        * Make sure the allocatable size hasn't shrunk.
+        * Make sure the allocatable size hasn't shrunk too much.
         */
        if (asize < vd->vdev_min_asize) {
                vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
@@ -1276,8 +1348,15 @@ vdev_open(vdev_t *vd)
                 */
                vd->vdev_asize = asize;
                vd->vdev_max_asize = max_asize;
-               if (vd->vdev_ashift == 0)
-                       vd->vdev_ashift = ashift;
+               if (vd->vdev_ashift == 0) {
+                       vd->vdev_ashift = ashift; /* use detected value */
+               }
+               if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
+                   vd->vdev_ashift > ASHIFT_MAX)) {
+                       vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
+                           VDEV_AUX_BAD_ASHIFT);
+                       return (SET_ERROR(EDOM));
+               }
        } else {
                /*
                 * Detect if the alignment requirement has increased.
@@ -1294,12 +1373,21 @@ vdev_open(vdev_t *vd)
        }
 
        /*
-        * If all children are healthy and the asize has increased,
-        * then we've experienced dynamic LUN growth.  If automatic
-        * expansion is enabled then use the additional space.
+        * If all children are healthy we update asize if either:
+        * The asize has increased, due to a device expansion caused by dynamic
+        * LUN growth or vdev replacement, and automatic expansion is enabled;
+        * making the additional space available.
+        *
+        * The asize has decreased, due to a device shrink usually caused by a
+        * vdev replace with a smaller device. This ensures that calculations
+        * based of max_asize and asize e.g. esize are always valid. It's safe
+        * to do this as we've already validated that asize is greater than
+        * vdev_min_asize.
         */
-       if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
-           (vd->vdev_expanding || spa->spa_autoexpand))
+       if (vd->vdev_state == VDEV_STATE_HEALTHY &&
+           ((asize > vd->vdev_asize &&
+           (vd->vdev_expanding || spa->spa_autoexpand)) ||
+           (asize < vd->vdev_asize)))
                vd->vdev_asize = asize;
 
        vdev_set_min_asize(vd);
@@ -1315,6 +1403,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,
@@ -1367,7 +1466,7 @@ vdev_validate(vdev_t *vd, boolean_t strict)
                    spa_last_synced_txg(spa) : -1ULL;
 
                if ((label = vdev_label_read_config(vd, txg)) == NULL) {
-                       vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
+                       vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
                            VDEV_AUX_BAD_LABEL);
                        return (0);
                }
@@ -1854,12 +1953,15 @@ vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
 
                /*
                 * If the vdev was resilvering and no longer has any
-                * DTLs then reset its resilvering flag.
+                * DTLs then reset its resilvering flag and dirty
+                * the top level so that we persist the change.
                 */
                if (vd->vdev_resilver_txg != 0 &&
                    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
-                   range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
+                   range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) {
                        vd->vdev_resilver_txg = 0;
+                       vdev_config_dirty(vd->vdev_top);
+               }
 
                mutex_exit(&vd->vdev_dtl_lock);
 
@@ -1936,6 +2038,51 @@ vdev_dtl_load(vdev_t *vd)
        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)
 {
@@ -1958,6 +2105,18 @@ vdev_dtl_sync(vdev_t *vd, uint64_t txg)
                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;
        }
@@ -2104,7 +2263,6 @@ vdev_load(vdev_t *vd)
            vdev_metaslab_init(vd, 0) != 0))
                vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
                    VDEV_AUX_CORRUPT_DATA);
-
        /*
         * If this is a leaf vdev, load its DTL.
         */
@@ -2164,6 +2322,8 @@ vdev_remove(vdev_t *vd, uint64_t txg)
        int m, i;
 
        tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
+       ASSERT(vd == vd->vdev_top);
+       ASSERT3U(txg, ==, spa_syncing_txg(spa));
 
        if (vd->vdev_ms != NULL) {
                metaslab_group_t *mg = vd->vdev_mg;
@@ -2205,6 +2365,11 @@ vdev_remove(vdev_t *vd, uint64_t txg)
                (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
                vd->vdev_ms_array = 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);
 }
 
@@ -2365,6 +2530,8 @@ int
 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
 {
        vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
+       boolean_t wasoffline;
+       vdev_state_t oldstate;
 
        spa_vdev_state_enter(spa, SCL_NONE);
 
@@ -2374,6 +2541,9 @@ vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
        if (!vd->vdev_ops->vdev_op_leaf)
                return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
 
+       wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
+       oldstate = vd->vdev_state;
+
        tvd = vd->vdev_top;
        vd->vdev_offline = B_FALSE;
        vd->vdev_tmpoffline = B_FALSE;
@@ -2409,6 +2579,12 @@ vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
                        return (spa_vdev_state_exit(spa, vd, ENOTSUP));
                spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
        }
+
+       if (wasoffline ||
+           (oldstate < VDEV_STATE_DEGRADED &&
+           vd->vdev_state >= VDEV_STATE_DEGRADED))
+               spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE);
+
        return (spa_vdev_state_exit(spa, vd, 0));
 }
 
@@ -2551,7 +2727,7 @@ vdev_clear(spa_t *spa, vdev_t *vd)
            !vdev_readable(vd) || !vdev_writeable(vd)) {
 
                /*
-                * When reopening in reponse to a clear event, it may be due to
+                * When reopening in response to a clear event, it may be due to
                 * a fmadm repair request.  In this case, if the device is
                 * still broken, we want to still post the ereport again.
                 */
@@ -2571,7 +2747,7 @@ vdev_clear(spa_t *spa, vdev_t *vd)
                if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
                        spa_async_request(spa, SPA_ASYNC_RESILVER);
 
-               spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_CLEAR);
+               spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
        }
 
        /*
@@ -2625,7 +2801,8 @@ vdev_allocatable(vdev_t *vd)
         * we're asking two separate questions about it.
         */
        return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
-           !vd->vdev_cant_write && !vd->vdev_ishole);
+           !vd->vdev_cant_write && !vd->vdev_ishole &&
+           vd->vdev_mg->mg_initialized);
 }
 
 boolean_t
@@ -2645,49 +2822,144 @@ 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)
 {
-       spa_t *spa = vd->vdev_spa;
-       vdev_t *rvd = 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];
 
-       ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
+               for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
+                       vsx->vsx_total_histo[t][b] +=
+                           cvsx->vsx_total_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;
-       if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
-               vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
+       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);
 
-                       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;
+               }
+       } 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)
+{
+       vdev_t *tvd = vd->vdev_top;
+       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;
+               /*
+                * Report expandable space on top-level, non-auxillary devices
+                * only. The expandable space is reported in terms of metaslab
+                * sized units since that determines how much space the pool
+                * can expand.
+                */
+               if (vd->vdev_aux == NULL && tvd != NULL) {
+                       vs->vs_esize = P2ALIGN(
+                           vd->vdev_max_asize - vd->vdev_asize,
+                           1ULL << tvd->vdev_ms_shift);
+               }
+               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)
 {
@@ -2721,6 +2993,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;
 
@@ -2771,8 +3044,33 @@ 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 &&
+                   (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
+
+                       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;
@@ -3109,6 +3407,17 @@ vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
        spa_t *spa = vd->vdev_spa;
 
        if (state == vd->vdev_state) {
+               /*
+                * Since vdev_offline() code path is already in an offline
+                * state we can miss a statechange event to OFFLINE. Check
+                * the previous state to catch this condition.
+                */
+               if (vd->vdev_ops->vdev_op_leaf &&
+                   (state == VDEV_STATE_OFFLINE) &&
+                   (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
+                       /* post an offline state change */
+                       zfs_post_state_change(spa, vd, vd->vdev_prevstate);
+               }
                vd->vdev_stat.vs_aux = aux;
                return;
        }
@@ -3132,19 +3441,6 @@ vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
            vd->vdev_ops->vdev_op_leaf)
                vd->vdev_ops->vdev_op_close(vd);
 
-       /*
-        * If we have brought this vdev back into service, we need
-        * to notify fmd so that it can gracefully repair any outstanding
-        * cases due to a missing device.  We do this in all cases, even those
-        * that probably don't correlate to a repaired fault.  This is sure to
-        * catch all cases, and we let the zfs-retire agent sort it out.  If
-        * this is a transient state it's OK, as the retire agent will
-        * double-check the state of the vdev before repairing it.
-        */
-       if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
-           vd->vdev_prevstate != state)
-               zfs_post_state_change(spa, vd);
-
        if (vd->vdev_removed &&
            state == VDEV_STATE_CANT_OPEN &&
            (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
@@ -3212,6 +3508,9 @@ vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
                        case VDEV_AUX_BAD_LABEL:
                                class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
                                break;
+                       case VDEV_AUX_BAD_ASHIFT:
+                               class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
+                               break;
                        default:
                                class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
                        }
@@ -3225,44 +3524,44 @@ vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
                vd->vdev_removed = B_FALSE;
        }
 
+       /*
+        * Notify ZED of any significant state-change on a leaf vdev.
+        *
+        */
+       if (vd->vdev_ops->vdev_op_leaf) {
+               /* preserve original state from a vdev_reopen() */
+               if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
+                   (vd->vdev_prevstate != vd->vdev_state) &&
+                   (save_state <= VDEV_STATE_CLOSED))
+                       save_state = vd->vdev_prevstate;
+
+               /* filter out state change due to initial vdev_open */
+               if (save_state > VDEV_STATE_CLOSED)
+                       zfs_post_state_change(spa, vd, save_state);
+       }
+
        if (!isopen && vd->vdev_parent)
                vdev_propagate_state(vd->vdev_parent);
 }
 
 /*
  * Check the vdev configuration to ensure that it's capable of supporting
- * a root pool.
+ * a root pool. We do not support partial configuration.
  */
 boolean_t
 vdev_is_bootable(vdev_t *vd)
 {
-#if defined(__sun__) || defined(__sun)
-       /*
-        * Currently, we do not support RAID-Z or partial configuration.
-        * In addition, only a single top-level vdev is allowed and none of the
-        * leaves can be wholedisks.
-        */
-       int c;
-
        if (!vd->vdev_ops->vdev_op_leaf) {
-               char *vdev_type = vd->vdev_ops->vdev_op_type;
+               const char *vdev_type = vd->vdev_ops->vdev_op_type;
 
-               if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
-                   vd->vdev_children > 1) {
+               if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0)
                        return (B_FALSE);
-               } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
-                   strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
-                       return (B_FALSE);
-               }
-       } else if (vd->vdev_wholedisk == 1) {
-               return (B_FALSE);
        }
 
-       for (c = 0; c < vd->vdev_children; c++) {
+       for (int c = 0; c < vd->vdev_children; c++) {
                if (!vdev_is_bootable(vd->vdev_child[c]))
                        return (B_FALSE);
        }
-#endif /* __sun__ || __sun */
        return (B_TRUE);
 }
 
@@ -3397,9 +3696,10 @@ EXPORT_SYMBOL(vdev_degrade);
 EXPORT_SYMBOL(vdev_online);
 EXPORT_SYMBOL(vdev_offline);
 EXPORT_SYMBOL(vdev_clear);
-
+/* BEGIN CSTYLED */
 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");
+/* END CSTYLED */
 #endif