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> 51*3912Slling #include <sys/dmu_objset.h> 52789Sahrens #include <sys/unique.h> 53789Sahrens #include <sys/dsl_pool.h> 54*3912Slling #include <sys/dsl_dataset.h> 55789Sahrens #include <sys/dsl_dir.h> 56789Sahrens #include <sys/dsl_prop.h> 57*3912Slling #include <sys/dsl_synctask.h> 58789Sahrens #include <sys/fs/zfs.h> 59789Sahrens #include <sys/callb.h> 60789Sahrens 612986Sek110237 int zio_taskq_threads = 8; 622986Sek110237 63789Sahrens /* 64789Sahrens * ========================================================================== 65789Sahrens * SPA state manipulation (open/create/destroy/import/export) 66789Sahrens * ========================================================================== 67789Sahrens */ 68789Sahrens 691544Seschrock static int 701544Seschrock spa_error_entry_compare(const void *a, const void *b) 711544Seschrock { 721544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 731544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 741544Seschrock int ret; 751544Seschrock 761544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 771544Seschrock sizeof (zbookmark_t)); 781544Seschrock 791544Seschrock if (ret < 0) 801544Seschrock return (-1); 811544Seschrock else if (ret > 0) 821544Seschrock return (1); 831544Seschrock else 841544Seschrock return (0); 851544Seschrock } 861544Seschrock 871544Seschrock /* 881544Seschrock * Utility function which retrieves copies of the current logs and 891544Seschrock * re-initializes them in the process. 901544Seschrock */ 911544Seschrock void 921544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 931544Seschrock { 941544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 951544Seschrock 961544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 971544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 981544Seschrock 991544Seschrock avl_create(&spa->spa_errlist_scrub, 1001544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1011544Seschrock offsetof(spa_error_entry_t, se_avl)); 1021544Seschrock avl_create(&spa->spa_errlist_last, 1031544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1041544Seschrock offsetof(spa_error_entry_t, se_avl)); 1051544Seschrock } 1061544Seschrock 107789Sahrens /* 108789Sahrens * Activate an uninitialized pool. 109789Sahrens */ 110789Sahrens static void 111789Sahrens spa_activate(spa_t *spa) 112789Sahrens { 113789Sahrens int t; 114789Sahrens 115789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 116789Sahrens 117789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 118789Sahrens 119789Sahrens spa->spa_normal_class = metaslab_class_create(); 120789Sahrens 121789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 122789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 1232986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 124789Sahrens TASKQ_PREPOPULATE); 125789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 1262986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 127789Sahrens TASKQ_PREPOPULATE); 128789Sahrens } 129789Sahrens 130789Sahrens rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 131789Sahrens 1322856Snd150628 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 1332856Snd150628 mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL); 1342856Snd150628 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 1352856Snd150628 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 1362856Snd150628 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 1372856Snd150628 mutex_init(&spa->spa_config_lock.scl_lock, NULL, MUTEX_DEFAULT, NULL); 1382856Snd150628 mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL); 1392926Sek110237 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 140*3912Slling mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 1412856Snd150628 142789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 143789Sahrens offsetof(vdev_t, vdev_dirty_node)); 144789Sahrens 145789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 146789Sahrens offsetof(struct vdev, vdev_txg_node)); 1471544Seschrock 1481544Seschrock avl_create(&spa->spa_errlist_scrub, 1491544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1501544Seschrock offsetof(spa_error_entry_t, se_avl)); 1511544Seschrock avl_create(&spa->spa_errlist_last, 1521544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1531544Seschrock offsetof(spa_error_entry_t, se_avl)); 154789Sahrens } 155789Sahrens 156789Sahrens /* 157789Sahrens * Opposite of spa_activate(). 158789Sahrens */ 159789Sahrens static void 160789Sahrens spa_deactivate(spa_t *spa) 161789Sahrens { 162789Sahrens int t; 163789Sahrens 164789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 165789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 166789Sahrens ASSERT(spa->spa_root_vdev == NULL); 167789Sahrens 168789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 169789Sahrens 170789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 171789Sahrens 172789Sahrens list_destroy(&spa->spa_dirty_list); 173789Sahrens 174789Sahrens rw_destroy(&spa->spa_traverse_lock); 175789Sahrens 176789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 177789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 178789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 179789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 180789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 181789Sahrens } 182789Sahrens 183789Sahrens metaslab_class_destroy(spa->spa_normal_class); 184789Sahrens spa->spa_normal_class = NULL; 185789Sahrens 1861544Seschrock /* 1871544Seschrock * If this was part of an import or the open otherwise failed, we may 1881544Seschrock * still have errors left in the queues. Empty them just in case. 1891544Seschrock */ 1901544Seschrock spa_errlog_drain(spa); 1911544Seschrock 1921544Seschrock avl_destroy(&spa->spa_errlist_scrub); 1931544Seschrock avl_destroy(&spa->spa_errlist_last); 1941544Seschrock 195789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 196789Sahrens } 197789Sahrens 198789Sahrens /* 199789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 200789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 201789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 202789Sahrens * All vdev validation is done by the vdev_alloc() routine. 203789Sahrens */ 2042082Seschrock static int 2052082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 2062082Seschrock uint_t id, int atype) 207789Sahrens { 208789Sahrens nvlist_t **child; 209789Sahrens uint_t c, children; 2102082Seschrock int error; 2112082Seschrock 2122082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 2132082Seschrock return (error); 2142082Seschrock 2152082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 2162082Seschrock return (0); 217789Sahrens 218789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 219789Sahrens &child, &children) != 0) { 2202082Seschrock vdev_free(*vdp); 2212082Seschrock *vdp = NULL; 2222082Seschrock return (EINVAL); 223789Sahrens } 224789Sahrens 225789Sahrens for (c = 0; c < children; c++) { 2262082Seschrock vdev_t *vd; 2272082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 2282082Seschrock atype)) != 0) { 2292082Seschrock vdev_free(*vdp); 2302082Seschrock *vdp = NULL; 2312082Seschrock return (error); 232789Sahrens } 233789Sahrens } 234789Sahrens 2352082Seschrock ASSERT(*vdp != NULL); 2362082Seschrock 2372082Seschrock return (0); 238789Sahrens } 239789Sahrens 240789Sahrens /* 241789Sahrens * Opposite of spa_load(). 242789Sahrens */ 243789Sahrens static void 244789Sahrens spa_unload(spa_t *spa) 245789Sahrens { 2462082Seschrock int i; 2472082Seschrock 248789Sahrens /* 2491544Seschrock * Stop async tasks. 2501544Seschrock */ 2511544Seschrock spa_async_suspend(spa); 2521544Seschrock 2531544Seschrock /* 254789Sahrens * Stop syncing. 255789Sahrens */ 256789Sahrens if (spa->spa_sync_on) { 257789Sahrens txg_sync_stop(spa->spa_dsl_pool); 258789Sahrens spa->spa_sync_on = B_FALSE; 259789Sahrens } 260789Sahrens 261789Sahrens /* 262789Sahrens * Wait for any outstanding prefetch I/O to complete. 263789Sahrens */ 2641544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2651544Seschrock spa_config_exit(spa, FTAG); 266789Sahrens 267789Sahrens /* 268789Sahrens * Close the dsl pool. 269789Sahrens */ 270789Sahrens if (spa->spa_dsl_pool) { 271789Sahrens dsl_pool_close(spa->spa_dsl_pool); 272789Sahrens spa->spa_dsl_pool = NULL; 273789Sahrens } 274789Sahrens 275789Sahrens /* 276789Sahrens * Close all vdevs. 277789Sahrens */ 2781585Sbonwick if (spa->spa_root_vdev) 279789Sahrens vdev_free(spa->spa_root_vdev); 2801585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 2811544Seschrock 2822082Seschrock for (i = 0; i < spa->spa_nspares; i++) 2832082Seschrock vdev_free(spa->spa_spares[i]); 2842082Seschrock if (spa->spa_spares) { 2852082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 2862082Seschrock spa->spa_spares = NULL; 2872082Seschrock } 2882082Seschrock if (spa->spa_sparelist) { 2892082Seschrock nvlist_free(spa->spa_sparelist); 2902082Seschrock spa->spa_sparelist = NULL; 2912082Seschrock } 2922082Seschrock 2931544Seschrock spa->spa_async_suspended = 0; 294789Sahrens } 295789Sahrens 296789Sahrens /* 2972082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 2982082Seschrock * this pool. When this is called, we have some form of basic information in 2992082Seschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 3002082Seschrock * re-generate a more complete list including status information. 3012082Seschrock */ 3022082Seschrock static void 3032082Seschrock spa_load_spares(spa_t *spa) 3042082Seschrock { 3052082Seschrock nvlist_t **spares; 3062082Seschrock uint_t nspares; 3072082Seschrock int i; 3083377Seschrock vdev_t *vd, *tvd; 3092082Seschrock 3102082Seschrock /* 3112082Seschrock * First, close and free any existing spare vdevs. 3122082Seschrock */ 3132082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3143377Seschrock vd = spa->spa_spares[i]; 3153377Seschrock 3163377Seschrock /* Undo the call to spa_activate() below */ 3173377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 3183377Seschrock tvd->vdev_isspare) 3193377Seschrock spa_spare_remove(tvd); 3203377Seschrock vdev_close(vd); 3213377Seschrock vdev_free(vd); 3222082Seschrock } 3233377Seschrock 3242082Seschrock if (spa->spa_spares) 3252082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 3262082Seschrock 3272082Seschrock if (spa->spa_sparelist == NULL) 3282082Seschrock nspares = 0; 3292082Seschrock else 3302082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 3312082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 3322082Seschrock 3332082Seschrock spa->spa_nspares = (int)nspares; 3342082Seschrock spa->spa_spares = NULL; 3352082Seschrock 3362082Seschrock if (nspares == 0) 3372082Seschrock return; 3382082Seschrock 3392082Seschrock /* 3402082Seschrock * Construct the array of vdevs, opening them to get status in the 3413377Seschrock * process. For each spare, there is potentially two different vdev_t 3423377Seschrock * structures associated with it: one in the list of spares (used only 3433377Seschrock * for basic validation purposes) and one in the active vdev 3443377Seschrock * configuration (if it's spared in). During this phase we open and 3453377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 3463377Seschrock * active configuration, then we also mark this vdev as an active spare. 3472082Seschrock */ 3482082Seschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 3492082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3502082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 3512082Seschrock VDEV_ALLOC_SPARE) == 0); 3522082Seschrock ASSERT(vd != NULL); 3532082Seschrock 3542082Seschrock spa->spa_spares[i] = vd; 3552082Seschrock 3563377Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 3573377Seschrock if (!tvd->vdev_isspare) 3583377Seschrock spa_spare_add(tvd); 3593377Seschrock 3603377Seschrock /* 3613377Seschrock * We only mark the spare active if we were successfully 3623377Seschrock * able to load the vdev. Otherwise, importing a pool 3633377Seschrock * with a bad active spare would result in strange 3643377Seschrock * behavior, because multiple pool would think the spare 3653377Seschrock * is actively in use. 3663377Seschrock * 3673377Seschrock * There is a vulnerability here to an equally bizarre 3683377Seschrock * circumstance, where a dead active spare is later 3693377Seschrock * brought back to life (onlined or otherwise). Given 3703377Seschrock * the rarity of this scenario, and the extra complexity 3713377Seschrock * it adds, we ignore the possibility. 3723377Seschrock */ 3733377Seschrock if (!vdev_is_dead(tvd)) 3743377Seschrock spa_spare_activate(tvd); 3753377Seschrock } 3763377Seschrock 3772082Seschrock if (vdev_open(vd) != 0) 3782082Seschrock continue; 3792082Seschrock 3802082Seschrock vd->vdev_top = vd; 3812082Seschrock (void) vdev_validate_spare(vd); 3822082Seschrock } 3832082Seschrock 3842082Seschrock /* 3852082Seschrock * Recompute the stashed list of spares, with status information 3862082Seschrock * this time. 3872082Seschrock */ 3882082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3892082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3902082Seschrock 3912082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 3922082Seschrock for (i = 0; i < spa->spa_nspares; i++) 3932082Seschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 3942082Seschrock B_TRUE, B_TRUE); 3952082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3962082Seschrock spares, spa->spa_nspares) == 0); 3972082Seschrock for (i = 0; i < spa->spa_nspares; i++) 3982082Seschrock nvlist_free(spares[i]); 3992082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 4002082Seschrock } 4012082Seschrock 4022082Seschrock static int 4032082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 4042082Seschrock { 4052082Seschrock dmu_buf_t *db; 4062082Seschrock char *packed = NULL; 4072082Seschrock size_t nvsize = 0; 4082082Seschrock int error; 4092082Seschrock *value = NULL; 4102082Seschrock 4112082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 4122082Seschrock nvsize = *(uint64_t *)db->db_data; 4132082Seschrock dmu_buf_rele(db, FTAG); 4142082Seschrock 4152082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 4162082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 4172082Seschrock if (error == 0) 4182082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 4192082Seschrock kmem_free(packed, nvsize); 4202082Seschrock 4212082Seschrock return (error); 4222082Seschrock } 4232082Seschrock 4242082Seschrock /* 425789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 4261544Seschrock * source of configuration information. 427789Sahrens */ 428789Sahrens static int 4291544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 430789Sahrens { 431789Sahrens int error = 0; 432789Sahrens nvlist_t *nvroot = NULL; 433789Sahrens vdev_t *rvd; 434789Sahrens uberblock_t *ub = &spa->spa_uberblock; 4351635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 436789Sahrens uint64_t pool_guid; 4372082Seschrock uint64_t version; 438789Sahrens zio_t *zio; 439789Sahrens 4401544Seschrock spa->spa_load_state = state; 4411635Sbonwick 442789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 4431733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 4441544Seschrock error = EINVAL; 4451544Seschrock goto out; 4461544Seschrock } 447789Sahrens 4482082Seschrock /* 4492082Seschrock * Versioning wasn't explicitly added to the label until later, so if 4502082Seschrock * it's not present treat it as the initial version. 4512082Seschrock */ 4522082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 4532082Seschrock version = ZFS_VERSION_INITIAL; 4542082Seschrock 4551733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 4561733Sbonwick &spa->spa_config_txg); 4571733Sbonwick 4581635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 4591544Seschrock spa_guid_exists(pool_guid, 0)) { 4601544Seschrock error = EEXIST; 4611544Seschrock goto out; 4621544Seschrock } 463789Sahrens 4642174Seschrock spa->spa_load_guid = pool_guid; 4652174Seschrock 466789Sahrens /* 4672082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 4682082Seschrock * value that will be returned by spa_version() since parsing the 4692082Seschrock * configuration requires knowing the version number. 470789Sahrens */ 4711544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 4722082Seschrock spa->spa_ubsync.ub_version = version; 4732082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 4741544Seschrock spa_config_exit(spa, FTAG); 475789Sahrens 4762082Seschrock if (error != 0) 4771544Seschrock goto out; 478789Sahrens 4791585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 480789Sahrens ASSERT(spa_guid(spa) == pool_guid); 481789Sahrens 482789Sahrens /* 483789Sahrens * Try to open all vdevs, loading each label in the process. 484789Sahrens */ 4851544Seschrock if (vdev_open(rvd) != 0) { 4861544Seschrock error = ENXIO; 4871544Seschrock goto out; 4881544Seschrock } 489789Sahrens 490789Sahrens /* 4911986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 4921986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 4931986Seschrock * flag. 4941986Seschrock */ 4951986Seschrock spa_config_enter(spa, RW_READER, FTAG); 4961986Seschrock error = vdev_validate(rvd); 4971986Seschrock spa_config_exit(spa, FTAG); 4981986Seschrock 4991986Seschrock if (error != 0) { 5001986Seschrock error = EBADF; 5011986Seschrock goto out; 5021986Seschrock } 5031986Seschrock 5041986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 5051986Seschrock error = ENXIO; 5061986Seschrock goto out; 5071986Seschrock } 5081986Seschrock 5091986Seschrock /* 510789Sahrens * Find the best uberblock. 511789Sahrens */ 512789Sahrens bzero(ub, sizeof (uberblock_t)); 513789Sahrens 514789Sahrens zio = zio_root(spa, NULL, NULL, 515789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 516789Sahrens vdev_uberblock_load(zio, rvd, ub); 517789Sahrens error = zio_wait(zio); 518789Sahrens 519789Sahrens /* 520789Sahrens * If we weren't able to find a single valid uberblock, return failure. 521789Sahrens */ 522789Sahrens if (ub->ub_txg == 0) { 5231760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5241760Seschrock VDEV_AUX_CORRUPT_DATA); 5251544Seschrock error = ENXIO; 5261544Seschrock goto out; 5271544Seschrock } 5281544Seschrock 5291544Seschrock /* 5301544Seschrock * If the pool is newer than the code, we can't open it. 5311544Seschrock */ 5321760Seschrock if (ub->ub_version > ZFS_VERSION) { 5331760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5341760Seschrock VDEV_AUX_VERSION_NEWER); 5351544Seschrock error = ENOTSUP; 5361544Seschrock goto out; 537789Sahrens } 538789Sahrens 539789Sahrens /* 540789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 541789Sahrens * incomplete configuration. 542789Sahrens */ 5431732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 5441544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5451544Seschrock VDEV_AUX_BAD_GUID_SUM); 5461544Seschrock error = ENXIO; 5471544Seschrock goto out; 548789Sahrens } 549789Sahrens 550789Sahrens /* 551789Sahrens * Initialize internal SPA structures. 552789Sahrens */ 553789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 554789Sahrens spa->spa_ubsync = spa->spa_uberblock; 555789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 5561544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 5571544Seschrock if (error) { 5581544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5591544Seschrock VDEV_AUX_CORRUPT_DATA); 5601544Seschrock goto out; 5611544Seschrock } 562789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 563789Sahrens 5641544Seschrock if (zap_lookup(spa->spa_meta_objset, 565789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 5661544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 5671544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5681544Seschrock VDEV_AUX_CORRUPT_DATA); 5691544Seschrock error = EIO; 5701544Seschrock goto out; 5711544Seschrock } 572789Sahrens 573789Sahrens if (!mosconfig) { 5742082Seschrock nvlist_t *newconfig; 5752082Seschrock 5762082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 5771544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5781544Seschrock VDEV_AUX_CORRUPT_DATA); 5791544Seschrock error = EIO; 5801544Seschrock goto out; 5811544Seschrock } 582789Sahrens 583789Sahrens spa_config_set(spa, newconfig); 584789Sahrens spa_unload(spa); 585789Sahrens spa_deactivate(spa); 586789Sahrens spa_activate(spa); 587789Sahrens 5881544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 5891544Seschrock } 5901544Seschrock 5911544Seschrock if (zap_lookup(spa->spa_meta_objset, 5921544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 5931544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 5941544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5951544Seschrock VDEV_AUX_CORRUPT_DATA); 5961544Seschrock error = EIO; 5971544Seschrock goto out; 598789Sahrens } 599789Sahrens 6001544Seschrock /* 6012082Seschrock * Load the bit that tells us to use the new accounting function 6022082Seschrock * (raid-z deflation). If we have an older pool, this will not 6032082Seschrock * be present. 6042082Seschrock */ 6052082Seschrock error = zap_lookup(spa->spa_meta_objset, 6062082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 6072082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 6082082Seschrock if (error != 0 && error != ENOENT) { 6092082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6102082Seschrock VDEV_AUX_CORRUPT_DATA); 6112082Seschrock error = EIO; 6122082Seschrock goto out; 6132082Seschrock } 6142082Seschrock 6152082Seschrock /* 6161544Seschrock * Load the persistent error log. If we have an older pool, this will 6171544Seschrock * not be present. 6181544Seschrock */ 6191544Seschrock error = zap_lookup(spa->spa_meta_objset, 6201544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 6211544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 6221807Sbonwick if (error != 0 && error != ENOENT) { 6231544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6241544Seschrock VDEV_AUX_CORRUPT_DATA); 6251544Seschrock error = EIO; 6261544Seschrock goto out; 6271544Seschrock } 6281544Seschrock 6291544Seschrock error = zap_lookup(spa->spa_meta_objset, 6301544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 6311544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 6321544Seschrock if (error != 0 && error != ENOENT) { 6331544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6341544Seschrock VDEV_AUX_CORRUPT_DATA); 6351544Seschrock error = EIO; 6361544Seschrock goto out; 6371544Seschrock } 638789Sahrens 639789Sahrens /* 6402926Sek110237 * Load the history object. If we have an older pool, this 6412926Sek110237 * will not be present. 6422926Sek110237 */ 6432926Sek110237 error = zap_lookup(spa->spa_meta_objset, 6442926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 6452926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 6462926Sek110237 if (error != 0 && error != ENOENT) { 6472926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6482926Sek110237 VDEV_AUX_CORRUPT_DATA); 6492926Sek110237 error = EIO; 6502926Sek110237 goto out; 6512926Sek110237 } 6522926Sek110237 6532926Sek110237 /* 6542082Seschrock * Load any hot spares for this pool. 6552082Seschrock */ 6562082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 6572082Seschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 6582082Seschrock if (error != 0 && error != ENOENT) { 6592082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6602082Seschrock VDEV_AUX_CORRUPT_DATA); 6612082Seschrock error = EIO; 6622082Seschrock goto out; 6632082Seschrock } 6642082Seschrock if (error == 0) { 6652082Seschrock ASSERT(spa_version(spa) >= ZFS_VERSION_SPARES); 6662082Seschrock if (load_nvlist(spa, spa->spa_spares_object, 6672082Seschrock &spa->spa_sparelist) != 0) { 6682082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6692082Seschrock VDEV_AUX_CORRUPT_DATA); 6702082Seschrock error = EIO; 6712082Seschrock goto out; 6722082Seschrock } 6732082Seschrock 6742082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6752082Seschrock spa_load_spares(spa); 6762082Seschrock spa_config_exit(spa, FTAG); 6772082Seschrock } 6782082Seschrock 679*3912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 680*3912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 681*3912Slling 682*3912Slling if (error && error != ENOENT) { 683*3912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 684*3912Slling VDEV_AUX_CORRUPT_DATA); 685*3912Slling error = EIO; 686*3912Slling goto out; 687*3912Slling } 688*3912Slling 689*3912Slling if (error == 0) { 690*3912Slling (void) zap_lookup(spa->spa_meta_objset, 691*3912Slling spa->spa_pool_props_object, 692*3912Slling zpool_prop_to_name(ZFS_PROP_BOOTFS), 693*3912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 694*3912Slling } 695*3912Slling 6962082Seschrock /* 6971986Seschrock * Load the vdev state for all toplevel vdevs. 698789Sahrens */ 6991986Seschrock vdev_load(rvd); 700789Sahrens 701789Sahrens /* 702789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 703789Sahrens */ 7041544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 705789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 7061544Seschrock spa_config_exit(spa, FTAG); 707789Sahrens 708789Sahrens /* 709789Sahrens * Check the state of the root vdev. If it can't be opened, it 710789Sahrens * indicates one or more toplevel vdevs are faulted. 711789Sahrens */ 7121544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 7131544Seschrock error = ENXIO; 7141544Seschrock goto out; 7151544Seschrock } 716789Sahrens 7171544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 7181635Sbonwick dmu_tx_t *tx; 7191635Sbonwick int need_update = B_FALSE; 7201585Sbonwick int c; 7211601Sbonwick 7221635Sbonwick /* 7231635Sbonwick * Claim log blocks that haven't been committed yet. 7241635Sbonwick * This must all happen in a single txg. 7251635Sbonwick */ 7261601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 727789Sahrens spa_first_txg(spa)); 7282417Sahrens (void) dmu_objset_find(spa->spa_name, 7292417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 730789Sahrens dmu_tx_commit(tx); 731789Sahrens 732789Sahrens spa->spa_sync_on = B_TRUE; 733789Sahrens txg_sync_start(spa->spa_dsl_pool); 734789Sahrens 735789Sahrens /* 736789Sahrens * Wait for all claims to sync. 737789Sahrens */ 738789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 7391585Sbonwick 7401585Sbonwick /* 7411635Sbonwick * If the config cache is stale, or we have uninitialized 7421635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 7431585Sbonwick */ 7441635Sbonwick if (config_cache_txg != spa->spa_config_txg || 7451635Sbonwick state == SPA_LOAD_IMPORT) 7461635Sbonwick need_update = B_TRUE; 7471635Sbonwick 7481635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 7491635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 7501635Sbonwick need_update = B_TRUE; 7511585Sbonwick 7521585Sbonwick /* 7531635Sbonwick * Update the config cache asychronously in case we're the 7541635Sbonwick * root pool, in which case the config cache isn't writable yet. 7551585Sbonwick */ 7561635Sbonwick if (need_update) 7571635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 758789Sahrens } 759789Sahrens 7601544Seschrock error = 0; 7611544Seschrock out: 7622082Seschrock if (error && error != EBADF) 7631544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 7641544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 7651544Seschrock spa->spa_ena = 0; 7661544Seschrock 7671544Seschrock return (error); 768789Sahrens } 769789Sahrens 770789Sahrens /* 771789Sahrens * Pool Open/Import 772789Sahrens * 773789Sahrens * The import case is identical to an open except that the configuration is sent 774789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 775789Sahrens * case of an open, the pool configuration will exist in the 776789Sahrens * POOL_STATE_UNITIALIZED state. 777789Sahrens * 778789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 779789Sahrens * the same time open the pool, without having to keep around the spa_t in some 780789Sahrens * ambiguous state. 781789Sahrens */ 782789Sahrens static int 783789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 784789Sahrens { 785789Sahrens spa_t *spa; 786789Sahrens int error; 787789Sahrens int loaded = B_FALSE; 788789Sahrens int locked = B_FALSE; 789789Sahrens 790789Sahrens *spapp = NULL; 791789Sahrens 792789Sahrens /* 793789Sahrens * As disgusting as this is, we need to support recursive calls to this 794789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 795789Sahrens * up calling spa_open() again. The real fix is to figure out how to 796789Sahrens * avoid dsl_dir_open() calling this in the first place. 797789Sahrens */ 798789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 799789Sahrens mutex_enter(&spa_namespace_lock); 800789Sahrens locked = B_TRUE; 801789Sahrens } 802789Sahrens 803789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 804789Sahrens if (locked) 805789Sahrens mutex_exit(&spa_namespace_lock); 806789Sahrens return (ENOENT); 807789Sahrens } 808789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 809789Sahrens 810789Sahrens spa_activate(spa); 811789Sahrens 8121635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 813789Sahrens 814789Sahrens if (error == EBADF) { 815789Sahrens /* 8161986Seschrock * If vdev_validate() returns failure (indicated by 8171986Seschrock * EBADF), it indicates that one of the vdevs indicates 8181986Seschrock * that the pool has been exported or destroyed. If 8191986Seschrock * this is the case, the config cache is out of sync and 8201986Seschrock * we should remove the pool from the namespace. 821789Sahrens */ 8222082Seschrock zfs_post_ok(spa, NULL); 823789Sahrens spa_unload(spa); 824789Sahrens spa_deactivate(spa); 825789Sahrens spa_remove(spa); 826789Sahrens spa_config_sync(); 827789Sahrens if (locked) 828789Sahrens mutex_exit(&spa_namespace_lock); 829789Sahrens return (ENOENT); 8301544Seschrock } 8311544Seschrock 8321544Seschrock if (error) { 833789Sahrens /* 834789Sahrens * We can't open the pool, but we still have useful 835789Sahrens * information: the state of each vdev after the 836789Sahrens * attempted vdev_open(). Return this to the user. 837789Sahrens */ 8381635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 8391635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 840789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 841789Sahrens B_TRUE); 8421635Sbonwick spa_config_exit(spa, FTAG); 8431635Sbonwick } 844789Sahrens spa_unload(spa); 845789Sahrens spa_deactivate(spa); 8461544Seschrock spa->spa_last_open_failed = B_TRUE; 847789Sahrens if (locked) 848789Sahrens mutex_exit(&spa_namespace_lock); 849789Sahrens *spapp = NULL; 850789Sahrens return (error); 8511544Seschrock } else { 8521544Seschrock zfs_post_ok(spa, NULL); 8531544Seschrock spa->spa_last_open_failed = B_FALSE; 854789Sahrens } 855789Sahrens 856789Sahrens loaded = B_TRUE; 857789Sahrens } 858789Sahrens 859789Sahrens spa_open_ref(spa, tag); 860789Sahrens if (locked) 861789Sahrens mutex_exit(&spa_namespace_lock); 862789Sahrens 863789Sahrens *spapp = spa; 864789Sahrens 865789Sahrens if (config != NULL) { 8661544Seschrock spa_config_enter(spa, RW_READER, FTAG); 867789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 8681544Seschrock spa_config_exit(spa, FTAG); 869789Sahrens } 870789Sahrens 871789Sahrens /* 872789Sahrens * If we just loaded the pool, resilver anything that's out of date. 873789Sahrens */ 874789Sahrens if (loaded && (spa_mode & FWRITE)) 875789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 876789Sahrens 877789Sahrens return (0); 878789Sahrens } 879789Sahrens 880789Sahrens int 881789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 882789Sahrens { 883789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 884789Sahrens } 885789Sahrens 8861544Seschrock /* 8871544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 8881544Seschrock * preventing it from being exported or destroyed. 8891544Seschrock */ 8901544Seschrock spa_t * 8911544Seschrock spa_inject_addref(char *name) 8921544Seschrock { 8931544Seschrock spa_t *spa; 8941544Seschrock 8951544Seschrock mutex_enter(&spa_namespace_lock); 8961544Seschrock if ((spa = spa_lookup(name)) == NULL) { 8971544Seschrock mutex_exit(&spa_namespace_lock); 8981544Seschrock return (NULL); 8991544Seschrock } 9001544Seschrock spa->spa_inject_ref++; 9011544Seschrock mutex_exit(&spa_namespace_lock); 9021544Seschrock 9031544Seschrock return (spa); 9041544Seschrock } 9051544Seschrock 9061544Seschrock void 9071544Seschrock spa_inject_delref(spa_t *spa) 9081544Seschrock { 9091544Seschrock mutex_enter(&spa_namespace_lock); 9101544Seschrock spa->spa_inject_ref--; 9111544Seschrock mutex_exit(&spa_namespace_lock); 9121544Seschrock } 9131544Seschrock 9142082Seschrock static void 9152082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 9162082Seschrock { 9172082Seschrock nvlist_t **spares; 9182082Seschrock uint_t i, nspares; 9192082Seschrock nvlist_t *nvroot; 9202082Seschrock uint64_t guid; 9212082Seschrock vdev_stat_t *vs; 9222082Seschrock uint_t vsc; 9233377Seschrock uint64_t pool; 9242082Seschrock 9252082Seschrock if (spa->spa_nspares == 0) 9262082Seschrock return; 9272082Seschrock 9282082Seschrock VERIFY(nvlist_lookup_nvlist(config, 9292082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 9302082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 9312082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 9322082Seschrock if (nspares != 0) { 9332082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 9342082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 9352082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 9362082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 9372082Seschrock 9382082Seschrock /* 9392082Seschrock * Go through and find any spares which have since been 9402082Seschrock * repurposed as an active spare. If this is the case, update 9412082Seschrock * their status appropriately. 9422082Seschrock */ 9432082Seschrock for (i = 0; i < nspares; i++) { 9442082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 9452082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 9463377Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 9472082Seschrock VERIFY(nvlist_lookup_uint64_array( 9482082Seschrock spares[i], ZPOOL_CONFIG_STATS, 9492082Seschrock (uint64_t **)&vs, &vsc) == 0); 9502082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 9512082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 9522082Seschrock } 9532082Seschrock } 9542082Seschrock } 9552082Seschrock } 9562082Seschrock 957789Sahrens int 9581544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 959789Sahrens { 960789Sahrens int error; 961789Sahrens spa_t *spa; 962789Sahrens 963789Sahrens *config = NULL; 964789Sahrens error = spa_open_common(name, &spa, FTAG, config); 965789Sahrens 9662082Seschrock if (spa && *config != NULL) { 9671544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 9681544Seschrock spa_get_errlog_size(spa)) == 0); 9691544Seschrock 9702082Seschrock spa_add_spares(spa, *config); 9712082Seschrock } 9722082Seschrock 9731544Seschrock /* 9741544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 9751544Seschrock * and call spa_lookup() directly. 9761544Seschrock */ 9771544Seschrock if (altroot) { 9781544Seschrock if (spa == NULL) { 9791544Seschrock mutex_enter(&spa_namespace_lock); 9801544Seschrock spa = spa_lookup(name); 9811544Seschrock if (spa) 9821544Seschrock spa_altroot(spa, altroot, buflen); 9831544Seschrock else 9841544Seschrock altroot[0] = '\0'; 9851544Seschrock spa = NULL; 9861544Seschrock mutex_exit(&spa_namespace_lock); 9871544Seschrock } else { 9881544Seschrock spa_altroot(spa, altroot, buflen); 9891544Seschrock } 9901544Seschrock } 9911544Seschrock 992789Sahrens if (spa != NULL) 993789Sahrens spa_close(spa, FTAG); 994789Sahrens 995789Sahrens return (error); 996789Sahrens } 997789Sahrens 998789Sahrens /* 9992082Seschrock * Validate that the 'spares' array is well formed. We must have an array of 10003377Seschrock * nvlists, each which describes a valid leaf vdev. If this is an import (mode 10013377Seschrock * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 10023377Seschrock * as they are well-formed. 10032082Seschrock */ 10042082Seschrock static int 10052082Seschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 10062082Seschrock { 10072082Seschrock nvlist_t **spares; 10082082Seschrock uint_t i, nspares; 10092082Seschrock vdev_t *vd; 10102082Seschrock int error; 10112082Seschrock 10122082Seschrock /* 10132082Seschrock * It's acceptable to have no spares specified. 10142082Seschrock */ 10152082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10162082Seschrock &spares, &nspares) != 0) 10172082Seschrock return (0); 10182082Seschrock 10192082Seschrock if (nspares == 0) 10202082Seschrock return (EINVAL); 10212082Seschrock 10222082Seschrock /* 10232082Seschrock * Make sure the pool is formatted with a version that supports hot 10242082Seschrock * spares. 10252082Seschrock */ 10262082Seschrock if (spa_version(spa) < ZFS_VERSION_SPARES) 10272082Seschrock return (ENOTSUP); 10282082Seschrock 10293377Seschrock /* 10303377Seschrock * Set the pending spare list so we correctly handle device in-use 10313377Seschrock * checking. 10323377Seschrock */ 10333377Seschrock spa->spa_pending_spares = spares; 10343377Seschrock spa->spa_pending_nspares = nspares; 10353377Seschrock 10362082Seschrock for (i = 0; i < nspares; i++) { 10372082Seschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 10382082Seschrock mode)) != 0) 10393377Seschrock goto out; 10402082Seschrock 10412082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 10422082Seschrock vdev_free(vd); 10433377Seschrock error = EINVAL; 10443377Seschrock goto out; 10452082Seschrock } 10462082Seschrock 10472082Seschrock vd->vdev_top = vd; 10483377Seschrock 10493377Seschrock if ((error = vdev_open(vd)) == 0 && 10503377Seschrock (error = vdev_label_init(vd, crtxg, 10513377Seschrock VDEV_LABEL_SPARE)) == 0) { 10523377Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 10533377Seschrock vd->vdev_guid) == 0); 10542082Seschrock } 10552082Seschrock 10562082Seschrock vdev_free(vd); 10573377Seschrock 10583377Seschrock if (error && mode != VDEV_ALLOC_SPARE) 10593377Seschrock goto out; 10603377Seschrock else 10613377Seschrock error = 0; 10622082Seschrock } 10632082Seschrock 10643377Seschrock out: 10653377Seschrock spa->spa_pending_spares = NULL; 10663377Seschrock spa->spa_pending_nspares = 0; 10673377Seschrock return (error); 10682082Seschrock } 10692082Seschrock 10702082Seschrock /* 1071789Sahrens * Pool Creation 1072789Sahrens */ 1073789Sahrens int 10741635Sbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot) 1075789Sahrens { 1076789Sahrens spa_t *spa; 10771635Sbonwick vdev_t *rvd; 1078789Sahrens dsl_pool_t *dp; 1079789Sahrens dmu_tx_t *tx; 10802082Seschrock int c, error = 0; 1081789Sahrens uint64_t txg = TXG_INITIAL; 10822082Seschrock nvlist_t **spares; 10832082Seschrock uint_t nspares; 1084789Sahrens 1085789Sahrens /* 1086789Sahrens * If this pool already exists, return failure. 1087789Sahrens */ 1088789Sahrens mutex_enter(&spa_namespace_lock); 1089789Sahrens if (spa_lookup(pool) != NULL) { 1090789Sahrens mutex_exit(&spa_namespace_lock); 1091789Sahrens return (EEXIST); 1092789Sahrens } 1093789Sahrens 1094789Sahrens /* 1095789Sahrens * Allocate a new spa_t structure. 1096789Sahrens */ 10971635Sbonwick spa = spa_add(pool, altroot); 1098789Sahrens spa_activate(spa); 1099789Sahrens 1100789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 11011760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 1102789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1103789Sahrens 11041635Sbonwick /* 11051635Sbonwick * Create the root vdev. 11061635Sbonwick */ 11071635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 11081635Sbonwick 11092082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 11102082Seschrock 11112082Seschrock ASSERT(error != 0 || rvd != NULL); 11122082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 11132082Seschrock 11142082Seschrock if (error == 0 && rvd->vdev_children == 0) 11151635Sbonwick error = EINVAL; 11162082Seschrock 11172082Seschrock if (error == 0 && 11182082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 11192082Seschrock (error = spa_validate_spares(spa, nvroot, txg, 11202082Seschrock VDEV_ALLOC_ADD)) == 0) { 11212082Seschrock for (c = 0; c < rvd->vdev_children; c++) 11222082Seschrock vdev_init(rvd->vdev_child[c], txg); 11232082Seschrock vdev_config_dirty(rvd); 11241635Sbonwick } 11251635Sbonwick 11261635Sbonwick spa_config_exit(spa, FTAG); 1127789Sahrens 11282082Seschrock if (error != 0) { 1129789Sahrens spa_unload(spa); 1130789Sahrens spa_deactivate(spa); 1131789Sahrens spa_remove(spa); 1132789Sahrens mutex_exit(&spa_namespace_lock); 1133789Sahrens return (error); 1134789Sahrens } 1135789Sahrens 11362082Seschrock /* 11372082Seschrock * Get the list of spares, if specified. 11382082Seschrock */ 11392082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 11402082Seschrock &spares, &nspares) == 0) { 11412082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 11422082Seschrock KM_SLEEP) == 0); 11432082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 11442082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 11452082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 11462082Seschrock spa_load_spares(spa); 11472082Seschrock spa_config_exit(spa, FTAG); 11482082Seschrock spa->spa_sync_spares = B_TRUE; 11492082Seschrock } 11502082Seschrock 1151789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1152789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1153789Sahrens 1154789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1155789Sahrens 1156789Sahrens /* 1157789Sahrens * Create the pool config object. 1158789Sahrens */ 1159789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1160789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1161789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1162789Sahrens 11631544Seschrock if (zap_add(spa->spa_meta_objset, 1164789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 11651544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 11661544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 11671544Seschrock } 1168789Sahrens 11692082Seschrock /* Newly created pools are always deflated. */ 11702082Seschrock spa->spa_deflate = TRUE; 11712082Seschrock if (zap_add(spa->spa_meta_objset, 11722082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 11732082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 11742082Seschrock cmn_err(CE_PANIC, "failed to add deflate"); 11752082Seschrock } 11762082Seschrock 1177789Sahrens /* 1178789Sahrens * Create the deferred-free bplist object. Turn off compression 1179789Sahrens * because sync-to-convergence takes longer if the blocksize 1180789Sahrens * keeps changing. 1181789Sahrens */ 1182789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1183789Sahrens 1 << 14, tx); 1184789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1185789Sahrens ZIO_COMPRESS_OFF, tx); 1186789Sahrens 11871544Seschrock if (zap_add(spa->spa_meta_objset, 1188789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11891544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 11901544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 11911544Seschrock } 1192789Sahrens 11932926Sek110237 /* 11942926Sek110237 * Create the pool's history object. 11952926Sek110237 */ 11962926Sek110237 spa_history_create_obj(spa, tx); 11972926Sek110237 1198789Sahrens dmu_tx_commit(tx); 1199789Sahrens 1200*3912Slling spa->spa_bootfs = zfs_prop_default_numeric(ZFS_PROP_BOOTFS); 1201789Sahrens spa->spa_sync_on = B_TRUE; 1202789Sahrens txg_sync_start(spa->spa_dsl_pool); 1203789Sahrens 1204789Sahrens /* 1205789Sahrens * We explicitly wait for the first transaction to complete so that our 1206789Sahrens * bean counters are appropriately updated. 1207789Sahrens */ 1208789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1209789Sahrens 1210789Sahrens spa_config_sync(); 1211789Sahrens 1212789Sahrens mutex_exit(&spa_namespace_lock); 1213789Sahrens 1214789Sahrens return (0); 1215789Sahrens } 1216789Sahrens 1217789Sahrens /* 1218789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1219789Sahrens * then call spa_load() to do the dirty work. 1220789Sahrens */ 1221789Sahrens int 12221635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 1223789Sahrens { 1224789Sahrens spa_t *spa; 1225789Sahrens int error; 12262082Seschrock nvlist_t *nvroot; 12272082Seschrock nvlist_t **spares; 12282082Seschrock uint_t nspares; 1229789Sahrens 1230789Sahrens if (!(spa_mode & FWRITE)) 1231789Sahrens return (EROFS); 1232789Sahrens 1233789Sahrens /* 1234789Sahrens * If a pool with this name exists, return failure. 1235789Sahrens */ 1236789Sahrens mutex_enter(&spa_namespace_lock); 1237789Sahrens if (spa_lookup(pool) != NULL) { 1238789Sahrens mutex_exit(&spa_namespace_lock); 1239789Sahrens return (EEXIST); 1240789Sahrens } 1241789Sahrens 1242789Sahrens /* 12431635Sbonwick * Create and initialize the spa structure. 1244789Sahrens */ 12451635Sbonwick spa = spa_add(pool, altroot); 1246789Sahrens spa_activate(spa); 1247789Sahrens 1248789Sahrens /* 12491635Sbonwick * Pass off the heavy lifting to spa_load(). 12501732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 12511732Sbonwick * is actually the one to trust when doing an import. 12521601Sbonwick */ 12531732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1254789Sahrens 12552082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12562082Seschrock /* 12572082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 12582082Seschrock * and conflicts with spa_has_spare(). 12592082Seschrock */ 12602082Seschrock if (spa->spa_sparelist) { 12612082Seschrock nvlist_free(spa->spa_sparelist); 12622082Seschrock spa->spa_sparelist = NULL; 12632082Seschrock spa_load_spares(spa); 12642082Seschrock } 12652082Seschrock 12662082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 12672082Seschrock &nvroot) == 0); 12682082Seschrock if (error == 0) 12692082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 12702082Seschrock VDEV_ALLOC_SPARE); 12712082Seschrock spa_config_exit(spa, FTAG); 12722082Seschrock 12732082Seschrock if (error != 0) { 1274789Sahrens spa_unload(spa); 1275789Sahrens spa_deactivate(spa); 1276789Sahrens spa_remove(spa); 1277789Sahrens mutex_exit(&spa_namespace_lock); 1278789Sahrens return (error); 1279789Sahrens } 1280789Sahrens 12811635Sbonwick /* 12822082Seschrock * Override any spares as specified by the user, as these may have 12832082Seschrock * correct device names/devids, etc. 12842082Seschrock */ 12852082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 12862082Seschrock &spares, &nspares) == 0) { 12872082Seschrock if (spa->spa_sparelist) 12882082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 12892082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 12902082Seschrock else 12912082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 12922082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 12932082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 12942082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 12952082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12962082Seschrock spa_load_spares(spa); 12972082Seschrock spa_config_exit(spa, FTAG); 12982082Seschrock spa->spa_sync_spares = B_TRUE; 12992082Seschrock } 13002082Seschrock 13012082Seschrock /* 13021635Sbonwick * Update the config cache to include the newly-imported pool. 13031635Sbonwick */ 13041635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 13051635Sbonwick 1306789Sahrens mutex_exit(&spa_namespace_lock); 1307789Sahrens 1308789Sahrens /* 1309789Sahrens * Resilver anything that's out of date. 1310789Sahrens */ 1311789Sahrens if (spa_mode & FWRITE) 1312789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1313789Sahrens 1314789Sahrens return (0); 1315789Sahrens } 1316789Sahrens 1317789Sahrens /* 1318789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1319789Sahrens * to get the vdev stats associated with the imported devices. 1320789Sahrens */ 1321789Sahrens #define TRYIMPORT_NAME "$import" 1322789Sahrens 1323789Sahrens nvlist_t * 1324789Sahrens spa_tryimport(nvlist_t *tryconfig) 1325789Sahrens { 1326789Sahrens nvlist_t *config = NULL; 1327789Sahrens char *poolname; 1328789Sahrens spa_t *spa; 1329789Sahrens uint64_t state; 1330789Sahrens 1331789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1332789Sahrens return (NULL); 1333789Sahrens 1334789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1335789Sahrens return (NULL); 1336789Sahrens 13371635Sbonwick /* 13381635Sbonwick * Create and initialize the spa structure. 13391635Sbonwick */ 1340789Sahrens mutex_enter(&spa_namespace_lock); 13411635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1342789Sahrens spa_activate(spa); 1343789Sahrens 1344789Sahrens /* 13451635Sbonwick * Pass off the heavy lifting to spa_load(). 13461732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 13471732Sbonwick * is actually the one to trust when doing an import. 1348789Sahrens */ 13491732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1350789Sahrens 1351789Sahrens /* 1352789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1353789Sahrens */ 1354789Sahrens if (spa->spa_root_vdev != NULL) { 13551635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1356789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 13571635Sbonwick spa_config_exit(spa, FTAG); 1358789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1359789Sahrens poolname) == 0); 1360789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1361789Sahrens state) == 0); 13622082Seschrock 13632082Seschrock /* 13642082Seschrock * Add the list of hot spares. 13652082Seschrock */ 13662082Seschrock spa_add_spares(spa, config); 1367789Sahrens } 1368789Sahrens 1369789Sahrens spa_unload(spa); 1370789Sahrens spa_deactivate(spa); 1371789Sahrens spa_remove(spa); 1372789Sahrens mutex_exit(&spa_namespace_lock); 1373789Sahrens 1374789Sahrens return (config); 1375789Sahrens } 1376789Sahrens 1377789Sahrens /* 1378789Sahrens * Pool export/destroy 1379789Sahrens * 1380789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1381789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1382789Sahrens * update the pool state and sync all the labels to disk, removing the 1383789Sahrens * configuration from the cache afterwards. 1384789Sahrens */ 1385789Sahrens static int 13861775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1387789Sahrens { 1388789Sahrens spa_t *spa; 1389789Sahrens 13901775Sbillm if (oldconfig) 13911775Sbillm *oldconfig = NULL; 13921775Sbillm 1393789Sahrens if (!(spa_mode & FWRITE)) 1394789Sahrens return (EROFS); 1395789Sahrens 1396789Sahrens mutex_enter(&spa_namespace_lock); 1397789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1398789Sahrens mutex_exit(&spa_namespace_lock); 1399789Sahrens return (ENOENT); 1400789Sahrens } 1401789Sahrens 1402789Sahrens /* 14031544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 14041544Seschrock * reacquire the namespace lock, and see if we can export. 14051544Seschrock */ 14061544Seschrock spa_open_ref(spa, FTAG); 14071544Seschrock mutex_exit(&spa_namespace_lock); 14081544Seschrock spa_async_suspend(spa); 14091544Seschrock mutex_enter(&spa_namespace_lock); 14101544Seschrock spa_close(spa, FTAG); 14111544Seschrock 14121544Seschrock /* 1413789Sahrens * The pool will be in core if it's openable, 1414789Sahrens * in which case we can modify its state. 1415789Sahrens */ 1416789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1417789Sahrens /* 1418789Sahrens * Objsets may be open only because they're dirty, so we 1419789Sahrens * have to force it to sync before checking spa_refcnt. 1420789Sahrens */ 1421789Sahrens spa_scrub_suspend(spa); 1422789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1423789Sahrens 14241544Seschrock /* 14251544Seschrock * A pool cannot be exported or destroyed if there are active 14261544Seschrock * references. If we are resetting a pool, allow references by 14271544Seschrock * fault injection handlers. 14281544Seschrock */ 14291544Seschrock if (!spa_refcount_zero(spa) || 14301544Seschrock (spa->spa_inject_ref != 0 && 14311544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1432789Sahrens spa_scrub_resume(spa); 14331544Seschrock spa_async_resume(spa); 1434789Sahrens mutex_exit(&spa_namespace_lock); 1435789Sahrens return (EBUSY); 1436789Sahrens } 1437789Sahrens 1438789Sahrens spa_scrub_resume(spa); 1439789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1440789Sahrens 1441789Sahrens /* 1442789Sahrens * We want this to be reflected on every label, 1443789Sahrens * so mark them all dirty. spa_unload() will do the 1444789Sahrens * final sync that pushes these changes out. 1445789Sahrens */ 14461544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 14471601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 14481544Seschrock spa->spa_state = new_state; 14491635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 14501544Seschrock vdev_config_dirty(spa->spa_root_vdev); 14511601Sbonwick spa_config_exit(spa, FTAG); 14521544Seschrock } 1453789Sahrens } 1454789Sahrens 1455789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1456789Sahrens spa_unload(spa); 1457789Sahrens spa_deactivate(spa); 1458789Sahrens } 1459789Sahrens 14601775Sbillm if (oldconfig && spa->spa_config) 14611775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 14621775Sbillm 14631544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 14641544Seschrock spa_remove(spa); 14651544Seschrock spa_config_sync(); 14661544Seschrock } 1467789Sahrens mutex_exit(&spa_namespace_lock); 1468789Sahrens 1469789Sahrens return (0); 1470789Sahrens } 1471789Sahrens 1472789Sahrens /* 1473789Sahrens * Destroy a storage pool. 1474789Sahrens */ 1475789Sahrens int 1476789Sahrens spa_destroy(char *pool) 1477789Sahrens { 14781775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1479789Sahrens } 1480789Sahrens 1481789Sahrens /* 1482789Sahrens * Export a storage pool. 1483789Sahrens */ 1484789Sahrens int 14851775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1486789Sahrens { 14871775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1488789Sahrens } 1489789Sahrens 1490789Sahrens /* 14911544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 14921544Seschrock * from the namespace in any way. 14931544Seschrock */ 14941544Seschrock int 14951544Seschrock spa_reset(char *pool) 14961544Seschrock { 14971775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 14981544Seschrock } 14991544Seschrock 15001544Seschrock 15011544Seschrock /* 1502789Sahrens * ========================================================================== 1503789Sahrens * Device manipulation 1504789Sahrens * ========================================================================== 1505789Sahrens */ 1506789Sahrens 1507789Sahrens /* 1508789Sahrens * Add capacity to a storage pool. 1509789Sahrens */ 1510789Sahrens int 1511789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1512789Sahrens { 1513789Sahrens uint64_t txg; 15141635Sbonwick int c, error; 1515789Sahrens vdev_t *rvd = spa->spa_root_vdev; 15161585Sbonwick vdev_t *vd, *tvd; 15172082Seschrock nvlist_t **spares; 15182082Seschrock uint_t i, nspares; 1519789Sahrens 1520789Sahrens txg = spa_vdev_enter(spa); 1521789Sahrens 15222082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 15232082Seschrock VDEV_ALLOC_ADD)) != 0) 15242082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 15252082Seschrock 15263377Seschrock spa->spa_pending_vdev = vd; 1527789Sahrens 15282082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 15292082Seschrock &spares, &nspares) != 0) 15302082Seschrock nspares = 0; 15312082Seschrock 15323377Seschrock if (vd->vdev_children == 0 && nspares == 0) { 15333377Seschrock spa->spa_pending_vdev = NULL; 15342082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 15353377Seschrock } 15362082Seschrock 15372082Seschrock if (vd->vdev_children != 0) { 15383377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 15393377Seschrock spa->spa_pending_vdev = NULL; 15402082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 15412082Seschrock } 15422082Seschrock } 15432082Seschrock 15443377Seschrock /* 15453377Seschrock * We must validate the spares after checking the children. Otherwise, 15463377Seschrock * vdev_inuse() will blindly overwrite the spare. 15473377Seschrock */ 15483377Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 15493377Seschrock VDEV_ALLOC_ADD)) != 0) { 15503377Seschrock spa->spa_pending_vdev = NULL; 15513377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 15523377Seschrock } 15533377Seschrock 15543377Seschrock spa->spa_pending_vdev = NULL; 15553377Seschrock 15563377Seschrock /* 15573377Seschrock * Transfer each new top-level vdev from vd to rvd. 15583377Seschrock */ 15593377Seschrock for (c = 0; c < vd->vdev_children; c++) { 15603377Seschrock tvd = vd->vdev_child[c]; 15613377Seschrock vdev_remove_child(vd, tvd); 15623377Seschrock tvd->vdev_id = rvd->vdev_children; 15633377Seschrock vdev_add_child(rvd, tvd); 15643377Seschrock vdev_config_dirty(tvd); 15653377Seschrock } 15663377Seschrock 15672082Seschrock if (nspares != 0) { 15682082Seschrock if (spa->spa_sparelist != NULL) { 15692082Seschrock nvlist_t **oldspares; 15702082Seschrock uint_t oldnspares; 15712082Seschrock nvlist_t **newspares; 15722082Seschrock 15732082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 15742082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 15752082Seschrock 15762082Seschrock newspares = kmem_alloc(sizeof (void *) * 15772082Seschrock (nspares + oldnspares), KM_SLEEP); 15782082Seschrock for (i = 0; i < oldnspares; i++) 15792082Seschrock VERIFY(nvlist_dup(oldspares[i], 15802082Seschrock &newspares[i], KM_SLEEP) == 0); 15812082Seschrock for (i = 0; i < nspares; i++) 15822082Seschrock VERIFY(nvlist_dup(spares[i], 15832082Seschrock &newspares[i + oldnspares], 15842082Seschrock KM_SLEEP) == 0); 15852082Seschrock 15862082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 15872082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 15882082Seschrock 15892082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 15902082Seschrock ZPOOL_CONFIG_SPARES, newspares, 15912082Seschrock nspares + oldnspares) == 0); 15922082Seschrock for (i = 0; i < oldnspares + nspares; i++) 15932082Seschrock nvlist_free(newspares[i]); 15942082Seschrock kmem_free(newspares, (oldnspares + nspares) * 15952082Seschrock sizeof (void *)); 15962082Seschrock } else { 15972082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 15982082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 15992082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 16002082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16012082Seschrock } 16022082Seschrock 16032082Seschrock spa_load_spares(spa); 16042082Seschrock spa->spa_sync_spares = B_TRUE; 1605789Sahrens } 1606789Sahrens 1607789Sahrens /* 16081585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 16091585Sbonwick * If other threads start allocating from these vdevs before we 16101585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 16111585Sbonwick * fail to open the pool because there are DVAs that the config cache 16121585Sbonwick * can't translate. Therefore, we first add the vdevs without 16131585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 16141635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 16151585Sbonwick * 16161585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 16171585Sbonwick * if we lose power at any point in this sequence, the remaining 16181585Sbonwick * steps will be completed the next time we load the pool. 1619789Sahrens */ 16201635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 16211585Sbonwick 16221635Sbonwick mutex_enter(&spa_namespace_lock); 16231635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 16241635Sbonwick mutex_exit(&spa_namespace_lock); 1625789Sahrens 16261635Sbonwick return (0); 1627789Sahrens } 1628789Sahrens 1629789Sahrens /* 1630789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1631789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1632789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1633789Sahrens * 1634789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1635789Sahrens * existing device; in this case the two devices are made into their own 1636789Sahrens * mirror using the 'replacing' vdev, which is functionally idendical to 1637789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1638789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1639789Sahrens * completion of resilvering, the first disk (the one being replaced) 1640789Sahrens * is automatically detached. 1641789Sahrens */ 1642789Sahrens int 16431544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1644789Sahrens { 1645789Sahrens uint64_t txg, open_txg; 1646789Sahrens int error; 1647789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1648789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 16492082Seschrock vdev_ops_t *pvops; 1650789Sahrens 1651789Sahrens txg = spa_vdev_enter(spa); 1652789Sahrens 16531544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1654789Sahrens 1655789Sahrens if (oldvd == NULL) 1656789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1657789Sahrens 16581585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 16591585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 16601585Sbonwick 1661789Sahrens pvd = oldvd->vdev_parent; 1662789Sahrens 16632082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 16642082Seschrock VDEV_ALLOC_ADD)) != 0 || newrootvd->vdev_children != 1) 1665789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1666789Sahrens 1667789Sahrens newvd = newrootvd->vdev_child[0]; 1668789Sahrens 1669789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1670789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1671789Sahrens 16722082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1673789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1674789Sahrens 16752082Seschrock if (!replacing) { 16762082Seschrock /* 16772082Seschrock * For attach, the only allowable parent is a mirror or the root 16782082Seschrock * vdev. 16792082Seschrock */ 16802082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 16812082Seschrock pvd->vdev_ops != &vdev_root_ops) 16822082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 16832082Seschrock 16842082Seschrock pvops = &vdev_mirror_ops; 16852082Seschrock } else { 16862082Seschrock /* 16872082Seschrock * Active hot spares can only be replaced by inactive hot 16882082Seschrock * spares. 16892082Seschrock */ 16902082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 16912082Seschrock pvd->vdev_child[1] == oldvd && 16922082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 16932082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 16942082Seschrock 16952082Seschrock /* 16962082Seschrock * If the source is a hot spare, and the parent isn't already a 16972082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 16983377Seschrock * want to create a replacing vdev. The user is not allowed to 16993377Seschrock * attach to a spared vdev child unless the 'isspare' state is 17003377Seschrock * the same (spare replaces spare, non-spare replaces 17013377Seschrock * non-spare). 17022082Seschrock */ 17032082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 17042082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17053377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 17063377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 17073377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 17082082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 17092082Seschrock newvd->vdev_isspare) 17102082Seschrock pvops = &vdev_spare_ops; 17112082Seschrock else 17122082Seschrock pvops = &vdev_replacing_ops; 17132082Seschrock } 17142082Seschrock 17151175Slling /* 17161175Slling * Compare the new device size with the replaceable/attachable 17171175Slling * device size. 17181175Slling */ 17191175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1720789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1721789Sahrens 17221732Sbonwick /* 17231732Sbonwick * The new device cannot have a higher alignment requirement 17241732Sbonwick * than the top-level vdev. 17251732Sbonwick */ 17261732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1727789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1728789Sahrens 1729789Sahrens /* 1730789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1731789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1732789Sahrens */ 1733789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1734789Sahrens spa_strfree(oldvd->vdev_path); 1735789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1736789Sahrens KM_SLEEP); 1737789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1738789Sahrens newvd->vdev_path, "old"); 1739789Sahrens if (oldvd->vdev_devid != NULL) { 1740789Sahrens spa_strfree(oldvd->vdev_devid); 1741789Sahrens oldvd->vdev_devid = NULL; 1742789Sahrens } 1743789Sahrens } 1744789Sahrens 1745789Sahrens /* 17462082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 17472082Seschrock * mirror/replacing/spare vdev above oldvd. 1748789Sahrens */ 1749789Sahrens if (pvd->vdev_ops != pvops) 1750789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1751789Sahrens 1752789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1753789Sahrens ASSERT(pvd->vdev_ops == pvops); 1754789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1755789Sahrens 1756789Sahrens /* 1757789Sahrens * Extract the new device from its root and add it to pvd. 1758789Sahrens */ 1759789Sahrens vdev_remove_child(newrootvd, newvd); 1760789Sahrens newvd->vdev_id = pvd->vdev_children; 1761789Sahrens vdev_add_child(pvd, newvd); 1762789Sahrens 17631544Seschrock /* 17641544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 17651544Seschrock * the addition of newvd may have decreased our parent's asize. 17661544Seschrock */ 17671544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 17681544Seschrock 1769789Sahrens tvd = newvd->vdev_top; 1770789Sahrens ASSERT(pvd->vdev_top == tvd); 1771789Sahrens ASSERT(tvd->vdev_parent == rvd); 1772789Sahrens 1773789Sahrens vdev_config_dirty(tvd); 1774789Sahrens 1775789Sahrens /* 1776789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1777789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1778789Sahrens */ 1779789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1780789Sahrens 1781789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1782789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1783789Sahrens open_txg - TXG_INITIAL + 1); 1784789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1785789Sahrens 17863377Seschrock if (newvd->vdev_isspare) 17873377Seschrock spa_spare_activate(newvd); 17881544Seschrock 1789789Sahrens /* 1790789Sahrens * Mark newvd's DTL dirty in this txg. 1791789Sahrens */ 17921732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1793789Sahrens 1794789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1795789Sahrens 1796789Sahrens /* 1797789Sahrens * Kick off a resilver to update newvd. 1798789Sahrens */ 1799789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1800789Sahrens 1801789Sahrens return (0); 1802789Sahrens } 1803789Sahrens 1804789Sahrens /* 1805789Sahrens * Detach a device from a mirror or replacing vdev. 1806789Sahrens * If 'replace_done' is specified, only detach if the parent 1807789Sahrens * is a replacing vdev. 1808789Sahrens */ 1809789Sahrens int 18101544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1811789Sahrens { 1812789Sahrens uint64_t txg; 1813789Sahrens int c, t, error; 1814789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1815789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 18162082Seschrock boolean_t unspare = B_FALSE; 18172082Seschrock uint64_t unspare_guid; 1818789Sahrens 1819789Sahrens txg = spa_vdev_enter(spa); 1820789Sahrens 18211544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1822789Sahrens 1823789Sahrens if (vd == NULL) 1824789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1825789Sahrens 18261585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 18271585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 18281585Sbonwick 1829789Sahrens pvd = vd->vdev_parent; 1830789Sahrens 1831789Sahrens /* 1832789Sahrens * If replace_done is specified, only remove this device if it's 18332082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 18342082Seschrock * disk can be removed. 1835789Sahrens */ 18362082Seschrock if (replace_done) { 18372082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 18382082Seschrock if (vd->vdev_id != 0) 18392082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 18402082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 18412082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 18422082Seschrock } 18432082Seschrock } 18442082Seschrock 18452082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 18462082Seschrock spa_version(spa) >= ZFS_VERSION_SPARES); 1847789Sahrens 1848789Sahrens /* 18492082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1850789Sahrens */ 1851789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 18522082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 18532082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1854789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1855789Sahrens 1856789Sahrens /* 1857789Sahrens * If there's only one replica, you can't detach it. 1858789Sahrens */ 1859789Sahrens if (pvd->vdev_children <= 1) 1860789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1861789Sahrens 1862789Sahrens /* 1863789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1864789Sahrens * valid copy of the data, which means we cannot safely detach it. 1865789Sahrens * 1866789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1867789Sahrens * precise DTL check. 1868789Sahrens */ 1869789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1870789Sahrens uint64_t dirty; 1871789Sahrens 1872789Sahrens cvd = pvd->vdev_child[c]; 1873789Sahrens if (cvd == vd) 1874789Sahrens continue; 1875789Sahrens if (vdev_is_dead(cvd)) 1876789Sahrens continue; 1877789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1878789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1879789Sahrens cvd->vdev_dtl_scrub.sm_space; 1880789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1881789Sahrens if (!dirty) 1882789Sahrens break; 1883789Sahrens } 18842082Seschrock 18852082Seschrock /* 18862082Seschrock * If we are a replacing or spare vdev, then we can always detach the 18872082Seschrock * latter child, as that is how one cancels the operation. 18882082Seschrock */ 18892082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 18902082Seschrock c == pvd->vdev_children) 1891789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1892789Sahrens 1893789Sahrens /* 18942082Seschrock * If we are detaching the original disk from a spare, then it implies 18952082Seschrock * that the spare should become a real disk, and be removed from the 18962082Seschrock * active spare list for the pool. 18972082Seschrock */ 18982082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 18992082Seschrock vd->vdev_id == 0) 19002082Seschrock unspare = B_TRUE; 19012082Seschrock 19022082Seschrock /* 1903789Sahrens * Erase the disk labels so the disk can be used for other things. 1904789Sahrens * This must be done after all other error cases are handled, 1905789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1906789Sahrens * But if we can't do it, don't treat the error as fatal -- 1907789Sahrens * it may be that the unwritability of the disk is the reason 1908789Sahrens * it's being detached! 1909789Sahrens */ 19103377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1911789Sahrens 1912789Sahrens /* 1913789Sahrens * Remove vd from its parent and compact the parent's children. 1914789Sahrens */ 1915789Sahrens vdev_remove_child(pvd, vd); 1916789Sahrens vdev_compact_children(pvd); 1917789Sahrens 1918789Sahrens /* 1919789Sahrens * Remember one of the remaining children so we can get tvd below. 1920789Sahrens */ 1921789Sahrens cvd = pvd->vdev_child[0]; 1922789Sahrens 1923789Sahrens /* 19242082Seschrock * If we need to remove the remaining child from the list of hot spares, 19252082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 19262082Seschrock * must do this before vdev_remove_parent(), because that can change the 19272082Seschrock * GUID if it creates a new toplevel GUID. 19282082Seschrock */ 19292082Seschrock if (unspare) { 19302082Seschrock ASSERT(cvd->vdev_isspare); 19313377Seschrock spa_spare_remove(cvd); 19322082Seschrock unspare_guid = cvd->vdev_guid; 19332082Seschrock } 19342082Seschrock 19352082Seschrock /* 1936789Sahrens * If the parent mirror/replacing vdev only has one child, 1937789Sahrens * the parent is no longer needed. Remove it from the tree. 1938789Sahrens */ 1939789Sahrens if (pvd->vdev_children == 1) 1940789Sahrens vdev_remove_parent(cvd); 1941789Sahrens 1942789Sahrens /* 1943789Sahrens * We don't set tvd until now because the parent we just removed 1944789Sahrens * may have been the previous top-level vdev. 1945789Sahrens */ 1946789Sahrens tvd = cvd->vdev_top; 1947789Sahrens ASSERT(tvd->vdev_parent == rvd); 1948789Sahrens 1949789Sahrens /* 19503377Seschrock * Reevaluate the parent vdev state. 1951789Sahrens */ 19523377Seschrock vdev_propagate_state(cvd->vdev_parent); 1953789Sahrens 1954789Sahrens /* 19553377Seschrock * If the device we just detached was smaller than the others, it may be 19563377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 19573377Seschrock * can't fail because the existing metaslabs are already in core, so 19583377Seschrock * there's nothing to read from disk. 1959789Sahrens */ 19601732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 1961789Sahrens 1962789Sahrens vdev_config_dirty(tvd); 1963789Sahrens 1964789Sahrens /* 19653377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 19663377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 19673377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 19683377Seschrock * prevent vd from being accessed after it's freed. 1969789Sahrens */ 1970789Sahrens for (t = 0; t < TXG_SIZE; t++) 1971789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 19721732Sbonwick vd->vdev_detached = B_TRUE; 19731732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 1974789Sahrens 19752082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 19762082Seschrock 19772082Seschrock /* 19783377Seschrock * If this was the removal of the original device in a hot spare vdev, 19793377Seschrock * then we want to go through and remove the device from the hot spare 19803377Seschrock * list of every other pool. 19812082Seschrock */ 19822082Seschrock if (unspare) { 19832082Seschrock spa = NULL; 19842082Seschrock mutex_enter(&spa_namespace_lock); 19852082Seschrock while ((spa = spa_next(spa)) != NULL) { 19862082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 19872082Seschrock continue; 19882082Seschrock 19892082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 19902082Seschrock } 19912082Seschrock mutex_exit(&spa_namespace_lock); 19922082Seschrock } 19932082Seschrock 19942082Seschrock return (error); 19952082Seschrock } 19962082Seschrock 19972082Seschrock /* 19982082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 19992082Seschrock * spares. 20002082Seschrock */ 20012082Seschrock int 20022082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 20032082Seschrock { 20042082Seschrock vdev_t *vd; 20052082Seschrock nvlist_t **spares, *nv, **newspares; 20062082Seschrock uint_t i, j, nspares; 20072082Seschrock int ret = 0; 20082082Seschrock 20092082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20102082Seschrock 20112082Seschrock vd = spa_lookup_by_guid(spa, guid); 20122082Seschrock 20132082Seschrock nv = NULL; 20142082Seschrock if (spa->spa_spares != NULL && 20152082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 20162082Seschrock &spares, &nspares) == 0) { 20172082Seschrock for (i = 0; i < nspares; i++) { 20182082Seschrock uint64_t theguid; 20192082Seschrock 20202082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 20212082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 20222082Seschrock if (theguid == guid) { 20232082Seschrock nv = spares[i]; 20242082Seschrock break; 20252082Seschrock } 20262082Seschrock } 20272082Seschrock } 20282082Seschrock 20292082Seschrock /* 20302082Seschrock * We only support removing a hot spare, and only if it's not currently 20312082Seschrock * in use in this pool. 20322082Seschrock */ 20332082Seschrock if (nv == NULL && vd == NULL) { 20342082Seschrock ret = ENOENT; 20352082Seschrock goto out; 20362082Seschrock } 20372082Seschrock 20382082Seschrock if (nv == NULL && vd != NULL) { 20392082Seschrock ret = ENOTSUP; 20402082Seschrock goto out; 20412082Seschrock } 20422082Seschrock 20432082Seschrock if (!unspare && nv != NULL && vd != NULL) { 20442082Seschrock ret = EBUSY; 20452082Seschrock goto out; 20462082Seschrock } 20472082Seschrock 20482082Seschrock if (nspares == 1) { 20492082Seschrock newspares = NULL; 20502082Seschrock } else { 20512082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 20522082Seschrock KM_SLEEP); 20532082Seschrock for (i = 0, j = 0; i < nspares; i++) { 20542082Seschrock if (spares[i] != nv) 20552082Seschrock VERIFY(nvlist_dup(spares[i], 20562082Seschrock &newspares[j++], KM_SLEEP) == 0); 20572082Seschrock } 20582082Seschrock } 20592082Seschrock 20602082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 20612082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 20622082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 20632082Seschrock newspares, nspares - 1) == 0); 20642082Seschrock for (i = 0; i < nspares - 1; i++) 20652082Seschrock nvlist_free(newspares[i]); 20662082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 20672082Seschrock spa_load_spares(spa); 20682082Seschrock spa->spa_sync_spares = B_TRUE; 20692082Seschrock 20702082Seschrock out: 20712082Seschrock spa_config_exit(spa, FTAG); 20722082Seschrock 20732082Seschrock return (ret); 2074789Sahrens } 2075789Sahrens 2076789Sahrens /* 20771544Seschrock * Find any device that's done replacing, so we can detach it. 2078789Sahrens */ 20791544Seschrock static vdev_t * 20801544Seschrock spa_vdev_replace_done_hunt(vdev_t *vd) 2081789Sahrens { 20821544Seschrock vdev_t *newvd, *oldvd; 2083789Sahrens int c; 2084789Sahrens 20851544Seschrock for (c = 0; c < vd->vdev_children; c++) { 20861544Seschrock oldvd = spa_vdev_replace_done_hunt(vd->vdev_child[c]); 20871544Seschrock if (oldvd != NULL) 20881544Seschrock return (oldvd); 20891544Seschrock } 2090789Sahrens 2091789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 20921544Seschrock oldvd = vd->vdev_child[0]; 20931544Seschrock newvd = vd->vdev_child[1]; 2094789Sahrens 20951544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 20961544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 20971544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 20981544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 20991544Seschrock return (oldvd); 21001544Seschrock } 21011544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 21021544Seschrock } 2103789Sahrens 21041544Seschrock return (NULL); 2105789Sahrens } 2106789Sahrens 21071544Seschrock static void 2108789Sahrens spa_vdev_replace_done(spa_t *spa) 2109789Sahrens { 21101544Seschrock vdev_t *vd; 21112082Seschrock vdev_t *pvd; 21121544Seschrock uint64_t guid; 21132082Seschrock uint64_t pguid = 0; 2114789Sahrens 21151544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2116789Sahrens 21171544Seschrock while ((vd = spa_vdev_replace_done_hunt(spa->spa_root_vdev)) != NULL) { 21181544Seschrock guid = vd->vdev_guid; 21192082Seschrock /* 21202082Seschrock * If we have just finished replacing a hot spared device, then 21212082Seschrock * we need to detach the parent's first child (the original hot 21222082Seschrock * spare) as well. 21232082Seschrock */ 21242082Seschrock pvd = vd->vdev_parent; 21252082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 21262082Seschrock pvd->vdev_id == 0) { 21272082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 21282082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 21292082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 21302082Seschrock } 21311544Seschrock spa_config_exit(spa, FTAG); 21321544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 21331544Seschrock return; 21342082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 21352082Seschrock return; 21361544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2137789Sahrens } 2138789Sahrens 21391544Seschrock spa_config_exit(spa, FTAG); 2140789Sahrens } 2141789Sahrens 2142789Sahrens /* 21431354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 21441354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 21451354Seschrock */ 21461354Seschrock int 21471354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 21481354Seschrock { 21491354Seschrock vdev_t *rvd, *vd; 21501354Seschrock uint64_t txg; 21511354Seschrock 21521354Seschrock rvd = spa->spa_root_vdev; 21531354Seschrock 21541354Seschrock txg = spa_vdev_enter(spa); 21551354Seschrock 21562082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 21572082Seschrock /* 21582082Seschrock * Determine if this is a reference to a hot spare. In that 21592082Seschrock * case, update the path as stored in the spare list. 21602082Seschrock */ 21612082Seschrock nvlist_t **spares; 21622082Seschrock uint_t i, nspares; 21632082Seschrock if (spa->spa_sparelist != NULL) { 21642082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 21652082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 21662082Seschrock for (i = 0; i < nspares; i++) { 21672082Seschrock uint64_t theguid; 21682082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 21692082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 21702082Seschrock if (theguid == guid) 21712082Seschrock break; 21722082Seschrock } 21732082Seschrock 21742082Seschrock if (i == nspares) 21752082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 21762082Seschrock 21772082Seschrock VERIFY(nvlist_add_string(spares[i], 21782082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 21792082Seschrock spa_load_spares(spa); 21802082Seschrock spa->spa_sync_spares = B_TRUE; 21812082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 21822082Seschrock } else { 21832082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 21842082Seschrock } 21852082Seschrock } 21861354Seschrock 21871585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 21881585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 21891585Sbonwick 21901354Seschrock spa_strfree(vd->vdev_path); 21911354Seschrock vd->vdev_path = spa_strdup(newpath); 21921354Seschrock 21931354Seschrock vdev_config_dirty(vd->vdev_top); 21941354Seschrock 21951354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 21961354Seschrock } 21971354Seschrock 21981354Seschrock /* 2199789Sahrens * ========================================================================== 2200789Sahrens * SPA Scrubbing 2201789Sahrens * ========================================================================== 2202789Sahrens */ 2203789Sahrens 2204789Sahrens static void 2205789Sahrens spa_scrub_io_done(zio_t *zio) 2206789Sahrens { 2207789Sahrens spa_t *spa = zio->io_spa; 2208789Sahrens 22093290Sjohansen zio_data_buf_free(zio->io_data, zio->io_size); 2210789Sahrens 2211789Sahrens mutex_enter(&spa->spa_scrub_lock); 22121544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 22131775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2214789Sahrens spa->spa_scrub_errors++; 2215789Sahrens mutex_enter(&vd->vdev_stat_lock); 2216789Sahrens vd->vdev_stat.vs_scrub_errors++; 2217789Sahrens mutex_exit(&vd->vdev_stat_lock); 2218789Sahrens } 22193697Smishra 22203697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 22211544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 22223697Smishra 22233697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 22243697Smishra 22251544Seschrock mutex_exit(&spa->spa_scrub_lock); 2226789Sahrens } 2227789Sahrens 2228789Sahrens static void 22291544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 22301544Seschrock zbookmark_t *zb) 2231789Sahrens { 2232789Sahrens size_t size = BP_GET_LSIZE(bp); 22333697Smishra void *data; 2234789Sahrens 2235789Sahrens mutex_enter(&spa->spa_scrub_lock); 22363697Smishra /* 22373697Smishra * Do not give too much work to vdev(s). 22383697Smishra */ 22393697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 22403697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 22413697Smishra } 2242789Sahrens spa->spa_scrub_inflight++; 2243789Sahrens mutex_exit(&spa->spa_scrub_lock); 2244789Sahrens 22453697Smishra data = zio_data_buf_alloc(size); 22463697Smishra 22471544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 22481544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 22491544Seschrock 22501807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 22511544Seschrock 2252789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 22531544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2254789Sahrens } 2255789Sahrens 2256789Sahrens /* ARGSUSED */ 2257789Sahrens static int 2258789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2259789Sahrens { 2260789Sahrens blkptr_t *bp = &bc->bc_blkptr; 22611775Sbillm vdev_t *vd = spa->spa_root_vdev; 22621775Sbillm dva_t *dva = bp->blk_dva; 22631775Sbillm int needs_resilver = B_FALSE; 22641775Sbillm int d; 2265789Sahrens 22661775Sbillm if (bc->bc_errno) { 2267789Sahrens /* 2268789Sahrens * We can't scrub this block, but we can continue to scrub 2269789Sahrens * the rest of the pool. Note the error and move along. 2270789Sahrens */ 2271789Sahrens mutex_enter(&spa->spa_scrub_lock); 2272789Sahrens spa->spa_scrub_errors++; 2273789Sahrens mutex_exit(&spa->spa_scrub_lock); 2274789Sahrens 22751775Sbillm mutex_enter(&vd->vdev_stat_lock); 22761775Sbillm vd->vdev_stat.vs_scrub_errors++; 22771775Sbillm mutex_exit(&vd->vdev_stat_lock); 2278789Sahrens 2279789Sahrens return (ERESTART); 2280789Sahrens } 2281789Sahrens 2282789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2283789Sahrens 22841775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 22851775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 22861775Sbillm 22871775Sbillm ASSERT(vd != NULL); 22881775Sbillm 22891775Sbillm /* 22901775Sbillm * Keep track of how much data we've examined so that 22911775Sbillm * zpool(1M) status can make useful progress reports. 22921775Sbillm */ 22931775Sbillm mutex_enter(&vd->vdev_stat_lock); 22941775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 22951775Sbillm mutex_exit(&vd->vdev_stat_lock); 2296789Sahrens 22971775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 22981775Sbillm if (DVA_GET_GANG(&dva[d])) { 22991775Sbillm /* 23001775Sbillm * Gang members may be spread across multiple 23011775Sbillm * vdevs, so the best we can do is look at the 23021775Sbillm * pool-wide DTL. 23031775Sbillm * XXX -- it would be better to change our 23041775Sbillm * allocation policy to ensure that this can't 23051775Sbillm * happen. 23061775Sbillm */ 23071775Sbillm vd = spa->spa_root_vdev; 23081775Sbillm } 23091775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 23101775Sbillm bp->blk_birth, 1)) 23111775Sbillm needs_resilver = B_TRUE; 2312789Sahrens } 23131775Sbillm } 23141775Sbillm 23151775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2316789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 23171544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 23181775Sbillm else if (needs_resilver) 23191775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 23201775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2321789Sahrens 2322789Sahrens return (0); 2323789Sahrens } 2324789Sahrens 2325789Sahrens static void 2326789Sahrens spa_scrub_thread(spa_t *spa) 2327789Sahrens { 2328789Sahrens callb_cpr_t cprinfo; 2329789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2330789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2331789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2332789Sahrens int error = 0; 2333789Sahrens boolean_t complete; 2334789Sahrens 2335789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2336789Sahrens 2337797Sbonwick /* 2338797Sbonwick * If we're restarting due to a snapshot create/delete, 2339797Sbonwick * wait for that to complete. 2340797Sbonwick */ 2341797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2342797Sbonwick 23431544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 23441544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 23451544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 23461544Seschrock 23471544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 23481544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2349789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2350789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 23511544Seschrock spa_config_exit(spa, FTAG); 2352789Sahrens 2353789Sahrens mutex_enter(&spa->spa_scrub_lock); 2354789Sahrens spa->spa_scrub_errors = 0; 2355789Sahrens spa->spa_scrub_active = 1; 23561544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2357789Sahrens 2358789Sahrens while (!spa->spa_scrub_stop) { 2359789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 23601544Seschrock while (spa->spa_scrub_suspended) { 2361789Sahrens spa->spa_scrub_active = 0; 2362789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2363789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2364789Sahrens spa->spa_scrub_active = 1; 2365789Sahrens } 2366789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2367789Sahrens 2368789Sahrens if (spa->spa_scrub_restart_txg != 0) 2369789Sahrens break; 2370789Sahrens 2371789Sahrens mutex_exit(&spa->spa_scrub_lock); 2372789Sahrens error = traverse_more(th); 2373789Sahrens mutex_enter(&spa->spa_scrub_lock); 2374789Sahrens if (error != EAGAIN) 2375789Sahrens break; 2376789Sahrens } 2377789Sahrens 2378789Sahrens while (spa->spa_scrub_inflight) 2379789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2380789Sahrens 23811601Sbonwick spa->spa_scrub_active = 0; 23821601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 23831601Sbonwick 23841601Sbonwick mutex_exit(&spa->spa_scrub_lock); 23851601Sbonwick 23861601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 23871601Sbonwick 23881601Sbonwick mutex_enter(&spa->spa_scrub_lock); 23891601Sbonwick 23901601Sbonwick /* 23911601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 23921601Sbonwick * AND the spa config lock to synchronize with any config changes 23931601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 23941601Sbonwick */ 2395789Sahrens if (spa->spa_scrub_restart_txg != 0) 2396789Sahrens error = ERESTART; 2397789Sahrens 23981544Seschrock if (spa->spa_scrub_stop) 23991544Seschrock error = EINTR; 24001544Seschrock 2401789Sahrens /* 24021544Seschrock * Even if there were uncorrectable errors, we consider the scrub 24031544Seschrock * completed. The downside is that if there is a transient error during 24041544Seschrock * a resilver, we won't resilver the data properly to the target. But 24051544Seschrock * if the damage is permanent (more likely) we will resilver forever, 24061544Seschrock * which isn't really acceptable. Since there is enough information for 24071544Seschrock * the user to know what has failed and why, this seems like a more 24081544Seschrock * tractable approach. 2409789Sahrens */ 24101544Seschrock complete = (error == 0); 2411789Sahrens 24121544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 24131544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2414789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2415789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2416789Sahrens 2417789Sahrens mutex_exit(&spa->spa_scrub_lock); 2418789Sahrens 2419789Sahrens /* 2420789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2421789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2422789Sahrens */ 2423789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2424789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2425789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 24261544Seschrock spa_errlog_rotate(spa); 24271601Sbonwick 24281544Seschrock spa_config_exit(spa, FTAG); 2429789Sahrens 2430789Sahrens mutex_enter(&spa->spa_scrub_lock); 2431789Sahrens 24321544Seschrock /* 24331544Seschrock * We may have finished replacing a device. 24341544Seschrock * Let the async thread assess this and handle the detach. 24351544Seschrock */ 24361544Seschrock spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 2437789Sahrens 2438789Sahrens /* 2439789Sahrens * If we were told to restart, our final act is to start a new scrub. 2440789Sahrens */ 2441789Sahrens if (error == ERESTART) 24421544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 24431544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2444789Sahrens 24451544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 24461544Seschrock spa->spa_scrub_active = 0; 24471544Seschrock spa->spa_scrub_thread = NULL; 24481544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2449789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2450789Sahrens thread_exit(); 2451789Sahrens } 2452789Sahrens 2453789Sahrens void 2454789Sahrens spa_scrub_suspend(spa_t *spa) 2455789Sahrens { 2456789Sahrens mutex_enter(&spa->spa_scrub_lock); 24571544Seschrock spa->spa_scrub_suspended++; 2458789Sahrens while (spa->spa_scrub_active) { 2459789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2460789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2461789Sahrens } 2462789Sahrens while (spa->spa_scrub_inflight) 2463789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2464789Sahrens mutex_exit(&spa->spa_scrub_lock); 2465789Sahrens } 2466789Sahrens 2467789Sahrens void 2468789Sahrens spa_scrub_resume(spa_t *spa) 2469789Sahrens { 2470789Sahrens mutex_enter(&spa->spa_scrub_lock); 24711544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 24721544Seschrock if (--spa->spa_scrub_suspended == 0) 2473789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2474789Sahrens mutex_exit(&spa->spa_scrub_lock); 2475789Sahrens } 2476789Sahrens 2477789Sahrens void 2478789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2479789Sahrens { 2480789Sahrens /* 2481789Sahrens * Something happened (e.g. snapshot create/delete) that means 2482789Sahrens * we must restart any in-progress scrubs. The itinerary will 2483789Sahrens * fix this properly. 2484789Sahrens */ 2485789Sahrens mutex_enter(&spa->spa_scrub_lock); 2486789Sahrens spa->spa_scrub_restart_txg = txg; 2487789Sahrens mutex_exit(&spa->spa_scrub_lock); 2488789Sahrens } 2489789Sahrens 24901544Seschrock int 24911544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2492789Sahrens { 2493789Sahrens space_seg_t *ss; 2494789Sahrens uint64_t mintxg, maxtxg; 2495789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2496789Sahrens 2497789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2498789Sahrens return (ENOTSUP); 2499789Sahrens 25001544Seschrock mutex_enter(&spa->spa_scrub_lock); 25011544Seschrock 2502789Sahrens /* 2503789Sahrens * If there's a scrub or resilver already in progress, stop it. 2504789Sahrens */ 2505789Sahrens while (spa->spa_scrub_thread != NULL) { 2506789Sahrens /* 2507789Sahrens * Don't stop a resilver unless forced. 2508789Sahrens */ 25091544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 25101544Seschrock mutex_exit(&spa->spa_scrub_lock); 2511789Sahrens return (EBUSY); 25121544Seschrock } 2513789Sahrens spa->spa_scrub_stop = 1; 2514789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2515789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2516789Sahrens } 2517789Sahrens 2518789Sahrens /* 2519789Sahrens * Terminate the previous traverse. 2520789Sahrens */ 2521789Sahrens if (spa->spa_scrub_th != NULL) { 2522789Sahrens traverse_fini(spa->spa_scrub_th); 2523789Sahrens spa->spa_scrub_th = NULL; 2524789Sahrens } 2525789Sahrens 25261544Seschrock if (rvd == NULL) { 25271544Seschrock ASSERT(spa->spa_scrub_stop == 0); 25281544Seschrock ASSERT(spa->spa_scrub_type == type); 25291544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 25301544Seschrock mutex_exit(&spa->spa_scrub_lock); 25311544Seschrock return (0); 25321544Seschrock } 2533789Sahrens 2534789Sahrens mintxg = TXG_INITIAL - 1; 2535789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2536789Sahrens 25371544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2538789Sahrens 25391544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 25401544Seschrock /* 25411544Seschrock * The pool-wide DTL is empty. 25421732Sbonwick * If this is a resilver, there's nothing to do except 25431732Sbonwick * check whether any in-progress replacements have completed. 25441544Seschrock */ 25451732Sbonwick if (type == POOL_SCRUB_RESILVER) { 25461544Seschrock type = POOL_SCRUB_NONE; 25471732Sbonwick spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 25481732Sbonwick } 25491544Seschrock } else { 25501544Seschrock /* 25511544Seschrock * The pool-wide DTL is non-empty. 25521544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 25531544Seschrock */ 25541544Seschrock if (type == POOL_SCRUB_EVERYTHING) 25551544Seschrock type = POOL_SCRUB_RESILVER; 25561544Seschrock } 2557789Sahrens 25581544Seschrock if (type == POOL_SCRUB_RESILVER) { 2559789Sahrens /* 2560789Sahrens * Determine the resilvering boundaries. 2561789Sahrens * 2562789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2563789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2564789Sahrens * 2565789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2566789Sahrens * so we don't claim to resilver a txg that's still changing. 2567789Sahrens */ 2568789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 25691544Seschrock mintxg = ss->ss_start - 1; 2570789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 25711544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 2572789Sahrens } 2573789Sahrens 25741544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 25751544Seschrock 25761544Seschrock spa->spa_scrub_stop = 0; 25771544Seschrock spa->spa_scrub_type = type; 25781544Seschrock spa->spa_scrub_restart_txg = 0; 25791544Seschrock 25801544Seschrock if (type != POOL_SCRUB_NONE) { 25811544Seschrock spa->spa_scrub_mintxg = mintxg; 2582789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2583789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 25841635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 25851635Sbonwick ZIO_FLAG_CANFAIL); 2586789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2587789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2588789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2589789Sahrens } 2590789Sahrens 25911544Seschrock mutex_exit(&spa->spa_scrub_lock); 25921544Seschrock 2593789Sahrens return (0); 2594789Sahrens } 2595789Sahrens 25961544Seschrock /* 25971544Seschrock * ========================================================================== 25981544Seschrock * SPA async task processing 25991544Seschrock * ========================================================================== 26001544Seschrock */ 26011544Seschrock 26021544Seschrock static void 26031544Seschrock spa_async_reopen(spa_t *spa) 2604789Sahrens { 26051544Seschrock vdev_t *rvd = spa->spa_root_vdev; 26061544Seschrock vdev_t *tvd; 26071544Seschrock int c; 26081544Seschrock 26091544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 26101544Seschrock 26111544Seschrock for (c = 0; c < rvd->vdev_children; c++) { 26121544Seschrock tvd = rvd->vdev_child[c]; 26131544Seschrock if (tvd->vdev_reopen_wanted) { 26141544Seschrock tvd->vdev_reopen_wanted = 0; 26151544Seschrock vdev_reopen(tvd); 26161544Seschrock } 26171544Seschrock } 2618789Sahrens 26191544Seschrock spa_config_exit(spa, FTAG); 26201544Seschrock } 26211544Seschrock 26221544Seschrock static void 26231544Seschrock spa_async_thread(spa_t *spa) 26241544Seschrock { 26251544Seschrock int tasks; 26261544Seschrock 26271544Seschrock ASSERT(spa->spa_sync_on); 2628789Sahrens 26291544Seschrock mutex_enter(&spa->spa_async_lock); 26301544Seschrock tasks = spa->spa_async_tasks; 26311544Seschrock spa->spa_async_tasks = 0; 26321544Seschrock mutex_exit(&spa->spa_async_lock); 26331544Seschrock 26341544Seschrock /* 26351635Sbonwick * See if the config needs to be updated. 26361635Sbonwick */ 26371635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 26381635Sbonwick mutex_enter(&spa_namespace_lock); 26391635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26401635Sbonwick mutex_exit(&spa_namespace_lock); 26411635Sbonwick } 26421635Sbonwick 26431635Sbonwick /* 26441544Seschrock * See if any devices need to be reopened. 26451544Seschrock */ 26461544Seschrock if (tasks & SPA_ASYNC_REOPEN) 26471544Seschrock spa_async_reopen(spa); 26481544Seschrock 26491544Seschrock /* 26501544Seschrock * If any devices are done replacing, detach them. 26511544Seschrock */ 26521544Seschrock if (tasks & SPA_ASYNC_REPLACE_DONE) 2653789Sahrens spa_vdev_replace_done(spa); 2654789Sahrens 26551544Seschrock /* 26561544Seschrock * Kick off a scrub. 26571544Seschrock */ 26581544Seschrock if (tasks & SPA_ASYNC_SCRUB) 26591544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 26601544Seschrock 26611544Seschrock /* 26621544Seschrock * Kick off a resilver. 26631544Seschrock */ 26641544Seschrock if (tasks & SPA_ASYNC_RESILVER) 26651544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 26661544Seschrock 26671544Seschrock /* 26681544Seschrock * Let the world know that we're done. 26691544Seschrock */ 26701544Seschrock mutex_enter(&spa->spa_async_lock); 26711544Seschrock spa->spa_async_thread = NULL; 26721544Seschrock cv_broadcast(&spa->spa_async_cv); 26731544Seschrock mutex_exit(&spa->spa_async_lock); 26741544Seschrock thread_exit(); 26751544Seschrock } 26761544Seschrock 26771544Seschrock void 26781544Seschrock spa_async_suspend(spa_t *spa) 26791544Seschrock { 26801544Seschrock mutex_enter(&spa->spa_async_lock); 26811544Seschrock spa->spa_async_suspended++; 26821544Seschrock while (spa->spa_async_thread != NULL) 26831544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 26841544Seschrock mutex_exit(&spa->spa_async_lock); 26851544Seschrock } 26861544Seschrock 26871544Seschrock void 26881544Seschrock spa_async_resume(spa_t *spa) 26891544Seschrock { 26901544Seschrock mutex_enter(&spa->spa_async_lock); 26911544Seschrock ASSERT(spa->spa_async_suspended != 0); 26921544Seschrock spa->spa_async_suspended--; 26931544Seschrock mutex_exit(&spa->spa_async_lock); 26941544Seschrock } 26951544Seschrock 26961544Seschrock static void 26971544Seschrock spa_async_dispatch(spa_t *spa) 26981544Seschrock { 26991544Seschrock mutex_enter(&spa->spa_async_lock); 27001544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 27011635Sbonwick spa->spa_async_thread == NULL && 27021635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 27031544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 27041544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 27051544Seschrock mutex_exit(&spa->spa_async_lock); 27061544Seschrock } 27071544Seschrock 27081544Seschrock void 27091544Seschrock spa_async_request(spa_t *spa, int task) 27101544Seschrock { 27111544Seschrock mutex_enter(&spa->spa_async_lock); 27121544Seschrock spa->spa_async_tasks |= task; 27131544Seschrock mutex_exit(&spa->spa_async_lock); 2714789Sahrens } 2715789Sahrens 2716789Sahrens /* 2717789Sahrens * ========================================================================== 2718789Sahrens * SPA syncing routines 2719789Sahrens * ========================================================================== 2720789Sahrens */ 2721789Sahrens 2722789Sahrens static void 2723789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2724789Sahrens { 2725789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2726789Sahrens dmu_tx_t *tx; 2727789Sahrens blkptr_t blk; 2728789Sahrens uint64_t itor = 0; 2729789Sahrens zio_t *zio; 2730789Sahrens int error; 2731789Sahrens uint8_t c = 1; 2732789Sahrens 2733789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2734789Sahrens 2735789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2736789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2737789Sahrens 2738789Sahrens error = zio_wait(zio); 2739789Sahrens ASSERT3U(error, ==, 0); 2740789Sahrens 2741789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2742789Sahrens bplist_vacate(bpl, tx); 2743789Sahrens 2744789Sahrens /* 2745789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2746789Sahrens * (Usually only the first block is needed.) 2747789Sahrens */ 2748789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2749789Sahrens dmu_tx_commit(tx); 2750789Sahrens } 2751789Sahrens 2752789Sahrens static void 27532082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 27542082Seschrock { 27552082Seschrock char *packed = NULL; 27562082Seschrock size_t nvsize = 0; 27572082Seschrock dmu_buf_t *db; 27582082Seschrock 27592082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 27602082Seschrock 27612082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 27622082Seschrock 27632082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 27642082Seschrock KM_SLEEP) == 0); 27652082Seschrock 27662082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 27672082Seschrock 27682082Seschrock kmem_free(packed, nvsize); 27692082Seschrock 27702082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 27712082Seschrock dmu_buf_will_dirty(db, tx); 27722082Seschrock *(uint64_t *)db->db_data = nvsize; 27732082Seschrock dmu_buf_rele(db, FTAG); 27742082Seschrock } 27752082Seschrock 27762082Seschrock static void 27772082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 27782082Seschrock { 27792082Seschrock nvlist_t *nvroot; 27802082Seschrock nvlist_t **spares; 27812082Seschrock int i; 27822082Seschrock 27832082Seschrock if (!spa->spa_sync_spares) 27842082Seschrock return; 27852082Seschrock 27862082Seschrock /* 27872082Seschrock * Update the MOS nvlist describing the list of available spares. 27882082Seschrock * spa_validate_spares() will have already made sure this nvlist is 27892082Seschrock * valid and the vdevs are labelled appropriately. 27902082Seschrock */ 27912082Seschrock if (spa->spa_spares_object == 0) { 27922082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 27932082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 27942082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 27952082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 27962082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 27972082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 27982082Seschrock } 27992082Seschrock 28002082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 28012082Seschrock if (spa->spa_nspares == 0) { 28022082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 28032082Seschrock NULL, 0) == 0); 28042082Seschrock } else { 28052082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 28062082Seschrock KM_SLEEP); 28072082Seschrock for (i = 0; i < spa->spa_nspares; i++) 28082082Seschrock spares[i] = vdev_config_generate(spa, 28092082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 28102082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 28112082Seschrock spares, spa->spa_nspares) == 0); 28122082Seschrock for (i = 0; i < spa->spa_nspares; i++) 28132082Seschrock nvlist_free(spares[i]); 28142082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 28152082Seschrock } 28162082Seschrock 28172082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 28182926Sek110237 nvlist_free(nvroot); 28192082Seschrock 28202082Seschrock spa->spa_sync_spares = B_FALSE; 28212082Seschrock } 28222082Seschrock 28232082Seschrock static void 2824789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2825789Sahrens { 2826789Sahrens nvlist_t *config; 2827789Sahrens 2828789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2829789Sahrens return; 2830789Sahrens 2831789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2832789Sahrens 28331635Sbonwick if (spa->spa_config_syncing) 28341635Sbonwick nvlist_free(spa->spa_config_syncing); 28351635Sbonwick spa->spa_config_syncing = config; 2836789Sahrens 28372082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2838789Sahrens } 2839789Sahrens 2840*3912Slling static void 2841*3912Slling spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 2842*3912Slling { 2843*3912Slling spa_t *spa = arg1; 2844*3912Slling nvlist_t *nvp = arg2; 2845*3912Slling nvpair_t *nvpair; 2846*3912Slling objset_t *mos = spa->spa_meta_objset; 2847*3912Slling uint64_t zapobj; 2848*3912Slling 2849*3912Slling mutex_enter(&spa->spa_props_lock); 2850*3912Slling if (spa->spa_pool_props_object == 0) { 2851*3912Slling zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 2852*3912Slling VERIFY(zapobj > 0); 2853*3912Slling 2854*3912Slling spa->spa_pool_props_object = zapobj; 2855*3912Slling 2856*3912Slling VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 2857*3912Slling DMU_POOL_PROPS, 8, 1, 2858*3912Slling &spa->spa_pool_props_object, tx) == 0); 2859*3912Slling } 2860*3912Slling mutex_exit(&spa->spa_props_lock); 2861*3912Slling 2862*3912Slling nvpair = NULL; 2863*3912Slling while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 2864*3912Slling switch (zpool_name_to_prop(nvpair_name(nvpair))) { 2865*3912Slling case ZFS_PROP_BOOTFS: 2866*3912Slling VERIFY(nvlist_lookup_uint64(nvp, 2867*3912Slling nvpair_name(nvpair), &spa->spa_bootfs) == 0); 2868*3912Slling VERIFY(zap_update(mos, 2869*3912Slling spa->spa_pool_props_object, 2870*3912Slling zpool_prop_to_name(ZFS_PROP_BOOTFS), 8, 1, 2871*3912Slling &spa->spa_bootfs, tx) == 0); 2872*3912Slling break; 2873*3912Slling } 2874*3912Slling } 2875*3912Slling } 2876*3912Slling 2877789Sahrens /* 2878789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 2879789Sahrens * part of the process, so we iterate until it converges. 2880789Sahrens */ 2881789Sahrens void 2882789Sahrens spa_sync(spa_t *spa, uint64_t txg) 2883789Sahrens { 2884789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 2885789Sahrens objset_t *mos = spa->spa_meta_objset; 2886789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 28871635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 2888789Sahrens vdev_t *vd; 2889789Sahrens dmu_tx_t *tx; 2890789Sahrens int dirty_vdevs; 2891789Sahrens 2892789Sahrens /* 2893789Sahrens * Lock out configuration changes. 2894789Sahrens */ 28951544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2896789Sahrens 2897789Sahrens spa->spa_syncing_txg = txg; 2898789Sahrens spa->spa_sync_pass = 0; 2899789Sahrens 29001544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 2901789Sahrens 29022082Seschrock tx = dmu_tx_create_assigned(dp, txg); 29032082Seschrock 29042082Seschrock /* 29052082Seschrock * If we are upgrading to ZFS_VERSION_RAIDZ_DEFLATE this txg, 29062082Seschrock * set spa_deflate if we have no raid-z vdevs. 29072082Seschrock */ 29082082Seschrock if (spa->spa_ubsync.ub_version < ZFS_VERSION_RAIDZ_DEFLATE && 29092082Seschrock spa->spa_uberblock.ub_version >= ZFS_VERSION_RAIDZ_DEFLATE) { 29102082Seschrock int i; 29112082Seschrock 29122082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 29132082Seschrock vd = rvd->vdev_child[i]; 29142082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 29152082Seschrock break; 29162082Seschrock } 29172082Seschrock if (i == rvd->vdev_children) { 29182082Seschrock spa->spa_deflate = TRUE; 29192082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 29202082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 29212082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 29222082Seschrock } 29232082Seschrock } 29242082Seschrock 2925789Sahrens /* 2926789Sahrens * If anything has changed in this txg, push the deferred frees 2927789Sahrens * from the previous txg. If not, leave them alone so that we 2928789Sahrens * don't generate work on an otherwise idle system. 2929789Sahrens */ 2930789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 29312329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 29322329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 2933789Sahrens spa_sync_deferred_frees(spa, txg); 2934789Sahrens 2935789Sahrens /* 2936789Sahrens * Iterate to convergence. 2937789Sahrens */ 2938789Sahrens do { 2939789Sahrens spa->spa_sync_pass++; 2940789Sahrens 2941789Sahrens spa_sync_config_object(spa, tx); 29422082Seschrock spa_sync_spares(spa, tx); 29431544Seschrock spa_errlog_sync(spa, txg); 2944789Sahrens dsl_pool_sync(dp, txg); 2945789Sahrens 2946789Sahrens dirty_vdevs = 0; 2947789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 2948789Sahrens vdev_sync(vd, txg); 2949789Sahrens dirty_vdevs++; 2950789Sahrens } 2951789Sahrens 2952789Sahrens bplist_sync(bpl, tx); 2953789Sahrens } while (dirty_vdevs); 2954789Sahrens 2955789Sahrens bplist_close(bpl); 2956789Sahrens 2957789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 2958789Sahrens 2959789Sahrens /* 2960789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 2961789Sahrens * to commit the transaction group. 29621635Sbonwick * 29631635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 29641635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 29651635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 29661635Sbonwick * If the write fails, try the next vdev until we're tried them all. 2967789Sahrens */ 29681635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 29691635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 29701635Sbonwick } else { 29711635Sbonwick int children = rvd->vdev_children; 29721635Sbonwick int c0 = spa_get_random(children); 29731635Sbonwick int c; 29741635Sbonwick 29751635Sbonwick for (c = 0; c < children; c++) { 29761635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 29771635Sbonwick if (vd->vdev_ms_array == 0) 29781635Sbonwick continue; 29791635Sbonwick if (vdev_config_sync(vd, txg) == 0) 29801635Sbonwick break; 29811635Sbonwick } 29821635Sbonwick if (c == children) 29831635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 29841635Sbonwick } 29851635Sbonwick 29862082Seschrock dmu_tx_commit(tx); 29872082Seschrock 29881635Sbonwick /* 29891635Sbonwick * Clear the dirty config list. 29901635Sbonwick */ 29911635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 29921635Sbonwick vdev_config_clean(vd); 29931635Sbonwick 29941635Sbonwick /* 29951635Sbonwick * Now that the new config has synced transactionally, 29961635Sbonwick * let it become visible to the config cache. 29971635Sbonwick */ 29981635Sbonwick if (spa->spa_config_syncing != NULL) { 29991635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 30001635Sbonwick spa->spa_config_txg = txg; 30011635Sbonwick spa->spa_config_syncing = NULL; 30021635Sbonwick } 3003789Sahrens 3004789Sahrens /* 3005789Sahrens * Make a stable copy of the fully synced uberblock. 3006789Sahrens * We use this as the root for pool traversals. 3007789Sahrens */ 3008789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3009789Sahrens 3010789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3011789Sahrens 3012789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3013789Sahrens spa->spa_traverse_wanted = 0; 3014789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3015789Sahrens rw_exit(&spa->spa_traverse_lock); 3016789Sahrens 3017789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3018789Sahrens 3019789Sahrens /* 3020789Sahrens * Clean up the ZIL records for the synced txg. 3021789Sahrens */ 3022789Sahrens dsl_pool_zil_clean(dp); 3023789Sahrens 3024789Sahrens /* 3025789Sahrens * Update usable space statistics. 3026789Sahrens */ 3027789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3028789Sahrens vdev_sync_done(vd, txg); 3029789Sahrens 3030789Sahrens /* 3031789Sahrens * It had better be the case that we didn't dirty anything 30322082Seschrock * since vdev_config_sync(). 3033789Sahrens */ 3034789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3035789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3036789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3037789Sahrens ASSERT(bpl->bpl_queue == NULL); 3038789Sahrens 30391544Seschrock spa_config_exit(spa, FTAG); 30401544Seschrock 30411544Seschrock /* 30421544Seschrock * If any async tasks have been requested, kick them off. 30431544Seschrock */ 30441544Seschrock spa_async_dispatch(spa); 3045789Sahrens } 3046789Sahrens 3047789Sahrens /* 3048789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3049789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3050789Sahrens * sync. 3051789Sahrens */ 3052789Sahrens void 3053789Sahrens spa_sync_allpools(void) 3054789Sahrens { 3055789Sahrens spa_t *spa = NULL; 3056789Sahrens mutex_enter(&spa_namespace_lock); 3057789Sahrens while ((spa = spa_next(spa)) != NULL) { 3058789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3059789Sahrens continue; 3060789Sahrens spa_open_ref(spa, FTAG); 3061789Sahrens mutex_exit(&spa_namespace_lock); 3062789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3063789Sahrens mutex_enter(&spa_namespace_lock); 3064789Sahrens spa_close(spa, FTAG); 3065789Sahrens } 3066789Sahrens mutex_exit(&spa_namespace_lock); 3067789Sahrens } 3068789Sahrens 3069789Sahrens /* 3070789Sahrens * ========================================================================== 3071789Sahrens * Miscellaneous routines 3072789Sahrens * ========================================================================== 3073789Sahrens */ 3074789Sahrens 3075789Sahrens /* 3076789Sahrens * Remove all pools in the system. 3077789Sahrens */ 3078789Sahrens void 3079789Sahrens spa_evict_all(void) 3080789Sahrens { 3081789Sahrens spa_t *spa; 3082789Sahrens 3083789Sahrens /* 3084789Sahrens * Remove all cached state. All pools should be closed now, 3085789Sahrens * so every spa in the AVL tree should be unreferenced. 3086789Sahrens */ 3087789Sahrens mutex_enter(&spa_namespace_lock); 3088789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3089789Sahrens /* 30901544Seschrock * Stop async tasks. The async thread may need to detach 30911544Seschrock * a device that's been replaced, which requires grabbing 30921544Seschrock * spa_namespace_lock, so we must drop it here. 3093789Sahrens */ 3094789Sahrens spa_open_ref(spa, FTAG); 3095789Sahrens mutex_exit(&spa_namespace_lock); 30961544Seschrock spa_async_suspend(spa); 3097789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3098789Sahrens mutex_enter(&spa_namespace_lock); 3099789Sahrens spa_close(spa, FTAG); 3100789Sahrens 3101789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3102789Sahrens spa_unload(spa); 3103789Sahrens spa_deactivate(spa); 3104789Sahrens } 3105789Sahrens spa_remove(spa); 3106789Sahrens } 3107789Sahrens mutex_exit(&spa_namespace_lock); 3108789Sahrens } 31091544Seschrock 31101544Seschrock vdev_t * 31111544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 31121544Seschrock { 31131544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 31141544Seschrock } 31151760Seschrock 31161760Seschrock void 31171760Seschrock spa_upgrade(spa_t *spa) 31181760Seschrock { 31191760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 31201760Seschrock 31211760Seschrock /* 31221760Seschrock * This should only be called for a non-faulted pool, and since a 31231760Seschrock * future version would result in an unopenable pool, this shouldn't be 31241760Seschrock * possible. 31251760Seschrock */ 31261760Seschrock ASSERT(spa->spa_uberblock.ub_version <= ZFS_VERSION); 31271760Seschrock 31281760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 31291760Seschrock vdev_config_dirty(spa->spa_root_vdev); 31301760Seschrock 31311760Seschrock spa_config_exit(spa, FTAG); 31322082Seschrock 31332082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 31341760Seschrock } 31352082Seschrock 31362082Seschrock boolean_t 31372082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 31382082Seschrock { 31392082Seschrock int i; 31403377Seschrock uint64_t spareguid; 31412082Seschrock 31422082Seschrock for (i = 0; i < spa->spa_nspares; i++) 31432082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 31442082Seschrock return (B_TRUE); 31452082Seschrock 31463377Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 31473377Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 31483377Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 31493377Seschrock spareguid == guid) 31503377Seschrock return (B_TRUE); 31513377Seschrock } 31523377Seschrock 31532082Seschrock return (B_FALSE); 31542082Seschrock } 3155*3912Slling 3156*3912Slling int 3157*3912Slling spa_set_props(spa_t *spa, nvlist_t *nvp) 3158*3912Slling { 3159*3912Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 3160*3912Slling spa, nvp, 3)); 3161*3912Slling } 3162*3912Slling 3163*3912Slling int 3164*3912Slling spa_get_props(spa_t *spa, nvlist_t **nvp) 3165*3912Slling { 3166*3912Slling zap_cursor_t zc; 3167*3912Slling zap_attribute_t za; 3168*3912Slling objset_t *mos = spa->spa_meta_objset; 3169*3912Slling zfs_source_t src; 3170*3912Slling zfs_prop_t prop; 3171*3912Slling nvlist_t *propval; 3172*3912Slling uint64_t value; 3173*3912Slling int err; 3174*3912Slling 3175*3912Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3176*3912Slling 3177*3912Slling mutex_enter(&spa->spa_props_lock); 3178*3912Slling /* If no props object, then just return empty nvlist */ 3179*3912Slling if (spa->spa_pool_props_object == 0) { 3180*3912Slling mutex_exit(&spa->spa_props_lock); 3181*3912Slling return (0); 3182*3912Slling } 3183*3912Slling 3184*3912Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 3185*3912Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 3186*3912Slling zap_cursor_advance(&zc)) { 3187*3912Slling 3188*3912Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 3189*3912Slling continue; 3190*3912Slling 3191*3912Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3192*3912Slling switch (za.za_integer_length) { 3193*3912Slling case 8: 3194*3912Slling if (zfs_prop_default_numeric(prop) == 3195*3912Slling za.za_first_integer) 3196*3912Slling src = ZFS_SRC_DEFAULT; 3197*3912Slling else 3198*3912Slling src = ZFS_SRC_LOCAL; 3199*3912Slling value = za.za_first_integer; 3200*3912Slling 3201*3912Slling if (prop == ZFS_PROP_BOOTFS) { 3202*3912Slling dsl_pool_t *dp; 3203*3912Slling dsl_dataset_t *ds = NULL; 3204*3912Slling char strval[MAXPATHLEN]; 3205*3912Slling 3206*3912Slling dp = spa_get_dsl(spa); 3207*3912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 3208*3912Slling if ((err = dsl_dataset_open_obj(dp, 3209*3912Slling za.za_first_integer, NULL, DS_MODE_NONE, 3210*3912Slling FTAG, &ds)) != 0) { 3211*3912Slling rw_exit(&dp->dp_config_rwlock); 3212*3912Slling break; 3213*3912Slling } 3214*3912Slling dsl_dataset_name(ds, strval); 3215*3912Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 3216*3912Slling rw_exit(&dp->dp_config_rwlock); 3217*3912Slling 3218*3912Slling VERIFY(nvlist_add_uint64(propval, 3219*3912Slling ZFS_PROP_SOURCE, src) == 0); 3220*3912Slling VERIFY(nvlist_add_string(propval, 3221*3912Slling ZFS_PROP_VALUE, strval) == 0); 3222*3912Slling } else { 3223*3912Slling VERIFY(nvlist_add_uint64(propval, 3224*3912Slling ZFS_PROP_SOURCE, src) == 0); 3225*3912Slling VERIFY(nvlist_add_uint64(propval, 3226*3912Slling ZFS_PROP_VALUE, value) == 0); 3227*3912Slling } 3228*3912Slling VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 3229*3912Slling propval) == 0); 3230*3912Slling break; 3231*3912Slling } 3232*3912Slling nvlist_free(propval); 3233*3912Slling } 3234*3912Slling zap_cursor_fini(&zc); 3235*3912Slling mutex_exit(&spa->spa_props_lock); 3236*3912Slling if (err && err != ENOENT) { 3237*3912Slling nvlist_free(*nvp); 3238*3912Slling return (err); 3239*3912Slling } 3240*3912Slling 3241*3912Slling return (0); 3242*3912Slling } 3243*3912Slling 3244*3912Slling /* 3245*3912Slling * If the bootfs property value is dsobj, clear it. 3246*3912Slling */ 3247*3912Slling void 3248*3912Slling spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 3249*3912Slling { 3250*3912Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 3251*3912Slling VERIFY(zap_remove(spa->spa_meta_objset, 3252*3912Slling spa->spa_pool_props_object, 3253*3912Slling zpool_prop_to_name(ZFS_PROP_BOOTFS), tx) == 0); 3254*3912Slling spa->spa_bootfs = 0; 3255*3912Slling } 3256*3912Slling } 3257