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(); 122*4527Sperrin 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 189*4527Sperrin metaslab_class_destroy(spa->spa_log_class); 190*4527Sperrin spa->spa_log_class = NULL; 191*4527Sperrin 1921544Seschrock /* 1931544Seschrock * If this was part of an import or the open otherwise failed, we may 1941544Seschrock * still have errors left in the queues. Empty them just in case. 1951544Seschrock */ 1961544Seschrock spa_errlog_drain(spa); 1971544Seschrock 1981544Seschrock avl_destroy(&spa->spa_errlist_scrub); 1991544Seschrock avl_destroy(&spa->spa_errlist_last); 2001544Seschrock 201789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 202789Sahrens } 203789Sahrens 204789Sahrens /* 205789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 206789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 207789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 208789Sahrens * All vdev validation is done by the vdev_alloc() routine. 209789Sahrens */ 2102082Seschrock static int 2112082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 2122082Seschrock uint_t id, int atype) 213789Sahrens { 214789Sahrens nvlist_t **child; 215789Sahrens uint_t c, children; 2162082Seschrock int error; 2172082Seschrock 2182082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 2192082Seschrock return (error); 2202082Seschrock 2212082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 2222082Seschrock return (0); 223789Sahrens 224789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 225789Sahrens &child, &children) != 0) { 2262082Seschrock vdev_free(*vdp); 2272082Seschrock *vdp = NULL; 2282082Seschrock return (EINVAL); 229789Sahrens } 230789Sahrens 231789Sahrens for (c = 0; c < children; c++) { 2322082Seschrock vdev_t *vd; 2332082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 2342082Seschrock atype)) != 0) { 2352082Seschrock vdev_free(*vdp); 2362082Seschrock *vdp = NULL; 2372082Seschrock return (error); 238789Sahrens } 239789Sahrens } 240789Sahrens 2412082Seschrock ASSERT(*vdp != NULL); 2422082Seschrock 2432082Seschrock return (0); 244789Sahrens } 245789Sahrens 246789Sahrens /* 247789Sahrens * Opposite of spa_load(). 248789Sahrens */ 249789Sahrens static void 250789Sahrens spa_unload(spa_t *spa) 251789Sahrens { 2522082Seschrock int i; 2532082Seschrock 254789Sahrens /* 2551544Seschrock * Stop async tasks. 2561544Seschrock */ 2571544Seschrock spa_async_suspend(spa); 2581544Seschrock 2591544Seschrock /* 260789Sahrens * Stop syncing. 261789Sahrens */ 262789Sahrens if (spa->spa_sync_on) { 263789Sahrens txg_sync_stop(spa->spa_dsl_pool); 264789Sahrens spa->spa_sync_on = B_FALSE; 265789Sahrens } 266789Sahrens 267789Sahrens /* 268789Sahrens * Wait for any outstanding prefetch I/O to complete. 269789Sahrens */ 2701544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2711544Seschrock spa_config_exit(spa, FTAG); 272789Sahrens 273789Sahrens /* 274789Sahrens * Close the dsl pool. 275789Sahrens */ 276789Sahrens if (spa->spa_dsl_pool) { 277789Sahrens dsl_pool_close(spa->spa_dsl_pool); 278789Sahrens spa->spa_dsl_pool = NULL; 279789Sahrens } 280789Sahrens 281789Sahrens /* 282789Sahrens * Close all vdevs. 283789Sahrens */ 2841585Sbonwick if (spa->spa_root_vdev) 285789Sahrens vdev_free(spa->spa_root_vdev); 2861585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 2871544Seschrock 2882082Seschrock for (i = 0; i < spa->spa_nspares; i++) 2892082Seschrock vdev_free(spa->spa_spares[i]); 2902082Seschrock if (spa->spa_spares) { 2912082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 2922082Seschrock spa->spa_spares = NULL; 2932082Seschrock } 2942082Seschrock if (spa->spa_sparelist) { 2952082Seschrock nvlist_free(spa->spa_sparelist); 2962082Seschrock spa->spa_sparelist = NULL; 2972082Seschrock } 2982082Seschrock 2991544Seschrock spa->spa_async_suspended = 0; 300789Sahrens } 301789Sahrens 302789Sahrens /* 3032082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 3042082Seschrock * this pool. When this is called, we have some form of basic information in 3052082Seschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 3062082Seschrock * re-generate a more complete list including status information. 3072082Seschrock */ 3082082Seschrock static void 3092082Seschrock spa_load_spares(spa_t *spa) 3102082Seschrock { 3112082Seschrock nvlist_t **spares; 3122082Seschrock uint_t nspares; 3132082Seschrock int i; 3143377Seschrock vdev_t *vd, *tvd; 3152082Seschrock 3162082Seschrock /* 3172082Seschrock * First, close and free any existing spare vdevs. 3182082Seschrock */ 3192082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3203377Seschrock vd = spa->spa_spares[i]; 3213377Seschrock 3223377Seschrock /* Undo the call to spa_activate() below */ 3233377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 3243377Seschrock tvd->vdev_isspare) 3253377Seschrock spa_spare_remove(tvd); 3263377Seschrock vdev_close(vd); 3273377Seschrock vdev_free(vd); 3282082Seschrock } 3293377Seschrock 3302082Seschrock if (spa->spa_spares) 3312082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 3322082Seschrock 3332082Seschrock if (spa->spa_sparelist == NULL) 3342082Seschrock nspares = 0; 3352082Seschrock else 3362082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 3372082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 3382082Seschrock 3392082Seschrock spa->spa_nspares = (int)nspares; 3402082Seschrock spa->spa_spares = NULL; 3412082Seschrock 3422082Seschrock if (nspares == 0) 3432082Seschrock return; 3442082Seschrock 3452082Seschrock /* 3462082Seschrock * Construct the array of vdevs, opening them to get status in the 3473377Seschrock * process. For each spare, there is potentially two different vdev_t 3483377Seschrock * structures associated with it: one in the list of spares (used only 3493377Seschrock * for basic validation purposes) and one in the active vdev 3503377Seschrock * configuration (if it's spared in). During this phase we open and 3513377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 3523377Seschrock * active configuration, then we also mark this vdev as an active spare. 3532082Seschrock */ 3542082Seschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 3552082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3562082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 3572082Seschrock VDEV_ALLOC_SPARE) == 0); 3582082Seschrock ASSERT(vd != NULL); 3592082Seschrock 3602082Seschrock spa->spa_spares[i] = vd; 3612082Seschrock 3623377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 3633377Seschrock if (!tvd->vdev_isspare) 3643377Seschrock spa_spare_add(tvd); 3653377Seschrock 3663377Seschrock /* 3673377Seschrock * We only mark the spare active if we were successfully 3683377Seschrock * able to load the vdev. Otherwise, importing a pool 3693377Seschrock * with a bad active spare would result in strange 3703377Seschrock * behavior, because multiple pool would think the spare 3713377Seschrock * is actively in use. 3723377Seschrock * 3733377Seschrock * There is a vulnerability here to an equally bizarre 3743377Seschrock * circumstance, where a dead active spare is later 3753377Seschrock * brought back to life (onlined or otherwise). Given 3763377Seschrock * the rarity of this scenario, and the extra complexity 3773377Seschrock * it adds, we ignore the possibility. 3783377Seschrock */ 3793377Seschrock if (!vdev_is_dead(tvd)) 3803377Seschrock spa_spare_activate(tvd); 3813377Seschrock } 3823377Seschrock 3832082Seschrock if (vdev_open(vd) != 0) 3842082Seschrock continue; 3852082Seschrock 3862082Seschrock vd->vdev_top = vd; 3872082Seschrock (void) vdev_validate_spare(vd); 3882082Seschrock } 3892082Seschrock 3902082Seschrock /* 3912082Seschrock * Recompute the stashed list of spares, with status information 3922082Seschrock * this time. 3932082Seschrock */ 3942082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3952082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3962082Seschrock 3972082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 3982082Seschrock for (i = 0; i < spa->spa_nspares; i++) 3992082Seschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 4002082Seschrock B_TRUE, B_TRUE); 4012082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 4022082Seschrock spares, spa->spa_nspares) == 0); 4032082Seschrock for (i = 0; i < spa->spa_nspares; i++) 4042082Seschrock nvlist_free(spares[i]); 4052082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 4062082Seschrock } 4072082Seschrock 4082082Seschrock static int 4092082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 4102082Seschrock { 4112082Seschrock dmu_buf_t *db; 4122082Seschrock char *packed = NULL; 4132082Seschrock size_t nvsize = 0; 4142082Seschrock int error; 4152082Seschrock *value = NULL; 4162082Seschrock 4172082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 4182082Seschrock nvsize = *(uint64_t *)db->db_data; 4192082Seschrock dmu_buf_rele(db, FTAG); 4202082Seschrock 4212082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 4222082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 4232082Seschrock if (error == 0) 4242082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 4252082Seschrock kmem_free(packed, nvsize); 4262082Seschrock 4272082Seschrock return (error); 4282082Seschrock } 4292082Seschrock 4302082Seschrock /* 4314451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 4324451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 4334451Seschrock */ 4344451Seschrock static void 4354451Seschrock spa_check_removed(vdev_t *vd) 4364451Seschrock { 4374451Seschrock int c; 4384451Seschrock 4394451Seschrock for (c = 0; c < vd->vdev_children; c++) 4404451Seschrock spa_check_removed(vd->vdev_child[c]); 4414451Seschrock 4424451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 4434451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 4444451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 4454451Seschrock } 4464451Seschrock } 4474451Seschrock 4484451Seschrock /* 449789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 4501544Seschrock * source of configuration information. 451789Sahrens */ 452789Sahrens static int 4531544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 454789Sahrens { 455789Sahrens int error = 0; 456789Sahrens nvlist_t *nvroot = NULL; 457789Sahrens vdev_t *rvd; 458789Sahrens uberblock_t *ub = &spa->spa_uberblock; 4591635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 460789Sahrens uint64_t pool_guid; 4612082Seschrock uint64_t version; 462789Sahrens zio_t *zio; 4634451Seschrock uint64_t autoreplace = 0; 464789Sahrens 4651544Seschrock spa->spa_load_state = state; 4661635Sbonwick 467789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 4681733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 4691544Seschrock error = EINVAL; 4701544Seschrock goto out; 4711544Seschrock } 472789Sahrens 4732082Seschrock /* 4742082Seschrock * Versioning wasn't explicitly added to the label until later, so if 4752082Seschrock * it's not present treat it as the initial version. 4762082Seschrock */ 4772082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 4782082Seschrock version = ZFS_VERSION_INITIAL; 4792082Seschrock 4801733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 4811733Sbonwick &spa->spa_config_txg); 4821733Sbonwick 4831635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 4841544Seschrock spa_guid_exists(pool_guid, 0)) { 4851544Seschrock error = EEXIST; 4861544Seschrock goto out; 4871544Seschrock } 488789Sahrens 4892174Seschrock spa->spa_load_guid = pool_guid; 4902174Seschrock 491789Sahrens /* 4922082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 4932082Seschrock * value that will be returned by spa_version() since parsing the 4942082Seschrock * configuration requires knowing the version number. 495789Sahrens */ 4961544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 4972082Seschrock spa->spa_ubsync.ub_version = version; 4982082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 4991544Seschrock spa_config_exit(spa, FTAG); 500789Sahrens 5012082Seschrock if (error != 0) 5021544Seschrock goto out; 503789Sahrens 5041585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 505789Sahrens ASSERT(spa_guid(spa) == pool_guid); 506789Sahrens 507789Sahrens /* 508789Sahrens * Try to open all vdevs, loading each label in the process. 509789Sahrens */ 5104070Smc142369 error = vdev_open(rvd); 5114070Smc142369 if (error != 0) 5121544Seschrock goto out; 513789Sahrens 514789Sahrens /* 5151986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 5161986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 5171986Seschrock * flag. 5181986Seschrock */ 5191986Seschrock spa_config_enter(spa, RW_READER, FTAG); 5201986Seschrock error = vdev_validate(rvd); 5211986Seschrock spa_config_exit(spa, FTAG); 5221986Seschrock 5234070Smc142369 if (error != 0) 5241986Seschrock goto out; 5251986Seschrock 5261986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 5271986Seschrock error = ENXIO; 5281986Seschrock goto out; 5291986Seschrock } 5301986Seschrock 5311986Seschrock /* 532789Sahrens * Find the best uberblock. 533789Sahrens */ 534789Sahrens bzero(ub, sizeof (uberblock_t)); 535789Sahrens 536789Sahrens zio = zio_root(spa, NULL, NULL, 537789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 538789Sahrens vdev_uberblock_load(zio, rvd, ub); 539789Sahrens error = zio_wait(zio); 540789Sahrens 541789Sahrens /* 542789Sahrens * If we weren't able to find a single valid uberblock, return failure. 543789Sahrens */ 544789Sahrens if (ub->ub_txg == 0) { 5451760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5461760Seschrock VDEV_AUX_CORRUPT_DATA); 5471544Seschrock error = ENXIO; 5481544Seschrock goto out; 5491544Seschrock } 5501544Seschrock 5511544Seschrock /* 5521544Seschrock * If the pool is newer than the code, we can't open it. 5531544Seschrock */ 5541760Seschrock if (ub->ub_version > ZFS_VERSION) { 5551760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5561760Seschrock VDEV_AUX_VERSION_NEWER); 5571544Seschrock error = ENOTSUP; 5581544Seschrock goto out; 559789Sahrens } 560789Sahrens 561789Sahrens /* 562789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 563789Sahrens * incomplete configuration. 564789Sahrens */ 5651732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 5661544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5671544Seschrock VDEV_AUX_BAD_GUID_SUM); 5681544Seschrock error = ENXIO; 5691544Seschrock goto out; 570789Sahrens } 571789Sahrens 572789Sahrens /* 573789Sahrens * Initialize internal SPA structures. 574789Sahrens */ 575789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 576789Sahrens spa->spa_ubsync = spa->spa_uberblock; 577789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 5781544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 5791544Seschrock if (error) { 5801544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5811544Seschrock VDEV_AUX_CORRUPT_DATA); 5821544Seschrock goto out; 5831544Seschrock } 584789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 585789Sahrens 5861544Seschrock if (zap_lookup(spa->spa_meta_objset, 587789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 5881544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 5891544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5901544Seschrock VDEV_AUX_CORRUPT_DATA); 5911544Seschrock error = EIO; 5921544Seschrock goto out; 5931544Seschrock } 594789Sahrens 595789Sahrens if (!mosconfig) { 5962082Seschrock nvlist_t *newconfig; 5973975Sek110237 uint64_t hostid; 5982082Seschrock 5992082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 6001544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6011544Seschrock VDEV_AUX_CORRUPT_DATA); 6021544Seschrock error = EIO; 6031544Seschrock goto out; 6041544Seschrock } 605789Sahrens 6063975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 6073975Sek110237 &hostid) == 0) { 6083975Sek110237 char *hostname; 6093975Sek110237 unsigned long myhostid = 0; 6103975Sek110237 6113975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 6123975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 6133975Sek110237 6143975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 6154178Slling if (hostid != 0 && myhostid != 0 && 6164178Slling (unsigned long)hostid != myhostid) { 6173975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 6183975Sek110237 "loaded as it was last accessed by " 6193975Sek110237 "another system (host: %s hostid: 0x%lx). " 6203975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 6213975Sek110237 spa->spa_name, hostname, 6223975Sek110237 (unsigned long)hostid); 6233975Sek110237 error = EBADF; 6243975Sek110237 goto out; 6253975Sek110237 } 6263975Sek110237 } 6273975Sek110237 628789Sahrens spa_config_set(spa, newconfig); 629789Sahrens spa_unload(spa); 630789Sahrens spa_deactivate(spa); 631789Sahrens spa_activate(spa); 632789Sahrens 6331544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 6341544Seschrock } 6351544Seschrock 6361544Seschrock if (zap_lookup(spa->spa_meta_objset, 6371544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 6381544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 6391544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6401544Seschrock VDEV_AUX_CORRUPT_DATA); 6411544Seschrock error = EIO; 6421544Seschrock goto out; 643789Sahrens } 644789Sahrens 6451544Seschrock /* 6462082Seschrock * Load the bit that tells us to use the new accounting function 6472082Seschrock * (raid-z deflation). If we have an older pool, this will not 6482082Seschrock * be present. 6492082Seschrock */ 6502082Seschrock error = zap_lookup(spa->spa_meta_objset, 6512082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 6522082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 6532082Seschrock if (error != 0 && error != ENOENT) { 6542082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6552082Seschrock VDEV_AUX_CORRUPT_DATA); 6562082Seschrock error = EIO; 6572082Seschrock goto out; 6582082Seschrock } 6592082Seschrock 6602082Seschrock /* 6611544Seschrock * Load the persistent error log. If we have an older pool, this will 6621544Seschrock * not be present. 6631544Seschrock */ 6641544Seschrock error = zap_lookup(spa->spa_meta_objset, 6651544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 6661544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 6671807Sbonwick if (error != 0 && error != ENOENT) { 6681544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6691544Seschrock VDEV_AUX_CORRUPT_DATA); 6701544Seschrock error = EIO; 6711544Seschrock goto out; 6721544Seschrock } 6731544Seschrock 6741544Seschrock error = zap_lookup(spa->spa_meta_objset, 6751544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 6761544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 6771544Seschrock if (error != 0 && error != ENOENT) { 6781544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6791544Seschrock VDEV_AUX_CORRUPT_DATA); 6801544Seschrock error = EIO; 6811544Seschrock goto out; 6821544Seschrock } 683789Sahrens 684789Sahrens /* 6852926Sek110237 * Load the history object. If we have an older pool, this 6862926Sek110237 * will not be present. 6872926Sek110237 */ 6882926Sek110237 error = zap_lookup(spa->spa_meta_objset, 6892926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 6902926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 6912926Sek110237 if (error != 0 && error != ENOENT) { 6922926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6932926Sek110237 VDEV_AUX_CORRUPT_DATA); 6942926Sek110237 error = EIO; 6952926Sek110237 goto out; 6962926Sek110237 } 6972926Sek110237 6982926Sek110237 /* 6992082Seschrock * Load any hot spares for this pool. 7002082Seschrock */ 7012082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7022082Seschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 7032082Seschrock if (error != 0 && error != ENOENT) { 7042082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7052082Seschrock VDEV_AUX_CORRUPT_DATA); 7062082Seschrock error = EIO; 7072082Seschrock goto out; 7082082Seschrock } 7092082Seschrock if (error == 0) { 7102082Seschrock ASSERT(spa_version(spa) >= ZFS_VERSION_SPARES); 7112082Seschrock if (load_nvlist(spa, spa->spa_spares_object, 7122082Seschrock &spa->spa_sparelist) != 0) { 7132082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7142082Seschrock VDEV_AUX_CORRUPT_DATA); 7152082Seschrock error = EIO; 7162082Seschrock goto out; 7172082Seschrock } 7182082Seschrock 7192082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 7202082Seschrock spa_load_spares(spa); 7212082Seschrock spa_config_exit(spa, FTAG); 7222082Seschrock } 7232082Seschrock 7243912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7253912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 7263912Slling 7273912Slling if (error && error != ENOENT) { 7283912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 7293912Slling VDEV_AUX_CORRUPT_DATA); 7303912Slling error = EIO; 7313912Slling goto out; 7323912Slling } 7333912Slling 7343912Slling if (error == 0) { 7353912Slling (void) zap_lookup(spa->spa_meta_objset, 7363912Slling spa->spa_pool_props_object, 7374451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 7383912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 7394451Seschrock (void) zap_lookup(spa->spa_meta_objset, 7404451Seschrock spa->spa_pool_props_object, 7414451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 7424451Seschrock sizeof (uint64_t), 1, &autoreplace); 7433912Slling } 7443912Slling 7452082Seschrock /* 7464451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 7474451Seschrock * the ZFS DE that it should not issue any faults for unopenable 7484451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 7494451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 7504451Seschrock * over. 7514451Seschrock */ 7524451Seschrock if (autoreplace) 7534451Seschrock spa_check_removed(spa->spa_root_vdev); 7544451Seschrock 7554451Seschrock /* 7561986Seschrock * Load the vdev state for all toplevel vdevs. 757789Sahrens */ 7581986Seschrock vdev_load(rvd); 759789Sahrens 760789Sahrens /* 761789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 762789Sahrens */ 7631544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 764789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 7651544Seschrock spa_config_exit(spa, FTAG); 766789Sahrens 767789Sahrens /* 768789Sahrens * Check the state of the root vdev. If it can't be opened, it 769789Sahrens * indicates one or more toplevel vdevs are faulted. 770789Sahrens */ 7711544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 7721544Seschrock error = ENXIO; 7731544Seschrock goto out; 7741544Seschrock } 775789Sahrens 7761544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 7771635Sbonwick dmu_tx_t *tx; 7781635Sbonwick int need_update = B_FALSE; 7791585Sbonwick int c; 7801601Sbonwick 7811635Sbonwick /* 7821635Sbonwick * Claim log blocks that haven't been committed yet. 7831635Sbonwick * This must all happen in a single txg. 7841635Sbonwick */ 7851601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 786789Sahrens spa_first_txg(spa)); 7872417Sahrens (void) dmu_objset_find(spa->spa_name, 7882417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 789789Sahrens dmu_tx_commit(tx); 790789Sahrens 791789Sahrens spa->spa_sync_on = B_TRUE; 792789Sahrens txg_sync_start(spa->spa_dsl_pool); 793789Sahrens 794789Sahrens /* 795789Sahrens * Wait for all claims to sync. 796789Sahrens */ 797789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 7981585Sbonwick 7991585Sbonwick /* 8001635Sbonwick * If the config cache is stale, or we have uninitialized 8011635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 8021585Sbonwick */ 8031635Sbonwick if (config_cache_txg != spa->spa_config_txg || 8041635Sbonwick state == SPA_LOAD_IMPORT) 8051635Sbonwick need_update = B_TRUE; 8061635Sbonwick 8071635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 8081635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 8091635Sbonwick need_update = B_TRUE; 8101585Sbonwick 8111585Sbonwick /* 8121635Sbonwick * Update the config cache asychronously in case we're the 8131635Sbonwick * root pool, in which case the config cache isn't writable yet. 8141585Sbonwick */ 8151635Sbonwick if (need_update) 8161635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 817789Sahrens } 818789Sahrens 8191544Seschrock error = 0; 8201544Seschrock out: 8212082Seschrock if (error && error != EBADF) 8221544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 8231544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 8241544Seschrock spa->spa_ena = 0; 8251544Seschrock 8261544Seschrock return (error); 827789Sahrens } 828789Sahrens 829789Sahrens /* 830789Sahrens * Pool Open/Import 831789Sahrens * 832789Sahrens * The import case is identical to an open except that the configuration is sent 833789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 834789Sahrens * case of an open, the pool configuration will exist in the 8354451Seschrock * POOL_STATE_UNINITIALIZED state. 836789Sahrens * 837789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 838789Sahrens * the same time open the pool, without having to keep around the spa_t in some 839789Sahrens * ambiguous state. 840789Sahrens */ 841789Sahrens static int 842789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 843789Sahrens { 844789Sahrens spa_t *spa; 845789Sahrens int error; 846789Sahrens int loaded = B_FALSE; 847789Sahrens int locked = B_FALSE; 848789Sahrens 849789Sahrens *spapp = NULL; 850789Sahrens 851789Sahrens /* 852789Sahrens * As disgusting as this is, we need to support recursive calls to this 853789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 854789Sahrens * up calling spa_open() again. The real fix is to figure out how to 855789Sahrens * avoid dsl_dir_open() calling this in the first place. 856789Sahrens */ 857789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 858789Sahrens mutex_enter(&spa_namespace_lock); 859789Sahrens locked = B_TRUE; 860789Sahrens } 861789Sahrens 862789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 863789Sahrens if (locked) 864789Sahrens mutex_exit(&spa_namespace_lock); 865789Sahrens return (ENOENT); 866789Sahrens } 867789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 868789Sahrens 869789Sahrens spa_activate(spa); 870789Sahrens 8711635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 872789Sahrens 873789Sahrens if (error == EBADF) { 874789Sahrens /* 8751986Seschrock * If vdev_validate() returns failure (indicated by 8761986Seschrock * EBADF), it indicates that one of the vdevs indicates 8771986Seschrock * that the pool has been exported or destroyed. If 8781986Seschrock * this is the case, the config cache is out of sync and 8791986Seschrock * we should remove the pool from the namespace. 880789Sahrens */ 8812082Seschrock zfs_post_ok(spa, NULL); 882789Sahrens spa_unload(spa); 883789Sahrens spa_deactivate(spa); 884789Sahrens spa_remove(spa); 885789Sahrens spa_config_sync(); 886789Sahrens if (locked) 887789Sahrens mutex_exit(&spa_namespace_lock); 888789Sahrens return (ENOENT); 8891544Seschrock } 8901544Seschrock 8911544Seschrock if (error) { 892789Sahrens /* 893789Sahrens * We can't open the pool, but we still have useful 894789Sahrens * information: the state of each vdev after the 895789Sahrens * attempted vdev_open(). Return this to the user. 896789Sahrens */ 8971635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 8981635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 899789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 900789Sahrens B_TRUE); 9011635Sbonwick spa_config_exit(spa, FTAG); 9021635Sbonwick } 903789Sahrens spa_unload(spa); 904789Sahrens spa_deactivate(spa); 9051544Seschrock spa->spa_last_open_failed = B_TRUE; 906789Sahrens if (locked) 907789Sahrens mutex_exit(&spa_namespace_lock); 908789Sahrens *spapp = NULL; 909789Sahrens return (error); 9101544Seschrock } else { 9111544Seschrock zfs_post_ok(spa, NULL); 9121544Seschrock spa->spa_last_open_failed = B_FALSE; 913789Sahrens } 914789Sahrens 915789Sahrens loaded = B_TRUE; 916789Sahrens } 917789Sahrens 918789Sahrens spa_open_ref(spa, tag); 9194451Seschrock 9204451Seschrock /* 9214451Seschrock * If we just loaded the pool, resilver anything that's out of date. 9224451Seschrock */ 9234451Seschrock if (loaded && (spa_mode & FWRITE)) 9244451Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 9254451Seschrock 926789Sahrens if (locked) 927789Sahrens mutex_exit(&spa_namespace_lock); 928789Sahrens 929789Sahrens *spapp = spa; 930789Sahrens 931789Sahrens if (config != NULL) { 9321544Seschrock spa_config_enter(spa, RW_READER, FTAG); 933789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 9341544Seschrock spa_config_exit(spa, FTAG); 935789Sahrens } 936789Sahrens 937789Sahrens return (0); 938789Sahrens } 939789Sahrens 940789Sahrens int 941789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 942789Sahrens { 943789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 944789Sahrens } 945789Sahrens 9461544Seschrock /* 9471544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 9481544Seschrock * preventing it from being exported or destroyed. 9491544Seschrock */ 9501544Seschrock spa_t * 9511544Seschrock spa_inject_addref(char *name) 9521544Seschrock { 9531544Seschrock spa_t *spa; 9541544Seschrock 9551544Seschrock mutex_enter(&spa_namespace_lock); 9561544Seschrock if ((spa = spa_lookup(name)) == NULL) { 9571544Seschrock mutex_exit(&spa_namespace_lock); 9581544Seschrock return (NULL); 9591544Seschrock } 9601544Seschrock spa->spa_inject_ref++; 9611544Seschrock mutex_exit(&spa_namespace_lock); 9621544Seschrock 9631544Seschrock return (spa); 9641544Seschrock } 9651544Seschrock 9661544Seschrock void 9671544Seschrock spa_inject_delref(spa_t *spa) 9681544Seschrock { 9691544Seschrock mutex_enter(&spa_namespace_lock); 9701544Seschrock spa->spa_inject_ref--; 9711544Seschrock mutex_exit(&spa_namespace_lock); 9721544Seschrock } 9731544Seschrock 9742082Seschrock static void 9752082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 9762082Seschrock { 9772082Seschrock nvlist_t **spares; 9782082Seschrock uint_t i, nspares; 9792082Seschrock nvlist_t *nvroot; 9802082Seschrock uint64_t guid; 9812082Seschrock vdev_stat_t *vs; 9822082Seschrock uint_t vsc; 9833377Seschrock uint64_t pool; 9842082Seschrock 9852082Seschrock if (spa->spa_nspares == 0) 9862082Seschrock return; 9872082Seschrock 9882082Seschrock VERIFY(nvlist_lookup_nvlist(config, 9892082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 9902082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 9912082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 9922082Seschrock if (nspares != 0) { 9932082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 9942082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 9952082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 9962082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 9972082Seschrock 9982082Seschrock /* 9992082Seschrock * Go through and find any spares which have since been 10002082Seschrock * repurposed as an active spare. If this is the case, update 10012082Seschrock * their status appropriately. 10022082Seschrock */ 10032082Seschrock for (i = 0; i < nspares; i++) { 10042082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 10052082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 10063377Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 10072082Seschrock VERIFY(nvlist_lookup_uint64_array( 10082082Seschrock spares[i], ZPOOL_CONFIG_STATS, 10092082Seschrock (uint64_t **)&vs, &vsc) == 0); 10102082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 10112082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 10122082Seschrock } 10132082Seschrock } 10142082Seschrock } 10152082Seschrock } 10162082Seschrock 1017789Sahrens int 10181544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1019789Sahrens { 1020789Sahrens int error; 1021789Sahrens spa_t *spa; 1022789Sahrens 1023789Sahrens *config = NULL; 1024789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1025789Sahrens 10262082Seschrock if (spa && *config != NULL) { 10271544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 10281544Seschrock spa_get_errlog_size(spa)) == 0); 10291544Seschrock 10302082Seschrock spa_add_spares(spa, *config); 10312082Seschrock } 10322082Seschrock 10331544Seschrock /* 10341544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 10351544Seschrock * and call spa_lookup() directly. 10361544Seschrock */ 10371544Seschrock if (altroot) { 10381544Seschrock if (spa == NULL) { 10391544Seschrock mutex_enter(&spa_namespace_lock); 10401544Seschrock spa = spa_lookup(name); 10411544Seschrock if (spa) 10421544Seschrock spa_altroot(spa, altroot, buflen); 10431544Seschrock else 10441544Seschrock altroot[0] = '\0'; 10451544Seschrock spa = NULL; 10461544Seschrock mutex_exit(&spa_namespace_lock); 10471544Seschrock } else { 10481544Seschrock spa_altroot(spa, altroot, buflen); 10491544Seschrock } 10501544Seschrock } 10511544Seschrock 1052789Sahrens if (spa != NULL) 1053789Sahrens spa_close(spa, FTAG); 1054789Sahrens 1055789Sahrens return (error); 1056789Sahrens } 1057789Sahrens 1058789Sahrens /* 10592082Seschrock * Validate that the 'spares' array is well formed. We must have an array of 10603377Seschrock * nvlists, each which describes a valid leaf vdev. If this is an import (mode 10613377Seschrock * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 10623377Seschrock * as they are well-formed. 10632082Seschrock */ 10642082Seschrock static int 10652082Seschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 10662082Seschrock { 10672082Seschrock nvlist_t **spares; 10682082Seschrock uint_t i, nspares; 10692082Seschrock vdev_t *vd; 10702082Seschrock int error; 10712082Seschrock 10722082Seschrock /* 10732082Seschrock * It's acceptable to have no spares specified. 10742082Seschrock */ 10752082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10762082Seschrock &spares, &nspares) != 0) 10772082Seschrock return (0); 10782082Seschrock 10792082Seschrock if (nspares == 0) 10802082Seschrock return (EINVAL); 10812082Seschrock 10822082Seschrock /* 10832082Seschrock * Make sure the pool is formatted with a version that supports hot 10842082Seschrock * spares. 10852082Seschrock */ 10862082Seschrock if (spa_version(spa) < ZFS_VERSION_SPARES) 10872082Seschrock return (ENOTSUP); 10882082Seschrock 10893377Seschrock /* 10903377Seschrock * Set the pending spare list so we correctly handle device in-use 10913377Seschrock * checking. 10923377Seschrock */ 10933377Seschrock spa->spa_pending_spares = spares; 10943377Seschrock spa->spa_pending_nspares = nspares; 10953377Seschrock 10962082Seschrock for (i = 0; i < nspares; i++) { 10972082Seschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 10982082Seschrock mode)) != 0) 10993377Seschrock goto out; 11002082Seschrock 11012082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 11022082Seschrock vdev_free(vd); 11033377Seschrock error = EINVAL; 11043377Seschrock goto out; 11052082Seschrock } 11062082Seschrock 11072082Seschrock vd->vdev_top = vd; 11083377Seschrock 11093377Seschrock if ((error = vdev_open(vd)) == 0 && 11103377Seschrock (error = vdev_label_init(vd, crtxg, 11113377Seschrock VDEV_LABEL_SPARE)) == 0) { 11123377Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 11133377Seschrock vd->vdev_guid) == 0); 11142082Seschrock } 11152082Seschrock 11162082Seschrock vdev_free(vd); 11173377Seschrock 11183377Seschrock if (error && mode != VDEV_ALLOC_SPARE) 11193377Seschrock goto out; 11203377Seschrock else 11213377Seschrock error = 0; 11222082Seschrock } 11232082Seschrock 11243377Seschrock out: 11253377Seschrock spa->spa_pending_spares = NULL; 11263377Seschrock spa->spa_pending_nspares = 0; 11273377Seschrock return (error); 11282082Seschrock } 11292082Seschrock 11302082Seschrock /* 1131789Sahrens * Pool Creation 1132789Sahrens */ 1133789Sahrens int 11341635Sbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot) 1135789Sahrens { 1136789Sahrens spa_t *spa; 11371635Sbonwick vdev_t *rvd; 1138789Sahrens dsl_pool_t *dp; 1139789Sahrens dmu_tx_t *tx; 11402082Seschrock int c, error = 0; 1141789Sahrens uint64_t txg = TXG_INITIAL; 11422082Seschrock nvlist_t **spares; 11432082Seschrock uint_t nspares; 1144789Sahrens 1145789Sahrens /* 1146789Sahrens * If this pool already exists, return failure. 1147789Sahrens */ 1148789Sahrens mutex_enter(&spa_namespace_lock); 1149789Sahrens if (spa_lookup(pool) != NULL) { 1150789Sahrens mutex_exit(&spa_namespace_lock); 1151789Sahrens return (EEXIST); 1152789Sahrens } 1153789Sahrens 1154789Sahrens /* 1155789Sahrens * Allocate a new spa_t structure. 1156789Sahrens */ 11571635Sbonwick spa = spa_add(pool, altroot); 1158789Sahrens spa_activate(spa); 1159789Sahrens 1160789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 11611760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 1162789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1163789Sahrens 11641635Sbonwick /* 11651635Sbonwick * Create the root vdev. 11661635Sbonwick */ 11671635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 11681635Sbonwick 11692082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 11702082Seschrock 11712082Seschrock ASSERT(error != 0 || rvd != NULL); 11722082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 11732082Seschrock 11742082Seschrock if (error == 0 && rvd->vdev_children == 0) 11751635Sbonwick error = EINVAL; 11762082Seschrock 11772082Seschrock if (error == 0 && 11782082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 11792082Seschrock (error = spa_validate_spares(spa, nvroot, txg, 11802082Seschrock VDEV_ALLOC_ADD)) == 0) { 11812082Seschrock for (c = 0; c < rvd->vdev_children; c++) 11822082Seschrock vdev_init(rvd->vdev_child[c], txg); 11832082Seschrock vdev_config_dirty(rvd); 11841635Sbonwick } 11851635Sbonwick 11861635Sbonwick spa_config_exit(spa, FTAG); 1187789Sahrens 11882082Seschrock if (error != 0) { 1189789Sahrens spa_unload(spa); 1190789Sahrens spa_deactivate(spa); 1191789Sahrens spa_remove(spa); 1192789Sahrens mutex_exit(&spa_namespace_lock); 1193789Sahrens return (error); 1194789Sahrens } 1195789Sahrens 11962082Seschrock /* 11972082Seschrock * Get the list of spares, if specified. 11982082Seschrock */ 11992082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 12002082Seschrock &spares, &nspares) == 0) { 12012082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 12022082Seschrock KM_SLEEP) == 0); 12032082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 12042082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 12052082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12062082Seschrock spa_load_spares(spa); 12072082Seschrock spa_config_exit(spa, FTAG); 12082082Seschrock spa->spa_sync_spares = B_TRUE; 12092082Seschrock } 12102082Seschrock 1211789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1212789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1213789Sahrens 1214789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1215789Sahrens 1216789Sahrens /* 1217789Sahrens * Create the pool config object. 1218789Sahrens */ 1219789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1220789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1221789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1222789Sahrens 12231544Seschrock if (zap_add(spa->spa_meta_objset, 1224789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 12251544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 12261544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 12271544Seschrock } 1228789Sahrens 12292082Seschrock /* Newly created pools are always deflated. */ 12302082Seschrock spa->spa_deflate = TRUE; 12312082Seschrock if (zap_add(spa->spa_meta_objset, 12322082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12332082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 12342082Seschrock cmn_err(CE_PANIC, "failed to add deflate"); 12352082Seschrock } 12362082Seschrock 1237789Sahrens /* 1238789Sahrens * Create the deferred-free bplist object. Turn off compression 1239789Sahrens * because sync-to-convergence takes longer if the blocksize 1240789Sahrens * keeps changing. 1241789Sahrens */ 1242789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1243789Sahrens 1 << 14, tx); 1244789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1245789Sahrens ZIO_COMPRESS_OFF, tx); 1246789Sahrens 12471544Seschrock if (zap_add(spa->spa_meta_objset, 1248789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12491544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 12501544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 12511544Seschrock } 1252789Sahrens 12532926Sek110237 /* 12542926Sek110237 * Create the pool's history object. 12552926Sek110237 */ 12562926Sek110237 spa_history_create_obj(spa, tx); 12572926Sek110237 1258789Sahrens dmu_tx_commit(tx); 1259789Sahrens 12604451Seschrock spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 1261789Sahrens spa->spa_sync_on = B_TRUE; 1262789Sahrens txg_sync_start(spa->spa_dsl_pool); 1263789Sahrens 1264789Sahrens /* 1265789Sahrens * We explicitly wait for the first transaction to complete so that our 1266789Sahrens * bean counters are appropriately updated. 1267789Sahrens */ 1268789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1269789Sahrens 1270789Sahrens spa_config_sync(); 1271789Sahrens 1272789Sahrens mutex_exit(&spa_namespace_lock); 1273789Sahrens 1274789Sahrens return (0); 1275789Sahrens } 1276789Sahrens 1277789Sahrens /* 1278789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1279789Sahrens * then call spa_load() to do the dirty work. 1280789Sahrens */ 1281789Sahrens int 12821635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 1283789Sahrens { 1284789Sahrens spa_t *spa; 1285789Sahrens int error; 12862082Seschrock nvlist_t *nvroot; 12872082Seschrock nvlist_t **spares; 12882082Seschrock uint_t nspares; 1289789Sahrens 1290789Sahrens if (!(spa_mode & FWRITE)) 1291789Sahrens return (EROFS); 1292789Sahrens 1293789Sahrens /* 1294789Sahrens * If a pool with this name exists, return failure. 1295789Sahrens */ 1296789Sahrens mutex_enter(&spa_namespace_lock); 1297789Sahrens if (spa_lookup(pool) != NULL) { 1298789Sahrens mutex_exit(&spa_namespace_lock); 1299789Sahrens return (EEXIST); 1300789Sahrens } 1301789Sahrens 1302789Sahrens /* 13031635Sbonwick * Create and initialize the spa structure. 1304789Sahrens */ 13051635Sbonwick spa = spa_add(pool, altroot); 1306789Sahrens spa_activate(spa); 1307789Sahrens 1308789Sahrens /* 13091635Sbonwick * Pass off the heavy lifting to spa_load(). 13101732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 13111732Sbonwick * is actually the one to trust when doing an import. 13121601Sbonwick */ 13131732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1314789Sahrens 13152082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13162082Seschrock /* 13172082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 13182082Seschrock * and conflicts with spa_has_spare(). 13192082Seschrock */ 13202082Seschrock if (spa->spa_sparelist) { 13212082Seschrock nvlist_free(spa->spa_sparelist); 13222082Seschrock spa->spa_sparelist = NULL; 13232082Seschrock spa_load_spares(spa); 13242082Seschrock } 13252082Seschrock 13262082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 13272082Seschrock &nvroot) == 0); 13282082Seschrock if (error == 0) 13292082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 13302082Seschrock VDEV_ALLOC_SPARE); 13312082Seschrock spa_config_exit(spa, FTAG); 13322082Seschrock 13332082Seschrock if (error != 0) { 1334789Sahrens spa_unload(spa); 1335789Sahrens spa_deactivate(spa); 1336789Sahrens spa_remove(spa); 1337789Sahrens mutex_exit(&spa_namespace_lock); 1338789Sahrens return (error); 1339789Sahrens } 1340789Sahrens 13411635Sbonwick /* 13422082Seschrock * Override any spares as specified by the user, as these may have 13432082Seschrock * correct device names/devids, etc. 13442082Seschrock */ 13452082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 13462082Seschrock &spares, &nspares) == 0) { 13472082Seschrock if (spa->spa_sparelist) 13482082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 13492082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 13502082Seschrock else 13512082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 13522082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 13532082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 13542082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 13552082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 13562082Seschrock spa_load_spares(spa); 13572082Seschrock spa_config_exit(spa, FTAG); 13582082Seschrock spa->spa_sync_spares = B_TRUE; 13592082Seschrock } 13602082Seschrock 13612082Seschrock /* 13621635Sbonwick * Update the config cache to include the newly-imported pool. 13631635Sbonwick */ 13641635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 13651635Sbonwick 1366789Sahrens /* 1367789Sahrens * Resilver anything that's out of date. 1368789Sahrens */ 1369789Sahrens if (spa_mode & FWRITE) 1370789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1371789Sahrens 13724451Seschrock mutex_exit(&spa_namespace_lock); 13734451Seschrock 1374789Sahrens return (0); 1375789Sahrens } 1376789Sahrens 1377789Sahrens /* 1378789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1379789Sahrens * to get the vdev stats associated with the imported devices. 1380789Sahrens */ 1381789Sahrens #define TRYIMPORT_NAME "$import" 1382789Sahrens 1383789Sahrens nvlist_t * 1384789Sahrens spa_tryimport(nvlist_t *tryconfig) 1385789Sahrens { 1386789Sahrens nvlist_t *config = NULL; 1387789Sahrens char *poolname; 1388789Sahrens spa_t *spa; 1389789Sahrens uint64_t state; 1390789Sahrens 1391789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1392789Sahrens return (NULL); 1393789Sahrens 1394789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1395789Sahrens return (NULL); 1396789Sahrens 13971635Sbonwick /* 13981635Sbonwick * Create and initialize the spa structure. 13991635Sbonwick */ 1400789Sahrens mutex_enter(&spa_namespace_lock); 14011635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1402789Sahrens spa_activate(spa); 1403789Sahrens 1404789Sahrens /* 14051635Sbonwick * Pass off the heavy lifting to spa_load(). 14061732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 14071732Sbonwick * is actually the one to trust when doing an import. 1408789Sahrens */ 14091732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1410789Sahrens 1411789Sahrens /* 1412789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1413789Sahrens */ 1414789Sahrens if (spa->spa_root_vdev != NULL) { 14151635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1416789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14171635Sbonwick spa_config_exit(spa, FTAG); 1418789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1419789Sahrens poolname) == 0); 1420789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1421789Sahrens state) == 0); 14223975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 14233975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 14242082Seschrock 14252082Seschrock /* 14262082Seschrock * Add the list of hot spares. 14272082Seschrock */ 14282082Seschrock spa_add_spares(spa, config); 1429789Sahrens } 1430789Sahrens 1431789Sahrens spa_unload(spa); 1432789Sahrens spa_deactivate(spa); 1433789Sahrens spa_remove(spa); 1434789Sahrens mutex_exit(&spa_namespace_lock); 1435789Sahrens 1436789Sahrens return (config); 1437789Sahrens } 1438789Sahrens 1439789Sahrens /* 1440789Sahrens * Pool export/destroy 1441789Sahrens * 1442789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1443789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1444789Sahrens * update the pool state and sync all the labels to disk, removing the 1445789Sahrens * configuration from the cache afterwards. 1446789Sahrens */ 1447789Sahrens static int 14481775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1449789Sahrens { 1450789Sahrens spa_t *spa; 1451789Sahrens 14521775Sbillm if (oldconfig) 14531775Sbillm *oldconfig = NULL; 14541775Sbillm 1455789Sahrens if (!(spa_mode & FWRITE)) 1456789Sahrens return (EROFS); 1457789Sahrens 1458789Sahrens mutex_enter(&spa_namespace_lock); 1459789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1460789Sahrens mutex_exit(&spa_namespace_lock); 1461789Sahrens return (ENOENT); 1462789Sahrens } 1463789Sahrens 1464789Sahrens /* 14651544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 14661544Seschrock * reacquire the namespace lock, and see if we can export. 14671544Seschrock */ 14681544Seschrock spa_open_ref(spa, FTAG); 14691544Seschrock mutex_exit(&spa_namespace_lock); 14701544Seschrock spa_async_suspend(spa); 14711544Seschrock mutex_enter(&spa_namespace_lock); 14721544Seschrock spa_close(spa, FTAG); 14731544Seschrock 14741544Seschrock /* 1475789Sahrens * The pool will be in core if it's openable, 1476789Sahrens * in which case we can modify its state. 1477789Sahrens */ 1478789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1479789Sahrens /* 1480789Sahrens * Objsets may be open only because they're dirty, so we 1481789Sahrens * have to force it to sync before checking spa_refcnt. 1482789Sahrens */ 1483789Sahrens spa_scrub_suspend(spa); 1484789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1485789Sahrens 14861544Seschrock /* 14871544Seschrock * A pool cannot be exported or destroyed if there are active 14881544Seschrock * references. If we are resetting a pool, allow references by 14891544Seschrock * fault injection handlers. 14901544Seschrock */ 14911544Seschrock if (!spa_refcount_zero(spa) || 14921544Seschrock (spa->spa_inject_ref != 0 && 14931544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1494789Sahrens spa_scrub_resume(spa); 14951544Seschrock spa_async_resume(spa); 1496789Sahrens mutex_exit(&spa_namespace_lock); 1497789Sahrens return (EBUSY); 1498789Sahrens } 1499789Sahrens 1500789Sahrens spa_scrub_resume(spa); 1501789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1502789Sahrens 1503789Sahrens /* 1504789Sahrens * We want this to be reflected on every label, 1505789Sahrens * so mark them all dirty. spa_unload() will do the 1506789Sahrens * final sync that pushes these changes out. 1507789Sahrens */ 15081544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15091601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15101544Seschrock spa->spa_state = new_state; 15111635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 15121544Seschrock vdev_config_dirty(spa->spa_root_vdev); 15131601Sbonwick spa_config_exit(spa, FTAG); 15141544Seschrock } 1515789Sahrens } 1516789Sahrens 15174451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 15184451Seschrock 1519789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1520789Sahrens spa_unload(spa); 1521789Sahrens spa_deactivate(spa); 1522789Sahrens } 1523789Sahrens 15241775Sbillm if (oldconfig && spa->spa_config) 15251775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 15261775Sbillm 15271544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 15281544Seschrock spa_remove(spa); 15291544Seschrock spa_config_sync(); 15301544Seschrock } 1531789Sahrens mutex_exit(&spa_namespace_lock); 1532789Sahrens 1533789Sahrens return (0); 1534789Sahrens } 1535789Sahrens 1536789Sahrens /* 1537789Sahrens * Destroy a storage pool. 1538789Sahrens */ 1539789Sahrens int 1540789Sahrens spa_destroy(char *pool) 1541789Sahrens { 15421775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1543789Sahrens } 1544789Sahrens 1545789Sahrens /* 1546789Sahrens * Export a storage pool. 1547789Sahrens */ 1548789Sahrens int 15491775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1550789Sahrens { 15511775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1552789Sahrens } 1553789Sahrens 1554789Sahrens /* 15551544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 15561544Seschrock * from the namespace in any way. 15571544Seschrock */ 15581544Seschrock int 15591544Seschrock spa_reset(char *pool) 15601544Seschrock { 15611775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 15621544Seschrock } 15631544Seschrock 15641544Seschrock 15651544Seschrock /* 1566789Sahrens * ========================================================================== 1567789Sahrens * Device manipulation 1568789Sahrens * ========================================================================== 1569789Sahrens */ 1570789Sahrens 1571789Sahrens /* 1572*4527Sperrin * Add a device to a storage pool. 1573789Sahrens */ 1574789Sahrens int 1575789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1576789Sahrens { 1577789Sahrens uint64_t txg; 15781635Sbonwick int c, error; 1579789Sahrens vdev_t *rvd = spa->spa_root_vdev; 15801585Sbonwick vdev_t *vd, *tvd; 15812082Seschrock nvlist_t **spares; 15822082Seschrock uint_t i, nspares; 1583789Sahrens 1584789Sahrens txg = spa_vdev_enter(spa); 1585789Sahrens 15862082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 15872082Seschrock VDEV_ALLOC_ADD)) != 0) 15882082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 15892082Seschrock 15903377Seschrock spa->spa_pending_vdev = vd; 1591789Sahrens 15922082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 15932082Seschrock &spares, &nspares) != 0) 15942082Seschrock nspares = 0; 15952082Seschrock 15963377Seschrock if (vd->vdev_children == 0 && nspares == 0) { 15973377Seschrock spa->spa_pending_vdev = NULL; 15982082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 15993377Seschrock } 16002082Seschrock 16012082Seschrock if (vd->vdev_children != 0) { 16023377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 16033377Seschrock spa->spa_pending_vdev = NULL; 16042082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16052082Seschrock } 16062082Seschrock } 16072082Seschrock 16083377Seschrock /* 16093377Seschrock * We must validate the spares after checking the children. Otherwise, 16103377Seschrock * vdev_inuse() will blindly overwrite the spare. 16113377Seschrock */ 16123377Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 16133377Seschrock VDEV_ALLOC_ADD)) != 0) { 16143377Seschrock spa->spa_pending_vdev = NULL; 16153377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 16163377Seschrock } 16173377Seschrock 16183377Seschrock spa->spa_pending_vdev = NULL; 16193377Seschrock 16203377Seschrock /* 16213377Seschrock * Transfer each new top-level vdev from vd to rvd. 16223377Seschrock */ 16233377Seschrock for (c = 0; c < vd->vdev_children; c++) { 16243377Seschrock tvd = vd->vdev_child[c]; 16253377Seschrock vdev_remove_child(vd, tvd); 16263377Seschrock tvd->vdev_id = rvd->vdev_children; 16273377Seschrock vdev_add_child(rvd, tvd); 16283377Seschrock vdev_config_dirty(tvd); 16293377Seschrock } 16303377Seschrock 16312082Seschrock if (nspares != 0) { 16322082Seschrock if (spa->spa_sparelist != NULL) { 16332082Seschrock nvlist_t **oldspares; 16342082Seschrock uint_t oldnspares; 16352082Seschrock nvlist_t **newspares; 16362082Seschrock 16372082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 16382082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 16392082Seschrock 16402082Seschrock newspares = kmem_alloc(sizeof (void *) * 16412082Seschrock (nspares + oldnspares), KM_SLEEP); 16422082Seschrock for (i = 0; i < oldnspares; i++) 16432082Seschrock VERIFY(nvlist_dup(oldspares[i], 16442082Seschrock &newspares[i], KM_SLEEP) == 0); 16452082Seschrock for (i = 0; i < nspares; i++) 16462082Seschrock VERIFY(nvlist_dup(spares[i], 16472082Seschrock &newspares[i + oldnspares], 16482082Seschrock KM_SLEEP) == 0); 16492082Seschrock 16502082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 16512082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 16522082Seschrock 16532082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16542082Seschrock ZPOOL_CONFIG_SPARES, newspares, 16552082Seschrock nspares + oldnspares) == 0); 16562082Seschrock for (i = 0; i < oldnspares + nspares; i++) 16572082Seschrock nvlist_free(newspares[i]); 16582082Seschrock kmem_free(newspares, (oldnspares + nspares) * 16592082Seschrock sizeof (void *)); 16602082Seschrock } else { 16612082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 16622082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 16632082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16642082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16652082Seschrock } 16662082Seschrock 16672082Seschrock spa_load_spares(spa); 16682082Seschrock spa->spa_sync_spares = B_TRUE; 1669789Sahrens } 1670789Sahrens 1671789Sahrens /* 16721585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 16731585Sbonwick * If other threads start allocating from these vdevs before we 16741585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 16751585Sbonwick * fail to open the pool because there are DVAs that the config cache 16761585Sbonwick * can't translate. Therefore, we first add the vdevs without 16771585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 16781635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 16791585Sbonwick * 16801585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 16811585Sbonwick * if we lose power at any point in this sequence, the remaining 16821585Sbonwick * steps will be completed the next time we load the pool. 1683789Sahrens */ 16841635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 16851585Sbonwick 16861635Sbonwick mutex_enter(&spa_namespace_lock); 16871635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 16881635Sbonwick mutex_exit(&spa_namespace_lock); 1689789Sahrens 16901635Sbonwick return (0); 1691789Sahrens } 1692789Sahrens 1693789Sahrens /* 1694789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1695789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1696789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1697789Sahrens * 1698789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1699789Sahrens * existing device; in this case the two devices are made into their own 17004451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 1701789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1702789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1703789Sahrens * completion of resilvering, the first disk (the one being replaced) 1704789Sahrens * is automatically detached. 1705789Sahrens */ 1706789Sahrens int 17071544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1708789Sahrens { 1709789Sahrens uint64_t txg, open_txg; 1710789Sahrens int error; 1711789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1712789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 17132082Seschrock vdev_ops_t *pvops; 1714*4527Sperrin int is_log; 1715789Sahrens 1716789Sahrens txg = spa_vdev_enter(spa); 1717789Sahrens 17181544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1719789Sahrens 1720789Sahrens if (oldvd == NULL) 1721789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1722789Sahrens 17231585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 17241585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17251585Sbonwick 1726789Sahrens pvd = oldvd->vdev_parent; 1727789Sahrens 17282082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 17294451Seschrock VDEV_ALLOC_ADD)) != 0) 17304451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 17314451Seschrock 17324451Seschrock if (newrootvd->vdev_children != 1) 1733789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1734789Sahrens 1735789Sahrens newvd = newrootvd->vdev_child[0]; 1736789Sahrens 1737789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1738789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1739789Sahrens 17402082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1741789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1742789Sahrens 1743*4527Sperrin /* 1744*4527Sperrin * Spares can't replace logs 1745*4527Sperrin */ 1746*4527Sperrin is_log = oldvd->vdev_islog; 1747*4527Sperrin if (is_log && newvd->vdev_isspare) 1748*4527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 1749*4527Sperrin 17502082Seschrock if (!replacing) { 17512082Seschrock /* 17522082Seschrock * For attach, the only allowable parent is a mirror or the root 17532082Seschrock * vdev. 17542082Seschrock */ 17552082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 17562082Seschrock pvd->vdev_ops != &vdev_root_ops) 17572082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17582082Seschrock 17592082Seschrock pvops = &vdev_mirror_ops; 17602082Seschrock } else { 17612082Seschrock /* 17622082Seschrock * Active hot spares can only be replaced by inactive hot 17632082Seschrock * spares. 17642082Seschrock */ 17652082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 17662082Seschrock pvd->vdev_child[1] == oldvd && 17672082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 17682082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17692082Seschrock 17702082Seschrock /* 17712082Seschrock * If the source is a hot spare, and the parent isn't already a 17722082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 17733377Seschrock * want to create a replacing vdev. The user is not allowed to 17743377Seschrock * attach to a spared vdev child unless the 'isspare' state is 17753377Seschrock * the same (spare replaces spare, non-spare replaces 17763377Seschrock * non-spare). 17772082Seschrock */ 17782082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 17792082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17803377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 17813377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 17823377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17832082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 17842082Seschrock newvd->vdev_isspare) 17852082Seschrock pvops = &vdev_spare_ops; 17862082Seschrock else 17872082Seschrock pvops = &vdev_replacing_ops; 17882082Seschrock } 17892082Seschrock 17901175Slling /* 17911175Slling * Compare the new device size with the replaceable/attachable 17921175Slling * device size. 17931175Slling */ 17941175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1795789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1796789Sahrens 17971732Sbonwick /* 17981732Sbonwick * The new device cannot have a higher alignment requirement 17991732Sbonwick * than the top-level vdev. 18001732Sbonwick */ 18011732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1802789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1803789Sahrens 1804789Sahrens /* 1805789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1806789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1807789Sahrens */ 1808789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1809789Sahrens spa_strfree(oldvd->vdev_path); 1810789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1811789Sahrens KM_SLEEP); 1812789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1813789Sahrens newvd->vdev_path, "old"); 1814789Sahrens if (oldvd->vdev_devid != NULL) { 1815789Sahrens spa_strfree(oldvd->vdev_devid); 1816789Sahrens oldvd->vdev_devid = NULL; 1817789Sahrens } 1818789Sahrens } 1819789Sahrens 1820789Sahrens /* 18212082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 18222082Seschrock * mirror/replacing/spare vdev above oldvd. 1823789Sahrens */ 1824789Sahrens if (pvd->vdev_ops != pvops) 1825789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1826789Sahrens 1827789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1828789Sahrens ASSERT(pvd->vdev_ops == pvops); 1829789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1830789Sahrens 1831789Sahrens /* 1832789Sahrens * Extract the new device from its root and add it to pvd. 1833789Sahrens */ 1834789Sahrens vdev_remove_child(newrootvd, newvd); 1835789Sahrens newvd->vdev_id = pvd->vdev_children; 1836789Sahrens vdev_add_child(pvd, newvd); 1837789Sahrens 18381544Seschrock /* 18391544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 18401544Seschrock * the addition of newvd may have decreased our parent's asize. 18411544Seschrock */ 18421544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 18431544Seschrock 1844789Sahrens tvd = newvd->vdev_top; 1845789Sahrens ASSERT(pvd->vdev_top == tvd); 1846789Sahrens ASSERT(tvd->vdev_parent == rvd); 1847789Sahrens 1848789Sahrens vdev_config_dirty(tvd); 1849789Sahrens 1850789Sahrens /* 1851789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1852789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1853789Sahrens */ 1854789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1855789Sahrens 1856789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1857789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1858789Sahrens open_txg - TXG_INITIAL + 1); 1859789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1860789Sahrens 18613377Seschrock if (newvd->vdev_isspare) 18623377Seschrock spa_spare_activate(newvd); 18631544Seschrock 1864789Sahrens /* 1865789Sahrens * Mark newvd's DTL dirty in this txg. 1866789Sahrens */ 18671732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1868789Sahrens 1869789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1870789Sahrens 1871789Sahrens /* 18724451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 18734451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 1874789Sahrens */ 18754451Seschrock mutex_enter(&spa_namespace_lock); 1876789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 18774451Seschrock mutex_exit(&spa_namespace_lock); 1878789Sahrens 1879789Sahrens return (0); 1880789Sahrens } 1881789Sahrens 1882789Sahrens /* 1883789Sahrens * Detach a device from a mirror or replacing vdev. 1884789Sahrens * If 'replace_done' is specified, only detach if the parent 1885789Sahrens * is a replacing vdev. 1886789Sahrens */ 1887789Sahrens int 18881544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1889789Sahrens { 1890789Sahrens uint64_t txg; 1891789Sahrens int c, t, error; 1892789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1893789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 18942082Seschrock boolean_t unspare = B_FALSE; 18952082Seschrock uint64_t unspare_guid; 1896789Sahrens 1897789Sahrens txg = spa_vdev_enter(spa); 1898789Sahrens 18991544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1900789Sahrens 1901789Sahrens if (vd == NULL) 1902789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1903789Sahrens 19041585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 19051585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19061585Sbonwick 1907789Sahrens pvd = vd->vdev_parent; 1908789Sahrens 1909789Sahrens /* 1910789Sahrens * If replace_done is specified, only remove this device if it's 19112082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 19122082Seschrock * disk can be removed. 1913789Sahrens */ 19142082Seschrock if (replace_done) { 19152082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 19162082Seschrock if (vd->vdev_id != 0) 19172082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19182082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 19192082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 19202082Seschrock } 19212082Seschrock } 19222082Seschrock 19232082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 19242082Seschrock spa_version(spa) >= ZFS_VERSION_SPARES); 1925789Sahrens 1926789Sahrens /* 19272082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1928789Sahrens */ 1929789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 19302082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 19312082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1932789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1933789Sahrens 1934789Sahrens /* 1935789Sahrens * If there's only one replica, you can't detach it. 1936789Sahrens */ 1937789Sahrens if (pvd->vdev_children <= 1) 1938789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1939789Sahrens 1940789Sahrens /* 1941789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1942789Sahrens * valid copy of the data, which means we cannot safely detach it. 1943789Sahrens * 1944789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1945789Sahrens * precise DTL check. 1946789Sahrens */ 1947789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1948789Sahrens uint64_t dirty; 1949789Sahrens 1950789Sahrens cvd = pvd->vdev_child[c]; 1951789Sahrens if (cvd == vd) 1952789Sahrens continue; 1953789Sahrens if (vdev_is_dead(cvd)) 1954789Sahrens continue; 1955789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1956789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1957789Sahrens cvd->vdev_dtl_scrub.sm_space; 1958789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1959789Sahrens if (!dirty) 1960789Sahrens break; 1961789Sahrens } 19622082Seschrock 19632082Seschrock /* 19642082Seschrock * If we are a replacing or spare vdev, then we can always detach the 19652082Seschrock * latter child, as that is how one cancels the operation. 19662082Seschrock */ 19672082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 19682082Seschrock c == pvd->vdev_children) 1969789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1970789Sahrens 1971789Sahrens /* 19722082Seschrock * If we are detaching the original disk from a spare, then it implies 19732082Seschrock * that the spare should become a real disk, and be removed from the 19742082Seschrock * active spare list for the pool. 19752082Seschrock */ 19762082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 19772082Seschrock vd->vdev_id == 0) 19782082Seschrock unspare = B_TRUE; 19792082Seschrock 19802082Seschrock /* 1981789Sahrens * Erase the disk labels so the disk can be used for other things. 1982789Sahrens * This must be done after all other error cases are handled, 1983789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1984789Sahrens * But if we can't do it, don't treat the error as fatal -- 1985789Sahrens * it may be that the unwritability of the disk is the reason 1986789Sahrens * it's being detached! 1987789Sahrens */ 19883377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1989789Sahrens 1990789Sahrens /* 1991789Sahrens * Remove vd from its parent and compact the parent's children. 1992789Sahrens */ 1993789Sahrens vdev_remove_child(pvd, vd); 1994789Sahrens vdev_compact_children(pvd); 1995789Sahrens 1996789Sahrens /* 1997789Sahrens * Remember one of the remaining children so we can get tvd below. 1998789Sahrens */ 1999789Sahrens cvd = pvd->vdev_child[0]; 2000789Sahrens 2001789Sahrens /* 20022082Seschrock * If we need to remove the remaining child from the list of hot spares, 20032082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 20042082Seschrock * must do this before vdev_remove_parent(), because that can change the 20052082Seschrock * GUID if it creates a new toplevel GUID. 20062082Seschrock */ 20072082Seschrock if (unspare) { 20082082Seschrock ASSERT(cvd->vdev_isspare); 20093377Seschrock spa_spare_remove(cvd); 20102082Seschrock unspare_guid = cvd->vdev_guid; 20112082Seschrock } 20122082Seschrock 20132082Seschrock /* 2014789Sahrens * If the parent mirror/replacing vdev only has one child, 2015789Sahrens * the parent is no longer needed. Remove it from the tree. 2016789Sahrens */ 2017789Sahrens if (pvd->vdev_children == 1) 2018789Sahrens vdev_remove_parent(cvd); 2019789Sahrens 2020789Sahrens /* 2021789Sahrens * We don't set tvd until now because the parent we just removed 2022789Sahrens * may have been the previous top-level vdev. 2023789Sahrens */ 2024789Sahrens tvd = cvd->vdev_top; 2025789Sahrens ASSERT(tvd->vdev_parent == rvd); 2026789Sahrens 2027789Sahrens /* 20283377Seschrock * Reevaluate the parent vdev state. 2029789Sahrens */ 20304451Seschrock vdev_propagate_state(cvd); 2031789Sahrens 2032789Sahrens /* 20333377Seschrock * If the device we just detached was smaller than the others, it may be 20343377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 20353377Seschrock * can't fail because the existing metaslabs are already in core, so 20363377Seschrock * there's nothing to read from disk. 2037789Sahrens */ 20381732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2039789Sahrens 2040789Sahrens vdev_config_dirty(tvd); 2041789Sahrens 2042789Sahrens /* 20433377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 20443377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 20453377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 20463377Seschrock * prevent vd from being accessed after it's freed. 2047789Sahrens */ 2048789Sahrens for (t = 0; t < TXG_SIZE; t++) 2049789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 20501732Sbonwick vd->vdev_detached = B_TRUE; 20511732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2052789Sahrens 20534451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 20544451Seschrock 20552082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 20562082Seschrock 20572082Seschrock /* 20583377Seschrock * If this was the removal of the original device in a hot spare vdev, 20593377Seschrock * then we want to go through and remove the device from the hot spare 20603377Seschrock * list of every other pool. 20612082Seschrock */ 20622082Seschrock if (unspare) { 20632082Seschrock spa = NULL; 20642082Seschrock mutex_enter(&spa_namespace_lock); 20652082Seschrock while ((spa = spa_next(spa)) != NULL) { 20662082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 20672082Seschrock continue; 20682082Seschrock 20692082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 20702082Seschrock } 20712082Seschrock mutex_exit(&spa_namespace_lock); 20722082Seschrock } 20732082Seschrock 20742082Seschrock return (error); 20752082Seschrock } 20762082Seschrock 20772082Seschrock /* 20782082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 20792082Seschrock * spares. 20802082Seschrock */ 20812082Seschrock int 20822082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 20832082Seschrock { 20842082Seschrock vdev_t *vd; 20852082Seschrock nvlist_t **spares, *nv, **newspares; 20862082Seschrock uint_t i, j, nspares; 20872082Seschrock int ret = 0; 20882082Seschrock 20892082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20902082Seschrock 20912082Seschrock vd = spa_lookup_by_guid(spa, guid); 20922082Seschrock 20932082Seschrock nv = NULL; 20942082Seschrock if (spa->spa_spares != NULL && 20952082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 20962082Seschrock &spares, &nspares) == 0) { 20972082Seschrock for (i = 0; i < nspares; i++) { 20982082Seschrock uint64_t theguid; 20992082Seschrock 21002082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 21012082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 21022082Seschrock if (theguid == guid) { 21032082Seschrock nv = spares[i]; 21042082Seschrock break; 21052082Seschrock } 21062082Seschrock } 21072082Seschrock } 21082082Seschrock 21092082Seschrock /* 21102082Seschrock * We only support removing a hot spare, and only if it's not currently 21112082Seschrock * in use in this pool. 21122082Seschrock */ 21132082Seschrock if (nv == NULL && vd == NULL) { 21142082Seschrock ret = ENOENT; 21152082Seschrock goto out; 21162082Seschrock } 21172082Seschrock 21182082Seschrock if (nv == NULL && vd != NULL) { 21192082Seschrock ret = ENOTSUP; 21202082Seschrock goto out; 21212082Seschrock } 21222082Seschrock 21232082Seschrock if (!unspare && nv != NULL && vd != NULL) { 21242082Seschrock ret = EBUSY; 21252082Seschrock goto out; 21262082Seschrock } 21272082Seschrock 21282082Seschrock if (nspares == 1) { 21292082Seschrock newspares = NULL; 21302082Seschrock } else { 21312082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 21322082Seschrock KM_SLEEP); 21332082Seschrock for (i = 0, j = 0; i < nspares; i++) { 21342082Seschrock if (spares[i] != nv) 21352082Seschrock VERIFY(nvlist_dup(spares[i], 21362082Seschrock &newspares[j++], KM_SLEEP) == 0); 21372082Seschrock } 21382082Seschrock } 21392082Seschrock 21402082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21412082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 21422082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 21432082Seschrock newspares, nspares - 1) == 0); 21442082Seschrock for (i = 0; i < nspares - 1; i++) 21452082Seschrock nvlist_free(newspares[i]); 21462082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 21472082Seschrock spa_load_spares(spa); 21482082Seschrock spa->spa_sync_spares = B_TRUE; 21492082Seschrock 21502082Seschrock out: 21512082Seschrock spa_config_exit(spa, FTAG); 21522082Seschrock 21532082Seschrock return (ret); 2154789Sahrens } 2155789Sahrens 2156789Sahrens /* 21574451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 21584451Seschrock * current spared, so we can detach it. 2159789Sahrens */ 21601544Seschrock static vdev_t * 21614451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2162789Sahrens { 21631544Seschrock vdev_t *newvd, *oldvd; 2164789Sahrens int c; 2165789Sahrens 21661544Seschrock for (c = 0; c < vd->vdev_children; c++) { 21674451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 21681544Seschrock if (oldvd != NULL) 21691544Seschrock return (oldvd); 21701544Seschrock } 2171789Sahrens 21724451Seschrock /* 21734451Seschrock * Check for a completed replacement. 21744451Seschrock */ 2175789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 21761544Seschrock oldvd = vd->vdev_child[0]; 21771544Seschrock newvd = vd->vdev_child[1]; 2178789Sahrens 21791544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 21801544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 21811544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 21821544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21831544Seschrock return (oldvd); 21841544Seschrock } 21851544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21861544Seschrock } 2187789Sahrens 21884451Seschrock /* 21894451Seschrock * Check for a completed resilver with the 'unspare' flag set. 21904451Seschrock */ 21914451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 21924451Seschrock newvd = vd->vdev_child[0]; 21934451Seschrock oldvd = vd->vdev_child[1]; 21944451Seschrock 21954451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 21964451Seschrock if (newvd->vdev_unspare && 21974451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 21984451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 21994451Seschrock newvd->vdev_unspare = 0; 22004451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22014451Seschrock return (oldvd); 22024451Seschrock } 22034451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 22044451Seschrock } 22054451Seschrock 22061544Seschrock return (NULL); 2207789Sahrens } 2208789Sahrens 22091544Seschrock static void 22104451Seschrock spa_vdev_resilver_done(spa_t *spa) 2211789Sahrens { 22121544Seschrock vdev_t *vd; 22132082Seschrock vdev_t *pvd; 22141544Seschrock uint64_t guid; 22152082Seschrock uint64_t pguid = 0; 2216789Sahrens 22171544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2218789Sahrens 22194451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 22201544Seschrock guid = vd->vdev_guid; 22212082Seschrock /* 22222082Seschrock * If we have just finished replacing a hot spared device, then 22232082Seschrock * we need to detach the parent's first child (the original hot 22242082Seschrock * spare) as well. 22252082Seschrock */ 22262082Seschrock pvd = vd->vdev_parent; 22272082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 22282082Seschrock pvd->vdev_id == 0) { 22292082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 22302082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 22312082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 22322082Seschrock } 22331544Seschrock spa_config_exit(spa, FTAG); 22341544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 22351544Seschrock return; 22362082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 22372082Seschrock return; 22381544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2239789Sahrens } 2240789Sahrens 22411544Seschrock spa_config_exit(spa, FTAG); 2242789Sahrens } 2243789Sahrens 2244789Sahrens /* 22451354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 22461354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 22471354Seschrock */ 22481354Seschrock int 22491354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 22501354Seschrock { 22511354Seschrock vdev_t *rvd, *vd; 22521354Seschrock uint64_t txg; 22531354Seschrock 22541354Seschrock rvd = spa->spa_root_vdev; 22551354Seschrock 22561354Seschrock txg = spa_vdev_enter(spa); 22571354Seschrock 22582082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 22592082Seschrock /* 22602082Seschrock * Determine if this is a reference to a hot spare. In that 22612082Seschrock * case, update the path as stored in the spare list. 22622082Seschrock */ 22632082Seschrock nvlist_t **spares; 22642082Seschrock uint_t i, nspares; 22652082Seschrock if (spa->spa_sparelist != NULL) { 22662082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 22672082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 22682082Seschrock for (i = 0; i < nspares; i++) { 22692082Seschrock uint64_t theguid; 22702082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 22712082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 22722082Seschrock if (theguid == guid) 22732082Seschrock break; 22742082Seschrock } 22752082Seschrock 22762082Seschrock if (i == nspares) 22772082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22782082Seschrock 22792082Seschrock VERIFY(nvlist_add_string(spares[i], 22802082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 22812082Seschrock spa_load_spares(spa); 22822082Seschrock spa->spa_sync_spares = B_TRUE; 22832082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 22842082Seschrock } else { 22852082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 22862082Seschrock } 22872082Seschrock } 22881354Seschrock 22891585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 22901585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 22911585Sbonwick 22921354Seschrock spa_strfree(vd->vdev_path); 22931354Seschrock vd->vdev_path = spa_strdup(newpath); 22941354Seschrock 22951354Seschrock vdev_config_dirty(vd->vdev_top); 22961354Seschrock 22971354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 22981354Seschrock } 22991354Seschrock 23001354Seschrock /* 2301789Sahrens * ========================================================================== 2302789Sahrens * SPA Scrubbing 2303789Sahrens * ========================================================================== 2304789Sahrens */ 2305789Sahrens 2306789Sahrens static void 2307789Sahrens spa_scrub_io_done(zio_t *zio) 2308789Sahrens { 2309789Sahrens spa_t *spa = zio->io_spa; 2310789Sahrens 23114309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2312789Sahrens 2313789Sahrens mutex_enter(&spa->spa_scrub_lock); 23141544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 23151775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2316789Sahrens spa->spa_scrub_errors++; 2317789Sahrens mutex_enter(&vd->vdev_stat_lock); 2318789Sahrens vd->vdev_stat.vs_scrub_errors++; 2319789Sahrens mutex_exit(&vd->vdev_stat_lock); 2320789Sahrens } 23213697Smishra 23223697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 23231544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 23243697Smishra 23253697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 23263697Smishra 23271544Seschrock mutex_exit(&spa->spa_scrub_lock); 2328789Sahrens } 2329789Sahrens 2330789Sahrens static void 23311544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 23321544Seschrock zbookmark_t *zb) 2333789Sahrens { 2334789Sahrens size_t size = BP_GET_LSIZE(bp); 23353697Smishra void *data; 2336789Sahrens 2337789Sahrens mutex_enter(&spa->spa_scrub_lock); 23383697Smishra /* 23393697Smishra * Do not give too much work to vdev(s). 23403697Smishra */ 23413697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 23423697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 23433697Smishra } 2344789Sahrens spa->spa_scrub_inflight++; 2345789Sahrens mutex_exit(&spa->spa_scrub_lock); 2346789Sahrens 23474309Smaybee data = arc_data_buf_alloc(size); 23483697Smishra 23491544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 23501544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 23511544Seschrock 23521807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 23531544Seschrock 2354789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 23551544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2356789Sahrens } 2357789Sahrens 2358789Sahrens /* ARGSUSED */ 2359789Sahrens static int 2360789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2361789Sahrens { 2362789Sahrens blkptr_t *bp = &bc->bc_blkptr; 23631775Sbillm vdev_t *vd = spa->spa_root_vdev; 23641775Sbillm dva_t *dva = bp->blk_dva; 23651775Sbillm int needs_resilver = B_FALSE; 23661775Sbillm int d; 2367789Sahrens 23681775Sbillm if (bc->bc_errno) { 2369789Sahrens /* 2370789Sahrens * We can't scrub this block, but we can continue to scrub 2371789Sahrens * the rest of the pool. Note the error and move along. 2372789Sahrens */ 2373789Sahrens mutex_enter(&spa->spa_scrub_lock); 2374789Sahrens spa->spa_scrub_errors++; 2375789Sahrens mutex_exit(&spa->spa_scrub_lock); 2376789Sahrens 23771775Sbillm mutex_enter(&vd->vdev_stat_lock); 23781775Sbillm vd->vdev_stat.vs_scrub_errors++; 23791775Sbillm mutex_exit(&vd->vdev_stat_lock); 2380789Sahrens 2381789Sahrens return (ERESTART); 2382789Sahrens } 2383789Sahrens 2384789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2385789Sahrens 23861775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 23871775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 23881775Sbillm 23891775Sbillm ASSERT(vd != NULL); 23901775Sbillm 23911775Sbillm /* 23921775Sbillm * Keep track of how much data we've examined so that 23931775Sbillm * zpool(1M) status can make useful progress reports. 23941775Sbillm */ 23951775Sbillm mutex_enter(&vd->vdev_stat_lock); 23961775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 23971775Sbillm mutex_exit(&vd->vdev_stat_lock); 2398789Sahrens 23991775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 24001775Sbillm if (DVA_GET_GANG(&dva[d])) { 24011775Sbillm /* 24021775Sbillm * Gang members may be spread across multiple 24031775Sbillm * vdevs, so the best we can do is look at the 24041775Sbillm * pool-wide DTL. 24051775Sbillm * XXX -- it would be better to change our 24061775Sbillm * allocation policy to ensure that this can't 24071775Sbillm * happen. 24081775Sbillm */ 24091775Sbillm vd = spa->spa_root_vdev; 24101775Sbillm } 24111775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 24121775Sbillm bp->blk_birth, 1)) 24131775Sbillm needs_resilver = B_TRUE; 2414789Sahrens } 24151775Sbillm } 24161775Sbillm 24171775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2418789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 24191544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 24201775Sbillm else if (needs_resilver) 24211775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 24221775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2423789Sahrens 2424789Sahrens return (0); 2425789Sahrens } 2426789Sahrens 2427789Sahrens static void 2428789Sahrens spa_scrub_thread(spa_t *spa) 2429789Sahrens { 2430789Sahrens callb_cpr_t cprinfo; 2431789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2432789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2433789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2434789Sahrens int error = 0; 2435789Sahrens boolean_t complete; 2436789Sahrens 2437789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2438789Sahrens 2439797Sbonwick /* 2440797Sbonwick * If we're restarting due to a snapshot create/delete, 2441797Sbonwick * wait for that to complete. 2442797Sbonwick */ 2443797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2444797Sbonwick 24451544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 24461544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 24471544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 24481544Seschrock 24491544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 24501544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2451789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2452789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 24531544Seschrock spa_config_exit(spa, FTAG); 2454789Sahrens 2455789Sahrens mutex_enter(&spa->spa_scrub_lock); 2456789Sahrens spa->spa_scrub_errors = 0; 2457789Sahrens spa->spa_scrub_active = 1; 24581544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2459789Sahrens 2460789Sahrens while (!spa->spa_scrub_stop) { 2461789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 24621544Seschrock while (spa->spa_scrub_suspended) { 2463789Sahrens spa->spa_scrub_active = 0; 2464789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2465789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2466789Sahrens spa->spa_scrub_active = 1; 2467789Sahrens } 2468789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2469789Sahrens 2470789Sahrens if (spa->spa_scrub_restart_txg != 0) 2471789Sahrens break; 2472789Sahrens 2473789Sahrens mutex_exit(&spa->spa_scrub_lock); 2474789Sahrens error = traverse_more(th); 2475789Sahrens mutex_enter(&spa->spa_scrub_lock); 2476789Sahrens if (error != EAGAIN) 2477789Sahrens break; 2478789Sahrens } 2479789Sahrens 2480789Sahrens while (spa->spa_scrub_inflight) 2481789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2482789Sahrens 24831601Sbonwick spa->spa_scrub_active = 0; 24841601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 24851601Sbonwick 24861601Sbonwick mutex_exit(&spa->spa_scrub_lock); 24871601Sbonwick 24881601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24891601Sbonwick 24901601Sbonwick mutex_enter(&spa->spa_scrub_lock); 24911601Sbonwick 24921601Sbonwick /* 24931601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 24941601Sbonwick * AND the spa config lock to synchronize with any config changes 24951601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 24961601Sbonwick */ 2497789Sahrens if (spa->spa_scrub_restart_txg != 0) 2498789Sahrens error = ERESTART; 2499789Sahrens 25001544Seschrock if (spa->spa_scrub_stop) 25011544Seschrock error = EINTR; 25021544Seschrock 2503789Sahrens /* 25041544Seschrock * Even if there were uncorrectable errors, we consider the scrub 25051544Seschrock * completed. The downside is that if there is a transient error during 25061544Seschrock * a resilver, we won't resilver the data properly to the target. But 25071544Seschrock * if the damage is permanent (more likely) we will resilver forever, 25081544Seschrock * which isn't really acceptable. Since there is enough information for 25091544Seschrock * the user to know what has failed and why, this seems like a more 25101544Seschrock * tractable approach. 2511789Sahrens */ 25121544Seschrock complete = (error == 0); 2513789Sahrens 25141544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 25151544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2516789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2517789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2518789Sahrens 2519789Sahrens mutex_exit(&spa->spa_scrub_lock); 2520789Sahrens 2521789Sahrens /* 2522789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2523789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2524789Sahrens */ 2525789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2526789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2527789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 25281544Seschrock spa_errlog_rotate(spa); 25291601Sbonwick 25304451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 25314451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 25324451Seschrock 25331544Seschrock spa_config_exit(spa, FTAG); 2534789Sahrens 2535789Sahrens mutex_enter(&spa->spa_scrub_lock); 2536789Sahrens 25371544Seschrock /* 25381544Seschrock * We may have finished replacing a device. 25391544Seschrock * Let the async thread assess this and handle the detach. 25401544Seschrock */ 25414451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2542789Sahrens 2543789Sahrens /* 2544789Sahrens * If we were told to restart, our final act is to start a new scrub. 2545789Sahrens */ 2546789Sahrens if (error == ERESTART) 25471544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 25481544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2549789Sahrens 25501544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 25511544Seschrock spa->spa_scrub_active = 0; 25521544Seschrock spa->spa_scrub_thread = NULL; 25531544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2554789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2555789Sahrens thread_exit(); 2556789Sahrens } 2557789Sahrens 2558789Sahrens void 2559789Sahrens spa_scrub_suspend(spa_t *spa) 2560789Sahrens { 2561789Sahrens mutex_enter(&spa->spa_scrub_lock); 25621544Seschrock spa->spa_scrub_suspended++; 2563789Sahrens while (spa->spa_scrub_active) { 2564789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2565789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2566789Sahrens } 2567789Sahrens while (spa->spa_scrub_inflight) 2568789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2569789Sahrens mutex_exit(&spa->spa_scrub_lock); 2570789Sahrens } 2571789Sahrens 2572789Sahrens void 2573789Sahrens spa_scrub_resume(spa_t *spa) 2574789Sahrens { 2575789Sahrens mutex_enter(&spa->spa_scrub_lock); 25761544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 25771544Seschrock if (--spa->spa_scrub_suspended == 0) 2578789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2579789Sahrens mutex_exit(&spa->spa_scrub_lock); 2580789Sahrens } 2581789Sahrens 2582789Sahrens void 2583789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2584789Sahrens { 2585789Sahrens /* 2586789Sahrens * Something happened (e.g. snapshot create/delete) that means 2587789Sahrens * we must restart any in-progress scrubs. The itinerary will 2588789Sahrens * fix this properly. 2589789Sahrens */ 2590789Sahrens mutex_enter(&spa->spa_scrub_lock); 2591789Sahrens spa->spa_scrub_restart_txg = txg; 2592789Sahrens mutex_exit(&spa->spa_scrub_lock); 2593789Sahrens } 2594789Sahrens 25951544Seschrock int 25961544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2597789Sahrens { 2598789Sahrens space_seg_t *ss; 2599789Sahrens uint64_t mintxg, maxtxg; 2600789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2601789Sahrens 2602789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2603789Sahrens return (ENOTSUP); 2604789Sahrens 26051544Seschrock mutex_enter(&spa->spa_scrub_lock); 26061544Seschrock 2607789Sahrens /* 2608789Sahrens * If there's a scrub or resilver already in progress, stop it. 2609789Sahrens */ 2610789Sahrens while (spa->spa_scrub_thread != NULL) { 2611789Sahrens /* 2612789Sahrens * Don't stop a resilver unless forced. 2613789Sahrens */ 26141544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 26151544Seschrock mutex_exit(&spa->spa_scrub_lock); 2616789Sahrens return (EBUSY); 26171544Seschrock } 2618789Sahrens spa->spa_scrub_stop = 1; 2619789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2620789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2621789Sahrens } 2622789Sahrens 2623789Sahrens /* 2624789Sahrens * Terminate the previous traverse. 2625789Sahrens */ 2626789Sahrens if (spa->spa_scrub_th != NULL) { 2627789Sahrens traverse_fini(spa->spa_scrub_th); 2628789Sahrens spa->spa_scrub_th = NULL; 2629789Sahrens } 2630789Sahrens 26311544Seschrock if (rvd == NULL) { 26321544Seschrock ASSERT(spa->spa_scrub_stop == 0); 26331544Seschrock ASSERT(spa->spa_scrub_type == type); 26341544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 26351544Seschrock mutex_exit(&spa->spa_scrub_lock); 26361544Seschrock return (0); 26371544Seschrock } 2638789Sahrens 2639789Sahrens mintxg = TXG_INITIAL - 1; 2640789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2641789Sahrens 26421544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2643789Sahrens 26441544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 26451544Seschrock /* 26461544Seschrock * The pool-wide DTL is empty. 26471732Sbonwick * If this is a resilver, there's nothing to do except 26481732Sbonwick * check whether any in-progress replacements have completed. 26491544Seschrock */ 26501732Sbonwick if (type == POOL_SCRUB_RESILVER) { 26511544Seschrock type = POOL_SCRUB_NONE; 26524451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 26531732Sbonwick } 26541544Seschrock } else { 26551544Seschrock /* 26561544Seschrock * The pool-wide DTL is non-empty. 26571544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 26581544Seschrock */ 26591544Seschrock if (type == POOL_SCRUB_EVERYTHING) 26601544Seschrock type = POOL_SCRUB_RESILVER; 26611544Seschrock } 2662789Sahrens 26631544Seschrock if (type == POOL_SCRUB_RESILVER) { 2664789Sahrens /* 2665789Sahrens * Determine the resilvering boundaries. 2666789Sahrens * 2667789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2668789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2669789Sahrens * 2670789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2671789Sahrens * so we don't claim to resilver a txg that's still changing. 2672789Sahrens */ 2673789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 26741544Seschrock mintxg = ss->ss_start - 1; 2675789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 26761544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 26774451Seschrock 26784451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 2679789Sahrens } 2680789Sahrens 26811544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 26821544Seschrock 26831544Seschrock spa->spa_scrub_stop = 0; 26841544Seschrock spa->spa_scrub_type = type; 26851544Seschrock spa->spa_scrub_restart_txg = 0; 26861544Seschrock 26871544Seschrock if (type != POOL_SCRUB_NONE) { 26881544Seschrock spa->spa_scrub_mintxg = mintxg; 2689789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2690789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 26911635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 26921635Sbonwick ZIO_FLAG_CANFAIL); 2693789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2694789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2695789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2696789Sahrens } 2697789Sahrens 26981544Seschrock mutex_exit(&spa->spa_scrub_lock); 26991544Seschrock 2700789Sahrens return (0); 2701789Sahrens } 2702789Sahrens 27031544Seschrock /* 27041544Seschrock * ========================================================================== 27051544Seschrock * SPA async task processing 27061544Seschrock * ========================================================================== 27071544Seschrock */ 27081544Seschrock 27091544Seschrock static void 27104451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 2711789Sahrens { 27121544Seschrock vdev_t *tvd; 27131544Seschrock int c; 27141544Seschrock 27154451Seschrock for (c = 0; c < vd->vdev_children; c++) { 27164451Seschrock tvd = vd->vdev_child[c]; 27174451Seschrock if (tvd->vdev_remove_wanted) { 27184451Seschrock tvd->vdev_remove_wanted = 0; 27194451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 27204451Seschrock VDEV_AUX_NONE); 27214451Seschrock vdev_clear(spa, tvd); 27224451Seschrock vdev_config_dirty(tvd->vdev_top); 27231544Seschrock } 27244451Seschrock spa_async_remove(spa, tvd); 27251544Seschrock } 27261544Seschrock } 27271544Seschrock 27281544Seschrock static void 27291544Seschrock spa_async_thread(spa_t *spa) 27301544Seschrock { 27311544Seschrock int tasks; 27324451Seschrock uint64_t txg; 27331544Seschrock 27341544Seschrock ASSERT(spa->spa_sync_on); 2735789Sahrens 27361544Seschrock mutex_enter(&spa->spa_async_lock); 27371544Seschrock tasks = spa->spa_async_tasks; 27381544Seschrock spa->spa_async_tasks = 0; 27391544Seschrock mutex_exit(&spa->spa_async_lock); 27401544Seschrock 27411544Seschrock /* 27421635Sbonwick * See if the config needs to be updated. 27431635Sbonwick */ 27441635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 27451635Sbonwick mutex_enter(&spa_namespace_lock); 27461635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27471635Sbonwick mutex_exit(&spa_namespace_lock); 27481635Sbonwick } 27491635Sbonwick 27501635Sbonwick /* 27514451Seschrock * See if any devices need to be marked REMOVED. 27521544Seschrock */ 27534451Seschrock if (tasks & SPA_ASYNC_REMOVE) { 27544451Seschrock txg = spa_vdev_enter(spa); 27554451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 27564451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 27574451Seschrock } 27581544Seschrock 27591544Seschrock /* 27601544Seschrock * If any devices are done replacing, detach them. 27611544Seschrock */ 27624451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 27634451Seschrock spa_vdev_resilver_done(spa); 2764789Sahrens 27651544Seschrock /* 27664451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 27674451Seschrock * scrub which can become a resilver), we need to hold 27684451Seschrock * spa_namespace_lock() because the sysevent we post via 27694451Seschrock * spa_event_notify() needs to get the name of the pool. 27701544Seschrock */ 27714451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 27724451Seschrock mutex_enter(&spa_namespace_lock); 27731544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 27744451Seschrock mutex_exit(&spa_namespace_lock); 27754451Seschrock } 27761544Seschrock 27771544Seschrock /* 27781544Seschrock * Kick off a resilver. 27791544Seschrock */ 27804451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 27814451Seschrock mutex_enter(&spa_namespace_lock); 27821544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 27834451Seschrock mutex_exit(&spa_namespace_lock); 27844451Seschrock } 27851544Seschrock 27861544Seschrock /* 27871544Seschrock * Let the world know that we're done. 27881544Seschrock */ 27891544Seschrock mutex_enter(&spa->spa_async_lock); 27901544Seschrock spa->spa_async_thread = NULL; 27911544Seschrock cv_broadcast(&spa->spa_async_cv); 27921544Seschrock mutex_exit(&spa->spa_async_lock); 27931544Seschrock thread_exit(); 27941544Seschrock } 27951544Seschrock 27961544Seschrock void 27971544Seschrock spa_async_suspend(spa_t *spa) 27981544Seschrock { 27991544Seschrock mutex_enter(&spa->spa_async_lock); 28001544Seschrock spa->spa_async_suspended++; 28011544Seschrock while (spa->spa_async_thread != NULL) 28021544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 28031544Seschrock mutex_exit(&spa->spa_async_lock); 28041544Seschrock } 28051544Seschrock 28061544Seschrock void 28071544Seschrock spa_async_resume(spa_t *spa) 28081544Seschrock { 28091544Seschrock mutex_enter(&spa->spa_async_lock); 28101544Seschrock ASSERT(spa->spa_async_suspended != 0); 28111544Seschrock spa->spa_async_suspended--; 28121544Seschrock mutex_exit(&spa->spa_async_lock); 28131544Seschrock } 28141544Seschrock 28151544Seschrock static void 28161544Seschrock spa_async_dispatch(spa_t *spa) 28171544Seschrock { 28181544Seschrock mutex_enter(&spa->spa_async_lock); 28191544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 28201635Sbonwick spa->spa_async_thread == NULL && 28211635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 28221544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 28231544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 28241544Seschrock mutex_exit(&spa->spa_async_lock); 28251544Seschrock } 28261544Seschrock 28271544Seschrock void 28281544Seschrock spa_async_request(spa_t *spa, int task) 28291544Seschrock { 28301544Seschrock mutex_enter(&spa->spa_async_lock); 28311544Seschrock spa->spa_async_tasks |= task; 28321544Seschrock mutex_exit(&spa->spa_async_lock); 2833789Sahrens } 2834789Sahrens 2835789Sahrens /* 2836789Sahrens * ========================================================================== 2837789Sahrens * SPA syncing routines 2838789Sahrens * ========================================================================== 2839789Sahrens */ 2840789Sahrens 2841789Sahrens static void 2842789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2843789Sahrens { 2844789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2845789Sahrens dmu_tx_t *tx; 2846789Sahrens blkptr_t blk; 2847789Sahrens uint64_t itor = 0; 2848789Sahrens zio_t *zio; 2849789Sahrens int error; 2850789Sahrens uint8_t c = 1; 2851789Sahrens 2852789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2853789Sahrens 2854789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2855789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2856789Sahrens 2857789Sahrens error = zio_wait(zio); 2858789Sahrens ASSERT3U(error, ==, 0); 2859789Sahrens 2860789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2861789Sahrens bplist_vacate(bpl, tx); 2862789Sahrens 2863789Sahrens /* 2864789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2865789Sahrens * (Usually only the first block is needed.) 2866789Sahrens */ 2867789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2868789Sahrens dmu_tx_commit(tx); 2869789Sahrens } 2870789Sahrens 2871789Sahrens static void 28722082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 28732082Seschrock { 28742082Seschrock char *packed = NULL; 28752082Seschrock size_t nvsize = 0; 28762082Seschrock dmu_buf_t *db; 28772082Seschrock 28782082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 28792082Seschrock 28802082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 28812082Seschrock 28822082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 28832082Seschrock KM_SLEEP) == 0); 28842082Seschrock 28852082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 28862082Seschrock 28872082Seschrock kmem_free(packed, nvsize); 28882082Seschrock 28892082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 28902082Seschrock dmu_buf_will_dirty(db, tx); 28912082Seschrock *(uint64_t *)db->db_data = nvsize; 28922082Seschrock dmu_buf_rele(db, FTAG); 28932082Seschrock } 28942082Seschrock 28952082Seschrock static void 28962082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 28972082Seschrock { 28982082Seschrock nvlist_t *nvroot; 28992082Seschrock nvlist_t **spares; 29002082Seschrock int i; 29012082Seschrock 29022082Seschrock if (!spa->spa_sync_spares) 29032082Seschrock return; 29042082Seschrock 29052082Seschrock /* 29062082Seschrock * Update the MOS nvlist describing the list of available spares. 29072082Seschrock * spa_validate_spares() will have already made sure this nvlist is 29084451Seschrock * valid and the vdevs are labeled appropriately. 29092082Seschrock */ 29102082Seschrock if (spa->spa_spares_object == 0) { 29112082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 29122082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 29132082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 29142082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 29152082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 29162082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 29172082Seschrock } 29182082Seschrock 29192082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 29202082Seschrock if (spa->spa_nspares == 0) { 29212082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29222082Seschrock NULL, 0) == 0); 29232082Seschrock } else { 29242082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 29252082Seschrock KM_SLEEP); 29262082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29272082Seschrock spares[i] = vdev_config_generate(spa, 29282082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 29292082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29302082Seschrock spares, spa->spa_nspares) == 0); 29312082Seschrock for (i = 0; i < spa->spa_nspares; i++) 29322082Seschrock nvlist_free(spares[i]); 29332082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 29342082Seschrock } 29352082Seschrock 29362082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 29372926Sek110237 nvlist_free(nvroot); 29382082Seschrock 29392082Seschrock spa->spa_sync_spares = B_FALSE; 29402082Seschrock } 29412082Seschrock 29422082Seschrock static void 2943789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2944789Sahrens { 2945789Sahrens nvlist_t *config; 2946789Sahrens 2947789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2948789Sahrens return; 2949789Sahrens 2950789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2951789Sahrens 29521635Sbonwick if (spa->spa_config_syncing) 29531635Sbonwick nvlist_free(spa->spa_config_syncing); 29541635Sbonwick spa->spa_config_syncing = config; 2955789Sahrens 29562082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2957789Sahrens } 2958789Sahrens 29593912Slling static void 29603912Slling spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 29613912Slling { 29623912Slling spa_t *spa = arg1; 29633912Slling nvlist_t *nvp = arg2; 29643912Slling nvpair_t *nvpair; 29653912Slling objset_t *mos = spa->spa_meta_objset; 29663912Slling uint64_t zapobj; 29674451Seschrock uint64_t intval; 29683912Slling 29693912Slling mutex_enter(&spa->spa_props_lock); 29703912Slling if (spa->spa_pool_props_object == 0) { 29713912Slling zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 29723912Slling VERIFY(zapobj > 0); 29733912Slling 29743912Slling spa->spa_pool_props_object = zapobj; 29753912Slling 29763912Slling VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 29773912Slling DMU_POOL_PROPS, 8, 1, 29783912Slling &spa->spa_pool_props_object, tx) == 0); 29793912Slling } 29803912Slling mutex_exit(&spa->spa_props_lock); 29813912Slling 29823912Slling nvpair = NULL; 29833912Slling while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 29843912Slling switch (zpool_name_to_prop(nvpair_name(nvpair))) { 29854451Seschrock case ZPOOL_PROP_BOOTFS: 29863912Slling VERIFY(nvlist_lookup_uint64(nvp, 29873912Slling nvpair_name(nvpair), &spa->spa_bootfs) == 0); 29883912Slling VERIFY(zap_update(mos, 29893912Slling spa->spa_pool_props_object, 29904451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 8, 1, 29913912Slling &spa->spa_bootfs, tx) == 0); 29923912Slling break; 29934451Seschrock 29944451Seschrock case ZPOOL_PROP_AUTOREPLACE: 29954451Seschrock VERIFY(nvlist_lookup_uint64(nvp, 29964451Seschrock nvpair_name(nvpair), &intval) == 0); 29974451Seschrock VERIFY(zap_update(mos, 29984451Seschrock spa->spa_pool_props_object, 29994451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 8, 1, 30004451Seschrock &intval, tx) == 0); 30014451Seschrock break; 30023912Slling } 30033912Slling } 30043912Slling } 30053912Slling 3006789Sahrens /* 3007789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3008789Sahrens * part of the process, so we iterate until it converges. 3009789Sahrens */ 3010789Sahrens void 3011789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3012789Sahrens { 3013789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3014789Sahrens objset_t *mos = spa->spa_meta_objset; 3015789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 30161635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3017789Sahrens vdev_t *vd; 3018789Sahrens dmu_tx_t *tx; 3019789Sahrens int dirty_vdevs; 3020789Sahrens 3021789Sahrens /* 3022789Sahrens * Lock out configuration changes. 3023789Sahrens */ 30241544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3025789Sahrens 3026789Sahrens spa->spa_syncing_txg = txg; 3027789Sahrens spa->spa_sync_pass = 0; 3028789Sahrens 30291544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3030789Sahrens 30312082Seschrock tx = dmu_tx_create_assigned(dp, txg); 30322082Seschrock 30332082Seschrock /* 30342082Seschrock * If we are upgrading to ZFS_VERSION_RAIDZ_DEFLATE this txg, 30352082Seschrock * set spa_deflate if we have no raid-z vdevs. 30362082Seschrock */ 30372082Seschrock if (spa->spa_ubsync.ub_version < ZFS_VERSION_RAIDZ_DEFLATE && 30382082Seschrock spa->spa_uberblock.ub_version >= ZFS_VERSION_RAIDZ_DEFLATE) { 30392082Seschrock int i; 30402082Seschrock 30412082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 30422082Seschrock vd = rvd->vdev_child[i]; 30432082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 30442082Seschrock break; 30452082Seschrock } 30462082Seschrock if (i == rvd->vdev_children) { 30472082Seschrock spa->spa_deflate = TRUE; 30482082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 30492082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 30502082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 30512082Seschrock } 30522082Seschrock } 30532082Seschrock 3054789Sahrens /* 3055789Sahrens * If anything has changed in this txg, push the deferred frees 3056789Sahrens * from the previous txg. If not, leave them alone so that we 3057789Sahrens * don't generate work on an otherwise idle system. 3058789Sahrens */ 3059789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 30602329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 30612329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3062789Sahrens spa_sync_deferred_frees(spa, txg); 3063789Sahrens 3064789Sahrens /* 3065789Sahrens * Iterate to convergence. 3066789Sahrens */ 3067789Sahrens do { 3068789Sahrens spa->spa_sync_pass++; 3069789Sahrens 3070789Sahrens spa_sync_config_object(spa, tx); 30712082Seschrock spa_sync_spares(spa, tx); 30721544Seschrock spa_errlog_sync(spa, txg); 3073789Sahrens dsl_pool_sync(dp, txg); 3074789Sahrens 3075789Sahrens dirty_vdevs = 0; 3076789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3077789Sahrens vdev_sync(vd, txg); 3078789Sahrens dirty_vdevs++; 3079789Sahrens } 3080789Sahrens 3081789Sahrens bplist_sync(bpl, tx); 3082789Sahrens } while (dirty_vdevs); 3083789Sahrens 3084789Sahrens bplist_close(bpl); 3085789Sahrens 3086789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3087789Sahrens 3088789Sahrens /* 3089789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3090789Sahrens * to commit the transaction group. 30911635Sbonwick * 30921635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 30931635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 30941635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 30951635Sbonwick * If the write fails, try the next vdev until we're tried them all. 3096789Sahrens */ 30971635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 30981635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 30991635Sbonwick } else { 31001635Sbonwick int children = rvd->vdev_children; 31011635Sbonwick int c0 = spa_get_random(children); 31021635Sbonwick int c; 31031635Sbonwick 31041635Sbonwick for (c = 0; c < children; c++) { 31051635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 31061635Sbonwick if (vd->vdev_ms_array == 0) 31071635Sbonwick continue; 31081635Sbonwick if (vdev_config_sync(vd, txg) == 0) 31091635Sbonwick break; 31101635Sbonwick } 31111635Sbonwick if (c == children) 31121635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 31131635Sbonwick } 31141635Sbonwick 31152082Seschrock dmu_tx_commit(tx); 31162082Seschrock 31171635Sbonwick /* 31181635Sbonwick * Clear the dirty config list. 31191635Sbonwick */ 31201635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 31211635Sbonwick vdev_config_clean(vd); 31221635Sbonwick 31231635Sbonwick /* 31241635Sbonwick * Now that the new config has synced transactionally, 31251635Sbonwick * let it become visible to the config cache. 31261635Sbonwick */ 31271635Sbonwick if (spa->spa_config_syncing != NULL) { 31281635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 31291635Sbonwick spa->spa_config_txg = txg; 31301635Sbonwick spa->spa_config_syncing = NULL; 31311635Sbonwick } 3132789Sahrens 3133789Sahrens /* 3134789Sahrens * Make a stable copy of the fully synced uberblock. 3135789Sahrens * We use this as the root for pool traversals. 3136789Sahrens */ 3137789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3138789Sahrens 3139789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3140789Sahrens 3141789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3142789Sahrens spa->spa_traverse_wanted = 0; 3143789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3144789Sahrens rw_exit(&spa->spa_traverse_lock); 3145789Sahrens 3146789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3147789Sahrens 3148789Sahrens /* 3149789Sahrens * Clean up the ZIL records for the synced txg. 3150789Sahrens */ 3151789Sahrens dsl_pool_zil_clean(dp); 3152789Sahrens 3153789Sahrens /* 3154789Sahrens * Update usable space statistics. 3155789Sahrens */ 3156789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3157789Sahrens vdev_sync_done(vd, txg); 3158789Sahrens 3159789Sahrens /* 3160789Sahrens * It had better be the case that we didn't dirty anything 31612082Seschrock * since vdev_config_sync(). 3162789Sahrens */ 3163789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3164789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3165789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3166789Sahrens ASSERT(bpl->bpl_queue == NULL); 3167789Sahrens 31681544Seschrock spa_config_exit(spa, FTAG); 31691544Seschrock 31701544Seschrock /* 31711544Seschrock * If any async tasks have been requested, kick them off. 31721544Seschrock */ 31731544Seschrock spa_async_dispatch(spa); 3174789Sahrens } 3175789Sahrens 3176789Sahrens /* 3177789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3178789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3179789Sahrens * sync. 3180789Sahrens */ 3181789Sahrens void 3182789Sahrens spa_sync_allpools(void) 3183789Sahrens { 3184789Sahrens spa_t *spa = NULL; 3185789Sahrens mutex_enter(&spa_namespace_lock); 3186789Sahrens while ((spa = spa_next(spa)) != NULL) { 3187789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3188789Sahrens continue; 3189789Sahrens spa_open_ref(spa, FTAG); 3190789Sahrens mutex_exit(&spa_namespace_lock); 3191789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3192789Sahrens mutex_enter(&spa_namespace_lock); 3193789Sahrens spa_close(spa, FTAG); 3194789Sahrens } 3195789Sahrens mutex_exit(&spa_namespace_lock); 3196789Sahrens } 3197789Sahrens 3198789Sahrens /* 3199789Sahrens * ========================================================================== 3200789Sahrens * Miscellaneous routines 3201789Sahrens * ========================================================================== 3202789Sahrens */ 3203789Sahrens 3204789Sahrens /* 3205789Sahrens * Remove all pools in the system. 3206789Sahrens */ 3207789Sahrens void 3208789Sahrens spa_evict_all(void) 3209789Sahrens { 3210789Sahrens spa_t *spa; 3211789Sahrens 3212789Sahrens /* 3213789Sahrens * Remove all cached state. All pools should be closed now, 3214789Sahrens * so every spa in the AVL tree should be unreferenced. 3215789Sahrens */ 3216789Sahrens mutex_enter(&spa_namespace_lock); 3217789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3218789Sahrens /* 32191544Seschrock * Stop async tasks. The async thread may need to detach 32201544Seschrock * a device that's been replaced, which requires grabbing 32211544Seschrock * spa_namespace_lock, so we must drop it here. 3222789Sahrens */ 3223789Sahrens spa_open_ref(spa, FTAG); 3224789Sahrens mutex_exit(&spa_namespace_lock); 32251544Seschrock spa_async_suspend(spa); 3226789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3227789Sahrens mutex_enter(&spa_namespace_lock); 3228789Sahrens spa_close(spa, FTAG); 3229789Sahrens 3230789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3231789Sahrens spa_unload(spa); 3232789Sahrens spa_deactivate(spa); 3233789Sahrens } 3234789Sahrens spa_remove(spa); 3235789Sahrens } 3236789Sahrens mutex_exit(&spa_namespace_lock); 3237789Sahrens } 32381544Seschrock 32391544Seschrock vdev_t * 32401544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 32411544Seschrock { 32421544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 32431544Seschrock } 32441760Seschrock 32451760Seschrock void 32461760Seschrock spa_upgrade(spa_t *spa) 32471760Seschrock { 32481760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 32491760Seschrock 32501760Seschrock /* 32511760Seschrock * This should only be called for a non-faulted pool, and since a 32521760Seschrock * future version would result in an unopenable pool, this shouldn't be 32531760Seschrock * possible. 32541760Seschrock */ 32551760Seschrock ASSERT(spa->spa_uberblock.ub_version <= ZFS_VERSION); 32561760Seschrock 32571760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 32581760Seschrock vdev_config_dirty(spa->spa_root_vdev); 32591760Seschrock 32601760Seschrock spa_config_exit(spa, FTAG); 32612082Seschrock 32622082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 32631760Seschrock } 32642082Seschrock 32652082Seschrock boolean_t 32662082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 32672082Seschrock { 32682082Seschrock int i; 32693377Seschrock uint64_t spareguid; 32702082Seschrock 32712082Seschrock for (i = 0; i < spa->spa_nspares; i++) 32722082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 32732082Seschrock return (B_TRUE); 32742082Seschrock 32753377Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 32763377Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 32773377Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 32783377Seschrock spareguid == guid) 32793377Seschrock return (B_TRUE); 32803377Seschrock } 32813377Seschrock 32822082Seschrock return (B_FALSE); 32832082Seschrock } 32843912Slling 32853912Slling int 32863912Slling spa_set_props(spa_t *spa, nvlist_t *nvp) 32873912Slling { 32883912Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 32893912Slling spa, nvp, 3)); 32903912Slling } 32913912Slling 32923912Slling int 32933912Slling spa_get_props(spa_t *spa, nvlist_t **nvp) 32943912Slling { 32953912Slling zap_cursor_t zc; 32963912Slling zap_attribute_t za; 32973912Slling objset_t *mos = spa->spa_meta_objset; 32983912Slling zfs_source_t src; 32994451Seschrock zpool_prop_t prop; 33003912Slling nvlist_t *propval; 33013912Slling uint64_t value; 33023912Slling int err; 33033912Slling 33043912Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33053912Slling 33063912Slling mutex_enter(&spa->spa_props_lock); 33073912Slling /* If no props object, then just return empty nvlist */ 33083912Slling if (spa->spa_pool_props_object == 0) { 33093912Slling mutex_exit(&spa->spa_props_lock); 33103912Slling return (0); 33113912Slling } 33123912Slling 33133912Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 33143912Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 33153912Slling zap_cursor_advance(&zc)) { 33163912Slling 33173912Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 33183912Slling continue; 33193912Slling 33203912Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 33213912Slling switch (za.za_integer_length) { 33223912Slling case 8: 33234451Seschrock if (zpool_prop_default_numeric(prop) == 33243912Slling za.za_first_integer) 33253912Slling src = ZFS_SRC_DEFAULT; 33263912Slling else 33273912Slling src = ZFS_SRC_LOCAL; 33283912Slling value = za.za_first_integer; 33293912Slling 33304451Seschrock if (prop == ZPOOL_PROP_BOOTFS) { 33313912Slling dsl_pool_t *dp; 33323912Slling dsl_dataset_t *ds = NULL; 33333912Slling char strval[MAXPATHLEN]; 33343912Slling 33353912Slling dp = spa_get_dsl(spa); 33363912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 33373912Slling if ((err = dsl_dataset_open_obj(dp, 33383912Slling za.za_first_integer, NULL, DS_MODE_NONE, 33393912Slling FTAG, &ds)) != 0) { 33403912Slling rw_exit(&dp->dp_config_rwlock); 33413912Slling break; 33423912Slling } 33433912Slling dsl_dataset_name(ds, strval); 33443912Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 33453912Slling rw_exit(&dp->dp_config_rwlock); 33463912Slling 33473912Slling VERIFY(nvlist_add_uint64(propval, 33483912Slling ZFS_PROP_SOURCE, src) == 0); 33493912Slling VERIFY(nvlist_add_string(propval, 33503912Slling ZFS_PROP_VALUE, strval) == 0); 33513912Slling } else { 33523912Slling VERIFY(nvlist_add_uint64(propval, 33533912Slling ZFS_PROP_SOURCE, src) == 0); 33543912Slling VERIFY(nvlist_add_uint64(propval, 33553912Slling ZFS_PROP_VALUE, value) == 0); 33563912Slling } 33573912Slling VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 33583912Slling propval) == 0); 33593912Slling break; 33603912Slling } 33613912Slling nvlist_free(propval); 33623912Slling } 33633912Slling zap_cursor_fini(&zc); 33643912Slling mutex_exit(&spa->spa_props_lock); 33653912Slling if (err && err != ENOENT) { 33663912Slling nvlist_free(*nvp); 33673912Slling return (err); 33683912Slling } 33693912Slling 33703912Slling return (0); 33713912Slling } 33723912Slling 33733912Slling /* 33743912Slling * If the bootfs property value is dsobj, clear it. 33753912Slling */ 33763912Slling void 33773912Slling spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 33783912Slling { 33793912Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 33803912Slling VERIFY(zap_remove(spa->spa_meta_objset, 33813912Slling spa->spa_pool_props_object, 33824451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 33833912Slling spa->spa_bootfs = 0; 33843912Slling } 33853912Slling } 33864451Seschrock 33874451Seschrock /* 33884451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 33894451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 33904451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 33914451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 33924451Seschrock * or zdb as real changes. 33934451Seschrock */ 33944451Seschrock void 33954451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 33964451Seschrock { 33974451Seschrock #ifdef _KERNEL 33984451Seschrock sysevent_t *ev; 33994451Seschrock sysevent_attr_list_t *attr = NULL; 34004451Seschrock sysevent_value_t value; 34014451Seschrock sysevent_id_t eid; 34024451Seschrock 34034451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 34044451Seschrock SE_SLEEP); 34054451Seschrock 34064451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34074451Seschrock value.value.sv_string = spa_name(spa); 34084451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 34094451Seschrock goto done; 34104451Seschrock 34114451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34124451Seschrock value.value.sv_uint64 = spa_guid(spa); 34134451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 34144451Seschrock goto done; 34154451Seschrock 34164451Seschrock if (vd) { 34174451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 34184451Seschrock value.value.sv_uint64 = vd->vdev_guid; 34194451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 34204451Seschrock SE_SLEEP) != 0) 34214451Seschrock goto done; 34224451Seschrock 34234451Seschrock if (vd->vdev_path) { 34244451Seschrock value.value_type = SE_DATA_TYPE_STRING; 34254451Seschrock value.value.sv_string = vd->vdev_path; 34264451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 34274451Seschrock &value, SE_SLEEP) != 0) 34284451Seschrock goto done; 34294451Seschrock } 34304451Seschrock } 34314451Seschrock 34324451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 34334451Seschrock 34344451Seschrock done: 34354451Seschrock if (attr) 34364451Seschrock sysevent_free_attr(attr); 34374451Seschrock sysevent_free(ev); 34384451Seschrock #endif 34394451Seschrock } 3440