1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 233377Seschrock * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens /* 30789Sahrens * This file contains all the routines used when modifying on-disk SPA state. 31789Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 32789Sahrens * pool. 33789Sahrens */ 34789Sahrens 35789Sahrens #include <sys/zfs_context.h> 361544Seschrock #include <sys/fm/fs/zfs.h> 37789Sahrens #include <sys/spa_impl.h> 38789Sahrens #include <sys/zio.h> 39789Sahrens #include <sys/zio_checksum.h> 40789Sahrens #include <sys/zio_compress.h> 41789Sahrens #include <sys/dmu.h> 42789Sahrens #include <sys/dmu_tx.h> 43789Sahrens #include <sys/zap.h> 44789Sahrens #include <sys/zil.h> 45789Sahrens #include <sys/vdev_impl.h> 46789Sahrens #include <sys/metaslab.h> 47789Sahrens #include <sys/uberblock_impl.h> 48789Sahrens #include <sys/txg.h> 49789Sahrens #include <sys/avl.h> 50789Sahrens #include <sys/dmu_traverse.h> 513912Slling #include <sys/dmu_objset.h> 52789Sahrens #include <sys/unique.h> 53789Sahrens #include <sys/dsl_pool.h> 543912Slling #include <sys/dsl_dataset.h> 55789Sahrens #include <sys/dsl_dir.h> 56789Sahrens #include <sys/dsl_prop.h> 573912Slling #include <sys/dsl_synctask.h> 58789Sahrens #include <sys/fs/zfs.h> 59789Sahrens #include <sys/callb.h> 603975Sek110237 #include <sys/systeminfo.h> 613975Sek110237 #include <sys/sunddi.h> 62789Sahrens 632986Sek110237 int zio_taskq_threads = 8; 642986Sek110237 65789Sahrens /* 66789Sahrens * ========================================================================== 67789Sahrens * SPA state manipulation (open/create/destroy/import/export) 68789Sahrens * ========================================================================== 69789Sahrens */ 70789Sahrens 711544Seschrock static int 721544Seschrock spa_error_entry_compare(const void *a, const void *b) 731544Seschrock { 741544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 751544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 761544Seschrock int ret; 771544Seschrock 781544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 791544Seschrock sizeof (zbookmark_t)); 801544Seschrock 811544Seschrock if (ret < 0) 821544Seschrock return (-1); 831544Seschrock else if (ret > 0) 841544Seschrock return (1); 851544Seschrock else 861544Seschrock return (0); 871544Seschrock } 881544Seschrock 891544Seschrock /* 901544Seschrock * Utility function which retrieves copies of the current logs and 911544Seschrock * re-initializes them in the process. 921544Seschrock */ 931544Seschrock void 941544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 951544Seschrock { 961544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 971544Seschrock 981544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 991544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 1001544Seschrock 1011544Seschrock avl_create(&spa->spa_errlist_scrub, 1021544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1031544Seschrock offsetof(spa_error_entry_t, se_avl)); 1041544Seschrock avl_create(&spa->spa_errlist_last, 1051544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1061544Seschrock offsetof(spa_error_entry_t, se_avl)); 1071544Seschrock } 1081544Seschrock 109789Sahrens /* 110789Sahrens * Activate an uninitialized pool. 111789Sahrens */ 112789Sahrens static void 113789Sahrens spa_activate(spa_t *spa) 114789Sahrens { 115789Sahrens int t; 116789Sahrens 117789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 118789Sahrens 119789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 120789Sahrens 121789Sahrens spa->spa_normal_class = metaslab_class_create(); 1224527Sperrin spa->spa_log_class = metaslab_class_create(); 123789Sahrens 124789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 125789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 1262986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 127789Sahrens TASKQ_PREPOPULATE); 128789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 1292986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 130789Sahrens TASKQ_PREPOPULATE); 131789Sahrens } 132789Sahrens 133789Sahrens rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 134789Sahrens 1354787Sahrens rprw_init(&spa->spa_config_lock); 1364787Sahrens 1372856Snd150628 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 1382856Snd150628 mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL); 1392856Snd150628 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 1402856Snd150628 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 1412856Snd150628 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 1422856Snd150628 mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL); 1432926Sek110237 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 1443912Slling mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 1452856Snd150628 146789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 147789Sahrens offsetof(vdev_t, vdev_dirty_node)); 148789Sahrens 149789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 150789Sahrens offsetof(struct vdev, vdev_txg_node)); 1511544Seschrock 1521544Seschrock avl_create(&spa->spa_errlist_scrub, 1531544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1541544Seschrock offsetof(spa_error_entry_t, se_avl)); 1551544Seschrock avl_create(&spa->spa_errlist_last, 1561544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1571544Seschrock offsetof(spa_error_entry_t, se_avl)); 158789Sahrens } 159789Sahrens 160789Sahrens /* 161789Sahrens * Opposite of spa_activate(). 162789Sahrens */ 163789Sahrens static void 164789Sahrens spa_deactivate(spa_t *spa) 165789Sahrens { 166789Sahrens int t; 167789Sahrens 168789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 169789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 170789Sahrens ASSERT(spa->spa_root_vdev == NULL); 171789Sahrens 172789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 173789Sahrens 174789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 175789Sahrens 176789Sahrens list_destroy(&spa->spa_dirty_list); 177789Sahrens 178789Sahrens rw_destroy(&spa->spa_traverse_lock); 179789Sahrens 180789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 181789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 182789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 183789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 184789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 185789Sahrens } 186789Sahrens 187789Sahrens metaslab_class_destroy(spa->spa_normal_class); 188789Sahrens spa->spa_normal_class = NULL; 189789Sahrens 1904527Sperrin metaslab_class_destroy(spa->spa_log_class); 1914527Sperrin spa->spa_log_class = NULL; 1924527Sperrin 1931544Seschrock /* 1941544Seschrock * If this was part of an import or the open otherwise failed, we may 1951544Seschrock * still have errors left in the queues. Empty them just in case. 1961544Seschrock */ 1971544Seschrock spa_errlog_drain(spa); 1981544Seschrock 1991544Seschrock avl_destroy(&spa->spa_errlist_scrub); 2001544Seschrock avl_destroy(&spa->spa_errlist_last); 2011544Seschrock 202789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 203789Sahrens } 204789Sahrens 205789Sahrens /* 206789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 207789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 208789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 209789Sahrens * All vdev validation is done by the vdev_alloc() routine. 210789Sahrens */ 2112082Seschrock static int 2122082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 2132082Seschrock uint_t id, int atype) 214789Sahrens { 215789Sahrens nvlist_t **child; 216789Sahrens uint_t c, children; 2172082Seschrock int error; 2182082Seschrock 2192082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 2202082Seschrock return (error); 2212082Seschrock 2222082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 2232082Seschrock return (0); 224789Sahrens 225789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 226789Sahrens &child, &children) != 0) { 2272082Seschrock vdev_free(*vdp); 2282082Seschrock *vdp = NULL; 2292082Seschrock return (EINVAL); 230789Sahrens } 231789Sahrens 232789Sahrens for (c = 0; c < children; c++) { 2332082Seschrock vdev_t *vd; 2342082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 2352082Seschrock atype)) != 0) { 2362082Seschrock vdev_free(*vdp); 2372082Seschrock *vdp = NULL; 2382082Seschrock return (error); 239789Sahrens } 240789Sahrens } 241789Sahrens 2422082Seschrock ASSERT(*vdp != NULL); 2432082Seschrock 2442082Seschrock return (0); 245789Sahrens } 246789Sahrens 247789Sahrens /* 248789Sahrens * Opposite of spa_load(). 249789Sahrens */ 250789Sahrens static void 251789Sahrens spa_unload(spa_t *spa) 252789Sahrens { 2532082Seschrock int i; 2542082Seschrock 255789Sahrens /* 2561544Seschrock * Stop async tasks. 2571544Seschrock */ 2581544Seschrock spa_async_suspend(spa); 2591544Seschrock 2601544Seschrock /* 261789Sahrens * Stop syncing. 262789Sahrens */ 263789Sahrens if (spa->spa_sync_on) { 264789Sahrens txg_sync_stop(spa->spa_dsl_pool); 265789Sahrens spa->spa_sync_on = B_FALSE; 266789Sahrens } 267789Sahrens 268789Sahrens /* 269789Sahrens * Wait for any outstanding prefetch I/O to complete. 270789Sahrens */ 2711544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2721544Seschrock spa_config_exit(spa, FTAG); 273789Sahrens 274789Sahrens /* 275789Sahrens * Close the dsl pool. 276789Sahrens */ 277789Sahrens if (spa->spa_dsl_pool) { 278789Sahrens dsl_pool_close(spa->spa_dsl_pool); 279789Sahrens spa->spa_dsl_pool = NULL; 280789Sahrens } 281789Sahrens 282789Sahrens /* 283789Sahrens * Close all vdevs. 284789Sahrens */ 2851585Sbonwick if (spa->spa_root_vdev) 286789Sahrens vdev_free(spa->spa_root_vdev); 2871585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 2881544Seschrock 2892082Seschrock for (i = 0; i < spa->spa_nspares; i++) 2902082Seschrock vdev_free(spa->spa_spares[i]); 2912082Seschrock if (spa->spa_spares) { 2922082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 2932082Seschrock spa->spa_spares = NULL; 2942082Seschrock } 2952082Seschrock if (spa->spa_sparelist) { 2962082Seschrock nvlist_free(spa->spa_sparelist); 2972082Seschrock spa->spa_sparelist = NULL; 2982082Seschrock } 2992082Seschrock 3001544Seschrock spa->spa_async_suspended = 0; 301789Sahrens } 302789Sahrens 303789Sahrens /* 3042082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 3052082Seschrock * this pool. When this is called, we have some form of basic information in 3062082Seschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 3072082Seschrock * re-generate a more complete list including status information. 3082082Seschrock */ 3092082Seschrock static void 3102082Seschrock spa_load_spares(spa_t *spa) 3112082Seschrock { 3122082Seschrock nvlist_t **spares; 3132082Seschrock uint_t nspares; 3142082Seschrock int i; 3153377Seschrock vdev_t *vd, *tvd; 3162082Seschrock 3172082Seschrock /* 3182082Seschrock * First, close and free any existing spare vdevs. 3192082Seschrock */ 3202082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3213377Seschrock vd = spa->spa_spares[i]; 3223377Seschrock 3233377Seschrock /* Undo the call to spa_activate() below */ 3243377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 3253377Seschrock tvd->vdev_isspare) 3263377Seschrock spa_spare_remove(tvd); 3273377Seschrock vdev_close(vd); 3283377Seschrock vdev_free(vd); 3292082Seschrock } 3303377Seschrock 3312082Seschrock if (spa->spa_spares) 3322082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 3332082Seschrock 3342082Seschrock if (spa->spa_sparelist == NULL) 3352082Seschrock nspares = 0; 3362082Seschrock else 3372082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 3382082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 3392082Seschrock 3402082Seschrock spa->spa_nspares = (int)nspares; 3412082Seschrock spa->spa_spares = NULL; 3422082Seschrock 3432082Seschrock if (nspares == 0) 3442082Seschrock return; 3452082Seschrock 3462082Seschrock /* 3472082Seschrock * Construct the array of vdevs, opening them to get status in the 3483377Seschrock * process. For each spare, there is potentially two different vdev_t 3493377Seschrock * structures associated with it: one in the list of spares (used only 3503377Seschrock * for basic validation purposes) and one in the active vdev 3513377Seschrock * configuration (if it's spared in). During this phase we open and 3523377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 3533377Seschrock * active configuration, then we also mark this vdev as an active spare. 3542082Seschrock */ 3552082Seschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 3562082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3572082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 3582082Seschrock VDEV_ALLOC_SPARE) == 0); 3592082Seschrock ASSERT(vd != NULL); 3602082Seschrock 3612082Seschrock spa->spa_spares[i] = vd; 3622082Seschrock 3633377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 3643377Seschrock if (!tvd->vdev_isspare) 3653377Seschrock spa_spare_add(tvd); 3663377Seschrock 3673377Seschrock /* 3683377Seschrock * We only mark the spare active if we were successfully 3693377Seschrock * able to load the vdev. Otherwise, importing a pool 3703377Seschrock * with a bad active spare would result in strange 3713377Seschrock * behavior, because multiple pool would think the spare 3723377Seschrock * is actively in use. 3733377Seschrock * 3743377Seschrock * There is a vulnerability here to an equally bizarre 3753377Seschrock * circumstance, where a dead active spare is later 3763377Seschrock * brought back to life (onlined or otherwise). Given 3773377Seschrock * the rarity of this scenario, and the extra complexity 3783377Seschrock * it adds, we ignore the possibility. 3793377Seschrock */ 3803377Seschrock if (!vdev_is_dead(tvd)) 3813377Seschrock spa_spare_activate(tvd); 3823377Seschrock } 3833377Seschrock 3842082Seschrock if (vdev_open(vd) != 0) 3852082Seschrock continue; 3862082Seschrock 3872082Seschrock vd->vdev_top = vd; 3882082Seschrock (void) vdev_validate_spare(vd); 3892082Seschrock } 3902082Seschrock 3912082Seschrock /* 3922082Seschrock * Recompute the stashed list of spares, with status information 3932082Seschrock * this time. 3942082Seschrock */ 3952082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3962082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3972082Seschrock 3982082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 3992082Seschrock for (i = 0; i < spa->spa_nspares; i++) 4002082Seschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 4012082Seschrock B_TRUE, B_TRUE); 4022082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 4032082Seschrock spares, spa->spa_nspares) == 0); 4042082Seschrock for (i = 0; i < spa->spa_nspares; i++) 4052082Seschrock nvlist_free(spares[i]); 4062082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 4072082Seschrock } 4082082Seschrock 4092082Seschrock static int 4102082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 4112082Seschrock { 4122082Seschrock dmu_buf_t *db; 4132082Seschrock char *packed = NULL; 4142082Seschrock size_t nvsize = 0; 4152082Seschrock int error; 4162082Seschrock *value = NULL; 4172082Seschrock 4182082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 4192082Seschrock nvsize = *(uint64_t *)db->db_data; 4202082Seschrock dmu_buf_rele(db, FTAG); 4212082Seschrock 4222082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 4232082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 4242082Seschrock if (error == 0) 4252082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 4262082Seschrock kmem_free(packed, nvsize); 4272082Seschrock 4282082Seschrock return (error); 4292082Seschrock } 4302082Seschrock 4312082Seschrock /* 4324451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 4334451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 4344451Seschrock */ 4354451Seschrock static void 4364451Seschrock spa_check_removed(vdev_t *vd) 4374451Seschrock { 4384451Seschrock int c; 4394451Seschrock 4404451Seschrock for (c = 0; c < vd->vdev_children; c++) 4414451Seschrock spa_check_removed(vd->vdev_child[c]); 4424451Seschrock 4434451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 4444451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 4454451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 4464451Seschrock } 4474451Seschrock } 4484451Seschrock 4494451Seschrock /* 450789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 4511544Seschrock * source of configuration information. 452789Sahrens */ 453789Sahrens static int 4541544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 455789Sahrens { 456789Sahrens int error = 0; 457789Sahrens nvlist_t *nvroot = NULL; 458789Sahrens vdev_t *rvd; 459789Sahrens uberblock_t *ub = &spa->spa_uberblock; 4601635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 461789Sahrens uint64_t pool_guid; 4622082Seschrock uint64_t version; 463789Sahrens zio_t *zio; 4644451Seschrock uint64_t autoreplace = 0; 465789Sahrens 4661544Seschrock spa->spa_load_state = state; 4671635Sbonwick 468789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 4691733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 4701544Seschrock error = EINVAL; 4711544Seschrock goto out; 4721544Seschrock } 473789Sahrens 4742082Seschrock /* 4752082Seschrock * Versioning wasn't explicitly added to the label until later, so if 4762082Seschrock * it's not present treat it as the initial version. 4772082Seschrock */ 4782082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 4794577Sahrens version = SPA_VERSION_INITIAL; 4802082Seschrock 4811733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 4821733Sbonwick &spa->spa_config_txg); 4831733Sbonwick 4841635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 4851544Seschrock spa_guid_exists(pool_guid, 0)) { 4861544Seschrock error = EEXIST; 4871544Seschrock goto out; 4881544Seschrock } 489789Sahrens 4902174Seschrock spa->spa_load_guid = pool_guid; 4912174Seschrock 492789Sahrens /* 4932082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 4942082Seschrock * value that will be returned by spa_version() since parsing the 4952082Seschrock * configuration requires knowing the version number. 496789Sahrens */ 4971544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 4982082Seschrock spa->spa_ubsync.ub_version = version; 4992082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 5001544Seschrock spa_config_exit(spa, FTAG); 501789Sahrens 5022082Seschrock if (error != 0) 5031544Seschrock goto out; 504789Sahrens 5051585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 506789Sahrens ASSERT(spa_guid(spa) == pool_guid); 507789Sahrens 508789Sahrens /* 509789Sahrens * Try to open all vdevs, loading each label in the process. 510789Sahrens */ 5114070Smc142369 error = vdev_open(rvd); 5124070Smc142369 if (error != 0) 5131544Seschrock goto out; 514789Sahrens 515789Sahrens /* 5161986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 5171986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 5181986Seschrock * flag. 5191986Seschrock */ 5201986Seschrock spa_config_enter(spa, RW_READER, FTAG); 5211986Seschrock error = vdev_validate(rvd); 5221986Seschrock spa_config_exit(spa, FTAG); 5231986Seschrock 5244070Smc142369 if (error != 0) 5251986Seschrock goto out; 5261986Seschrock 5271986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 5281986Seschrock error = ENXIO; 5291986Seschrock goto out; 5301986Seschrock } 5311986Seschrock 5321986Seschrock /* 533789Sahrens * Find the best uberblock. 534789Sahrens */ 535789Sahrens bzero(ub, sizeof (uberblock_t)); 536789Sahrens 537789Sahrens zio = zio_root(spa, NULL, NULL, 538789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 539789Sahrens vdev_uberblock_load(zio, rvd, ub); 540789Sahrens error = zio_wait(zio); 541789Sahrens 542789Sahrens /* 543789Sahrens * If we weren't able to find a single valid uberblock, return failure. 544789Sahrens */ 545789Sahrens if (ub->ub_txg == 0) { 5461760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5471760Seschrock VDEV_AUX_CORRUPT_DATA); 5481544Seschrock error = ENXIO; 5491544Seschrock goto out; 5501544Seschrock } 5511544Seschrock 5521544Seschrock /* 5531544Seschrock * If the pool is newer than the code, we can't open it. 5541544Seschrock */ 5554577Sahrens if (ub->ub_version > SPA_VERSION) { 5561760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5571760Seschrock VDEV_AUX_VERSION_NEWER); 5581544Seschrock error = ENOTSUP; 5591544Seschrock goto out; 560789Sahrens } 561789Sahrens 562789Sahrens /* 563789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 564789Sahrens * incomplete configuration. 565789Sahrens */ 5661732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 5671544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5681544Seschrock VDEV_AUX_BAD_GUID_SUM); 5691544Seschrock error = ENXIO; 5701544Seschrock goto out; 571789Sahrens } 572789Sahrens 573789Sahrens /* 574789Sahrens * Initialize internal SPA structures. 575789Sahrens */ 576789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 577789Sahrens spa->spa_ubsync = spa->spa_uberblock; 578789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 5791544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 5801544Seschrock if (error) { 5811544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5821544Seschrock VDEV_AUX_CORRUPT_DATA); 5831544Seschrock goto out; 5841544Seschrock } 585789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 586789Sahrens 5871544Seschrock if (zap_lookup(spa->spa_meta_objset, 588789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 5891544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 5901544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5911544Seschrock VDEV_AUX_CORRUPT_DATA); 5921544Seschrock error = EIO; 5931544Seschrock goto out; 5941544Seschrock } 595789Sahrens 596789Sahrens if (!mosconfig) { 5972082Seschrock nvlist_t *newconfig; 5983975Sek110237 uint64_t hostid; 5992082Seschrock 6002082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 6011544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6021544Seschrock VDEV_AUX_CORRUPT_DATA); 6031544Seschrock error = EIO; 6041544Seschrock goto out; 6051544Seschrock } 606789Sahrens 6073975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 6083975Sek110237 &hostid) == 0) { 6093975Sek110237 char *hostname; 6103975Sek110237 unsigned long myhostid = 0; 6113975Sek110237 6123975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 6133975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 6143975Sek110237 6153975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 6164178Slling if (hostid != 0 && myhostid != 0 && 6174178Slling (unsigned long)hostid != myhostid) { 6183975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 6193975Sek110237 "loaded as it was last accessed by " 6203975Sek110237 "another system (host: %s hostid: 0x%lx). " 6213975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 6223975Sek110237 spa->spa_name, hostname, 6233975Sek110237 (unsigned long)hostid); 6243975Sek110237 error = EBADF; 6253975Sek110237 goto out; 6263975Sek110237 } 6273975Sek110237 } 6283975Sek110237 629789Sahrens spa_config_set(spa, newconfig); 630789Sahrens spa_unload(spa); 631789Sahrens spa_deactivate(spa); 632789Sahrens spa_activate(spa); 633789Sahrens 6341544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 6351544Seschrock } 6361544Seschrock 6371544Seschrock if (zap_lookup(spa->spa_meta_objset, 6381544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 6391544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 6401544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6411544Seschrock VDEV_AUX_CORRUPT_DATA); 6421544Seschrock error = EIO; 6431544Seschrock goto out; 644789Sahrens } 645789Sahrens 6461544Seschrock /* 6472082Seschrock * Load the bit that tells us to use the new accounting function 6482082Seschrock * (raid-z deflation). If we have an older pool, this will not 6492082Seschrock * be present. 6502082Seschrock */ 6512082Seschrock error = zap_lookup(spa->spa_meta_objset, 6522082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 6532082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 6542082Seschrock if (error != 0 && error != ENOENT) { 6552082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6562082Seschrock VDEV_AUX_CORRUPT_DATA); 6572082Seschrock error = EIO; 6582082Seschrock goto out; 6592082Seschrock } 6602082Seschrock 6612082Seschrock /* 6621544Seschrock * Load the persistent error log. If we have an older pool, this will 6631544Seschrock * not be present. 6641544Seschrock */ 6651544Seschrock error = zap_lookup(spa->spa_meta_objset, 6661544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 6671544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 6681807Sbonwick if (error != 0 && error != ENOENT) { 6691544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6701544Seschrock VDEV_AUX_CORRUPT_DATA); 6711544Seschrock error = EIO; 6721544Seschrock goto out; 6731544Seschrock } 6741544Seschrock 6751544Seschrock error = zap_lookup(spa->spa_meta_objset, 6761544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 6771544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 6781544Seschrock if (error != 0 && error != ENOENT) { 6791544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6801544Seschrock VDEV_AUX_CORRUPT_DATA); 6811544Seschrock error = EIO; 6821544Seschrock goto out; 6831544Seschrock } 684789Sahrens 685789Sahrens /* 6862926Sek110237 * Load the history object. If we have an older pool, this 6872926Sek110237 * will not be present. 6882926Sek110237 */ 6892926Sek110237 error = zap_lookup(spa->spa_meta_objset, 6902926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 6912926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 6922926Sek110237 if (error != 0 && error != ENOENT) { 6932926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6942926Sek110237 VDEV_AUX_CORRUPT_DATA); 6952926Sek110237 error = EIO; 6962926Sek110237 goto out; 6972926Sek110237 } 6982926Sek110237 6992926Sek110237 /* 7002082Seschrock * Load any hot spares for this pool. 7012082Seschrock */ 7022082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7032082Seschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 7042082Seschrock if (error != 0 && error != ENOENT) { 7052082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7062082Seschrock VDEV_AUX_CORRUPT_DATA); 7072082Seschrock error = EIO; 7082082Seschrock goto out; 7092082Seschrock } 7102082Seschrock if (error == 0) { 7114577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 7122082Seschrock if (load_nvlist(spa, spa->spa_spares_object, 7132082Seschrock &spa->spa_sparelist) != 0) { 7142082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7152082Seschrock VDEV_AUX_CORRUPT_DATA); 7162082Seschrock error = EIO; 7172082Seschrock goto out; 7182082Seschrock } 7192082Seschrock 7202082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 7212082Seschrock spa_load_spares(spa); 7222082Seschrock spa_config_exit(spa, FTAG); 7232082Seschrock } 7242082Seschrock 7254543Smarks spa->spa_delegation = zfs_prop_default_numeric(ZPOOL_PROP_DELEGATION); 7264543Smarks 7273912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7283912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 7293912Slling 7303912Slling if (error && error != ENOENT) { 7313912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7323912Slling VDEV_AUX_CORRUPT_DATA); 7333912Slling error = EIO; 7343912Slling goto out; 7353912Slling } 7363912Slling 7373912Slling if (error == 0) { 7383912Slling (void) zap_lookup(spa->spa_meta_objset, 7393912Slling spa->spa_pool_props_object, 7404451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 7413912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 7424451Seschrock (void) zap_lookup(spa->spa_meta_objset, 7434451Seschrock spa->spa_pool_props_object, 7444451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 7454451Seschrock sizeof (uint64_t), 1, &autoreplace); 7464543Smarks (void) zap_lookup(spa->spa_meta_objset, 7474543Smarks spa->spa_pool_props_object, 7484543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 7494543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 7503912Slling } 7513912Slling 7522082Seschrock /* 7534451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 7544451Seschrock * the ZFS DE that it should not issue any faults for unopenable 7554451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 7564451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 7574451Seschrock * over. 7584451Seschrock */ 7594451Seschrock if (autoreplace) 7604451Seschrock spa_check_removed(spa->spa_root_vdev); 7614451Seschrock 7624451Seschrock /* 7631986Seschrock * Load the vdev state for all toplevel vdevs. 764789Sahrens */ 7651986Seschrock vdev_load(rvd); 766789Sahrens 767789Sahrens /* 768789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 769789Sahrens */ 7701544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 771789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 7721544Seschrock spa_config_exit(spa, FTAG); 773789Sahrens 774789Sahrens /* 775789Sahrens * Check the state of the root vdev. If it can't be opened, it 776789Sahrens * indicates one or more toplevel vdevs are faulted. 777789Sahrens */ 7781544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 7791544Seschrock error = ENXIO; 7801544Seschrock goto out; 7811544Seschrock } 782789Sahrens 7831544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 7841635Sbonwick dmu_tx_t *tx; 7851635Sbonwick int need_update = B_FALSE; 7861585Sbonwick int c; 7871601Sbonwick 7881635Sbonwick /* 7891635Sbonwick * Claim log blocks that haven't been committed yet. 7901635Sbonwick * This must all happen in a single txg. 7911635Sbonwick */ 7921601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 793789Sahrens spa_first_txg(spa)); 7942417Sahrens (void) dmu_objset_find(spa->spa_name, 7952417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 796789Sahrens dmu_tx_commit(tx); 797789Sahrens 798789Sahrens spa->spa_sync_on = B_TRUE; 799789Sahrens txg_sync_start(spa->spa_dsl_pool); 800789Sahrens 801789Sahrens /* 802789Sahrens * Wait for all claims to sync. 803789Sahrens */ 804789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 8051585Sbonwick 8061585Sbonwick /* 8071635Sbonwick * If the config cache is stale, or we have uninitialized 8081635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 8091585Sbonwick */ 8101635Sbonwick if (config_cache_txg != spa->spa_config_txg || 8111635Sbonwick state == SPA_LOAD_IMPORT) 8121635Sbonwick need_update = B_TRUE; 8131635Sbonwick 8141635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 8151635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 8161635Sbonwick need_update = B_TRUE; 8171585Sbonwick 8181585Sbonwick /* 8191635Sbonwick * Update the config cache asychronously in case we're the 8201635Sbonwick * root pool, in which case the config cache isn't writable yet. 8211585Sbonwick */ 8221635Sbonwick if (need_update) 8231635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 824789Sahrens } 825789Sahrens 8261544Seschrock error = 0; 8271544Seschrock out: 8282082Seschrock if (error && error != EBADF) 8291544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 8301544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 8311544Seschrock spa->spa_ena = 0; 8321544Seschrock 8331544Seschrock return (error); 834789Sahrens } 835789Sahrens 836789Sahrens /* 837789Sahrens * Pool Open/Import 838789Sahrens * 839789Sahrens * The import case is identical to an open except that the configuration is sent 840789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 841789Sahrens * case of an open, the pool configuration will exist in the 8424451Seschrock * POOL_STATE_UNINITIALIZED state. 843789Sahrens * 844789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 845789Sahrens * the same time open the pool, without having to keep around the spa_t in some 846789Sahrens * ambiguous state. 847789Sahrens */ 848789Sahrens static int 849789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 850789Sahrens { 851789Sahrens spa_t *spa; 852789Sahrens int error; 853789Sahrens int loaded = B_FALSE; 854789Sahrens int locked = B_FALSE; 855789Sahrens 856789Sahrens *spapp = NULL; 857789Sahrens 858789Sahrens /* 859789Sahrens * As disgusting as this is, we need to support recursive calls to this 860789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 861789Sahrens * up calling spa_open() again. The real fix is to figure out how to 862789Sahrens * avoid dsl_dir_open() calling this in the first place. 863789Sahrens */ 864789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 865789Sahrens mutex_enter(&spa_namespace_lock); 866789Sahrens locked = B_TRUE; 867789Sahrens } 868789Sahrens 869789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 870789Sahrens if (locked) 871789Sahrens mutex_exit(&spa_namespace_lock); 872789Sahrens return (ENOENT); 873789Sahrens } 874789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 875789Sahrens 876789Sahrens spa_activate(spa); 877789Sahrens 8781635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 879789Sahrens 880789Sahrens if (error == EBADF) { 881789Sahrens /* 8821986Seschrock * If vdev_validate() returns failure (indicated by 8831986Seschrock * EBADF), it indicates that one of the vdevs indicates 8841986Seschrock * that the pool has been exported or destroyed. If 8851986Seschrock * this is the case, the config cache is out of sync and 8861986Seschrock * we should remove the pool from the namespace. 887789Sahrens */ 8882082Seschrock zfs_post_ok(spa, NULL); 889789Sahrens spa_unload(spa); 890789Sahrens spa_deactivate(spa); 891789Sahrens spa_remove(spa); 892789Sahrens spa_config_sync(); 893789Sahrens if (locked) 894789Sahrens mutex_exit(&spa_namespace_lock); 895789Sahrens return (ENOENT); 8961544Seschrock } 8971544Seschrock 8981544Seschrock if (error) { 899789Sahrens /* 900789Sahrens * We can't open the pool, but we still have useful 901789Sahrens * information: the state of each vdev after the 902789Sahrens * attempted vdev_open(). Return this to the user. 903789Sahrens */ 9041635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 9051635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 906789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 907789Sahrens B_TRUE); 9081635Sbonwick spa_config_exit(spa, FTAG); 9091635Sbonwick } 910789Sahrens spa_unload(spa); 911789Sahrens spa_deactivate(spa); 9121544Seschrock spa->spa_last_open_failed = B_TRUE; 913789Sahrens if (locked) 914789Sahrens mutex_exit(&spa_namespace_lock); 915789Sahrens *spapp = NULL; 916789Sahrens return (error); 9171544Seschrock } else { 9181544Seschrock zfs_post_ok(spa, NULL); 9191544Seschrock spa->spa_last_open_failed = B_FALSE; 920789Sahrens } 921789Sahrens 922789Sahrens loaded = B_TRUE; 923789Sahrens } 924789Sahrens 925789Sahrens spa_open_ref(spa, tag); 9264451Seschrock 9274451Seschrock /* 9284451Seschrock * If we just loaded the pool, resilver anything that's out of date. 9294451Seschrock */ 9304451Seschrock if (loaded && (spa_mode & FWRITE)) 9314451Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 9324451Seschrock 933789Sahrens if (locked) 934789Sahrens mutex_exit(&spa_namespace_lock); 935789Sahrens 936789Sahrens *spapp = spa; 937789Sahrens 938789Sahrens if (config != NULL) { 9391544Seschrock spa_config_enter(spa, RW_READER, FTAG); 940789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 9411544Seschrock spa_config_exit(spa, FTAG); 942789Sahrens } 943789Sahrens 944789Sahrens return (0); 945789Sahrens } 946789Sahrens 947789Sahrens int 948789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 949789Sahrens { 950789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 951789Sahrens } 952789Sahrens 9531544Seschrock /* 9541544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 9551544Seschrock * preventing it from being exported or destroyed. 9561544Seschrock */ 9571544Seschrock spa_t * 9581544Seschrock spa_inject_addref(char *name) 9591544Seschrock { 9601544Seschrock spa_t *spa; 9611544Seschrock 9621544Seschrock mutex_enter(&spa_namespace_lock); 9631544Seschrock if ((spa = spa_lookup(name)) == NULL) { 9641544Seschrock mutex_exit(&spa_namespace_lock); 9651544Seschrock return (NULL); 9661544Seschrock } 9671544Seschrock spa->spa_inject_ref++; 9681544Seschrock mutex_exit(&spa_namespace_lock); 9691544Seschrock 9701544Seschrock return (spa); 9711544Seschrock } 9721544Seschrock 9731544Seschrock void 9741544Seschrock spa_inject_delref(spa_t *spa) 9751544Seschrock { 9761544Seschrock mutex_enter(&spa_namespace_lock); 9771544Seschrock spa->spa_inject_ref--; 9781544Seschrock mutex_exit(&spa_namespace_lock); 9791544Seschrock } 9801544Seschrock 9812082Seschrock static void 9822082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 9832082Seschrock { 9842082Seschrock nvlist_t **spares; 9852082Seschrock uint_t i, nspares; 9862082Seschrock nvlist_t *nvroot; 9872082Seschrock uint64_t guid; 9882082Seschrock vdev_stat_t *vs; 9892082Seschrock uint_t vsc; 9903377Seschrock uint64_t pool; 9912082Seschrock 9922082Seschrock if (spa->spa_nspares == 0) 9932082Seschrock return; 9942082Seschrock 9952082Seschrock VERIFY(nvlist_lookup_nvlist(config, 9962082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 9972082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 9982082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 9992082Seschrock if (nspares != 0) { 10002082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 10012082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 10022082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 10032082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 10042082Seschrock 10052082Seschrock /* 10062082Seschrock * Go through and find any spares which have since been 10072082Seschrock * repurposed as an active spare. If this is the case, update 10082082Seschrock * their status appropriately. 10092082Seschrock */ 10102082Seschrock for (i = 0; i < nspares; i++) { 10112082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 10122082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 10133377Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 10142082Seschrock VERIFY(nvlist_lookup_uint64_array( 10152082Seschrock spares[i], ZPOOL_CONFIG_STATS, 10162082Seschrock (uint64_t **)&vs, &vsc) == 0); 10172082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 10182082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 10192082Seschrock } 10202082Seschrock } 10212082Seschrock } 10222082Seschrock } 10232082Seschrock 1024789Sahrens int 10251544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1026789Sahrens { 1027789Sahrens int error; 1028789Sahrens spa_t *spa; 1029789Sahrens 1030789Sahrens *config = NULL; 1031789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1032789Sahrens 10332082Seschrock if (spa && *config != NULL) { 10341544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 10351544Seschrock spa_get_errlog_size(spa)) == 0); 10361544Seschrock 10372082Seschrock spa_add_spares(spa, *config); 10382082Seschrock } 10392082Seschrock 10401544Seschrock /* 10411544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 10421544Seschrock * and call spa_lookup() directly. 10431544Seschrock */ 10441544Seschrock if (altroot) { 10451544Seschrock if (spa == NULL) { 10461544Seschrock mutex_enter(&spa_namespace_lock); 10471544Seschrock spa = spa_lookup(name); 10481544Seschrock if (spa) 10491544Seschrock spa_altroot(spa, altroot, buflen); 10501544Seschrock else 10511544Seschrock altroot[0] = '\0'; 10521544Seschrock spa = NULL; 10531544Seschrock mutex_exit(&spa_namespace_lock); 10541544Seschrock } else { 10551544Seschrock spa_altroot(spa, altroot, buflen); 10561544Seschrock } 10571544Seschrock } 10581544Seschrock 1059789Sahrens if (spa != NULL) 1060789Sahrens spa_close(spa, FTAG); 1061789Sahrens 1062789Sahrens return (error); 1063789Sahrens } 1064789Sahrens 1065789Sahrens /* 10662082Seschrock * Validate that the 'spares' array is well formed. We must have an array of 10673377Seschrock * nvlists, each which describes a valid leaf vdev. If this is an import (mode 10683377Seschrock * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 10693377Seschrock * as they are well-formed. 10702082Seschrock */ 10712082Seschrock static int 10722082Seschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 10732082Seschrock { 10742082Seschrock nvlist_t **spares; 10752082Seschrock uint_t i, nspares; 10762082Seschrock vdev_t *vd; 10772082Seschrock int error; 10782082Seschrock 10792082Seschrock /* 10802082Seschrock * It's acceptable to have no spares specified. 10812082Seschrock */ 10822082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10832082Seschrock &spares, &nspares) != 0) 10842082Seschrock return (0); 10852082Seschrock 10862082Seschrock if (nspares == 0) 10872082Seschrock return (EINVAL); 10882082Seschrock 10892082Seschrock /* 10902082Seschrock * Make sure the pool is formatted with a version that supports hot 10912082Seschrock * spares. 10922082Seschrock */ 10934577Sahrens if (spa_version(spa) < SPA_VERSION_SPARES) 10942082Seschrock return (ENOTSUP); 10952082Seschrock 10963377Seschrock /* 10973377Seschrock * Set the pending spare list so we correctly handle device in-use 10983377Seschrock * checking. 10993377Seschrock */ 11003377Seschrock spa->spa_pending_spares = spares; 11013377Seschrock spa->spa_pending_nspares = nspares; 11023377Seschrock 11032082Seschrock for (i = 0; i < nspares; i++) { 11042082Seschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 11052082Seschrock mode)) != 0) 11063377Seschrock goto out; 11072082Seschrock 11082082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 11092082Seschrock vdev_free(vd); 11103377Seschrock error = EINVAL; 11113377Seschrock goto out; 11122082Seschrock } 11132082Seschrock 11142082Seschrock vd->vdev_top = vd; 11153377Seschrock 11163377Seschrock if ((error = vdev_open(vd)) == 0 && 11173377Seschrock (error = vdev_label_init(vd, crtxg, 11183377Seschrock VDEV_LABEL_SPARE)) == 0) { 11193377Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 11203377Seschrock vd->vdev_guid) == 0); 11212082Seschrock } 11222082Seschrock 11232082Seschrock vdev_free(vd); 11243377Seschrock 11253377Seschrock if (error && mode != VDEV_ALLOC_SPARE) 11263377Seschrock goto out; 11273377Seschrock else 11283377Seschrock error = 0; 11292082Seschrock } 11302082Seschrock 11313377Seschrock out: 11323377Seschrock spa->spa_pending_spares = NULL; 11333377Seschrock spa->spa_pending_nspares = 0; 11343377Seschrock return (error); 11352082Seschrock } 11362082Seschrock 11372082Seschrock /* 1138789Sahrens * Pool Creation 1139789Sahrens */ 1140789Sahrens int 11414715Sek110237 spa_create(const char *pool, nvlist_t *nvroot, const char *altroot, 11424715Sek110237 const char *history_str) 1143789Sahrens { 1144789Sahrens spa_t *spa; 11451635Sbonwick vdev_t *rvd; 1146789Sahrens dsl_pool_t *dp; 1147789Sahrens dmu_tx_t *tx; 11482082Seschrock int c, error = 0; 1149789Sahrens uint64_t txg = TXG_INITIAL; 11502082Seschrock nvlist_t **spares; 11512082Seschrock uint_t nspares; 1152789Sahrens 1153789Sahrens /* 1154789Sahrens * If this pool already exists, return failure. 1155789Sahrens */ 1156789Sahrens mutex_enter(&spa_namespace_lock); 1157789Sahrens if (spa_lookup(pool) != NULL) { 1158789Sahrens mutex_exit(&spa_namespace_lock); 1159789Sahrens return (EEXIST); 1160789Sahrens } 1161789Sahrens 1162789Sahrens /* 1163789Sahrens * Allocate a new spa_t structure. 1164789Sahrens */ 11651635Sbonwick spa = spa_add(pool, altroot); 1166789Sahrens spa_activate(spa); 1167789Sahrens 1168789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 11694577Sahrens spa->spa_uberblock.ub_version = SPA_VERSION; 1170789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1171789Sahrens 11721635Sbonwick /* 11731635Sbonwick * Create the root vdev. 11741635Sbonwick */ 11751635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 11761635Sbonwick 11772082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 11782082Seschrock 11792082Seschrock ASSERT(error != 0 || rvd != NULL); 11802082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 11812082Seschrock 11822082Seschrock if (error == 0 && rvd->vdev_children == 0) 11831635Sbonwick error = EINVAL; 11842082Seschrock 11852082Seschrock if (error == 0 && 11862082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 11872082Seschrock (error = spa_validate_spares(spa, nvroot, txg, 11882082Seschrock VDEV_ALLOC_ADD)) == 0) { 11892082Seschrock for (c = 0; c < rvd->vdev_children; c++) 11902082Seschrock vdev_init(rvd->vdev_child[c], txg); 11912082Seschrock vdev_config_dirty(rvd); 11921635Sbonwick } 11931635Sbonwick 11941635Sbonwick spa_config_exit(spa, FTAG); 1195789Sahrens 11962082Seschrock if (error != 0) { 1197789Sahrens spa_unload(spa); 1198789Sahrens spa_deactivate(spa); 1199789Sahrens spa_remove(spa); 1200789Sahrens mutex_exit(&spa_namespace_lock); 1201789Sahrens return (error); 1202789Sahrens } 1203789Sahrens 12042082Seschrock /* 12052082Seschrock * Get the list of spares, if specified. 12062082Seschrock */ 12072082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 12082082Seschrock &spares, &nspares) == 0) { 12092082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 12102082Seschrock KM_SLEEP) == 0); 12112082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 12122082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 12132082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12142082Seschrock spa_load_spares(spa); 12152082Seschrock spa_config_exit(spa, FTAG); 12162082Seschrock spa->spa_sync_spares = B_TRUE; 12172082Seschrock } 12182082Seschrock 1219789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1220789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1221789Sahrens 1222789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1223789Sahrens 1224789Sahrens /* 1225789Sahrens * Create the pool config object. 1226789Sahrens */ 1227789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1228789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1229789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1230789Sahrens 12311544Seschrock if (zap_add(spa->spa_meta_objset, 1232789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 12331544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 12341544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 12351544Seschrock } 1236789Sahrens 12372082Seschrock /* Newly created pools are always deflated. */ 12382082Seschrock spa->spa_deflate = TRUE; 12392082Seschrock if (zap_add(spa->spa_meta_objset, 12402082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12412082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 12422082Seschrock cmn_err(CE_PANIC, "failed to add deflate"); 12432082Seschrock } 12442082Seschrock 1245789Sahrens /* 1246789Sahrens * Create the deferred-free bplist object. Turn off compression 1247789Sahrens * because sync-to-convergence takes longer if the blocksize 1248789Sahrens * keeps changing. 1249789Sahrens */ 1250789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1251789Sahrens 1 << 14, tx); 1252789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1253789Sahrens ZIO_COMPRESS_OFF, tx); 1254789Sahrens 12551544Seschrock if (zap_add(spa->spa_meta_objset, 1256789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12571544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 12581544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 12591544Seschrock } 1260789Sahrens 12612926Sek110237 /* 12622926Sek110237 * Create the pool's history object. 12632926Sek110237 */ 12642926Sek110237 spa_history_create_obj(spa, tx); 12652926Sek110237 1266789Sahrens dmu_tx_commit(tx); 1267789Sahrens 12684451Seschrock spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 12694543Smarks spa->spa_delegation = zfs_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1270789Sahrens spa->spa_sync_on = B_TRUE; 1271789Sahrens txg_sync_start(spa->spa_dsl_pool); 1272789Sahrens 1273789Sahrens /* 1274789Sahrens * We explicitly wait for the first transaction to complete so that our 1275789Sahrens * bean counters are appropriately updated. 1276789Sahrens */ 1277789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1278789Sahrens 1279789Sahrens spa_config_sync(); 1280789Sahrens 12814715Sek110237 if (history_str != NULL) 12824715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 12834715Sek110237 1284789Sahrens mutex_exit(&spa_namespace_lock); 1285789Sahrens 1286789Sahrens return (0); 1287789Sahrens } 1288789Sahrens 1289789Sahrens /* 1290789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1291789Sahrens * then call spa_load() to do the dirty work. 1292789Sahrens */ 1293789Sahrens int 12941635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 1295789Sahrens { 1296789Sahrens spa_t *spa; 1297789Sahrens int error; 12982082Seschrock nvlist_t *nvroot; 12992082Seschrock nvlist_t **spares; 13002082Seschrock uint_t nspares; 1301789Sahrens 1302789Sahrens /* 1303789Sahrens * If a pool with this name exists, return failure. 1304789Sahrens */ 1305789Sahrens mutex_enter(&spa_namespace_lock); 1306789Sahrens if (spa_lookup(pool) != NULL) { 1307789Sahrens mutex_exit(&spa_namespace_lock); 1308789Sahrens return (EEXIST); 1309789Sahrens } 1310789Sahrens 1311789Sahrens /* 13121635Sbonwick * Create and initialize the spa structure. 1313789Sahrens */ 13141635Sbonwick spa = spa_add(pool, altroot); 1315789Sahrens spa_activate(spa); 1316789Sahrens 1317789Sahrens /* 13181635Sbonwick * Pass off the heavy lifting to spa_load(). 13191732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 13201732Sbonwick * is actually the one to trust when doing an import. 13211601Sbonwick */ 13221732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1323789Sahrens 13242082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13252082Seschrock /* 13262082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 13272082Seschrock * and conflicts with spa_has_spare(). 13282082Seschrock */ 13292082Seschrock if (spa->spa_sparelist) { 13302082Seschrock nvlist_free(spa->spa_sparelist); 13312082Seschrock spa->spa_sparelist = NULL; 13322082Seschrock spa_load_spares(spa); 13332082Seschrock } 13342082Seschrock 13352082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 13362082Seschrock &nvroot) == 0); 13372082Seschrock if (error == 0) 13382082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 13392082Seschrock VDEV_ALLOC_SPARE); 13402082Seschrock spa_config_exit(spa, FTAG); 13412082Seschrock 13422082Seschrock if (error != 0) { 1343789Sahrens spa_unload(spa); 1344789Sahrens spa_deactivate(spa); 1345789Sahrens spa_remove(spa); 1346789Sahrens mutex_exit(&spa_namespace_lock); 1347789Sahrens return (error); 1348789Sahrens } 1349789Sahrens 13501635Sbonwick /* 13512082Seschrock * Override any spares as specified by the user, as these may have 13522082Seschrock * correct device names/devids, etc. 13532082Seschrock */ 13542082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 13552082Seschrock &spares, &nspares) == 0) { 13562082Seschrock if (spa->spa_sparelist) 13572082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 13582082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 13592082Seschrock else 13602082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 13612082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 13622082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 13632082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 13642082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13652082Seschrock spa_load_spares(spa); 13662082Seschrock spa_config_exit(spa, FTAG); 13672082Seschrock spa->spa_sync_spares = B_TRUE; 13682082Seschrock } 13692082Seschrock 13702082Seschrock /* 13711635Sbonwick * Update the config cache to include the newly-imported pool. 13721635Sbonwick */ 13734627Sck153898 if (spa_mode & FWRITE) 13744627Sck153898 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 13751635Sbonwick 1376789Sahrens /* 1377789Sahrens * Resilver anything that's out of date. 1378789Sahrens */ 1379789Sahrens if (spa_mode & FWRITE) 1380789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1381789Sahrens 13824451Seschrock mutex_exit(&spa_namespace_lock); 13834451Seschrock 1384789Sahrens return (0); 1385789Sahrens } 1386789Sahrens 1387789Sahrens /* 1388789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1389789Sahrens * to get the vdev stats associated with the imported devices. 1390789Sahrens */ 1391789Sahrens #define TRYIMPORT_NAME "$import" 1392789Sahrens 1393789Sahrens nvlist_t * 1394789Sahrens spa_tryimport(nvlist_t *tryconfig) 1395789Sahrens { 1396789Sahrens nvlist_t *config = NULL; 1397789Sahrens char *poolname; 1398789Sahrens spa_t *spa; 1399789Sahrens uint64_t state; 1400789Sahrens 1401789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1402789Sahrens return (NULL); 1403789Sahrens 1404789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1405789Sahrens return (NULL); 1406789Sahrens 14071635Sbonwick /* 14081635Sbonwick * Create and initialize the spa structure. 14091635Sbonwick */ 1410789Sahrens mutex_enter(&spa_namespace_lock); 14111635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1412789Sahrens spa_activate(spa); 1413789Sahrens 1414789Sahrens /* 14151635Sbonwick * Pass off the heavy lifting to spa_load(). 14161732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 14171732Sbonwick * is actually the one to trust when doing an import. 1418789Sahrens */ 14191732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1420789Sahrens 1421789Sahrens /* 1422789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1423789Sahrens */ 1424789Sahrens if (spa->spa_root_vdev != NULL) { 14251635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1426789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14271635Sbonwick spa_config_exit(spa, FTAG); 1428789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1429789Sahrens poolname) == 0); 1430789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1431789Sahrens state) == 0); 14323975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 14333975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 14342082Seschrock 14352082Seschrock /* 14362082Seschrock * Add the list of hot spares. 14372082Seschrock */ 14382082Seschrock spa_add_spares(spa, config); 1439789Sahrens } 1440789Sahrens 1441789Sahrens spa_unload(spa); 1442789Sahrens spa_deactivate(spa); 1443789Sahrens spa_remove(spa); 1444789Sahrens mutex_exit(&spa_namespace_lock); 1445789Sahrens 1446789Sahrens return (config); 1447789Sahrens } 1448789Sahrens 1449789Sahrens /* 1450789Sahrens * Pool export/destroy 1451789Sahrens * 1452789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1453789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1454789Sahrens * update the pool state and sync all the labels to disk, removing the 1455789Sahrens * configuration from the cache afterwards. 1456789Sahrens */ 1457789Sahrens static int 14581775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1459789Sahrens { 1460789Sahrens spa_t *spa; 1461789Sahrens 14621775Sbillm if (oldconfig) 14631775Sbillm *oldconfig = NULL; 14641775Sbillm 1465789Sahrens if (!(spa_mode & FWRITE)) 1466789Sahrens return (EROFS); 1467789Sahrens 1468789Sahrens mutex_enter(&spa_namespace_lock); 1469789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1470789Sahrens mutex_exit(&spa_namespace_lock); 1471789Sahrens return (ENOENT); 1472789Sahrens } 1473789Sahrens 1474789Sahrens /* 14751544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 14761544Seschrock * reacquire the namespace lock, and see if we can export. 14771544Seschrock */ 14781544Seschrock spa_open_ref(spa, FTAG); 14791544Seschrock mutex_exit(&spa_namespace_lock); 14801544Seschrock spa_async_suspend(spa); 14811544Seschrock mutex_enter(&spa_namespace_lock); 14821544Seschrock spa_close(spa, FTAG); 14831544Seschrock 14841544Seschrock /* 1485789Sahrens * The pool will be in core if it's openable, 1486789Sahrens * in which case we can modify its state. 1487789Sahrens */ 1488789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1489789Sahrens /* 1490789Sahrens * Objsets may be open only because they're dirty, so we 1491789Sahrens * have to force it to sync before checking spa_refcnt. 1492789Sahrens */ 1493789Sahrens spa_scrub_suspend(spa); 1494789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1495789Sahrens 14961544Seschrock /* 14971544Seschrock * A pool cannot be exported or destroyed if there are active 14981544Seschrock * references. If we are resetting a pool, allow references by 14991544Seschrock * fault injection handlers. 15001544Seschrock */ 15011544Seschrock if (!spa_refcount_zero(spa) || 15021544Seschrock (spa->spa_inject_ref != 0 && 15031544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1504789Sahrens spa_scrub_resume(spa); 15051544Seschrock spa_async_resume(spa); 1506789Sahrens mutex_exit(&spa_namespace_lock); 1507789Sahrens return (EBUSY); 1508789Sahrens } 1509789Sahrens 1510789Sahrens spa_scrub_resume(spa); 1511789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1512789Sahrens 1513789Sahrens /* 1514789Sahrens * We want this to be reflected on every label, 1515789Sahrens * so mark them all dirty. spa_unload() will do the 1516789Sahrens * final sync that pushes these changes out. 1517789Sahrens */ 15181544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15191601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15201544Seschrock spa->spa_state = new_state; 15211635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 15221544Seschrock vdev_config_dirty(spa->spa_root_vdev); 15231601Sbonwick spa_config_exit(spa, FTAG); 15241544Seschrock } 1525789Sahrens } 1526789Sahrens 15274451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 15284451Seschrock 1529789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1530789Sahrens spa_unload(spa); 1531789Sahrens spa_deactivate(spa); 1532789Sahrens } 1533789Sahrens 15341775Sbillm if (oldconfig && spa->spa_config) 15351775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 15361775Sbillm 15371544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15381544Seschrock spa_remove(spa); 15391544Seschrock spa_config_sync(); 15401544Seschrock } 1541789Sahrens mutex_exit(&spa_namespace_lock); 1542789Sahrens 1543789Sahrens return (0); 1544789Sahrens } 1545789Sahrens 1546789Sahrens /* 1547789Sahrens * Destroy a storage pool. 1548789Sahrens */ 1549789Sahrens int 1550789Sahrens spa_destroy(char *pool) 1551789Sahrens { 15521775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1553789Sahrens } 1554789Sahrens 1555789Sahrens /* 1556789Sahrens * Export a storage pool. 1557789Sahrens */ 1558789Sahrens int 15591775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1560789Sahrens { 15611775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1562789Sahrens } 1563789Sahrens 1564789Sahrens /* 15651544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 15661544Seschrock * from the namespace in any way. 15671544Seschrock */ 15681544Seschrock int 15691544Seschrock spa_reset(char *pool) 15701544Seschrock { 15711775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 15721544Seschrock } 15731544Seschrock 15741544Seschrock 15751544Seschrock /* 1576789Sahrens * ========================================================================== 1577789Sahrens * Device manipulation 1578789Sahrens * ========================================================================== 1579789Sahrens */ 1580789Sahrens 1581789Sahrens /* 15824527Sperrin * Add a device to a storage pool. 1583789Sahrens */ 1584789Sahrens int 1585789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1586789Sahrens { 1587789Sahrens uint64_t txg; 15881635Sbonwick int c, error; 1589789Sahrens vdev_t *rvd = spa->spa_root_vdev; 15901585Sbonwick vdev_t *vd, *tvd; 15912082Seschrock nvlist_t **spares; 15922082Seschrock uint_t i, nspares; 1593789Sahrens 1594789Sahrens txg = spa_vdev_enter(spa); 1595789Sahrens 15962082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 15972082Seschrock VDEV_ALLOC_ADD)) != 0) 15982082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 15992082Seschrock 16003377Seschrock spa->spa_pending_vdev = vd; 1601789Sahrens 16022082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 16032082Seschrock &spares, &nspares) != 0) 16042082Seschrock nspares = 0; 16052082Seschrock 16063377Seschrock if (vd->vdev_children == 0 && nspares == 0) { 16073377Seschrock spa->spa_pending_vdev = NULL; 16082082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 16093377Seschrock } 16102082Seschrock 16112082Seschrock if (vd->vdev_children != 0) { 16123377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 16133377Seschrock spa->spa_pending_vdev = NULL; 16142082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16152082Seschrock } 16162082Seschrock } 16172082Seschrock 16183377Seschrock /* 16193377Seschrock * We must validate the spares after checking the children. Otherwise, 16203377Seschrock * vdev_inuse() will blindly overwrite the spare. 16213377Seschrock */ 16223377Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 16233377Seschrock VDEV_ALLOC_ADD)) != 0) { 16243377Seschrock spa->spa_pending_vdev = NULL; 16253377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16263377Seschrock } 16273377Seschrock 16283377Seschrock spa->spa_pending_vdev = NULL; 16293377Seschrock 16303377Seschrock /* 16313377Seschrock * Transfer each new top-level vdev from vd to rvd. 16323377Seschrock */ 16333377Seschrock for (c = 0; c < vd->vdev_children; c++) { 16343377Seschrock tvd = vd->vdev_child[c]; 16353377Seschrock vdev_remove_child(vd, tvd); 16363377Seschrock tvd->vdev_id = rvd->vdev_children; 16373377Seschrock vdev_add_child(rvd, tvd); 16383377Seschrock vdev_config_dirty(tvd); 16393377Seschrock } 16403377Seschrock 16412082Seschrock if (nspares != 0) { 16422082Seschrock if (spa->spa_sparelist != NULL) { 16432082Seschrock nvlist_t **oldspares; 16442082Seschrock uint_t oldnspares; 16452082Seschrock nvlist_t **newspares; 16462082Seschrock 16472082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 16482082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 16492082Seschrock 16502082Seschrock newspares = kmem_alloc(sizeof (void *) * 16512082Seschrock (nspares + oldnspares), KM_SLEEP); 16522082Seschrock for (i = 0; i < oldnspares; i++) 16532082Seschrock VERIFY(nvlist_dup(oldspares[i], 16542082Seschrock &newspares[i], KM_SLEEP) == 0); 16552082Seschrock for (i = 0; i < nspares; i++) 16562082Seschrock VERIFY(nvlist_dup(spares[i], 16572082Seschrock &newspares[i + oldnspares], 16582082Seschrock KM_SLEEP) == 0); 16592082Seschrock 16602082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 16612082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 16622082Seschrock 16632082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16642082Seschrock ZPOOL_CONFIG_SPARES, newspares, 16652082Seschrock nspares + oldnspares) == 0); 16662082Seschrock for (i = 0; i < oldnspares + nspares; i++) 16672082Seschrock nvlist_free(newspares[i]); 16682082Seschrock kmem_free(newspares, (oldnspares + nspares) * 16692082Seschrock sizeof (void *)); 16702082Seschrock } else { 16712082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 16722082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 16732082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16742082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16752082Seschrock } 16762082Seschrock 16772082Seschrock spa_load_spares(spa); 16782082Seschrock spa->spa_sync_spares = B_TRUE; 1679789Sahrens } 1680789Sahrens 1681789Sahrens /* 16821585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 16831585Sbonwick * If other threads start allocating from these vdevs before we 16841585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 16851585Sbonwick * fail to open the pool because there are DVAs that the config cache 16861585Sbonwick * can't translate. Therefore, we first add the vdevs without 16871585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 16881635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 16891585Sbonwick * 16901585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 16911585Sbonwick * if we lose power at any point in this sequence, the remaining 16921585Sbonwick * steps will be completed the next time we load the pool. 1693789Sahrens */ 16941635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 16951585Sbonwick 16961635Sbonwick mutex_enter(&spa_namespace_lock); 16971635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 16981635Sbonwick mutex_exit(&spa_namespace_lock); 1699789Sahrens 17001635Sbonwick return (0); 1701789Sahrens } 1702789Sahrens 1703789Sahrens /* 1704789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1705789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1706789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1707789Sahrens * 1708789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1709789Sahrens * existing device; in this case the two devices are made into their own 17104451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 1711789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1712789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1713789Sahrens * completion of resilvering, the first disk (the one being replaced) 1714789Sahrens * is automatically detached. 1715789Sahrens */ 1716789Sahrens int 17171544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1718789Sahrens { 1719789Sahrens uint64_t txg, open_txg; 1720789Sahrens int error; 1721789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1722789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 17232082Seschrock vdev_ops_t *pvops; 17244527Sperrin int is_log; 1725789Sahrens 1726789Sahrens txg = spa_vdev_enter(spa); 1727789Sahrens 17281544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1729789Sahrens 1730789Sahrens if (oldvd == NULL) 1731789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1732789Sahrens 17331585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 17341585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17351585Sbonwick 1736789Sahrens pvd = oldvd->vdev_parent; 1737789Sahrens 17382082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 17394451Seschrock VDEV_ALLOC_ADD)) != 0) 17404451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 17414451Seschrock 17424451Seschrock if (newrootvd->vdev_children != 1) 1743789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1744789Sahrens 1745789Sahrens newvd = newrootvd->vdev_child[0]; 1746789Sahrens 1747789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1748789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1749789Sahrens 17502082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1751789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1752789Sahrens 17534527Sperrin /* 17544527Sperrin * Spares can't replace logs 17554527Sperrin */ 17564527Sperrin is_log = oldvd->vdev_islog; 17574527Sperrin if (is_log && newvd->vdev_isspare) 17584527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17594527Sperrin 17602082Seschrock if (!replacing) { 17612082Seschrock /* 17622082Seschrock * For attach, the only allowable parent is a mirror or the root 17632082Seschrock * vdev. 17642082Seschrock */ 17652082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 17662082Seschrock pvd->vdev_ops != &vdev_root_ops) 17672082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17682082Seschrock 17692082Seschrock pvops = &vdev_mirror_ops; 17702082Seschrock } else { 17712082Seschrock /* 17722082Seschrock * Active hot spares can only be replaced by inactive hot 17732082Seschrock * spares. 17742082Seschrock */ 17752082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 17762082Seschrock pvd->vdev_child[1] == oldvd && 17772082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 17782082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17792082Seschrock 17802082Seschrock /* 17812082Seschrock * If the source is a hot spare, and the parent isn't already a 17822082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 17833377Seschrock * want to create a replacing vdev. The user is not allowed to 17843377Seschrock * attach to a spared vdev child unless the 'isspare' state is 17853377Seschrock * the same (spare replaces spare, non-spare replaces 17863377Seschrock * non-spare). 17872082Seschrock */ 17882082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 17892082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17903377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 17913377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 17923377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17932082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 17942082Seschrock newvd->vdev_isspare) 17952082Seschrock pvops = &vdev_spare_ops; 17962082Seschrock else 17972082Seschrock pvops = &vdev_replacing_ops; 17982082Seschrock } 17992082Seschrock 18001175Slling /* 18011175Slling * Compare the new device size with the replaceable/attachable 18021175Slling * device size. 18031175Slling */ 18041175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1805789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1806789Sahrens 18071732Sbonwick /* 18081732Sbonwick * The new device cannot have a higher alignment requirement 18091732Sbonwick * than the top-level vdev. 18101732Sbonwick */ 18111732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1812789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1813789Sahrens 1814789Sahrens /* 1815789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1816789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1817789Sahrens */ 1818789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1819789Sahrens spa_strfree(oldvd->vdev_path); 1820789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1821789Sahrens KM_SLEEP); 1822789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1823789Sahrens newvd->vdev_path, "old"); 1824789Sahrens if (oldvd->vdev_devid != NULL) { 1825789Sahrens spa_strfree(oldvd->vdev_devid); 1826789Sahrens oldvd->vdev_devid = NULL; 1827789Sahrens } 1828789Sahrens } 1829789Sahrens 1830789Sahrens /* 18312082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 18322082Seschrock * mirror/replacing/spare vdev above oldvd. 1833789Sahrens */ 1834789Sahrens if (pvd->vdev_ops != pvops) 1835789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1836789Sahrens 1837789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1838789Sahrens ASSERT(pvd->vdev_ops == pvops); 1839789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1840789Sahrens 1841789Sahrens /* 1842789Sahrens * Extract the new device from its root and add it to pvd. 1843789Sahrens */ 1844789Sahrens vdev_remove_child(newrootvd, newvd); 1845789Sahrens newvd->vdev_id = pvd->vdev_children; 1846789Sahrens vdev_add_child(pvd, newvd); 1847789Sahrens 18481544Seschrock /* 18491544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 18501544Seschrock * the addition of newvd may have decreased our parent's asize. 18511544Seschrock */ 18521544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 18531544Seschrock 1854789Sahrens tvd = newvd->vdev_top; 1855789Sahrens ASSERT(pvd->vdev_top == tvd); 1856789Sahrens ASSERT(tvd->vdev_parent == rvd); 1857789Sahrens 1858789Sahrens vdev_config_dirty(tvd); 1859789Sahrens 1860789Sahrens /* 1861789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1862789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1863789Sahrens */ 1864789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1865789Sahrens 1866789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1867789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1868789Sahrens open_txg - TXG_INITIAL + 1); 1869789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1870789Sahrens 18713377Seschrock if (newvd->vdev_isspare) 18723377Seschrock spa_spare_activate(newvd); 18731544Seschrock 1874789Sahrens /* 1875789Sahrens * Mark newvd's DTL dirty in this txg. 1876789Sahrens */ 18771732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1878789Sahrens 1879789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1880789Sahrens 1881789Sahrens /* 18824451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 18834451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 1884789Sahrens */ 18854451Seschrock mutex_enter(&spa_namespace_lock); 1886789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 18874451Seschrock mutex_exit(&spa_namespace_lock); 1888789Sahrens 1889789Sahrens return (0); 1890789Sahrens } 1891789Sahrens 1892789Sahrens /* 1893789Sahrens * Detach a device from a mirror or replacing vdev. 1894789Sahrens * If 'replace_done' is specified, only detach if the parent 1895789Sahrens * is a replacing vdev. 1896789Sahrens */ 1897789Sahrens int 18981544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1899789Sahrens { 1900789Sahrens uint64_t txg; 1901789Sahrens int c, t, error; 1902789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1903789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 19042082Seschrock boolean_t unspare = B_FALSE; 19052082Seschrock uint64_t unspare_guid; 1906789Sahrens 1907789Sahrens txg = spa_vdev_enter(spa); 1908789Sahrens 19091544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1910789Sahrens 1911789Sahrens if (vd == NULL) 1912789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1913789Sahrens 19141585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 19151585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19161585Sbonwick 1917789Sahrens pvd = vd->vdev_parent; 1918789Sahrens 1919789Sahrens /* 1920789Sahrens * If replace_done is specified, only remove this device if it's 19212082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 19222082Seschrock * disk can be removed. 1923789Sahrens */ 19242082Seschrock if (replace_done) { 19252082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 19262082Seschrock if (vd->vdev_id != 0) 19272082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19282082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 19292082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19302082Seschrock } 19312082Seschrock } 19322082Seschrock 19332082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 19344577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 1935789Sahrens 1936789Sahrens /* 19372082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1938789Sahrens */ 1939789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 19402082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 19412082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1942789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1943789Sahrens 1944789Sahrens /* 1945789Sahrens * If there's only one replica, you can't detach it. 1946789Sahrens */ 1947789Sahrens if (pvd->vdev_children <= 1) 1948789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1949789Sahrens 1950789Sahrens /* 1951789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1952789Sahrens * valid copy of the data, which means we cannot safely detach it. 1953789Sahrens * 1954789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1955789Sahrens * precise DTL check. 1956789Sahrens */ 1957789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1958789Sahrens uint64_t dirty; 1959789Sahrens 1960789Sahrens cvd = pvd->vdev_child[c]; 1961789Sahrens if (cvd == vd) 1962789Sahrens continue; 1963789Sahrens if (vdev_is_dead(cvd)) 1964789Sahrens continue; 1965789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1966789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1967789Sahrens cvd->vdev_dtl_scrub.sm_space; 1968789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1969789Sahrens if (!dirty) 1970789Sahrens break; 1971789Sahrens } 19722082Seschrock 19732082Seschrock /* 19742082Seschrock * If we are a replacing or spare vdev, then we can always detach the 19752082Seschrock * latter child, as that is how one cancels the operation. 19762082Seschrock */ 19772082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 19782082Seschrock c == pvd->vdev_children) 1979789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1980789Sahrens 1981789Sahrens /* 19822082Seschrock * If we are detaching the original disk from a spare, then it implies 19832082Seschrock * that the spare should become a real disk, and be removed from the 19842082Seschrock * active spare list for the pool. 19852082Seschrock */ 19862082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 19872082Seschrock vd->vdev_id == 0) 19882082Seschrock unspare = B_TRUE; 19892082Seschrock 19902082Seschrock /* 1991789Sahrens * Erase the disk labels so the disk can be used for other things. 1992789Sahrens * This must be done after all other error cases are handled, 1993789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1994789Sahrens * But if we can't do it, don't treat the error as fatal -- 1995789Sahrens * it may be that the unwritability of the disk is the reason 1996789Sahrens * it's being detached! 1997789Sahrens */ 19983377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1999789Sahrens 2000789Sahrens /* 2001789Sahrens * Remove vd from its parent and compact the parent's children. 2002789Sahrens */ 2003789Sahrens vdev_remove_child(pvd, vd); 2004789Sahrens vdev_compact_children(pvd); 2005789Sahrens 2006789Sahrens /* 2007789Sahrens * Remember one of the remaining children so we can get tvd below. 2008789Sahrens */ 2009789Sahrens cvd = pvd->vdev_child[0]; 2010789Sahrens 2011789Sahrens /* 20122082Seschrock * If we need to remove the remaining child from the list of hot spares, 20132082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 20142082Seschrock * must do this before vdev_remove_parent(), because that can change the 20152082Seschrock * GUID if it creates a new toplevel GUID. 20162082Seschrock */ 20172082Seschrock if (unspare) { 20182082Seschrock ASSERT(cvd->vdev_isspare); 20193377Seschrock spa_spare_remove(cvd); 20202082Seschrock unspare_guid = cvd->vdev_guid; 20212082Seschrock } 20222082Seschrock 20232082Seschrock /* 2024789Sahrens * If the parent mirror/replacing vdev only has one child, 2025789Sahrens * the parent is no longer needed. Remove it from the tree. 2026789Sahrens */ 2027789Sahrens if (pvd->vdev_children == 1) 2028789Sahrens vdev_remove_parent(cvd); 2029789Sahrens 2030789Sahrens /* 2031789Sahrens * We don't set tvd until now because the parent we just removed 2032789Sahrens * may have been the previous top-level vdev. 2033789Sahrens */ 2034789Sahrens tvd = cvd->vdev_top; 2035789Sahrens ASSERT(tvd->vdev_parent == rvd); 2036789Sahrens 2037789Sahrens /* 20383377Seschrock * Reevaluate the parent vdev state. 2039789Sahrens */ 20404451Seschrock vdev_propagate_state(cvd); 2041789Sahrens 2042789Sahrens /* 20433377Seschrock * If the device we just detached was smaller than the others, it may be 20443377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 20453377Seschrock * can't fail because the existing metaslabs are already in core, so 20463377Seschrock * there's nothing to read from disk. 2047789Sahrens */ 20481732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2049789Sahrens 2050789Sahrens vdev_config_dirty(tvd); 2051789Sahrens 2052789Sahrens /* 20533377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 20543377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 20553377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 20563377Seschrock * prevent vd from being accessed after it's freed. 2057789Sahrens */ 2058789Sahrens for (t = 0; t < TXG_SIZE; t++) 2059789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 20601732Sbonwick vd->vdev_detached = B_TRUE; 20611732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2062789Sahrens 20634451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 20644451Seschrock 20652082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 20662082Seschrock 20672082Seschrock /* 20683377Seschrock * If this was the removal of the original device in a hot spare vdev, 20693377Seschrock * then we want to go through and remove the device from the hot spare 20703377Seschrock * list of every other pool. 20712082Seschrock */ 20722082Seschrock if (unspare) { 20732082Seschrock spa = NULL; 20742082Seschrock mutex_enter(&spa_namespace_lock); 20752082Seschrock while ((spa = spa_next(spa)) != NULL) { 20762082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 20772082Seschrock continue; 20782082Seschrock 20792082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 20802082Seschrock } 20812082Seschrock mutex_exit(&spa_namespace_lock); 20822082Seschrock } 20832082Seschrock 20842082Seschrock return (error); 20852082Seschrock } 20862082Seschrock 20872082Seschrock /* 20882082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 20892082Seschrock * spares. 20902082Seschrock */ 20912082Seschrock int 20922082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 20932082Seschrock { 20942082Seschrock vdev_t *vd; 20952082Seschrock nvlist_t **spares, *nv, **newspares; 20962082Seschrock uint_t i, j, nspares; 20972082Seschrock int ret = 0; 20982082Seschrock 20992082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21002082Seschrock 21012082Seschrock vd = spa_lookup_by_guid(spa, guid); 21022082Seschrock 21032082Seschrock nv = NULL; 21042082Seschrock if (spa->spa_spares != NULL && 21052082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21062082Seschrock &spares, &nspares) == 0) { 21072082Seschrock for (i = 0; i < nspares; i++) { 21082082Seschrock uint64_t theguid; 21092082Seschrock 21102082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 21112082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 21122082Seschrock if (theguid == guid) { 21132082Seschrock nv = spares[i]; 21142082Seschrock break; 21152082Seschrock } 21162082Seschrock } 21172082Seschrock } 21182082Seschrock 21192082Seschrock /* 21202082Seschrock * We only support removing a hot spare, and only if it's not currently 21212082Seschrock * in use in this pool. 21222082Seschrock */ 21232082Seschrock if (nv == NULL && vd == NULL) { 21242082Seschrock ret = ENOENT; 21252082Seschrock goto out; 21262082Seschrock } 21272082Seschrock 21282082Seschrock if (nv == NULL && vd != NULL) { 21292082Seschrock ret = ENOTSUP; 21302082Seschrock goto out; 21312082Seschrock } 21322082Seschrock 21332082Seschrock if (!unspare && nv != NULL && vd != NULL) { 21342082Seschrock ret = EBUSY; 21352082Seschrock goto out; 21362082Seschrock } 21372082Seschrock 21382082Seschrock if (nspares == 1) { 21392082Seschrock newspares = NULL; 21402082Seschrock } else { 21412082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 21422082Seschrock KM_SLEEP); 21432082Seschrock for (i = 0, j = 0; i < nspares; i++) { 21442082Seschrock if (spares[i] != nv) 21452082Seschrock VERIFY(nvlist_dup(spares[i], 21462082Seschrock &newspares[j++], KM_SLEEP) == 0); 21472082Seschrock } 21482082Seschrock } 21492082Seschrock 21502082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21512082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 21522082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21532082Seschrock newspares, nspares - 1) == 0); 21542082Seschrock for (i = 0; i < nspares - 1; i++) 21552082Seschrock nvlist_free(newspares[i]); 21562082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 21572082Seschrock spa_load_spares(spa); 21582082Seschrock spa->spa_sync_spares = B_TRUE; 21592082Seschrock 21602082Seschrock out: 21612082Seschrock spa_config_exit(spa, FTAG); 21622082Seschrock 21632082Seschrock return (ret); 2164789Sahrens } 2165789Sahrens 2166789Sahrens /* 21674451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 21684451Seschrock * current spared, so we can detach it. 2169789Sahrens */ 21701544Seschrock static vdev_t * 21714451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2172789Sahrens { 21731544Seschrock vdev_t *newvd, *oldvd; 2174789Sahrens int c; 2175789Sahrens 21761544Seschrock for (c = 0; c < vd->vdev_children; c++) { 21774451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 21781544Seschrock if (oldvd != NULL) 21791544Seschrock return (oldvd); 21801544Seschrock } 2181789Sahrens 21824451Seschrock /* 21834451Seschrock * Check for a completed replacement. 21844451Seschrock */ 2185789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 21861544Seschrock oldvd = vd->vdev_child[0]; 21871544Seschrock newvd = vd->vdev_child[1]; 2188789Sahrens 21891544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 21901544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 21911544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 21921544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21931544Seschrock return (oldvd); 21941544Seschrock } 21951544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21961544Seschrock } 2197789Sahrens 21984451Seschrock /* 21994451Seschrock * Check for a completed resilver with the 'unspare' flag set. 22004451Seschrock */ 22014451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 22024451Seschrock newvd = vd->vdev_child[0]; 22034451Seschrock oldvd = vd->vdev_child[1]; 22044451Seschrock 22054451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 22064451Seschrock if (newvd->vdev_unspare && 22074451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 22084451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 22094451Seschrock newvd->vdev_unspare = 0; 22104451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22114451Seschrock return (oldvd); 22124451Seschrock } 22134451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22144451Seschrock } 22154451Seschrock 22161544Seschrock return (NULL); 2217789Sahrens } 2218789Sahrens 22191544Seschrock static void 22204451Seschrock spa_vdev_resilver_done(spa_t *spa) 2221789Sahrens { 22221544Seschrock vdev_t *vd; 22232082Seschrock vdev_t *pvd; 22241544Seschrock uint64_t guid; 22252082Seschrock uint64_t pguid = 0; 2226789Sahrens 22271544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2228789Sahrens 22294451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 22301544Seschrock guid = vd->vdev_guid; 22312082Seschrock /* 22322082Seschrock * If we have just finished replacing a hot spared device, then 22332082Seschrock * we need to detach the parent's first child (the original hot 22342082Seschrock * spare) as well. 22352082Seschrock */ 22362082Seschrock pvd = vd->vdev_parent; 22372082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 22382082Seschrock pvd->vdev_id == 0) { 22392082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 22402082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 22412082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 22422082Seschrock } 22431544Seschrock spa_config_exit(spa, FTAG); 22441544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 22451544Seschrock return; 22462082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 22472082Seschrock return; 22481544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2249789Sahrens } 2250789Sahrens 22511544Seschrock spa_config_exit(spa, FTAG); 2252789Sahrens } 2253789Sahrens 2254789Sahrens /* 22551354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 22561354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 22571354Seschrock */ 22581354Seschrock int 22591354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 22601354Seschrock { 22611354Seschrock vdev_t *rvd, *vd; 22621354Seschrock uint64_t txg; 22631354Seschrock 22641354Seschrock rvd = spa->spa_root_vdev; 22651354Seschrock 22661354Seschrock txg = spa_vdev_enter(spa); 22671354Seschrock 22682082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 22692082Seschrock /* 22702082Seschrock * Determine if this is a reference to a hot spare. In that 22712082Seschrock * case, update the path as stored in the spare list. 22722082Seschrock */ 22732082Seschrock nvlist_t **spares; 22742082Seschrock uint_t i, nspares; 22752082Seschrock if (spa->spa_sparelist != NULL) { 22762082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 22772082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 22782082Seschrock for (i = 0; i < nspares; i++) { 22792082Seschrock uint64_t theguid; 22802082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 22812082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 22822082Seschrock if (theguid == guid) 22832082Seschrock break; 22842082Seschrock } 22852082Seschrock 22862082Seschrock if (i == nspares) 22872082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22882082Seschrock 22892082Seschrock VERIFY(nvlist_add_string(spares[i], 22902082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 22912082Seschrock spa_load_spares(spa); 22922082Seschrock spa->spa_sync_spares = B_TRUE; 22932082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 22942082Seschrock } else { 22952082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22962082Seschrock } 22972082Seschrock } 22981354Seschrock 22991585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 23001585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 23011585Sbonwick 23021354Seschrock spa_strfree(vd->vdev_path); 23031354Seschrock vd->vdev_path = spa_strdup(newpath); 23041354Seschrock 23051354Seschrock vdev_config_dirty(vd->vdev_top); 23061354Seschrock 23071354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 23081354Seschrock } 23091354Seschrock 23101354Seschrock /* 2311789Sahrens * ========================================================================== 2312789Sahrens * SPA Scrubbing 2313789Sahrens * ========================================================================== 2314789Sahrens */ 2315789Sahrens 2316789Sahrens static void 2317789Sahrens spa_scrub_io_done(zio_t *zio) 2318789Sahrens { 2319789Sahrens spa_t *spa = zio->io_spa; 2320789Sahrens 23214309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2322789Sahrens 2323789Sahrens mutex_enter(&spa->spa_scrub_lock); 23241544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 23251775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2326789Sahrens spa->spa_scrub_errors++; 2327789Sahrens mutex_enter(&vd->vdev_stat_lock); 2328789Sahrens vd->vdev_stat.vs_scrub_errors++; 2329789Sahrens mutex_exit(&vd->vdev_stat_lock); 2330789Sahrens } 23313697Smishra 23323697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 23331544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 23343697Smishra 23353697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 23363697Smishra 23371544Seschrock mutex_exit(&spa->spa_scrub_lock); 2338789Sahrens } 2339789Sahrens 2340789Sahrens static void 23411544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 23421544Seschrock zbookmark_t *zb) 2343789Sahrens { 2344789Sahrens size_t size = BP_GET_LSIZE(bp); 23453697Smishra void *data; 2346789Sahrens 2347789Sahrens mutex_enter(&spa->spa_scrub_lock); 23483697Smishra /* 23493697Smishra * Do not give too much work to vdev(s). 23503697Smishra */ 23513697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 23523697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 23533697Smishra } 2354789Sahrens spa->spa_scrub_inflight++; 2355789Sahrens mutex_exit(&spa->spa_scrub_lock); 2356789Sahrens 23574309Smaybee data = arc_data_buf_alloc(size); 23583697Smishra 23591544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 23601544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 23611544Seschrock 23621807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 23631544Seschrock 2364789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 23651544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2366789Sahrens } 2367789Sahrens 2368789Sahrens /* ARGSUSED */ 2369789Sahrens static int 2370789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2371789Sahrens { 2372789Sahrens blkptr_t *bp = &bc->bc_blkptr; 23731775Sbillm vdev_t *vd = spa->spa_root_vdev; 23741775Sbillm dva_t *dva = bp->blk_dva; 23751775Sbillm int needs_resilver = B_FALSE; 23761775Sbillm int d; 2377789Sahrens 23781775Sbillm if (bc->bc_errno) { 2379789Sahrens /* 2380789Sahrens * We can't scrub this block, but we can continue to scrub 2381789Sahrens * the rest of the pool. Note the error and move along. 2382789Sahrens */ 2383789Sahrens mutex_enter(&spa->spa_scrub_lock); 2384789Sahrens spa->spa_scrub_errors++; 2385789Sahrens mutex_exit(&spa->spa_scrub_lock); 2386789Sahrens 23871775Sbillm mutex_enter(&vd->vdev_stat_lock); 23881775Sbillm vd->vdev_stat.vs_scrub_errors++; 23891775Sbillm mutex_exit(&vd->vdev_stat_lock); 2390789Sahrens 2391789Sahrens return (ERESTART); 2392789Sahrens } 2393789Sahrens 2394789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2395789Sahrens 23961775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 23971775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 23981775Sbillm 23991775Sbillm ASSERT(vd != NULL); 24001775Sbillm 24011775Sbillm /* 24021775Sbillm * Keep track of how much data we've examined so that 24031775Sbillm * zpool(1M) status can make useful progress reports. 24041775Sbillm */ 24051775Sbillm mutex_enter(&vd->vdev_stat_lock); 24061775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 24071775Sbillm mutex_exit(&vd->vdev_stat_lock); 2408789Sahrens 24091775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 24101775Sbillm if (DVA_GET_GANG(&dva[d])) { 24111775Sbillm /* 24121775Sbillm * Gang members may be spread across multiple 24131775Sbillm * vdevs, so the best we can do is look at the 24141775Sbillm * pool-wide DTL. 24151775Sbillm * XXX -- it would be better to change our 24161775Sbillm * allocation policy to ensure that this can't 24171775Sbillm * happen. 24181775Sbillm */ 24191775Sbillm vd = spa->spa_root_vdev; 24201775Sbillm } 24211775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 24221775Sbillm bp->blk_birth, 1)) 24231775Sbillm needs_resilver = B_TRUE; 2424789Sahrens } 24251775Sbillm } 24261775Sbillm 24271775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2428789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 24291544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 24301775Sbillm else if (needs_resilver) 24311775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 24321775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2433789Sahrens 2434789Sahrens return (0); 2435789Sahrens } 2436789Sahrens 2437789Sahrens static void 2438789Sahrens spa_scrub_thread(spa_t *spa) 2439789Sahrens { 2440789Sahrens callb_cpr_t cprinfo; 2441789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2442789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2443789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2444789Sahrens int error = 0; 2445789Sahrens boolean_t complete; 2446789Sahrens 2447789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2448789Sahrens 2449797Sbonwick /* 2450797Sbonwick * If we're restarting due to a snapshot create/delete, 2451797Sbonwick * wait for that to complete. 2452797Sbonwick */ 2453797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2454797Sbonwick 24551544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 24561544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 24571544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 24581544Seschrock 24591544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 24601544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2461789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2462789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 24631544Seschrock spa_config_exit(spa, FTAG); 2464789Sahrens 2465789Sahrens mutex_enter(&spa->spa_scrub_lock); 2466789Sahrens spa->spa_scrub_errors = 0; 2467789Sahrens spa->spa_scrub_active = 1; 24681544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2469789Sahrens 2470789Sahrens while (!spa->spa_scrub_stop) { 2471789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 24721544Seschrock while (spa->spa_scrub_suspended) { 2473789Sahrens spa->spa_scrub_active = 0; 2474789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2475789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2476789Sahrens spa->spa_scrub_active = 1; 2477789Sahrens } 2478789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2479789Sahrens 2480789Sahrens if (spa->spa_scrub_restart_txg != 0) 2481789Sahrens break; 2482789Sahrens 2483789Sahrens mutex_exit(&spa->spa_scrub_lock); 2484789Sahrens error = traverse_more(th); 2485789Sahrens mutex_enter(&spa->spa_scrub_lock); 2486789Sahrens if (error != EAGAIN) 2487789Sahrens break; 2488789Sahrens } 2489789Sahrens 2490789Sahrens while (spa->spa_scrub_inflight) 2491789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2492789Sahrens 24931601Sbonwick spa->spa_scrub_active = 0; 24941601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 24951601Sbonwick 24961601Sbonwick mutex_exit(&spa->spa_scrub_lock); 24971601Sbonwick 24981601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24991601Sbonwick 25001601Sbonwick mutex_enter(&spa->spa_scrub_lock); 25011601Sbonwick 25021601Sbonwick /* 25031601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 25041601Sbonwick * AND the spa config lock to synchronize with any config changes 25051601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 25061601Sbonwick */ 2507789Sahrens if (spa->spa_scrub_restart_txg != 0) 2508789Sahrens error = ERESTART; 2509789Sahrens 25101544Seschrock if (spa->spa_scrub_stop) 25111544Seschrock error = EINTR; 25121544Seschrock 2513789Sahrens /* 25141544Seschrock * Even if there were uncorrectable errors, we consider the scrub 25151544Seschrock * completed. The downside is that if there is a transient error during 25161544Seschrock * a resilver, we won't resilver the data properly to the target. But 25171544Seschrock * if the damage is permanent (more likely) we will resilver forever, 25181544Seschrock * which isn't really acceptable. Since there is enough information for 25191544Seschrock * the user to know what has failed and why, this seems like a more 25201544Seschrock * tractable approach. 2521789Sahrens */ 25221544Seschrock complete = (error == 0); 2523789Sahrens 25241544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 25251544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2526789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2527789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2528789Sahrens 2529789Sahrens mutex_exit(&spa->spa_scrub_lock); 2530789Sahrens 2531789Sahrens /* 2532789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2533789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2534789Sahrens */ 2535789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2536789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2537789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 25381544Seschrock spa_errlog_rotate(spa); 25391601Sbonwick 25404451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 25414451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 25424451Seschrock 25431544Seschrock spa_config_exit(spa, FTAG); 2544789Sahrens 2545789Sahrens mutex_enter(&spa->spa_scrub_lock); 2546789Sahrens 25471544Seschrock /* 25481544Seschrock * We may have finished replacing a device. 25491544Seschrock * Let the async thread assess this and handle the detach. 25501544Seschrock */ 25514451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2552789Sahrens 2553789Sahrens /* 2554789Sahrens * If we were told to restart, our final act is to start a new scrub. 2555789Sahrens */ 2556789Sahrens if (error == ERESTART) 25571544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 25581544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2559789Sahrens 25601544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 25611544Seschrock spa->spa_scrub_active = 0; 25621544Seschrock spa->spa_scrub_thread = NULL; 25631544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2564789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2565789Sahrens thread_exit(); 2566789Sahrens } 2567789Sahrens 2568789Sahrens void 2569789Sahrens spa_scrub_suspend(spa_t *spa) 2570789Sahrens { 2571789Sahrens mutex_enter(&spa->spa_scrub_lock); 25721544Seschrock spa->spa_scrub_suspended++; 2573789Sahrens while (spa->spa_scrub_active) { 2574789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2575789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2576789Sahrens } 2577789Sahrens while (spa->spa_scrub_inflight) 2578789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2579789Sahrens mutex_exit(&spa->spa_scrub_lock); 2580789Sahrens } 2581789Sahrens 2582789Sahrens void 2583789Sahrens spa_scrub_resume(spa_t *spa) 2584789Sahrens { 2585789Sahrens mutex_enter(&spa->spa_scrub_lock); 25861544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 25871544Seschrock if (--spa->spa_scrub_suspended == 0) 2588789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2589789Sahrens mutex_exit(&spa->spa_scrub_lock); 2590789Sahrens } 2591789Sahrens 2592789Sahrens void 2593789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2594789Sahrens { 2595789Sahrens /* 2596789Sahrens * Something happened (e.g. snapshot create/delete) that means 2597789Sahrens * we must restart any in-progress scrubs. The itinerary will 2598789Sahrens * fix this properly. 2599789Sahrens */ 2600789Sahrens mutex_enter(&spa->spa_scrub_lock); 2601789Sahrens spa->spa_scrub_restart_txg = txg; 2602789Sahrens mutex_exit(&spa->spa_scrub_lock); 2603789Sahrens } 2604789Sahrens 26051544Seschrock int 26061544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2607789Sahrens { 2608789Sahrens space_seg_t *ss; 2609789Sahrens uint64_t mintxg, maxtxg; 2610789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2611789Sahrens 2612*4808Sek110237 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2613*4808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 2614*4808Sek110237 2615789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2616789Sahrens return (ENOTSUP); 2617789Sahrens 26181544Seschrock mutex_enter(&spa->spa_scrub_lock); 26191544Seschrock 2620789Sahrens /* 2621789Sahrens * If there's a scrub or resilver already in progress, stop it. 2622789Sahrens */ 2623789Sahrens while (spa->spa_scrub_thread != NULL) { 2624789Sahrens /* 2625789Sahrens * Don't stop a resilver unless forced. 2626789Sahrens */ 26271544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 26281544Seschrock mutex_exit(&spa->spa_scrub_lock); 2629789Sahrens return (EBUSY); 26301544Seschrock } 2631789Sahrens spa->spa_scrub_stop = 1; 2632789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2633789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2634789Sahrens } 2635789Sahrens 2636789Sahrens /* 2637789Sahrens * Terminate the previous traverse. 2638789Sahrens */ 2639789Sahrens if (spa->spa_scrub_th != NULL) { 2640789Sahrens traverse_fini(spa->spa_scrub_th); 2641789Sahrens spa->spa_scrub_th = NULL; 2642789Sahrens } 2643789Sahrens 26441544Seschrock if (rvd == NULL) { 26451544Seschrock ASSERT(spa->spa_scrub_stop == 0); 26461544Seschrock ASSERT(spa->spa_scrub_type == type); 26471544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 26481544Seschrock mutex_exit(&spa->spa_scrub_lock); 26491544Seschrock return (0); 26501544Seschrock } 2651789Sahrens 2652789Sahrens mintxg = TXG_INITIAL - 1; 2653789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2654789Sahrens 26551544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2656789Sahrens 26571544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 26581544Seschrock /* 26591544Seschrock * The pool-wide DTL is empty. 26601732Sbonwick * If this is a resilver, there's nothing to do except 26611732Sbonwick * check whether any in-progress replacements have completed. 26621544Seschrock */ 26631732Sbonwick if (type == POOL_SCRUB_RESILVER) { 26641544Seschrock type = POOL_SCRUB_NONE; 26654451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 26661732Sbonwick } 26671544Seschrock } else { 26681544Seschrock /* 26691544Seschrock * The pool-wide DTL is non-empty. 26701544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 26711544Seschrock */ 26721544Seschrock if (type == POOL_SCRUB_EVERYTHING) 26731544Seschrock type = POOL_SCRUB_RESILVER; 26741544Seschrock } 2675789Sahrens 26761544Seschrock if (type == POOL_SCRUB_RESILVER) { 2677789Sahrens /* 2678789Sahrens * Determine the resilvering boundaries. 2679789Sahrens * 2680789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2681789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2682789Sahrens * 2683789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2684789Sahrens * so we don't claim to resilver a txg that's still changing. 2685789Sahrens */ 2686789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 26871544Seschrock mintxg = ss->ss_start - 1; 2688789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 26891544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 26904451Seschrock 26914451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 2692789Sahrens } 2693789Sahrens 26941544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 26951544Seschrock 26961544Seschrock spa->spa_scrub_stop = 0; 26971544Seschrock spa->spa_scrub_type = type; 26981544Seschrock spa->spa_scrub_restart_txg = 0; 26991544Seschrock 27001544Seschrock if (type != POOL_SCRUB_NONE) { 27011544Seschrock spa->spa_scrub_mintxg = mintxg; 2702789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2703789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 27041635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 27051635Sbonwick ZIO_FLAG_CANFAIL); 2706789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2707789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2708789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2709789Sahrens } 2710789Sahrens 27111544Seschrock mutex_exit(&spa->spa_scrub_lock); 27121544Seschrock 2713789Sahrens return (0); 2714789Sahrens } 2715789Sahrens 27161544Seschrock /* 27171544Seschrock * ========================================================================== 27181544Seschrock * SPA async task processing 27191544Seschrock * ========================================================================== 27201544Seschrock */ 27211544Seschrock 27221544Seschrock static void 27234451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 2724789Sahrens { 27251544Seschrock vdev_t *tvd; 27261544Seschrock int c; 27271544Seschrock 27284451Seschrock for (c = 0; c < vd->vdev_children; c++) { 27294451Seschrock tvd = vd->vdev_child[c]; 27304451Seschrock if (tvd->vdev_remove_wanted) { 27314451Seschrock tvd->vdev_remove_wanted = 0; 27324451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 27334451Seschrock VDEV_AUX_NONE); 27344451Seschrock vdev_clear(spa, tvd); 27354451Seschrock vdev_config_dirty(tvd->vdev_top); 27361544Seschrock } 27374451Seschrock spa_async_remove(spa, tvd); 27381544Seschrock } 27391544Seschrock } 27401544Seschrock 27411544Seschrock static void 27421544Seschrock spa_async_thread(spa_t *spa) 27431544Seschrock { 27441544Seschrock int tasks; 27454451Seschrock uint64_t txg; 27461544Seschrock 27471544Seschrock ASSERT(spa->spa_sync_on); 2748789Sahrens 27491544Seschrock mutex_enter(&spa->spa_async_lock); 27501544Seschrock tasks = spa->spa_async_tasks; 27511544Seschrock spa->spa_async_tasks = 0; 27521544Seschrock mutex_exit(&spa->spa_async_lock); 27531544Seschrock 27541544Seschrock /* 27551635Sbonwick * See if the config needs to be updated. 27561635Sbonwick */ 27571635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 27581635Sbonwick mutex_enter(&spa_namespace_lock); 27591635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27601635Sbonwick mutex_exit(&spa_namespace_lock); 27611635Sbonwick } 27621635Sbonwick 27631635Sbonwick /* 27644451Seschrock * See if any devices need to be marked REMOVED. 27651544Seschrock */ 27664451Seschrock if (tasks & SPA_ASYNC_REMOVE) { 27674451Seschrock txg = spa_vdev_enter(spa); 27684451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 27694451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 27704451Seschrock } 27711544Seschrock 27721544Seschrock /* 27731544Seschrock * If any devices are done replacing, detach them. 27741544Seschrock */ 27754451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 27764451Seschrock spa_vdev_resilver_done(spa); 2777789Sahrens 27781544Seschrock /* 27794451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 27804451Seschrock * scrub which can become a resilver), we need to hold 27814451Seschrock * spa_namespace_lock() because the sysevent we post via 27824451Seschrock * spa_event_notify() needs to get the name of the pool. 27831544Seschrock */ 27844451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 27854451Seschrock mutex_enter(&spa_namespace_lock); 27861544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 27874451Seschrock mutex_exit(&spa_namespace_lock); 27884451Seschrock } 27891544Seschrock 27901544Seschrock /* 27911544Seschrock * Kick off a resilver. 27921544Seschrock */ 27934451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 27944451Seschrock mutex_enter(&spa_namespace_lock); 27951544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 27964451Seschrock mutex_exit(&spa_namespace_lock); 27974451Seschrock } 27981544Seschrock 27991544Seschrock /* 28001544Seschrock * Let the world know that we're done. 28011544Seschrock */ 28021544Seschrock mutex_enter(&spa->spa_async_lock); 28031544Seschrock spa->spa_async_thread = NULL; 28041544Seschrock cv_broadcast(&spa->spa_async_cv); 28051544Seschrock mutex_exit(&spa->spa_async_lock); 28061544Seschrock thread_exit(); 28071544Seschrock } 28081544Seschrock 28091544Seschrock void 28101544Seschrock spa_async_suspend(spa_t *spa) 28111544Seschrock { 28121544Seschrock mutex_enter(&spa->spa_async_lock); 28131544Seschrock spa->spa_async_suspended++; 28141544Seschrock while (spa->spa_async_thread != NULL) 28151544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 28161544Seschrock mutex_exit(&spa->spa_async_lock); 28171544Seschrock } 28181544Seschrock 28191544Seschrock void 28201544Seschrock spa_async_resume(spa_t *spa) 28211544Seschrock { 28221544Seschrock mutex_enter(&spa->spa_async_lock); 28231544Seschrock ASSERT(spa->spa_async_suspended != 0); 28241544Seschrock spa->spa_async_suspended--; 28251544Seschrock mutex_exit(&spa->spa_async_lock); 28261544Seschrock } 28271544Seschrock 28281544Seschrock static void 28291544Seschrock spa_async_dispatch(spa_t *spa) 28301544Seschrock { 28311544Seschrock mutex_enter(&spa->spa_async_lock); 28321544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 28331635Sbonwick spa->spa_async_thread == NULL && 28341635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 28351544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 28361544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 28371544Seschrock mutex_exit(&spa->spa_async_lock); 28381544Seschrock } 28391544Seschrock 28401544Seschrock void 28411544Seschrock spa_async_request(spa_t *spa, int task) 28421544Seschrock { 28431544Seschrock mutex_enter(&spa->spa_async_lock); 28441544Seschrock spa->spa_async_tasks |= task; 28451544Seschrock mutex_exit(&spa->spa_async_lock); 2846789Sahrens } 2847789Sahrens 2848789Sahrens /* 2849789Sahrens * ========================================================================== 2850789Sahrens * SPA syncing routines 2851789Sahrens * ========================================================================== 2852789Sahrens */ 2853789Sahrens 2854789Sahrens static void 2855789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2856789Sahrens { 2857789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2858789Sahrens dmu_tx_t *tx; 2859789Sahrens blkptr_t blk; 2860789Sahrens uint64_t itor = 0; 2861789Sahrens zio_t *zio; 2862789Sahrens int error; 2863789Sahrens uint8_t c = 1; 2864789Sahrens 2865789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2866789Sahrens 2867789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2868789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2869789Sahrens 2870789Sahrens error = zio_wait(zio); 2871789Sahrens ASSERT3U(error, ==, 0); 2872789Sahrens 2873789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2874789Sahrens bplist_vacate(bpl, tx); 2875789Sahrens 2876789Sahrens /* 2877789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2878789Sahrens * (Usually only the first block is needed.) 2879789Sahrens */ 2880789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2881789Sahrens dmu_tx_commit(tx); 2882789Sahrens } 2883789Sahrens 2884789Sahrens static void 28852082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 28862082Seschrock { 28872082Seschrock char *packed = NULL; 28882082Seschrock size_t nvsize = 0; 28892082Seschrock dmu_buf_t *db; 28902082Seschrock 28912082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 28922082Seschrock 28932082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 28942082Seschrock 28952082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 28962082Seschrock KM_SLEEP) == 0); 28972082Seschrock 28982082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 28992082Seschrock 29002082Seschrock kmem_free(packed, nvsize); 29012082Seschrock 29022082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 29032082Seschrock dmu_buf_will_dirty(db, tx); 29042082Seschrock *(uint64_t *)db->db_data = nvsize; 29052082Seschrock dmu_buf_rele(db, FTAG); 29062082Seschrock } 29072082Seschrock 29082082Seschrock static void 29092082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 29102082Seschrock { 29112082Seschrock nvlist_t *nvroot; 29122082Seschrock nvlist_t **spares; 29132082Seschrock int i; 29142082Seschrock 29152082Seschrock if (!spa->spa_sync_spares) 29162082Seschrock return; 29172082Seschrock 29182082Seschrock /* 29192082Seschrock * Update the MOS nvlist describing the list of available spares. 29202082Seschrock * spa_validate_spares() will have already made sure this nvlist is 29214451Seschrock * valid and the vdevs are labeled appropriately. 29222082Seschrock */ 29232082Seschrock if (spa->spa_spares_object == 0) { 29242082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 29252082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 29262082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 29272082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 29282082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 29292082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 29302082Seschrock } 29312082Seschrock 29322082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 29332082Seschrock if (spa->spa_nspares == 0) { 29342082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29352082Seschrock NULL, 0) == 0); 29362082Seschrock } else { 29372082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 29382082Seschrock KM_SLEEP); 29392082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29402082Seschrock spares[i] = vdev_config_generate(spa, 29412082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 29422082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29432082Seschrock spares, spa->spa_nspares) == 0); 29442082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29452082Seschrock nvlist_free(spares[i]); 29462082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 29472082Seschrock } 29482082Seschrock 29492082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 29502926Sek110237 nvlist_free(nvroot); 29512082Seschrock 29522082Seschrock spa->spa_sync_spares = B_FALSE; 29532082Seschrock } 29542082Seschrock 29552082Seschrock static void 2956789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2957789Sahrens { 2958789Sahrens nvlist_t *config; 2959789Sahrens 2960789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2961789Sahrens return; 2962789Sahrens 2963789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2964789Sahrens 29651635Sbonwick if (spa->spa_config_syncing) 29661635Sbonwick nvlist_free(spa->spa_config_syncing); 29671635Sbonwick spa->spa_config_syncing = config; 2968789Sahrens 29692082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2970789Sahrens } 2971789Sahrens 29723912Slling static void 29734543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 29743912Slling { 29753912Slling spa_t *spa = arg1; 29763912Slling nvlist_t *nvp = arg2; 29773912Slling nvpair_t *nvpair; 29783912Slling objset_t *mos = spa->spa_meta_objset; 29793912Slling uint64_t zapobj; 29804451Seschrock uint64_t intval; 29813912Slling 29823912Slling mutex_enter(&spa->spa_props_lock); 29833912Slling if (spa->spa_pool_props_object == 0) { 29843912Slling zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 29853912Slling VERIFY(zapobj > 0); 29863912Slling 29873912Slling spa->spa_pool_props_object = zapobj; 29883912Slling 29893912Slling VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 29903912Slling DMU_POOL_PROPS, 8, 1, 29913912Slling &spa->spa_pool_props_object, tx) == 0); 29923912Slling } 29933912Slling mutex_exit(&spa->spa_props_lock); 29943912Slling 29953912Slling nvpair = NULL; 29963912Slling while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 29973912Slling switch (zpool_name_to_prop(nvpair_name(nvpair))) { 29984543Smarks case ZPOOL_PROP_DELEGATION: 29994543Smarks VERIFY(nvlist_lookup_uint64(nvp, 30004543Smarks nvpair_name(nvpair), &intval) == 0); 30014543Smarks VERIFY(zap_update(mos, 30024543Smarks spa->spa_pool_props_object, 30034543Smarks nvpair_name(nvpair), 8, 1, 30044543Smarks &intval, tx) == 0); 30054543Smarks spa->spa_delegation = intval; 30064543Smarks break; 30074451Seschrock case ZPOOL_PROP_BOOTFS: 30083912Slling VERIFY(nvlist_lookup_uint64(nvp, 30093912Slling nvpair_name(nvpair), &spa->spa_bootfs) == 0); 30104543Smarks intval = spa->spa_bootfs; 30113912Slling VERIFY(zap_update(mos, 30123912Slling spa->spa_pool_props_object, 30134451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 8, 1, 30144543Smarks &intval, tx) == 0); 30153912Slling break; 30164451Seschrock 30174451Seschrock case ZPOOL_PROP_AUTOREPLACE: 30184451Seschrock VERIFY(nvlist_lookup_uint64(nvp, 30194451Seschrock nvpair_name(nvpair), &intval) == 0); 30204451Seschrock VERIFY(zap_update(mos, 30214451Seschrock spa->spa_pool_props_object, 30224451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 8, 1, 30234451Seschrock &intval, tx) == 0); 30244451Seschrock break; 30253912Slling } 30264543Smarks spa_history_internal_log(LOG_POOL_PROPSET, 30274543Smarks spa, tx, cr, "%s %lld %s", 30284543Smarks nvpair_name(nvpair), intval, 30294543Smarks spa->spa_name); 30303912Slling } 30313912Slling } 30323912Slling 3033789Sahrens /* 3034789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3035789Sahrens * part of the process, so we iterate until it converges. 3036789Sahrens */ 3037789Sahrens void 3038789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3039789Sahrens { 3040789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3041789Sahrens objset_t *mos = spa->spa_meta_objset; 3042789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 30431635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3044789Sahrens vdev_t *vd; 3045789Sahrens dmu_tx_t *tx; 3046789Sahrens int dirty_vdevs; 3047789Sahrens 3048789Sahrens /* 3049789Sahrens * Lock out configuration changes. 3050789Sahrens */ 30511544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3052789Sahrens 3053789Sahrens spa->spa_syncing_txg = txg; 3054789Sahrens spa->spa_sync_pass = 0; 3055789Sahrens 30561544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3057789Sahrens 30582082Seschrock tx = dmu_tx_create_assigned(dp, txg); 30592082Seschrock 30602082Seschrock /* 30614577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 30622082Seschrock * set spa_deflate if we have no raid-z vdevs. 30632082Seschrock */ 30644577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 30654577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 30662082Seschrock int i; 30672082Seschrock 30682082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 30692082Seschrock vd = rvd->vdev_child[i]; 30702082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 30712082Seschrock break; 30722082Seschrock } 30732082Seschrock if (i == rvd->vdev_children) { 30742082Seschrock spa->spa_deflate = TRUE; 30752082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 30762082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 30772082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 30782082Seschrock } 30792082Seschrock } 30802082Seschrock 3081789Sahrens /* 3082789Sahrens * If anything has changed in this txg, push the deferred frees 3083789Sahrens * from the previous txg. If not, leave them alone so that we 3084789Sahrens * don't generate work on an otherwise idle system. 3085789Sahrens */ 3086789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 30872329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 30882329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3089789Sahrens spa_sync_deferred_frees(spa, txg); 3090789Sahrens 3091789Sahrens /* 3092789Sahrens * Iterate to convergence. 3093789Sahrens */ 3094789Sahrens do { 3095789Sahrens spa->spa_sync_pass++; 3096789Sahrens 3097789Sahrens spa_sync_config_object(spa, tx); 30982082Seschrock spa_sync_spares(spa, tx); 30991544Seschrock spa_errlog_sync(spa, txg); 3100789Sahrens dsl_pool_sync(dp, txg); 3101789Sahrens 3102789Sahrens dirty_vdevs = 0; 3103789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3104789Sahrens vdev_sync(vd, txg); 3105789Sahrens dirty_vdevs++; 3106789Sahrens } 3107789Sahrens 3108789Sahrens bplist_sync(bpl, tx); 3109789Sahrens } while (dirty_vdevs); 3110789Sahrens 3111789Sahrens bplist_close(bpl); 3112789Sahrens 3113789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3114789Sahrens 3115789Sahrens /* 3116789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3117789Sahrens * to commit the transaction group. 31181635Sbonwick * 31191635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 31201635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 31211635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 31221635Sbonwick * If the write fails, try the next vdev until we're tried them all. 3123789Sahrens */ 31241635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 31251635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31261635Sbonwick } else { 31271635Sbonwick int children = rvd->vdev_children; 31281635Sbonwick int c0 = spa_get_random(children); 31291635Sbonwick int c; 31301635Sbonwick 31311635Sbonwick for (c = 0; c < children; c++) { 31321635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 31331635Sbonwick if (vd->vdev_ms_array == 0) 31341635Sbonwick continue; 31351635Sbonwick if (vdev_config_sync(vd, txg) == 0) 31361635Sbonwick break; 31371635Sbonwick } 31381635Sbonwick if (c == children) 31391635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31401635Sbonwick } 31411635Sbonwick 31422082Seschrock dmu_tx_commit(tx); 31432082Seschrock 31441635Sbonwick /* 31451635Sbonwick * Clear the dirty config list. 31461635Sbonwick */ 31471635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 31481635Sbonwick vdev_config_clean(vd); 31491635Sbonwick 31501635Sbonwick /* 31511635Sbonwick * Now that the new config has synced transactionally, 31521635Sbonwick * let it become visible to the config cache. 31531635Sbonwick */ 31541635Sbonwick if (spa->spa_config_syncing != NULL) { 31551635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 31561635Sbonwick spa->spa_config_txg = txg; 31571635Sbonwick spa->spa_config_syncing = NULL; 31581635Sbonwick } 3159789Sahrens 3160789Sahrens /* 3161789Sahrens * Make a stable copy of the fully synced uberblock. 3162789Sahrens * We use this as the root for pool traversals. 3163789Sahrens */ 3164789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3165789Sahrens 3166789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3167789Sahrens 3168789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3169789Sahrens spa->spa_traverse_wanted = 0; 3170789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3171789Sahrens rw_exit(&spa->spa_traverse_lock); 3172789Sahrens 3173789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3174789Sahrens 3175789Sahrens /* 3176789Sahrens * Clean up the ZIL records for the synced txg. 3177789Sahrens */ 3178789Sahrens dsl_pool_zil_clean(dp); 3179789Sahrens 3180789Sahrens /* 3181789Sahrens * Update usable space statistics. 3182789Sahrens */ 3183789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3184789Sahrens vdev_sync_done(vd, txg); 3185789Sahrens 3186789Sahrens /* 3187789Sahrens * It had better be the case that we didn't dirty anything 31882082Seschrock * since vdev_config_sync(). 3189789Sahrens */ 3190789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3191789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3192789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3193789Sahrens ASSERT(bpl->bpl_queue == NULL); 3194789Sahrens 31951544Seschrock spa_config_exit(spa, FTAG); 31961544Seschrock 31971544Seschrock /* 31981544Seschrock * If any async tasks have been requested, kick them off. 31991544Seschrock */ 32001544Seschrock spa_async_dispatch(spa); 3201789Sahrens } 3202789Sahrens 3203789Sahrens /* 3204789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3205789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3206789Sahrens * sync. 3207789Sahrens */ 3208789Sahrens void 3209789Sahrens spa_sync_allpools(void) 3210789Sahrens { 3211789Sahrens spa_t *spa = NULL; 3212789Sahrens mutex_enter(&spa_namespace_lock); 3213789Sahrens while ((spa = spa_next(spa)) != NULL) { 3214789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3215789Sahrens continue; 3216789Sahrens spa_open_ref(spa, FTAG); 3217789Sahrens mutex_exit(&spa_namespace_lock); 3218789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3219789Sahrens mutex_enter(&spa_namespace_lock); 3220789Sahrens spa_close(spa, FTAG); 3221789Sahrens } 3222789Sahrens mutex_exit(&spa_namespace_lock); 3223789Sahrens } 3224789Sahrens 3225789Sahrens /* 3226789Sahrens * ========================================================================== 3227789Sahrens * Miscellaneous routines 3228789Sahrens * ========================================================================== 3229789Sahrens */ 3230789Sahrens 3231789Sahrens /* 3232789Sahrens * Remove all pools in the system. 3233789Sahrens */ 3234789Sahrens void 3235789Sahrens spa_evict_all(void) 3236789Sahrens { 3237789Sahrens spa_t *spa; 3238789Sahrens 3239789Sahrens /* 3240789Sahrens * Remove all cached state. All pools should be closed now, 3241789Sahrens * so every spa in the AVL tree should be unreferenced. 3242789Sahrens */ 3243789Sahrens mutex_enter(&spa_namespace_lock); 3244789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3245789Sahrens /* 32461544Seschrock * Stop async tasks. The async thread may need to detach 32471544Seschrock * a device that's been replaced, which requires grabbing 32481544Seschrock * spa_namespace_lock, so we must drop it here. 3249789Sahrens */ 3250789Sahrens spa_open_ref(spa, FTAG); 3251789Sahrens mutex_exit(&spa_namespace_lock); 32521544Seschrock spa_async_suspend(spa); 3253*4808Sek110237 mutex_enter(&spa_namespace_lock); 3254789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3255789Sahrens spa_close(spa, FTAG); 3256789Sahrens 3257789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3258789Sahrens spa_unload(spa); 3259789Sahrens spa_deactivate(spa); 3260789Sahrens } 3261789Sahrens spa_remove(spa); 3262789Sahrens } 3263789Sahrens mutex_exit(&spa_namespace_lock); 3264789Sahrens } 32651544Seschrock 32661544Seschrock vdev_t * 32671544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 32681544Seschrock { 32691544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 32701544Seschrock } 32711760Seschrock 32721760Seschrock void 32731760Seschrock spa_upgrade(spa_t *spa) 32741760Seschrock { 32751760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 32761760Seschrock 32771760Seschrock /* 32781760Seschrock * This should only be called for a non-faulted pool, and since a 32791760Seschrock * future version would result in an unopenable pool, this shouldn't be 32801760Seschrock * possible. 32811760Seschrock */ 32824577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 32834577Sahrens 32844577Sahrens spa->spa_uberblock.ub_version = SPA_VERSION; 32851760Seschrock vdev_config_dirty(spa->spa_root_vdev); 32861760Seschrock 32871760Seschrock spa_config_exit(spa, FTAG); 32882082Seschrock 32892082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 32901760Seschrock } 32912082Seschrock 32922082Seschrock boolean_t 32932082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 32942082Seschrock { 32952082Seschrock int i; 32963377Seschrock uint64_t spareguid; 32972082Seschrock 32982082Seschrock for (i = 0; i < spa->spa_nspares; i++) 32992082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 33002082Seschrock return (B_TRUE); 33012082Seschrock 33023377Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 33033377Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 33043377Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 33053377Seschrock spareguid == guid) 33063377Seschrock return (B_TRUE); 33073377Seschrock } 33083377Seschrock 33092082Seschrock return (B_FALSE); 33102082Seschrock } 33113912Slling 33123912Slling int 33133912Slling spa_set_props(spa_t *spa, nvlist_t *nvp) 33143912Slling { 33153912Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 33163912Slling spa, nvp, 3)); 33173912Slling } 33183912Slling 33193912Slling int 33203912Slling spa_get_props(spa_t *spa, nvlist_t **nvp) 33213912Slling { 33223912Slling zap_cursor_t zc; 33233912Slling zap_attribute_t za; 33243912Slling objset_t *mos = spa->spa_meta_objset; 33253912Slling zfs_source_t src; 33264451Seschrock zpool_prop_t prop; 33273912Slling nvlist_t *propval; 33283912Slling uint64_t value; 33293912Slling int err; 33303912Slling 33313912Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33323912Slling 33333912Slling mutex_enter(&spa->spa_props_lock); 33343912Slling /* If no props object, then just return empty nvlist */ 33353912Slling if (spa->spa_pool_props_object == 0) { 33363912Slling mutex_exit(&spa->spa_props_lock); 33373912Slling return (0); 33383912Slling } 33393912Slling 33403912Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 33413912Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 33423912Slling zap_cursor_advance(&zc)) { 33433912Slling 33443912Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 33453912Slling continue; 33463912Slling 33473912Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33483912Slling switch (za.za_integer_length) { 33493912Slling case 8: 33504451Seschrock if (zpool_prop_default_numeric(prop) == 33513912Slling za.za_first_integer) 33523912Slling src = ZFS_SRC_DEFAULT; 33533912Slling else 33543912Slling src = ZFS_SRC_LOCAL; 33553912Slling value = za.za_first_integer; 33563912Slling 33574451Seschrock if (prop == ZPOOL_PROP_BOOTFS) { 33583912Slling dsl_pool_t *dp; 33593912Slling dsl_dataset_t *ds = NULL; 33603912Slling char strval[MAXPATHLEN]; 33613912Slling 33623912Slling dp = spa_get_dsl(spa); 33633912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 33643912Slling if ((err = dsl_dataset_open_obj(dp, 33653912Slling za.za_first_integer, NULL, DS_MODE_NONE, 33663912Slling FTAG, &ds)) != 0) { 33673912Slling rw_exit(&dp->dp_config_rwlock); 33683912Slling break; 33693912Slling } 33703912Slling dsl_dataset_name(ds, strval); 33713912Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 33723912Slling rw_exit(&dp->dp_config_rwlock); 33733912Slling 33743912Slling VERIFY(nvlist_add_uint64(propval, 33753912Slling ZFS_PROP_SOURCE, src) == 0); 33763912Slling VERIFY(nvlist_add_string(propval, 33773912Slling ZFS_PROP_VALUE, strval) == 0); 33783912Slling } else { 33793912Slling VERIFY(nvlist_add_uint64(propval, 33803912Slling ZFS_PROP_SOURCE, src) == 0); 33813912Slling VERIFY(nvlist_add_uint64(propval, 33823912Slling ZFS_PROP_VALUE, value) == 0); 33833912Slling } 33843912Slling VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 33853912Slling propval) == 0); 33863912Slling break; 33873912Slling } 33883912Slling nvlist_free(propval); 33893912Slling } 33903912Slling zap_cursor_fini(&zc); 33913912Slling mutex_exit(&spa->spa_props_lock); 33923912Slling if (err && err != ENOENT) { 33933912Slling nvlist_free(*nvp); 33943912Slling return (err); 33953912Slling } 33963912Slling 33973912Slling return (0); 33983912Slling } 33993912Slling 34003912Slling /* 34013912Slling * If the bootfs property value is dsobj, clear it. 34023912Slling */ 34033912Slling void 34043912Slling spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 34053912Slling { 34063912Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 34073912Slling VERIFY(zap_remove(spa->spa_meta_objset, 34083912Slling spa->spa_pool_props_object, 34094451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 34103912Slling spa->spa_bootfs = 0; 34113912Slling } 34123912Slling } 34134451Seschrock 34144451Seschrock /* 34154451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 34164451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 34174451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 34184451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 34194451Seschrock * or zdb as real changes. 34204451Seschrock */ 34214451Seschrock void 34224451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 34234451Seschrock { 34244451Seschrock #ifdef _KERNEL 34254451Seschrock sysevent_t *ev; 34264451Seschrock sysevent_attr_list_t *attr = NULL; 34274451Seschrock sysevent_value_t value; 34284451Seschrock sysevent_id_t eid; 34294451Seschrock 34304451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 34314451Seschrock SE_SLEEP); 34324451Seschrock 34334451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34344451Seschrock value.value.sv_string = spa_name(spa); 34354451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 34364451Seschrock goto done; 34374451Seschrock 34384451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34394451Seschrock value.value.sv_uint64 = spa_guid(spa); 34404451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 34414451Seschrock goto done; 34424451Seschrock 34434451Seschrock if (vd) { 34444451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34454451Seschrock value.value.sv_uint64 = vd->vdev_guid; 34464451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 34474451Seschrock SE_SLEEP) != 0) 34484451Seschrock goto done; 34494451Seschrock 34504451Seschrock if (vd->vdev_path) { 34514451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34524451Seschrock value.value.sv_string = vd->vdev_path; 34534451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 34544451Seschrock &value, SE_SLEEP) != 0) 34554451Seschrock goto done; 34564451Seschrock } 34574451Seschrock } 34584451Seschrock 34594451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 34604451Seschrock 34614451Seschrock done: 34624451Seschrock if (attr) 34634451Seschrock sysevent_free_attr(attr); 34644451Seschrock sysevent_free(ev); 34654451Seschrock #endif 34664451Seschrock } 3467