1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22c67d9675Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 30fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 31fa9e4066Sahrens * pool. 32fa9e4066Sahrens */ 33fa9e4066Sahrens 34fa9e4066Sahrens #include <sys/zfs_context.h> 35ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 36fa9e4066Sahrens #include <sys/spa_impl.h> 37fa9e4066Sahrens #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/zio_checksum.h> 39fa9e4066Sahrens #include <sys/zio_compress.h> 40fa9e4066Sahrens #include <sys/dmu.h> 41fa9e4066Sahrens #include <sys/dmu_tx.h> 42fa9e4066Sahrens #include <sys/zap.h> 43fa9e4066Sahrens #include <sys/zil.h> 44fa9e4066Sahrens #include <sys/vdev_impl.h> 45fa9e4066Sahrens #include <sys/metaslab.h> 46fa9e4066Sahrens #include <sys/uberblock_impl.h> 47fa9e4066Sahrens #include <sys/txg.h> 48fa9e4066Sahrens #include <sys/avl.h> 49fa9e4066Sahrens #include <sys/dmu_traverse.h> 50fa9e4066Sahrens #include <sys/unique.h> 51fa9e4066Sahrens #include <sys/dsl_pool.h> 52fa9e4066Sahrens #include <sys/dsl_dir.h> 53fa9e4066Sahrens #include <sys/dsl_prop.h> 54fa9e4066Sahrens #include <sys/fs/zfs.h> 55fa9e4066Sahrens #include <sys/callb.h> 56fa9e4066Sahrens 57fa9e4066Sahrens /* 58fa9e4066Sahrens * ========================================================================== 59fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 60fa9e4066Sahrens * ========================================================================== 61fa9e4066Sahrens */ 62fa9e4066Sahrens 63ea8dc4b6Seschrock static int 64ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 65ea8dc4b6Seschrock { 66ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 67ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 68ea8dc4b6Seschrock int ret; 69ea8dc4b6Seschrock 70ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 71ea8dc4b6Seschrock sizeof (zbookmark_t)); 72ea8dc4b6Seschrock 73ea8dc4b6Seschrock if (ret < 0) 74ea8dc4b6Seschrock return (-1); 75ea8dc4b6Seschrock else if (ret > 0) 76ea8dc4b6Seschrock return (1); 77ea8dc4b6Seschrock else 78ea8dc4b6Seschrock return (0); 79ea8dc4b6Seschrock } 80ea8dc4b6Seschrock 81ea8dc4b6Seschrock /* 82ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 83ea8dc4b6Seschrock * re-initializes them in the process. 84ea8dc4b6Seschrock */ 85ea8dc4b6Seschrock void 86ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 87ea8dc4b6Seschrock { 88ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 89ea8dc4b6Seschrock 90ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 91ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 92ea8dc4b6Seschrock 93ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 94ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 95ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 96ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 97ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 98ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 99ea8dc4b6Seschrock } 100ea8dc4b6Seschrock 101fa9e4066Sahrens /* 102fa9e4066Sahrens * Activate an uninitialized pool. 103fa9e4066Sahrens */ 104fa9e4066Sahrens static void 105fa9e4066Sahrens spa_activate(spa_t *spa) 106fa9e4066Sahrens { 107fa9e4066Sahrens int t; 108fa9e4066Sahrens 109fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 110fa9e4066Sahrens 111fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 112fa9e4066Sahrens 113fa9e4066Sahrens spa->spa_normal_class = metaslab_class_create(); 114fa9e4066Sahrens 115fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 116fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 117fa9e4066Sahrens 8, maxclsyspri, 50, INT_MAX, 118fa9e4066Sahrens TASKQ_PREPOPULATE); 119fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 120fa9e4066Sahrens 8, maxclsyspri, 50, INT_MAX, 121fa9e4066Sahrens TASKQ_PREPOPULATE); 122fa9e4066Sahrens } 123fa9e4066Sahrens 124fa9e4066Sahrens rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 125fa9e4066Sahrens 126fa9e4066Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 127fa9e4066Sahrens offsetof(vdev_t, vdev_dirty_node)); 128fa9e4066Sahrens 129fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 130fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 131ea8dc4b6Seschrock 132ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 133ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 134ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 135ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 136ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 137ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 138fa9e4066Sahrens } 139fa9e4066Sahrens 140fa9e4066Sahrens /* 141fa9e4066Sahrens * Opposite of spa_activate(). 142fa9e4066Sahrens */ 143fa9e4066Sahrens static void 144fa9e4066Sahrens spa_deactivate(spa_t *spa) 145fa9e4066Sahrens { 146fa9e4066Sahrens int t; 147fa9e4066Sahrens 148fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 149fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 150fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 151fa9e4066Sahrens 152fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 153fa9e4066Sahrens 154fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 155fa9e4066Sahrens 156fa9e4066Sahrens list_destroy(&spa->spa_dirty_list); 157fa9e4066Sahrens 158fa9e4066Sahrens rw_destroy(&spa->spa_traverse_lock); 159fa9e4066Sahrens 160fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 161fa9e4066Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 162fa9e4066Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 163fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = NULL; 164fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = NULL; 165fa9e4066Sahrens } 166fa9e4066Sahrens 167fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 168fa9e4066Sahrens spa->spa_normal_class = NULL; 169fa9e4066Sahrens 170ea8dc4b6Seschrock /* 171ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 172ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 173ea8dc4b6Seschrock */ 174ea8dc4b6Seschrock spa_errlog_drain(spa); 175ea8dc4b6Seschrock 176ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 177ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 178ea8dc4b6Seschrock 179fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 180fa9e4066Sahrens } 181fa9e4066Sahrens 182fa9e4066Sahrens /* 183fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 184fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 185fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 186fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 187fa9e4066Sahrens */ 188fa9e4066Sahrens static vdev_t * 189fa9e4066Sahrens spa_config_parse(spa_t *spa, nvlist_t *nv, vdev_t *parent, uint_t id, int atype) 190fa9e4066Sahrens { 191fa9e4066Sahrens nvlist_t **child; 192fa9e4066Sahrens uint_t c, children; 193fa9e4066Sahrens vdev_t *vd; 194fa9e4066Sahrens 195fa9e4066Sahrens if ((vd = vdev_alloc(spa, nv, parent, id, atype)) == NULL) 196fa9e4066Sahrens return (NULL); 197fa9e4066Sahrens 198fa9e4066Sahrens if (vd->vdev_ops->vdev_op_leaf) 199fa9e4066Sahrens return (vd); 200fa9e4066Sahrens 201fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 202fa9e4066Sahrens &child, &children) != 0) { 203fa9e4066Sahrens vdev_free(vd); 204fa9e4066Sahrens return (NULL); 205fa9e4066Sahrens } 206fa9e4066Sahrens 207fa9e4066Sahrens for (c = 0; c < children; c++) { 208fa9e4066Sahrens if (spa_config_parse(spa, child[c], vd, c, atype) == NULL) { 209fa9e4066Sahrens vdev_free(vd); 210fa9e4066Sahrens return (NULL); 211fa9e4066Sahrens } 212fa9e4066Sahrens } 213fa9e4066Sahrens 214fa9e4066Sahrens return (vd); 215fa9e4066Sahrens } 216fa9e4066Sahrens 217fa9e4066Sahrens /* 218fa9e4066Sahrens * Opposite of spa_load(). 219fa9e4066Sahrens */ 220fa9e4066Sahrens static void 221fa9e4066Sahrens spa_unload(spa_t *spa) 222fa9e4066Sahrens { 223ea8dc4b6Seschrock /* 224ea8dc4b6Seschrock * Stop async tasks. 225ea8dc4b6Seschrock */ 226ea8dc4b6Seschrock spa_async_suspend(spa); 227ea8dc4b6Seschrock 228fa9e4066Sahrens /* 229fa9e4066Sahrens * Stop syncing. 230fa9e4066Sahrens */ 231fa9e4066Sahrens if (spa->spa_sync_on) { 232fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 233fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 234fa9e4066Sahrens } 235fa9e4066Sahrens 236fa9e4066Sahrens /* 237fa9e4066Sahrens * Wait for any outstanding prefetch I/O to complete. 238fa9e4066Sahrens */ 239ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 240ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 241fa9e4066Sahrens 242fa9e4066Sahrens /* 243fa9e4066Sahrens * Close the dsl pool. 244fa9e4066Sahrens */ 245fa9e4066Sahrens if (spa->spa_dsl_pool) { 246fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 247fa9e4066Sahrens spa->spa_dsl_pool = NULL; 248fa9e4066Sahrens } 249fa9e4066Sahrens 250fa9e4066Sahrens /* 251fa9e4066Sahrens * Close all vdevs. 252fa9e4066Sahrens */ 2530e34b6a7Sbonwick if (spa->spa_root_vdev) 254fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 2550e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 256ea8dc4b6Seschrock 257ea8dc4b6Seschrock spa->spa_async_suspended = 0; 258fa9e4066Sahrens } 259fa9e4066Sahrens 260fa9e4066Sahrens /* 261fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 262ea8dc4b6Seschrock * source of configuration information. 263fa9e4066Sahrens */ 264fa9e4066Sahrens static int 265ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 266fa9e4066Sahrens { 267fa9e4066Sahrens int error = 0; 268fa9e4066Sahrens nvlist_t *nvroot = NULL; 269fa9e4066Sahrens vdev_t *rvd; 270fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 2710373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 272fa9e4066Sahrens uint64_t pool_guid; 273fa9e4066Sahrens zio_t *zio; 274fa9e4066Sahrens 275ea8dc4b6Seschrock spa->spa_load_state = state; 2760373e76bSbonwick 277fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 278a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 279ea8dc4b6Seschrock error = EINVAL; 280ea8dc4b6Seschrock goto out; 281ea8dc4b6Seschrock } 282fa9e4066Sahrens 283a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 284a9926bf0Sbonwick &spa->spa_config_txg); 285a9926bf0Sbonwick 2860373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 287ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 288ea8dc4b6Seschrock error = EEXIST; 289ea8dc4b6Seschrock goto out; 290ea8dc4b6Seschrock } 291fa9e4066Sahrens 292fa9e4066Sahrens /* 293fa9e4066Sahrens * Parse the configuration into a vdev tree. 294fa9e4066Sahrens */ 295ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 296fa9e4066Sahrens rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 297ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 298fa9e4066Sahrens 299ea8dc4b6Seschrock if (rvd == NULL) { 300ea8dc4b6Seschrock error = EINVAL; 301ea8dc4b6Seschrock goto out; 302ea8dc4b6Seschrock } 303fa9e4066Sahrens 3040e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 305fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 306fa9e4066Sahrens 307fa9e4066Sahrens /* 308fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 309fa9e4066Sahrens */ 310ea8dc4b6Seschrock if (vdev_open(rvd) != 0) { 311ea8dc4b6Seschrock error = ENXIO; 312ea8dc4b6Seschrock goto out; 313ea8dc4b6Seschrock } 314fa9e4066Sahrens 315*560e6e96Seschrock /* 316*560e6e96Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 317*560e6e96Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 318*560e6e96Seschrock * flag. 319*560e6e96Seschrock */ 320*560e6e96Seschrock spa_config_enter(spa, RW_READER, FTAG); 321*560e6e96Seschrock error = vdev_validate(rvd); 322*560e6e96Seschrock spa_config_exit(spa, FTAG); 323*560e6e96Seschrock 324*560e6e96Seschrock if (error != 0) { 325*560e6e96Seschrock error = EBADF; 326*560e6e96Seschrock goto out; 327*560e6e96Seschrock } 328*560e6e96Seschrock 329*560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 330*560e6e96Seschrock error = ENXIO; 331*560e6e96Seschrock goto out; 332*560e6e96Seschrock } 333*560e6e96Seschrock 334fa9e4066Sahrens /* 335fa9e4066Sahrens * Find the best uberblock. 336fa9e4066Sahrens */ 337fa9e4066Sahrens bzero(ub, sizeof (uberblock_t)); 338fa9e4066Sahrens 339fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, 340fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 341fa9e4066Sahrens vdev_uberblock_load(zio, rvd, ub); 342fa9e4066Sahrens error = zio_wait(zio); 343fa9e4066Sahrens 344fa9e4066Sahrens /* 345fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 346fa9e4066Sahrens */ 347fa9e4066Sahrens if (ub->ub_txg == 0) { 348eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 349eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 350ea8dc4b6Seschrock error = ENXIO; 351ea8dc4b6Seschrock goto out; 352ea8dc4b6Seschrock } 353ea8dc4b6Seschrock 354ea8dc4b6Seschrock /* 355ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 356ea8dc4b6Seschrock */ 357eaca9bbdSeschrock if (ub->ub_version > ZFS_VERSION) { 358eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 359eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 360ea8dc4b6Seschrock error = ENOTSUP; 361ea8dc4b6Seschrock goto out; 362fa9e4066Sahrens } 363fa9e4066Sahrens 364fa9e4066Sahrens /* 365fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 366fa9e4066Sahrens * incomplete configuration. 367fa9e4066Sahrens */ 368ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 369ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 370ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 371ea8dc4b6Seschrock error = ENXIO; 372ea8dc4b6Seschrock goto out; 373fa9e4066Sahrens } 374fa9e4066Sahrens 375fa9e4066Sahrens /* 376fa9e4066Sahrens * Initialize internal SPA structures. 377fa9e4066Sahrens */ 378fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 379fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 380fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 381ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 382ea8dc4b6Seschrock if (error) { 383ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 384ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 385ea8dc4b6Seschrock goto out; 386ea8dc4b6Seschrock } 387fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 388fa9e4066Sahrens 389ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 390fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 391ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 392ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 393ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 394ea8dc4b6Seschrock error = EIO; 395ea8dc4b6Seschrock goto out; 396ea8dc4b6Seschrock } 397fa9e4066Sahrens 398fa9e4066Sahrens if (!mosconfig) { 399fa9e4066Sahrens dmu_buf_t *db; 400fa9e4066Sahrens char *packed = NULL; 401fa9e4066Sahrens size_t nvsize = 0; 402fa9e4066Sahrens nvlist_t *newconfig = NULL; 403fa9e4066Sahrens 404ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, 405ea8dc4b6Seschrock spa->spa_config_object, FTAG, &db)); 406fa9e4066Sahrens nvsize = *(uint64_t *)db->db_data; 407ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 408fa9e4066Sahrens 409fa9e4066Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 410ea8dc4b6Seschrock error = dmu_read(spa->spa_meta_objset, 411fa9e4066Sahrens spa->spa_config_object, 0, nvsize, packed); 412fa9e4066Sahrens if (error == 0) 413fa9e4066Sahrens error = nvlist_unpack(packed, nvsize, &newconfig, 0); 414fa9e4066Sahrens kmem_free(packed, nvsize); 415fa9e4066Sahrens 416ea8dc4b6Seschrock if (error) { 417ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 418ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 419ea8dc4b6Seschrock error = EIO; 420ea8dc4b6Seschrock goto out; 421ea8dc4b6Seschrock } 422fa9e4066Sahrens 423fa9e4066Sahrens spa_config_set(spa, newconfig); 424fa9e4066Sahrens 425fa9e4066Sahrens spa_unload(spa); 426fa9e4066Sahrens spa_deactivate(spa); 427fa9e4066Sahrens spa_activate(spa); 428fa9e4066Sahrens 429ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 430fa9e4066Sahrens } 431fa9e4066Sahrens 432ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 433fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 434ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 435ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 436ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 437ea8dc4b6Seschrock error = EIO; 438ea8dc4b6Seschrock goto out; 439ea8dc4b6Seschrock } 440fa9e4066Sahrens 441fa9e4066Sahrens /* 442ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 443ea8dc4b6Seschrock * not be present. 444fa9e4066Sahrens */ 445ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 446ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 447ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 448d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 449ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 450ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 451ea8dc4b6Seschrock error = EIO; 452ea8dc4b6Seschrock goto out; 453ea8dc4b6Seschrock } 454ea8dc4b6Seschrock 455ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 456ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 457ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 458ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 459ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 460ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 461ea8dc4b6Seschrock error = EIO; 462ea8dc4b6Seschrock goto out; 463ea8dc4b6Seschrock } 464ea8dc4b6Seschrock 465ea8dc4b6Seschrock /* 466*560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 467ea8dc4b6Seschrock */ 468*560e6e96Seschrock vdev_load(rvd); 4690373e76bSbonwick 470fa9e4066Sahrens /* 471fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 472fa9e4066Sahrens */ 473ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 474fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 475ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 476fa9e4066Sahrens 477fa9e4066Sahrens /* 478fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 479fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 480fa9e4066Sahrens */ 481ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 482ea8dc4b6Seschrock error = ENXIO; 483ea8dc4b6Seschrock goto out; 484ea8dc4b6Seschrock } 485fa9e4066Sahrens 486ea8dc4b6Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 4875dabedeeSbonwick dmu_tx_t *tx; 4880373e76bSbonwick int need_update = B_FALSE; 4890373e76bSbonwick int c; 4905dabedeeSbonwick 4910373e76bSbonwick /* 4920373e76bSbonwick * Claim log blocks that haven't been committed yet. 4930373e76bSbonwick * This must all happen in a single txg. 4940373e76bSbonwick */ 4955dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 496fa9e4066Sahrens spa_first_txg(spa)); 497fa9e4066Sahrens dmu_objset_find(spa->spa_name, zil_claim, tx, 0); 498fa9e4066Sahrens dmu_tx_commit(tx); 499fa9e4066Sahrens 500fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 501fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 502fa9e4066Sahrens 503fa9e4066Sahrens /* 504fa9e4066Sahrens * Wait for all claims to sync. 505fa9e4066Sahrens */ 506fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 5070e34b6a7Sbonwick 5080e34b6a7Sbonwick /* 5090373e76bSbonwick * If the config cache is stale, or we have uninitialized 5100373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 5110e34b6a7Sbonwick */ 5120373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 5130373e76bSbonwick state == SPA_LOAD_IMPORT) 5140373e76bSbonwick need_update = B_TRUE; 5150373e76bSbonwick 5160373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) 5170373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 5180373e76bSbonwick need_update = B_TRUE; 5190e34b6a7Sbonwick 5200e34b6a7Sbonwick /* 5210373e76bSbonwick * Update the config cache asychronously in case we're the 5220373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 5230e34b6a7Sbonwick */ 5240373e76bSbonwick if (need_update) 5250373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 526fa9e4066Sahrens } 527fa9e4066Sahrens 528ea8dc4b6Seschrock error = 0; 529ea8dc4b6Seschrock out: 530ea8dc4b6Seschrock if (error) 531ea8dc4b6Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 532ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 533ea8dc4b6Seschrock spa->spa_ena = 0; 534ea8dc4b6Seschrock 535ea8dc4b6Seschrock return (error); 536fa9e4066Sahrens } 537fa9e4066Sahrens 538fa9e4066Sahrens /* 539fa9e4066Sahrens * Pool Open/Import 540fa9e4066Sahrens * 541fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 542fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 543fa9e4066Sahrens * case of an open, the pool configuration will exist in the 544fa9e4066Sahrens * POOL_STATE_UNITIALIZED state. 545fa9e4066Sahrens * 546fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 547fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 548fa9e4066Sahrens * ambiguous state. 549fa9e4066Sahrens */ 550fa9e4066Sahrens static int 551fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 552fa9e4066Sahrens { 553fa9e4066Sahrens spa_t *spa; 554fa9e4066Sahrens int error; 555fa9e4066Sahrens int loaded = B_FALSE; 556fa9e4066Sahrens int locked = B_FALSE; 557fa9e4066Sahrens 558fa9e4066Sahrens *spapp = NULL; 559fa9e4066Sahrens 560fa9e4066Sahrens /* 561fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 562fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 563fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 564fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 565fa9e4066Sahrens */ 566fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 567fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 568fa9e4066Sahrens locked = B_TRUE; 569fa9e4066Sahrens } 570fa9e4066Sahrens 571fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 572fa9e4066Sahrens if (locked) 573fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 574fa9e4066Sahrens return (ENOENT); 575fa9e4066Sahrens } 576fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 577fa9e4066Sahrens 578fa9e4066Sahrens spa_activate(spa); 579fa9e4066Sahrens 5800373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 581fa9e4066Sahrens 582fa9e4066Sahrens if (error == EBADF) { 583fa9e4066Sahrens /* 584*560e6e96Seschrock * If vdev_validate() returns failure (indicated by 585*560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 586*560e6e96Seschrock * that the pool has been exported or destroyed. If 587*560e6e96Seschrock * this is the case, the config cache is out of sync and 588*560e6e96Seschrock * we should remove the pool from the namespace. 589fa9e4066Sahrens */ 590fa9e4066Sahrens spa_unload(spa); 591fa9e4066Sahrens spa_deactivate(spa); 592fa9e4066Sahrens spa_remove(spa); 593fa9e4066Sahrens spa_config_sync(); 594fa9e4066Sahrens if (locked) 595fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 596fa9e4066Sahrens return (ENOENT); 597ea8dc4b6Seschrock } 598ea8dc4b6Seschrock 599ea8dc4b6Seschrock if (error) { 600fa9e4066Sahrens /* 601fa9e4066Sahrens * We can't open the pool, but we still have useful 602fa9e4066Sahrens * information: the state of each vdev after the 603fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 604fa9e4066Sahrens */ 6050373e76bSbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 6060373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 607fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 608fa9e4066Sahrens B_TRUE); 6090373e76bSbonwick spa_config_exit(spa, FTAG); 6100373e76bSbonwick } 611fa9e4066Sahrens spa_unload(spa); 612fa9e4066Sahrens spa_deactivate(spa); 613ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 614fa9e4066Sahrens if (locked) 615fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 616fa9e4066Sahrens *spapp = NULL; 617fa9e4066Sahrens return (error); 618ea8dc4b6Seschrock } else { 619ea8dc4b6Seschrock zfs_post_ok(spa, NULL); 620ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 621fa9e4066Sahrens } 622fa9e4066Sahrens 623fa9e4066Sahrens loaded = B_TRUE; 624fa9e4066Sahrens } 625fa9e4066Sahrens 626fa9e4066Sahrens spa_open_ref(spa, tag); 627fa9e4066Sahrens if (locked) 628fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 629fa9e4066Sahrens 630fa9e4066Sahrens *spapp = spa; 631fa9e4066Sahrens 632fa9e4066Sahrens if (config != NULL) { 633ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 634fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 635ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 636fa9e4066Sahrens } 637fa9e4066Sahrens 638fa9e4066Sahrens /* 639fa9e4066Sahrens * If we just loaded the pool, resilver anything that's out of date. 640fa9e4066Sahrens */ 641fa9e4066Sahrens if (loaded && (spa_mode & FWRITE)) 642fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 643fa9e4066Sahrens 644fa9e4066Sahrens return (0); 645fa9e4066Sahrens } 646fa9e4066Sahrens 647fa9e4066Sahrens int 648fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 649fa9e4066Sahrens { 650fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 651fa9e4066Sahrens } 652fa9e4066Sahrens 653ea8dc4b6Seschrock /* 654ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 655ea8dc4b6Seschrock * preventing it from being exported or destroyed. 656ea8dc4b6Seschrock */ 657ea8dc4b6Seschrock spa_t * 658ea8dc4b6Seschrock spa_inject_addref(char *name) 659ea8dc4b6Seschrock { 660ea8dc4b6Seschrock spa_t *spa; 661ea8dc4b6Seschrock 662ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 663ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 664ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 665ea8dc4b6Seschrock return (NULL); 666ea8dc4b6Seschrock } 667ea8dc4b6Seschrock spa->spa_inject_ref++; 668ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 669ea8dc4b6Seschrock 670ea8dc4b6Seschrock return (spa); 671ea8dc4b6Seschrock } 672ea8dc4b6Seschrock 673ea8dc4b6Seschrock void 674ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 675ea8dc4b6Seschrock { 676ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 677ea8dc4b6Seschrock spa->spa_inject_ref--; 678ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 679ea8dc4b6Seschrock } 680ea8dc4b6Seschrock 681fa9e4066Sahrens int 682ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 683fa9e4066Sahrens { 684fa9e4066Sahrens int error; 685fa9e4066Sahrens spa_t *spa; 686fa9e4066Sahrens 687fa9e4066Sahrens *config = NULL; 688fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 689fa9e4066Sahrens 690ea8dc4b6Seschrock if (spa && *config != NULL) 691ea8dc4b6Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 692ea8dc4b6Seschrock spa_get_errlog_size(spa)) == 0); 693ea8dc4b6Seschrock 694ea8dc4b6Seschrock /* 695ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 696ea8dc4b6Seschrock * and call spa_lookup() directly. 697ea8dc4b6Seschrock */ 698ea8dc4b6Seschrock if (altroot) { 699ea8dc4b6Seschrock if (spa == NULL) { 700ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 701ea8dc4b6Seschrock spa = spa_lookup(name); 702ea8dc4b6Seschrock if (spa) 703ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 704ea8dc4b6Seschrock else 705ea8dc4b6Seschrock altroot[0] = '\0'; 706ea8dc4b6Seschrock spa = NULL; 707ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 708ea8dc4b6Seschrock } else { 709ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 710ea8dc4b6Seschrock } 711ea8dc4b6Seschrock } 712ea8dc4b6Seschrock 713fa9e4066Sahrens if (spa != NULL) 714fa9e4066Sahrens spa_close(spa, FTAG); 715fa9e4066Sahrens 716fa9e4066Sahrens return (error); 717fa9e4066Sahrens } 718fa9e4066Sahrens 719fa9e4066Sahrens /* 720fa9e4066Sahrens * Pool Creation 721fa9e4066Sahrens */ 722fa9e4066Sahrens int 7230373e76bSbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot) 724fa9e4066Sahrens { 725fa9e4066Sahrens spa_t *spa; 7260373e76bSbonwick vdev_t *rvd; 727fa9e4066Sahrens dsl_pool_t *dp; 728fa9e4066Sahrens dmu_tx_t *tx; 7290373e76bSbonwick int c, error; 730fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 731fa9e4066Sahrens 732fa9e4066Sahrens /* 733fa9e4066Sahrens * If this pool already exists, return failure. 734fa9e4066Sahrens */ 735fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 736fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 737fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 738fa9e4066Sahrens return (EEXIST); 739fa9e4066Sahrens } 740fa9e4066Sahrens 741fa9e4066Sahrens /* 742fa9e4066Sahrens * Allocate a new spa_t structure. 743fa9e4066Sahrens */ 7440373e76bSbonwick spa = spa_add(pool, altroot); 745fa9e4066Sahrens spa_activate(spa); 746fa9e4066Sahrens 747fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 748eaca9bbdSeschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 749fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 750fa9e4066Sahrens 7510373e76bSbonwick /* 7520373e76bSbonwick * Create the root vdev. 7530373e76bSbonwick */ 7540373e76bSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 7550373e76bSbonwick 7560373e76bSbonwick rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD); 7570373e76bSbonwick 7580373e76bSbonwick ASSERT(spa->spa_root_vdev == rvd); 7590373e76bSbonwick 7600373e76bSbonwick if (rvd == NULL) { 7610373e76bSbonwick error = EINVAL; 7620373e76bSbonwick } else { 7630373e76bSbonwick if ((error = vdev_create(rvd, txg)) == 0) { 7640373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) 7650373e76bSbonwick vdev_init(rvd->vdev_child[c], txg); 7660373e76bSbonwick vdev_config_dirty(rvd); 7670373e76bSbonwick } 7680373e76bSbonwick } 7690373e76bSbonwick 7700373e76bSbonwick spa_config_exit(spa, FTAG); 771fa9e4066Sahrens 772fa9e4066Sahrens if (error) { 773fa9e4066Sahrens spa_unload(spa); 774fa9e4066Sahrens spa_deactivate(spa); 775fa9e4066Sahrens spa_remove(spa); 776fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 777fa9e4066Sahrens return (error); 778fa9e4066Sahrens } 779fa9e4066Sahrens 780fa9e4066Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 781fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 782fa9e4066Sahrens 783fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 784fa9e4066Sahrens 785fa9e4066Sahrens /* 786fa9e4066Sahrens * Create the pool config object. 787fa9e4066Sahrens */ 788fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 789fa9e4066Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 790fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 791fa9e4066Sahrens 792ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 793fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 794ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 795ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 796ea8dc4b6Seschrock } 797fa9e4066Sahrens 798fa9e4066Sahrens /* 799fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 800fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 801fa9e4066Sahrens * keeps changing. 802fa9e4066Sahrens */ 803fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 804fa9e4066Sahrens 1 << 14, tx); 805fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 806fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 807fa9e4066Sahrens 808ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 809fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 810ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 811ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 812ea8dc4b6Seschrock } 813fa9e4066Sahrens 814fa9e4066Sahrens dmu_tx_commit(tx); 815fa9e4066Sahrens 816fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 817fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 818fa9e4066Sahrens 819fa9e4066Sahrens /* 820fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 821fa9e4066Sahrens * bean counters are appropriately updated. 822fa9e4066Sahrens */ 823fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 824fa9e4066Sahrens 825fa9e4066Sahrens spa_config_sync(); 826fa9e4066Sahrens 827fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 828fa9e4066Sahrens 829fa9e4066Sahrens return (0); 830fa9e4066Sahrens } 831fa9e4066Sahrens 832fa9e4066Sahrens /* 833fa9e4066Sahrens * Import the given pool into the system. We set up the necessary spa_t and 834fa9e4066Sahrens * then call spa_load() to do the dirty work. 835fa9e4066Sahrens */ 836fa9e4066Sahrens int 8370373e76bSbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot) 838fa9e4066Sahrens { 839fa9e4066Sahrens spa_t *spa; 840fa9e4066Sahrens int error; 841fa9e4066Sahrens 842fa9e4066Sahrens if (!(spa_mode & FWRITE)) 843fa9e4066Sahrens return (EROFS); 844fa9e4066Sahrens 845fa9e4066Sahrens /* 846fa9e4066Sahrens * If a pool with this name exists, return failure. 847fa9e4066Sahrens */ 848fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 849fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 850fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 851fa9e4066Sahrens return (EEXIST); 852fa9e4066Sahrens } 853fa9e4066Sahrens 854fa9e4066Sahrens /* 8550373e76bSbonwick * Create and initialize the spa structure. 856fa9e4066Sahrens */ 8570373e76bSbonwick spa = spa_add(pool, altroot); 858fa9e4066Sahrens spa_activate(spa); 859fa9e4066Sahrens 8605dabedeeSbonwick /* 8610373e76bSbonwick * Pass off the heavy lifting to spa_load(). 862ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 863ecc2d604Sbonwick * is actually the one to trust when doing an import. 8645dabedeeSbonwick */ 865ecc2d604Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 866fa9e4066Sahrens 867fa9e4066Sahrens if (error) { 868fa9e4066Sahrens spa_unload(spa); 869fa9e4066Sahrens spa_deactivate(spa); 870fa9e4066Sahrens spa_remove(spa); 871fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 872fa9e4066Sahrens return (error); 873fa9e4066Sahrens } 874fa9e4066Sahrens 8750373e76bSbonwick /* 8760373e76bSbonwick * Update the config cache to include the newly-imported pool. 8770373e76bSbonwick */ 8780373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 8790373e76bSbonwick 880fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 881fa9e4066Sahrens 882fa9e4066Sahrens /* 883fa9e4066Sahrens * Resilver anything that's out of date. 884fa9e4066Sahrens */ 885fa9e4066Sahrens if (spa_mode & FWRITE) 886fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 887fa9e4066Sahrens 888fa9e4066Sahrens return (0); 889fa9e4066Sahrens } 890fa9e4066Sahrens 891fa9e4066Sahrens /* 892fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 893fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 894fa9e4066Sahrens */ 895fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 896fa9e4066Sahrens 897fa9e4066Sahrens nvlist_t * 898fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 899fa9e4066Sahrens { 900fa9e4066Sahrens nvlist_t *config = NULL; 901fa9e4066Sahrens char *poolname; 902fa9e4066Sahrens spa_t *spa; 903fa9e4066Sahrens uint64_t state; 904fa9e4066Sahrens 905fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 906fa9e4066Sahrens return (NULL); 907fa9e4066Sahrens 908fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 909fa9e4066Sahrens return (NULL); 910fa9e4066Sahrens 911fa9e4066Sahrens /* 9120373e76bSbonwick * Create and initialize the spa structure. 913fa9e4066Sahrens */ 9140373e76bSbonwick mutex_enter(&spa_namespace_lock); 9150373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 916fa9e4066Sahrens spa_activate(spa); 917fa9e4066Sahrens 918fa9e4066Sahrens /* 9190373e76bSbonwick * Pass off the heavy lifting to spa_load(). 920ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 921ecc2d604Sbonwick * is actually the one to trust when doing an import. 922fa9e4066Sahrens */ 923ecc2d604Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 924fa9e4066Sahrens 925fa9e4066Sahrens /* 926fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 927fa9e4066Sahrens */ 928fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 9290373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 930fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 9310373e76bSbonwick spa_config_exit(spa, FTAG); 932fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 933fa9e4066Sahrens poolname) == 0); 934fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 935fa9e4066Sahrens state) == 0); 936fa9e4066Sahrens } 937fa9e4066Sahrens 938fa9e4066Sahrens spa_unload(spa); 939fa9e4066Sahrens spa_deactivate(spa); 940fa9e4066Sahrens spa_remove(spa); 941fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 942fa9e4066Sahrens 943fa9e4066Sahrens return (config); 944fa9e4066Sahrens } 945fa9e4066Sahrens 946fa9e4066Sahrens /* 947fa9e4066Sahrens * Pool export/destroy 948fa9e4066Sahrens * 949fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 950fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 951fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 952fa9e4066Sahrens * configuration from the cache afterwards. 953fa9e4066Sahrens */ 954fa9e4066Sahrens static int 95544cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 956fa9e4066Sahrens { 957fa9e4066Sahrens spa_t *spa; 958fa9e4066Sahrens 95944cd46caSbillm if (oldconfig) 96044cd46caSbillm *oldconfig = NULL; 96144cd46caSbillm 962fa9e4066Sahrens if (!(spa_mode & FWRITE)) 963fa9e4066Sahrens return (EROFS); 964fa9e4066Sahrens 965fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 966fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 967fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 968fa9e4066Sahrens return (ENOENT); 969fa9e4066Sahrens } 970fa9e4066Sahrens 971ea8dc4b6Seschrock /* 972ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 973ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 974ea8dc4b6Seschrock */ 975ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 976ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 977ea8dc4b6Seschrock spa_async_suspend(spa); 978ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 979ea8dc4b6Seschrock spa_close(spa, FTAG); 980ea8dc4b6Seschrock 981fa9e4066Sahrens /* 982fa9e4066Sahrens * The pool will be in core if it's openable, 983fa9e4066Sahrens * in which case we can modify its state. 984fa9e4066Sahrens */ 985fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 986fa9e4066Sahrens /* 987fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 988fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 989fa9e4066Sahrens */ 990fa9e4066Sahrens spa_scrub_suspend(spa); 991fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 992fa9e4066Sahrens 993ea8dc4b6Seschrock /* 994ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 995ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 996ea8dc4b6Seschrock * fault injection handlers. 997ea8dc4b6Seschrock */ 998ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 999ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 1000ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1001fa9e4066Sahrens spa_scrub_resume(spa); 1002ea8dc4b6Seschrock spa_async_resume(spa); 1003fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1004fa9e4066Sahrens return (EBUSY); 1005fa9e4066Sahrens } 1006fa9e4066Sahrens 1007fa9e4066Sahrens spa_scrub_resume(spa); 1008fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1009fa9e4066Sahrens 1010fa9e4066Sahrens /* 1011fa9e4066Sahrens * We want this to be reflected on every label, 1012fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 1013fa9e4066Sahrens * final sync that pushes these changes out. 1014fa9e4066Sahrens */ 1015ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 10165dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 1017ea8dc4b6Seschrock spa->spa_state = new_state; 10180373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 1019ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 10205dabedeeSbonwick spa_config_exit(spa, FTAG); 1021ea8dc4b6Seschrock } 1022fa9e4066Sahrens } 1023fa9e4066Sahrens 1024fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1025fa9e4066Sahrens spa_unload(spa); 1026fa9e4066Sahrens spa_deactivate(spa); 1027fa9e4066Sahrens } 1028fa9e4066Sahrens 102944cd46caSbillm if (oldconfig && spa->spa_config) 103044cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 103144cd46caSbillm 1032ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 1033ea8dc4b6Seschrock spa_remove(spa); 1034ea8dc4b6Seschrock spa_config_sync(); 1035ea8dc4b6Seschrock } 1036fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1037fa9e4066Sahrens 1038fa9e4066Sahrens return (0); 1039fa9e4066Sahrens } 1040fa9e4066Sahrens 1041fa9e4066Sahrens /* 1042fa9e4066Sahrens * Destroy a storage pool. 1043fa9e4066Sahrens */ 1044fa9e4066Sahrens int 1045fa9e4066Sahrens spa_destroy(char *pool) 1046fa9e4066Sahrens { 104744cd46caSbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1048fa9e4066Sahrens } 1049fa9e4066Sahrens 1050fa9e4066Sahrens /* 1051fa9e4066Sahrens * Export a storage pool. 1052fa9e4066Sahrens */ 1053fa9e4066Sahrens int 105444cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig) 1055fa9e4066Sahrens { 105644cd46caSbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1057fa9e4066Sahrens } 1058fa9e4066Sahrens 1059ea8dc4b6Seschrock /* 1060ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 1061ea8dc4b6Seschrock * from the namespace in any way. 1062ea8dc4b6Seschrock */ 1063ea8dc4b6Seschrock int 1064ea8dc4b6Seschrock spa_reset(char *pool) 1065ea8dc4b6Seschrock { 106644cd46caSbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 1067ea8dc4b6Seschrock } 1068ea8dc4b6Seschrock 1069ea8dc4b6Seschrock 1070fa9e4066Sahrens /* 1071fa9e4066Sahrens * ========================================================================== 1072fa9e4066Sahrens * Device manipulation 1073fa9e4066Sahrens * ========================================================================== 1074fa9e4066Sahrens */ 1075fa9e4066Sahrens 1076fa9e4066Sahrens /* 1077fa9e4066Sahrens * Add capacity to a storage pool. 1078fa9e4066Sahrens */ 1079fa9e4066Sahrens int 1080fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1081fa9e4066Sahrens { 1082fa9e4066Sahrens uint64_t txg; 10830373e76bSbonwick int c, error; 1084fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 10850e34b6a7Sbonwick vdev_t *vd, *tvd; 1086fa9e4066Sahrens 1087fa9e4066Sahrens txg = spa_vdev_enter(spa); 1088fa9e4066Sahrens 1089fa9e4066Sahrens vd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD); 1090fa9e4066Sahrens 1091fa9e4066Sahrens if (vd == NULL) 1092fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 1093fa9e4066Sahrens 1094fa9e4066Sahrens if ((error = vdev_create(vd, txg)) != 0) 1095fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, error)); 1096fa9e4066Sahrens 1097fa9e4066Sahrens /* 10980e34b6a7Sbonwick * Transfer each new top-level vdev from vd to rvd. 1099fa9e4066Sahrens */ 11000373e76bSbonwick for (c = 0; c < vd->vdev_children; c++) { 11010e34b6a7Sbonwick tvd = vd->vdev_child[c]; 11020373e76bSbonwick vdev_remove_child(vd, tvd); 11030373e76bSbonwick tvd->vdev_id = rvd->vdev_children; 11040373e76bSbonwick vdev_add_child(rvd, tvd); 1105fa9e4066Sahrens vdev_config_dirty(tvd); 1106fa9e4066Sahrens } 1107fa9e4066Sahrens 1108fa9e4066Sahrens /* 11090e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 11100e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 11110e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 11120e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 11130e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 11140e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 11150373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 11160e34b6a7Sbonwick * 11170e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 11180e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 11190e34b6a7Sbonwick * steps will be completed the next time we load the pool. 11200e34b6a7Sbonwick */ 11210373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 11220e34b6a7Sbonwick 11230373e76bSbonwick mutex_enter(&spa_namespace_lock); 11240373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 11250373e76bSbonwick mutex_exit(&spa_namespace_lock); 1126fa9e4066Sahrens 11270373e76bSbonwick return (0); 1128fa9e4066Sahrens } 1129fa9e4066Sahrens 1130fa9e4066Sahrens /* 1131fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 1132fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 1133fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 1134fa9e4066Sahrens * 1135fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 1136fa9e4066Sahrens * existing device; in this case the two devices are made into their own 1137fa9e4066Sahrens * mirror using the 'replacing' vdev, which is functionally idendical to 1138fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 1139fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 1140fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 1141fa9e4066Sahrens * is automatically detached. 1142fa9e4066Sahrens */ 1143fa9e4066Sahrens int 1144ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1145fa9e4066Sahrens { 1146fa9e4066Sahrens uint64_t txg, open_txg; 1147fa9e4066Sahrens int error; 1148fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 1149fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 1150fa9e4066Sahrens vdev_ops_t *pvops = replacing ? &vdev_replacing_ops : &vdev_mirror_ops; 1151fa9e4066Sahrens 1152fa9e4066Sahrens txg = spa_vdev_enter(spa); 1153fa9e4066Sahrens 1154ea8dc4b6Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 1155fa9e4066Sahrens 1156fa9e4066Sahrens if (oldvd == NULL) 1157fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1158fa9e4066Sahrens 11590e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 11600e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 11610e34b6a7Sbonwick 1162fa9e4066Sahrens pvd = oldvd->vdev_parent; 1163fa9e4066Sahrens 1164fa9e4066Sahrens /* 1165fa9e4066Sahrens * The parent must be a mirror or the root, unless we're replacing; 1166fa9e4066Sahrens * in that case, the parent can be anything but another replacing vdev. 1167fa9e4066Sahrens */ 1168fa9e4066Sahrens if (pvd->vdev_ops != &vdev_mirror_ops && 1169fa9e4066Sahrens pvd->vdev_ops != &vdev_root_ops && 1170fa9e4066Sahrens (!replacing || pvd->vdev_ops == &vdev_replacing_ops)) 1171fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1172fa9e4066Sahrens 1173fa9e4066Sahrens newrootvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD); 1174fa9e4066Sahrens 1175fa9e4066Sahrens if (newrootvd == NULL || newrootvd->vdev_children != 1) 1176fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1177fa9e4066Sahrens 1178fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 1179fa9e4066Sahrens 1180fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 1181fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1182fa9e4066Sahrens 1183fa9e4066Sahrens if ((error = vdev_create(newrootvd, txg)) != 0) 1184fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 1185fa9e4066Sahrens 11862a79c5feSlling /* 11872a79c5feSlling * Compare the new device size with the replaceable/attachable 11882a79c5feSlling * device size. 11892a79c5feSlling */ 11902a79c5feSlling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1191fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1192fa9e4066Sahrens 1193ecc2d604Sbonwick /* 1194ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 1195ecc2d604Sbonwick * than the top-level vdev. 1196ecc2d604Sbonwick */ 1197ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1198fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1199fa9e4066Sahrens 1200fa9e4066Sahrens /* 1201fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 1202fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 1203fa9e4066Sahrens */ 1204fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1205fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 1206fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1207fa9e4066Sahrens KM_SLEEP); 1208fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 1209fa9e4066Sahrens newvd->vdev_path, "old"); 1210fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 1211fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 1212fa9e4066Sahrens oldvd->vdev_devid = NULL; 1213fa9e4066Sahrens } 1214fa9e4066Sahrens } 1215fa9e4066Sahrens 1216fa9e4066Sahrens /* 1217fa9e4066Sahrens * If the parent is not a mirror, or if we're replacing, 1218fa9e4066Sahrens * insert the new mirror/replacing vdev above oldvd. 1219fa9e4066Sahrens */ 1220fa9e4066Sahrens if (pvd->vdev_ops != pvops) 1221fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 1222fa9e4066Sahrens 1223fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 1224fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 1225fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 1226fa9e4066Sahrens 1227fa9e4066Sahrens /* 1228fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 1229fa9e4066Sahrens */ 1230fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 1231fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 1232fa9e4066Sahrens vdev_add_child(pvd, newvd); 1233fa9e4066Sahrens 1234ea8dc4b6Seschrock /* 1235ea8dc4b6Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 1236ea8dc4b6Seschrock * the addition of newvd may have decreased our parent's asize. 1237ea8dc4b6Seschrock */ 1238ea8dc4b6Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 1239ea8dc4b6Seschrock 1240fa9e4066Sahrens tvd = newvd->vdev_top; 1241fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 1242fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 1243fa9e4066Sahrens 1244fa9e4066Sahrens vdev_config_dirty(tvd); 1245fa9e4066Sahrens 1246fa9e4066Sahrens /* 1247fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1248fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1249fa9e4066Sahrens */ 1250fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 1251fa9e4066Sahrens 1252fa9e4066Sahrens mutex_enter(&newvd->vdev_dtl_lock); 1253fa9e4066Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1254fa9e4066Sahrens open_txg - TXG_INITIAL + 1); 1255fa9e4066Sahrens mutex_exit(&newvd->vdev_dtl_lock); 1256fa9e4066Sahrens 1257ea8dc4b6Seschrock dprintf("attached %s in txg %llu\n", newvd->vdev_path, txg); 1258ea8dc4b6Seschrock 1259fa9e4066Sahrens /* 1260fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 1261fa9e4066Sahrens */ 1262ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 1263fa9e4066Sahrens 1264fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1265fa9e4066Sahrens 1266fa9e4066Sahrens /* 1267fa9e4066Sahrens * Kick off a resilver to update newvd. 1268fa9e4066Sahrens */ 1269fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1270fa9e4066Sahrens 1271fa9e4066Sahrens return (0); 1272fa9e4066Sahrens } 1273fa9e4066Sahrens 1274fa9e4066Sahrens /* 1275fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 1276fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 1277fa9e4066Sahrens * is a replacing vdev. 1278fa9e4066Sahrens */ 1279fa9e4066Sahrens int 1280ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1281fa9e4066Sahrens { 1282fa9e4066Sahrens uint64_t txg; 1283fa9e4066Sahrens int c, t, error; 1284fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 1285fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 1286fa9e4066Sahrens 1287fa9e4066Sahrens txg = spa_vdev_enter(spa); 1288fa9e4066Sahrens 1289ea8dc4b6Seschrock vd = vdev_lookup_by_guid(rvd, guid); 1290fa9e4066Sahrens 1291fa9e4066Sahrens if (vd == NULL) 1292fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1293fa9e4066Sahrens 12940e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 12950e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 12960e34b6a7Sbonwick 1297fa9e4066Sahrens pvd = vd->vdev_parent; 1298fa9e4066Sahrens 1299fa9e4066Sahrens /* 1300fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 1301fa9e4066Sahrens * the first child of a replacing vdev. 1302fa9e4066Sahrens */ 1303fa9e4066Sahrens if (replace_done && 1304fa9e4066Sahrens (vd->vdev_id != 0 || pvd->vdev_ops != &vdev_replacing_ops)) 1305fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1306fa9e4066Sahrens 1307fa9e4066Sahrens /* 1308fa9e4066Sahrens * Only mirror and replacing vdevs support detach. 1309fa9e4066Sahrens */ 1310fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 1311fa9e4066Sahrens pvd->vdev_ops != &vdev_mirror_ops) 1312fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1313fa9e4066Sahrens 1314fa9e4066Sahrens /* 1315fa9e4066Sahrens * If there's only one replica, you can't detach it. 1316fa9e4066Sahrens */ 1317fa9e4066Sahrens if (pvd->vdev_children <= 1) 1318fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1319fa9e4066Sahrens 1320fa9e4066Sahrens /* 1321fa9e4066Sahrens * If all siblings have non-empty DTLs, this device may have the only 1322fa9e4066Sahrens * valid copy of the data, which means we cannot safely detach it. 1323fa9e4066Sahrens * 1324fa9e4066Sahrens * XXX -- as in the vdev_offline() case, we really want a more 1325fa9e4066Sahrens * precise DTL check. 1326fa9e4066Sahrens */ 1327fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) { 1328fa9e4066Sahrens uint64_t dirty; 1329fa9e4066Sahrens 1330fa9e4066Sahrens cvd = pvd->vdev_child[c]; 1331fa9e4066Sahrens if (cvd == vd) 1332fa9e4066Sahrens continue; 1333fa9e4066Sahrens if (vdev_is_dead(cvd)) 1334fa9e4066Sahrens continue; 1335fa9e4066Sahrens mutex_enter(&cvd->vdev_dtl_lock); 1336fa9e4066Sahrens dirty = cvd->vdev_dtl_map.sm_space | 1337fa9e4066Sahrens cvd->vdev_dtl_scrub.sm_space; 1338fa9e4066Sahrens mutex_exit(&cvd->vdev_dtl_lock); 1339fa9e4066Sahrens if (!dirty) 1340fa9e4066Sahrens break; 1341fa9e4066Sahrens } 1342fa9e4066Sahrens if (c == pvd->vdev_children) 1343fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1344fa9e4066Sahrens 1345fa9e4066Sahrens /* 1346fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 1347fa9e4066Sahrens * This must be done after all other error cases are handled, 1348fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 1349fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 1350fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 1351fa9e4066Sahrens * it's being detached! 1352fa9e4066Sahrens */ 1353fa9e4066Sahrens error = vdev_label_init(vd, 0); 1354fa9e4066Sahrens if (error) 1355fa9e4066Sahrens dprintf("unable to erase labels on %s\n", vdev_description(vd)); 1356fa9e4066Sahrens 1357fa9e4066Sahrens /* 1358fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 1359fa9e4066Sahrens */ 1360fa9e4066Sahrens vdev_remove_child(pvd, vd); 1361fa9e4066Sahrens vdev_compact_children(pvd); 1362fa9e4066Sahrens 1363fa9e4066Sahrens /* 1364fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 1365fa9e4066Sahrens */ 1366fa9e4066Sahrens cvd = pvd->vdev_child[0]; 1367fa9e4066Sahrens 1368fa9e4066Sahrens /* 1369fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 1370fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 1371fa9e4066Sahrens */ 1372fa9e4066Sahrens if (pvd->vdev_children == 1) 1373fa9e4066Sahrens vdev_remove_parent(cvd); 1374fa9e4066Sahrens 1375fa9e4066Sahrens /* 1376fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 1377fa9e4066Sahrens * may have been the previous top-level vdev. 1378fa9e4066Sahrens */ 1379fa9e4066Sahrens tvd = cvd->vdev_top; 1380fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 1381fa9e4066Sahrens 1382fa9e4066Sahrens /* 1383fa9e4066Sahrens * Reopen this top-level vdev to reassess health after detach. 1384fa9e4066Sahrens */ 1385ea8dc4b6Seschrock vdev_reopen(tvd); 1386fa9e4066Sahrens 1387fa9e4066Sahrens /* 1388fa9e4066Sahrens * If the device we just detached was smaller than the others, 1389ecc2d604Sbonwick * it may be possible to add metaslabs (i.e. grow the pool). 1390ecc2d604Sbonwick * vdev_metaslab_init() can't fail because the existing metaslabs 1391ecc2d604Sbonwick * are already in core, so there's nothing to read from disk. 1392fa9e4066Sahrens */ 1393ecc2d604Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 1394fa9e4066Sahrens 1395fa9e4066Sahrens vdev_config_dirty(tvd); 1396fa9e4066Sahrens 1397fa9e4066Sahrens /* 1398fa9e4066Sahrens * Mark vd's DTL as dirty in this txg. 1399fa9e4066Sahrens * vdev_dtl_sync() will see that vd->vdev_detached is set 1400fa9e4066Sahrens * and free vd's DTL object in syncing context. 1401fa9e4066Sahrens * But first make sure we're not on any *other* txg's DTL list, 1402fa9e4066Sahrens * to prevent vd from being accessed after it's freed. 1403fa9e4066Sahrens */ 1404fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) 1405fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 1406ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 1407ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 1408fa9e4066Sahrens 1409ea8dc4b6Seschrock dprintf("detached %s in txg %llu\n", vd->vdev_path, txg); 1410fa9e4066Sahrens 1411fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, 0)); 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 1414fa9e4066Sahrens /* 1415ea8dc4b6Seschrock * Find any device that's done replacing, so we can detach it. 1416fa9e4066Sahrens */ 1417ea8dc4b6Seschrock static vdev_t * 1418ea8dc4b6Seschrock spa_vdev_replace_done_hunt(vdev_t *vd) 1419fa9e4066Sahrens { 1420ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 1421fa9e4066Sahrens int c; 1422fa9e4066Sahrens 1423ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) { 1424ea8dc4b6Seschrock oldvd = spa_vdev_replace_done_hunt(vd->vdev_child[c]); 1425ea8dc4b6Seschrock if (oldvd != NULL) 1426ea8dc4b6Seschrock return (oldvd); 1427ea8dc4b6Seschrock } 1428fa9e4066Sahrens 1429fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 1430ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 1431ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 1432ea8dc4b6Seschrock 1433ea8dc4b6Seschrock mutex_enter(&newvd->vdev_dtl_lock); 1434ea8dc4b6Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 1435ea8dc4b6Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 1436ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 1437ea8dc4b6Seschrock return (oldvd); 1438fa9e4066Sahrens } 1439ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 1440fa9e4066Sahrens } 1441ea8dc4b6Seschrock 1442ea8dc4b6Seschrock return (NULL); 1443fa9e4066Sahrens } 1444fa9e4066Sahrens 1445ea8dc4b6Seschrock static void 1446fa9e4066Sahrens spa_vdev_replace_done(spa_t *spa) 1447fa9e4066Sahrens { 1448ea8dc4b6Seschrock vdev_t *vd; 1449ea8dc4b6Seschrock uint64_t guid; 1450ea8dc4b6Seschrock 1451ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1452ea8dc4b6Seschrock 1453ea8dc4b6Seschrock while ((vd = spa_vdev_replace_done_hunt(spa->spa_root_vdev)) != NULL) { 1454ea8dc4b6Seschrock guid = vd->vdev_guid; 1455ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1456ea8dc4b6Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 1457ea8dc4b6Seschrock return; 1458ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1459fa9e4066Sahrens } 1460fa9e4066Sahrens 1461ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1462fa9e4066Sahrens } 1463fa9e4066Sahrens 1464c67d9675Seschrock /* 1465c67d9675Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 1466c67d9675Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 1467c67d9675Seschrock */ 1468c67d9675Seschrock int 1469c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 1470c67d9675Seschrock { 1471c67d9675Seschrock vdev_t *rvd, *vd; 1472c67d9675Seschrock uint64_t txg; 1473c67d9675Seschrock 1474c67d9675Seschrock rvd = spa->spa_root_vdev; 1475c67d9675Seschrock 1476c67d9675Seschrock txg = spa_vdev_enter(spa); 1477c67d9675Seschrock 1478c67d9675Seschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1479c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 1480c67d9675Seschrock 14810e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 14820e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 14830e34b6a7Sbonwick 1484c67d9675Seschrock spa_strfree(vd->vdev_path); 1485c67d9675Seschrock vd->vdev_path = spa_strdup(newpath); 1486c67d9675Seschrock 1487c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 1488c67d9675Seschrock 1489c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 1490c67d9675Seschrock } 1491c67d9675Seschrock 1492fa9e4066Sahrens /* 1493fa9e4066Sahrens * ========================================================================== 1494fa9e4066Sahrens * SPA Scrubbing 1495fa9e4066Sahrens * ========================================================================== 1496fa9e4066Sahrens */ 1497fa9e4066Sahrens 1498ea8dc4b6Seschrock void 1499ea8dc4b6Seschrock spa_scrub_throttle(spa_t *spa, int direction) 1500ea8dc4b6Seschrock { 1501ea8dc4b6Seschrock mutex_enter(&spa->spa_scrub_lock); 1502ea8dc4b6Seschrock spa->spa_scrub_throttled += direction; 1503ea8dc4b6Seschrock ASSERT(spa->spa_scrub_throttled >= 0); 1504ea8dc4b6Seschrock if (spa->spa_scrub_throttled == 0) 1505ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 1506ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 1507ea8dc4b6Seschrock } 1508fa9e4066Sahrens 1509fa9e4066Sahrens static void 1510fa9e4066Sahrens spa_scrub_io_done(zio_t *zio) 1511fa9e4066Sahrens { 1512fa9e4066Sahrens spa_t *spa = zio->io_spa; 1513fa9e4066Sahrens 1514fa9e4066Sahrens zio_buf_free(zio->io_data, zio->io_size); 1515fa9e4066Sahrens 1516fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1517ea8dc4b6Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 151844cd46caSbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 1519ea8dc4b6Seschrock spa->spa_scrub_errors++; 1520fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 1521fa9e4066Sahrens vd->vdev_stat.vs_scrub_errors++; 1522fa9e4066Sahrens mutex_exit(&vd->vdev_stat_lock); 1523fa9e4066Sahrens } 1524ea8dc4b6Seschrock if (--spa->spa_scrub_inflight == 0) { 1525ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 1526ea8dc4b6Seschrock ASSERT(spa->spa_scrub_throttled == 0); 1527ea8dc4b6Seschrock } 1528ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 1529fa9e4066Sahrens } 1530fa9e4066Sahrens 1531fa9e4066Sahrens static void 1532ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 1533ea8dc4b6Seschrock zbookmark_t *zb) 1534fa9e4066Sahrens { 1535fa9e4066Sahrens size_t size = BP_GET_LSIZE(bp); 1536fa9e4066Sahrens void *data = zio_buf_alloc(size); 1537fa9e4066Sahrens 1538fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1539fa9e4066Sahrens spa->spa_scrub_inflight++; 1540fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1541fa9e4066Sahrens 1542ea8dc4b6Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 1543ea8dc4b6Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 1544ea8dc4b6Seschrock 1545d80c45e0Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 1546ea8dc4b6Seschrock 1547fa9e4066Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 1548ea8dc4b6Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 1549fa9e4066Sahrens } 1550fa9e4066Sahrens 1551fa9e4066Sahrens /* ARGSUSED */ 1552fa9e4066Sahrens static int 1553fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 1554fa9e4066Sahrens { 1555fa9e4066Sahrens blkptr_t *bp = &bc->bc_blkptr; 155644cd46caSbillm vdev_t *vd = spa->spa_root_vdev; 155744cd46caSbillm dva_t *dva = bp->blk_dva; 155844cd46caSbillm int needs_resilver = B_FALSE; 155944cd46caSbillm int d; 1560fa9e4066Sahrens 156144cd46caSbillm if (bc->bc_errno) { 1562fa9e4066Sahrens /* 1563fa9e4066Sahrens * We can't scrub this block, but we can continue to scrub 1564fa9e4066Sahrens * the rest of the pool. Note the error and move along. 1565fa9e4066Sahrens */ 1566fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1567fa9e4066Sahrens spa->spa_scrub_errors++; 1568fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1569fa9e4066Sahrens 157044cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 157144cd46caSbillm vd->vdev_stat.vs_scrub_errors++; 157244cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 1573fa9e4066Sahrens 1574fa9e4066Sahrens return (ERESTART); 1575fa9e4066Sahrens } 1576fa9e4066Sahrens 1577fa9e4066Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 1578fa9e4066Sahrens 157944cd46caSbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 158044cd46caSbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 1581fa9e4066Sahrens 158244cd46caSbillm ASSERT(vd != NULL); 158344cd46caSbillm 158444cd46caSbillm /* 158544cd46caSbillm * Keep track of how much data we've examined so that 158644cd46caSbillm * zpool(1M) status can make useful progress reports. 158744cd46caSbillm */ 158844cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 158944cd46caSbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 159044cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 159144cd46caSbillm 159244cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 159344cd46caSbillm if (DVA_GET_GANG(&dva[d])) { 159444cd46caSbillm /* 159544cd46caSbillm * Gang members may be spread across multiple 159644cd46caSbillm * vdevs, so the best we can do is look at the 159744cd46caSbillm * pool-wide DTL. 159844cd46caSbillm * XXX -- it would be better to change our 159944cd46caSbillm * allocation policy to ensure that this can't 160044cd46caSbillm * happen. 160144cd46caSbillm */ 160244cd46caSbillm vd = spa->spa_root_vdev; 160344cd46caSbillm } 160444cd46caSbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 160544cd46caSbillm bp->blk_birth, 1)) 160644cd46caSbillm needs_resilver = B_TRUE; 1607fa9e4066Sahrens } 160844cd46caSbillm } 160944cd46caSbillm 161044cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 1611fa9e4066Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 1612ea8dc4b6Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 161344cd46caSbillm else if (needs_resilver) 161444cd46caSbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 161544cd46caSbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 1616fa9e4066Sahrens 1617fa9e4066Sahrens return (0); 1618fa9e4066Sahrens } 1619fa9e4066Sahrens 1620fa9e4066Sahrens static void 1621fa9e4066Sahrens spa_scrub_thread(spa_t *spa) 1622fa9e4066Sahrens { 1623fa9e4066Sahrens callb_cpr_t cprinfo; 1624fa9e4066Sahrens traverse_handle_t *th = spa->spa_scrub_th; 1625fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 1626fa9e4066Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 1627fa9e4066Sahrens int error = 0; 1628fa9e4066Sahrens boolean_t complete; 1629fa9e4066Sahrens 1630fa9e4066Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 1631fa9e4066Sahrens 1632f0aa80d4Sbonwick /* 1633f0aa80d4Sbonwick * If we're restarting due to a snapshot create/delete, 1634f0aa80d4Sbonwick * wait for that to complete. 1635f0aa80d4Sbonwick */ 1636f0aa80d4Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 1637f0aa80d4Sbonwick 1638ea8dc4b6Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 1639ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 1640ea8dc4b6Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 1641ea8dc4b6Seschrock 1642ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1643ea8dc4b6Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 1644fa9e4066Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 1645fa9e4066Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 1646ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1647fa9e4066Sahrens 1648fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1649fa9e4066Sahrens spa->spa_scrub_errors = 0; 1650fa9e4066Sahrens spa->spa_scrub_active = 1; 1651ea8dc4b6Seschrock ASSERT(spa->spa_scrub_inflight == 0); 1652ea8dc4b6Seschrock ASSERT(spa->spa_scrub_throttled == 0); 1653fa9e4066Sahrens 1654fa9e4066Sahrens while (!spa->spa_scrub_stop) { 1655fa9e4066Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 1656ea8dc4b6Seschrock while (spa->spa_scrub_suspended) { 1657fa9e4066Sahrens spa->spa_scrub_active = 0; 1658fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 1659fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 1660fa9e4066Sahrens spa->spa_scrub_active = 1; 1661fa9e4066Sahrens } 1662fa9e4066Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 1663fa9e4066Sahrens 1664fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 1665fa9e4066Sahrens break; 1666fa9e4066Sahrens 1667fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1668fa9e4066Sahrens error = traverse_more(th); 1669fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1670fa9e4066Sahrens if (error != EAGAIN) 1671fa9e4066Sahrens break; 1672ea8dc4b6Seschrock 1673ea8dc4b6Seschrock while (spa->spa_scrub_throttled > 0) 1674ea8dc4b6Seschrock cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1675fa9e4066Sahrens } 1676fa9e4066Sahrens 1677fa9e4066Sahrens while (spa->spa_scrub_inflight) 1678fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1679fa9e4066Sahrens 16805dabedeeSbonwick spa->spa_scrub_active = 0; 16815dabedeeSbonwick cv_broadcast(&spa->spa_scrub_cv); 16825dabedeeSbonwick 16835dabedeeSbonwick mutex_exit(&spa->spa_scrub_lock); 16845dabedeeSbonwick 16855dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 16865dabedeeSbonwick 16875dabedeeSbonwick mutex_enter(&spa->spa_scrub_lock); 16885dabedeeSbonwick 16895dabedeeSbonwick /* 16905dabedeeSbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 16915dabedeeSbonwick * AND the spa config lock to synchronize with any config changes 16925dabedeeSbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 16935dabedeeSbonwick */ 1694fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 1695fa9e4066Sahrens error = ERESTART; 1696fa9e4066Sahrens 1697ea8dc4b6Seschrock if (spa->spa_scrub_stop) 1698ea8dc4b6Seschrock error = EINTR; 1699ea8dc4b6Seschrock 1700fa9e4066Sahrens /* 1701ea8dc4b6Seschrock * Even if there were uncorrectable errors, we consider the scrub 1702ea8dc4b6Seschrock * completed. The downside is that if there is a transient error during 1703ea8dc4b6Seschrock * a resilver, we won't resilver the data properly to the target. But 1704ea8dc4b6Seschrock * if the damage is permanent (more likely) we will resilver forever, 1705ea8dc4b6Seschrock * which isn't really acceptable. Since there is enough information for 1706ea8dc4b6Seschrock * the user to know what has failed and why, this seems like a more 1707ea8dc4b6Seschrock * tractable approach. 1708fa9e4066Sahrens */ 1709ea8dc4b6Seschrock complete = (error == 0); 1710fa9e4066Sahrens 1711ea8dc4b6Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 1712ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 1713fa9e4066Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 1714fa9e4066Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 1715fa9e4066Sahrens 1716fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1717fa9e4066Sahrens 1718fa9e4066Sahrens /* 1719fa9e4066Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 1720fa9e4066Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 1721fa9e4066Sahrens */ 1722fa9e4066Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 1723fa9e4066Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 1724fa9e4066Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 1725ea8dc4b6Seschrock spa_errlog_rotate(spa); 17265dabedeeSbonwick 1727ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1728fa9e4066Sahrens 1729fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1730fa9e4066Sahrens 1731ea8dc4b6Seschrock /* 1732ea8dc4b6Seschrock * We may have finished replacing a device. 1733ea8dc4b6Seschrock * Let the async thread assess this and handle the detach. 1734ea8dc4b6Seschrock */ 1735ea8dc4b6Seschrock spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 1736fa9e4066Sahrens 1737fa9e4066Sahrens /* 1738fa9e4066Sahrens * If we were told to restart, our final act is to start a new scrub. 1739fa9e4066Sahrens */ 1740fa9e4066Sahrens if (error == ERESTART) 1741ea8dc4b6Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 1742ea8dc4b6Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 1743fa9e4066Sahrens 1744ea8dc4b6Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 1745ea8dc4b6Seschrock spa->spa_scrub_active = 0; 1746ea8dc4b6Seschrock spa->spa_scrub_thread = NULL; 1747ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_cv); 1748fa9e4066Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 1749fa9e4066Sahrens thread_exit(); 1750fa9e4066Sahrens } 1751fa9e4066Sahrens 1752fa9e4066Sahrens void 1753fa9e4066Sahrens spa_scrub_suspend(spa_t *spa) 1754fa9e4066Sahrens { 1755fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1756ea8dc4b6Seschrock spa->spa_scrub_suspended++; 1757fa9e4066Sahrens while (spa->spa_scrub_active) { 1758fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 1759fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 1760fa9e4066Sahrens } 1761fa9e4066Sahrens while (spa->spa_scrub_inflight) 1762fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1763fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1764fa9e4066Sahrens } 1765fa9e4066Sahrens 1766fa9e4066Sahrens void 1767fa9e4066Sahrens spa_scrub_resume(spa_t *spa) 1768fa9e4066Sahrens { 1769fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1770ea8dc4b6Seschrock ASSERT(spa->spa_scrub_suspended != 0); 1771ea8dc4b6Seschrock if (--spa->spa_scrub_suspended == 0) 1772fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 1773fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1774fa9e4066Sahrens } 1775fa9e4066Sahrens 1776fa9e4066Sahrens void 1777fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 1778fa9e4066Sahrens { 1779fa9e4066Sahrens /* 1780fa9e4066Sahrens * Something happened (e.g. snapshot create/delete) that means 1781fa9e4066Sahrens * we must restart any in-progress scrubs. The itinerary will 1782fa9e4066Sahrens * fix this properly. 1783fa9e4066Sahrens */ 1784fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 1785fa9e4066Sahrens spa->spa_scrub_restart_txg = txg; 1786fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 1787fa9e4066Sahrens } 1788fa9e4066Sahrens 1789ea8dc4b6Seschrock int 1790ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 1791fa9e4066Sahrens { 1792fa9e4066Sahrens space_seg_t *ss; 1793fa9e4066Sahrens uint64_t mintxg, maxtxg; 1794fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 1795fa9e4066Sahrens 1796fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 1797fa9e4066Sahrens return (ENOTSUP); 1798fa9e4066Sahrens 1799ea8dc4b6Seschrock mutex_enter(&spa->spa_scrub_lock); 1800ea8dc4b6Seschrock 1801fa9e4066Sahrens /* 1802fa9e4066Sahrens * If there's a scrub or resilver already in progress, stop it. 1803fa9e4066Sahrens */ 1804fa9e4066Sahrens while (spa->spa_scrub_thread != NULL) { 1805fa9e4066Sahrens /* 1806fa9e4066Sahrens * Don't stop a resilver unless forced. 1807fa9e4066Sahrens */ 1808ea8dc4b6Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 1809ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 1810fa9e4066Sahrens return (EBUSY); 1811ea8dc4b6Seschrock } 1812fa9e4066Sahrens spa->spa_scrub_stop = 1; 1813fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 1814fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 1815fa9e4066Sahrens } 1816fa9e4066Sahrens 1817fa9e4066Sahrens /* 1818fa9e4066Sahrens * Terminate the previous traverse. 1819fa9e4066Sahrens */ 1820fa9e4066Sahrens if (spa->spa_scrub_th != NULL) { 1821fa9e4066Sahrens traverse_fini(spa->spa_scrub_th); 1822fa9e4066Sahrens spa->spa_scrub_th = NULL; 1823fa9e4066Sahrens } 1824fa9e4066Sahrens 1825ea8dc4b6Seschrock if (rvd == NULL) { 1826ea8dc4b6Seschrock ASSERT(spa->spa_scrub_stop == 0); 1827ea8dc4b6Seschrock ASSERT(spa->spa_scrub_type == type); 1828ea8dc4b6Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 1829ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 1830ea8dc4b6Seschrock return (0); 1831ea8dc4b6Seschrock } 1832fa9e4066Sahrens 1833fa9e4066Sahrens mintxg = TXG_INITIAL - 1; 1834fa9e4066Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 1835fa9e4066Sahrens 1836ea8dc4b6Seschrock mutex_enter(&rvd->vdev_dtl_lock); 1837fa9e4066Sahrens 1838ea8dc4b6Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 1839ea8dc4b6Seschrock /* 1840ea8dc4b6Seschrock * The pool-wide DTL is empty. 1841ecc2d604Sbonwick * If this is a resilver, there's nothing to do except 1842ecc2d604Sbonwick * check whether any in-progress replacements have completed. 1843ea8dc4b6Seschrock */ 1844ecc2d604Sbonwick if (type == POOL_SCRUB_RESILVER) { 1845ea8dc4b6Seschrock type = POOL_SCRUB_NONE; 1846ecc2d604Sbonwick spa_async_request(spa, SPA_ASYNC_REPLACE_DONE); 1847ecc2d604Sbonwick } 1848ea8dc4b6Seschrock } else { 1849ea8dc4b6Seschrock /* 1850ea8dc4b6Seschrock * The pool-wide DTL is non-empty. 1851ea8dc4b6Seschrock * If this is a normal scrub, upgrade to a resilver instead. 1852ea8dc4b6Seschrock */ 1853ea8dc4b6Seschrock if (type == POOL_SCRUB_EVERYTHING) 1854ea8dc4b6Seschrock type = POOL_SCRUB_RESILVER; 1855ea8dc4b6Seschrock } 1856fa9e4066Sahrens 1857ea8dc4b6Seschrock if (type == POOL_SCRUB_RESILVER) { 1858fa9e4066Sahrens /* 1859fa9e4066Sahrens * Determine the resilvering boundaries. 1860fa9e4066Sahrens * 1861fa9e4066Sahrens * Note: (mintxg, maxtxg) is an open interval, 1862fa9e4066Sahrens * i.e. mintxg and maxtxg themselves are not included. 1863fa9e4066Sahrens * 1864fa9e4066Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 1865fa9e4066Sahrens * so we don't claim to resilver a txg that's still changing. 1866fa9e4066Sahrens */ 1867fa9e4066Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 1868ea8dc4b6Seschrock mintxg = ss->ss_start - 1; 1869fa9e4066Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 1870ea8dc4b6Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 1871fa9e4066Sahrens } 1872fa9e4066Sahrens 1873ea8dc4b6Seschrock mutex_exit(&rvd->vdev_dtl_lock); 1874ea8dc4b6Seschrock 1875ea8dc4b6Seschrock spa->spa_scrub_stop = 0; 1876ea8dc4b6Seschrock spa->spa_scrub_type = type; 1877ea8dc4b6Seschrock spa->spa_scrub_restart_txg = 0; 1878ea8dc4b6Seschrock 1879ea8dc4b6Seschrock if (type != POOL_SCRUB_NONE) { 1880ea8dc4b6Seschrock spa->spa_scrub_mintxg = mintxg; 1881fa9e4066Sahrens spa->spa_scrub_maxtxg = maxtxg; 1882fa9e4066Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 18830373e76bSbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 18840373e76bSbonwick ZIO_FLAG_CANFAIL); 1885fa9e4066Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 1886fa9e4066Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 1887fa9e4066Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 1888fa9e4066Sahrens } 1889fa9e4066Sahrens 1890ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 1891ea8dc4b6Seschrock 1892fa9e4066Sahrens return (0); 1893fa9e4066Sahrens } 1894fa9e4066Sahrens 1895ea8dc4b6Seschrock /* 1896ea8dc4b6Seschrock * ========================================================================== 1897ea8dc4b6Seschrock * SPA async task processing 1898ea8dc4b6Seschrock * ========================================================================== 1899ea8dc4b6Seschrock */ 1900ea8dc4b6Seschrock 1901ea8dc4b6Seschrock static void 1902ea8dc4b6Seschrock spa_async_reopen(spa_t *spa) 1903fa9e4066Sahrens { 1904ea8dc4b6Seschrock vdev_t *rvd = spa->spa_root_vdev; 1905ea8dc4b6Seschrock vdev_t *tvd; 1906ea8dc4b6Seschrock int c; 1907fa9e4066Sahrens 1908ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1909ea8dc4b6Seschrock 1910ea8dc4b6Seschrock for (c = 0; c < rvd->vdev_children; c++) { 1911ea8dc4b6Seschrock tvd = rvd->vdev_child[c]; 1912ea8dc4b6Seschrock if (tvd->vdev_reopen_wanted) { 1913ea8dc4b6Seschrock tvd->vdev_reopen_wanted = 0; 1914ea8dc4b6Seschrock vdev_reopen(tvd); 1915ea8dc4b6Seschrock } 1916ea8dc4b6Seschrock } 1917ea8dc4b6Seschrock 1918ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1919ea8dc4b6Seschrock } 1920fa9e4066Sahrens 1921ea8dc4b6Seschrock static void 1922ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 1923ea8dc4b6Seschrock { 1924ea8dc4b6Seschrock int tasks; 1925ea8dc4b6Seschrock 1926ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 1927ea8dc4b6Seschrock 1928ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 1929ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 1930ea8dc4b6Seschrock spa->spa_async_tasks = 0; 1931ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 1932ea8dc4b6Seschrock 19330373e76bSbonwick /* 19340373e76bSbonwick * See if the config needs to be updated. 19350373e76bSbonwick */ 19360373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 19370373e76bSbonwick mutex_enter(&spa_namespace_lock); 19380373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 19390373e76bSbonwick mutex_exit(&spa_namespace_lock); 19400373e76bSbonwick } 19410373e76bSbonwick 1942ea8dc4b6Seschrock /* 1943ea8dc4b6Seschrock * See if any devices need to be reopened. 1944ea8dc4b6Seschrock */ 1945ea8dc4b6Seschrock if (tasks & SPA_ASYNC_REOPEN) 1946ea8dc4b6Seschrock spa_async_reopen(spa); 1947ea8dc4b6Seschrock 1948ea8dc4b6Seschrock /* 1949ea8dc4b6Seschrock * If any devices are done replacing, detach them. 1950ea8dc4b6Seschrock */ 1951ea8dc4b6Seschrock if (tasks & SPA_ASYNC_REPLACE_DONE) 1952fa9e4066Sahrens spa_vdev_replace_done(spa); 1953fa9e4066Sahrens 1954ea8dc4b6Seschrock /* 1955ea8dc4b6Seschrock * Kick off a scrub. 1956ea8dc4b6Seschrock */ 1957ea8dc4b6Seschrock if (tasks & SPA_ASYNC_SCRUB) 1958ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 1959ea8dc4b6Seschrock 1960ea8dc4b6Seschrock /* 1961ea8dc4b6Seschrock * Kick off a resilver. 1962ea8dc4b6Seschrock */ 1963ea8dc4b6Seschrock if (tasks & SPA_ASYNC_RESILVER) 1964ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1965ea8dc4b6Seschrock 1966ea8dc4b6Seschrock /* 1967ea8dc4b6Seschrock * Let the world know that we're done. 1968ea8dc4b6Seschrock */ 1969ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 1970ea8dc4b6Seschrock spa->spa_async_thread = NULL; 1971ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 1972ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 1973ea8dc4b6Seschrock thread_exit(); 1974ea8dc4b6Seschrock } 1975ea8dc4b6Seschrock 1976ea8dc4b6Seschrock void 1977ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 1978ea8dc4b6Seschrock { 1979ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 1980ea8dc4b6Seschrock spa->spa_async_suspended++; 1981ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 1982ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 1983ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 1984ea8dc4b6Seschrock } 1985ea8dc4b6Seschrock 1986ea8dc4b6Seschrock void 1987ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 1988ea8dc4b6Seschrock { 1989ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 1990ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 1991ea8dc4b6Seschrock spa->spa_async_suspended--; 1992ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 1993ea8dc4b6Seschrock } 1994ea8dc4b6Seschrock 1995ea8dc4b6Seschrock static void 1996ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 1997ea8dc4b6Seschrock { 1998ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 1999ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 20000373e76bSbonwick spa->spa_async_thread == NULL && 20010373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 2002ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 2003ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 2004ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 2005ea8dc4b6Seschrock } 2006ea8dc4b6Seschrock 2007ea8dc4b6Seschrock void 2008ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 2009ea8dc4b6Seschrock { 2010ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 2011ea8dc4b6Seschrock spa->spa_async_tasks |= task; 2012ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 2013fa9e4066Sahrens } 2014fa9e4066Sahrens 2015fa9e4066Sahrens /* 2016fa9e4066Sahrens * ========================================================================== 2017fa9e4066Sahrens * SPA syncing routines 2018fa9e4066Sahrens * ========================================================================== 2019fa9e4066Sahrens */ 2020fa9e4066Sahrens 2021fa9e4066Sahrens static void 2022fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2023fa9e4066Sahrens { 2024fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 2025fa9e4066Sahrens dmu_tx_t *tx; 2026fa9e4066Sahrens blkptr_t blk; 2027fa9e4066Sahrens uint64_t itor = 0; 2028fa9e4066Sahrens zio_t *zio; 2029fa9e4066Sahrens int error; 2030fa9e4066Sahrens uint8_t c = 1; 2031fa9e4066Sahrens 2032fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2033fa9e4066Sahrens 2034fa9e4066Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 2035fa9e4066Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2036fa9e4066Sahrens 2037fa9e4066Sahrens error = zio_wait(zio); 2038fa9e4066Sahrens ASSERT3U(error, ==, 0); 2039fa9e4066Sahrens 2040fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2041fa9e4066Sahrens bplist_vacate(bpl, tx); 2042fa9e4066Sahrens 2043fa9e4066Sahrens /* 2044fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 2045fa9e4066Sahrens * (Usually only the first block is needed.) 2046fa9e4066Sahrens */ 2047fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2048fa9e4066Sahrens dmu_tx_commit(tx); 2049fa9e4066Sahrens } 2050fa9e4066Sahrens 2051fa9e4066Sahrens static void 2052fa9e4066Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2053fa9e4066Sahrens { 2054fa9e4066Sahrens nvlist_t *config; 2055fa9e4066Sahrens char *packed = NULL; 2056fa9e4066Sahrens size_t nvsize = 0; 2057fa9e4066Sahrens dmu_buf_t *db; 2058fa9e4066Sahrens 2059fa9e4066Sahrens if (list_is_empty(&spa->spa_dirty_list)) 2060fa9e4066Sahrens return; 2061fa9e4066Sahrens 2062fa9e4066Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2063fa9e4066Sahrens 20640373e76bSbonwick if (spa->spa_config_syncing) 20650373e76bSbonwick nvlist_free(spa->spa_config_syncing); 20660373e76bSbonwick spa->spa_config_syncing = config; 2067fa9e4066Sahrens 2068fa9e4066Sahrens VERIFY(nvlist_size(config, &nvsize, NV_ENCODE_XDR) == 0); 2069fa9e4066Sahrens 2070fa9e4066Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 2071fa9e4066Sahrens 2072ea8dc4b6Seschrock VERIFY(nvlist_pack(config, &packed, &nvsize, NV_ENCODE_XDR, 2073ea8dc4b6Seschrock KM_SLEEP) == 0); 2074fa9e4066Sahrens 2075fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_config_object, 0, nvsize, 2076fa9e4066Sahrens packed, tx); 2077fa9e4066Sahrens 2078fa9e4066Sahrens kmem_free(packed, nvsize); 2079fa9e4066Sahrens 2080ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, 2081ea8dc4b6Seschrock spa->spa_config_object, FTAG, &db)); 2082fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 2083fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 2084ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 2085fa9e4066Sahrens } 2086fa9e4066Sahrens 2087fa9e4066Sahrens /* 2088fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 2089fa9e4066Sahrens * part of the process, so we iterate until it converges. 2090fa9e4066Sahrens */ 2091fa9e4066Sahrens void 2092fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 2093fa9e4066Sahrens { 2094fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 2095fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 2096fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 20970373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 2098fa9e4066Sahrens vdev_t *vd; 2099fa9e4066Sahrens dmu_tx_t *tx; 2100fa9e4066Sahrens int dirty_vdevs; 2101fa9e4066Sahrens 2102fa9e4066Sahrens /* 2103fa9e4066Sahrens * Lock out configuration changes. 2104fa9e4066Sahrens */ 2105ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2106fa9e4066Sahrens 2107fa9e4066Sahrens spa->spa_syncing_txg = txg; 2108fa9e4066Sahrens spa->spa_sync_pass = 0; 2109fa9e4066Sahrens 2110ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 2111fa9e4066Sahrens 2112fa9e4066Sahrens /* 2113fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 2114fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 2115fa9e4066Sahrens * don't generate work on an otherwise idle system. 2116fa9e4066Sahrens */ 2117fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 2118fa9e4066Sahrens !txg_list_empty(&dp->dp_dirty_dirs, txg)) 2119fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 2120fa9e4066Sahrens 2121fa9e4066Sahrens /* 2122fa9e4066Sahrens * Iterate to convergence. 2123fa9e4066Sahrens */ 2124fa9e4066Sahrens do { 2125fa9e4066Sahrens spa->spa_sync_pass++; 2126fa9e4066Sahrens 2127fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 2128fa9e4066Sahrens spa_sync_config_object(spa, tx); 2129fa9e4066Sahrens dmu_tx_commit(tx); 2130fa9e4066Sahrens 2131ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 2132ea8dc4b6Seschrock 2133fa9e4066Sahrens dsl_pool_sync(dp, txg); 2134fa9e4066Sahrens 2135fa9e4066Sahrens dirty_vdevs = 0; 2136fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 2137fa9e4066Sahrens vdev_sync(vd, txg); 2138fa9e4066Sahrens dirty_vdevs++; 2139fa9e4066Sahrens } 2140fa9e4066Sahrens 2141fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 2142fa9e4066Sahrens bplist_sync(bpl, tx); 2143fa9e4066Sahrens dmu_tx_commit(tx); 2144fa9e4066Sahrens 2145fa9e4066Sahrens } while (dirty_vdevs); 2146fa9e4066Sahrens 2147fa9e4066Sahrens bplist_close(bpl); 2148fa9e4066Sahrens 2149fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 2150fa9e4066Sahrens 2151fa9e4066Sahrens /* 2152fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 2153fa9e4066Sahrens * to commit the transaction group. 21540373e76bSbonwick * 21550373e76bSbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 21560373e76bSbonwick * Otherwise, pick a random top-level vdev that's known to be 21570373e76bSbonwick * visible in the config cache (see spa_vdev_add() for details). 21580373e76bSbonwick * If the write fails, try the next vdev until we're tried them all. 21590373e76bSbonwick */ 21600373e76bSbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 21610373e76bSbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 21620373e76bSbonwick } else { 21630373e76bSbonwick int children = rvd->vdev_children; 21640373e76bSbonwick int c0 = spa_get_random(children); 21650373e76bSbonwick int c; 21660373e76bSbonwick 21670373e76bSbonwick for (c = 0; c < children; c++) { 21680373e76bSbonwick vd = rvd->vdev_child[(c0 + c) % children]; 21690373e76bSbonwick if (vd->vdev_ms_array == 0) 21700373e76bSbonwick continue; 21710373e76bSbonwick if (vdev_config_sync(vd, txg) == 0) 21720373e76bSbonwick break; 21730373e76bSbonwick } 21740373e76bSbonwick if (c == children) 21750373e76bSbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 21760373e76bSbonwick } 21770373e76bSbonwick 21780373e76bSbonwick /* 21790373e76bSbonwick * Clear the dirty config list. 2180fa9e4066Sahrens */ 21810373e76bSbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 21820373e76bSbonwick vdev_config_clean(vd); 21830373e76bSbonwick 21840373e76bSbonwick /* 21850373e76bSbonwick * Now that the new config has synced transactionally, 21860373e76bSbonwick * let it become visible to the config cache. 21870373e76bSbonwick */ 21880373e76bSbonwick if (spa->spa_config_syncing != NULL) { 21890373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 21900373e76bSbonwick spa->spa_config_txg = txg; 21910373e76bSbonwick spa->spa_config_syncing = NULL; 21920373e76bSbonwick } 2193fa9e4066Sahrens 2194fa9e4066Sahrens /* 2195fa9e4066Sahrens * Make a stable copy of the fully synced uberblock. 2196fa9e4066Sahrens * We use this as the root for pool traversals. 2197fa9e4066Sahrens */ 2198fa9e4066Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 2199fa9e4066Sahrens 2200fa9e4066Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 2201fa9e4066Sahrens 2202fa9e4066Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 2203fa9e4066Sahrens spa->spa_traverse_wanted = 0; 2204fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2205fa9e4066Sahrens rw_exit(&spa->spa_traverse_lock); 2206fa9e4066Sahrens 2207fa9e4066Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 2208fa9e4066Sahrens 2209fa9e4066Sahrens /* 2210fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 2211fa9e4066Sahrens */ 2212fa9e4066Sahrens dsl_pool_zil_clean(dp); 2213fa9e4066Sahrens 2214fa9e4066Sahrens /* 2215fa9e4066Sahrens * Update usable space statistics. 2216fa9e4066Sahrens */ 2217fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 2218fa9e4066Sahrens vdev_sync_done(vd, txg); 2219fa9e4066Sahrens 2220fa9e4066Sahrens /* 2221fa9e4066Sahrens * It had better be the case that we didn't dirty anything 2222fa9e4066Sahrens * since spa_sync_labels(). 2223fa9e4066Sahrens */ 2224fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 2225fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 2226fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 2227fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 2228fa9e4066Sahrens 2229ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2230ea8dc4b6Seschrock 2231ea8dc4b6Seschrock /* 2232ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 2233ea8dc4b6Seschrock */ 2234ea8dc4b6Seschrock spa_async_dispatch(spa); 2235fa9e4066Sahrens } 2236fa9e4066Sahrens 2237fa9e4066Sahrens /* 2238fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 2239fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 2240fa9e4066Sahrens * sync. 2241fa9e4066Sahrens */ 2242fa9e4066Sahrens void 2243fa9e4066Sahrens spa_sync_allpools(void) 2244fa9e4066Sahrens { 2245fa9e4066Sahrens spa_t *spa = NULL; 2246fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2247fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 2248fa9e4066Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 2249fa9e4066Sahrens continue; 2250fa9e4066Sahrens spa_open_ref(spa, FTAG); 2251fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2252fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 2253fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2254fa9e4066Sahrens spa_close(spa, FTAG); 2255fa9e4066Sahrens } 2256fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2257fa9e4066Sahrens } 2258fa9e4066Sahrens 2259fa9e4066Sahrens /* 2260fa9e4066Sahrens * ========================================================================== 2261fa9e4066Sahrens * Miscellaneous routines 2262fa9e4066Sahrens * ========================================================================== 2263fa9e4066Sahrens */ 2264fa9e4066Sahrens 2265fa9e4066Sahrens /* 2266fa9e4066Sahrens * Remove all pools in the system. 2267fa9e4066Sahrens */ 2268fa9e4066Sahrens void 2269fa9e4066Sahrens spa_evict_all(void) 2270fa9e4066Sahrens { 2271fa9e4066Sahrens spa_t *spa; 2272fa9e4066Sahrens 2273fa9e4066Sahrens /* 2274fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 2275fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 2276fa9e4066Sahrens */ 2277fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2278fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 2279fa9e4066Sahrens /* 2280ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 2281ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 2282ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 2283fa9e4066Sahrens */ 2284fa9e4066Sahrens spa_open_ref(spa, FTAG); 2285fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2286ea8dc4b6Seschrock spa_async_suspend(spa); 2287fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2288fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2289fa9e4066Sahrens spa_close(spa, FTAG); 2290fa9e4066Sahrens 2291fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2292fa9e4066Sahrens spa_unload(spa); 2293fa9e4066Sahrens spa_deactivate(spa); 2294fa9e4066Sahrens } 2295fa9e4066Sahrens spa_remove(spa); 2296fa9e4066Sahrens } 2297fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2298fa9e4066Sahrens } 2299ea8dc4b6Seschrock 2300ea8dc4b6Seschrock vdev_t * 2301ea8dc4b6Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 2302ea8dc4b6Seschrock { 2303ea8dc4b6Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 2304ea8dc4b6Seschrock } 2305eaca9bbdSeschrock 2306eaca9bbdSeschrock void 2307eaca9bbdSeschrock spa_upgrade(spa_t *spa) 2308eaca9bbdSeschrock { 2309eaca9bbdSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 2310eaca9bbdSeschrock 2311eaca9bbdSeschrock /* 2312eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 2313eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 2314eaca9bbdSeschrock * possible. 2315eaca9bbdSeschrock */ 2316eaca9bbdSeschrock ASSERT(spa->spa_uberblock.ub_version <= ZFS_VERSION); 2317eaca9bbdSeschrock 2318eaca9bbdSeschrock spa->spa_uberblock.ub_version = ZFS_VERSION; 2319eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 2320eaca9bbdSeschrock 2321eaca9bbdSeschrock spa_config_exit(spa, FTAG); 2322eaca9bbdSeschrock } 2323