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 1352856Snd150628 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 1362856Snd150628 mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL); 1372856Snd150628 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 1382856Snd150628 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 1392856Snd150628 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 1402856Snd150628 mutex_init(&spa->spa_config_lock.scl_lock, NULL, MUTEX_DEFAULT, NULL); 1412856Snd150628 mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL); 1422926Sek110237 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 1433912Slling mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 1442856Snd150628 145789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 146789Sahrens offsetof(vdev_t, vdev_dirty_node)); 147789Sahrens 148789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 149789Sahrens offsetof(struct vdev, vdev_txg_node)); 1501544Seschrock 1511544Seschrock avl_create(&spa->spa_errlist_scrub, 1521544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1531544Seschrock offsetof(spa_error_entry_t, se_avl)); 1541544Seschrock avl_create(&spa->spa_errlist_last, 1551544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1561544Seschrock offsetof(spa_error_entry_t, se_avl)); 157789Sahrens } 158789Sahrens 159789Sahrens /* 160789Sahrens * Opposite of spa_activate(). 161789Sahrens */ 162789Sahrens static void 163789Sahrens spa_deactivate(spa_t *spa) 164789Sahrens { 165789Sahrens int t; 166789Sahrens 167789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 168789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 169789Sahrens ASSERT(spa->spa_root_vdev == NULL); 170789Sahrens 171789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 172789Sahrens 173789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 174789Sahrens 175789Sahrens list_destroy(&spa->spa_dirty_list); 176789Sahrens 177789Sahrens rw_destroy(&spa->spa_traverse_lock); 178789Sahrens 179789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 180789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 181789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 182789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 183789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 184789Sahrens } 185789Sahrens 186789Sahrens metaslab_class_destroy(spa->spa_normal_class); 187789Sahrens spa->spa_normal_class = NULL; 188789Sahrens 1894527Sperrin metaslab_class_destroy(spa->spa_log_class); 1904527Sperrin spa->spa_log_class = NULL; 1914527Sperrin 1921544Seschrock /* 1931544Seschrock * If this was part of an import or the open otherwise failed, we may 1941544Seschrock * still have errors left in the queues. Empty them just in case. 1951544Seschrock */ 1961544Seschrock spa_errlog_drain(spa); 1971544Seschrock 1981544Seschrock avl_destroy(&spa->spa_errlist_scrub); 1991544Seschrock avl_destroy(&spa->spa_errlist_last); 2001544Seschrock 201789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 202789Sahrens } 203789Sahrens 204789Sahrens /* 205789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 206789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 207789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 208789Sahrens * All vdev validation is done by the vdev_alloc() routine. 209789Sahrens */ 2102082Seschrock static int 2112082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 2122082Seschrock uint_t id, int atype) 213789Sahrens { 214789Sahrens nvlist_t **child; 215789Sahrens uint_t c, children; 2162082Seschrock int error; 2172082Seschrock 2182082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 2192082Seschrock return (error); 2202082Seschrock 2212082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 2222082Seschrock return (0); 223789Sahrens 224789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 225789Sahrens &child, &children) != 0) { 2262082Seschrock vdev_free(*vdp); 2272082Seschrock *vdp = NULL; 2282082Seschrock return (EINVAL); 229789Sahrens } 230789Sahrens 231789Sahrens for (c = 0; c < children; c++) { 2322082Seschrock vdev_t *vd; 2332082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 2342082Seschrock atype)) != 0) { 2352082Seschrock vdev_free(*vdp); 2362082Seschrock *vdp = NULL; 2372082Seschrock return (error); 238789Sahrens } 239789Sahrens } 240789Sahrens 2412082Seschrock ASSERT(*vdp != NULL); 2422082Seschrock 2432082Seschrock return (0); 244789Sahrens } 245789Sahrens 246789Sahrens /* 247789Sahrens * Opposite of spa_load(). 248789Sahrens */ 249789Sahrens static void 250789Sahrens spa_unload(spa_t *spa) 251789Sahrens { 2522082Seschrock int i; 2532082Seschrock 254789Sahrens /* 2551544Seschrock * Stop async tasks. 2561544Seschrock */ 2571544Seschrock spa_async_suspend(spa); 2581544Seschrock 2591544Seschrock /* 260789Sahrens * Stop syncing. 261789Sahrens */ 262789Sahrens if (spa->spa_sync_on) { 263789Sahrens txg_sync_stop(spa->spa_dsl_pool); 264789Sahrens spa->spa_sync_on = B_FALSE; 265789Sahrens } 266789Sahrens 267789Sahrens /* 268789Sahrens * Wait for any outstanding prefetch I/O to complete. 269789Sahrens */ 2701544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2711544Seschrock spa_config_exit(spa, FTAG); 272789Sahrens 273789Sahrens /* 274789Sahrens * Close the dsl pool. 275789Sahrens */ 276789Sahrens if (spa->spa_dsl_pool) { 277789Sahrens dsl_pool_close(spa->spa_dsl_pool); 278789Sahrens spa->spa_dsl_pool = NULL; 279789Sahrens } 280789Sahrens 281789Sahrens /* 282789Sahrens * Close all vdevs. 283789Sahrens */ 2841585Sbonwick if (spa->spa_root_vdev) 285789Sahrens vdev_free(spa->spa_root_vdev); 2861585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 2871544Seschrock 2882082Seschrock for (i = 0; i < spa->spa_nspares; i++) 2892082Seschrock vdev_free(spa->spa_spares[i]); 2902082Seschrock if (spa->spa_spares) { 2912082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 2922082Seschrock spa->spa_spares = NULL; 2932082Seschrock } 2942082Seschrock if (spa->spa_sparelist) { 2952082Seschrock nvlist_free(spa->spa_sparelist); 2962082Seschrock spa->spa_sparelist = NULL; 2972082Seschrock } 2982082Seschrock 2991544Seschrock spa->spa_async_suspended = 0; 300789Sahrens } 301789Sahrens 302789Sahrens /* 3032082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 3042082Seschrock * this pool. When this is called, we have some form of basic information in 3052082Seschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 3062082Seschrock * re-generate a more complete list including status information. 3072082Seschrock */ 3082082Seschrock static void 3092082Seschrock spa_load_spares(spa_t *spa) 3102082Seschrock { 3112082Seschrock nvlist_t **spares; 3122082Seschrock uint_t nspares; 3132082Seschrock int i; 3143377Seschrock vdev_t *vd, *tvd; 3152082Seschrock 3162082Seschrock /* 3172082Seschrock * First, close and free any existing spare vdevs. 3182082Seschrock */ 3192082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3203377Seschrock vd = spa->spa_spares[i]; 3213377Seschrock 3223377Seschrock /* Undo the call to spa_activate() below */ 3233377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 3243377Seschrock tvd->vdev_isspare) 3253377Seschrock spa_spare_remove(tvd); 3263377Seschrock vdev_close(vd); 3273377Seschrock vdev_free(vd); 3282082Seschrock } 3293377Seschrock 3302082Seschrock if (spa->spa_spares) 3312082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 3322082Seschrock 3332082Seschrock if (spa->spa_sparelist == NULL) 3342082Seschrock nspares = 0; 3352082Seschrock else 3362082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 3372082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 3382082Seschrock 3392082Seschrock spa->spa_nspares = (int)nspares; 3402082Seschrock spa->spa_spares = NULL; 3412082Seschrock 3422082Seschrock if (nspares == 0) 3432082Seschrock return; 3442082Seschrock 3452082Seschrock /* 3462082Seschrock * Construct the array of vdevs, opening them to get status in the 3473377Seschrock * process. For each spare, there is potentially two different vdev_t 3483377Seschrock * structures associated with it: one in the list of spares (used only 3493377Seschrock * for basic validation purposes) and one in the active vdev 3503377Seschrock * configuration (if it's spared in). During this phase we open and 3513377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 3523377Seschrock * active configuration, then we also mark this vdev as an active spare. 3532082Seschrock */ 3542082Seschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 3552082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3562082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 3572082Seschrock VDEV_ALLOC_SPARE) == 0); 3582082Seschrock ASSERT(vd != NULL); 3592082Seschrock 3602082Seschrock spa->spa_spares[i] = vd; 3612082Seschrock 3623377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 3633377Seschrock if (!tvd->vdev_isspare) 3643377Seschrock spa_spare_add(tvd); 3653377Seschrock 3663377Seschrock /* 3673377Seschrock * We only mark the spare active if we were successfully 3683377Seschrock * able to load the vdev. Otherwise, importing a pool 3693377Seschrock * with a bad active spare would result in strange 3703377Seschrock * behavior, because multiple pool would think the spare 3713377Seschrock * is actively in use. 3723377Seschrock * 3733377Seschrock * There is a vulnerability here to an equally bizarre 3743377Seschrock * circumstance, where a dead active spare is later 3753377Seschrock * brought back to life (onlined or otherwise). Given 3763377Seschrock * the rarity of this scenario, and the extra complexity 3773377Seschrock * it adds, we ignore the possibility. 3783377Seschrock */ 3793377Seschrock if (!vdev_is_dead(tvd)) 3803377Seschrock spa_spare_activate(tvd); 3813377Seschrock } 3823377Seschrock 3832082Seschrock if (vdev_open(vd) != 0) 3842082Seschrock continue; 3852082Seschrock 3862082Seschrock vd->vdev_top = vd; 3872082Seschrock (void) vdev_validate_spare(vd); 3882082Seschrock } 3892082Seschrock 3902082Seschrock /* 3912082Seschrock * Recompute the stashed list of spares, with status information 3922082Seschrock * this time. 3932082Seschrock */ 3942082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3952082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3962082Seschrock 3972082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 3982082Seschrock for (i = 0; i < spa->spa_nspares; i++) 3992082Seschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 4002082Seschrock B_TRUE, B_TRUE); 4012082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 4022082Seschrock spares, spa->spa_nspares) == 0); 4032082Seschrock for (i = 0; i < spa->spa_nspares; i++) 4042082Seschrock nvlist_free(spares[i]); 4052082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 4062082Seschrock } 4072082Seschrock 4082082Seschrock static int 4092082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 4102082Seschrock { 4112082Seschrock dmu_buf_t *db; 4122082Seschrock char *packed = NULL; 4132082Seschrock size_t nvsize = 0; 4142082Seschrock int error; 4152082Seschrock *value = NULL; 4162082Seschrock 4172082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 4182082Seschrock nvsize = *(uint64_t *)db->db_data; 4192082Seschrock dmu_buf_rele(db, FTAG); 4202082Seschrock 4212082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 4222082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 4232082Seschrock if (error == 0) 4242082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 4252082Seschrock kmem_free(packed, nvsize); 4262082Seschrock 4272082Seschrock return (error); 4282082Seschrock } 4292082Seschrock 4302082Seschrock /* 4314451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 4324451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 4334451Seschrock */ 4344451Seschrock static void 4354451Seschrock spa_check_removed(vdev_t *vd) 4364451Seschrock { 4374451Seschrock int c; 4384451Seschrock 4394451Seschrock for (c = 0; c < vd->vdev_children; c++) 4404451Seschrock spa_check_removed(vd->vdev_child[c]); 4414451Seschrock 4424451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 4434451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 4444451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 4454451Seschrock } 4464451Seschrock } 4474451Seschrock 4484451Seschrock /* 449789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 4501544Seschrock * source of configuration information. 451789Sahrens */ 452789Sahrens static int 4531544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 454789Sahrens { 455789Sahrens int error = 0; 456789Sahrens nvlist_t *nvroot = NULL; 457789Sahrens vdev_t *rvd; 458789Sahrens uberblock_t *ub = &spa->spa_uberblock; 4591635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 460789Sahrens uint64_t pool_guid; 4612082Seschrock uint64_t version; 462789Sahrens zio_t *zio; 4634451Seschrock uint64_t autoreplace = 0; 464789Sahrens 4651544Seschrock spa->spa_load_state = state; 4661635Sbonwick 467789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 4681733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 4691544Seschrock error = EINVAL; 4701544Seschrock goto out; 4711544Seschrock } 472789Sahrens 4732082Seschrock /* 4742082Seschrock * Versioning wasn't explicitly added to the label until later, so if 4752082Seschrock * it's not present treat it as the initial version. 4762082Seschrock */ 4772082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 4782082Seschrock version = ZFS_VERSION_INITIAL; 4792082Seschrock 4801733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 4811733Sbonwick &spa->spa_config_txg); 4821733Sbonwick 4831635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 4841544Seschrock spa_guid_exists(pool_guid, 0)) { 4851544Seschrock error = EEXIST; 4861544Seschrock goto out; 4871544Seschrock } 488789Sahrens 4892174Seschrock spa->spa_load_guid = pool_guid; 4902174Seschrock 491789Sahrens /* 4922082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 4932082Seschrock * value that will be returned by spa_version() since parsing the 4942082Seschrock * configuration requires knowing the version number. 495789Sahrens */ 4961544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 4972082Seschrock spa->spa_ubsync.ub_version = version; 4982082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 4991544Seschrock spa_config_exit(spa, FTAG); 500789Sahrens 5012082Seschrock if (error != 0) 5021544Seschrock goto out; 503789Sahrens 5041585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 505789Sahrens ASSERT(spa_guid(spa) == pool_guid); 506789Sahrens 507789Sahrens /* 508789Sahrens * Try to open all vdevs, loading each label in the process. 509789Sahrens */ 5104070Smc142369 error = vdev_open(rvd); 5114070Smc142369 if (error != 0) 5121544Seschrock goto out; 513789Sahrens 514789Sahrens /* 5151986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 5161986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 5171986Seschrock * flag. 5181986Seschrock */ 5191986Seschrock spa_config_enter(spa, RW_READER, FTAG); 5201986Seschrock error = vdev_validate(rvd); 5211986Seschrock spa_config_exit(spa, FTAG); 5221986Seschrock 5234070Smc142369 if (error != 0) 5241986Seschrock goto out; 5251986Seschrock 5261986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 5271986Seschrock error = ENXIO; 5281986Seschrock goto out; 5291986Seschrock } 5301986Seschrock 5311986Seschrock /* 532789Sahrens * Find the best uberblock. 533789Sahrens */ 534789Sahrens bzero(ub, sizeof (uberblock_t)); 535789Sahrens 536789Sahrens zio = zio_root(spa, NULL, NULL, 537789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 538789Sahrens vdev_uberblock_load(zio, rvd, ub); 539789Sahrens error = zio_wait(zio); 540789Sahrens 541789Sahrens /* 542789Sahrens * If we weren't able to find a single valid uberblock, return failure. 543789Sahrens */ 544789Sahrens if (ub->ub_txg == 0) { 5451760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5461760Seschrock VDEV_AUX_CORRUPT_DATA); 5471544Seschrock error = ENXIO; 5481544Seschrock goto out; 5491544Seschrock } 5501544Seschrock 5511544Seschrock /* 5521544Seschrock * If the pool is newer than the code, we can't open it. 5531544Seschrock */ 5541760Seschrock if (ub->ub_version > ZFS_VERSION) { 5551760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5561760Seschrock VDEV_AUX_VERSION_NEWER); 5571544Seschrock error = ENOTSUP; 5581544Seschrock goto out; 559789Sahrens } 560789Sahrens 561789Sahrens /* 562789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 563789Sahrens * incomplete configuration. 564789Sahrens */ 5651732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 5661544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5671544Seschrock VDEV_AUX_BAD_GUID_SUM); 5681544Seschrock error = ENXIO; 5691544Seschrock goto out; 570789Sahrens } 571789Sahrens 572789Sahrens /* 573789Sahrens * Initialize internal SPA structures. 574789Sahrens */ 575789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 576789Sahrens spa->spa_ubsync = spa->spa_uberblock; 577789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 5781544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 5791544Seschrock if (error) { 5801544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5811544Seschrock VDEV_AUX_CORRUPT_DATA); 5821544Seschrock goto out; 5831544Seschrock } 584789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 585789Sahrens 5861544Seschrock if (zap_lookup(spa->spa_meta_objset, 587789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 5881544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 5891544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5901544Seschrock VDEV_AUX_CORRUPT_DATA); 5911544Seschrock error = EIO; 5921544Seschrock goto out; 5931544Seschrock } 594789Sahrens 595789Sahrens if (!mosconfig) { 5962082Seschrock nvlist_t *newconfig; 5973975Sek110237 uint64_t hostid; 5982082Seschrock 5992082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 6001544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6011544Seschrock VDEV_AUX_CORRUPT_DATA); 6021544Seschrock error = EIO; 6031544Seschrock goto out; 6041544Seschrock } 605789Sahrens 6063975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 6073975Sek110237 &hostid) == 0) { 6083975Sek110237 char *hostname; 6093975Sek110237 unsigned long myhostid = 0; 6103975Sek110237 6113975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 6123975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 6133975Sek110237 6143975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 6154178Slling if (hostid != 0 && myhostid != 0 && 6164178Slling (unsigned long)hostid != myhostid) { 6173975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 6183975Sek110237 "loaded as it was last accessed by " 6193975Sek110237 "another system (host: %s hostid: 0x%lx). " 6203975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 6213975Sek110237 spa->spa_name, hostname, 6223975Sek110237 (unsigned long)hostid); 6233975Sek110237 error = EBADF; 6243975Sek110237 goto out; 6253975Sek110237 } 6263975Sek110237 } 6273975Sek110237 628789Sahrens spa_config_set(spa, newconfig); 629789Sahrens spa_unload(spa); 630789Sahrens spa_deactivate(spa); 631789Sahrens spa_activate(spa); 632789Sahrens 6331544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 6341544Seschrock } 6351544Seschrock 6361544Seschrock if (zap_lookup(spa->spa_meta_objset, 6371544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 6381544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 6391544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6401544Seschrock VDEV_AUX_CORRUPT_DATA); 6411544Seschrock error = EIO; 6421544Seschrock goto out; 643789Sahrens } 644789Sahrens 6451544Seschrock /* 6462082Seschrock * Load the bit that tells us to use the new accounting function 6472082Seschrock * (raid-z deflation). If we have an older pool, this will not 6482082Seschrock * be present. 6492082Seschrock */ 6502082Seschrock error = zap_lookup(spa->spa_meta_objset, 6512082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 6522082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 6532082Seschrock if (error != 0 && error != ENOENT) { 6542082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6552082Seschrock VDEV_AUX_CORRUPT_DATA); 6562082Seschrock error = EIO; 6572082Seschrock goto out; 6582082Seschrock } 6592082Seschrock 6602082Seschrock /* 6611544Seschrock * Load the persistent error log. If we have an older pool, this will 6621544Seschrock * not be present. 6631544Seschrock */ 6641544Seschrock error = zap_lookup(spa->spa_meta_objset, 6651544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 6661544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 6671807Sbonwick if (error != 0 && error != ENOENT) { 6681544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6691544Seschrock VDEV_AUX_CORRUPT_DATA); 6701544Seschrock error = EIO; 6711544Seschrock goto out; 6721544Seschrock } 6731544Seschrock 6741544Seschrock error = zap_lookup(spa->spa_meta_objset, 6751544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 6761544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 6771544Seschrock if (error != 0 && error != ENOENT) { 6781544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6791544Seschrock VDEV_AUX_CORRUPT_DATA); 6801544Seschrock error = EIO; 6811544Seschrock goto out; 6821544Seschrock } 683789Sahrens 684789Sahrens /* 6852926Sek110237 * Load the history object. If we have an older pool, this 6862926Sek110237 * will not be present. 6872926Sek110237 */ 6882926Sek110237 error = zap_lookup(spa->spa_meta_objset, 6892926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 6902926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 6912926Sek110237 if (error != 0 && error != ENOENT) { 6922926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6932926Sek110237 VDEV_AUX_CORRUPT_DATA); 6942926Sek110237 error = EIO; 6952926Sek110237 goto out; 6962926Sek110237 } 6972926Sek110237 6982926Sek110237 /* 6992082Seschrock * Load any hot spares for this pool. 7002082Seschrock */ 7012082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7022082Seschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 7032082Seschrock if (error != 0 && error != ENOENT) { 7042082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7052082Seschrock VDEV_AUX_CORRUPT_DATA); 7062082Seschrock error = EIO; 7072082Seschrock goto out; 7082082Seschrock } 7092082Seschrock if (error == 0) { 7102082Seschrock ASSERT(spa_version(spa) >= ZFS_VERSION_SPARES); 7112082Seschrock if (load_nvlist(spa, spa->spa_spares_object, 7122082Seschrock &spa->spa_sparelist) != 0) { 7132082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7142082Seschrock VDEV_AUX_CORRUPT_DATA); 7152082Seschrock error = EIO; 7162082Seschrock goto out; 7172082Seschrock } 7182082Seschrock 7192082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 7202082Seschrock spa_load_spares(spa); 7212082Seschrock spa_config_exit(spa, FTAG); 7222082Seschrock } 7232082Seschrock 724*4543Smarks spa->spa_delegation = zfs_prop_default_numeric(ZPOOL_PROP_DELEGATION); 725*4543Smarks 7263912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7273912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 7283912Slling 7293912Slling if (error && error != ENOENT) { 7303912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7313912Slling VDEV_AUX_CORRUPT_DATA); 7323912Slling error = EIO; 7333912Slling goto out; 7343912Slling } 7353912Slling 7363912Slling if (error == 0) { 7373912Slling (void) zap_lookup(spa->spa_meta_objset, 7383912Slling spa->spa_pool_props_object, 7394451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 7403912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 7414451Seschrock (void) zap_lookup(spa->spa_meta_objset, 7424451Seschrock spa->spa_pool_props_object, 7434451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 7444451Seschrock sizeof (uint64_t), 1, &autoreplace); 745*4543Smarks (void) zap_lookup(spa->spa_meta_objset, 746*4543Smarks spa->spa_pool_props_object, 747*4543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 748*4543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 7493912Slling } 7503912Slling 7512082Seschrock /* 7524451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 7534451Seschrock * the ZFS DE that it should not issue any faults for unopenable 7544451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 7554451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 7564451Seschrock * over. 7574451Seschrock */ 7584451Seschrock if (autoreplace) 7594451Seschrock spa_check_removed(spa->spa_root_vdev); 7604451Seschrock 7614451Seschrock /* 7621986Seschrock * Load the vdev state for all toplevel vdevs. 763789Sahrens */ 7641986Seschrock vdev_load(rvd); 765789Sahrens 766789Sahrens /* 767789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 768789Sahrens */ 7691544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 770789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 7711544Seschrock spa_config_exit(spa, FTAG); 772789Sahrens 773789Sahrens /* 774789Sahrens * Check the state of the root vdev. If it can't be opened, it 775789Sahrens * indicates one or more toplevel vdevs are faulted. 776789Sahrens */ 7771544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 7781544Seschrock error = ENXIO; 7791544Seschrock goto out; 7801544Seschrock } 781789Sahrens 7821544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 7831635Sbonwick dmu_tx_t *tx; 7841635Sbonwick int need_update = B_FALSE; 7851585Sbonwick int c; 7861601Sbonwick 7871635Sbonwick /* 7881635Sbonwick * Claim log blocks that haven't been committed yet. 7891635Sbonwick * This must all happen in a single txg. 7901635Sbonwick */ 7911601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 792789Sahrens spa_first_txg(spa)); 7932417Sahrens (void) dmu_objset_find(spa->spa_name, 7942417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 795789Sahrens dmu_tx_commit(tx); 796789Sahrens 797789Sahrens spa->spa_sync_on = B_TRUE; 798789Sahrens txg_sync_start(spa->spa_dsl_pool); 799789Sahrens 800789Sahrens /* 801789Sahrens * Wait for all claims to sync. 802789Sahrens */ 803789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 8041585Sbonwick 8051585Sbonwick /* 8061635Sbonwick * If the config cache is stale, or we have uninitialized 8071635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 8081585Sbonwick */ 8091635Sbonwick if (config_cache_txg != spa->spa_config_txg || 8101635Sbonwick state == SPA_LOAD_IMPORT) 8111635Sbonwick need_update = B_TRUE; 8121635Sbonwick 8131635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 8141635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 8151635Sbonwick need_update = B_TRUE; 8161585Sbonwick 8171585Sbonwick /* 8181635Sbonwick * Update the config cache asychronously in case we're the 8191635Sbonwick * root pool, in which case the config cache isn't writable yet. 8201585Sbonwick */ 8211635Sbonwick if (need_update) 8221635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 823789Sahrens } 824789Sahrens 8251544Seschrock error = 0; 8261544Seschrock out: 8272082Seschrock if (error && error != EBADF) 8281544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 8291544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 8301544Seschrock spa->spa_ena = 0; 8311544Seschrock 8321544Seschrock return (error); 833789Sahrens } 834789Sahrens 835789Sahrens /* 836789Sahrens * Pool Open/Import 837789Sahrens * 838789Sahrens * The import case is identical to an open except that the configuration is sent 839789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 840789Sahrens * case of an open, the pool configuration will exist in the 8414451Seschrock * POOL_STATE_UNINITIALIZED state. 842789Sahrens * 843789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 844789Sahrens * the same time open the pool, without having to keep around the spa_t in some 845789Sahrens * ambiguous state. 846789Sahrens */ 847789Sahrens static int 848789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 849789Sahrens { 850789Sahrens spa_t *spa; 851789Sahrens int error; 852789Sahrens int loaded = B_FALSE; 853789Sahrens int locked = B_FALSE; 854789Sahrens 855789Sahrens *spapp = NULL; 856789Sahrens 857789Sahrens /* 858789Sahrens * As disgusting as this is, we need to support recursive calls to this 859789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 860789Sahrens * up calling spa_open() again. The real fix is to figure out how to 861789Sahrens * avoid dsl_dir_open() calling this in the first place. 862789Sahrens */ 863789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 864789Sahrens mutex_enter(&spa_namespace_lock); 865789Sahrens locked = B_TRUE; 866789Sahrens } 867789Sahrens 868789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 869789Sahrens if (locked) 870789Sahrens mutex_exit(&spa_namespace_lock); 871789Sahrens return (ENOENT); 872789Sahrens } 873789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 874789Sahrens 875789Sahrens spa_activate(spa); 876789Sahrens 8771635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 878789Sahrens 879789Sahrens if (error == EBADF) { 880789Sahrens /* 8811986Seschrock * If vdev_validate() returns failure (indicated by 8821986Seschrock * EBADF), it indicates that one of the vdevs indicates 8831986Seschrock * that the pool has been exported or destroyed. If 8841986Seschrock * this is the case, the config cache is out of sync and 8851986Seschrock * we should remove the pool from the namespace. 886789Sahrens */ 8872082Seschrock zfs_post_ok(spa, NULL); 888789Sahrens spa_unload(spa); 889789Sahrens spa_deactivate(spa); 890789Sahrens spa_remove(spa); 891789Sahrens spa_config_sync(); 892789Sahrens if (locked) 893789Sahrens mutex_exit(&spa_namespace_lock); 894789Sahrens return (ENOENT); 8951544Seschrock } 8961544Seschrock 8971544Seschrock if (error) { 898789Sahrens /* 899789Sahrens * We can't open the pool, but we still have useful 900789Sahrens * information: the state of each vdev after the 901789Sahrens * attempted vdev_open(). Return this to the user. 902789Sahrens */ 9031635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 9041635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 905789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 906789Sahrens B_TRUE); 9071635Sbonwick spa_config_exit(spa, FTAG); 9081635Sbonwick } 909789Sahrens spa_unload(spa); 910789Sahrens spa_deactivate(spa); 9111544Seschrock spa->spa_last_open_failed = B_TRUE; 912789Sahrens if (locked) 913789Sahrens mutex_exit(&spa_namespace_lock); 914789Sahrens *spapp = NULL; 915789Sahrens return (error); 9161544Seschrock } else { 9171544Seschrock zfs_post_ok(spa, NULL); 9181544Seschrock spa->spa_last_open_failed = B_FALSE; 919789Sahrens } 920789Sahrens 921789Sahrens loaded = B_TRUE; 922789Sahrens } 923789Sahrens 924789Sahrens spa_open_ref(spa, tag); 9254451Seschrock 9264451Seschrock /* 9274451Seschrock * If we just loaded the pool, resilver anything that's out of date. 9284451Seschrock */ 9294451Seschrock if (loaded && (spa_mode & FWRITE)) 9304451Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 9314451Seschrock 932789Sahrens if (locked) 933789Sahrens mutex_exit(&spa_namespace_lock); 934789Sahrens 935789Sahrens *spapp = spa; 936789Sahrens 937789Sahrens if (config != NULL) { 9381544Seschrock spa_config_enter(spa, RW_READER, FTAG); 939789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 9401544Seschrock spa_config_exit(spa, FTAG); 941789Sahrens } 942789Sahrens 943789Sahrens return (0); 944789Sahrens } 945789Sahrens 946789Sahrens int 947789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 948789Sahrens { 949789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 950789Sahrens } 951789Sahrens 9521544Seschrock /* 9531544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 9541544Seschrock * preventing it from being exported or destroyed. 9551544Seschrock */ 9561544Seschrock spa_t * 9571544Seschrock spa_inject_addref(char *name) 9581544Seschrock { 9591544Seschrock spa_t *spa; 9601544Seschrock 9611544Seschrock mutex_enter(&spa_namespace_lock); 9621544Seschrock if ((spa = spa_lookup(name)) == NULL) { 9631544Seschrock mutex_exit(&spa_namespace_lock); 9641544Seschrock return (NULL); 9651544Seschrock } 9661544Seschrock spa->spa_inject_ref++; 9671544Seschrock mutex_exit(&spa_namespace_lock); 9681544Seschrock 9691544Seschrock return (spa); 9701544Seschrock } 9711544Seschrock 9721544Seschrock void 9731544Seschrock spa_inject_delref(spa_t *spa) 9741544Seschrock { 9751544Seschrock mutex_enter(&spa_namespace_lock); 9761544Seschrock spa->spa_inject_ref--; 9771544Seschrock mutex_exit(&spa_namespace_lock); 9781544Seschrock } 9791544Seschrock 9802082Seschrock static void 9812082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 9822082Seschrock { 9832082Seschrock nvlist_t **spares; 9842082Seschrock uint_t i, nspares; 9852082Seschrock nvlist_t *nvroot; 9862082Seschrock uint64_t guid; 9872082Seschrock vdev_stat_t *vs; 9882082Seschrock uint_t vsc; 9893377Seschrock uint64_t pool; 9902082Seschrock 9912082Seschrock if (spa->spa_nspares == 0) 9922082Seschrock return; 9932082Seschrock 9942082Seschrock VERIFY(nvlist_lookup_nvlist(config, 9952082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 9962082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 9972082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 9982082Seschrock if (nspares != 0) { 9992082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 10002082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 10012082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 10022082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 10032082Seschrock 10042082Seschrock /* 10052082Seschrock * Go through and find any spares which have since been 10062082Seschrock * repurposed as an active spare. If this is the case, update 10072082Seschrock * their status appropriately. 10082082Seschrock */ 10092082Seschrock for (i = 0; i < nspares; i++) { 10102082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 10112082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 10123377Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 10132082Seschrock VERIFY(nvlist_lookup_uint64_array( 10142082Seschrock spares[i], ZPOOL_CONFIG_STATS, 10152082Seschrock (uint64_t **)&vs, &vsc) == 0); 10162082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 10172082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 10182082Seschrock } 10192082Seschrock } 10202082Seschrock } 10212082Seschrock } 10222082Seschrock 1023789Sahrens int 10241544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1025789Sahrens { 1026789Sahrens int error; 1027789Sahrens spa_t *spa; 1028789Sahrens 1029789Sahrens *config = NULL; 1030789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1031789Sahrens 10322082Seschrock if (spa && *config != NULL) { 10331544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 10341544Seschrock spa_get_errlog_size(spa)) == 0); 10351544Seschrock 10362082Seschrock spa_add_spares(spa, *config); 10372082Seschrock } 10382082Seschrock 10391544Seschrock /* 10401544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 10411544Seschrock * and call spa_lookup() directly. 10421544Seschrock */ 10431544Seschrock if (altroot) { 10441544Seschrock if (spa == NULL) { 10451544Seschrock mutex_enter(&spa_namespace_lock); 10461544Seschrock spa = spa_lookup(name); 10471544Seschrock if (spa) 10481544Seschrock spa_altroot(spa, altroot, buflen); 10491544Seschrock else 10501544Seschrock altroot[0] = '\0'; 10511544Seschrock spa = NULL; 10521544Seschrock mutex_exit(&spa_namespace_lock); 10531544Seschrock } else { 10541544Seschrock spa_altroot(spa, altroot, buflen); 10551544Seschrock } 10561544Seschrock } 10571544Seschrock 1058789Sahrens if (spa != NULL) 1059789Sahrens spa_close(spa, FTAG); 1060789Sahrens 1061789Sahrens return (error); 1062789Sahrens } 1063789Sahrens 1064789Sahrens /* 10652082Seschrock * Validate that the 'spares' array is well formed. We must have an array of 10663377Seschrock * nvlists, each which describes a valid leaf vdev. If this is an import (mode 10673377Seschrock * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 10683377Seschrock * as they are well-formed. 10692082Seschrock */ 10702082Seschrock static int 10712082Seschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 10722082Seschrock { 10732082Seschrock nvlist_t **spares; 10742082Seschrock uint_t i, nspares; 10752082Seschrock vdev_t *vd; 10762082Seschrock int error; 10772082Seschrock 10782082Seschrock /* 10792082Seschrock * It's acceptable to have no spares specified. 10802082Seschrock */ 10812082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10822082Seschrock &spares, &nspares) != 0) 10832082Seschrock return (0); 10842082Seschrock 10852082Seschrock if (nspares == 0) 10862082Seschrock return (EINVAL); 10872082Seschrock 10882082Seschrock /* 10892082Seschrock * Make sure the pool is formatted with a version that supports hot 10902082Seschrock * spares. 10912082Seschrock */ 10922082Seschrock if (spa_version(spa) < ZFS_VERSION_SPARES) 10932082Seschrock return (ENOTSUP); 10942082Seschrock 10953377Seschrock /* 10963377Seschrock * Set the pending spare list so we correctly handle device in-use 10973377Seschrock * checking. 10983377Seschrock */ 10993377Seschrock spa->spa_pending_spares = spares; 11003377Seschrock spa->spa_pending_nspares = nspares; 11013377Seschrock 11022082Seschrock for (i = 0; i < nspares; i++) { 11032082Seschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 11042082Seschrock mode)) != 0) 11053377Seschrock goto out; 11062082Seschrock 11072082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 11082082Seschrock vdev_free(vd); 11093377Seschrock error = EINVAL; 11103377Seschrock goto out; 11112082Seschrock } 11122082Seschrock 11132082Seschrock vd->vdev_top = vd; 11143377Seschrock 11153377Seschrock if ((error = vdev_open(vd)) == 0 && 11163377Seschrock (error = vdev_label_init(vd, crtxg, 11173377Seschrock VDEV_LABEL_SPARE)) == 0) { 11183377Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 11193377Seschrock vd->vdev_guid) == 0); 11202082Seschrock } 11212082Seschrock 11222082Seschrock vdev_free(vd); 11233377Seschrock 11243377Seschrock if (error && mode != VDEV_ALLOC_SPARE) 11253377Seschrock goto out; 11263377Seschrock else 11273377Seschrock error = 0; 11282082Seschrock } 11292082Seschrock 11303377Seschrock out: 11313377Seschrock spa->spa_pending_spares = NULL; 11323377Seschrock spa->spa_pending_nspares = 0; 11333377Seschrock return (error); 11342082Seschrock } 11352082Seschrock 11362082Seschrock /* 1137789Sahrens * Pool Creation 1138789Sahrens */ 1139789Sahrens int 11401635Sbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot) 1141789Sahrens { 1142789Sahrens spa_t *spa; 11431635Sbonwick vdev_t *rvd; 1144789Sahrens dsl_pool_t *dp; 1145789Sahrens dmu_tx_t *tx; 11462082Seschrock int c, error = 0; 1147789Sahrens uint64_t txg = TXG_INITIAL; 11482082Seschrock nvlist_t **spares; 11492082Seschrock uint_t nspares; 1150789Sahrens 1151789Sahrens /* 1152789Sahrens * If this pool already exists, return failure. 1153789Sahrens */ 1154789Sahrens mutex_enter(&spa_namespace_lock); 1155789Sahrens if (spa_lookup(pool) != NULL) { 1156789Sahrens mutex_exit(&spa_namespace_lock); 1157789Sahrens return (EEXIST); 1158789Sahrens } 1159789Sahrens 1160789Sahrens /* 1161789Sahrens * Allocate a new spa_t structure. 1162789Sahrens */ 11631635Sbonwick spa = spa_add(pool, altroot); 1164789Sahrens spa_activate(spa); 1165789Sahrens 1166789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 11671760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 1168789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1169789Sahrens 11701635Sbonwick /* 11711635Sbonwick * Create the root vdev. 11721635Sbonwick */ 11731635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 11741635Sbonwick 11752082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 11762082Seschrock 11772082Seschrock ASSERT(error != 0 || rvd != NULL); 11782082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 11792082Seschrock 11802082Seschrock if (error == 0 && rvd->vdev_children == 0) 11811635Sbonwick error = EINVAL; 11822082Seschrock 11832082Seschrock if (error == 0 && 11842082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 11852082Seschrock (error = spa_validate_spares(spa, nvroot, txg, 11862082Seschrock VDEV_ALLOC_ADD)) == 0) { 11872082Seschrock for (c = 0; c < rvd->vdev_children; c++) 11882082Seschrock vdev_init(rvd->vdev_child[c], txg); 11892082Seschrock vdev_config_dirty(rvd); 11901635Sbonwick } 11911635Sbonwick 11921635Sbonwick spa_config_exit(spa, FTAG); 1193789Sahrens 11942082Seschrock if (error != 0) { 1195789Sahrens spa_unload(spa); 1196789Sahrens spa_deactivate(spa); 1197789Sahrens spa_remove(spa); 1198789Sahrens mutex_exit(&spa_namespace_lock); 1199789Sahrens return (error); 1200789Sahrens } 1201789Sahrens 12022082Seschrock /* 12032082Seschrock * Get the list of spares, if specified. 12042082Seschrock */ 12052082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 12062082Seschrock &spares, &nspares) == 0) { 12072082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 12082082Seschrock KM_SLEEP) == 0); 12092082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 12102082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 12112082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12122082Seschrock spa_load_spares(spa); 12132082Seschrock spa_config_exit(spa, FTAG); 12142082Seschrock spa->spa_sync_spares = B_TRUE; 12152082Seschrock } 12162082Seschrock 1217789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1218789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1219789Sahrens 1220789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1221789Sahrens 1222789Sahrens /* 1223789Sahrens * Create the pool config object. 1224789Sahrens */ 1225789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1226789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1227789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1228789Sahrens 12291544Seschrock if (zap_add(spa->spa_meta_objset, 1230789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 12311544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 12321544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 12331544Seschrock } 1234789Sahrens 12352082Seschrock /* Newly created pools are always deflated. */ 12362082Seschrock spa->spa_deflate = TRUE; 12372082Seschrock if (zap_add(spa->spa_meta_objset, 12382082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12392082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 12402082Seschrock cmn_err(CE_PANIC, "failed to add deflate"); 12412082Seschrock } 12422082Seschrock 1243789Sahrens /* 1244789Sahrens * Create the deferred-free bplist object. Turn off compression 1245789Sahrens * because sync-to-convergence takes longer if the blocksize 1246789Sahrens * keeps changing. 1247789Sahrens */ 1248789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1249789Sahrens 1 << 14, tx); 1250789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1251789Sahrens ZIO_COMPRESS_OFF, tx); 1252789Sahrens 12531544Seschrock if (zap_add(spa->spa_meta_objset, 1254789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12551544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 12561544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 12571544Seschrock } 1258789Sahrens 12592926Sek110237 /* 12602926Sek110237 * Create the pool's history object. 12612926Sek110237 */ 12622926Sek110237 spa_history_create_obj(spa, tx); 12632926Sek110237 1264789Sahrens dmu_tx_commit(tx); 1265789Sahrens 12664451Seschrock spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 1267*4543Smarks spa->spa_delegation = zfs_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1268789Sahrens spa->spa_sync_on = B_TRUE; 1269789Sahrens txg_sync_start(spa->spa_dsl_pool); 1270789Sahrens 1271789Sahrens /* 1272789Sahrens * We explicitly wait for the first transaction to complete so that our 1273789Sahrens * bean counters are appropriately updated. 1274789Sahrens */ 1275789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1276789Sahrens 1277789Sahrens spa_config_sync(); 1278789Sahrens 1279789Sahrens mutex_exit(&spa_namespace_lock); 1280789Sahrens 1281789Sahrens return (0); 1282789Sahrens } 1283789Sahrens 1284789Sahrens /* 1285789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1286789Sahrens * then call spa_load() to do the dirty work. 1287789Sahrens */ 1288789Sahrens int 12891635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 1290789Sahrens { 1291789Sahrens spa_t *spa; 1292789Sahrens int error; 12932082Seschrock nvlist_t *nvroot; 12942082Seschrock nvlist_t **spares; 12952082Seschrock uint_t nspares; 1296789Sahrens 1297789Sahrens if (!(spa_mode & FWRITE)) 1298789Sahrens return (EROFS); 1299789Sahrens 1300789Sahrens /* 1301789Sahrens * If a pool with this name exists, return failure. 1302789Sahrens */ 1303789Sahrens mutex_enter(&spa_namespace_lock); 1304789Sahrens if (spa_lookup(pool) != NULL) { 1305789Sahrens mutex_exit(&spa_namespace_lock); 1306789Sahrens return (EEXIST); 1307789Sahrens } 1308789Sahrens 1309789Sahrens /* 13101635Sbonwick * Create and initialize the spa structure. 1311789Sahrens */ 13121635Sbonwick spa = spa_add(pool, altroot); 1313789Sahrens spa_activate(spa); 1314789Sahrens 1315789Sahrens /* 13161635Sbonwick * Pass off the heavy lifting to spa_load(). 13171732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 13181732Sbonwick * is actually the one to trust when doing an import. 13191601Sbonwick */ 13201732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1321789Sahrens 13222082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13232082Seschrock /* 13242082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 13252082Seschrock * and conflicts with spa_has_spare(). 13262082Seschrock */ 13272082Seschrock if (spa->spa_sparelist) { 13282082Seschrock nvlist_free(spa->spa_sparelist); 13292082Seschrock spa->spa_sparelist = NULL; 13302082Seschrock spa_load_spares(spa); 13312082Seschrock } 13322082Seschrock 13332082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 13342082Seschrock &nvroot) == 0); 13352082Seschrock if (error == 0) 13362082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 13372082Seschrock VDEV_ALLOC_SPARE); 13382082Seschrock spa_config_exit(spa, FTAG); 13392082Seschrock 13402082Seschrock if (error != 0) { 1341789Sahrens spa_unload(spa); 1342789Sahrens spa_deactivate(spa); 1343789Sahrens spa_remove(spa); 1344789Sahrens mutex_exit(&spa_namespace_lock); 1345789Sahrens return (error); 1346789Sahrens } 1347789Sahrens 13481635Sbonwick /* 13492082Seschrock * Override any spares as specified by the user, as these may have 13502082Seschrock * correct device names/devids, etc. 13512082Seschrock */ 13522082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 13532082Seschrock &spares, &nspares) == 0) { 13542082Seschrock if (spa->spa_sparelist) 13552082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 13562082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 13572082Seschrock else 13582082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 13592082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 13602082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 13612082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 13622082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13632082Seschrock spa_load_spares(spa); 13642082Seschrock spa_config_exit(spa, FTAG); 13652082Seschrock spa->spa_sync_spares = B_TRUE; 13662082Seschrock } 13672082Seschrock 13682082Seschrock /* 13691635Sbonwick * Update the config cache to include the newly-imported pool. 13701635Sbonwick */ 13711635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 13721635Sbonwick 1373789Sahrens /* 1374789Sahrens * Resilver anything that's out of date. 1375789Sahrens */ 1376789Sahrens if (spa_mode & FWRITE) 1377789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1378789Sahrens 13794451Seschrock mutex_exit(&spa_namespace_lock); 13804451Seschrock 1381789Sahrens return (0); 1382789Sahrens } 1383789Sahrens 1384789Sahrens /* 1385789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1386789Sahrens * to get the vdev stats associated with the imported devices. 1387789Sahrens */ 1388789Sahrens #define TRYIMPORT_NAME "$import" 1389789Sahrens 1390789Sahrens nvlist_t * 1391789Sahrens spa_tryimport(nvlist_t *tryconfig) 1392789Sahrens { 1393789Sahrens nvlist_t *config = NULL; 1394789Sahrens char *poolname; 1395789Sahrens spa_t *spa; 1396789Sahrens uint64_t state; 1397789Sahrens 1398789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1399789Sahrens return (NULL); 1400789Sahrens 1401789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1402789Sahrens return (NULL); 1403789Sahrens 14041635Sbonwick /* 14051635Sbonwick * Create and initialize the spa structure. 14061635Sbonwick */ 1407789Sahrens mutex_enter(&spa_namespace_lock); 14081635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1409789Sahrens spa_activate(spa); 1410789Sahrens 1411789Sahrens /* 14121635Sbonwick * Pass off the heavy lifting to spa_load(). 14131732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 14141732Sbonwick * is actually the one to trust when doing an import. 1415789Sahrens */ 14161732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1417789Sahrens 1418789Sahrens /* 1419789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1420789Sahrens */ 1421789Sahrens if (spa->spa_root_vdev != NULL) { 14221635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1423789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14241635Sbonwick spa_config_exit(spa, FTAG); 1425789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1426789Sahrens poolname) == 0); 1427789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1428789Sahrens state) == 0); 14293975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 14303975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 14312082Seschrock 14322082Seschrock /* 14332082Seschrock * Add the list of hot spares. 14342082Seschrock */ 14352082Seschrock spa_add_spares(spa, config); 1436789Sahrens } 1437789Sahrens 1438789Sahrens spa_unload(spa); 1439789Sahrens spa_deactivate(spa); 1440789Sahrens spa_remove(spa); 1441789Sahrens mutex_exit(&spa_namespace_lock); 1442789Sahrens 1443789Sahrens return (config); 1444789Sahrens } 1445789Sahrens 1446789Sahrens /* 1447789Sahrens * Pool export/destroy 1448789Sahrens * 1449789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1450789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1451789Sahrens * update the pool state and sync all the labels to disk, removing the 1452789Sahrens * configuration from the cache afterwards. 1453789Sahrens */ 1454789Sahrens static int 14551775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1456789Sahrens { 1457789Sahrens spa_t *spa; 1458789Sahrens 14591775Sbillm if (oldconfig) 14601775Sbillm *oldconfig = NULL; 14611775Sbillm 1462789Sahrens if (!(spa_mode & FWRITE)) 1463789Sahrens return (EROFS); 1464789Sahrens 1465789Sahrens mutex_enter(&spa_namespace_lock); 1466789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1467789Sahrens mutex_exit(&spa_namespace_lock); 1468789Sahrens return (ENOENT); 1469789Sahrens } 1470789Sahrens 1471789Sahrens /* 14721544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 14731544Seschrock * reacquire the namespace lock, and see if we can export. 14741544Seschrock */ 14751544Seschrock spa_open_ref(spa, FTAG); 14761544Seschrock mutex_exit(&spa_namespace_lock); 14771544Seschrock spa_async_suspend(spa); 14781544Seschrock mutex_enter(&spa_namespace_lock); 14791544Seschrock spa_close(spa, FTAG); 14801544Seschrock 14811544Seschrock /* 1482789Sahrens * The pool will be in core if it's openable, 1483789Sahrens * in which case we can modify its state. 1484789Sahrens */ 1485789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1486789Sahrens /* 1487789Sahrens * Objsets may be open only because they're dirty, so we 1488789Sahrens * have to force it to sync before checking spa_refcnt. 1489789Sahrens */ 1490789Sahrens spa_scrub_suspend(spa); 1491789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1492789Sahrens 14931544Seschrock /* 14941544Seschrock * A pool cannot be exported or destroyed if there are active 14951544Seschrock * references. If we are resetting a pool, allow references by 14961544Seschrock * fault injection handlers. 14971544Seschrock */ 14981544Seschrock if (!spa_refcount_zero(spa) || 14991544Seschrock (spa->spa_inject_ref != 0 && 15001544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1501789Sahrens spa_scrub_resume(spa); 15021544Seschrock spa_async_resume(spa); 1503789Sahrens mutex_exit(&spa_namespace_lock); 1504789Sahrens return (EBUSY); 1505789Sahrens } 1506789Sahrens 1507789Sahrens spa_scrub_resume(spa); 1508789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1509789Sahrens 1510789Sahrens /* 1511789Sahrens * We want this to be reflected on every label, 1512789Sahrens * so mark them all dirty. spa_unload() will do the 1513789Sahrens * final sync that pushes these changes out. 1514789Sahrens */ 15151544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15161601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15171544Seschrock spa->spa_state = new_state; 15181635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 15191544Seschrock vdev_config_dirty(spa->spa_root_vdev); 15201601Sbonwick spa_config_exit(spa, FTAG); 15211544Seschrock } 1522789Sahrens } 1523789Sahrens 15244451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 15254451Seschrock 1526789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1527789Sahrens spa_unload(spa); 1528789Sahrens spa_deactivate(spa); 1529789Sahrens } 1530789Sahrens 15311775Sbillm if (oldconfig && spa->spa_config) 15321775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 15331775Sbillm 15341544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15351544Seschrock spa_remove(spa); 15361544Seschrock spa_config_sync(); 15371544Seschrock } 1538789Sahrens mutex_exit(&spa_namespace_lock); 1539789Sahrens 1540789Sahrens return (0); 1541789Sahrens } 1542789Sahrens 1543789Sahrens /* 1544789Sahrens * Destroy a storage pool. 1545789Sahrens */ 1546789Sahrens int 1547789Sahrens spa_destroy(char *pool) 1548789Sahrens { 15491775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1550789Sahrens } 1551789Sahrens 1552789Sahrens /* 1553789Sahrens * Export a storage pool. 1554789Sahrens */ 1555789Sahrens int 15561775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1557789Sahrens { 15581775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1559789Sahrens } 1560789Sahrens 1561789Sahrens /* 15621544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 15631544Seschrock * from the namespace in any way. 15641544Seschrock */ 15651544Seschrock int 15661544Seschrock spa_reset(char *pool) 15671544Seschrock { 15681775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 15691544Seschrock } 15701544Seschrock 15711544Seschrock 15721544Seschrock /* 1573789Sahrens * ========================================================================== 1574789Sahrens * Device manipulation 1575789Sahrens * ========================================================================== 1576789Sahrens */ 1577789Sahrens 1578789Sahrens /* 15794527Sperrin * Add a device to a storage pool. 1580789Sahrens */ 1581789Sahrens int 1582789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1583789Sahrens { 1584789Sahrens uint64_t txg; 15851635Sbonwick int c, error; 1586789Sahrens vdev_t *rvd = spa->spa_root_vdev; 15871585Sbonwick vdev_t *vd, *tvd; 15882082Seschrock nvlist_t **spares; 15892082Seschrock uint_t i, nspares; 1590789Sahrens 1591789Sahrens txg = spa_vdev_enter(spa); 1592789Sahrens 15932082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 15942082Seschrock VDEV_ALLOC_ADD)) != 0) 15952082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 15962082Seschrock 15973377Seschrock spa->spa_pending_vdev = vd; 1598789Sahrens 15992082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 16002082Seschrock &spares, &nspares) != 0) 16012082Seschrock nspares = 0; 16022082Seschrock 16033377Seschrock if (vd->vdev_children == 0 && nspares == 0) { 16043377Seschrock spa->spa_pending_vdev = NULL; 16052082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 16063377Seschrock } 16072082Seschrock 16082082Seschrock if (vd->vdev_children != 0) { 16093377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 16103377Seschrock spa->spa_pending_vdev = NULL; 16112082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16122082Seschrock } 16132082Seschrock } 16142082Seschrock 16153377Seschrock /* 16163377Seschrock * We must validate the spares after checking the children. Otherwise, 16173377Seschrock * vdev_inuse() will blindly overwrite the spare. 16183377Seschrock */ 16193377Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 16203377Seschrock VDEV_ALLOC_ADD)) != 0) { 16213377Seschrock spa->spa_pending_vdev = NULL; 16223377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16233377Seschrock } 16243377Seschrock 16253377Seschrock spa->spa_pending_vdev = NULL; 16263377Seschrock 16273377Seschrock /* 16283377Seschrock * Transfer each new top-level vdev from vd to rvd. 16293377Seschrock */ 16303377Seschrock for (c = 0; c < vd->vdev_children; c++) { 16313377Seschrock tvd = vd->vdev_child[c]; 16323377Seschrock vdev_remove_child(vd, tvd); 16333377Seschrock tvd->vdev_id = rvd->vdev_children; 16343377Seschrock vdev_add_child(rvd, tvd); 16353377Seschrock vdev_config_dirty(tvd); 16363377Seschrock } 16373377Seschrock 16382082Seschrock if (nspares != 0) { 16392082Seschrock if (spa->spa_sparelist != NULL) { 16402082Seschrock nvlist_t **oldspares; 16412082Seschrock uint_t oldnspares; 16422082Seschrock nvlist_t **newspares; 16432082Seschrock 16442082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 16452082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 16462082Seschrock 16472082Seschrock newspares = kmem_alloc(sizeof (void *) * 16482082Seschrock (nspares + oldnspares), KM_SLEEP); 16492082Seschrock for (i = 0; i < oldnspares; i++) 16502082Seschrock VERIFY(nvlist_dup(oldspares[i], 16512082Seschrock &newspares[i], KM_SLEEP) == 0); 16522082Seschrock for (i = 0; i < nspares; i++) 16532082Seschrock VERIFY(nvlist_dup(spares[i], 16542082Seschrock &newspares[i + oldnspares], 16552082Seschrock KM_SLEEP) == 0); 16562082Seschrock 16572082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 16582082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 16592082Seschrock 16602082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16612082Seschrock ZPOOL_CONFIG_SPARES, newspares, 16622082Seschrock nspares + oldnspares) == 0); 16632082Seschrock for (i = 0; i < oldnspares + nspares; i++) 16642082Seschrock nvlist_free(newspares[i]); 16652082Seschrock kmem_free(newspares, (oldnspares + nspares) * 16662082Seschrock sizeof (void *)); 16672082Seschrock } else { 16682082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 16692082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 16702082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16712082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16722082Seschrock } 16732082Seschrock 16742082Seschrock spa_load_spares(spa); 16752082Seschrock spa->spa_sync_spares = B_TRUE; 1676789Sahrens } 1677789Sahrens 1678789Sahrens /* 16791585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 16801585Sbonwick * If other threads start allocating from these vdevs before we 16811585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 16821585Sbonwick * fail to open the pool because there are DVAs that the config cache 16831585Sbonwick * can't translate. Therefore, we first add the vdevs without 16841585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 16851635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 16861585Sbonwick * 16871585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 16881585Sbonwick * if we lose power at any point in this sequence, the remaining 16891585Sbonwick * steps will be completed the next time we load the pool. 1690789Sahrens */ 16911635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 16921585Sbonwick 16931635Sbonwick mutex_enter(&spa_namespace_lock); 16941635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 16951635Sbonwick mutex_exit(&spa_namespace_lock); 1696789Sahrens 16971635Sbonwick return (0); 1698789Sahrens } 1699789Sahrens 1700789Sahrens /* 1701789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1702789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1703789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1704789Sahrens * 1705789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1706789Sahrens * existing device; in this case the two devices are made into their own 17074451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 1708789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1709789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1710789Sahrens * completion of resilvering, the first disk (the one being replaced) 1711789Sahrens * is automatically detached. 1712789Sahrens */ 1713789Sahrens int 17141544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1715789Sahrens { 1716789Sahrens uint64_t txg, open_txg; 1717789Sahrens int error; 1718789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1719789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 17202082Seschrock vdev_ops_t *pvops; 17214527Sperrin int is_log; 1722789Sahrens 1723789Sahrens txg = spa_vdev_enter(spa); 1724789Sahrens 17251544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1726789Sahrens 1727789Sahrens if (oldvd == NULL) 1728789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1729789Sahrens 17301585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 17311585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17321585Sbonwick 1733789Sahrens pvd = oldvd->vdev_parent; 1734789Sahrens 17352082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 17364451Seschrock VDEV_ALLOC_ADD)) != 0) 17374451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 17384451Seschrock 17394451Seschrock if (newrootvd->vdev_children != 1) 1740789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1741789Sahrens 1742789Sahrens newvd = newrootvd->vdev_child[0]; 1743789Sahrens 1744789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1745789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1746789Sahrens 17472082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1748789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1749789Sahrens 17504527Sperrin /* 17514527Sperrin * Spares can't replace logs 17524527Sperrin */ 17534527Sperrin is_log = oldvd->vdev_islog; 17544527Sperrin if (is_log && newvd->vdev_isspare) 17554527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17564527Sperrin 17572082Seschrock if (!replacing) { 17582082Seschrock /* 17592082Seschrock * For attach, the only allowable parent is a mirror or the root 17602082Seschrock * vdev. 17612082Seschrock */ 17622082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 17632082Seschrock pvd->vdev_ops != &vdev_root_ops) 17642082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17652082Seschrock 17662082Seschrock pvops = &vdev_mirror_ops; 17672082Seschrock } else { 17682082Seschrock /* 17692082Seschrock * Active hot spares can only be replaced by inactive hot 17702082Seschrock * spares. 17712082Seschrock */ 17722082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 17732082Seschrock pvd->vdev_child[1] == oldvd && 17742082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 17752082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17762082Seschrock 17772082Seschrock /* 17782082Seschrock * If the source is a hot spare, and the parent isn't already a 17792082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 17803377Seschrock * want to create a replacing vdev. The user is not allowed to 17813377Seschrock * attach to a spared vdev child unless the 'isspare' state is 17823377Seschrock * the same (spare replaces spare, non-spare replaces 17833377Seschrock * non-spare). 17842082Seschrock */ 17852082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 17862082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17873377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 17883377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 17893377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17902082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 17912082Seschrock newvd->vdev_isspare) 17922082Seschrock pvops = &vdev_spare_ops; 17932082Seschrock else 17942082Seschrock pvops = &vdev_replacing_ops; 17952082Seschrock } 17962082Seschrock 17971175Slling /* 17981175Slling * Compare the new device size with the replaceable/attachable 17991175Slling * device size. 18001175Slling */ 18011175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1802789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1803789Sahrens 18041732Sbonwick /* 18051732Sbonwick * The new device cannot have a higher alignment requirement 18061732Sbonwick * than the top-level vdev. 18071732Sbonwick */ 18081732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1809789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1810789Sahrens 1811789Sahrens /* 1812789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1813789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1814789Sahrens */ 1815789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1816789Sahrens spa_strfree(oldvd->vdev_path); 1817789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1818789Sahrens KM_SLEEP); 1819789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1820789Sahrens newvd->vdev_path, "old"); 1821789Sahrens if (oldvd->vdev_devid != NULL) { 1822789Sahrens spa_strfree(oldvd->vdev_devid); 1823789Sahrens oldvd->vdev_devid = NULL; 1824789Sahrens } 1825789Sahrens } 1826789Sahrens 1827789Sahrens /* 18282082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 18292082Seschrock * mirror/replacing/spare vdev above oldvd. 1830789Sahrens */ 1831789Sahrens if (pvd->vdev_ops != pvops) 1832789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1833789Sahrens 1834789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1835789Sahrens ASSERT(pvd->vdev_ops == pvops); 1836789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1837789Sahrens 1838789Sahrens /* 1839789Sahrens * Extract the new device from its root and add it to pvd. 1840789Sahrens */ 1841789Sahrens vdev_remove_child(newrootvd, newvd); 1842789Sahrens newvd->vdev_id = pvd->vdev_children; 1843789Sahrens vdev_add_child(pvd, newvd); 1844789Sahrens 18451544Seschrock /* 18461544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 18471544Seschrock * the addition of newvd may have decreased our parent's asize. 18481544Seschrock */ 18491544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 18501544Seschrock 1851789Sahrens tvd = newvd->vdev_top; 1852789Sahrens ASSERT(pvd->vdev_top == tvd); 1853789Sahrens ASSERT(tvd->vdev_parent == rvd); 1854789Sahrens 1855789Sahrens vdev_config_dirty(tvd); 1856789Sahrens 1857789Sahrens /* 1858789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1859789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1860789Sahrens */ 1861789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1862789Sahrens 1863789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1864789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1865789Sahrens open_txg - TXG_INITIAL + 1); 1866789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1867789Sahrens 18683377Seschrock if (newvd->vdev_isspare) 18693377Seschrock spa_spare_activate(newvd); 18701544Seschrock 1871789Sahrens /* 1872789Sahrens * Mark newvd's DTL dirty in this txg. 1873789Sahrens */ 18741732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1875789Sahrens 1876789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1877789Sahrens 1878789Sahrens /* 18794451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 18804451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 1881789Sahrens */ 18824451Seschrock mutex_enter(&spa_namespace_lock); 1883789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 18844451Seschrock mutex_exit(&spa_namespace_lock); 1885789Sahrens 1886789Sahrens return (0); 1887789Sahrens } 1888789Sahrens 1889789Sahrens /* 1890789Sahrens * Detach a device from a mirror or replacing vdev. 1891789Sahrens * If 'replace_done' is specified, only detach if the parent 1892789Sahrens * is a replacing vdev. 1893789Sahrens */ 1894789Sahrens int 18951544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1896789Sahrens { 1897789Sahrens uint64_t txg; 1898789Sahrens int c, t, error; 1899789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1900789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 19012082Seschrock boolean_t unspare = B_FALSE; 19022082Seschrock uint64_t unspare_guid; 1903789Sahrens 1904789Sahrens txg = spa_vdev_enter(spa); 1905789Sahrens 19061544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1907789Sahrens 1908789Sahrens if (vd == NULL) 1909789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1910789Sahrens 19111585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 19121585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19131585Sbonwick 1914789Sahrens pvd = vd->vdev_parent; 1915789Sahrens 1916789Sahrens /* 1917789Sahrens * If replace_done is specified, only remove this device if it's 19182082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 19192082Seschrock * disk can be removed. 1920789Sahrens */ 19212082Seschrock if (replace_done) { 19222082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 19232082Seschrock if (vd->vdev_id != 0) 19242082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19252082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 19262082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19272082Seschrock } 19282082Seschrock } 19292082Seschrock 19302082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 19312082Seschrock spa_version(spa) >= ZFS_VERSION_SPARES); 1932789Sahrens 1933789Sahrens /* 19342082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1935789Sahrens */ 1936789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 19372082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 19382082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1939789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1940789Sahrens 1941789Sahrens /* 1942789Sahrens * If there's only one replica, you can't detach it. 1943789Sahrens */ 1944789Sahrens if (pvd->vdev_children <= 1) 1945789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1946789Sahrens 1947789Sahrens /* 1948789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1949789Sahrens * valid copy of the data, which means we cannot safely detach it. 1950789Sahrens * 1951789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1952789Sahrens * precise DTL check. 1953789Sahrens */ 1954789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1955789Sahrens uint64_t dirty; 1956789Sahrens 1957789Sahrens cvd = pvd->vdev_child[c]; 1958789Sahrens if (cvd == vd) 1959789Sahrens continue; 1960789Sahrens if (vdev_is_dead(cvd)) 1961789Sahrens continue; 1962789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1963789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1964789Sahrens cvd->vdev_dtl_scrub.sm_space; 1965789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1966789Sahrens if (!dirty) 1967789Sahrens break; 1968789Sahrens } 19692082Seschrock 19702082Seschrock /* 19712082Seschrock * If we are a replacing or spare vdev, then we can always detach the 19722082Seschrock * latter child, as that is how one cancels the operation. 19732082Seschrock */ 19742082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 19752082Seschrock c == pvd->vdev_children) 1976789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1977789Sahrens 1978789Sahrens /* 19792082Seschrock * If we are detaching the original disk from a spare, then it implies 19802082Seschrock * that the spare should become a real disk, and be removed from the 19812082Seschrock * active spare list for the pool. 19822082Seschrock */ 19832082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 19842082Seschrock vd->vdev_id == 0) 19852082Seschrock unspare = B_TRUE; 19862082Seschrock 19872082Seschrock /* 1988789Sahrens * Erase the disk labels so the disk can be used for other things. 1989789Sahrens * This must be done after all other error cases are handled, 1990789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1991789Sahrens * But if we can't do it, don't treat the error as fatal -- 1992789Sahrens * it may be that the unwritability of the disk is the reason 1993789Sahrens * it's being detached! 1994789Sahrens */ 19953377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1996789Sahrens 1997789Sahrens /* 1998789Sahrens * Remove vd from its parent and compact the parent's children. 1999789Sahrens */ 2000789Sahrens vdev_remove_child(pvd, vd); 2001789Sahrens vdev_compact_children(pvd); 2002789Sahrens 2003789Sahrens /* 2004789Sahrens * Remember one of the remaining children so we can get tvd below. 2005789Sahrens */ 2006789Sahrens cvd = pvd->vdev_child[0]; 2007789Sahrens 2008789Sahrens /* 20092082Seschrock * If we need to remove the remaining child from the list of hot spares, 20102082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 20112082Seschrock * must do this before vdev_remove_parent(), because that can change the 20122082Seschrock * GUID if it creates a new toplevel GUID. 20132082Seschrock */ 20142082Seschrock if (unspare) { 20152082Seschrock ASSERT(cvd->vdev_isspare); 20163377Seschrock spa_spare_remove(cvd); 20172082Seschrock unspare_guid = cvd->vdev_guid; 20182082Seschrock } 20192082Seschrock 20202082Seschrock /* 2021789Sahrens * If the parent mirror/replacing vdev only has one child, 2022789Sahrens * the parent is no longer needed. Remove it from the tree. 2023789Sahrens */ 2024789Sahrens if (pvd->vdev_children == 1) 2025789Sahrens vdev_remove_parent(cvd); 2026789Sahrens 2027789Sahrens /* 2028789Sahrens * We don't set tvd until now because the parent we just removed 2029789Sahrens * may have been the previous top-level vdev. 2030789Sahrens */ 2031789Sahrens tvd = cvd->vdev_top; 2032789Sahrens ASSERT(tvd->vdev_parent == rvd); 2033789Sahrens 2034789Sahrens /* 20353377Seschrock * Reevaluate the parent vdev state. 2036789Sahrens */ 20374451Seschrock vdev_propagate_state(cvd); 2038789Sahrens 2039789Sahrens /* 20403377Seschrock * If the device we just detached was smaller than the others, it may be 20413377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 20423377Seschrock * can't fail because the existing metaslabs are already in core, so 20433377Seschrock * there's nothing to read from disk. 2044789Sahrens */ 20451732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2046789Sahrens 2047789Sahrens vdev_config_dirty(tvd); 2048789Sahrens 2049789Sahrens /* 20503377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 20513377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 20523377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 20533377Seschrock * prevent vd from being accessed after it's freed. 2054789Sahrens */ 2055789Sahrens for (t = 0; t < TXG_SIZE; t++) 2056789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 20571732Sbonwick vd->vdev_detached = B_TRUE; 20581732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2059789Sahrens 20604451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 20614451Seschrock 20622082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 20632082Seschrock 20642082Seschrock /* 20653377Seschrock * If this was the removal of the original device in a hot spare vdev, 20663377Seschrock * then we want to go through and remove the device from the hot spare 20673377Seschrock * list of every other pool. 20682082Seschrock */ 20692082Seschrock if (unspare) { 20702082Seschrock spa = NULL; 20712082Seschrock mutex_enter(&spa_namespace_lock); 20722082Seschrock while ((spa = spa_next(spa)) != NULL) { 20732082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 20742082Seschrock continue; 20752082Seschrock 20762082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 20772082Seschrock } 20782082Seschrock mutex_exit(&spa_namespace_lock); 20792082Seschrock } 20802082Seschrock 20812082Seschrock return (error); 20822082Seschrock } 20832082Seschrock 20842082Seschrock /* 20852082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 20862082Seschrock * spares. 20872082Seschrock */ 20882082Seschrock int 20892082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 20902082Seschrock { 20912082Seschrock vdev_t *vd; 20922082Seschrock nvlist_t **spares, *nv, **newspares; 20932082Seschrock uint_t i, j, nspares; 20942082Seschrock int ret = 0; 20952082Seschrock 20962082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20972082Seschrock 20982082Seschrock vd = spa_lookup_by_guid(spa, guid); 20992082Seschrock 21002082Seschrock nv = NULL; 21012082Seschrock if (spa->spa_spares != NULL && 21022082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21032082Seschrock &spares, &nspares) == 0) { 21042082Seschrock for (i = 0; i < nspares; i++) { 21052082Seschrock uint64_t theguid; 21062082Seschrock 21072082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 21082082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 21092082Seschrock if (theguid == guid) { 21102082Seschrock nv = spares[i]; 21112082Seschrock break; 21122082Seschrock } 21132082Seschrock } 21142082Seschrock } 21152082Seschrock 21162082Seschrock /* 21172082Seschrock * We only support removing a hot spare, and only if it's not currently 21182082Seschrock * in use in this pool. 21192082Seschrock */ 21202082Seschrock if (nv == NULL && vd == NULL) { 21212082Seschrock ret = ENOENT; 21222082Seschrock goto out; 21232082Seschrock } 21242082Seschrock 21252082Seschrock if (nv == NULL && vd != NULL) { 21262082Seschrock ret = ENOTSUP; 21272082Seschrock goto out; 21282082Seschrock } 21292082Seschrock 21302082Seschrock if (!unspare && nv != NULL && vd != NULL) { 21312082Seschrock ret = EBUSY; 21322082Seschrock goto out; 21332082Seschrock } 21342082Seschrock 21352082Seschrock if (nspares == 1) { 21362082Seschrock newspares = NULL; 21372082Seschrock } else { 21382082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 21392082Seschrock KM_SLEEP); 21402082Seschrock for (i = 0, j = 0; i < nspares; i++) { 21412082Seschrock if (spares[i] != nv) 21422082Seschrock VERIFY(nvlist_dup(spares[i], 21432082Seschrock &newspares[j++], KM_SLEEP) == 0); 21442082Seschrock } 21452082Seschrock } 21462082Seschrock 21472082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21482082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 21492082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21502082Seschrock newspares, nspares - 1) == 0); 21512082Seschrock for (i = 0; i < nspares - 1; i++) 21522082Seschrock nvlist_free(newspares[i]); 21532082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 21542082Seschrock spa_load_spares(spa); 21552082Seschrock spa->spa_sync_spares = B_TRUE; 21562082Seschrock 21572082Seschrock out: 21582082Seschrock spa_config_exit(spa, FTAG); 21592082Seschrock 21602082Seschrock return (ret); 2161789Sahrens } 2162789Sahrens 2163789Sahrens /* 21644451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 21654451Seschrock * current spared, so we can detach it. 2166789Sahrens */ 21671544Seschrock static vdev_t * 21684451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2169789Sahrens { 21701544Seschrock vdev_t *newvd, *oldvd; 2171789Sahrens int c; 2172789Sahrens 21731544Seschrock for (c = 0; c < vd->vdev_children; c++) { 21744451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 21751544Seschrock if (oldvd != NULL) 21761544Seschrock return (oldvd); 21771544Seschrock } 2178789Sahrens 21794451Seschrock /* 21804451Seschrock * Check for a completed replacement. 21814451Seschrock */ 2182789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 21831544Seschrock oldvd = vd->vdev_child[0]; 21841544Seschrock newvd = vd->vdev_child[1]; 2185789Sahrens 21861544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 21871544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 21881544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 21891544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21901544Seschrock return (oldvd); 21911544Seschrock } 21921544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21931544Seschrock } 2194789Sahrens 21954451Seschrock /* 21964451Seschrock * Check for a completed resilver with the 'unspare' flag set. 21974451Seschrock */ 21984451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 21994451Seschrock newvd = vd->vdev_child[0]; 22004451Seschrock oldvd = vd->vdev_child[1]; 22014451Seschrock 22024451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 22034451Seschrock if (newvd->vdev_unspare && 22044451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 22054451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 22064451Seschrock newvd->vdev_unspare = 0; 22074451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22084451Seschrock return (oldvd); 22094451Seschrock } 22104451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22114451Seschrock } 22124451Seschrock 22131544Seschrock return (NULL); 2214789Sahrens } 2215789Sahrens 22161544Seschrock static void 22174451Seschrock spa_vdev_resilver_done(spa_t *spa) 2218789Sahrens { 22191544Seschrock vdev_t *vd; 22202082Seschrock vdev_t *pvd; 22211544Seschrock uint64_t guid; 22222082Seschrock uint64_t pguid = 0; 2223789Sahrens 22241544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2225789Sahrens 22264451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 22271544Seschrock guid = vd->vdev_guid; 22282082Seschrock /* 22292082Seschrock * If we have just finished replacing a hot spared device, then 22302082Seschrock * we need to detach the parent's first child (the original hot 22312082Seschrock * spare) as well. 22322082Seschrock */ 22332082Seschrock pvd = vd->vdev_parent; 22342082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 22352082Seschrock pvd->vdev_id == 0) { 22362082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 22372082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 22382082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 22392082Seschrock } 22401544Seschrock spa_config_exit(spa, FTAG); 22411544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 22421544Seschrock return; 22432082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 22442082Seschrock return; 22451544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2246789Sahrens } 2247789Sahrens 22481544Seschrock spa_config_exit(spa, FTAG); 2249789Sahrens } 2250789Sahrens 2251789Sahrens /* 22521354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 22531354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 22541354Seschrock */ 22551354Seschrock int 22561354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 22571354Seschrock { 22581354Seschrock vdev_t *rvd, *vd; 22591354Seschrock uint64_t txg; 22601354Seschrock 22611354Seschrock rvd = spa->spa_root_vdev; 22621354Seschrock 22631354Seschrock txg = spa_vdev_enter(spa); 22641354Seschrock 22652082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 22662082Seschrock /* 22672082Seschrock * Determine if this is a reference to a hot spare. In that 22682082Seschrock * case, update the path as stored in the spare list. 22692082Seschrock */ 22702082Seschrock nvlist_t **spares; 22712082Seschrock uint_t i, nspares; 22722082Seschrock if (spa->spa_sparelist != NULL) { 22732082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 22742082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 22752082Seschrock for (i = 0; i < nspares; i++) { 22762082Seschrock uint64_t theguid; 22772082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 22782082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 22792082Seschrock if (theguid == guid) 22802082Seschrock break; 22812082Seschrock } 22822082Seschrock 22832082Seschrock if (i == nspares) 22842082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22852082Seschrock 22862082Seschrock VERIFY(nvlist_add_string(spares[i], 22872082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 22882082Seschrock spa_load_spares(spa); 22892082Seschrock spa->spa_sync_spares = B_TRUE; 22902082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 22912082Seschrock } else { 22922082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22932082Seschrock } 22942082Seschrock } 22951354Seschrock 22961585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 22971585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 22981585Sbonwick 22991354Seschrock spa_strfree(vd->vdev_path); 23001354Seschrock vd->vdev_path = spa_strdup(newpath); 23011354Seschrock 23021354Seschrock vdev_config_dirty(vd->vdev_top); 23031354Seschrock 23041354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 23051354Seschrock } 23061354Seschrock 23071354Seschrock /* 2308789Sahrens * ========================================================================== 2309789Sahrens * SPA Scrubbing 2310789Sahrens * ========================================================================== 2311789Sahrens */ 2312789Sahrens 2313789Sahrens static void 2314789Sahrens spa_scrub_io_done(zio_t *zio) 2315789Sahrens { 2316789Sahrens spa_t *spa = zio->io_spa; 2317789Sahrens 23184309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2319789Sahrens 2320789Sahrens mutex_enter(&spa->spa_scrub_lock); 23211544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 23221775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2323789Sahrens spa->spa_scrub_errors++; 2324789Sahrens mutex_enter(&vd->vdev_stat_lock); 2325789Sahrens vd->vdev_stat.vs_scrub_errors++; 2326789Sahrens mutex_exit(&vd->vdev_stat_lock); 2327789Sahrens } 23283697Smishra 23293697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 23301544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 23313697Smishra 23323697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 23333697Smishra 23341544Seschrock mutex_exit(&spa->spa_scrub_lock); 2335789Sahrens } 2336789Sahrens 2337789Sahrens static void 23381544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 23391544Seschrock zbookmark_t *zb) 2340789Sahrens { 2341789Sahrens size_t size = BP_GET_LSIZE(bp); 23423697Smishra void *data; 2343789Sahrens 2344789Sahrens mutex_enter(&spa->spa_scrub_lock); 23453697Smishra /* 23463697Smishra * Do not give too much work to vdev(s). 23473697Smishra */ 23483697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 23493697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 23503697Smishra } 2351789Sahrens spa->spa_scrub_inflight++; 2352789Sahrens mutex_exit(&spa->spa_scrub_lock); 2353789Sahrens 23544309Smaybee data = arc_data_buf_alloc(size); 23553697Smishra 23561544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 23571544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 23581544Seschrock 23591807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 23601544Seschrock 2361789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 23621544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2363789Sahrens } 2364789Sahrens 2365789Sahrens /* ARGSUSED */ 2366789Sahrens static int 2367789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2368789Sahrens { 2369789Sahrens blkptr_t *bp = &bc->bc_blkptr; 23701775Sbillm vdev_t *vd = spa->spa_root_vdev; 23711775Sbillm dva_t *dva = bp->blk_dva; 23721775Sbillm int needs_resilver = B_FALSE; 23731775Sbillm int d; 2374789Sahrens 23751775Sbillm if (bc->bc_errno) { 2376789Sahrens /* 2377789Sahrens * We can't scrub this block, but we can continue to scrub 2378789Sahrens * the rest of the pool. Note the error and move along. 2379789Sahrens */ 2380789Sahrens mutex_enter(&spa->spa_scrub_lock); 2381789Sahrens spa->spa_scrub_errors++; 2382789Sahrens mutex_exit(&spa->spa_scrub_lock); 2383789Sahrens 23841775Sbillm mutex_enter(&vd->vdev_stat_lock); 23851775Sbillm vd->vdev_stat.vs_scrub_errors++; 23861775Sbillm mutex_exit(&vd->vdev_stat_lock); 2387789Sahrens 2388789Sahrens return (ERESTART); 2389789Sahrens } 2390789Sahrens 2391789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2392789Sahrens 23931775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 23941775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 23951775Sbillm 23961775Sbillm ASSERT(vd != NULL); 23971775Sbillm 23981775Sbillm /* 23991775Sbillm * Keep track of how much data we've examined so that 24001775Sbillm * zpool(1M) status can make useful progress reports. 24011775Sbillm */ 24021775Sbillm mutex_enter(&vd->vdev_stat_lock); 24031775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 24041775Sbillm mutex_exit(&vd->vdev_stat_lock); 2405789Sahrens 24061775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 24071775Sbillm if (DVA_GET_GANG(&dva[d])) { 24081775Sbillm /* 24091775Sbillm * Gang members may be spread across multiple 24101775Sbillm * vdevs, so the best we can do is look at the 24111775Sbillm * pool-wide DTL. 24121775Sbillm * XXX -- it would be better to change our 24131775Sbillm * allocation policy to ensure that this can't 24141775Sbillm * happen. 24151775Sbillm */ 24161775Sbillm vd = spa->spa_root_vdev; 24171775Sbillm } 24181775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 24191775Sbillm bp->blk_birth, 1)) 24201775Sbillm needs_resilver = B_TRUE; 2421789Sahrens } 24221775Sbillm } 24231775Sbillm 24241775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2425789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 24261544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 24271775Sbillm else if (needs_resilver) 24281775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 24291775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2430789Sahrens 2431789Sahrens return (0); 2432789Sahrens } 2433789Sahrens 2434789Sahrens static void 2435789Sahrens spa_scrub_thread(spa_t *spa) 2436789Sahrens { 2437789Sahrens callb_cpr_t cprinfo; 2438789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2439789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2440789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2441789Sahrens int error = 0; 2442789Sahrens boolean_t complete; 2443789Sahrens 2444789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2445789Sahrens 2446797Sbonwick /* 2447797Sbonwick * If we're restarting due to a snapshot create/delete, 2448797Sbonwick * wait for that to complete. 2449797Sbonwick */ 2450797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2451797Sbonwick 24521544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 24531544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 24541544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 24551544Seschrock 24561544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 24571544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2458789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2459789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 24601544Seschrock spa_config_exit(spa, FTAG); 2461789Sahrens 2462789Sahrens mutex_enter(&spa->spa_scrub_lock); 2463789Sahrens spa->spa_scrub_errors = 0; 2464789Sahrens spa->spa_scrub_active = 1; 24651544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2466789Sahrens 2467789Sahrens while (!spa->spa_scrub_stop) { 2468789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 24691544Seschrock while (spa->spa_scrub_suspended) { 2470789Sahrens spa->spa_scrub_active = 0; 2471789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2472789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2473789Sahrens spa->spa_scrub_active = 1; 2474789Sahrens } 2475789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2476789Sahrens 2477789Sahrens if (spa->spa_scrub_restart_txg != 0) 2478789Sahrens break; 2479789Sahrens 2480789Sahrens mutex_exit(&spa->spa_scrub_lock); 2481789Sahrens error = traverse_more(th); 2482789Sahrens mutex_enter(&spa->spa_scrub_lock); 2483789Sahrens if (error != EAGAIN) 2484789Sahrens break; 2485789Sahrens } 2486789Sahrens 2487789Sahrens while (spa->spa_scrub_inflight) 2488789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2489789Sahrens 24901601Sbonwick spa->spa_scrub_active = 0; 24911601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 24921601Sbonwick 24931601Sbonwick mutex_exit(&spa->spa_scrub_lock); 24941601Sbonwick 24951601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24961601Sbonwick 24971601Sbonwick mutex_enter(&spa->spa_scrub_lock); 24981601Sbonwick 24991601Sbonwick /* 25001601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 25011601Sbonwick * AND the spa config lock to synchronize with any config changes 25021601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 25031601Sbonwick */ 2504789Sahrens if (spa->spa_scrub_restart_txg != 0) 2505789Sahrens error = ERESTART; 2506789Sahrens 25071544Seschrock if (spa->spa_scrub_stop) 25081544Seschrock error = EINTR; 25091544Seschrock 2510789Sahrens /* 25111544Seschrock * Even if there were uncorrectable errors, we consider the scrub 25121544Seschrock * completed. The downside is that if there is a transient error during 25131544Seschrock * a resilver, we won't resilver the data properly to the target. But 25141544Seschrock * if the damage is permanent (more likely) we will resilver forever, 25151544Seschrock * which isn't really acceptable. Since there is enough information for 25161544Seschrock * the user to know what has failed and why, this seems like a more 25171544Seschrock * tractable approach. 2518789Sahrens */ 25191544Seschrock complete = (error == 0); 2520789Sahrens 25211544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 25221544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2523789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2524789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2525789Sahrens 2526789Sahrens mutex_exit(&spa->spa_scrub_lock); 2527789Sahrens 2528789Sahrens /* 2529789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2530789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2531789Sahrens */ 2532789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2533789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2534789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 25351544Seschrock spa_errlog_rotate(spa); 25361601Sbonwick 25374451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 25384451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 25394451Seschrock 25401544Seschrock spa_config_exit(spa, FTAG); 2541789Sahrens 2542789Sahrens mutex_enter(&spa->spa_scrub_lock); 2543789Sahrens 25441544Seschrock /* 25451544Seschrock * We may have finished replacing a device. 25461544Seschrock * Let the async thread assess this and handle the detach. 25471544Seschrock */ 25484451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2549789Sahrens 2550789Sahrens /* 2551789Sahrens * If we were told to restart, our final act is to start a new scrub. 2552789Sahrens */ 2553789Sahrens if (error == ERESTART) 25541544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 25551544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2556789Sahrens 25571544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 25581544Seschrock spa->spa_scrub_active = 0; 25591544Seschrock spa->spa_scrub_thread = NULL; 25601544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2561789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2562789Sahrens thread_exit(); 2563789Sahrens } 2564789Sahrens 2565789Sahrens void 2566789Sahrens spa_scrub_suspend(spa_t *spa) 2567789Sahrens { 2568789Sahrens mutex_enter(&spa->spa_scrub_lock); 25691544Seschrock spa->spa_scrub_suspended++; 2570789Sahrens while (spa->spa_scrub_active) { 2571789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2572789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2573789Sahrens } 2574789Sahrens while (spa->spa_scrub_inflight) 2575789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2576789Sahrens mutex_exit(&spa->spa_scrub_lock); 2577789Sahrens } 2578789Sahrens 2579789Sahrens void 2580789Sahrens spa_scrub_resume(spa_t *spa) 2581789Sahrens { 2582789Sahrens mutex_enter(&spa->spa_scrub_lock); 25831544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 25841544Seschrock if (--spa->spa_scrub_suspended == 0) 2585789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2586789Sahrens mutex_exit(&spa->spa_scrub_lock); 2587789Sahrens } 2588789Sahrens 2589789Sahrens void 2590789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2591789Sahrens { 2592789Sahrens /* 2593789Sahrens * Something happened (e.g. snapshot create/delete) that means 2594789Sahrens * we must restart any in-progress scrubs. The itinerary will 2595789Sahrens * fix this properly. 2596789Sahrens */ 2597789Sahrens mutex_enter(&spa->spa_scrub_lock); 2598789Sahrens spa->spa_scrub_restart_txg = txg; 2599789Sahrens mutex_exit(&spa->spa_scrub_lock); 2600789Sahrens } 2601789Sahrens 26021544Seschrock int 26031544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2604789Sahrens { 2605789Sahrens space_seg_t *ss; 2606789Sahrens uint64_t mintxg, maxtxg; 2607789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2608789Sahrens 2609789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2610789Sahrens return (ENOTSUP); 2611789Sahrens 26121544Seschrock mutex_enter(&spa->spa_scrub_lock); 26131544Seschrock 2614789Sahrens /* 2615789Sahrens * If there's a scrub or resilver already in progress, stop it. 2616789Sahrens */ 2617789Sahrens while (spa->spa_scrub_thread != NULL) { 2618789Sahrens /* 2619789Sahrens * Don't stop a resilver unless forced. 2620789Sahrens */ 26211544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 26221544Seschrock mutex_exit(&spa->spa_scrub_lock); 2623789Sahrens return (EBUSY); 26241544Seschrock } 2625789Sahrens spa->spa_scrub_stop = 1; 2626789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2627789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2628789Sahrens } 2629789Sahrens 2630789Sahrens /* 2631789Sahrens * Terminate the previous traverse. 2632789Sahrens */ 2633789Sahrens if (spa->spa_scrub_th != NULL) { 2634789Sahrens traverse_fini(spa->spa_scrub_th); 2635789Sahrens spa->spa_scrub_th = NULL; 2636789Sahrens } 2637789Sahrens 26381544Seschrock if (rvd == NULL) { 26391544Seschrock ASSERT(spa->spa_scrub_stop == 0); 26401544Seschrock ASSERT(spa->spa_scrub_type == type); 26411544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 26421544Seschrock mutex_exit(&spa->spa_scrub_lock); 26431544Seschrock return (0); 26441544Seschrock } 2645789Sahrens 2646789Sahrens mintxg = TXG_INITIAL - 1; 2647789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2648789Sahrens 26491544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2650789Sahrens 26511544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 26521544Seschrock /* 26531544Seschrock * The pool-wide DTL is empty. 26541732Sbonwick * If this is a resilver, there's nothing to do except 26551732Sbonwick * check whether any in-progress replacements have completed. 26561544Seschrock */ 26571732Sbonwick if (type == POOL_SCRUB_RESILVER) { 26581544Seschrock type = POOL_SCRUB_NONE; 26594451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 26601732Sbonwick } 26611544Seschrock } else { 26621544Seschrock /* 26631544Seschrock * The pool-wide DTL is non-empty. 26641544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 26651544Seschrock */ 26661544Seschrock if (type == POOL_SCRUB_EVERYTHING) 26671544Seschrock type = POOL_SCRUB_RESILVER; 26681544Seschrock } 2669789Sahrens 26701544Seschrock if (type == POOL_SCRUB_RESILVER) { 2671789Sahrens /* 2672789Sahrens * Determine the resilvering boundaries. 2673789Sahrens * 2674789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2675789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2676789Sahrens * 2677789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2678789Sahrens * so we don't claim to resilver a txg that's still changing. 2679789Sahrens */ 2680789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 26811544Seschrock mintxg = ss->ss_start - 1; 2682789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 26831544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 26844451Seschrock 26854451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 2686789Sahrens } 2687789Sahrens 26881544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 26891544Seschrock 26901544Seschrock spa->spa_scrub_stop = 0; 26911544Seschrock spa->spa_scrub_type = type; 26921544Seschrock spa->spa_scrub_restart_txg = 0; 26931544Seschrock 26941544Seschrock if (type != POOL_SCRUB_NONE) { 26951544Seschrock spa->spa_scrub_mintxg = mintxg; 2696789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2697789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 26981635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 26991635Sbonwick ZIO_FLAG_CANFAIL); 2700789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2701789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2702789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2703789Sahrens } 2704789Sahrens 27051544Seschrock mutex_exit(&spa->spa_scrub_lock); 27061544Seschrock 2707789Sahrens return (0); 2708789Sahrens } 2709789Sahrens 27101544Seschrock /* 27111544Seschrock * ========================================================================== 27121544Seschrock * SPA async task processing 27131544Seschrock * ========================================================================== 27141544Seschrock */ 27151544Seschrock 27161544Seschrock static void 27174451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 2718789Sahrens { 27191544Seschrock vdev_t *tvd; 27201544Seschrock int c; 27211544Seschrock 27224451Seschrock for (c = 0; c < vd->vdev_children; c++) { 27234451Seschrock tvd = vd->vdev_child[c]; 27244451Seschrock if (tvd->vdev_remove_wanted) { 27254451Seschrock tvd->vdev_remove_wanted = 0; 27264451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 27274451Seschrock VDEV_AUX_NONE); 27284451Seschrock vdev_clear(spa, tvd); 27294451Seschrock vdev_config_dirty(tvd->vdev_top); 27301544Seschrock } 27314451Seschrock spa_async_remove(spa, tvd); 27321544Seschrock } 27331544Seschrock } 27341544Seschrock 27351544Seschrock static void 27361544Seschrock spa_async_thread(spa_t *spa) 27371544Seschrock { 27381544Seschrock int tasks; 27394451Seschrock uint64_t txg; 27401544Seschrock 27411544Seschrock ASSERT(spa->spa_sync_on); 2742789Sahrens 27431544Seschrock mutex_enter(&spa->spa_async_lock); 27441544Seschrock tasks = spa->spa_async_tasks; 27451544Seschrock spa->spa_async_tasks = 0; 27461544Seschrock mutex_exit(&spa->spa_async_lock); 27471544Seschrock 27481544Seschrock /* 27491635Sbonwick * See if the config needs to be updated. 27501635Sbonwick */ 27511635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 27521635Sbonwick mutex_enter(&spa_namespace_lock); 27531635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27541635Sbonwick mutex_exit(&spa_namespace_lock); 27551635Sbonwick } 27561635Sbonwick 27571635Sbonwick /* 27584451Seschrock * See if any devices need to be marked REMOVED. 27591544Seschrock */ 27604451Seschrock if (tasks & SPA_ASYNC_REMOVE) { 27614451Seschrock txg = spa_vdev_enter(spa); 27624451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 27634451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 27644451Seschrock } 27651544Seschrock 27661544Seschrock /* 27671544Seschrock * If any devices are done replacing, detach them. 27681544Seschrock */ 27694451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 27704451Seschrock spa_vdev_resilver_done(spa); 2771789Sahrens 27721544Seschrock /* 27734451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 27744451Seschrock * scrub which can become a resilver), we need to hold 27754451Seschrock * spa_namespace_lock() because the sysevent we post via 27764451Seschrock * spa_event_notify() needs to get the name of the pool. 27771544Seschrock */ 27784451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 27794451Seschrock mutex_enter(&spa_namespace_lock); 27801544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 27814451Seschrock mutex_exit(&spa_namespace_lock); 27824451Seschrock } 27831544Seschrock 27841544Seschrock /* 27851544Seschrock * Kick off a resilver. 27861544Seschrock */ 27874451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 27884451Seschrock mutex_enter(&spa_namespace_lock); 27891544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 27904451Seschrock mutex_exit(&spa_namespace_lock); 27914451Seschrock } 27921544Seschrock 27931544Seschrock /* 27941544Seschrock * Let the world know that we're done. 27951544Seschrock */ 27961544Seschrock mutex_enter(&spa->spa_async_lock); 27971544Seschrock spa->spa_async_thread = NULL; 27981544Seschrock cv_broadcast(&spa->spa_async_cv); 27991544Seschrock mutex_exit(&spa->spa_async_lock); 28001544Seschrock thread_exit(); 28011544Seschrock } 28021544Seschrock 28031544Seschrock void 28041544Seschrock spa_async_suspend(spa_t *spa) 28051544Seschrock { 28061544Seschrock mutex_enter(&spa->spa_async_lock); 28071544Seschrock spa->spa_async_suspended++; 28081544Seschrock while (spa->spa_async_thread != NULL) 28091544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 28101544Seschrock mutex_exit(&spa->spa_async_lock); 28111544Seschrock } 28121544Seschrock 28131544Seschrock void 28141544Seschrock spa_async_resume(spa_t *spa) 28151544Seschrock { 28161544Seschrock mutex_enter(&spa->spa_async_lock); 28171544Seschrock ASSERT(spa->spa_async_suspended != 0); 28181544Seschrock spa->spa_async_suspended--; 28191544Seschrock mutex_exit(&spa->spa_async_lock); 28201544Seschrock } 28211544Seschrock 28221544Seschrock static void 28231544Seschrock spa_async_dispatch(spa_t *spa) 28241544Seschrock { 28251544Seschrock mutex_enter(&spa->spa_async_lock); 28261544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 28271635Sbonwick spa->spa_async_thread == NULL && 28281635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 28291544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 28301544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 28311544Seschrock mutex_exit(&spa->spa_async_lock); 28321544Seschrock } 28331544Seschrock 28341544Seschrock void 28351544Seschrock spa_async_request(spa_t *spa, int task) 28361544Seschrock { 28371544Seschrock mutex_enter(&spa->spa_async_lock); 28381544Seschrock spa->spa_async_tasks |= task; 28391544Seschrock mutex_exit(&spa->spa_async_lock); 2840789Sahrens } 2841789Sahrens 2842789Sahrens /* 2843789Sahrens * ========================================================================== 2844789Sahrens * SPA syncing routines 2845789Sahrens * ========================================================================== 2846789Sahrens */ 2847789Sahrens 2848789Sahrens static void 2849789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2850789Sahrens { 2851789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2852789Sahrens dmu_tx_t *tx; 2853789Sahrens blkptr_t blk; 2854789Sahrens uint64_t itor = 0; 2855789Sahrens zio_t *zio; 2856789Sahrens int error; 2857789Sahrens uint8_t c = 1; 2858789Sahrens 2859789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2860789Sahrens 2861789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2862789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2863789Sahrens 2864789Sahrens error = zio_wait(zio); 2865789Sahrens ASSERT3U(error, ==, 0); 2866789Sahrens 2867789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2868789Sahrens bplist_vacate(bpl, tx); 2869789Sahrens 2870789Sahrens /* 2871789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2872789Sahrens * (Usually only the first block is needed.) 2873789Sahrens */ 2874789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2875789Sahrens dmu_tx_commit(tx); 2876789Sahrens } 2877789Sahrens 2878789Sahrens static void 28792082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 28802082Seschrock { 28812082Seschrock char *packed = NULL; 28822082Seschrock size_t nvsize = 0; 28832082Seschrock dmu_buf_t *db; 28842082Seschrock 28852082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 28862082Seschrock 28872082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 28882082Seschrock 28892082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 28902082Seschrock KM_SLEEP) == 0); 28912082Seschrock 28922082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 28932082Seschrock 28942082Seschrock kmem_free(packed, nvsize); 28952082Seschrock 28962082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 28972082Seschrock dmu_buf_will_dirty(db, tx); 28982082Seschrock *(uint64_t *)db->db_data = nvsize; 28992082Seschrock dmu_buf_rele(db, FTAG); 29002082Seschrock } 29012082Seschrock 29022082Seschrock static void 29032082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 29042082Seschrock { 29052082Seschrock nvlist_t *nvroot; 29062082Seschrock nvlist_t **spares; 29072082Seschrock int i; 29082082Seschrock 29092082Seschrock if (!spa->spa_sync_spares) 29102082Seschrock return; 29112082Seschrock 29122082Seschrock /* 29132082Seschrock * Update the MOS nvlist describing the list of available spares. 29142082Seschrock * spa_validate_spares() will have already made sure this nvlist is 29154451Seschrock * valid and the vdevs are labeled appropriately. 29162082Seschrock */ 29172082Seschrock if (spa->spa_spares_object == 0) { 29182082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 29192082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 29202082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 29212082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 29222082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 29232082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 29242082Seschrock } 29252082Seschrock 29262082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 29272082Seschrock if (spa->spa_nspares == 0) { 29282082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29292082Seschrock NULL, 0) == 0); 29302082Seschrock } else { 29312082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 29322082Seschrock KM_SLEEP); 29332082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29342082Seschrock spares[i] = vdev_config_generate(spa, 29352082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 29362082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29372082Seschrock spares, spa->spa_nspares) == 0); 29382082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29392082Seschrock nvlist_free(spares[i]); 29402082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 29412082Seschrock } 29422082Seschrock 29432082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 29442926Sek110237 nvlist_free(nvroot); 29452082Seschrock 29462082Seschrock spa->spa_sync_spares = B_FALSE; 29472082Seschrock } 29482082Seschrock 29492082Seschrock static void 2950789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2951789Sahrens { 2952789Sahrens nvlist_t *config; 2953789Sahrens 2954789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2955789Sahrens return; 2956789Sahrens 2957789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2958789Sahrens 29591635Sbonwick if (spa->spa_config_syncing) 29601635Sbonwick nvlist_free(spa->spa_config_syncing); 29611635Sbonwick spa->spa_config_syncing = config; 2962789Sahrens 29632082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2964789Sahrens } 2965789Sahrens 29663912Slling static void 2967*4543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 29683912Slling { 29693912Slling spa_t *spa = arg1; 29703912Slling nvlist_t *nvp = arg2; 29713912Slling nvpair_t *nvpair; 29723912Slling objset_t *mos = spa->spa_meta_objset; 29733912Slling uint64_t zapobj; 29744451Seschrock uint64_t intval; 29753912Slling 29763912Slling mutex_enter(&spa->spa_props_lock); 29773912Slling if (spa->spa_pool_props_object == 0) { 29783912Slling zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 29793912Slling VERIFY(zapobj > 0); 29803912Slling 29813912Slling spa->spa_pool_props_object = zapobj; 29823912Slling 29833912Slling VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 29843912Slling DMU_POOL_PROPS, 8, 1, 29853912Slling &spa->spa_pool_props_object, tx) == 0); 29863912Slling } 29873912Slling mutex_exit(&spa->spa_props_lock); 29883912Slling 29893912Slling nvpair = NULL; 29903912Slling while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 29913912Slling switch (zpool_name_to_prop(nvpair_name(nvpair))) { 2992*4543Smarks case ZPOOL_PROP_DELEGATION: 2993*4543Smarks VERIFY(nvlist_lookup_uint64(nvp, 2994*4543Smarks nvpair_name(nvpair), &intval) == 0); 2995*4543Smarks VERIFY(zap_update(mos, 2996*4543Smarks spa->spa_pool_props_object, 2997*4543Smarks nvpair_name(nvpair), 8, 1, 2998*4543Smarks &intval, tx) == 0); 2999*4543Smarks spa->spa_delegation = intval; 3000*4543Smarks break; 30014451Seschrock case ZPOOL_PROP_BOOTFS: 30023912Slling VERIFY(nvlist_lookup_uint64(nvp, 30033912Slling nvpair_name(nvpair), &spa->spa_bootfs) == 0); 3004*4543Smarks intval = spa->spa_bootfs; 30053912Slling VERIFY(zap_update(mos, 30063912Slling spa->spa_pool_props_object, 30074451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 8, 1, 3008*4543Smarks &intval, tx) == 0); 30093912Slling break; 30104451Seschrock 30114451Seschrock case ZPOOL_PROP_AUTOREPLACE: 30124451Seschrock VERIFY(nvlist_lookup_uint64(nvp, 30134451Seschrock nvpair_name(nvpair), &intval) == 0); 30144451Seschrock VERIFY(zap_update(mos, 30154451Seschrock spa->spa_pool_props_object, 30164451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 8, 1, 30174451Seschrock &intval, tx) == 0); 30184451Seschrock break; 30193912Slling } 3020*4543Smarks spa_history_internal_log(LOG_POOL_PROPSET, 3021*4543Smarks spa, tx, cr, "%s %lld %s", 3022*4543Smarks nvpair_name(nvpair), intval, 3023*4543Smarks spa->spa_name); 30243912Slling } 30253912Slling } 30263912Slling 3027789Sahrens /* 3028789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3029789Sahrens * part of the process, so we iterate until it converges. 3030789Sahrens */ 3031789Sahrens void 3032789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3033789Sahrens { 3034789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3035789Sahrens objset_t *mos = spa->spa_meta_objset; 3036789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 30371635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3038789Sahrens vdev_t *vd; 3039789Sahrens dmu_tx_t *tx; 3040789Sahrens int dirty_vdevs; 3041789Sahrens 3042789Sahrens /* 3043789Sahrens * Lock out configuration changes. 3044789Sahrens */ 30451544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3046789Sahrens 3047789Sahrens spa->spa_syncing_txg = txg; 3048789Sahrens spa->spa_sync_pass = 0; 3049789Sahrens 30501544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3051789Sahrens 30522082Seschrock tx = dmu_tx_create_assigned(dp, txg); 30532082Seschrock 30542082Seschrock /* 30552082Seschrock * If we are upgrading to ZFS_VERSION_RAIDZ_DEFLATE this txg, 30562082Seschrock * set spa_deflate if we have no raid-z vdevs. 30572082Seschrock */ 30582082Seschrock if (spa->spa_ubsync.ub_version < ZFS_VERSION_RAIDZ_DEFLATE && 30592082Seschrock spa->spa_uberblock.ub_version >= ZFS_VERSION_RAIDZ_DEFLATE) { 30602082Seschrock int i; 30612082Seschrock 30622082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 30632082Seschrock vd = rvd->vdev_child[i]; 30642082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 30652082Seschrock break; 30662082Seschrock } 30672082Seschrock if (i == rvd->vdev_children) { 30682082Seschrock spa->spa_deflate = TRUE; 30692082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 30702082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 30712082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 30722082Seschrock } 30732082Seschrock } 30742082Seschrock 3075789Sahrens /* 3076789Sahrens * If anything has changed in this txg, push the deferred frees 3077789Sahrens * from the previous txg. If not, leave them alone so that we 3078789Sahrens * don't generate work on an otherwise idle system. 3079789Sahrens */ 3080789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 30812329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 30822329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3083789Sahrens spa_sync_deferred_frees(spa, txg); 3084789Sahrens 3085789Sahrens /* 3086789Sahrens * Iterate to convergence. 3087789Sahrens */ 3088789Sahrens do { 3089789Sahrens spa->spa_sync_pass++; 3090789Sahrens 3091789Sahrens spa_sync_config_object(spa, tx); 30922082Seschrock spa_sync_spares(spa, tx); 30931544Seschrock spa_errlog_sync(spa, txg); 3094789Sahrens dsl_pool_sync(dp, txg); 3095789Sahrens 3096789Sahrens dirty_vdevs = 0; 3097789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3098789Sahrens vdev_sync(vd, txg); 3099789Sahrens dirty_vdevs++; 3100789Sahrens } 3101789Sahrens 3102789Sahrens bplist_sync(bpl, tx); 3103789Sahrens } while (dirty_vdevs); 3104789Sahrens 3105789Sahrens bplist_close(bpl); 3106789Sahrens 3107789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3108789Sahrens 3109789Sahrens /* 3110789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3111789Sahrens * to commit the transaction group. 31121635Sbonwick * 31131635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 31141635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 31151635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 31161635Sbonwick * If the write fails, try the next vdev until we're tried them all. 3117789Sahrens */ 31181635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 31191635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31201635Sbonwick } else { 31211635Sbonwick int children = rvd->vdev_children; 31221635Sbonwick int c0 = spa_get_random(children); 31231635Sbonwick int c; 31241635Sbonwick 31251635Sbonwick for (c = 0; c < children; c++) { 31261635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 31271635Sbonwick if (vd->vdev_ms_array == 0) 31281635Sbonwick continue; 31291635Sbonwick if (vdev_config_sync(vd, txg) == 0) 31301635Sbonwick break; 31311635Sbonwick } 31321635Sbonwick if (c == children) 31331635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31341635Sbonwick } 31351635Sbonwick 31362082Seschrock dmu_tx_commit(tx); 31372082Seschrock 31381635Sbonwick /* 31391635Sbonwick * Clear the dirty config list. 31401635Sbonwick */ 31411635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 31421635Sbonwick vdev_config_clean(vd); 31431635Sbonwick 31441635Sbonwick /* 31451635Sbonwick * Now that the new config has synced transactionally, 31461635Sbonwick * let it become visible to the config cache. 31471635Sbonwick */ 31481635Sbonwick if (spa->spa_config_syncing != NULL) { 31491635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 31501635Sbonwick spa->spa_config_txg = txg; 31511635Sbonwick spa->spa_config_syncing = NULL; 31521635Sbonwick } 3153789Sahrens 3154789Sahrens /* 3155789Sahrens * Make a stable copy of the fully synced uberblock. 3156789Sahrens * We use this as the root for pool traversals. 3157789Sahrens */ 3158789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3159789Sahrens 3160789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3161789Sahrens 3162789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3163789Sahrens spa->spa_traverse_wanted = 0; 3164789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3165789Sahrens rw_exit(&spa->spa_traverse_lock); 3166789Sahrens 3167789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3168789Sahrens 3169789Sahrens /* 3170789Sahrens * Clean up the ZIL records for the synced txg. 3171789Sahrens */ 3172789Sahrens dsl_pool_zil_clean(dp); 3173789Sahrens 3174789Sahrens /* 3175789Sahrens * Update usable space statistics. 3176789Sahrens */ 3177789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3178789Sahrens vdev_sync_done(vd, txg); 3179789Sahrens 3180789Sahrens /* 3181789Sahrens * It had better be the case that we didn't dirty anything 31822082Seschrock * since vdev_config_sync(). 3183789Sahrens */ 3184789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3185789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3186789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3187789Sahrens ASSERT(bpl->bpl_queue == NULL); 3188789Sahrens 31891544Seschrock spa_config_exit(spa, FTAG); 31901544Seschrock 31911544Seschrock /* 31921544Seschrock * If any async tasks have been requested, kick them off. 31931544Seschrock */ 31941544Seschrock spa_async_dispatch(spa); 3195789Sahrens } 3196789Sahrens 3197789Sahrens /* 3198789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3199789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3200789Sahrens * sync. 3201789Sahrens */ 3202789Sahrens void 3203789Sahrens spa_sync_allpools(void) 3204789Sahrens { 3205789Sahrens spa_t *spa = NULL; 3206789Sahrens mutex_enter(&spa_namespace_lock); 3207789Sahrens while ((spa = spa_next(spa)) != NULL) { 3208789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3209789Sahrens continue; 3210789Sahrens spa_open_ref(spa, FTAG); 3211789Sahrens mutex_exit(&spa_namespace_lock); 3212789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3213789Sahrens mutex_enter(&spa_namespace_lock); 3214789Sahrens spa_close(spa, FTAG); 3215789Sahrens } 3216789Sahrens mutex_exit(&spa_namespace_lock); 3217789Sahrens } 3218789Sahrens 3219789Sahrens /* 3220789Sahrens * ========================================================================== 3221789Sahrens * Miscellaneous routines 3222789Sahrens * ========================================================================== 3223789Sahrens */ 3224789Sahrens 3225789Sahrens /* 3226789Sahrens * Remove all pools in the system. 3227789Sahrens */ 3228789Sahrens void 3229789Sahrens spa_evict_all(void) 3230789Sahrens { 3231789Sahrens spa_t *spa; 3232789Sahrens 3233789Sahrens /* 3234789Sahrens * Remove all cached state. All pools should be closed now, 3235789Sahrens * so every spa in the AVL tree should be unreferenced. 3236789Sahrens */ 3237789Sahrens mutex_enter(&spa_namespace_lock); 3238789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3239789Sahrens /* 32401544Seschrock * Stop async tasks. The async thread may need to detach 32411544Seschrock * a device that's been replaced, which requires grabbing 32421544Seschrock * spa_namespace_lock, so we must drop it here. 3243789Sahrens */ 3244789Sahrens spa_open_ref(spa, FTAG); 3245789Sahrens mutex_exit(&spa_namespace_lock); 32461544Seschrock spa_async_suspend(spa); 3247789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3248789Sahrens mutex_enter(&spa_namespace_lock); 3249789Sahrens spa_close(spa, FTAG); 3250789Sahrens 3251789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3252789Sahrens spa_unload(spa); 3253789Sahrens spa_deactivate(spa); 3254789Sahrens } 3255789Sahrens spa_remove(spa); 3256789Sahrens } 3257789Sahrens mutex_exit(&spa_namespace_lock); 3258789Sahrens } 32591544Seschrock 32601544Seschrock vdev_t * 32611544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 32621544Seschrock { 32631544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 32641544Seschrock } 32651760Seschrock 32661760Seschrock void 32671760Seschrock spa_upgrade(spa_t *spa) 32681760Seschrock { 32691760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 32701760Seschrock 32711760Seschrock /* 32721760Seschrock * This should only be called for a non-faulted pool, and since a 32731760Seschrock * future version would result in an unopenable pool, this shouldn't be 32741760Seschrock * possible. 32751760Seschrock */ 32761760Seschrock ASSERT(spa->spa_uberblock.ub_version <= ZFS_VERSION); 32771760Seschrock 32781760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 32791760Seschrock vdev_config_dirty(spa->spa_root_vdev); 32801760Seschrock 32811760Seschrock spa_config_exit(spa, FTAG); 32822082Seschrock 32832082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 32841760Seschrock } 32852082Seschrock 32862082Seschrock boolean_t 32872082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 32882082Seschrock { 32892082Seschrock int i; 32903377Seschrock uint64_t spareguid; 32912082Seschrock 32922082Seschrock for (i = 0; i < spa->spa_nspares; i++) 32932082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 32942082Seschrock return (B_TRUE); 32952082Seschrock 32963377Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 32973377Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 32983377Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 32993377Seschrock spareguid == guid) 33003377Seschrock return (B_TRUE); 33013377Seschrock } 33023377Seschrock 33032082Seschrock return (B_FALSE); 33042082Seschrock } 33053912Slling 33063912Slling int 33073912Slling spa_set_props(spa_t *spa, nvlist_t *nvp) 33083912Slling { 33093912Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 33103912Slling spa, nvp, 3)); 33113912Slling } 33123912Slling 33133912Slling int 33143912Slling spa_get_props(spa_t *spa, nvlist_t **nvp) 33153912Slling { 33163912Slling zap_cursor_t zc; 33173912Slling zap_attribute_t za; 33183912Slling objset_t *mos = spa->spa_meta_objset; 33193912Slling zfs_source_t src; 33204451Seschrock zpool_prop_t prop; 33213912Slling nvlist_t *propval; 33223912Slling uint64_t value; 33233912Slling int err; 33243912Slling 33253912Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33263912Slling 33273912Slling mutex_enter(&spa->spa_props_lock); 33283912Slling /* If no props object, then just return empty nvlist */ 33293912Slling if (spa->spa_pool_props_object == 0) { 33303912Slling mutex_exit(&spa->spa_props_lock); 33313912Slling return (0); 33323912Slling } 33333912Slling 33343912Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 33353912Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 33363912Slling zap_cursor_advance(&zc)) { 33373912Slling 33383912Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 33393912Slling continue; 33403912Slling 33413912Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33423912Slling switch (za.za_integer_length) { 33433912Slling case 8: 33444451Seschrock if (zpool_prop_default_numeric(prop) == 33453912Slling za.za_first_integer) 33463912Slling src = ZFS_SRC_DEFAULT; 33473912Slling else 33483912Slling src = ZFS_SRC_LOCAL; 33493912Slling value = za.za_first_integer; 33503912Slling 33514451Seschrock if (prop == ZPOOL_PROP_BOOTFS) { 33523912Slling dsl_pool_t *dp; 33533912Slling dsl_dataset_t *ds = NULL; 33543912Slling char strval[MAXPATHLEN]; 33553912Slling 33563912Slling dp = spa_get_dsl(spa); 33573912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 33583912Slling if ((err = dsl_dataset_open_obj(dp, 33593912Slling za.za_first_integer, NULL, DS_MODE_NONE, 33603912Slling FTAG, &ds)) != 0) { 33613912Slling rw_exit(&dp->dp_config_rwlock); 33623912Slling break; 33633912Slling } 33643912Slling dsl_dataset_name(ds, strval); 33653912Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 33663912Slling rw_exit(&dp->dp_config_rwlock); 33673912Slling 33683912Slling VERIFY(nvlist_add_uint64(propval, 33693912Slling ZFS_PROP_SOURCE, src) == 0); 33703912Slling VERIFY(nvlist_add_string(propval, 33713912Slling ZFS_PROP_VALUE, strval) == 0); 33723912Slling } else { 33733912Slling VERIFY(nvlist_add_uint64(propval, 33743912Slling ZFS_PROP_SOURCE, src) == 0); 33753912Slling VERIFY(nvlist_add_uint64(propval, 33763912Slling ZFS_PROP_VALUE, value) == 0); 33773912Slling } 33783912Slling VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 33793912Slling propval) == 0); 33803912Slling break; 33813912Slling } 33823912Slling nvlist_free(propval); 33833912Slling } 33843912Slling zap_cursor_fini(&zc); 33853912Slling mutex_exit(&spa->spa_props_lock); 33863912Slling if (err && err != ENOENT) { 33873912Slling nvlist_free(*nvp); 33883912Slling return (err); 33893912Slling } 33903912Slling 33913912Slling return (0); 33923912Slling } 33933912Slling 33943912Slling /* 33953912Slling * If the bootfs property value is dsobj, clear it. 33963912Slling */ 33973912Slling void 33983912Slling spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 33993912Slling { 34003912Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 34013912Slling VERIFY(zap_remove(spa->spa_meta_objset, 34023912Slling spa->spa_pool_props_object, 34034451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 34043912Slling spa->spa_bootfs = 0; 34053912Slling } 34063912Slling } 34074451Seschrock 34084451Seschrock /* 34094451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 34104451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 34114451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 34124451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 34134451Seschrock * or zdb as real changes. 34144451Seschrock */ 34154451Seschrock void 34164451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 34174451Seschrock { 34184451Seschrock #ifdef _KERNEL 34194451Seschrock sysevent_t *ev; 34204451Seschrock sysevent_attr_list_t *attr = NULL; 34214451Seschrock sysevent_value_t value; 34224451Seschrock sysevent_id_t eid; 34234451Seschrock 34244451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 34254451Seschrock SE_SLEEP); 34264451Seschrock 34274451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34284451Seschrock value.value.sv_string = spa_name(spa); 34294451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 34304451Seschrock goto done; 34314451Seschrock 34324451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34334451Seschrock value.value.sv_uint64 = spa_guid(spa); 34344451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 34354451Seschrock goto done; 34364451Seschrock 34374451Seschrock if (vd) { 34384451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34394451Seschrock value.value.sv_uint64 = vd->vdev_guid; 34404451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 34414451Seschrock SE_SLEEP) != 0) 34424451Seschrock goto done; 34434451Seschrock 34444451Seschrock if (vd->vdev_path) { 34454451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34464451Seschrock value.value.sv_string = vd->vdev_path; 34474451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 34484451Seschrock &value, SE_SLEEP) != 0) 34494451Seschrock goto done; 34504451Seschrock } 34514451Seschrock } 34524451Seschrock 34534451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 34544451Seschrock 34554451Seschrock done: 34564451Seschrock if (attr) 34574451Seschrock sysevent_free_attr(attr); 34584451Seschrock sysevent_free(ev); 34594451Seschrock #endif 34604451Seschrock } 3461