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 1140*4715Sek110237 spa_create(const char *pool, nvlist_t *nvroot, const char *altroot, 1141*4715Sek110237 const char *history_str) 1142789Sahrens { 1143789Sahrens spa_t *spa; 11441635Sbonwick vdev_t *rvd; 1145789Sahrens dsl_pool_t *dp; 1146789Sahrens dmu_tx_t *tx; 11472082Seschrock int c, error = 0; 1148789Sahrens uint64_t txg = TXG_INITIAL; 11492082Seschrock nvlist_t **spares; 11502082Seschrock uint_t nspares; 1151789Sahrens 1152789Sahrens /* 1153789Sahrens * If this pool already exists, return failure. 1154789Sahrens */ 1155789Sahrens mutex_enter(&spa_namespace_lock); 1156789Sahrens if (spa_lookup(pool) != NULL) { 1157789Sahrens mutex_exit(&spa_namespace_lock); 1158789Sahrens return (EEXIST); 1159789Sahrens } 1160789Sahrens 1161789Sahrens /* 1162789Sahrens * Allocate a new spa_t structure. 1163789Sahrens */ 11641635Sbonwick spa = spa_add(pool, altroot); 1165789Sahrens spa_activate(spa); 1166789Sahrens 1167789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 11684577Sahrens spa->spa_uberblock.ub_version = SPA_VERSION; 1169789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1170789Sahrens 11711635Sbonwick /* 11721635Sbonwick * Create the root vdev. 11731635Sbonwick */ 11741635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 11751635Sbonwick 11762082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 11772082Seschrock 11782082Seschrock ASSERT(error != 0 || rvd != NULL); 11792082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 11802082Seschrock 11812082Seschrock if (error == 0 && rvd->vdev_children == 0) 11821635Sbonwick error = EINVAL; 11832082Seschrock 11842082Seschrock if (error == 0 && 11852082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 11862082Seschrock (error = spa_validate_spares(spa, nvroot, txg, 11872082Seschrock VDEV_ALLOC_ADD)) == 0) { 11882082Seschrock for (c = 0; c < rvd->vdev_children; c++) 11892082Seschrock vdev_init(rvd->vdev_child[c], txg); 11902082Seschrock vdev_config_dirty(rvd); 11911635Sbonwick } 11921635Sbonwick 11931635Sbonwick spa_config_exit(spa, FTAG); 1194789Sahrens 11952082Seschrock if (error != 0) { 1196789Sahrens spa_unload(spa); 1197789Sahrens spa_deactivate(spa); 1198789Sahrens spa_remove(spa); 1199789Sahrens mutex_exit(&spa_namespace_lock); 1200789Sahrens return (error); 1201789Sahrens } 1202789Sahrens 12032082Seschrock /* 12042082Seschrock * Get the list of spares, if specified. 12052082Seschrock */ 12062082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 12072082Seschrock &spares, &nspares) == 0) { 12082082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 12092082Seschrock KM_SLEEP) == 0); 12102082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 12112082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 12122082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12132082Seschrock spa_load_spares(spa); 12142082Seschrock spa_config_exit(spa, FTAG); 12152082Seschrock spa->spa_sync_spares = B_TRUE; 12162082Seschrock } 12172082Seschrock 1218789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1219789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1220789Sahrens 1221789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1222789Sahrens 1223789Sahrens /* 1224789Sahrens * Create the pool config object. 1225789Sahrens */ 1226789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1227789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1228789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1229789Sahrens 12301544Seschrock if (zap_add(spa->spa_meta_objset, 1231789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 12321544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 12331544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 12341544Seschrock } 1235789Sahrens 12362082Seschrock /* Newly created pools are always deflated. */ 12372082Seschrock spa->spa_deflate = TRUE; 12382082Seschrock if (zap_add(spa->spa_meta_objset, 12392082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12402082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 12412082Seschrock cmn_err(CE_PANIC, "failed to add deflate"); 12422082Seschrock } 12432082Seschrock 1244789Sahrens /* 1245789Sahrens * Create the deferred-free bplist object. Turn off compression 1246789Sahrens * because sync-to-convergence takes longer if the blocksize 1247789Sahrens * keeps changing. 1248789Sahrens */ 1249789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1250789Sahrens 1 << 14, tx); 1251789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1252789Sahrens ZIO_COMPRESS_OFF, tx); 1253789Sahrens 12541544Seschrock if (zap_add(spa->spa_meta_objset, 1255789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12561544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 12571544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 12581544Seschrock } 1259789Sahrens 12602926Sek110237 /* 12612926Sek110237 * Create the pool's history object. 12622926Sek110237 */ 12632926Sek110237 spa_history_create_obj(spa, tx); 12642926Sek110237 1265789Sahrens dmu_tx_commit(tx); 1266789Sahrens 12674451Seschrock spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 12684543Smarks spa->spa_delegation = zfs_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1269789Sahrens spa->spa_sync_on = B_TRUE; 1270789Sahrens txg_sync_start(spa->spa_dsl_pool); 1271789Sahrens 1272789Sahrens /* 1273789Sahrens * We explicitly wait for the first transaction to complete so that our 1274789Sahrens * bean counters are appropriately updated. 1275789Sahrens */ 1276789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1277789Sahrens 1278789Sahrens spa_config_sync(); 1279789Sahrens 1280*4715Sek110237 if (history_str != NULL) 1281*4715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 1282*4715Sek110237 1283789Sahrens mutex_exit(&spa_namespace_lock); 1284789Sahrens 1285789Sahrens return (0); 1286789Sahrens } 1287789Sahrens 1288789Sahrens /* 1289789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1290789Sahrens * then call spa_load() to do the dirty work. 1291789Sahrens */ 1292789Sahrens int 12931635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 1294789Sahrens { 1295789Sahrens spa_t *spa; 1296789Sahrens int error; 12972082Seschrock nvlist_t *nvroot; 12982082Seschrock nvlist_t **spares; 12992082Seschrock uint_t nspares; 1300789Sahrens 1301789Sahrens /* 1302789Sahrens * If a pool with this name exists, return failure. 1303789Sahrens */ 1304789Sahrens mutex_enter(&spa_namespace_lock); 1305789Sahrens if (spa_lookup(pool) != NULL) { 1306789Sahrens mutex_exit(&spa_namespace_lock); 1307789Sahrens return (EEXIST); 1308789Sahrens } 1309789Sahrens 1310789Sahrens /* 13111635Sbonwick * Create and initialize the spa structure. 1312789Sahrens */ 13131635Sbonwick spa = spa_add(pool, altroot); 1314789Sahrens spa_activate(spa); 1315789Sahrens 1316789Sahrens /* 13171635Sbonwick * Pass off the heavy lifting to spa_load(). 13181732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 13191732Sbonwick * is actually the one to trust when doing an import. 13201601Sbonwick */ 13211732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1322789Sahrens 13232082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13242082Seschrock /* 13252082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 13262082Seschrock * and conflicts with spa_has_spare(). 13272082Seschrock */ 13282082Seschrock if (spa->spa_sparelist) { 13292082Seschrock nvlist_free(spa->spa_sparelist); 13302082Seschrock spa->spa_sparelist = NULL; 13312082Seschrock spa_load_spares(spa); 13322082Seschrock } 13332082Seschrock 13342082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 13352082Seschrock &nvroot) == 0); 13362082Seschrock if (error == 0) 13372082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 13382082Seschrock VDEV_ALLOC_SPARE); 13392082Seschrock spa_config_exit(spa, FTAG); 13402082Seschrock 13412082Seschrock if (error != 0) { 1342789Sahrens spa_unload(spa); 1343789Sahrens spa_deactivate(spa); 1344789Sahrens spa_remove(spa); 1345789Sahrens mutex_exit(&spa_namespace_lock); 1346789Sahrens return (error); 1347789Sahrens } 1348789Sahrens 13491635Sbonwick /* 13502082Seschrock * Override any spares as specified by the user, as these may have 13512082Seschrock * correct device names/devids, etc. 13522082Seschrock */ 13532082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 13542082Seschrock &spares, &nspares) == 0) { 13552082Seschrock if (spa->spa_sparelist) 13562082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 13572082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 13582082Seschrock else 13592082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 13602082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 13612082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 13622082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 13632082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13642082Seschrock spa_load_spares(spa); 13652082Seschrock spa_config_exit(spa, FTAG); 13662082Seschrock spa->spa_sync_spares = B_TRUE; 13672082Seschrock } 13682082Seschrock 13692082Seschrock /* 13701635Sbonwick * Update the config cache to include the newly-imported pool. 13711635Sbonwick */ 13724627Sck153898 if (spa_mode & FWRITE) 13734627Sck153898 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 13741635Sbonwick 1375789Sahrens /* 1376789Sahrens * Resilver anything that's out of date. 1377789Sahrens */ 1378789Sahrens if (spa_mode & FWRITE) 1379789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1380789Sahrens 13814451Seschrock mutex_exit(&spa_namespace_lock); 13824451Seschrock 1383789Sahrens return (0); 1384789Sahrens } 1385789Sahrens 1386789Sahrens /* 1387789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1388789Sahrens * to get the vdev stats associated with the imported devices. 1389789Sahrens */ 1390789Sahrens #define TRYIMPORT_NAME "$import" 1391789Sahrens 1392789Sahrens nvlist_t * 1393789Sahrens spa_tryimport(nvlist_t *tryconfig) 1394789Sahrens { 1395789Sahrens nvlist_t *config = NULL; 1396789Sahrens char *poolname; 1397789Sahrens spa_t *spa; 1398789Sahrens uint64_t state; 1399789Sahrens 1400789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1401789Sahrens return (NULL); 1402789Sahrens 1403789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1404789Sahrens return (NULL); 1405789Sahrens 14061635Sbonwick /* 14071635Sbonwick * Create and initialize the spa structure. 14081635Sbonwick */ 1409789Sahrens mutex_enter(&spa_namespace_lock); 14101635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1411789Sahrens spa_activate(spa); 1412789Sahrens 1413789Sahrens /* 14141635Sbonwick * Pass off the heavy lifting to spa_load(). 14151732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 14161732Sbonwick * is actually the one to trust when doing an import. 1417789Sahrens */ 14181732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1419789Sahrens 1420789Sahrens /* 1421789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1422789Sahrens */ 1423789Sahrens if (spa->spa_root_vdev != NULL) { 14241635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1425789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14261635Sbonwick spa_config_exit(spa, FTAG); 1427789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1428789Sahrens poolname) == 0); 1429789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1430789Sahrens state) == 0); 14313975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 14323975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 14332082Seschrock 14342082Seschrock /* 14352082Seschrock * Add the list of hot spares. 14362082Seschrock */ 14372082Seschrock spa_add_spares(spa, config); 1438789Sahrens } 1439789Sahrens 1440789Sahrens spa_unload(spa); 1441789Sahrens spa_deactivate(spa); 1442789Sahrens spa_remove(spa); 1443789Sahrens mutex_exit(&spa_namespace_lock); 1444789Sahrens 1445789Sahrens return (config); 1446789Sahrens } 1447789Sahrens 1448789Sahrens /* 1449789Sahrens * Pool export/destroy 1450789Sahrens * 1451789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1452789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1453789Sahrens * update the pool state and sync all the labels to disk, removing the 1454789Sahrens * configuration from the cache afterwards. 1455789Sahrens */ 1456789Sahrens static int 14571775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1458789Sahrens { 1459789Sahrens spa_t *spa; 1460789Sahrens 14611775Sbillm if (oldconfig) 14621775Sbillm *oldconfig = NULL; 14631775Sbillm 1464789Sahrens if (!(spa_mode & FWRITE)) 1465789Sahrens return (EROFS); 1466789Sahrens 1467789Sahrens mutex_enter(&spa_namespace_lock); 1468789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1469789Sahrens mutex_exit(&spa_namespace_lock); 1470789Sahrens return (ENOENT); 1471789Sahrens } 1472789Sahrens 1473789Sahrens /* 14741544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 14751544Seschrock * reacquire the namespace lock, and see if we can export. 14761544Seschrock */ 14771544Seschrock spa_open_ref(spa, FTAG); 14781544Seschrock mutex_exit(&spa_namespace_lock); 14791544Seschrock spa_async_suspend(spa); 14801544Seschrock mutex_enter(&spa_namespace_lock); 14811544Seschrock spa_close(spa, FTAG); 14821544Seschrock 14831544Seschrock /* 1484789Sahrens * The pool will be in core if it's openable, 1485789Sahrens * in which case we can modify its state. 1486789Sahrens */ 1487789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1488789Sahrens /* 1489789Sahrens * Objsets may be open only because they're dirty, so we 1490789Sahrens * have to force it to sync before checking spa_refcnt. 1491789Sahrens */ 1492789Sahrens spa_scrub_suspend(spa); 1493789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1494789Sahrens 14951544Seschrock /* 14961544Seschrock * A pool cannot be exported or destroyed if there are active 14971544Seschrock * references. If we are resetting a pool, allow references by 14981544Seschrock * fault injection handlers. 14991544Seschrock */ 15001544Seschrock if (!spa_refcount_zero(spa) || 15011544Seschrock (spa->spa_inject_ref != 0 && 15021544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1503789Sahrens spa_scrub_resume(spa); 15041544Seschrock spa_async_resume(spa); 1505789Sahrens mutex_exit(&spa_namespace_lock); 1506789Sahrens return (EBUSY); 1507789Sahrens } 1508789Sahrens 1509789Sahrens spa_scrub_resume(spa); 1510789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1511789Sahrens 1512789Sahrens /* 1513789Sahrens * We want this to be reflected on every label, 1514789Sahrens * so mark them all dirty. spa_unload() will do the 1515789Sahrens * final sync that pushes these changes out. 1516789Sahrens */ 15171544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15181601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15191544Seschrock spa->spa_state = new_state; 15201635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 15211544Seschrock vdev_config_dirty(spa->spa_root_vdev); 15221601Sbonwick spa_config_exit(spa, FTAG); 15231544Seschrock } 1524789Sahrens } 1525789Sahrens 15264451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 15274451Seschrock 1528789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1529789Sahrens spa_unload(spa); 1530789Sahrens spa_deactivate(spa); 1531789Sahrens } 1532789Sahrens 15331775Sbillm if (oldconfig && spa->spa_config) 15341775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 15351775Sbillm 15361544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15371544Seschrock spa_remove(spa); 15381544Seschrock spa_config_sync(); 15391544Seschrock } 1540789Sahrens mutex_exit(&spa_namespace_lock); 1541789Sahrens 1542789Sahrens return (0); 1543789Sahrens } 1544789Sahrens 1545789Sahrens /* 1546789Sahrens * Destroy a storage pool. 1547789Sahrens */ 1548789Sahrens int 1549789Sahrens spa_destroy(char *pool) 1550789Sahrens { 15511775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1552789Sahrens } 1553789Sahrens 1554789Sahrens /* 1555789Sahrens * Export a storage pool. 1556789Sahrens */ 1557789Sahrens int 15581775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1559789Sahrens { 15601775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1561789Sahrens } 1562789Sahrens 1563789Sahrens /* 15641544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 15651544Seschrock * from the namespace in any way. 15661544Seschrock */ 15671544Seschrock int 15681544Seschrock spa_reset(char *pool) 15691544Seschrock { 15701775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 15711544Seschrock } 15721544Seschrock 15731544Seschrock 15741544Seschrock /* 1575789Sahrens * ========================================================================== 1576789Sahrens * Device manipulation 1577789Sahrens * ========================================================================== 1578789Sahrens */ 1579789Sahrens 1580789Sahrens /* 15814527Sperrin * Add a device to a storage pool. 1582789Sahrens */ 1583789Sahrens int 1584789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1585789Sahrens { 1586789Sahrens uint64_t txg; 15871635Sbonwick int c, error; 1588789Sahrens vdev_t *rvd = spa->spa_root_vdev; 15891585Sbonwick vdev_t *vd, *tvd; 15902082Seschrock nvlist_t **spares; 15912082Seschrock uint_t i, nspares; 1592789Sahrens 1593789Sahrens txg = spa_vdev_enter(spa); 1594789Sahrens 15952082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 15962082Seschrock VDEV_ALLOC_ADD)) != 0) 15972082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 15982082Seschrock 15993377Seschrock spa->spa_pending_vdev = vd; 1600789Sahrens 16012082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 16022082Seschrock &spares, &nspares) != 0) 16032082Seschrock nspares = 0; 16042082Seschrock 16053377Seschrock if (vd->vdev_children == 0 && nspares == 0) { 16063377Seschrock spa->spa_pending_vdev = NULL; 16072082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 16083377Seschrock } 16092082Seschrock 16102082Seschrock if (vd->vdev_children != 0) { 16113377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 16123377Seschrock spa->spa_pending_vdev = NULL; 16132082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16142082Seschrock } 16152082Seschrock } 16162082Seschrock 16173377Seschrock /* 16183377Seschrock * We must validate the spares after checking the children. Otherwise, 16193377Seschrock * vdev_inuse() will blindly overwrite the spare. 16203377Seschrock */ 16213377Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 16223377Seschrock VDEV_ALLOC_ADD)) != 0) { 16233377Seschrock spa->spa_pending_vdev = NULL; 16243377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16253377Seschrock } 16263377Seschrock 16273377Seschrock spa->spa_pending_vdev = NULL; 16283377Seschrock 16293377Seschrock /* 16303377Seschrock * Transfer each new top-level vdev from vd to rvd. 16313377Seschrock */ 16323377Seschrock for (c = 0; c < vd->vdev_children; c++) { 16333377Seschrock tvd = vd->vdev_child[c]; 16343377Seschrock vdev_remove_child(vd, tvd); 16353377Seschrock tvd->vdev_id = rvd->vdev_children; 16363377Seschrock vdev_add_child(rvd, tvd); 16373377Seschrock vdev_config_dirty(tvd); 16383377Seschrock } 16393377Seschrock 16402082Seschrock if (nspares != 0) { 16412082Seschrock if (spa->spa_sparelist != NULL) { 16422082Seschrock nvlist_t **oldspares; 16432082Seschrock uint_t oldnspares; 16442082Seschrock nvlist_t **newspares; 16452082Seschrock 16462082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 16472082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 16482082Seschrock 16492082Seschrock newspares = kmem_alloc(sizeof (void *) * 16502082Seschrock (nspares + oldnspares), KM_SLEEP); 16512082Seschrock for (i = 0; i < oldnspares; i++) 16522082Seschrock VERIFY(nvlist_dup(oldspares[i], 16532082Seschrock &newspares[i], KM_SLEEP) == 0); 16542082Seschrock for (i = 0; i < nspares; i++) 16552082Seschrock VERIFY(nvlist_dup(spares[i], 16562082Seschrock &newspares[i + oldnspares], 16572082Seschrock KM_SLEEP) == 0); 16582082Seschrock 16592082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 16602082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 16612082Seschrock 16622082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16632082Seschrock ZPOOL_CONFIG_SPARES, newspares, 16642082Seschrock nspares + oldnspares) == 0); 16652082Seschrock for (i = 0; i < oldnspares + nspares; i++) 16662082Seschrock nvlist_free(newspares[i]); 16672082Seschrock kmem_free(newspares, (oldnspares + nspares) * 16682082Seschrock sizeof (void *)); 16692082Seschrock } else { 16702082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 16712082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 16722082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16732082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16742082Seschrock } 16752082Seschrock 16762082Seschrock spa_load_spares(spa); 16772082Seschrock spa->spa_sync_spares = B_TRUE; 1678789Sahrens } 1679789Sahrens 1680789Sahrens /* 16811585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 16821585Sbonwick * If other threads start allocating from these vdevs before we 16831585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 16841585Sbonwick * fail to open the pool because there are DVAs that the config cache 16851585Sbonwick * can't translate. Therefore, we first add the vdevs without 16861585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 16871635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 16881585Sbonwick * 16891585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 16901585Sbonwick * if we lose power at any point in this sequence, the remaining 16911585Sbonwick * steps will be completed the next time we load the pool. 1692789Sahrens */ 16931635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 16941585Sbonwick 16951635Sbonwick mutex_enter(&spa_namespace_lock); 16961635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 16971635Sbonwick mutex_exit(&spa_namespace_lock); 1698789Sahrens 16991635Sbonwick return (0); 1700789Sahrens } 1701789Sahrens 1702789Sahrens /* 1703789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1704789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1705789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1706789Sahrens * 1707789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1708789Sahrens * existing device; in this case the two devices are made into their own 17094451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 1710789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1711789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1712789Sahrens * completion of resilvering, the first disk (the one being replaced) 1713789Sahrens * is automatically detached. 1714789Sahrens */ 1715789Sahrens int 17161544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1717789Sahrens { 1718789Sahrens uint64_t txg, open_txg; 1719789Sahrens int error; 1720789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1721789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 17222082Seschrock vdev_ops_t *pvops; 17234527Sperrin int is_log; 1724789Sahrens 1725789Sahrens txg = spa_vdev_enter(spa); 1726789Sahrens 17271544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1728789Sahrens 1729789Sahrens if (oldvd == NULL) 1730789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1731789Sahrens 17321585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 17331585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17341585Sbonwick 1735789Sahrens pvd = oldvd->vdev_parent; 1736789Sahrens 17372082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 17384451Seschrock VDEV_ALLOC_ADD)) != 0) 17394451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 17404451Seschrock 17414451Seschrock if (newrootvd->vdev_children != 1) 1742789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1743789Sahrens 1744789Sahrens newvd = newrootvd->vdev_child[0]; 1745789Sahrens 1746789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1747789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1748789Sahrens 17492082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1750789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1751789Sahrens 17524527Sperrin /* 17534527Sperrin * Spares can't replace logs 17544527Sperrin */ 17554527Sperrin is_log = oldvd->vdev_islog; 17564527Sperrin if (is_log && newvd->vdev_isspare) 17574527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17584527Sperrin 17592082Seschrock if (!replacing) { 17602082Seschrock /* 17612082Seschrock * For attach, the only allowable parent is a mirror or the root 17622082Seschrock * vdev. 17632082Seschrock */ 17642082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 17652082Seschrock pvd->vdev_ops != &vdev_root_ops) 17662082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17672082Seschrock 17682082Seschrock pvops = &vdev_mirror_ops; 17692082Seschrock } else { 17702082Seschrock /* 17712082Seschrock * Active hot spares can only be replaced by inactive hot 17722082Seschrock * spares. 17732082Seschrock */ 17742082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 17752082Seschrock pvd->vdev_child[1] == oldvd && 17762082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 17772082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17782082Seschrock 17792082Seschrock /* 17802082Seschrock * If the source is a hot spare, and the parent isn't already a 17812082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 17823377Seschrock * want to create a replacing vdev. The user is not allowed to 17833377Seschrock * attach to a spared vdev child unless the 'isspare' state is 17843377Seschrock * the same (spare replaces spare, non-spare replaces 17853377Seschrock * non-spare). 17862082Seschrock */ 17872082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 17882082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17893377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 17903377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 17913377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17922082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 17932082Seschrock newvd->vdev_isspare) 17942082Seschrock pvops = &vdev_spare_ops; 17952082Seschrock else 17962082Seschrock pvops = &vdev_replacing_ops; 17972082Seschrock } 17982082Seschrock 17991175Slling /* 18001175Slling * Compare the new device size with the replaceable/attachable 18011175Slling * device size. 18021175Slling */ 18031175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1804789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1805789Sahrens 18061732Sbonwick /* 18071732Sbonwick * The new device cannot have a higher alignment requirement 18081732Sbonwick * than the top-level vdev. 18091732Sbonwick */ 18101732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1811789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1812789Sahrens 1813789Sahrens /* 1814789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1815789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1816789Sahrens */ 1817789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1818789Sahrens spa_strfree(oldvd->vdev_path); 1819789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1820789Sahrens KM_SLEEP); 1821789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1822789Sahrens newvd->vdev_path, "old"); 1823789Sahrens if (oldvd->vdev_devid != NULL) { 1824789Sahrens spa_strfree(oldvd->vdev_devid); 1825789Sahrens oldvd->vdev_devid = NULL; 1826789Sahrens } 1827789Sahrens } 1828789Sahrens 1829789Sahrens /* 18302082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 18312082Seschrock * mirror/replacing/spare vdev above oldvd. 1832789Sahrens */ 1833789Sahrens if (pvd->vdev_ops != pvops) 1834789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1835789Sahrens 1836789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1837789Sahrens ASSERT(pvd->vdev_ops == pvops); 1838789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1839789Sahrens 1840789Sahrens /* 1841789Sahrens * Extract the new device from its root and add it to pvd. 1842789Sahrens */ 1843789Sahrens vdev_remove_child(newrootvd, newvd); 1844789Sahrens newvd->vdev_id = pvd->vdev_children; 1845789Sahrens vdev_add_child(pvd, newvd); 1846789Sahrens 18471544Seschrock /* 18481544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 18491544Seschrock * the addition of newvd may have decreased our parent's asize. 18501544Seschrock */ 18511544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 18521544Seschrock 1853789Sahrens tvd = newvd->vdev_top; 1854789Sahrens ASSERT(pvd->vdev_top == tvd); 1855789Sahrens ASSERT(tvd->vdev_parent == rvd); 1856789Sahrens 1857789Sahrens vdev_config_dirty(tvd); 1858789Sahrens 1859789Sahrens /* 1860789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1861789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1862789Sahrens */ 1863789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1864789Sahrens 1865789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1866789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1867789Sahrens open_txg - TXG_INITIAL + 1); 1868789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1869789Sahrens 18703377Seschrock if (newvd->vdev_isspare) 18713377Seschrock spa_spare_activate(newvd); 18721544Seschrock 1873789Sahrens /* 1874789Sahrens * Mark newvd's DTL dirty in this txg. 1875789Sahrens */ 18761732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1877789Sahrens 1878789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1879789Sahrens 1880789Sahrens /* 18814451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 18824451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 1883789Sahrens */ 18844451Seschrock mutex_enter(&spa_namespace_lock); 1885789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 18864451Seschrock mutex_exit(&spa_namespace_lock); 1887789Sahrens 1888789Sahrens return (0); 1889789Sahrens } 1890789Sahrens 1891789Sahrens /* 1892789Sahrens * Detach a device from a mirror or replacing vdev. 1893789Sahrens * If 'replace_done' is specified, only detach if the parent 1894789Sahrens * is a replacing vdev. 1895789Sahrens */ 1896789Sahrens int 18971544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1898789Sahrens { 1899789Sahrens uint64_t txg; 1900789Sahrens int c, t, error; 1901789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1902789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 19032082Seschrock boolean_t unspare = B_FALSE; 19042082Seschrock uint64_t unspare_guid; 1905789Sahrens 1906789Sahrens txg = spa_vdev_enter(spa); 1907789Sahrens 19081544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1909789Sahrens 1910789Sahrens if (vd == NULL) 1911789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1912789Sahrens 19131585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 19141585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19151585Sbonwick 1916789Sahrens pvd = vd->vdev_parent; 1917789Sahrens 1918789Sahrens /* 1919789Sahrens * If replace_done is specified, only remove this device if it's 19202082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 19212082Seschrock * disk can be removed. 1922789Sahrens */ 19232082Seschrock if (replace_done) { 19242082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 19252082Seschrock if (vd->vdev_id != 0) 19262082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19272082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 19282082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19292082Seschrock } 19302082Seschrock } 19312082Seschrock 19322082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 19334577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 1934789Sahrens 1935789Sahrens /* 19362082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1937789Sahrens */ 1938789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 19392082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 19402082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1941789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1942789Sahrens 1943789Sahrens /* 1944789Sahrens * If there's only one replica, you can't detach it. 1945789Sahrens */ 1946789Sahrens if (pvd->vdev_children <= 1) 1947789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1948789Sahrens 1949789Sahrens /* 1950789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1951789Sahrens * valid copy of the data, which means we cannot safely detach it. 1952789Sahrens * 1953789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1954789Sahrens * precise DTL check. 1955789Sahrens */ 1956789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1957789Sahrens uint64_t dirty; 1958789Sahrens 1959789Sahrens cvd = pvd->vdev_child[c]; 1960789Sahrens if (cvd == vd) 1961789Sahrens continue; 1962789Sahrens if (vdev_is_dead(cvd)) 1963789Sahrens continue; 1964789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1965789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1966789Sahrens cvd->vdev_dtl_scrub.sm_space; 1967789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1968789Sahrens if (!dirty) 1969789Sahrens break; 1970789Sahrens } 19712082Seschrock 19722082Seschrock /* 19732082Seschrock * If we are a replacing or spare vdev, then we can always detach the 19742082Seschrock * latter child, as that is how one cancels the operation. 19752082Seschrock */ 19762082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 19772082Seschrock c == pvd->vdev_children) 1978789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1979789Sahrens 1980789Sahrens /* 19812082Seschrock * If we are detaching the original disk from a spare, then it implies 19822082Seschrock * that the spare should become a real disk, and be removed from the 19832082Seschrock * active spare list for the pool. 19842082Seschrock */ 19852082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 19862082Seschrock vd->vdev_id == 0) 19872082Seschrock unspare = B_TRUE; 19882082Seschrock 19892082Seschrock /* 1990789Sahrens * Erase the disk labels so the disk can be used for other things. 1991789Sahrens * This must be done after all other error cases are handled, 1992789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1993789Sahrens * But if we can't do it, don't treat the error as fatal -- 1994789Sahrens * it may be that the unwritability of the disk is the reason 1995789Sahrens * it's being detached! 1996789Sahrens */ 19973377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1998789Sahrens 1999789Sahrens /* 2000789Sahrens * Remove vd from its parent and compact the parent's children. 2001789Sahrens */ 2002789Sahrens vdev_remove_child(pvd, vd); 2003789Sahrens vdev_compact_children(pvd); 2004789Sahrens 2005789Sahrens /* 2006789Sahrens * Remember one of the remaining children so we can get tvd below. 2007789Sahrens */ 2008789Sahrens cvd = pvd->vdev_child[0]; 2009789Sahrens 2010789Sahrens /* 20112082Seschrock * If we need to remove the remaining child from the list of hot spares, 20122082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 20132082Seschrock * must do this before vdev_remove_parent(), because that can change the 20142082Seschrock * GUID if it creates a new toplevel GUID. 20152082Seschrock */ 20162082Seschrock if (unspare) { 20172082Seschrock ASSERT(cvd->vdev_isspare); 20183377Seschrock spa_spare_remove(cvd); 20192082Seschrock unspare_guid = cvd->vdev_guid; 20202082Seschrock } 20212082Seschrock 20222082Seschrock /* 2023789Sahrens * If the parent mirror/replacing vdev only has one child, 2024789Sahrens * the parent is no longer needed. Remove it from the tree. 2025789Sahrens */ 2026789Sahrens if (pvd->vdev_children == 1) 2027789Sahrens vdev_remove_parent(cvd); 2028789Sahrens 2029789Sahrens /* 2030789Sahrens * We don't set tvd until now because the parent we just removed 2031789Sahrens * may have been the previous top-level vdev. 2032789Sahrens */ 2033789Sahrens tvd = cvd->vdev_top; 2034789Sahrens ASSERT(tvd->vdev_parent == rvd); 2035789Sahrens 2036789Sahrens /* 20373377Seschrock * Reevaluate the parent vdev state. 2038789Sahrens */ 20394451Seschrock vdev_propagate_state(cvd); 2040789Sahrens 2041789Sahrens /* 20423377Seschrock * If the device we just detached was smaller than the others, it may be 20433377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 20443377Seschrock * can't fail because the existing metaslabs are already in core, so 20453377Seschrock * there's nothing to read from disk. 2046789Sahrens */ 20471732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2048789Sahrens 2049789Sahrens vdev_config_dirty(tvd); 2050789Sahrens 2051789Sahrens /* 20523377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 20533377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 20543377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 20553377Seschrock * prevent vd from being accessed after it's freed. 2056789Sahrens */ 2057789Sahrens for (t = 0; t < TXG_SIZE; t++) 2058789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 20591732Sbonwick vd->vdev_detached = B_TRUE; 20601732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2061789Sahrens 20624451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 20634451Seschrock 20642082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 20652082Seschrock 20662082Seschrock /* 20673377Seschrock * If this was the removal of the original device in a hot spare vdev, 20683377Seschrock * then we want to go through and remove the device from the hot spare 20693377Seschrock * list of every other pool. 20702082Seschrock */ 20712082Seschrock if (unspare) { 20722082Seschrock spa = NULL; 20732082Seschrock mutex_enter(&spa_namespace_lock); 20742082Seschrock while ((spa = spa_next(spa)) != NULL) { 20752082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 20762082Seschrock continue; 20772082Seschrock 20782082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 20792082Seschrock } 20802082Seschrock mutex_exit(&spa_namespace_lock); 20812082Seschrock } 20822082Seschrock 20832082Seschrock return (error); 20842082Seschrock } 20852082Seschrock 20862082Seschrock /* 20872082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 20882082Seschrock * spares. 20892082Seschrock */ 20902082Seschrock int 20912082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 20922082Seschrock { 20932082Seschrock vdev_t *vd; 20942082Seschrock nvlist_t **spares, *nv, **newspares; 20952082Seschrock uint_t i, j, nspares; 20962082Seschrock int ret = 0; 20972082Seschrock 20982082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20992082Seschrock 21002082Seschrock vd = spa_lookup_by_guid(spa, guid); 21012082Seschrock 21022082Seschrock nv = NULL; 21032082Seschrock if (spa->spa_spares != NULL && 21042082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21052082Seschrock &spares, &nspares) == 0) { 21062082Seschrock for (i = 0; i < nspares; i++) { 21072082Seschrock uint64_t theguid; 21082082Seschrock 21092082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 21102082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 21112082Seschrock if (theguid == guid) { 21122082Seschrock nv = spares[i]; 21132082Seschrock break; 21142082Seschrock } 21152082Seschrock } 21162082Seschrock } 21172082Seschrock 21182082Seschrock /* 21192082Seschrock * We only support removing a hot spare, and only if it's not currently 21202082Seschrock * in use in this pool. 21212082Seschrock */ 21222082Seschrock if (nv == NULL && vd == NULL) { 21232082Seschrock ret = ENOENT; 21242082Seschrock goto out; 21252082Seschrock } 21262082Seschrock 21272082Seschrock if (nv == NULL && vd != NULL) { 21282082Seschrock ret = ENOTSUP; 21292082Seschrock goto out; 21302082Seschrock } 21312082Seschrock 21322082Seschrock if (!unspare && nv != NULL && vd != NULL) { 21332082Seschrock ret = EBUSY; 21342082Seschrock goto out; 21352082Seschrock } 21362082Seschrock 21372082Seschrock if (nspares == 1) { 21382082Seschrock newspares = NULL; 21392082Seschrock } else { 21402082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 21412082Seschrock KM_SLEEP); 21422082Seschrock for (i = 0, j = 0; i < nspares; i++) { 21432082Seschrock if (spares[i] != nv) 21442082Seschrock VERIFY(nvlist_dup(spares[i], 21452082Seschrock &newspares[j++], KM_SLEEP) == 0); 21462082Seschrock } 21472082Seschrock } 21482082Seschrock 21492082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21502082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 21512082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21522082Seschrock newspares, nspares - 1) == 0); 21532082Seschrock for (i = 0; i < nspares - 1; i++) 21542082Seschrock nvlist_free(newspares[i]); 21552082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 21562082Seschrock spa_load_spares(spa); 21572082Seschrock spa->spa_sync_spares = B_TRUE; 21582082Seschrock 21592082Seschrock out: 21602082Seschrock spa_config_exit(spa, FTAG); 21612082Seschrock 21622082Seschrock return (ret); 2163789Sahrens } 2164789Sahrens 2165789Sahrens /* 21664451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 21674451Seschrock * current spared, so we can detach it. 2168789Sahrens */ 21691544Seschrock static vdev_t * 21704451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2171789Sahrens { 21721544Seschrock vdev_t *newvd, *oldvd; 2173789Sahrens int c; 2174789Sahrens 21751544Seschrock for (c = 0; c < vd->vdev_children; c++) { 21764451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 21771544Seschrock if (oldvd != NULL) 21781544Seschrock return (oldvd); 21791544Seschrock } 2180789Sahrens 21814451Seschrock /* 21824451Seschrock * Check for a completed replacement. 21834451Seschrock */ 2184789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 21851544Seschrock oldvd = vd->vdev_child[0]; 21861544Seschrock newvd = vd->vdev_child[1]; 2187789Sahrens 21881544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 21891544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 21901544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 21911544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21921544Seschrock return (oldvd); 21931544Seschrock } 21941544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21951544Seschrock } 2196789Sahrens 21974451Seschrock /* 21984451Seschrock * Check for a completed resilver with the 'unspare' flag set. 21994451Seschrock */ 22004451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 22014451Seschrock newvd = vd->vdev_child[0]; 22024451Seschrock oldvd = vd->vdev_child[1]; 22034451Seschrock 22044451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 22054451Seschrock if (newvd->vdev_unspare && 22064451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 22074451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 22084451Seschrock newvd->vdev_unspare = 0; 22094451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22104451Seschrock return (oldvd); 22114451Seschrock } 22124451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22134451Seschrock } 22144451Seschrock 22151544Seschrock return (NULL); 2216789Sahrens } 2217789Sahrens 22181544Seschrock static void 22194451Seschrock spa_vdev_resilver_done(spa_t *spa) 2220789Sahrens { 22211544Seschrock vdev_t *vd; 22222082Seschrock vdev_t *pvd; 22231544Seschrock uint64_t guid; 22242082Seschrock uint64_t pguid = 0; 2225789Sahrens 22261544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2227789Sahrens 22284451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 22291544Seschrock guid = vd->vdev_guid; 22302082Seschrock /* 22312082Seschrock * If we have just finished replacing a hot spared device, then 22322082Seschrock * we need to detach the parent's first child (the original hot 22332082Seschrock * spare) as well. 22342082Seschrock */ 22352082Seschrock pvd = vd->vdev_parent; 22362082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 22372082Seschrock pvd->vdev_id == 0) { 22382082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 22392082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 22402082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 22412082Seschrock } 22421544Seschrock spa_config_exit(spa, FTAG); 22431544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 22441544Seschrock return; 22452082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 22462082Seschrock return; 22471544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2248789Sahrens } 2249789Sahrens 22501544Seschrock spa_config_exit(spa, FTAG); 2251789Sahrens } 2252789Sahrens 2253789Sahrens /* 22541354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 22551354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 22561354Seschrock */ 22571354Seschrock int 22581354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 22591354Seschrock { 22601354Seschrock vdev_t *rvd, *vd; 22611354Seschrock uint64_t txg; 22621354Seschrock 22631354Seschrock rvd = spa->spa_root_vdev; 22641354Seschrock 22651354Seschrock txg = spa_vdev_enter(spa); 22661354Seschrock 22672082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 22682082Seschrock /* 22692082Seschrock * Determine if this is a reference to a hot spare. In that 22702082Seschrock * case, update the path as stored in the spare list. 22712082Seschrock */ 22722082Seschrock nvlist_t **spares; 22732082Seschrock uint_t i, nspares; 22742082Seschrock if (spa->spa_sparelist != NULL) { 22752082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 22762082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 22772082Seschrock for (i = 0; i < nspares; i++) { 22782082Seschrock uint64_t theguid; 22792082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 22802082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 22812082Seschrock if (theguid == guid) 22822082Seschrock break; 22832082Seschrock } 22842082Seschrock 22852082Seschrock if (i == nspares) 22862082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22872082Seschrock 22882082Seschrock VERIFY(nvlist_add_string(spares[i], 22892082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 22902082Seschrock spa_load_spares(spa); 22912082Seschrock spa->spa_sync_spares = B_TRUE; 22922082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 22932082Seschrock } else { 22942082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22952082Seschrock } 22962082Seschrock } 22971354Seschrock 22981585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 22991585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 23001585Sbonwick 23011354Seschrock spa_strfree(vd->vdev_path); 23021354Seschrock vd->vdev_path = spa_strdup(newpath); 23031354Seschrock 23041354Seschrock vdev_config_dirty(vd->vdev_top); 23051354Seschrock 23061354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 23071354Seschrock } 23081354Seschrock 23091354Seschrock /* 2310789Sahrens * ========================================================================== 2311789Sahrens * SPA Scrubbing 2312789Sahrens * ========================================================================== 2313789Sahrens */ 2314789Sahrens 2315789Sahrens static void 2316789Sahrens spa_scrub_io_done(zio_t *zio) 2317789Sahrens { 2318789Sahrens spa_t *spa = zio->io_spa; 2319789Sahrens 23204309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2321789Sahrens 2322789Sahrens mutex_enter(&spa->spa_scrub_lock); 23231544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 23241775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2325789Sahrens spa->spa_scrub_errors++; 2326789Sahrens mutex_enter(&vd->vdev_stat_lock); 2327789Sahrens vd->vdev_stat.vs_scrub_errors++; 2328789Sahrens mutex_exit(&vd->vdev_stat_lock); 2329789Sahrens } 23303697Smishra 23313697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 23321544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 23333697Smishra 23343697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 23353697Smishra 23361544Seschrock mutex_exit(&spa->spa_scrub_lock); 2337789Sahrens } 2338789Sahrens 2339789Sahrens static void 23401544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 23411544Seschrock zbookmark_t *zb) 2342789Sahrens { 2343789Sahrens size_t size = BP_GET_LSIZE(bp); 23443697Smishra void *data; 2345789Sahrens 2346789Sahrens mutex_enter(&spa->spa_scrub_lock); 23473697Smishra /* 23483697Smishra * Do not give too much work to vdev(s). 23493697Smishra */ 23503697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 23513697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 23523697Smishra } 2353789Sahrens spa->spa_scrub_inflight++; 2354789Sahrens mutex_exit(&spa->spa_scrub_lock); 2355789Sahrens 23564309Smaybee data = arc_data_buf_alloc(size); 23573697Smishra 23581544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 23591544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 23601544Seschrock 23611807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 23621544Seschrock 2363789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 23641544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2365789Sahrens } 2366789Sahrens 2367789Sahrens /* ARGSUSED */ 2368789Sahrens static int 2369789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2370789Sahrens { 2371789Sahrens blkptr_t *bp = &bc->bc_blkptr; 23721775Sbillm vdev_t *vd = spa->spa_root_vdev; 23731775Sbillm dva_t *dva = bp->blk_dva; 23741775Sbillm int needs_resilver = B_FALSE; 23751775Sbillm int d; 2376789Sahrens 23771775Sbillm if (bc->bc_errno) { 2378789Sahrens /* 2379789Sahrens * We can't scrub this block, but we can continue to scrub 2380789Sahrens * the rest of the pool. Note the error and move along. 2381789Sahrens */ 2382789Sahrens mutex_enter(&spa->spa_scrub_lock); 2383789Sahrens spa->spa_scrub_errors++; 2384789Sahrens mutex_exit(&spa->spa_scrub_lock); 2385789Sahrens 23861775Sbillm mutex_enter(&vd->vdev_stat_lock); 23871775Sbillm vd->vdev_stat.vs_scrub_errors++; 23881775Sbillm mutex_exit(&vd->vdev_stat_lock); 2389789Sahrens 2390789Sahrens return (ERESTART); 2391789Sahrens } 2392789Sahrens 2393789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2394789Sahrens 23951775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 23961775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 23971775Sbillm 23981775Sbillm ASSERT(vd != NULL); 23991775Sbillm 24001775Sbillm /* 24011775Sbillm * Keep track of how much data we've examined so that 24021775Sbillm * zpool(1M) status can make useful progress reports. 24031775Sbillm */ 24041775Sbillm mutex_enter(&vd->vdev_stat_lock); 24051775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 24061775Sbillm mutex_exit(&vd->vdev_stat_lock); 2407789Sahrens 24081775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 24091775Sbillm if (DVA_GET_GANG(&dva[d])) { 24101775Sbillm /* 24111775Sbillm * Gang members may be spread across multiple 24121775Sbillm * vdevs, so the best we can do is look at the 24131775Sbillm * pool-wide DTL. 24141775Sbillm * XXX -- it would be better to change our 24151775Sbillm * allocation policy to ensure that this can't 24161775Sbillm * happen. 24171775Sbillm */ 24181775Sbillm vd = spa->spa_root_vdev; 24191775Sbillm } 24201775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 24211775Sbillm bp->blk_birth, 1)) 24221775Sbillm needs_resilver = B_TRUE; 2423789Sahrens } 24241775Sbillm } 24251775Sbillm 24261775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2427789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 24281544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 24291775Sbillm else if (needs_resilver) 24301775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 24311775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2432789Sahrens 2433789Sahrens return (0); 2434789Sahrens } 2435789Sahrens 2436789Sahrens static void 2437789Sahrens spa_scrub_thread(spa_t *spa) 2438789Sahrens { 2439789Sahrens callb_cpr_t cprinfo; 2440789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2441789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2442789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2443789Sahrens int error = 0; 2444789Sahrens boolean_t complete; 2445789Sahrens 2446789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2447789Sahrens 2448797Sbonwick /* 2449797Sbonwick * If we're restarting due to a snapshot create/delete, 2450797Sbonwick * wait for that to complete. 2451797Sbonwick */ 2452797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2453797Sbonwick 24541544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 24551544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 24561544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 24571544Seschrock 24581544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 24591544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2460789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2461789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 24621544Seschrock spa_config_exit(spa, FTAG); 2463789Sahrens 2464789Sahrens mutex_enter(&spa->spa_scrub_lock); 2465789Sahrens spa->spa_scrub_errors = 0; 2466789Sahrens spa->spa_scrub_active = 1; 24671544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2468789Sahrens 2469789Sahrens while (!spa->spa_scrub_stop) { 2470789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 24711544Seschrock while (spa->spa_scrub_suspended) { 2472789Sahrens spa->spa_scrub_active = 0; 2473789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2474789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2475789Sahrens spa->spa_scrub_active = 1; 2476789Sahrens } 2477789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2478789Sahrens 2479789Sahrens if (spa->spa_scrub_restart_txg != 0) 2480789Sahrens break; 2481789Sahrens 2482789Sahrens mutex_exit(&spa->spa_scrub_lock); 2483789Sahrens error = traverse_more(th); 2484789Sahrens mutex_enter(&spa->spa_scrub_lock); 2485789Sahrens if (error != EAGAIN) 2486789Sahrens break; 2487789Sahrens } 2488789Sahrens 2489789Sahrens while (spa->spa_scrub_inflight) 2490789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2491789Sahrens 24921601Sbonwick spa->spa_scrub_active = 0; 24931601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 24941601Sbonwick 24951601Sbonwick mutex_exit(&spa->spa_scrub_lock); 24961601Sbonwick 24971601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24981601Sbonwick 24991601Sbonwick mutex_enter(&spa->spa_scrub_lock); 25001601Sbonwick 25011601Sbonwick /* 25021601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 25031601Sbonwick * AND the spa config lock to synchronize with any config changes 25041601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 25051601Sbonwick */ 2506789Sahrens if (spa->spa_scrub_restart_txg != 0) 2507789Sahrens error = ERESTART; 2508789Sahrens 25091544Seschrock if (spa->spa_scrub_stop) 25101544Seschrock error = EINTR; 25111544Seschrock 2512789Sahrens /* 25131544Seschrock * Even if there were uncorrectable errors, we consider the scrub 25141544Seschrock * completed. The downside is that if there is a transient error during 25151544Seschrock * a resilver, we won't resilver the data properly to the target. But 25161544Seschrock * if the damage is permanent (more likely) we will resilver forever, 25171544Seschrock * which isn't really acceptable. Since there is enough information for 25181544Seschrock * the user to know what has failed and why, this seems like a more 25191544Seschrock * tractable approach. 2520789Sahrens */ 25211544Seschrock complete = (error == 0); 2522789Sahrens 25231544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 25241544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2525789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2526789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2527789Sahrens 2528789Sahrens mutex_exit(&spa->spa_scrub_lock); 2529789Sahrens 2530789Sahrens /* 2531789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2532789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2533789Sahrens */ 2534789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2535789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2536789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 25371544Seschrock spa_errlog_rotate(spa); 25381601Sbonwick 25394451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 25404451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 25414451Seschrock 25421544Seschrock spa_config_exit(spa, FTAG); 2543789Sahrens 2544789Sahrens mutex_enter(&spa->spa_scrub_lock); 2545789Sahrens 25461544Seschrock /* 25471544Seschrock * We may have finished replacing a device. 25481544Seschrock * Let the async thread assess this and handle the detach. 25491544Seschrock */ 25504451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2551789Sahrens 2552789Sahrens /* 2553789Sahrens * If we were told to restart, our final act is to start a new scrub. 2554789Sahrens */ 2555789Sahrens if (error == ERESTART) 25561544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 25571544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2558789Sahrens 25591544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 25601544Seschrock spa->spa_scrub_active = 0; 25611544Seschrock spa->spa_scrub_thread = NULL; 25621544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2563789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2564789Sahrens thread_exit(); 2565789Sahrens } 2566789Sahrens 2567789Sahrens void 2568789Sahrens spa_scrub_suspend(spa_t *spa) 2569789Sahrens { 2570789Sahrens mutex_enter(&spa->spa_scrub_lock); 25711544Seschrock spa->spa_scrub_suspended++; 2572789Sahrens while (spa->spa_scrub_active) { 2573789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2574789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2575789Sahrens } 2576789Sahrens while (spa->spa_scrub_inflight) 2577789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2578789Sahrens mutex_exit(&spa->spa_scrub_lock); 2579789Sahrens } 2580789Sahrens 2581789Sahrens void 2582789Sahrens spa_scrub_resume(spa_t *spa) 2583789Sahrens { 2584789Sahrens mutex_enter(&spa->spa_scrub_lock); 25851544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 25861544Seschrock if (--spa->spa_scrub_suspended == 0) 2587789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2588789Sahrens mutex_exit(&spa->spa_scrub_lock); 2589789Sahrens } 2590789Sahrens 2591789Sahrens void 2592789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2593789Sahrens { 2594789Sahrens /* 2595789Sahrens * Something happened (e.g. snapshot create/delete) that means 2596789Sahrens * we must restart any in-progress scrubs. The itinerary will 2597789Sahrens * fix this properly. 2598789Sahrens */ 2599789Sahrens mutex_enter(&spa->spa_scrub_lock); 2600789Sahrens spa->spa_scrub_restart_txg = txg; 2601789Sahrens mutex_exit(&spa->spa_scrub_lock); 2602789Sahrens } 2603789Sahrens 26041544Seschrock int 26051544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2606789Sahrens { 2607789Sahrens space_seg_t *ss; 2608789Sahrens uint64_t mintxg, maxtxg; 2609789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2610789Sahrens 2611789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2612789Sahrens return (ENOTSUP); 2613789Sahrens 26141544Seschrock mutex_enter(&spa->spa_scrub_lock); 26151544Seschrock 2616789Sahrens /* 2617789Sahrens * If there's a scrub or resilver already in progress, stop it. 2618789Sahrens */ 2619789Sahrens while (spa->spa_scrub_thread != NULL) { 2620789Sahrens /* 2621789Sahrens * Don't stop a resilver unless forced. 2622789Sahrens */ 26231544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 26241544Seschrock mutex_exit(&spa->spa_scrub_lock); 2625789Sahrens return (EBUSY); 26261544Seschrock } 2627789Sahrens spa->spa_scrub_stop = 1; 2628789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2629789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2630789Sahrens } 2631789Sahrens 2632789Sahrens /* 2633789Sahrens * Terminate the previous traverse. 2634789Sahrens */ 2635789Sahrens if (spa->spa_scrub_th != NULL) { 2636789Sahrens traverse_fini(spa->spa_scrub_th); 2637789Sahrens spa->spa_scrub_th = NULL; 2638789Sahrens } 2639789Sahrens 26401544Seschrock if (rvd == NULL) { 26411544Seschrock ASSERT(spa->spa_scrub_stop == 0); 26421544Seschrock ASSERT(spa->spa_scrub_type == type); 26431544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 26441544Seschrock mutex_exit(&spa->spa_scrub_lock); 26451544Seschrock return (0); 26461544Seschrock } 2647789Sahrens 2648789Sahrens mintxg = TXG_INITIAL - 1; 2649789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2650789Sahrens 26511544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2652789Sahrens 26531544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 26541544Seschrock /* 26551544Seschrock * The pool-wide DTL is empty. 26561732Sbonwick * If this is a resilver, there's nothing to do except 26571732Sbonwick * check whether any in-progress replacements have completed. 26581544Seschrock */ 26591732Sbonwick if (type == POOL_SCRUB_RESILVER) { 26601544Seschrock type = POOL_SCRUB_NONE; 26614451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 26621732Sbonwick } 26631544Seschrock } else { 26641544Seschrock /* 26651544Seschrock * The pool-wide DTL is non-empty. 26661544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 26671544Seschrock */ 26681544Seschrock if (type == POOL_SCRUB_EVERYTHING) 26691544Seschrock type = POOL_SCRUB_RESILVER; 26701544Seschrock } 2671789Sahrens 26721544Seschrock if (type == POOL_SCRUB_RESILVER) { 2673789Sahrens /* 2674789Sahrens * Determine the resilvering boundaries. 2675789Sahrens * 2676789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2677789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2678789Sahrens * 2679789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2680789Sahrens * so we don't claim to resilver a txg that's still changing. 2681789Sahrens */ 2682789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 26831544Seschrock mintxg = ss->ss_start - 1; 2684789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 26851544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 26864451Seschrock 26874451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 2688789Sahrens } 2689789Sahrens 26901544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 26911544Seschrock 26921544Seschrock spa->spa_scrub_stop = 0; 26931544Seschrock spa->spa_scrub_type = type; 26941544Seschrock spa->spa_scrub_restart_txg = 0; 26951544Seschrock 26961544Seschrock if (type != POOL_SCRUB_NONE) { 26971544Seschrock spa->spa_scrub_mintxg = mintxg; 2698789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2699789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 27001635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 27011635Sbonwick ZIO_FLAG_CANFAIL); 2702789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2703789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2704789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2705789Sahrens } 2706789Sahrens 27071544Seschrock mutex_exit(&spa->spa_scrub_lock); 27081544Seschrock 2709789Sahrens return (0); 2710789Sahrens } 2711789Sahrens 27121544Seschrock /* 27131544Seschrock * ========================================================================== 27141544Seschrock * SPA async task processing 27151544Seschrock * ========================================================================== 27161544Seschrock */ 27171544Seschrock 27181544Seschrock static void 27194451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 2720789Sahrens { 27211544Seschrock vdev_t *tvd; 27221544Seschrock int c; 27231544Seschrock 27244451Seschrock for (c = 0; c < vd->vdev_children; c++) { 27254451Seschrock tvd = vd->vdev_child[c]; 27264451Seschrock if (tvd->vdev_remove_wanted) { 27274451Seschrock tvd->vdev_remove_wanted = 0; 27284451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 27294451Seschrock VDEV_AUX_NONE); 27304451Seschrock vdev_clear(spa, tvd); 27314451Seschrock vdev_config_dirty(tvd->vdev_top); 27321544Seschrock } 27334451Seschrock spa_async_remove(spa, tvd); 27341544Seschrock } 27351544Seschrock } 27361544Seschrock 27371544Seschrock static void 27381544Seschrock spa_async_thread(spa_t *spa) 27391544Seschrock { 27401544Seschrock int tasks; 27414451Seschrock uint64_t txg; 27421544Seschrock 27431544Seschrock ASSERT(spa->spa_sync_on); 2744789Sahrens 27451544Seschrock mutex_enter(&spa->spa_async_lock); 27461544Seschrock tasks = spa->spa_async_tasks; 27471544Seschrock spa->spa_async_tasks = 0; 27481544Seschrock mutex_exit(&spa->spa_async_lock); 27491544Seschrock 27501544Seschrock /* 27511635Sbonwick * See if the config needs to be updated. 27521635Sbonwick */ 27531635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 27541635Sbonwick mutex_enter(&spa_namespace_lock); 27551635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27561635Sbonwick mutex_exit(&spa_namespace_lock); 27571635Sbonwick } 27581635Sbonwick 27591635Sbonwick /* 27604451Seschrock * See if any devices need to be marked REMOVED. 27611544Seschrock */ 27624451Seschrock if (tasks & SPA_ASYNC_REMOVE) { 27634451Seschrock txg = spa_vdev_enter(spa); 27644451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 27654451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 27664451Seschrock } 27671544Seschrock 27681544Seschrock /* 27691544Seschrock * If any devices are done replacing, detach them. 27701544Seschrock */ 27714451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 27724451Seschrock spa_vdev_resilver_done(spa); 2773789Sahrens 27741544Seschrock /* 27754451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 27764451Seschrock * scrub which can become a resilver), we need to hold 27774451Seschrock * spa_namespace_lock() because the sysevent we post via 27784451Seschrock * spa_event_notify() needs to get the name of the pool. 27791544Seschrock */ 27804451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 27814451Seschrock mutex_enter(&spa_namespace_lock); 27821544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 27834451Seschrock mutex_exit(&spa_namespace_lock); 27844451Seschrock } 27851544Seschrock 27861544Seschrock /* 27871544Seschrock * Kick off a resilver. 27881544Seschrock */ 27894451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 27904451Seschrock mutex_enter(&spa_namespace_lock); 27911544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 27924451Seschrock mutex_exit(&spa_namespace_lock); 27934451Seschrock } 27941544Seschrock 27951544Seschrock /* 27961544Seschrock * Let the world know that we're done. 27971544Seschrock */ 27981544Seschrock mutex_enter(&spa->spa_async_lock); 27991544Seschrock spa->spa_async_thread = NULL; 28001544Seschrock cv_broadcast(&spa->spa_async_cv); 28011544Seschrock mutex_exit(&spa->spa_async_lock); 28021544Seschrock thread_exit(); 28031544Seschrock } 28041544Seschrock 28051544Seschrock void 28061544Seschrock spa_async_suspend(spa_t *spa) 28071544Seschrock { 28081544Seschrock mutex_enter(&spa->spa_async_lock); 28091544Seschrock spa->spa_async_suspended++; 28101544Seschrock while (spa->spa_async_thread != NULL) 28111544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 28121544Seschrock mutex_exit(&spa->spa_async_lock); 28131544Seschrock } 28141544Seschrock 28151544Seschrock void 28161544Seschrock spa_async_resume(spa_t *spa) 28171544Seschrock { 28181544Seschrock mutex_enter(&spa->spa_async_lock); 28191544Seschrock ASSERT(spa->spa_async_suspended != 0); 28201544Seschrock spa->spa_async_suspended--; 28211544Seschrock mutex_exit(&spa->spa_async_lock); 28221544Seschrock } 28231544Seschrock 28241544Seschrock static void 28251544Seschrock spa_async_dispatch(spa_t *spa) 28261544Seschrock { 28271544Seschrock mutex_enter(&spa->spa_async_lock); 28281544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 28291635Sbonwick spa->spa_async_thread == NULL && 28301635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 28311544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 28321544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 28331544Seschrock mutex_exit(&spa->spa_async_lock); 28341544Seschrock } 28351544Seschrock 28361544Seschrock void 28371544Seschrock spa_async_request(spa_t *spa, int task) 28381544Seschrock { 28391544Seschrock mutex_enter(&spa->spa_async_lock); 28401544Seschrock spa->spa_async_tasks |= task; 28411544Seschrock mutex_exit(&spa->spa_async_lock); 2842789Sahrens } 2843789Sahrens 2844789Sahrens /* 2845789Sahrens * ========================================================================== 2846789Sahrens * SPA syncing routines 2847789Sahrens * ========================================================================== 2848789Sahrens */ 2849789Sahrens 2850789Sahrens static void 2851789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2852789Sahrens { 2853789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2854789Sahrens dmu_tx_t *tx; 2855789Sahrens blkptr_t blk; 2856789Sahrens uint64_t itor = 0; 2857789Sahrens zio_t *zio; 2858789Sahrens int error; 2859789Sahrens uint8_t c = 1; 2860789Sahrens 2861789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2862789Sahrens 2863789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2864789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2865789Sahrens 2866789Sahrens error = zio_wait(zio); 2867789Sahrens ASSERT3U(error, ==, 0); 2868789Sahrens 2869789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2870789Sahrens bplist_vacate(bpl, tx); 2871789Sahrens 2872789Sahrens /* 2873789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2874789Sahrens * (Usually only the first block is needed.) 2875789Sahrens */ 2876789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2877789Sahrens dmu_tx_commit(tx); 2878789Sahrens } 2879789Sahrens 2880789Sahrens static void 28812082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 28822082Seschrock { 28832082Seschrock char *packed = NULL; 28842082Seschrock size_t nvsize = 0; 28852082Seschrock dmu_buf_t *db; 28862082Seschrock 28872082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 28882082Seschrock 28892082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 28902082Seschrock 28912082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 28922082Seschrock KM_SLEEP) == 0); 28932082Seschrock 28942082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 28952082Seschrock 28962082Seschrock kmem_free(packed, nvsize); 28972082Seschrock 28982082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 28992082Seschrock dmu_buf_will_dirty(db, tx); 29002082Seschrock *(uint64_t *)db->db_data = nvsize; 29012082Seschrock dmu_buf_rele(db, FTAG); 29022082Seschrock } 29032082Seschrock 29042082Seschrock static void 29052082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 29062082Seschrock { 29072082Seschrock nvlist_t *nvroot; 29082082Seschrock nvlist_t **spares; 29092082Seschrock int i; 29102082Seschrock 29112082Seschrock if (!spa->spa_sync_spares) 29122082Seschrock return; 29132082Seschrock 29142082Seschrock /* 29152082Seschrock * Update the MOS nvlist describing the list of available spares. 29162082Seschrock * spa_validate_spares() will have already made sure this nvlist is 29174451Seschrock * valid and the vdevs are labeled appropriately. 29182082Seschrock */ 29192082Seschrock if (spa->spa_spares_object == 0) { 29202082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 29212082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 29222082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 29232082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 29242082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 29252082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 29262082Seschrock } 29272082Seschrock 29282082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 29292082Seschrock if (spa->spa_nspares == 0) { 29302082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29312082Seschrock NULL, 0) == 0); 29322082Seschrock } else { 29332082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 29342082Seschrock KM_SLEEP); 29352082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29362082Seschrock spares[i] = vdev_config_generate(spa, 29372082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 29382082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29392082Seschrock spares, spa->spa_nspares) == 0); 29402082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29412082Seschrock nvlist_free(spares[i]); 29422082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 29432082Seschrock } 29442082Seschrock 29452082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 29462926Sek110237 nvlist_free(nvroot); 29472082Seschrock 29482082Seschrock spa->spa_sync_spares = B_FALSE; 29492082Seschrock } 29502082Seschrock 29512082Seschrock static void 2952789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2953789Sahrens { 2954789Sahrens nvlist_t *config; 2955789Sahrens 2956789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2957789Sahrens return; 2958789Sahrens 2959789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2960789Sahrens 29611635Sbonwick if (spa->spa_config_syncing) 29621635Sbonwick nvlist_free(spa->spa_config_syncing); 29631635Sbonwick spa->spa_config_syncing = config; 2964789Sahrens 29652082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2966789Sahrens } 2967789Sahrens 29683912Slling static void 29694543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 29703912Slling { 29713912Slling spa_t *spa = arg1; 29723912Slling nvlist_t *nvp = arg2; 29733912Slling nvpair_t *nvpair; 29743912Slling objset_t *mos = spa->spa_meta_objset; 29753912Slling uint64_t zapobj; 29764451Seschrock uint64_t intval; 29773912Slling 29783912Slling mutex_enter(&spa->spa_props_lock); 29793912Slling if (spa->spa_pool_props_object == 0) { 29803912Slling zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 29813912Slling VERIFY(zapobj > 0); 29823912Slling 29833912Slling spa->spa_pool_props_object = zapobj; 29843912Slling 29853912Slling VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 29863912Slling DMU_POOL_PROPS, 8, 1, 29873912Slling &spa->spa_pool_props_object, tx) == 0); 29883912Slling } 29893912Slling mutex_exit(&spa->spa_props_lock); 29903912Slling 29913912Slling nvpair = NULL; 29923912Slling while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 29933912Slling switch (zpool_name_to_prop(nvpair_name(nvpair))) { 29944543Smarks case ZPOOL_PROP_DELEGATION: 29954543Smarks VERIFY(nvlist_lookup_uint64(nvp, 29964543Smarks nvpair_name(nvpair), &intval) == 0); 29974543Smarks VERIFY(zap_update(mos, 29984543Smarks spa->spa_pool_props_object, 29994543Smarks nvpair_name(nvpair), 8, 1, 30004543Smarks &intval, tx) == 0); 30014543Smarks spa->spa_delegation = intval; 30024543Smarks break; 30034451Seschrock case ZPOOL_PROP_BOOTFS: 30043912Slling VERIFY(nvlist_lookup_uint64(nvp, 30053912Slling nvpair_name(nvpair), &spa->spa_bootfs) == 0); 30064543Smarks intval = spa->spa_bootfs; 30073912Slling VERIFY(zap_update(mos, 30083912Slling spa->spa_pool_props_object, 30094451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 8, 1, 30104543Smarks &intval, tx) == 0); 30113912Slling break; 30124451Seschrock 30134451Seschrock case ZPOOL_PROP_AUTOREPLACE: 30144451Seschrock VERIFY(nvlist_lookup_uint64(nvp, 30154451Seschrock nvpair_name(nvpair), &intval) == 0); 30164451Seschrock VERIFY(zap_update(mos, 30174451Seschrock spa->spa_pool_props_object, 30184451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 8, 1, 30194451Seschrock &intval, tx) == 0); 30204451Seschrock break; 30213912Slling } 30224543Smarks spa_history_internal_log(LOG_POOL_PROPSET, 30234543Smarks spa, tx, cr, "%s %lld %s", 30244543Smarks nvpair_name(nvpair), intval, 30254543Smarks spa->spa_name); 30263912Slling } 30273912Slling } 30283912Slling 3029789Sahrens /* 3030789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3031789Sahrens * part of the process, so we iterate until it converges. 3032789Sahrens */ 3033789Sahrens void 3034789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3035789Sahrens { 3036789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3037789Sahrens objset_t *mos = spa->spa_meta_objset; 3038789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 30391635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3040789Sahrens vdev_t *vd; 3041789Sahrens dmu_tx_t *tx; 3042789Sahrens int dirty_vdevs; 3043789Sahrens 3044789Sahrens /* 3045789Sahrens * Lock out configuration changes. 3046789Sahrens */ 30471544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3048789Sahrens 3049789Sahrens spa->spa_syncing_txg = txg; 3050789Sahrens spa->spa_sync_pass = 0; 3051789Sahrens 30521544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3053789Sahrens 30542082Seschrock tx = dmu_tx_create_assigned(dp, txg); 30552082Seschrock 30562082Seschrock /* 30574577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 30582082Seschrock * set spa_deflate if we have no raid-z vdevs. 30592082Seschrock */ 30604577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 30614577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 30622082Seschrock int i; 30632082Seschrock 30642082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 30652082Seschrock vd = rvd->vdev_child[i]; 30662082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 30672082Seschrock break; 30682082Seschrock } 30692082Seschrock if (i == rvd->vdev_children) { 30702082Seschrock spa->spa_deflate = TRUE; 30712082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 30722082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 30732082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 30742082Seschrock } 30752082Seschrock } 30762082Seschrock 3077789Sahrens /* 3078789Sahrens * If anything has changed in this txg, push the deferred frees 3079789Sahrens * from the previous txg. If not, leave them alone so that we 3080789Sahrens * don't generate work on an otherwise idle system. 3081789Sahrens */ 3082789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 30832329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 30842329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3085789Sahrens spa_sync_deferred_frees(spa, txg); 3086789Sahrens 3087789Sahrens /* 3088789Sahrens * Iterate to convergence. 3089789Sahrens */ 3090789Sahrens do { 3091789Sahrens spa->spa_sync_pass++; 3092789Sahrens 3093789Sahrens spa_sync_config_object(spa, tx); 30942082Seschrock spa_sync_spares(spa, tx); 30951544Seschrock spa_errlog_sync(spa, txg); 3096789Sahrens dsl_pool_sync(dp, txg); 3097789Sahrens 3098789Sahrens dirty_vdevs = 0; 3099789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3100789Sahrens vdev_sync(vd, txg); 3101789Sahrens dirty_vdevs++; 3102789Sahrens } 3103789Sahrens 3104789Sahrens bplist_sync(bpl, tx); 3105789Sahrens } while (dirty_vdevs); 3106789Sahrens 3107789Sahrens bplist_close(bpl); 3108789Sahrens 3109789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3110789Sahrens 3111789Sahrens /* 3112789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3113789Sahrens * to commit the transaction group. 31141635Sbonwick * 31151635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 31161635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 31171635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 31181635Sbonwick * If the write fails, try the next vdev until we're tried them all. 3119789Sahrens */ 31201635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 31211635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31221635Sbonwick } else { 31231635Sbonwick int children = rvd->vdev_children; 31241635Sbonwick int c0 = spa_get_random(children); 31251635Sbonwick int c; 31261635Sbonwick 31271635Sbonwick for (c = 0; c < children; c++) { 31281635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 31291635Sbonwick if (vd->vdev_ms_array == 0) 31301635Sbonwick continue; 31311635Sbonwick if (vdev_config_sync(vd, txg) == 0) 31321635Sbonwick break; 31331635Sbonwick } 31341635Sbonwick if (c == children) 31351635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31361635Sbonwick } 31371635Sbonwick 31382082Seschrock dmu_tx_commit(tx); 31392082Seschrock 31401635Sbonwick /* 31411635Sbonwick * Clear the dirty config list. 31421635Sbonwick */ 31431635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 31441635Sbonwick vdev_config_clean(vd); 31451635Sbonwick 31461635Sbonwick /* 31471635Sbonwick * Now that the new config has synced transactionally, 31481635Sbonwick * let it become visible to the config cache. 31491635Sbonwick */ 31501635Sbonwick if (spa->spa_config_syncing != NULL) { 31511635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 31521635Sbonwick spa->spa_config_txg = txg; 31531635Sbonwick spa->spa_config_syncing = NULL; 31541635Sbonwick } 3155789Sahrens 3156789Sahrens /* 3157789Sahrens * Make a stable copy of the fully synced uberblock. 3158789Sahrens * We use this as the root for pool traversals. 3159789Sahrens */ 3160789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3161789Sahrens 3162789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3163789Sahrens 3164789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3165789Sahrens spa->spa_traverse_wanted = 0; 3166789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3167789Sahrens rw_exit(&spa->spa_traverse_lock); 3168789Sahrens 3169789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3170789Sahrens 3171789Sahrens /* 3172789Sahrens * Clean up the ZIL records for the synced txg. 3173789Sahrens */ 3174789Sahrens dsl_pool_zil_clean(dp); 3175789Sahrens 3176789Sahrens /* 3177789Sahrens * Update usable space statistics. 3178789Sahrens */ 3179789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3180789Sahrens vdev_sync_done(vd, txg); 3181789Sahrens 3182789Sahrens /* 3183789Sahrens * It had better be the case that we didn't dirty anything 31842082Seschrock * since vdev_config_sync(). 3185789Sahrens */ 3186789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3187789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3188789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3189789Sahrens ASSERT(bpl->bpl_queue == NULL); 3190789Sahrens 31911544Seschrock spa_config_exit(spa, FTAG); 31921544Seschrock 31931544Seschrock /* 31941544Seschrock * If any async tasks have been requested, kick them off. 31951544Seschrock */ 31961544Seschrock spa_async_dispatch(spa); 3197789Sahrens } 3198789Sahrens 3199789Sahrens /* 3200789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3201789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3202789Sahrens * sync. 3203789Sahrens */ 3204789Sahrens void 3205789Sahrens spa_sync_allpools(void) 3206789Sahrens { 3207789Sahrens spa_t *spa = NULL; 3208789Sahrens mutex_enter(&spa_namespace_lock); 3209789Sahrens while ((spa = spa_next(spa)) != NULL) { 3210789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3211789Sahrens continue; 3212789Sahrens spa_open_ref(spa, FTAG); 3213789Sahrens mutex_exit(&spa_namespace_lock); 3214789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3215789Sahrens mutex_enter(&spa_namespace_lock); 3216789Sahrens spa_close(spa, FTAG); 3217789Sahrens } 3218789Sahrens mutex_exit(&spa_namespace_lock); 3219789Sahrens } 3220789Sahrens 3221789Sahrens /* 3222789Sahrens * ========================================================================== 3223789Sahrens * Miscellaneous routines 3224789Sahrens * ========================================================================== 3225789Sahrens */ 3226789Sahrens 3227789Sahrens /* 3228789Sahrens * Remove all pools in the system. 3229789Sahrens */ 3230789Sahrens void 3231789Sahrens spa_evict_all(void) 3232789Sahrens { 3233789Sahrens spa_t *spa; 3234789Sahrens 3235789Sahrens /* 3236789Sahrens * Remove all cached state. All pools should be closed now, 3237789Sahrens * so every spa in the AVL tree should be unreferenced. 3238789Sahrens */ 3239789Sahrens mutex_enter(&spa_namespace_lock); 3240789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3241789Sahrens /* 32421544Seschrock * Stop async tasks. The async thread may need to detach 32431544Seschrock * a device that's been replaced, which requires grabbing 32441544Seschrock * spa_namespace_lock, so we must drop it here. 3245789Sahrens */ 3246789Sahrens spa_open_ref(spa, FTAG); 3247789Sahrens mutex_exit(&spa_namespace_lock); 32481544Seschrock spa_async_suspend(spa); 3249789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3250789Sahrens mutex_enter(&spa_namespace_lock); 3251789Sahrens spa_close(spa, FTAG); 3252789Sahrens 3253789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3254789Sahrens spa_unload(spa); 3255789Sahrens spa_deactivate(spa); 3256789Sahrens } 3257789Sahrens spa_remove(spa); 3258789Sahrens } 3259789Sahrens mutex_exit(&spa_namespace_lock); 3260789Sahrens } 32611544Seschrock 32621544Seschrock vdev_t * 32631544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 32641544Seschrock { 32651544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 32661544Seschrock } 32671760Seschrock 32681760Seschrock void 32691760Seschrock spa_upgrade(spa_t *spa) 32701760Seschrock { 32711760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 32721760Seschrock 32731760Seschrock /* 32741760Seschrock * This should only be called for a non-faulted pool, and since a 32751760Seschrock * future version would result in an unopenable pool, this shouldn't be 32761760Seschrock * possible. 32771760Seschrock */ 32784577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 32794577Sahrens 32804577Sahrens spa->spa_uberblock.ub_version = SPA_VERSION; 32811760Seschrock vdev_config_dirty(spa->spa_root_vdev); 32821760Seschrock 32831760Seschrock spa_config_exit(spa, FTAG); 32842082Seschrock 32852082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 32861760Seschrock } 32872082Seschrock 32882082Seschrock boolean_t 32892082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 32902082Seschrock { 32912082Seschrock int i; 32923377Seschrock uint64_t spareguid; 32932082Seschrock 32942082Seschrock for (i = 0; i < spa->spa_nspares; i++) 32952082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 32962082Seschrock return (B_TRUE); 32972082Seschrock 32983377Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 32993377Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 33003377Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 33013377Seschrock spareguid == guid) 33023377Seschrock return (B_TRUE); 33033377Seschrock } 33043377Seschrock 33052082Seschrock return (B_FALSE); 33062082Seschrock } 33073912Slling 33083912Slling int 33093912Slling spa_set_props(spa_t *spa, nvlist_t *nvp) 33103912Slling { 33113912Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 33123912Slling spa, nvp, 3)); 33133912Slling } 33143912Slling 33153912Slling int 33163912Slling spa_get_props(spa_t *spa, nvlist_t **nvp) 33173912Slling { 33183912Slling zap_cursor_t zc; 33193912Slling zap_attribute_t za; 33203912Slling objset_t *mos = spa->spa_meta_objset; 33213912Slling zfs_source_t src; 33224451Seschrock zpool_prop_t prop; 33233912Slling nvlist_t *propval; 33243912Slling uint64_t value; 33253912Slling int err; 33263912Slling 33273912Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33283912Slling 33293912Slling mutex_enter(&spa->spa_props_lock); 33303912Slling /* If no props object, then just return empty nvlist */ 33313912Slling if (spa->spa_pool_props_object == 0) { 33323912Slling mutex_exit(&spa->spa_props_lock); 33333912Slling return (0); 33343912Slling } 33353912Slling 33363912Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 33373912Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 33383912Slling zap_cursor_advance(&zc)) { 33393912Slling 33403912Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 33413912Slling continue; 33423912Slling 33433912Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33443912Slling switch (za.za_integer_length) { 33453912Slling case 8: 33464451Seschrock if (zpool_prop_default_numeric(prop) == 33473912Slling za.za_first_integer) 33483912Slling src = ZFS_SRC_DEFAULT; 33493912Slling else 33503912Slling src = ZFS_SRC_LOCAL; 33513912Slling value = za.za_first_integer; 33523912Slling 33534451Seschrock if (prop == ZPOOL_PROP_BOOTFS) { 33543912Slling dsl_pool_t *dp; 33553912Slling dsl_dataset_t *ds = NULL; 33563912Slling char strval[MAXPATHLEN]; 33573912Slling 33583912Slling dp = spa_get_dsl(spa); 33593912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 33603912Slling if ((err = dsl_dataset_open_obj(dp, 33613912Slling za.za_first_integer, NULL, DS_MODE_NONE, 33623912Slling FTAG, &ds)) != 0) { 33633912Slling rw_exit(&dp->dp_config_rwlock); 33643912Slling break; 33653912Slling } 33663912Slling dsl_dataset_name(ds, strval); 33673912Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 33683912Slling rw_exit(&dp->dp_config_rwlock); 33693912Slling 33703912Slling VERIFY(nvlist_add_uint64(propval, 33713912Slling ZFS_PROP_SOURCE, src) == 0); 33723912Slling VERIFY(nvlist_add_string(propval, 33733912Slling ZFS_PROP_VALUE, strval) == 0); 33743912Slling } else { 33753912Slling VERIFY(nvlist_add_uint64(propval, 33763912Slling ZFS_PROP_SOURCE, src) == 0); 33773912Slling VERIFY(nvlist_add_uint64(propval, 33783912Slling ZFS_PROP_VALUE, value) == 0); 33793912Slling } 33803912Slling VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 33813912Slling propval) == 0); 33823912Slling break; 33833912Slling } 33843912Slling nvlist_free(propval); 33853912Slling } 33863912Slling zap_cursor_fini(&zc); 33873912Slling mutex_exit(&spa->spa_props_lock); 33883912Slling if (err && err != ENOENT) { 33893912Slling nvlist_free(*nvp); 33903912Slling return (err); 33913912Slling } 33923912Slling 33933912Slling return (0); 33943912Slling } 33953912Slling 33963912Slling /* 33973912Slling * If the bootfs property value is dsobj, clear it. 33983912Slling */ 33993912Slling void 34003912Slling spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 34013912Slling { 34023912Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 34033912Slling VERIFY(zap_remove(spa->spa_meta_objset, 34043912Slling spa->spa_pool_props_object, 34054451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 34063912Slling spa->spa_bootfs = 0; 34073912Slling } 34083912Slling } 34094451Seschrock 34104451Seschrock /* 34114451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 34124451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 34134451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 34144451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 34154451Seschrock * or zdb as real changes. 34164451Seschrock */ 34174451Seschrock void 34184451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 34194451Seschrock { 34204451Seschrock #ifdef _KERNEL 34214451Seschrock sysevent_t *ev; 34224451Seschrock sysevent_attr_list_t *attr = NULL; 34234451Seschrock sysevent_value_t value; 34244451Seschrock sysevent_id_t eid; 34254451Seschrock 34264451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 34274451Seschrock SE_SLEEP); 34284451Seschrock 34294451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34304451Seschrock value.value.sv_string = spa_name(spa); 34314451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 34324451Seschrock goto done; 34334451Seschrock 34344451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34354451Seschrock value.value.sv_uint64 = spa_guid(spa); 34364451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 34374451Seschrock goto done; 34384451Seschrock 34394451Seschrock if (vd) { 34404451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34414451Seschrock value.value.sv_uint64 = vd->vdev_guid; 34424451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 34434451Seschrock SE_SLEEP) != 0) 34444451Seschrock goto done; 34454451Seschrock 34464451Seschrock if (vd->vdev_path) { 34474451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34484451Seschrock value.value.sv_string = vd->vdev_path; 34494451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 34504451Seschrock &value, SE_SLEEP) != 0) 34514451Seschrock goto done; 34524451Seschrock } 34534451Seschrock } 34544451Seschrock 34554451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 34564451Seschrock 34574451Seschrock done: 34584451Seschrock if (attr) 34594451Seschrock sysevent_free_attr(attr); 34604451Seschrock sysevent_free(ev); 34614451Seschrock #endif 34624451Seschrock } 3463