]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/spa.c
Fix "zpool get guid,freeing,leaked" source
[mirror_zfs.git] / module / zfs / spa.c
index d7b800adfa61f8809329cd2e779c3bae2155168c..374322a037f553c2cdd05c44d15b608c069dbef9 100644 (file)
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2015, Nexenta Systems, Inc.  All rights reserved.
  * Copyright (c) 2013, 2014, Nexenta Systems, Inc.  All rights reserved.
  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
+ * Copyright (c) 2016 Actifio, Inc. All rights reserved.
  */
 
 /*
 #include "zfs_prop.h"
 #include "zfs_comutil.h"
 
+/*
+ * The interval, in seconds, at which failed configuration cache file writes
+ * should be retried.
+ */
+static int zfs_ccw_retry_interval = 300;
+
 typedef enum zti_modes {
        ZTI_MODE_FIXED,                 /* value is # of threads (min 1) */
        ZTI_MODE_BATCH,                 /* cpu-intensive; value is ignored */
@@ -192,7 +200,7 @@ spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
        vdev_t *rvd = spa->spa_root_vdev;
        dsl_pool_t *pool = spa->spa_dsl_pool;
        uint64_t size, alloc, cap, version;
-       zprop_source_t src = ZPROP_SRC_NONE;
+       const zprop_source_t src = ZPROP_SRC_NONE;
        spa_config_dirent_t *dp;
        metaslab_class_t *mc = spa_normal_class(spa);
 
@@ -224,11 +232,13 @@ spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
                    rvd->vdev_state, src);
 
                version = spa_version(spa);
-               if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
-                       src = ZPROP_SRC_DEFAULT;
-               else
-                       src = ZPROP_SRC_LOCAL;
-               spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
+               if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) {
+                       spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
+                           version, ZPROP_SRC_DEFAULT);
+               } else {
+                       spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
+                           version, ZPROP_SRC_LOCAL);
+               }
        }
 
        if (pool != NULL) {
@@ -274,6 +284,14 @@ spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
                    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
        }
 
+       if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
+               spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
+                   DNODE_MAX_SIZE, ZPROP_SRC_NONE);
+       } else {
+               spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
+                   DNODE_MIN_SIZE, ZPROP_SRC_NONE);
+       }
+
        if ((dp = list_head(&spa->spa_config_list)) != NULL) {
                if (dp->scd_path == NULL) {
                        spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
@@ -346,8 +364,7 @@ spa_prop_get(spa_t *spa, nvlist_t **nvp)
                                        break;
                                }
 
-                               strval = kmem_alloc(
-                                   MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
+                               strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
                                    KM_SLEEP);
                                dsl_dataset_name(ds, strval);
                                dsl_dataset_rele(ds, FTAG);
@@ -360,8 +377,7 @@ spa_prop_get(spa_t *spa, nvlist_t **nvp)
                        spa_prop_add_list(*nvp, prop, strval, intval, src);
 
                        if (strval != NULL)
-                               kmem_free(strval,
-                                   MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
+                               kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
 
                        break;
 
@@ -505,7 +521,8 @@ spa_prop_validate(spa_t *spa, nvlist_t *props)
                                /*
                                 * Must be ZPL, and its property settings
                                 * must be supported by GRUB (compression
-                                * is not gzip, and large blocks are not used).
+                                * is not gzip, and large blocks or large
+                                * dnodes are not used).
                                 */
 
                                if (dmu_objset_type(os) != DMU_OST_ZFS) {
@@ -522,6 +539,12 @@ spa_prop_validate(spa_t *spa, nvlist_t *props)
                                    &propval)) == 0 &&
                                    propval > SPA_OLD_MAXBLOCKSIZE) {
                                        error = SET_ERROR(ENOTSUP);
+                               } else if ((error =
+                                   dsl_prop_get_int_ds(dmu_objset_ds(os),
+                                   zfs_prop_to_name(ZFS_PROP_DNODESIZE),
+                                   &propval)) == 0 &&
+                                   propval != ZFS_DNSIZE_LEGACY) {
+                                       error = SET_ERROR(ENOTSUP);
                                } else {
                                        objnum = dmu_objset_id(os);
                                }
@@ -582,7 +605,6 @@ spa_prop_validate(spa_t *spa, nvlist_t *props)
                                        error = SET_ERROR(EINVAL);
                                        break;
                                }
-                               check++;
                        }
                        if (strlen(strval) > ZPROP_MAX_COMMENT)
                                error = SET_ERROR(E2BIG);
@@ -782,7 +804,7 @@ spa_change_guid(spa_t *spa)
 
        if (error == 0) {
                spa_config_sync(spa, B_FALSE, B_TRUE);
-               spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_REGUID);
+               spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
        }
 
        mutex_exit(&spa_namespace_lock);
@@ -1131,6 +1153,24 @@ spa_activate(spa_t *spa, int mode)
        avl_create(&spa->spa_errlist_last,
            spa_error_entry_compare, sizeof (spa_error_entry_t),
            offsetof(spa_error_entry_t, se_avl));
+
+       /*
+        * This taskq is used to perform zvol-minor-related tasks
+        * asynchronously. This has several advantages, including easy
+        * resolution of various deadlocks (zfsonlinux bug #3681).
+        *
+        * The taskq must be single threaded to ensure tasks are always
+        * processed in the order in which they were dispatched.
+        *
+        * A taskq per pool allows one to keep the pools independent.
+        * This way if one pool is suspended, it will not impact another.
+        *
+        * The preferred location to dispatch a zvol minor task is a sync
+        * task. In this context, there is easy access to the spa_t and minimal
+        * error handling is required because the sync task must succeed.
+        */
+       spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri,
+           1, INT_MAX, 0);
 }
 
 /*
@@ -1149,6 +1189,11 @@ spa_deactivate(spa_t *spa)
 
        spa_evicting_os_wait(spa);
 
+       if (spa->spa_zvol_taskq) {
+               taskq_destroy(spa->spa_zvol_taskq);
+               spa->spa_zvol_taskq = NULL;
+       }
+
        txg_list_destroy(&spa->spa_vdev_txg_list);
 
        list_destroy(&spa->spa_config_dirty_list);
@@ -1639,9 +1684,23 @@ spa_check_removed(vdev_t *vd)
 
        if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
            !vd->vdev_ishole) {
-               zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE,
-                   vd->vdev_spa, vd, NULL, 0, 0);
-               spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_CHECK);
+               zfs_post_autoreplace(vd->vdev_spa, vd);
+               spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
+       }
+}
+
+static void
+spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd)
+{
+       uint64_t i;
+
+       ASSERT3U(vd->vdev_children, ==, mvd->vdev_children);
+
+       vd->vdev_top_zap = mvd->vdev_top_zap;
+       vd->vdev_leaf_zap = mvd->vdev_leaf_zap;
+
+       for (i = 0; i < vd->vdev_children; i++) {
+               spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]);
        }
 }
 
@@ -1749,16 +1808,25 @@ spa_config_valid(spa_t *spa, nvlist_t *config)
                        spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
 
                        vdev_reopen(rvd);
-               } else if (mtvd->vdev_islog) {
+               } else {
+                       if (mtvd->vdev_islog) {
+                               /*
+                                * Load the slog device's state from the MOS
+                                * config since it's possible that the label
+                                * does not contain the most up-to-date
+                                * information.
+                                */
+                               vdev_load_log_state(tvd, mtvd);
+                               vdev_reopen(tvd);
+                       }
+
                        /*
-                        * Load the slog device's state from the MOS config
-                        * since it's possible that the label does not
-                        * contain the most up-to-date information.
+                        * Per-vdev ZAP info is stored exclusively in the MOS.
                         */
-                       vdev_load_log_state(tvd, mtvd);
-                       vdev_reopen(tvd);
+                       spa_config_valid_zaps(tvd, mtvd);
                }
        }
+
        vdev_free(mrvd);
        spa_config_exit(spa, SCL_ALL, FTAG);
 
@@ -1892,9 +1960,9 @@ spa_load_verify_done(zio_t *zio)
        if (error) {
                if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
                    type != DMU_OT_INTENT_LOG)
-                       atomic_add_64(&sle->sle_meta_count, 1);
+                       atomic_inc_64(&sle->sle_meta_count);
                else
-                       atomic_add_64(&sle->sle_data_count, 1);
+                       atomic_inc_64(&sle->sle_data_count);
        }
        zio_data_buf_free(zio->io_data, zio->io_size);
 
@@ -1950,6 +2018,16 @@ spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
        return (0);
 }
 
+/* ARGSUSED */
+int
+verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
+{
+       if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
+               return (SET_ERROR(ENAMETOOLONG));
+
+       return (0);
+}
+
 static int
 spa_load_verify(spa_t *spa)
 {
@@ -1964,6 +2042,14 @@ spa_load_verify(spa_t *spa)
        if (policy.zrp_request & ZPOOL_NEVER_REWIND)
                return (0);
 
+       dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
+       error = dmu_objset_find_dp(spa->spa_dsl_pool,
+           spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
+           DS_FIND_CHILDREN);
+       dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
+       if (error != 0)
+               return (error);
+
        rio = zio_root(spa, NULL, &sle,
            ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
 
@@ -2185,6 +2271,38 @@ spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
        return (error);
 }
 
+#ifdef ZFS_DEBUG
+/*
+ * Count the number of per-vdev ZAPs associated with all of the vdevs in the
+ * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
+ * spa's per-vdev ZAP list.
+ */
+static uint64_t
+vdev_count_verify_zaps(vdev_t *vd)
+{
+       spa_t *spa = vd->vdev_spa;
+       uint64_t total = 0;
+       uint64_t i;
+
+       if (vd->vdev_top_zap != 0) {
+               total++;
+               ASSERT0(zap_lookup_int(spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps, vd->vdev_top_zap));
+       }
+       if (vd->vdev_leaf_zap != 0) {
+               total++;
+               ASSERT0(zap_lookup_int(spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
+       }
+
+       for (i = 0; i < vd->vdev_children; i++) {
+               total += vdev_count_verify_zaps(vd->vdev_child[i]);
+       }
+
+       return (total);
+}
+#endif
+
 /*
  * Load an existing storage pool, using the pool's builtin spa_config as a
  * source of configuration information.
@@ -2205,6 +2323,7 @@ spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
        int parse, i;
        uint64_t obj;
        boolean_t missing_feat_write = B_FALSE;
+       nvlist_t *mos_config;
 
        /*
         * If this is an untrusted config, access the pool in read-only mode.
@@ -2603,6 +2722,38 @@ spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
        if (error != 0 && error != ENOENT)
                return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
 
+       /*
+        * Load the per-vdev ZAP map. If we have an older pool, this will not
+        * be present; in this case, defer its creation to a later time to
+        * avoid dirtying the MOS this early / out of sync context. See
+        * spa_sync_config_object.
+        */
+
+       /* The sentinel is only available in the MOS config. */
+       if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0)
+               return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
+
+       error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
+           &spa->spa_all_vdev_zaps);
+
+       if (error != ENOENT && error != 0) {
+               return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
+       } else if (error == 0 && !nvlist_exists(mos_config,
+           ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
+               /*
+                * An older version of ZFS overwrote the sentinel value, so
+                * we have orphaned per-vdev ZAPs in the MOS. Defer their
+                * destruction to later; see spa_sync_config_object.
+                */
+               spa->spa_avz_action = AVZ_ACTION_DESTROY;
+               /*
+                * We're assuming that no vdevs have had their ZAPs created
+                * before this. Better be sure of it.
+                */
+               ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
+       }
+       nvlist_free(mos_config);
+
        /*
         * If we're assembling the pool from the split-off vdevs of
         * an existing pool, we don't want to attach the spares & cache
@@ -2946,6 +3097,8 @@ spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
 
        if (config && (rewind_error || state != SPA_LOAD_RECOVER))
                spa_config_set(spa, config);
+       else
+               nvlist_free(config);
 
        if (state == SPA_LOAD_RECOVER) {
                ASSERT3P(loadinfo, ==, NULL);
@@ -3083,10 +3236,8 @@ spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
                mutex_exit(&spa_namespace_lock);
        }
 
-#ifdef _KERNEL
        if (firstopen)
-               zvol_create_minors(spa->spa_name);
-#endif
+               zvol_create_minors(spa, spa_name(spa), B_TRUE);
 
        *spapp = spa;
 
@@ -3234,6 +3385,8 @@ spa_add_l2cache(spa_t *spa, nvlist_t *config)
                            ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
                            == 0);
                        vdev_get_stats(vd, vs);
+                       vdev_config_generate_stats(vd, l2cache[i]);
+
                }
        }
 }
@@ -3807,6 +3960,7 @@ spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
        txg_wait_synced(spa->spa_dsl_pool, txg);
 
        spa_config_sync(spa, B_FALSE, B_TRUE);
+       spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
 
        spa_history_log_version(spa, "create");
 
@@ -3822,211 +3976,6 @@ spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
        return (0);
 }
 
-#ifdef _KERNEL
-/*
- * Get the root pool information from the root disk, then import the root pool
- * during the system boot up time.
- */
-extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
-
-static nvlist_t *
-spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
-{
-       nvlist_t *config;
-       nvlist_t *nvtop, *nvroot;
-       uint64_t pgid;
-
-       if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
-               return (NULL);
-
-       /*
-        * Add this top-level vdev to the child array.
-        */
-       VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
-           &nvtop) == 0);
-       VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
-           &pgid) == 0);
-       VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
-
-       /*
-        * Put this pool's top-level vdevs into a root vdev.
-        */
-       VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
-       VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
-           VDEV_TYPE_ROOT) == 0);
-       VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
-       VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
-       VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
-           &nvtop, 1) == 0);
-
-       /*
-        * Replace the existing vdev_tree with the new root vdev in
-        * this pool's configuration (remove the old, add the new).
-        */
-       VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
-       nvlist_free(nvroot);
-       return (config);
-}
-
-/*
- * Walk the vdev tree and see if we can find a device with "better"
- * configuration. A configuration is "better" if the label on that
- * device has a more recent txg.
- */
-static void
-spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
-{
-       int c;
-
-       for (c = 0; c < vd->vdev_children; c++)
-               spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
-
-       if (vd->vdev_ops->vdev_op_leaf) {
-               nvlist_t *label;
-               uint64_t label_txg;
-
-               if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
-                   &label) != 0)
-                       return;
-
-               VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
-                   &label_txg) == 0);
-
-               /*
-                * Do we have a better boot device?
-                */
-               if (label_txg > *txg) {
-                       *txg = label_txg;
-                       *avd = vd;
-               }
-               nvlist_free(label);
-       }
-}
-
-/*
- * Import a root pool.
- *
- * For x86. devpath_list will consist of devid and/or physpath name of
- * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
- * The GRUB "findroot" command will return the vdev we should boot.
- *
- * For Sparc, devpath_list consists the physpath name of the booting device
- * no matter the rootpool is a single device pool or a mirrored pool.
- * e.g.
- *     "/pci@1f,0/ide@d/disk@0,0:a"
- */
-int
-spa_import_rootpool(char *devpath, char *devid)
-{
-       spa_t *spa;
-       vdev_t *rvd, *bvd, *avd = NULL;
-       nvlist_t *config, *nvtop;
-       uint64_t guid, txg;
-       char *pname;
-       int error;
-
-       /*
-        * Read the label from the boot device and generate a configuration.
-        */
-       config = spa_generate_rootconf(devpath, devid, &guid);
-#if defined(_OBP) && defined(_KERNEL)
-       if (config == NULL) {
-               if (strstr(devpath, "/iscsi/ssd") != NULL) {
-                       /* iscsi boot */
-                       get_iscsi_bootpath_phy(devpath);
-                       config = spa_generate_rootconf(devpath, devid, &guid);
-               }
-       }
-#endif
-       if (config == NULL) {
-               cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
-                   devpath);
-               return (SET_ERROR(EIO));
-       }
-
-       VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
-           &pname) == 0);
-       VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
-
-       mutex_enter(&spa_namespace_lock);
-       if ((spa = spa_lookup(pname)) != NULL) {
-               /*
-                * Remove the existing root pool from the namespace so that we
-                * can replace it with the correct config we just read in.
-                */
-               spa_remove(spa);
-       }
-
-       spa = spa_add(pname, config, NULL);
-       spa->spa_is_root = B_TRUE;
-       spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
-
-       /*
-        * Build up a vdev tree based on the boot device's label config.
-        */
-       VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
-           &nvtop) == 0);
-       spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
-       error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
-           VDEV_ALLOC_ROOTPOOL);
-       spa_config_exit(spa, SCL_ALL, FTAG);
-       if (error) {
-               mutex_exit(&spa_namespace_lock);
-               nvlist_free(config);
-               cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
-                   pname);
-               return (error);
-       }
-
-       /*
-        * Get the boot vdev.
-        */
-       if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
-               cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
-                   (u_longlong_t)guid);
-               error = SET_ERROR(ENOENT);
-               goto out;
-       }
-
-       /*
-        * Determine if there is a better boot device.
-        */
-       avd = bvd;
-       spa_alt_rootvdev(rvd, &avd, &txg);
-       if (avd != bvd) {
-               cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
-                   "try booting from '%s'", avd->vdev_path);
-               error = SET_ERROR(EINVAL);
-               goto out;
-       }
-
-       /*
-        * If the boot device is part of a spare vdev then ensure that
-        * we're booting off the active spare.
-        */
-       if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
-           !bvd->vdev_isspare) {
-               cmn_err(CE_NOTE, "The boot device is currently spared. Please "
-                   "try booting from '%s'",
-                   bvd->vdev_parent->
-                   vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
-               error = SET_ERROR(EINVAL);
-               goto out;
-       }
-
-       error = 0;
-out:
-       spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
-       vdev_free(rvd);
-       spa_config_exit(spa, SCL_ALL, FTAG);
-       mutex_exit(&spa_namespace_lock);
-
-       nvlist_free(config);
-       return (error);
-}
-
-#endif
-
 /*
  * Import a non-root pool into the system.
  */
@@ -4074,6 +4023,7 @@ spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
                        spa_configfile_set(spa, props, B_FALSE);
 
                spa_config_sync(spa, B_FALSE, B_TRUE);
+               spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
 
                mutex_exit(&spa_namespace_lock);
                return (0);
@@ -4204,12 +4154,13 @@ spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
         */
        spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
 
-       mutex_exit(&spa_namespace_lock);
        spa_history_log_version(spa, "import");
 
-#ifdef _KERNEL
-       zvol_create_minors(pool);
-#endif
+       spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
+
+       zvol_create_minors(spa, pool, B_TRUE);
+
+       mutex_exit(&spa_namespace_lock);
 
        return (0);
 }
@@ -4344,6 +4295,10 @@ spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
        spa_open_ref(spa, FTAG);
        mutex_exit(&spa_namespace_lock);
        spa_async_suspend(spa);
+       if (spa->spa_zvol_taskq) {
+               zvol_remove_minors(spa, spa_name(spa), B_TRUE);
+               taskq_wait(spa->spa_zvol_taskq);
+       }
        mutex_enter(&spa_namespace_lock);
        spa_close(spa, FTAG);
 
@@ -4402,7 +4357,7 @@ spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
        }
 
 export_spa:
-       spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY);
+       spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
 
        if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
                spa_unload(spa);
@@ -4558,6 +4513,7 @@ spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
 
        mutex_enter(&spa_namespace_lock);
        spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
+       spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
        mutex_exit(&spa_namespace_lock);
 
        return (0);
@@ -4733,7 +4689,7 @@ spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
 
        if (newvd->vdev_isspare) {
                spa_spare_activate(newvd);
-               spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE);
+               spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
        }
 
        oldvdpath = spa_strdup(oldvd->vdev_path);
@@ -4752,6 +4708,11 @@ spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
         */
        dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
 
+       if (spa->spa_bootfs)
+               spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
+
+       spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
+
        /*
         * Commit the config
         */
@@ -4766,9 +4727,6 @@ spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
        spa_strfree(oldvdpath);
        spa_strfree(newvdpath);
 
-       if (spa->spa_bootfs)
-               spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH);
-
        return (0);
 }
 
@@ -4967,7 +4925,7 @@ spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
        vd->vdev_detached = B_TRUE;
        vdev_dirty(tvd, VDD_DTL, vd, txg);
 
-       spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE);
+       spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
 
        /* hang on to the spa before we release the lock */
        spa_open_ref(spa, FTAG);
@@ -5142,6 +5100,16 @@ spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
                    vml[c]->vdev_top->vdev_asize) == 0);
                VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
                    vml[c]->vdev_top->vdev_ashift) == 0);
+
+               /* transfer per-vdev ZAPs */
+               ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
+               VERIFY0(nvlist_add_uint64(child[c],
+                   ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
+
+               ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
+               VERIFY0(nvlist_add_uint64(child[c],
+                   ZPOOL_CONFIG_VDEV_TOP_ZAP,
+                   vml[c]->vdev_parent->vdev_top_zap));
        }
 
        if (error != 0) {
@@ -5183,11 +5151,13 @@ spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
            spa->spa_config_txg) == 0);
        VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
            spa_generate_guid(NULL)) == 0);
+       VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
        (void) nvlist_lookup_string(props,
            zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
 
        /* add the new pool to the namespace */
        newspa = spa_add(newname, config, altroot);
+       newspa->spa_avz_action = AVZ_ACTION_REBUILD;
        newspa->spa_config_txg = spa->spa_config_txg;
        spa_set_log_state(newspa, SPA_LOG_CLEAR);
 
@@ -5245,9 +5215,11 @@ spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
                        if (error == 0)
                                spa_history_log_internal(spa, "detach", tx,
                                    "vdev=%s", vml[c]->vdev_path);
+
                        vdev_free(vml[c]);
                }
        }
+       spa->spa_avz_action = AVZ_ACTION_REBUILD;
        vdev_config_dirty(spa->spa_root_vdev);
        spa->spa_config_splitting = NULL;
        nvlist_free(nvl);
@@ -5469,6 +5441,7 @@ spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
                } else {
                        error = SET_ERROR(EBUSY);
                }
+               spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX);
        } else if (spa->spa_l2cache.sav_vdevs != NULL &&
            nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
            ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
@@ -5480,6 +5453,7 @@ spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
                    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
                spa_load_l2cache(spa);
                spa->spa_l2cache.sav_sync = B_TRUE;
+               spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX);
        } else if (vd != NULL && vd->vdev_islog) {
                ASSERT(!locked);
                ASSERT(vd == vd->vdev_top);
@@ -5518,6 +5492,7 @@ spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
                 */
                spa_vdev_remove_from_namespace(spa, vd);
 
+               spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_DEV);
        } else if (vd != NULL) {
                /*
                 * Normal vdevs cannot be removed (yet).
@@ -5805,7 +5780,7 @@ spa_async_autoexpand(spa_t *spa, vdev_t *vd)
        if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
                return;
 
-       spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND);
+       spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_AUTOEXPAND);
 }
 
 static void
@@ -5912,13 +5887,34 @@ spa_async_resume(spa_t *spa)
        mutex_exit(&spa->spa_async_lock);
 }
 
+static boolean_t
+spa_async_tasks_pending(spa_t *spa)
+{
+       uint_t non_config_tasks;
+       uint_t config_task;
+       boolean_t config_task_suspended;
+
+       non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
+       config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
+       if (spa->spa_ccw_fail_time == 0) {
+               config_task_suspended = B_FALSE;
+       } else {
+               config_task_suspended =
+                   (gethrtime() - spa->spa_ccw_fail_time) <
+                   (zfs_ccw_retry_interval * NANOSEC);
+       }
+
+       return (non_config_tasks || (config_task && !config_task_suspended));
+}
+
 static void
 spa_async_dispatch(spa_t *spa)
 {
        mutex_enter(&spa->spa_async_lock);
-       if (spa->spa_async_tasks && !spa->spa_async_suspended &&
+       if (spa_async_tasks_pending(spa) &&
+           !spa->spa_async_suspended &&
            spa->spa_async_thread == NULL &&
-           rootdir != NULL && !vn_is_readonly(rootdir))
+           rootdir != NULL)
                spa->spa_async_thread = thread_create(NULL, 0,
                    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
        mutex_exit(&spa->spa_async_lock);
@@ -6060,16 +6056,120 @@ spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
        sav->sav_sync = B_FALSE;
 }
 
+/*
+ * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
+ * The all-vdev ZAP must be empty.
+ */
+static void
+spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
+{
+       spa_t *spa = vd->vdev_spa;
+       uint64_t i;
+
+       if (vd->vdev_top_zap != 0) {
+               VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
+                   vd->vdev_top_zap, tx));
+       }
+       if (vd->vdev_leaf_zap != 0) {
+               VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
+                   vd->vdev_leaf_zap, tx));
+       }
+       for (i = 0; i < vd->vdev_children; i++) {
+               spa_avz_build(vd->vdev_child[i], avz, tx);
+       }
+}
+
 static void
 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
 {
        nvlist_t *config;
 
-       if (list_is_empty(&spa->spa_config_dirty_list))
+       /*
+        * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
+        * its config may not be dirty but we still need to build per-vdev ZAPs.
+        * Similarly, if the pool is being assembled (e.g. after a split), we
+        * need to rebuild the AVZ although the config may not be dirty.
+        */
+       if (list_is_empty(&spa->spa_config_dirty_list) &&
+           spa->spa_avz_action == AVZ_ACTION_NONE)
                return;
 
        spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
 
+       ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
+           spa->spa_all_vdev_zaps != 0);
+
+       if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
+               zap_cursor_t zc;
+               zap_attribute_t za;
+
+               /* Make and build the new AVZ */
+               uint64_t new_avz = zap_create(spa->spa_meta_objset,
+                   DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
+               spa_avz_build(spa->spa_root_vdev, new_avz, tx);
+
+               /* Diff old AVZ with new one */
+               for (zap_cursor_init(&zc, spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps);
+                   zap_cursor_retrieve(&zc, &za) == 0;
+                   zap_cursor_advance(&zc)) {
+                       uint64_t vdzap = za.za_first_integer;
+                       if (zap_lookup_int(spa->spa_meta_objset, new_avz,
+                           vdzap) == ENOENT) {
+                               /*
+                                * ZAP is listed in old AVZ but not in new one;
+                                * destroy it
+                                */
+                               VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
+                                   tx));
+                       }
+               }
+
+               zap_cursor_fini(&zc);
+
+               /* Destroy the old AVZ */
+               VERIFY0(zap_destroy(spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps, tx));
+
+               /* Replace the old AVZ in the dir obj with the new one */
+               VERIFY0(zap_update(spa->spa_meta_objset,
+                   DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
+                   sizeof (new_avz), 1, &new_avz, tx));
+
+               spa->spa_all_vdev_zaps = new_avz;
+       } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
+               zap_cursor_t zc;
+               zap_attribute_t za;
+
+               /* Walk through the AVZ and destroy all listed ZAPs */
+               for (zap_cursor_init(&zc, spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps);
+                   zap_cursor_retrieve(&zc, &za) == 0;
+                   zap_cursor_advance(&zc)) {
+                       uint64_t zap = za.za_first_integer;
+                       VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
+               }
+
+               zap_cursor_fini(&zc);
+
+               /* Destroy and unlink the AVZ itself */
+               VERIFY0(zap_destroy(spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps, tx));
+               VERIFY0(zap_remove(spa->spa_meta_objset,
+                   DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
+               spa->spa_all_vdev_zaps = 0;
+       }
+
+       if (spa->spa_all_vdev_zaps == 0) {
+               spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
+                   DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
+                   DMU_POOL_VDEV_ZAP_MAP, tx);
+       }
+       spa->spa_avz_action = AVZ_ACTION_NONE;
+
+       /* Create ZAPs for vdevs that don't have them. */
+       vdev_construct_zaps(spa->spa_root_vdev, tx);
+
        config = spa_config_generate(spa, spa->spa_root_vdev,
            dmu_tx_get_txg(tx), B_FALSE);
 
@@ -6083,8 +6183,7 @@ spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
 
        spa_config_exit(spa, SCL_STATE, FTAG);
 
-       if (spa->spa_config_syncing)
-               nvlist_free(spa->spa_config_syncing);
+       nvlist_free(spa->spa_config_syncing);
        spa->spa_config_syncing = config;
 
        spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
@@ -6461,6 +6560,23 @@ spa_sync(spa_t *spa, uint64_t txg)
 
        } while (dmu_objset_is_dirty(mos, txg));
 
+#ifdef ZFS_DEBUG
+       if (!list_is_empty(&spa->spa_config_dirty_list)) {
+               /*
+                * Make sure that the number of ZAPs for all the vdevs matches
+                * the number of ZAPs in the per-vdev ZAP list. This only gets
+                * called if the config is dirty; otherwise there may be
+                * outstanding AVZ operations that weren't completed in
+                * spa_sync_config_object.
+                */
+               uint64_t all_vdev_zap_entry_count;
+               ASSERT0(zap_count(spa->spa_meta_objset,
+                   spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
+               ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
+                   all_vdev_zap_entry_count);
+       }
+#endif
+
        /*
         * Rewrite the vdev configuration (which includes the uberblock)
         * to commit the transaction group.
@@ -6491,16 +6607,10 @@ spa_sync(spa_t *spa, uint64_t txg)
                                if (svdcount == SPA_DVAS_PER_BP)
                                        break;
                        }
-                       error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
-                       if (error != 0)
-                               error = vdev_config_sync(svd, svdcount, txg,
-                                   B_TRUE);
+                       error = vdev_config_sync(svd, svdcount, txg);
                } else {
                        error = vdev_config_sync(rvd->vdev_child,
-                           rvd->vdev_children, txg, B_FALSE);
-                       if (error != 0)
-                               error = vdev_config_sync(rvd->vdev_child,
-                                   rvd->vdev_children, txg, B_TRUE);
+                           rvd->vdev_children, txg);
                }
 
                if (error == 0)
@@ -6720,7 +6830,8 @@ spa_has_active_shared_spare(spa_t *spa)
 }
 
 /*
- * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h.  The payload will be
+ * Post a zevent corresponding to the given sysevent.   The 'name' must be one
+ * of the event definitions in sys/sysevent/eventdefs.h.  The payload will be
  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
  * in the userland libzpool, as we don't want consumers to misinterpret ztest
  * or zdb as real changes.
@@ -6728,9 +6839,7 @@ spa_has_active_shared_spare(spa_t *spa)
 void
 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
 {
-#ifdef _KERNEL
-       zfs_ereport_post(name, spa, vd, NULL, 0, 0);
-#endif
+       zfs_post_sysevent(spa, vd, name);
 }
 
 #if defined(_KERNEL) && defined(HAVE_SPL)
@@ -6739,7 +6848,6 @@ EXPORT_SYMBOL(spa_open);
 EXPORT_SYMBOL(spa_open_rewind);
 EXPORT_SYMBOL(spa_get_stats);
 EXPORT_SYMBOL(spa_create);
-EXPORT_SYMBOL(spa_import_rootpool);
 EXPORT_SYMBOL(spa_import);
 EXPORT_SYMBOL(spa_tryimport);
 EXPORT_SYMBOL(spa_destroy);