]> 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 d42ff18d83f8de7f73b839849521db3d69caa26b..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.
@@ -32,6 +32,7 @@
  * 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
  */
 
@@ -49,7 +50,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 #include <time.h>
 #include <unistd.h>
 #include <pwd.h>
@@ -126,9 +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.
@@ -335,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;
@@ -344,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"
@@ -386,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"));
@@ -397,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"
@@ -420,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> "
@@ -470,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;
 
@@ -489,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;
@@ -513,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(7).\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);
+               }
        }
 
        /*
@@ -552,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
@@ -573,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 &&
@@ -598,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;
@@ -624,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);
        }
 
@@ -679,7 +810,7 @@ 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, is_hole = B_FALSE;
-               char *class = "";
+               const char *class = "";
 
                (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
                    &is_hole);
@@ -772,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) {
@@ -795,9 +926,10 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
                    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);
                }
 
@@ -832,7 +964,7 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
                        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);
@@ -872,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);
@@ -884,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.
@@ -901,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;
@@ -913,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;
@@ -944,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);
@@ -964,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--;
@@ -994,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);
@@ -1099,7 +1267,7 @@ zpool_do_add(int argc, char **argv)
 
                ret = 0;
        } else {
-               ret = (zpool_add(zhp, nvroot) != 0);
+               ret = (zpool_add(zhp, nvroot, check_ashift) != 0);
        }
 
        nvlist_free(props);
@@ -1163,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);
        }
@@ -1253,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;
@@ -1286,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)) {
@@ -1301,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
@@ -1467,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':
@@ -1545,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;
@@ -1720,24 +1895,26 @@ zpool_do_create(int argc, char **argv)
                 */
                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);
 
                        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);
-                               if (strcmp(propval,
+                               } else if (strcmp(propval,
                                    ZFS_FEATURE_ENABLED) == 0 &&
-                                   !requested_features[i])
+                                   !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 &&
@@ -1756,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);
                        }
@@ -1930,7 +2107,7 @@ zpool_do_export(int argc, char **argv)
                }
 
                return (for_each_pool(argc, argv, B_TRUE, NULL,
-                   B_FALSE, zpool_export_one, &cb));
+                   ZFS_TYPE_POOL, B_FALSE, zpool_export_one, &cb));
        }
 
        /* check arguments */
@@ -1939,8 +2116,8 @@ zpool_do_export(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       ret = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, zpool_export_one,
-           &cb);
+       ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, zpool_export_one, &cb);
 
        return (ret);
 }
@@ -1953,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);
 }
@@ -2046,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) ||
@@ -2102,17 +2260,17 @@ zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
 
                        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;
        }
@@ -2131,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;
@@ -2141,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:
@@ -2182,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;
@@ -2191,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:
@@ -2225,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 ||
@@ -2243,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.
  */
@@ -2259,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)
@@ -2288,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);
@@ -2302,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 {
@@ -2317,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) {
@@ -2334,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", "-");
+                       }
                }
        }
 
@@ -2391,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:
@@ -2436,13 +2661,26 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
                    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 */
        root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
            ZPOOL_CONFIG_VDEV_TREE);
        (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) ?
@@ -2454,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)"));
                }
@@ -2515,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 ||
@@ -2643,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);
@@ -2687,17 +2926,17 @@ 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,
@@ -3000,9 +3239,8 @@ show_import(nvlist_t *config, boolean_t report_error)
                                    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 "
@@ -3069,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);
@@ -3096,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);
@@ -3118,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))
@@ -3131,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);
 
@@ -3152,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);
@@ -3171,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);
@@ -3180,17 +3435,20 @@ 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);
@@ -3234,7 +3492,7 @@ import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
                        if (first)
                                first = B_FALSE;
                        else if (!do_all)
-                               (void) printf("\n");
+                               (void) fputc('\n', stdout);
 
                        if (do_all) {
                                err |= do_import(config, NULL, mntopts,
@@ -3253,7 +3511,7 @@ import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
                                        (void) show_import(config, B_TRUE);
                        }
                } else if (import->poolname != NULL) {
-                       char *name;
+                       const char *name;
 
                        /*
                         * We are searching for a pool based on name.
@@ -3328,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);
@@ -3578,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':
@@ -3587,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;
 
@@ -3685,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);
@@ -3749,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)) {
@@ -3805,7 +4067,7 @@ zpool_do_import(int argc, char **argv)
                 */
                idata.scan = B_TRUE;
                nvlist_free(pools);
-               pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops);
+               pools = zpool_search_import(&lpch, &idata);
 
                err = import_pools(pools, props, mntopts, flags,
                    argc >= 1 ? argv[0] : NULL,
@@ -3857,24 +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, B_FALSE, 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 */
@@ -4128,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";
@@ -4186,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";
@@ -4219,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");
 }
 
@@ -4228,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.
@@ -4243,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);
 }
 
 /*
@@ -4532,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,
@@ -4566,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);
        }
 
@@ -4656,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
@@ -4696,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
@@ -4709,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;
        }
@@ -4758,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("  ");
@@ -4816,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);
@@ -4825,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);
@@ -4850,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]);
                                }
@@ -4859,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);
@@ -4883,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);
@@ -4946,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);
@@ -4988,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 {
@@ -5153,27 +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(void *zhp_data, nvlist_t *nv, void *cb_data)
 {
-       iostat_cbdata_t *cb = cb_data;
-       char *name = NULL;
-       int ret = 0;
+       uint64_t guid;
+       vdev_cbdata_t *cb = cb_data;
        zpool_handle_t *zhp = zhp_data;
 
-       name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
-
-       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)
@@ -5189,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;
@@ -5202,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,
-                   B_FALSE, 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);
 }
@@ -5239,8 +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, B_FALSE, 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. */
@@ -5263,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;
@@ -5287,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,
@@ -5297,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.
@@ -5305,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.
@@ -5322,19 +5614,6 @@ get_interval_count_filter_guids(int *argc, char **argv, float *interval,
            interval, count);
 }
 
-/*
- * 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.
@@ -5363,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;
@@ -5398,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)
@@ -5415,7 +5700,7 @@ 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, *tmp;
 
@@ -5448,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
@@ -5626,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;
@@ -5647,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 "));
@@ -5665,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);
@@ -5680,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.
                 */
@@ -5691,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, parsable, &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) {
@@ -5799,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;
                        }
@@ -5835,6 +6123,7 @@ zpool_do_iostat(int argc, char **argv)
                                print_iostat_header(&cb);
 
                        if (skip) {
+                               (void) fflush(stdout);
                                (void) fsleep(interval);
                                continue;
                        }
@@ -5852,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)
@@ -5865,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);
        }
 
@@ -5919,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 {
@@ -5937,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);
 }
 
 /*
@@ -5957,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;
 
@@ -5974,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 = "-";
@@ -5995,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
@@ -6025,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:
@@ -6086,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);
@@ -6120,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,
@@ -6149,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,
@@ -6172,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) {
@@ -6206,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);
@@ -6272,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;
@@ -6314,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) {
@@ -6365,7 +6658,7 @@ zpool_do_list(int argc, char **argv)
 
        for (;;) {
                if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
-                   cb.cb_literal, &ret)) == NULL)
+                   ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL)
                        return (1);
 
                if (pool_list_count(list) == 0)
@@ -6390,6 +6683,8 @@ zpool_do_list(int argc, char **argv)
                        break;
 
                pool_list_free(list);
+
+               (void) fflush(stdout);
                (void) fsleep(interval);
        }
 
@@ -6526,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);
@@ -6547,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)
 {
@@ -6555,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)
@@ -6583,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)
 {
@@ -6660,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;
@@ -6796,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);
@@ -6812,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)
@@ -6825,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);
@@ -6839,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;
 
@@ -6858,8 +7181,31 @@ zpool_do_online(int argc, char **argv)
                return (1);
 
        for (i = 1; i < argc; i++) {
-               if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
-                       if (newstate != VDEV_STATE_HEALTHY) {
+               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' "
                                    "onlined, but remains in faulted state\n"),
                                    argv[i]);
@@ -6871,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;
@@ -6883,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)
 {
@@ -6900,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;
@@ -6910,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);
@@ -6917,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;
 
@@ -6936,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 */
@@ -6960,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.
  */
@@ -6972,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;
@@ -6989,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);
@@ -6996,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;
 
@@ -7036,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;
 
@@ -7121,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, B_FALSE, 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);
 }
@@ -7193,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.
@@ -7210,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;
@@ -7229,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 ||
@@ -7251,13 +7686,13 @@ zpool_do_scrub(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       error = for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE,
-           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, B_FALSE,
-                   wait_callback, &act);
+               error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+                   B_FALSE, wait_callback, &act);
        }
 
        return (error);
@@ -7295,8 +7730,8 @@ zpool_do_resilver(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       return (for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE,
-           scrub_callback, &cb));
+       return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
+           B_FALSE, scrub_callback, &cb));
 }
 
 /*
@@ -7353,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;
@@ -7456,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.
  */
@@ -7463,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:"));
@@ -7486,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,
@@ -7506,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));
                }
@@ -7519,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));
@@ -7529,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));
        }
@@ -7538,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;
@@ -7550,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 "
@@ -7593,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;
@@ -7605,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));
@@ -7623,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;
@@ -7648,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 "
@@ -7690,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);
                }
        }
@@ -7781,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) {
@@ -7795,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.
@@ -7927,6 +8463,97 @@ print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
        }
 }
 
+/*
+ * 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)
 {
@@ -8092,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;
@@ -8137,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:
@@ -8504,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)
@@ -8533,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);
 
@@ -8555,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)
@@ -8599,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.
  */
@@ -8627,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) {
@@ -8654,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;
@@ -8672,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') {
@@ -8718,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, cb.cb_literal,
-                   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)
@@ -8738,6 +9369,7 @@ zpool_do_status(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
+               (void) fflush(stdout);
                (void) fsleep(interval);
        }
 
@@ -8764,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);
 
@@ -9034,7 +9666,6 @@ upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
        return (0);
 }
 
-/* ARGSUSED */
 static int
 upgrade_one(zpool_handle_t *zhp, void *data)
 {
@@ -9172,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;
@@ -9278,8 +9909,8 @@ zpool_do_upgrade(int argc, char **argv)
                        (void) printf(gettext("\n"));
                }
        } else {
-               ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE,
-                   upgrade_one, &cb);
+               ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
+                   B_FALSE, upgrade_one, &cb);
        }
 
        return (ret);
@@ -9475,8 +10106,8 @@ zpool_do_history(int argc, char **argv)
        argc -= optind;
        argv += optind;
 
-       ret = for_each_pool(argc, argv, B_FALSE, NULL, B_FALSE, 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"));
@@ -9497,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;
 
@@ -9533,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);
@@ -9731,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);
@@ -9760,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);
@@ -9799,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;
 
@@ -9866,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);
 
@@ -9874,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);
 }
 
@@ -9934,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;
 
@@ -9947,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) {
@@ -9958,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 "
@@ -9994,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 '?':
@@ -10024,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"));
@@ -10038,28 +10812,32 @@ zpool_do_get(int argc, char **argv)
                cb.cb_proplist = &fake_name;
        }
 
-       ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_literal,
-           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) {
@@ -10120,9 +10898,24 @@ set_callback(zpool_handle_t *zhp, void *data)
 
        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);
 }
 
@@ -10131,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]);
@@ -10149,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 "
@@ -10164,9 +10961,41 @@ zpool_do_set(int argc, char **argv)
 
        *(cb.cb_value) = '\0';
        cb.cb_value++;
+       argc -= 2;
+       argv += 2;
 
-       error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, B_FALSE,
-           set_callback, &cb);
+       /* 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);
+       }
+
+       /* 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);
 }
@@ -10243,7 +11072,7 @@ 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);
 
@@ -10292,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 */
@@ -10306,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 &&
@@ -10315,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 */
@@ -10352,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] =
@@ -10373,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);
@@ -10451,9 +11289,7 @@ int
 zpool_do_wait(int argc, char **argv)
 {
        boolean_t verbose = B_FALSE;
-       int c;
-       char *value;
-       int i;
+       int c, i;
        unsigned long count;
        pthread_t status_thr;
        int error = 0;
@@ -10487,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);
@@ -10606,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;
 
@@ -10628,10 +11461,27 @@ 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);
 }
 
 /*
@@ -10701,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);
@@ -10714,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;