]> git.proxmox.com Git - mirror_zfs.git/blobdiff - cmd/zpool/zpool_main.c
zpool: status -t is not documented in help message
[mirror_zfs.git] / cmd / zpool / zpool_main.c
index 9bf28c29f10cd97481d9b11697963b19c6c07176..5d3191476462344ea6a7bdb5a23b7a657bdec993 100644 (file)
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
- * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2018 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.
+ * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
+ * Copyright (c) 2017 Datto Inc.
+ * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
+ * Copyright (c) 2017, Intel Corporation.
+ * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
  */
 
 #include <assert.h>
@@ -33,6 +38,7 @@
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <libgen.h>
 #include <libintl.h>
 #include <libuutil.h>
 #include <string.h>
 #include <strings.h>
 #include <unistd.h>
-#include <priv.h>
 #include <pwd.h>
 #include <zone.h>
+#include <sys/wait.h>
 #include <zfs_prop.h>
 #include <sys/fs/zfs.h>
 #include <sys/stat.h>
+#include <sys/systeminfo.h>
+#include <sys/fm/fs/zfs.h>
 #include <sys/fm/util.h>
 #include <sys/fm/protocol.h>
 #include <sys/zfs_ioctl.h>
+#include <sys/mount.h>
+#include <sys/sysmacros.h>
+
+#include <math.h>
 
 #include <libzfs.h>
+#include <libzutil.h>
 
 #include "zpool_util.h"
 #include "zfs_comutil.h"
@@ -67,6 +80,8 @@ static int zpool_do_add(int, char **);
 static int zpool_do_remove(int, char **);
 static int zpool_do_labelclear(int, char **);
 
+static int zpool_do_checkpoint(int, char **);
+
 static int zpool_do_list(int, char **);
 static int zpool_do_iostat(int, char **);
 static int zpool_do_status(int, char **);
@@ -83,7 +98,10 @@ static int zpool_do_detach(int, char **);
 static int zpool_do_replace(int, char **);
 static int zpool_do_split(int, char **);
 
+static int zpool_do_initialize(int, char **);
 static int zpool_do_scrub(int, char **);
+static int zpool_do_resilver(int, char **);
+static int zpool_do_trim(int, char **);
 
 static int zpool_do_import(int, char **);
 static int zpool_do_export(int, char **);
@@ -96,6 +114,10 @@ static int zpool_do_events(int, char **);
 static int zpool_do_get(int, char **);
 static int zpool_do_set(int, char **);
 
+static int zpool_do_sync(int, char **);
+
+static int zpool_do_version(int, char **);
+
 /*
  * These libumem hooks provide a reasonable set of defaults for the allocator's
  * debugging facilities.
@@ -120,6 +142,7 @@ typedef enum {
        HELP_ATTACH,
        HELP_CLEAR,
        HELP_CREATE,
+       HELP_CHECKPOINT,
        HELP_DESTROY,
        HELP_DETACH,
        HELP_EXPORT,
@@ -132,18 +155,102 @@ typedef enum {
        HELP_ONLINE,
        HELP_REPLACE,
        HELP_REMOVE,
+       HELP_INITIALIZE,
        HELP_SCRUB,
+       HELP_RESILVER,
+       HELP_TRIM,
        HELP_STATUS,
        HELP_UPGRADE,
        HELP_EVENTS,
        HELP_GET,
        HELP_SET,
        HELP_SPLIT,
+       HELP_SYNC,
        HELP_REGUID,
-       HELP_REOPEN
+       HELP_REOPEN,
+       HELP_VERSION
 } zpool_help_t;
 
 
+/*
+ * Flags for stats to display with "zpool iostats"
+ */
+enum iostat_type {
+       IOS_DEFAULT = 0,
+       IOS_LATENCY = 1,
+       IOS_QUEUES = 2,
+       IOS_L_HISTO = 3,
+       IOS_RQ_HISTO = 4,
+       IOS_COUNT,      /* always last element */
+};
+
+/* iostat_type entries as bitmasks */
+#define        IOS_DEFAULT_M   (1ULL << IOS_DEFAULT)
+#define        IOS_LATENCY_M   (1ULL << IOS_LATENCY)
+#define        IOS_QUEUES_M    (1ULL << IOS_QUEUES)
+#define        IOS_L_HISTO_M   (1ULL << IOS_L_HISTO)
+#define        IOS_RQ_HISTO_M  (1ULL << IOS_RQ_HISTO)
+
+/* Mask of all the histo bits */
+#define        IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
+
+/*
+ * Lookup table for iostat flags to nvlist names.  Basically a list
+ * of all the nvlists a flag requires.  Also specifies the order in
+ * which data gets printed in zpool iostat.
+ */
+static const char *vsx_type_to_nvlist[IOS_COUNT][13] = {
+       [IOS_L_HISTO] = {
+           ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
+           NULL},
+       [IOS_LATENCY] = {
+           ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
+           ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
+           NULL},
+       [IOS_QUEUES] = {
+           ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
+           ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
+           ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
+           ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
+           ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
+           ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
+           NULL},
+       [IOS_RQ_HISTO] = {
+           ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
+           ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
+           ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
+           ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
+           ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
+           ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
+           ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
+           ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
+           ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
+           ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
+           ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
+           ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
+           NULL},
+};
+
+
+/*
+ * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
+ * Right now, only one histo bit is ever set at one time, so we can
+ * just do a highbit64(a)
+ */
+#define        IOS_HISTO_IDX(a)        (highbit64(a & IOS_ANYHISTO_M) - 1)
+
 typedef struct zpool_command {
        const char      *name;
        int             (*func)(int, char **);
@@ -160,6 +267,8 @@ typedef struct zpool_command {
  * the generic usage message.
  */
 static zpool_command_t command_table[] = {
+       { "version",    zpool_do_version,       HELP_VERSION            },
+       { NULL },
        { "create",     zpool_do_create,        HELP_CREATE             },
        { "destroy",    zpool_do_destroy,       HELP_DESTROY            },
        { NULL },
@@ -168,6 +277,8 @@ static zpool_command_t command_table[] = {
        { NULL },
        { "labelclear", zpool_do_labelclear,    HELP_LABELCLEAR         },
        { NULL },
+       { "checkpoint", zpool_do_checkpoint,    HELP_CHECKPOINT         },
+       { NULL },
        { "list",       zpool_do_list,          HELP_LIST               },
        { "iostat",     zpool_do_iostat,        HELP_IOSTAT             },
        { "status",     zpool_do_status,        HELP_STATUS             },
@@ -182,7 +293,10 @@ static zpool_command_t command_table[] = {
        { "replace",    zpool_do_replace,       HELP_REPLACE            },
        { "split",      zpool_do_split,         HELP_SPLIT              },
        { NULL },
+       { "initialize", zpool_do_initialize,    HELP_INITIALIZE         },
+       { "resilver",   zpool_do_resilver,      HELP_RESILVER           },
        { "scrub",      zpool_do_scrub,         HELP_SCRUB              },
+       { "trim",       zpool_do_trim,          HELP_TRIM               },
        { NULL },
        { "import",     zpool_do_import,        HELP_IMPORT             },
        { "export",     zpool_do_export,        HELP_EXPORT             },
@@ -194,9 +308,12 @@ static zpool_command_t command_table[] = {
        { NULL },
        { "get",        zpool_do_get,           HELP_GET                },
        { "set",        zpool_do_set,           HELP_SET                },
+       { "sync",       zpool_do_sync,          HELP_SYNC               },
 };
 
-#define        NCOMMAND        (sizeof (command_table) / sizeof (command_table[0]))
+#define        NCOMMAND        (ARRAY_SIZE(command_table))
+
+#define        VDEV_ALLOC_CLASS_LOGS   "logs"
 
 static zpool_command_t *current_command;
 static char history_str[HIS_MAX_RECORD_LEN];
@@ -204,10 +321,11 @@ static boolean_t log_history = B_TRUE;
 static uint_t timestamp_fmt = NODATE;
 
 static const char *
-get_usage(zpool_help_t idx) {
+get_usage(zpool_help_t idx)
+{
        switch (idx) {
        case HELP_ADD:
-               return (gettext("\tadd [-fn] [-o property=value] "
+               return (gettext("\tadd [-fgLnP] [-o property=value] "
                    "<pool> <vdev> ...\n"));
        case HELP_ATTACH:
                return (gettext("\tattach [-f] [-o property=value] "
@@ -218,6 +336,8 @@ get_usage(zpool_help_t idx) {
                return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
                    "\t    [-O file-system-property=value] ... \n"
                    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
+       case HELP_CHECKPOINT:
+               return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
        case HELP_DESTROY:
                return (gettext("\tdestroy [-f] <pool>\n"));
        case HELP_DETACH:
@@ -228,61 +348,101 @@ get_usage(zpool_help_t idx) {
                return (gettext("\thistory [-il] [<pool>] ...\n"));
        case HELP_IMPORT:
                return (gettext("\timport [-d dir] [-D]\n"
-                   "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
                    "\timport [-o mntopts] [-o property=value] ... \n"
-                   "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
+                   "\t    [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
                    "[-R root] [-F [-n]] -a\n"
                    "\timport [-o mntopts] [-o property=value] ... \n"
-                   "\t    [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
+                   "\t    [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
                    "[-R root] [-F [-n]]\n"
-                   "\t    <pool | id> [newpool]\n"));
+                   "\t    [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
        case HELP_IOSTAT:
-               return (gettext("\tiostat [-v] [-T d|u] [-y] [pool] ... "
-                   "[interval [count]]\n"));
+               return (gettext("\tiostat [[[-c [script1,script2,...]"
+                   "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
+                   "\t    [[pool ...]|[pool vdev ...]|[vdev ...]]"
+                   " [[-n] interval [count]]\n"));
        case HELP_LABELCLEAR:
                return (gettext("\tlabelclear [-f] <vdev>\n"));
        case HELP_LIST:
-               return (gettext("\tlist [-Hv] [-o property[,...]] "
-                   "[-T d|u] [pool] ... [interval [count]]\n"));
+               return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
+                   "[-T d|u] [pool] ... \n"
+                   "\t    [interval [count]]\n"));
        case HELP_OFFLINE:
-               return (gettext("\toffline [-t] <pool> <device> ...\n"));
+               return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
        case HELP_ONLINE:
-               return (gettext("\tonline <pool> <device> ...\n"));
+               return (gettext("\tonline [-e] <pool> <device> ...\n"));
        case HELP_REPLACE:
                return (gettext("\treplace [-f] [-o property=value] "
                    "<pool> <device> [new-device]\n"));
        case HELP_REMOVE:
-               return (gettext("\tremove <pool> <device> ...\n"));
+               return (gettext("\tremove [-nps] <pool> <device> ...\n"));
        case HELP_REOPEN:
-               return (gettext("\treopen <pool>\n"));
+               return (gettext("\treopen [-n] <pool>\n"));
+       case HELP_INITIALIZE:
+               return (gettext("\tinitialize [-c | -s] <pool> "
+                   "[<device> ...]\n"));
        case HELP_SCRUB:
-               return (gettext("\tscrub [-s] <pool> ...\n"));
+               return (gettext("\tscrub [-s | -p] <pool> ...\n"));
+       case HELP_RESILVER:
+               return (gettext("\tresilver <pool> ...\n"));
+       case HELP_TRIM:
+               return (gettext("\ttrim [-d] [-r <rate>] [-c | -s] <pool> "
+                   "[<device> ...]\n"));
        case HELP_STATUS:
-               return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
-                   "[count]]\n"));
+               return (gettext("\tstatus [-c [script1,script2,...]] "
+                   "[-igLpPstvxD]  [-T d|u] [pool] ... \n"
+                   "\t    [interval [count]]\n"));
        case HELP_UPGRADE:
                return (gettext("\tupgrade\n"
                    "\tupgrade -v\n"
                    "\tupgrade [-V version] <-a | pool ...>\n"));
        case HELP_EVENTS:
-               return (gettext("\tevents [-vHfc]\n"));
+               return (gettext("\tevents [-vHf [pool] | -c]\n"));
        case HELP_GET:
-               return (gettext("\tget [-pH] <\"all\" | property[,...]> "
-                   "<pool> ...\n"));
+               return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
+                   "<\"all\" | property[,...]> <pool> ...\n"));
        case HELP_SET:
                return (gettext("\tset <property=value> <pool> \n"));
        case HELP_SPLIT:
-               return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
+               return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
                    "\t    [-o property=value] <pool> <newpool> "
                    "[<device> ...]\n"));
        case HELP_REGUID:
                return (gettext("\treguid <pool>\n"));
+       case HELP_SYNC:
+               return (gettext("\tsync [pool] ...\n"));
+       case HELP_VERSION:
+               return (gettext("\tversion\n"));
        }
 
        abort();
        /* NOTREACHED */
 }
 
+static void
+zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
+{
+       uint_t children = 0;
+       nvlist_t **child;
+       uint_t i;
+
+       (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
+           &child, &children);
+
+       if (children == 0) {
+               char *path = zpool_vdev_name(g_zfs, zhp, nvroot,
+                   VDEV_NAME_PATH);
+
+               if (strcmp(path, VDEV_TYPE_INDIRECT) != 0)
+                       fnvlist_add_boolean(res, path);
+
+               free(path);
+               return;
+       }
+
+       for (i = 0; i < children; i++) {
+               zpool_collect_leaves(zhp, child[i], res);
+       }
+}
 
 /*
  * Callback routine that will print out a pool property value.
@@ -292,7 +452,7 @@ print_prop_cb(int prop, void *cb)
 {
        FILE *fp = cb;
 
-       (void) fprintf(fp, "\t%-15s  ", zpool_prop_to_name(prop));
+       (void) fprintf(fp, "\t%-19s  ", zpool_prop_to_name(prop));
 
        if (zpool_prop_readonly(prop))
                (void) fprintf(fp, "  NO   ");
@@ -344,14 +504,14 @@ usage(boolean_t requested)
                (void) fprintf(fp,
                    gettext("\nthe following properties are supported:\n"));
 
-               (void) fprintf(fp, "\n\t%-15s  %s   %s\n\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);
 
-               (void) fprintf(fp, "\t%-15s   ", "feature@...");
+               (void) fprintf(fp, "\t%-19s   ", "feature@...");
                (void) fprintf(fp, "YES   disabled | enabled | active\n");
 
                (void) fprintf(fp, gettext("\nThe feature@ properties must be "
@@ -369,32 +529,137 @@ usage(boolean_t requested)
        exit(requested ? 0 : 2);
 }
 
-void
+/*
+ * zpool initialize [-c | -s] <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.
+ */
+int
+zpool_do_initialize(int argc, char **argv)
+{
+       int c;
+       char *poolname;
+       zpool_handle_t *zhp;
+       nvlist_t *vdevs;
+       int err = 0;
+
+       struct option long_options[] = {
+               {"cancel",      no_argument,            NULL, 'c'},
+               {"suspend",     no_argument,            NULL, 's'},
+               {0, 0, 0, 0}
+       };
+
+       pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
+       while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) {
+               switch (c) {
+               case 'c':
+                       if (cmd_type != POOL_INITIALIZE_START &&
+                           cmd_type != POOL_INITIALIZE_CANCEL) {
+                               (void) fprintf(stderr, gettext("-c cannot be "
+                                   "combined with other options\n"));
+                               usage(B_FALSE);
+                       }
+                       cmd_type = POOL_INITIALIZE_CANCEL;
+                       break;
+               case 's':
+                       if (cmd_type != POOL_INITIALIZE_START &&
+                           cmd_type != POOL_INITIALIZE_SUSPEND) {
+                               (void) fprintf(stderr, gettext("-s cannot be "
+                                   "combined with other options\n"));
+                               usage(B_FALSE);
+                       }
+                       cmd_type = POOL_INITIALIZE_SUSPEND;
+                       break;
+               case '?':
+                       if (optopt != 0) {
+                               (void) fprintf(stderr,
+                                   gettext("invalid option '%c'\n"), optopt);
+                       } else {
+                               (void) fprintf(stderr,
+                                   gettext("invalid option '%s'\n"),
+                                   argv[optind - 1]);
+                       }
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing pool name argument\n"));
+               usage(B_FALSE);
+               return (-1);
+       }
+
+       poolname = argv[0];
+       zhp = zpool_open(g_zfs, poolname);
+       if (zhp == NULL)
+               return (-1);
+
+       vdevs = fnvlist_alloc();
+       if (argc == 1) {
+               /* no individual leaf vdevs specified, so add them all */
+               nvlist_t *config = zpool_get_config(zhp, NULL);
+               nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
+                   ZPOOL_CONFIG_VDEV_TREE);
+               zpool_collect_leaves(zhp, nvroot, vdevs);
+       } else {
+               for (int i = 1; i < argc; i++) {
+                       fnvlist_add_boolean(vdevs, argv[i]);
+               }
+       }
+
+       err = zpool_initialize(zhp, cmd_type, vdevs);
+
+       fnvlist_free(vdevs);
+       zpool_close(zhp);
+
+       return (err);
+}
+
+/*
+ * print a pool vdev config for dry runs
+ */
+static void
 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
-    boolean_t print_logs)
+    const char *match, int name_flags)
 {
        nvlist_t **child;
        uint_t c, children;
        char *vname;
-
-       if (name != NULL)
-               (void) printf("\t%*s%s\n", indent, "", name);
+       boolean_t printed = B_FALSE;
 
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
-           &child, &children) != 0)
+           &child, &children) != 0) {
+               if (name != NULL)
+                       (void) printf("\t%*s%s\n", indent, "", name);
                return;
+       }
 
        for (c = 0; c < children; c++) {
                uint64_t is_log = B_FALSE;
+               char *class = "";
 
                (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
                    &is_log);
-               if ((is_log && !print_logs) || (!is_log && print_logs))
+               if (is_log)
+                       class = VDEV_ALLOC_BIAS_LOG;
+               (void) nvlist_lookup_string(child[c],
+                   ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
+               if (strcmp(match, class) != 0)
                        continue;
 
-               vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
-               print_vdev_tree(zhp, vname, child[c], indent + 2,
-                   B_FALSE);
+               if (!printed && name != NULL) {
+                       (void) printf("\t%*s%s\n", indent, "", name);
+                       printed = B_TRUE;
+               }
+               vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
+               print_vdev_tree(zhp, vname, child[c], indent + 2, "",
+                   name_flags);
                free(vname);
        }
 }
@@ -418,8 +683,7 @@ static int
 add_prop_list(const char *propname, char *propval, nvlist_t **props,
     boolean_t poolprop)
 {
-       zpool_prop_t prop = ZPROP_INVAL;
-       zfs_prop_t fprop;
+       zpool_prop_t prop = ZPOOL_PROP_INVAL;
        nvlist_t *proplist;
        const char *normnm;
        char *strval;
@@ -436,7 +700,7 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
        if (poolprop) {
                const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
 
-               if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
+               if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
                    !zpool_prop_feature(propname)) {
                        (void) fprintf(stderr, gettext("property '%s' is "
                            "not a valid pool property\n"), propname);
@@ -447,7 +711,7 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
                 * feature@ properties and version should not be specified
                 * at the same time.
                 */
-               if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
+               if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
                    nvlist_exists(proplist, vname)) ||
                    (prop == ZPOOL_PROP_VERSION &&
                    prop_list_contains_feature(proplist))) {
@@ -463,10 +727,18 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
                else
                        normnm = zpool_prop_to_name(prop);
        } else {
-               if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
-                       normnm = zfs_prop_to_name(fprop);
-               } else {
+               zfs_prop_t fsprop = zfs_name_to_prop(propname);
+
+               if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
+                   B_FALSE)) {
+                       normnm = zfs_prop_to_name(fsprop);
+               } else if (zfs_prop_user(propname) ||
+                   zfs_prop_userquota(propname)) {
                        normnm = propname;
+               } else {
+                       (void) fprintf(stderr, gettext("property '%s' is "
+                           "not a valid filesystem property\n"), propname);
+                       return (2);
                }
        }
 
@@ -502,12 +774,15 @@ add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
 }
 
 /*
- * zpool add [-fn] [-o property=value] <pool> <vdev> ...
+ * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
  *
  *     -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.
  *     -n      Do not add the devices, but display the resulting layout if
  *             they were to be added.
  *     -o      Set property=value.
+ *     -P      Display full path for vdev name.
  *
  * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
  * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
@@ -518,6 +793,7 @@ zpool_do_add(int argc, char **argv)
 {
        boolean_t force = B_FALSE;
        boolean_t dryrun = B_FALSE;
+       int name_flags = 0;
        int c;
        nvlist_t *nvroot;
        char *poolname;
@@ -528,11 +804,17 @@ zpool_do_add(int argc, char **argv)
        char *propval;
 
        /* check options */
-       while ((c = getopt(argc, argv, "fno:")) != -1) {
+       while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
                switch (c) {
                case 'f':
                        force = B_TRUE;
                        break;
+               case 'g':
+                       name_flags |= VDEV_NAME_GUID;
+                       break;
+               case 'L':
+                       name_flags |= VDEV_NAME_FOLLOW_LINKS;
+                       break;
                case 'n':
                        dryrun = B_TRUE;
                        break;
@@ -549,6 +831,9 @@ zpool_do_add(int argc, char **argv)
                            (add_prop_list(optarg, propval, &props, B_TRUE)))
                                usage(B_FALSE);
                        break;
+               case 'P':
+                       name_flags |= VDEV_NAME_PATH;
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -584,6 +869,20 @@ zpool_do_add(int argc, char **argv)
                return (1);
        }
 
+       /* unless manually specified use "ashift" pool property (if set) */
+       if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
+               int intval;
+               zprop_source_t src;
+               char strval[ZPOOL_MAXPROPLEN];
+
+               intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
+               if (src != ZPROP_SRC_DEFAULT) {
+                       (void) sprintf(strval, "%" PRId32, intval);
+                       verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
+                           &props, B_TRUE) == 0);
+               }
+       }
+
        /* pass off to get_vdev_spec for processing */
        nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
            argc, argv);
@@ -606,16 +905,25 @@ zpool_do_add(int argc, char **argv)
                    "configuration:\n"), zpool_get_name(zhp));
 
                /* print original main pool and new tree */
-               print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
-               print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
-
-               /* Do the same for the logs */
-               if (num_logs(poolnvroot) > 0) {
-                       print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
-                       print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
-               } else if (num_logs(nvroot) > 0) {
-                       print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
-               }
+               print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
+                   name_flags | VDEV_NAME_TYPE_ID);
+               print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
+
+               /* print other classes: 'dedup', 'special', and 'log' */
+               print_vdev_tree(zhp, "dedup", poolnvroot, 0,
+                   VDEV_ALLOC_BIAS_DEDUP, name_flags);
+               print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_DEDUP,
+                   name_flags);
+
+               print_vdev_tree(zhp, "special", poolnvroot, 0,
+                   VDEV_ALLOC_BIAS_SPECIAL, name_flags);
+               print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_SPECIAL,
+                   name_flags);
+
+               print_vdev_tree(zhp, "logs", poolnvroot, 0, VDEV_ALLOC_BIAS_LOG,
+                   name_flags);
+               print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_LOG,
+                   name_flags);
 
                /* Do the same for the caches */
                if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
@@ -624,7 +932,7 @@ zpool_do_add(int argc, char **argv)
                        (void) printf(gettext("\tcache\n"));
                        for (c = 0; c < l2children; c++) {
                                vname = zpool_vdev_name(g_zfs, NULL,
-                                   l2child[c], B_FALSE);
+                                   l2child[c], name_flags);
                                (void) printf("\t  %s\n", vname);
                                free(vname);
                        }
@@ -635,7 +943,7 @@ zpool_do_add(int argc, char **argv)
                                (void) printf(gettext("\tcache\n"));
                        for (c = 0; c < l2children; c++) {
                                vname = zpool_vdev_name(g_zfs, NULL,
-                                   l2child[c], B_FALSE);
+                                   l2child[c], name_flags);
                                (void) printf("\t  %s\n", vname);
                                free(vname);
                        }
@@ -656,44 +964,106 @@ zpool_do_add(int argc, char **argv)
 /*
  * zpool remove  <pool> <vdev> ...
  *
- * Removes the given vdev from the pool.  Currently, this supports removing
- * spares, cache, and log devices from the pool.
+ * Removes the given vdev from the pool.
  */
 int
 zpool_do_remove(int argc, char **argv)
 {
        char *poolname;
        int i, ret = 0;
-       zpool_handle_t *zhp;
+       zpool_handle_t *zhp = NULL;
+       boolean_t stop = B_FALSE;
+       char c;
+       boolean_t noop = B_FALSE;
+       boolean_t parsable = B_FALSE;
 
-       argc--;
-       argv++;
+       /* check options */
+       while ((c = getopt(argc, argv, "nps")) != -1) {
+               switch (c) {
+               case 'n':
+                       noop = B_TRUE;
+                       break;
+               case 'p':
+                       parsable = B_TRUE;
+                       break;
+               case 's':
+                       stop = B_TRUE;
+                       break;
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
 
        /* get pool name and check number of arguments */
        if (argc < 1) {
                (void) fprintf(stderr, gettext("missing pool name argument\n"));
                usage(B_FALSE);
        }
-       if (argc < 2) {
-               (void) fprintf(stderr, gettext("missing device\n"));
-               usage(B_FALSE);
-       }
 
        poolname = argv[0];
 
        if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
                return (1);
 
-       for (i = 1; i < argc; i++) {
-               if (zpool_vdev_remove(zhp, argv[i]) != 0)
+       if (stop && noop) {
+               (void) fprintf(stderr, gettext("stop request ignored\n"));
+               return (0);
+       }
+
+       if (stop) {
+               if (argc > 1) {
+                       (void) fprintf(stderr, gettext("too many arguments\n"));
+                       usage(B_FALSE);
+               }
+               if (zpool_vdev_remove_cancel(zhp) != 0)
                        ret = 1;
+       } else {
+               if (argc < 2) {
+                       (void) fprintf(stderr, gettext("missing device\n"));
+                       usage(B_FALSE);
+               }
+
+               for (i = 1; i < argc; i++) {
+                       if (noop) {
+                               uint64_t size;
+
+                               if (zpool_vdev_indirect_size(zhp, argv[i],
+                                   &size) != 0) {
+                                       ret = 1;
+                                       break;
+                               }
+                               if (parsable) {
+                                       (void) printf("%s %llu\n",
+                                           argv[i], (unsigned long long)size);
+                               } else {
+                                       char valstr[32];
+                                       zfs_nicenum(size, valstr,
+                                           sizeof (valstr));
+                                       (void) printf("Memory that will be "
+                                           "used after removing %s: %s\n",
+                                           argv[i], valstr);
+                               }
+                       } else {
+                               if (zpool_vdev_remove(zhp, argv[i]) != 0)
+                                       ret = 1;
+                       }
+               }
        }
+       zpool_close(zhp);
 
        return (ret);
 }
 
 /*
- * zpool labelclear <vdev>
+ * zpool labelclear [-f] <vdev>
+ *
+ *     -f      Force clearing the label for the vdevs which are members of
+ *             the exported or foreign pools.
  *
  * Verifies that the vdev is not active and zeros out the label information
  * on the device.
@@ -701,8 +1071,11 @@ zpool_do_remove(int argc, char **argv)
 int
 zpool_do_labelclear(int argc, char **argv)
 {
-       char *vdev, *name;
+       char vdev[MAXPATHLEN];
+       char *name = NULL;
+       struct stat st;
        int c, fd = -1, ret = 0;
+       nvlist_t *config;
        pool_state_t state;
        boolean_t inuse = B_FALSE;
        boolean_t force = B_FALSE;
@@ -725,90 +1098,118 @@ zpool_do_labelclear(int argc, char **argv)
 
        /* get vdev name */
        if (argc < 1) {
-               (void) fprintf(stderr, gettext("missing vdev device name\n"));
+               (void) fprintf(stderr, gettext("missing vdev name\n"));
                usage(B_FALSE);
        }
-
-       vdev = argv[0];
-       if ((fd = open(vdev, O_RDWR)) < 0) {
-               (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
-               return (B_FALSE);
+       if (argc > 1) {
+               (void) fprintf(stderr, gettext("too many arguments\n"));
+               usage(B_FALSE);
        }
 
-       name = NULL;
-       if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
-               if (force)
-                       goto wipe_label;
+       /*
+        * Check if we were given absolute path and use it as is.
+        * 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) {
+               int error;
+
+               error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
+               if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
+                       if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
+                               error = ENOENT;
+               }
 
-               (void) fprintf(stderr,
-                   gettext("Unable to determine pool state for %s\n"
-                   "Use -f to force the clearing any label data\n"), vdev);
+               if (error || (stat(vdev, &st) != 0)) {
+                       (void) fprintf(stderr, gettext(
+                           "failed to find device %s, try specifying absolute "
+                           "path instead\n"), argv[0]);
+                       return (1);
+               }
+       }
 
+       if ((fd = open(vdev, O_RDWR)) < 0) {
+               (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
+                   vdev, strerror(errno));
                return (1);
        }
 
-       if (inuse) {
-               switch (state) {
-               default:
-               case POOL_STATE_ACTIVE:
-               case POOL_STATE_SPARE:
-               case POOL_STATE_L2CACHE:
-                       (void) fprintf(stderr,
-                           gettext("labelclear operation failed.\n"
-                           "\tVdev %s is a member (%s), of pool \"%s\".\n"
-                           "\tTo remove label information from this device, "
-                           "export or destroy\n\tthe pool, or remove %s from "
-                           "the configuration of this pool\n\tand retry the "
-                           "labelclear operation.\n"),
-                           vdev, zpool_pool_state_to_name(state), name, vdev);
-                       ret = 1;
-                       goto errout;
+       /*
+        * Flush all dirty pages for the block device.  This should not be
+        * fatal when the device does not support BLKFLSBUF as would be the
+        * case for a file vdev.
+        */
+       if ((ioctl(fd, BLKFLSBUF) != 0) && (errno != ENOTTY))
+               (void) fprintf(stderr, gettext("failed to invalidate "
+                   "cache for %s: %s\n"), vdev, strerror(errno));
 
-               case POOL_STATE_EXPORTED:
-                       if (force)
-                               break;
+       if (zpool_read_label(fd, &config, NULL) != 0) {
+               (void) fprintf(stderr,
+                   gettext("failed to read label from %s\n"), vdev);
+               ret = 1;
+               goto errout;
+       }
+       nvlist_free(config);
 
-                       (void) fprintf(stderr,
-                           gettext("labelclear operation failed.\n\tVdev "
-                           "%s is a member of the exported pool \"%s\".\n"
-                           "\tUse \"zpool labelclear -f %s\" to force the "
-                           "removal of label\n\tinformation.\n"),
-                           vdev, name, vdev);
-                       ret = 1;
-                       goto errout;
+       ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
+       if (ret != 0) {
+               (void) fprintf(stderr,
+                   gettext("failed to check state for %s\n"), vdev);
+               ret = 1;
+               goto errout;
+       }
 
-               case POOL_STATE_POTENTIALLY_ACTIVE:
-                       if (force)
-                               break;
+       if (!inuse)
+               goto wipe_label;
 
-                       (void) fprintf(stderr,
-                           gettext("labelclear operation failed.\n"
-                           "\tVdev %s is a member of the pool \"%s\".\n"
-                           "\tThis pool is unknown to this system, but may "
-                           "be active on\n\tanother system. Use "
-                           "\'zpool labelclear -f %s\' to force the\n"
-                           "\tremoval of label information.\n"),
-                           vdev, name, vdev);
-                       ret = 1;
-                       goto errout;
+       switch (state) {
+       default:
+       case POOL_STATE_ACTIVE:
+       case POOL_STATE_SPARE:
+       case POOL_STATE_L2CACHE:
+               (void) fprintf(stderr, gettext(
+                   "%s is a member (%s) of pool \"%s\"\n"),
+                   vdev, zpool_pool_state_to_name(state), name);
+               ret = 1;
+               goto errout;
 
-               case POOL_STATE_DESTROYED:
-                       /* inuse should never be set for a destroyed pool... */
+       case POOL_STATE_EXPORTED:
+               if (force)
                        break;
-               }
+               (void) fprintf(stderr, gettext(
+                   "use '-f' to override the following error:\n"
+                   "%s is a member of exported pool \"%s\"\n"),
+                   vdev, name);
+               ret = 1;
+               goto errout;
+
+       case POOL_STATE_POTENTIALLY_ACTIVE:
+               if (force)
+                       break;
+               (void) fprintf(stderr, gettext(
+                   "use '-f' to override the following error:\n"
+                   "%s is a member of potentially active pool \"%s\"\n"),
+                   vdev, name);
+               ret = 1;
+               goto errout;
+
+       case POOL_STATE_DESTROYED:
+               /* inuse should never be set for a destroyed pool */
+               assert(0);
+               break;
        }
 
 wipe_label:
-       if (zpool_clear_label(fd) != 0) {
+       ret = zpool_clear_label(fd);
+       if (ret != 0) {
                (void) fprintf(stderr,
-                   gettext("Label clear failed on vdev %s\n"), vdev);
-               ret = 1;
+                   gettext("failed to clear label for %s\n"), vdev);
        }
 
 errout:
-       close(fd);
-       if (name != NULL)
-               free(name);
+       free(name);
+       (void) close(fd);
 
        return (ret);
 }
@@ -825,6 +1226,7 @@ errout:
  *      -m     Set default mountpoint for the root dataset.  By default it's
  *             '/<pool>'
  *     -o      Set property=value.
+ *     -o      Set feature@feature=enabled|disabled.
  *     -d      Don't automatically enable all supported pool features
  *             (individual features can be enabled with -o).
  *     -O      Set fsproperty=value in the pool's root file system
@@ -902,6 +1304,8 @@ zpool_do_create(int argc, char **argv)
                                        enable_all_pool_feat = B_FALSE;
                                }
                        }
+                       if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
+                               altroot = propval;
                        break;
                case 'O':
                        if ((propval = strchr(optarg, '=')) == NULL) {
@@ -1082,31 +1486,39 @@ zpool_do_create(int argc, char **argv)
                (void) printf(gettext("would create '%s' with the "
                    "following layout:\n\n"), poolname);
 
-               print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
-               if (num_logs(nvroot) > 0)
-                       print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
+               print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
+               print_vdev_tree(NULL, "dedup", nvroot, 0,
+                   VDEV_ALLOC_BIAS_DEDUP, 0);
+               print_vdev_tree(NULL, "special", nvroot, 0,
+                   VDEV_ALLOC_BIAS_SPECIAL, 0);
+               print_vdev_tree(NULL, "logs", nvroot, 0,
+                   VDEV_ALLOC_BIAS_LOG, 0);
 
                ret = 0;
        } else {
                /*
                 * Hand off to libzfs.
                 */
-               if (enable_all_pool_feat) {
-                       spa_feature_t i;
-                       for (i = 0; i < SPA_FEATURES; i++) {
-                               char propname[MAXPATHLEN];
-                               zfeature_info_t *feat = &spa_feature_table[i];
-
-                               (void) snprintf(propname, sizeof (propname),
-                                   "feature@%s", feat->fi_uname);
+               spa_feature_t i;
+               for (i = 0; i < SPA_FEATURES; i++) {
+                       char propname[MAXPATHLEN];
+                       char *propval;
+                       zfeature_info_t *feat = &spa_feature_table[i];
 
-                               /*
-                                * Skip feature if user specified it manually
-                                * on the command line.
-                                */
-                               if (nvlist_exists(props, propname))
-                                       continue;
+                       (void) snprintf(propname, sizeof (propname),
+                           "feature@%s", feat->fi_uname);
 
+                       /*
+                        * Only features contained in props will be enabled:
+                        * remove from the nvlist every ZFS_FEATURE_DISABLED
+                        * value and add every missing ZFS_FEATURE_ENABLED if
+                        * enable_all_pool_feat is set.
+                        */
+                       if (!nvlist_lookup_string(props, propname, &propval)) {
+                               if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
+                                       (void) nvlist_remove_all(props,
+                                           propname);
+                       } else if (enable_all_pool_feat) {
                                ret = add_prop_list(propname,
                                    ZFS_FEATURE_ENABLED, &props, B_TRUE);
                                if (ret != 0)
@@ -1200,6 +1612,7 @@ zpool_do_destroy(int argc, char **argv)
        if (zpool_disable_datasets(zhp, force) != 0) {
                (void) fprintf(stderr, gettext("could not destroy '%s': "
                    "could not unmount datasets\n"), zpool_get_name(zhp));
+               zpool_close(zhp);
                return (1);
        }
 
@@ -1311,13 +1724,15 @@ zpool_do_export(int argc, char **argv)
  * name column.
  */
 static int
-max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
+max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
+    int name_flags)
 {
-       char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
+       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;
 
@@ -1327,7 +1742,7 @@ max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
            &child, &children) == 0) {
                for (c = 0; c < children; c++)
                        if ((ret = max_width(zhp, child[c], depth + 2,
-                           max)) > max)
+                           max, name_flags)) > max)
                                max = ret;
        }
 
@@ -1335,7 +1750,7 @@ max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
            &child, &children) == 0) {
                for (c = 0; c < children; c++)
                        if ((ret = max_width(zhp, child[c], depth + 2,
-                           max)) > max)
+                           max, name_flags)) > max)
                                max = ret;
        }
 
@@ -1343,11 +1758,10 @@ max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
            &child, &children) == 0) {
                for (c = 0; c < children; c++)
                        if ((ret = max_width(zhp, child[c], depth + 2,
-                           max)) > max)
+                           max, name_flags)) > max)
                                max = ret;
        }
 
-
        return (max);
 }
 
@@ -1396,31 +1810,223 @@ find_spare(zpool_handle_t *zhp, void *data)
        return (0);
 }
 
-/*
- * Print out configuration state as requested by status_callback.
- */
-void
-print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
-    int namewidth, int depth, boolean_t isspare)
+typedef struct status_cbdata {
+       int             cb_count;
+       int             cb_name_flags;
+       int             cb_namewidth;
+       boolean_t       cb_allpools;
+       boolean_t       cb_verbose;
+       boolean_t       cb_literal;
+       boolean_t       cb_explain;
+       boolean_t       cb_first;
+       boolean_t       cb_dedup_stats;
+       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;
+} status_cbdata_t;
+
+/* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
+static int
+is_blank_str(char *str)
 {
-       nvlist_t **child;
-       uint_t c, children;
-       pool_scan_stat_t *ps = NULL;
-       vdev_stat_t *vs;
-       char rbuf[6], wbuf[6], cbuf[6];
-       char *vname;
-       uint64_t notpresent;
-       spare_cbdata_t cb;
-       char *state;
+       while (str != NULL && *str != '\0') {
+               if (!isblank(*str))
+                       return (0);
+               str++;
+       }
+       return (1);
+}
 
-       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
-           &child, &children) != 0)
-               children = 0;
+/* 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)
+{
+       vdev_cmd_data_t *data;
+       int i, j;
+       char *val;
+
+       for (i = 0; i < vcdl->count; i++) {
+               if ((strcmp(vcdl->data[i].path, path) != 0) ||
+                   (strcmp(vcdl->data[i].pool, pool) != 0)) {
+                       /* Not the vdev we're looking for */
+                       continue;
+               }
 
-       verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
-           (uint64_t **)&vs, &c) == 0);
+               data = &vcdl->data[i];
+               /* Print out all the output values for this vdev */
+               for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
+                       val = NULL;
+                       /* Does this vdev have values for this column? */
+                       for (int k = 0; k < data->cols_cnt; k++) {
+                               if (strcmp(data->cols[k],
+                                   vcdl->uniq_cols[j]) == 0) {
+                                       /* yes it does, record the value */
+                                       val = data->lines[k];
+                                       break;
+                               }
+                       }
+                       /*
+                        * Mark empty values with dashes to make output
+                        * awk-able.
+                        */
+                       if (is_blank_str(val))
+                               val = "-";
 
-       state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
+                       printf("%*s", vcdl->uniq_cols_width[j], val);
+                       if (j < vcdl->uniq_cols_cnt - 1)
+                               printf("  ");
+               }
+
+               /* 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("  ");
+
+                       val = data->lines[j];
+                       printf("%s", val ? val : "");
+               }
+               break;
+       }
+}
+
+/*
+ * Print vdev initialization status for leaves
+ */
+static void
+print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
+{
+       if (verbose) {
+               if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
+                   vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
+                   vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
+                   !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;
+                       if (vs->vs_initialize_state !=
+                           VDEV_INITIALIZE_COMPLETE) {
+                               initialize_pct = (vs->vs_initialize_bytes_done *
+                                   100 / (vs->vs_initialize_bytes_est + 1));
+                       }
+
+                       (void) localtime_r(&t, &zaction_ts);
+                       (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
+
+                       switch (vs->vs_initialize_state) {
+                       case VDEV_INITIALIZE_SUSPENDED:
+                               (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
+                                   gettext("suspended, started at"), tbuf);
+                               break;
+                       case VDEV_INITIALIZE_ACTIVE:
+                               (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
+                                   gettext("started at"), tbuf);
+                               break;
+                       case VDEV_INITIALIZE_COMPLETE:
+                               (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
+                                   gettext("completed at"), tbuf);
+                               break;
+                       }
+
+                       (void) printf(gettext("  (%d%% initialized%s)"),
+                           initialize_pct, zbuf);
+               } else {
+                       (void) printf(gettext("  (uninitialized)"));
+               }
+       } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) {
+               (void) printf(gettext("  (initializing)"));
+       }
+}
+
+/*
+ * Print vdev TRIM status for leaves
+ */
+static void
+print_status_trim(vdev_stat_t *vs, boolean_t verbose)
+{
+       if (verbose) {
+               if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE ||
+                   vs->vs_trim_state == VDEV_TRIM_SUSPENDED ||
+                   vs->vs_trim_state == VDEV_TRIM_COMPLETE) &&
+                   !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;
+                       if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) {
+                               trim_pct = (vs->vs_trim_bytes_done *
+                                   100 / (vs->vs_trim_bytes_est + 1));
+                       }
+
+                       (void) localtime_r(&t, &zaction_ts);
+                       (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
+
+                       switch (vs->vs_trim_state) {
+                       case VDEV_TRIM_SUSPENDED:
+                               (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
+                                   gettext("suspended, started at"), tbuf);
+                               break;
+                       case VDEV_TRIM_ACTIVE:
+                               (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
+                                   gettext("started at"), tbuf);
+                               break;
+                       case VDEV_TRIM_COMPLETE:
+                               (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
+                                   gettext("completed at"), tbuf);
+                               break;
+                       }
+
+                       (void) printf(gettext("  (%d%% trimmed%s)"),
+                           trim_pct, zbuf);
+               } else if (vs->vs_trim_notsup) {
+                       (void) printf(gettext("  (trim unsupported)"));
+               } else {
+                       (void) printf(gettext("  (untrimmed)"));
+               }
+       } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) {
+               (void) printf(gettext("  (trimming)"));
+       }
+}
+
+/*
+ * Print out configuration state as requested by status_callback.
+ */
+static void
+print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
+    nvlist_t *nv, int depth, boolean_t isspare)
+{
+       nvlist_t **child, *root;
+       uint_t c, children;
+       pool_scan_stat_t *ps = NULL;
+       vdev_stat_t *vs;
+       char rbuf[6], wbuf[6], cbuf[6];
+       char *vname;
+       uint64_t notpresent;
+       spare_cbdata_t spare_cb;
+       const char *state;
+       char *type;
+       char *path = NULL;
+
+       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
+           &child, &children) != 0)
+               children = 0;
+
+       verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
+           (uint64_t **)&vs, &c) == 0);
+
+       verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
+
+       if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
+               return;
+
+       state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
        if (isspare) {
                /*
                 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
@@ -1432,19 +2038,42 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                        state = "AVAIL";
        }
 
-       (void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
+       (void) printf("\t%*s%-*s  %-8s", depth, "", cb->cb_namewidth - depth,
            name, state);
 
        if (!isspare) {
-               zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
-               zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
-               zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
-               (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
+               if (cb->cb_literal) {
+                       printf(" %5llu %5llu %5llu",
+                           (u_longlong_t)vs->vs_read_errors,
+                           (u_longlong_t)vs->vs_write_errors,
+                           (u_longlong_t)vs->vs_checksum_errors);
+               } else {
+                       zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
+                       zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
+                       zfs_nicenum(vs->vs_checksum_errors, cbuf,
+                           sizeof (cbuf));
+                       printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
+               }
+
+               if (cb->cb_print_slow_ios) {
+                       if (children == 0)  {
+                               /* Only leafs vdevs have slow IOs */
+                               zfs_nicenum(vs->vs_slow_ios, rbuf,
+                                   sizeof (rbuf));
+                       } else {
+                               snprintf(rbuf, sizeof (rbuf), "-");
+                       }
+
+                       if (cb->cb_literal)
+                               printf(" %5llu", (u_longlong_t)vs->vs_slow_ios);
+                       else
+                               printf(" %5s", rbuf);
+               }
+
        }
 
        if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
            &notpresent) == 0) {
-               char *path;
                verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
                (void) printf("  was %s", path);
        } else if (vs->vs_aux != 0) {
@@ -1473,17 +2102,17 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
 
                case VDEV_AUX_SPARED:
                        verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
-                           &cb.cb_guid) == 0);
-                       if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
-                               if (strcmp(zpool_get_name(cb.cb_zhp),
+                           &spare_cb.cb_guid) == 0);
+                       if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
+                               if (strcmp(zpool_get_name(spare_cb.cb_zhp),
                                    zpool_get_name(zhp)) == 0)
                                        (void) printf(gettext("currently in "
                                            "use"));
                                else
                                        (void) printf(gettext("in use by "
                                            "pool '%s'"),
-                                           zpool_get_name(cb.cb_zhp));
-                               zpool_close(cb.cb_zhp);
+                                           zpool_get_name(spare_cb.cb_zhp));
+                               zpool_close(spare_cb.cb_zhp);
                        } else {
                                (void) printf(gettext("currently in use"));
                        }
@@ -1509,20 +2138,47 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                        (void) printf(gettext("split into new pool"));
                        break;
 
+               case VDEV_AUX_ACTIVE:
+                       (void) printf(gettext("currently in use"));
+                       break;
+
+               case VDEV_AUX_CHILDREN_OFFLINE:
+                       (void) printf(gettext("all children offline"));
+                       break;
+
                default:
                        (void) printf(gettext("corrupted data"));
                        break;
                }
        }
 
-       (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
+       /* 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 && ps->pss_state == DSS_SCANNING &&
-           vs->vs_scan_processed != 0 && children == 0) {
-               (void) printf(gettext("  (%s)"),
-                   (ps->pss_func == POOL_SCAN_RESILVER) ?
-                   "resilvering" : "repairing");
+       if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) {
+               if (vs->vs_scan_processed != 0) {
+                       (void) printf(gettext("  (%s)"),
+                           (ps->pss_func == POOL_SCAN_RESILVER) ?
+                           "resilvering" : "repairing");
+               } else if (vs->vs_resilver_deferred) {
+                       (void) printf(gettext("  (awaiting resilver)"));
+               }
+       }
+
+       if (cb->vcdl != NULL) {
+               if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
+                       printf("  ");
+                       zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
+               }
+       }
+
+       /* Display vdev initialization and trim status for leaves */
+       if (children == 0) {
+               print_status_initialize(vs, cb->cb_print_vdev_init);
+               print_status_trim(vs, cb->cb_print_vdev_trim);
        }
 
        (void) printf("\n");
@@ -1537,20 +2193,26 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                    &ishole);
                if (islog || ishole)
                        continue;
-               vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
-               print_status_config(zhp, vname, child[c],
-                   namewidth, depth + 2, isspare);
+               /* Only print normal classes here */
+               if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
+                       continue;
+
+               vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                   cb->cb_name_flags | VDEV_NAME_TYPE_ID);
+
+               print_status_config(zhp, cb, vname, child[c], depth + 2,
+                   isspare);
                free(vname);
        }
 }
 
-
 /*
  * Print the configuration of an exported pool.  Iterate over all vdevs in the
  * pool, printing out the name and status for each one.
  */
-void
-print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
+static void
+print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
+    int depth)
 {
        nvlist_t **child;
        uint_t c, children;
@@ -1565,7 +2227,7 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
        verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t **)&vs, &c) == 0);
 
-       (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
+       (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
        (void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
 
        if (vs->vs_aux != 0) {
@@ -1596,6 +2258,14 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
                        (void) printf(gettext("too many errors"));
                        break;
 
+               case VDEV_AUX_ACTIVE:
+                       (void) printf(gettext("currently in use"));
+                       break;
+
+               case VDEV_AUX_CHILDREN_OFFLINE:
+                       (void) printf(gettext("all children offline"));
+                       break;
+
                default:
                        (void) printf(gettext("corrupted data"));
                        break;
@@ -1614,9 +2284,12 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
                    &is_log);
                if (is_log)
                        continue;
+               if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
+                       continue;
 
-               vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
-               print_import_config(vname, child[c], namewidth, depth + 2);
+               vname = zpool_vdev_name(g_zfs, NULL, child[c],
+                   cb->cb_name_flags | VDEV_NAME_TYPE_ID);
+               print_import_config(cb, vname, child[c], depth + 2);
                free(vname);
        }
 
@@ -1624,7 +2297,8 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
            &child, &children) == 0) {
                (void) printf(gettext("\tcache\n"));
                for (c = 0; c < children; c++) {
-                       vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
+                       vname = zpool_vdev_name(g_zfs, NULL, child[c],
+                           cb->cb_name_flags);
                        (void) printf("\t  %s\n", vname);
                        free(vname);
                }
@@ -1634,7 +2308,8 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
            &child, &children) == 0) {
                (void) printf(gettext("\tspares\n"));
                for (c = 0; c < children; c++) {
-                       vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
+                       vname = zpool_vdev_name(g_zfs, NULL, child[c],
+                           cb->cb_name_flags);
                        (void) printf("\t  %s\n", vname);
                        free(vname);
                }
@@ -1642,39 +2317,62 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
 }
 
 /*
- * Print log vdevs.
- * Logs are recorded as top level vdevs in the main pool child array
- * but with "is_log" set to 1. We use either print_status_config() or
- * print_import_config() to print the top level logs then any log
- * children (eg mirrored slogs) are printed recursively - which
- * works because only the top level vdev is marked "is_log"
+ * Print specialized class vdevs.
+ *
+ * These are recorded as top level vdevs in the main pool child array
+ * but with "is_log" set to 1 or an "alloc_bias" string. We use either
+ * print_status_config() or print_import_config() to print the top level
+ * class vdevs then any of their children (eg mirrored slogs) are printed
+ * recursively - which works because only the top level vdev is marked.
  */
 static void
-print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
+print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
+    const char *class)
 {
        uint_t c, children;
        nvlist_t **child;
+       boolean_t printed = B_FALSE;
+
+       assert(zhp != NULL || !cb->cb_verbose);
 
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
            &children) != 0)
                return;
 
-       (void) printf(gettext("\tlogs\n"));
-
        for (c = 0; c < children; c++) {
                uint64_t is_log = B_FALSE;
-               char *name;
+               char *bias = NULL;
+               char *type = NULL;
 
                (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
                    &is_log);
-               if (!is_log)
+
+               if (is_log) {
+                       bias = VDEV_ALLOC_CLASS_LOGS;
+               } else {
+                       (void) nvlist_lookup_string(child[c],
+                           ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
+                       (void) nvlist_lookup_string(child[c],
+                           ZPOOL_CONFIG_TYPE, &type);
+               }
+
+               if (bias == NULL || strcmp(bias, class) != 0)
                        continue;
-               name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
-               if (verbose)
-                       print_status_config(zhp, name, child[c], namewidth,
-                           2, B_FALSE);
+               if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
+                       continue;
+
+               if (!printed) {
+                       (void) printf("\t%s\t\n", gettext(class));
+                       printed = B_TRUE;
+               }
+
+               char *name = zpool_vdev_name(g_zfs, zhp, child[c],
+                   cb->cb_name_flags | VDEV_NAME_TYPE_ID);
+               if (cb->cb_print_status)
+                       print_status_config(zhp, cb, name, child[c], 2,
+                           B_FALSE);
                else
-                       print_import_config(name, child[c], namewidth, 2);
+                       print_import_config(cb, name, child[c], 2);
                free(name);
        }
 }
@@ -1689,14 +2387,16 @@ show_import(nvlist_t *config)
        vdev_stat_t *vs;
        char *name;
        uint64_t guid;
+       uint64_t hostid = 0;
        char *msgid;
-       nvlist_t *nvroot;
+       char *hostname = "unknown";
+       nvlist_t *nvroot, *nvinfo;
        zpool_status_t reason;
        zpool_errata_t errata;
        const char *health;
        uint_t vsc;
-       int namewidth;
        char *comment;
+       status_cbdata_t cb = { 0 };
 
        verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
            &name) == 0);
@@ -1766,7 +2466,7 @@ show_import(nvlist_t *config)
 
        case ZPOOL_STATUS_UNSUP_FEAT_READ:
                (void) printf(gettext("status: The pool uses the following "
-                   "feature(s) not supported on this sytem:\n"));
+                   "feature(s) not supported on this system:\n"));
                zpool_print_unsup_feat(config);
                break;
 
@@ -1778,6 +2478,17 @@ show_import(nvlist_t *config)
                zpool_print_unsup_feat(config);
                break;
 
+       case ZPOOL_STATUS_HOSTID_ACTIVE:
+               (void) printf(gettext(" status: The pool is currently "
+                   "imported by another system.\n"));
+               break;
+
+       case ZPOOL_STATUS_HOSTID_REQUIRED:
+               (void) printf(gettext(" status: The pool has the "
+                   "multihost property on.  It cannot\n\tbe safely imported "
+                   "when the system hostid is not set.\n"));
+               break;
+
        case ZPOOL_STATUS_HOSTID_MISMATCH:
                (void) printf(gettext(" status: The pool was last accessed by "
                    "another system.\n"));
@@ -1847,6 +2558,30 @@ show_import(nvlist_t *config)
                                    "updating.\n"));
                                break;
 
+                       case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
+                               (void) printf(gettext(" action: Existing "
+                                   "encrypted datasets contain an on-disk "
+                                   "incompatibility, which\n\tneeds to be "
+                                   "corrected. Backup these datasets to new "
+                                   "encrypted datasets\n\tand destroy the "
+                                   "old ones.\n"));
+                               break;
+
+                       case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
+                               (void) printf(gettext(" action: Existing "
+                                   "encrypted snapshots and bookmarks contain "
+                                   "an on-disk\n\tincompatibility. This may "
+                                   "cause on-disk corruption if they are used"
+                                   "\n\twith 'zfs recv'. To correct the "
+                                   "issue, enable the bookmark_v2 feature.\n\t"
+                                   "No additional action is needed if there "
+                                   "are no encrypted snapshots or\n\t"
+                                   "bookmarks. If preserving the encrypted "
+                                   "snapshots and bookmarks is\n\trequired, "
+                                   "use a non-raw send to backup and restore "
+                                   "them. Alternately,\n\tthey may be removed"
+                                   " to resolve the incompatibility.\n"));
+                               break;
                        default:
                                /*
                                 * All errata must contain an action message.
@@ -1891,6 +2626,27 @@ show_import(nvlist_t *config)
                            "imported. Attach the missing\n\tdevices and try "
                            "again.\n"));
                        break;
+               case ZPOOL_STATUS_HOSTID_ACTIVE:
+                       VERIFY0(nvlist_lookup_nvlist(config,
+                           ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
+
+                       if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
+                               hostname = fnvlist_lookup_string(nvinfo,
+                                   ZPOOL_CONFIG_MMP_HOSTNAME);
+
+                       if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
+                               hostid = fnvlist_lookup_uint64(nvinfo,
+                                   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);
+                       break;
+               case ZPOOL_STATUS_HOSTID_REQUIRED:
+                       (void) printf(gettext(" action: Set a unique system "
+                           "hostid with the zgenhostid(8) command.\n"));
+                       break;
                default:
                        (void) printf(gettext(" action: The pool cannot be "
                            "imported due to damaged devices or data.\n"));
@@ -1923,13 +2679,16 @@ show_import(nvlist_t *config)
 
        (void) printf(gettext(" config:\n\n"));
 
-       namewidth = max_width(NULL, nvroot, 0, 0);
-       if (namewidth < 10)
-               namewidth = 10;
+       cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
+           VDEV_NAME_TYPE_ID);
+       if (cb.cb_namewidth < 10)
+               cb.cb_namewidth = 10;
+
+       print_import_config(&cb, name, nvroot, 0);
 
-       print_import_config(name, nvroot, namewidth, 0);
-       if (num_logs(nvroot) > 0)
-               print_logs(NULL, nvroot, namewidth, B_FALSE);
+       print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
+       print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
+       print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
 
        if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
                (void) printf(gettext("\n\tAdditional devices are known to "
@@ -1938,6 +2697,31 @@ show_import(nvlist_t *config)
        }
 }
 
+static boolean_t
+zfs_force_import_required(nvlist_t *config)
+{
+       uint64_t state;
+       uint64_t hostid = 0;
+       nvlist_t *nvinfo;
+
+       state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
+       (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);
+
+               if (mmp_state != MMP_STATE_INACTIVE)
+                       return (B_TRUE);
+       }
+
+       return (B_FALSE);
+}
+
 /*
  * Perform the import for the given configuration.  This passes the heavy
  * lifting off to zpool_import_props(), and then mounts the datasets contained
@@ -1947,50 +2731,76 @@ static int
 do_import(nvlist_t *config, const char *newname, const char *mntopts,
     nvlist_t *props, int flags)
 {
+       int ret = 0;
        zpool_handle_t *zhp;
        char *name;
-       uint64_t state;
        uint64_t version;
 
-       verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
-           &name) == 0);
+       name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
+       version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
 
-       verify(nvlist_lookup_uint64(config,
-           ZPOOL_CONFIG_POOL_STATE, &state) == 0);
-       verify(nvlist_lookup_uint64(config,
-           ZPOOL_CONFIG_VERSION, &version) == 0);
        if (!SPA_VERSION_IS_SUPPORTED(version)) {
                (void) fprintf(stderr, gettext("cannot import '%s': pool "
                    "is formatted using an unsupported ZFS version\n"), name);
                return (1);
-       } else if (state != POOL_STATE_EXPORTED &&
+       } else if (zfs_force_import_required(config) &&
            !(flags & ZFS_IMPORT_ANY_HOST)) {
-               uint64_t hostid = 0;
-               unsigned long system_hostid = get_system_hostid();
+               mmp_state_t mmp_state = MMP_STATE_INACTIVE;
+               nvlist_t *nvinfo;
 
-               (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
-                   &hostid);
+               nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
+               if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
+                       mmp_state = fnvlist_lookup_uint64(nvinfo,
+                           ZPOOL_CONFIG_MMP_STATE);
 
-               if (hostid != 0 && (unsigned long)hostid != system_hostid) {
-                       char *hostname;
-                       uint64_t timestamp;
-                       time_t t;
+               if (mmp_state == MMP_STATE_ACTIVE) {
+                       char *hostname = "<unknown>";
+                       uint64_t hostid = 0;
 
-                       verify(nvlist_lookup_string(config,
-                           ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
-                       verify(nvlist_lookup_uint64(config,
-                           ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
-                       t = timestamp;
-                       (void) fprintf(stderr, gettext("cannot import "
-                           "'%s': pool may be in use from other "
-                           "system, it was last accessed by %s "
-                           "(hostid: 0x%lx) on %s"), name, hostname,
-                           (unsigned long)hostid,
-                           asctime(localtime(&t)));
-                       (void) fprintf(stderr, gettext("use '-f' to "
-                           "import anyway\n"));
-                       return (1);
+                       if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
+                               hostname = fnvlist_lookup_string(nvinfo,
+                                   ZPOOL_CONFIG_MMP_HOSTNAME);
+
+                       if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
+                               hostid = fnvlist_lookup_uint64(nvinfo,
+                                   ZPOOL_CONFIG_MMP_HOSTID);
+
+                       (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);
+               } 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;
+                       uint64_t hostid = 0;
+
+                       if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
+                               hostname = fnvlist_lookup_string(config,
+                                   ZPOOL_CONFIG_HOSTNAME);
+
+                       if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
+                               timestamp = fnvlist_lookup_uint64(config,
+                                   ZPOOL_CONFIG_TIMESTAMP);
+
+                       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"
+                           "The pool can be imported, use 'zpool import -f' "
+                           "to import the pool.\n"), name, hostname,
+                           (unsigned long)hostid, ctime((time_t *)&timestamp));
                }
+
+               return (1);
        }
 
        if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
@@ -2002,6 +2812,16 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
        if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
                return (1);
 
+       /*
+        * 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)
+                       ret = 1;
+       }
+
        if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
            !(flags & ZFS_IMPORT_ONLY) &&
            zpool_enable_datasets(zhp, mntopts, 0) != 0) {
@@ -2010,14 +2830,121 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
        }
 
        zpool_close(zhp);
-       return (0);
+       return (ret);
+}
+
+typedef struct target_exists_args {
+       const char      *poolname;
+       uint64_t        poolguid;
+} target_exists_args_t;
+
+static int
+name_or_guid_exists(zpool_handle_t *zhp, void *data)
+{
+       target_exists_args_t *args = data;
+       nvlist_t *config = zpool_get_config(zhp, NULL);
+       int found = 0;
+
+       if (config == NULL)
+               return (0);
+
+       if (args->poolname != NULL) {
+               char *pool_name;
+
+               verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
+                   &pool_name) == 0);
+               if (strcmp(pool_name, args->poolname) == 0)
+                       found = 1;
+       } else {
+               uint64_t pool_guid;
+
+               verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
+                   &pool_guid) == 0);
+               if (pool_guid == args->poolguid)
+                       found = 1;
+       }
+       zpool_close(zhp);
+
+       return (found);
+}
+/*
+ * zpool checkpoint <pool>
+ *       checkpoint --discard <pool>
+ *
+ *       -d         Discard the checkpoint from a checkpointed
+ *       --discard  pool.
+ *
+ * Checkpoints the specified pool, by taking a "snapshot" of its
+ * current state. A pool can only have one checkpoint at a time.
+ */
+int
+zpool_do_checkpoint(int argc, char **argv)
+{
+       boolean_t discard;
+       char *pool;
+       zpool_handle_t *zhp;
+       int c, err;
+
+       struct option long_options[] = {
+               {"discard", no_argument, NULL, 'd'},
+               {0, 0, 0, 0}
+       };
+
+       discard = B_FALSE;
+       while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
+               switch (c) {
+               case 'd':
+                       discard = B_TRUE;
+                       break;
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing pool argument\n"));
+               usage(B_FALSE);
+       }
+
+       if (argc > 1) {
+               (void) fprintf(stderr, gettext("too many arguments\n"));
+               usage(B_FALSE);
+       }
+
+       pool = argv[0];
+
+       if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
+               /* As a special case, check for use of '/' in the name */
+               if (strchr(pool, '/') != NULL)
+                       (void) fprintf(stderr, gettext("'zpool checkpoint' "
+                           "doesn't work on datasets. To save the state "
+                           "of a dataset from a specific point in time "
+                           "please use 'zfs snapshot'\n"));
+               return (1);
+       }
+
+       if (discard)
+               err = (zpool_discard_checkpoint(zhp) != 0);
+       else
+               err = (zpool_checkpoint(zhp) != 0);
+
+       zpool_close(zhp);
+
+       return (err);
 }
 
+#define        CHECKPOINT_OPT  1024
+
 /*
  * zpool import [-d dir] [-D]
- *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
+ *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
  *              [-d dir | -c cachefile] [-f] -a
- *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
+ *       import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
  *              [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
  *
  *      -c     Read pool information from a cachefile instead of searching
@@ -2052,8 +2979,16 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
  *
  *       -a    Import all pools found.
  *
+ *       -l    Load encryption keys while importing.
+ *
  *       -o    Set property=value and/or temporary mount options (without '=').
  *
+ *      -s     Scan using the default search path, the libblkid cache will
+ *             not be consulted.
+ *
+ *       --rewind-to-checkpoint
+ *             Import the pool and revert back to the checkpoint.
+ *
  * The import command scans for pools to import, and import pools based on pool
  * name and GUID.  The pool can also be renamed as part of the import process.
  */
@@ -2083,13 +3018,21 @@ zpool_do_import(int argc, char **argv)
        boolean_t dryrun = B_FALSE;
        boolean_t do_rewind = B_FALSE;
        boolean_t xtreme_rewind = B_FALSE;
+       boolean_t do_scan = B_FALSE;
+       boolean_t pool_exists = B_FALSE;
        uint64_t pool_state, txg = -1ULL;
        char *cachefile = NULL;
        importargs_t idata = { 0 };
        char *endptr;
 
+       struct option long_options[] = {
+               {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
+               {0, 0, 0, 0}
+       };
+
        /* check options */
-       while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
+       while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
+           long_options, NULL)) != -1) {
                switch (c) {
                case 'a':
                        do_all = B_TRUE;
@@ -2119,6 +3062,9 @@ zpool_do_import(int argc, char **argv)
                case 'F':
                        do_rewind = B_TRUE;
                        break;
+               case 'l':
+                       flags |= ZFS_IMPORT_LOAD_KEYS;
+                       break;
                case 'm':
                        flags |= ZFS_IMPORT_MISSING_LOG;
                        break;
@@ -2147,6 +3093,9 @@ zpool_do_import(int argc, char **argv)
                            ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
                                goto error;
                        break;
+               case 's':
+                       do_scan = B_TRUE;
+                       break;
                case 't':
                        flags |= ZFS_IMPORT_TEMP_NAME;
                        if (add_prop_list_default(zpool_prop_to_name(
@@ -2170,6 +3119,9 @@ zpool_do_import(int argc, char **argv)
                case 'X':
                        xtreme_rewind = B_TRUE;
                        break;
+               case CHECKPOINT_OPT:
+                       flags |= ZFS_IMPORT_CHECKPOINT;
+                       break;
                case ':':
                        (void) fprintf(stderr, gettext("missing argument for "
                            "'%c' option\n"), optopt);
@@ -2190,6 +3142,17 @@ zpool_do_import(int argc, char **argv)
                usage(B_FALSE);
        }
 
+       if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
+               (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
+               usage(B_FALSE);
+       }
+
+       if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
+               (void) fprintf(stderr, gettext("-l is only meaningful during "
+                   "an import\n"));
+               usage(B_FALSE);
+       }
+
        if ((dryrun || xtreme_rewind) && !do_rewind) {
                (void) fprintf(stderr,
                    gettext("-n or -X only meaningful with -F\n"));
@@ -2204,8 +3167,9 @@ zpool_do_import(int argc, char **argv)
 
        /* In the future, we can capture further policy and include it here */
        if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
-           nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
-           nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
+           nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
+           nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
+           rewind_policy) != 0)
                goto error;
 
        /* check argument count */
@@ -2219,21 +3183,21 @@ zpool_do_import(int argc, char **argv)
                        (void) fprintf(stderr, gettext("too many arguments\n"));
                        usage(B_FALSE);
                }
+       }
 
-               /*
-                * Check for the SYS_CONFIG privilege.  We do this explicitly
-                * here because otherwise any attempt to discover pools will
-                * silently fail.
-                */
-               if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
-                       (void) fprintf(stderr, gettext("cannot "
-                           "discover pools: permission denied\n"));
-                       if (searchdirs != NULL)
-                               free(searchdirs);
+       /*
+        * Check for the effective uid.  We do this explicitly here because
+        * otherwise any attempt to discover pools will silently fail.
+        */
+       if (argc == 0 && geteuid() != 0) {
+               (void) fprintf(stderr, gettext("cannot "
+                   "discover pools: permission denied\n"));
+               if (searchdirs != NULL)
+                       free(searchdirs);
 
-                       nvlist_free(policy);
-                       return (1);
-               }
+               nvlist_free(props);
+               nvlist_free(policy);
+               return (1);
        }
 
        /*
@@ -2263,7 +3227,8 @@ zpool_do_import(int argc, char **argv)
                /*
                 * User specified a name or guid.  Ensure it's unique.
                 */
-               idata.unique = B_TRUE;
+               target_exists_args_t search = {searchname, searchguid};
+               pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search);
        }
 
        /*
@@ -2296,10 +3261,12 @@ zpool_do_import(int argc, char **argv)
        idata.poolname = searchname;
        idata.guid = searchguid;
        idata.cachefile = cachefile;
+       idata.scan = do_scan;
+       idata.policy = policy;
 
-       pools = zpool_search_import(g_zfs, &idata);
+       pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops);
 
-       if (pools != NULL && idata.exists &&
+       if (pools != NULL && pool_exists &&
            (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
                (void) fprintf(stderr, gettext("cannot import '%s': "
                    "a pool with that name already exists\n"),
@@ -2308,7 +3275,7 @@ zpool_do_import(int argc, char **argv)
                    "<pool | id> <newpool>' to give it a new name\n"),
                    "zpool import");
                err = 1;
-       } else if (pools == NULL && idata.exists) {
+       } else if (pools == NULL && pool_exists) {
                (void) fprintf(stderr, gettext("cannot import '%s': "
                    "a pool with that name is already created/imported,\n"),
                    argv[0]);
@@ -2329,6 +3296,8 @@ zpool_do_import(int argc, char **argv)
                if (envdup != NULL)
                        free(envdup);
                nvlist_free(policy);
+               nvlist_free(pools);
+               nvlist_free(props);
                return (1);
        }
 
@@ -2352,7 +3321,7 @@ zpool_do_import(int argc, char **argv)
                if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
                        continue;
 
-               verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
+               verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
                    policy) == 0);
 
                if (argc == 0) {
@@ -2436,61 +3405,862 @@ error:
        return (err ? 1 : 0);
 }
 
-typedef struct iostat_cbdata {
-       boolean_t cb_verbose;
-       int cb_namewidth;
-       int cb_iteration;
-       zpool_list_t *cb_list;
-} iostat_cbdata_t;
+/*
+ * zpool sync [-f] [pool] ...
+ *
+ * -f (undocumented) force uberblock (and config including zpool cache file)
+ *    update.
+ *
+ * Sync the specified pool(s).
+ * Without arguments "zpool sync" will sync all pools.
+ * This command initiates TXG sync(s) and will return after the TXG(s) commit.
+ *
+ */
+static int
+zpool_do_sync(int argc, char **argv)
+{
+       int ret;
+       boolean_t force = B_FALSE;
+
+       /* check options */
+       while ((ret  = getopt(argc, argv, "f")) != -1) {
+               switch (ret) {
+               case 'f':
+                       force = B_TRUE;
+                       break;
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       /* if argc == 0 we will execute zpool_sync_one on all pools */
+       ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
+
+       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;
+} iostat_cbdata_t;
+
+/*  iostat labels */
+typedef struct name_and_columns {
+       const char *name;       /* Column name */
+       unsigned int columns;   /* Center name to this number of columns */
+} name_and_columns_t;
+
+#define        IOSTAT_MAX_LABELS       13      /* Max number of labels on one line */
+
+static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
+{
+       [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
+           {NULL}},
+       [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
+           {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL}},
+       [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
+           {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
+           {"trimq_write", 2}, {NULL}},
+       [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
+           {"asyncq_wait", 2}, {NULL}},
+       [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
+           {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
+           {"trim", 2}, {NULL}},
+};
+
+/* Shorthand - if "columns" field not set, default to 1 column */
+static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
+{
+       [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
+           {"write"}, {NULL}},
+       [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
+           {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL}},
+       [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
+           {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
+           {"pend"}, {"activ"}, {NULL}},
+       [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
+           {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL}},
+       [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
+           {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}},
+};
+
+static const char *histo_to_title[] = {
+       [IOS_L_HISTO] = "latency",
+       [IOS_RQ_HISTO] = "req_size",
+};
+
+/*
+ * Return the number of labels in a null-terminated name_and_columns_t
+ * array.
+ *
+ */
+static unsigned int
+label_array_len(const name_and_columns_t *labels)
+{
+       int i = 0;
+
+       while (labels[i].name)
+               i++;
+
+       return (i);
+}
+
+/*
+ * Return the number of strings in a null-terminated string array.
+ * For example:
+ *
+ *     const char foo[] = {"bar", "baz", NULL}
+ *
+ * returns 2
+ */
+static uint64_t
+str_array_len(const char *array[])
+{
+       uint64_t i = 0;
+       while (array[i])
+               i++;
+
+       return (i);
+}
+
+
+/*
+ * Return a default column width for default/latency/queue columns. This does
+ * not include histograms, which have their columns autosized.
+ */
+static unsigned int
+default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
+{
+       unsigned long column_width = 5; /* Normal niceprint */
+       static unsigned long widths[] = {
+               /*
+                * Choose some sane default column sizes for printing the
+                * raw numbers.
+                */
+               [IOS_DEFAULT] = 15, /* 1PB capacity */
+               [IOS_LATENCY] = 10, /* 1B ns = 10sec */
+               [IOS_QUEUES] = 6,   /* 1M queue entries */
+               [IOS_L_HISTO] = 10, /* 1B ns = 10sec */
+               [IOS_RQ_HISTO] = 6, /* 1M queue entries */
+       };
+
+       if (cb->cb_literal)
+               column_width = widths[type];
+
+       return (column_width);
+}
+
+/*
+ * Print the column labels, i.e:
+ *
+ *   capacity     operations     bandwidth
+ * alloc   free   read  write   read  write  ...
+ *
+ * If force_column_width is set, use it for the column width.  If not set, use
+ * the default column width.
+ */
+void
+print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
+    const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
+{
+       int i, idx, s;
+       int text_start, rw_column_width, spaces_to_end;
+       uint64_t flags = cb->cb_flags;
+       uint64_t f;
+       unsigned int column_width = force_column_width;
+
+       /* For each bit set in flags */
+       for (f = flags; f; f &= ~(1ULL << idx)) {
+               idx = lowbit64(f) - 1;
+               if (!force_column_width)
+                       column_width = default_column_width(cb, idx);
+               /* Print our top labels centered over "read  write" label. */
+               for (i = 0; i < label_array_len(labels[idx]); i++) {
+                       const char *name = labels[idx][i].name;
+                       /*
+                        * We treat labels[][].columns == 0 as shorthand
+                        * for one column.  It makes writing out the label
+                        * tables more concise.
+                        */
+                       unsigned int columns = MAX(1, labels[idx][i].columns);
+                       unsigned int slen = strlen(name);
+
+                       rw_column_width = (column_width * columns) +
+                           (2 * (columns - 1));
+
+                       text_start = (int)((rw_column_width) / columns -
+                           slen / columns);
+                       if (text_start < 0)
+                               text_start = 0;
+
+                       printf("  ");   /* Two spaces between columns */
+
+                       /* Space from beginning of column to label */
+                       for (s = 0; s < text_start; s++)
+                               printf(" ");
+
+                       printf("%s", name);
+
+                       /* Print space after label to end of column */
+                       spaces_to_end = rw_column_width - text_start - slen;
+                       if (spaces_to_end < 0)
+                               spaces_to_end = 0;
+
+                       for (s = 0; s < spaces_to_end; s++)
+                               printf(" ");
+               }
+       }
+}
+
+
+/*
+ * print_cmd_columns - Print custom column titles from -c
+ *
+ * If the user specified the "zpool status|iostat -c" then print their custom
+ * column titles in the header.  For example, print_cmd_columns() would print
+ * the "  col1  col2" part of this:
+ *
+ * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
+ * ...
+ *           capacity     operations     bandwidth
+ * pool        alloc   free   read  write   read  write  col1  col2
+ * ----------  -----  -----  -----  -----  -----  -----  ----  ----
+ * mypool       269K  1008M      0      0    107    946
+ *   mirror     269K  1008M      0      0    107    946
+ *     sdb         -      -      0      0    102    473  val1  val2
+ *     sdc         -      -      0      0      5    473  val1  val2
+ * ----------  -----  -----  -----  -----  -----  -----  ----  ----
+ */
+void
+print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
+{
+       int i, j;
+       vdev_cmd_data_t *data = &vcdl->data[0];
+
+       if (vcdl->count == 0 || data == NULL)
+               return;
+
+       /*
+        * Each vdev cmd should have the same column names unless the user did
+        * something weird with their cmd.  Just take the column names from the
+        * first vdev and assume it works for all of them.
+        */
+       for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
+               printf("  ");
+               if (use_dashes) {
+                       for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
+                               printf("-");
+               } else {
+                       printf("%*s", vcdl->uniq_cols_width[i],
+                           vcdl->uniq_cols[i]);
+               }
+       }
+}
+
+
+/*
+ * Utility function to print out a line of dashes like:
+ *
+ *     --------------------------------  -----  -----  -----  -----  -----
+ *
+ * ...or a dashed named-row line like:
+ *
+ *     logs                                  -      -      -      -      -
+ *
+ * @cb:                                iostat data
+ *
+ * @force_column_width         If non-zero, use the value as the column width.
+ *                             Otherwise use the default column widths.
+ *
+ * @name:                      Print a dashed named-row line starting
+ *                             with @name.  Otherwise, print a regular
+ *                             dashed line.
+ */
+static void
+print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
+    const char *name)
+{
+       int i;
+       unsigned int namewidth;
+       uint64_t flags = cb->cb_flags;
+       uint64_t f;
+       int idx;
+       const name_and_columns_t *labels;
+       const char *title;
+
+
+       if (cb->cb_flags & IOS_ANYHISTO_M) {
+               title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
+       } else if (cb->cb_vdev_names_count) {
+               title = "vdev";
+       } else  {
+               title = "pool";
+       }
+
+       namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
+           name ? strlen(name) : 0);
+
+
+       if (name) {
+               printf("%-*s", namewidth, name);
+       } else {
+               for (i = 0; i < namewidth; i++)
+                       (void) printf("-");
+       }
+
+       /* For each bit in flags */
+       for (f = flags; f; f &= ~(1ULL << idx)) {
+               unsigned int column_width;
+               idx = lowbit64(f) - 1;
+               if (force_column_width)
+                       column_width = force_column_width;
+               else
+                       column_width = default_column_width(cb, idx);
+
+               labels = iostat_bottom_labels[idx];
+               for (i = 0; i < label_array_len(labels); i++) {
+                       if (name)
+                               printf("  %*s-", column_width - 1, " ");
+                       else
+                               printf("  %.*s", column_width,
+                                   "--------------------");
+               }
+       }
+}
+
+
+static void
+print_iostat_separator_impl(iostat_cbdata_t *cb,
+    unsigned int force_column_width)
+{
+       print_iostat_dashes(cb, force_column_width, NULL);
+}
 
 static void
 print_iostat_separator(iostat_cbdata_t *cb)
 {
-       int i = 0;
+       print_iostat_separator_impl(cb, 0);
+}
+
+static void
+print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
+    const char *histo_vdev_name)
+{
+       unsigned int namewidth;
+       const char *title;
+
+       if (cb->cb_flags & IOS_ANYHISTO_M) {
+               title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
+       } else if (cb->cb_vdev_names_count) {
+               title = "vdev";
+       } else  {
+               title = "pool";
+       }
+
+       namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
+           histo_vdev_name ? strlen(histo_vdev_name) : 0);
 
-       for (i = 0; i < cb->cb_namewidth; i++)
-               (void) printf("-");
-       (void) printf("  -----  -----  -----  -----  -----  -----\n");
+       if (histo_vdev_name)
+               printf("%-*s", namewidth, histo_vdev_name);
+       else
+               printf("%*s", namewidth, "");
+
+
+       print_iostat_labels(cb, force_column_width, iostat_top_labels);
+       printf("\n");
+
+       printf("%-*s", namewidth, title);
+
+       print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
+       if (cb->vcdl != NULL)
+               print_cmd_columns(cb->vcdl, 0);
+
+       printf("\n");
+
+       print_iostat_separator_impl(cb, force_column_width);
+
+       if (cb->vcdl != NULL)
+               print_cmd_columns(cb->vcdl, 1);
+
+       printf("\n");
 }
 
 static void
 print_iostat_header(iostat_cbdata_t *cb)
 {
-       (void) printf("%*s     capacity     operations    bandwidth\n",
-           cb->cb_namewidth, "");
-       (void) printf("%-*s  alloc   free   read  write   read  write\n",
-           cb->cb_namewidth, "pool");
-       print_iostat_separator(cb);
+       print_iostat_header_impl(cb, 0, NULL);
 }
 
+
 /*
  * Display a single statistic.
  */
 static void
-print_one_stat(uint64_t value)
+print_one_stat(uint64_t value, enum zfs_nicenum_format format,
+    unsigned int column_size, boolean_t scripted)
 {
        char buf[64];
 
-       zfs_nicenum(value, buf, sizeof (buf));
-       (void) printf("  %5s", buf);
+       zfs_nicenum_format(value, buf, sizeof (buf), format);
+
+       if (scripted)
+               printf("\t%s", buf);
+       else
+               printf("  %*s", column_size, buf);
+}
+
+/*
+ * Calculate the default vdev stats
+ *
+ * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
+ * stats into calcvs.
+ */
+static void
+calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
+    vdev_stat_t *calcvs)
+{
+       int i;
+
+       memcpy(calcvs, newvs, sizeof (*calcvs));
+       for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
+               calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
+
+       for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
+               calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
+}
+
+/*
+ * Internal representation of the extended iostats data.
+ *
+ * The extended iostat stats are exported in nvlists as either uint64_t arrays
+ * or single uint64_t's.  We make both look like arrays to make them easier
+ * to process.  In order to make single uint64_t's look like arrays, we set
+ * __data to the stat data, and then set *data = &__data with count = 1.  Then,
+ * we can just use *data and count.
+ */
+struct stat_array {
+       uint64_t *data;
+       uint_t count;   /* Number of entries in data[] */
+       uint64_t __data; /* Only used when data is a single uint64_t */
+};
+
+static uint64_t
+stat_histo_max(struct stat_array *nva, unsigned int len)
+{
+       uint64_t max = 0;
+       int i;
+       for (i = 0; i < len; i++)
+               max = MAX(max, array64_max(nva[i].data, nva[i].count));
+
+       return (max);
 }
 
+/*
+ * Helper function to lookup a uint64_t array or uint64_t value and store its
+ * data as a stat_array.  If the nvpair is a single uint64_t value, then we make
+ * it look like a one element array to make it easier to process.
+ */
+static int
+nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
+    struct stat_array *nva)
+{
+       nvpair_t *tmp;
+       int ret;
+
+       verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
+       switch (nvpair_type(tmp)) {
+       case DATA_TYPE_UINT64_ARRAY:
+               ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
+               break;
+       case DATA_TYPE_UINT64:
+               ret = nvpair_value_uint64(tmp, &nva->__data);
+               nva->data = &nva->__data;
+               nva->count = 1;
+               break;
+       default:
+               /* Not a uint64_t */
+               ret = EINVAL;
+               break;
+       }
+
+       return (ret);
+}
+
+/*
+ * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
+ * subtract them, and return the results in a newly allocated stat_array.
+ * You must free the returned array after you are done with it with
+ * free_calc_stats().
+ *
+ * Additionally, you can set "oldnv" to NULL if you simply want the newnv
+ * values.
+ */
+static struct stat_array *
+calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
+    nvlist_t *newnv)
+{
+       nvlist_t *oldnvx = NULL, *newnvx;
+       struct stat_array *oldnva, *newnva, *calcnva;
+       int i, j;
+       unsigned int alloc_size = (sizeof (struct stat_array)) * len;
+
+       /* Extract our extended stats nvlist from the main list */
+       verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
+           &newnvx) == 0);
+       if (oldnv) {
+               verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
+                   &oldnvx) == 0);
+       }
+
+       newnva = safe_malloc(alloc_size);
+       oldnva = safe_malloc(alloc_size);
+       calcnva = safe_malloc(alloc_size);
+
+       for (j = 0; j < len; j++) {
+               verify(nvpair64_to_stat_array(newnvx, names[j],
+                   &newnva[j]) == 0);
+               calcnva[j].count = newnva[j].count;
+               alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
+               calcnva[j].data = safe_malloc(alloc_size);
+               memcpy(calcnva[j].data, newnva[j].data, alloc_size);
+
+               if (oldnvx) {
+                       verify(nvpair64_to_stat_array(oldnvx, names[j],
+                           &oldnva[j]) == 0);
+                       for (i = 0; i < oldnva[j].count; i++)
+                               calcnva[j].data[i] -= oldnva[j].data[i];
+               }
+       }
+       free(newnva);
+       free(oldnva);
+       return (calcnva);
+}
+
+static void
+free_calc_stats(struct stat_array *nva, unsigned int len)
+{
+       int i;
+       for (i = 0; i < len; i++)
+               free(nva[i].data);
+
+       free(nva);
+}
+
+static void
+print_iostat_histo(struct stat_array *nva, unsigned int len,
+    iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
+    double scale)
+{
+       int i, j;
+       char buf[6];
+       uint64_t val;
+       enum zfs_nicenum_format format;
+       unsigned int buckets;
+       unsigned int start_bucket;
+
+       if (cb->cb_literal)
+               format = ZFS_NICENUM_RAW;
+       else
+               format = ZFS_NICENUM_1024;
+
+       /* All these histos are the same size, so just use nva[0].count */
+       buckets = nva[0].count;
+
+       if (cb->cb_flags & IOS_RQ_HISTO_M) {
+               /* Start at 512 - req size should never be lower than this */
+               start_bucket = 9;
+       } else {
+               start_bucket = 0;
+       }
+
+       for (j = start_bucket; j < buckets; j++) {
+               /* Print histogram bucket label */
+               if (cb->cb_flags & IOS_L_HISTO_M) {
+                       /* Ending range of this bucket */
+                       val = (1UL << (j + 1)) - 1;
+                       zfs_nicetime(val, buf, sizeof (buf));
+               } else {
+                       /* Request size (starting range of bucket) */
+                       val = (1UL << j);
+                       zfs_nicenum(val, buf, sizeof (buf));
+               }
+
+               if (cb->cb_scripted)
+                       printf("%llu", (u_longlong_t)val);
+               else
+                       printf("%-*s", namewidth, buf);
+
+               /* Print the values on the line */
+               for (i = 0; i < len; i++) {
+                       print_one_stat(nva[i].data[j] * scale, format,
+                           column_width, cb->cb_scripted);
+               }
+               printf("\n");
+       }
+}
+
+static void
+print_solid_separator(unsigned int length)
+{
+       while (length--)
+               printf("-");
+       printf("\n");
+}
+
+static void
+print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
+    nvlist_t *newnv, double scale, const char *name)
+{
+       unsigned int column_width;
+       unsigned int namewidth;
+       unsigned int entire_width;
+       enum iostat_type type;
+       struct stat_array *nva;
+       const char **names;
+       unsigned int names_len;
+
+       /* What type of histo are we? */
+       type = IOS_HISTO_IDX(cb->cb_flags);
+
+       /* Get NULL-terminated array of nvlist names for our histo */
+       names = vsx_type_to_nvlist[type];
+       names_len = str_array_len(names); /* num of names */
+
+       nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
+
+       if (cb->cb_literal) {
+               column_width = MAX(5,
+                   (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
+       } else {
+               column_width = 5;
+       }
+
+       namewidth = MAX(cb->cb_namewidth,
+           strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
+
+       /*
+        * Calculate the entire line width of what we're printing.  The
+        * +2 is for the two spaces between columns:
+        */
+       /*       read  write                            */
+       /*      -----  -----                            */
+       /*      |___|  <---------- column_width         */
+       /*                                              */
+       /*      |__________|  <--- entire_width         */
+       /*                                              */
+       entire_width = namewidth + (column_width + 2) *
+           label_array_len(iostat_bottom_labels[type]);
+
+       if (cb->cb_scripted)
+               printf("%s\n", name);
+       else
+               print_iostat_header_impl(cb, column_width, name);
+
+       print_iostat_histo(nva, names_len, cb, column_width,
+           namewidth, scale);
+
+       free_calc_stats(nva, names_len);
+       if (!cb->cb_scripted)
+               print_solid_separator(entire_width);
+}
+
+/*
+ * Calculate the average latency of a power-of-two latency histogram
+ */
+static uint64_t
+single_histo_average(uint64_t *histo, unsigned int buckets)
+{
+       int i;
+       uint64_t count = 0, total = 0;
+
+       for (i = 0; i < buckets; i++) {
+               /*
+                * Our buckets are power-of-two latency ranges.  Use the
+                * midpoint latency of each bucket to calculate the average.
+                * For example:
+                *
+                * Bucket          Midpoint
+                * 8ns-15ns:       12ns
+                * 16ns-31ns:      24ns
+                * ...
+                */
+               if (histo[i] != 0) {
+                       total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
+                       count += histo[i];
+               }
+       }
+
+       /* Prevent divide by zero */
+       return (count == 0 ? 0 : total / count);
+}
+
+static void
+print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv,
+    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,
+               ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
+               ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
+               ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
+               ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
+               ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
+               ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
+               ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
+               ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
+               ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
+               ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
+       };
+
+       struct stat_array *nva;
+
+       unsigned int column_width = default_column_width(cb, IOS_QUEUES);
+       enum zfs_nicenum_format format;
+
+       nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
+
+       if (cb->cb_literal)
+               format = ZFS_NICENUM_RAW;
+       else
+               format = ZFS_NICENUM_1024;
+
+       for (i = 0; i < ARRAY_SIZE(names); i++) {
+               val = nva[i].data[0];
+               print_one_stat(val, format, column_width, cb->cb_scripted);
+       }
+
+       free_calc_stats(nva, ARRAY_SIZE(names));
+}
+
+static void
+print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
+    nvlist_t *newnv)
+{
+       int i;
+       uint64_t val;
+       const char *names[] = {
+               ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
+               ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
+       };
+       struct stat_array *nva;
+
+       unsigned int column_width = default_column_width(cb, IOS_LATENCY);
+       enum zfs_nicenum_format format;
+
+       nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
+
+       if (cb->cb_literal)
+               format = ZFS_NICENUM_RAWTIME;
+       else
+               format = ZFS_NICENUM_TIME;
+
+       /* Print our avg latencies on the line */
+       for (i = 0; i < ARRAY_SIZE(names); i++) {
+               /* Compute average latency for a latency histo */
+               val = single_histo_average(nva[i].data, nva[i].count);
+               print_one_stat(val, format, column_width, cb->cb_scripted);
+       }
+       free_calc_stats(nva, ARRAY_SIZE(names));
+}
+
+/*
+ * Print default statistics (capacity/operations/bandwidth)
+ */
+static void
+print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
+{
+       unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
+       enum zfs_nicenum_format format;
+       char na;        /* char to print for "not applicable" values */
+
+       if (cb->cb_literal) {
+               format = ZFS_NICENUM_RAW;
+               na = '0';
+       } else {
+               format = ZFS_NICENUM_1024;
+               na = '-';
+       }
+
+       /* only toplevel vdevs have capacity stats */
+       if (vs->vs_space == 0) {
+               if (cb->cb_scripted)
+                       printf("\t%c\t%c", na, na);
+               else
+                       printf("  %*c  %*c", column_width, na, column_width,
+                           na);
+       } else {
+               print_one_stat(vs->vs_alloc, format, column_width,
+                   cb->cb_scripted);
+               print_one_stat(vs->vs_space - vs->vs_alloc, format,
+                   column_width, cb->cb_scripted);
+       }
+
+       print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
+           format, column_width, cb->cb_scripted);
+       print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
+           format, column_width, cb->cb_scripted);
+       print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
+           format, column_width, cb->cb_scripted);
+       print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
+           format, column_width, cb->cb_scripted);
+}
+
+static const char *class_name[] = {
+       VDEV_ALLOC_BIAS_DEDUP,
+       VDEV_ALLOC_BIAS_SPECIAL,
+       VDEV_ALLOC_CLASS_LOGS
+};
+
 /*
  * Print out all the statistics for the given vdev.  This can either be the
  * toplevel configuration, or called recursively.  If 'name' is NULL, then this
  * is a verbose output, and we don't want to display the toplevel pool stats.
+ *
+ * Returns the number of stat lines printed.
  */
-void
+static unsigned int
 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
     nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
 {
        nvlist_t **oldchild, **newchild;
-       uint_t c, children;
-       vdev_stat_t *oldvs, *newvs;
+       uint_t c, children, oldchildren;
+       vdev_stat_t *oldvs, *newvs, *calcvs;
        vdev_stat_t zerovs = { 0 };
+       char *vname;
+       int i;
+       int ret = 0;
        uint64_t tdelta;
        double scale;
-       char *vname;
+
+       calcvs = safe_malloc(sizeof (*calcvs));
+
+       if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
+               return (ret);
 
        if (oldnv != NULL) {
                verify(nvlist_lookup_uint64_array(oldnv,
@@ -2499,55 +4269,111 @@ print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
                oldvs = &zerovs;
        }
 
+       /* Do we only want to see a specific vdev? */
+       for (i = 0; i < cb->cb_vdev_names_count; i++) {
+               /* Yes we do.  Is this the vdev? */
+               if (strcmp(name, cb->cb_vdev_names[i]) == 0) {
+                       /*
+                        * This is our vdev.  Since it is the only vdev we
+                        * will be displaying, make depth = 0 so that it
+                        * doesn't get indented.
+                        */
+                       depth = 0;
+                       break;
+               }
+       }
+
+       if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) {
+               /* Couldn't match the name */
+               goto children;
+       }
+
+
        verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t **)&newvs, &c) == 0);
 
-       if (strlen(name) + depth > cb->cb_namewidth)
-               (void) printf("%*s%s", depth, "", name);
-       else
-               (void) printf("%*s%s%*s", depth, "", name,
-                   (int)(cb->cb_namewidth - strlen(name) - depth), "");
+       /*
+        * Print the vdev name unless it's is a histogram.  Histograms
+        * display the vdev name in the header itself.
+        */
+       if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
+               if (cb->cb_scripted) {
+                       printf("%s", name);
+               } else {
+                       if (strlen(name) + depth > cb->cb_namewidth)
+                               (void) printf("%*s%s", depth, "", name);
+                       else
+                               (void) printf("%*s%s%*s", depth, "", name,
+                                   (int)(cb->cb_namewidth - strlen(name) -
+                                   depth), "");
+               }
+       }
 
+       /* Calculate our scaling factor */
        tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
-
-       if (tdelta == 0)
-               scale = 1.0;
-       else
-               scale = (double)NANOSEC / tdelta;
-
-       /* only toplevel vdevs have capacity stats */
-       if (newvs->vs_space == 0) {
-               (void) printf("      -      -");
+       if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
+               /*
+                * If we specify printing histograms with no time interval, then
+                * print the histogram numbers over the entire lifetime of the
+                * vdev.
+                */
+               scale = 1;
        } else {
-               print_one_stat(newvs->vs_alloc);
-               print_one_stat(newvs->vs_space - newvs->vs_alloc);
+               if (tdelta == 0)
+                       scale = 1.0;
+               else
+                       scale = (double)NANOSEC / tdelta;
+       }
+
+       if (cb->cb_flags & IOS_DEFAULT_M) {
+               calc_default_iostats(oldvs, newvs, calcvs);
+               print_iostat_default(calcvs, cb, scale);
+       }
+       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);
+       if (cb->cb_flags & IOS_ANYHISTO_M) {
+               printf("\n");
+               print_iostat_histos(cb, oldnv, newnv, scale, name);
        }
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
-           oldvs->vs_ops[ZIO_TYPE_READ])));
+       if (cb->vcdl != NULL) {
+               char *path;
+               if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
+                   &path) == 0) {
+                       printf("  ");
+                       zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
+               }
+       }
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
-           oldvs->vs_ops[ZIO_TYPE_WRITE])));
+       if (!(cb->cb_flags & IOS_ANYHISTO_M))
+               printf("\n");
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
-           oldvs->vs_bytes[ZIO_TYPE_READ])));
+       ret++;
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
-           oldvs->vs_bytes[ZIO_TYPE_WRITE])));
+children:
 
-       (void) printf("\n");
+       free(calcvs);
 
        if (!cb->cb_verbose)
-               return;
+               return (ret);
 
        if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
            &newchild, &children) != 0)
-               return;
+               return (ret);
 
-       if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
-           &oldchild, &c) != 0)
-               return;
+       if (oldnv) {
+               if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
+                   &oldchild, &oldchildren) != 0)
+                       return (ret);
 
+               children = MIN(oldchildren, children);
+       }
+
+       /*
+        * print normal top-level devices
+        */
        for (c = 0; c < children; c++) {
                uint64_t ishole = B_FALSE, islog = B_FALSE;
 
@@ -2560,35 +4386,58 @@ print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
                if (ishole || islog)
                        continue;
 
-               vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
-               print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
+               if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
+                       continue;
+
+               vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
+                   cb->cb_name_flags);
+               ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
                    newchild[c], cb, depth + 2);
                free(vname);
        }
 
        /*
-        * Log device section
+        * print all other top-level devices
         */
-
-       if (num_logs(newnv) > 0) {
-               (void) printf("%-*s      -      -      -      -      -      "
-                   "-\n", cb->cb_namewidth, "logs");
+       for (uint_t n = 0; n < 3; n++) {
+               boolean_t printed = B_FALSE;
 
                for (c = 0; c < children; c++) {
                        uint64_t islog = B_FALSE;
+                       char *bias = NULL;
+                       char *type = NULL;
+
                        (void) nvlist_lookup_uint64(newchild[c],
                            ZPOOL_CONFIG_IS_LOG, &islog);
-
                        if (islog) {
-                               vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
-                                   B_FALSE);
-                               print_vdev_stats(zhp, vname, oldnv ?
-                                   oldchild[c] : NULL, newchild[c],
-                                   cb, depth + 2);
-                               free(vname);
+                               bias = VDEV_ALLOC_CLASS_LOGS;
+                       } else {
+                               (void) nvlist_lookup_string(newchild[c],
+                                   ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
+                               (void) nvlist_lookup_string(newchild[c],
+                                   ZPOOL_CONFIG_TYPE, &type);
+                       }
+                       if (bias == NULL || strcmp(bias, class_name[n]) != 0)
+                               continue;
+                       if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
+                               continue;
+
+                       if (!printed) {
+                               if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
+                                   !cb->cb_scripted && !cb->cb_vdev_names) {
+                                       print_iostat_dashes(cb, 0,
+                                           class_name[n]);
+                               }
+                               printf("\n");
+                               printed = B_TRUE;
                        }
-               }
 
+                       vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
+                           cb->cb_name_flags);
+                       ret += print_vdev_stats(zhp, vname, oldnv ?
+                           oldchild[c] : NULL, newchild[c], cb, depth + 2);
+                       free(vname);
+               }
        }
 
        /*
@@ -2596,23 +4445,33 @@ print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
         */
        if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
            &newchild, &children) != 0)
-               return;
+               return (ret);
 
-       if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
-           &oldchild, &c) != 0)
-               return;
+       if (oldnv) {
+               if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
+                   &oldchild, &oldchildren) != 0)
+                       return (ret);
+
+               children = MIN(oldchildren, children);
+       }
 
        if (children > 0) {
-               (void) printf("%-*s      -      -      -      -      -      "
-                   "-\n", cb->cb_namewidth, "cache");
+               if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
+                   !cb->cb_vdev_names) {
+                       print_iostat_dashes(cb, 0, "cache");
+               }
+               printf("\n");
+
                for (c = 0; c < children; c++) {
                        vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
-                           B_FALSE);
-                       print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
-                           newchild[c], cb, depth + 2);
+                           cb->cb_name_flags);
+                       ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
+                           : NULL, newchild[c], cb, depth + 2);
                        free(vname);
                }
        }
+
+       return (ret);
 }
 
 static int
@@ -2642,6 +4501,7 @@ print_iostat(zpool_handle_t *zhp, void *data)
        iostat_cbdata_t *cb = data;
        nvlist_t *oldconfig, *newconfig;
        nvlist_t *oldnvroot, *newnvroot;
+       int ret;
 
        newconfig = zpool_get_config(zhp, &oldconfig);
 
@@ -2657,15 +4517,18 @@ print_iostat(zpool_handle_t *zhp, void *data)
                verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
                    &oldnvroot) == 0);
 
-       /*
-        * Print out the statistics for the pool.
-        */
-       print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
-
-       if (cb->cb_verbose)
+       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) {
                print_iostat_separator(cb);
+               if (cb->vcdl != NULL) {
+                       print_cmd_columns(cb->vcdl, 1);
+               }
+               printf("\n");
+       }
 
-       return (0);
+       return (ret);
 }
 
 static int
@@ -2686,55 +4549,50 @@ get_columns(void)
        return (columns);
 }
 
-int
-get_namewidth(zpool_handle_t *zhp, void *data)
+/*
+ * Return the required length of the pool/vdev name column.  The minimum
+ * allowed width and output formatting flags must be provided.
+ */
+static int
+get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
 {
-       iostat_cbdata_t *cb = data;
        nvlist_t *config, *nvroot;
-       int columns;
+       int width = min_width;
 
        if ((config = zpool_get_config(zhp, NULL)) != NULL) {
                verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
                    &nvroot) == 0);
-               if (!cb->cb_verbose)
-                       cb->cb_namewidth = strlen(zpool_get_name(zhp));
-               else
-                       cb->cb_namewidth = max_width(zhp, nvroot, 0,
-                           cb->cb_namewidth);
+               unsigned int poolname_len = strlen(zpool_get_name(zhp));
+               if (verbose == B_FALSE) {
+                       width = MAX(poolname_len, min_width);
+               } else {
+                       width = MAX(poolname_len,
+                           max_width(zhp, nvroot, 0, min_width, flags));
+               }
        }
 
-       /*
-        * The width must be at least 10, but may be as large as the
-        * column width - 42 so that we can still fit in one line.
-        */
-       columns = get_columns();
-
-       if (cb->cb_namewidth < 10)
-               cb->cb_namewidth = 10;
-       if (cb->cb_namewidth > columns - 42)
-               cb->cb_namewidth = columns - 42;
-
-       return (0);
+       return (width);
 }
 
 /*
  * Parse the input string, get the 'interval' and 'count' value if there is one.
  */
 static void
-get_interval_count(int *argcp, char **argv, unsigned long *iv,
+get_interval_count(int *argcp, char **argv, float *iv,
     unsigned long *cnt)
 {
-       unsigned long interval = 0, count = 0;
+       float interval = 0;
+       unsigned long count = 0;
        int argc = *argcp;
 
        /*
         * Determine if the last argument is an integer or a pool name
         */
-       if (argc > 0 && isdigit(argv[argc - 1][0])) {
+       if (argc > 0 && isnumber(argv[argc - 1])) {
                char *end;
 
                errno = 0;
-               interval = strtoul(argv[argc - 1], &end, 10);
+               interval = strtof(argv[argc - 1], &end);
 
                if (*end == '\0' && errno == 0) {
                        if (interval == 0) {
@@ -2760,12 +4618,12 @@ get_interval_count(int *argcp, char **argv, unsigned long *iv,
         * If the last argument is also an integer, then we have both a count
         * and an interval.
         */
-       if (argc > 0 && isdigit(argv[argc - 1][0])) {
+       if (argc > 0 && isnumber(argv[argc - 1])) {
                char *end;
 
                errno = 0;
                count = interval;
-               interval = strtoul(argv[argc - 1], &end, 10);
+               interval = strtof(argv[argc - 1], &end);
 
                if (*end == '\0' && errno == 0) {
                        if (interval == 0) {
@@ -2800,10 +4658,380 @@ get_timestamp_arg(char c)
 }
 
 /*
- * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
+ * Return stat flags that are supported by all pools by both the module and
+ * zpool iostat.  "*data" should be initialized to all 0xFFs before running.
+ * It will get ANDed down until only the flags that are supported on all pools
+ * remain.
+ */
+static int
+get_stat_flags_cb(zpool_handle_t *zhp, void *data)
+{
+       uint64_t *mask = data;
+       nvlist_t *config, *nvroot, *nvx;
+       uint64_t flags = 0;
+       int i, j;
+
+       config = zpool_get_config(zhp, NULL);
+       verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
+           &nvroot) == 0);
+
+       /* Default stats are always supported, but for completeness.. */
+       if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
+               flags |= IOS_DEFAULT_M;
+
+       /* Get our extended stats nvlist from the main list */
+       if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
+           &nvx) != 0) {
+               /*
+                * No extended stats; they're probably running an older
+                * module.  No big deal, we support that too.
+                */
+               goto end;
+       }
+
+       /* For each extended stat, make sure all its nvpairs are supported */
+       for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
+               if (!vsx_type_to_nvlist[j][0])
+                       continue;
+
+               /* Start off by assuming the flag is supported, then check */
+               flags |= (1ULL << j);
+               for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
+                       if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
+                               /* flag isn't supported */
+                               flags = flags & ~(1ULL  << j);
+                               break;
+                       }
+               }
+       }
+end:
+       *mask = *mask & flags;
+       return (0);
+}
+
+/*
+ * Return a bitmask of stats that are supported on all pools by both the module
+ * and zpool iostat.
+ */
+static uint64_t
+get_stat_flags(zpool_list_t *list)
+{
+       uint64_t mask = -1;
+
+       /*
+        * get_stat_flags_cb() will lop off bits from "mask" until only the
+        * flags that are supported on all pools remain.
+        */
+       pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
+       return (mask);
+}
+
+/*
+ * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
+ */
+static int
+is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data)
+{
+       iostat_cbdata_t *cb = cb_data;
+       char *name = NULL;
+       int ret = 0;
+
+       name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
+
+       if (strcmp(name, cb->cb_vdev_names[0]) == 0)
+               ret = 1; /* match */
+       free(name);
+
+       return (ret);
+}
+
+/*
+ * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
+ */
+static int
+is_vdev(zpool_handle_t *zhp, void *cb_data)
+{
+       return (for_each_vdev(zhp, is_vdev_cb, cb_data));
+}
+
+/*
+ * Check if vdevs are in a pool
+ *
+ * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
+ * return 0.  If pool_name is NULL, then search all pools.
+ */
+static int
+are_vdevs_in_pool(int argc, char **argv, char *pool_name,
+    iostat_cbdata_t *cb)
+{
+       char **tmp_name;
+       int ret = 0;
+       int i;
+       int pool_count = 0;
+
+       if ((argc == 0) || !*argv)
+               return (0);
+
+       if (pool_name)
+               pool_count = 1;
+
+       /* Temporarily hijack cb_vdev_names for a second... */
+       tmp_name = cb->cb_vdev_names;
+
+       /* Go though our list of prospective vdev names */
+       for (i = 0; i < argc; i++) {
+               cb->cb_vdev_names = argv + i;
+
+               /* Is this name a vdev in our pools? */
+               ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
+                   is_vdev, cb);
+               if (!ret) {
+                       /* No match */
+                       break;
+               }
+       }
+
+       cb->cb_vdev_names = tmp_name;
+
+       return (ret);
+}
+
+static int
+is_pool_cb(zpool_handle_t *zhp, void *data)
+{
+       char *name = data;
+       if (strcmp(name, zpool_get_name(zhp)) == 0)
+               return (1);
+
+       return (0);
+}
+
+/*
+ * Do we have a pool named *name?  If so, return 1, otherwise 0.
+ */
+static int
+is_pool(char *name)
+{
+       return (for_each_pool(0, NULL, B_TRUE, NULL,  is_pool_cb, name));
+}
+
+/* Are all our argv[] strings pool names?  If so return 1, 0 otherwise. */
+static int
+are_all_pools(int argc, char **argv)
+{
+       if ((argc == 0) || !*argv)
+               return (0);
+
+       while (--argc >= 0)
+               if (!is_pool(argv[argc]))
+                       return (0);
+
+       return (1);
+}
+
+/*
+ * Helper function to print out vdev/pool names we can't resolve.  Used for an
+ * error message.
+ */
+static void
+error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
+    iostat_cbdata_t *cb)
+{
+       int i;
+       char *name;
+       char *str;
+       for (i = 0; i < argc; i++) {
+               name = argv[i];
+
+               if (is_pool(name))
+                       str = gettext("pool");
+               else if (are_vdevs_in_pool(1, &name, pool_name, cb))
+                       str = gettext("vdev in this pool");
+               else if (are_vdevs_in_pool(1, &name, NULL, cb))
+                       str = gettext("vdev in another pool");
+               else
+                       str = gettext("unknown");
+
+               fprintf(stderr, "\t%s (%s)\n", name, str);
+       }
+}
+
+/*
+ * 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.
+ */
+static void
+get_interval_count_filter_guids(int *argc, char **argv, float *interval,
+    unsigned long *count, iostat_cbdata_t *cb)
+{
+       char **tmpargv = argv;
+       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)) {
+               /*
+                * The last arg is not a guid, so it's probably an
+                * interval value.
+                */
+               argc_for_interval++;
+
+               if (*argc >= 2 &&
+                   !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
+                       /*
+                        * The 2nd to last arg is not a guid, so it's probably
+                        * an interval value.
+                        */
+                       argc_for_interval++;
+               }
+       }
+
+       /* Point to our list of possible intervals */
+       tmpargv = &argv[*argc - argc_for_interval];
+
+       *argc = *argc - argc_for_interval;
+       get_interval_count(&argc_for_interval, tmpargv,
+           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);
+}
+
+/*
+ * Run one of the zpool status/iostat -c scripts with the help (-h) option and
+ * print the result.
+ *
+ * name:       Short name of the script ('iostat').
+ * path:       Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
+ */
+static void
+print_zpool_script_help(char *name, char *path)
+{
+       char *argv[] = {path, "-h", NULL};
+       char **lines = NULL;
+       int lines_cnt = 0;
+       int rc;
+
+       rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
+           &lines_cnt);
+       if (rc != 0 || lines == NULL || lines_cnt <= 0) {
+               if (lines != NULL)
+                       libzfs_free_str_array(lines, lines_cnt);
+               return;
+       }
+
+       for (int i = 0; i < lines_cnt; i++)
+               if (!is_blank_str(lines[i]))
+                       printf("  %-14s  %s\n", name, lines[i]);
+
+       libzfs_free_str_array(lines, lines_cnt);
+}
+
+/*
+ * Go though the zpool status/iostat -c scripts in the user's path, run their
+ * help option (-h), and print out the results.
+ */
+static void
+print_zpool_dir_scripts(char *dirpath)
+{
+       DIR *dir;
+       struct dirent *ent;
+       char fullpath[MAXPATHLEN];
+       struct stat dir_stat;
+
+       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);
+
+                       /* Print the scripts */
+                       if (stat(fullpath, &dir_stat) == 0)
+                               if (dir_stat.st_mode & S_IXUSR &&
+                                   S_ISREG(dir_stat.st_mode))
+                                       print_zpool_script_help(ent->d_name,
+                                           fullpath);
+               }
+               closedir(dir);
+       }
+}
+
+/*
+ * Print out help text for all zpool status/iostat -c scripts.
+ */
+static void
+print_zpool_script_list(char *subcommand)
+{
+       char *dir, *sp;
+
+       printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
+
+       sp = zpool_get_cmd_search_path();
+       if (sp == NULL)
+               return;
+
+       dir = strtok(sp, ":");
+       while (dir != NULL) {
+               print_zpool_dir_scripts(dir);
+               dir = strtok(NULL, ":");
+       }
+
+       free(sp);
+}
+
+/*
+ * Set the minimum pool/vdev name column width.  The width must be at least 10,
+ * but may be as large as the column width - 42 so it still fits on one line.
+ */
+static int
+get_namewidth_iostat(zpool_handle_t *zhp, void *data)
+{
+       iostat_cbdata_t *cb = data;
+       int width, columns;
+
+       width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
+           cb->cb_verbose);
+       columns = get_columns();
+
+       if (width < 10)
+               width = 10;
+       if (width > columns - 42)
+               width = columns - 42;
+
+       cb->cb_namewidth = width;
+
+       return (0);
+}
+
+/*
+ * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
+ *              [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
+ *              [interval [count]]
  *
+ *     -c CMD  For each vdev, run command CMD
+ *     -g      Display guid for individual vdev name.
+ *     -L      Follow links when resolving vdev path name.
+ *     -P      Display full path for vdev name.
  *     -v      Display statistics for individual vdevs
+ *     -h      Display help
+ *     -p      Display values in parsable (exact) format.
+ *     -H      Scripted mode.  Don't display headers, and separate properties
+ *             by a single tab.
+ *     -l      Display average latency
+ *     -q      Display queue depths
+ *     -w      Display latency histograms
+ *     -r      Display request size histogram
  *     -T      Display a timestamp in date(1) or Unix format
+ *     -n      Only print headers once
  *
  * This command can be tricky because we want to be able to deal with pool
  * creation/destruction as well as vdev configuration changes.  The bulk of this
@@ -2817,27 +5045,106 @@ zpool_do_iostat(int argc, char **argv)
        int c;
        int ret;
        int npools;
-       unsigned long interval = 0, count = 0;
+       float interval = 0;
+       unsigned long count = 0;
+       struct winsize win;
+       int winheight = 24;
        zpool_list_t *list;
        boolean_t verbose = B_FALSE;
+       boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
+       boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
        boolean_t omit_since_boot = B_FALSE;
-       iostat_cbdata_t cb;
+       boolean_t guid = B_FALSE;
+       boolean_t follow_links = B_FALSE;
+       boolean_t full_name = B_FALSE;
+       boolean_t headers_once = B_FALSE;
+       iostat_cbdata_t cb = { 0 };
+       char *cmd = NULL;
+
+       /* Used for printing error message */
+       const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
+           [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
+
+       uint64_t unsupported_flags;
 
        /* check options */
-       while ((c = getopt(argc, argv, "T:vy")) != -1) {
+       while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) {
                switch (c) {
+               case 'c':
+                       if (cmd != NULL) {
+                               fprintf(stderr,
+                                   gettext("Can't set -c flag twice\n"));
+                               exit(1);
+                       }
+
+                       if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
+                           !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
+                               fprintf(stderr, gettext(
+                                   "Can't run -c, disabled by "
+                                   "ZPOOL_SCRIPTS_ENABLED.\n"));
+                               exit(1);
+                       }
+
+                       if ((getuid() <= 0 || geteuid() <= 0) &&
+                           !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
+                               fprintf(stderr, gettext(
+                                   "Can't run -c with root privileges "
+                                   "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
+                               exit(1);
+                       }
+                       cmd = optarg;
+                       verbose = B_TRUE;
+                       break;
+               case 'g':
+                       guid = B_TRUE;
+                       break;
+               case 'L':
+                       follow_links = B_TRUE;
+                       break;
+               case 'P':
+                       full_name = B_TRUE;
+                       break;
                case 'T':
                        get_timestamp_arg(*optarg);
                        break;
                case 'v':
                        verbose = B_TRUE;
                        break;
+               case 'p':
+                       parsable = B_TRUE;
+                       break;
+               case 'l':
+                       latency = B_TRUE;
+                       break;
+               case 'q':
+                       queues = B_TRUE;
+                       break;
+               case 'H':
+                       scripted = B_TRUE;
+                       break;
+               case 'w':
+                       l_histo = B_TRUE;
+                       break;
+               case 'r':
+                       rq_histo = B_TRUE;
+                       break;
                case 'y':
                        omit_since_boot = B_TRUE;
                        break;
+               case 'n':
+                       headers_once = B_TRUE;
+                       break;
+               case 'h':
+                       usage(B_FALSE);
+                       break;
                case '?':
-                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
-                           optopt);
+                       if (optopt == 'c') {
+                               print_zpool_script_list("iostat");
+                               exit(0);
+                       } else {
+                               fprintf(stderr,
+                                   gettext("invalid option '%c'\n"), optopt);
+                       }
                        usage(B_FALSE);
                }
        }
@@ -2845,7 +5152,70 @@ zpool_do_iostat(int argc, char **argv)
        argc -= optind;
        argv += optind;
 
-       get_interval_count(&argc, argv, &interval, &count);
+       cb.cb_literal = parsable;
+       cb.cb_scripted = scripted;
+
+       if (guid)
+               cb.cb_name_flags |= VDEV_NAME_GUID;
+       if (follow_links)
+               cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
+       if (full_name)
+               cb.cb_name_flags |= VDEV_NAME_PATH;
+       cb.cb_iteration = 0;
+       cb.cb_namewidth = 0;
+       cb.cb_verbose = verbose;
+
+       /* Get our interval and count values (if any) */
+       if (guid) {
+               get_interval_count_filter_guids(&argc, argv, &interval,
+                   &count, &cb);
+       } else {
+               get_interval_count(&argc, argv, &interval, &count);
+       }
+
+       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_vdevs_in_pool(argc, argv, NULL, &cb)) {
+               /* All the args are vdevs */
+               cb.cb_vdev_names = argv;
+               cb.cb_vdev_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)) {
+                       /* ...and the rest are vdev names */
+                       cb.cb_vdev_names = argv + 1;
+                       cb.cb_vdev_names_count = argc - 1;
+                       argc = 1; /* One pool to process */
+               } else {
+                       fprintf(stderr, gettext("Expected either a list of "));
+                       fprintf(stderr, gettext("pools, or list of vdevs in"));
+                       fprintf(stderr, " \"%s\", ", argv[0]);
+                       fprintf(stderr, gettext("but got:\n"));
+                       error_list_unresolved_vdevs(argc - 1, argv + 1,
+                           argv[0], &cb);
+                       fprintf(stderr, "\n");
+                       usage(B_FALSE);
+                       return (1);
+               }
+       } else {
+               /*
+                * The args don't make sense. The first arg isn't a pool name,
+                * nor are all the args vdevs.
+                */
+               fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
+               fprintf(stderr, "\n");
+               return (1);
+       }
+
+       if (cb.cb_vdev_names_count != 0) {
+               /*
+                * If user specified vdevs, it implies verbose.
+                */
+               cb.cb_verbose = B_TRUE;
+       }
 
        /*
         * Construct the list of all interesting pools.
@@ -2859,9 +5229,25 @@ zpool_do_iostat(int argc, char **argv)
                return (1);
        }
 
-       if (pool_list_count(list) == 0 && interval == 0) {
+       if (pool_list_count(list) == 0 && interval == 0) {
+               pool_list_free(list);
+               (void) fprintf(stderr, gettext("no pools available\n"));
+               return (1);
+       }
+
+       if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
+               pool_list_free(list);
+               (void) fprintf(stderr,
+                   gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
+               usage(B_FALSE);
+               return (1);
+       }
+
+       if (l_histo && rq_histo) {
                pool_list_free(list);
-               (void) fprintf(stderr, gettext("no pools available\n"));
+               (void) fprintf(stderr,
+                   gettext("Only one of [-r|-w] can be passed at a time\n"));
+               usage(B_FALSE);
                return (1);
        }
 
@@ -2869,9 +5255,45 @@ zpool_do_iostat(int argc, char **argv)
         * Enter the main iostat loop.
         */
        cb.cb_list = list;
-       cb.cb_verbose = verbose;
-       cb.cb_iteration = 0;
-       cb.cb_namewidth = 0;
+
+       if (l_histo) {
+               /*
+                * Histograms tables look out of place when you try to display
+                * them with the other stats, so make a rule that you can only
+                * print histograms by themselves.
+                */
+               cb.cb_flags = IOS_L_HISTO_M;
+       } else if (rq_histo) {
+               cb.cb_flags = IOS_RQ_HISTO_M;
+       } else {
+               cb.cb_flags = IOS_DEFAULT_M;
+               if (latency)
+                       cb.cb_flags |= IOS_LATENCY_M;
+               if (queues)
+                       cb.cb_flags |= IOS_QUEUES_M;
+       }
+
+       /*
+        * See if the module supports all the stats we want to display.
+        */
+       unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
+       if (unsupported_flags) {
+               uint64_t f;
+               int idx;
+               fprintf(stderr,
+                   gettext("The loaded zfs module doesn't support:"));
+
+               /* for each bit set in unsupported_flags */
+               for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
+                       idx = lowbit64(f) - 1;
+                       fprintf(stderr, " -%c", flag_to_arg[idx]);
+               }
+
+               fprintf(stderr, ".  Try running a newer module.\n");
+               pool_list_free(list);
+
+               return (1);
+       }
 
        for (;;) {
                if ((npools = pool_list_count(list)) == 0)
@@ -2882,7 +5304,7 @@ zpool_do_iostat(int argc, char **argv)
                         * we skip any printing.
                         */
                        boolean_t skip = (omit_since_boot &&
-                               cb.cb_iteration == 0);
+                           cb.cb_iteration == 0);
 
                        /*
                         * Refresh all statistics.  This is done as an
@@ -2891,44 +5313,92 @@ zpool_do_iostat(int argc, char **argv)
                         * properly accounted for.
                         */
                        (void) pool_list_iter(list, B_FALSE, refresh_iostat,
-                               &cb);
+                           &cb);
 
                        /*
                         * Iterate over all pools to determine the maximum width
                         * for the pool / device name column across all pools.
                         */
                        cb.cb_namewidth = 0;
-                       (void) pool_list_iter(list, B_FALSE, get_namewidth,
-                               &cb);
+                       (void) pool_list_iter(list, B_FALSE,
+                           get_namewidth_iostat, &cb);
 
                        if (timestamp_fmt != NODATE)
                                print_timestamp(timestamp_fmt);
 
+                       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);
+                       } else {
+                               cb.vcdl = NULL;
+                       }
+
+                       /*
+                        * Are we connected to TTY? If not, headers_once
+                        * should be true, to avoid breaking scripts.
+                        */
+                       if (isatty(fileno(stdout)) == 0)
+                               headers_once = B_TRUE;
+
+                       /*
+                        * Check terminal size so we can print headers
+                        * even when terminal window has its height
+                        * changed.
+                        */
+                       if (headers_once == B_FALSE) {
+                               if (ioctl(1, TIOCGWINSZ, &win) != -1 &&
+                                   win.ws_row > 0)
+                                       winheight = win.ws_row;
+                               else
+                                       headers_once = B_TRUE;
+                       }
+
                        /*
                         * If it's the first time and we're not skipping it,
                         * or either skip or verbose mode, print the header.
+                        *
+                        * The histogram code explicitly prints its header on
+                        * every vdev, so skip this for histograms.
                         */
-                       if ((++cb.cb_iteration == 1 && !skip) ||
-                               (skip != verbose))
+                       if (((++cb.cb_iteration == 1 && !skip) ||
+                           (skip != verbose) ||
+                           (!headers_once &&
+                           (cb.cb_iteration % winheight) == 0)) &&
+                           (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
+                           !cb.cb_scripted)
                                print_iostat_header(&cb);
 
                        if (skip) {
-                               (void) sleep(interval);
+                               (void) fsleep(interval);
                                continue;
                        }
 
-                       (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
+                       pool_list_iter(list, B_FALSE, print_iostat, &cb);
 
                        /*
                         * If there's more than one pool, and we're not in
                         * verbose mode (which prints a separator for us),
                         * then print a separator.
+                        *
+                        * In addition, if we're printing specific vdevs then
+                        * we also want an ending separator.
                         */
-                       if (npools > 1 && !verbose)
+                       if (((npools > 1 && !verbose &&
+                           !(cb.cb_flags & IOS_ANYHISTO_M)) ||
+                           (!(cb.cb_flags & IOS_ANYHISTO_M) &&
+                           cb.cb_vdev_names_count)) &&
+                           !cb.cb_scripted) {
                                print_iostat_separator(&cb);
+                               if (cb.vcdl != NULL)
+                                       print_cmd_columns(cb.vcdl, 1);
+                               printf("\n");
+                       }
+
+                       if (cb.vcdl != NULL)
+                               free_vdev_cmd_data_list(cb.vcdl);
 
-                       if (verbose)
-                               (void) printf("\n");
                }
 
                /*
@@ -2943,7 +5413,7 @@ zpool_do_iostat(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
-               (void) sleep(interval);
+               (void) fsleep(interval);
        }
 
        pool_list_free(list);
@@ -2953,11 +5423,14 @@ zpool_do_iostat(int argc, char **argv)
 
 typedef struct list_cbdata {
        boolean_t       cb_verbose;
+       int             cb_name_flags;
        int             cb_namewidth;
        boolean_t       cb_scripted;
        zprop_list_t    *cb_proplist;
+       boolean_t       cb_literal;
 } list_cbdata_t;
 
+
 /*
  * Given a list of columns to display, output appropriate headers for each one.
  */
@@ -3012,7 +5485,7 @@ print_header(list_cbdata_t *cb)
 
 /*
  * Given a pool and a list of properties, print out all the properties according
- * to the described layout.
+ * to the described layout. Used by zpool_do_list().
  */
 static void
 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
@@ -3047,7 +5520,7 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
                right_justify = B_FALSE;
                if (pl->pl_prop != ZPROP_INVAL) {
                        if (zpool_get_prop(zhp, pl->pl_prop, property,
-                           sizeof (property), NULL) != 0)
+                           sizeof (property), NULL, cb->cb_literal) != 0)
                                propstr = "-";
                        else
                                propstr = property;
@@ -3080,8 +5553,8 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
 }
 
 static void
-print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
-    boolean_t valid)
+print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
+    boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format)
 {
        char propval[64];
        boolean_t fixed;
@@ -3089,25 +5562,41 @@ print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
 
        switch (prop) {
        case ZPOOL_PROP_EXPANDSZ:
+       case ZPOOL_PROP_CHECKPOINT:
+       case ZPOOL_PROP_DEDUPRATIO:
                if (value == 0)
                        (void) strlcpy(propval, "-", sizeof (propval));
                else
-                       zfs_nicenum(value, propval, sizeof (propval));
+                       zfs_nicenum_format(value, propval, sizeof (propval),
+                           format);
                break;
        case ZPOOL_PROP_FRAGMENTATION:
                if (value == ZFS_FRAG_INVALID) {
                        (void) strlcpy(propval, "-", sizeof (propval));
+               } else if (format == ZFS_NICENUM_RAW) {
+                       (void) snprintf(propval, sizeof (propval), "%llu",
+                           (unsigned long long)value);
                } else {
                        (void) snprintf(propval, sizeof (propval), "%llu%%",
                            (unsigned long long)value);
                }
                break;
        case ZPOOL_PROP_CAPACITY:
-               (void) snprintf(propval, sizeof (propval), "%llu%%",
-                   (unsigned long long)value);
+               /* capacity value is in parts-per-10,000 (aka permyriad) */
+               if (format == ZFS_NICENUM_RAW)
+                       (void) snprintf(propval, sizeof (propval), "%llu",
+                           (unsigned long long)value / 100);
+               else
+                       (void) snprintf(propval, sizeof (propval),
+                           value < 1000 ? "%1.2f%%" : value < 10000 ?
+                           "%2.1f%%" : "%3.0f%%", value / 100.0);
+               break;
+       case ZPOOL_PROP_HEALTH:
+               width = 8;
+               snprintf(propval, sizeof (propval), "%-*s", (int)width, str);
                break;
        default:
-               zfs_nicenum(value, propval, sizeof (propval));
+               zfs_nicenum_format(value, propval, sizeof (propval), format);
        }
 
        if (!valid)
@@ -3119,9 +5608,13 @@ print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
                (void) printf("  %*s", (int)width, propval);
 }
 
+/*
+ * print static default line per vdev
+ * not compatible with '-o' <proplist> option
+ */
 void
 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
-    list_cbdata_t *cb, int depth)
+    list_cbdata_t *cb, int depth, boolean_t isspare)
 {
        nvlist_t **child;
        vdev_stat_t *vs;
@@ -3129,8 +5622,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;
-       boolean_t haslog = B_FALSE;
-       char *dashes = "%-*s      -      -      -         -      -      -\n";
+       char *dashes = "%-*s      -      -      -        -         "
+           "-      -      -      -  -\n";
 
        verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t **)&vs, &c) == 0);
@@ -3138,6 +5631,16 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
        if (name != NULL) {
                boolean_t toplevel = (vs->vs_space != 0);
                uint64_t cap;
+               enum zfs_nicenum_format format;
+               const char *state;
+
+               if (cb->cb_literal)
+                       format = ZFS_NICENUM_RAW;
+               else
+                       format = ZFS_NICENUM_1024;
+
+               if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
+                       return;
 
                if (scripted)
                        (void) printf("\t%s", name);
@@ -3153,20 +5656,35 @@ 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, scripted,
-                   toplevel);
-               print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
-                   toplevel);
+               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,
-                   scripted, toplevel);
-               print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
-                   B_TRUE);
+                   NULL, scripted, toplevel, format);
+               print_one_column(ZPOOL_PROP_CHECKPOINT,
+                   vs->vs_checkpoint_space, NULL, scripted, toplevel, format);
+               print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL,
+                   scripted, B_TRUE, format);
                print_one_column(ZPOOL_PROP_FRAGMENTATION,
-                   vs->vs_fragmentation, scripted,
-                   (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
+                   vs->vs_fragmentation, NULL, scripted,
+                   (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
+                   format);
                cap = (vs->vs_space == 0) ? 0 :
-                   (vs->vs_alloc * 100 / vs->vs_space);
-               print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
+                   (vs->vs_alloc * 10000 / vs->vs_space);
+               print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL,
+                   scripted, toplevel, format);
+               print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL,
+                   scripted, toplevel, format);
+               state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
+               if (isspare) {
+                       if (vs->vs_aux == VDEV_AUX_SPARED)
+                               state = "INUSE";
+                       else if (vs->vs_state == VDEV_STATE_HEALTHY)
+                               state = "AVAIL";
+               }
+               print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted,
+                   B_TRUE, format);
                (void) printf("\n");
        }
 
@@ -3174,6 +5692,7 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
            &child, &children) != 0)
                return;
 
+       /* list the normal vdevs first */
        for (c = 0; c < children; c++) {
                uint64_t ishole = B_FALSE;
 
@@ -3182,25 +5701,50 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                        continue;
 
                if (nvlist_lookup_uint64(child[c],
-                   ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
-                       haslog = B_TRUE;
+                   ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
                        continue;
-               }
 
-               vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
-               print_list_stats(zhp, vname, child[c], cb, depth + 2);
+               if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
+                       continue;
+
+               vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                   cb->cb_name_flags);
+               print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE);
                free(vname);
        }
 
-       if (haslog == B_TRUE) {
-               /* LINTED E_SEC_PRINTF_VAR_FMT */
-               (void) printf(dashes, cb->cb_namewidth, "log");
+       /* list the classes: 'logs', 'dedup', and 'special' */
+       for (uint_t n = 0; n < 3; n++) {
+               boolean_t printed = B_FALSE;
+
                for (c = 0; c < children; c++) {
+                       char *bias = NULL;
+                       char *type = NULL;
+
                        if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
-                           &islog) != 0 || !islog)
+                           &islog) == 0 && islog) {
+                               bias = VDEV_ALLOC_CLASS_LOGS;
+                       } else {
+                               (void) nvlist_lookup_string(child[c],
+                                   ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
+                               (void) nvlist_lookup_string(child[c],
+                                   ZPOOL_CONFIG_TYPE, &type);
+                       }
+                       if (bias == NULL || strcmp(bias, class_name[n]) != 0)
+                               continue;
+                       if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
                                continue;
-                       vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
-                       print_list_stats(zhp, vname, child[c], cb, depth + 2);
+
+                       if (!printed) {
+                               /* LINTED E_SEC_PRINTF_VAR_FMT */
+                               (void) printf(dashes, cb->cb_namewidth,
+                                   class_name[n]);
+                               printed = B_TRUE;
+                       }
+                       vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                           cb->cb_name_flags);
+                       print_list_stats(zhp, vname, child[c], cb, depth + 2,
+                           B_FALSE);
                        free(vname);
                }
        }
@@ -3210,8 +5754,10 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                /* LINTED E_SEC_PRINTF_VAR_FMT */
                (void) printf(dashes, cb->cb_namewidth, "cache");
                for (c = 0; c < children; c++) {
-                       vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
-                       print_list_stats(zhp, vname, child[c], cb, depth + 2);
+                       vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                           cb->cb_name_flags);
+                       print_list_stats(zhp, vname, child[c], cb, depth + 2,
+                           B_FALSE);
                        free(vname);
                }
        }
@@ -3221,14 +5767,15 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                /* LINTED E_SEC_PRINTF_VAR_FMT */
                (void) printf(dashes, cb->cb_namewidth, "spare");
                for (c = 0; c < children; c++) {
-                       vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
-                       print_list_stats(zhp, vname, child[c], cb, depth + 2);
+                       vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                           cb->cb_name_flags);
+                       print_list_stats(zhp, vname, child[c], cb, depth + 2,
+                           B_TRUE);
                        free(vname);
                }
        }
 }
 
-
 /*
  * Generic callback function to list a pool.
  */
@@ -3236,30 +5783,54 @@ int
 list_callback(zpool_handle_t *zhp, void *data)
 {
        list_cbdata_t *cbp = data;
-       nvlist_t *config;
-       nvlist_t *nvroot;
-
-       config = zpool_get_config(zhp, NULL);
 
        print_pool(zhp, cbp);
-       if (!cbp->cb_verbose)
-               return (0);
 
-       verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
-           &nvroot) == 0);
-       print_list_stats(zhp, NULL, nvroot, cbp, 0);
+       if (cbp->cb_verbose) {
+               nvlist_t *config, *nvroot;
+
+               config = zpool_get_config(zhp, NULL);
+               verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
+                   &nvroot) == 0);
+               print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE);
+       }
+
+       return (0);
+}
+
+/*
+ * Set the minimum pool/vdev name column width.  The width must be at least 9,
+ * but may be as large as needed.
+ */
+static int
+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);
+
+       if (width < 9)
+               width = 9;
+
+       cb->cb_namewidth = width;
 
        return (0);
 }
 
 /*
- * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
+ * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
  *
+ *     -g      Display guid for individual vdev name.
  *     -H      Scripted mode.  Don't display headers, and separate properties
  *             by a single tab.
+ *     -L      Follow links when resolving vdev path name.
  *     -o      List of properties to display.  Defaults to
  *             "name,size,allocated,free,expandsize,fragmentation,capacity,"
  *             "dedupratio,health,altroot"
+ *     -p      Display values in parsable (exact) format.
+ *     -P      Display full path for vdev name.
  *     -T      Display a timestamp in date(1) or Unix format
  *
  * List all pools in the system, whether or not they're healthy.  Output space
@@ -3272,27 +5843,41 @@ zpool_do_list(int argc, char **argv)
        int ret = 0;
        list_cbdata_t cb = { 0 };
        static char default_props[] =
-           "name,size,allocated,free,expandsize,fragmentation,capacity,"
-           "dedupratio,health,altroot";
+           "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
+           "capacity,dedupratio,health,altroot";
        char *props = default_props;
-       unsigned long interval = 0, count = 0;
+       float interval = 0;
+       unsigned long count = 0;
        zpool_list_t *list;
        boolean_t first = B_TRUE;
 
        /* check options */
-       while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
+       while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
                switch (c) {
+               case 'g':
+                       cb.cb_name_flags |= VDEV_NAME_GUID;
+                       break;
                case 'H':
                        cb.cb_scripted = B_TRUE;
                        break;
+               case 'L':
+                       cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
+                       break;
                case 'o':
                        props = optarg;
                        break;
+               case 'P':
+                       cb.cb_name_flags |= VDEV_NAME_PATH;
+                       break;
+               case 'p':
+                       cb.cb_literal = B_TRUE;
+                       break;
                case 'T':
                        get_timestamp_arg(*optarg);
                        break;
                case 'v':
                        cb.cb_verbose = B_TRUE;
+                       cb.cb_namewidth = 8;    /* 8 until precalc is avail */
                        break;
                case ':':
                        (void) fprintf(stderr, gettext("missing argument for "
@@ -3322,6 +5907,9 @@ zpool_do_list(int argc, char **argv)
                if (pool_list_count(list) == 0)
                        break;
 
+               cb.cb_namewidth = 0;
+               (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb);
+
                if (timestamp_fmt != NODATE)
                        print_timestamp(timestamp_fmt);
 
@@ -3338,7 +5926,7 @@ zpool_do_list(int argc, char **argv)
                        break;
 
                pool_list_free(list);
-               (void) sleep(interval);
+               (void) fsleep(interval);
        }
 
        if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
@@ -3428,25 +6016,44 @@ zpool_do_attach_or_replace(int argc, char **argv, int replacing)
                usage(B_FALSE);
        }
 
-       if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
+       if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
+               nvlist_free(props);
                return (1);
+       }
 
        if (zpool_get_config(zhp, NULL) == NULL) {
                (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
                    poolname);
                zpool_close(zhp);
+               nvlist_free(props);
                return (1);
        }
 
+       /* unless manually specified use "ashift" pool property (if set) */
+       if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
+               int intval;
+               zprop_source_t src;
+               char strval[ZPOOL_MAXPROPLEN];
+
+               intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
+               if (src != ZPROP_SRC_DEFAULT) {
+                       (void) sprintf(strval, "%" PRId32, intval);
+                       verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
+                           &props, B_TRUE) == 0);
+               }
+       }
+
        nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
            argc, argv);
        if (nvroot == NULL) {
                zpool_close(zhp);
+               nvlist_free(props);
                return (1);
        }
 
        ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
 
+       nvlist_free(props);
        nvlist_free(nvroot);
        zpool_close(zhp);
 
@@ -3543,14 +6150,18 @@ zpool_do_detach(int argc, char **argv)
 }
 
 /*
- * zpool split [-n] [-o prop=val] ...
+ * zpool split [-gLnP] [-o prop=val] ...
  *             [-o mntopt] ...
  *             [-R altroot] <pool> <newpool> [<device> ...]
  *
+ *     -g      Display guid for individual vdev name.
+ *     -L      Follow links when resolving vdev path name.
  *     -n      Do not split the pool, but display the resulting layout if
  *             it were to be split.
  *     -o      Set property=value, or set mount options.
+ *     -P      Display full path for vdev name.
  *     -R      Mount the split-off pool under an alternate root.
+ *     -l      Load encryption keys while importing.
  *
  * Splits the named pool and gives it the new pool name.  Devices to be split
  * off may be listed, provided that no more than one device is specified
@@ -3568,25 +6179,35 @@ zpool_do_split(int argc, char **argv)
        char *mntopts = NULL;
        splitflags_t flags;
        int c, ret = 0;
+       boolean_t loadkeys = B_FALSE;
        zpool_handle_t *zhp;
        nvlist_t *config, *props = NULL;
 
        flags.dryrun = B_FALSE;
        flags.import = B_FALSE;
+       flags.name_flags = 0;
 
        /* check options */
-       while ((c = getopt(argc, argv, ":R:no:")) != -1) {
+       while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
                switch (c) {
+               case 'g':
+                       flags.name_flags |= VDEV_NAME_GUID;
+                       break;
+               case 'L':
+                       flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
+                       break;
                case 'R':
                        flags.import = B_TRUE;
                        if (add_prop_list(
                            zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
                            &props, B_TRUE) != 0) {
-                               if (props)
-                                       nvlist_free(props);
+                               nvlist_free(props);
                                usage(B_FALSE);
                        }
                        break;
+               case 'l':
+                       loadkeys = B_TRUE;
+                       break;
                case 'n':
                        flags.dryrun = B_TRUE;
                        break;
@@ -3596,14 +6217,16 @@ zpool_do_split(int argc, char **argv)
                                propval++;
                                if (add_prop_list(optarg, propval,
                                    &props, B_TRUE) != 0) {
-                                       if (props)
-                                               nvlist_free(props);
+                                       nvlist_free(props);
                                        usage(B_FALSE);
                                }
                        } else {
                                mntopts = optarg;
                        }
                        break;
+               case 'P':
+                       flags.name_flags |= VDEV_NAME_PATH;
+                       break;
                case ':':
                        (void) fprintf(stderr, gettext("missing argument for "
                            "'%c' option\n"), optopt);
@@ -3623,6 +6246,12 @@ zpool_do_split(int argc, char **argv)
                usage(B_FALSE);
        }
 
+       if (!flags.import && loadkeys) {
+               (void) fprintf(stderr, gettext("loading keys is only "
+                   "valid when importing the pool\n"));
+               usage(B_FALSE);
+       }
+
        argc -= optind;
        argv += optind;
 
@@ -3641,8 +6270,10 @@ zpool_do_split(int argc, char **argv)
        argc -= 2;
        argv += 2;
 
-       if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
+       if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
+               nvlist_free(props);
                return (1);
+       }
 
        config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
        if (config == NULL) {
@@ -3651,22 +6282,35 @@ zpool_do_split(int argc, char **argv)
                if (flags.dryrun) {
                        (void) printf(gettext("would create '%s' with the "
                            "following layout:\n\n"), newpool);
-                       print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
+                       print_vdev_tree(NULL, newpool, config, 0, "",
+                           flags.name_flags);
                }
-               nvlist_free(config);
        }
 
        zpool_close(zhp);
 
-       if (ret != 0 || flags.dryrun || !flags.import)
+       if (ret != 0 || flags.dryrun || !flags.import) {
+               nvlist_free(config);
+               nvlist_free(props);
                return (ret);
+       }
 
        /*
         * The split was successful. Now we need to open the new
         * pool and import it.
         */
-       if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
+       if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
+               nvlist_free(config);
+               nvlist_free(props);
                return (1);
+       }
+
+       if (loadkeys) {
+               ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
+               if (ret != 0)
+                       ret = 1;
+       }
+
        if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
            zpool_enable_datasets(zhp, mntopts, 0) != 0) {
                ret = 1;
@@ -3676,6 +6320,8 @@ zpool_do_split(int argc, char **argv)
                    "different altroot\n"), "zpool import");
        }
        zpool_close(zhp);
+       nvlist_free(config);
+       nvlist_free(props);
 
        return (ret);
 }
@@ -3755,11 +6401,9 @@ zpool_do_online(int argc, char **argv)
 /*
  * zpool offline [-ft] <pool> <device> ...
  *
- *     -f      Force the device into the offline state, even if doing
- *             so would appear to compromise pool availability.
- *             (not supported yet)
+ *     -f      Force the device into a faulted state.
  *
- *     -t      Only take the device off-line temporarily.  The offline
+ *     -t      Only take the device off-line temporarily.  The offline/faulted
  *             state will not be persistent across reboots.
  */
 /* ARGSUSED */
@@ -3771,14 +6415,17 @@ zpool_do_offline(int argc, char **argv)
        zpool_handle_t *zhp;
        int ret = 0;
        boolean_t istmp = B_FALSE;
+       boolean_t fault = B_FALSE;
 
        /* check options */
        while ((c = getopt(argc, argv, "ft")) != -1) {
                switch (c) {
+               case 'f':
+                       fault = B_TRUE;
+                       break;
                case 't':
                        istmp = B_TRUE;
                        break;
-               case 'f':
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -3805,8 +6452,22 @@ zpool_do_offline(int argc, char **argv)
                return (1);
 
        for (i = 1; i < argc; i++) {
-               if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
-                       ret = 1;
+               if (fault) {
+                       uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
+                       vdev_aux_t aux;
+                       if (istmp == B_FALSE) {
+                               /* Force the fault to persist across imports */
+                               aux = VDEV_AUX_EXTERNAL_PERSIST;
+                       } else {
+                               aux = VDEV_AUX_EXTERNAL;
+                       }
+
+                       if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
+                               ret = 1;
+               } else {
+                       if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
+                               ret = 1;
+               }
        }
 
        zpool_close(zhp);
@@ -3878,8 +6539,10 @@ zpool_do_clear(int argc, char **argv)
 
        /* In future, further rewind policy choices can be passed along here */
        if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
-           nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
+           nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
+           rewind_policy) != 0) {
                return (1);
+       }
 
        pool = argv[0];
        device = argc == 2 ? argv[1] : NULL;
@@ -3955,12 +6618,14 @@ zpool_do_reopen(int argc, char **argv)
 {
        int c;
        int ret = 0;
-       zpool_handle_t *zhp;
-       char *pool;
+       boolean_t scrub_restart = B_TRUE;
 
        /* check options */
-       while ((c = getopt(argc, argv, "")) != -1) {
+       while ((c = getopt(argc, argv, "n")) != -1) {
                switch (c) {
+               case 'n':
+                       scrub_restart = B_FALSE;
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -3968,25 +6633,13 @@ zpool_do_reopen(int argc, char **argv)
                }
        }
 
-       argc--;
-       argv++;
-
-       if (argc < 1) {
-               (void) fprintf(stderr, gettext("missing pool name\n"));
-               usage(B_FALSE);
-       }
-
-       if (argc > 1) {
-               (void) fprintf(stderr, gettext("too many arguments\n"));
-               usage(B_FALSE);
-       }
+       argc -= optind;
+       argv += optind;
 
-       pool = argv[0];
-       if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
-               return (1);
+       /* if argc == 0 we will execute zpool_reopen_one on all pools */
+       ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one,
+           &scrub_restart);
 
-       ret = zpool_reopen(zhp);
-       zpool_close(zhp);
        return (ret);
 }
 
@@ -3994,8 +6647,35 @@ typedef struct scrub_cbdata {
        int     cb_type;
        int     cb_argc;
        char    **cb_argv;
+       pool_scrub_cmd_t cb_scrub_cmd;
 } scrub_cbdata_t;
 
+static boolean_t
+zpool_has_checkpoint(zpool_handle_t *zhp)
+{
+       nvlist_t *config, *nvroot;
+
+       config = zpool_get_config(zhp, NULL);
+
+       if (config != NULL) {
+               pool_checkpoint_stat_t *pcs = NULL;
+               uint_t c;
+
+               nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
+               (void) nvlist_lookup_uint64_array(nvroot,
+                   ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
+
+               if (pcs == NULL || pcs->pcs_state == CS_NONE)
+                       return (B_FALSE);
+
+               assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
+                   pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
+               return (B_TRUE);
+       }
+
+       return (B_FALSE);
+}
+
 int
 scrub_callback(zpool_handle_t *zhp, void *data)
 {
@@ -4006,20 +6686,28 @@ scrub_callback(zpool_handle_t *zhp, void *data)
         * Ignore faulted pools.
         */
        if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
-               (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
+               (void) fprintf(stderr, gettext("cannot scan '%s': pool is "
                    "currently unavailable\n"), zpool_get_name(zhp));
                return (1);
        }
 
-       err = zpool_scan(zhp, cb->cb_type);
+       err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
+
+       if (err == 0 && zpool_has_checkpoint(zhp) &&
+           cb->cb_type == POOL_SCAN_SCRUB) {
+               (void) printf(gettext("warning: will not scrub state that "
+                   "belongs to the checkpoint of pool '%s'\n"),
+                   zpool_get_name(zhp));
+       }
 
        return (err != 0);
 }
 
 /*
- * zpool scrub [-s] <pool> ...
+ * zpool scrub [-s | -p] <pool> ...
  *
  *     -s      Stop.  Stops any in-progress scrub.
+ *     -p      Pause. Pause in-progress scrub.
  */
 int
 zpool_do_scrub(int argc, char **argv)
@@ -4028,54 +6716,216 @@ zpool_do_scrub(int argc, char **argv)
        scrub_cbdata_t cb;
 
        cb.cb_type = POOL_SCAN_SCRUB;
+       cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
 
        /* check options */
-       while ((c = getopt(argc, argv, "s")) != -1) {
+       while ((c = getopt(argc, argv, "sp")) != -1) {
                switch (c) {
                case 's':
                        cb.cb_type = POOL_SCAN_NONE;
                        break;
+               case 'p':
+                       cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
+                       break;
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       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"));
+               usage(B_FALSE);
+       }
+
+       cb.cb_argc = argc;
+       cb.cb_argv = argv;
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing pool name argument\n"));
+               usage(B_FALSE);
+       }
+
+       return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
+}
+
+/*
+ * zpool resilver <pool> ...
+ *
+ *     Restarts any in-progress resilver
+ */
+int
+zpool_do_resilver(int argc, char **argv)
+{
+       int c;
+       scrub_cbdata_t cb;
+
+       cb.cb_type = POOL_SCAN_RESILVER;
+       cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
+       cb.cb_argc = argc;
+       cb.cb_argv = argv;
+
+       /* check options */
+       while ((c = getopt(argc, argv, "")) != -1) {
+               switch (c) {
+               case '?':
+                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
+                           optopt);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing pool name argument\n"));
+               usage(B_FALSE);
+       }
+
+       return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
+}
+
+/*
+ * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
+ *
+ *     -c              Cancel. Ends any in-progress trim.
+ *     -d              Secure trim.  Requires kernel and device support.
+ *     -r <rate>       Sets the TRIM rate in bytes (per second). Supports
+ *                     adding a multiplier suffix such as 'k' or 'm'.
+ *     -s              Suspend. TRIM can then be restarted with no flags.
+ */
+int
+zpool_do_trim(int argc, char **argv)
+{
+       struct option long_options[] = {
+               {"cancel",      no_argument,            NULL,   'c'},
+               {"secure",      no_argument,            NULL,   'd'},
+               {"rate",        required_argument,      NULL,   'r'},
+               {"suspend",     no_argument,            NULL,   's'},
+               {0, 0, 0, 0}
+       };
+
+       pool_trim_func_t cmd_type = POOL_TRIM_START;
+       uint64_t rate = 0;
+       boolean_t secure = B_FALSE;
+
+       int c;
+       while ((c = getopt_long(argc, argv, "cdr:s", long_options, NULL))
+           != -1) {
+               switch (c) {
+               case 'c':
+                       if (cmd_type != POOL_TRIM_START &&
+                           cmd_type != POOL_TRIM_CANCEL) {
+                               (void) fprintf(stderr, gettext("-c cannot be "
+                                   "combined with other options\n"));
+                               usage(B_FALSE);
+                       }
+                       cmd_type = POOL_TRIM_CANCEL;
+                       break;
+               case 'd':
+                       if (cmd_type != POOL_TRIM_START) {
+                               (void) fprintf(stderr, gettext("-d cannot be "
+                                   "combined with the -c or -s options\n"));
+                               usage(B_FALSE);
+                       }
+                       secure = B_TRUE;
+                       break;
+               case 'r':
+                       if (cmd_type != POOL_TRIM_START) {
+                               (void) fprintf(stderr, gettext("-r cannot be "
+                                   "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"));
+                               usage(B_FALSE);
+                       }
+                       break;
+               case 's':
+                       if (cmd_type != POOL_TRIM_START &&
+                           cmd_type != POOL_TRIM_SUSPEND) {
+                               (void) fprintf(stderr, gettext("-s cannot be "
+                                   "combined with other options\n"));
+                               usage(B_FALSE);
+                       }
+                       cmd_type = POOL_TRIM_SUSPEND;
+                       break;
                case '?':
-                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
-                           optopt);
+                       if (optopt != 0) {
+                               (void) fprintf(stderr,
+                                   gettext("invalid option '%c'\n"), optopt);
+                       } else {
+                               (void) fprintf(stderr,
+                                   gettext("invalid option '%s'\n"),
+                                   argv[optind - 1]);
+                       }
                        usage(B_FALSE);
                }
        }
 
-       cb.cb_argc = argc;
-       cb.cb_argv = argv;
        argc -= optind;
        argv += optind;
 
        if (argc < 1) {
                (void) fprintf(stderr, gettext("missing pool name argument\n"));
                usage(B_FALSE);
+               return (-1);
        }
 
-       return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
-}
+       char *poolname = argv[0];
+       zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
+       if (zhp == NULL)
+               return (-1);
 
-typedef struct status_cbdata {
-       int             cb_count;
-       boolean_t       cb_allpools;
-       boolean_t       cb_verbose;
-       boolean_t       cb_explain;
-       boolean_t       cb_first;
-       boolean_t       cb_dedup_stats;
-} status_cbdata_t;
+       trimflags_t trim_flags = {
+               .secure = secure,
+               .rate = rate,
+       };
+
+       nvlist_t *vdevs = fnvlist_alloc();
+       if (argc == 1) {
+               /* no individual leaf vdevs specified, so add them all */
+               nvlist_t *config = zpool_get_config(zhp, NULL);
+               nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
+                   ZPOOL_CONFIG_VDEV_TREE);
+               zpool_collect_leaves(zhp, nvroot, vdevs);
+               trim_flags.fullpool = B_TRUE;
+       } else {
+               trim_flags.fullpool = B_FALSE;
+               for (int i = 1; i < argc; i++) {
+                       fnvlist_add_boolean(vdevs, argv[i]);
+               }
+       }
+
+       int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags);
+
+       fnvlist_free(vdevs);
+       zpool_close(zhp);
+
+       return (error);
+}
 
 /*
  * Print out detailed scrub status.
  */
-void
+static void
 print_scan_status(pool_scan_stat_t *ps)
 {
-       time_t start, end;
-       uint64_t elapsed, mins_left, hours_left;
-       uint64_t pass_exam, examined, total;
-       uint_t rate;
+       time_t start, end, pause;
+       uint64_t total_secs_left;
+       uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
+       uint64_t pass_scanned, scanned, pass_issued, issued, total;
+       uint64_t scan_rate, issue_rate;
        double fraction_done;
-       char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
+       char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
+       char srate_buf[7], irate_buf[7];
 
        (void) printf(gettext("  scan: "));
 
@@ -4088,30 +6938,36 @@ print_scan_status(pool_scan_stat_t *ps)
 
        start = ps->pss_start_time;
        end = ps->pss_end_time;
-       zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
+       pause = ps->pss_pass_scrub_pause;
+
+       zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
 
        assert(ps->pss_func == POOL_SCAN_SCRUB ||
            ps->pss_func == POOL_SCAN_RESILVER);
-       /*
-        * Scan is finished or canceled.
-        */
+
+       /* Scan is finished or canceled. */
        if (ps->pss_state == DSS_FINISHED) {
-               uint64_t minutes_taken = (end - start) / 60;
-               char *fmt = NULL;
+               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);
 
                if (ps->pss_func == POOL_SCAN_SCRUB) {
-                       fmt = gettext("scrub repaired %s in %lluh%um with "
-                           "%llu errors on %s");
+                       (void) printf(gettext("scrub repaired %s "
+                           "in %llu days %02llu:%02llu:%02llu "
+                           "with %llu errors on %s"), processed_buf,
+                           (u_longlong_t)days_left, (u_longlong_t)hours_left,
+                           (u_longlong_t)mins_left, (u_longlong_t)secs_left,
+                           (u_longlong_t)ps->pss_errors, ctime(&end));
                } else if (ps->pss_func == POOL_SCAN_RESILVER) {
-                       fmt = gettext("resilvered %s in %lluh%um with "
-                           "%llu errors on %s");
+                       (void) printf(gettext("resilvered %s "
+                           "in %llu days %02llu:%02llu:%02llu "
+                           "with %llu errors on %s"), processed_buf,
+                           (u_longlong_t)days_left, (u_longlong_t)hours_left,
+                           (u_longlong_t)mins_left, (u_longlong_t)secs_left,
+                           (u_longlong_t)ps->pss_errors, ctime(&end));
                }
-               /* LINTED */
-               (void) printf(fmt, processed_buf,
-                   (u_longlong_t)(minutes_taken / 60),
-                   (uint_t)(minutes_taken % 60),
-                   (u_longlong_t)ps->pss_errors,
-                   ctime((time_t *)&end));
                return;
        } else if (ps->pss_state == DSS_CANCELED) {
                if (ps->pss_func == POOL_SCAN_SCRUB) {
@@ -4126,54 +6982,257 @@ print_scan_status(pool_scan_stat_t *ps)
 
        assert(ps->pss_state == DSS_SCANNING);
 
-       /*
-        * Scan is in progress.
-        */
+       /* Scan is in progress. Resilvers can't be paused. */
        if (ps->pss_func == POOL_SCAN_SCRUB) {
-               (void) printf(gettext("scrub in progress since %s"),
-                   ctime(&start));
+               if (pause == 0) {
+                       (void) printf(gettext("scrub in progress since %s"),
+                           ctime(&start));
+               } else {
+                       (void) printf(gettext("scrub paused since %s"),
+                           ctime(&pause));
+                       (void) printf(gettext("\tscrub started on %s"),
+                           ctime(&start));
+               }
        } else if (ps->pss_func == POOL_SCAN_RESILVER) {
                (void) printf(gettext("resilver in progress since %s"),
                    ctime(&start));
        }
 
-       examined = ps->pss_examined ? ps->pss_examined : 1;
+       scanned = ps->pss_examined;
+       pass_scanned = ps->pss_pass_exam;
+       issued = ps->pss_issued;
+       pass_issued = ps->pss_pass_issued;
        total = ps->pss_to_examine;
-       fraction_done = (double)examined / total;
 
-       /* elapsed time for this pass */
+       /* we are only done with a block once we have issued the IO for it */
+       fraction_done = (double)issued / total;
+
+       /* elapsed time for this pass, rounding up to 1 if it's 0 */
        elapsed = time(NULL) - ps->pss_pass_start;
-       elapsed = elapsed ? elapsed : 1;
-       pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
-       rate = pass_exam / elapsed;
-       rate = rate ? rate : 1;
-       mins_left = ((total - examined) / rate) / 60;
-       hours_left = mins_left / 60;
+       elapsed -= ps->pss_pass_scrub_spent_paused;
+       elapsed = (elapsed != 0) ? elapsed : 1;
+
+       scan_rate = pass_scanned / elapsed;
+       issue_rate = pass_issued / elapsed;
+       total_secs_left = (issue_rate != 0 && total >= issued) ?
+           ((total - issued) / issue_rate) : UINT64_MAX;
+
+       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);
+
+       /* 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));
+
+       /* 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);
+       }
+
+       if (ps->pss_func == POOL_SCAN_RESILVER) {
+               (void) printf(gettext("\t%s resilvered, %.2f%% done"),
+                   processed_buf, 100 * fraction_done);
+       } else if (ps->pss_func == POOL_SCAN_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) {
+                       (void) printf(gettext(", %llu days "
+                           "%02llu:%02llu:%02llu to go\n"),
+                           (u_longlong_t)days_left, (u_longlong_t)hours_left,
+                           (u_longlong_t)mins_left, (u_longlong_t)secs_left);
+               } else {
+                       (void) printf(gettext(", no estimated "
+                           "completion time\n"));
+               }
+       } else {
+               (void) printf(gettext("\n"));
+       }
+}
+
+/*
+ * As we don't scrub checkpointed blocks, we want to warn the
+ * user that we skipped scanning some blocks if a checkpoint exists
+ * or existed at any time during the scan.
+ */
+static void
+print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
+{
+       if (ps == NULL || pcs == NULL)
+               return;
+
+       if (pcs->pcs_state == CS_NONE ||
+           pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
+               return;
+
+       assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
+
+       if (ps->pss_state == DSS_NONE)
+               return;
+
+       if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
+           ps->pss_end_time < pcs->pcs_start_time)
+               return;
+
+       if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
+               (void) printf(gettext("    scan warning: skipped blocks "
+                   "that are only referenced by the checkpoint.\n"));
+       } else {
+               assert(ps->pss_state == DSS_SCANNING);
+               (void) printf(gettext("    scan warning: skipping blocks "
+                   "that are only referenced by the checkpoint.\n"));
+       }
+}
+
+/*
+ * Print out detailed removal status.
+ */
+static void
+print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
+{
+       char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
+       time_t start, end;
+       nvlist_t *config, *nvroot;
+       nvlist_t **child;
+       uint_t children;
+       char *vdev_name;
+
+       if (prs == NULL || prs->prs_state == DSS_NONE)
+               return;
 
-       zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
-       zfs_nicenum(total, total_buf, sizeof (total_buf));
-       zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
+       /*
+        * Determine name of vdev.
+        */
+       config = zpool_get_config(zhp, NULL);
+       nvroot = fnvlist_lookup_nvlist(config,
+           ZPOOL_CONFIG_VDEV_TREE);
+       verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
+           &child, &children) == 0);
+       assert(prs->prs_removing_vdev < children);
+       vdev_name = zpool_vdev_name(g_zfs, zhp,
+           child[prs->prs_removing_vdev], B_TRUE);
+
+       (void) printf(gettext("remove: "));
+
+       start = prs->prs_start_time;
+       end = prs->prs_end_time;
+       zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
 
        /*
-        * do not print estimated time if hours_left is more than 30 days
+        * Removal is finished or canceled.
         */
-       (void) printf(gettext("\t%s scanned out of %s at %s/s"),
-           examined_buf, total_buf, rate_buf);
-       if (hours_left < (30 * 24)) {
-               (void) printf(gettext(", %lluh%um to go\n"),
-                   (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
+       if (prs->prs_state == DSS_FINISHED) {
+               uint64_t minutes_taken = (end - start) / 60;
+
+               (void) printf(gettext("Removal of vdev %llu copied %s "
+                   "in %lluh%um, completed on %s"),
+                   (longlong_t)prs->prs_removing_vdev,
+                   copied_buf,
+                   (u_longlong_t)(minutes_taken / 60),
+                   (uint_t)(minutes_taken % 60),
+                   ctime((time_t *)&end));
+       } else if (prs->prs_state == DSS_CANCELED) {
+               (void) printf(gettext("Removal of %s canceled on %s"),
+                   vdev_name, ctime(&end));
        } else {
+               uint64_t copied, total, elapsed, mins_left, hours_left;
+               double fraction_done;
+               uint_t rate;
+
+               assert(prs->prs_state == DSS_SCANNING);
+
+               /*
+                * Removal is in progress.
+                */
                (void) printf(gettext(
-                   ", (scan is slow, no estimated time)\n"));
+                   "Evacuation of %s in progress since %s"),
+                   vdev_name, ctime(&start));
+
+               copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
+               total = prs->prs_to_copy;
+               fraction_done = (double)copied / total;
+
+               /* elapsed time for this pass */
+               elapsed = time(NULL) - prs->prs_start_time;
+               elapsed = elapsed > 0 ? elapsed : 1;
+               rate = copied / elapsed;
+               rate = rate > 0 ? rate : 1;
+               mins_left = ((total - copied) / rate) / 60;
+               hours_left = mins_left / 60;
+
+               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("    %s copied out of %s at %s/s, "
+                   "%.2f%% done"),
+                   examined_buf, total_buf, rate_buf, 100 * fraction_done);
+               if (hours_left < (30 * 24)) {
+                       (void) printf(gettext(", %lluh%um to go\n"),
+                           (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
+               } else {
+                       (void) printf(gettext(
+                           ", (copy is slow, no estimated time)\n"));
+               }
        }
 
-       if (ps->pss_func == POOL_SCAN_RESILVER) {
-               (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
-                   processed_buf, 100 * fraction_done);
-       } else if (ps->pss_func == POOL_SCAN_SCRUB) {
-               (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
-                   processed_buf, 100 * fraction_done);
+       if (prs->prs_mapping_memory > 0) {
+               char mem_buf[7];
+               zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
+               (void) printf(gettext("    %s memory used for "
+                   "removed device mappings\n"),
+                   mem_buf);
+       }
+}
+
+static void
+print_checkpoint_status(pool_checkpoint_stat_t *pcs)
+{
+       time_t start;
+       char space_buf[7];
+
+       if (pcs == NULL || pcs->pcs_state == CS_NONE)
+               return;
+
+       (void) printf(gettext("checkpoint: "));
+
+       start = pcs->pcs_start_time;
+       zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
+
+       if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
+               char *date = ctime(&start);
+
+               /*
+                * ctime() adds a newline at the end of the generated
+                * string, thus the weird format specifier and the
+                * strlen() call used to chop it off from the output.
+                */
+               (void) printf(gettext("created %.*s, consumes %s\n"),
+                   (int)(strlen(date) - 1), date, space_buf);
+               return;
        }
+
+       assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
+
+       (void) printf(gettext("discarding, %s remaining.\n"),
+           space_buf);
 }
 
 static void
@@ -4184,11 +7243,8 @@ print_error_log(zpool_handle_t *zhp)
        char *pathname;
        size_t len = MAXPATHLEN * 2;
 
-       if (zpool_get_errlog(zhp, &nverrlist) != 0) {
-               (void) printf("errors: List of errors unavailable "
-                   "(insufficient privileges)\n");
+       if (zpool_get_errlog(zhp, &nverrlist) != 0)
                return;
-       }
 
        (void) printf("errors: Permanent errors have been "
            "detected in the following files:\n\n");
@@ -4212,8 +7268,8 @@ print_error_log(zpool_handle_t *zhp)
 }
 
 static void
-print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
-    int namewidth)
+print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
+    uint_t nspares)
 {
        uint_t i;
        char *name;
@@ -4224,16 +7280,16 @@ print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
        (void) printf(gettext("\tspares\n"));
 
        for (i = 0; i < nspares; i++) {
-               name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
-               print_status_config(zhp, name, spares[i],
-                   namewidth, 2, B_TRUE);
+               name = zpool_vdev_name(g_zfs, zhp, spares[i],
+                   cb->cb_name_flags);
+               print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
                free(name);
        }
 }
 
 static void
-print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
-    int namewidth)
+print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
+    uint_t nl2cache)
 {
        uint_t i;
        char *name;
@@ -4244,9 +7300,9 @@ print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
        (void) printf(gettext("\tcache\n"));
 
        for (i = 0; i < nl2cache; i++) {
-               name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
-               print_status_config(zhp, name, l2cache[i],
-                   namewidth, 2, B_FALSE);
+               name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
+                   cb->cb_name_flags);
+               print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
                free(name);
        }
 }
@@ -4258,6 +7314,7 @@ print_dedup_stats(nvlist_t *config)
        ddt_stat_t *dds;
        ddt_object_t *ddo;
        uint_t c;
+       char dspace[6], mspace[6];
 
        /*
         * If the pool was faulted then we may not have been able to
@@ -4275,10 +7332,12 @@ print_dedup_stats(nvlist_t *config)
                return;
        }
 
-       (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
+       zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
+       zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
+       (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
            (u_longlong_t)ddo->ddo_count,
-           (u_longlong_t)ddo->ddo_dspace,
-           (u_longlong_t)ddo->ddo_mspace);
+           dspace,
+           mspace);
 
        verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
            (uint64_t **)&dds, &c) == 0);
@@ -4341,11 +7400,11 @@ status_callback(zpool_handle_t *zhp, void *data)
        else
                (void) printf("\n");
 
-       verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
-           &nvroot) == 0);
+       nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
        verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t **)&vs, &c) == 0);
-       health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
+
+       health = zpool_get_state_str(zhp);
 
        (void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
        (void) printf(gettext(" state: %s\n"), health);
@@ -4514,6 +7573,15 @@ status_callback(zpool_handle_t *zhp, void *data)
                    "to be recovered.\n"));
                break;
 
+       case ZPOOL_STATUS_IO_FAILURE_MMP:
+               (void) printf(gettext("status: The pool is suspended because "
+                   "multihost writes failed or were delayed;\n\tanother "
+                   "system could import the pool undetected.\n"));
+               (void) printf(gettext("action: Make sure the pool's devices "
+                   "are connected, then reboot your system and\n\timport the "
+                   "pool.\n"));
+               break;
+
        case ZPOOL_STATUS_IO_FAILURE_WAIT:
        case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
                (void) printf(gettext("status: One or more devices are "
@@ -4525,7 +7593,7 @@ status_callback(zpool_handle_t *zhp, void *data)
        case ZPOOL_STATUS_BAD_LOG:
                (void) printf(gettext("status: An intent log record "
                    "could not be read.\n"
-                   "\tWaiting for adminstrator intervention to fix the "
+                   "\tWaiting for administrator intervention to fix the "
                    "faulted pool.\n"));
                (void) printf(gettext("action: Either restore the affected "
                    "device(s) and run 'zpool online',\n"
@@ -4557,6 +7625,32 @@ status_callback(zpool_handle_t *zhp, void *data)
                            "run 'zpool scrub'.\n"));
                        break;
 
+               case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
+                       (void) printf(gettext("\tExisting encrypted datasets "
+                           "contain an on-disk incompatibility\n\twhich "
+                           "needs to be corrected.\n"));
+                       (void) printf(gettext("action: To correct the issue "
+                           "backup existing encrypted datasets to new\n\t"
+                           "encrypted datasets and destroy the old ones. "
+                           "'zfs mount -o ro' can\n\tbe used to temporarily "
+                           "mount existing encrypted datasets readonly.\n"));
+                       break;
+
+               case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
+                       (void) printf(gettext("\tExisting encrypted snapshots "
+                           "and bookmarks contain an on-disk\n\tincompat"
+                           "ibility. This may cause on-disk corruption if "
+                           "they are used\n\twith 'zfs recv'.\n"));
+                       (void) printf(gettext("action: To correct the issue, "
+                           "enable the bookmark_v2 feature. No additional\n\t"
+                           "action is needed if there are no encrypted "
+                           "snapshots or bookmarks.\n\tIf preserving the "
+                           "encrypted snapshots and bookmarks is required, "
+                           "use\n\ta non-raw send to backup and restore them. "
+                           "Alternately, they may be\n\tremoved to resolve "
+                           "the incompatibility.\n"));
+                       break;
+
                default:
                        /*
                         * All errata which allow the pool to be imported
@@ -4578,35 +7672,57 @@ status_callback(zpool_handle_t *zhp, void *data)
                    msgid);
 
        if (config != NULL) {
-               int namewidth;
                uint64_t nerr;
                nvlist_t **spares, **l2cache;
                uint_t nspares, nl2cache;
+               pool_checkpoint_stat_t *pcs = NULL;
                pool_scan_stat_t *ps = NULL;
+               pool_removal_stat_t *prs = NULL;
 
+               (void) nvlist_lookup_uint64_array(nvroot,
+                   ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
                (void) nvlist_lookup_uint64_array(nvroot,
                    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
+               (void) nvlist_lookup_uint64_array(nvroot,
+                   ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
+
                print_scan_status(ps);
+               print_checkpoint_scan_warning(ps, pcs);
+               print_removal_status(zhp, prs);
+               print_checkpoint_status(pcs);
 
-               namewidth = max_width(zhp, nvroot, 0, 0);
-               if (namewidth < 10)
-                       namewidth = 10;
+               cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
+                   cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
+               if (cbp->cb_namewidth < 10)
+                       cbp->cb_namewidth = 10;
 
                (void) printf(gettext("config:\n\n"));
-               (void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
-                   "NAME", "STATE", "READ", "WRITE", "CKSUM");
-               print_status_config(zhp, zpool_get_name(zhp), nvroot,
-                   namewidth, 0, B_FALSE);
+               (void) printf(gettext("\t%-*s  %-8s %5s %5s %5s"),
+                   cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
+                   "CKSUM");
+
+               if (cbp->cb_print_slow_ios)
+                       (void) printf(" %5s", gettext("SLOW"));
+
+               if (cbp->vcdl != NULL)
+                       print_cmd_columns(cbp->vcdl, 0);
+
+               printf("\n");
+
+               print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
+                   B_FALSE);
+
+               print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
+               print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
+               print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
 
-               if (num_logs(nvroot) > 0)
-                       print_logs(zhp, nvroot, namewidth, B_TRUE);
                if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
                    &l2cache, &nl2cache) == 0)
-                       print_l2cache(zhp, l2cache, nl2cache, namewidth);
+                       print_l2cache(zhp, cbp, l2cache, nl2cache);
 
                if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
                    &spares, &nspares) == 0)
-                       print_spares(zhp, spares, nspares, namewidth);
+                       print_spares(zhp, cbp, spares, nspares);
 
                if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
                    &nerr) == 0) {
@@ -4654,11 +7770,20 @@ status_callback(zpool_handle_t *zhp, void *data)
 }
 
 /*
- * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
+ * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
+ *              [interval [count]]
  *
+ *     -c CMD  For each vdev, run command CMD
+ *     -i      Display vdev initialization status.
+ *     -g      Display guid for individual vdev name.
+ *     -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
  *
  * Describes the health status of all pools or some subset.
@@ -4668,12 +7793,56 @@ zpool_do_status(int argc, char **argv)
 {
        int c;
        int ret;
-       unsigned long interval = 0, count = 0;
+       float interval = 0;
+       unsigned long count = 0;
        status_cbdata_t cb = { 0 };
+       char *cmd = NULL;
 
        /* check options */
-       while ((c = getopt(argc, argv, "vxDT:")) != -1) {
+       while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) {
                switch (c) {
+               case 'c':
+                       if (cmd != NULL) {
+                               fprintf(stderr,
+                                   gettext("Can't set -c flag twice\n"));
+                               exit(1);
+                       }
+
+                       if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
+                           !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
+                               fprintf(stderr, gettext(
+                                   "Can't run -c, disabled by "
+                                   "ZPOOL_SCRIPTS_ENABLED.\n"));
+                               exit(1);
+                       }
+
+                       if ((getuid() <= 0 || geteuid() <= 0) &&
+                           !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
+                               fprintf(stderr, gettext(
+                                   "Can't run -c with root privileges "
+                                   "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
+                               exit(1);
+                       }
+                       cmd = optarg;
+                       break;
+               case 'i':
+                       cb.cb_print_vdev_init = B_TRUE;
+                       break;
+               case 'g':
+                       cb.cb_name_flags |= VDEV_NAME_GUID;
+                       break;
+               case 'L':
+                       cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
+                       break;
+               case 'p':
+                       cb.cb_literal = B_TRUE;
+                       break;
+               case 'P':
+                       cb.cb_name_flags |= VDEV_NAME_PATH;
+                       break;
+               case 's':
+                       cb.cb_print_slow_ios = B_TRUE;
+                       break;
                case 'v':
                        cb.cb_verbose = B_TRUE;
                        break;
@@ -4683,12 +7852,20 @@ zpool_do_status(int argc, char **argv)
                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);
                        break;
                case '?':
-                       (void) fprintf(stderr, gettext("invalid option '%c'\n"),
-                           optopt);
+                       if (optopt == 'c') {
+                               print_zpool_script_list("status");
+                               exit(0);
+                       } else {
+                               fprintf(stderr,
+                                   gettext("invalid option '%c'\n"), optopt);
+                       }
                        usage(B_FALSE);
                }
        }
@@ -4702,14 +7879,22 @@ zpool_do_status(int argc, char **argv)
                cb.cb_allpools = B_TRUE;
 
        cb.cb_first = B_TRUE;
+       cb.cb_print_status = B_TRUE;
 
        for (;;) {
                if (timestamp_fmt != NODATE)
                        print_timestamp(timestamp_fmt);
 
+               if (cmd != NULL)
+                       cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
+                           NULL, NULL, 0, 0);
+
                ret = for_each_pool(argc, argv, B_TRUE, NULL,
                    status_callback, &cb);
 
+               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)
@@ -4724,7 +7909,7 @@ zpool_do_status(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
-               (void) sleep(interval);
+               (void) fsleep(interval);
        }
 
        return (0);
@@ -4740,7 +7925,7 @@ typedef struct upgrade_cbdata {
 static int
 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
 {
-       int zfs_version = (int) zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
+       int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
        int *count = (int *)unsupp_fs;
 
        if (zfs_version > ZPL_VERSION) {
@@ -4779,7 +7964,7 @@ upgrade_version(zpool_handle_t *zhp, uint64_t version)
        if (unsupp_fs) {
                (void) fprintf(stderr, gettext("Upgrade not performed due "
                    "to %d unsupported filesystems (max v%d).\n"),
-                   unsupp_fs, (int) ZPL_VERSION);
+                   unsupp_fs, (int)ZPL_VERSION);
                return (1);
        }
 
@@ -4790,12 +7975,12 @@ upgrade_version(zpool_handle_t *zhp, uint64_t version)
        if (version >= SPA_VERSION_FEATURES) {
                (void) printf(gettext("Successfully upgraded "
                    "'%s' from version %llu to feature flags.\n"),
-                   zpool_get_name(zhp), (u_longlong_t) oldversion);
+                   zpool_get_name(zhp), (u_longlong_t)oldversion);
        } else {
                (void) printf(gettext("Successfully upgraded "
                    "'%s' from version %llu to version %llu.\n"),
-                   zpool_get_name(zhp), (u_longlong_t) oldversion,
-                   (u_longlong_t) version);
+                   zpool_get_name(zhp), (u_longlong_t)oldversion,
+                   (u_longlong_t)version);
        }
 
        return (0);
@@ -5002,14 +8187,14 @@ upgrade_one(zpool_handle_t *zhp, void *data)
        if (cur_version > cbp->cb_version) {
                (void) printf(gettext("Pool '%s' is already formatted "
                    "using more current version '%llu'.\n\n"),
-                   zpool_get_name(zhp), (u_longlong_t) cur_version);
+                   zpool_get_name(zhp), (u_longlong_t)cur_version);
                return (0);
        }
 
        if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
                (void) printf(gettext("Pool '%s' is already formatted "
                    "using version %llu.\n\n"), zpool_get_name(zhp),
-                   (u_longlong_t) cbp->cb_version);
+                   (u_longlong_t)cbp->cb_version);
                return (0);
        }
 
@@ -5196,7 +8381,7 @@ zpool_do_upgrade(int argc, char **argv)
                        } else {
                                (void) printf(gettext("All pools are already "
                                    "formatted with version %llu or higher.\n"),
-                                   (u_longlong_t) cb.cb_version);
+                                   (u_longlong_t)cb.cb_version);
                        }
                }
        } else if (argc == 0) {
@@ -5287,14 +8472,14 @@ get_history_one(zpool_handle_t *zhp, void *data)
                        }
                        (void) printf("%s [internal %s txg:%lld] %s", tbuf,
                            zfs_history_event_names[ievent],
-                           (longlong_t) fnvlist_lookup_uint64(
+                           (longlong_t)fnvlist_lookup_uint64(
                            rec, ZPOOL_HIST_TXG),
                            fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
                } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
                        if (!cb->internal)
                                continue;
                        (void) printf("%s [txg:%lld] %s", tbuf,
-                           (longlong_t) fnvlist_lookup_uint64(
+                           (longlong_t)fnvlist_lookup_uint64(
                            rec, ZPOOL_HIST_TXG),
                            fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
                        if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
@@ -5321,6 +8506,11 @@ get_history_one(zpool_handle_t *zhp, void *data)
                                dump_nvlist(fnvlist_lookup_nvlist(rec,
                                    ZPOOL_HIST_OUTPUT_NVL), 8);
                        }
+                       if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
+                               (void) printf("    errno: %lld\n",
+                                   (longlong_t)fnvlist_lookup_int64(rec,
+                                   ZPOOL_HIST_ERRNO));
+                       }
                } else {
                        if (!cb->internal)
                                continue;
@@ -5405,10 +8595,11 @@ typedef struct ev_opts {
        int scripted;
        int follow;
        int clear;
+       char poolname[ZFS_MAX_DATASET_NAME_LEN];
 } ev_opts_t;
 
 static void
-zpool_do_events_short(nvlist_t *nvl)
+zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
 {
        char ctime_str[26], str[32], *ptr;
        int64_t *tv;
@@ -5421,7 +8612,10 @@ zpool_do_events_short(nvlist_t *nvl)
        (void) strncpy(str+7, ctime_str+20, 4);         /* '1993' */
        (void) strncpy(str+12, ctime_str+11, 8);        /* '21:49:08' */
        (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
-       (void) printf(gettext("%s "), str);
+       if (opts->scripted)
+               (void) printf(gettext("%s\t"), str);
+       else
+               (void) printf(gettext("%s "), str);
 
        verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
        (void) printf(gettext("%s\n"), ptr);
@@ -5500,7 +8694,20 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
 
                case DATA_TYPE_UINT64:
                        (void) nvpair_value_uint64(nvp, &i64);
-                       printf(gettext("0x%llx"), (u_longlong_t)i64);
+                       /*
+                        * translate vdev state values to readable
+                        * strings to aide zpool events consumers
+                        */
+                       if (strcmp(name,
+                           FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
+                           strcmp(name,
+                           FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
+                               printf(gettext("\"%s\" (0x%llx)"),
+                                   zpool_state_to_name(i64, VDEV_AUX_NONE),
+                                   (u_longlong_t)i64);
+                       } else {
+                               printf(gettext("0x%llx"), (u_longlong_t)i64);
+                       }
                        break;
 
                case DATA_TYPE_HRTIME:
@@ -5642,6 +8849,7 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
                case DATA_TYPE_BOOLEAN_ARRAY:
                case DATA_TYPE_BYTE_ARRAY:
                case DATA_TYPE_DOUBLE:
+               case DATA_TYPE_DONTCARE:
                case DATA_TYPE_UNKNOWN:
                        printf(gettext("<unknown>"));
                        break;
@@ -5656,6 +8864,7 @@ zpool_do_events_next(ev_opts_t *opts)
 {
        nvlist_t *nvl;
        int zevent_fd, ret, dropped;
+       char *pool;
 
        zevent_fd = open(ZFS_DEV, O_RDWR);
        VERIFY(zevent_fd >= 0);
@@ -5672,7 +8881,12 @@ zpool_do_events_next(ev_opts_t *opts)
                if (dropped > 0)
                        (void) printf(gettext("dropped %d events\n"), dropped);
 
-               zpool_do_events_short(nvl);
+               if (strlen(opts->poolname) > 0 &&
+                   nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
+                   strcmp(opts->poolname, pool) != 0)
+                       continue;
+
+               zpool_do_events_short(nvl, opts);
 
                if (opts->verbose) {
                        zpool_do_events_nvprint(nvl, 8);
@@ -5701,7 +8915,7 @@ zpool_do_events_clear(ev_opts_t *opts)
 }
 
 /*
- * zpool events [-vfc]
+ * zpool events [-vHf [pool] | -c]
  *
  * Displays events logs by ZFS.
  */
@@ -5736,6 +8950,25 @@ zpool_do_events(int argc, char **argv)
        argc -= optind;
        argv += optind;
 
+       if (argc > 1) {
+               (void) fprintf(stderr, gettext("too many arguments\n"));
+               usage(B_FALSE);
+       } else if (argc == 1) {
+               (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
+               if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
+                       (void) fprintf(stderr,
+                           gettext("invalid pool name '%s'\n"), opts.poolname);
+                       usage(B_FALSE);
+               }
+       }
+
+       if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
+           opts.clear) {
+               (void) fprintf(stderr,
+                   gettext("invalid options combined with -c\n"));
+               usage(B_FALSE);
+       }
+
        if (opts.clear)
                ret = zpool_do_events_clear(&opts);
        else
@@ -5774,7 +9007,7 @@ get_callback(zpool_handle_t *zhp, void *data)
                                    NULL, NULL);
                        }
                } else {
-                       if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
+                       if (zpool_get_prop(zhp, pl->pl_prop, value,
                            sizeof (value), &srctype, cbp->cb_literal) != 0)
                                continue;
 
@@ -5786,24 +9019,99 @@ get_callback(zpool_handle_t *zhp, void *data)
        return (0);
 }
 
+/*
+ * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
+ *
+ *     -H      Scripted mode.  Don't display headers, and separate properties
+ *             by a single tab.
+ *     -o      List of columns to display.  Defaults to
+ *             "name,property,value,source".
+ *     -p      Display values in parsable (exact) format.
+ *
+ * Get properties of pools in the system. Output space statistics
+ * for each one as well as other attributes.
+ */
 int
 zpool_do_get(int argc, char **argv)
 {
        zprop_get_cbdata_t cb = { 0 };
        zprop_list_t fake_name = { 0 };
-       int c, ret;
+       int ret;
+       int c, i;
+       char *value;
+
+       cb.cb_first = B_TRUE;
+
+       /*
+        * Set up default columns and sources.
+        */
+       cb.cb_sources = ZPROP_SRC_ALL;
+       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;
+       cb.cb_type = ZFS_TYPE_POOL;
 
        /* check options */
-       while ((c = getopt(argc, argv, "pH")) != -1) {
+       while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
                switch (c) {
                case 'p':
                        cb.cb_literal = B_TRUE;
                        break;
-
                case 'H':
                        cb.cb_scripted = B_TRUE;
                        break;
+               case 'o':
+                       bzero(&cb.cb_columns, sizeof (cb.cb_columns));
+                       i = 0;
+                       while (*optarg != '\0') {
+                               static char *col_subopts[] =
+                               { "name", "property", "value", "source",
+                               "all", NULL };
+
+                               if (i == ZFS_GET_NCOLS) {
+                                       (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:
+                                       if (i > 0) {
+                                               (void) fprintf(stderr,
+                                                   gettext("\"all\" conflicts "
+                                                   "with specific fields "
+                                                   "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);
+                               }
+                       }
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -5820,15 +9128,8 @@ zpool_do_get(int argc, char **argv)
                usage(B_FALSE);
        }
 
-       cb.cb_first = B_TRUE;
-       cb.cb_sources = ZPROP_SRC_ALL;
-       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;
-       cb.cb_type = ZFS_TYPE_POOL;
-
-       if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
+       if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
+           ZFS_TYPE_POOL) != 0)
                usage(B_FALSE);
 
        argc--;
@@ -5934,17 +9235,29 @@ find_command_idx(char *command, int *idx)
        return (1);
 }
 
+/*
+ * Display version message
+ */
+static int
+zpool_do_version(int argc, char **argv)
+{
+       if (zfs_version_print() == -1)
+               return (1);
+
+       return (0);
+}
+
 int
 main(int argc, char **argv)
 {
-       int ret;
+       int ret = 0;
        int i = 0;
        char *cmdname;
+       char **newargv;
 
        (void) setlocale(LC_ALL, "");
        (void) textdomain(TEXT_DOMAIN);
-
-       dprintf_setup(&argc, argv);
+       srand(time(NULL));
 
        opterr = 0;
 
@@ -5964,6 +9277,12 @@ main(int argc, char **argv)
        if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
                usage(B_TRUE);
 
+       /*
+        * Special case '-V|--version'
+        */
+       if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
+               return (zpool_do_version(argc, argv));
+
        if ((g_zfs = libzfs_init()) == NULL) {
                (void) fprintf(stderr, "%s", libzfs_error_init(errno));
                return (1);
@@ -5973,25 +9292,41 @@ main(int argc, char **argv)
 
        zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
 
+       /*
+        * 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]));
+       for (i = 0; i < argc; i++)
+               newargv[i] = strdup(argv[i]);
+       newargv[argc] = NULL;
+
        /*
         * Run the appropriate command.
         */
        if (find_command_idx(cmdname, &i) == 0) {
                current_command = &command_table[i];
-               ret = command_table[i].func(argc - 1, argv + 1);
+               ret = command_table[i].func(argc - 1, newargv + 1);
        } else if (strchr(cmdname, '=')) {
                verify(find_command_idx("set", &i) == 0);
                current_command = &command_table[i];
-               ret = command_table[i].func(argc, argv);
+               ret = command_table[i].func(argc, newargv);
        } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
                /*
                 * 'freeze' is a vile debugging abomination, so we treat
                 * it as such.
                 */
-               char buf[16384];
-               int fd = open(ZFS_DEV, O_RDWR);
-               (void) strcpy((void *)buf, argv[2]);
-               return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
+               zfs_cmd_t zc = {"\0"};
+
+               (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
+               ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
+               if (ret != 0) {
+                       (void) fprintf(stderr,
+                       gettext("failed to freeze pool: %d\n"), errno);
+                       ret = 1;
+               }
+
+               log_history = 0;
        } else {
                (void) fprintf(stderr, gettext("unrecognized "
                    "command '%s'\n"), cmdname);
@@ -5999,6 +9334,10 @@ main(int argc, char **argv)
                ret = 1;
        }
 
+       for (i = 0; i < argc; i++)
+               free(newargv[i]);
+       free(newargv);
+
        if (ret == 0 && log_history)
                (void) zpool_log_history(g_zfs, history_str);