]> git.proxmox.com Git - mirror_zfs.git/blobdiff - cmd/zpool/zpool_main.c
Encryption patch follow-up
[mirror_zfs.git] / cmd / zpool / zpool_main.c
index da9f3e1a337bb8169e116a5d4e4b4653e0d056d9..c61639cc24a77ddad73097ce840f70c0f6ef7ff9 100644 (file)
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
- * Copyright (c) 2012 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
  * 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.
  */
 
 #include <assert.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 <math.h>
 
 #include <libzfs.h>
 
@@ -94,6 +102,8 @@ 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 **);
+
 /*
  * These libumem hooks provide a reasonable set of defaults for the allocator's
  * debugging facilities.
@@ -137,11 +147,86 @@ typedef enum {
        HELP_GET,
        HELP_SET,
        HELP_SPLIT,
+       HELP_SYNC,
        HELP_REGUID,
        HELP_REOPEN
 } 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][11] = {
+       [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,
+           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,
+           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,
+           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,
+           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 **);
@@ -192,20 +277,22 @@ 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))
 
-zpool_command_t *current_command;
+static zpool_command_t *current_command;
 static char history_str[HIS_MAX_RECORD_LEN];
-
+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] "
@@ -221,43 +308,46 @@ get_usage(zpool_help_t idx) {
        case HELP_DETACH:
                return (gettext("\tdetach <pool> <device>\n"));
        case HELP_EXPORT:
-               return (gettext("\texport [-f] <pool> ...\n"));
+               return (gettext("\texport [-af] <pool> ...\n"));
        case HELP_HISTORY:
                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 [-d dir | -c cachefile] [-F [-n]] [-l] "
+                   "<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"));
        case HELP_IOSTAT:
-               return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
-                   "[count]]\n"));
+               return (gettext("\tiostat [[[-c [script1,script2,...]"
+                   "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
+                   "\t    [[pool ...]|[pool vdev ...]|[vdev ...]]"
+                   " [interval [count]]\n"));
        case HELP_LABELCLEAR:
                return (gettext("\tlabelclear [-f] <vdev>\n"));
        case HELP_LIST:
-               return (gettext("\tlist [-H] [-o property[,...]] "
+               return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
                    "[-T d|u] [pool] ... [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"));
        case HELP_REPLACE:
-               return (gettext("\treplace [-f] <pool> <device> "
-                   "[new-device]\n"));
+               return (gettext("\treplace [-f] [-o property=value] "
+                   "<pool> <device> [new-device]\n"));
        case HELP_REMOVE:
                return (gettext("\tremove <pool> <device> ...\n"));
        case HELP_REOPEN:
                return (gettext("\treopen <pool>\n"));
        case HELP_SCRUB:
-               return (gettext("\tscrub [-s] <pool> ...\n"));
+               return (gettext("\tscrub [-s | -p] <pool> ...\n"));
        case HELP_STATUS:
-               return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
-                   "[count]]\n"));
+               return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
+                   "[-T d|u] [pool] ... [interval [count]]\n"));
        case HELP_UPGRADE:
                return (gettext("\tupgrade\n"
                    "\tupgrade -v\n"
@@ -265,16 +355,18 @@ get_usage(zpool_help_t idx) {
        case HELP_EVENTS:
                return (gettext("\tevents [-vHfc]\n"));
        case HELP_GET:
-               return (gettext("\tget <\"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"));
        }
 
        abort();
@@ -369,7 +461,7 @@ usage(boolean_t requested)
 
 void
 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
-    boolean_t print_logs)
+    boolean_t print_logs, int name_flags)
 {
        nvlist_t **child;
        uint_t c, children;
@@ -390,9 +482,9 @@ print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
                if ((is_log && !print_logs) || (!is_log && print_logs))
                        continue;
 
-               vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
+               vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
                print_vdev_tree(zhp, vname, child[c], indent + 2,
-                   B_FALSE);
+                   B_FALSE, name_flags);
                free(vname);
        }
 }
@@ -485,12 +577,30 @@ add_prop_list(const char *propname, char *propval, nvlist_t **props,
 }
 
 /*
- * zpool add [-fn] [-o property=value] <pool> <vdev> ...
+ * Set a default property pair (name, string-value) in a property nvlist
+ */
+static int
+add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
+    boolean_t poolprop)
+{
+       char *pval;
+
+       if (nvlist_lookup_string(*props, propname, &pval) == 0)
+               return (0);
+
+       return (add_prop_list(propname, propval, props, B_TRUE));
+}
+
+/*
+ * 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
@@ -501,6 +611,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;
@@ -511,11 +622,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;
@@ -532,6 +649,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);
@@ -567,6 +687,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);
@@ -577,6 +711,10 @@ zpool_do_add(int argc, char **argv)
 
        if (dryrun) {
                nvlist_t *poolnvroot;
+               nvlist_t **l2child;
+               uint_t l2children, c;
+               char *vname;
+               boolean_t hadcache = B_FALSE;
 
                verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
                    &poolnvroot) == 0);
@@ -585,15 +723,43 @@ 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);
+               print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE,
+                   name_flags);
+               print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE, name_flags);
 
                /* 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);
+                       print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE,
+                           name_flags);
+                       print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE,
+                           name_flags);
                } else if (num_logs(nvroot) > 0) {
-                       print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
+                       print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE,
+                           name_flags);
+               }
+
+               /* Do the same for the caches */
+               if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
+                   &l2child, &l2children) == 0 && l2children) {
+                       hadcache = B_TRUE;
+                       (void) printf(gettext("\tcache\n"));
+                       for (c = 0; c < l2children; c++) {
+                               vname = zpool_vdev_name(g_zfs, NULL,
+                                   l2child[c], name_flags);
+                               (void) printf("\t  %s\n", vname);
+                               free(vname);
+                       }
+               }
+               if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
+                   &l2child, &l2children) == 0 && l2children) {
+                       if (!hadcache)
+                               (void) printf(gettext("\tcache\n"));
+                       for (c = 0; c < l2children; c++) {
+                               vname = zpool_vdev_name(g_zfs, NULL,
+                                   l2child[c], name_flags);
+                               (void) printf("\t  %s\n", vname);
+                               free(vname);
+                       }
                }
 
                ret = 0;
@@ -619,7 +785,7 @@ zpool_do_remove(int argc, char **argv)
 {
        char *poolname;
        int i, ret = 0;
-       zpool_handle_t *zhp;
+       zpool_handle_t *zhp = NULL;
 
        argc--;
        argv++;
@@ -643,12 +809,16 @@ zpool_do_remove(int argc, char **argv)
                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.
@@ -656,8 +826,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;
@@ -680,90 +853,111 @@ 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);
        }
+       if (argc > 1) {
+               (void) fprintf(stderr, gettext("too many arguments\n"));
+               usage(B_FALSE);
+       }
+
+       /*
+        * 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;
+               }
+
+               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);
+               }
+       }
 
-       vdev = argv[0];
        if ((fd = open(vdev, O_RDWR)) < 0) {
-               (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
-               return (B_FALSE);
+               (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
+                   vdev, strerror(errno));
+               return (1);
        }
 
-       name = NULL;
-       if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
-               if (force)
-                       goto wipe_label;
+       if (ioctl(fd, BLKFLSBUF) != 0)
+               (void) fprintf(stderr, gettext("failed to invalidate "
+                   "cache for %s: %s\n"), vdev, strerror(errno));
 
+       if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) {
                (void) fprintf(stderr,
-                   gettext("Unable to determine pool state for %s\n"
-                   "Use -f to force the clearing any label data\n"), vdev);
-
+                   gettext("failed to check state for %s\n"), vdev);
                return (1);
        }
+       nvlist_free(config);
 
-       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;
-
-               case POOL_STATE_EXPORTED:
-                       if (force)
-                               break;
+       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);
+               return (1);
+       }
 
-                       (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;
+       if (!inuse)
+               goto wipe_label;
 
-               case POOL_STATE_POTENTIALLY_ACTIVE:
-                       if (force)
-                               break;
+       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;
 
-                       (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;
+       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_DESTROYED:
-                       /* inuse should never be set for a destroyed pool... */
+       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);
 }
@@ -780,6 +974,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
@@ -798,6 +993,7 @@ zpool_do_create(int argc, char **argv)
        int c;
        nvlist_t *nvroot = NULL;
        char *poolname;
+       char *tname = NULL;
        int ret = 1;
        char *altroot = NULL;
        char *mountpoint = NULL;
@@ -806,7 +1002,7 @@ zpool_do_create(int argc, char **argv)
        char *propval;
 
        /* check options */
-       while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
+       while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
                switch (c) {
                case 'f':
                        force = B_TRUE;
@@ -822,15 +1018,12 @@ zpool_do_create(int argc, char **argv)
                        if (add_prop_list(zpool_prop_to_name(
                            ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
                                goto errout;
-                       if (nvlist_lookup_string(props,
-                           zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
-                           &propval) == 0)
-                               break;
-                       if (add_prop_list(zpool_prop_to_name(
+                       if (add_prop_list_default(zpool_prop_to_name(
                            ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
                                goto errout;
                        break;
                case 'm':
+                       /* Equivalent to -O mountpoint=optarg */
                        mountpoint = optarg;
                        break;
                case 'o':
@@ -859,6 +1052,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) {
@@ -869,8 +1064,39 @@ zpool_do_create(int argc, char **argv)
                        *propval = '\0';
                        propval++;
 
-                       if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
+                       /*
+                        * Mountpoints are checked and then added later.
+                        * Uniquely among properties, they can be specified
+                        * more than once, to avoid conflict with -m.
+                        */
+                       if (0 == strcmp(optarg,
+                           zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
+                               mountpoint = propval;
+                       } else if (add_prop_list(optarg, propval, &fsprops,
+                           B_FALSE)) {
+                               goto errout;
+                       }
+                       break;
+               case 't':
+                       /*
+                        * Sanity check temporary pool name.
+                        */
+                       if (strchr(optarg, '/') != NULL) {
+                               (void) fprintf(stderr, gettext("cannot create "
+                                   "'%s': invalid character '/' in temporary "
+                                   "name\n"), optarg);
+                               (void) fprintf(stderr, gettext("use 'zfs "
+                                   "create' to create a dataset\n"));
+                               goto errout;
+                       }
+
+                       if (add_prop_list(zpool_prop_to_name(
+                           ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
+                               goto errout;
+                       if (add_prop_list_default(zpool_prop_to_name(
+                           ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
                                goto errout;
+                       tname = optarg;
                        break;
                case ':':
                        (void) fprintf(stderr, gettext("missing argument for "
@@ -987,6 +1213,18 @@ zpool_do_create(int argc, char **argv)
                }
        }
 
+       /*
+        * Now that the mountpoint's validity has been checked, ensure that
+        * the property is set appropriately prior to creating the pool.
+        */
+       if (mountpoint != NULL) {
+               ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
+                   mountpoint, &fsprops, B_FALSE);
+               if (ret != 0)
+                       goto errout;
+       }
+
+       ret = 1;
        if (dryrun) {
                /*
                 * For a dry run invocation, print out a basic message and run
@@ -996,46 +1234,48 @@ 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);
+               print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE, 0);
                if (num_logs(nvroot) > 0)
-                       print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
+                       print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE, 0);
 
                ret = 0;
        } else {
                /*
                 * Hand off to libzfs.
                 */
-               if (enable_all_pool_feat) {
-                       int 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);
 
-                               if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
-                                   &props, B_TRUE) != 0)
+                       /*
+                        * 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)
                                        goto errout;
                        }
                }
+
+               ret = 1;
                if (zpool_create(g_zfs, poolname,
                    nvroot, props, fsprops) == 0) {
-                       zfs_handle_t *pool = zfs_open(g_zfs, poolname,
-                           ZFS_TYPE_FILESYSTEM);
+                       zfs_handle_t *pool = zfs_open(g_zfs,
+                           tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
                        if (pool != NULL) {
-                               if (mountpoint != NULL)
-                                       verify(zfs_prop_set(pool,
-                                           zfs_prop_to_name(
-                                           ZFS_PROP_MOUNTPOINT),
-                                           mountpoint) == 0);
                                if (zfs_mount(pool, NULL, 0) == 0)
                                        ret = zfs_shareall(pool);
                                zfs_close(pool);
@@ -1116,19 +1356,53 @@ 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);
        }
 
-       ret = (zpool_destroy(zhp) != 0);
+       /* The history must be logged as part of the export */
+       log_history = B_FALSE;
+
+       ret = (zpool_destroy(zhp, history_str) != 0);
 
        zpool_close(zhp);
 
        return (ret);
 }
 
+typedef struct export_cbdata {
+       boolean_t force;
+       boolean_t hardforce;
+} export_cbdata_t;
+
+/*
+ * Export one pool
+ */
+int
+zpool_export_one(zpool_handle_t *zhp, void *data)
+{
+       export_cbdata_t *cb = data;
+
+       if (zpool_disable_datasets(zhp, cb->force) != 0)
+               return (1);
+
+       /* The history must be logged as part of the export */
+       log_history = B_FALSE;
+
+       if (cb->hardforce) {
+               if (zpool_export_force(zhp, history_str) != 0)
+                       return (1);
+       } else if (zpool_export(zhp, cb->force, history_str) != 0) {
+               return (1);
+       }
+
+       return (0);
+}
+
 /*
  * zpool export [-f] <pool> ...
  *
+ *     -a      Export all pools
  *     -f      Forcefully unmount datasets
  *
  * Export the given pools.  By default, the command will attempt to cleanly
@@ -1138,16 +1412,18 @@ zpool_do_destroy(int argc, char **argv)
 int
 zpool_do_export(int argc, char **argv)
 {
+       export_cbdata_t cb;
+       boolean_t do_all = B_FALSE;
        boolean_t force = B_FALSE;
        boolean_t hardforce = B_FALSE;
-       int c;
-       zpool_handle_t *zhp;
-       int ret;
-       int i;
+       int c, ret;
 
        /* check options */
-       while ((c = getopt(argc, argv, "fF")) != -1) {
+       while ((c = getopt(argc, argv, "afF")) != -1) {
                switch (c) {
+               case 'a':
+                       do_all = B_TRUE;
+                       break;
                case 'f':
                        force = B_TRUE;
                        break;
@@ -1161,37 +1437,28 @@ zpool_do_export(int argc, char **argv)
                }
        }
 
+       cb.force = force;
+       cb.hardforce = hardforce;
        argc -= optind;
        argv += optind;
 
+       if (do_all) {
+               if (argc != 0) {
+                       (void) fprintf(stderr, gettext("too many arguments\n"));
+                       usage(B_FALSE);
+               }
+
+               return (for_each_pool(argc, argv, B_TRUE, NULL,
+                   zpool_export_one, &cb));
+       }
+
        /* check arguments */
        if (argc < 1) {
                (void) fprintf(stderr, gettext("missing pool argument\n"));
                usage(B_FALSE);
        }
 
-       ret = 0;
-       for (i = 0; i < argc; i++) {
-               if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
-                       ret = 1;
-                       continue;
-               }
-
-               if (zpool_disable_datasets(zhp, force) != 0) {
-                       ret = 1;
-                       zpool_close(zhp);
-                       continue;
-               }
-
-               if (hardforce) {
-                       if (zpool_export_force(zhp) != 0)
-                               ret = 1;
-               } else if (zpool_export(zhp, force) != 0) {
-                       ret = 1;
-               }
-
-               zpool_close(zhp);
-       }
+       ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
 
        return (ret);
 }
@@ -1201,13 +1468,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;
 
@@ -1217,7 +1486,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;
        }
 
@@ -1225,7 +1494,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;
        }
 
@@ -1233,11 +1502,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);
 }
 
@@ -1286,12 +1554,90 @@ find_spare(zpool_handle_t *zhp, void *data)
        return (0);
 }
 
+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_explain;
+       boolean_t       cb_first;
+       boolean_t       cb_dedup_stats;
+       boolean_t       cb_print_status;
+       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)
+{
+       while (str != NULL && *str != '\0') {
+               if (!isblank(*str))
+                       return (0);
+               str++;
+       }
+       return (1);
+}
+
+/* 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;
+               }
+
+               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 = "-";
+
+                       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 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)
+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;
        uint_t c, children;
@@ -1300,8 +1646,9 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
        char rbuf[6], wbuf[6], cbuf[6];
        char *vname;
        uint64_t notpresent;
-       spare_cbdata_t cb;
+       spare_cbdata_t spare_cb;
        char *state;
+       char *path = NULL;
 
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
            &child, &children) != 0)
@@ -1322,7 +1669,7 @@ 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) {
@@ -1334,7 +1681,6 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
 
        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) {
@@ -1363,17 +1709,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"));
                        }
@@ -1399,6 +1745,10 @@ 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;
+
                default:
                        (void) printf(gettext("corrupted data"));
                        break;
@@ -1415,6 +1765,13 @@ print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                    "resilvering" : "repairing");
        }
 
+       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);
+               }
+       }
+
        (void) printf("\n");
 
        for (c = 0; c < children; c++) {
@@ -1427,20 +1784,21 @@ 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);
+               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;
@@ -1455,7 +1813,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) {
@@ -1486,6 +1844,10 @@ 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;
+
                default:
                        (void) printf(gettext("corrupted data"));
                        break;
@@ -1505,8 +1867,9 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
                if (is_log)
                        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);
        }
 
@@ -1514,7 +1877,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);
                }
@@ -1524,7 +1888,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);
                }
@@ -1540,7 +1905,7 @@ print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
  * works because only the top level vdev is marked "is_log"
  */
 static void
-print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
+print_logs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv)
 {
        uint_t c, children;
        nvlist_t **child;
@@ -1559,12 +1924,13 @@ print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
                    &is_log);
                if (!is_log)
                        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);
+               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);
        }
 }
@@ -1579,13 +1945,16 @@ show_import(nvlist_t *config)
        vdev_stat_t *vs;
        char *name;
        uint64_t guid;
+       uint64_t hostid = 0;
        char *msgid;
-       nvlist_t *nvroot;
-       int reason;
+       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);
@@ -1600,7 +1969,7 @@ show_import(nvlist_t *config)
            (uint64_t **)&vs, &vsc) == 0);
        health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
 
-       reason = zpool_import_status(config, &msgid);
+       reason = zpool_import_status(config, &msgid, &errata);
 
        (void) printf(gettext("   pool: %s\n"), name);
        (void) printf(gettext("     id: %llu\n"), (u_longlong_t)guid);
@@ -1655,7 +2024,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;
 
@@ -1667,6 +2036,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"));
@@ -1688,6 +2068,11 @@ show_import(nvlist_t *config)
                    "resilvered.\n"));
                break;
 
+       case ZPOOL_STATUS_ERRATA:
+               (void) printf(gettext(" status: Errata #%d detected.\n"),
+                   errata);
+               break;
+
        default:
                /*
                 * No other status can be seen when importing pools.
@@ -1709,6 +2094,34 @@ show_import(nvlist_t *config)
                        (void) printf(gettext(" action: The pool can be "
                            "imported using its name or numeric "
                            "identifier and\n\tthe '-f' flag.\n"));
+               } else if (reason == ZPOOL_STATUS_ERRATA) {
+                       switch (errata) {
+                       case ZPOOL_ERRATA_NONE:
+                               break;
+
+                       case ZPOOL_ERRATA_ZOL_2094_SCRUB:
+                               (void) printf(gettext(" action: The pool can "
+                                   "be imported using its name or numeric "
+                                   "identifier,\n\thowever there is a compat"
+                                   "ibility issue which should be corrected"
+                                   "\n\tby running 'zpool scrub'\n"));
+                               break;
+
+                       case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
+                               (void) printf(gettext(" action: The pool can"
+                                   "not be imported with this version of ZFS "
+                                   "due to\n\tan active asynchronous destroy. "
+                                   "Revert to an earlier version\n\tand "
+                                   "allow the destroy to complete before "
+                                   "updating.\n"));
+                               break;
+
+                       default:
+                               /*
+                                * All errata must contain an action message.
+                                */
+                               assert(0);
+                       }
                } else {
                        (void) printf(gettext(" action: The pool can be "
                            "imported using its name or numeric "
@@ -1747,6 +2160,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"));
@@ -1779,13 +2213,13 @@ 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, 0, VDEV_NAME_TYPE_ID);
+       if (cb.cb_namewidth < 10)
+               cb.cb_namewidth = 10;
 
-       print_import_config(name, nvroot, namewidth, 0);
+       print_import_config(&cb, name, nvroot, 0);
        if (num_logs(nvroot) > 0)
-               print_logs(NULL, nvroot, namewidth, B_FALSE);
+               print_logs(NULL, &cb, nvroot);
 
        if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
                (void) printf(gettext("\n\tAdditional devices are known to "
@@ -1794,66 +2228,112 @@ show_import(nvlist_t *config)
        }
 }
 
-/*
- * Perform the import for the given configuration.  This passes the heavy
- * lifting off to zpool_import_props(), and then mounts the datasets contained
- * within the pool.
- */
-static int
-do_import(nvlist_t *config, const char *newname, const char *mntopts,
-    nvlist_t *props, int flags)
+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
+ * within the pool.
+ */
+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);
+       state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
+       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;
-
-               if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
-                   &hostid) == 0) {
-                       unsigned long system_hostid = gethostid() & 0xffffffff;
-
-                       if ((unsigned long)hostid != system_hostid) {
-                               char *hostname;
-                               uint64_t timestamp;
-                               time_t t;
-
-                               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);
-                       }
+               mmp_state_t mmp_state = MMP_STATE_INACTIVE;
+               nvlist_t *nvinfo;
+
+               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 (mmp_state == MMP_STATE_ACTIVE) {
+                       char *hostname = "<unknown>";
+                       uint64_t hostid = 0;
+
+                       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 may be in use from other system\n"), name);
-                       (void) fprintf(stderr, gettext("use '-f' to import "
-                           "anyway\n"));
-                       return (1);
+                           "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)
@@ -1865,6 +2345,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) {
@@ -1873,14 +2363,14 @@ do_import(nvlist_t *config, const char *newname, const char *mntopts,
        }
 
        zpool_close(zhp);
-       return (0);
+       return (ret);
 }
 
 /*
  * 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
@@ -1915,8 +2405,13 @@ 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.
+ *
  * 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.
  */
@@ -1946,13 +2441,14 @@ 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;
        uint64_t pool_state, txg = -1ULL;
        char *cachefile = NULL;
        importargs_t idata = { 0 };
        char *endptr;
 
        /* check options */
-       while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
+       while ((c = getopt(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
                switch (c) {
                case 'a':
                        do_all = B_TRUE;
@@ -1982,6 +2478,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;
@@ -2006,17 +2505,23 @@ zpool_do_import(int argc, char **argv)
                        if (add_prop_list(zpool_prop_to_name(
                            ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
                                goto error;
-                       if (nvlist_lookup_string(props,
-                           zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
-                           &propval) == 0)
-                               break;
-                       if (add_prop_list(zpool_prop_to_name(
+                       if (add_prop_list_default(zpool_prop_to_name(
+                           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(
                            ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
                                goto error;
                        break;
+
                case 'T':
                        errno = 0;
-                       txg = strtoull(optarg, &endptr, 10);
+                       txg = strtoull(optarg, &endptr, 0);
                        if (errno != 0 || *endptr != '\0') {
                                (void) fprintf(stderr,
                                    gettext("invalid txg value\n"));
@@ -2050,6 +2555,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"));
@@ -2079,21 +2595,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);
        }
 
        /*
@@ -2114,8 +2630,10 @@ zpool_do_import(int argc, char **argv)
 
                errno = 0;
                searchguid = strtoull(argv[0], &endptr, 10);
-               if (errno != 0 || *endptr != '\0')
+               if (errno != 0 || *endptr != '\0') {
                        searchname = argv[0];
+                       searchguid = 0;
+               }
                found_config = NULL;
 
                /*
@@ -2154,6 +2672,7 @@ zpool_do_import(int argc, char **argv)
        idata.poolname = searchname;
        idata.guid = searchguid;
        idata.cachefile = cachefile;
+       idata.scan = do_scan;
 
        pools = zpool_search_import(g_zfs, &idata);
 
@@ -2187,6 +2706,8 @@ zpool_do_import(int argc, char **argv)
                if (envdup != NULL)
                        free(envdup);
                nvlist_free(policy);
+               nvlist_free(pools);
+               nvlist_free(props);
                return (1);
        }
 
@@ -2294,183 +2815,1024 @@ error:
        return (err ? 1 : 0);
 }
 
+/*
+ * 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 {
-       boolean_t cb_verbose;
+       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;
 
-static void
-print_iostat_separator(iostat_cbdata_t *cb)
+/*  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       11      /* 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"}},
+       [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
+           {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
+           {NULL}},
+       [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2},
+           {"sync_queue", 2}, {"async_queue", 2}, {NULL}},
+       [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
+           {"async_read", 2}, {"async_write", 2}, {"scrub", 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"}, {NULL}},
+       [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
+           {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
+       [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
+           {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}},
+       [IOS_RQ_HISTO] = {{"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;
 
-       for (i = 0; i < cb->cb_namewidth; i++)
-               (void) printf("-");
-       (void) printf("  -----  -----  -----  -----  -----  -----\n");
+       while (labels[i].name)
+               i++;
+
+       return (i);
 }
 
-static void
-print_iostat_header(iostat_cbdata_t *cb)
+/*
+ * 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[])
 {
-       (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);
+       uint64_t i = 0;
+       while (array[i])
+               i++;
+
+       return (i);
 }
 
+
 /*
- * Display a single statistic.
+ * Return a default column width for default/latency/queue columns. This does
+ * not include histograms, which have their columns autosized.
  */
-static void
-print_one_stat(uint64_t value)
+static unsigned int
+default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
 {
-       char buf[64];
+       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 */
+       };
 
-       zfs_nicenum(value, buf, sizeof (buf));
-       (void) printf("  %5s", buf);
+       if (cb->cb_literal)
+               column_width = widths[type];
+
+       return (column_width);
 }
 
 /*
- * 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.
+ * 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_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
-    nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
+print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
+    const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
 {
-       nvlist_t **oldchild, **newchild;
-       uint_t c, children;
-       vdev_stat_t *oldvs, *newvs;
-       vdev_stat_t zerovs = { 0 };
-       uint64_t tdelta;
-       double scale;
-       char *vname;
+       int i, idx, s;
+       unsigned 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);
 
-       if (oldnv != NULL) {
-               verify(nvlist_lookup_uint64_array(oldnv,
-                   ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
-       } else {
-               oldvs = &zerovs;
-       }
+                       rw_column_width = (column_width * columns) +
+                           (2 * (columns - 1));
 
-       verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
-           (uint64_t **)&newvs, &c) == 0);
+                       text_start = (int)((rw_column_width)/columns -
+                           slen/columns);
 
-       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), "");
+                       printf("  ");   /* Two spaces between columns */
 
-       tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
+                       /* Space from beginning of column to label */
+                       for (s = 0; s < text_start; s++)
+                               printf(" ");
 
-       if (tdelta == 0)
-               scale = 1.0;
-       else
-               scale = (double)NANOSEC / tdelta;
+                       printf("%s", name);
 
-       /* only toplevel vdevs have capacity stats */
-       if (newvs->vs_space == 0) {
-               (void) printf("      -      -");
-       } else {
-               print_one_stat(newvs->vs_alloc);
-               print_one_stat(newvs->vs_space - newvs->vs_alloc);
+                       /* Print space after label to end of column */
+                       spaces_to_end = rw_column_width - text_start - slen;
+                       for (s = 0; s < spaces_to_end; s++)
+                               printf(" ");
+
+               }
        }
+}
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
-           oldvs->vs_ops[ZIO_TYPE_READ])));
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
-           oldvs->vs_ops[ZIO_TYPE_WRITE])));
+/*
+ * 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];
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
-           oldvs->vs_bytes[ZIO_TYPE_READ])));
+       if (vcdl->count == 0 || data == NULL)
+               return;
 
-       print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
-           oldvs->vs_bytes[ZIO_TYPE_WRITE])));
+       /*
+        * 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]);
+               }
+       }
+}
 
-       (void) printf("\n");
 
-       if (!cb->cb_verbose)
-               return;
+/*
+ * 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 (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
-           &newchild, &children) != 0)
-               return;
 
-       if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
-           &oldchild, &c) != 0)
-               return;
+       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";
+       }
 
-       for (c = 0; c < children; c++) {
-               uint64_t ishole = B_FALSE, islog = B_FALSE;
+       namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
+           name ? strlen(name) : 0);
 
-               (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
-                   &ishole);
 
-               (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
-                   &islog);
+       if (name) {
+               printf("%-*s", namewidth, name);
+       } else {
+               for (i = 0; i < namewidth; i++)
+                       (void) printf("-");
+       }
 
-               if (ishole || islog)
-                       continue;
+       /* 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);
 
-               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);
+               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,
+                                   "--------------------");
+               }
        }
+}
 
-       /*
-        * Log device section
-        */
 
-       if (num_logs(newnv) > 0) {
-               (void) printf("%-*s      -      -      -      -      -      "
-                   "-\n", cb->cb_namewidth, "logs");
+static void
+print_iostat_separator_impl(iostat_cbdata_t *cb,
+    unsigned int force_column_width)
+{
+       print_iostat_dashes(cb, force_column_width, NULL);
+}
 
-               for (c = 0; c < children; c++) {
-                       uint64_t islog = B_FALSE;
-                       (void) nvlist_lookup_uint64(newchild[c],
-                           ZPOOL_CONFIG_IS_LOG, &islog);
+static void
+print_iostat_separator(iostat_cbdata_t *cb)
+{
+       print_iostat_separator_impl(cb, 0);
+}
 
-                       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);
-                       }
-               }
+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";
        }
 
-       /*
-        * Include level 2 ARC devices in iostat output
-        */
-       if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
-           &newchild, &children) != 0)
-               return;
+       namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
+           histo_vdev_name ? strlen(histo_vdev_name) : 0);
 
-       if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
-           &oldchild, &c) != 0)
-               return;
+       if (histo_vdev_name)
+               printf("%-*s", namewidth, histo_vdev_name);
+       else
+               printf("%*s", namewidth, "");
 
-       if (children > 0) {
-               (void) printf("%-*s      -      -      -      -      -      "
-                   "-\n", cb->cb_namewidth, "cache");
-               for (c = 0; c < children; c++) {
+
+       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)
+{
+       print_iostat_header_impl(cb, 0, NULL);
+}
+
+
+/*
+ * Display a single statistic.
+ */
+static void
+print_one_stat(uint64_t value, enum zfs_nicenum_format format,
+    unsigned int column_size, boolean_t scripted)
+{
+       char buf[64];
+
+       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, double scale)
+{
+       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,
+       };
+
+       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] * scale;
+               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, double scale)
+{
+       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,
+       };
+       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) * scale;
+               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);
+}
+
+/*
+ * 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.
+ */
+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, *calcvs;
+       vdev_stat_t zerovs = { 0 };
+       char *vname;
+       int i;
+       int ret = 0;
+       uint64_t tdelta;
+       double scale;
+
+       calcvs = safe_malloc(sizeof (*calcvs));
+
+       if (oldnv != NULL) {
+               verify(nvlist_lookup_uint64_array(oldnv,
+                   ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
+       } else {
+               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);
+
+       /*
+        * 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 ((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 {
+               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, scale);
+       if (cb->cb_flags & IOS_QUEUES_M)
+               print_iostat_queues(cb, oldnv, newnv, scale);
+       if (cb->cb_flags & IOS_ANYHISTO_M) {
+               printf("\n");
+               print_iostat_histos(cb, oldnv, newnv, scale, name);
+       }
+
+       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);
+               }
+       }
+
+       if (!(cb->cb_flags & IOS_ANYHISTO_M))
+               printf("\n");
+
+       ret++;
+
+children:
+
+       free(calcvs);
+
+       if (!cb->cb_verbose)
+               return (ret);
+
+       if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
+           &newchild, &children) != 0)
+               return (ret);
+
+       if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
+           &oldchild, &c) != 0)
+               return (ret);
+
+       for (c = 0; c < children; c++) {
+               uint64_t ishole = B_FALSE, islog = B_FALSE;
+
+               (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
+                   &ishole);
+
+               (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
+                   &islog);
+
+               if (ishole || islog)
+                       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
+        */
+
+       if (num_logs(newnv) > 0) {
+               if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
+                   !cb->cb_vdev_names) {
+                       print_iostat_dashes(cb, 0, "logs");
+               }
+               printf("\n");
+
+               for (c = 0; c < children; c++) {
+                       uint64_t islog = B_FALSE;
+                       (void) nvlist_lookup_uint64(newchild[c],
+                           ZPOOL_CONFIG_IS_LOG, &islog);
+
+                       if (islog) {
+                               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);
+                       }
+               }
+
+       }
+
+       /*
+        * Include level 2 ARC devices in iostat output
+        */
+       if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
+           &newchild, &children) != 0)
+               return (ret);
+
+       if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
+           &oldchild, &c) != 0)
+               return (ret);
+
+       if (children > 0) {
+               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
@@ -2500,6 +3862,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);
 
@@ -2515,152 +3878,502 @@ 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);
+       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 (ret);
+}
+
+static int
+get_columns(void)
+{
+       struct winsize ws;
+       int columns = 80;
+       int error;
+
+       if (isatty(STDOUT_FILENO)) {
+               error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
+               if (error == 0)
+                       columns = ws.ws_col;
+       } else {
+               columns = 999;
+       }
+
+       return (columns);
+}
+
+int
+get_namewidth(zpool_handle_t *zhp, void *data)
+{
+       iostat_cbdata_t *cb = data;
+       nvlist_t *config, *nvroot;
+       int columns;
+
+       if ((config = zpool_get_config(zhp, NULL)) != NULL) {
+               verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
+                   &nvroot) == 0);
+               unsigned int poolname_len = strlen(zpool_get_name(zhp));
+               if (!cb->cb_verbose)
+                       cb->cb_namewidth = poolname_len;
+               else
+                       cb->cb_namewidth = MAX(poolname_len,
+                           max_width(zhp, nvroot, 0, cb->cb_namewidth,
+                           cb->cb_name_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);
+}
+
+/*
+ * Parse the input string, get the 'interval' and 'count' value if there is one.
+ */
+static void
+get_interval_count(int *argcp, char **argv, float *iv,
+    unsigned long *cnt)
+{
+       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 && isnumber(argv[argc - 1])) {
+               char *end;
+
+               errno = 0;
+               interval = strtof(argv[argc - 1], &end);
+
+               if (*end == '\0' && errno == 0) {
+                       if (interval == 0) {
+                               (void) fprintf(stderr, gettext("interval "
+                                   "cannot be zero\n"));
+                               usage(B_FALSE);
+                       }
+                       /*
+                        * Ignore the last parameter
+                        */
+                       argc--;
+               } else {
+                       /*
+                        * If this is not a valid number, just plow on.  The
+                        * user will get a more informative error message later
+                        * on.
+                        */
+                       interval = 0;
+               }
+       }
+
+       /*
+        * If the last argument is also an integer, then we have both a count
+        * and an interval.
+        */
+       if (argc > 0 && isnumber(argv[argc - 1])) {
+               char *end;
+
+               errno = 0;
+               count = interval;
+               interval = strtof(argv[argc - 1], &end);
+
+               if (*end == '\0' && errno == 0) {
+                       if (interval == 0) {
+                               (void) fprintf(stderr, gettext("interval "
+                                   "cannot be zero\n"));
+                               usage(B_FALSE);
+                       }
+
+                       /*
+                        * Ignore the last parameter
+                        */
+                       argc--;
+               } else {
+                       interval = 0;
+               }
+       }
+
+       *iv = interval;
+       *cnt = count;
+       *argcp = argc;
+}
+
+static void
+get_timestamp_arg(char c)
+{
+       if (c == 'u')
+               timestamp_fmt = UDATE;
+       else if (c == 'd')
+               timestamp_fmt = DDATE;
+       else
+               usage(B_FALSE);
+}
+
+/*
+ * 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;
+               }
+       }
 
-       if (cb->cb_verbose)
-               print_iostat_separator(cb);
+       cb->cb_vdev_names = tmp_name;
 
-       return (0);
+       return (ret);
 }
 
 static int
-get_columns(void)
+is_pool_cb(zpool_handle_t *zhp, void *data)
 {
-       struct winsize ws;
-       int columns = 80;
-       int error;
+       char *name = data;
+       if (strcmp(name, zpool_get_name(zhp)) == 0)
+               return (1);
 
-       if (isatty(STDOUT_FILENO)) {
-               error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
-               if (error == 0)
-                       columns = ws.ws_col;
-       } else {
-               columns = 999;
-       }
+       return (0);
+}
 
-       return columns;
+/*
+ * 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));
 }
 
-int
-get_namewidth(zpool_handle_t *zhp, void *data)
+/* Are all our argv[] strings pool names?  If so return 1, 0 otherwise. */
+static int
+are_all_pools(int argc, char **argv)
 {
-       iostat_cbdata_t *cb = data;
-       nvlist_t *config, *nvroot;
-       int columns;
+       if ((argc == 0) || !*argv)
+               return (0);
 
-       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);
-       }
+       while (--argc >= 0)
+               if (!is_pool(argv[argc]))
+                       return (0);
 
-       /*
-        * 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();
+       return (1);
+}
 
-       if (cb->cb_namewidth < 10)
-               cb->cb_namewidth = 10;
-       if (cb->cb_namewidth > columns - 42)
-               cb->cb_namewidth = columns - 42;
+/*
+ * 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");
 
-       return (0);
+               fprintf(stderr, "\t%s (%s)\n", name, str);
+       }
 }
 
 /*
- * Parse the input string, get the 'interval' and 'count' value if there is one.
+ * 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(int *argcp, char **argv, unsigned long *iv,
-    unsigned long *cnt)
+get_interval_count_filter_guids(int *argc, char **argv, float *interval,
+    unsigned long *count, iostat_cbdata_t *cb)
 {
-       unsigned long interval = 0, count = 0;
-       int argc = *argcp;
+       char **tmpargv = argv;
+       int argc_for_interval = 0;
 
-       /*
-        * Determine if the last argument is an integer or a pool name
-        */
-       if (argc > 0 && isdigit(argv[argc - 1][0])) {
-               char *end;
-
-               errno = 0;
-               interval = strtoul(argv[argc - 1], &end, 10);
+       /* 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 (*end == '\0' && errno == 0) {
-                       if (interval == 0) {
-                               (void) fprintf(stderr, gettext("interval "
-                                   "cannot be zero\n"));
-                               usage(B_FALSE);
-                       }
-                       /*
-                        * Ignore the last parameter
-                        */
-                       argc--;
-               } else {
+               if (*argc >= 2 &&
+                   !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) {
                        /*
-                        * If this is not a valid number, just plow on.  The
-                        * user will get a more informative error message later
-                        * on.
+                        * The 2nd to last arg is not a guid, so it's probably
+                        * an interval value.
                         */
-                       interval = 0;
+                       argc_for_interval++;
                }
        }
 
-       /*
-        * 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])) {
-               char *end;
+       /* Point to our list of possible intervals */
+       tmpargv = &argv[*argc - argc_for_interval];
 
-               errno = 0;
-               count = interval;
-               interval = strtoul(argv[argc - 1], &end, 10);
+       *argc = *argc - argc_for_interval;
+       get_interval_count(&argc_for_interval, tmpargv,
+           interval, count);
+}
 
-               if (*end == '\0' && errno == 0) {
-                       if (interval == 0) {
-                               (void) fprintf(stderr, gettext("interval "
-                                   "cannot be zero\n"));
-                               usage(B_FALSE);
-                       }
+/*
+ * 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);
+}
 
-                       /*
-                        * Ignore the last parameter
-                        */
-                       argc--;
-               } else {
-                       interval = 0;
-               }
+/*
+ * 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;
        }
 
-       *iv = interval;
-       *cnt = count;
-       *argcp = argc;
+       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
-get_timestamp_arg(char c)
+print_zpool_dir_scripts(char *dirpath)
 {
-       if (c == 'u')
-               timestamp_fmt = UDATE;
-       else if (c == 'd')
-               timestamp_fmt = DDATE;
-       else
-               usage(B_FALSE);
+       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);
 }
 
 /*
- * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
+ * 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
  *
  * This command can be tricky because we want to be able to deal with pool
@@ -2675,23 +4388,100 @@ 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;
        zpool_list_t *list;
        boolean_t verbose = B_FALSE;
-       iostat_cbdata_t cb;
+       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;
+       boolean_t guid = B_FALSE;
+       boolean_t follow_links = B_FALSE;
+       boolean_t full_name = 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:v")) != -1) {
+       while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -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 '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);
                }
        }
@@ -2699,7 +4489,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.
@@ -2719,20 +4572,77 @@ zpool_do_iostat(int argc, char **argv)
                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("Only one of [-r|-w] can be passed at a time\n"));
+               usage(B_FALSE);
+               return (1);
+       }
+
        /*
         * Enter the main iostat loop.
         */
        cb.cb_list = list;
-       cb.cb_verbose = verbose;
-       cb.cb_iteration = 0;
-       cb.cb_namewidth = 0;
 
-       for (;;) {
-               pool_list_update(list);
+       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)
                        (void) fprintf(stderr, gettext("no pools available\n"));
                else {
+                       /*
+                        * If this is the first iteration and -y was supplied
+                        * we skip any printing.
+                        */
+                       boolean_t skip = (omit_since_boot &&
+                           cb.cb_iteration == 0);
+
                        /*
                         * Refresh all statistics.  This is done as an
                         * explicit step before calculating the maximum name
@@ -2740,7 +4650,7 @@ 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
@@ -2748,30 +4658,63 @@ zpool_do_iostat(int argc, char **argv)
                         */
                        cb.cb_namewidth = 0;
                        (void) pool_list_iter(list, B_FALSE, get_namewidth,
-                               &cb);
+                           &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;
+                       }
+
                        /*
-                        * If it's the first time, or verbose mode, print the
-                        * header.
+                        * 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 || verbose)
+                       if (((++cb.cb_iteration == 1 && !skip) ||
+                           (skip != verbose)) &&
+                           (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
+                           !cb.cb_scripted)
                                print_iostat_header(&cb);
 
-                       (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
+                       if (skip) {
+                               (void) fsleep(interval);
+                               continue;
+                       }
+
+
+                       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");
                }
 
                /*
@@ -2786,7 +4729,7 @@ zpool_do_iostat(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
-               (void) sleep(interval);
+               (void) fsleep(interval);
        }
 
        pool_list_free(list);
@@ -2796,9 +4739,11 @@ 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;
 
 /*
@@ -2889,11 +4834,8 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
 
                right_justify = B_FALSE;
                if (pl->pl_prop != ZPROP_INVAL) {
-                       if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
-                           zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
-                               propstr = "-";
-                       else if (zpool_get_prop(zhp, pl->pl_prop, property,
-                           sizeof (property), NULL) != 0)
+                       if (zpool_get_prop(zhp, pl->pl_prop, property,
+                           sizeof (property), NULL, cb->cb_literal) != 0)
                                propstr = "-";
                        else
                                propstr = property;
@@ -2926,15 +4868,45 @@ 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)
+print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
+    boolean_t valid, enum zfs_nicenum_format format)
 {
        char propval[64];
        boolean_t fixed;
        size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
 
-       zfs_nicenum(value, propval, sizeof (propval));
+       switch (prop) {
+       case ZPOOL_PROP_EXPANDSZ:
+               if (value == 0)
+                       (void) strlcpy(propval, "-", sizeof (propval));
+               else
+                       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:
+               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;
+       default:
+               zfs_nicenum_format(value, propval, sizeof (propval), format);
+       }
 
-       if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
+       if (!valid)
                (void) strlcpy(propval, "-", sizeof (propval));
 
        if (scripted)
@@ -2952,11 +4924,23 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
        uint_t c, children;
        char *vname;
        boolean_t scripted = cb->cb_scripted;
+       uint64_t islog = B_FALSE;
+       boolean_t haslog = B_FALSE;
+       char *dashes = "%-*s      -      -      -         -      -      -\n";
 
        verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t **)&vs, &c) == 0);
 
        if (name != NULL) {
+               boolean_t toplevel = (vs->vs_space != 0);
+               uint64_t cap;
+               enum zfs_nicenum_format format;
+
+               if (cb->cb_literal)
+                       format = ZFS_NICENUM_RAW;
+               else
+                       format = ZFS_NICENUM_1024;
+
                if (scripted)
                        (void) printf("\t%s", name);
                else if (strlen(name) + depth > cb->cb_namewidth)
@@ -2964,23 +4948,29 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                else
                        (void) printf("%*s%s%*s", depth, "", name,
                            (int)(cb->cb_namewidth - strlen(name) - depth), "");
-
-               /* only toplevel vdevs have capacity stats */
-               if (vs->vs_space == 0) {
-                       if (scripted)
-                               (void) printf("\t-\t-\t-");
-                       else
-                               (void) printf("      -      -      -");
-               } else {
-                       print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
-                           scripted);
-                       print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
-                           scripted);
-                       print_one_column(ZPOOL_PROP_FREE,
-                           vs->vs_space - vs->vs_alloc, scripted);
-               }
-               print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
-                   scripted);
+
+               /*
+                * Print the properties for the individual vdevs. Some
+                * properties are only applicable to toplevel vdevs. The
+                * '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, format);
+               print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
+                   toplevel, format);
+               print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
+                   scripted, toplevel, format);
+               print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
+                   B_TRUE, format);
+               print_one_column(ZPOOL_PROP_FRAGMENTATION,
+                   vs->vs_fragmentation, 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,
+                   format);
                (void) printf("\n");
        }
 
@@ -2995,24 +4985,51 @@ print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
                    ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
                        continue;
 
-               vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
+               if (nvlist_lookup_uint64(child[c],
+                   ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
+                       haslog = B_TRUE;
+                       continue;
+               }
+
+               vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                   cb->cb_name_flags);
                print_list_stats(zhp, vname, child[c], cb, depth + 2);
                free(vname);
        }
 
-       /*
-        * Include level 2 ARC devices in iostat output
-        */
+       if (haslog == B_TRUE) {
+               /* LINTED E_SEC_PRINTF_VAR_FMT */
+               (void) printf(dashes, cb->cb_namewidth, "log");
+               for (c = 0; c < children; c++) {
+                       if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
+                           &islog) != 0 || !islog)
+                               continue;
+                       vname = zpool_vdev_name(g_zfs, zhp, child[c],
+                           cb->cb_name_flags);
+                       print_list_stats(zhp, vname, child[c], cb, depth + 2);
+                       free(vname);
+               }
+       }
+
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
-           &child, &children) != 0)
-               return;
+           &child, &children) == 0 && children > 0) {
+               /* 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],
+                           cb->cb_name_flags);
+                       print_list_stats(zhp, vname, child[c], cb, depth + 2);
+                       free(vname);
+               }
+       }
 
-       if (children > 0) {
-               (void) printf("%-*s      -      -      -      -      -      "
-                   "-\n", cb->cb_namewidth, "cache");
+       if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
+           &children) == 0 && children > 0) {
+               /* 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);
+                           cb->cb_name_flags);
                        print_list_stats(zhp, vname, child[c], cb, depth + 2);
                        free(vname);
                }
@@ -3044,12 +5061,17 @@ list_callback(zpool_handle_t *zhp, void *data)
 }
 
 /*
- * 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,capacity,health,altroot"
+ *             "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
@@ -3059,25 +5081,38 @@ int
 zpool_do_list(int argc, char **argv)
 {
        int c;
-       int ret;
+       int ret = 0;
        list_cbdata_t cb = { 0 };
        static char default_props[] =
-           "name,size,allocated,free,capacity,dedupratio,"
-           "health,altroot";
+           "name,size,allocated,free,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;
@@ -3104,17 +5139,10 @@ zpool_do_list(int argc, char **argv)
        if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
                usage(B_FALSE);
 
-       if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
-               return (1);
-
-       if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
-               (void) printf(gettext("no pools available\n"));
-               zprop_free_list(cb.cb_proplist);
-               return (0);
-       }
-
        for (;;) {
-               pool_list_update(list);
+               if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
+                   &ret)) == NULL)
+                       return (1);
 
                if (pool_list_count(list) == 0)
                        break;
@@ -3134,9 +5162,16 @@ zpool_do_list(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
-               (void) sleep(interval);
+               pool_list_free(list);
+               (void) fsleep(interval);
+       }
+
+       if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
+               (void) printf(gettext("no pools available\n"));
+               ret = 0;
        }
 
+       pool_list_free(list);
        zprop_free_list(cb.cb_proplist);
        return (ret);
 }
@@ -3218,25 +5253,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);
 
@@ -3333,14 +5387,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
@@ -3358,25 +5416,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;
@@ -3386,14 +5454,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);
@@ -3413,6 +5483,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;
 
@@ -3431,8 +5507,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) {
@@ -3441,22 +5519,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, B_FALSE,
+                           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;
@@ -3466,6 +5557,8 @@ zpool_do_split(int argc, char **argv)
                    "different altroot\n"), "zpool import");
        }
        zpool_close(zhp);
+       nvlist_free(config);
+       nvlist_free(props);
 
        return (ret);
 }
@@ -3545,11 +5638,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 */
@@ -3561,14 +5652,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);
@@ -3595,8 +5689,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);
@@ -3784,6 +5892,7 @@ typedef struct scrub_cbdata {
        int     cb_type;
        int     cb_argc;
        char    **cb_argv;
+       pool_scrub_cmd_t cb_scrub_cmd;
 } scrub_cbdata_t;
 
 int
@@ -3801,15 +5910,16 @@ scrub_callback(zpool_handle_t *zhp, void *data)
                return (1);
        }
 
-       err = zpool_scan(zhp, cb->cb_type);
+       err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
 
        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)
@@ -3818,13 +5928,17 @@ 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);
@@ -3832,6 +5946,13 @@ zpool_do_scrub(int argc, char **argv)
                }
        }
 
+       if (cb.cb_type == POOL_SCAN_NONE &&
+           cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
+               (void) fprintf(stderr, gettext("invalid option combination: "
+                   "-s and -p are mutually exclusive\n"));
+               usage(B_FALSE);
+       }
+
        cb.cb_argc = argc;
        cb.cb_argv = argv;
        argc -= optind;
@@ -3845,22 +5966,13 @@ zpool_do_scrub(int argc, char **argv)
        return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
 }
 
-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;
-
 /*
  * Print out detailed scrub status.
  */
 void
 print_scan_status(pool_scan_stat_t *ps)
 {
-       time_t start, end;
+       time_t start, end, pause;
        uint64_t elapsed, mins_left, hours_left;
        uint64_t pass_exam, examined, total;
        uint_t rate;
@@ -3878,7 +5990,8 @@ 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);
@@ -3920,8 +6033,17 @@ print_scan_status(pool_scan_stat_t *ps)
         * Scan is in progress.
         */
        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 {
+                       char buf[32];
+                       struct tm *p = localtime(&pause);
+                       (void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
+                       (void) printf(gettext("scrub paused since %s\n"), buf);
+                       (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));
@@ -3933,6 +6055,7 @@ print_scan_status(pool_scan_stat_t *ps)
 
        /* elapsed time for this pass */
        elapsed = time(NULL) - ps->pss_pass_start;
+       elapsed -= ps->pss_pass_scrub_spent_paused;
        elapsed = elapsed ? elapsed : 1;
        pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
        rate = pass_exam / elapsed;
@@ -3940,28 +6063,34 @@ print_scan_status(pool_scan_stat_t *ps)
        mins_left = ((total - examined) / rate) / 60;
        hours_left = mins_left / 60;
 
-       zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
-       zfs_nicenum(total, total_buf, sizeof (total_buf));
-       zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
+       zfs_nicebytes(examined, examined_buf, sizeof (examined_buf));
+       zfs_nicebytes(total, total_buf, sizeof (total_buf));
 
        /*
         * do not print estimated time if hours_left is more than 30 days
+        * or we have a paused scrub
         */
-       (void) printf(gettext("    %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 (pause == 0) {
+               zfs_nicebytes(rate, rate_buf, sizeof (rate_buf));
+               (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));
+               } else {
+                       (void) printf(gettext(
+                           ", (scan is slow, no estimated time)\n"));
+               }
        } else {
-               (void) printf(gettext(
-                   ", (scan is slow, no estimated time)\n"));
+               (void) printf(gettext("\t%s scanned out of %s\n"),
+                   examined_buf, total_buf);
        }
 
        if (ps->pss_func == POOL_SCAN_RESILVER) {
-               (void) printf(gettext("    %s resilvered, %.2f%% done\n"),
+               (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("    %s repaired, %.2f%% done\n"),
+               (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
                    processed_buf, 100 * fraction_done);
        }
 }
@@ -3974,11 +6103,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");
@@ -4002,8 +6128,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;
@@ -4014,16 +6140,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;
@@ -4034,9 +6160,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);
        }
 }
@@ -4048,6 +6174,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
@@ -4065,10 +6192,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);
@@ -4098,13 +6227,14 @@ status_callback(zpool_handle_t *zhp, void *data)
        status_cbdata_t *cbp = data;
        nvlist_t *config, *nvroot;
        char *msgid;
-       int reason;
+       zpool_status_t reason;
+       zpool_errata_t errata;
        const char *health;
        uint_t c;
        vdev_stat_t *vs;
 
        config = zpool_get_config(zhp, NULL);
-       reason = zpool_get_status(zhp, &msgid);
+       reason = zpool_get_status(zhp, &msgid, &errata);
 
        cbp->cb_count++;
 
@@ -4314,7 +6444,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"
@@ -4322,6 +6452,39 @@ status_callback(zpool_handle_t *zhp, void *data)
                    "'zpool clear'.\n"));
                break;
 
+       case ZPOOL_STATUS_HOSTID_MISMATCH:
+               (void) printf(gettext("status: Mismatch between pool hostid "
+                   "and system hostid on imported pool.\n\tThis pool was "
+                   "previously imported into a system with a different "
+                   "hostid,\n\tand then was verbatim imported into this "
+                   "system.\n"));
+               (void) printf(gettext("action: Export this pool on all systems "
+                   "on which it is imported.\n"
+                   "\tThen import it to correct the mismatch.\n"));
+               break;
+
+       case ZPOOL_STATUS_ERRATA:
+               (void) printf(gettext("status: Errata #%d detected.\n"),
+                   errata);
+
+               switch (errata) {
+               case ZPOOL_ERRATA_NONE:
+                       break;
+
+               case ZPOOL_ERRATA_ZOL_2094_SCRUB:
+                       (void) printf(gettext("action: To correct the issue "
+                           "run 'zpool scrub'.\n"));
+                       break;
+
+               default:
+                       /*
+                        * All errata which allow the pool to be imported
+                        * must contain an action message.
+                        */
+                       assert(0);
+               }
+               break;
+
        default:
                /*
                 * The remaining errors can't actually be generated, yet.
@@ -4334,7 +6497,6 @@ 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;
@@ -4344,25 +6506,32 @@ status_callback(zpool_handle_t *zhp, void *data)
                    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
                print_scan_status(ps);
 
-               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->vcdl != NULL)
+                       print_cmd_columns(cbp->vcdl, 0);
+
+               printf("\n");
+               print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
+                   B_FALSE);
 
                if (num_logs(nvroot) > 0)
-                       print_logs(zhp, nvroot, namewidth, B_TRUE);
+                       print_logs(zhp, cbp, nvroot);
                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) {
@@ -4410,8 +6579,13 @@ status_callback(zpool_handle_t *zhp, void *data)
 }
 
 /*
- * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
+ * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
+ *              [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 complete error logs
  *     -x      Display only pools with potential problems
  *     -D      Display dedup status (undocumented)
@@ -4424,12 +6598,47 @@ 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:gLPvxDT:")) != -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 'g':
+                       cb.cb_name_flags |= VDEV_NAME_GUID;
+                       break;
+               case 'L':
+                       cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
+                       break;
+               case 'P':
+                       cb.cb_name_flags |= VDEV_NAME_PATH;
+                       break;
                case 'v':
                        cb.cb_verbose = B_TRUE;
                        break;
@@ -4443,8 +6652,13 @@ zpool_do_status(int argc, char **argv)
                        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);
                }
        }
@@ -4458,14 +6672,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)
@@ -4480,7 +6702,7 @@ zpool_do_status(int argc, char **argv)
                if (count != 0 && --count == 0)
                        break;
 
-               (void) sleep(interval);
+               (void) fsleep(interval);
        }
 
        return (0);
@@ -4493,12 +6715,33 @@ typedef struct upgrade_cbdata {
        char    **cb_argv;
 } upgrade_cbdata_t;
 
+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 *count = (int *)unsupp_fs;
+
+       if (zfs_version > ZPL_VERSION) {
+               (void) printf(gettext("%s (v%d) is not supported by this "
+                   "implementation of ZFS.\n"),
+                   zfs_get_name(zhp), zfs_version);
+               (*count)++;
+       }
+
+       zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
+
+       zfs_close(zhp);
+
+       return (0);
+}
+
 static int
 upgrade_version(zpool_handle_t *zhp, uint64_t version)
 {
        int ret;
        nvlist_t *config;
        uint64_t oldversion;
+       int unsupp_fs = 0;
 
        config = zpool_get_config(zhp, NULL);
        verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
@@ -4507,6 +6750,17 @@ upgrade_version(zpool_handle_t *zhp, uint64_t version)
        assert(SPA_VERSION_IS_SUPPORTED(oldversion));
        assert(oldversion < version);
 
+       ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
+       if (ret != 0)
+               return (ret);
+
+       if (unsupp_fs) {
+               (void) fprintf(stderr, gettext("Upgrade not performed due "
+                   "to %d unsupported filesystems (max v%d).\n"),
+                   unsupp_fs, (int)ZPL_VERSION);
+               return (1);
+       }
+
        ret = zpool_upgrade(zhp, version);
        if (ret != 0)
                return (ret);
@@ -4514,12 +6768,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);
@@ -4585,13 +6839,6 @@ upgrade_cb(zpool_handle_t *zhp, void *arg)
                        return (ret);
                printnl = B_TRUE;
 
-#if 0
-               /*
-                * XXX: This code can be enabled when Illumos commit
-                * 4445fffbbb1ea25fd0e9ea68b9380dd7a6709025 is merged.
-                * It reworks the history logging among other things.
-                */
-
                /*
                 * If they did "zpool upgrade -a", then we could
                 * be doing ioctls to different pools.  We need
@@ -4600,7 +6847,6 @@ upgrade_cb(zpool_handle_t *zhp, void *arg)
                 */
                (void) zpool_log_history(g_zfs, history_str);
                log_history = B_FALSE;
-#endif
        }
 
        if (cbp->cb_version >= SPA_VERSION_FEATURES) {
@@ -4700,6 +6946,14 @@ upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
 
                                (void) printf(gettext("      %s\n"), fname);
                        }
+                       /*
+                        * If they did "zpool upgrade -a", then we could
+                        * be doing ioctls to different pools.  We need
+                        * to log this history once to each pool, and bypass
+                        * the normal history logging that happens in main().
+                        */
+                       (void) zpool_log_history(g_zfs, history_str);
+                       log_history = B_FALSE;
                }
        }
 
@@ -4716,7 +6970,7 @@ upgrade_one(zpool_handle_t *zhp, void *data)
        int ret;
 
        if (strcmp("log", zpool_get_name(zhp)) == 0) {
-               (void) printf(gettext("'log' is now a reserved word\n"
+               (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
                    "Pool 'log' must be renamed using export and import"
                    " to upgrade.\n"));
                return (1);
@@ -4726,14 +6980,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);
        }
 
@@ -4855,7 +7109,8 @@ zpool_do_upgrade(int argc, char **argv)
                    "---------------\n");
                for (i = 0; i < SPA_FEATURES; i++) {
                        zfeature_info_t *fi = &spa_feature_table[i];
-                       const char *ro = fi->fi_can_readonly ?
+                       const char *ro =
+                           (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
                            " (read-only compatible)" : "";
 
                        (void) printf("%-37s%s\n", fi->fi_uname, ro);
@@ -4919,7 +7174,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) {
@@ -4955,8 +7210,8 @@ zpool_do_upgrade(int argc, char **argv)
 
 typedef struct hist_cbdata {
        boolean_t first;
-       int longfmt;
-       int internal;
+       boolean_t longfmt;
+       boolean_t internal;
 } hist_cbdata_t;
 
 /*
@@ -4968,21 +7223,8 @@ get_history_one(zpool_handle_t *zhp, void *data)
        nvlist_t *nvhis;
        nvlist_t **records;
        uint_t numrecords;
-       char *cmdstr;
-       char *pathstr;
-       uint64_t dst_time;
-       time_t tsec;
-       struct tm t;
-       char tbuf[30];
        int ret, i;
-       uint64_t who;
-       struct passwd *pwd;
-       char *hostname;
-       char *zonename;
-       char internalstr[MAXPATHLEN];
        hist_cbdata_t *cb = (hist_cbdata_t *)data;
-       uint64_t txg;
-       uint64_t ievent;
 
        cb->first = B_FALSE;
 
@@ -4994,62 +7236,95 @@ get_history_one(zpool_handle_t *zhp, void *data)
        verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
            &records, &numrecords) == 0);
        for (i = 0; i < numrecords; i++) {
-               if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
-                   &dst_time) != 0)
-                       continue;
+               nvlist_t *rec = records[i];
+               char tbuf[30] = "";
 
-               /* is it an internal event or a standard event? */
-               if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
-                   &cmdstr) != 0) {
-                       if (cb->internal == 0)
-                               continue;
+               if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
+                       time_t tsec;
+                       struct tm t;
 
-                       if (nvlist_lookup_uint64(records[i],
-                           ZPOOL_HIST_INT_EVENT, &ievent) != 0)
+                       tsec = fnvlist_lookup_uint64(records[i],
+                           ZPOOL_HIST_TIME);
+                       (void) localtime_r(&tsec, &t);
+                       (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
+               }
+
+               if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
+                       (void) printf("%s %s", tbuf,
+                           fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
+               } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
+                       int ievent =
+                           fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
+                       if (!cb->internal)
+                               continue;
+                       if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
+                               (void) printf("%s unrecognized record:\n",
+                                   tbuf);
+                               dump_nvlist(rec, 4);
+                               continue;
+                       }
+                       (void) printf("%s [internal %s txg:%lld] %s", tbuf,
+                           zfs_history_event_names[ievent],
+                           (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(
+                           rec, ZPOOL_HIST_TXG),
+                           fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
+                       if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
+                               (void) printf(" %s (%llu)",
+                                   fnvlist_lookup_string(rec,
+                                   ZPOOL_HIST_DSNAME),
+                                   (u_longlong_t)fnvlist_lookup_uint64(rec,
+                                   ZPOOL_HIST_DSID));
+                       }
+                       (void) printf(" %s", fnvlist_lookup_string(rec,
+                           ZPOOL_HIST_INT_STR));
+               } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
+                       if (!cb->internal)
                                continue;
-                       verify(nvlist_lookup_uint64(records[i],
-                           ZPOOL_HIST_TXG, &txg) == 0);
-                       verify(nvlist_lookup_string(records[i],
-                           ZPOOL_HIST_INT_STR, &pathstr) == 0);
-                       if (ievent >= LOG_END)
+                       (void) printf("%s ioctl %s\n", tbuf,
+                           fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
+                       if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
+                               (void) printf("    input:\n");
+                               dump_nvlist(fnvlist_lookup_nvlist(rec,
+                                   ZPOOL_HIST_INPUT_NVL), 8);
+                       }
+                       if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
+                               (void) printf("    output:\n");
+                               dump_nvlist(fnvlist_lookup_nvlist(rec,
+                                   ZPOOL_HIST_OUTPUT_NVL), 8);
+                       }
+               } else {
+                       if (!cb->internal)
                                continue;
-                       (void) snprintf(internalstr,
-                           sizeof (internalstr),
-                           "[internal %s txg:%llu] %s",
-                           zfs_history_event_names[ievent], (u_longlong_t)txg,
-                           pathstr);
-                       cmdstr = internalstr;
+                       (void) printf("%s unrecognized record:\n", tbuf);
+                       dump_nvlist(rec, 4);
                }
-               tsec = dst_time;
-               (void) localtime_r(&tsec, &t);
-               (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
-               (void) printf("%s %s", tbuf, cmdstr);
 
                if (!cb->longfmt) {
                        (void) printf("\n");
                        continue;
                }
                (void) printf(" [");
-               if (nvlist_lookup_uint64(records[i],
-                   ZPOOL_HIST_WHO, &who) == 0) {
-                       pwd = getpwuid((uid_t)who);
-                       if (pwd)
-                               (void) printf("user %s on",
-                                   pwd->pw_name);
-                       else
-                               (void) printf("user %d on",
-                                   (int)who);
-               } else {
-                       (void) printf(gettext("no info]\n"));
-                       continue;
+               if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
+                       uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
+                       struct passwd *pwd = getpwuid(who);
+                       (void) printf("user %d ", (int)who);
+                       if (pwd != NULL)
+                               (void) printf("(%s) ", pwd->pw_name);
                }
-               if (nvlist_lookup_string(records[i],
-                   ZPOOL_HIST_HOST, &hostname) == 0) {
-                       (void) printf(" %s", hostname);
+               if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
+                       (void) printf("on %s",
+                           fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
                }
-               if (nvlist_lookup_string(records[i],
-                   ZPOOL_HIST_ZONE, &zonename) == 0) {
-                       (void) printf(":%s", zonename);
+               if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
+                       (void) printf(":%s",
+                           fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
                }
 
                (void) printf("]");
@@ -5066,8 +7341,6 @@ get_history_one(zpool_handle_t *zhp, void *data)
  *
  * Displays the history of commands that modified pools.
  */
-
-
 int
 zpool_do_history(int argc, char **argv)
 {
@@ -5080,10 +7353,10 @@ zpool_do_history(int argc, char **argv)
        while ((c = getopt(argc, argv, "li")) != -1) {
                switch (c) {
                case 'l':
-                       cbdata.longfmt = 1;
+                       cbdata.longfmt = B_TRUE;
                        break;
                case 'i':
-                       cbdata.internal = 1;
+                       cbdata.internal = B_TRUE;
                        break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
@@ -5113,7 +7386,7 @@ typedef struct ev_opts {
 } 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;
@@ -5122,11 +7395,14 @@ zpool_do_events_short(nvlist_t *nvl)
        verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
        memset(str, ' ', 32);
        (void) ctime_r((const time_t *)&tv[0], ctime_str);
-       (void) strncpy(str,    ctime_str+4,  6);             /* 'Jun 30'     */
-       (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);
+       (void) strncpy(str, ctime_str+4,  6);           /* 'Jun 30' */
+       (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' */
+       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);
@@ -5205,7 +7481,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:
@@ -5233,10 +7522,10 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
                        printf(gettext("(%d embedded nvlists)\n"), nelem);
                        for (i = 0; i < nelem; i++) {
                                printf(gettext("%*s%s[%d] = %s\n"),
-                                      depth, "", name, i, "(embedded nvlist)");
+                                   depth, "", name, i, "(embedded nvlist)");
                                zpool_do_events_nvprint(val[i], depth + 8);
                                printf(gettext("%*s(end %s[%i])\n"),
-                                      depth, "", name, i);
+                                   depth, "", name, i);
                        }
                        printf(gettext("%*s(end %s)\n"), depth, "", name);
                        }
@@ -5314,7 +7603,8 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
 
                        (void) nvpair_value_int64_array(nvp, &val, &nelem);
                        for (i = 0; i < nelem; i++)
-                               printf(gettext("0x%llx "), (u_longlong_t)val[i]);
+                               printf(gettext("0x%llx "),
+                                   (u_longlong_t)val[i]);
 
                        break;
                        }
@@ -5325,12 +7615,24 @@ zpool_do_events_nvprint(nvlist_t *nvl, int depth)
 
                        (void) nvpair_value_uint64_array(nvp, &val, &nelem);
                        for (i = 0; i < nelem; i++)
-                               printf(gettext("0x%llx "), (u_longlong_t)val[i]);
+                               printf(gettext("0x%llx "),
+                                   (u_longlong_t)val[i]);
+
+                       break;
+                       }
+
+               case DATA_TYPE_STRING_ARRAY: {
+                       char **str;
+                       uint_t i, nelem;
+
+                       (void) nvpair_value_string_array(nvp, &str, &nelem);
+                       for (i = 0; i < nelem; i++)
+                               printf(gettext("\"%s\" "),
+                                   str[i] ? str[i] : "<NULL>");
 
                        break;
                        }
 
-               case DATA_TYPE_STRING_ARRAY:
                case DATA_TYPE_BOOLEAN_ARRAY:
                case DATA_TYPE_BYTE_ARRAY:
                case DATA_TYPE_DOUBLE:
@@ -5347,24 +7649,24 @@ static int
 zpool_do_events_next(ev_opts_t *opts)
 {
        nvlist_t *nvl;
-       int cleanup_fd, ret, dropped;
+       int zevent_fd, ret, dropped;
 
-        cleanup_fd = open(ZFS_DEV, O_RDWR);
-        VERIFY(cleanup_fd >= 0);
+       zevent_fd = open(ZFS_DEV, O_RDWR);
+       VERIFY(zevent_fd >= 0);
 
        if (!opts->scripted)
                (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
 
        while (1) {
                ret = zpool_events_next(g_zfs, &nvl, &dropped,
-                   !!opts->follow, cleanup_fd);
+                   (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
                if (ret || nvl == NULL)
                        break;
 
                if (dropped > 0)
                        (void) printf(gettext("dropped %d events\n"), dropped);
 
-               zpool_do_events_short(nvl);
+               zpool_do_events_short(nvl, opts);
 
                if (opts->verbose) {
                        zpool_do_events_nvprint(nvl, 8);
@@ -5375,7 +7677,7 @@ zpool_do_events_next(ev_opts_t *opts)
                nvlist_free(nvl);
        }
 
-        VERIFY(0 == close(cleanup_fd));
+       VERIFY(0 == close(zevent_fd));
 
        return (ret);
 }
@@ -5433,7 +7735,7 @@ zpool_do_events(int argc, char **argv)
        else
                ret = zpool_do_events_next(&opts);
 
-       return ret;
+       return (ret);
 }
 
 static int
@@ -5467,7 +7769,7 @@ get_callback(zpool_handle_t *zhp, void *data)
                        }
                } else {
                        if (zpool_get_prop(zhp, pl->pl_prop, value,
-                           sizeof (value), &srctype) != 0)
+                           sizeof (value), &srctype, cbp->cb_literal) != 0)
                                continue;
 
                        zprop_print_one_property(zpool_get_name(zhp), cbp,
@@ -5478,20 +7780,32 @@ 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 ret;
-
-       if (argc < 2) {
-               (void) fprintf(stderr, gettext("missing property "
-                   "argument\n"));
-               usage(B_FALSE);
-       }
+       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;
@@ -5499,10 +7813,89 @@ zpool_do_get(int argc, char **argv)
        cb.cb_columns[3] = GET_COL_SOURCE;
        cb.cb_type = ZFS_TYPE_POOL;
 
-       if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
+       /* check options */
+       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);
+                       usage(B_FALSE);
+               }
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 1) {
+               (void) fprintf(stderr, gettext("missing property "
+                   "argument\n"));
+               usage(B_FALSE);
+       }
+
+       if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
            ZFS_TYPE_POOL) != 0)
                usage(B_FALSE);
 
+       argc--;
+       argv++;
+
        if (cb.cb_proplist != NULL) {
                fake_name.pl_prop = ZPOOL_PROP_NAME;
                fake_name.pl_width = strlen(gettext("NAME"));
@@ -5510,7 +7903,7 @@ zpool_do_get(int argc, char **argv)
                cb.cb_proplist = &fake_name;
        }
 
-       ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
+       ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
            get_callback, &cb);
 
        if (cb.cb_proplist == &fake_name)
@@ -5606,12 +7999,13 @@ find_command_idx(char *command, int *idx)
 int
 main(int argc, char **argv)
 {
-       int ret;
+       int ret = 0;
        int i = 0;
        char *cmdname;
 
        (void) setlocale(LC_ALL, "");
        (void) textdomain(TEXT_DOMAIN);
+       srand(time(NULL));
 
        opterr = 0;
 
@@ -5628,17 +8022,17 @@ main(int argc, char **argv)
        /*
         * Special case '-?'
         */
-       if ((strcmp(cmdname, "-?") == 0) ||
-            strcmp(cmdname, "--help") == 0)
+       if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
                usage(B_TRUE);
 
-       if ((g_zfs = libzfs_init()) == NULL)
+       if ((g_zfs = libzfs_init()) == NULL) {
+               (void) fprintf(stderr, "%s", libzfs_error_init(errno));
                return (1);
+       }
 
        libzfs_print_on_error(g_zfs, B_TRUE);
 
-       zpool_set_history_str("zpool", argc, argv, history_str);
-       verify(zpool_stage_history(g_zfs, history_str) == 0);
+       zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
 
        /*
         * Run the appropriate command.
@@ -5655,10 +8049,17 @@ main(int argc, char **argv)
                 * '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);
@@ -5666,6 +8067,9 @@ main(int argc, char **argv)
                ret = 1;
        }
 
+       if (ret == 0 && log_history)
+               (void) zpool_log_history(g_zfs, history_str);
+
        libzfs_fini(g_zfs);
 
        /*