]> git.proxmox.com Git - mirror_zfs.git/blobdiff - cmd/zpool/zpool_main.c
Fix locale-specific time
[mirror_zfs.git] / cmd / zpool / zpool_main.c
index cd1c66323af1aa21a8871c849fbaeef48c9fa24e..d670cd1afeb1bf53de1e83fe3b0ea4ff712c4d96 100644 (file)
@@ -6,7 +6,7 @@
  * You may not use this file except in compliance with the License.
  *
  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
- * or http://www.opensolaris.org/os/licensing.
+ * or https://opensource.org/licenses/CDDL-1.0.
  * See the License for the specific language governing permissions
  * and limitations under the License.
  *
@@ -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) 2011, 2020 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
  * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
  * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
  * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
@@ -31,6 +31,9 @@
  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
  * Copyright (c) 2017, Intel Corporation.
  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
+ * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
+ * Copyright (c) 2021, Klara Inc.
+ * Copyright [2021] Hewlett Packard Enterprise Development LP
  */
 
 #include <assert.h>
@@ -47,7 +50,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 #include <time.h>
 #include <unistd.h>
 #include <pwd.h>
@@ -124,6 +126,18 @@ static int zpool_do_version(int, char **);
 
 static int zpool_do_wait(int, char **);
 
+static int zpool_do_help(int argc, char **argv);
+
+static zpool_compat_status_t zpool_do_load_compat(
+    const char *, boolean_t *);
+
+enum zpool_options {
+       ZPOOL_OPTION_POWER = 1024,
+       ZPOOL_OPTION_ALLOW_INUSE,
+       ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH,
+       ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH
+};
+
 /*
  * These libumem hooks provide a reasonable set of defaults for the allocator's
  * debugging facilities.
@@ -206,7 +220,7 @@ enum iostat_type {
  * of all the nvlists a flag requires.  Also specifies the order in
  * which data gets printed in zpool iostat.
  */
-static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
+static const char *vsx_type_to_nvlist[IOS_COUNT][15] = {
        [IOS_L_HISTO] = {
            ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
            ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
@@ -218,6 +232,7 @@ static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
            ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
            ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
            ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
            NULL},
        [IOS_LATENCY] = {
            ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
@@ -225,6 +240,7 @@ static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
            ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
            ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
            ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
            NULL},
        [IOS_QUEUES] = {
            ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
@@ -233,6 +249,7 @@ static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
            ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
            ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
            ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
+           ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
            NULL},
        [IOS_RQ_HISTO] = {
            ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
@@ -247,6 +264,8 @@ static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
            ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
            ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
            ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
+           ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
+           ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
            NULL},
 };
 
@@ -325,6 +344,7 @@ static zpool_command_t command_table[] = {
 #define        VDEV_ALLOC_CLASS_LOGS   "logs"
 
 static zpool_command_t *current_command;
+static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
 static char history_str[HIS_MAX_RECORD_LEN];
 static boolean_t log_history = B_TRUE;
 static uint_t timestamp_fmt = NODATE;
@@ -334,13 +354,13 @@ get_usage(zpool_help_t idx)
 {
        switch (idx) {
        case HELP_ADD:
-               return (gettext("\tadd [-fgLnP] [-o property=value] "
+               return (gettext("\tadd [-afgLnP] [-o property=value] "
                    "<pool> <vdev> ...\n"));
        case HELP_ATTACH:
                return (gettext("\tattach [-fsw] [-o property=value] "
                    "<pool> <device> <new-device>\n"));
        case HELP_CLEAR:
-               return (gettext("\tclear [-nF] <pool> [device]\n"));
+               return (gettext("\tclear [[--power]|[-nF]] <pool> [device]\n"));
        case HELP_CREATE:
                return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
                    "\t    [-O file-system-property=value] ... \n"
@@ -376,9 +396,11 @@ get_usage(zpool_help_t idx)
                    "[-T d|u] [pool] ... \n"
                    "\t    [interval [count]]\n"));
        case HELP_OFFLINE:
-               return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
+               return (gettext("\toffline [--power]|[[-f][-t]] <pool> "
+                   "<device> ...\n"));
        case HELP_ONLINE:
-               return (gettext("\tonline [-e] <pool> <device> ...\n"));
+               return (gettext("\tonline [--power][-e] <pool> <device> "
+                   "...\n"));
        case HELP_REPLACE:
                return (gettext("\treplace [-fsw] [-o property=value] "
                    "<pool> <device> [new-device]\n"));
@@ -387,18 +409,18 @@ get_usage(zpool_help_t idx)
        case HELP_REOPEN:
                return (gettext("\treopen [-n] <pool>\n"));
        case HELP_INITIALIZE:
-               return (gettext("\tinitialize [-c | -s] [-w] <pool> "
+               return (gettext("\tinitialize [-c | -s | -u] [-w] <pool> "
                    "[<device> ...]\n"));
        case HELP_SCRUB:
-               return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
+               return (gettext("\tscrub [-s | -p] [-w] [-e] <pool> ...\n"));
        case HELP_RESILVER:
                return (gettext("\tresilver <pool> ...\n"));
        case HELP_TRIM:
                return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
                    "[<device> ...]\n"));
        case HELP_STATUS:
-               return (gettext("\tstatus [-c [script1,script2,...]] "
-                   "[-igLpPstvxD]  [-T d|u] [pool] ... \n"
+               return (gettext("\tstatus [--power] [-c [script1,script2,...]] "
+                   "[-DegiLpPstvx] [-T d|u] [pool] ...\n"
                    "\t    [interval [count]]\n"));
        case HELP_UPGRADE:
                return (gettext("\tupgrade\n"
@@ -410,7 +432,8 @@ get_usage(zpool_help_t idx)
                return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
                    "<\"all\" | property[,...]> <pool> ...\n"));
        case HELP_SET:
-               return (gettext("\tset <property=value> <pool> \n"));
+               return (gettext("\tset <property=value> <pool>\n"
+                   "\tset <vdev_property=value> <pool> <vdev>\n"));
        case HELP_SPLIT:
                return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
                    "\t    [-o property=value] <pool> <newpool> "
@@ -424,10 +447,9 @@ get_usage(zpool_help_t idx)
        case HELP_WAIT:
                return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
                    "<pool> [interval]\n"));
+       default:
+               __builtin_unreachable();
        }
-
-       abort();
-       /* NOTREACHED */
 }
 
 static void
@@ -461,7 +483,7 @@ zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
  * Callback routine that will print out a pool property value.
  */
 static int
-print_prop_cb(int prop, void *cb)
+print_pool_prop_cb(int prop, void *cb)
 {
        FILE *fp = cb;
 
@@ -480,12 +502,106 @@ print_prop_cb(int prop, void *cb)
        return (ZPROP_CONT);
 }
 
+/*
+ * Callback routine that will print out a vdev property value.
+ */
+static int
+print_vdev_prop_cb(int prop, void *cb)
+{
+       FILE *fp = cb;
+
+       (void) fprintf(fp, "\t%-19s  ", vdev_prop_to_name(prop));
+
+       if (vdev_prop_readonly(prop))
+               (void) fprintf(fp, "  NO   ");
+       else
+               (void) fprintf(fp, " YES   ");
+
+       if (vdev_prop_values(prop) == NULL)
+               (void) fprintf(fp, "-\n");
+       else
+               (void) fprintf(fp, "%s\n", vdev_prop_values(prop));
+
+       return (ZPROP_CONT);
+}
+
+/*
+ * Given a leaf vdev name like 'L5' return its VDEV_CONFIG_PATH like
+ * '/dev/disk/by-vdev/L5'.
+ */
+static const char *
+vdev_name_to_path(zpool_handle_t *zhp, char *vdev)
+{
+       nvlist_t *vdev_nv = zpool_find_vdev(zhp, vdev, NULL, NULL, NULL);
+       if (vdev_nv == NULL) {
+               return (NULL);
+       }
+       return (fnvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_PATH));
+}
+
+static int
+zpool_power_on(zpool_handle_t *zhp, char *vdev)
+{
+       return (zpool_power(zhp, vdev, B_TRUE));
+}
+
+static int
+zpool_power_on_and_disk_wait(zpool_handle_t *zhp, char *vdev)
+{
+       int rc;
+
+       rc = zpool_power_on(zhp, vdev);
+       if (rc != 0)
+               return (rc);
+
+       zpool_disk_wait(vdev_name_to_path(zhp, vdev));
+
+       return (0);
+}
+
+static int
+zpool_power_on_pool_and_wait_for_devices(zpool_handle_t *zhp)
+{
+       nvlist_t *nv;
+       const char *path = NULL;
+       int rc;
+
+       /* Power up all the devices first */
+       FOR_EACH_REAL_LEAF_VDEV(zhp, nv) {
+               path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
+               if (path != NULL) {
+                       rc = zpool_power_on(zhp, (char *)path);
+                       if (rc != 0) {
+                               return (rc);
+                       }
+               }
+       }
+
+       /*
+        * Wait for their devices to show up.  Since we powered them on
+        * at roughly the same time, they should all come online around
+        * the same time.
+        */
+       FOR_EACH_REAL_LEAF_VDEV(zhp, nv) {
+               path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
+               zpool_disk_wait(path);
+       }
+
+       return (0);
+}
+
+static int
+zpool_power_off(zpool_handle_t *zhp, char *vdev)
+{
+       return (zpool_power(zhp, vdev, B_FALSE));
+}
+
 /*
  * Display usage message.  If we're inside a command, display only the usage for
  * that command.  Otherwise, iterate over the entire command table and display
  * a complete usage message.
  */
-static void
+static __attribute__((noreturn)) void
 usage(boolean_t requested)
 {
        FILE *fp = requested ? stdout : stderr;
@@ -504,31 +620,43 @@ usage(boolean_t requested)
                                (void) fprintf(fp, "%s",
                                    get_usage(command_table[i].usage));
                }
+
+               (void) fprintf(fp,
+                   gettext("\nFor further help on a command or topic, "
+                   "run: %s\n"), "zpool help [<topic>]");
        } else {
                (void) fprintf(fp, gettext("usage:\n"));
                (void) fprintf(fp, "%s", get_usage(current_command->usage));
        }
 
        if (current_command != NULL &&
+           current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) &&
            ((strcmp(current_command->name, "set") == 0) ||
            (strcmp(current_command->name, "get") == 0) ||
            (strcmp(current_command->name, "list") == 0))) {
 
-               (void) fprintf(fp,
+               (void) fprintf(fp, "%s",
                    gettext("\nthe following properties are supported:\n"));
 
                (void) fprintf(fp, "\n\t%-19s  %s   %s\n\n",
                    "PROPERTY", "EDIT", "VALUES");
 
                /* Iterate over all properties */
-               (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
-                   ZFS_TYPE_POOL);
+               if (current_prop_type == ZFS_TYPE_POOL) {
+                       (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE,
+                           B_TRUE, current_prop_type);
 
-               (void) fprintf(fp, "\t%-19s   ", "feature@...");
-               (void) fprintf(fp, "YES   disabled | enabled | active\n");
+                       (void) fprintf(fp, "\t%-19s   ", "feature@...");
+                       (void) fprintf(fp, "YES   "
+                           "disabled | enabled | active\n");
 
-               (void) fprintf(fp, gettext("\nThe feature@ properties must be "
-                   "appended with a feature name.\nSee zpool-features(5).\n"));
+                       (void) fprintf(fp, gettext("\nThe feature@ properties "
+                           "must be appended with a feature name.\n"
+                           "See zpool-features(7).\n"));
+               } else if (current_prop_type == ZFS_TYPE_VDEV) {
+                       (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE,
+                           B_TRUE, current_prop_type);
+               }
        }
 
        /*
@@ -543,12 +671,13 @@ usage(boolean_t requested)
 }
 
 /*
- * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
+ * zpool initialize [-c | -s | -u] [-w] <pool> [<vdev> ...]
  * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
  * if none specified.
  *
  *     -c      Cancel. Ends active initializing.
  *     -s      Suspend. Initializing can then be restarted with no flags.
+ *     -u      Uninitialize. Clears initialization state.
  *     -w      Wait. Blocks until initializing has completed.
  */
 int
@@ -564,12 +693,14 @@ zpool_do_initialize(int argc, char **argv)
        struct option long_options[] = {
                {"cancel",      no_argument,            NULL, 'c'},
                {"suspend",     no_argument,            NULL, 's'},
+               {"uninit",      no_argument,            NULL, 'u'},
                {"wait",        no_argument,            NULL, 'w'},
                {0, 0, 0, 0}
        };
 
        pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
-       while ((c = getopt_long(argc, argv, "csw", long_options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "csuw", long_options,
+           NULL)) != -1) {
                switch (c) {
                case 'c':
                        if (cmd_type != POOL_INITIALIZE_START &&
@@ -589,6 +720,15 @@ zpool_do_initialize(int argc, char **argv)
                        }
                        cmd_type = POOL_INITIALIZE_SUSPEND;
                        break;
+               case 'u':
+                       if (cmd_type != POOL_INITIALIZE_START &&
+                           cmd_type != POOL_INITIALIZE_UNINIT) {
+                               (void) fprintf(stderr, gettext("-u cannot be "
+                                   "combined with other options\n"));
+                               usage(B_FALSE);
+                       }
+                       cmd_type = POOL_INITIALIZE_UNINIT;
+                       break;
                case 'w':
                        wait = B_TRUE;
                        break;
@@ -615,8 +755,8 @@ zpool_do_initialize(int argc, char **argv)
        }
 
        if (wait && (cmd_type != POOL_INITIALIZE_START)) {
-               (void) fprintf(stderr, gettext("-w cannot be used with -c or "
-                   "-s\n"));
+               (void) fprintf(stderr, gettext("-w cannot be used with -c, -s"
+                   "or -u\n"));
                usage(B_FALSE);
        }
 
@@ -669,8 +809,15 @@ print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
        }
 
        for (c = 0; c < children; c++) {
-               uint64_t is_log = B_FALSE;
-               char *class = "";
+               uint64_t is_log = B_FALSE, is_hole = B_FALSE;
+               const char *class = "";
+
+               (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
+                   &is_hole);
+
+               if (is_hole == B_TRUE) {
+                       continue;
+               }
 
                (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
                    &is_log);
@@ -692,6 +839,54 @@ print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
        }
 }
 
+/*
+ * Print the list of l2cache devices for dry runs.
+ */
+static void
+print_cache_list(nvlist_t *nv, int indent)
+{
+       nvlist_t **child;
+       uint_t c, children;
+
+       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
+           &child, &children) == 0 && children > 0) {
+               (void) printf("\t%*s%s\n", indent, "", "cache");
+       } else {
+               return;
+       }
+       for (c = 0; c < children; c++) {
+               char *vname;
+
+               vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
+               (void) printf("\t%*s%s\n", indent + 2, "", vname);
+               free(vname);
+       }
+}
+
+/*
+ * Print the list of spares for dry runs.
+ */
+static void
+print_spare_list(nvlist_t *nv, int indent)
+{
+       nvlist_t **child;
+       uint_t c, children;
+
+       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
+           &child, &children) == 0 && children > 0) {
+               (void) printf("\t%*s%s\n", indent, "", "spares");
+       } else {
+               return;
+       }
+       for (c = 0; c < children; c++) {
+               char *vname;
+
+               vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
+               (void) printf("\t%*s%s\n", indent + 2, "", vname);
+               free(vname);
+       }
+}
+
 static boolean_t
 prop_list_contains_feature(nvlist_t *proplist)
 {
@@ -708,13 +903,13 @@ prop_list_contains_feature(nvlist_t *proplist)
  * Add a property pair (name, string-value) into a property nvlist.
  */
 static int
-add_prop_list(const char *propname, char *propval, nvlist_t **props,
+add_prop_list(const char *propname, const char *propval, nvlist_t **props,
     boolean_t poolprop)
 {
        zpool_prop_t prop = ZPOOL_PROP_INVAL;
        nvlist_t *proplist;
        const char *normnm;
-       char *strval;
+       const char *strval;
 
        if (*props == NULL &&
            nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
@@ -727,11 +922,14 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
 
        if (poolprop) {
                const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
+               const char *cname =
+                   zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY);
 
                if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
-                   !zpool_prop_feature(propname)) {
+                   (!zpool_prop_feature(propname) &&
+                   !zpool_prop_vdev(propname))) {
                        (void) fprintf(stderr, gettext("property '%s' is "
-                           "not a valid pool property\n"), propname);
+                           "not a valid pool or vdev property\n"), propname);
                        return (2);
                }
 
@@ -749,8 +947,24 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
                        return (2);
                }
 
+               /*
+                * if version is specified, only "legacy" compatibility
+                * may be requested
+                */
+               if ((prop == ZPOOL_PROP_COMPATIBILITY &&
+                   strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 &&
+                   nvlist_exists(proplist, vname)) ||
+                   (prop == ZPOOL_PROP_VERSION &&
+                   nvlist_exists(proplist, cname) &&
+                   strcmp(fnvlist_lookup_string(proplist, cname),
+                   ZPOOL_COMPAT_LEGACY) != 0)) {
+                       (void) fprintf(stderr, gettext("when 'version' is "
+                           "specified, the 'compatibility' feature may only "
+                           "be set to '" ZPOOL_COMPAT_LEGACY "'\n"));
+                       return (2);
+               }
 
-               if (zpool_prop_feature(propname))
+               if (zpool_prop_feature(propname) || zpool_prop_vdev(propname))
                        normnm = propname;
                else
                        normnm = zpool_prop_to_name(prop);
@@ -790,10 +1004,10 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
  * Set a default property pair (name, string-value) in a property nvlist
  */
 static int
-add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
-    boolean_t poolprop)
+add_prop_list_default(const char *propname, const char *propval,
+    nvlist_t **props)
 {
-       char *pval;
+       const char *pval;
 
        if (nvlist_lookup_string(*props, propname, &pval) == 0)
                return (0);
@@ -802,8 +1016,9 @@ add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
 }
 
 /*
- * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
+ * zpool add [-afgLnP] [-o property=value] <pool> <vdev> ...
  *
+ *     -a      Disable the ashift validation checks
  *     -f      Force addition of devices, even if they appear in use
  *     -g      Display guid for individual vdev name.
  *     -L      Follow links when resolving vdev path name.
@@ -819,8 +1034,11 @@ add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
 int
 zpool_do_add(int argc, char **argv)
 {
-       boolean_t force = B_FALSE;
+       boolean_t check_replication = B_TRUE;
+       boolean_t check_inuse = B_TRUE;
        boolean_t dryrun = B_FALSE;
+       boolean_t check_ashift = B_TRUE;
+       boolean_t force = B_FALSE;
        int name_flags = 0;
        int c;
        nvlist_t *nvroot;
@@ -831,8 +1049,18 @@ zpool_do_add(int argc, char **argv)
        nvlist_t *props = NULL;
        char *propval;
 
+       struct option long_options[] = {
+               {"allow-in-use", no_argument, NULL, ZPOOL_OPTION_ALLOW_INUSE},
+               {"allow-replication-mismatch", no_argument, NULL,
+                   ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH},
+               {"allow-ashift-mismatch", no_argument, NULL,
+                   ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH},
+               {0, 0, 0, 0}
+       };
+
        /* check options */
-       while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
+       while ((c = getopt_long(argc, argv, "fgLno:P", long_options, NULL))
+           != -1) {
                switch (c) {
                case 'f':
                        force = B_TRUE;
@@ -862,6 +1090,15 @@ zpool_do_add(int argc, char **argv)
                case 'P':
                        name_flags |= VDEV_NAME_PATH;
                        break;
+               case ZPOOL_OPTION_ALLOW_INUSE:
+                       check_inuse = B_FALSE;
+                       break;
+               case ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH:
+                       check_replication = B_FALSE;
+                       break;
+               case ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH:
+                       check_ashift = B_FALSE;
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -882,6 +1119,19 @@ zpool_do_add(int argc, char **argv)
                usage(B_FALSE);
        }
 
+       if (force) {
+               if (!check_inuse || !check_replication || !check_ashift) {
+                       (void) fprintf(stderr, gettext("'-f' option is not "
+                           "allowed with '--allow-replication-mismatch', "
+                           "'--allow-ashift-mismatch', or "
+                           "'--allow-in-use'\n"));
+                       usage(B_FALSE);
+               }
+               check_inuse = B_FALSE;
+               check_replication = B_FALSE;
+               check_ashift = B_FALSE;
+       }
+
        poolname = argv[0];
 
        argc--;
@@ -912,8 +1162,8 @@ zpool_do_add(int argc, char **argv)
        }
 
        /* pass off to make_root_vdev for processing */
-       nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
-           argc, argv);
+       nvroot = make_root_vdev(zhp, props, !check_inuse,
+           check_replication, B_FALSE, dryrun, argc, argv);
        if (nvroot == NULL) {
                zpool_close(zhp);
                return (1);
@@ -921,16 +1171,16 @@ zpool_do_add(int argc, char **argv)
 
        if (dryrun) {
                nvlist_t *poolnvroot;
-               nvlist_t **l2child;
-               uint_t l2children, c;
+               nvlist_t **l2child, **sparechild;
+               uint_t l2children, sparechildren, c;
                char *vname;
-               boolean_t hadcache = B_FALSE;
+               boolean_t hadcache = B_FALSE, hadspare = B_FALSE;
 
                verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
                    &poolnvroot) == 0);
 
                (void) printf(gettext("would update '%s' to the following "
-                   "configuration:\n"), zpool_get_name(zhp));
+                   "configuration:\n\n"), zpool_get_name(zhp));
 
                /* print original main pool and new tree */
                print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
@@ -991,10 +1241,33 @@ zpool_do_add(int argc, char **argv)
                                free(vname);
                        }
                }
+               /* And finally the spares */
+               if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES,
+                   &sparechild, &sparechildren) == 0 && sparechildren > 0) {
+                       hadspare = B_TRUE;
+                       (void) printf(gettext("\tspares\n"));
+                       for (c = 0; c < sparechildren; c++) {
+                               vname = zpool_vdev_name(g_zfs, NULL,
+                                   sparechild[c], name_flags);
+                               (void) printf("\t  %s\n", vname);
+                               free(vname);
+                       }
+               }
+               if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
+                   &sparechild, &sparechildren) == 0 && sparechildren > 0) {
+                       if (!hadspare)
+                               (void) printf(gettext("\tspares\n"));
+                       for (c = 0; c < sparechildren; c++) {
+                               vname = zpool_vdev_name(g_zfs, NULL,
+                                   sparechild[c], name_flags);
+                               (void) printf("\t  %s\n", vname);
+                               free(vname);
+                       }
+               }
 
                ret = 0;
        } else {
-               ret = (zpool_add(zhp, nvroot) != 0);
+               ret = (zpool_add(zhp, nvroot, check_ashift) != 0);
        }
 
        nvlist_free(props);
@@ -1058,6 +1331,7 @@ zpool_do_remove(int argc, char **argv)
                return (1);
 
        if (stop && noop) {
+               zpool_close(zhp);
                (void) fprintf(stderr, gettext("stop request ignored\n"));
                return (0);
        }
@@ -1114,6 +1388,26 @@ zpool_do_remove(int argc, char **argv)
        return (ret);
 }
 
+/*
+ * Return 1 if a vdev is active (being used in a pool)
+ * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
+ *
+ * This is useful for checking if a disk in an active pool is offlined or
+ * faulted.
+ */
+static int
+vdev_is_active(char *vdev_path)
+{
+       int fd;
+       fd = open(vdev_path, O_EXCL);
+       if (fd < 0) {
+               return (1);   /* cant open O_EXCL - disk is active */
+       }
+
+       close(fd);
+       return (0);   /* disk is inactive in the pool */
+}
+
 /*
  * zpool labelclear [-f] <vdev>
  *
@@ -1128,7 +1422,6 @@ zpool_do_labelclear(int argc, char **argv)
 {
        char vdev[MAXPATHLEN];
        char *name = NULL;
-       struct stat st;
        int c, fd = -1, ret = 0;
        nvlist_t *config;
        pool_state_t state;
@@ -1161,14 +1454,20 @@ zpool_do_labelclear(int argc, char **argv)
                usage(B_FALSE);
        }
 
+       (void) strlcpy(vdev, argv[0], sizeof (vdev));
+
        /*
-        * Check if we were given absolute path and use it as is.
+        * If we cannot open an absolute path, we quit.
         * Otherwise if the provided vdev name doesn't point to a file,
         * try prepending expected disk paths and partition numbers.
         */
-       (void) strlcpy(vdev, argv[0], sizeof (vdev));
-       if (vdev[0] != '/' && stat(vdev, &st) != 0) {
+       if ((fd = open(vdev, O_RDWR)) < 0) {
                int error;
+               if (vdev[0] == '/') {
+                       (void) fprintf(stderr, gettext("failed to open "
+                           "%s: %s\n"), vdev, strerror(errno));
+                       return (1);
+               }
 
                error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
                if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
@@ -1176,20 +1475,21 @@ zpool_do_labelclear(int argc, char **argv)
                                error = ENOENT;
                }
 
-               if (error || (stat(vdev, &st) != 0)) {
-                       (void) fprintf(stderr, gettext(
-                           "failed to find device %s, try specifying absolute "
-                           "path instead\n"), argv[0]);
+               if (error || ((fd = open(vdev, O_RDWR)) < 0)) {
+                       if (errno == ENOENT) {
+                               (void) fprintf(stderr, gettext(
+                                   "failed to find device %s, try "
+                                   "specifying absolute path instead\n"),
+                                   argv[0]);
+                               return (1);
+                       }
+
+                       (void) fprintf(stderr, gettext("failed to open %s:"
+                           " %s\n"), vdev, strerror(errno));
                        return (1);
                }
        }
 
-       if ((fd = open(vdev, O_RDWR)) < 0) {
-               (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
-                   vdev, strerror(errno));
-               return (1);
-       }
-
        /*
         * Flush all dirty pages for the block device.  This should not be
         * fatal when the device does not support BLKFLSBUF as would be the
@@ -1223,9 +1523,23 @@ zpool_do_labelclear(int argc, char **argv)
        case POOL_STATE_ACTIVE:
        case POOL_STATE_SPARE:
        case POOL_STATE_L2CACHE:
+               /*
+                * We allow the user to call 'zpool offline -f'
+                * on an offlined disk in an active pool. We can check if
+                * the disk is online by calling vdev_is_active().
+                */
+               if (force && !vdev_is_active(vdev))
+                       break;
+
                (void) fprintf(stderr, gettext(
-                   "%s is a member (%s) of pool \"%s\"\n"),
+                   "%s is a member (%s) of pool \"%s\""),
                    vdev, zpool_pool_state_to_name(state), name);
+
+               if (force) {
+                       (void) fprintf(stderr, gettext(
+                           ". Offline the disk first to clear its label."));
+               }
+               printf("\n");
                ret = 1;
                goto errout;
 
@@ -1296,13 +1610,15 @@ zpool_do_create(int argc, char **argv)
 {
        boolean_t force = B_FALSE;
        boolean_t dryrun = B_FALSE;
-       boolean_t enable_all_pool_feat = B_TRUE;
+       boolean_t enable_pool_features = B_TRUE;
+
        int c;
        nvlist_t *nvroot = NULL;
        char *poolname;
        char *tname = NULL;
        int ret = 1;
        char *altroot = NULL;
+       char *compat = NULL;
        char *mountpoint = NULL;
        nvlist_t *fsprops = NULL;
        nvlist_t *props = NULL;
@@ -1318,7 +1634,7 @@ zpool_do_create(int argc, char **argv)
                        dryrun = B_TRUE;
                        break;
                case 'd':
-                       enable_all_pool_feat = B_FALSE;
+                       enable_pool_features = B_FALSE;
                        break;
                case 'R':
                        altroot = optarg;
@@ -1326,7 +1642,7 @@ zpool_do_create(int argc, char **argv)
                            ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
                                goto errout;
                        if (add_prop_list_default(zpool_prop_to_name(
-                           ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
+                           ZPOOL_PROP_CACHEFILE), "none", &props))
                                goto errout;
                        break;
                case 'm':
@@ -1356,11 +1672,14 @@ zpool_do_create(int argc, char **argv)
                                ver = strtoull(propval, &end, 10);
                                if (*end == '\0' &&
                                    ver < SPA_VERSION_FEATURES) {
-                                       enable_all_pool_feat = B_FALSE;
+                                       enable_pool_features = B_FALSE;
                                }
                        }
                        if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
                                altroot = propval;
+                       if (zpool_name_to_prop(optarg) ==
+                           ZPOOL_PROP_COMPATIBILITY)
+                               compat = propval;
                        break;
                case 'O':
                        if ((propval = strchr(optarg, '=')) == NULL) {
@@ -1401,7 +1720,7 @@ zpool_do_create(int argc, char **argv)
                            ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
                                goto errout;
                        if (add_prop_list_default(zpool_prop_to_name(
-                           ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
+                           ZPOOL_PROP_CACHEFILE), "none", &props))
                                goto errout;
                        tname = optarg;
                        break;
@@ -1548,32 +1867,58 @@ zpool_do_create(int argc, char **argv)
                    VDEV_ALLOC_BIAS_SPECIAL, 0);
                print_vdev_tree(NULL, "logs", nvroot, 0,
                    VDEV_ALLOC_BIAS_LOG, 0);
+               print_cache_list(nvroot, 0);
+               print_spare_list(nvroot, 0);
 
                ret = 0;
        } else {
                /*
-                * Hand off to libzfs.
+                * Load in feature set.
+                * Note: if compatibility property not given, we'll have
+                * NULL, which means 'all features'.
                 */
-               spa_feature_t i;
-               for (i = 0; i < SPA_FEATURES; i++) {
+               boolean_t requested_features[SPA_FEATURES];
+               if (zpool_do_load_compat(compat, requested_features) !=
+                   ZPOOL_COMPATIBILITY_OK)
+                       goto errout;
+
+               /*
+                * props contains list of features to enable.
+                * For each feature:
+                *  - remove it if feature@name=disabled
+                *  - leave it there if feature@name=enabled
+                *  - add it if:
+                *    - enable_pool_features (ie: no '-d' or '-o version')
+                *    - it's supported by the kernel module
+                *    - it's in the requested feature set
+                *  - warn if it's enabled but not in compat
+                */
+               for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
                        char propname[MAXPATHLEN];
-                       char *propval;
+                       const char *propval;
                        zfeature_info_t *feat = &spa_feature_table[i];
 
                        (void) snprintf(propname, sizeof (propname),
                            "feature@%s", feat->fi_uname);
 
-                       /*
-                        * Only features contained in props will be enabled:
-                        * remove from the nvlist every ZFS_FEATURE_DISABLED
-                        * value and add every missing ZFS_FEATURE_ENABLED if
-                        * enable_all_pool_feat is set.
-                        */
                        if (!nvlist_lookup_string(props, propname, &propval)) {
-                               if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
+                               if (strcmp(propval,
+                                   ZFS_FEATURE_DISABLED) == 0) {
                                        (void) nvlist_remove_all(props,
                                            propname);
-                       } else if (enable_all_pool_feat) {
+                               } else if (strcmp(propval,
+                                   ZFS_FEATURE_ENABLED) == 0 &&
+                                   !requested_features[i]) {
+                                       (void) fprintf(stderr, gettext(
+                                           "Warning: feature \"%s\" enabled "
+                                           "but is not in specified "
+                                           "'compatibility' feature set.\n"),
+                                           feat->fi_uname);
+                               }
+                       } else if (
+                           enable_pool_features &&
+                           feat->fi_zfs_mod_supported &&
+                           requested_features[i]) {
                                ret = add_prop_list(propname,
                                    ZFS_FEATURE_ENABLED, &props, B_TRUE);
                                if (ret != 0)
@@ -1588,8 +1933,8 @@ zpool_do_create(int argc, char **argv)
                            tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
                        if (pool != NULL) {
                                if (zfs_mount(pool, NULL, 0) == 0) {
-                                       ret = zfs_shareall(pool);
-                                       zfs_commit_all_shares();
+                                       ret = zfs_share(pool, NULL);
+                                       zfs_commit_shares(NULL);
                                }
                                zfs_close(pool);
                        }
@@ -1762,7 +2107,7 @@ zpool_do_export(int argc, char **argv)
                }
 
                return (for_each_pool(argc, argv, B_TRUE, NULL,
-                   zpool_export_one, &cb));
+                   ZFS_TYPE_POOL, B_FALSE, zpool_export_one, &cb));
        }
 
        /* check arguments */
@@ -1771,7 +2116,8 @@ zpool_do_export(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
+       ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, zpool_export_one, &cb);
 
        return (ret);
 }
@@ -1784,40 +2130,21 @@ static int
 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
     int name_flags)
 {
-       char *name;
-       nvlist_t **child;
-       uint_t c, children;
-       int ret;
-
-       name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
-       if (strlen(name) + depth > max)
-               max = strlen(name) + depth;
+       static const char *const subtypes[] =
+           {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN};
 
+       char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
+       max = MAX(strlen(name) + depth, max);
        free(name);
 
-       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
-           &child, &children) == 0) {
-               for (c = 0; c < children; c++)
-                       if ((ret = max_width(zhp, child[c], depth + 2,
-                           max, name_flags)) > max)
-                               max = ret;
-       }
-
-       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
-           &child, &children) == 0) {
-               for (c = 0; c < children; c++)
-                       if ((ret = max_width(zhp, child[c], depth + 2,
-                           max, name_flags)) > max)
-                               max = ret;
-       }
-
-       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
-           &child, &children) == 0) {
-               for (c = 0; c < children; c++)
-                       if ((ret = max_width(zhp, child[c], depth + 2,
-                           max, name_flags)) > max)
-                               max = ret;
-       }
+       nvlist_t **child;
+       uint_t children;
+       for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i)
+               if (nvlist_lookup_nvlist_array(nv, subtypes[i],
+                   &child, &children) == 0)
+                       for (uint_t c = 0; c < children; ++c)
+                               max = MAX(max_width(zhp, child[c], depth + 2,
+                                   max, name_flags), max);
 
        return (max);
 }
@@ -1877,32 +2204,32 @@ typedef struct status_cbdata {
        boolean_t       cb_explain;
        boolean_t       cb_first;
        boolean_t       cb_dedup_stats;
+       boolean_t       cb_print_unhealthy;
        boolean_t       cb_print_status;
        boolean_t       cb_print_slow_ios;
        boolean_t       cb_print_vdev_init;
        boolean_t       cb_print_vdev_trim;
        vdev_cmd_data_list_t    *vcdl;
+       boolean_t       cb_print_power;
 } status_cbdata_t;
 
 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
-static int
-is_blank_str(char *str)
+static boolean_t
+is_blank_str(const char *str)
 {
-       while (str != NULL && *str != '\0') {
+       for (; str != NULL && *str != '\0'; ++str)
                if (!isblank(*str))
-                       return (0);
-               str++;
-       }
-       return (1);
+                       return (B_FALSE);
+       return (B_TRUE);
 }
 
 /* Print command output lines for specific vdev in a specific pool */
 static void
-zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
+zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path)
 {
        vdev_cmd_data_t *data;
        int i, j;
-       char *val;
+       const char *val;
 
        for (i = 0; i < vcdl->count; i++) {
                if ((strcmp(vcdl->data[i].path, path) != 0) ||
@@ -1928,22 +2255,22 @@ zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
                         * Mark empty values with dashes to make output
                         * awk-able.
                         */
-                       if (is_blank_str(val))
+                       if (val == NULL || is_blank_str(val))
                                val = "-";
 
                        printf("%*s", vcdl->uniq_cols_width[j], val);
                        if (j < vcdl->uniq_cols_cnt - 1)
-                               printf("  ");
+                               fputs("  ", stdout);
                }
 
                /* Print out any values that aren't in a column at the end */
                for (j = data->cols_cnt; j < data->lines_cnt; j++) {
                        /* Did we have any columns?  If so print a spacer. */
                        if (vcdl->uniq_cols_cnt > 0)
-                               printf("  ");
+                               fputs("  ", stdout);
 
                        val = data->lines[j];
-                       printf("%s", val ? val : "");
+                       fputs(val ?: "", stdout);
                }
                break;
        }
@@ -1962,7 +2289,6 @@ print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
                    !vs->vs_scan_removing) {
                        char zbuf[1024];
                        char tbuf[256];
-                       struct tm zaction_ts;
 
                        time_t t = vs->vs_initialize_action_time;
                        int initialize_pct = 100;
@@ -1972,8 +2298,8 @@ print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
                                    100 / (vs->vs_initialize_bytes_est + 1));
                        }
 
-                       (void) localtime_r(&t, &zaction_ts);
-                       (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
+                       (void) ctime_r(&t, tbuf);
+                       tbuf[24] = 0;
 
                        switch (vs->vs_initialize_state) {
                        case VDEV_INITIALIZE_SUSPENDED:
@@ -2013,7 +2339,6 @@ print_status_trim(vdev_stat_t *vs, boolean_t verbose)
                    !vs->vs_scan_removing) {
                        char zbuf[1024];
                        char tbuf[256];
-                       struct tm zaction_ts;
 
                        time_t t = vs->vs_trim_action_time;
                        int trim_pct = 100;
@@ -2022,8 +2347,8 @@ print_status_trim(vdev_stat_t *vs, boolean_t verbose)
                                    100 / (vs->vs_trim_bytes_est + 1));
                        }
 
-                       (void) localtime_r(&t, &zaction_ts);
-                       (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
+                       (void) ctime_r(&t, tbuf);
+                       tbuf[24] = 0;
 
                        switch (vs->vs_trim_state) {
                        case VDEV_TRIM_SUSPENDED:
@@ -2056,7 +2381,7 @@ print_status_trim(vdev_stat_t *vs, boolean_t verbose)
  * Return the color associated with a health string.  This includes returning
  * NULL for no color change.
  */
-static char *
+static const char *
 health_str_to_color(const char *health)
 {
        if (strcmp(health, gettext("FAULTED")) == 0 ||
@@ -2074,6 +2399,35 @@ health_str_to_color(const char *health)
        return (NULL);
 }
 
+/*
+ * Called for each leaf vdev.  Returns 0 if the vdev is healthy.
+ * A vdev is unhealthy if any of the following are true:
+ * 1) there are read, write, or checksum errors,
+ * 2) its state is not ONLINE, or
+ * 3) slow IO reporting was requested (-s) and there are slow IOs.
+ */
+static int
+vdev_health_check_cb(void *hdl_data, nvlist_t *nv, void *data)
+{
+       status_cbdata_t *cb = data;
+       vdev_stat_t *vs;
+       uint_t vsc;
+       (void) hdl_data;
+
+       if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
+           (uint64_t **)&vs, &vsc) != 0)
+               return (1);
+
+       if (vs->vs_checksum_errors || vs->vs_read_errors ||
+           vs->vs_write_errors || vs->vs_state != VDEV_STATE_HEALTHY)
+               return (1);
+
+       if (cb->cb_print_slow_ios && vs->vs_slow_ios)
+               return (1);
+
+       return (0);
+}
+
 /*
  * Print out configuration state as requested by status_callback.
  */
@@ -2090,9 +2444,10 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
        uint64_t notpresent;
        spare_cbdata_t spare_cb;
        const char *state;
-       char *type;
-       char *path = NULL;
-       char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL;
+       const char *type;
+       const char *path = NULL;
+       const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL,
+           *scolor = NULL;
 
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
            &child, &children) != 0)
@@ -2119,6 +2474,15 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                        state = gettext("AVAIL");
        }
 
+       /*
+        * If '-e' is specified then top-level vdevs and their children
+        * can be pruned if all of their leaves are healthy.
+        */
+       if (cb->cb_print_unhealthy && depth > 0 &&
+           for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) {
+               return;
+       }
+
        printf_color(health_str_to_color(state),
            "\t%*s%-*s  %-8s", depth, "", cb->cb_namewidth - depth,
            name, state);
@@ -2133,14 +2497,17 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                if (vs->vs_checksum_errors)
                        ccolor = ANSI_RED;
 
+               if (vs->vs_slow_ios)
+                       scolor = ANSI_BLUE;
+
                if (cb->cb_literal) {
-                       printf(" ");
+                       fputc(' ', stdout);
                        printf_color(rcolor, "%5llu",
                            (u_longlong_t)vs->vs_read_errors);
-                       printf(" ");
+                       fputc(' ', stdout);
                        printf_color(wcolor, "%5llu",
                            (u_longlong_t)vs->vs_write_errors);
-                       printf(" ");
+                       fputc(' ', stdout);
                        printf_color(ccolor, "%5llu",
                            (u_longlong_t)vs->vs_checksum_errors);
                } else {
@@ -2148,11 +2515,11 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                        zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
                        zfs_nicenum(vs->vs_checksum_errors, cbuf,
                            sizeof (cbuf));
-                       printf(" ");
+                       fputc(' ', stdout);
                        printf_color(rcolor, "%5s", rbuf);
-                       printf(" ");
+                       fputc(' ', stdout);
                        printf_color(wcolor, "%5s", wbuf);
-                       printf(" ");
+                       fputc(' ', stdout);
                        printf_color(ccolor, "%5s", cbuf);
                }
                if (cb->cb_print_slow_ios) {
@@ -2165,9 +2532,30 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                        }
 
                        if (cb->cb_literal)
-                               printf(" %5llu", (u_longlong_t)vs->vs_slow_ios);
+                               printf_color(scolor, " %5llu",
+                                   (u_longlong_t)vs->vs_slow_ios);
                        else
-                               printf(" %5s", rbuf);
+                               printf_color(scolor, " %5s", rbuf);
+               }
+               if (cb->cb_print_power) {
+                       if (children == 0)  {
+                               /* Only leaf vdevs have physical slots */
+                               switch (zpool_power_current_state(zhp, (char *)
+                                   fnvlist_lookup_string(nv,
+                                   ZPOOL_CONFIG_PATH))) {
+                               case 0:
+                                       printf_color(ANSI_RED, " %5s",
+                                           gettext("off"));
+                                       break;
+                               case 1:
+                                       printf(" %5s", gettext("on"));
+                                       break;
+                               default:
+                                       printf(" %5s", "-");
+                               }
+                       } else {
+                               printf(" %5s", "-");
+                       }
                }
        }
 
@@ -2222,7 +2610,13 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                        break;
 
                case VDEV_AUX_ERR_EXCEEDED:
-                       (void) printf(gettext("too many errors"));
+                       if (vs->vs_read_errors + vs->vs_write_errors +
+                           vs->vs_checksum_errors == 0 && children == 0 &&
+                           vs->vs_slow_ios > 0) {
+                               (void) printf(gettext("too many slow I/Os"));
+                       } else {
+                               (void) printf(gettext("too many errors"));
+                       }
                        break;
 
                case VDEV_AUX_IO_FAILURE:
@@ -2249,11 +2643,28 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                        (void) printf(gettext("all children offline"));
                        break;
 
+               case VDEV_AUX_BAD_LABEL:
+                       (void) printf(gettext("invalid label"));
+                       break;
+
                default:
                        (void) printf(gettext("corrupted data"));
                        break;
                }
                color_end();
+       } else if (children == 0 && !isspare &&
+           getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL &&
+           VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
+           vs->vs_configured_ashift < vs->vs_physical_ashift) {
+               (void) printf(
+                   gettext("  block size: %dB configured, %dB native"),
+                   1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
+       }
+
+       if (vs->vs_scan_removing != 0) {
+               (void) printf(gettext("  (removing)"));
+       } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) {
+               (void) printf(gettext("  (non-allocating)"));
        }
 
        /* The root vdev has the scrub/resilver stats */
@@ -2262,7 +2673,14 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
        (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
            (uint64_t **)&ps, &c);
 
-       if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) {
+       /*
+        * If you force fault a drive that's resilvering, its scan stats can
+        * get frozen in time, giving the false impression that it's
+        * being resilvered.  That's why we check the state to see if the vdev
+        * is healthy before reporting "resilvering" or "repairing".
+        */
+       if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 &&
+           vs->vs_state == VDEV_STATE_HEALTHY) {
                if (vs->vs_scan_processed != 0) {
                        (void) printf(gettext("  (%s)"),
                            (ps->pss_func == POOL_SCAN_RESILVER) ?
@@ -2274,7 +2692,7 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
 
        /* The top-level vdevs have the rebuild stats */
        if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE &&
-           children == 0) {
+           children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) {
                if (vs->vs_rebuild_processed != 0) {
                        (void) printf(gettext("  (resilvering)"));
                }
@@ -2287,7 +2705,7 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                }
        }
 
-       /* Display vdev initialization and trim status for leaves */
+       /* Display vdev initialization and trim status for leaves. */
        if (children == 0) {
                print_status_initialize(vs, cb->cb_print_vdev_init);
                print_status_trim(vs, cb->cb_print_vdev_trim);
@@ -2335,7 +2753,8 @@ print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
        nvlist_t **child;
        uint_t c, children;
        vdev_stat_t *vs;
-       char *type, *vname;
+       const char *type;
+       char *vname;
 
        verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
        if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
@@ -2384,6 +2803,10 @@ print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
                        (void) printf(gettext("all children offline"));
                        break;
 
+               case VDEV_AUX_BAD_LABEL:
+                       (void) printf(gettext("invalid label"));
+                       break;
+
                default:
                        (void) printf(gettext("corrupted data"));
                        break;
@@ -2459,14 +2882,14 @@ print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
 
        for (c = 0; c < children; c++) {
                uint64_t is_log = B_FALSE;
-               char *bias = NULL;
-               char *type = NULL;
+               const char *bias = NULL;
+               const char *type = NULL;
 
                (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
                    &is_log);
 
                if (is_log) {
-                       bias = VDEV_ALLOC_CLASS_LOGS;
+                       bias = (char *)VDEV_ALLOC_CLASS_LOGS;
                } else {
                        (void) nvlist_lookup_string(child[c],
                            ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
@@ -2498,22 +2921,22 @@ print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
 /*
  * Display the status for the given pool.
  */
-static void
-show_import(nvlist_t *config)
+static int
+show_import(nvlist_t *config, boolean_t report_error)
 {
        uint64_t pool_state;
        vdev_stat_t *vs;
-       char *name;
+       const char *name;
        uint64_t guid;
        uint64_t hostid = 0;
-       char *msgid;
-       char *hostname = "unknown";
+       const char *msgid;
+       const char *hostname = "unknown";
        nvlist_t *nvroot, *nvinfo;
        zpool_status_t reason;
        zpool_errata_t errata;
        const char *health;
        uint_t vsc;
-       char *comment;
+       const char *comment;
        status_cbdata_t cb = { 0 };
 
        verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
@@ -2531,6 +2954,13 @@ show_import(nvlist_t *config)
 
        reason = zpool_import_status(config, &msgid, &errata);
 
+       /*
+        * If we're importing using a cachefile, then we won't report any
+        * errors unless we are in the scan phase of the import.
+        */
+       if (reason != ZPOOL_STATUS_OK && !report_error)
+               return (reason);
+
        (void) printf(gettext("   pool: %s\n"), name);
        (void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
        (void) printf(gettext("  state: %s"), health);
@@ -2585,8 +3015,24 @@ show_import(nvlist_t *config)
 
        case ZPOOL_STATUS_FEAT_DISABLED:
                printf_color(ANSI_BOLD, gettext("status: "));
-               printf_color(ANSI_YELLOW, gettext("Some supported features are "
-                   "not enabled on the pool.\n"));
+               printf_color(ANSI_YELLOW, gettext("Some supported "
+                   "features are not enabled on the pool.\n\t"
+                   "(Note that they may be intentionally disabled "
+                   "if the\n\t'compatibility' property is set.)\n"));
+               break;
+
+       case ZPOOL_STATUS_COMPATIBILITY_ERR:
+               printf_color(ANSI_BOLD, gettext("status: "));
+               printf_color(ANSI_YELLOW, gettext("Error reading or parsing "
+                   "the file(s) indicated by the 'compatibility'\n"
+                   "property.\n"));
+               break;
+
+       case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
+               printf_color(ANSI_BOLD, gettext("status: "));
+               printf_color(ANSI_YELLOW, gettext("One or more features "
+                   "are enabled on the pool despite not being\n"
+                   "requested by the 'compatibility' property.\n"));
                break;
 
        case ZPOOL_STATUS_UNSUP_FEAT_READ:
@@ -2654,6 +3100,13 @@ show_import(nvlist_t *config)
                    errata);
                break;
 
+       case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
+               printf_color(ANSI_BOLD, gettext("status: "));
+               printf_color(ANSI_YELLOW, gettext("One or more devices are "
+                   "configured to use a non-native block size.\n"
+                   "\tExpect reduced performance.\n"));
+               break;
+
        default:
                /*
                 * No other status can be seen when importing pools.
@@ -2671,6 +3124,12 @@ show_import(nvlist_t *config)
                            "imported using its name or numeric identifier, "
                            "though\n\tsome features will not be available "
                            "without an explicit 'zpool upgrade'.\n"));
+               } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) {
+                       (void) printf(gettext(" action: The pool can be "
+                           "imported using its name or numeric\n\tidentifier, "
+                           "though the file(s) indicated by its "
+                           "'compatibility'\n\tproperty cannot be parsed at "
+                           "this time.\n"));
                } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
                        (void) printf(gettext(" action: The pool can be "
                            "imported using its name or numeric "
@@ -2780,9 +3239,8 @@ show_import(nvlist_t *config)
                                    ZPOOL_CONFIG_MMP_HOSTID);
 
                        (void) printf(gettext(" action: The pool must be "
-                           "exported from %s (hostid=%lx)\n\tbefore it "
-                           "can be safely imported.\n"), hostname,
-                           (unsigned long) hostid);
+                           "exported from %s (hostid=%"PRIx64")\n\tbefore it "
+                           "can be safely imported.\n"), hostname, hostid);
                        break;
                case ZPOOL_STATUS_HOSTID_REQUIRED:
                        (void) printf(gettext(" action: Set a unique system "
@@ -2838,6 +3296,7 @@ show_import(nvlist_t *config)
                    "be part of this pool, though their\n\texact "
                    "configuration cannot be determined.\n"));
        }
+       return (0);
 }
 
 static boolean_t
@@ -2848,12 +3307,22 @@ zfs_force_import_required(nvlist_t *config)
        nvlist_t *nvinfo;
 
        state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
-       (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
+       nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
+
+       /*
+        * The hostid on LOAD_INFO comes from the MOS label via
+        * spa_tryimport(). If its not there then we're likely talking to an
+        * older kernel, so use the top one, which will be from the label
+        * discovered in zpool_find_import(), or if a cachefile is in use, the
+        * local hostid.
+        */
+       if (nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_HOSTID, &hostid) != 0)
+               (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
+                   &hostid);
 
        if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
                return (B_TRUE);
 
-       nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
        if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
                mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
                    ZPOOL_CONFIG_MMP_STATE);
@@ -2875,8 +3344,9 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
     nvlist_t *props, int flags)
 {
        int ret = 0;
+       int ms_status = 0;
        zpool_handle_t *zhp;
-       char *name;
+       const char *name;
        uint64_t version;
 
        name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
@@ -2897,7 +3367,7 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
                            ZPOOL_CONFIG_MMP_STATE);
 
                if (mmp_state == MMP_STATE_ACTIVE) {
-                       char *hostname = "<unknown>";
+                       const char *hostname = "<unknown>";
                        uint64_t hostid = 0;
 
                        if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
@@ -2910,20 +3380,23 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
 
                        (void) fprintf(stderr, gettext("cannot import '%s': "
                            "pool is imported on %s (hostid: "
-                           "0x%lx)\nExport the pool on the other system, "
-                           "then run 'zpool import'.\n"),
-                           name, hostname, (unsigned long) hostid);
+                           "0x%"PRIx64")\nExport the pool on the other "
+                           "system, then run 'zpool import'.\n"),
+                           name, hostname, hostid);
                } else if (mmp_state == MMP_STATE_NO_HOSTID) {
                        (void) fprintf(stderr, gettext("Cannot import '%s': "
                            "pool has the multihost property on and the\n"
                            "system's hostid is not set. Set a unique hostid "
                            "with the zgenhostid(8) command.\n"), name);
                } else {
-                       char *hostname = "<unknown>";
-                       uint64_t timestamp = 0;
+                       const char *hostname = "<unknown>";
+                       time_t timestamp = 0;
                        uint64_t hostid = 0;
 
-                       if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
+                       if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTNAME))
+                               hostname = fnvlist_lookup_string(nvinfo,
+                                   ZPOOL_CONFIG_HOSTNAME);
+                       else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
                                hostname = fnvlist_lookup_string(config,
                                    ZPOOL_CONFIG_HOSTNAME);
 
@@ -2931,16 +3404,19 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
                                timestamp = fnvlist_lookup_uint64(config,
                                    ZPOOL_CONFIG_TIMESTAMP);
 
-                       if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
+                       if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTID))
+                               hostid = fnvlist_lookup_uint64(nvinfo,
+                                   ZPOOL_CONFIG_HOSTID);
+                       else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
                                hostid = fnvlist_lookup_uint64(config,
                                    ZPOOL_CONFIG_HOSTID);
 
                        (void) fprintf(stderr, gettext("cannot import '%s': "
                            "pool was previously in use from another system.\n"
-                           "Last accessed by %s (hostid=%lx) at %s"
+                           "Last accessed by %s (hostid=%"PRIx64") at %s"
                            "The pool can be imported, use 'zpool import -f' "
                            "to import the pool.\n"), name, hostname,
-                           (unsigned long)hostid, ctime((time_t *)&timestamp));
+                           hostid, ctime(&timestamp));
                }
 
                return (1);
@@ -2950,7 +3426,7 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
                return (1);
 
        if (newname != NULL)
-               name = (char *)newname;
+               name = newname;
 
        if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
                return (1);
@@ -2959,23 +3435,141 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
         * Loading keys is best effort. We don't want to return immediately
         * if it fails but we do want to give the error to the caller.
         */
-       if (flags & ZFS_IMPORT_LOAD_KEYS) {
-               ret = zfs_crypto_attempt_load_keys(g_zfs, name);
-               if (ret != 0)
+       if (flags & ZFS_IMPORT_LOAD_KEYS &&
+           zfs_crypto_attempt_load_keys(g_zfs, name) != 0)
                        ret = 1;
-       }
 
        if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
-           !(flags & ZFS_IMPORT_ONLY) &&
-           zpool_enable_datasets(zhp, mntopts, 0) != 0) {
-               zpool_close(zhp);
-               return (1);
+           !(flags & ZFS_IMPORT_ONLY)) {
+               ms_status = zpool_enable_datasets(zhp, mntopts, 0);
+               if (ms_status == EZFS_SHAREFAILED) {
+                       (void) fprintf(stderr, gettext("Import was "
+                           "successful, but unable to share some datasets"));
+               } else if (ms_status == EZFS_MOUNTFAILED) {
+                       (void) fprintf(stderr, gettext("Import was "
+                           "successful, but unable to mount some datasets"));
+               }
        }
 
        zpool_close(zhp);
        return (ret);
 }
 
+static int
+import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
+    char *orig_name, char *new_name,
+    boolean_t do_destroyed, boolean_t pool_specified, boolean_t do_all,
+    importargs_t *import)
+{
+       nvlist_t *config = NULL;
+       nvlist_t *found_config = NULL;
+       uint64_t pool_state;
+
+       /*
+        * At this point we have a list of import candidate configs. Even if
+        * we were searching by pool name or guid, we still need to
+        * post-process the list to deal with pool state and possible
+        * duplicate names.
+        */
+       int err = 0;
+       nvpair_t *elem = NULL;
+       boolean_t first = B_TRUE;
+       while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
+
+               verify(nvpair_value_nvlist(elem, &config) == 0);
+
+               verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
+                   &pool_state) == 0);
+               if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
+                       continue;
+               if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
+                       continue;
+
+               verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
+                   import->policy) == 0);
+
+               if (!pool_specified) {
+                       if (first)
+                               first = B_FALSE;
+                       else if (!do_all)
+                               (void) fputc('\n', stdout);
+
+                       if (do_all) {
+                               err |= do_import(config, NULL, mntopts,
+                                   props, flags);
+                       } else {
+                               /*
+                                * If we're importing from cachefile, then
+                                * we don't want to report errors until we
+                                * are in the scan phase of the import. If
+                                * we get an error, then we return that error
+                                * to invoke the scan phase.
+                                */
+                               if (import->cachefile && !import->scan)
+                                       err = show_import(config, B_FALSE);
+                               else
+                                       (void) show_import(config, B_TRUE);
+                       }
+               } else if (import->poolname != NULL) {
+                       const char *name;
+
+                       /*
+                        * We are searching for a pool based on name.
+                        */
+                       verify(nvlist_lookup_string(config,
+                           ZPOOL_CONFIG_POOL_NAME, &name) == 0);
+
+                       if (strcmp(name, import->poolname) == 0) {
+                               if (found_config != NULL) {
+                                       (void) fprintf(stderr, gettext(
+                                           "cannot import '%s': more than "
+                                           "one matching pool\n"),
+                                           import->poolname);
+                                       (void) fprintf(stderr, gettext(
+                                           "import by numeric ID instead\n"));
+                                       err = B_TRUE;
+                               }
+                               found_config = config;
+                       }
+               } else {
+                       uint64_t guid;
+
+                       /*
+                        * Search for a pool by guid.
+                        */
+                       verify(nvlist_lookup_uint64(config,
+                           ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
+
+                       if (guid == import->guid)
+                               found_config = config;
+               }
+       }
+
+       /*
+        * If we were searching for a specific pool, verify that we found a
+        * pool, and then do the import.
+        */
+       if (pool_specified && err == 0) {
+               if (found_config == NULL) {
+                       (void) fprintf(stderr, gettext("cannot import '%s': "
+                           "no such pool available\n"), orig_name);
+                       err = B_TRUE;
+               } else {
+                       err |= do_import(found_config, new_name,
+                           mntopts, props, flags);
+               }
+       }
+
+       /*
+        * If we were just looking for pools, report an error if none were
+        * found.
+        */
+       if (!pool_specified && first)
+               (void) fprintf(stderr,
+                   gettext("no pools available to import\n"));
+       return (err);
+}
+
 typedef struct target_exists_args {
        const char      *poolname;
        uint64_t        poolguid;
@@ -2992,7 +3586,7 @@ name_or_guid_exists(zpool_handle_t *zhp, void *data)
                return (0);
 
        if (args->poolname != NULL) {
-               char *pool_name;
+               const char *pool_name;
 
                verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
                    &pool_name) == 0);
@@ -3103,51 +3697,54 @@ zpool_do_checkpoint(int argc, char **argv)
 /*
  * zpool import [-d dir] [-D]
  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
- *              [-d dir | -c cachefile] [-f] -a
+ *              [-d dir | -c cachefile | -s] [-f] -a
  *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
- *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
+ *              [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
+ *              [newpool]
  *
- *      -c     Read pool information from a cachefile instead of searching
- *             devices.
+ *     -c      Read pool information from a cachefile instead of searching
+ *             devices. If importing from a cachefile config fails, then
+ *             fallback to searching for devices only in the directories that
+ *             exist in the cachefile.
  *
- *       -d    Scan in a specific directory, other than /dev/.  More than
+ *     -d      Scan in a specific directory, other than /dev/.  More than
  *             one directory can be specified using multiple '-d' options.
  *
- *       -D     Scan for previously destroyed pools or import all or only
- *              specified destroyed pools.
+ *     -D      Scan for previously destroyed pools or import all or only
+ *             specified destroyed pools.
  *
- *       -R    Temporarily import the pool, with all mountpoints relative to
+ *     -R      Temporarily import the pool, with all mountpoints relative to
  *             the given root.  The pool will remain exported when the machine
  *             is rebooted.
  *
- *       -V    Import even in the presence of faulted vdevs.  This is an
- *             intentionally undocumented option for testing purposes, and
- *             treats the pool configuration as complete, leaving any bad
+ *     -V      Import even in the presence of faulted vdevs.  This is an
+ *             intentionally undocumented option for testing purposes, and
+ *             treats the pool configuration as complete, leaving any bad
  *             vdevs in the FAULTED state. In other words, it does verbatim
  *             import.
  *
- *       -f    Force import, even if it appears that the pool is active.
+ *     -f      Force import, even if it appears that the pool is active.
  *
- *       -F     Attempt rewind if necessary.
+ *     -F      Attempt rewind if necessary.
  *
- *       -n     See if rewind would work, but don't actually rewind.
+ *     -n      See if rewind would work, but don't actually rewind.
  *
- *       -N     Import the pool but don't mount datasets.
+ *     -N      Import the pool but don't mount datasets.
  *
- *       -T     Specify a starting txg to use for import. This option is
- *             intentionally undocumented option for testing purposes.
+ *     -T      Specify a starting txg to use for import. This option is
+ *             intentionally undocumented option for testing purposes.
  *
- *       -a    Import all pools found.
+ *     -a      Import all pools found.
  *
- *       -l    Load encryption keys while importing.
+ *     -l      Load encryption keys while importing.
  *
- *       -o    Set property=value and/or temporary mount options (without '=').
+ *     -o      Set property=value and/or temporary mount options (without '=').
  *
- *      -s     Scan using the default search path, the libblkid cache will
- *             not be consulted.
+ *     -s      Scan using the default search path, the libblkid cache will
+ *             not be consulted.
  *
- *       --rewind-to-checkpoint
- *             Import the pool and revert back to the checkpoint.
+ *     --rewind-to-checkpoint
+ *             Import the pool and revert back to the checkpoint.
  *
  * The import command scans for pools to import, and import pools based on pool
  * name and GUID.  The pool can also be renamed as part of the import process.
@@ -3164,15 +3761,11 @@ zpool_do_import(int argc, char **argv)
        boolean_t do_all = B_FALSE;
        boolean_t do_destroyed = B_FALSE;
        char *mntopts = NULL;
-       nvpair_t *elem;
-       nvlist_t *config;
        uint64_t searchguid = 0;
        char *searchname = NULL;
        char *propval;
-       nvlist_t *found_config;
        nvlist_t *policy = NULL;
        nvlist_t *props = NULL;
-       boolean_t first;
        int flags = ZFS_IMPORT_NORMAL;
        uint32_t rewind_policy = ZPOOL_NO_REWIND;
        boolean_t dryrun = B_FALSE;
@@ -3180,7 +3773,8 @@ zpool_do_import(int argc, char **argv)
        boolean_t xtreme_rewind = B_FALSE;
        boolean_t do_scan = B_FALSE;
        boolean_t pool_exists = B_FALSE;
-       uint64_t pool_state, txg = -1ULL;
+       boolean_t pool_specified = B_FALSE;
+       uint64_t txg = -1ULL;
        char *cachefile = NULL;
        importargs_t idata = { 0 };
        char *endptr;
@@ -3201,16 +3795,8 @@ zpool_do_import(int argc, char **argv)
                        cachefile = optarg;
                        break;
                case 'd':
-                       if (searchdirs == NULL) {
-                               searchdirs = safe_malloc(sizeof (char *));
-                       } else {
-                               char **tmp = safe_malloc((nsearch + 1) *
-                                   sizeof (char *));
-                               bcopy(searchdirs, tmp, nsearch *
-                                   sizeof (char *));
-                               free(searchdirs);
-                               searchdirs = tmp;
-                       }
+                       searchdirs = safe_realloc(searchdirs,
+                           (nsearch + 1) * sizeof (char *));
                        searchdirs[nsearch++] = optarg;
                        break;
                case 'D':
@@ -3250,7 +3836,7 @@ zpool_do_import(int argc, char **argv)
                            ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
                                goto error;
                        if (add_prop_list_default(zpool_prop_to_name(
-                           ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
+                           ZPOOL_PROP_CACHEFILE), "none", &props))
                                goto error;
                        break;
                case 's':
@@ -3259,7 +3845,7 @@ zpool_do_import(int argc, char **argv)
                case 't':
                        flags |= ZFS_IMPORT_TEMP_NAME;
                        if (add_prop_list_default(zpool_prop_to_name(
-                           ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
+                           ZPOOL_PROP_CACHEFILE), "none", &props))
                                goto error;
                        break;
 
@@ -3302,6 +3888,11 @@ zpool_do_import(int argc, char **argv)
                usage(B_FALSE);
        }
 
+       if (cachefile && do_scan) {
+               (void) fprintf(stderr, gettext("-c is incompatible with -s\n"));
+               usage(B_FALSE);
+       }
+
        if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
                (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
                usage(B_FALSE);
@@ -3352,9 +3943,8 @@ zpool_do_import(int argc, char **argv)
        if (argc == 0 && geteuid() != 0) {
                (void) fprintf(stderr, gettext("cannot "
                    "discover pools: permission denied\n"));
-               if (searchdirs != NULL)
-                       free(searchdirs);
 
+               free(searchdirs);
                nvlist_free(props);
                nvlist_free(policy);
                return (1);
@@ -3382,7 +3972,7 @@ zpool_do_import(int argc, char **argv)
                        searchname = argv[0];
                        searchguid = 0;
                }
-               found_config = NULL;
+               pool_specified = B_TRUE;
 
                /*
                 * User specified a name or guid.  Ensure it's unique.
@@ -3395,24 +3985,16 @@ zpool_do_import(int argc, char **argv)
         * Check the environment for the preferred search path.
         */
        if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
-               char *dir;
+               char *dir, *tmp = NULL;
 
                envdup = strdup(env);
 
-               dir = strtok(envdup, ":");
-               while (dir != NULL) {
-                       if (searchdirs == NULL) {
-                               searchdirs = safe_malloc(sizeof (char *));
-                       } else {
-                               char **tmp = safe_malloc((nsearch + 1) *
-                                   sizeof (char *));
-                               bcopy(searchdirs, tmp, nsearch *
-                                   sizeof (char *));
-                               free(searchdirs);
-                               searchdirs = tmp;
-                       }
+               for (dir = strtok_r(envdup, ":", &tmp);
+                   dir != NULL;
+                   dir = strtok_r(NULL, ":", &tmp)) {
+                       searchdirs = safe_realloc(searchdirs,
+                           (nsearch + 1) * sizeof (char *));
                        searchdirs[nsearch++] = dir;
-                       dir = strtok(NULL, ":");
                }
        }
 
@@ -3424,7 +4006,12 @@ zpool_do_import(int argc, char **argv)
        idata.scan = do_scan;
        idata.policy = policy;
 
-       pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops);
+       libpc_handle_t lpch = {
+               .lpc_lib_handle = g_zfs,
+               .lpc_ops = &libzfs_config_ops,
+               .lpc_printerr = B_TRUE
+       };
+       pools = zpool_search_import(&lpch, &idata);
 
        if (pools != NULL && pool_exists &&
            (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
@@ -3451,116 +4038,49 @@ zpool_do_import(int argc, char **argv)
        }
 
        if (err == 1) {
-               if (searchdirs != NULL)
-                       free(searchdirs);
-               if (envdup != NULL)
-                       free(envdup);
+               free(searchdirs);
+               free(envdup);
                nvlist_free(policy);
                nvlist_free(pools);
                nvlist_free(props);
                return (1);
        }
 
+       err = import_pools(pools, props, mntopts, flags,
+           argc >= 1 ? argv[0] : NULL,
+           argc >= 2 ? argv[1] : NULL,
+           do_destroyed, pool_specified, do_all, &idata);
+
        /*
-        * At this point we have a list of import candidate configs. Even if
-        * we were searching by pool name or guid, we still need to
-        * post-process the list to deal with pool state and possible
-        * duplicate names.
+        * If we're using the cachefile and we failed to import, then
+        * fallback to scanning the directory for pools that match
+        * those in the cachefile.
         */
-       err = 0;
-       elem = NULL;
-       first = B_TRUE;
-       while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
-
-               verify(nvpair_value_nvlist(elem, &config) == 0);
-
-               verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
-                   &pool_state) == 0);
-               if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
-                       continue;
-               if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
-                       continue;
-
-               verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
-                   policy) == 0);
-
-               if (argc == 0) {
-                       if (first)
-                               first = B_FALSE;
-                       else if (!do_all)
-                               (void) printf("\n");
-
-                       if (do_all) {
-                               err |= do_import(config, NULL, mntopts,
-                                   props, flags);
-                       } else {
-                               show_import(config);
-                       }
-               } else if (searchname != NULL) {
-                       char *name;
-
-                       /*
-                        * We are searching for a pool based on name.
-                        */
-                       verify(nvlist_lookup_string(config,
-                           ZPOOL_CONFIG_POOL_NAME, &name) == 0);
-
-                       if (strcmp(name, searchname) == 0) {
-                               if (found_config != NULL) {
-                                       (void) fprintf(stderr, gettext(
-                                           "cannot import '%s': more than "
-                                           "one matching pool\n"), searchname);
-                                       (void) fprintf(stderr, gettext(
-                                           "import by numeric ID instead\n"));
-                                       err = B_TRUE;
-                               }
-                               found_config = config;
-                       }
-               } else {
-                       uint64_t guid;
-
-                       /*
-                        * Search for a pool by guid.
-                        */
-                       verify(nvlist_lookup_uint64(config,
-                           ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
+       if (err != 0 && cachefile != NULL) {
+               (void) printf(gettext("cachefile import failed, retrying\n"));
 
-                       if (guid == searchguid)
-                               found_config = config;
-               }
-       }
+               /*
+                * We use the scan flag to gather the directories that exist
+                * in the cachefile. If we need to fallback to searching for
+                * the pool config, we will only search devices in these
+                * directories.
+                */
+               idata.scan = B_TRUE;
+               nvlist_free(pools);
+               pools = zpool_search_import(&lpch, &idata);
 
-       /*
-        * If we were searching for a specific pool, verify that we found a
-        * pool, and then do the import.
-        */
-       if (argc != 0 && err == 0) {
-               if (found_config == NULL) {
-                       (void) fprintf(stderr, gettext("cannot import '%s': "
-                           "no such pool available\n"), argv[0]);
-                       err = B_TRUE;
-               } else {
-                       err |= do_import(found_config, argc == 1 ? NULL :
-                           argv[1], mntopts, props, flags);
-               }
+               err = import_pools(pools, props, mntopts, flags,
+                   argc >= 1 ? argv[0] : NULL,
+                   argc >= 2 ? argv[1] : NULL,
+                   do_destroyed, pool_specified, do_all, &idata);
        }
 
-       /*
-        * If we were just looking for pools, report an error if none were
-        * found.
-        */
-       if (argc == 0 && first)
-               (void) fprintf(stderr,
-                   gettext("no pools available to import\n"));
-
 error:
        nvlist_free(props);
        nvlist_free(pools);
        nvlist_free(policy);
-       if (searchdirs != NULL)
-               free(searchdirs);
-       if (envdup != NULL)
-               free(envdup);
+       free(searchdirs);
+       free(envdup);
 
        return (err ? 1 : 0);
 }
@@ -3599,23 +4119,22 @@ zpool_do_sync(int argc, char **argv)
        argv += optind;
 
        /* if argc == 0 we will execute zpool_sync_one on all pools */
-       ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
+       ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, zpool_sync_one, &force);
 
        return (ret);
 }
 
 typedef struct iostat_cbdata {
        uint64_t cb_flags;
-       int cb_name_flags;
        int cb_namewidth;
        int cb_iteration;
-       char **cb_vdev_names; /* Only show these vdevs */
-       unsigned int cb_vdev_names_count;
        boolean_t cb_verbose;
        boolean_t cb_literal;
        boolean_t cb_scripted;
        zpool_list_t *cb_list;
        vdev_cmd_data_list_t *vcdl;
+       vdev_cbdata_t cb_vdevs;
 } iostat_cbdata_t;
 
 /*  iostat labels */
@@ -3624,22 +4143,23 @@ typedef struct name_and_columns {
        unsigned int columns;   /* Center name to this number of columns */
 } name_and_columns_t;
 
-#define        IOSTAT_MAX_LABELS       13      /* Max number of labels on one line */
+#define        IOSTAT_MAX_LABELS       15      /* Max number of labels on one line */
 
 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
 {
        [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
            {NULL}},
        [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
-           {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL}},
+           {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
+           {NULL}},
        [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
            {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
-           {"trimq_write", 2}, {NULL}},
+           {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}},
        [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
            {"asyncq_wait", 2}, {NULL}},
        [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
            {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
-           {"trim", 2}, {NULL}},
+           {"trim", 2}, {"rebuild", 2}, {NULL}},
 };
 
 /* Shorthand - if "columns" field not set, default to 1 column */
@@ -3648,14 +4168,17 @@ static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
        [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
            {"write"}, {NULL}},
        [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
-           {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL}},
+           {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
+           {NULL}},
        [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
            {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
-           {"pend"}, {"activ"}, {NULL}},
+           {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
        [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
-           {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL}},
+           {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
+           {NULL}},
        [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
-           {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
+           {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
+           {"ind"}, {"agg"}, {NULL}},
 };
 
 static const char *histo_to_title[] = {
@@ -3865,7 +4388,7 @@ print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
 
        if (cb->cb_flags & IOS_ANYHISTO_M) {
                title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
-       } else if (cb->cb_vdev_names_count) {
+       } else if (cb->cb_vdevs.cb_names_count) {
                title = "vdev";
        } else  {
                title = "pool";
@@ -3923,9 +4446,11 @@ print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
        unsigned int namewidth;
        const char *title;
 
+       color_start(ANSI_BOLD);
+
        if (cb->cb_flags & IOS_ANYHISTO_M) {
                title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
-       } else if (cb->cb_vdev_names_count) {
+       } else if (cb->cb_vdevs.cb_names_count) {
                title = "vdev";
        } else  {
                title = "pool";
@@ -3956,6 +4481,8 @@ print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
        if (cb->vcdl != NULL)
                print_cmd_columns(cb->vcdl, 1);
 
+       color_end();
+
        printf("\n");
 }
 
@@ -3965,6 +4492,37 @@ print_iostat_header(iostat_cbdata_t *cb)
        print_iostat_header_impl(cb, 0, NULL);
 }
 
+/*
+ * Prints a size string (i.e. 120M) with the suffix ("M") colored
+ * by order of magnitude. Uses column_size to add padding.
+ */
+static void
+print_stat_color(const char *statbuf, unsigned int column_size)
+{
+       fputs("  ", stdout);
+       size_t len = strlen(statbuf);
+       while (len < column_size) {
+               fputc(' ', stdout);
+               column_size--;
+       }
+       if (*statbuf == '0') {
+               color_start(ANSI_GRAY);
+               fputc('0', stdout);
+       } else {
+               for (; *statbuf; statbuf++) {
+                       if (*statbuf == 'K') color_start(ANSI_GREEN);
+                       else if (*statbuf == 'M') color_start(ANSI_YELLOW);
+                       else if (*statbuf == 'G') color_start(ANSI_RED);
+                       else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE);
+                       else if (*statbuf == 'P') color_start(ANSI_MAGENTA);
+                       else if (*statbuf == 'E') color_start(ANSI_CYAN);
+                       fputc(*statbuf, stdout);
+                       if (--column_size <= 0)
+                               break;
+               }
+       }
+       color_end();
+}
 
 /*
  * Display a single statistic.
@@ -3980,7 +4538,7 @@ print_one_stat(uint64_t value, enum zfs_nicenum_format format,
        if (scripted)
                printf("\t%s", buf);
        else
-               printf("  %*s", column_size, buf);
+               print_stat_color(buf, column_size);
 }
 
 /*
@@ -4269,11 +4827,8 @@ single_histo_average(uint64_t *histo, unsigned int buckets)
 }
 
 static void
-print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
-    nvlist_t *newnv)
+print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv)
 {
-       int i;
-       uint64_t val;
        const char *names[] = {
                ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
                ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
@@ -4287,6 +4842,8 @@ print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
                ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
                ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
                ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
+               ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
+               ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
        };
 
        struct stat_array *nva;
@@ -4301,8 +4858,8 @@ print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
        else
                format = ZFS_NICENUM_1024;
 
-       for (i = 0; i < ARRAY_SIZE(names); i++) {
-               val = nva[i].data[0];
+       for (int i = 0; i < ARRAY_SIZE(names); i++) {
+               uint64_t val = nva[i].data[0];
                print_one_stat(val, format, column_width, cb->cb_scripted);
        }
 
@@ -4326,6 +4883,7 @@ print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
                ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
                ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
                ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
        };
        struct stat_array *nva;
 
@@ -4390,7 +4948,7 @@ print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
            format, column_width, cb->cb_scripted);
 }
 
-static const char *class_name[] = {
+static const char *const class_name[] = {
        VDEV_ALLOC_BIAS_DEDUP,
        VDEV_ALLOC_BIAS_SPECIAL,
        VDEV_ALLOC_CLASS_LOGS
@@ -4430,9 +4988,9 @@ print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
        }
 
        /* Do we only want to see a specific vdev? */
-       for (i = 0; i < cb->cb_vdev_names_count; i++) {
+       for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) {
                /* Yes we do.  Is this the vdev? */
-               if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
+               if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) {
                        /*
                         * This is our vdev.  Since it is the only vdev we
                         * will be displaying, make depth = 0 so that it
@@ -4443,7 +5001,7 @@ print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
                }
        }
 
-       if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
+       if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) {
                /* Couldn't match the name */
                goto children;
        }
@@ -4492,14 +5050,14 @@ print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
        if (cb->cb_flags & IOS_LATENCY_M)
                print_iostat_latency(cb, oldnv, newnv);
        if (cb->cb_flags & IOS_QUEUES_M)
-               print_iostat_queues(cb, oldnv, newnv);
+               print_iostat_queues(cb, newnv);
        if (cb->cb_flags & IOS_ANYHISTO_M) {
                printf("\n");
                print_iostat_histos(cb, oldnv, newnv, scale, name);
        }
 
        if (cb->vcdl != NULL) {
-               char *path;
+               const char *path;
                if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
                    &path) == 0) {
                        printf("  ");
@@ -4550,7 +5108,7 @@ children:
                        continue;
 
                vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
-                   cb->cb_name_flags);
+                   cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
                ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
                    newchild[c], cb, depth + 2);
                free(vname);
@@ -4559,13 +5117,13 @@ children:
        /*
         * print all other top-level devices
         */
-       for (uint_t n = 0; n < 3; n++) {
+       for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
                boolean_t printed = B_FALSE;
 
                for (c = 0; c < children; c++) {
                        uint64_t islog = B_FALSE;
-                       char *bias = NULL;
-                       char *type = NULL;
+                       const char *bias = NULL;
+                       const char *type = NULL;
 
                        (void) nvlist_lookup_uint64(newchild[c],
                            ZPOOL_CONFIG_IS_LOG, &islog);
@@ -4584,7 +5142,8 @@ children:
 
                        if (!printed) {
                                if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
-                                   !cb->cb_scripted && !cb->cb_vdev_names) {
+                                   !cb->cb_scripted &&
+                                   !cb->cb_vdevs.cb_names) {
                                        print_iostat_dashes(cb, 0,
                                            class_name[n]);
                                }
@@ -4593,7 +5152,7 @@ children:
                        }
 
                        vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
-                           cb->cb_name_flags);
+                           cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
                        ret += print_vdev_stats(zhp, vname, oldnv ?
                            oldchild[c] : NULL, newchild[c], cb, depth + 2);
                        free(vname);
@@ -4617,14 +5176,14 @@ children:
 
        if (children > 0) {
                if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
-                   !cb->cb_vdev_names) {
+                   !cb->cb_vdevs.cb_names) {
                        print_iostat_dashes(cb, 0, "cache");
                }
                printf("\n");
 
                for (c = 0; c < children; c++) {
                        vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
-                           cb->cb_name_flags);
+                           cb->cb_vdevs.cb_name_flags);
                        ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
                            : NULL, newchild[c], cb, depth + 2);
                        free(vname);
@@ -4680,7 +5239,8 @@ print_iostat(zpool_handle_t *zhp, void *data)
        ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
            cb, 0);
        if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
-           !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) {
+           !cb->cb_scripted && cb->cb_verbose &&
+           !cb->cb_vdevs.cb_names_count) {
                print_iostat_separator(cb);
                if (cb->vcdl != NULL) {
                        print_cmd_columns(cb->vcdl, 1);
@@ -4722,7 +5282,7 @@ get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
        if ((config = zpool_get_config(zhp, NULL)) != NULL) {
                verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
                    &nvroot) == 0);
-               unsigned int poolname_len = strlen(zpool_get_name(zhp));
+               size_t poolname_len = strlen(zpool_get_name(zhp));
                if (verbose == B_FALSE) {
                        width = MAX(poolname_len, min_width);
                } else {
@@ -4756,8 +5316,8 @@ get_interval_count(int *argcp, char **argv, float *iv,
 
                if (*end == '\0' && errno == 0) {
                        if (interval == 0) {
-                               (void) fprintf(stderr, gettext("interval "
-                                   "cannot be zero\n"));
+                               (void) fprintf(stderr, gettext(
+                                   "interval cannot be zero\n"));
                                usage(B_FALSE);
                        }
                        /*
@@ -4787,8 +5347,8 @@ get_interval_count(int *argcp, char **argv, float *iv,
 
                if (*end == '\0' && errno == 0) {
                        if (interval == 0) {
-                               (void) fprintf(stderr, gettext("interval "
-                                   "cannot be zero\n"));
+                               (void) fprintf(stderr, gettext(
+                                   "interval cannot be zero\n"));
                                usage(B_FALSE);
                        }
 
@@ -4887,26 +5447,23 @@ get_stat_flags(zpool_list_t *list)
 }
 
 /*
- * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
+ * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
  */
 static int
-is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
+is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data)
 {
-       iostat_cbdata_t *cb = cb_data;
-       char *name = NULL;
-       int ret = 0;
-
-       name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
+       uint64_t guid;
+       vdev_cbdata_t *cb = cb_data;
+       zpool_handle_t *zhp = zhp_data;
 
-       if (strcmp(name, cb->cb_vdev_names[0]) == 0)
-               ret = 1; /* match */
-       free(name);
+       if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
+               return (0);
 
-       return (ret);
+       return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0]));
 }
 
 /*
- * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
+ * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
  */
 static int
 is_vdev(zpool_handle_t *zhp, void *cb_data)
@@ -4922,7 +5479,7 @@ is_vdev(zpool_handle_t *zhp, void *cb_data)
  */
 static int
 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
-    iostat_cbdata_t *cb)
+    vdev_cbdata_t *cb)
 {
        char **tmp_name;
        int ret = 0;
@@ -4935,23 +5492,23 @@ are_vdevs_in_pool(int argc, char **argv, char *pool_name,
        if (pool_name)
                pool_count = 1;
 
-       /* Temporarily hijack cb_vdev_names for a second... */
-       tmp_name = cb->cb_vdev_names;
+       /* Temporarily hijack cb_names for a second... */
+       tmp_name = cb->cb_names;
 
        /* Go though our list of prospective vdev names */
        for (i = 0; i < argc; i++) {
-               cb->cb_vdev_names = argv + i;
+               cb->cb_names = argv + i;
 
                /* Is this name a vdev in our pools? */
                ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
-                   is_vdev, cb);
+                   ZFS_TYPE_POOL, B_FALSE, is_vdev, cb);
                if (!ret) {
                        /* No match */
                        break;
                }
        }
 
-       cb->cb_vdev_names = tmp_name;
+       cb->cb_names = tmp_name;
 
        return (ret);
 }
@@ -4972,7 +5529,8 @@ is_pool_cb(zpool_handle_t *zhp, void *data)
 static int
 is_pool(char *name)
 {
-       return (for_each_pool(0, NULL, B_TRUE, NULL,  is_pool_cb, name));
+       return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE,
+           is_pool_cb, name));
 }
 
 /* Are all our argv[] strings pool names?  If so return 1, 0 otherwise. */
@@ -4995,7 +5553,7 @@ are_all_pools(int argc, char **argv)
  */
 static void
 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
-    iostat_cbdata_t *cb)
+    vdev_cbdata_t *cb)
 {
        int i;
        char *name;
@@ -5019,7 +5577,7 @@ error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
 /*
  * Same as get_interval_count(), but with additional checks to not misinterpret
  * guids as interval/count values.  Assumes VDEV_NAME_GUID is set in
- * cb.cb_name_flags.
+ * cb.cb_vdevs.cb_name_flags.
  */
 static void
 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
@@ -5029,7 +5587,8 @@ get_interval_count_filter_guids(int *argc, char **argv, float *interval,
        int argc_for_interval = 0;
 
        /* Is the last arg an interval value?  Or a guid? */
-       if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) {
+       if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL,
+           &cb->cb_vdevs)) {
                /*
                 * The last arg is not a guid, so it's probably an
                 * interval value.
@@ -5037,7 +5596,8 @@ get_interval_count_filter_guids(int *argc, char **argv, float *interval,
                argc_for_interval++;
 
                if (*argc >= 2 &&
-                   !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
+                   !are_vdevs_in_pool(1, &argv[*argc - 2], NULL,
+                   &cb->cb_vdevs)) {
                        /*
                         * The 2nd to last arg is not a guid, so it's probably
                         * an interval value.
@@ -5055,21 +5615,8 @@ get_interval_count_filter_guids(int *argc, char **argv, float *interval,
 }
 
 /*
- * Floating point sleep().  Allows you to pass in a floating point value for
- * seconds.
- */
-static void
-fsleep(float sec)
-{
-       struct timespec req;
-       req.tv_sec = floor(sec);
-       req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
-       nanosleep(&req, NULL);
-}
-
-/*
- * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
- * if we were unable to determine its size.
+ * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
+ * if we were unable to determine its size.
  */
 static int
 terminal_height(void)
@@ -5095,7 +5642,7 @@ terminal_height(void)
 static void
 print_zpool_script_help(char *name, char *path)
 {
-       char *argv[] = {path, "-h", NULL};
+       char *argv[] = {path, (char *)"-h", NULL};
        char **lines = NULL;
        int lines_cnt = 0;
        int rc;
@@ -5130,7 +5677,13 @@ print_zpool_dir_scripts(char *dirpath)
        if ((dir = opendir(dirpath)) != NULL) {
                /* print all the files and directories within directory */
                while ((ent = readdir(dir)) != NULL) {
-                       sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
+                       if (snprintf(fullpath, sizeof (fullpath), "%s/%s",
+                           dirpath, ent->d_name) >= sizeof (fullpath)) {
+                               (void) fprintf(stderr,
+                                   gettext("internal error: "
+                                   "ZPOOL_SCRIPTS_PATH too large.\n"));
+                               exit(1);
+                       }
 
                        /* Print the scripts */
                        if (stat(fullpath, &dir_stat) == 0)
@@ -5147,9 +5700,9 @@ print_zpool_dir_scripts(char *dirpath)
  * Print out help text for all zpool status/iostat -c scripts.
  */
 static void
-print_zpool_script_list(char *subcommand)
+print_zpool_script_list(const char *subcommand)
 {
-       char *dir, *sp;
+       char *dir, *sp, *tmp;
 
        printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
 
@@ -5157,11 +5710,10 @@ print_zpool_script_list(char *subcommand)
        if (sp == NULL)
                return;
 
-       dir = strtok(sp, ":");
-       while (dir != NULL) {
+       for (dir = strtok_r(sp, ":", &tmp);
+           dir != NULL;
+           dir = strtok_r(NULL, ":", &tmp))
                print_zpool_dir_scripts(dir);
-               dir = strtok(NULL, ":");
-       }
 
        free(sp);
 }
@@ -5181,8 +5733,8 @@ get_namewidth_iostat(zpool_handle_t *zhp, void *data)
         * get_namewidth() returns the maximum width of any name in that column
         * for any pool/vdev/device line that will be output.
         */
-       width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
-           cb->cb_verbose);
+       width = get_namewidth(zhp, cb->cb_namewidth,
+           cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
 
        /*
         * The width we are calculating is the width of the header and also the
@@ -5359,11 +5911,11 @@ zpool_do_iostat(int argc, char **argv)
        cb.cb_scripted = scripted;
 
        if (guid)
-               cb.cb_name_flags |= VDEV_NAME_GUID;
+               cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID;
        if (follow_links)
-               cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
+               cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
        if (full_name)
-               cb.cb_name_flags |= VDEV_NAME_PATH;
+               cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH;
        cb.cb_iteration = 0;
        cb.cb_namewidth = 0;
        cb.cb_verbose = verbose;
@@ -5380,17 +5932,18 @@ zpool_do_iostat(int argc, char **argv)
                /* No args, so just print the defaults. */
        } else if (are_all_pools(argc, argv)) {
                /* All the args are pool names */
-       } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) {
+       } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
                /* All the args are vdevs */
-               cb.cb_vdev_names = argv;
-               cb.cb_vdev_names_count = argc;
+               cb.cb_vdevs.cb_names = argv;
+               cb.cb_vdevs.cb_names_count = argc;
                argc = 0; /* No pools to process */
        } else if (are_all_pools(1, argv)) {
                /* The first arg is a pool name */
-               if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) {
+               if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
+                   &cb.cb_vdevs)) {
                        /* ...and the rest are vdev names */
-                       cb.cb_vdev_names = argv + 1;
-                       cb.cb_vdev_names_count = argc - 1;
+                       cb.cb_vdevs.cb_names = argv + 1;
+                       cb.cb_vdevs.cb_names_count = argc - 1;
                        argc = 1; /* One pool to process */
                } else {
                        fprintf(stderr, gettext("Expected either a list of "));
@@ -5398,7 +5951,7 @@ zpool_do_iostat(int argc, char **argv)
                        fprintf(stderr, " \"%s\", ", argv[0]);
                        fprintf(stderr, gettext("but got:\n"));
                        error_list_unresolved_vdevs(argc - 1, argv + 1,
-                           argv[0], &cb);
+                           argv[0], &cb.cb_vdevs);
                        fprintf(stderr, "\n");
                        usage(B_FALSE);
                        return (1);
@@ -5413,7 +5966,7 @@ zpool_do_iostat(int argc, char **argv)
                return (1);
        }
 
-       if (cb.cb_vdev_names_count != 0) {
+       if (cb.cb_vdevs.cb_names_count != 0) {
                /*
                 * If user specified vdevs, it implies verbose.
                 */
@@ -5424,7 +5977,8 @@ zpool_do_iostat(int argc, char **argv)
         * Construct the list of all interesting pools.
         */
        ret = 0;
-       if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
+       if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable,
+           &ret)) == NULL)
                return (1);
 
        if (pool_list_count(list) == 0 && argc != 0) {
@@ -5532,8 +6086,9 @@ zpool_do_iostat(int argc, char **argv)
                        if (cmd != NULL && cb.cb_verbose &&
                            !(cb.cb_flags & IOS_ANYHISTO_M)) {
                                cb.vcdl = all_pools_for_each_vdev_run(argc,
-                                   argv, cmd, g_zfs, cb.cb_vdev_names,
-                                   cb.cb_vdev_names_count, cb.cb_name_flags);
+                                   argv, cmd, g_zfs, cb.cb_vdevs.cb_names,
+                                   cb.cb_vdevs.cb_names_count,
+                                   cb.cb_vdevs.cb_name_flags);
                        } else {
                                cb.vcdl = NULL;
                        }
@@ -5568,6 +6123,7 @@ zpool_do_iostat(int argc, char **argv)
                                print_iostat_header(&cb);
 
                        if (skip) {
+                               (void) fflush(stdout);
                                (void) fsleep(interval);
                                continue;
                        }
@@ -5585,7 +6141,7 @@ zpool_do_iostat(int argc, char **argv)
                        if (((npools > 1 && !verbose &&
                            !(cb.cb_flags & IOS_ANYHISTO_M)) ||
                            (!(cb.cb_flags & IOS_ANYHISTO_M) &&
-                           cb.cb_vdev_names_count)) &&
+                           cb.cb_vdevs.cb_names_count)) &&
                            !cb.cb_scripted) {
                                print_iostat_separator(&cb);
                                if (cb.vcdl != NULL)
@@ -5598,18 +6154,13 @@ zpool_do_iostat(int argc, char **argv)
 
                }
 
-               /*
-                * Flush the output so that redirection to a file isn't buffered
-                * indefinitely.
-                */
-               (void) fflush(stdout);
-
                if (interval == 0)
                        break;
 
                if (count != 0 && --count == 0)
                        break;
 
+               (void) fflush(stdout);
                (void) fsleep(interval);
        }
 
@@ -5652,12 +6203,12 @@ print_header(list_cbdata_t *cb)
                }
 
                if (!first)
-                       (void) printf("  ");
+                       (void) fputs("  ", stdout);
                else
                        first = B_FALSE;
 
                right_justify = B_FALSE;
-               if (pl->pl_prop != ZPROP_INVAL) {
+               if (pl->pl_prop != ZPROP_USERPROP) {
                        header = zpool_prop_column_name(pl->pl_prop);
                        right_justify = zpool_prop_align_right(pl->pl_prop);
                } else {
@@ -5670,14 +6221,14 @@ print_header(list_cbdata_t *cb)
                }
 
                if (pl->pl_next == NULL && !right_justify)
-                       (void) printf("%s", header);
+                       (void) fputs(header, stdout);
                else if (right_justify)
                        (void) printf("%*s", (int)width, header);
                else
                        (void) printf("%-*s", (int)width, header);
        }
 
-       (void) printf("\n");
+       (void) fputc('\n', stdout);
 }
 
 /*
@@ -5690,7 +6241,7 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
        zprop_list_t *pl = cb->cb_proplist;
        boolean_t first = B_TRUE;
        char property[ZPOOL_MAXPROPLEN];
-       char *propstr;
+       const char *propstr;
        boolean_t right_justify;
        size_t width;
 
@@ -5707,15 +6258,15 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
 
                if (!first) {
                        if (cb->cb_scripted)
-                               (void) printf("\t");
+                               (void) fputc('\t', stdout);
                        else
-                               (void) printf("  ");
+                               (void) fputs("  ", stdout);
                } else {
                        first = B_FALSE;
                }
 
                right_justify = B_FALSE;
-               if (pl->pl_prop != ZPROP_INVAL) {
+               if (pl->pl_prop != ZPROP_USERPROP) {
                        if (zpool_get_prop(zhp, pl->pl_prop, property,
                            sizeof (property), NULL, cb->cb_literal) != 0)
                                propstr = "-";
@@ -5728,25 +6279,28 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
                    zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
                    sizeof (property)) == 0) {
                        propstr = property;
+               } else if (zfs_prop_user(pl->pl_user_prop) &&
+                   zpool_get_userprop(zhp, pl->pl_user_prop, property,
+                   sizeof (property), NULL) == 0) {
+                       propstr = property;
                } else {
                        propstr = "-";
                }
 
-
                /*
                 * If this is being called in scripted mode, or if this is the
                 * last column and it is left-justified, don't include a width
                 * format specifier.
                 */
                if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
-                       (void) printf("%s", propstr);
+                       (void) fputs(propstr, stdout);
                else if (right_justify)
                        (void) printf("%*s", (int)width, propstr);
                else
                        (void) printf("%-*s", (int)width, propstr);
        }
 
-       (void) printf("\n");
+       (void) fputc('\n', stdout);
 }
 
 static void
@@ -5758,6 +6312,7 @@ print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
        size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
 
        switch (prop) {
+       case ZPOOL_PROP_SIZE:
        case ZPOOL_PROP_EXPANDSZ:
        case ZPOOL_PROP_CHECKPOINT:
        case ZPOOL_PROP_DEDUPRATIO:
@@ -5790,7 +6345,7 @@ print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
                break;
        case ZPOOL_PROP_HEALTH:
                width = 8;
-               snprintf(propval, sizeof (propval), "%-*s", (int)width, str);
+               (void) strlcpy(propval, str, sizeof (propval));
                break;
        default:
                zfs_nicenum_format(value, propval, sizeof (propval), format);
@@ -5819,8 +6374,8 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
        char *vname;
        boolean_t scripted = cb->cb_scripted;
        uint64_t islog = B_FALSE;
-       char *dashes = "%-*s      -      -      -        -         "
-           "-      -      -      -  -\n";
+       const char *dashes = "%-*s      -      -      -        -         "
+           "-      -      -      -         -\n";
 
        verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t **)&vs, &c) == 0);
@@ -5853,8 +6408,12 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                 * 'toplevel' boolean value is passed to the print_one_column()
                 * to indicate that the value is valid.
                 */
-               print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, scripted,
-                   toplevel, format);
+               if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace)
+                       print_one_column(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL,
+                           scripted, B_TRUE, format);
+               else
+                       print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL,
+                           scripted, toplevel, format);
                print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL,
                    scripted, toplevel, format);
                print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
@@ -5882,7 +6441,7 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                }
                print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted,
                    B_TRUE, format);
-               (void) printf("\n");
+               (void) fputc('\n', stdout);
        }
 
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
@@ -5905,18 +6464,18 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                        continue;
 
                vname = zpool_vdev_name(g_zfs, zhp, child[c],
-                   cb->cb_name_flags);
+                   cb->cb_name_flags | VDEV_NAME_TYPE_ID);
                print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE);
                free(vname);
        }
 
        /* list the classes: 'logs', 'dedup', and 'special' */
-       for (uint_t n = 0; n < 3; n++) {
+       for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
                boolean_t printed = B_FALSE;
 
                for (c = 0; c < children; c++) {
-                       char *bias = NULL;
-                       char *type = NULL;
+                       const char *bias = NULL;
+                       const char *type = NULL;
 
                        if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
                            &islog) == 0 && islog) {
@@ -5939,7 +6498,7 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                                printed = B_TRUE;
                        }
                        vname = zpool_vdev_name(g_zfs, zhp, child[c],
-                           cb->cb_name_flags);
+                           cb->cb_name_flags | VDEV_NAME_TYPE_ID);
                        print_list_stats(zhp, vname, child[c], cb, depth + 2,
                            B_FALSE);
                        free(vname);
@@ -6005,8 +6564,8 @@ get_namewidth_list(zpool_handle_t *zhp, void *data)
        list_cbdata_t *cb = data;
        int width;
 
-       width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
-           cb->cb_verbose);
+       width = get_namewidth(zhp, cb->cb_namewidth,
+           cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
 
        if (width < 9)
                width = 9;
@@ -6047,6 +6606,7 @@ zpool_do_list(int argc, char **argv)
        unsigned long count = 0;
        zpool_list_t *list;
        boolean_t first = B_TRUE;
+       current_prop_type = ZFS_TYPE_POOL;
 
        /* check options */
        while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
@@ -6098,7 +6658,7 @@ zpool_do_list(int argc, char **argv)
 
        for (;;) {
                if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
-                   &ret)) == NULL)
+                   ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL)
                        return (1);
 
                if (pool_list_count(list) == 0)
@@ -6123,6 +6683,8 @@ zpool_do_list(int argc, char **argv)
                        break;
 
                pool_list_free(list);
+
+               (void) fflush(stdout);
                (void) fsleep(interval);
        }
 
@@ -6259,9 +6821,17 @@ zpool_do_attach_or_replace(int argc, char **argv, int replacing)
        ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing,
            rebuild);
 
-       if (ret == 0 && wait)
-               ret = zpool_wait(zhp,
-                   replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER);
+       if (ret == 0 && wait) {
+               zpool_wait_activity_t activity = ZPOOL_WAIT_RESILVER;
+               char raidz_prefix[] = "raidz";
+               if (replacing) {
+                       activity = ZPOOL_WAIT_REPLACE;
+               } else if (strncmp(old_disk,
+                   raidz_prefix, strlen(raidz_prefix)) == 0) {
+                       activity = ZPOOL_WAIT_RAIDZ_EXPAND;
+               }
+               ret = zpool_wait(zhp, activity);
+       }
 
        nvlist_free(props);
        nvlist_free(nvroot);
@@ -6280,7 +6850,6 @@ zpool_do_attach_or_replace(int argc, char **argv, int replacing)
  *
  * Replace <device> with <new_device>.
  */
-/* ARGSUSED */
 int
 zpool_do_replace(int argc, char **argv)
 {
@@ -6288,17 +6857,21 @@ zpool_do_replace(int argc, char **argv)
 }
 
 /*
- * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
+ * zpool attach [-fsw] [-o property=value] <pool> <device>|<vdev> <new_device>
  *
  *     -f      Force attach, even if <new_device> appears to be in use.
  *     -s      Use sequential instead of healing reconstruction for resilver.
  *     -o      Set property=value.
- *     -w      Wait for resilvering to complete before returning
+ *     -w      Wait for resilvering (mirror) or expansion (raidz) to complete
+ *             before returning.
  *
- * Attach <new_device> to the mirror containing <device>.  If <device> is not
- * part of a mirror, then <device> will be transformed into a mirror of
- * <device> and <new_device>.  In either case, <new_device> will begin life
- * with a DTL of [0, now], and will immediately begin to resilver itself.
+ * Attach <new_device> to a <device> or <vdev>, where the vdev can be of type
+ * mirror or raidz. If <device> is not part of a mirror, then <device> will
+ * be transformed into a mirror of <device> and <new_device>. When a mirror
+ * is involved, <new_device> will begin life with a DTL of [0, now], and will
+ * immediately begin to resilver itself. For the raidz case, a expansion will
+ * commence and reflow the raidz data across all the disks including the
+ * <new_device>.
  */
 int
 zpool_do_attach(int argc, char **argv)
@@ -6316,7 +6889,6 @@ zpool_do_attach(int argc, char **argv)
  * is the last device in the mirror, or if the DTLs indicate that this device
  * has the only valid copy of some data.
  */
-/* ARGSUSED */
 int
 zpool_do_detach(int argc, char **argv)
 {
@@ -6393,6 +6965,7 @@ zpool_do_split(int argc, char **argv)
        char *mntopts = NULL;
        splitflags_t flags;
        int c, ret = 0;
+       int ms_status = 0;
        boolean_t loadkeys = B_FALSE;
        zpool_handle_t *zhp;
        nvlist_t *config, *props = NULL;
@@ -6498,6 +7071,10 @@ zpool_do_split(int argc, char **argv)
                            "following layout:\n\n"), newpool);
                        print_vdev_tree(NULL, newpool, config, 0, "",
                            flags.name_flags);
+                       print_vdev_tree(NULL, "dedup", config, 0,
+                           VDEV_ALLOC_BIAS_DEDUP, 0);
+                       print_vdev_tree(NULL, "special", config, 0,
+                           VDEV_ALLOC_BIAS_SPECIAL, 0);
                }
        }
 
@@ -6525,13 +7102,18 @@ zpool_do_split(int argc, char **argv)
                        ret = 1;
        }
 
-       if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
-           zpool_enable_datasets(zhp, mntopts, 0) != 0) {
-               ret = 1;
-               (void) fprintf(stderr, gettext("Split was successful, but "
-                   "the datasets could not all be mounted\n"));
-               (void) fprintf(stderr, gettext("Try doing '%s' with a "
-                   "different altroot\n"), "zpool import");
+       if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) {
+               ms_status = zpool_enable_datasets(zhp, mntopts, 0);
+               if (ms_status == EZFS_SHAREFAILED) {
+                       (void) fprintf(stderr, gettext("Split was successful, "
+                           "datasets are mounted but sharing of some datasets "
+                           "has failed\n"));
+               } else if (ms_status == EZFS_MOUNTFAILED) {
+                       (void) fprintf(stderr, gettext("Split was successful"
+                           ", but some datasets could not be mounted\n"));
+                       (void) fprintf(stderr, gettext("Try doing '%s' with a "
+                           "different altroot\n"), "zpool import");
+               }
        }
        zpool_close(zhp);
        nvlist_free(config);
@@ -6541,9 +7123,10 @@ zpool_do_split(int argc, char **argv)
 }
 
 
-
 /*
- * zpool online <pool> <device> ...
+ * zpool online [--power] <pool> <device> ...
+ *
+ * --power: Power on the enclosure slot to the drive (if possible)
  */
 int
 zpool_do_online(int argc, char **argv)
@@ -6554,13 +7137,21 @@ zpool_do_online(int argc, char **argv)
        int ret = 0;
        vdev_state_t newstate;
        int flags = 0;
+       boolean_t is_power_on = B_FALSE;
+       struct option long_options[] = {
+               {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
+               {0, 0, 0, 0}
+       };
 
        /* check options */
-       while ((c = getopt(argc, argv, "e")) != -1) {
+       while ((c = getopt_long(argc, argv, "e", long_options, NULL)) != -1) {
                switch (c) {
                case 'e':
                        flags |= ZFS_ONLINE_EXPAND;
                        break;
+               case ZPOOL_OPTION_POWER:
+                       is_power_on = B_TRUE;
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -6568,6 +7159,9 @@ zpool_do_online(int argc, char **argv)
                }
        }
 
+       if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
+               is_power_on = B_TRUE;
+
        argc -= optind;
        argv += optind;
 
@@ -6587,6 +7181,29 @@ zpool_do_online(int argc, char **argv)
                return (1);
 
        for (i = 1; i < argc; i++) {
+               vdev_state_t oldstate;
+               boolean_t avail_spare, l2cache;
+               int rc;
+
+               if (is_power_on) {
+                       rc = zpool_power_on_and_disk_wait(zhp, argv[i]);
+                       if (rc == ENOTSUP) {
+                               (void) fprintf(stderr,
+                                   gettext("Power control not supported\n"));
+                       }
+                       if (rc != 0)
+                               return (rc);
+               }
+
+               nvlist_t *tgt = zpool_find_vdev(zhp, argv[i], &avail_spare,
+                   &l2cache, NULL);
+               if (tgt == NULL) {
+                       ret = 1;
+                       continue;
+               }
+               uint_t vsc;
+               oldstate = ((vdev_stat_t *)fnvlist_lookup_uint64_array(tgt,
+                   ZPOOL_CONFIG_VDEV_STATS, &vsc))->vs_state;
                if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
                        if (newstate != VDEV_STATE_HEALTHY) {
                                (void) printf(gettext("warning: device '%s' "
@@ -6600,6 +7217,17 @@ zpool_do_online(int argc, char **argv)
                                        (void) printf(gettext("use 'zpool "
                                            "replace' to replace devices "
                                            "that are no longer present\n"));
+                               if ((flags & ZFS_ONLINE_EXPAND)) {
+                                       (void) printf(gettext("%s: failed "
+                                           "to expand usable space on "
+                                           "unhealthy device '%s'\n"),
+                                           (oldstate >= VDEV_STATE_DEGRADED ?
+                                           "error" : "warning"), argv[i]);
+                                       if (oldstate >= VDEV_STATE_DEGRADED) {
+                                               ret = 1;
+                                               break;
+                                       }
+                               }
                        }
                } else {
                        ret = 1;
@@ -6612,14 +7240,16 @@ zpool_do_online(int argc, char **argv)
 }
 
 /*
- * zpool offline [-ft] <pool> <device> ...
+ * zpool offline [-ft]|[--power] <pool> <device> ...
+ *
  *
  *     -f      Force the device into a faulted state.
  *
  *     -t      Only take the device off-line temporarily.  The offline/faulted
  *             state will not be persistent across reboots.
+ *
+ *     --power Power off the enclosure slot to the drive (if possible)
  */
-/* ARGSUSED */
 int
 zpool_do_offline(int argc, char **argv)
 {
@@ -6629,9 +7259,15 @@ zpool_do_offline(int argc, char **argv)
        int ret = 0;
        boolean_t istmp = B_FALSE;
        boolean_t fault = B_FALSE;
+       boolean_t is_power_off = B_FALSE;
+
+       struct option long_options[] = {
+               {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
+               {0, 0, 0, 0}
+       };
 
        /* check options */
-       while ((c = getopt(argc, argv, "ft")) != -1) {
+       while ((c = getopt_long(argc, argv, "ft", long_options, NULL)) != -1) {
                switch (c) {
                case 'f':
                        fault = B_TRUE;
@@ -6639,6 +7275,9 @@ zpool_do_offline(int argc, char **argv)
                case 't':
                        istmp = B_TRUE;
                        break;
+               case ZPOOL_OPTION_POWER:
+                       is_power_off = B_TRUE;
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -6646,6 +7285,20 @@ zpool_do_offline(int argc, char **argv)
                }
        }
 
+       if (is_power_off && fault) {
+               (void) fprintf(stderr,
+                   gettext("-0 and -f cannot be used together\n"));
+               usage(B_FALSE);
+               return (1);
+       }
+
+       if (is_power_off && istmp) {
+               (void) fprintf(stderr,
+                   gettext("-0 and -t cannot be used together\n"));
+               usage(B_FALSE);
+               return (1);
+       }
+
        argc -= optind;
        argv += optind;
 
@@ -6665,8 +7318,22 @@ zpool_do_offline(int argc, char **argv)
                return (1);
 
        for (i = 1; i < argc; i++) {
-               if (fault) {
-                       uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
+               uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
+               if (is_power_off) {
+                       /*
+                        * Note: we have to power off first, then set REMOVED,
+                        * or else zpool_vdev_set_removed_state() returns
+                        * EAGAIN.
+                        */
+                       ret = zpool_power_off(zhp, argv[i]);
+                       if (ret != 0) {
+                               (void) fprintf(stderr, "%s %s %d\n",
+                                   gettext("unable to power off slot for"),
+                                   argv[i], ret);
+                       }
+                       zpool_vdev_set_removed_state(zhp, guid, VDEV_AUX_NONE);
+
+               } else if (fault) {
                        vdev_aux_t aux;
                        if (istmp == B_FALSE) {
                                /* Force the fault to persist across imports */
@@ -6689,7 +7356,7 @@ zpool_do_offline(int argc, char **argv)
 }
 
 /*
- * zpool clear <pool> [device]
+ * zpool clear [-nF]|[--power] <pool> [device]
  *
  * Clear all errors associated with a pool or a particular device.
  */
@@ -6701,13 +7368,20 @@ zpool_do_clear(int argc, char **argv)
        boolean_t dryrun = B_FALSE;
        boolean_t do_rewind = B_FALSE;
        boolean_t xtreme_rewind = B_FALSE;
+       boolean_t is_power_on = B_FALSE;
        uint32_t rewind_policy = ZPOOL_NO_REWIND;
        nvlist_t *policy = NULL;
        zpool_handle_t *zhp;
        char *pool, *device;
 
+       struct option long_options[] = {
+               {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
+               {0, 0, 0, 0}
+       };
+
        /* check options */
-       while ((c = getopt(argc, argv, "FnX")) != -1) {
+       while ((c = getopt_long(argc, argv, "FnX", long_options,
+           NULL)) != -1) {
                switch (c) {
                case 'F':
                        do_rewind = B_TRUE;
@@ -6718,6 +7392,9 @@ zpool_do_clear(int argc, char **argv)
                case 'X':
                        xtreme_rewind = B_TRUE;
                        break;
+               case ZPOOL_OPTION_POWER:
+                       is_power_on = B_TRUE;
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -6725,6 +7402,9 @@ zpool_do_clear(int argc, char **argv)
                }
        }
 
+       if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
+               is_power_on = B_TRUE;
+
        argc -= optind;
        argv += optind;
 
@@ -6765,6 +7445,14 @@ zpool_do_clear(int argc, char **argv)
                return (1);
        }
 
+       if (is_power_on) {
+               if (device == NULL) {
+                       zpool_power_on_pool_and_wait_for_devices(zhp);
+               } else {
+                       zpool_power_on_and_disk_wait(zhp, device);
+               }
+       }
+
        if (zpool_clear(zhp, device, policy) != 0)
                ret = 1;
 
@@ -6850,8 +7538,8 @@ zpool_do_reopen(int argc, char **argv)
        argv += optind;
 
        /* if argc == 0 we will execute zpool_reopen_one on all pools */
-       ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
-           &scrub_restart);
+       ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, zpool_reopen_one, &scrub_restart);
 
        return (ret);
 }
@@ -6922,8 +7610,9 @@ wait_callback(zpool_handle_t *zhp, void *data)
 }
 
 /*
- * zpool scrub [-s | -p] [-w] <pool> ...
+ * zpool scrub [-s | -p] [-w] [-e] <pool> ...
  *
+ *     -e      Only scrub blocks in the error log.
  *     -s      Stop.  Stops any in-progress scrub.
  *     -p      Pause. Pause in-progress scrub.
  *     -w      Wait.  Blocks until scrub has completed.
@@ -6939,14 +7628,21 @@ zpool_do_scrub(int argc, char **argv)
        cb.cb_type = POOL_SCAN_SCRUB;
        cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
 
+       boolean_t is_error_scrub = B_FALSE;
+       boolean_t is_pause = B_FALSE;
+       boolean_t is_stop = B_FALSE;
+
        /* check options */
-       while ((c = getopt(argc, argv, "spw")) != -1) {
+       while ((c = getopt(argc, argv, "spwe")) != -1) {
                switch (c) {
+               case 'e':
+                       is_error_scrub = B_TRUE;
+                       break;
                case 's':
-                       cb.cb_type = POOL_SCAN_NONE;
+                       is_stop = B_TRUE;
                        break;
                case 'p':
-                       cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
+                       is_pause = B_TRUE;
                        break;
                case 'w':
                        wait = B_TRUE;
@@ -6958,11 +7654,21 @@ zpool_do_scrub(int argc, char **argv)
                }
        }
 
-       if (cb.cb_type == POOL_SCAN_NONE &&
-           cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
-               (void) fprintf(stderr, gettext("invalid option combination: "
-                   "-s and -p are mutually exclusive\n"));
+       if (is_pause && is_stop) {
+               (void) fprintf(stderr, gettext("invalid option "
+                   "combination :-s and -p are mutually exclusive\n"));
                usage(B_FALSE);
+       } else {
+               if (is_error_scrub)
+                       cb.cb_type = POOL_SCAN_ERRORSCRUB;
+
+               if (is_pause) {
+                       cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
+               } else if (is_stop) {
+                       cb.cb_type = POOL_SCAN_NONE;
+               } else {
+                       cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
+               }
        }
 
        if (wait && (cb.cb_type == POOL_SCAN_NONE ||
@@ -6980,12 +7686,13 @@ zpool_do_scrub(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       error = for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb);
+       error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, scrub_callback, &cb);
 
        if (wait && !error) {
                zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB;
-               error = for_each_pool(argc, argv, B_TRUE, NULL, wait_callback,
-                   &act);
+               error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+                   B_FALSE, wait_callback, &act);
        }
 
        return (error);
@@ -7023,7 +7730,8 @@ zpool_do_resilver(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
+       return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, scrub_callback, &cb));
 }
 
 /*
@@ -7080,9 +7788,10 @@ zpool_do_trim(int argc, char **argv)
                                    "combined with the -c or -s options\n"));
                                usage(B_FALSE);
                        }
-                       if (zfs_nicestrtonum(NULL, optarg, &rate) == -1) {
-                               (void) fprintf(stderr,
-                                   gettext("invalid value for rate\n"));
+                       if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) {
+                               (void) fprintf(stderr, "%s: %s\n",
+                                   gettext("invalid value for rate"),
+                                   libzfs_error_description(g_zfs));
                                usage(B_FALSE);
                        }
                        break;
@@ -7183,6 +7892,70 @@ secs_to_dhms(uint64_t total, char *buf)
        }
 }
 
+/*
+ * Print out detailed error scrub status.
+ */
+static void
+print_err_scrub_status(pool_scan_stat_t *ps)
+{
+       time_t start, end, pause;
+       uint64_t total_secs_left;
+       uint64_t secs_left, mins_left, hours_left, days_left;
+       uint64_t examined, to_be_examined;
+
+       if (ps == NULL || ps->pss_error_scrub_func != POOL_SCAN_ERRORSCRUB) {
+               return;
+       }
+
+       (void) printf(gettext(" scrub: "));
+
+       start = ps->pss_error_scrub_start;
+       end = ps->pss_error_scrub_end;
+       pause = ps->pss_pass_error_scrub_pause;
+       examined = ps->pss_error_scrub_examined;
+       to_be_examined = ps->pss_error_scrub_to_be_examined;
+
+       assert(ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB);
+
+       if (ps->pss_error_scrub_state == DSS_FINISHED) {
+               total_secs_left = end - start;
+               days_left = total_secs_left / 60 / 60 / 24;
+               hours_left = (total_secs_left / 60 / 60) % 24;
+               mins_left = (total_secs_left / 60) % 60;
+               secs_left = (total_secs_left % 60);
+
+               (void) printf(gettext("scrubbed %llu error blocks in %llu days "
+                   "%02llu:%02llu:%02llu on %s"), (u_longlong_t)examined,
+                   (u_longlong_t)days_left, (u_longlong_t)hours_left,
+                   (u_longlong_t)mins_left, (u_longlong_t)secs_left,
+                   ctime(&end));
+
+               return;
+       } else if (ps->pss_error_scrub_state == DSS_CANCELED) {
+               (void) printf(gettext("error scrub canceled on %s"),
+                   ctime(&end));
+               return;
+       }
+       assert(ps->pss_error_scrub_state == DSS_ERRORSCRUBBING);
+
+       /* Error scrub is in progress. */
+       if (pause == 0) {
+               (void) printf(gettext("error scrub in progress since %s"),
+                   ctime(&start));
+       } else {
+               (void) printf(gettext("error scrub paused since %s"),
+                   ctime(&pause));
+               (void) printf(gettext("\terror scrub started on %s"),
+                   ctime(&start));
+       }
+
+       double fraction_done = (double)examined / (to_be_examined + examined);
+       (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error"
+           " blocks"), 100 * fraction_done, (u_longlong_t)examined);
+
+       (void) printf("\n");
+}
+
 /*
  * Print out detailed scrub status.
  */
@@ -7190,11 +7963,11 @@ static void
 print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
 {
        time_t start, end, pause;
-       uint64_t pass_scanned, scanned, pass_issued, issued, total;
+       uint64_t pass_scanned, scanned, pass_issued, issued, total_s, total_i;
        uint64_t elapsed, scan_rate, issue_rate;
        double fraction_done;
-       char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
-       char srate_buf[7], irate_buf[7], time_buf[32];
+       char processed_buf[7], scanned_buf[7], issued_buf[7], total_s_buf[7];
+       char total_i_buf[7], srate_buf[7], irate_buf[7], time_buf[32];
 
        printf("  ");
        printf_color(ANSI_BOLD, gettext("scan:"));
@@ -7213,19 +7986,20 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
 
        zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
 
-       assert(ps->pss_func == POOL_SCAN_SCRUB ||
-           ps->pss_func == POOL_SCAN_RESILVER);
+       int is_resilver = ps->pss_func == POOL_SCAN_RESILVER;
+       int is_scrub = ps->pss_func == POOL_SCAN_SCRUB;
+       assert(is_resilver || is_scrub);
 
        /* Scan is finished or canceled. */
        if (ps->pss_state == DSS_FINISHED) {
                secs_to_dhms(end - start, time_buf);
 
-               if (ps->pss_func == POOL_SCAN_SCRUB) {
+               if (is_scrub) {
                        (void) printf(gettext("scrub repaired %s "
                            "in %s with %llu errors on %s"), processed_buf,
                            time_buf, (u_longlong_t)ps->pss_errors,
                            ctime(&end));
-               } else if (ps->pss_func == POOL_SCAN_RESILVER) {
+               } else if (is_resilver) {
                        (void) printf(gettext("resilvered %s "
                            "in %s with %llu errors on %s"), processed_buf,
                            time_buf, (u_longlong_t)ps->pss_errors,
@@ -7233,10 +8007,10 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
                }
                return;
        } else if (ps->pss_state == DSS_CANCELED) {
-               if (ps->pss_func == POOL_SCAN_SCRUB) {
+               if (is_scrub) {
                        (void) printf(gettext("scrub canceled on %s"),
                            ctime(&end));
-               } else if (ps->pss_func == POOL_SCAN_RESILVER) {
+               } else if (is_resilver) {
                        (void) printf(gettext("resilver canceled on %s"),
                            ctime(&end));
                }
@@ -7246,7 +8020,7 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
        assert(ps->pss_state == DSS_SCANNING);
 
        /* Scan is in progress. Resilvers can't be paused. */
-       if (ps->pss_func == POOL_SCAN_SCRUB) {
+       if (is_scrub) {
                if (pause == 0) {
                        (void) printf(gettext("scrub in progress since %s"),
                            ctime(&start));
@@ -7256,7 +8030,7 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
                        (void) printf(gettext("\tscrub started on %s"),
                            ctime(&start));
                }
-       } else if (ps->pss_func == POOL_SCAN_RESILVER) {
+       } else if (is_resilver) {
                (void) printf(gettext("resilver in progress since %s"),
                    ctime(&start));
        }
@@ -7265,10 +8039,11 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
        pass_scanned = ps->pss_pass_exam;
        issued = ps->pss_issued;
        pass_issued = ps->pss_pass_issued;
-       total = ps->pss_to_examine;
+       total_s = ps->pss_to_examine;
+       total_i = ps->pss_to_examine - ps->pss_skipped;
 
        /* we are only done with a block once we have issued the IO for it */
-       fraction_done = (double)issued / total;
+       fraction_done = (double)issued / total_i;
 
        /* elapsed time for this pass, rounding up to 1 if it's 0 */
        elapsed = time(NULL) - ps->pss_pass_start;
@@ -7277,38 +8052,47 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
 
        scan_rate = pass_scanned / elapsed;
        issue_rate = pass_issued / elapsed;
-       uint64_t total_secs_left = (issue_rate != 0 && total >= issued) ?
-           ((total - issued) / issue_rate) : UINT64_MAX;
-       secs_to_dhms(total_secs_left, time_buf);
 
        /* format all of the numbers we will be reporting */
        zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
        zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
-       zfs_nicebytes(total, total_buf, sizeof (total_buf));
-       zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
-       zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
+       zfs_nicebytes(total_s, total_s_buf, sizeof (total_s_buf));
+       zfs_nicebytes(total_i, total_i_buf, sizeof (total_i_buf));
 
        /* do not print estimated time if we have a paused scrub */
-       if (pause == 0) {
-               (void) printf(gettext("\t%s scanned at %s/s, "
-                   "%s issued at %s/s, %s total\n"),
-                   scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
-       } else {
-               (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
-                   scanned_buf, issued_buf, total_buf);
+       (void) printf(gettext("\t%s / %s scanned"), scanned_buf, total_s_buf);
+       if (pause == 0 && scan_rate > 0) {
+               zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
+               (void) printf(gettext(" at %s/s"), srate_buf);
        }
+       (void) printf(gettext(", %s / %s issued"), issued_buf, total_i_buf);
+       if (pause == 0 && issue_rate > 0) {
+               zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
+               (void) printf(gettext(" at %s/s"), irate_buf);
+       }
+       (void) printf(gettext("\n"));
 
-       if (ps->pss_func == POOL_SCAN_RESILVER) {
+       if (is_resilver) {
                (void) printf(gettext("\t%s resilvered, %.2f%% done"),
                    processed_buf, 100 * fraction_done);
-       } else if (ps->pss_func == POOL_SCAN_SCRUB) {
+       } else if (is_scrub) {
                (void) printf(gettext("\t%s repaired, %.2f%% done"),
                    processed_buf, 100 * fraction_done);
        }
 
        if (pause == 0) {
-               if (total_secs_left != UINT64_MAX &&
-                   issue_rate >= 10 * 1024 * 1024) {
+               /*
+                * Only provide an estimate iff:
+                * 1) we haven't yet issued all we expected, and
+                * 2) the issue rate exceeds 10 MB/s, and
+                * 3) it's either:
+                *    a) a resilver which has started repairs, or
+                *    b) a scrub which has entered the issue phase.
+                */
+               if (total_i >= issued && issue_rate >= 10 * 1024 * 1024 &&
+                   ((is_resilver && ps->pss_processed > 0) ||
+                   (is_scrub && issued > 0))) {
+                       secs_to_dhms((total_i - issued) / issue_rate, time_buf);
                        (void) printf(gettext(", %s to go\n"), time_buf);
                } else {
                        (void) printf(gettext(", no estimated "
@@ -7320,7 +8104,7 @@ print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
 }
 
 static void
-print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
+print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, uint_t c, char *vdev_name)
 {
        if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE)
                return;
@@ -7332,17 +8116,20 @@ print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
        uint64_t bytes_scanned = vrs->vrs_bytes_scanned;
        uint64_t bytes_issued = vrs->vrs_bytes_issued;
        uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt;
-       uint64_t bytes_est = vrs->vrs_bytes_est;
+       uint64_t bytes_est_s = vrs->vrs_bytes_est;
+       uint64_t bytes_est_i = vrs->vrs_bytes_est;
+       if (c > offsetof(vdev_rebuild_stat_t, vrs_pass_bytes_skipped) / 8)
+               bytes_est_i -= vrs->vrs_pass_bytes_skipped;
        uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned /
            (vrs->vrs_pass_time_ms + 1)) * 1000;
        uint64_t issue_rate = (vrs->vrs_pass_bytes_issued /
            (vrs->vrs_pass_time_ms + 1)) * 1000;
        double scan_pct = MIN((double)bytes_scanned * 100 /
-           (bytes_est + 1), 100);
+           (bytes_est_s + 1), 100);
 
        /* Format all of the numbers we will be reporting */
        char bytes_scanned_buf[7], bytes_issued_buf[7];
-       char bytes_rebuilt_buf[7], bytes_est_buf[7];
+       char bytes_rebuilt_buf[7], bytes_est_s_buf[7], bytes_est_i_buf[7];
        char scan_rate_buf[7], issue_rate_buf[7], time_buf[32];
        zfs_nicebytes(bytes_scanned, bytes_scanned_buf,
            sizeof (bytes_scanned_buf));
@@ -7350,9 +8137,8 @@ print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
            sizeof (bytes_issued_buf));
        zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf,
            sizeof (bytes_rebuilt_buf));
-       zfs_nicebytes(bytes_est, bytes_est_buf, sizeof (bytes_est_buf));
-       zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf));
-       zfs_nicebytes(issue_rate, issue_rate_buf, sizeof (issue_rate_buf));
+       zfs_nicebytes(bytes_est_s, bytes_est_s_buf, sizeof (bytes_est_s_buf));
+       zfs_nicebytes(bytes_est_i, bytes_est_i_buf, sizeof (bytes_est_i_buf));
 
        time_t start = vrs->vrs_start_time;
        time_t end = vrs->vrs_end_time;
@@ -7375,17 +8161,29 @@ print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
 
        assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE);
 
-       secs_to_dhms(MAX((int64_t)bytes_est - (int64_t)bytes_scanned, 0) /
-           MAX(scan_rate, 1), time_buf);
+       (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf,
+           bytes_est_s_buf);
+       if (scan_rate > 0) {
+               zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf));
+               (void) printf(gettext(" at %s/s"), scan_rate_buf);
+       }
+       (void) printf(gettext(", %s / %s issued"), bytes_issued_buf,
+           bytes_est_i_buf);
+       if (issue_rate > 0) {
+               zfs_nicebytes(issue_rate, issue_rate_buf,
+                   sizeof (issue_rate_buf));
+               (void) printf(gettext(" at %s/s"), issue_rate_buf);
+       }
+       (void) printf(gettext("\n"));
 
-       (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
-           "%s total\n"), bytes_scanned_buf, scan_rate_buf,
-           bytes_issued_buf, issue_rate_buf, bytes_est_buf);
        (void) printf(gettext("\t%s resilvered, %.2f%% done"),
            bytes_rebuilt_buf, scan_pct);
 
        if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
-               if (scan_rate >= 10 * 1024 * 1024) {
+               if (bytes_est_s >= bytes_scanned &&
+                   scan_rate >= 10 * 1024 * 1024) {
+                       secs_to_dhms((bytes_est_s - bytes_scanned) / scan_rate,
+                           time_buf);
                        (void) printf(gettext(", %s to go\n"), time_buf);
                } else {
                        (void) printf(gettext(", no estimated "
@@ -7417,7 +8215,7 @@ print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot)
                    ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
                        char *name = zpool_vdev_name(g_zfs, zhp,
                            child[c], VDEV_NAME_TYPE_ID);
-                       print_rebuild_status_impl(vrs, name);
+                       print_rebuild_status_impl(vrs, i, name);
                        free(name);
                }
        }
@@ -7508,10 +8306,12 @@ print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot)
 {
        uint64_t rebuild_end_time = 0, resilver_end_time = 0;
        boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE;
+       boolean_t have_errorscrub = B_FALSE;
        boolean_t active_resilver = B_FALSE;
        pool_checkpoint_stat_t *pcs = NULL;
        pool_scan_stat_t *ps = NULL;
        uint_t c;
+       time_t scrub_start = 0, errorscrub_start = 0;
 
        if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
            (uint64_t **)&ps, &c) == 0) {
@@ -7522,14 +8322,23 @@ print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot)
 
                have_resilver = (ps->pss_func == POOL_SCAN_RESILVER);
                have_scrub = (ps->pss_func == POOL_SCAN_SCRUB);
+               scrub_start = ps->pss_start_time;
+               if (c > offsetof(pool_scan_stat_t,
+                   pss_pass_error_scrub_pause) / 8) {
+                       have_errorscrub = (ps->pss_error_scrub_func ==
+                           POOL_SCAN_ERRORSCRUB);
+                       errorscrub_start = ps->pss_error_scrub_start;
+               }
        }
 
        boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time);
        boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0));
 
        /* Always print the scrub status when available. */
-       if (have_scrub)
+       if (have_scrub && scrub_start > errorscrub_start)
                print_scan_scrub_resilver_status(ps);
+       else if (have_errorscrub && errorscrub_start >= scrub_start)
+               print_err_scrub_status(ps);
 
        /*
         * When there is an active resilver or rebuild print its status.
@@ -7576,7 +8385,7 @@ print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
        vdev_name = zpool_vdev_name(g_zfs, zhp,
            child[prs->prs_removing_vdev], B_TRUE);
 
-       (void) printf(gettext("remove: "));
+       printf_color(ANSI_BOLD, gettext("remove: "));
 
        start = prs->prs_start_time;
        end = prs->prs_end_time;
@@ -7632,8 +8441,8 @@ print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
                 * do not print estimated time if hours_left is more than
                 * 30 days
                 */
-               (void) printf(gettext("    %s copied out of %s at %s/s, "
-                   "%.2f%% done"),
+               (void) printf(gettext(
+                   "\t%s copied out of %s at %s/s, %.2f%% done"),
                    examined_buf, total_buf, rate_buf, 100 * fraction_done);
                if (hours_left < (30 * 24)) {
                        (void) printf(gettext(", %lluh%um to go\n"),
@@ -7648,12 +8457,103 @@ print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
        if (prs->prs_mapping_memory > 0) {
                char mem_buf[7];
                zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
-               (void) printf(gettext("    %s memory used for "
-                   "removed device mappings\n"),
+               (void) printf(gettext(
+                   "\t%s memory used for removed device mappings\n"),
                    mem_buf);
        }
 }
 
+/*
+ * Print out detailed raidz expansion status.
+ */
+static void
+print_raidz_expand_status(zpool_handle_t *zhp, pool_raidz_expand_stat_t *pres)
+{
+       char copied_buf[7];
+
+       if (pres == NULL || pres->pres_state == DSS_NONE)
+               return;
+
+       /*
+        * Determine name of vdev.
+        */
+       nvlist_t *config = zpool_get_config(zhp, NULL);
+       nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
+           ZPOOL_CONFIG_VDEV_TREE);
+       nvlist_t **child;
+       uint_t children;
+       verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
+           &child, &children) == 0);
+       assert(pres->pres_expanding_vdev < children);
+
+       printf_color(ANSI_BOLD, gettext("expand: "));
+
+       time_t start = pres->pres_start_time;
+       time_t end = pres->pres_end_time;
+       char *vname =
+           zpool_vdev_name(g_zfs, zhp, child[pres->pres_expanding_vdev], 0);
+       zfs_nicenum(pres->pres_reflowed, copied_buf, sizeof (copied_buf));
+
+       /*
+        * Expansion is finished or canceled.
+        */
+       if (pres->pres_state == DSS_FINISHED) {
+               char time_buf[32];
+               secs_to_dhms(end - start, time_buf);
+
+               (void) printf(gettext("expanded %s-%u copied %s in %s, "
+                   "on %s"), vname, (int)pres->pres_expanding_vdev,
+                   copied_buf, time_buf, ctime((time_t *)&end));
+       } else {
+               char examined_buf[7], total_buf[7], rate_buf[7];
+               uint64_t copied, total, elapsed, secs_left;
+               double fraction_done;
+               uint_t rate;
+
+               assert(pres->pres_state == DSS_SCANNING);
+
+               /*
+                * Expansion is in progress.
+                */
+               (void) printf(gettext(
+                   "expansion of %s-%u in progress since %s"),
+                   vname, (int)pres->pres_expanding_vdev, ctime(&start));
+
+               copied = pres->pres_reflowed > 0 ? pres->pres_reflowed : 1;
+               total = pres->pres_to_reflow;
+               fraction_done = (double)copied / total;
+
+               /* elapsed time for this pass */
+               elapsed = time(NULL) - pres->pres_start_time;
+               elapsed = elapsed > 0 ? elapsed : 1;
+               rate = copied / elapsed;
+               rate = rate > 0 ? rate : 1;
+               secs_left = (total - copied) / rate;
+
+               zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
+               zfs_nicenum(total, total_buf, sizeof (total_buf));
+               zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
+
+               /*
+                * do not print estimated time if hours_left is more than
+                * 30 days
+                */
+               (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"),
+                   examined_buf, total_buf, rate_buf, 100 * fraction_done);
+               if (pres->pres_waiting_for_resilver) {
+                       (void) printf(gettext(", paused for resilver or "
+                           "clear\n"));
+               } else if (secs_left < (30 * 24 * 3600)) {
+                       char time_buf[32];
+                       secs_to_dhms(secs_left, time_buf);
+                       (void) printf(gettext(", %s to go\n"), time_buf);
+               } else {
+                       (void) printf(gettext(
+                           ", (copy is slow, no estimated time)\n"));
+               }
+       }
+       free(vname);
+}
 static void
 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
 {
@@ -7819,7 +8719,7 @@ status_callback(zpool_handle_t *zhp, void *data)
 {
        status_cbdata_t *cbp = data;
        nvlist_t *config, *nvroot;
-       char *msgid;
+       const char *msgid;
        zpool_status_t reason;
        zpool_errata_t errata;
        const char *health;
@@ -7838,7 +8738,9 @@ status_callback(zpool_handle_t *zhp, void *data)
        if (cbp->cb_explain &&
            (reason == ZPOOL_STATUS_OK ||
            reason == ZPOOL_STATUS_VERSION_OLDER ||
-           reason == ZPOOL_STATUS_FEAT_DISABLED)) {
+           reason == ZPOOL_STATUS_FEAT_DISABLED ||
+           reason == ZPOOL_STATUS_COMPATIBILITY_ERR ||
+           reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) {
                if (!cbp->cb_allpools) {
                        (void) printf(gettext("pool '%s' is healthy\n"),
                            zpool_get_name(zhp));
@@ -7862,12 +8764,12 @@ status_callback(zpool_handle_t *zhp, void *data)
        printf("  ");
        printf_color(ANSI_BOLD, gettext("pool:"));
        printf(" %s\n", zpool_get_name(zhp));
-       printf(" ");
+       fputc(' ', stdout);
        printf_color(ANSI_BOLD, gettext("state: "));
 
        printf_color(health_str_to_color(health), "%s", health);
 
-       printf("\n");
+       fputc('\n', stdout);
 
        switch (reason) {
        case ZPOOL_STATUS_MISSING_DEV_R:
@@ -8013,14 +8915,40 @@ status_callback(zpool_handle_t *zhp, void *data)
 
        case ZPOOL_STATUS_FEAT_DISABLED:
                printf_color(ANSI_BOLD, gettext("status: "));
-               printf_color(ANSI_YELLOW, gettext("Some supported features are "
-                   "not enabled on the pool. The pool can\n\tstill be used, "
-                   "but some features are unavailable.\n"));
+               printf_color(ANSI_YELLOW, gettext("Some supported and "
+                   "requested features are not enabled on the pool.\n\t"
+                   "The pool can still be used, but some features are "
+                   "unavailable.\n"));
                printf_color(ANSI_BOLD, gettext("action: "));
                printf_color(ANSI_YELLOW, gettext("Enable all features using "
                    "'zpool upgrade'. Once this is done,\n\tthe pool may no "
                    "longer be accessible by software that does not support\n\t"
-                   "the features. See zpool-features(5) for details.\n"));
+                   "the features. See zpool-features(7) for details.\n"));
+               break;
+
+       case ZPOOL_STATUS_COMPATIBILITY_ERR:
+               printf_color(ANSI_BOLD, gettext("status: "));
+               printf_color(ANSI_YELLOW, gettext("This pool has a "
+                   "compatibility list specified, but it could not be\n\t"
+                   "read/parsed at this time. The pool can still be used, "
+                   "but this\n\tshould be investigated.\n"));
+               printf_color(ANSI_BOLD, gettext("action: "));
+               printf_color(ANSI_YELLOW, gettext("Check the value of the "
+                   "'compatibility' property against the\n\t"
+                   "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or "
+                   ZPOOL_DATA_COMPAT_D ".\n"));
+               break;
+
+       case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
+               printf_color(ANSI_BOLD, gettext("status: "));
+               printf_color(ANSI_YELLOW, gettext("One or more features "
+                   "are enabled on the pool despite not being\n\t"
+                   "requested by the 'compatibility' property.\n"));
+               printf_color(ANSI_BOLD, gettext("action: "));
+               printf_color(ANSI_YELLOW, gettext("Consider setting "
+                   "'compatibility' to an appropriate value, or\n\t"
+                   "adding needed features to the relevant file in\n\t"
+                   ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n"));
                break;
 
        case ZPOOL_STATUS_UNSUP_FEAT_READ:
@@ -8203,19 +9131,24 @@ status_callback(zpool_handle_t *zhp, void *data)
                uint64_t nerr;
                nvlist_t **spares, **l2cache;
                uint_t nspares, nl2cache;
-               pool_checkpoint_stat_t *pcs = NULL;
-               pool_removal_stat_t *prs = NULL;
 
                print_scan_status(zhp, nvroot);
 
+               pool_removal_stat_t *prs = NULL;
                (void) nvlist_lookup_uint64_array(nvroot,
                    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
                print_removal_status(zhp, prs);
 
+               pool_checkpoint_stat_t *pcs = NULL;
                (void) nvlist_lookup_uint64_array(nvroot,
                    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
                print_checkpoint_status(pcs);
 
+               pool_raidz_expand_stat_t *pres = NULL;
+               (void) nvlist_lookup_uint64_array(nvroot,
+                   ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c);
+               print_raidz_expand_status(zhp, pres);
+
                cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
                    cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
                if (cbp->cb_namewidth < 10)
@@ -8232,6 +9165,10 @@ status_callback(zpool_handle_t *zhp, void *data)
                        printf_color(ANSI_BOLD, " %5s", gettext("SLOW"));
                }
 
+               if (cbp->cb_print_power) {
+                       printf_color(ANSI_BOLD, " %5s", gettext("POWER"));
+               }
+
                if (cbp->vcdl != NULL)
                        print_cmd_columns(cbp->vcdl, 0);
 
@@ -8254,37 +9191,19 @@ status_callback(zpool_handle_t *zhp, void *data)
 
                if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
                    &nerr) == 0) {
-                       nvlist_t *nverrlist = NULL;
-
-                       /*
-                        * If the approximate error count is small, get a
-                        * precise count by fetching the entire log and
-                        * uniquifying the results.
-                        */
-                       if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
-                           zpool_get_errlog(zhp, &nverrlist) == 0) {
-                               nvpair_t *elem;
-
-                               elem = NULL;
-                               nerr = 0;
-                               while ((elem = nvlist_next_nvpair(nverrlist,
-                                   elem)) != NULL) {
-                                       nerr++;
-                               }
-                       }
-                       nvlist_free(nverrlist);
-
                        (void) printf("\n");
-
-                       if (nerr == 0)
-                               (void) printf(gettext("errors: No known data "
-                                   "errors\n"));
-                       else if (!cbp->cb_verbose)
+                       if (nerr == 0) {
+                               (void) printf(gettext(
+                                   "errors: No known data errors\n"));
+                       } else if (!cbp->cb_verbose) {
+                               color_start(ANSI_RED);
                                (void) printf(gettext("errors: %llu data "
                                    "errors, use '-v' for a list\n"),
                                    (u_longlong_t)nerr);
-                       else
+                               color_end();
+                       } else {
                                print_error_log(zhp);
+                       }
                }
 
                if (cbp->cb_dedup_stats)
@@ -8298,21 +9217,23 @@ status_callback(zpool_handle_t *zhp, void *data)
 }
 
 /*
- * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
- *              [interval [count]]
+ * zpool status [-c [script1,script2,...]] [-DegiLpPstvx] [--power] [-T d|u] ...
+ *              [pool] [interval [count]]
  *
  *     -c CMD  For each vdev, run command CMD
- *     -i      Display vdev initialization status.
+ *     -D      Display dedup status (undocumented)
+ *     -e      Display only unhealthy vdevs
  *     -g      Display guid for individual vdev name.
+ *     -i      Display vdev initialization status.
  *     -L      Follow links when resolving vdev path name.
  *     -p      Display values in parsable (exact) format.
  *     -P      Display full path for vdev name.
  *     -s      Display slow IOs column.
- *     -v      Display complete error logs
- *     -x      Display only pools with potential problems
- *     -D      Display dedup status (undocumented)
  *     -t      Display vdev TRIM status.
  *     -T      Display a timestamp in date(1) or Unix format
+ *     -v      Display complete error logs
+ *     -x      Display only pools with potential problems
+ *     --power Display vdev enclosure slot power status
  *
  * Describes the health status of all pools or some subset.
  */
@@ -8326,8 +9247,14 @@ zpool_do_status(int argc, char **argv)
        status_cbdata_t cb = { 0 };
        char *cmd = NULL;
 
+       struct option long_options[] = {
+               {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
+               {0, 0, 0, 0}
+       };
+
        /* check options */
-       while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) {
+       while ((c = getopt_long(argc, argv, "c:DegiLpPstT:vx", long_options,
+           NULL)) != -1) {
                switch (c) {
                case 'c':
                        if (cmd != NULL) {
@@ -8353,12 +9280,18 @@ zpool_do_status(int argc, char **argv)
                        }
                        cmd = optarg;
                        break;
-               case 'i':
-                       cb.cb_print_vdev_init = B_TRUE;
+               case 'D':
+                       cb.cb_dedup_stats = B_TRUE;
+                       break;
+               case 'e':
+                       cb.cb_print_unhealthy = B_TRUE;
                        break;
                case 'g':
                        cb.cb_name_flags |= VDEV_NAME_GUID;
                        break;
+               case 'i':
+                       cb.cb_print_vdev_init = B_TRUE;
+                       break;
                case 'L':
                        cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
                        break;
@@ -8371,20 +9304,20 @@ zpool_do_status(int argc, char **argv)
                case 's':
                        cb.cb_print_slow_ios = B_TRUE;
                        break;
+               case 't':
+                       cb.cb_print_vdev_trim = B_TRUE;
+                       break;
+               case 'T':
+                       get_timestamp_arg(*optarg);
+                       break;
                case 'v':
                        cb.cb_verbose = B_TRUE;
                        break;
                case 'x':
                        cb.cb_explain = B_TRUE;
                        break;
-               case 'D':
-                       cb.cb_dedup_stats = B_TRUE;
-                       break;
-               case 't':
-                       cb.cb_print_vdev_trim = B_TRUE;
-                       break;
-               case 'T':
-                       get_timestamp_arg(*optarg);
+               case ZPOOL_OPTION_POWER:
+                       cb.cb_print_power = B_TRUE;
                        break;
                case '?':
                        if (optopt == 'c') {
@@ -8417,12 +9350,11 @@ zpool_do_status(int argc, char **argv)
                        cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
                            NULL, NULL, 0, 0);
 
-               ret = for_each_pool(argc, argv, B_TRUE, NULL,
-                   status_callback, &cb);
+               ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+                   cb.cb_literal, status_callback, &cb);
 
                if (cb.vcdl != NULL)
                        free_vdev_cmd_data_list(cb.vcdl);
-
                if (argc == 0 && cb.cb_count == 0)
                        (void) fprintf(stderr, gettext("no pools available\n"));
                else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
@@ -8437,6 +9369,7 @@ zpool_do_status(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
+               (void) fflush(stdout);
                (void) fsleep(interval);
        }
 
@@ -8463,7 +9396,7 @@ check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
                (*count)++;
        }
 
-       zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
+       zfs_iter_filesystems_v2(zhp, 0, check_unsupp_fs, unsupp_fs);
 
        zfs_close(zhp);
 
@@ -8482,6 +9415,11 @@ upgrade_version(zpool_handle_t *zhp, uint64_t version)
        verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
            &oldversion) == 0);
 
+       char compat[ZFS_MAXPROPLEN];
+       if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
+           ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
+               compat[0] = '\0';
+
        assert(SPA_VERSION_IS_SUPPORTED(oldversion));
        assert(oldversion < version);
 
@@ -8496,6 +9434,13 @@ upgrade_version(zpool_handle_t *zhp, uint64_t version)
                return (1);
        }
 
+       if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
+               (void) fprintf(stderr, gettext("Upgrade not performed because "
+                   "'compatibility' property set to '"
+                   ZPOOL_COMPAT_LEGACY "'.\n"));
+               return (1);
+       }
+
        ret = zpool_upgrade(zhp, version);
        if (ret != 0)
                return (ret);
@@ -8521,11 +9466,25 @@ upgrade_enable_all(zpool_handle_t *zhp, int *countp)
        boolean_t firstff = B_TRUE;
        nvlist_t *enabled = zpool_get_features(zhp);
 
+       char compat[ZFS_MAXPROPLEN];
+       if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
+           ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
+               compat[0] = '\0';
+
+       boolean_t requested_features[SPA_FEATURES];
+       if (zpool_do_load_compat(compat, requested_features) !=
+           ZPOOL_COMPATIBILITY_OK)
+               return (-1);
+
        count = 0;
        for (i = 0; i < SPA_FEATURES; i++) {
                const char *fname = spa_feature_table[i].fi_uname;
                const char *fguid = spa_feature_table[i].fi_guid;
-               if (!nvlist_exists(enabled, fguid)) {
+
+               if (!spa_feature_table[i].fi_zfs_mod_supported)
+                       continue;
+
+               if (!nvlist_exists(enabled, fguid) && requested_features[i]) {
                        char *propname;
                        verify(-1 != asprintf(&propname, "feature@%s", fname));
                        ret = zpool_set_prop(zhp, propname,
@@ -8558,7 +9517,7 @@ upgrade_cb(zpool_handle_t *zhp, void *arg)
        upgrade_cbdata_t *cbp = arg;
        nvlist_t *config;
        uint64_t version;
-       boolean_t printnl = B_FALSE;
+       boolean_t modified_pool = B_FALSE;
        int ret;
 
        config = zpool_get_config(zhp, NULL);
@@ -8572,7 +9531,7 @@ upgrade_cb(zpool_handle_t *zhp, void *arg)
                ret = upgrade_version(zhp, cbp->cb_version);
                if (ret != 0)
                        return (ret);
-               printnl = B_TRUE;
+               modified_pool = B_TRUE;
 
                /*
                 * If they did "zpool upgrade -a", then we could
@@ -8592,12 +9551,13 @@ upgrade_cb(zpool_handle_t *zhp, void *arg)
 
                if (count > 0) {
                        cbp->cb_first = B_FALSE;
-                       printnl = B_TRUE;
+                       modified_pool = B_TRUE;
                }
        }
 
-       if (printnl) {
-               (void) printf(gettext("\n"));
+       if (modified_pool) {
+               (void) printf("\n");
+               (void) after_zpool_upgrade(zhp);
        }
 
        return (0);
@@ -8623,7 +9583,10 @@ upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
                            "be upgraded to use feature flags.  After "
                            "being upgraded, these pools\nwill no "
                            "longer be accessible by software that does not "
-                           "support feature\nflags.\n\n"));
+                           "support feature\nflags.\n\n"
+                           "Note that setting a pool's 'compatibility' "
+                           "feature to '" ZPOOL_COMPAT_LEGACY "' will\n"
+                           "inhibit upgrades.\n\n"));
                        (void) printf(gettext("VER  POOL\n"));
                        (void) printf(gettext("---  ------------\n"));
                        cbp->cb_first = B_FALSE;
@@ -8655,6 +9618,10 @@ upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
                for (i = 0; i < SPA_FEATURES; i++) {
                        const char *fguid = spa_feature_table[i].fi_guid;
                        const char *fname = spa_feature_table[i].fi_uname;
+
+                       if (!spa_feature_table[i].fi_zfs_mod_supported)
+                               continue;
+
                        if (!nvlist_exists(enabled, fguid)) {
                                if (cbp->cb_first) {
                                        (void) printf(gettext("\nSome "
@@ -8664,8 +9631,12 @@ upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
                                            "pool may become incompatible with "
                                            "software\nthat does not support "
                                            "the feature. See "
-                                           "zpool-features(5) for "
-                                           "details.\n\n"));
+                                           "zpool-features(7) for "
+                                           "details.\n\n"
+                                           "Note that the pool "
+                                           "'compatibility' feature can be "
+                                           "used to inhibit\nfeature "
+                                           "upgrades.\n\n"));
                                        (void) printf(gettext("POOL  "
                                            "FEATURE\n"));
                                        (void) printf(gettext("------"
@@ -8695,11 +9666,10 @@ upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
        return (0);
 }
 
-/* ARGSUSED */
 static int
 upgrade_one(zpool_handle_t *zhp, void *data)
 {
-       boolean_t printnl = B_FALSE;
+       boolean_t modified_pool = B_FALSE;
        upgrade_cbdata_t *cbp = data;
        uint64_t cur_version;
        int ret;
@@ -8727,7 +9697,7 @@ upgrade_one(zpool_handle_t *zhp, void *data)
        }
 
        if (cur_version != cbp->cb_version) {
-               printnl = B_TRUE;
+               modified_pool = B_TRUE;
                ret = upgrade_version(zhp, cbp->cb_version);
                if (ret != 0)
                        return (ret);
@@ -8740,16 +9710,17 @@ upgrade_one(zpool_handle_t *zhp, void *data)
                        return (ret);
 
                if (count != 0) {
-                       printnl = B_TRUE;
+                       modified_pool = B_TRUE;
                } else if (cur_version == SPA_VERSION) {
                        (void) printf(gettext("Pool '%s' already has all "
-                           "supported features enabled.\n"),
+                           "supported and requested features enabled.\n"),
                            zpool_get_name(zhp));
                }
        }
 
-       if (printnl) {
-               (void) printf(gettext("\n"));
+       if (modified_pool) {
+               (void) printf("\n");
+               (void) after_zpool_upgrade(zhp);
        }
 
        return (0);
@@ -8832,7 +9803,7 @@ zpool_do_upgrade(int argc, char **argv)
                }
        }
 
-       (void) printf(gettext("This system supports ZFS pool feature "
+       (void) printf("%s", gettext("This system supports ZFS pool feature "
            "flags.\n\n"));
        if (showversions) {
                int i;
@@ -8844,6 +9815,8 @@ zpool_do_upgrade(int argc, char **argv)
                    "---------------\n");
                for (i = 0; i < SPA_FEATURES; i++) {
                        zfeature_info_t *fi = &spa_feature_table[i];
+                       if (!fi->fi_zfs_mod_supported)
+                               continue;
                        const char *ro =
                            (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
                            " (read-only compatible)" : "";
@@ -8904,8 +9877,8 @@ zpool_do_upgrade(int argc, char **argv)
                                (void) printf(gettext("All pools are already "
                                    "formatted using feature flags.\n\n"));
                                (void) printf(gettext("Every feature flags "
-                                   "pool already has all supported features "
-                                   "enabled.\n"));
+                                   "pool already has all supported and "
+                                   "requested features enabled.\n"));
                        } else {
                                (void) printf(gettext("All pools are already "
                                    "formatted with version %llu or higher.\n"),
@@ -8931,13 +9904,13 @@ zpool_do_upgrade(int argc, char **argv)
 
                if (cb.cb_first) {
                        (void) printf(gettext("Every feature flags pool has "
-                           "all supported features enabled.\n"));
+                           "all supported and requested features enabled.\n"));
                } else {
                        (void) printf(gettext("\n"));
                }
        } else {
-               ret = for_each_pool(argc, argv, B_FALSE, NULL,
-                   upgrade_one, &cb);
+               ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
+                   B_FALSE, upgrade_one, &cb);
        }
 
        return (ret);
@@ -8960,7 +9933,7 @@ print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
            &records, &numrecords) == 0);
        for (i = 0; i < numrecords; i++) {
                nvlist_t *rec = records[i];
-               char tbuf[30] = "";
+               char tbuf[64] = "";
 
                if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
                        time_t tsec;
@@ -8972,6 +9945,14 @@ print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
                        (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
                }
 
+               if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) {
+                       uint64_t elapsed_ns = fnvlist_lookup_int64(records[i],
+                           ZPOOL_HIST_ELAPSED_NS);
+                       (void) snprintf(tbuf + strlen(tbuf),
+                           sizeof (tbuf) - strlen(tbuf),
+                           " (%lldms)", (long long)elapsed_ns / 1000 / 1000);
+               }
+
                if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
                        (void) printf("%s %s", tbuf,
                            fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
@@ -9022,6 +10003,12 @@ print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
                                dump_nvlist(fnvlist_lookup_nvlist(rec,
                                    ZPOOL_HIST_OUTPUT_NVL), 8);
                        }
+                       if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) {
+                               (void) printf("    output nvlist omitted; "
+                                   "original size: %lldKB\n",
+                                   (longlong_t)fnvlist_lookup_int64(rec,
+                                   ZPOOL_HIST_OUTPUT_SIZE) / 1024);
+                       }
                        if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
                                (void) printf("    errno: %lld\n",
                                    (longlong_t)fnvlist_lookup_int64(rec,
@@ -9119,8 +10106,8 @@ zpool_do_history(int argc, char **argv)
        argc -= optind;
        argv += optind;
 
-       ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
-           &cbdata);
+       ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, get_history_one, &cbdata);
 
        if (argc == 0 && cbdata.first == B_TRUE) {
                (void) fprintf(stderr, gettext("no pools available\n"));
@@ -9141,7 +10128,8 @@ typedef struct ev_opts {
 static void
 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
 {
-       char ctime_str[26], str[32], *ptr;
+       char ctime_str[26], str[32];
+       const char *ptr;
        int64_t *tv;
        uint_t n;
 
@@ -9177,7 +10165,7 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
                uint16_t i16;
                uint32_t i32;
                uint64_t i64;
-               char *str;
+               const char *str;
                nvlist_t *cnv;
 
                printf(gettext("%*s%s = "), depth, "", name);
@@ -9375,7 +10363,7 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
                        }
 
                case DATA_TYPE_STRING_ARRAY: {
-                       char **str;
+                       const char **str;
                        uint_t i, nelem;
 
                        (void) nvpair_value_string_array(nvp, &str, &nelem);
@@ -9404,7 +10392,7 @@ zpool_do_events_next(ev_opts_t *opts)
 {
        nvlist_t *nvl;
        int zevent_fd, ret, dropped;
-       char *pool;
+       const char *pool;
 
        zevent_fd = open(ZFS_DEV, O_RDWR);
        VERIFY(zevent_fd >= 0);
@@ -9443,7 +10431,7 @@ zpool_do_events_next(ev_opts_t *opts)
 }
 
 static int
-zpool_do_events_clear(ev_opts_t *opts)
+zpool_do_events_clear(void)
 {
        int count, ret;
 
@@ -9510,7 +10498,7 @@ zpool_do_events(int argc, char **argv)
        }
 
        if (opts.clear)
-               ret = zpool_do_events_clear(&opts);
+               ret = zpool_do_events_clear();
        else
                ret = zpool_do_events_next(&opts);
 
@@ -9518,44 +10506,145 @@ zpool_do_events(int argc, char **argv)
 }
 
 static int
-get_callback(zpool_handle_t *zhp, void *data)
+get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data)
 {
        zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
-       char value[MAXNAMELEN];
+       char value[ZFS_MAXPROPLEN];
        zprop_source_t srctype;
-       zprop_list_t *pl;
-
-       for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
 
+       for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL;
+           pl = pl->pl_next) {
+               char *prop_name;
                /*
-                * Skip the special fake placeholder. This will also skip
+                * If the first property is pool name, it is a special
+                * placeholder that we can skip. This will also skip
                 * over the name property when 'all' is specified.
                 */
                if (pl->pl_prop == ZPOOL_PROP_NAME &&
                    pl == cbp->cb_proplist)
                        continue;
 
-               if (pl->pl_prop == ZPROP_INVAL &&
-                   (zpool_prop_feature(pl->pl_user_prop) ||
-                   zpool_prop_unsupported(pl->pl_user_prop))) {
-                       srctype = ZPROP_SRC_LOCAL;
+               if (pl->pl_prop == ZPROP_INVAL) {
+                       prop_name = pl->pl_user_prop;
+               } else {
+                       prop_name = (char *)vdev_prop_to_name(pl->pl_prop);
+               }
+               if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop,
+                   prop_name, value, sizeof (value), &srctype,
+                   cbp->cb_literal) == 0) {
+                       zprop_print_one_property(vdevname, cbp, prop_name,
+                           value, srctype, NULL, NULL);
+               }
+       }
+
+       return (0);
+}
+
+static int
+get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data)
+{
+       zpool_handle_t *zhp = zhp_data;
+       zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
+       char *vdevname;
+       const char *type;
+       int ret;
+
+       /*
+        * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the
+        * pool name for display purposes, which is not desired. Fallback to
+        * zpool_vdev_name() when not dealing with the root vdev.
+        */
+       type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE);
+       if (zhp != NULL && strcmp(type, "root") == 0)
+               vdevname = strdup("root-0");
+       else
+               vdevname = zpool_vdev_name(g_zfs, zhp, nv,
+                   cbp->cb_vdevs.cb_name_flags);
+
+       (void) vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist);
+
+       ret = get_callback_vdev(zhp, vdevname, data);
+
+       free(vdevname);
+
+       return (ret);
+}
+
+static int
+get_callback(zpool_handle_t *zhp, void *data)
+{
+       zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
+       char value[ZFS_MAXPROPLEN];
+       zprop_source_t srctype;
+       zprop_list_t *pl;
+       int vid;
+
+       if (cbp->cb_type == ZFS_TYPE_VDEV) {
+               if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) {
+                       for_each_vdev(zhp, get_callback_vdev_cb, data);
+               } else {
+                       /* Adjust column widths for vdev properties */
+                       for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
+                           vid++) {
+                               vdev_expand_proplist(zhp,
+                                   cbp->cb_vdevs.cb_names[vid],
+                                   &cbp->cb_proplist);
+                       }
+                       /* Display the properties */
+                       for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
+                           vid++) {
+                               get_callback_vdev(zhp,
+                                   cbp->cb_vdevs.cb_names[vid], data);
+                       }
+               }
+       } else {
+               assert(cbp->cb_type == ZFS_TYPE_POOL);
+               for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
+                       /*
+                        * Skip the special fake placeholder. This will also
+                        * skip over the name property when 'all' is specified.
+                        */
+                       if (pl->pl_prop == ZPOOL_PROP_NAME &&
+                           pl == cbp->cb_proplist)
+                               continue;
+
+                       if (pl->pl_prop == ZPROP_INVAL &&
+                           zfs_prop_user(pl->pl_user_prop)) {
+                               srctype = ZPROP_SRC_LOCAL;
+
+                               if (zpool_get_userprop(zhp, pl->pl_user_prop,
+                                   value, sizeof (value), &srctype) != 0)
+                                       continue;
 
-                       if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
-                           value, sizeof (value)) == 0) {
                                zprop_print_one_property(zpool_get_name(zhp),
                                    cbp, pl->pl_user_prop, value, srctype,
                                    NULL, NULL);
-                       }
-               } else {
-                       if (zpool_get_prop(zhp, pl->pl_prop, value,
-                           sizeof (value), &srctype, cbp->cb_literal) != 0)
-                               continue;
+                       } else if (pl->pl_prop == ZPROP_INVAL &&
+                           (zpool_prop_feature(pl->pl_user_prop) ||
+                           zpool_prop_unsupported(pl->pl_user_prop))) {
+                               srctype = ZPROP_SRC_LOCAL;
+
+                               if (zpool_prop_get_feature(zhp,
+                                   pl->pl_user_prop, value,
+                                   sizeof (value)) == 0) {
+                                       zprop_print_one_property(
+                                           zpool_get_name(zhp), cbp,
+                                           pl->pl_user_prop, value, srctype,
+                                           NULL, NULL);
+                               }
+                       } else {
+                               if (zpool_get_prop(zhp, pl->pl_prop, value,
+                                   sizeof (value), &srctype,
+                                   cbp->cb_literal) != 0)
+                                       continue;
 
-                       zprop_print_one_property(zpool_get_name(zhp), cbp,
-                           zpool_prop_to_name(pl->pl_prop), value, srctype,
-                           NULL, NULL);
+                               zprop_print_one_property(zpool_get_name(zhp),
+                                   cbp, zpool_prop_to_name(pl->pl_prop),
+                                   value, srctype, NULL, NULL);
+                       }
                }
        }
+
        return (0);
 }
 
@@ -9578,7 +10667,8 @@ zpool_do_get(int argc, char **argv)
        zprop_list_t fake_name = { 0 };
        int ret;
        int c, i;
-       char *value;
+       char *propstr = NULL;
+       char *vdev = NULL;
 
        cb.cb_first = B_TRUE;
 
@@ -9591,6 +10681,8 @@ zpool_do_get(int argc, char **argv)
        cb.cb_columns[2] = GET_COL_VALUE;
        cb.cb_columns[3] = GET_COL_SOURCE;
        cb.cb_type = ZFS_TYPE_POOL;
+       cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
+       current_prop_type = cb.cb_type;
 
        /* check options */
        while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
@@ -9602,35 +10694,34 @@ zpool_do_get(int argc, char **argv)
                        cb.cb_scripted = B_TRUE;
                        break;
                case 'o':
-                       bzero(&cb.cb_columns, sizeof (cb.cb_columns));
+                       memset(&cb.cb_columns, 0, sizeof (cb.cb_columns));
                        i = 0;
-                       while (*optarg != '\0') {
-                               static char *col_subopts[] =
+
+                       for (char *tok; (tok = strsep(&optarg, ",")); ) {
+                               static const char *const col_opts[] =
                                { "name", "property", "value", "source",
-                               "all", NULL };
+                                   "all" };
+                               static const zfs_get_column_t col_cols[] =
+                               { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE,
+                                   GET_COL_SOURCE };
 
-                               if (i == ZFS_GET_NCOLS) {
+                               if (i == ZFS_GET_NCOLS - 1) {
                                        (void) fprintf(stderr, gettext("too "
                                        "many fields given to -o "
                                        "option\n"));
                                        usage(B_FALSE);
                                }
 
-                               switch (getsubopt(&optarg, col_subopts,
-                                   &value)) {
-                               case 0:
-                                       cb.cb_columns[i++] = GET_COL_NAME;
-                                       break;
-                               case 1:
-                                       cb.cb_columns[i++] = GET_COL_PROPERTY;
-                                       break;
-                               case 2:
-                                       cb.cb_columns[i++] = GET_COL_VALUE;
-                                       break;
-                               case 3:
-                                       cb.cb_columns[i++] = GET_COL_SOURCE;
-                                       break;
-                               case 4:
+                               for (c = 0; c < ARRAY_SIZE(col_opts); ++c)
+                                       if (strcmp(tok, col_opts[c]) == 0)
+                                               goto found;
+
+                               (void) fprintf(stderr,
+                                   gettext("invalid column name '%s'\n"), tok);
+                               usage(B_FALSE);
+
+found:
+                               if (c >= 4) {
                                        if (i > 0) {
                                                (void) fprintf(stderr,
                                                    gettext("\"all\" conflicts "
@@ -9638,18 +10729,12 @@ zpool_do_get(int argc, char **argv)
                                                    "given to -o option\n"));
                                                usage(B_FALSE);
                                        }
-                                       cb.cb_columns[0] = GET_COL_NAME;
-                                       cb.cb_columns[1] = GET_COL_PROPERTY;
-                                       cb.cb_columns[2] = GET_COL_VALUE;
-                                       cb.cb_columns[3] = GET_COL_SOURCE;
-                                       i = ZFS_GET_NCOLS;
-                                       break;
-                               default:
-                                       (void) fprintf(stderr,
-                                           gettext("invalid column name "
-                                           "'%s'\n"), value);
-                                       usage(B_FALSE);
-                               }
+
+                                       memcpy(cb.cb_columns, col_cols,
+                                           sizeof (col_cols));
+                                       i = ZFS_GET_NCOLS - 1;
+                               } else
+                                       cb.cb_columns[i++] = col_cols[c];
                        }
                        break;
                case '?':
@@ -9668,13 +10753,58 @@ zpool_do_get(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
-           ZFS_TYPE_POOL) != 0)
-               usage(B_FALSE);
+       /* Properties list is needed later by zprop_get_list() */
+       propstr = argv[0];
 
        argc--;
        argv++;
 
+       if (argc == 0) {
+               /* No args, so just print the defaults. */
+       } else if (are_all_pools(argc, argv)) {
+               /* All the args are pool names */
+       } else if (are_all_pools(1, argv)) {
+               /* The first arg is a pool name */
+               if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) ||
+                   (argc == 2 && strcmp(argv[1], "root") == 0) ||
+                   are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
+                   &cb.cb_vdevs)) {
+
+                       if (strcmp(argv[1], "root") == 0)
+                               vdev = strdup("root-0");
+                       else
+                               vdev = strdup(argv[1]);
+
+                       /* ... and the rest are vdev names */
+                       cb.cb_vdevs.cb_names = &vdev;
+                       cb.cb_vdevs.cb_names_count = argc - 1;
+                       cb.cb_type = ZFS_TYPE_VDEV;
+                       argc = 1; /* One pool to process */
+               } else {
+                       fprintf(stderr, gettext("Expected a list of vdevs in"
+                           " \"%s\", but got:\n"), argv[0]);
+                       error_list_unresolved_vdevs(argc - 1, argv + 1,
+                           argv[0], &cb.cb_vdevs);
+                       fprintf(stderr, "\n");
+                       usage(B_FALSE);
+                       return (1);
+               }
+       } else {
+               /*
+                * The first arg isn't the name of a valid pool.
+                */
+               fprintf(stderr, gettext("Cannot get properties of %s: "
+                   "no such pool available.\n"), argv[0]);
+               return (1);
+       }
+
+       if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist,
+           cb.cb_type) != 0) {
+               /* Use correct list of valid properties (pool or vdev) */
+               current_prop_type = cb.cb_type;
+               usage(B_FALSE);
+       }
+
        if (cb.cb_proplist != NULL) {
                fake_name.pl_prop = ZPOOL_PROP_NAME;
                fake_name.pl_width = strlen(gettext("NAME"));
@@ -9682,34 +10812,110 @@ zpool_do_get(int argc, char **argv)
                cb.cb_proplist = &fake_name;
        }
 
-       ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
-           get_callback, &cb);
+       ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type,
+           cb.cb_literal, get_callback, &cb);
 
        if (cb.cb_proplist == &fake_name)
                zprop_free_list(fake_name.pl_next);
        else
                zprop_free_list(cb.cb_proplist);
 
+       if (vdev != NULL)
+               free(vdev);
+
        return (ret);
 }
 
 typedef struct set_cbdata {
        char *cb_propname;
        char *cb_value;
+       zfs_type_t cb_type;
+       vdev_cbdata_t cb_vdevs;
        boolean_t cb_any_successful;
 } set_cbdata_t;
 
 static int
-set_callback(zpool_handle_t *zhp, void *data)
+set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb)
 {
        int error;
-       set_cbdata_t *cb = (set_cbdata_t *)data;
+
+       /* Check if we have out-of-bounds features */
+       if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) {
+               boolean_t features[SPA_FEATURES];
+               if (zpool_do_load_compat(cb->cb_value, features) !=
+                   ZPOOL_COMPATIBILITY_OK)
+                       return (-1);
+
+               nvlist_t *enabled = zpool_get_features(zhp);
+               spa_feature_t i;
+               for (i = 0; i < SPA_FEATURES; i++) {
+                       const char *fguid = spa_feature_table[i].fi_guid;
+                       if (nvlist_exists(enabled, fguid) && !features[i])
+                               break;
+               }
+               if (i < SPA_FEATURES)
+                       (void) fprintf(stderr, gettext("Warning: one or "
+                           "more features already enabled on pool '%s'\n"
+                           "are not present in this compatibility set.\n"),
+                           zpool_get_name(zhp));
+       }
+
+       /* if we're setting a feature, check it's in compatibility set */
+       if (zpool_prop_feature(cb->cb_propname) &&
+           strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) {
+               char *fname = strchr(cb->cb_propname, '@') + 1;
+               spa_feature_t f;
+
+               if (zfeature_lookup_name(fname, &f) == 0) {
+                       char compat[ZFS_MAXPROPLEN];
+                       if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY,
+                           compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
+                               compat[0] = '\0';
+
+                       boolean_t features[SPA_FEATURES];
+                       if (zpool_do_load_compat(compat, features) !=
+                           ZPOOL_COMPATIBILITY_OK) {
+                               (void) fprintf(stderr, gettext("Error: "
+                                   "cannot enable feature '%s' on pool '%s'\n"
+                                   "because the pool's 'compatibility' "
+                                   "property cannot be parsed.\n"),
+                                   fname, zpool_get_name(zhp));
+                               return (-1);
+                       }
+
+                       if (!features[f]) {
+                               (void) fprintf(stderr, gettext("Error: "
+                                   "cannot enable feature '%s' on pool '%s'\n"
+                                   "as it is not specified in this pool's "
+                                   "current compatibility set.\n"
+                                   "Consider setting 'compatibility' to a "
+                                   "less restrictive set, or to 'off'.\n"),
+                                   fname, zpool_get_name(zhp));
+                               return (-1);
+                       }
+               }
+       }
 
        error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
 
-       if (!error)
-               cb->cb_any_successful = B_TRUE;
+       return (error);
+}
 
+static int
+set_callback(zpool_handle_t *zhp, void *data)
+{
+       int error;
+       set_cbdata_t *cb = (set_cbdata_t *)data;
+
+       if (cb->cb_type == ZFS_TYPE_VDEV) {
+               error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names,
+                   cb->cb_propname, cb->cb_value);
+       } else {
+               assert(cb->cb_type == ZFS_TYPE_POOL);
+               error = set_pool_callback(zhp, cb);
+       }
+
+       cb->cb_any_successful = !error;
        return (error);
 }
 
@@ -9718,7 +10924,9 @@ zpool_do_set(int argc, char **argv)
 {
        set_cbdata_t cb = { 0 };
        int error;
+       char *vdev = NULL;
 
+       current_prop_type = ZFS_TYPE_POOL;
        if (argc > 1 && argv[1][0] == '-') {
                (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                    argv[1][1]);
@@ -9736,12 +10944,14 @@ zpool_do_set(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       if (argc > 3) {
+       if (argc > 4) {
                (void) fprintf(stderr, gettext("too many pool names\n"));
                usage(B_FALSE);
        }
 
        cb.cb_propname = argv[1];
+       cb.cb_type = ZFS_TYPE_POOL;
+       cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
        cb.cb_value = strchr(cb.cb_propname, '=');
        if (cb.cb_value == NULL) {
                (void) fprintf(stderr, gettext("missing value in "
@@ -9751,9 +10961,41 @@ zpool_do_set(int argc, char **argv)
 
        *(cb.cb_value) = '\0';
        cb.cb_value++;
+       argc -= 2;
+       argv += 2;
+
+       /* argv[0] is pool name */
+       if (!is_pool(argv[0])) {
+               (void) fprintf(stderr,
+                   gettext("cannot open '%s': is not a pool\n"), argv[0]);
+               return (EINVAL);
+       }
 
-       error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
-           set_callback, &cb);
+       /* argv[1], when supplied, is vdev name */
+       if (argc == 2) {
+
+               if (strcmp(argv[1], "root") == 0)
+                       vdev = strdup("root-0");
+               else
+                       vdev = strdup(argv[1]);
+
+               if (!are_vdevs_in_pool(1, &vdev, argv[0], &cb.cb_vdevs)) {
+                       (void) fprintf(stderr, gettext(
+                           "cannot find '%s' in '%s': device not in pool\n"),
+                           vdev, argv[0]);
+                       free(vdev);
+                       return (EINVAL);
+               }
+               cb.cb_vdevs.cb_names = &vdev;
+               cb.cb_vdevs.cb_names_count = 1;
+               cb.cb_type = ZFS_TYPE_VDEV;
+       }
+
+       error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, set_callback, &cb);
+
+       if (vdev != NULL)
+               free(vdev);
 
        return (error);
 }
@@ -9830,12 +11072,13 @@ vdev_any_spare_replacing(nvlist_t *nv)
 {
        nvlist_t **child;
        uint_t c, children;
-       char *vdev_type;
+       const char *vdev_type;
 
        (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type);
 
        if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 ||
-           strcmp(vdev_type, VDEV_TYPE_SPARE) == 0) {
+           strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 ||
+           strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) {
                return (B_TRUE);
        }
 
@@ -9878,8 +11121,9 @@ print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
        pool_checkpoint_stat_t *pcs = NULL;
        pool_scan_stat_t *pss = NULL;
        pool_removal_stat_t *prs = NULL;
-       char *headers[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
-           "REMOVE", "RESILVER", "SCRUB", "TRIM"};
+       pool_raidz_expand_stat_t *pres = NULL;
+       const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE",
+           "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"};
        int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES];
 
        /* Calculate the width of each column */
@@ -9892,6 +11136,9 @@ print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
                col_widths[i] = MAX(strlen(headers[i]), 6) + 2;
        }
 
+       if (timestamp_fmt != NODATE)
+               print_timestamp(timestamp_fmt);
+
        /* Print header if appropriate */
        int term_height = terminal_height();
        boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 &&
@@ -9901,7 +11148,7 @@ print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
                        if (wd->wd_enabled[i])
                                (void) printf("%*s", col_widths[i], headers[i]);
                }
-               (void) printf("\n");
+               (void) fputc('\n', stdout);
        }
 
        /* Bytes of work remaining in each activity */
@@ -9938,6 +11185,13 @@ print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
                    vdev_activity_top_remaining(nvroot);
        }
 
+       (void) nvlist_lookup_uint64_array(nvroot,
+           ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c);
+       if (pres != NULL && pres->pres_state == DSS_SCANNING) {
+               int64_t rem = pres->pres_to_reflow - pres->pres_reflowed;
+               bytes_rem[ZPOOL_WAIT_RAIDZ_EXPAND] = rem;
+       }
+
        bytes_rem[ZPOOL_WAIT_INITIALIZE] =
            vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE);
        bytes_rem[ZPOOL_WAIT_TRIM] =
@@ -9959,19 +11213,17 @@ print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
        if (vdev_any_spare_replacing(nvroot))
                bytes_rem[ZPOOL_WAIT_REPLACE] =  bytes_rem[ZPOOL_WAIT_RESILVER];
 
-       if (timestamp_fmt != NODATE)
-               print_timestamp(timestamp_fmt);
-
        for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
                char buf[64];
                if (!wd->wd_enabled[i])
                        continue;
 
-               if (wd->wd_exact)
+               if (wd->wd_exact) {
                        (void) snprintf(buf, sizeof (buf), "%" PRIi64,
                            bytes_rem[i]);
-               else
+               } else {
                        zfs_nicenum(bytes_rem[i], buf, sizeof (buf));
+               }
 
                if (wd->wd_scripted)
                        (void) printf(i == 0 ? "%s" : "\t%s", buf);
@@ -10037,9 +11289,7 @@ int
 zpool_do_wait(int argc, char **argv)
 {
        boolean_t verbose = B_FALSE;
-       char c;
-       char *value;
-       int i;
+       int c, i;
        unsigned long count;
        pthread_t status_thr;
        int error = 0;
@@ -10073,28 +11323,27 @@ zpool_do_wait(int argc, char **argv)
                        get_timestamp_arg(*optarg);
                        break;
                case 't':
-               {
-                       static char *col_subopts[] = { "discard", "free",
-                           "initialize", "replace", "remove", "resilver",
-                           "scrub", "trim", NULL };
-
                        /* Reset activities array */
-                       bzero(&wd.wd_enabled, sizeof (wd.wd_enabled));
-                       while (*optarg != '\0') {
-                               int activity = getsubopt(&optarg, col_subopts,
-                                   &value);
-
-                               if (activity < 0) {
-                                       (void) fprintf(stderr,
-                                           gettext("invalid activity '%s'\n"),
-                                           value);
-                                       usage(B_FALSE);
-                               }
+                       memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled));
+
+                       for (char *tok; (tok = strsep(&optarg, ",")); ) {
+                               static const char *const col_opts[] = {
+                                   "discard", "free", "initialize", "replace",
+                                   "remove", "resilver", "scrub", "trim",
+                                   "raidz_expand" };
+
+                               for (i = 0; i < ARRAY_SIZE(col_opts); ++i)
+                                       if (strcmp(tok, col_opts[i]) == 0) {
+                                               wd.wd_enabled[i] = B_TRUE;
+                                               goto found;
+                                       }
 
-                               wd.wd_enabled[activity] = B_TRUE;
+                               (void) fprintf(stderr,
+                                   gettext("invalid activity '%s'\n"), tok);
+                               usage(B_FALSE);
+found:;
                        }
                        break;
-               }
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -10192,11 +11441,9 @@ zpool_do_wait(int argc, char **argv)
 }
 
 static int
-find_command_idx(char *command, int *idx)
+find_command_idx(const char *command, int *idx)
 {
-       int i;
-
-       for (i = 0; i < NCOMMAND; i++) {
+       for (int i = 0; i < NCOMMAND; ++i) {
                if (command_table[i].name == NULL)
                        continue;
 
@@ -10214,10 +11461,57 @@ find_command_idx(char *command, int *idx)
 static int
 zpool_do_version(int argc, char **argv)
 {
-       if (zfs_version_print() == -1)
-               return (1);
+       (void) argc, (void) argv;
+       return (zfs_version_print() != 0);
+}
 
-       return (0);
+/* Display documentation */
+static int
+zpool_do_help(int argc, char **argv)
+{
+       char page[MAXNAMELEN];
+       if (argc < 3 || strcmp(argv[2], "zpool") == 0)
+               strcpy(page, "zpool");
+       else if (strcmp(argv[2], "concepts") == 0 ||
+           strcmp(argv[2], "props") == 0)
+               snprintf(page, sizeof (page), "zpool%s", argv[2]);
+       else
+               snprintf(page, sizeof (page), "zpool-%s", argv[2]);
+
+       execlp("man", "man", page, NULL);
+
+       fprintf(stderr, "couldn't run man program: %s", strerror(errno));
+       return (-1);
+}
+
+/*
+ * Do zpool_load_compat() and print error message on failure
+ */
+static zpool_compat_status_t
+zpool_do_load_compat(const char *compat, boolean_t *list)
+{
+       char report[1024];
+
+       zpool_compat_status_t ret;
+
+       ret = zpool_load_compat(compat, list, report, 1024);
+       switch (ret) {
+
+       case ZPOOL_COMPATIBILITY_OK:
+               break;
+
+       case ZPOOL_COMPATIBILITY_NOFILES:
+       case ZPOOL_COMPATIBILITY_BADFILE:
+       case ZPOOL_COMPATIBILITY_BADTOKEN:
+               (void) fprintf(stderr, "Error: %s\n", report);
+               break;
+
+       case ZPOOL_COMPATIBILITY_WARNTOKEN:
+               (void) fprintf(stderr, "Warning: %s\n", report);
+               ret = ZPOOL_COMPATIBILITY_OK;
+               break;
+       }
+       return (ret);
 }
 
 int
@@ -10257,6 +11551,12 @@ main(int argc, char **argv)
        if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
                return (zpool_do_version(argc, argv));
 
+       /*
+        * Special case 'help'
+        */
+       if (strcmp(cmdname, "help") == 0)
+               return (zpool_do_help(argc, argv));
+
        if ((g_zfs = libzfs_init()) == NULL) {
                (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
                return (1);
@@ -10270,7 +11570,7 @@ main(int argc, char **argv)
         * Many commands modify input strings for string parsing reasons.
         * We create a copy to protect the original argv.
         */
-       newargv = malloc((argc + 1) * sizeof (newargv[0]));
+       newargv = safe_malloc((argc + 1) * sizeof (newargv[0]));
        for (i = 0; i < argc; i++)
                newargv[i] = strdup(argv[i]);
        newargv[argc] = NULL;