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 /* 231354Seschrock * Copyright 2006 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> 51789Sahrens #include <sys/unique.h> 52789Sahrens #include <sys/dsl_pool.h> 53789Sahrens #include <sys/dsl_dir.h> 54789Sahrens #include <sys/dsl_prop.h> 55789Sahrens #include <sys/fs/zfs.h> 56789Sahrens #include <sys/callb.h> 57789Sahrens 58789Sahrens /* 59789Sahrens * ========================================================================== 60789Sahrens * SPA state manipulation (open/create/destroy/import/export) 61789Sahrens * ========================================================================== 62789Sahrens */ 63789Sahrens 641544Seschrock static int 651544Seschrock spa_error_entry_compare(const void *a, const void *b) 661544Seschrock { 671544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 681544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 691544Seschrock int ret; 701544Seschrock 711544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 721544Seschrock sizeof (zbookmark_t)); 731544Seschrock 741544Seschrock if (ret < 0) 751544Seschrock return (-1); 761544Seschrock else if (ret > 0) 771544Seschrock return (1); 781544Seschrock else 791544Seschrock return (0); 801544Seschrock } 811544Seschrock 821544Seschrock /* 831544Seschrock * Utility function which retrieves copies of the current logs and 841544Seschrock * re-initializes them in the process. 851544Seschrock */ 861544Seschrock void 871544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 881544Seschrock { 891544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 901544Seschrock 911544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 921544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 931544Seschrock 941544Seschrock avl_create(&spa->spa_errlist_scrub, 951544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 961544Seschrock offsetof(spa_error_entry_t, se_avl)); 971544Seschrock avl_create(&spa->spa_errlist_last, 981544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 991544Seschrock offsetof(spa_error_entry_t, se_avl)); 1001544Seschrock } 1011544Seschrock 102789Sahrens /* 103789Sahrens * Activate an uninitialized pool. 104789Sahrens */ 105789Sahrens static void 106789Sahrens spa_activate(spa_t *spa) 107789Sahrens { 108789Sahrens int t; 109789Sahrens 110789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 111789Sahrens 112789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 113789Sahrens 114789Sahrens spa->spa_normal_class = metaslab_class_create(); 115789Sahrens 116789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 117789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 118789Sahrens 8, maxclsyspri, 50, INT_MAX, 119789Sahrens TASKQ_PREPOPULATE); 120789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 121789Sahrens 8, maxclsyspri, 50, INT_MAX, 122789Sahrens TASKQ_PREPOPULATE); 123789Sahrens } 124789Sahrens 125789Sahrens rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 126789Sahrens 127*2856Snd150628 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 128*2856Snd150628 mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL); 129*2856Snd150628 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 130*2856Snd150628 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 131*2856Snd150628 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 132*2856Snd150628 mutex_init(&spa->spa_config_lock.scl_lock, NULL, MUTEX_DEFAULT, NULL); 133*2856Snd150628 mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL); 134*2856Snd150628 135789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 136789Sahrens offsetof(vdev_t, vdev_dirty_node)); 137789Sahrens 138789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 139789Sahrens offsetof(struct vdev, vdev_txg_node)); 1401544Seschrock 1411544Seschrock avl_create(&spa->spa_errlist_scrub, 1421544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1431544Seschrock offsetof(spa_error_entry_t, se_avl)); 1441544Seschrock avl_create(&spa->spa_errlist_last, 1451544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1461544Seschrock offsetof(spa_error_entry_t, se_avl)); 147789Sahrens } 148789Sahrens 149789Sahrens /* 150789Sahrens * Opposite of spa_activate(). 151789Sahrens */ 152789Sahrens static void 153789Sahrens spa_deactivate(spa_t *spa) 154789Sahrens { 155789Sahrens int t; 156789Sahrens 157789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 158789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 159789Sahrens ASSERT(spa->spa_root_vdev == NULL); 160789Sahrens 161789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 162789Sahrens 163789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 164789Sahrens 165789Sahrens list_destroy(&spa->spa_dirty_list); 166789Sahrens 167789Sahrens rw_destroy(&spa->spa_traverse_lock); 168789Sahrens 169789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 170789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 171789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 172789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 173789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 174789Sahrens } 175789Sahrens 176789Sahrens metaslab_class_destroy(spa->spa_normal_class); 177789Sahrens spa->spa_normal_class = NULL; 178789Sahrens 1791544Seschrock /* 1801544Seschrock * If this was part of an import or the open otherwise failed, we may 1811544Seschrock * still have errors left in the queues. Empty them just in case. 1821544Seschrock */ 1831544Seschrock spa_errlog_drain(spa); 1841544Seschrock 1851544Seschrock avl_destroy(&spa->spa_errlist_scrub); 1861544Seschrock avl_destroy(&spa->spa_errlist_last); 1871544Seschrock 188789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 189789Sahrens } 190789Sahrens 191789Sahrens /* 192789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 193789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 194789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 195789Sahrens * All vdev validation is done by the vdev_alloc() routine. 196789Sahrens */ 1972082Seschrock static int 1982082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 1992082Seschrock uint_t id, int atype) 200789Sahrens { 201789Sahrens nvlist_t **child; 202789Sahrens uint_t c, children; 2032082Seschrock int error; 2042082Seschrock 2052082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 2062082Seschrock return (error); 2072082Seschrock 2082082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 2092082Seschrock return (0); 210789Sahrens 211789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 212789Sahrens &child, &children) != 0) { 2132082Seschrock vdev_free(*vdp); 2142082Seschrock *vdp = NULL; 2152082Seschrock return (EINVAL); 216789Sahrens } 217789Sahrens 218789Sahrens for (c = 0; c < children; c++) { 2192082Seschrock vdev_t *vd; 2202082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 2212082Seschrock atype)) != 0) { 2222082Seschrock vdev_free(*vdp); 2232082Seschrock *vdp = NULL; 2242082Seschrock return (error); 225789Sahrens } 226789Sahrens } 227789Sahrens 2282082Seschrock ASSERT(*vdp != NULL); 2292082Seschrock 2302082Seschrock return (0); 231789Sahrens } 232789Sahrens 233789Sahrens /* 234789Sahrens * Opposite of spa_load(). 235789Sahrens */ 236789Sahrens static void 237789Sahrens spa_unload(spa_t *spa) 238789Sahrens { 2392082Seschrock int i; 2402082Seschrock 241789Sahrens /* 2421544Seschrock * Stop async tasks. 2431544Seschrock */ 2441544Seschrock spa_async_suspend(spa); 2451544Seschrock 2461544Seschrock /* 247789Sahrens * Stop syncing. 248789Sahrens */ 249789Sahrens if (spa->spa_sync_on) { 250789Sahrens txg_sync_stop(spa->spa_dsl_pool); 251789Sahrens spa->spa_sync_on = B_FALSE; 252789Sahrens } 253789Sahrens 254789Sahrens /* 255789Sahrens * Wait for any outstanding prefetch I/O to complete. 256789Sahrens */ 2571544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2581544Seschrock spa_config_exit(spa, FTAG); 259789Sahrens 260789Sahrens /* 261789Sahrens * Close the dsl pool. 262789Sahrens */ 263789Sahrens if (spa->spa_dsl_pool) { 264789Sahrens dsl_pool_close(spa->spa_dsl_pool); 265789Sahrens spa->spa_dsl_pool = NULL; 266789Sahrens } 267789Sahrens 268789Sahrens /* 269789Sahrens * Close all vdevs. 270789Sahrens */ 2711585Sbonwick if (spa->spa_root_vdev) 272789Sahrens vdev_free(spa->spa_root_vdev); 2731585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 2741544Seschrock 2752082Seschrock for (i = 0; i < spa->spa_nspares; i++) 2762082Seschrock vdev_free(spa->spa_spares[i]); 2772082Seschrock if (spa->spa_spares) { 2782082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 2792082Seschrock spa->spa_spares = NULL; 2802082Seschrock } 2812082Seschrock if (spa->spa_sparelist) { 2822082Seschrock nvlist_free(spa->spa_sparelist); 2832082Seschrock spa->spa_sparelist = NULL; 2842082Seschrock } 2852082Seschrock 2861544Seschrock spa->spa_async_suspended = 0; 287789Sahrens } 288789Sahrens 289789Sahrens /* 2902082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 2912082Seschrock * this pool. When this is called, we have some form of basic information in 2922082Seschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 2932082Seschrock * re-generate a more complete list including status information. 2942082Seschrock */ 2952082Seschrock static void 2962082Seschrock spa_load_spares(spa_t *spa) 2972082Seschrock { 2982082Seschrock nvlist_t **spares; 2992082Seschrock uint_t nspares; 3002082Seschrock int i; 3012082Seschrock 3022082Seschrock /* 3032082Seschrock * First, close and free any existing spare vdevs. 3042082Seschrock */ 3052082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3062082Seschrock vdev_close(spa->spa_spares[i]); 3072082Seschrock vdev_free(spa->spa_spares[i]); 3082082Seschrock } 3092082Seschrock if (spa->spa_spares) 3102082Seschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 3112082Seschrock 3122082Seschrock if (spa->spa_sparelist == NULL) 3132082Seschrock nspares = 0; 3142082Seschrock else 3152082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 3162082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 3172082Seschrock 3182082Seschrock spa->spa_nspares = (int)nspares; 3192082Seschrock spa->spa_spares = NULL; 3202082Seschrock 3212082Seschrock if (nspares == 0) 3222082Seschrock return; 3232082Seschrock 3242082Seschrock /* 3252082Seschrock * Construct the array of vdevs, opening them to get status in the 3262082Seschrock * process. 3272082Seschrock */ 3282082Seschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 3292082Seschrock for (i = 0; i < spa->spa_nspares; i++) { 3302082Seschrock vdev_t *vd; 3312082Seschrock 3322082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 3332082Seschrock VDEV_ALLOC_SPARE) == 0); 3342082Seschrock ASSERT(vd != NULL); 3352082Seschrock 3362082Seschrock spa->spa_spares[i] = vd; 3372082Seschrock 3382082Seschrock if (vdev_open(vd) != 0) 3392082Seschrock continue; 3402082Seschrock 3412082Seschrock vd->vdev_top = vd; 3422082Seschrock (void) vdev_validate_spare(vd); 3432082Seschrock } 3442082Seschrock 3452082Seschrock /* 3462082Seschrock * Recompute the stashed list of spares, with status information 3472082Seschrock * this time. 3482082Seschrock */ 3492082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3502082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3512082Seschrock 3522082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 3532082Seschrock for (i = 0; i < spa->spa_nspares; i++) 3542082Seschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 3552082Seschrock B_TRUE, B_TRUE); 3562082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 3572082Seschrock spares, spa->spa_nspares) == 0); 3582082Seschrock for (i = 0; i < spa->spa_nspares; i++) 3592082Seschrock nvlist_free(spares[i]); 3602082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 3612082Seschrock } 3622082Seschrock 3632082Seschrock static int 3642082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 3652082Seschrock { 3662082Seschrock dmu_buf_t *db; 3672082Seschrock char *packed = NULL; 3682082Seschrock size_t nvsize = 0; 3692082Seschrock int error; 3702082Seschrock *value = NULL; 3712082Seschrock 3722082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3732082Seschrock nvsize = *(uint64_t *)db->db_data; 3742082Seschrock dmu_buf_rele(db, FTAG); 3752082Seschrock 3762082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 3772082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 3782082Seschrock if (error == 0) 3792082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 3802082Seschrock kmem_free(packed, nvsize); 3812082Seschrock 3822082Seschrock return (error); 3832082Seschrock } 3842082Seschrock 3852082Seschrock /* 386789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 3871544Seschrock * source of configuration information. 388789Sahrens */ 389789Sahrens static int 3901544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 391789Sahrens { 392789Sahrens int error = 0; 393789Sahrens nvlist_t *nvroot = NULL; 394789Sahrens vdev_t *rvd; 395789Sahrens uberblock_t *ub = &spa->spa_uberblock; 3961635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 397789Sahrens uint64_t pool_guid; 3982082Seschrock uint64_t version; 399789Sahrens zio_t *zio; 400789Sahrens 4011544Seschrock spa->spa_load_state = state; 4021635Sbonwick 403789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 4041733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 4051544Seschrock error = EINVAL; 4061544Seschrock goto out; 4071544Seschrock } 408789Sahrens 4092082Seschrock /* 4102082Seschrock * Versioning wasn't explicitly added to the label until later, so if 4112082Seschrock * it's not present treat it as the initial version. 4122082Seschrock */ 4132082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 4142082Seschrock version = ZFS_VERSION_INITIAL; 4152082Seschrock 4161733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 4171733Sbonwick &spa->spa_config_txg); 4181733Sbonwick 4191635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 4201544Seschrock spa_guid_exists(pool_guid, 0)) { 4211544Seschrock error = EEXIST; 4221544Seschrock goto out; 4231544Seschrock } 424789Sahrens 4252174Seschrock spa->spa_load_guid = pool_guid; 4262174Seschrock 427789Sahrens /* 4282082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 4292082Seschrock * value that will be returned by spa_version() since parsing the 4302082Seschrock * configuration requires knowing the version number. 431789Sahrens */ 4321544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 4332082Seschrock spa->spa_ubsync.ub_version = version; 4342082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 4351544Seschrock spa_config_exit(spa, FTAG); 436789Sahrens 4372082Seschrock if (error != 0) 4381544Seschrock goto out; 439789Sahrens 4401585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 441789Sahrens ASSERT(spa_guid(spa) == pool_guid); 442789Sahrens 443789Sahrens /* 444789Sahrens * Try to open all vdevs, loading each label in the process. 445789Sahrens */ 4461544Seschrock if (vdev_open(rvd) != 0) { 4471544Seschrock error = ENXIO; 4481544Seschrock goto out; 4491544Seschrock } 450789Sahrens 451789Sahrens /* 4521986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 4531986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 4541986Seschrock * flag. 4551986Seschrock */ 4561986Seschrock spa_config_enter(spa, RW_READER, FTAG); 4571986Seschrock error = vdev_validate(rvd); 4581986Seschrock spa_config_exit(spa, FTAG); 4591986Seschrock 4601986Seschrock if (error != 0) { 4611986Seschrock error = EBADF; 4621986Seschrock goto out; 4631986Seschrock } 4641986Seschrock 4651986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 4661986Seschrock error = ENXIO; 4671986Seschrock goto out; 4681986Seschrock } 4691986Seschrock 4701986Seschrock /* 471789Sahrens * Find the best uberblock. 472789Sahrens */ 473789Sahrens bzero(ub, sizeof (uberblock_t)); 474789Sahrens 475789Sahrens zio = zio_root(spa, NULL, NULL, 476789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 477789Sahrens vdev_uberblock_load(zio, rvd, ub); 478789Sahrens error = zio_wait(zio); 479789Sahrens 480789Sahrens /* 481789Sahrens * If we weren't able to find a single valid uberblock, return failure. 482789Sahrens */ 483789Sahrens if (ub->ub_txg == 0) { 4841760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 4851760Seschrock VDEV_AUX_CORRUPT_DATA); 4861544Seschrock error = ENXIO; 4871544Seschrock goto out; 4881544Seschrock } 4891544Seschrock 4901544Seschrock /* 4911544Seschrock * If the pool is newer than the code, we can't open it. 4921544Seschrock */ 4931760Seschrock if (ub->ub_version > ZFS_VERSION) { 4941760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 4951760Seschrock VDEV_AUX_VERSION_NEWER); 4961544Seschrock error = ENOTSUP; 4971544Seschrock goto out; 498789Sahrens } 499789Sahrens 500789Sahrens /* 501789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 502789Sahrens * incomplete configuration. 503789Sahrens */ 5041732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 5051544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5061544Seschrock VDEV_AUX_BAD_GUID_SUM); 5071544Seschrock error = ENXIO; 5081544Seschrock goto out; 509789Sahrens } 510789Sahrens 511789Sahrens /* 512789Sahrens * Initialize internal SPA structures. 513789Sahrens */ 514789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 515789Sahrens spa->spa_ubsync = spa->spa_uberblock; 516789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 5171544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 5181544Seschrock if (error) { 5191544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5201544Seschrock VDEV_AUX_CORRUPT_DATA); 5211544Seschrock goto out; 5221544Seschrock } 523789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 524789Sahrens 5251544Seschrock if (zap_lookup(spa->spa_meta_objset, 526789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 5271544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 5281544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5291544Seschrock VDEV_AUX_CORRUPT_DATA); 5301544Seschrock error = EIO; 5311544Seschrock goto out; 5321544Seschrock } 533789Sahrens 534789Sahrens if (!mosconfig) { 5352082Seschrock nvlist_t *newconfig; 5362082Seschrock 5372082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 5381544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5391544Seschrock VDEV_AUX_CORRUPT_DATA); 5401544Seschrock error = EIO; 5411544Seschrock goto out; 5421544Seschrock } 543789Sahrens 544789Sahrens spa_config_set(spa, newconfig); 545789Sahrens spa_unload(spa); 546789Sahrens spa_deactivate(spa); 547789Sahrens spa_activate(spa); 548789Sahrens 5491544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 5501544Seschrock } 5511544Seschrock 5521544Seschrock if (zap_lookup(spa->spa_meta_objset, 5531544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 5541544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 5551544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5561544Seschrock VDEV_AUX_CORRUPT_DATA); 5571544Seschrock error = EIO; 5581544Seschrock goto out; 559789Sahrens } 560789Sahrens 5611544Seschrock /* 5622082Seschrock * Load the bit that tells us to use the new accounting function 5632082Seschrock * (raid-z deflation). If we have an older pool, this will not 5642082Seschrock * be present. 5652082Seschrock */ 5662082Seschrock error = zap_lookup(spa->spa_meta_objset, 5672082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 5682082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 5692082Seschrock if (error != 0 && error != ENOENT) { 5702082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5712082Seschrock VDEV_AUX_CORRUPT_DATA); 5722082Seschrock error = EIO; 5732082Seschrock goto out; 5742082Seschrock } 5752082Seschrock 5762082Seschrock /* 5771544Seschrock * Load the persistent error log. If we have an older pool, this will 5781544Seschrock * not be present. 5791544Seschrock */ 5801544Seschrock error = zap_lookup(spa->spa_meta_objset, 5811544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 5821544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 5831807Sbonwick if (error != 0 && error != ENOENT) { 5841544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5851544Seschrock VDEV_AUX_CORRUPT_DATA); 5861544Seschrock error = EIO; 5871544Seschrock goto out; 5881544Seschrock } 5891544Seschrock 5901544Seschrock error = zap_lookup(spa->spa_meta_objset, 5911544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 5921544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 5931544Seschrock if (error != 0 && error != ENOENT) { 5941544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 5951544Seschrock VDEV_AUX_CORRUPT_DATA); 5961544Seschrock error = EIO; 5971544Seschrock goto out; 5981544Seschrock } 599789Sahrens 600789Sahrens /* 6012082Seschrock * Load any hot spares for this pool. 6022082Seschrock */ 6032082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 6042082Seschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 6052082Seschrock if (error != 0 && error != ENOENT) { 6062082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6072082Seschrock VDEV_AUX_CORRUPT_DATA); 6082082Seschrock error = EIO; 6092082Seschrock goto out; 6102082Seschrock } 6112082Seschrock if (error == 0) { 6122082Seschrock ASSERT(spa_version(spa) >= ZFS_VERSION_SPARES); 6132082Seschrock if (load_nvlist(spa, spa->spa_spares_object, 6142082Seschrock &spa->spa_sparelist) != 0) { 6152082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 6162082Seschrock VDEV_AUX_CORRUPT_DATA); 6172082Seschrock error = EIO; 6182082Seschrock goto out; 6192082Seschrock } 6202082Seschrock 6212082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6222082Seschrock spa_load_spares(spa); 6232082Seschrock spa_config_exit(spa, FTAG); 6242082Seschrock } 6252082Seschrock 6262082Seschrock /* 6271986Seschrock * Load the vdev state for all toplevel vdevs. 628789Sahrens */ 6291986Seschrock vdev_load(rvd); 630789Sahrens 631789Sahrens /* 632789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 633789Sahrens */ 6341544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 635789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 6361544Seschrock spa_config_exit(spa, FTAG); 637789Sahrens 638789Sahrens /* 639789Sahrens * Check the state of the root vdev. If it can't be opened, it 640789Sahrens * indicates one or more toplevel vdevs are faulted. 641789Sahrens */ 6421544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 6431544Seschrock error = ENXIO; 6441544Seschrock goto out; 6451544Seschrock } 646789Sahrens 6471544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 6481635Sbonwick dmu_tx_t *tx; 6491635Sbonwick int need_update = B_FALSE; 6501585Sbonwick int c; 6511601Sbonwick 6521635Sbonwick /* 6531635Sbonwick * Claim log blocks that haven't been committed yet. 6541635Sbonwick * This must all happen in a single txg. 6551635Sbonwick */ 6561601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 657789Sahrens spa_first_txg(spa)); 6582417Sahrens (void) dmu_objset_find(spa->spa_name, 6592417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 660789Sahrens dmu_tx_commit(tx); 661789Sahrens 662789Sahrens spa->spa_sync_on = B_TRUE; 663789Sahrens txg_sync_start(spa->spa_dsl_pool); 664789Sahrens 665789Sahrens /* 666789Sahrens * Wait for all claims to sync. 667789Sahrens */ 668789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 6691585Sbonwick 6701585Sbonwick /* 6711635Sbonwick * If the config cache is stale, or we have uninitialized 6721635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 6731585Sbonwick */ 6741635Sbonwick if (config_cache_txg != spa->spa_config_txg || 6751635Sbonwick state == SPA_LOAD_IMPORT) 6761635Sbonwick need_update = B_TRUE; 6771635Sbonwick 6781635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 6791635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 6801635Sbonwick need_update = B_TRUE; 6811585Sbonwick 6821585Sbonwick /* 6831635Sbonwick * Update the config cache asychronously in case we're the 6841635Sbonwick * root pool, in which case the config cache isn't writable yet. 6851585Sbonwick */ 6861635Sbonwick if (need_update) 6871635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 688789Sahrens } 689789Sahrens 6901544Seschrock error = 0; 6911544Seschrock out: 6922082Seschrock if (error && error != EBADF) 6931544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 6941544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 6951544Seschrock spa->spa_ena = 0; 6961544Seschrock 6971544Seschrock return (error); 698789Sahrens } 699789Sahrens 700789Sahrens /* 701789Sahrens * Pool Open/Import 702789Sahrens * 703789Sahrens * The import case is identical to an open except that the configuration is sent 704789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 705789Sahrens * case of an open, the pool configuration will exist in the 706789Sahrens * POOL_STATE_UNITIALIZED state. 707789Sahrens * 708789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 709789Sahrens * the same time open the pool, without having to keep around the spa_t in some 710789Sahrens * ambiguous state. 711789Sahrens */ 712789Sahrens static int 713789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 714789Sahrens { 715789Sahrens spa_t *spa; 716789Sahrens int error; 717789Sahrens int loaded = B_FALSE; 718789Sahrens int locked = B_FALSE; 719789Sahrens 720789Sahrens *spapp = NULL; 721789Sahrens 722789Sahrens /* 723789Sahrens * As disgusting as this is, we need to support recursive calls to this 724789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 725789Sahrens * up calling spa_open() again. The real fix is to figure out how to 726789Sahrens * avoid dsl_dir_open() calling this in the first place. 727789Sahrens */ 728789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 729789Sahrens mutex_enter(&spa_namespace_lock); 730789Sahrens locked = B_TRUE; 731789Sahrens } 732789Sahrens 733789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 734789Sahrens if (locked) 735789Sahrens mutex_exit(&spa_namespace_lock); 736789Sahrens return (ENOENT); 737789Sahrens } 738789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 739789Sahrens 740789Sahrens spa_activate(spa); 741789Sahrens 7421635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 743789Sahrens 744789Sahrens if (error == EBADF) { 745789Sahrens /* 7461986Seschrock * If vdev_validate() returns failure (indicated by 7471986Seschrock * EBADF), it indicates that one of the vdevs indicates 7481986Seschrock * that the pool has been exported or destroyed. If 7491986Seschrock * this is the case, the config cache is out of sync and 7501986Seschrock * we should remove the pool from the namespace. 751789Sahrens */ 7522082Seschrock zfs_post_ok(spa, NULL); 753789Sahrens spa_unload(spa); 754789Sahrens spa_deactivate(spa); 755789Sahrens spa_remove(spa); 756789Sahrens spa_config_sync(); 757789Sahrens if (locked) 758789Sahrens mutex_exit(&spa_namespace_lock); 759789Sahrens return (ENOENT); 7601544Seschrock } 7611544Seschrock 7621544Seschrock if (error) { 763789Sahrens /* 764789Sahrens * We can't open the pool, but we still have useful 765789Sahrens * information: the state of each vdev after the 766789Sahrens * attempted vdev_open(). Return this to the user. 767789Sahrens */ 7681635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 7691635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 770789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 771789Sahrens B_TRUE); 7721635Sbonwick spa_config_exit(spa, FTAG); 7731635Sbonwick } 774789Sahrens spa_unload(spa); 775789Sahrens spa_deactivate(spa); 7761544Seschrock spa->spa_last_open_failed = B_TRUE; 777789Sahrens if (locked) 778789Sahrens mutex_exit(&spa_namespace_lock); 779789Sahrens *spapp = NULL; 780789Sahrens return (error); 7811544Seschrock } else { 7821544Seschrock zfs_post_ok(spa, NULL); 7831544Seschrock spa->spa_last_open_failed = B_FALSE; 784789Sahrens } 785789Sahrens 786789Sahrens loaded = B_TRUE; 787789Sahrens } 788789Sahrens 789789Sahrens spa_open_ref(spa, tag); 790789Sahrens if (locked) 791789Sahrens mutex_exit(&spa_namespace_lock); 792789Sahrens 793789Sahrens *spapp = spa; 794789Sahrens 795789Sahrens if (config != NULL) { 7961544Seschrock spa_config_enter(spa, RW_READER, FTAG); 797789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 7981544Seschrock spa_config_exit(spa, FTAG); 799789Sahrens } 800789Sahrens 801789Sahrens /* 802789Sahrens * If we just loaded the pool, resilver anything that's out of date. 803789Sahrens */ 804789Sahrens if (loaded && (spa_mode & FWRITE)) 805789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 806789Sahrens 807789Sahrens return (0); 808789Sahrens } 809789Sahrens 810789Sahrens int 811789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 812789Sahrens { 813789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 814789Sahrens } 815789Sahrens 8161544Seschrock /* 8171544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 8181544Seschrock * preventing it from being exported or destroyed. 8191544Seschrock */ 8201544Seschrock spa_t * 8211544Seschrock spa_inject_addref(char *name) 8221544Seschrock { 8231544Seschrock spa_t *spa; 8241544Seschrock 8251544Seschrock mutex_enter(&spa_namespace_lock); 8261544Seschrock if ((spa = spa_lookup(name)) == NULL) { 8271544Seschrock mutex_exit(&spa_namespace_lock); 8281544Seschrock return (NULL); 8291544Seschrock } 8301544Seschrock spa->spa_inject_ref++; 8311544Seschrock mutex_exit(&spa_namespace_lock); 8321544Seschrock 8331544Seschrock return (spa); 8341544Seschrock } 8351544Seschrock 8361544Seschrock void 8371544Seschrock spa_inject_delref(spa_t *spa) 8381544Seschrock { 8391544Seschrock mutex_enter(&spa_namespace_lock); 8401544Seschrock spa->spa_inject_ref--; 8411544Seschrock mutex_exit(&spa_namespace_lock); 8421544Seschrock } 8431544Seschrock 8442082Seschrock static void 8452082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 8462082Seschrock { 8472082Seschrock nvlist_t **spares; 8482082Seschrock uint_t i, nspares; 8492082Seschrock nvlist_t *nvroot; 8502082Seschrock uint64_t guid; 8512082Seschrock vdev_stat_t *vs; 8522082Seschrock uint_t vsc; 8532082Seschrock 8542082Seschrock if (spa->spa_nspares == 0) 8552082Seschrock return; 8562082Seschrock 8572082Seschrock VERIFY(nvlist_lookup_nvlist(config, 8582082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 8592082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 8602082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 8612082Seschrock if (nspares != 0) { 8622082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 8632082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 8642082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 8652082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 8662082Seschrock 8672082Seschrock /* 8682082Seschrock * Go through and find any spares which have since been 8692082Seschrock * repurposed as an active spare. If this is the case, update 8702082Seschrock * their status appropriately. 8712082Seschrock */ 8722082Seschrock for (i = 0; i < nspares; i++) { 8732082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 8742082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 8752082Seschrock if (spa_spare_inuse(guid)) { 8762082Seschrock VERIFY(nvlist_lookup_uint64_array( 8772082Seschrock spares[i], ZPOOL_CONFIG_STATS, 8782082Seschrock (uint64_t **)&vs, &vsc) == 0); 8792082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 8802082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 8812082Seschrock } 8822082Seschrock } 8832082Seschrock } 8842082Seschrock } 8852082Seschrock 886789Sahrens int 8871544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 888789Sahrens { 889789Sahrens int error; 890789Sahrens spa_t *spa; 891789Sahrens 892789Sahrens *config = NULL; 893789Sahrens error = spa_open_common(name, &spa, FTAG, config); 894789Sahrens 8952082Seschrock if (spa && *config != NULL) { 8961544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 8971544Seschrock spa_get_errlog_size(spa)) == 0); 8981544Seschrock 8992082Seschrock spa_add_spares(spa, *config); 9002082Seschrock } 9012082Seschrock 9021544Seschrock /* 9031544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 9041544Seschrock * and call spa_lookup() directly. 9051544Seschrock */ 9061544Seschrock if (altroot) { 9071544Seschrock if (spa == NULL) { 9081544Seschrock mutex_enter(&spa_namespace_lock); 9091544Seschrock spa = spa_lookup(name); 9101544Seschrock if (spa) 9111544Seschrock spa_altroot(spa, altroot, buflen); 9121544Seschrock else 9131544Seschrock altroot[0] = '\0'; 9141544Seschrock spa = NULL; 9151544Seschrock mutex_exit(&spa_namespace_lock); 9161544Seschrock } else { 9171544Seschrock spa_altroot(spa, altroot, buflen); 9181544Seschrock } 9191544Seschrock } 9201544Seschrock 921789Sahrens if (spa != NULL) 922789Sahrens spa_close(spa, FTAG); 923789Sahrens 924789Sahrens return (error); 925789Sahrens } 926789Sahrens 927789Sahrens /* 9282082Seschrock * Validate that the 'spares' array is well formed. We must have an array of 9292082Seschrock * nvlists, each which describes a valid leaf vdev. 9302082Seschrock */ 9312082Seschrock static int 9322082Seschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 9332082Seschrock { 9342082Seschrock nvlist_t **spares; 9352082Seschrock uint_t i, nspares; 9362082Seschrock vdev_t *vd; 9372082Seschrock int error; 9382082Seschrock 9392082Seschrock /* 9402082Seschrock * It's acceptable to have no spares specified. 9412082Seschrock */ 9422082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 9432082Seschrock &spares, &nspares) != 0) 9442082Seschrock return (0); 9452082Seschrock 9462082Seschrock if (nspares == 0) 9472082Seschrock return (EINVAL); 9482082Seschrock 9492082Seschrock /* 9502082Seschrock * Make sure the pool is formatted with a version that supports hot 9512082Seschrock * spares. 9522082Seschrock */ 9532082Seschrock if (spa_version(spa) < ZFS_VERSION_SPARES) 9542082Seschrock return (ENOTSUP); 9552082Seschrock 9562082Seschrock for (i = 0; i < nspares; i++) { 9572082Seschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 9582082Seschrock mode)) != 0) 9592082Seschrock return (error); 9602082Seschrock 9612082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 9622082Seschrock vdev_free(vd); 9632082Seschrock return (EINVAL); 9642082Seschrock } 9652082Seschrock 9662082Seschrock if ((error = vdev_open(vd)) != 0) { 9672082Seschrock vdev_free(vd); 9682082Seschrock return (error); 9692082Seschrock } 9702082Seschrock 9712082Seschrock vd->vdev_top = vd; 9722082Seschrock if ((error = vdev_label_spare(vd, crtxg)) != 0) { 9732082Seschrock vdev_free(vd); 9742082Seschrock return (error); 9752082Seschrock } 9762082Seschrock 9772082Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 9782082Seschrock vd->vdev_guid) == 0); 9792082Seschrock 9802082Seschrock vdev_free(vd); 9812082Seschrock } 9822082Seschrock 9832082Seschrock return (0); 9842082Seschrock } 9852082Seschrock 9862082Seschrock /* 987789Sahrens * Pool Creation 988789Sahrens */ 989789Sahrens int 9901635Sbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot) 991789Sahrens { 992789Sahrens spa_t *spa; 9931635Sbonwick vdev_t *rvd; 994789Sahrens dsl_pool_t *dp; 995789Sahrens dmu_tx_t *tx; 9962082Seschrock int c, error = 0; 997789Sahrens uint64_t txg = TXG_INITIAL; 9982082Seschrock nvlist_t **spares; 9992082Seschrock uint_t nspares; 1000789Sahrens 1001789Sahrens /* 1002789Sahrens * If this pool already exists, return failure. 1003789Sahrens */ 1004789Sahrens mutex_enter(&spa_namespace_lock); 1005789Sahrens if (spa_lookup(pool) != NULL) { 1006789Sahrens mutex_exit(&spa_namespace_lock); 1007789Sahrens return (EEXIST); 1008789Sahrens } 1009789Sahrens 1010789Sahrens /* 1011789Sahrens * Allocate a new spa_t structure. 1012789Sahrens */ 10131635Sbonwick spa = spa_add(pool, altroot); 1014789Sahrens spa_activate(spa); 1015789Sahrens 1016789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 10171760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 1018789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1019789Sahrens 10201635Sbonwick /* 10211635Sbonwick * Create the root vdev. 10221635Sbonwick */ 10231635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 10241635Sbonwick 10252082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 10262082Seschrock 10272082Seschrock ASSERT(error != 0 || rvd != NULL); 10282082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 10292082Seschrock 10302082Seschrock if (error == 0 && rvd->vdev_children == 0) 10311635Sbonwick error = EINVAL; 10322082Seschrock 10332082Seschrock if (error == 0 && 10342082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 10352082Seschrock (error = spa_validate_spares(spa, nvroot, txg, 10362082Seschrock VDEV_ALLOC_ADD)) == 0) { 10372082Seschrock for (c = 0; c < rvd->vdev_children; c++) 10382082Seschrock vdev_init(rvd->vdev_child[c], txg); 10392082Seschrock vdev_config_dirty(rvd); 10401635Sbonwick } 10411635Sbonwick 10421635Sbonwick spa_config_exit(spa, FTAG); 1043789Sahrens 10442082Seschrock if (error != 0) { 1045789Sahrens spa_unload(spa); 1046789Sahrens spa_deactivate(spa); 1047789Sahrens spa_remove(spa); 1048789Sahrens mutex_exit(&spa_namespace_lock); 1049789Sahrens return (error); 1050789Sahrens } 1051789Sahrens 10522082Seschrock /* 10532082Seschrock * Get the list of spares, if specified. 10542082Seschrock */ 10552082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10562082Seschrock &spares, &nspares) == 0) { 10572082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 10582082Seschrock KM_SLEEP) == 0); 10592082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 10602082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 10612082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 10622082Seschrock spa_load_spares(spa); 10632082Seschrock spa_config_exit(spa, FTAG); 10642082Seschrock spa->spa_sync_spares = B_TRUE; 10652082Seschrock } 10662082Seschrock 1067789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1068789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1069789Sahrens 1070789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1071789Sahrens 1072789Sahrens /* 1073789Sahrens * Create the pool config object. 1074789Sahrens */ 1075789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1076789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1077789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1078789Sahrens 10791544Seschrock if (zap_add(spa->spa_meta_objset, 1080789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 10811544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 10821544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 10831544Seschrock } 1084789Sahrens 10852082Seschrock /* Newly created pools are always deflated. */ 10862082Seschrock spa->spa_deflate = TRUE; 10872082Seschrock if (zap_add(spa->spa_meta_objset, 10882082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 10892082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 10902082Seschrock cmn_err(CE_PANIC, "failed to add deflate"); 10912082Seschrock } 10922082Seschrock 1093789Sahrens /* 1094789Sahrens * Create the deferred-free bplist object. Turn off compression 1095789Sahrens * because sync-to-convergence takes longer if the blocksize 1096789Sahrens * keeps changing. 1097789Sahrens */ 1098789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1099789Sahrens 1 << 14, tx); 1100789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1101789Sahrens ZIO_COMPRESS_OFF, tx); 1102789Sahrens 11031544Seschrock if (zap_add(spa->spa_meta_objset, 1104789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11051544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 11061544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 11071544Seschrock } 1108789Sahrens 1109789Sahrens dmu_tx_commit(tx); 1110789Sahrens 1111789Sahrens spa->spa_sync_on = B_TRUE; 1112789Sahrens txg_sync_start(spa->spa_dsl_pool); 1113789Sahrens 1114789Sahrens /* 1115789Sahrens * We explicitly wait for the first transaction to complete so that our 1116789Sahrens * bean counters are appropriately updated. 1117789Sahrens */ 1118789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1119789Sahrens 1120789Sahrens spa_config_sync(); 1121789Sahrens 1122789Sahrens mutex_exit(&spa_namespace_lock); 1123789Sahrens 1124789Sahrens return (0); 1125789Sahrens } 1126789Sahrens 1127789Sahrens /* 1128789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1129789Sahrens * then call spa_load() to do the dirty work. 1130789Sahrens */ 1131789Sahrens int 11321635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 1133789Sahrens { 1134789Sahrens spa_t *spa; 1135789Sahrens int error; 11362082Seschrock nvlist_t *nvroot; 11372082Seschrock nvlist_t **spares; 11382082Seschrock uint_t nspares; 1139789Sahrens 1140789Sahrens if (!(spa_mode & FWRITE)) 1141789Sahrens return (EROFS); 1142789Sahrens 1143789Sahrens /* 1144789Sahrens * If a pool with this name exists, return failure. 1145789Sahrens */ 1146789Sahrens mutex_enter(&spa_namespace_lock); 1147789Sahrens if (spa_lookup(pool) != NULL) { 1148789Sahrens mutex_exit(&spa_namespace_lock); 1149789Sahrens return (EEXIST); 1150789Sahrens } 1151789Sahrens 1152789Sahrens /* 11531635Sbonwick * Create and initialize the spa structure. 1154789Sahrens */ 11551635Sbonwick spa = spa_add(pool, altroot); 1156789Sahrens spa_activate(spa); 1157789Sahrens 1158789Sahrens /* 11591635Sbonwick * Pass off the heavy lifting to spa_load(). 11601732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 11611732Sbonwick * is actually the one to trust when doing an import. 11621601Sbonwick */ 11631732Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1164789Sahrens 11652082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 11662082Seschrock /* 11672082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 11682082Seschrock * and conflicts with spa_has_spare(). 11692082Seschrock */ 11702082Seschrock if (spa->spa_sparelist) { 11712082Seschrock nvlist_free(spa->spa_sparelist); 11722082Seschrock spa->spa_sparelist = NULL; 11732082Seschrock spa_load_spares(spa); 11742082Seschrock } 11752082Seschrock 11762082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 11772082Seschrock &nvroot) == 0); 11782082Seschrock if (error == 0) 11792082Seschrock error = spa_validate_spares(spa, nvroot, -1ULL, 11802082Seschrock VDEV_ALLOC_SPARE); 11812082Seschrock spa_config_exit(spa, FTAG); 11822082Seschrock 11832082Seschrock if (error != 0) { 1184789Sahrens spa_unload(spa); 1185789Sahrens spa_deactivate(spa); 1186789Sahrens spa_remove(spa); 1187789Sahrens mutex_exit(&spa_namespace_lock); 1188789Sahrens return (error); 1189789Sahrens } 1190789Sahrens 11911635Sbonwick /* 11922082Seschrock * Override any spares as specified by the user, as these may have 11932082Seschrock * correct device names/devids, etc. 11942082Seschrock */ 11952082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 11962082Seschrock &spares, &nspares) == 0) { 11972082Seschrock if (spa->spa_sparelist) 11982082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 11992082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 12002082Seschrock else 12012082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 12022082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 12032082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 12042082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 12052082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12062082Seschrock spa_load_spares(spa); 12072082Seschrock spa_config_exit(spa, FTAG); 12082082Seschrock spa->spa_sync_spares = B_TRUE; 12092082Seschrock } 12102082Seschrock 12112082Seschrock /* 12121635Sbonwick * Update the config cache to include the newly-imported pool. 12131635Sbonwick */ 12141635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 12151635Sbonwick 1216789Sahrens mutex_exit(&spa_namespace_lock); 1217789Sahrens 1218789Sahrens /* 1219789Sahrens * Resilver anything that's out of date. 1220789Sahrens */ 1221789Sahrens if (spa_mode & FWRITE) 1222789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1223789Sahrens 1224789Sahrens return (0); 1225789Sahrens } 1226789Sahrens 1227789Sahrens /* 1228789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1229789Sahrens * to get the vdev stats associated with the imported devices. 1230789Sahrens */ 1231789Sahrens #define TRYIMPORT_NAME "$import" 1232789Sahrens 1233789Sahrens nvlist_t * 1234789Sahrens spa_tryimport(nvlist_t *tryconfig) 1235789Sahrens { 1236789Sahrens nvlist_t *config = NULL; 1237789Sahrens char *poolname; 1238789Sahrens spa_t *spa; 1239789Sahrens uint64_t state; 1240789Sahrens 1241789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1242789Sahrens return (NULL); 1243789Sahrens 1244789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1245789Sahrens return (NULL); 1246789Sahrens 12471635Sbonwick /* 12481635Sbonwick * Create and initialize the spa structure. 12491635Sbonwick */ 1250789Sahrens mutex_enter(&spa_namespace_lock); 12511635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1252789Sahrens spa_activate(spa); 1253789Sahrens 1254789Sahrens /* 12551635Sbonwick * Pass off the heavy lifting to spa_load(). 12561732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 12571732Sbonwick * is actually the one to trust when doing an import. 1258789Sahrens */ 12591732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1260789Sahrens 1261789Sahrens /* 1262789Sahrens * If 'tryconfig' was at least parsable, return the current config. 1263789Sahrens */ 1264789Sahrens if (spa->spa_root_vdev != NULL) { 12651635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1266789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 12671635Sbonwick spa_config_exit(spa, FTAG); 1268789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1269789Sahrens poolname) == 0); 1270789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1271789Sahrens state) == 0); 12722082Seschrock 12732082Seschrock /* 12742082Seschrock * Add the list of hot spares. 12752082Seschrock */ 12762082Seschrock spa_add_spares(spa, config); 1277789Sahrens } 1278789Sahrens 1279789Sahrens spa_unload(spa); 1280789Sahrens spa_deactivate(spa); 1281789Sahrens spa_remove(spa); 1282789Sahrens mutex_exit(&spa_namespace_lock); 1283789Sahrens 1284789Sahrens return (config); 1285789Sahrens } 1286789Sahrens 1287789Sahrens /* 1288789Sahrens * Pool export/destroy 1289789Sahrens * 1290789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1291789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1292789Sahrens * update the pool state and sync all the labels to disk, removing the 1293789Sahrens * configuration from the cache afterwards. 1294789Sahrens */ 1295789Sahrens static int 12961775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1297789Sahrens { 1298789Sahrens spa_t *spa; 1299789Sahrens 13001775Sbillm if (oldconfig) 13011775Sbillm *oldconfig = NULL; 13021775Sbillm 1303789Sahrens if (!(spa_mode & FWRITE)) 1304789Sahrens return (EROFS); 1305789Sahrens 1306789Sahrens mutex_enter(&spa_namespace_lock); 1307789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1308789Sahrens mutex_exit(&spa_namespace_lock); 1309789Sahrens return (ENOENT); 1310789Sahrens } 1311789Sahrens 1312789Sahrens /* 13131544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 13141544Seschrock * reacquire the namespace lock, and see if we can export. 13151544Seschrock */ 13161544Seschrock spa_open_ref(spa, FTAG); 13171544Seschrock mutex_exit(&spa_namespace_lock); 13181544Seschrock spa_async_suspend(spa); 13191544Seschrock mutex_enter(&spa_namespace_lock); 13201544Seschrock spa_close(spa, FTAG); 13211544Seschrock 13221544Seschrock /* 1323789Sahrens * The pool will be in core if it's openable, 1324789Sahrens * in which case we can modify its state. 1325789Sahrens */ 1326789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1327789Sahrens /* 1328789Sahrens * Objsets may be open only because they're dirty, so we 1329789Sahrens * have to force it to sync before checking spa_refcnt. 1330789Sahrens */ 1331789Sahrens spa_scrub_suspend(spa); 1332789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1333789Sahrens 13341544Seschrock /* 13351544Seschrock * A pool cannot be exported or destroyed if there are active 13361544Seschrock * references. If we are resetting a pool, allow references by 13371544Seschrock * fault injection handlers. 13381544Seschrock */ 13391544Seschrock if (!spa_refcount_zero(spa) || 13401544Seschrock (spa->spa_inject_ref != 0 && 13411544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1342789Sahrens spa_scrub_resume(spa); 13431544Seschrock spa_async_resume(spa); 1344789Sahrens mutex_exit(&spa_namespace_lock); 1345789Sahrens return (EBUSY); 1346789Sahrens } 1347789Sahrens 1348789Sahrens spa_scrub_resume(spa); 1349789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1350789Sahrens 1351789Sahrens /* 1352789Sahrens * We want this to be reflected on every label, 1353789Sahrens * so mark them all dirty. spa_unload() will do the 1354789Sahrens * final sync that pushes these changes out. 1355789Sahrens */ 13561544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 13571601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 13581544Seschrock spa->spa_state = new_state; 13591635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 13601544Seschrock vdev_config_dirty(spa->spa_root_vdev); 13611601Sbonwick spa_config_exit(spa, FTAG); 13621544Seschrock } 1363789Sahrens } 1364789Sahrens 1365789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1366789Sahrens spa_unload(spa); 1367789Sahrens spa_deactivate(spa); 1368789Sahrens } 1369789Sahrens 13701775Sbillm if (oldconfig && spa->spa_config) 13711775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 13721775Sbillm 13731544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 13741544Seschrock spa_remove(spa); 13751544Seschrock spa_config_sync(); 13761544Seschrock } 1377789Sahrens mutex_exit(&spa_namespace_lock); 1378789Sahrens 1379789Sahrens return (0); 1380789Sahrens } 1381789Sahrens 1382789Sahrens /* 1383789Sahrens * Destroy a storage pool. 1384789Sahrens */ 1385789Sahrens int 1386789Sahrens spa_destroy(char *pool) 1387789Sahrens { 13881775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1389789Sahrens } 1390789Sahrens 1391789Sahrens /* 1392789Sahrens * Export a storage pool. 1393789Sahrens */ 1394789Sahrens int 13951775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 1396789Sahrens { 13971775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1398789Sahrens } 1399789Sahrens 1400789Sahrens /* 14011544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 14021544Seschrock * from the namespace in any way. 14031544Seschrock */ 14041544Seschrock int 14051544Seschrock spa_reset(char *pool) 14061544Seschrock { 14071775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 14081544Seschrock } 14091544Seschrock 14101544Seschrock 14111544Seschrock /* 1412789Sahrens * ========================================================================== 1413789Sahrens * Device manipulation 1414789Sahrens * ========================================================================== 1415789Sahrens */ 1416789Sahrens 1417789Sahrens /* 1418789Sahrens * Add capacity to a storage pool. 1419789Sahrens */ 1420789Sahrens int 1421789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1422789Sahrens { 1423789Sahrens uint64_t txg; 14241635Sbonwick int c, error; 1425789Sahrens vdev_t *rvd = spa->spa_root_vdev; 14261585Sbonwick vdev_t *vd, *tvd; 14272082Seschrock nvlist_t **spares; 14282082Seschrock uint_t i, nspares; 1429789Sahrens 1430789Sahrens txg = spa_vdev_enter(spa); 1431789Sahrens 14322082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 14332082Seschrock VDEV_ALLOC_ADD)) != 0) 14342082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 14352082Seschrock 14362082Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 14372082Seschrock VDEV_ALLOC_ADD)) != 0) 1438789Sahrens return (spa_vdev_exit(spa, vd, txg, error)); 1439789Sahrens 14402082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 14412082Seschrock &spares, &nspares) != 0) 14422082Seschrock nspares = 0; 14432082Seschrock 14442082Seschrock if (vd->vdev_children == 0 && nspares == 0) 14452082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 14462082Seschrock 14472082Seschrock if (vd->vdev_children != 0) { 14482082Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) 14492082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 14502082Seschrock 14512082Seschrock /* 14522082Seschrock * Transfer each new top-level vdev from vd to rvd. 14532082Seschrock */ 14542082Seschrock for (c = 0; c < vd->vdev_children; c++) { 14552082Seschrock tvd = vd->vdev_child[c]; 14562082Seschrock vdev_remove_child(vd, tvd); 14572082Seschrock tvd->vdev_id = rvd->vdev_children; 14582082Seschrock vdev_add_child(rvd, tvd); 14592082Seschrock vdev_config_dirty(tvd); 14602082Seschrock } 14612082Seschrock } 14622082Seschrock 14632082Seschrock if (nspares != 0) { 14642082Seschrock if (spa->spa_sparelist != NULL) { 14652082Seschrock nvlist_t **oldspares; 14662082Seschrock uint_t oldnspares; 14672082Seschrock nvlist_t **newspares; 14682082Seschrock 14692082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 14702082Seschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 14712082Seschrock 14722082Seschrock newspares = kmem_alloc(sizeof (void *) * 14732082Seschrock (nspares + oldnspares), KM_SLEEP); 14742082Seschrock for (i = 0; i < oldnspares; i++) 14752082Seschrock VERIFY(nvlist_dup(oldspares[i], 14762082Seschrock &newspares[i], KM_SLEEP) == 0); 14772082Seschrock for (i = 0; i < nspares; i++) 14782082Seschrock VERIFY(nvlist_dup(spares[i], 14792082Seschrock &newspares[i + oldnspares], 14802082Seschrock KM_SLEEP) == 0); 14812082Seschrock 14822082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, 14832082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 14842082Seschrock 14852082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 14862082Seschrock ZPOOL_CONFIG_SPARES, newspares, 14872082Seschrock nspares + oldnspares) == 0); 14882082Seschrock for (i = 0; i < oldnspares + nspares; i++) 14892082Seschrock nvlist_free(newspares[i]); 14902082Seschrock kmem_free(newspares, (oldnspares + nspares) * 14912082Seschrock sizeof (void *)); 14922082Seschrock } else { 14932082Seschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 14942082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 14952082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 14962082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 14972082Seschrock } 14982082Seschrock 14992082Seschrock spa_load_spares(spa); 15002082Seschrock spa->spa_sync_spares = B_TRUE; 1501789Sahrens } 1502789Sahrens 1503789Sahrens /* 15041585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 15051585Sbonwick * If other threads start allocating from these vdevs before we 15061585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 15071585Sbonwick * fail to open the pool because there are DVAs that the config cache 15081585Sbonwick * can't translate. Therefore, we first add the vdevs without 15091585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 15101635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 15111585Sbonwick * 15121585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 15131585Sbonwick * if we lose power at any point in this sequence, the remaining 15141585Sbonwick * steps will be completed the next time we load the pool. 1515789Sahrens */ 15161635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 15171585Sbonwick 15181635Sbonwick mutex_enter(&spa_namespace_lock); 15191635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 15201635Sbonwick mutex_exit(&spa_namespace_lock); 1521789Sahrens 15221635Sbonwick return (0); 1523789Sahrens } 1524789Sahrens 1525789Sahrens /* 1526789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1527789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1528789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1529789Sahrens * 1530789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1531789Sahrens * existing device; in this case the two devices are made into their own 1532789Sahrens * mirror using the 'replacing' vdev, which is functionally idendical to 1533789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1534789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1535789Sahrens * completion of resilvering, the first disk (the one being replaced) 1536789Sahrens * is automatically detached. 1537789Sahrens */ 1538789Sahrens int 15391544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1540789Sahrens { 1541789Sahrens uint64_t txg, open_txg; 1542789Sahrens int error; 1543789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1544789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 15452082Seschrock vdev_ops_t *pvops; 1546789Sahrens 1547789Sahrens txg = spa_vdev_enter(spa); 1548789Sahrens 15491544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1550789Sahrens 1551789Sahrens if (oldvd == NULL) 1552789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1553789Sahrens 15541585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 15551585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 15561585Sbonwick 1557789Sahrens pvd = oldvd->vdev_parent; 1558789Sahrens 15592082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 15602082Seschrock VDEV_ALLOC_ADD)) != 0 || newrootvd->vdev_children != 1) 1561789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1562789Sahrens 1563789Sahrens newvd = newrootvd->vdev_child[0]; 1564789Sahrens 1565789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1566789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1567789Sahrens 15682082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1569789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1570789Sahrens 15712082Seschrock if (!replacing) { 15722082Seschrock /* 15732082Seschrock * For attach, the only allowable parent is a mirror or the root 15742082Seschrock * vdev. 15752082Seschrock */ 15762082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 15772082Seschrock pvd->vdev_ops != &vdev_root_ops) 15782082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 15792082Seschrock 15802082Seschrock pvops = &vdev_mirror_ops; 15812082Seschrock } else { 15822082Seschrock /* 15832082Seschrock * Active hot spares can only be replaced by inactive hot 15842082Seschrock * spares. 15852082Seschrock */ 15862082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 15872082Seschrock pvd->vdev_child[1] == oldvd && 15882082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 15892082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 15902082Seschrock 15912082Seschrock /* 15922082Seschrock * If the source is a hot spare, and the parent isn't already a 15932082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 15942082Seschrock * want to create a replacing vdev. 15952082Seschrock */ 15962082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 15972082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 15982082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 15992082Seschrock newvd->vdev_isspare) 16002082Seschrock pvops = &vdev_spare_ops; 16012082Seschrock else 16022082Seschrock pvops = &vdev_replacing_ops; 16032082Seschrock } 16042082Seschrock 16051175Slling /* 16061175Slling * Compare the new device size with the replaceable/attachable 16071175Slling * device size. 16081175Slling */ 16091175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1610789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1611789Sahrens 16121732Sbonwick /* 16131732Sbonwick * The new device cannot have a higher alignment requirement 16141732Sbonwick * than the top-level vdev. 16151732Sbonwick */ 16161732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1617789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1618789Sahrens 1619789Sahrens /* 1620789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1621789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1622789Sahrens */ 1623789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1624789Sahrens spa_strfree(oldvd->vdev_path); 1625789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1626789Sahrens KM_SLEEP); 1627789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1628789Sahrens newvd->vdev_path, "old"); 1629789Sahrens if (oldvd->vdev_devid != NULL) { 1630789Sahrens spa_strfree(oldvd->vdev_devid); 1631789Sahrens oldvd->vdev_devid = NULL; 1632789Sahrens } 1633789Sahrens } 1634789Sahrens 1635789Sahrens /* 16362082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 16372082Seschrock * mirror/replacing/spare vdev above oldvd. 1638789Sahrens */ 1639789Sahrens if (pvd->vdev_ops != pvops) 1640789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1641789Sahrens 1642789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1643789Sahrens ASSERT(pvd->vdev_ops == pvops); 1644789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1645789Sahrens 1646789Sahrens /* 1647789Sahrens * Extract the new device from its root and add it to pvd. 1648789Sahrens */ 1649789Sahrens vdev_remove_child(newrootvd, newvd); 1650789Sahrens newvd->vdev_id = pvd->vdev_children; 1651789Sahrens vdev_add_child(pvd, newvd); 1652789Sahrens 16531544Seschrock /* 16541544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 16551544Seschrock * the addition of newvd may have decreased our parent's asize. 16561544Seschrock */ 16571544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 16581544Seschrock 1659789Sahrens tvd = newvd->vdev_top; 1660789Sahrens ASSERT(pvd->vdev_top == tvd); 1661789Sahrens ASSERT(tvd->vdev_parent == rvd); 1662789Sahrens 1663789Sahrens vdev_config_dirty(tvd); 1664789Sahrens 1665789Sahrens /* 1666789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1667789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1668789Sahrens */ 1669789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1670789Sahrens 1671789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1672789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1673789Sahrens open_txg - TXG_INITIAL + 1); 1674789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1675789Sahrens 16761544Seschrock dprintf("attached %s in txg %llu\n", newvd->vdev_path, txg); 16771544Seschrock 1678789Sahrens /* 1679789Sahrens * Mark newvd's DTL dirty in this txg. 1680789Sahrens */ 16811732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1682789Sahrens 1683789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1684789Sahrens 1685789Sahrens /* 1686789Sahrens * Kick off a resilver to update newvd. 1687789Sahrens */ 1688789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1689789Sahrens 1690789Sahrens return (0); 1691789Sahrens } 1692789Sahrens 1693789Sahrens /* 1694789Sahrens * Detach a device from a mirror or replacing vdev. 1695789Sahrens * If 'replace_done' is specified, only detach if the parent 1696789Sahrens * is a replacing vdev. 1697789Sahrens */ 1698789Sahrens int 16991544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1700789Sahrens { 1701789Sahrens uint64_t txg; 1702789Sahrens int c, t, error; 1703789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1704789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 17052082Seschrock boolean_t unspare = B_FALSE; 17062082Seschrock uint64_t unspare_guid; 1707789Sahrens 1708789Sahrens txg = spa_vdev_enter(spa); 1709789Sahrens 17101544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1711789Sahrens 1712789Sahrens if (vd == NULL) 1713789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1714789Sahrens 17151585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 17161585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17171585Sbonwick 1718789Sahrens pvd = vd->vdev_parent; 1719789Sahrens 1720789Sahrens /* 1721789Sahrens * If replace_done is specified, only remove this device if it's 17222082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 17232082Seschrock * disk can be removed. 1724789Sahrens */ 17252082Seschrock if (replace_done) { 17262082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 17272082Seschrock if (vd->vdev_id != 0) 17282082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17292082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 17302082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 17312082Seschrock } 17322082Seschrock } 17332082Seschrock 17342082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 17352082Seschrock spa_version(spa) >= ZFS_VERSION_SPARES); 1736789Sahrens 1737789Sahrens /* 17382082Seschrock * Only mirror, replacing, and spare vdevs support detach. 1739789Sahrens */ 1740789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 17412082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 17422082Seschrock pvd->vdev_ops != &vdev_spare_ops) 1743789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1744789Sahrens 1745789Sahrens /* 1746789Sahrens * If there's only one replica, you can't detach it. 1747789Sahrens */ 1748789Sahrens if (pvd->vdev_children <= 1) 1749789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1750789Sahrens 1751789Sahrens /* 1752789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1753789Sahrens * valid copy of the data, which means we cannot safely detach it. 1754789Sahrens * 1755789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1756789Sahrens * precise DTL check. 1757789Sahrens */ 1758789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1759789Sahrens uint64_t dirty; 1760789Sahrens 1761789Sahrens cvd = pvd->vdev_child[c]; 1762789Sahrens if (cvd == vd) 1763789Sahrens continue; 1764789Sahrens if (vdev_is_dead(cvd)) 1765789Sahrens continue; 1766789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1767789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1768789Sahrens cvd->vdev_dtl_scrub.sm_space; 1769789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1770789Sahrens if (!dirty) 1771789Sahrens break; 1772789Sahrens } 17732082Seschrock 17742082Seschrock /* 17752082Seschrock * If we are a replacing or spare vdev, then we can always detach the 17762082Seschrock * latter child, as that is how one cancels the operation. 17772082Seschrock */ 17782082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 17792082Seschrock c == pvd->vdev_children) 1780789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1781789Sahrens 1782789Sahrens /* 17832082Seschrock * If we are detaching the original disk from a spare, then it implies 17842082Seschrock * that the spare should become a real disk, and be removed from the 17852082Seschrock * active spare list for the pool. 17862082Seschrock */ 17872082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 17882082Seschrock vd->vdev_id == 0) 17892082Seschrock unspare = B_TRUE; 17902082Seschrock 17912082Seschrock /* 1792789Sahrens * Erase the disk labels so the disk can be used for other things. 1793789Sahrens * This must be done after all other error cases are handled, 1794789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1795789Sahrens * But if we can't do it, don't treat the error as fatal -- 1796789Sahrens * it may be that the unwritability of the disk is the reason 1797789Sahrens * it's being detached! 1798789Sahrens */ 17992082Seschrock error = vdev_label_init(vd, 0, B_FALSE); 1800789Sahrens if (error) 1801789Sahrens dprintf("unable to erase labels on %s\n", vdev_description(vd)); 1802789Sahrens 1803789Sahrens /* 1804789Sahrens * Remove vd from its parent and compact the parent's children. 1805789Sahrens */ 1806789Sahrens vdev_remove_child(pvd, vd); 1807789Sahrens vdev_compact_children(pvd); 1808789Sahrens 1809789Sahrens /* 1810789Sahrens * Remember one of the remaining children so we can get tvd below. 1811789Sahrens */ 1812789Sahrens cvd = pvd->vdev_child[0]; 1813789Sahrens 1814789Sahrens /* 18152082Seschrock * If we need to remove the remaining child from the list of hot spares, 18162082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 18172082Seschrock * must do this before vdev_remove_parent(), because that can change the 18182082Seschrock * GUID if it creates a new toplevel GUID. 18192082Seschrock */ 18202082Seschrock if (unspare) { 18212082Seschrock ASSERT(cvd->vdev_isspare); 18222082Seschrock spa_spare_remove(cvd->vdev_guid); 18232082Seschrock cvd->vdev_isspare = B_FALSE; 18242082Seschrock unspare_guid = cvd->vdev_guid; 18252082Seschrock } 18262082Seschrock 18272082Seschrock /* 1828789Sahrens * If the parent mirror/replacing vdev only has one child, 1829789Sahrens * the parent is no longer needed. Remove it from the tree. 1830789Sahrens */ 1831789Sahrens if (pvd->vdev_children == 1) 1832789Sahrens vdev_remove_parent(cvd); 1833789Sahrens 1834789Sahrens /* 1835789Sahrens * We don't set tvd until now because the parent we just removed 1836789Sahrens * may have been the previous top-level vdev. 1837789Sahrens */ 1838789Sahrens tvd = cvd->vdev_top; 1839789Sahrens ASSERT(tvd->vdev_parent == rvd); 1840789Sahrens 1841789Sahrens /* 1842789Sahrens * Reopen this top-level vdev to reassess health after detach. 1843789Sahrens */ 18441544Seschrock vdev_reopen(tvd); 1845789Sahrens 1846789Sahrens /* 1847789Sahrens * If the device we just detached was smaller than the others, 18481732Sbonwick * it may be possible to add metaslabs (i.e. grow the pool). 18491732Sbonwick * vdev_metaslab_init() can't fail because the existing metaslabs 18501732Sbonwick * are already in core, so there's nothing to read from disk. 1851789Sahrens */ 18521732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 1853789Sahrens 1854789Sahrens vdev_config_dirty(tvd); 1855789Sahrens 1856789Sahrens /* 1857789Sahrens * Mark vd's DTL as dirty in this txg. 1858789Sahrens * vdev_dtl_sync() will see that vd->vdev_detached is set 1859789Sahrens * and free vd's DTL object in syncing context. 1860789Sahrens * But first make sure we're not on any *other* txg's DTL list, 1861789Sahrens * to prevent vd from being accessed after it's freed. 1862789Sahrens */ 1863789Sahrens for (t = 0; t < TXG_SIZE; t++) 1864789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 18651732Sbonwick vd->vdev_detached = B_TRUE; 18661732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 1867789Sahrens 18681544Seschrock dprintf("detached %s in txg %llu\n", vd->vdev_path, txg); 1869789Sahrens 18702082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 18712082Seschrock 18722082Seschrock /* 18732082Seschrock * If we are supposed to remove the given vdev from the list of spares, 18742082Seschrock * iterate over all pools in the system and replace it if it's present. 18752082Seschrock */ 18762082Seschrock if (unspare) { 18772082Seschrock spa = NULL; 18782082Seschrock mutex_enter(&spa_namespace_lock); 18792082Seschrock while ((spa = spa_next(spa)) != NULL) { 18802082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 18812082Seschrock continue; 18822082Seschrock 18832082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 18842082Seschrock } 18852082Seschrock mutex_exit(&spa_namespace_lock); 18862082Seschrock } 18872082Seschrock 18882082Seschrock return (error); 18892082Seschrock } 18902082Seschrock 18912082Seschrock /* 18922082Seschrock * Remove a device from the pool. Currently, this supports removing only hot 18932082Seschrock * spares. 18942082Seschrock */ 18952082Seschrock int 18962082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 18972082Seschrock { 18982082Seschrock vdev_t *vd; 18992082Seschrock nvlist_t **spares, *nv, **newspares; 19002082Seschrock uint_t i, j, nspares; 19012082Seschrock int ret = 0; 19022082Seschrock 19032082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 19042082Seschrock 19052082Seschrock vd = spa_lookup_by_guid(spa, guid); 19062082Seschrock 19072082Seschrock nv = NULL; 19082082Seschrock if (spa->spa_spares != NULL && 19092082Seschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 19102082Seschrock &spares, &nspares) == 0) { 19112082Seschrock for (i = 0; i < nspares; i++) { 19122082Seschrock uint64_t theguid; 19132082Seschrock 19142082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 19152082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 19162082Seschrock if (theguid == guid) { 19172082Seschrock nv = spares[i]; 19182082Seschrock break; 19192082Seschrock } 19202082Seschrock } 19212082Seschrock } 19222082Seschrock 19232082Seschrock /* 19242082Seschrock * We only support removing a hot spare, and only if it's not currently 19252082Seschrock * in use in this pool. 19262082Seschrock */ 19272082Seschrock if (nv == NULL && vd == NULL) { 19282082Seschrock ret = ENOENT; 19292082Seschrock goto out; 19302082Seschrock } 19312082Seschrock 19322082Seschrock if (nv == NULL && vd != NULL) { 19332082Seschrock ret = ENOTSUP; 19342082Seschrock goto out; 19352082Seschrock } 19362082Seschrock 19372082Seschrock if (!unspare && nv != NULL && vd != NULL) { 19382082Seschrock ret = EBUSY; 19392082Seschrock goto out; 19402082Seschrock } 19412082Seschrock 19422082Seschrock if (nspares == 1) { 19432082Seschrock newspares = NULL; 19442082Seschrock } else { 19452082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 19462082Seschrock KM_SLEEP); 19472082Seschrock for (i = 0, j = 0; i < nspares; i++) { 19482082Seschrock if (spares[i] != nv) 19492082Seschrock VERIFY(nvlist_dup(spares[i], 19502082Seschrock &newspares[j++], KM_SLEEP) == 0); 19512082Seschrock } 19522082Seschrock } 19532082Seschrock 19542082Seschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 19552082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 19562082Seschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 19572082Seschrock newspares, nspares - 1) == 0); 19582082Seschrock for (i = 0; i < nspares - 1; i++) 19592082Seschrock nvlist_free(newspares[i]); 19602082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 19612082Seschrock spa_load_spares(spa); 19622082Seschrock spa->spa_sync_spares = B_TRUE; 19632082Seschrock 19642082Seschrock out: 19652082Seschrock spa_config_exit(spa, FTAG); 19662082Seschrock 19672082Seschrock return (ret); 1968789Sahrens } 1969789Sahrens 1970789Sahrens /* 19711544Seschrock * Find any device that's done replacing, so we can detach it. 1972789Sahrens */ 19731544Seschrock static vdev_t * 19741544Seschrock spa_vdev_replace_done_hunt(vdev_t *vd) 1975789Sahrens { 19761544Seschrock vdev_t *newvd, *oldvd; 1977789Sahrens int c; 1978789Sahrens 19791544Seschrock for (c = 0; c < vd->vdev_children; c++) { 19801544Seschrock oldvd = spa_vdev_replace_done_hunt(vd->vdev_child[c]); 19811544Seschrock if (oldvd != NULL) 19821544Seschrock return (oldvd); 19831544Seschrock } 1984789Sahrens 1985789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 19861544Seschrock oldvd = vd->vdev_child[0]; 19871544Seschrock newvd = vd->vdev_child[1]; 1988789Sahrens 19891544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 19901544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 19911544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 19921544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 19931544Seschrock return (oldvd); 19941544Seschrock } 19951544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 19961544Seschrock } 1997789Sahrens 19981544Seschrock return (NULL); 1999789Sahrens } 2000789Sahrens 20011544Seschrock static void 2002789Sahrens spa_vdev_replace_done(spa_t *spa) 2003789Sahrens { 20041544Seschrock vdev_t *vd; 20052082Seschrock vdev_t *pvd; 20061544Seschrock uint64_t guid; 20072082Seschrock uint64_t pguid = 0; 2008789Sahrens 20091544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2010789Sahrens 20111544Seschrock while ((vd = spa_vdev_replace_done_hunt(spa->spa_root_vdev)) != NULL) { 20121544Seschrock guid = vd->vdev_guid; 20132082Seschrock /* 20142082Seschrock * If we have just finished replacing a hot spared device, then 20152082Seschrock * we need to detach the parent's first child (the original hot 20162082Seschrock * spare) as well. 20172082Seschrock */ 20182082Seschrock pvd = vd->vdev_parent; 20192082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 20202082Seschrock pvd->vdev_id == 0) { 20212082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 20222082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 20232082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 20242082Seschrock } 20251544Seschrock spa_config_exit(spa, FTAG); 20261544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 20271544Seschrock return; 20282082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 20292082Seschrock return; 20301544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2031789Sahrens } 2032789Sahrens 20331544Seschrock spa_config_exit(spa, FTAG); 2034789Sahrens } 2035789Sahrens 2036789Sahrens /* 20371354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 20381354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 20391354Seschrock */ 20401354Seschrock int 20411354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 20421354Seschrock { 20431354Seschrock vdev_t *rvd, *vd; 20441354Seschrock uint64_t txg; 20451354Seschrock 20461354Seschrock rvd = spa->spa_root_vdev; 20471354Seschrock 20481354Seschrock txg = spa_vdev_enter(spa); 20491354Seschrock 20502082Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 20512082Seschrock /* 20522082Seschrock * Determine if this is a reference to a hot spare. In that 20532082Seschrock * case, update the path as stored in the spare list. 20542082Seschrock */ 20552082Seschrock nvlist_t **spares; 20562082Seschrock uint_t i, nspares; 20572082Seschrock if (spa->spa_sparelist != NULL) { 20582082Seschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 20592082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20602082Seschrock for (i = 0; i < nspares; i++) { 20612082Seschrock uint64_t theguid; 20622082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 20632082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 20642082Seschrock if (theguid == guid) 20652082Seschrock break; 20662082Seschrock } 20672082Seschrock 20682082Seschrock if (i == nspares) 20692082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 20702082Seschrock 20712082Seschrock VERIFY(nvlist_add_string(spares[i], 20722082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0); 20732082Seschrock spa_load_spares(spa); 20742082Seschrock spa->spa_sync_spares = B_TRUE; 20752082Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 20762082Seschrock } else { 20772082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 20782082Seschrock } 20792082Seschrock } 20801354Seschrock 20811585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 20821585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 20831585Sbonwick 20841354Seschrock spa_strfree(vd->vdev_path); 20851354Seschrock vd->vdev_path = spa_strdup(newpath); 20861354Seschrock 20871354Seschrock vdev_config_dirty(vd->vdev_top); 20881354Seschrock 20891354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 20901354Seschrock } 20911354Seschrock 20921354Seschrock /* 2093789Sahrens * ========================================================================== 2094789Sahrens * SPA Scrubbing 2095789Sahrens * ========================================================================== 2096789Sahrens */ 2097789Sahrens 20981544Seschrock void 20991544Seschrock spa_scrub_throttle(spa_t *spa, int direction) 21001544Seschrock { 21011544Seschrock mutex_enter(&spa->spa_scrub_lock); 21021544Seschrock spa->spa_scrub_throttled += direction; 21031544Seschrock ASSERT(spa->spa_scrub_throttled >= 0); 21041544Seschrock if (spa->spa_scrub_throttled == 0) 21051544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 21061544Seschrock mutex_exit(&spa->spa_scrub_lock); 21071544Seschrock } 2108789Sahrens 2109789Sahrens static void 2110789Sahrens spa_scrub_io_done(zio_t *zio) 2111789Sahrens { 2112789Sahrens spa_t *spa = zio->io_spa; 2113789Sahrens 2114789Sahrens zio_buf_free(zio->io_data, zio->io_size); 2115789Sahrens 2116789Sahrens mutex_enter(&spa->spa_scrub_lock); 21171544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 21181775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2119789Sahrens spa->spa_scrub_errors++; 2120789Sahrens mutex_enter(&vd->vdev_stat_lock); 2121789Sahrens vd->vdev_stat.vs_scrub_errors++; 2122789Sahrens mutex_exit(&vd->vdev_stat_lock); 2123789Sahrens } 21241544Seschrock if (--spa->spa_scrub_inflight == 0) { 21251544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 21261544Seschrock ASSERT(spa->spa_scrub_throttled == 0); 21271544Seschrock } 21281544Seschrock mutex_exit(&spa->spa_scrub_lock); 2129789Sahrens } 2130789Sahrens 2131789Sahrens static void 21321544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 21331544Seschrock zbookmark_t *zb) 2134789Sahrens { 2135789Sahrens size_t size = BP_GET_LSIZE(bp); 2136789Sahrens void *data = zio_buf_alloc(size); 2137789Sahrens 2138789Sahrens mutex_enter(&spa->spa_scrub_lock); 2139789Sahrens spa->spa_scrub_inflight++; 2140789Sahrens mutex_exit(&spa->spa_scrub_lock); 2141789Sahrens 21421544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 21431544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 21441544Seschrock 21451807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 21461544Seschrock 2147789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 21481544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2149789Sahrens } 2150789Sahrens 2151789Sahrens /* ARGSUSED */ 2152789Sahrens static int 2153789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2154789Sahrens { 2155789Sahrens blkptr_t *bp = &bc->bc_blkptr; 21561775Sbillm vdev_t *vd = spa->spa_root_vdev; 21571775Sbillm dva_t *dva = bp->blk_dva; 21581775Sbillm int needs_resilver = B_FALSE; 21591775Sbillm int d; 2160789Sahrens 21611775Sbillm if (bc->bc_errno) { 2162789Sahrens /* 2163789Sahrens * We can't scrub this block, but we can continue to scrub 2164789Sahrens * the rest of the pool. Note the error and move along. 2165789Sahrens */ 2166789Sahrens mutex_enter(&spa->spa_scrub_lock); 2167789Sahrens spa->spa_scrub_errors++; 2168789Sahrens mutex_exit(&spa->spa_scrub_lock); 2169789Sahrens 21701775Sbillm mutex_enter(&vd->vdev_stat_lock); 21711775Sbillm vd->vdev_stat.vs_scrub_errors++; 21721775Sbillm mutex_exit(&vd->vdev_stat_lock); 2173789Sahrens 2174789Sahrens return (ERESTART); 2175789Sahrens } 2176789Sahrens 2177789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2178789Sahrens 21791775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 21801775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 21811775Sbillm 21821775Sbillm ASSERT(vd != NULL); 21831775Sbillm 21841775Sbillm /* 21851775Sbillm * Keep track of how much data we've examined so that 21861775Sbillm * zpool(1M) status can make useful progress reports. 21871775Sbillm */ 21881775Sbillm mutex_enter(&vd->vdev_stat_lock); 21891775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 21901775Sbillm mutex_exit(&vd->vdev_stat_lock); 2191789Sahrens 21921775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 21931775Sbillm if (DVA_GET_GANG(&dva[d])) { 21941775Sbillm /* 21951775Sbillm * Gang members may be spread across multiple 21961775Sbillm * vdevs, so the best we can do is look at the 21971775Sbillm * pool-wide DTL. 21981775Sbillm * XXX -- it would be better to change our 21991775Sbillm * allocation policy to ensure that this can't 22001775Sbillm * happen. 22011775Sbillm */ 22021775Sbillm vd = spa->spa_root_vdev; 22031775Sbillm } 22041775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 22051775Sbillm bp->blk_birth, 1)) 22061775Sbillm needs_resilver = B_TRUE; 2207789Sahrens } 22081775Sbillm } 22091775Sbillm 22101775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2211789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 22121544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 22131775Sbillm else if (needs_resilver) 22141775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 22151775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2216789Sahrens 2217789Sahrens return (0); 2218789Sahrens } 2219789Sahrens 2220789Sahrens static void 2221789Sahrens spa_scrub_thread(spa_t *spa) 2222789Sahrens { 2223789Sahrens callb_cpr_t cprinfo; 2224789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2225789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2226789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2227789Sahrens int error = 0; 2228789Sahrens boolean_t complete; 2229789Sahrens 2230789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2231789Sahrens 2232797Sbonwick /* 2233797Sbonwick * If we're restarting due to a snapshot create/delete, 2234797Sbonwick * wait for that to complete. 2235797Sbonwick */ 2236797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2237797Sbonwick 22381544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 22391544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 22401544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 22411544Seschrock 22421544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 22431544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2244789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2245789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 22461544Seschrock spa_config_exit(spa, FTAG); 2247789Sahrens 2248789Sahrens mutex_enter(&spa->spa_scrub_lock); 2249789Sahrens spa->spa_scrub_errors = 0; 2250789Sahrens spa->spa_scrub_active = 1; 22511544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 22521544Seschrock ASSERT(spa->spa_scrub_throttled == 0); 2253789Sahrens 2254789Sahrens while (!spa->spa_scrub_stop) { 2255789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 22561544Seschrock while (spa->spa_scrub_suspended) { 2257789Sahrens spa->spa_scrub_active = 0; 2258789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2259789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2260789Sahrens spa->spa_scrub_active = 1; 2261789Sahrens } 2262789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2263789Sahrens 2264789Sahrens if (spa->spa_scrub_restart_txg != 0) 2265789Sahrens break; 2266789Sahrens 2267789Sahrens mutex_exit(&spa->spa_scrub_lock); 2268789Sahrens error = traverse_more(th); 2269789Sahrens mutex_enter(&spa->spa_scrub_lock); 2270789Sahrens if (error != EAGAIN) 2271789Sahrens break; 22721544Seschrock 22731544Seschrock while (spa->spa_scrub_throttled > 0) 22741544Seschrock cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2275789Sahrens } 2276789Sahrens 2277789Sahrens while (spa->spa_scrub_inflight) 2278789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2279789Sahrens 22801601Sbonwick spa->spa_scrub_active = 0; 22811601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 22821601Sbonwick 22831601Sbonwick mutex_exit(&spa->spa_scrub_lock); 22841601Sbonwick 22851601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 22861601Sbonwick 22871601Sbonwick mutex_enter(&spa->spa_scrub_lock); 22881601Sbonwick 22891601Sbonwick /* 22901601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 22911601Sbonwick * AND the spa config lock to synchronize with any config changes 22921601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 22931601Sbonwick */ 2294789Sahrens if (spa->spa_scrub_restart_txg != 0) 2295789Sahrens error = ERESTART; 2296789Sahrens 22971544Seschrock if (spa->spa_scrub_stop) 22981544Seschrock error = EINTR; 22991544Seschrock 2300789Sahrens /* 23011544Seschrock * Even if there were uncorrectable errors, we consider the scrub 23021544Seschrock * completed. The downside is that if there is a transient error during 23031544Seschrock * a resilver, we won't resilver the data properly to the target. But 23041544Seschrock * if the damage is permanent (more likely) we will resilver forever, 23051544Seschrock * which isn't really acceptable. Since there is enough information for 23061544Seschrock * the user to know what has failed and why, this seems like a more 23071544Seschrock * tractable approach. 2308789Sahrens */ 23091544Seschrock complete = (error == 0); 2310789Sahrens 23111544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 23121544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2313789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2314789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2315789Sahrens 2316789Sahrens mutex_exit(&spa->spa_scrub_lock); 2317789Sahrens 2318789Sahrens /* 2319789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2320789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2321789Sahrens */ 2322789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2323789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2324789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 23251544Seschrock spa_errlog_rotate(spa); 23261601Sbonwick 23271544Seschrock spa_config_exit(spa, FTAG); 2328789Sahrens 2329789Sahrens mutex_enter(&spa->spa_scrub_lock); 2330789Sahrens 23311544Seschrock /* 23321544Seschrock * We may have finished replacing a device. 23331544Seschrock * Let the async thread assess this and handle the detach. 23341544Seschrock */ 23351544Seschrock spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 2336789Sahrens 2337789Sahrens /* 2338789Sahrens * If we were told to restart, our final act is to start a new scrub. 2339789Sahrens */ 2340789Sahrens if (error == ERESTART) 23411544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 23421544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2343789Sahrens 23441544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 23451544Seschrock spa->spa_scrub_active = 0; 23461544Seschrock spa->spa_scrub_thread = NULL; 23471544Seschrock cv_broadcast(&spa->spa_scrub_cv); 2348789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2349789Sahrens thread_exit(); 2350789Sahrens } 2351789Sahrens 2352789Sahrens void 2353789Sahrens spa_scrub_suspend(spa_t *spa) 2354789Sahrens { 2355789Sahrens mutex_enter(&spa->spa_scrub_lock); 23561544Seschrock spa->spa_scrub_suspended++; 2357789Sahrens while (spa->spa_scrub_active) { 2358789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2359789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2360789Sahrens } 2361789Sahrens while (spa->spa_scrub_inflight) 2362789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2363789Sahrens mutex_exit(&spa->spa_scrub_lock); 2364789Sahrens } 2365789Sahrens 2366789Sahrens void 2367789Sahrens spa_scrub_resume(spa_t *spa) 2368789Sahrens { 2369789Sahrens mutex_enter(&spa->spa_scrub_lock); 23701544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 23711544Seschrock if (--spa->spa_scrub_suspended == 0) 2372789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2373789Sahrens mutex_exit(&spa->spa_scrub_lock); 2374789Sahrens } 2375789Sahrens 2376789Sahrens void 2377789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2378789Sahrens { 2379789Sahrens /* 2380789Sahrens * Something happened (e.g. snapshot create/delete) that means 2381789Sahrens * we must restart any in-progress scrubs. The itinerary will 2382789Sahrens * fix this properly. 2383789Sahrens */ 2384789Sahrens mutex_enter(&spa->spa_scrub_lock); 2385789Sahrens spa->spa_scrub_restart_txg = txg; 2386789Sahrens mutex_exit(&spa->spa_scrub_lock); 2387789Sahrens } 2388789Sahrens 23891544Seschrock int 23901544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2391789Sahrens { 2392789Sahrens space_seg_t *ss; 2393789Sahrens uint64_t mintxg, maxtxg; 2394789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2395789Sahrens 2396789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2397789Sahrens return (ENOTSUP); 2398789Sahrens 23991544Seschrock mutex_enter(&spa->spa_scrub_lock); 24001544Seschrock 2401789Sahrens /* 2402789Sahrens * If there's a scrub or resilver already in progress, stop it. 2403789Sahrens */ 2404789Sahrens while (spa->spa_scrub_thread != NULL) { 2405789Sahrens /* 2406789Sahrens * Don't stop a resilver unless forced. 2407789Sahrens */ 24081544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 24091544Seschrock mutex_exit(&spa->spa_scrub_lock); 2410789Sahrens return (EBUSY); 24111544Seschrock } 2412789Sahrens spa->spa_scrub_stop = 1; 2413789Sahrens cv_broadcast(&spa->spa_scrub_cv); 2414789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2415789Sahrens } 2416789Sahrens 2417789Sahrens /* 2418789Sahrens * Terminate the previous traverse. 2419789Sahrens */ 2420789Sahrens if (spa->spa_scrub_th != NULL) { 2421789Sahrens traverse_fini(spa->spa_scrub_th); 2422789Sahrens spa->spa_scrub_th = NULL; 2423789Sahrens } 2424789Sahrens 24251544Seschrock if (rvd == NULL) { 24261544Seschrock ASSERT(spa->spa_scrub_stop == 0); 24271544Seschrock ASSERT(spa->spa_scrub_type == type); 24281544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 24291544Seschrock mutex_exit(&spa->spa_scrub_lock); 24301544Seschrock return (0); 24311544Seschrock } 2432789Sahrens 2433789Sahrens mintxg = TXG_INITIAL - 1; 2434789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 2435789Sahrens 24361544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 2437789Sahrens 24381544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 24391544Seschrock /* 24401544Seschrock * The pool-wide DTL is empty. 24411732Sbonwick * If this is a resilver, there's nothing to do except 24421732Sbonwick * check whether any in-progress replacements have completed. 24431544Seschrock */ 24441732Sbonwick if (type == POOL_SCRUB_RESILVER) { 24451544Seschrock type = POOL_SCRUB_NONE; 24461732Sbonwick spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 24471732Sbonwick } 24481544Seschrock } else { 24491544Seschrock /* 24501544Seschrock * The pool-wide DTL is non-empty. 24511544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 24521544Seschrock */ 24531544Seschrock if (type == POOL_SCRUB_EVERYTHING) 24541544Seschrock type = POOL_SCRUB_RESILVER; 24551544Seschrock } 2456789Sahrens 24571544Seschrock if (type == POOL_SCRUB_RESILVER) { 2458789Sahrens /* 2459789Sahrens * Determine the resilvering boundaries. 2460789Sahrens * 2461789Sahrens * Note: (mintxg, maxtxg) is an open interval, 2462789Sahrens * i.e. mintxg and maxtxg themselves are not included. 2463789Sahrens * 2464789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2465789Sahrens * so we don't claim to resilver a txg that's still changing. 2466789Sahrens */ 2467789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 24681544Seschrock mintxg = ss->ss_start - 1; 2469789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 24701544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 2471789Sahrens } 2472789Sahrens 24731544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 24741544Seschrock 24751544Seschrock spa->spa_scrub_stop = 0; 24761544Seschrock spa->spa_scrub_type = type; 24771544Seschrock spa->spa_scrub_restart_txg = 0; 24781544Seschrock 24791544Seschrock if (type != POOL_SCRUB_NONE) { 24801544Seschrock spa->spa_scrub_mintxg = mintxg; 2481789Sahrens spa->spa_scrub_maxtxg = maxtxg; 2482789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 24831635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 24841635Sbonwick ZIO_FLAG_CANFAIL); 2485789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2486789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 2487789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2488789Sahrens } 2489789Sahrens 24901544Seschrock mutex_exit(&spa->spa_scrub_lock); 24911544Seschrock 2492789Sahrens return (0); 2493789Sahrens } 2494789Sahrens 24951544Seschrock /* 24961544Seschrock * ========================================================================== 24971544Seschrock * SPA async task processing 24981544Seschrock * ========================================================================== 24991544Seschrock */ 25001544Seschrock 25011544Seschrock static void 25021544Seschrock spa_async_reopen(spa_t *spa) 2503789Sahrens { 25041544Seschrock vdev_t *rvd = spa->spa_root_vdev; 25051544Seschrock vdev_t *tvd; 25061544Seschrock int c; 25071544Seschrock 25081544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 25091544Seschrock 25101544Seschrock for (c = 0; c < rvd->vdev_children; c++) { 25111544Seschrock tvd = rvd->vdev_child[c]; 25121544Seschrock if (tvd->vdev_reopen_wanted) { 25131544Seschrock tvd->vdev_reopen_wanted = 0; 25141544Seschrock vdev_reopen(tvd); 25151544Seschrock } 25161544Seschrock } 2517789Sahrens 25181544Seschrock spa_config_exit(spa, FTAG); 25191544Seschrock } 25201544Seschrock 25211544Seschrock static void 25221544Seschrock spa_async_thread(spa_t *spa) 25231544Seschrock { 25241544Seschrock int tasks; 25251544Seschrock 25261544Seschrock ASSERT(spa->spa_sync_on); 2527789Sahrens 25281544Seschrock mutex_enter(&spa->spa_async_lock); 25291544Seschrock tasks = spa->spa_async_tasks; 25301544Seschrock spa->spa_async_tasks = 0; 25311544Seschrock mutex_exit(&spa->spa_async_lock); 25321544Seschrock 25331544Seschrock /* 25341635Sbonwick * See if the config needs to be updated. 25351635Sbonwick */ 25361635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 25371635Sbonwick mutex_enter(&spa_namespace_lock); 25381635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 25391635Sbonwick mutex_exit(&spa_namespace_lock); 25401635Sbonwick } 25411635Sbonwick 25421635Sbonwick /* 25431544Seschrock * See if any devices need to be reopened. 25441544Seschrock */ 25451544Seschrock if (tasks & SPA_ASYNC_REOPEN) 25461544Seschrock spa_async_reopen(spa); 25471544Seschrock 25481544Seschrock /* 25491544Seschrock * If any devices are done replacing, detach them. 25501544Seschrock */ 25511544Seschrock if (tasks & SPA_ASYNC_REPLACE_DONE) 2552789Sahrens spa_vdev_replace_done(spa); 2553789Sahrens 25541544Seschrock /* 25551544Seschrock * Kick off a scrub. 25561544Seschrock */ 25571544Seschrock if (tasks & SPA_ASYNC_SCRUB) 25581544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 25591544Seschrock 25601544Seschrock /* 25611544Seschrock * Kick off a resilver. 25621544Seschrock */ 25631544Seschrock if (tasks & SPA_ASYNC_RESILVER) 25641544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 25651544Seschrock 25661544Seschrock /* 25671544Seschrock * Let the world know that we're done. 25681544Seschrock */ 25691544Seschrock mutex_enter(&spa->spa_async_lock); 25701544Seschrock spa->spa_async_thread = NULL; 25711544Seschrock cv_broadcast(&spa->spa_async_cv); 25721544Seschrock mutex_exit(&spa->spa_async_lock); 25731544Seschrock thread_exit(); 25741544Seschrock } 25751544Seschrock 25761544Seschrock void 25771544Seschrock spa_async_suspend(spa_t *spa) 25781544Seschrock { 25791544Seschrock mutex_enter(&spa->spa_async_lock); 25801544Seschrock spa->spa_async_suspended++; 25811544Seschrock while (spa->spa_async_thread != NULL) 25821544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 25831544Seschrock mutex_exit(&spa->spa_async_lock); 25841544Seschrock } 25851544Seschrock 25861544Seschrock void 25871544Seschrock spa_async_resume(spa_t *spa) 25881544Seschrock { 25891544Seschrock mutex_enter(&spa->spa_async_lock); 25901544Seschrock ASSERT(spa->spa_async_suspended != 0); 25911544Seschrock spa->spa_async_suspended--; 25921544Seschrock mutex_exit(&spa->spa_async_lock); 25931544Seschrock } 25941544Seschrock 25951544Seschrock static void 25961544Seschrock spa_async_dispatch(spa_t *spa) 25971544Seschrock { 25981544Seschrock mutex_enter(&spa->spa_async_lock); 25991544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 26001635Sbonwick spa->spa_async_thread == NULL && 26011635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 26021544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 26031544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 26041544Seschrock mutex_exit(&spa->spa_async_lock); 26051544Seschrock } 26061544Seschrock 26071544Seschrock void 26081544Seschrock spa_async_request(spa_t *spa, int task) 26091544Seschrock { 26101544Seschrock mutex_enter(&spa->spa_async_lock); 26111544Seschrock spa->spa_async_tasks |= task; 26121544Seschrock mutex_exit(&spa->spa_async_lock); 2613789Sahrens } 2614789Sahrens 2615789Sahrens /* 2616789Sahrens * ========================================================================== 2617789Sahrens * SPA syncing routines 2618789Sahrens * ========================================================================== 2619789Sahrens */ 2620789Sahrens 2621789Sahrens static void 2622789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2623789Sahrens { 2624789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2625789Sahrens dmu_tx_t *tx; 2626789Sahrens blkptr_t blk; 2627789Sahrens uint64_t itor = 0; 2628789Sahrens zio_t *zio; 2629789Sahrens int error; 2630789Sahrens uint8_t c = 1; 2631789Sahrens 2632789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2633789Sahrens 2634789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2635789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2636789Sahrens 2637789Sahrens error = zio_wait(zio); 2638789Sahrens ASSERT3U(error, ==, 0); 2639789Sahrens 2640789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2641789Sahrens bplist_vacate(bpl, tx); 2642789Sahrens 2643789Sahrens /* 2644789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2645789Sahrens * (Usually only the first block is needed.) 2646789Sahrens */ 2647789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2648789Sahrens dmu_tx_commit(tx); 2649789Sahrens } 2650789Sahrens 2651789Sahrens static void 26522082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 26532082Seschrock { 26542082Seschrock char *packed = NULL; 26552082Seschrock size_t nvsize = 0; 26562082Seschrock dmu_buf_t *db; 26572082Seschrock 26582082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 26592082Seschrock 26602082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 26612082Seschrock 26622082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 26632082Seschrock KM_SLEEP) == 0); 26642082Seschrock 26652082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 26662082Seschrock 26672082Seschrock kmem_free(packed, nvsize); 26682082Seschrock 26692082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 26702082Seschrock dmu_buf_will_dirty(db, tx); 26712082Seschrock *(uint64_t *)db->db_data = nvsize; 26722082Seschrock dmu_buf_rele(db, FTAG); 26732082Seschrock } 26742082Seschrock 26752082Seschrock static void 26762082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 26772082Seschrock { 26782082Seschrock nvlist_t *nvroot; 26792082Seschrock nvlist_t **spares; 26802082Seschrock int i; 26812082Seschrock 26822082Seschrock if (!spa->spa_sync_spares) 26832082Seschrock return; 26842082Seschrock 26852082Seschrock /* 26862082Seschrock * Update the MOS nvlist describing the list of available spares. 26872082Seschrock * spa_validate_spares() will have already made sure this nvlist is 26882082Seschrock * valid and the vdevs are labelled appropriately. 26892082Seschrock */ 26902082Seschrock if (spa->spa_spares_object == 0) { 26912082Seschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 26922082Seschrock DMU_OT_PACKED_NVLIST, 1 << 14, 26932082Seschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 26942082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 26952082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 26962082Seschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 26972082Seschrock } 26982082Seschrock 26992082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 27002082Seschrock if (spa->spa_nspares == 0) { 27012082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 27022082Seschrock NULL, 0) == 0); 27032082Seschrock } else { 27042082Seschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 27052082Seschrock KM_SLEEP); 27062082Seschrock for (i = 0; i < spa->spa_nspares; i++) 27072082Seschrock spares[i] = vdev_config_generate(spa, 27082082Seschrock spa->spa_spares[i], B_FALSE, B_TRUE); 27092082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 27102082Seschrock spares, spa->spa_nspares) == 0); 27112082Seschrock for (i = 0; i < spa->spa_nspares; i++) 27122082Seschrock nvlist_free(spares[i]); 27132082Seschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 27142082Seschrock } 27152082Seschrock 27162082Seschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 27172082Seschrock 27182082Seschrock spa->spa_sync_spares = B_FALSE; 27192082Seschrock } 27202082Seschrock 27212082Seschrock static void 2722789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2723789Sahrens { 2724789Sahrens nvlist_t *config; 2725789Sahrens 2726789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2727789Sahrens return; 2728789Sahrens 2729789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2730789Sahrens 27311635Sbonwick if (spa->spa_config_syncing) 27321635Sbonwick nvlist_free(spa->spa_config_syncing); 27331635Sbonwick spa->spa_config_syncing = config; 2734789Sahrens 27352082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2736789Sahrens } 2737789Sahrens 2738789Sahrens /* 2739789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 2740789Sahrens * part of the process, so we iterate until it converges. 2741789Sahrens */ 2742789Sahrens void 2743789Sahrens spa_sync(spa_t *spa, uint64_t txg) 2744789Sahrens { 2745789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 2746789Sahrens objset_t *mos = spa->spa_meta_objset; 2747789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 27481635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 2749789Sahrens vdev_t *vd; 2750789Sahrens dmu_tx_t *tx; 2751789Sahrens int dirty_vdevs; 2752789Sahrens 2753789Sahrens /* 2754789Sahrens * Lock out configuration changes. 2755789Sahrens */ 27561544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2757789Sahrens 2758789Sahrens spa->spa_syncing_txg = txg; 2759789Sahrens spa->spa_sync_pass = 0; 2760789Sahrens 27611544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 2762789Sahrens 27632082Seschrock tx = dmu_tx_create_assigned(dp, txg); 27642082Seschrock 27652082Seschrock /* 27662082Seschrock * If we are upgrading to ZFS_VERSION_RAIDZ_DEFLATE this txg, 27672082Seschrock * set spa_deflate if we have no raid-z vdevs. 27682082Seschrock */ 27692082Seschrock if (spa->spa_ubsync.ub_version < ZFS_VERSION_RAIDZ_DEFLATE && 27702082Seschrock spa->spa_uberblock.ub_version >= ZFS_VERSION_RAIDZ_DEFLATE) { 27712082Seschrock int i; 27722082Seschrock 27732082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 27742082Seschrock vd = rvd->vdev_child[i]; 27752082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 27762082Seschrock break; 27772082Seschrock } 27782082Seschrock if (i == rvd->vdev_children) { 27792082Seschrock spa->spa_deflate = TRUE; 27802082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 27812082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 27822082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 27832082Seschrock } 27842082Seschrock } 27852082Seschrock 2786789Sahrens /* 2787789Sahrens * If anything has changed in this txg, push the deferred frees 2788789Sahrens * from the previous txg. If not, leave them alone so that we 2789789Sahrens * don't generate work on an otherwise idle system. 2790789Sahrens */ 2791789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 27922329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 27932329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 2794789Sahrens spa_sync_deferred_frees(spa, txg); 2795789Sahrens 2796789Sahrens /* 2797789Sahrens * Iterate to convergence. 2798789Sahrens */ 2799789Sahrens do { 2800789Sahrens spa->spa_sync_pass++; 2801789Sahrens 2802789Sahrens spa_sync_config_object(spa, tx); 28032082Seschrock spa_sync_spares(spa, tx); 28041544Seschrock spa_errlog_sync(spa, txg); 2805789Sahrens dsl_pool_sync(dp, txg); 2806789Sahrens 2807789Sahrens dirty_vdevs = 0; 2808789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 2809789Sahrens vdev_sync(vd, txg); 2810789Sahrens dirty_vdevs++; 2811789Sahrens } 2812789Sahrens 2813789Sahrens bplist_sync(bpl, tx); 2814789Sahrens } while (dirty_vdevs); 2815789Sahrens 2816789Sahrens bplist_close(bpl); 2817789Sahrens 2818789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 2819789Sahrens 2820789Sahrens /* 2821789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 2822789Sahrens * to commit the transaction group. 28231635Sbonwick * 28241635Sbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 28251635Sbonwick * Otherwise, pick a random top-level vdev that's known to be 28261635Sbonwick * visible in the config cache (see spa_vdev_add() for details). 28271635Sbonwick * If the write fails, try the next vdev until we're tried them all. 2828789Sahrens */ 28291635Sbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 28301635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 28311635Sbonwick } else { 28321635Sbonwick int children = rvd->vdev_children; 28331635Sbonwick int c0 = spa_get_random(children); 28341635Sbonwick int c; 28351635Sbonwick 28361635Sbonwick for (c = 0; c < children; c++) { 28371635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 28381635Sbonwick if (vd->vdev_ms_array == 0) 28391635Sbonwick continue; 28401635Sbonwick if (vdev_config_sync(vd, txg) == 0) 28411635Sbonwick break; 28421635Sbonwick } 28431635Sbonwick if (c == children) 28441635Sbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 28451635Sbonwick } 28461635Sbonwick 28472082Seschrock dmu_tx_commit(tx); 28482082Seschrock 28491635Sbonwick /* 28501635Sbonwick * Clear the dirty config list. 28511635Sbonwick */ 28521635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 28531635Sbonwick vdev_config_clean(vd); 28541635Sbonwick 28551635Sbonwick /* 28561635Sbonwick * Now that the new config has synced transactionally, 28571635Sbonwick * let it become visible to the config cache. 28581635Sbonwick */ 28591635Sbonwick if (spa->spa_config_syncing != NULL) { 28601635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 28611635Sbonwick spa->spa_config_txg = txg; 28621635Sbonwick spa->spa_config_syncing = NULL; 28631635Sbonwick } 2864789Sahrens 2865789Sahrens /* 2866789Sahrens * Make a stable copy of the fully synced uberblock. 2867789Sahrens * We use this as the root for pool traversals. 2868789Sahrens */ 2869789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 2870789Sahrens 2871789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 2872789Sahrens 2873789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 2874789Sahrens spa->spa_traverse_wanted = 0; 2875789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2876789Sahrens rw_exit(&spa->spa_traverse_lock); 2877789Sahrens 2878789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 2879789Sahrens 2880789Sahrens /* 2881789Sahrens * Clean up the ZIL records for the synced txg. 2882789Sahrens */ 2883789Sahrens dsl_pool_zil_clean(dp); 2884789Sahrens 2885789Sahrens /* 2886789Sahrens * Update usable space statistics. 2887789Sahrens */ 2888789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 2889789Sahrens vdev_sync_done(vd, txg); 2890789Sahrens 2891789Sahrens /* 2892789Sahrens * It had better be the case that we didn't dirty anything 28932082Seschrock * since vdev_config_sync(). 2894789Sahrens */ 2895789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 2896789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 2897789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 2898789Sahrens ASSERT(bpl->bpl_queue == NULL); 2899789Sahrens 29001544Seschrock spa_config_exit(spa, FTAG); 29011544Seschrock 29021544Seschrock /* 29031544Seschrock * If any async tasks have been requested, kick them off. 29041544Seschrock */ 29051544Seschrock spa_async_dispatch(spa); 2906789Sahrens } 2907789Sahrens 2908789Sahrens /* 2909789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 2910789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 2911789Sahrens * sync. 2912789Sahrens */ 2913789Sahrens void 2914789Sahrens spa_sync_allpools(void) 2915789Sahrens { 2916789Sahrens spa_t *spa = NULL; 2917789Sahrens mutex_enter(&spa_namespace_lock); 2918789Sahrens while ((spa = spa_next(spa)) != NULL) { 2919789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 2920789Sahrens continue; 2921789Sahrens spa_open_ref(spa, FTAG); 2922789Sahrens mutex_exit(&spa_namespace_lock); 2923789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 2924789Sahrens mutex_enter(&spa_namespace_lock); 2925789Sahrens spa_close(spa, FTAG); 2926789Sahrens } 2927789Sahrens mutex_exit(&spa_namespace_lock); 2928789Sahrens } 2929789Sahrens 2930789Sahrens /* 2931789Sahrens * ========================================================================== 2932789Sahrens * Miscellaneous routines 2933789Sahrens * ========================================================================== 2934789Sahrens */ 2935789Sahrens 2936789Sahrens /* 2937789Sahrens * Remove all pools in the system. 2938789Sahrens */ 2939789Sahrens void 2940789Sahrens spa_evict_all(void) 2941789Sahrens { 2942789Sahrens spa_t *spa; 2943789Sahrens 2944789Sahrens /* 2945789Sahrens * Remove all cached state. All pools should be closed now, 2946789Sahrens * so every spa in the AVL tree should be unreferenced. 2947789Sahrens */ 2948789Sahrens mutex_enter(&spa_namespace_lock); 2949789Sahrens while ((spa = spa_next(NULL)) != NULL) { 2950789Sahrens /* 29511544Seschrock * Stop async tasks. The async thread may need to detach 29521544Seschrock * a device that's been replaced, which requires grabbing 29531544Seschrock * spa_namespace_lock, so we must drop it here. 2954789Sahrens */ 2955789Sahrens spa_open_ref(spa, FTAG); 2956789Sahrens mutex_exit(&spa_namespace_lock); 29571544Seschrock spa_async_suspend(spa); 2958789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2959789Sahrens mutex_enter(&spa_namespace_lock); 2960789Sahrens spa_close(spa, FTAG); 2961789Sahrens 2962789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2963789Sahrens spa_unload(spa); 2964789Sahrens spa_deactivate(spa); 2965789Sahrens } 2966789Sahrens spa_remove(spa); 2967789Sahrens } 2968789Sahrens mutex_exit(&spa_namespace_lock); 2969789Sahrens } 29701544Seschrock 29711544Seschrock vdev_t * 29721544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 29731544Seschrock { 29741544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 29751544Seschrock } 29761760Seschrock 29771760Seschrock void 29781760Seschrock spa_upgrade(spa_t *spa) 29791760Seschrock { 29801760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 29811760Seschrock 29821760Seschrock /* 29831760Seschrock * This should only be called for a non-faulted pool, and since a 29841760Seschrock * future version would result in an unopenable pool, this shouldn't be 29851760Seschrock * possible. 29861760Seschrock */ 29871760Seschrock ASSERT(spa->spa_uberblock.ub_version <= ZFS_VERSION); 29881760Seschrock 29891760Seschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 29901760Seschrock vdev_config_dirty(spa->spa_root_vdev); 29911760Seschrock 29921760Seschrock spa_config_exit(spa, FTAG); 29932082Seschrock 29942082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 29951760Seschrock } 29962082Seschrock 29972082Seschrock boolean_t 29982082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 29992082Seschrock { 30002082Seschrock int i; 30012082Seschrock 30022082Seschrock for (i = 0; i < spa->spa_nspares; i++) 30032082Seschrock if (spa->spa_spares[i]->vdev_guid == guid) 30042082Seschrock return (B_TRUE); 30052082Seschrock 30062082Seschrock return (B_FALSE); 30072082Seschrock } 3008