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) 4784577Sahrens version = SPA_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 */ 5544577Sahrens if (ub->ub_version > SPA_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) { 7104577Sahrens ASSERT(spa_version(spa) >= SPA_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 7244543Smarks spa->spa_delegation = zfs_prop_default_numeric(ZPOOL_PROP_DELEGATION); 7254543Smarks 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); 7454543Smarks (void) zap_lookup(spa->spa_meta_objset, 7464543Smarks spa->spa_pool_props_object, 7474543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 7484543Smarks 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 */ 10924577Sahrens if (spa_version(spa) < SPA_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; 11674577Sahrens spa->spa_uberblock.ub_version = SPA_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); 12674543Smarks 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 /* 1298789Sahrens * If a pool with this name exists, return failure. 1299789Sahrens */ 1300789Sahrens mutex_enter(&spa_namespace_lock); 1301789Sahrens if (spa_lookup(pool) != NULL) { 1302789Sahrens mutex_exit(&spa_namespace_lock); 1303789Sahrens return (EEXIST); 1304789Sahrens } 1305789Sahrens 1306789Sahrens /* 13071635Sbonwick * Create and initialize the spa structure. 1308789Sahrens */ 13091635Sbonwick spa = spa_add(pool, altroot); 1310789Sahrens spa_activate(spa); 1311789Sahrens 1312789Sahrens /* 13131635Sbonwick * Pass off the heavy lifting to spa_load(). 13141732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 13151732Sbonwick * is actually the one to trust when doing an import. 13161601Sbonwick */ 13171732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1318789Sahrens 13192082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13202082Seschrock /* 13212082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 13222082Seschrock * and conflicts with spa_has_spare(). 13232082Seschrock */ 13242082Seschrock if (spa->spa_sparelist) { 13252082Seschrock nvlist_free(spa->spa_sparelist); 13262082Seschrock spa->spa_sparelist = NULL; 13272082Seschrock spa_load_spares(spa); 13282082Seschrock } 13292082Seschrock 13302082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 13312082Seschrock &nvroot) == 0); 13322082Seschrock if (error == 0) 13332082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 13342082Seschrock VDEV_ALLOC_SPARE); 13352082Seschrock spa_config_exit(spa, FTAG); 13362082Seschrock 13372082Seschrock if (error != 0) { 1338789Sahrens spa_unload(spa); 1339789Sahrens spa_deactivate(spa); 1340789Sahrens spa_remove(spa); 1341789Sahrens mutex_exit(&spa_namespace_lock); 1342789Sahrens return (error); 1343789Sahrens } 1344789Sahrens 13451635Sbonwick /* 13462082Seschrock * Override any spares as specified by the user, as these may have 13472082Seschrock * correct device names/devids, etc. 13482082Seschrock */ 13492082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 13502082Seschrock &spares, &nspares) == 0) { 13512082Seschrock if (spa->spa_sparelist) 13522082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 13532082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 13542082Seschrock else 13552082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 13562082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 13572082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 13582082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 13592082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13602082Seschrock spa_load_spares(spa); 13612082Seschrock spa_config_exit(spa, FTAG); 13622082Seschrock spa->spa_sync_spares = B_TRUE; 13632082Seschrock } 13642082Seschrock 13652082Seschrock /* 13661635Sbonwick * Update the config cache to include the newly-imported pool. 13671635Sbonwick */ 1368*4627Sck153898 if (spa_mode & FWRITE) 1369*4627Sck153898 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 13701635Sbonwick 1371789Sahrens /* 1372789Sahrens * Resilver anything that's out of date. 1373789Sahrens */ 1374789Sahrens if (spa_mode & FWRITE) 1375789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1376789Sahrens 13774451Seschrock mutex_exit(&spa_namespace_lock); 13784451Seschrock 1379789Sahrens return (0); 1380789Sahrens } 1381789Sahrens 1382789Sahrens /* 1383789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1384789Sahrens * to get the vdev stats associated with the imported devices. 1385789Sahrens */ 1386789Sahrens #define TRYIMPORT_NAME "$import" 1387789Sahrens 1388789Sahrens nvlist_t * 1389789Sahrens spa_tryimport(nvlist_t *tryconfig) 1390789Sahrens { 1391789Sahrens nvlist_t *config = NULL; 1392789Sahrens char *poolname; 1393789Sahrens spa_t *spa; 1394789Sahrens uint64_t state; 1395789Sahrens 1396789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1397789Sahrens return (NULL); 1398789Sahrens 1399789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1400789Sahrens return (NULL); 1401789Sahrens 14021635Sbonwick /* 14031635Sbonwick * Create and initialize the spa structure. 14041635Sbonwick */ 1405789Sahrens mutex_enter(&spa_namespace_lock); 14061635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1407789Sahrens spa_activate(spa); 1408789Sahrens 1409789Sahrens /* 14101635Sbonwick * Pass off the heavy lifting to spa_load(). 14111732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 14121732Sbonwick * is actually the one to trust when doing an import. 1413789Sahrens */ 14141732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1415789Sahrens 1416789Sahrens /* 1417789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1418789Sahrens */ 1419789Sahrens if (spa->spa_root_vdev != NULL) { 14201635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1421789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14221635Sbonwick spa_config_exit(spa, FTAG); 1423789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1424789Sahrens poolname) == 0); 1425789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1426789Sahrens state) == 0); 14273975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 14283975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 14292082Seschrock 14302082Seschrock /* 14312082Seschrock * Add the list of hot spares. 14322082Seschrock */ 14332082Seschrock spa_add_spares(spa, config); 1434789Sahrens } 1435789Sahrens 1436789Sahrens spa_unload(spa); 1437789Sahrens spa_deactivate(spa); 1438789Sahrens spa_remove(spa); 1439789Sahrens mutex_exit(&spa_namespace_lock); 1440789Sahrens 1441789Sahrens return (config); 1442789Sahrens } 1443789Sahrens 1444789Sahrens /* 1445789Sahrens * Pool export/destroy 1446789Sahrens * 1447789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1448789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1449789Sahrens * update the pool state and sync all the labels to disk, removing the 1450789Sahrens * configuration from the cache afterwards. 1451789Sahrens */ 1452789Sahrens static int 14531775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1454789Sahrens { 1455789Sahrens spa_t *spa; 1456789Sahrens 14571775Sbillm if (oldconfig) 14581775Sbillm *oldconfig = NULL; 14591775Sbillm 1460789Sahrens if (!(spa_mode & FWRITE)) 1461789Sahrens return (EROFS); 1462789Sahrens 1463789Sahrens mutex_enter(&spa_namespace_lock); 1464789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1465789Sahrens mutex_exit(&spa_namespace_lock); 1466789Sahrens return (ENOENT); 1467789Sahrens } 1468789Sahrens 1469789Sahrens /* 14701544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 14711544Seschrock * reacquire the namespace lock, and see if we can export. 14721544Seschrock */ 14731544Seschrock spa_open_ref(spa, FTAG); 14741544Seschrock mutex_exit(&spa_namespace_lock); 14751544Seschrock spa_async_suspend(spa); 14761544Seschrock mutex_enter(&spa_namespace_lock); 14771544Seschrock spa_close(spa, FTAG); 14781544Seschrock 14791544Seschrock /* 1480789Sahrens * The pool will be in core if it's openable, 1481789Sahrens * in which case we can modify its state. 1482789Sahrens */ 1483789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1484789Sahrens /* 1485789Sahrens * Objsets may be open only because they're dirty, so we 1486789Sahrens * have to force it to sync before checking spa_refcnt. 1487789Sahrens */ 1488789Sahrens spa_scrub_suspend(spa); 1489789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1490789Sahrens 14911544Seschrock /* 14921544Seschrock * A pool cannot be exported or destroyed if there are active 14931544Seschrock * references. If we are resetting a pool, allow references by 14941544Seschrock * fault injection handlers. 14951544Seschrock */ 14961544Seschrock if (!spa_refcount_zero(spa) || 14971544Seschrock (spa->spa_inject_ref != 0 && 14981544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1499789Sahrens spa_scrub_resume(spa); 15001544Seschrock spa_async_resume(spa); 1501789Sahrens mutex_exit(&spa_namespace_lock); 1502789Sahrens return (EBUSY); 1503789Sahrens } 1504789Sahrens 1505789Sahrens spa_scrub_resume(spa); 1506789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1507789Sahrens 1508789Sahrens /* 1509789Sahrens * We want this to be reflected on every label, 1510789Sahrens * so mark them all dirty. spa_unload() will do the 1511789Sahrens * final sync that pushes these changes out. 1512789Sahrens */ 15131544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15141601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15151544Seschrock spa->spa_state = new_state; 15161635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 15171544Seschrock vdev_config_dirty(spa->spa_root_vdev); 15181601Sbonwick spa_config_exit(spa, FTAG); 15191544Seschrock } 1520789Sahrens } 1521789Sahrens 15224451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 15234451Seschrock 1524789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1525789Sahrens spa_unload(spa); 1526789Sahrens spa_deactivate(spa); 1527789Sahrens } 1528789Sahrens 15291775Sbillm if (oldconfig && spa->spa_config) 15301775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 15311775Sbillm 15321544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15331544Seschrock spa_remove(spa); 15341544Seschrock spa_config_sync(); 15351544Seschrock } 1536789Sahrens mutex_exit(&spa_namespace_lock); 1537789Sahrens 1538789Sahrens return (0); 1539789Sahrens } 1540789Sahrens 1541789Sahrens /* 1542789Sahrens * Destroy a storage pool. 1543789Sahrens */ 1544789Sahrens int 1545789Sahrens spa_destroy(char *pool) 1546789Sahrens { 15471775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1548789Sahrens } 1549789Sahrens 1550789Sahrens /* 1551789Sahrens * Export a storage pool. 1552789Sahrens */ 1553789Sahrens int 15541775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1555789Sahrens { 15561775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1557789Sahrens } 1558789Sahrens 1559789Sahrens /* 15601544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 15611544Seschrock * from the namespace in any way. 15621544Seschrock */ 15631544Seschrock int 15641544Seschrock spa_reset(char *pool) 15651544Seschrock { 15661775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 15671544Seschrock } 15681544Seschrock 15691544Seschrock 15701544Seschrock /* 1571789Sahrens * ========================================================================== 1572789Sahrens * Device manipulation 1573789Sahrens * ========================================================================== 1574789Sahrens */ 1575789Sahrens 1576789Sahrens /* 15774527Sperrin * Add a device to a storage pool. 1578789Sahrens */ 1579789Sahrens int 1580789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1581789Sahrens { 1582789Sahrens uint64_t txg; 15831635Sbonwick int c, error; 1584789Sahrens vdev_t *rvd = spa->spa_root_vdev; 15851585Sbonwick vdev_t *vd, *tvd; 15862082Seschrock nvlist_t **spares; 15872082Seschrock uint_t i, nspares; 1588789Sahrens 1589789Sahrens txg = spa_vdev_enter(spa); 1590789Sahrens 15912082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 15922082Seschrock VDEV_ALLOC_ADD)) != 0) 15932082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 15942082Seschrock 15953377Seschrock spa->spa_pending_vdev = vd; 1596789Sahrens 15972082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 15982082Seschrock &spares, &nspares) != 0) 15992082Seschrock nspares = 0; 16002082Seschrock 16013377Seschrock if (vd->vdev_children == 0 && nspares == 0) { 16023377Seschrock spa->spa_pending_vdev = NULL; 16032082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 16043377Seschrock } 16052082Seschrock 16062082Seschrock if (vd->vdev_children != 0) { 16073377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 16083377Seschrock spa->spa_pending_vdev = NULL; 16092082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16102082Seschrock } 16112082Seschrock } 16122082Seschrock 16133377Seschrock /* 16143377Seschrock * We must validate the spares after checking the children. Otherwise, 16153377Seschrock * vdev_inuse() will blindly overwrite the spare. 16163377Seschrock */ 16173377Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 16183377Seschrock VDEV_ALLOC_ADD)) != 0) { 16193377Seschrock spa->spa_pending_vdev = NULL; 16203377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16213377Seschrock } 16223377Seschrock 16233377Seschrock spa->spa_pending_vdev = NULL; 16243377Seschrock 16253377Seschrock /* 16263377Seschrock * Transfer each new top-level vdev from vd to rvd. 16273377Seschrock */ 16283377Seschrock for (c = 0; c < vd->vdev_children; c++) { 16293377Seschrock tvd = vd->vdev_child[c]; 16303377Seschrock vdev_remove_child(vd, tvd); 16313377Seschrock tvd->vdev_id = rvd->vdev_children; 16323377Seschrock vdev_add_child(rvd, tvd); 16333377Seschrock vdev_config_dirty(tvd); 16343377Seschrock } 16353377Seschrock 16362082Seschrock if (nspares != 0) { 16372082Seschrock if (spa->spa_sparelist != NULL) { 16382082Seschrock nvlist_t **oldspares; 16392082Seschrock uint_t oldnspares; 16402082Seschrock nvlist_t **newspares; 16412082Seschrock 16422082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 16432082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 16442082Seschrock 16452082Seschrock newspares = kmem_alloc(sizeof (void *) * 16462082Seschrock (nspares + oldnspares), KM_SLEEP); 16472082Seschrock for (i = 0; i < oldnspares; i++) 16482082Seschrock VERIFY(nvlist_dup(oldspares[i], 16492082Seschrock &newspares[i], KM_SLEEP) == 0); 16502082Seschrock for (i = 0; i < nspares; i++) 16512082Seschrock VERIFY(nvlist_dup(spares[i], 16522082Seschrock &newspares[i + oldnspares], 16532082Seschrock KM_SLEEP) == 0); 16542082Seschrock 16552082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 16562082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 16572082Seschrock 16582082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16592082Seschrock ZPOOL_CONFIG_SPARES, newspares, 16602082Seschrock nspares + oldnspares) == 0); 16612082Seschrock for (i = 0; i < oldnspares + nspares; i++) 16622082Seschrock nvlist_free(newspares[i]); 16632082Seschrock kmem_free(newspares, (oldnspares + nspares) * 16642082Seschrock sizeof (void *)); 16652082Seschrock } else { 16662082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 16672082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 16682082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16692082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16702082Seschrock } 16712082Seschrock 16722082Seschrock spa_load_spares(spa); 16732082Seschrock spa->spa_sync_spares = B_TRUE; 1674789Sahrens } 1675789Sahrens 1676789Sahrens /* 16771585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 16781585Sbonwick * If other threads start allocating from these vdevs before we 16791585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 16801585Sbonwick * fail to open the pool because there are DVAs that the config cache 16811585Sbonwick * can't translate. Therefore, we first add the vdevs without 16821585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 16831635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 16841585Sbonwick * 16851585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 16861585Sbonwick * if we lose power at any point in this sequence, the remaining 16871585Sbonwick * steps will be completed the next time we load the pool. 1688789Sahrens */ 16891635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 16901585Sbonwick 16911635Sbonwick mutex_enter(&spa_namespace_lock); 16921635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 16931635Sbonwick mutex_exit(&spa_namespace_lock); 1694789Sahrens 16951635Sbonwick return (0); 1696789Sahrens } 1697789Sahrens 1698789Sahrens /* 1699789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1700789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1701789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1702789Sahrens * 1703789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1704789Sahrens * existing device; in this case the two devices are made into their own 17054451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 1706789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1707789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1708789Sahrens * completion of resilvering, the first disk (the one being replaced) 1709789Sahrens * is automatically detached. 1710789Sahrens */ 1711789Sahrens int 17121544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1713789Sahrens { 1714789Sahrens uint64_t txg, open_txg; 1715789Sahrens int error; 1716789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1717789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 17182082Seschrock vdev_ops_t *pvops; 17194527Sperrin int is_log; 1720789Sahrens 1721789Sahrens txg = spa_vdev_enter(spa); 1722789Sahrens 17231544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1724789Sahrens 1725789Sahrens if (oldvd == NULL) 1726789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1727789Sahrens 17281585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 17291585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17301585Sbonwick 1731789Sahrens pvd = oldvd->vdev_parent; 1732789Sahrens 17332082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 17344451Seschrock VDEV_ALLOC_ADD)) != 0) 17354451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 17364451Seschrock 17374451Seschrock if (newrootvd->vdev_children != 1) 1738789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1739789Sahrens 1740789Sahrens newvd = newrootvd->vdev_child[0]; 1741789Sahrens 1742789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1743789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1744789Sahrens 17452082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1746789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1747789Sahrens 17484527Sperrin /* 17494527Sperrin * Spares can't replace logs 17504527Sperrin */ 17514527Sperrin is_log = oldvd->vdev_islog; 17524527Sperrin if (is_log && newvd->vdev_isspare) 17534527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17544527Sperrin 17552082Seschrock if (!replacing) { 17562082Seschrock /* 17572082Seschrock * For attach, the only allowable parent is a mirror or the root 17582082Seschrock * vdev. 17592082Seschrock */ 17602082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 17612082Seschrock pvd->vdev_ops != &vdev_root_ops) 17622082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17632082Seschrock 17642082Seschrock pvops = &vdev_mirror_ops; 17652082Seschrock } else { 17662082Seschrock /* 17672082Seschrock * Active hot spares can only be replaced by inactive hot 17682082Seschrock * spares. 17692082Seschrock */ 17702082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 17712082Seschrock pvd->vdev_child[1] == oldvd && 17722082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 17732082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17742082Seschrock 17752082Seschrock /* 17762082Seschrock * If the source is a hot spare, and the parent isn't already a 17772082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 17783377Seschrock * want to create a replacing vdev. The user is not allowed to 17793377Seschrock * attach to a spared vdev child unless the 'isspare' state is 17803377Seschrock * the same (spare replaces spare, non-spare replaces 17813377Seschrock * non-spare). 17822082Seschrock */ 17832082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 17842082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17853377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 17863377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 17873377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17882082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 17892082Seschrock newvd->vdev_isspare) 17902082Seschrock pvops = &vdev_spare_ops; 17912082Seschrock else 17922082Seschrock pvops = &vdev_replacing_ops; 17932082Seschrock } 17942082Seschrock 17951175Slling /* 17961175Slling * Compare the new device size with the replaceable/attachable 17971175Slling * device size. 17981175Slling */ 17991175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1800789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1801789Sahrens 18021732Sbonwick /* 18031732Sbonwick * The new device cannot have a higher alignment requirement 18041732Sbonwick * than the top-level vdev. 18051732Sbonwick */ 18061732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1807789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1808789Sahrens 1809789Sahrens /* 1810789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1811789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1812789Sahrens */ 1813789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1814789Sahrens spa_strfree(oldvd->vdev_path); 1815789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1816789Sahrens KM_SLEEP); 1817789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1818789Sahrens newvd->vdev_path, "old"); 1819789Sahrens if (oldvd->vdev_devid != NULL) { 1820789Sahrens spa_strfree(oldvd->vdev_devid); 1821789Sahrens oldvd->vdev_devid = NULL; 1822789Sahrens } 1823789Sahrens } 1824789Sahrens 1825789Sahrens /* 18262082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 18272082Seschrock * mirror/replacing/spare vdev above oldvd. 1828789Sahrens */ 1829789Sahrens if (pvd->vdev_ops != pvops) 1830789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1831789Sahrens 1832789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1833789Sahrens ASSERT(pvd->vdev_ops == pvops); 1834789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1835789Sahrens 1836789Sahrens /* 1837789Sahrens * Extract the new device from its root and add it to pvd. 1838789Sahrens */ 1839789Sahrens vdev_remove_child(newrootvd, newvd); 1840789Sahrens newvd->vdev_id = pvd->vdev_children; 1841789Sahrens vdev_add_child(pvd, newvd); 1842789Sahrens 18431544Seschrock /* 18441544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 18451544Seschrock * the addition of newvd may have decreased our parent's asize. 18461544Seschrock */ 18471544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 18481544Seschrock 1849789Sahrens tvd = newvd->vdev_top; 1850789Sahrens ASSERT(pvd->vdev_top == tvd); 1851789Sahrens ASSERT(tvd->vdev_parent == rvd); 1852789Sahrens 1853789Sahrens vdev_config_dirty(tvd); 1854789Sahrens 1855789Sahrens /* 1856789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1857789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1858789Sahrens */ 1859789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1860789Sahrens 1861789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1862789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1863789Sahrens open_txg - TXG_INITIAL + 1); 1864789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1865789Sahrens 18663377Seschrock if (newvd->vdev_isspare) 18673377Seschrock spa_spare_activate(newvd); 18681544Seschrock 1869789Sahrens /* 1870789Sahrens * Mark newvd's DTL dirty in this txg. 1871789Sahrens */ 18721732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1873789Sahrens 1874789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1875789Sahrens 1876789Sahrens /* 18774451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 18784451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 1879789Sahrens */ 18804451Seschrock mutex_enter(&spa_namespace_lock); 1881789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 18824451Seschrock mutex_exit(&spa_namespace_lock); 1883789Sahrens 1884789Sahrens return (0); 1885789Sahrens } 1886789Sahrens 1887789Sahrens /* 1888789Sahrens * Detach a device from a mirror or replacing vdev. 1889789Sahrens * If 'replace_done' is specified, only detach if the parent 1890789Sahrens * is a replacing vdev. 1891789Sahrens */ 1892789Sahrens int 18931544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1894789Sahrens { 1895789Sahrens uint64_t txg; 1896789Sahrens int c, t, error; 1897789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1898789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 18992082Seschrock boolean_t unspare = B_FALSE; 19002082Seschrock uint64_t unspare_guid; 1901789Sahrens 1902789Sahrens txg = spa_vdev_enter(spa); 1903789Sahrens 19041544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1905789Sahrens 1906789Sahrens if (vd == NULL) 1907789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1908789Sahrens 19091585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 19101585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19111585Sbonwick 1912789Sahrens pvd = vd->vdev_parent; 1913789Sahrens 1914789Sahrens /* 1915789Sahrens * If replace_done is specified, only remove this device if it's 19162082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 19172082Seschrock * disk can be removed. 1918789Sahrens */ 19192082Seschrock if (replace_done) { 19202082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 19212082Seschrock if (vd->vdev_id != 0) 19222082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19232082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 19242082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19252082Seschrock } 19262082Seschrock } 19272082Seschrock 19282082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 19294577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 1930789Sahrens 1931789Sahrens /* 19322082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1933789Sahrens */ 1934789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 19352082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 19362082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1937789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1938789Sahrens 1939789Sahrens /* 1940789Sahrens * If there's only one replica, you can't detach it. 1941789Sahrens */ 1942789Sahrens if (pvd->vdev_children <= 1) 1943789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1944789Sahrens 1945789Sahrens /* 1946789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1947789Sahrens * valid copy of the data, which means we cannot safely detach it. 1948789Sahrens * 1949789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1950789Sahrens * precise DTL check. 1951789Sahrens */ 1952789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1953789Sahrens uint64_t dirty; 1954789Sahrens 1955789Sahrens cvd = pvd->vdev_child[c]; 1956789Sahrens if (cvd == vd) 1957789Sahrens continue; 1958789Sahrens if (vdev_is_dead(cvd)) 1959789Sahrens continue; 1960789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1961789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1962789Sahrens cvd->vdev_dtl_scrub.sm_space; 1963789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1964789Sahrens if (!dirty) 1965789Sahrens break; 1966789Sahrens } 19672082Seschrock 19682082Seschrock /* 19692082Seschrock * If we are a replacing or spare vdev, then we can always detach the 19702082Seschrock * latter child, as that is how one cancels the operation. 19712082Seschrock */ 19722082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 19732082Seschrock c == pvd->vdev_children) 1974789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1975789Sahrens 1976789Sahrens /* 19772082Seschrock * If we are detaching the original disk from a spare, then it implies 19782082Seschrock * that the spare should become a real disk, and be removed from the 19792082Seschrock * active spare list for the pool. 19802082Seschrock */ 19812082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 19822082Seschrock vd->vdev_id == 0) 19832082Seschrock unspare = B_TRUE; 19842082Seschrock 19852082Seschrock /* 1986789Sahrens * Erase the disk labels so the disk can be used for other things. 1987789Sahrens * This must be done after all other error cases are handled, 1988789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1989789Sahrens * But if we can't do it, don't treat the error as fatal -- 1990789Sahrens * it may be that the unwritability of the disk is the reason 1991789Sahrens * it's being detached! 1992789Sahrens */ 19933377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1994789Sahrens 1995789Sahrens /* 1996789Sahrens * Remove vd from its parent and compact the parent's children. 1997789Sahrens */ 1998789Sahrens vdev_remove_child(pvd, vd); 1999789Sahrens vdev_compact_children(pvd); 2000789Sahrens 2001789Sahrens /* 2002789Sahrens * Remember one of the remaining children so we can get tvd below. 2003789Sahrens */ 2004789Sahrens cvd = pvd->vdev_child[0]; 2005789Sahrens 2006789Sahrens /* 20072082Seschrock * If we need to remove the remaining child from the list of hot spares, 20082082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 20092082Seschrock * must do this before vdev_remove_parent(), because that can change the 20102082Seschrock * GUID if it creates a new toplevel GUID. 20112082Seschrock */ 20122082Seschrock if (unspare) { 20132082Seschrock ASSERT(cvd->vdev_isspare); 20143377Seschrock spa_spare_remove(cvd); 20152082Seschrock unspare_guid = cvd->vdev_guid; 20162082Seschrock } 20172082Seschrock 20182082Seschrock /* 2019789Sahrens * If the parent mirror/replacing vdev only has one child, 2020789Sahrens * the parent is no longer needed. Remove it from the tree. 2021789Sahrens */ 2022789Sahrens if (pvd->vdev_children == 1) 2023789Sahrens vdev_remove_parent(cvd); 2024789Sahrens 2025789Sahrens /* 2026789Sahrens * We don't set tvd until now because the parent we just removed 2027789Sahrens * may have been the previous top-level vdev. 2028789Sahrens */ 2029789Sahrens tvd = cvd->vdev_top; 2030789Sahrens ASSERT(tvd->vdev_parent == rvd); 2031789Sahrens 2032789Sahrens /* 20333377Seschrock * Reevaluate the parent vdev state. 2034789Sahrens */ 20354451Seschrock vdev_propagate_state(cvd); 2036789Sahrens 2037789Sahrens /* 20383377Seschrock * If the device we just detached was smaller than the others, it may be 20393377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 20403377Seschrock * can't fail because the existing metaslabs are already in core, so 20413377Seschrock * there's nothing to read from disk. 2042789Sahrens */ 20431732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2044789Sahrens 2045789Sahrens vdev_config_dirty(tvd); 2046789Sahrens 2047789Sahrens /* 20483377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 20493377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 20503377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 20513377Seschrock * prevent vd from being accessed after it's freed. 2052789Sahrens */ 2053789Sahrens for (t = 0; t < TXG_SIZE; t++) 2054789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 20551732Sbonwick vd->vdev_detached = B_TRUE; 20561732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2057789Sahrens 20584451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 20594451Seschrock 20602082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 20612082Seschrock 20622082Seschrock /* 20633377Seschrock * If this was the removal of the original device in a hot spare vdev, 20643377Seschrock * then we want to go through and remove the device from the hot spare 20653377Seschrock * list of every other pool. 20662082Seschrock */ 20672082Seschrock if (unspare) { 20682082Seschrock spa = NULL; 20692082Seschrock mutex_enter(&spa_namespace_lock); 20702082Seschrock while ((spa = spa_next(spa)) != NULL) { 20712082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 20722082Seschrock continue; 20732082Seschrock 20742082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 20752082Seschrock } 20762082Seschrock mutex_exit(&spa_namespace_lock); 20772082Seschrock } 20782082Seschrock 20792082Seschrock return (error); 20802082Seschrock } 20812082Seschrock 20822082Seschrock /* 20832082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 20842082Seschrock * spares. 20852082Seschrock */ 20862082Seschrock int 20872082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 20882082Seschrock { 20892082Seschrock vdev_t *vd; 20902082Seschrock nvlist_t **spares, *nv, **newspares; 20912082Seschrock uint_t i, j, nspares; 20922082Seschrock int ret = 0; 20932082Seschrock 20942082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20952082Seschrock 20962082Seschrock vd = spa_lookup_by_guid(spa, guid); 20972082Seschrock 20982082Seschrock nv = NULL; 20992082Seschrock if (spa->spa_spares != NULL && 21002082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21012082Seschrock &spares, &nspares) == 0) { 21022082Seschrock for (i = 0; i < nspares; i++) { 21032082Seschrock uint64_t theguid; 21042082Seschrock 21052082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 21062082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 21072082Seschrock if (theguid == guid) { 21082082Seschrock nv = spares[i]; 21092082Seschrock break; 21102082Seschrock } 21112082Seschrock } 21122082Seschrock } 21132082Seschrock 21142082Seschrock /* 21152082Seschrock * We only support removing a hot spare, and only if it's not currently 21162082Seschrock * in use in this pool. 21172082Seschrock */ 21182082Seschrock if (nv == NULL && vd == NULL) { 21192082Seschrock ret = ENOENT; 21202082Seschrock goto out; 21212082Seschrock } 21222082Seschrock 21232082Seschrock if (nv == NULL && vd != NULL) { 21242082Seschrock ret = ENOTSUP; 21252082Seschrock goto out; 21262082Seschrock } 21272082Seschrock 21282082Seschrock if (!unspare && nv != NULL && vd != NULL) { 21292082Seschrock ret = EBUSY; 21302082Seschrock goto out; 21312082Seschrock } 21322082Seschrock 21332082Seschrock if (nspares == 1) { 21342082Seschrock newspares = NULL; 21352082Seschrock } else { 21362082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 21372082Seschrock KM_SLEEP); 21382082Seschrock for (i = 0, j = 0; i < nspares; i++) { 21392082Seschrock if (spares[i] != nv) 21402082Seschrock VERIFY(nvlist_dup(spares[i], 21412082Seschrock &newspares[j++], KM_SLEEP) == 0); 21422082Seschrock } 21432082Seschrock } 21442082Seschrock 21452082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21462082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 21472082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21482082Seschrock newspares, nspares - 1) == 0); 21492082Seschrock for (i = 0; i < nspares - 1; i++) 21502082Seschrock nvlist_free(newspares[i]); 21512082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 21522082Seschrock spa_load_spares(spa); 21532082Seschrock spa->spa_sync_spares = B_TRUE; 21542082Seschrock 21552082Seschrock out: 21562082Seschrock spa_config_exit(spa, FTAG); 21572082Seschrock 21582082Seschrock return (ret); 2159789Sahrens } 2160789Sahrens 2161789Sahrens /* 21624451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 21634451Seschrock * current spared, so we can detach it. 2164789Sahrens */ 21651544Seschrock static vdev_t * 21664451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2167789Sahrens { 21681544Seschrock vdev_t *newvd, *oldvd; 2169789Sahrens int c; 2170789Sahrens 21711544Seschrock for (c = 0; c < vd->vdev_children; c++) { 21724451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 21731544Seschrock if (oldvd != NULL) 21741544Seschrock return (oldvd); 21751544Seschrock } 2176789Sahrens 21774451Seschrock /* 21784451Seschrock * Check for a completed replacement. 21794451Seschrock */ 2180789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 21811544Seschrock oldvd = vd->vdev_child[0]; 21821544Seschrock newvd = vd->vdev_child[1]; 2183789Sahrens 21841544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 21851544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 21861544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 21871544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21881544Seschrock return (oldvd); 21891544Seschrock } 21901544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21911544Seschrock } 2192789Sahrens 21934451Seschrock /* 21944451Seschrock * Check for a completed resilver with the 'unspare' flag set. 21954451Seschrock */ 21964451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 21974451Seschrock newvd = vd->vdev_child[0]; 21984451Seschrock oldvd = vd->vdev_child[1]; 21994451Seschrock 22004451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 22014451Seschrock if (newvd->vdev_unspare && 22024451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 22034451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 22044451Seschrock newvd->vdev_unspare = 0; 22054451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22064451Seschrock return (oldvd); 22074451Seschrock } 22084451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22094451Seschrock } 22104451Seschrock 22111544Seschrock return (NULL); 2212789Sahrens } 2213789Sahrens 22141544Seschrock static void 22154451Seschrock spa_vdev_resilver_done(spa_t *spa) 2216789Sahrens { 22171544Seschrock vdev_t *vd; 22182082Seschrock vdev_t *pvd; 22191544Seschrock uint64_t guid; 22202082Seschrock uint64_t pguid = 0; 2221789Sahrens 22221544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2223789Sahrens 22244451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 22251544Seschrock guid = vd->vdev_guid; 22262082Seschrock /* 22272082Seschrock * If we have just finished replacing a hot spared device, then 22282082Seschrock * we need to detach the parent's first child (the original hot 22292082Seschrock * spare) as well. 22302082Seschrock */ 22312082Seschrock pvd = vd->vdev_parent; 22322082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 22332082Seschrock pvd->vdev_id == 0) { 22342082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 22352082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 22362082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 22372082Seschrock } 22381544Seschrock spa_config_exit(spa, FTAG); 22391544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 22401544Seschrock return; 22412082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 22422082Seschrock return; 22431544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2244789Sahrens } 2245789Sahrens 22461544Seschrock spa_config_exit(spa, FTAG); 2247789Sahrens } 2248789Sahrens 2249789Sahrens /* 22501354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 22511354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 22521354Seschrock */ 22531354Seschrock int 22541354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 22551354Seschrock { 22561354Seschrock vdev_t *rvd, *vd; 22571354Seschrock uint64_t txg; 22581354Seschrock 22591354Seschrock rvd = spa->spa_root_vdev; 22601354Seschrock 22611354Seschrock txg = spa_vdev_enter(spa); 22621354Seschrock 22632082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 22642082Seschrock /* 22652082Seschrock * Determine if this is a reference to a hot spare. In that 22662082Seschrock * case, update the path as stored in the spare list. 22672082Seschrock */ 22682082Seschrock nvlist_t **spares; 22692082Seschrock uint_t i, nspares; 22702082Seschrock if (spa->spa_sparelist != NULL) { 22712082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 22722082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 22732082Seschrock for (i = 0; i < nspares; i++) { 22742082Seschrock uint64_t theguid; 22752082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 22762082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 22772082Seschrock if (theguid == guid) 22782082Seschrock break; 22792082Seschrock } 22802082Seschrock 22812082Seschrock if (i == nspares) 22822082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22832082Seschrock 22842082Seschrock VERIFY(nvlist_add_string(spares[i], 22852082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 22862082Seschrock spa_load_spares(spa); 22872082Seschrock spa->spa_sync_spares = B_TRUE; 22882082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 22892082Seschrock } else { 22902082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22912082Seschrock } 22922082Seschrock } 22931354Seschrock 22941585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 22951585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 22961585Sbonwick 22971354Seschrock spa_strfree(vd->vdev_path); 22981354Seschrock vd->vdev_path = spa_strdup(newpath); 22991354Seschrock 23001354Seschrock vdev_config_dirty(vd->vdev_top); 23011354Seschrock 23021354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 23031354Seschrock } 23041354Seschrock 23051354Seschrock /* 2306789Sahrens * ========================================================================== 2307789Sahrens * SPA Scrubbing 2308789Sahrens * ========================================================================== 2309789Sahrens */ 2310789Sahrens 2311789Sahrens static void 2312789Sahrens spa_scrub_io_done(zio_t *zio) 2313789Sahrens { 2314789Sahrens spa_t *spa = zio->io_spa; 2315789Sahrens 23164309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2317789Sahrens 2318789Sahrens mutex_enter(&spa->spa_scrub_lock); 23191544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 23201775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2321789Sahrens spa->spa_scrub_errors++; 2322789Sahrens mutex_enter(&vd->vdev_stat_lock); 2323789Sahrens vd->vdev_stat.vs_scrub_errors++; 2324789Sahrens mutex_exit(&vd->vdev_stat_lock); 2325789Sahrens } 23263697Smishra 23273697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 23281544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 23293697Smishra 23303697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 23313697Smishra 23321544Seschrock mutex_exit(&spa->spa_scrub_lock); 2333789Sahrens } 2334789Sahrens 2335789Sahrens static void 23361544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 23371544Seschrock zbookmark_t *zb) 2338789Sahrens { 2339789Sahrens size_t size = BP_GET_LSIZE(bp); 23403697Smishra void *data; 2341789Sahrens 2342789Sahrens mutex_enter(&spa->spa_scrub_lock); 23433697Smishra /* 23443697Smishra * Do not give too much work to vdev(s). 23453697Smishra */ 23463697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 23473697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 23483697Smishra } 2349789Sahrens spa->spa_scrub_inflight++; 2350789Sahrens mutex_exit(&spa->spa_scrub_lock); 2351789Sahrens 23524309Smaybee data = arc_data_buf_alloc(size); 23533697Smishra 23541544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 23551544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 23561544Seschrock 23571807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 23581544Seschrock 2359789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 23601544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2361789Sahrens } 2362789Sahrens 2363789Sahrens /* ARGSUSED */ 2364789Sahrens static int 2365789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2366789Sahrens { 2367789Sahrens blkptr_t *bp = &bc->bc_blkptr; 23681775Sbillm vdev_t *vd = spa->spa_root_vdev; 23691775Sbillm dva_t *dva = bp->blk_dva; 23701775Sbillm int needs_resilver = B_FALSE; 23711775Sbillm int d; 2372789Sahrens 23731775Sbillm if (bc->bc_errno) { 2374789Sahrens /* 2375789Sahrens * We can't scrub this block, but we can continue to scrub 2376789Sahrens * the rest of the pool. Note the error and move along. 2377789Sahrens */ 2378789Sahrens mutex_enter(&spa->spa_scrub_lock); 2379789Sahrens spa->spa_scrub_errors++; 2380789Sahrens mutex_exit(&spa->spa_scrub_lock); 2381789Sahrens 23821775Sbillm mutex_enter(&vd->vdev_stat_lock); 23831775Sbillm vd->vdev_stat.vs_scrub_errors++; 23841775Sbillm mutex_exit(&vd->vdev_stat_lock); 2385789Sahrens 2386789Sahrens return (ERESTART); 2387789Sahrens } 2388789Sahrens 2389789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2390789Sahrens 23911775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 23921775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 23931775Sbillm 23941775Sbillm ASSERT(vd != NULL); 23951775Sbillm 23961775Sbillm /* 23971775Sbillm * Keep track of how much data we've examined so that 23981775Sbillm * zpool(1M) status can make useful progress reports. 23991775Sbillm */ 24001775Sbillm mutex_enter(&vd->vdev_stat_lock); 24011775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 24021775Sbillm mutex_exit(&vd->vdev_stat_lock); 2403789Sahrens 24041775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 24051775Sbillm if (DVA_GET_GANG(&dva[d])) { 24061775Sbillm /* 24071775Sbillm * Gang members may be spread across multiple 24081775Sbillm * vdevs, so the best we can do is look at the 24091775Sbillm * pool-wide DTL. 24101775Sbillm * XXX -- it would be better to change our 24111775Sbillm * allocation policy to ensure that this can't 24121775Sbillm * happen. 24131775Sbillm */ 24141775Sbillm vd = spa->spa_root_vdev; 24151775Sbillm } 24161775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 24171775Sbillm bp->blk_birth, 1)) 24181775Sbillm needs_resilver = B_TRUE; 2419789Sahrens } 24201775Sbillm } 24211775Sbillm 24221775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2423789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 24241544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 24251775Sbillm else if (needs_resilver) 24261775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 24271775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2428789Sahrens 2429789Sahrens return (0); 2430789Sahrens } 2431789Sahrens 2432789Sahrens static void 2433789Sahrens spa_scrub_thread(spa_t *spa) 2434789Sahrens { 2435789Sahrens callb_cpr_t cprinfo; 2436789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2437789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2438789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2439789Sahrens int error = 0; 2440789Sahrens boolean_t complete; 2441789Sahrens 2442789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2443789Sahrens 2444797Sbonwick /* 2445797Sbonwick * If we're restarting due to a snapshot create/delete, 2446797Sbonwick * wait for that to complete. 2447797Sbonwick */ 2448797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2449797Sbonwick 24501544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 24511544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 24521544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 24531544Seschrock 24541544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 24551544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2456789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2457789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 24581544Seschrock spa_config_exit(spa, FTAG); 2459789Sahrens 2460789Sahrens mutex_enter(&spa->spa_scrub_lock); 2461789Sahrens spa->spa_scrub_errors = 0; 2462789Sahrens spa->spa_scrub_active = 1; 24631544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2464789Sahrens 2465789Sahrens while (!spa->spa_scrub_stop) { 2466789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 24671544Seschrock while (spa->spa_scrub_suspended) { 2468789Sahrens spa->spa_scrub_active = 0; 2469789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2470789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2471789Sahrens spa->spa_scrub_active = 1; 2472789Sahrens } 2473789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2474789Sahrens 2475789Sahrens if (spa->spa_scrub_restart_txg != 0) 2476789Sahrens break; 2477789Sahrens 2478789Sahrens mutex_exit(&spa->spa_scrub_lock); 2479789Sahrens error = traverse_more(th); 2480789Sahrens mutex_enter(&spa->spa_scrub_lock); 2481789Sahrens if (error != EAGAIN) 2482789Sahrens break; 2483789Sahrens } 2484789Sahrens 2485789Sahrens while (spa->spa_scrub_inflight) 2486789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2487789Sahrens 24881601Sbonwick spa->spa_scrub_active = 0; 24891601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 24901601Sbonwick 24911601Sbonwick mutex_exit(&spa->spa_scrub_lock); 24921601Sbonwick 24931601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24941601Sbonwick 24951601Sbonwick mutex_enter(&spa->spa_scrub_lock); 24961601Sbonwick 24971601Sbonwick /* 24981601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 24991601Sbonwick * AND the spa config lock to synchronize with any config changes 25001601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 25011601Sbonwick */ 2502789Sahrens if (spa->spa_scrub_restart_txg != 0) 2503789Sahrens error = ERESTART; 2504789Sahrens 25051544Seschrock if (spa->spa_scrub_stop) 25061544Seschrock error = EINTR; 25071544Seschrock 2508789Sahrens /* 25091544Seschrock * Even if there were uncorrectable errors, we consider the scrub 25101544Seschrock * completed. The downside is that if there is a transient error during 25111544Seschrock * a resilver, we won't resilver the data properly to the target. But 25121544Seschrock * if the damage is permanent (more likely) we will resilver forever, 25131544Seschrock * which isn't really acceptable. Since there is enough information for 25141544Seschrock * the user to know what has failed and why, this seems like a more 25151544Seschrock * tractable approach. 2516789Sahrens */ 25171544Seschrock complete = (error == 0); 2518789Sahrens 25191544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 25201544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2521789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2522789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2523789Sahrens 2524789Sahrens mutex_exit(&spa->spa_scrub_lock); 2525789Sahrens 2526789Sahrens /* 2527789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2528789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2529789Sahrens */ 2530789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2531789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2532789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 25331544Seschrock spa_errlog_rotate(spa); 25341601Sbonwick 25354451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 25364451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 25374451Seschrock 25381544Seschrock spa_config_exit(spa, FTAG); 2539789Sahrens 2540789Sahrens mutex_enter(&spa->spa_scrub_lock); 2541789Sahrens 25421544Seschrock /* 25431544Seschrock * We may have finished replacing a device. 25441544Seschrock * Let the async thread assess this and handle the detach. 25451544Seschrock */ 25464451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2547789Sahrens 2548789Sahrens /* 2549789Sahrens * If we were told to restart, our final act is to start a new scrub. 2550789Sahrens */ 2551789Sahrens if (error == ERESTART) 25521544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 25531544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2554789Sahrens 25551544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 25561544Seschrock spa->spa_scrub_active = 0; 25571544Seschrock spa->spa_scrub_thread = NULL; 25581544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2559789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2560789Sahrens thread_exit(); 2561789Sahrens } 2562789Sahrens 2563789Sahrens void 2564789Sahrens spa_scrub_suspend(spa_t *spa) 2565789Sahrens { 2566789Sahrens mutex_enter(&spa->spa_scrub_lock); 25671544Seschrock spa->spa_scrub_suspended++; 2568789Sahrens while (spa->spa_scrub_active) { 2569789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2570789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2571789Sahrens } 2572789Sahrens while (spa->spa_scrub_inflight) 2573789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2574789Sahrens mutex_exit(&spa->spa_scrub_lock); 2575789Sahrens } 2576789Sahrens 2577789Sahrens void 2578789Sahrens spa_scrub_resume(spa_t *spa) 2579789Sahrens { 2580789Sahrens mutex_enter(&spa->spa_scrub_lock); 25811544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 25821544Seschrock if (--spa->spa_scrub_suspended == 0) 2583789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2584789Sahrens mutex_exit(&spa->spa_scrub_lock); 2585789Sahrens } 2586789Sahrens 2587789Sahrens void 2588789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2589789Sahrens { 2590789Sahrens /* 2591789Sahrens * Something happened (e.g. snapshot create/delete) that means 2592789Sahrens * we must restart any in-progress scrubs. The itinerary will 2593789Sahrens * fix this properly. 2594789Sahrens */ 2595789Sahrens mutex_enter(&spa->spa_scrub_lock); 2596789Sahrens spa->spa_scrub_restart_txg = txg; 2597789Sahrens mutex_exit(&spa->spa_scrub_lock); 2598789Sahrens } 2599789Sahrens 26001544Seschrock int 26011544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2602789Sahrens { 2603789Sahrens space_seg_t *ss; 2604789Sahrens uint64_t mintxg, maxtxg; 2605789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2606789Sahrens 2607789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2608789Sahrens return (ENOTSUP); 2609789Sahrens 26101544Seschrock mutex_enter(&spa->spa_scrub_lock); 26111544Seschrock 2612789Sahrens /* 2613789Sahrens * If there's a scrub or resilver already in progress, stop it. 2614789Sahrens */ 2615789Sahrens while (spa->spa_scrub_thread != NULL) { 2616789Sahrens /* 2617789Sahrens * Don't stop a resilver unless forced. 2618789Sahrens */ 26191544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 26201544Seschrock mutex_exit(&spa->spa_scrub_lock); 2621789Sahrens return (EBUSY); 26221544Seschrock } 2623789Sahrens spa->spa_scrub_stop = 1; 2624789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2625789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2626789Sahrens } 2627789Sahrens 2628789Sahrens /* 2629789Sahrens * Terminate the previous traverse. 2630789Sahrens */ 2631789Sahrens if (spa->spa_scrub_th != NULL) { 2632789Sahrens traverse_fini(spa->spa_scrub_th); 2633789Sahrens spa->spa_scrub_th = NULL; 2634789Sahrens } 2635789Sahrens 26361544Seschrock if (rvd == NULL) { 26371544Seschrock ASSERT(spa->spa_scrub_stop == 0); 26381544Seschrock ASSERT(spa->spa_scrub_type == type); 26391544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 26401544Seschrock mutex_exit(&spa->spa_scrub_lock); 26411544Seschrock return (0); 26421544Seschrock } 2643789Sahrens 2644789Sahrens mintxg = TXG_INITIAL - 1; 2645789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2646789Sahrens 26471544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2648789Sahrens 26491544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 26501544Seschrock /* 26511544Seschrock * The pool-wide DTL is empty. 26521732Sbonwick * If this is a resilver, there's nothing to do except 26531732Sbonwick * check whether any in-progress replacements have completed. 26541544Seschrock */ 26551732Sbonwick if (type == POOL_SCRUB_RESILVER) { 26561544Seschrock type = POOL_SCRUB_NONE; 26574451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 26581732Sbonwick } 26591544Seschrock } else { 26601544Seschrock /* 26611544Seschrock * The pool-wide DTL is non-empty. 26621544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 26631544Seschrock */ 26641544Seschrock if (type == POOL_SCRUB_EVERYTHING) 26651544Seschrock type = POOL_SCRUB_RESILVER; 26661544Seschrock } 2667789Sahrens 26681544Seschrock if (type == POOL_SCRUB_RESILVER) { 2669789Sahrens /* 2670789Sahrens * Determine the resilvering boundaries. 2671789Sahrens * 2672789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2673789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2674789Sahrens * 2675789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2676789Sahrens * so we don't claim to resilver a txg that's still changing. 2677789Sahrens */ 2678789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 26791544Seschrock mintxg = ss->ss_start - 1; 2680789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 26811544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 26824451Seschrock 26834451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 2684789Sahrens } 2685789Sahrens 26861544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 26871544Seschrock 26881544Seschrock spa->spa_scrub_stop = 0; 26891544Seschrock spa->spa_scrub_type = type; 26901544Seschrock spa->spa_scrub_restart_txg = 0; 26911544Seschrock 26921544Seschrock if (type != POOL_SCRUB_NONE) { 26931544Seschrock spa->spa_scrub_mintxg = mintxg; 2694789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2695789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 26961635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 26971635Sbonwick ZIO_FLAG_CANFAIL); 2698789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2699789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2700789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2701789Sahrens } 2702789Sahrens 27031544Seschrock mutex_exit(&spa->spa_scrub_lock); 27041544Seschrock 2705789Sahrens return (0); 2706789Sahrens } 2707789Sahrens 27081544Seschrock /* 27091544Seschrock * ========================================================================== 27101544Seschrock * SPA async task processing 27111544Seschrock * ========================================================================== 27121544Seschrock */ 27131544Seschrock 27141544Seschrock static void 27154451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 2716789Sahrens { 27171544Seschrock vdev_t *tvd; 27181544Seschrock int c; 27191544Seschrock 27204451Seschrock for (c = 0; c < vd->vdev_children; c++) { 27214451Seschrock tvd = vd->vdev_child[c]; 27224451Seschrock if (tvd->vdev_remove_wanted) { 27234451Seschrock tvd->vdev_remove_wanted = 0; 27244451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 27254451Seschrock VDEV_AUX_NONE); 27264451Seschrock vdev_clear(spa, tvd); 27274451Seschrock vdev_config_dirty(tvd->vdev_top); 27281544Seschrock } 27294451Seschrock spa_async_remove(spa, tvd); 27301544Seschrock } 27311544Seschrock } 27321544Seschrock 27331544Seschrock static void 27341544Seschrock spa_async_thread(spa_t *spa) 27351544Seschrock { 27361544Seschrock int tasks; 27374451Seschrock uint64_t txg; 27381544Seschrock 27391544Seschrock ASSERT(spa->spa_sync_on); 2740789Sahrens 27411544Seschrock mutex_enter(&spa->spa_async_lock); 27421544Seschrock tasks = spa->spa_async_tasks; 27431544Seschrock spa->spa_async_tasks = 0; 27441544Seschrock mutex_exit(&spa->spa_async_lock); 27451544Seschrock 27461544Seschrock /* 27471635Sbonwick * See if the config needs to be updated. 27481635Sbonwick */ 27491635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 27501635Sbonwick mutex_enter(&spa_namespace_lock); 27511635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27521635Sbonwick mutex_exit(&spa_namespace_lock); 27531635Sbonwick } 27541635Sbonwick 27551635Sbonwick /* 27564451Seschrock * See if any devices need to be marked REMOVED. 27571544Seschrock */ 27584451Seschrock if (tasks & SPA_ASYNC_REMOVE) { 27594451Seschrock txg = spa_vdev_enter(spa); 27604451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 27614451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 27624451Seschrock } 27631544Seschrock 27641544Seschrock /* 27651544Seschrock * If any devices are done replacing, detach them. 27661544Seschrock */ 27674451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 27684451Seschrock spa_vdev_resilver_done(spa); 2769789Sahrens 27701544Seschrock /* 27714451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 27724451Seschrock * scrub which can become a resilver), we need to hold 27734451Seschrock * spa_namespace_lock() because the sysevent we post via 27744451Seschrock * spa_event_notify() needs to get the name of the pool. 27751544Seschrock */ 27764451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 27774451Seschrock mutex_enter(&spa_namespace_lock); 27781544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 27794451Seschrock mutex_exit(&spa_namespace_lock); 27804451Seschrock } 27811544Seschrock 27821544Seschrock /* 27831544Seschrock * Kick off a resilver. 27841544Seschrock */ 27854451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 27864451Seschrock mutex_enter(&spa_namespace_lock); 27871544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 27884451Seschrock mutex_exit(&spa_namespace_lock); 27894451Seschrock } 27901544Seschrock 27911544Seschrock /* 27921544Seschrock * Let the world know that we're done. 27931544Seschrock */ 27941544Seschrock mutex_enter(&spa->spa_async_lock); 27951544Seschrock spa->spa_async_thread = NULL; 27961544Seschrock cv_broadcast(&spa->spa_async_cv); 27971544Seschrock mutex_exit(&spa->spa_async_lock); 27981544Seschrock thread_exit(); 27991544Seschrock } 28001544Seschrock 28011544Seschrock void 28021544Seschrock spa_async_suspend(spa_t *spa) 28031544Seschrock { 28041544Seschrock mutex_enter(&spa->spa_async_lock); 28051544Seschrock spa->spa_async_suspended++; 28061544Seschrock while (spa->spa_async_thread != NULL) 28071544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 28081544Seschrock mutex_exit(&spa->spa_async_lock); 28091544Seschrock } 28101544Seschrock 28111544Seschrock void 28121544Seschrock spa_async_resume(spa_t *spa) 28131544Seschrock { 28141544Seschrock mutex_enter(&spa->spa_async_lock); 28151544Seschrock ASSERT(spa->spa_async_suspended != 0); 28161544Seschrock spa->spa_async_suspended--; 28171544Seschrock mutex_exit(&spa->spa_async_lock); 28181544Seschrock } 28191544Seschrock 28201544Seschrock static void 28211544Seschrock spa_async_dispatch(spa_t *spa) 28221544Seschrock { 28231544Seschrock mutex_enter(&spa->spa_async_lock); 28241544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 28251635Sbonwick spa->spa_async_thread == NULL && 28261635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 28271544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 28281544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 28291544Seschrock mutex_exit(&spa->spa_async_lock); 28301544Seschrock } 28311544Seschrock 28321544Seschrock void 28331544Seschrock spa_async_request(spa_t *spa, int task) 28341544Seschrock { 28351544Seschrock mutex_enter(&spa->spa_async_lock); 28361544Seschrock spa->spa_async_tasks |= task; 28371544Seschrock mutex_exit(&spa->spa_async_lock); 2838789Sahrens } 2839789Sahrens 2840789Sahrens /* 2841789Sahrens * ========================================================================== 2842789Sahrens * SPA syncing routines 2843789Sahrens * ========================================================================== 2844789Sahrens */ 2845789Sahrens 2846789Sahrens static void 2847789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2848789Sahrens { 2849789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2850789Sahrens dmu_tx_t *tx; 2851789Sahrens blkptr_t blk; 2852789Sahrens uint64_t itor = 0; 2853789Sahrens zio_t *zio; 2854789Sahrens int error; 2855789Sahrens uint8_t c = 1; 2856789Sahrens 2857789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2858789Sahrens 2859789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2860789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2861789Sahrens 2862789Sahrens error = zio_wait(zio); 2863789Sahrens ASSERT3U(error, ==, 0); 2864789Sahrens 2865789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2866789Sahrens bplist_vacate(bpl, tx); 2867789Sahrens 2868789Sahrens /* 2869789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2870789Sahrens * (Usually only the first block is needed.) 2871789Sahrens */ 2872789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2873789Sahrens dmu_tx_commit(tx); 2874789Sahrens } 2875789Sahrens 2876789Sahrens static void 28772082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 28782082Seschrock { 28792082Seschrock char *packed = NULL; 28802082Seschrock size_t nvsize = 0; 28812082Seschrock dmu_buf_t *db; 28822082Seschrock 28832082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 28842082Seschrock 28852082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 28862082Seschrock 28872082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 28882082Seschrock KM_SLEEP) == 0); 28892082Seschrock 28902082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 28912082Seschrock 28922082Seschrock kmem_free(packed, nvsize); 28932082Seschrock 28942082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 28952082Seschrock dmu_buf_will_dirty(db, tx); 28962082Seschrock *(uint64_t *)db->db_data = nvsize; 28972082Seschrock dmu_buf_rele(db, FTAG); 28982082Seschrock } 28992082Seschrock 29002082Seschrock static void 29012082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 29022082Seschrock { 29032082Seschrock nvlist_t *nvroot; 29042082Seschrock nvlist_t **spares; 29052082Seschrock int i; 29062082Seschrock 29072082Seschrock if (!spa->spa_sync_spares) 29082082Seschrock return; 29092082Seschrock 29102082Seschrock /* 29112082Seschrock * Update the MOS nvlist describing the list of available spares. 29122082Seschrock * spa_validate_spares() will have already made sure this nvlist is 29134451Seschrock * valid and the vdevs are labeled appropriately. 29142082Seschrock */ 29152082Seschrock if (spa->spa_spares_object == 0) { 29162082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 29172082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 29182082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 29192082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 29202082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 29212082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 29222082Seschrock } 29232082Seschrock 29242082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 29252082Seschrock if (spa->spa_nspares == 0) { 29262082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29272082Seschrock NULL, 0) == 0); 29282082Seschrock } else { 29292082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 29302082Seschrock KM_SLEEP); 29312082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29322082Seschrock spares[i] = vdev_config_generate(spa, 29332082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 29342082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29352082Seschrock spares, spa->spa_nspares) == 0); 29362082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29372082Seschrock nvlist_free(spares[i]); 29382082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 29392082Seschrock } 29402082Seschrock 29412082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 29422926Sek110237 nvlist_free(nvroot); 29432082Seschrock 29442082Seschrock spa->spa_sync_spares = B_FALSE; 29452082Seschrock } 29462082Seschrock 29472082Seschrock static void 2948789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2949789Sahrens { 2950789Sahrens nvlist_t *config; 2951789Sahrens 2952789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2953789Sahrens return; 2954789Sahrens 2955789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2956789Sahrens 29571635Sbonwick if (spa->spa_config_syncing) 29581635Sbonwick nvlist_free(spa->spa_config_syncing); 29591635Sbonwick spa->spa_config_syncing = config; 2960789Sahrens 29612082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2962789Sahrens } 2963789Sahrens 29643912Slling static void 29654543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 29663912Slling { 29673912Slling spa_t *spa = arg1; 29683912Slling nvlist_t *nvp = arg2; 29693912Slling nvpair_t *nvpair; 29703912Slling objset_t *mos = spa->spa_meta_objset; 29713912Slling uint64_t zapobj; 29724451Seschrock uint64_t intval; 29733912Slling 29743912Slling mutex_enter(&spa->spa_props_lock); 29753912Slling if (spa->spa_pool_props_object == 0) { 29763912Slling zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 29773912Slling VERIFY(zapobj > 0); 29783912Slling 29793912Slling spa->spa_pool_props_object = zapobj; 29803912Slling 29813912Slling VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 29823912Slling DMU_POOL_PROPS, 8, 1, 29833912Slling &spa->spa_pool_props_object, tx) == 0); 29843912Slling } 29853912Slling mutex_exit(&spa->spa_props_lock); 29863912Slling 29873912Slling nvpair = NULL; 29883912Slling while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 29893912Slling switch (zpool_name_to_prop(nvpair_name(nvpair))) { 29904543Smarks case ZPOOL_PROP_DELEGATION: 29914543Smarks VERIFY(nvlist_lookup_uint64(nvp, 29924543Smarks nvpair_name(nvpair), &intval) == 0); 29934543Smarks VERIFY(zap_update(mos, 29944543Smarks spa->spa_pool_props_object, 29954543Smarks nvpair_name(nvpair), 8, 1, 29964543Smarks &intval, tx) == 0); 29974543Smarks spa->spa_delegation = intval; 29984543Smarks break; 29994451Seschrock case ZPOOL_PROP_BOOTFS: 30003912Slling VERIFY(nvlist_lookup_uint64(nvp, 30013912Slling nvpair_name(nvpair), &spa->spa_bootfs) == 0); 30024543Smarks intval = spa->spa_bootfs; 30033912Slling VERIFY(zap_update(mos, 30043912Slling spa->spa_pool_props_object, 30054451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 8, 1, 30064543Smarks &intval, tx) == 0); 30073912Slling break; 30084451Seschrock 30094451Seschrock case ZPOOL_PROP_AUTOREPLACE: 30104451Seschrock VERIFY(nvlist_lookup_uint64(nvp, 30114451Seschrock nvpair_name(nvpair), &intval) == 0); 30124451Seschrock VERIFY(zap_update(mos, 30134451Seschrock spa->spa_pool_props_object, 30144451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 8, 1, 30154451Seschrock &intval, tx) == 0); 30164451Seschrock break; 30173912Slling } 30184543Smarks spa_history_internal_log(LOG_POOL_PROPSET, 30194543Smarks spa, tx, cr, "%s %lld %s", 30204543Smarks nvpair_name(nvpair), intval, 30214543Smarks spa->spa_name); 30223912Slling } 30233912Slling } 30243912Slling 3025789Sahrens /* 3026789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3027789Sahrens * part of the process, so we iterate until it converges. 3028789Sahrens */ 3029789Sahrens void 3030789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3031789Sahrens { 3032789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3033789Sahrens objset_t *mos = spa->spa_meta_objset; 3034789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 30351635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3036789Sahrens vdev_t *vd; 3037789Sahrens dmu_tx_t *tx; 3038789Sahrens int dirty_vdevs; 3039789Sahrens 3040789Sahrens /* 3041789Sahrens * Lock out configuration changes. 3042789Sahrens */ 30431544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3044789Sahrens 3045789Sahrens spa->spa_syncing_txg = txg; 3046789Sahrens spa->spa_sync_pass = 0; 3047789Sahrens 30481544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3049789Sahrens 30502082Seschrock tx = dmu_tx_create_assigned(dp, txg); 30512082Seschrock 30522082Seschrock /* 30534577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 30542082Seschrock * set spa_deflate if we have no raid-z vdevs. 30552082Seschrock */ 30564577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 30574577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 30582082Seschrock int i; 30592082Seschrock 30602082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 30612082Seschrock vd = rvd->vdev_child[i]; 30622082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 30632082Seschrock break; 30642082Seschrock } 30652082Seschrock if (i == rvd->vdev_children) { 30662082Seschrock spa->spa_deflate = TRUE; 30672082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 30682082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 30692082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 30702082Seschrock } 30712082Seschrock } 30722082Seschrock 3073789Sahrens /* 3074789Sahrens * If anything has changed in this txg, push the deferred frees 3075789Sahrens * from the previous txg. If not, leave them alone so that we 3076789Sahrens * don't generate work on an otherwise idle system. 3077789Sahrens */ 3078789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 30792329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 30802329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3081789Sahrens spa_sync_deferred_frees(spa, txg); 3082789Sahrens 3083789Sahrens /* 3084789Sahrens * Iterate to convergence. 3085789Sahrens */ 3086789Sahrens do { 3087789Sahrens spa->spa_sync_pass++; 3088789Sahrens 3089789Sahrens spa_sync_config_object(spa, tx); 30902082Seschrock spa_sync_spares(spa, tx); 30911544Seschrock spa_errlog_sync(spa, txg); 3092789Sahrens dsl_pool_sync(dp, txg); 3093789Sahrens 3094789Sahrens dirty_vdevs = 0; 3095789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3096789Sahrens vdev_sync(vd, txg); 3097789Sahrens dirty_vdevs++; 3098789Sahrens } 3099789Sahrens 3100789Sahrens bplist_sync(bpl, tx); 3101789Sahrens } while (dirty_vdevs); 3102789Sahrens 3103789Sahrens bplist_close(bpl); 3104789Sahrens 3105789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3106789Sahrens 3107789Sahrens /* 3108789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3109789Sahrens * to commit the transaction group. 31101635Sbonwick * 31111635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 31121635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 31131635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 31141635Sbonwick * If the write fails, try the next vdev until we're tried them all. 3115789Sahrens */ 31161635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 31171635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31181635Sbonwick } else { 31191635Sbonwick int children = rvd->vdev_children; 31201635Sbonwick int c0 = spa_get_random(children); 31211635Sbonwick int c; 31221635Sbonwick 31231635Sbonwick for (c = 0; c < children; c++) { 31241635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 31251635Sbonwick if (vd->vdev_ms_array == 0) 31261635Sbonwick continue; 31271635Sbonwick if (vdev_config_sync(vd, txg) == 0) 31281635Sbonwick break; 31291635Sbonwick } 31301635Sbonwick if (c == children) 31311635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31321635Sbonwick } 31331635Sbonwick 31342082Seschrock dmu_tx_commit(tx); 31352082Seschrock 31361635Sbonwick /* 31371635Sbonwick * Clear the dirty config list. 31381635Sbonwick */ 31391635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 31401635Sbonwick vdev_config_clean(vd); 31411635Sbonwick 31421635Sbonwick /* 31431635Sbonwick * Now that the new config has synced transactionally, 31441635Sbonwick * let it become visible to the config cache. 31451635Sbonwick */ 31461635Sbonwick if (spa->spa_config_syncing != NULL) { 31471635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 31481635Sbonwick spa->spa_config_txg = txg; 31491635Sbonwick spa->spa_config_syncing = NULL; 31501635Sbonwick } 3151789Sahrens 3152789Sahrens /* 3153789Sahrens * Make a stable copy of the fully synced uberblock. 3154789Sahrens * We use this as the root for pool traversals. 3155789Sahrens */ 3156789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3157789Sahrens 3158789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3159789Sahrens 3160789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3161789Sahrens spa->spa_traverse_wanted = 0; 3162789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3163789Sahrens rw_exit(&spa->spa_traverse_lock); 3164789Sahrens 3165789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3166789Sahrens 3167789Sahrens /* 3168789Sahrens * Clean up the ZIL records for the synced txg. 3169789Sahrens */ 3170789Sahrens dsl_pool_zil_clean(dp); 3171789Sahrens 3172789Sahrens /* 3173789Sahrens * Update usable space statistics. 3174789Sahrens */ 3175789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3176789Sahrens vdev_sync_done(vd, txg); 3177789Sahrens 3178789Sahrens /* 3179789Sahrens * It had better be the case that we didn't dirty anything 31802082Seschrock * since vdev_config_sync(). 3181789Sahrens */ 3182789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3183789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3184789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3185789Sahrens ASSERT(bpl->bpl_queue == NULL); 3186789Sahrens 31871544Seschrock spa_config_exit(spa, FTAG); 31881544Seschrock 31891544Seschrock /* 31901544Seschrock * If any async tasks have been requested, kick them off. 31911544Seschrock */ 31921544Seschrock spa_async_dispatch(spa); 3193789Sahrens } 3194789Sahrens 3195789Sahrens /* 3196789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3197789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3198789Sahrens * sync. 3199789Sahrens */ 3200789Sahrens void 3201789Sahrens spa_sync_allpools(void) 3202789Sahrens { 3203789Sahrens spa_t *spa = NULL; 3204789Sahrens mutex_enter(&spa_namespace_lock); 3205789Sahrens while ((spa = spa_next(spa)) != NULL) { 3206789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3207789Sahrens continue; 3208789Sahrens spa_open_ref(spa, FTAG); 3209789Sahrens mutex_exit(&spa_namespace_lock); 3210789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3211789Sahrens mutex_enter(&spa_namespace_lock); 3212789Sahrens spa_close(spa, FTAG); 3213789Sahrens } 3214789Sahrens mutex_exit(&spa_namespace_lock); 3215789Sahrens } 3216789Sahrens 3217789Sahrens /* 3218789Sahrens * ========================================================================== 3219789Sahrens * Miscellaneous routines 3220789Sahrens * ========================================================================== 3221789Sahrens */ 3222789Sahrens 3223789Sahrens /* 3224789Sahrens * Remove all pools in the system. 3225789Sahrens */ 3226789Sahrens void 3227789Sahrens spa_evict_all(void) 3228789Sahrens { 3229789Sahrens spa_t *spa; 3230789Sahrens 3231789Sahrens /* 3232789Sahrens * Remove all cached state. All pools should be closed now, 3233789Sahrens * so every spa in the AVL tree should be unreferenced. 3234789Sahrens */ 3235789Sahrens mutex_enter(&spa_namespace_lock); 3236789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3237789Sahrens /* 32381544Seschrock * Stop async tasks. The async thread may need to detach 32391544Seschrock * a device that's been replaced, which requires grabbing 32401544Seschrock * spa_namespace_lock, so we must drop it here. 3241789Sahrens */ 3242789Sahrens spa_open_ref(spa, FTAG); 3243789Sahrens mutex_exit(&spa_namespace_lock); 32441544Seschrock spa_async_suspend(spa); 3245789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3246789Sahrens mutex_enter(&spa_namespace_lock); 3247789Sahrens spa_close(spa, FTAG); 3248789Sahrens 3249789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3250789Sahrens spa_unload(spa); 3251789Sahrens spa_deactivate(spa); 3252789Sahrens } 3253789Sahrens spa_remove(spa); 3254789Sahrens } 3255789Sahrens mutex_exit(&spa_namespace_lock); 3256789Sahrens } 32571544Seschrock 32581544Seschrock vdev_t * 32591544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 32601544Seschrock { 32611544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 32621544Seschrock } 32631760Seschrock 32641760Seschrock void 32651760Seschrock spa_upgrade(spa_t *spa) 32661760Seschrock { 32671760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 32681760Seschrock 32691760Seschrock /* 32701760Seschrock * This should only be called for a non-faulted pool, and since a 32711760Seschrock * future version would result in an unopenable pool, this shouldn't be 32721760Seschrock * possible. 32731760Seschrock */ 32744577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 32754577Sahrens 32764577Sahrens spa->spa_uberblock.ub_version = SPA_VERSION; 32771760Seschrock vdev_config_dirty(spa->spa_root_vdev); 32781760Seschrock 32791760Seschrock spa_config_exit(spa, FTAG); 32802082Seschrock 32812082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 32821760Seschrock } 32832082Seschrock 32842082Seschrock boolean_t 32852082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 32862082Seschrock { 32872082Seschrock int i; 32883377Seschrock uint64_t spareguid; 32892082Seschrock 32902082Seschrock for (i = 0; i < spa->spa_nspares; i++) 32912082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 32922082Seschrock return (B_TRUE); 32932082Seschrock 32943377Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 32953377Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 32963377Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 32973377Seschrock spareguid == guid) 32983377Seschrock return (B_TRUE); 32993377Seschrock } 33003377Seschrock 33012082Seschrock return (B_FALSE); 33022082Seschrock } 33033912Slling 33043912Slling int 33053912Slling spa_set_props(spa_t *spa, nvlist_t *nvp) 33063912Slling { 33073912Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 33083912Slling spa, nvp, 3)); 33093912Slling } 33103912Slling 33113912Slling int 33123912Slling spa_get_props(spa_t *spa, nvlist_t **nvp) 33133912Slling { 33143912Slling zap_cursor_t zc; 33153912Slling zap_attribute_t za; 33163912Slling objset_t *mos = spa->spa_meta_objset; 33173912Slling zfs_source_t src; 33184451Seschrock zpool_prop_t prop; 33193912Slling nvlist_t *propval; 33203912Slling uint64_t value; 33213912Slling int err; 33223912Slling 33233912Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33243912Slling 33253912Slling mutex_enter(&spa->spa_props_lock); 33263912Slling /* If no props object, then just return empty nvlist */ 33273912Slling if (spa->spa_pool_props_object == 0) { 33283912Slling mutex_exit(&spa->spa_props_lock); 33293912Slling return (0); 33303912Slling } 33313912Slling 33323912Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 33333912Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 33343912Slling zap_cursor_advance(&zc)) { 33353912Slling 33363912Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 33373912Slling continue; 33383912Slling 33393912Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33403912Slling switch (za.za_integer_length) { 33413912Slling case 8: 33424451Seschrock if (zpool_prop_default_numeric(prop) == 33433912Slling za.za_first_integer) 33443912Slling src = ZFS_SRC_DEFAULT; 33453912Slling else 33463912Slling src = ZFS_SRC_LOCAL; 33473912Slling value = za.za_first_integer; 33483912Slling 33494451Seschrock if (prop == ZPOOL_PROP_BOOTFS) { 33503912Slling dsl_pool_t *dp; 33513912Slling dsl_dataset_t *ds = NULL; 33523912Slling char strval[MAXPATHLEN]; 33533912Slling 33543912Slling dp = spa_get_dsl(spa); 33553912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 33563912Slling if ((err = dsl_dataset_open_obj(dp, 33573912Slling za.za_first_integer, NULL, DS_MODE_NONE, 33583912Slling FTAG, &ds)) != 0) { 33593912Slling rw_exit(&dp->dp_config_rwlock); 33603912Slling break; 33613912Slling } 33623912Slling dsl_dataset_name(ds, strval); 33633912Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 33643912Slling rw_exit(&dp->dp_config_rwlock); 33653912Slling 33663912Slling VERIFY(nvlist_add_uint64(propval, 33673912Slling ZFS_PROP_SOURCE, src) == 0); 33683912Slling VERIFY(nvlist_add_string(propval, 33693912Slling ZFS_PROP_VALUE, strval) == 0); 33703912Slling } else { 33713912Slling VERIFY(nvlist_add_uint64(propval, 33723912Slling ZFS_PROP_SOURCE, src) == 0); 33733912Slling VERIFY(nvlist_add_uint64(propval, 33743912Slling ZFS_PROP_VALUE, value) == 0); 33753912Slling } 33763912Slling VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 33773912Slling propval) == 0); 33783912Slling break; 33793912Slling } 33803912Slling nvlist_free(propval); 33813912Slling } 33823912Slling zap_cursor_fini(&zc); 33833912Slling mutex_exit(&spa->spa_props_lock); 33843912Slling if (err && err != ENOENT) { 33853912Slling nvlist_free(*nvp); 33863912Slling return (err); 33873912Slling } 33883912Slling 33893912Slling return (0); 33903912Slling } 33913912Slling 33923912Slling /* 33933912Slling * If the bootfs property value is dsobj, clear it. 33943912Slling */ 33953912Slling void 33963912Slling spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 33973912Slling { 33983912Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 33993912Slling VERIFY(zap_remove(spa->spa_meta_objset, 34003912Slling spa->spa_pool_props_object, 34014451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 34023912Slling spa->spa_bootfs = 0; 34033912Slling } 34043912Slling } 34054451Seschrock 34064451Seschrock /* 34074451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 34084451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 34094451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 34104451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 34114451Seschrock * or zdb as real changes. 34124451Seschrock */ 34134451Seschrock void 34144451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 34154451Seschrock { 34164451Seschrock #ifdef _KERNEL 34174451Seschrock sysevent_t *ev; 34184451Seschrock sysevent_attr_list_t *attr = NULL; 34194451Seschrock sysevent_value_t value; 34204451Seschrock sysevent_id_t eid; 34214451Seschrock 34224451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 34234451Seschrock SE_SLEEP); 34244451Seschrock 34254451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34264451Seschrock value.value.sv_string = spa_name(spa); 34274451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 34284451Seschrock goto done; 34294451Seschrock 34304451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34314451Seschrock value.value.sv_uint64 = spa_guid(spa); 34324451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 34334451Seschrock goto done; 34344451Seschrock 34354451Seschrock if (vd) { 34364451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34374451Seschrock value.value.sv_uint64 = vd->vdev_guid; 34384451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 34394451Seschrock SE_SLEEP) != 0) 34404451Seschrock goto done; 34414451Seschrock 34424451Seschrock if (vd->vdev_path) { 34434451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34444451Seschrock value.value.sv_string = vd->vdev_path; 34454451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 34464451Seschrock &value, SE_SLEEP) != 0) 34474451Seschrock goto done; 34484451Seschrock } 34494451Seschrock } 34504451Seschrock 34514451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 34524451Seschrock 34534451Seschrock done: 34544451Seschrock if (attr) 34554451Seschrock sysevent_free_attr(attr); 34564451Seschrock sysevent_free(ev); 34574451Seschrock #endif 34584451Seschrock } 3459