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 */ 21789Sahrens /* 221354Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens /* 29789Sahrens * This file contains all the routines used when modifying on-disk SPA state. 30789Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 31789Sahrens * pool. 32789Sahrens */ 33789Sahrens 34789Sahrens #include <sys/zfs_context.h> 351544Seschrock #include <sys/fm/fs/zfs.h> 36789Sahrens #include <sys/spa_impl.h> 37789Sahrens #include <sys/zio.h> 38789Sahrens #include <sys/zio_checksum.h> 39789Sahrens #include <sys/zio_compress.h> 40789Sahrens #include <sys/dmu.h> 41789Sahrens #include <sys/dmu_tx.h> 42789Sahrens #include <sys/zap.h> 43789Sahrens #include <sys/zil.h> 44789Sahrens #include <sys/vdev_impl.h> 45789Sahrens #include <sys/metaslab.h> 46789Sahrens #include <sys/uberblock_impl.h> 47789Sahrens #include <sys/txg.h> 48789Sahrens #include <sys/avl.h> 49789Sahrens #include <sys/dmu_traverse.h> 50789Sahrens #include <sys/unique.h> 51789Sahrens #include <sys/dsl_pool.h> 52789Sahrens #include <sys/dsl_dir.h> 53789Sahrens #include <sys/dsl_prop.h> 54789Sahrens #include <sys/fs/zfs.h> 55789Sahrens #include <sys/callb.h> 56789Sahrens 57789Sahrens static uint32_t spa_active_count; 58789Sahrens 59789Sahrens /* 60789Sahrens * ========================================================================== 61789Sahrens * SPA state manipulation (open/create/destroy/import/export) 62789Sahrens * ========================================================================== 63789Sahrens */ 64789Sahrens 651544Seschrock static int 661544Seschrock spa_error_entry_compare(const void *a, const void *b) 671544Seschrock { 681544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 691544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 701544Seschrock int ret; 711544Seschrock 721544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 731544Seschrock sizeof (zbookmark_t)); 741544Seschrock 751544Seschrock if (ret < 0) 761544Seschrock return (-1); 771544Seschrock else if (ret > 0) 781544Seschrock return (1); 791544Seschrock else 801544Seschrock return (0); 811544Seschrock } 821544Seschrock 831544Seschrock /* 841544Seschrock * Utility function which retrieves copies of the current logs and 851544Seschrock * re-initializes them in the process. 861544Seschrock */ 871544Seschrock void 881544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 891544Seschrock { 901544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 911544Seschrock 921544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 931544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 941544Seschrock 951544Seschrock avl_create(&spa->spa_errlist_scrub, 961544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 971544Seschrock offsetof(spa_error_entry_t, se_avl)); 981544Seschrock avl_create(&spa->spa_errlist_last, 991544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1001544Seschrock offsetof(spa_error_entry_t, se_avl)); 1011544Seschrock } 1021544Seschrock 103789Sahrens /* 104789Sahrens * Activate an uninitialized pool. 105789Sahrens */ 106789Sahrens static void 107789Sahrens spa_activate(spa_t *spa) 108789Sahrens { 109789Sahrens int t; 110789Sahrens 111789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 112789Sahrens 113789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 114789Sahrens 115789Sahrens spa->spa_normal_class = metaslab_class_create(); 116789Sahrens 117789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 118789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 119789Sahrens 8, maxclsyspri, 50, INT_MAX, 120789Sahrens TASKQ_PREPOPULATE); 121789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 122789Sahrens 8, maxclsyspri, 50, INT_MAX, 123789Sahrens TASKQ_PREPOPULATE); 124789Sahrens } 125789Sahrens 126789Sahrens rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 127789Sahrens 128789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 129789Sahrens offsetof(vdev_t, vdev_dirty_node)); 130789Sahrens 131789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 132789Sahrens offsetof(struct vdev, vdev_txg_node)); 1331544Seschrock 1341544Seschrock avl_create(&spa->spa_errlist_scrub, 1351544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1361544Seschrock offsetof(spa_error_entry_t, se_avl)); 1371544Seschrock avl_create(&spa->spa_errlist_last, 1381544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1391544Seschrock offsetof(spa_error_entry_t, se_avl)); 140789Sahrens } 141789Sahrens 142789Sahrens /* 143789Sahrens * Opposite of spa_activate(). 144789Sahrens */ 145789Sahrens static void 146789Sahrens spa_deactivate(spa_t *spa) 147789Sahrens { 148789Sahrens int t; 149789Sahrens 150789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 151789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 152789Sahrens ASSERT(spa->spa_root_vdev == NULL); 153789Sahrens 154789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 155789Sahrens 156789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 157789Sahrens 158789Sahrens list_destroy(&spa->spa_dirty_list); 159789Sahrens 160789Sahrens rw_destroy(&spa->spa_traverse_lock); 161789Sahrens 162789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 163789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 164789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 165789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 166789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 167789Sahrens } 168789Sahrens 169789Sahrens metaslab_class_destroy(spa->spa_normal_class); 170789Sahrens spa->spa_normal_class = NULL; 171789Sahrens 1721544Seschrock /* 1731544Seschrock * If this was part of an import or the open otherwise failed, we may 1741544Seschrock * still have errors left in the queues. Empty them just in case. 1751544Seschrock */ 1761544Seschrock spa_errlog_drain(spa); 1771544Seschrock 1781544Seschrock avl_destroy(&spa->spa_errlist_scrub); 1791544Seschrock avl_destroy(&spa->spa_errlist_last); 1801544Seschrock 181789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 182789Sahrens } 183789Sahrens 184789Sahrens /* 185789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 186789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 187789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 188789Sahrens * All vdev validation is done by the vdev_alloc() routine. 189789Sahrens */ 190789Sahrens static vdev_t * 191789Sahrens spa_config_parse(spa_t *spa, nvlist_t *nv, vdev_t *parent, uint_t id, int atype) 192789Sahrens { 193789Sahrens nvlist_t **child; 194789Sahrens uint_t c, children; 195789Sahrens vdev_t *vd; 196789Sahrens 197789Sahrens if ((vd = vdev_alloc(spa, nv, parent, id, atype)) == NULL) 198789Sahrens return (NULL); 199789Sahrens 200789Sahrens if (vd->vdev_ops->vdev_op_leaf) 201789Sahrens return (vd); 202789Sahrens 203789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 204789Sahrens &child, &children) != 0) { 205789Sahrens vdev_free(vd); 206789Sahrens return (NULL); 207789Sahrens } 208789Sahrens 209789Sahrens for (c = 0; c < children; c++) { 210789Sahrens if (spa_config_parse(spa, child[c], vd, c, atype) == NULL) { 211789Sahrens vdev_free(vd); 212789Sahrens return (NULL); 213789Sahrens } 214789Sahrens } 215789Sahrens 216789Sahrens return (vd); 217789Sahrens } 218789Sahrens 219789Sahrens /* 220789Sahrens * Opposite of spa_load(). 221789Sahrens */ 222789Sahrens static void 223789Sahrens spa_unload(spa_t *spa) 224789Sahrens { 225789Sahrens /* 2261544Seschrock * Stop async tasks. 2271544Seschrock */ 2281544Seschrock spa_async_suspend(spa); 2291544Seschrock 2301544Seschrock /* 231789Sahrens * Stop syncing. 232789Sahrens */ 233789Sahrens if (spa->spa_sync_on) { 234789Sahrens txg_sync_stop(spa->spa_dsl_pool); 235789Sahrens spa->spa_sync_on = B_FALSE; 236789Sahrens } 237789Sahrens 238789Sahrens /* 239789Sahrens * Wait for any outstanding prefetch I/O to complete. 240789Sahrens */ 2411544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2421544Seschrock spa_config_exit(spa, FTAG); 243789Sahrens 244789Sahrens /* 245789Sahrens * Close the dsl pool. 246789Sahrens */ 247789Sahrens if (spa->spa_dsl_pool) { 248789Sahrens dsl_pool_close(spa->spa_dsl_pool); 249789Sahrens spa->spa_dsl_pool = NULL; 250789Sahrens } 251789Sahrens 252789Sahrens /* 253789Sahrens * Close all vdevs. 254789Sahrens */ 255*1585Sbonwick if (spa->spa_root_vdev) 256789Sahrens vdev_free(spa->spa_root_vdev); 257*1585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 2581544Seschrock 2591544Seschrock spa->spa_async_suspended = 0; 260789Sahrens } 261789Sahrens 262789Sahrens /* 263789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2641544Seschrock * source of configuration information. 265789Sahrens */ 266789Sahrens static int 2671544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 268789Sahrens { 269789Sahrens int error = 0; 270*1585Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 271789Sahrens nvlist_t *nvroot = NULL; 272789Sahrens vdev_t *rvd; 273789Sahrens uberblock_t *ub = &spa->spa_uberblock; 274789Sahrens uint64_t pool_guid; 275789Sahrens zio_t *zio; 276789Sahrens 2771544Seschrock spa->spa_load_state = state; 278789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 2791544Seschrock nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 2801544Seschrock error = EINVAL; 2811544Seschrock goto out; 2821544Seschrock } 283789Sahrens 284789Sahrens (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 285789Sahrens &spa->spa_config_txg); 286789Sahrens 2871544Seschrock if ((spa->spa_load_state == SPA_LOAD_IMPORT || 2881544Seschrock spa->spa_load_state == SPA_LOAD_TRYIMPORT) && 2891544Seschrock spa_guid_exists(pool_guid, 0)) { 2901544Seschrock error = EEXIST; 2911544Seschrock goto out; 2921544Seschrock } 293789Sahrens 294789Sahrens /* 295789Sahrens * Parse the configuration into a vdev tree. 296789Sahrens */ 2971544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 298789Sahrens rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 2991544Seschrock spa_config_exit(spa, FTAG); 300789Sahrens 3011544Seschrock if (rvd == NULL) { 3021544Seschrock error = EINVAL; 3031544Seschrock goto out; 3041544Seschrock } 305789Sahrens 306*1585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 307789Sahrens ASSERT(spa_guid(spa) == pool_guid); 308789Sahrens 309789Sahrens /* 310789Sahrens * Try to open all vdevs, loading each label in the process. 311789Sahrens */ 3121544Seschrock if (vdev_open(rvd) != 0) { 3131544Seschrock error = ENXIO; 3141544Seschrock goto out; 3151544Seschrock } 316789Sahrens 317789Sahrens /* 318789Sahrens * Find the best uberblock. 319789Sahrens */ 320789Sahrens bzero(ub, sizeof (uberblock_t)); 321789Sahrens 322789Sahrens zio = zio_root(spa, NULL, NULL, 323789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 324789Sahrens vdev_uberblock_load(zio, rvd, ub); 325789Sahrens error = zio_wait(zio); 326789Sahrens 327789Sahrens /* 328789Sahrens * If we weren't able to find a single valid uberblock, return failure. 329789Sahrens */ 330789Sahrens if (ub->ub_txg == 0) { 3311544Seschrock error = ENXIO; 3321544Seschrock goto out; 3331544Seschrock } 3341544Seschrock 3351544Seschrock /* 3361544Seschrock * If the pool is newer than the code, we can't open it. 3371544Seschrock */ 3381544Seschrock if (ub->ub_version > UBERBLOCK_VERSION) { 3391544Seschrock error = ENOTSUP; 3401544Seschrock goto out; 341789Sahrens } 342789Sahrens 343789Sahrens /* 344789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 345789Sahrens * incomplete configuration. 346789Sahrens */ 347789Sahrens if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 3481544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 3491544Seschrock VDEV_AUX_BAD_GUID_SUM); 3501544Seschrock error = ENXIO; 3511544Seschrock goto out; 352789Sahrens } 353789Sahrens 354789Sahrens /* 355789Sahrens * Initialize internal SPA structures. 356789Sahrens */ 357789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 358789Sahrens spa->spa_ubsync = spa->spa_uberblock; 359789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 3601544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 3611544Seschrock if (error) { 3621544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 3631544Seschrock VDEV_AUX_CORRUPT_DATA); 3641544Seschrock goto out; 3651544Seschrock } 366789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 367789Sahrens 3681544Seschrock if (zap_lookup(spa->spa_meta_objset, 369789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3701544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 3711544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 3721544Seschrock VDEV_AUX_CORRUPT_DATA); 3731544Seschrock error = EIO; 3741544Seschrock goto out; 3751544Seschrock } 376789Sahrens 377789Sahrens if (!mosconfig) { 378789Sahrens dmu_buf_t *db; 379789Sahrens char *packed = NULL; 380789Sahrens size_t nvsize = 0; 381789Sahrens nvlist_t *newconfig = NULL; 382789Sahrens 3831544Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, 3841544Seschrock spa->spa_config_object, FTAG, &db)); 385789Sahrens nvsize = *(uint64_t *)db->db_data; 3861544Seschrock dmu_buf_rele(db, FTAG); 387789Sahrens 388789Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 3891544Seschrock error = dmu_read(spa->spa_meta_objset, 390789Sahrens spa->spa_config_object, 0, nvsize, packed); 391789Sahrens if (error == 0) 392789Sahrens error = nvlist_unpack(packed, nvsize, &newconfig, 0); 393789Sahrens kmem_free(packed, nvsize); 394789Sahrens 3951544Seschrock if (error) { 3961544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 3971544Seschrock VDEV_AUX_CORRUPT_DATA); 3981544Seschrock error = EIO; 3991544Seschrock goto out; 4001544Seschrock } 401789Sahrens 402789Sahrens spa_config_set(spa, newconfig); 403789Sahrens 404789Sahrens spa_unload(spa); 405789Sahrens spa_deactivate(spa); 406789Sahrens spa_activate(spa); 407789Sahrens 4081544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 4091544Seschrock } 4101544Seschrock 4111544Seschrock if (zap_lookup(spa->spa_meta_objset, 4121544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 4131544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 4141544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 4151544Seschrock VDEV_AUX_CORRUPT_DATA); 4161544Seschrock error = EIO; 4171544Seschrock goto out; 418789Sahrens } 419789Sahrens 4201544Seschrock /* 4211544Seschrock * Load the persistent error log. If we have an older pool, this will 4221544Seschrock * not be present. 4231544Seschrock */ 4241544Seschrock error = zap_lookup(spa->spa_meta_objset, 4251544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 4261544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 4271544Seschrock if (error != 0 &&error != ENOENT) { 4281544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 4291544Seschrock VDEV_AUX_CORRUPT_DATA); 4301544Seschrock error = EIO; 4311544Seschrock goto out; 4321544Seschrock } 4331544Seschrock 4341544Seschrock error = zap_lookup(spa->spa_meta_objset, 4351544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 4361544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 4371544Seschrock if (error != 0 && error != ENOENT) { 4381544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 4391544Seschrock VDEV_AUX_CORRUPT_DATA); 4401544Seschrock error = EIO; 4411544Seschrock goto out; 4421544Seschrock } 443789Sahrens 444789Sahrens /* 4451544Seschrock * Load the vdev state for all top level vdevs. We need to grab the 4461544Seschrock * config lock because all label I/O is done with the 4471544Seschrock * ZIO_FLAG_CONFIG_HELD flag. 448789Sahrens */ 4491544Seschrock spa_config_enter(spa, RW_READER, FTAG); 4501544Seschrock if ((error = vdev_load(rvd)) != 0) { 4511544Seschrock spa_config_exit(spa, FTAG); 4521544Seschrock goto out; 4531544Seschrock } 4541544Seschrock spa_config_exit(spa, FTAG); 455789Sahrens 456789Sahrens /* 457789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 458789Sahrens */ 4591544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 460789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 4611544Seschrock spa_config_exit(spa, FTAG); 462789Sahrens 463789Sahrens /* 464789Sahrens * Check the state of the root vdev. If it can't be opened, it 465789Sahrens * indicates one or more toplevel vdevs are faulted. 466789Sahrens */ 4671544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 4681544Seschrock error = ENXIO; 4691544Seschrock goto out; 4701544Seschrock } 471789Sahrens 472789Sahrens /* 473789Sahrens * Claim log blocks that haven't been committed yet, and update all 474789Sahrens * top-level vdevs to sync any config changes found in vdev_load(). 475789Sahrens * This must all happen in a single txg. 476789Sahrens */ 4771544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 478*1585Sbonwick int c; 479789Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), 480789Sahrens spa_first_txg(spa)); 481789Sahrens dmu_objset_find(spa->spa_name, zil_claim, tx, 0); 482789Sahrens vdev_config_dirty(rvd); 483789Sahrens dmu_tx_commit(tx); 484789Sahrens 485789Sahrens spa->spa_sync_on = B_TRUE; 486789Sahrens txg_sync_start(spa->spa_dsl_pool); 487789Sahrens 488789Sahrens /* 489789Sahrens * Wait for all claims to sync. 490789Sahrens */ 491789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 492*1585Sbonwick 493*1585Sbonwick /* 494*1585Sbonwick * If the config cache is stale relative to the mosconfig, 495*1585Sbonwick * sync the config cache. 496*1585Sbonwick */ 497*1585Sbonwick if (config_cache_txg != spa->spa_config_txg) 498*1585Sbonwick spa_config_sync(); 499*1585Sbonwick 500*1585Sbonwick /* 501*1585Sbonwick * If we have top-level vdevs that were added but have 502*1585Sbonwick * not yet been prepared for allocation, do that now. 503*1585Sbonwick * (It's safe now because the config cache is up to date, 504*1585Sbonwick * so it will be able to translate the new DVAs.) 505*1585Sbonwick * See comments in spa_vdev_add() for full details. 506*1585Sbonwick */ 507*1585Sbonwick for (c = 0; c < rvd->vdev_children; c++) { 508*1585Sbonwick vdev_t *tvd = rvd->vdev_child[c]; 509*1585Sbonwick if (tvd->vdev_ms_array == 0) { 510*1585Sbonwick uint64_t txg = spa_last_synced_txg(spa) + 1; 511*1585Sbonwick ASSERT(tvd->vdev_ms_shift == 0); 512*1585Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 513*1585Sbonwick vdev_init(tvd, txg); 514*1585Sbonwick vdev_config_dirty(tvd); 515*1585Sbonwick spa_config_set(spa, 516*1585Sbonwick spa_config_generate(spa, rvd, txg, 0)); 517*1585Sbonwick spa_config_exit(spa, FTAG); 518*1585Sbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 519*1585Sbonwick ASSERT(tvd->vdev_ms_shift != 0); 520*1585Sbonwick ASSERT(tvd->vdev_ms_array != 0); 521*1585Sbonwick spa_config_sync(); 522*1585Sbonwick } 523*1585Sbonwick } 524789Sahrens } 525789Sahrens 5261544Seschrock error = 0; 5271544Seschrock out: 5281544Seschrock if (error) 5291544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 5301544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 5311544Seschrock spa->spa_ena = 0; 5321544Seschrock 5331544Seschrock return (error); 534789Sahrens } 535789Sahrens 536789Sahrens /* 537789Sahrens * Pool Open/Import 538789Sahrens * 539789Sahrens * The import case is identical to an open except that the configuration is sent 540789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 541789Sahrens * case of an open, the pool configuration will exist in the 542789Sahrens * POOL_STATE_UNITIALIZED state. 543789Sahrens * 544789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 545789Sahrens * the same time open the pool, without having to keep around the spa_t in some 546789Sahrens * ambiguous state. 547789Sahrens */ 548789Sahrens static int 549789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 550789Sahrens { 551789Sahrens spa_t *spa; 552789Sahrens int error; 553789Sahrens int loaded = B_FALSE; 554789Sahrens int locked = B_FALSE; 555789Sahrens 556789Sahrens *spapp = NULL; 557789Sahrens 558789Sahrens /* 559789Sahrens * As disgusting as this is, we need to support recursive calls to this 560789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 561789Sahrens * up calling spa_open() again. The real fix is to figure out how to 562789Sahrens * avoid dsl_dir_open() calling this in the first place. 563789Sahrens */ 564789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 565789Sahrens mutex_enter(&spa_namespace_lock); 566789Sahrens locked = B_TRUE; 567789Sahrens } 568789Sahrens 569789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 570789Sahrens if (locked) 571789Sahrens mutex_exit(&spa_namespace_lock); 572789Sahrens return (ENOENT); 573789Sahrens } 574789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 575789Sahrens 576789Sahrens spa_activate(spa); 577789Sahrens 578789Sahrens error = spa_load(spa, spa->spa_config, 5791544Seschrock SPA_LOAD_OPEN, B_FALSE); 580789Sahrens 581789Sahrens if (error == EBADF) { 582789Sahrens /* 583789Sahrens * If vdev_load() returns EBADF, it indicates that one 584789Sahrens * of the vdevs indicates that the pool has been 585789Sahrens * exported or destroyed. If this is the case, the 586789Sahrens * config cache is out of sync and we should remove the 587789Sahrens * pool from the namespace. 588789Sahrens */ 589789Sahrens spa_unload(spa); 590789Sahrens spa_deactivate(spa); 591789Sahrens spa_remove(spa); 592789Sahrens spa_config_sync(); 593789Sahrens if (locked) 594789Sahrens mutex_exit(&spa_namespace_lock); 595789Sahrens return (ENOENT); 5961544Seschrock } 5971544Seschrock 5981544Seschrock if (error) { 599789Sahrens /* 600789Sahrens * We can't open the pool, but we still have useful 601789Sahrens * information: the state of each vdev after the 602789Sahrens * attempted vdev_open(). Return this to the user. 603789Sahrens */ 604789Sahrens if (config != NULL && spa->spa_root_vdev != NULL) 605789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 606789Sahrens B_TRUE); 607789Sahrens spa_unload(spa); 608789Sahrens spa_deactivate(spa); 6091544Seschrock spa->spa_last_open_failed = B_TRUE; 610789Sahrens if (locked) 611789Sahrens mutex_exit(&spa_namespace_lock); 612789Sahrens *spapp = NULL; 613789Sahrens return (error); 6141544Seschrock } else { 6151544Seschrock zfs_post_ok(spa, NULL); 6161544Seschrock spa->spa_last_open_failed = B_FALSE; 617789Sahrens } 618789Sahrens 619789Sahrens loaded = B_TRUE; 620789Sahrens } 621789Sahrens 622789Sahrens spa_open_ref(spa, tag); 623789Sahrens if (locked) 624789Sahrens mutex_exit(&spa_namespace_lock); 625789Sahrens 626789Sahrens *spapp = spa; 627789Sahrens 628789Sahrens if (config != NULL) { 6291544Seschrock spa_config_enter(spa, RW_READER, FTAG); 630789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 6311544Seschrock spa_config_exit(spa, FTAG); 632789Sahrens } 633789Sahrens 634789Sahrens /* 635789Sahrens * If we just loaded the pool, resilver anything that's out of date. 636789Sahrens */ 637789Sahrens if (loaded && (spa_mode & FWRITE)) 638789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 639789Sahrens 640789Sahrens return (0); 641789Sahrens } 642789Sahrens 643789Sahrens int 644789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 645789Sahrens { 646789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 647789Sahrens } 648789Sahrens 6491544Seschrock /* 6501544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 6511544Seschrock * preventing it from being exported or destroyed. 6521544Seschrock */ 6531544Seschrock spa_t * 6541544Seschrock spa_inject_addref(char *name) 6551544Seschrock { 6561544Seschrock spa_t *spa; 6571544Seschrock 6581544Seschrock mutex_enter(&spa_namespace_lock); 6591544Seschrock if ((spa = spa_lookup(name)) == NULL) { 6601544Seschrock mutex_exit(&spa_namespace_lock); 6611544Seschrock return (NULL); 6621544Seschrock } 6631544Seschrock spa->spa_inject_ref++; 6641544Seschrock mutex_exit(&spa_namespace_lock); 6651544Seschrock 6661544Seschrock return (spa); 6671544Seschrock } 6681544Seschrock 6691544Seschrock void 6701544Seschrock spa_inject_delref(spa_t *spa) 6711544Seschrock { 6721544Seschrock mutex_enter(&spa_namespace_lock); 6731544Seschrock spa->spa_inject_ref--; 6741544Seschrock mutex_exit(&spa_namespace_lock); 6751544Seschrock } 6761544Seschrock 677789Sahrens int 6781544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 679789Sahrens { 680789Sahrens int error; 681789Sahrens spa_t *spa; 682789Sahrens 683789Sahrens *config = NULL; 684789Sahrens error = spa_open_common(name, &spa, FTAG, config); 685789Sahrens 6861544Seschrock if (spa && *config != NULL) 6871544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 6881544Seschrock spa_get_errlog_size(spa)) == 0); 6891544Seschrock 6901544Seschrock /* 6911544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 6921544Seschrock * and call spa_lookup() directly. 6931544Seschrock */ 6941544Seschrock if (altroot) { 6951544Seschrock if (spa == NULL) { 6961544Seschrock mutex_enter(&spa_namespace_lock); 6971544Seschrock spa = spa_lookup(name); 6981544Seschrock if (spa) 6991544Seschrock spa_altroot(spa, altroot, buflen); 7001544Seschrock else 7011544Seschrock altroot[0] = '\0'; 7021544Seschrock spa = NULL; 7031544Seschrock mutex_exit(&spa_namespace_lock); 7041544Seschrock } else { 7051544Seschrock spa_altroot(spa, altroot, buflen); 7061544Seschrock } 7071544Seschrock } 7081544Seschrock 709789Sahrens if (spa != NULL) 710789Sahrens spa_close(spa, FTAG); 711789Sahrens 712789Sahrens return (error); 713789Sahrens } 714789Sahrens 715789Sahrens /* 716789Sahrens * Pool Creation 717789Sahrens */ 718789Sahrens int 719789Sahrens spa_create(const char *pool, nvlist_t *nvroot, char *altroot) 720789Sahrens { 721789Sahrens spa_t *spa; 722789Sahrens dsl_pool_t *dp; 723789Sahrens dmu_tx_t *tx; 724789Sahrens int error; 725789Sahrens uint64_t txg = TXG_INITIAL; 726789Sahrens 727789Sahrens /* 728789Sahrens * If this pool already exists, return failure. 729789Sahrens */ 730789Sahrens mutex_enter(&spa_namespace_lock); 731789Sahrens if (spa_lookup(pool) != NULL) { 732789Sahrens mutex_exit(&spa_namespace_lock); 733789Sahrens return (EEXIST); 734789Sahrens } 735789Sahrens spa = spa_add(pool); 736789Sahrens 737789Sahrens /* 738789Sahrens * Allocate a new spa_t structure. 739789Sahrens */ 740789Sahrens spa_activate(spa); 741789Sahrens 742789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 743789Sahrens spa->spa_ubsync = spa->spa_uberblock; 744789Sahrens 745789Sahrens error = spa_vdev_add(spa, nvroot); 746789Sahrens 747789Sahrens if (error) { 748789Sahrens spa_unload(spa); 749789Sahrens spa_deactivate(spa); 750789Sahrens spa_remove(spa); 751789Sahrens mutex_exit(&spa_namespace_lock); 752789Sahrens return (error); 753789Sahrens } 754789Sahrens 755789Sahrens if (altroot != NULL) { 756789Sahrens spa->spa_root = spa_strdup(altroot); 757789Sahrens atomic_add_32(&spa_active_count, 1); 758789Sahrens } 759789Sahrens 760789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 761789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 762789Sahrens 763789Sahrens tx = dmu_tx_create_assigned(dp, txg); 764789Sahrens 765789Sahrens /* 766789Sahrens * Create the pool config object. 767789Sahrens */ 768789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 769789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 770789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 771789Sahrens 7721544Seschrock if (zap_add(spa->spa_meta_objset, 773789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 7741544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 7751544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 7761544Seschrock } 777789Sahrens 778789Sahrens /* 779789Sahrens * Create the deferred-free bplist object. Turn off compression 780789Sahrens * because sync-to-convergence takes longer if the blocksize 781789Sahrens * keeps changing. 782789Sahrens */ 783789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 784789Sahrens 1 << 14, tx); 785789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 786789Sahrens ZIO_COMPRESS_OFF, tx); 787789Sahrens 7881544Seschrock if (zap_add(spa->spa_meta_objset, 789789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 7901544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 7911544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 7921544Seschrock } 793789Sahrens 794789Sahrens dmu_tx_commit(tx); 795789Sahrens 796789Sahrens spa->spa_sync_on = B_TRUE; 797789Sahrens txg_sync_start(spa->spa_dsl_pool); 798789Sahrens 799789Sahrens /* 800789Sahrens * We explicitly wait for the first transaction to complete so that our 801789Sahrens * bean counters are appropriately updated. 802789Sahrens */ 803789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 804789Sahrens 805789Sahrens spa_config_sync(); 806789Sahrens 807789Sahrens mutex_exit(&spa_namespace_lock); 808789Sahrens 809789Sahrens return (0); 810789Sahrens } 811789Sahrens 812789Sahrens /* 813789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 814789Sahrens * then call spa_load() to do the dirty work. 815789Sahrens */ 816789Sahrens int 817789Sahrens spa_import(const char *pool, nvlist_t *config, char *altroot) 818789Sahrens { 819789Sahrens spa_t *spa; 820789Sahrens int error; 821789Sahrens 822789Sahrens if (!(spa_mode & FWRITE)) 823789Sahrens return (EROFS); 824789Sahrens 825789Sahrens /* 826789Sahrens * If a pool with this name exists, return failure. 827789Sahrens */ 828789Sahrens mutex_enter(&spa_namespace_lock); 829789Sahrens if (spa_lookup(pool) != NULL) { 830789Sahrens mutex_exit(&spa_namespace_lock); 831789Sahrens return (EEXIST); 832789Sahrens } 833789Sahrens 834789Sahrens /* 835789Sahrens * Create an initialize the spa structure 836789Sahrens */ 837789Sahrens spa = spa_add(pool); 838789Sahrens spa_activate(spa); 839789Sahrens 840789Sahrens /* 841789Sahrens * Pass off the heavy lifting to spa_load(). We pass TRUE for mosconfig 842789Sahrens * so that we don't try to open the pool if the config is damaged. 843789Sahrens */ 8441544Seschrock error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 845789Sahrens 846789Sahrens if (error) { 847789Sahrens spa_unload(spa); 848789Sahrens spa_deactivate(spa); 849789Sahrens spa_remove(spa); 850789Sahrens mutex_exit(&spa_namespace_lock); 851789Sahrens return (error); 852789Sahrens } 853789Sahrens 854789Sahrens /* 855789Sahrens * Set the alternate root, if there is one. 856789Sahrens */ 857789Sahrens if (altroot != NULL) { 858789Sahrens atomic_add_32(&spa_active_count, 1); 859789Sahrens spa->spa_root = spa_strdup(altroot); 860789Sahrens } 861789Sahrens 862789Sahrens /* 863789Sahrens * Initialize the config based on the in-core state. 864789Sahrens */ 865789Sahrens config = spa_config_generate(spa, NULL, spa_last_synced_txg(spa), 0); 866789Sahrens 867789Sahrens spa_config_set(spa, config); 868789Sahrens 869789Sahrens /* 870789Sahrens * Sync the configuration cache. 871789Sahrens */ 872789Sahrens spa_config_sync(); 873789Sahrens 874789Sahrens mutex_exit(&spa_namespace_lock); 875789Sahrens 876789Sahrens /* 877789Sahrens * Resilver anything that's out of date. 878789Sahrens */ 879789Sahrens if (spa_mode & FWRITE) 880789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 881789Sahrens 882789Sahrens return (0); 883789Sahrens } 884789Sahrens 885789Sahrens /* 886789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 887789Sahrens * to get the vdev stats associated with the imported devices. 888789Sahrens */ 889789Sahrens #define TRYIMPORT_NAME "$import" 890789Sahrens 891789Sahrens nvlist_t * 892789Sahrens spa_tryimport(nvlist_t *tryconfig) 893789Sahrens { 894789Sahrens nvlist_t *config = NULL; 895789Sahrens char *poolname; 896789Sahrens spa_t *spa; 897789Sahrens uint64_t state; 898789Sahrens 899789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 900789Sahrens return (NULL); 901789Sahrens 902789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 903789Sahrens return (NULL); 904789Sahrens 905789Sahrens mutex_enter(&spa_namespace_lock); 906789Sahrens spa = spa_add(TRYIMPORT_NAME); 907789Sahrens 908789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 909789Sahrens 910789Sahrens /* 911789Sahrens * Initialize the spa_t structure. 912789Sahrens */ 913789Sahrens spa_activate(spa); 914789Sahrens 915789Sahrens /* 916789Sahrens * Pass off the heavy lifting to spa_load(). We pass TRUE for mosconfig 917789Sahrens * so we don't try to open the pool if the config is damaged. 918789Sahrens */ 9191544Seschrock (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 920789Sahrens 921789Sahrens /* 922789Sahrens * If 'tryconfig' was at least parsable, return the current config. 923789Sahrens */ 924789Sahrens if (spa->spa_root_vdev != NULL) { 925789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 926789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 927789Sahrens poolname) == 0); 928789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 929789Sahrens state) == 0); 930789Sahrens } 931789Sahrens 932789Sahrens spa_unload(spa); 933789Sahrens spa_deactivate(spa); 934789Sahrens spa_remove(spa); 935789Sahrens mutex_exit(&spa_namespace_lock); 936789Sahrens 937789Sahrens return (config); 938789Sahrens } 939789Sahrens 940789Sahrens /* 941789Sahrens * Pool export/destroy 942789Sahrens * 943789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 944789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 945789Sahrens * update the pool state and sync all the labels to disk, removing the 946789Sahrens * configuration from the cache afterwards. 947789Sahrens */ 948789Sahrens static int 949789Sahrens spa_export_common(char *pool, int new_state) 950789Sahrens { 951789Sahrens spa_t *spa; 952789Sahrens 953789Sahrens if (!(spa_mode & FWRITE)) 954789Sahrens return (EROFS); 955789Sahrens 956789Sahrens mutex_enter(&spa_namespace_lock); 957789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 958789Sahrens mutex_exit(&spa_namespace_lock); 959789Sahrens return (ENOENT); 960789Sahrens } 961789Sahrens 962789Sahrens /* 9631544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 9641544Seschrock * reacquire the namespace lock, and see if we can export. 9651544Seschrock */ 9661544Seschrock spa_open_ref(spa, FTAG); 9671544Seschrock mutex_exit(&spa_namespace_lock); 9681544Seschrock spa_async_suspend(spa); 9691544Seschrock mutex_enter(&spa_namespace_lock); 9701544Seschrock spa_close(spa, FTAG); 9711544Seschrock 9721544Seschrock /* 973789Sahrens * The pool will be in core if it's openable, 974789Sahrens * in which case we can modify its state. 975789Sahrens */ 976789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 977789Sahrens /* 978789Sahrens * Objsets may be open only because they're dirty, so we 979789Sahrens * have to force it to sync before checking spa_refcnt. 980789Sahrens */ 981789Sahrens spa_scrub_suspend(spa); 982789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 983789Sahrens 9841544Seschrock /* 9851544Seschrock * A pool cannot be exported or destroyed if there are active 9861544Seschrock * references. If we are resetting a pool, allow references by 9871544Seschrock * fault injection handlers. 9881544Seschrock */ 9891544Seschrock if (!spa_refcount_zero(spa) || 9901544Seschrock (spa->spa_inject_ref != 0 && 9911544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 992789Sahrens spa_scrub_resume(spa); 9931544Seschrock spa_async_resume(spa); 994789Sahrens mutex_exit(&spa_namespace_lock); 995789Sahrens return (EBUSY); 996789Sahrens } 997789Sahrens 998789Sahrens spa_scrub_resume(spa); 999789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1000789Sahrens 1001789Sahrens if (spa->spa_root != NULL) 1002789Sahrens atomic_add_32(&spa_active_count, -1); 1003789Sahrens 1004789Sahrens /* 1005789Sahrens * We want this to be reflected on every label, 1006789Sahrens * so mark them all dirty. spa_unload() will do the 1007789Sahrens * final sync that pushes these changes out. 1008789Sahrens */ 10091544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 10101544Seschrock spa->spa_state = new_state; 10111544Seschrock vdev_config_dirty(spa->spa_root_vdev); 10121544Seschrock } 1013789Sahrens } 1014789Sahrens 1015789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1016789Sahrens spa_unload(spa); 1017789Sahrens spa_deactivate(spa); 1018789Sahrens } 1019789Sahrens 10201544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 10211544Seschrock spa_remove(spa); 10221544Seschrock spa_config_sync(); 10231544Seschrock } 1024789Sahrens mutex_exit(&spa_namespace_lock); 1025789Sahrens 1026789Sahrens return (0); 1027789Sahrens } 1028789Sahrens 1029789Sahrens /* 1030789Sahrens * Destroy a storage pool. 1031789Sahrens */ 1032789Sahrens int 1033789Sahrens spa_destroy(char *pool) 1034789Sahrens { 1035789Sahrens return (spa_export_common(pool, POOL_STATE_DESTROYED)); 1036789Sahrens } 1037789Sahrens 1038789Sahrens /* 1039789Sahrens * Export a storage pool. 1040789Sahrens */ 1041789Sahrens int 1042789Sahrens spa_export(char *pool) 1043789Sahrens { 1044789Sahrens return (spa_export_common(pool, POOL_STATE_EXPORTED)); 1045789Sahrens } 1046789Sahrens 1047789Sahrens /* 10481544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 10491544Seschrock * from the namespace in any way. 10501544Seschrock */ 10511544Seschrock int 10521544Seschrock spa_reset(char *pool) 10531544Seschrock { 10541544Seschrock return (spa_export_common(pool, POOL_STATE_UNINITIALIZED)); 10551544Seschrock } 10561544Seschrock 10571544Seschrock 10581544Seschrock /* 1059789Sahrens * ========================================================================== 1060789Sahrens * Device manipulation 1061789Sahrens * ========================================================================== 1062789Sahrens */ 1063789Sahrens 1064789Sahrens /* 1065789Sahrens * Add capacity to a storage pool. 1066789Sahrens */ 1067789Sahrens int 1068789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1069789Sahrens { 1070789Sahrens uint64_t txg; 1071*1585Sbonwick int c, c0, children, error; 1072789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1073*1585Sbonwick vdev_t *vd, *tvd; 1074789Sahrens 1075789Sahrens txg = spa_vdev_enter(spa); 1076789Sahrens 1077789Sahrens vd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD); 1078789Sahrens 1079789Sahrens if (vd == NULL) 1080789Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 1081789Sahrens 1082*1585Sbonwick if (rvd == NULL) { /* spa_create() */ 1083*1585Sbonwick rvd = vd; 1084*1585Sbonwick c0 = 0; 1085*1585Sbonwick } else { 1086*1585Sbonwick c0 = rvd->vdev_children; 1087*1585Sbonwick } 1088*1585Sbonwick 1089*1585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1090789Sahrens 1091789Sahrens if ((error = vdev_create(vd, txg)) != 0) 1092789Sahrens return (spa_vdev_exit(spa, vd, txg, error)); 1093789Sahrens 1094*1585Sbonwick children = vd->vdev_children; 1095*1585Sbonwick 1096789Sahrens /* 1097*1585Sbonwick * Transfer each new top-level vdev from vd to rvd. 1098789Sahrens */ 1099*1585Sbonwick for (c = 0; c < children; c++) { 1100*1585Sbonwick tvd = vd->vdev_child[c]; 1101789Sahrens if (vd != rvd) { 1102789Sahrens vdev_remove_child(vd, tvd); 1103*1585Sbonwick tvd->vdev_id = c0 + c; 1104789Sahrens vdev_add_child(rvd, tvd); 1105789Sahrens } 1106789Sahrens vdev_config_dirty(tvd); 1107789Sahrens } 1108789Sahrens 1109789Sahrens /* 1110*1585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 1111*1585Sbonwick * If other threads start allocating from these vdevs before we 1112*1585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 1113*1585Sbonwick * fail to open the pool because there are DVAs that the config cache 1114*1585Sbonwick * can't translate. Therefore, we first add the vdevs without 1115*1585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 1116*1585Sbonwick * initialize the metaslabs; and sync the config cache again. 1117*1585Sbonwick * 1118*1585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 1119*1585Sbonwick * if we lose power at any point in this sequence, the remaining 1120*1585Sbonwick * steps will be completed the next time we load the pool. 1121789Sahrens */ 1122*1585Sbonwick if (vd != rvd) { 1123*1585Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 1124*1585Sbonwick txg = spa_vdev_enter(spa); 1125*1585Sbonwick vd = NULL; 1126*1585Sbonwick } 1127*1585Sbonwick 1128*1585Sbonwick /* 1129*1585Sbonwick * Now that the config is safely on disk, we can use the new space. 1130*1585Sbonwick */ 1131*1585Sbonwick for (c = 0; c < children; c++) { 1132*1585Sbonwick tvd = rvd->vdev_child[c0 + c]; 1133*1585Sbonwick ASSERT(tvd->vdev_ms_array == 0); 1134*1585Sbonwick vdev_init(tvd, txg); 1135*1585Sbonwick vdev_config_dirty(tvd); 1136*1585Sbonwick } 1137789Sahrens 1138789Sahrens return (spa_vdev_exit(spa, vd, txg, 0)); 1139789Sahrens } 1140789Sahrens 1141789Sahrens /* 1142789Sahrens * Attach a device to a mirror. The arguments are the path to any device 1143789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1144789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1145789Sahrens * 1146789Sahrens * If 'replacing' is specified, the new device is intended to replace the 1147789Sahrens * existing device; in this case the two devices are made into their own 1148789Sahrens * mirror using the 'replacing' vdev, which is functionally idendical to 1149789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1150789Sahrens * extra rules: you can't attach to it after it's been created, and upon 1151789Sahrens * completion of resilvering, the first disk (the one being replaced) 1152789Sahrens * is automatically detached. 1153789Sahrens */ 1154789Sahrens int 11551544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1156789Sahrens { 1157789Sahrens uint64_t txg, open_txg; 1158789Sahrens int error; 1159789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1160789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 1161789Sahrens vdev_ops_t *pvops = replacing ? &vdev_replacing_ops : &vdev_mirror_ops; 1162789Sahrens 1163789Sahrens txg = spa_vdev_enter(spa); 1164789Sahrens 11651544Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1166789Sahrens 1167789Sahrens if (oldvd == NULL) 1168789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1169789Sahrens 1170*1585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 1171*1585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1172*1585Sbonwick 1173789Sahrens pvd = oldvd->vdev_parent; 1174789Sahrens 1175789Sahrens /* 1176789Sahrens * The parent must be a mirror or the root, unless we're replacing; 1177789Sahrens * in that case, the parent can be anything but another replacing vdev. 1178789Sahrens */ 1179789Sahrens if (pvd->vdev_ops != &vdev_mirror_ops && 1180789Sahrens pvd->vdev_ops != &vdev_root_ops && 1181789Sahrens (!replacing || pvd->vdev_ops == &vdev_replacing_ops)) 1182789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1183789Sahrens 1184789Sahrens newrootvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD); 1185789Sahrens 1186789Sahrens if (newrootvd == NULL || newrootvd->vdev_children != 1) 1187789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1188789Sahrens 1189789Sahrens newvd = newrootvd->vdev_child[0]; 1190789Sahrens 1191789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1192789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1193789Sahrens 1194789Sahrens if ((error = vdev_create(newrootvd, txg)) != 0) 1195789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1196789Sahrens 11971175Slling /* 11981175Slling * Compare the new device size with the replaceable/attachable 11991175Slling * device size. 12001175Slling */ 12011175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1202789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1203789Sahrens 1204789Sahrens if (newvd->vdev_ashift != oldvd->vdev_ashift && oldvd->vdev_ashift != 0) 1205789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1206789Sahrens 1207789Sahrens /* 1208789Sahrens * If this is an in-place replacement, update oldvd's path and devid 1209789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1210789Sahrens */ 1211789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1212789Sahrens spa_strfree(oldvd->vdev_path); 1213789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1214789Sahrens KM_SLEEP); 1215789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1216789Sahrens newvd->vdev_path, "old"); 1217789Sahrens if (oldvd->vdev_devid != NULL) { 1218789Sahrens spa_strfree(oldvd->vdev_devid); 1219789Sahrens oldvd->vdev_devid = NULL; 1220789Sahrens } 1221789Sahrens } 1222789Sahrens 1223789Sahrens /* 1224789Sahrens * If the parent is not a mirror, or if we're replacing, 1225789Sahrens * insert the new mirror/replacing vdev above oldvd. 1226789Sahrens */ 1227789Sahrens if (pvd->vdev_ops != pvops) 1228789Sahrens pvd = vdev_add_parent(oldvd, pvops); 1229789Sahrens 1230789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1231789Sahrens ASSERT(pvd->vdev_ops == pvops); 1232789Sahrens ASSERT(oldvd->vdev_parent == pvd); 1233789Sahrens 1234789Sahrens /* 1235789Sahrens * Extract the new device from its root and add it to pvd. 1236789Sahrens */ 1237789Sahrens vdev_remove_child(newrootvd, newvd); 1238789Sahrens newvd->vdev_id = pvd->vdev_children; 1239789Sahrens vdev_add_child(pvd, newvd); 1240789Sahrens 12411544Seschrock /* 12421544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 12431544Seschrock * the addition of newvd may have decreased our parent's asize. 12441544Seschrock */ 12451544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 12461544Seschrock 1247789Sahrens tvd = newvd->vdev_top; 1248789Sahrens ASSERT(pvd->vdev_top == tvd); 1249789Sahrens ASSERT(tvd->vdev_parent == rvd); 1250789Sahrens 1251789Sahrens vdev_config_dirty(tvd); 1252789Sahrens 1253789Sahrens /* 1254789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1255789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1256789Sahrens */ 1257789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1258789Sahrens 1259789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1260789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1261789Sahrens open_txg - TXG_INITIAL + 1); 1262789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1263789Sahrens 12641544Seschrock dprintf("attached %s in txg %llu\n", newvd->vdev_path, txg); 12651544Seschrock 1266789Sahrens /* 1267789Sahrens * Mark newvd's DTL dirty in this txg. 1268789Sahrens */ 1269789Sahrens vdev_dirty(tvd, VDD_DTL, txg); 1270789Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, newvd, txg); 1271789Sahrens 1272789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1273789Sahrens 1274789Sahrens /* 1275789Sahrens * Kick off a resilver to update newvd. 1276789Sahrens */ 1277789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1278789Sahrens 1279789Sahrens return (0); 1280789Sahrens } 1281789Sahrens 1282789Sahrens /* 1283789Sahrens * Detach a device from a mirror or replacing vdev. 1284789Sahrens * If 'replace_done' is specified, only detach if the parent 1285789Sahrens * is a replacing vdev. 1286789Sahrens */ 1287789Sahrens int 12881544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1289789Sahrens { 1290789Sahrens uint64_t txg; 1291789Sahrens int c, t, error; 1292789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1293789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 1294789Sahrens 1295789Sahrens txg = spa_vdev_enter(spa); 1296789Sahrens 12971544Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1298789Sahrens 1299789Sahrens if (vd == NULL) 1300789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1301789Sahrens 1302*1585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 1303*1585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1304*1585Sbonwick 1305789Sahrens pvd = vd->vdev_parent; 1306789Sahrens 1307789Sahrens /* 1308789Sahrens * If replace_done is specified, only remove this device if it's 1309789Sahrens * the first child of a replacing vdev. 1310789Sahrens */ 1311789Sahrens if (replace_done && 1312789Sahrens (vd->vdev_id != 0 || pvd->vdev_ops != &vdev_replacing_ops)) 1313789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1314789Sahrens 1315789Sahrens /* 1316789Sahrens * Only mirror and replacing vdevs support detach. 1317789Sahrens */ 1318789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 1319789Sahrens pvd->vdev_ops != &vdev_mirror_ops) 1320789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1321789Sahrens 1322789Sahrens /* 1323789Sahrens * If there's only one replica, you can't detach it. 1324789Sahrens */ 1325789Sahrens if (pvd->vdev_children <= 1) 1326789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1327789Sahrens 1328789Sahrens /* 1329789Sahrens * If all siblings have non-empty DTLs, this device may have the only 1330789Sahrens * valid copy of the data, which means we cannot safely detach it. 1331789Sahrens * 1332789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1333789Sahrens * precise DTL check. 1334789Sahrens */ 1335789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1336789Sahrens uint64_t dirty; 1337789Sahrens 1338789Sahrens cvd = pvd->vdev_child[c]; 1339789Sahrens if (cvd == vd) 1340789Sahrens continue; 1341789Sahrens if (vdev_is_dead(cvd)) 1342789Sahrens continue; 1343789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1344789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1345789Sahrens cvd->vdev_dtl_scrub.sm_space; 1346789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1347789Sahrens if (!dirty) 1348789Sahrens break; 1349789Sahrens } 1350789Sahrens if (c == pvd->vdev_children) 1351789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1352789Sahrens 1353789Sahrens /* 1354789Sahrens * Erase the disk labels so the disk can be used for other things. 1355789Sahrens * This must be done after all other error cases are handled, 1356789Sahrens * but before we disembowel vd (so we can still do I/O to it). 1357789Sahrens * But if we can't do it, don't treat the error as fatal -- 1358789Sahrens * it may be that the unwritability of the disk is the reason 1359789Sahrens * it's being detached! 1360789Sahrens */ 1361789Sahrens error = vdev_label_init(vd, 0); 1362789Sahrens if (error) 1363789Sahrens dprintf("unable to erase labels on %s\n", vdev_description(vd)); 1364789Sahrens 1365789Sahrens /* 1366789Sahrens * Remove vd from its parent and compact the parent's children. 1367789Sahrens */ 1368789Sahrens vdev_remove_child(pvd, vd); 1369789Sahrens vdev_compact_children(pvd); 1370789Sahrens 1371789Sahrens /* 1372789Sahrens * Remember one of the remaining children so we can get tvd below. 1373789Sahrens */ 1374789Sahrens cvd = pvd->vdev_child[0]; 1375789Sahrens 1376789Sahrens /* 1377789Sahrens * If the parent mirror/replacing vdev only has one child, 1378789Sahrens * the parent is no longer needed. Remove it from the tree. 1379789Sahrens */ 1380789Sahrens if (pvd->vdev_children == 1) 1381789Sahrens vdev_remove_parent(cvd); 1382789Sahrens 1383789Sahrens /* 1384789Sahrens * We don't set tvd until now because the parent we just removed 1385789Sahrens * may have been the previous top-level vdev. 1386789Sahrens */ 1387789Sahrens tvd = cvd->vdev_top; 1388789Sahrens ASSERT(tvd->vdev_parent == rvd); 1389789Sahrens 1390789Sahrens /* 1391789Sahrens * Reopen this top-level vdev to reassess health after detach. 1392789Sahrens */ 13931544Seschrock vdev_reopen(tvd); 1394789Sahrens 1395789Sahrens /* 1396789Sahrens * If the device we just detached was smaller than the others, 13971544Seschrock * it may be possible to add metaslabs (i.e. grow the pool). We ignore 13981544Seschrock * the error here because the detach still succeeded - we just weren't 13991544Seschrock * able to reinitialize the metaslabs. This pool is in for a world of 14001544Seschrock * hurt, in any case. 1401789Sahrens */ 14021544Seschrock (void) vdev_metaslab_init(tvd, txg); 1403789Sahrens 1404789Sahrens vdev_config_dirty(tvd); 1405789Sahrens 1406789Sahrens /* 1407789Sahrens * Mark vd's DTL as dirty in this txg. 1408789Sahrens * vdev_dtl_sync() will see that vd->vdev_detached is set 1409789Sahrens * and free vd's DTL object in syncing context. 1410789Sahrens * But first make sure we're not on any *other* txg's DTL list, 1411789Sahrens * to prevent vd from being accessed after it's freed. 1412789Sahrens */ 1413789Sahrens vdev_dirty(tvd, VDD_DTL, txg); 1414789Sahrens vd->vdev_detached = B_TRUE; 1415789Sahrens for (t = 0; t < TXG_SIZE; t++) 1416789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 1417789Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, txg); 1418789Sahrens 14191544Seschrock dprintf("detached %s in txg %llu\n", vd->vdev_path, txg); 1420789Sahrens 1421789Sahrens return (spa_vdev_exit(spa, vd, txg, 0)); 1422789Sahrens } 1423789Sahrens 1424789Sahrens /* 14251544Seschrock * Find any device that's done replacing, so we can detach it. 1426789Sahrens */ 14271544Seschrock static vdev_t * 14281544Seschrock spa_vdev_replace_done_hunt(vdev_t *vd) 1429789Sahrens { 14301544Seschrock vdev_t *newvd, *oldvd; 1431789Sahrens int c; 1432789Sahrens 14331544Seschrock for (c = 0; c < vd->vdev_children; c++) { 14341544Seschrock oldvd = spa_vdev_replace_done_hunt(vd->vdev_child[c]); 14351544Seschrock if (oldvd != NULL) 14361544Seschrock return (oldvd); 14371544Seschrock } 1438789Sahrens 1439789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 14401544Seschrock oldvd = vd->vdev_child[0]; 14411544Seschrock newvd = vd->vdev_child[1]; 1442789Sahrens 14431544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 14441544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 14451544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 14461544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 14471544Seschrock return (oldvd); 14481544Seschrock } 14491544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 14501544Seschrock } 1451789Sahrens 14521544Seschrock return (NULL); 1453789Sahrens } 1454789Sahrens 14551544Seschrock static void 1456789Sahrens spa_vdev_replace_done(spa_t *spa) 1457789Sahrens { 14581544Seschrock vdev_t *vd; 14591544Seschrock uint64_t guid; 1460789Sahrens 14611544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1462789Sahrens 14631544Seschrock while ((vd = spa_vdev_replace_done_hunt(spa->spa_root_vdev)) != NULL) { 14641544Seschrock guid = vd->vdev_guid; 14651544Seschrock spa_config_exit(spa, FTAG); 14661544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 14671544Seschrock return; 14681544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1469789Sahrens } 1470789Sahrens 14711544Seschrock spa_config_exit(spa, FTAG); 1472789Sahrens } 1473789Sahrens 1474789Sahrens /* 14751354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 14761354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 14771354Seschrock */ 14781354Seschrock int 14791354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 14801354Seschrock { 14811354Seschrock vdev_t *rvd, *vd; 14821354Seschrock uint64_t txg; 14831354Seschrock 14841354Seschrock rvd = spa->spa_root_vdev; 14851354Seschrock 14861354Seschrock txg = spa_vdev_enter(spa); 14871354Seschrock 14881354Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 14891354Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 14901354Seschrock 1491*1585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 1492*1585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1493*1585Sbonwick 14941354Seschrock spa_strfree(vd->vdev_path); 14951354Seschrock vd->vdev_path = spa_strdup(newpath); 14961354Seschrock 14971354Seschrock vdev_config_dirty(vd->vdev_top); 14981354Seschrock 14991354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 15001354Seschrock } 15011354Seschrock 15021354Seschrock /* 1503789Sahrens * ========================================================================== 1504789Sahrens * SPA Scrubbing 1505789Sahrens * ========================================================================== 1506789Sahrens */ 1507789Sahrens 15081544Seschrock void 15091544Seschrock spa_scrub_throttle(spa_t *spa, int direction) 15101544Seschrock { 15111544Seschrock mutex_enter(&spa->spa_scrub_lock); 15121544Seschrock spa->spa_scrub_throttled += direction; 15131544Seschrock ASSERT(spa->spa_scrub_throttled >= 0); 15141544Seschrock if (spa->spa_scrub_throttled == 0) 15151544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 15161544Seschrock mutex_exit(&spa->spa_scrub_lock); 15171544Seschrock } 1518789Sahrens 1519789Sahrens static void 1520789Sahrens spa_scrub_io_done(zio_t *zio) 1521789Sahrens { 1522789Sahrens spa_t *spa = zio->io_spa; 1523789Sahrens 1524789Sahrens zio_buf_free(zio->io_data, zio->io_size); 1525789Sahrens 1526789Sahrens mutex_enter(&spa->spa_scrub_lock); 15271544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 15281544Seschrock vdev_t *vd = zio->io_vd; 1529789Sahrens spa->spa_scrub_errors++; 1530789Sahrens mutex_enter(&vd->vdev_stat_lock); 1531789Sahrens vd->vdev_stat.vs_scrub_errors++; 1532789Sahrens mutex_exit(&vd->vdev_stat_lock); 1533789Sahrens } 15341544Seschrock if (--spa->spa_scrub_inflight == 0) { 15351544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 15361544Seschrock ASSERT(spa->spa_scrub_throttled == 0); 15371544Seschrock } 15381544Seschrock mutex_exit(&spa->spa_scrub_lock); 1539789Sahrens } 1540789Sahrens 1541789Sahrens static void 15421544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 15431544Seschrock zbookmark_t *zb) 1544789Sahrens { 1545789Sahrens size_t size = BP_GET_LSIZE(bp); 1546789Sahrens void *data = zio_buf_alloc(size); 1547789Sahrens 1548789Sahrens mutex_enter(&spa->spa_scrub_lock); 1549789Sahrens spa->spa_scrub_inflight++; 1550789Sahrens mutex_exit(&spa->spa_scrub_lock); 1551789Sahrens 15521544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 15531544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 15541544Seschrock 15551544Seschrock flags |= ZIO_FLAG_CANFAIL; 15561544Seschrock 1557789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 15581544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 1559789Sahrens } 1560789Sahrens 1561789Sahrens /* ARGSUSED */ 1562789Sahrens static int 1563789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 1564789Sahrens { 1565789Sahrens blkptr_t *bp = &bc->bc_blkptr; 1566789Sahrens vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[0])); 1567789Sahrens 1568789Sahrens if (bc->bc_errno || vd == NULL) { 1569789Sahrens /* 1570789Sahrens * We can't scrub this block, but we can continue to scrub 1571789Sahrens * the rest of the pool. Note the error and move along. 1572789Sahrens */ 1573789Sahrens mutex_enter(&spa->spa_scrub_lock); 1574789Sahrens spa->spa_scrub_errors++; 1575789Sahrens mutex_exit(&spa->spa_scrub_lock); 1576789Sahrens 1577789Sahrens if (vd != NULL) { 1578789Sahrens mutex_enter(&vd->vdev_stat_lock); 1579789Sahrens vd->vdev_stat.vs_scrub_errors++; 1580789Sahrens mutex_exit(&vd->vdev_stat_lock); 1581789Sahrens } 1582789Sahrens 1583789Sahrens return (ERESTART); 1584789Sahrens } 1585789Sahrens 1586789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 1587789Sahrens 1588789Sahrens /* 1589789Sahrens * Keep track of how much data we've examined so that 1590789Sahrens * zpool(1M) status can make useful progress reports. 1591789Sahrens */ 1592789Sahrens mutex_enter(&vd->vdev_stat_lock); 1593789Sahrens vd->vdev_stat.vs_scrub_examined += BP_GET_ASIZE(bp); 1594789Sahrens mutex_exit(&vd->vdev_stat_lock); 1595789Sahrens 1596789Sahrens if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 1597789Sahrens if (DVA_GET_GANG(&bp->blk_dva[0])) { 1598789Sahrens /* 1599789Sahrens * Gang members may be spread across multiple vdevs, 1600789Sahrens * so the best we can do is look at the pool-wide DTL. 1601789Sahrens * XXX -- it would be better to change our allocation 1602789Sahrens * policy to ensure that this can't happen. 1603789Sahrens */ 1604789Sahrens vd = spa->spa_root_vdev; 1605789Sahrens } 1606789Sahrens if (vdev_dtl_contains(&vd->vdev_dtl_map, bp->blk_birth, 1)) { 1607789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 16081544Seschrock ZIO_FLAG_RESILVER, &bc->bc_bookmark); 1609789Sahrens } 1610789Sahrens } else { 1611789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 16121544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 1613789Sahrens } 1614789Sahrens 1615789Sahrens return (0); 1616789Sahrens } 1617789Sahrens 1618789Sahrens static void 1619789Sahrens spa_scrub_thread(spa_t *spa) 1620789Sahrens { 1621789Sahrens callb_cpr_t cprinfo; 1622789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 1623789Sahrens vdev_t *rvd = spa->spa_root_vdev; 1624789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 1625789Sahrens int error = 0; 1626789Sahrens boolean_t complete; 1627789Sahrens 1628789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 1629789Sahrens 1630797Sbonwick /* 1631797Sbonwick * If we're restarting due to a snapshot create/delete, 1632797Sbonwick * wait for that to complete. 1633797Sbonwick */ 1634797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 1635797Sbonwick 16361544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 16371544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 16381544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 16391544Seschrock 16401544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 16411544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 1642789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 1643789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 16441544Seschrock spa_config_exit(spa, FTAG); 1645789Sahrens 1646789Sahrens mutex_enter(&spa->spa_scrub_lock); 1647789Sahrens spa->spa_scrub_errors = 0; 1648789Sahrens spa->spa_scrub_active = 1; 16491544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 16501544Seschrock ASSERT(spa->spa_scrub_throttled == 0); 1651789Sahrens 1652789Sahrens while (!spa->spa_scrub_stop) { 1653789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 16541544Seschrock while (spa->spa_scrub_suspended) { 1655789Sahrens spa->spa_scrub_active = 0; 1656789Sahrens cv_broadcast(&spa->spa_scrub_cv); 1657789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 1658789Sahrens spa->spa_scrub_active = 1; 1659789Sahrens } 1660789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 1661789Sahrens 1662789Sahrens if (spa->spa_scrub_restart_txg != 0) 1663789Sahrens break; 1664789Sahrens 1665789Sahrens mutex_exit(&spa->spa_scrub_lock); 1666789Sahrens error = traverse_more(th); 1667789Sahrens mutex_enter(&spa->spa_scrub_lock); 1668789Sahrens if (error != EAGAIN) 1669789Sahrens break; 16701544Seschrock 16711544Seschrock while (spa->spa_scrub_throttled > 0) 16721544Seschrock cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1673789Sahrens } 1674789Sahrens 1675789Sahrens while (spa->spa_scrub_inflight) 1676789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1677789Sahrens 1678789Sahrens if (spa->spa_scrub_restart_txg != 0) 1679789Sahrens error = ERESTART; 1680789Sahrens 16811544Seschrock if (spa->spa_scrub_stop) 16821544Seschrock error = EINTR; 16831544Seschrock 1684789Sahrens spa->spa_scrub_active = 0; 1685789Sahrens cv_broadcast(&spa->spa_scrub_cv); 1686789Sahrens 1687789Sahrens /* 16881544Seschrock * Even if there were uncorrectable errors, we consider the scrub 16891544Seschrock * completed. The downside is that if there is a transient error during 16901544Seschrock * a resilver, we won't resilver the data properly to the target. But 16911544Seschrock * if the damage is permanent (more likely) we will resilver forever, 16921544Seschrock * which isn't really acceptable. Since there is enough information for 16931544Seschrock * the user to know what has failed and why, this seems like a more 16941544Seschrock * tractable approach. 1695789Sahrens */ 16961544Seschrock complete = (error == 0); 1697789Sahrens 16981544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 16991544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 1700789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 1701789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 1702789Sahrens 1703789Sahrens mutex_exit(&spa->spa_scrub_lock); 1704789Sahrens 1705789Sahrens /* 1706789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 1707789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 1708789Sahrens */ 17091544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1710789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 1711789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 1712789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 17131544Seschrock spa_errlog_rotate(spa); 17141544Seschrock spa_config_exit(spa, FTAG); 1715789Sahrens 1716789Sahrens mutex_enter(&spa->spa_scrub_lock); 1717789Sahrens 17181544Seschrock /* 17191544Seschrock * We may have finished replacing a device. 17201544Seschrock * Let the async thread assess this and handle the detach. 17211544Seschrock */ 17221544Seschrock spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 1723789Sahrens 1724789Sahrens /* 1725789Sahrens * If we were told to restart, our final act is to start a new scrub. 1726789Sahrens */ 1727789Sahrens if (error == ERESTART) 17281544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 17291544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 1730789Sahrens 17311544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 17321544Seschrock spa->spa_scrub_active = 0; 17331544Seschrock spa->spa_scrub_thread = NULL; 17341544Seschrock cv_broadcast(&spa->spa_scrub_cv); 1735789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 1736789Sahrens thread_exit(); 1737789Sahrens } 1738789Sahrens 1739789Sahrens void 1740789Sahrens spa_scrub_suspend(spa_t *spa) 1741789Sahrens { 1742789Sahrens mutex_enter(&spa->spa_scrub_lock); 17431544Seschrock spa->spa_scrub_suspended++; 1744789Sahrens while (spa->spa_scrub_active) { 1745789Sahrens cv_broadcast(&spa->spa_scrub_cv); 1746789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 1747789Sahrens } 1748789Sahrens while (spa->spa_scrub_inflight) 1749789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1750789Sahrens mutex_exit(&spa->spa_scrub_lock); 1751789Sahrens } 1752789Sahrens 1753789Sahrens void 1754789Sahrens spa_scrub_resume(spa_t *spa) 1755789Sahrens { 1756789Sahrens mutex_enter(&spa->spa_scrub_lock); 17571544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 17581544Seschrock if (--spa->spa_scrub_suspended == 0) 1759789Sahrens cv_broadcast(&spa->spa_scrub_cv); 1760789Sahrens mutex_exit(&spa->spa_scrub_lock); 1761789Sahrens } 1762789Sahrens 1763789Sahrens void 1764789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 1765789Sahrens { 1766789Sahrens /* 1767789Sahrens * Something happened (e.g. snapshot create/delete) that means 1768789Sahrens * we must restart any in-progress scrubs. The itinerary will 1769789Sahrens * fix this properly. 1770789Sahrens */ 1771789Sahrens mutex_enter(&spa->spa_scrub_lock); 1772789Sahrens spa->spa_scrub_restart_txg = txg; 1773789Sahrens mutex_exit(&spa->spa_scrub_lock); 1774789Sahrens } 1775789Sahrens 17761544Seschrock int 17771544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 1778789Sahrens { 1779789Sahrens space_seg_t *ss; 1780789Sahrens uint64_t mintxg, maxtxg; 1781789Sahrens vdev_t *rvd = spa->spa_root_vdev; 17821544Seschrock int advance = ADVANCE_PRE | ADVANCE_ZIL; 1783789Sahrens 1784789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 1785789Sahrens return (ENOTSUP); 1786789Sahrens 17871544Seschrock mutex_enter(&spa->spa_scrub_lock); 17881544Seschrock 1789789Sahrens /* 1790789Sahrens * If there's a scrub or resilver already in progress, stop it. 1791789Sahrens */ 1792789Sahrens while (spa->spa_scrub_thread != NULL) { 1793789Sahrens /* 1794789Sahrens * Don't stop a resilver unless forced. 1795789Sahrens */ 17961544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 17971544Seschrock mutex_exit(&spa->spa_scrub_lock); 1798789Sahrens return (EBUSY); 17991544Seschrock } 1800789Sahrens spa->spa_scrub_stop = 1; 1801789Sahrens cv_broadcast(&spa->spa_scrub_cv); 1802789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 1803789Sahrens } 1804789Sahrens 1805789Sahrens /* 1806789Sahrens * Terminate the previous traverse. 1807789Sahrens */ 1808789Sahrens if (spa->spa_scrub_th != NULL) { 1809789Sahrens traverse_fini(spa->spa_scrub_th); 1810789Sahrens spa->spa_scrub_th = NULL; 1811789Sahrens } 1812789Sahrens 18131544Seschrock if (rvd == NULL) { 18141544Seschrock ASSERT(spa->spa_scrub_stop == 0); 18151544Seschrock ASSERT(spa->spa_scrub_type == type); 18161544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 18171544Seschrock mutex_exit(&spa->spa_scrub_lock); 18181544Seschrock return (0); 18191544Seschrock } 1820789Sahrens 1821789Sahrens mintxg = TXG_INITIAL - 1; 1822789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 1823789Sahrens 18241544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 1825789Sahrens 18261544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 18271544Seschrock /* 18281544Seschrock * The pool-wide DTL is empty. 18291544Seschrock * If this is a resilver, there's nothing to do. 18301544Seschrock */ 18311544Seschrock if (type == POOL_SCRUB_RESILVER) 18321544Seschrock type = POOL_SCRUB_NONE; 18331544Seschrock } else { 18341544Seschrock /* 18351544Seschrock * The pool-wide DTL is non-empty. 18361544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 18371544Seschrock */ 18381544Seschrock if (type == POOL_SCRUB_EVERYTHING) 18391544Seschrock type = POOL_SCRUB_RESILVER; 18401544Seschrock } 1841789Sahrens 18421544Seschrock if (type == POOL_SCRUB_RESILVER) { 1843789Sahrens /* 1844789Sahrens * Determine the resilvering boundaries. 1845789Sahrens * 1846789Sahrens * Note: (mintxg, maxtxg) is an open interval, 1847789Sahrens * i.e. mintxg and maxtxg themselves are not included. 1848789Sahrens * 1849789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 1850789Sahrens * so we don't claim to resilver a txg that's still changing. 1851789Sahrens */ 1852789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 18531544Seschrock mintxg = ss->ss_start - 1; 1854789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 18551544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 1856789Sahrens 18571544Seschrock advance |= ADVANCE_PRUNE; 1858789Sahrens } 1859789Sahrens 18601544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 18611544Seschrock 18621544Seschrock spa->spa_scrub_stop = 0; 18631544Seschrock spa->spa_scrub_type = type; 18641544Seschrock spa->spa_scrub_restart_txg = 0; 18651544Seschrock 18661544Seschrock if (type != POOL_SCRUB_NONE) { 18671544Seschrock spa->spa_scrub_mintxg = mintxg; 1868789Sahrens spa->spa_scrub_maxtxg = maxtxg; 1869789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 1870789Sahrens advance, ZIO_FLAG_CANFAIL); 1871789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 1872789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 1873789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 1874789Sahrens } 1875789Sahrens 18761544Seschrock mutex_exit(&spa->spa_scrub_lock); 18771544Seschrock 1878789Sahrens return (0); 1879789Sahrens } 1880789Sahrens 18811544Seschrock /* 18821544Seschrock * ========================================================================== 18831544Seschrock * SPA async task processing 18841544Seschrock * ========================================================================== 18851544Seschrock */ 18861544Seschrock 18871544Seschrock static void 18881544Seschrock spa_async_reopen(spa_t *spa) 1889789Sahrens { 18901544Seschrock vdev_t *rvd = spa->spa_root_vdev; 18911544Seschrock vdev_t *tvd; 18921544Seschrock int c; 18931544Seschrock 18941544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 18951544Seschrock 18961544Seschrock for (c = 0; c < rvd->vdev_children; c++) { 18971544Seschrock tvd = rvd->vdev_child[c]; 18981544Seschrock if (tvd->vdev_reopen_wanted) { 18991544Seschrock tvd->vdev_reopen_wanted = 0; 19001544Seschrock vdev_reopen(tvd); 19011544Seschrock } 19021544Seschrock } 1903789Sahrens 19041544Seschrock spa_config_exit(spa, FTAG); 19051544Seschrock } 19061544Seschrock 19071544Seschrock static void 19081544Seschrock spa_async_thread(spa_t *spa) 19091544Seschrock { 19101544Seschrock int tasks; 19111544Seschrock 19121544Seschrock ASSERT(spa->spa_sync_on); 1913789Sahrens 19141544Seschrock mutex_enter(&spa->spa_async_lock); 19151544Seschrock tasks = spa->spa_async_tasks; 19161544Seschrock spa->spa_async_tasks = 0; 19171544Seschrock mutex_exit(&spa->spa_async_lock); 19181544Seschrock 19191544Seschrock /* 19201544Seschrock * See if any devices need to be reopened. 19211544Seschrock */ 19221544Seschrock if (tasks & SPA_ASYNC_REOPEN) 19231544Seschrock spa_async_reopen(spa); 19241544Seschrock 19251544Seschrock /* 19261544Seschrock * If any devices are done replacing, detach them. 19271544Seschrock */ 19281544Seschrock if (tasks & SPA_ASYNC_REPLACE_DONE) 1929789Sahrens spa_vdev_replace_done(spa); 1930789Sahrens 19311544Seschrock /* 19321544Seschrock * Kick off a scrub. 19331544Seschrock */ 19341544Seschrock if (tasks & SPA_ASYNC_SCRUB) 19351544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 19361544Seschrock 19371544Seschrock /* 19381544Seschrock * Kick off a resilver. 19391544Seschrock */ 19401544Seschrock if (tasks & SPA_ASYNC_RESILVER) 19411544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 19421544Seschrock 19431544Seschrock /* 19441544Seschrock * Let the world know that we're done. 19451544Seschrock */ 19461544Seschrock mutex_enter(&spa->spa_async_lock); 19471544Seschrock spa->spa_async_thread = NULL; 19481544Seschrock cv_broadcast(&spa->spa_async_cv); 19491544Seschrock mutex_exit(&spa->spa_async_lock); 19501544Seschrock thread_exit(); 19511544Seschrock } 19521544Seschrock 19531544Seschrock void 19541544Seschrock spa_async_suspend(spa_t *spa) 19551544Seschrock { 19561544Seschrock mutex_enter(&spa->spa_async_lock); 19571544Seschrock spa->spa_async_suspended++; 19581544Seschrock while (spa->spa_async_thread != NULL) 19591544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 19601544Seschrock mutex_exit(&spa->spa_async_lock); 19611544Seschrock } 19621544Seschrock 19631544Seschrock void 19641544Seschrock spa_async_resume(spa_t *spa) 19651544Seschrock { 19661544Seschrock mutex_enter(&spa->spa_async_lock); 19671544Seschrock ASSERT(spa->spa_async_suspended != 0); 19681544Seschrock spa->spa_async_suspended--; 19691544Seschrock mutex_exit(&spa->spa_async_lock); 19701544Seschrock } 19711544Seschrock 19721544Seschrock static void 19731544Seschrock spa_async_dispatch(spa_t *spa) 19741544Seschrock { 19751544Seschrock mutex_enter(&spa->spa_async_lock); 19761544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 19771544Seschrock spa->spa_async_thread == NULL) 19781544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 19791544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 19801544Seschrock mutex_exit(&spa->spa_async_lock); 19811544Seschrock } 19821544Seschrock 19831544Seschrock void 19841544Seschrock spa_async_request(spa_t *spa, int task) 19851544Seschrock { 19861544Seschrock mutex_enter(&spa->spa_async_lock); 19871544Seschrock spa->spa_async_tasks |= task; 19881544Seschrock mutex_exit(&spa->spa_async_lock); 1989789Sahrens } 1990789Sahrens 1991789Sahrens /* 1992789Sahrens * ========================================================================== 1993789Sahrens * SPA syncing routines 1994789Sahrens * ========================================================================== 1995789Sahrens */ 1996789Sahrens 1997789Sahrens static void 1998789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 1999789Sahrens { 2000789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2001789Sahrens dmu_tx_t *tx; 2002789Sahrens blkptr_t blk; 2003789Sahrens uint64_t itor = 0; 2004789Sahrens zio_t *zio; 2005789Sahrens int error; 2006789Sahrens uint8_t c = 1; 2007789Sahrens 2008789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2009789Sahrens 2010789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2011789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2012789Sahrens 2013789Sahrens error = zio_wait(zio); 2014789Sahrens ASSERT3U(error, ==, 0); 2015789Sahrens 2016789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2017789Sahrens bplist_vacate(bpl, tx); 2018789Sahrens 2019789Sahrens /* 2020789Sahrens * Pre-dirty the first block so we sync to convergence faster. 2021789Sahrens * (Usually only the first block is needed.) 2022789Sahrens */ 2023789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2024789Sahrens dmu_tx_commit(tx); 2025789Sahrens } 2026789Sahrens 2027789Sahrens static void 2028789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2029789Sahrens { 2030789Sahrens nvlist_t *config; 2031789Sahrens char *packed = NULL; 2032789Sahrens size_t nvsize = 0; 2033789Sahrens dmu_buf_t *db; 2034789Sahrens 2035789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2036789Sahrens return; 2037789Sahrens 2038789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2039789Sahrens 2040789Sahrens spa_config_set(spa, config); 2041789Sahrens 2042789Sahrens VERIFY(nvlist_size(config, &nvsize, NV_ENCODE_XDR) == 0); 2043789Sahrens 2044789Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 2045789Sahrens 20461544Seschrock VERIFY(nvlist_pack(config, &packed, &nvsize, NV_ENCODE_XDR, 20471544Seschrock KM_SLEEP) == 0); 2048789Sahrens 2049789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_config_object, 0, nvsize, 2050789Sahrens packed, tx); 2051789Sahrens 2052789Sahrens kmem_free(packed, nvsize); 2053789Sahrens 20541544Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, 20551544Seschrock spa->spa_config_object, FTAG, &db)); 2056789Sahrens dmu_buf_will_dirty(db, tx); 2057789Sahrens *(uint64_t *)db->db_data = nvsize; 20581544Seschrock dmu_buf_rele(db, FTAG); 2059789Sahrens } 2060789Sahrens 2061789Sahrens /* 2062789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 2063789Sahrens * part of the process, so we iterate until it converges. 2064789Sahrens */ 2065789Sahrens void 2066789Sahrens spa_sync(spa_t *spa, uint64_t txg) 2067789Sahrens { 2068789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 2069789Sahrens objset_t *mos = spa->spa_meta_objset; 2070789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2071789Sahrens vdev_t *vd; 2072789Sahrens dmu_tx_t *tx; 2073789Sahrens int dirty_vdevs; 2074789Sahrens 2075789Sahrens /* 2076789Sahrens * Lock out configuration changes. 2077789Sahrens */ 20781544Seschrock spa_config_enter(spa, RW_READER, FTAG); 2079789Sahrens 2080789Sahrens spa->spa_syncing_txg = txg; 2081789Sahrens spa->spa_sync_pass = 0; 2082789Sahrens 20831544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 2084789Sahrens 2085789Sahrens /* 2086789Sahrens * If anything has changed in this txg, push the deferred frees 2087789Sahrens * from the previous txg. If not, leave them alone so that we 2088789Sahrens * don't generate work on an otherwise idle system. 2089789Sahrens */ 2090789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 2091789Sahrens !txg_list_empty(&dp->dp_dirty_dirs, txg)) 2092789Sahrens spa_sync_deferred_frees(spa, txg); 2093789Sahrens 2094789Sahrens /* 2095789Sahrens * Iterate to convergence. 2096789Sahrens */ 2097789Sahrens do { 2098789Sahrens spa->spa_sync_pass++; 2099789Sahrens 2100789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2101789Sahrens spa_sync_config_object(spa, tx); 2102789Sahrens dmu_tx_commit(tx); 2103789Sahrens 21041544Seschrock spa_errlog_sync(spa, txg); 21051544Seschrock 2106789Sahrens dsl_pool_sync(dp, txg); 2107789Sahrens 2108789Sahrens dirty_vdevs = 0; 2109789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 2110789Sahrens vdev_sync(vd, txg); 2111789Sahrens dirty_vdevs++; 2112789Sahrens } 2113789Sahrens 2114789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2115789Sahrens bplist_sync(bpl, tx); 2116789Sahrens dmu_tx_commit(tx); 2117789Sahrens 2118789Sahrens } while (dirty_vdevs); 2119789Sahrens 2120789Sahrens bplist_close(bpl); 2121789Sahrens 2122789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 2123789Sahrens 2124789Sahrens /* 2125789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 2126789Sahrens * to commit the transaction group. 2127789Sahrens */ 21281544Seschrock VERIFY(0 == spa_sync_labels(spa, txg)); 2129789Sahrens 2130789Sahrens /* 2131789Sahrens * Make a stable copy of the fully synced uberblock. 2132789Sahrens * We use this as the root for pool traversals. 2133789Sahrens */ 2134789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 2135789Sahrens 2136789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 2137789Sahrens 2138789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 2139789Sahrens spa->spa_traverse_wanted = 0; 2140789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2141789Sahrens rw_exit(&spa->spa_traverse_lock); 2142789Sahrens 2143789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 2144789Sahrens 2145789Sahrens /* 2146789Sahrens * Clean up the ZIL records for the synced txg. 2147789Sahrens */ 2148789Sahrens dsl_pool_zil_clean(dp); 2149789Sahrens 2150789Sahrens /* 2151789Sahrens * Update usable space statistics. 2152789Sahrens */ 2153789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 2154789Sahrens vdev_sync_done(vd, txg); 2155789Sahrens 2156789Sahrens /* 2157789Sahrens * It had better be the case that we didn't dirty anything 2158789Sahrens * since spa_sync_labels(). 2159789Sahrens */ 2160789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 2161789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 2162789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 2163789Sahrens ASSERT(bpl->bpl_queue == NULL); 2164789Sahrens 21651544Seschrock spa_config_exit(spa, FTAG); 21661544Seschrock 21671544Seschrock /* 21681544Seschrock * If any async tasks have been requested, kick them off. 21691544Seschrock */ 21701544Seschrock spa_async_dispatch(spa); 2171789Sahrens } 2172789Sahrens 2173789Sahrens /* 2174789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 2175789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 2176789Sahrens * sync. 2177789Sahrens */ 2178789Sahrens void 2179789Sahrens spa_sync_allpools(void) 2180789Sahrens { 2181789Sahrens spa_t *spa = NULL; 2182789Sahrens mutex_enter(&spa_namespace_lock); 2183789Sahrens while ((spa = spa_next(spa)) != NULL) { 2184789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 2185789Sahrens continue; 2186789Sahrens spa_open_ref(spa, FTAG); 2187789Sahrens mutex_exit(&spa_namespace_lock); 2188789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 2189789Sahrens mutex_enter(&spa_namespace_lock); 2190789Sahrens spa_close(spa, FTAG); 2191789Sahrens } 2192789Sahrens mutex_exit(&spa_namespace_lock); 2193789Sahrens } 2194789Sahrens 2195789Sahrens /* 2196789Sahrens * ========================================================================== 2197789Sahrens * Miscellaneous routines 2198789Sahrens * ========================================================================== 2199789Sahrens */ 2200789Sahrens 2201789Sahrens int 2202789Sahrens spa_busy(void) 2203789Sahrens { 2204789Sahrens return (spa_active_count != 0); 2205789Sahrens } 2206789Sahrens 2207789Sahrens /* 2208789Sahrens * Remove all pools in the system. 2209789Sahrens */ 2210789Sahrens void 2211789Sahrens spa_evict_all(void) 2212789Sahrens { 2213789Sahrens spa_t *spa; 2214789Sahrens 2215789Sahrens /* 2216789Sahrens * Remove all cached state. All pools should be closed now, 2217789Sahrens * so every spa in the AVL tree should be unreferenced. 2218789Sahrens */ 2219789Sahrens mutex_enter(&spa_namespace_lock); 2220789Sahrens while ((spa = spa_next(NULL)) != NULL) { 2221789Sahrens /* 22221544Seschrock * Stop async tasks. The async thread may need to detach 22231544Seschrock * a device that's been replaced, which requires grabbing 22241544Seschrock * spa_namespace_lock, so we must drop it here. 2225789Sahrens */ 2226789Sahrens spa_open_ref(spa, FTAG); 2227789Sahrens mutex_exit(&spa_namespace_lock); 22281544Seschrock spa_async_suspend(spa); 2229789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2230789Sahrens mutex_enter(&spa_namespace_lock); 2231789Sahrens spa_close(spa, FTAG); 2232789Sahrens 2233789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2234789Sahrens spa_unload(spa); 2235789Sahrens spa_deactivate(spa); 2236789Sahrens } 2237789Sahrens spa_remove(spa); 2238789Sahrens } 2239789Sahrens mutex_exit(&spa_namespace_lock); 2240789Sahrens } 22411544Seschrock 22421544Seschrock vdev_t * 22431544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 22441544Seschrock { 22451544Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 22461544Seschrock } 2247