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 */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 2339c23413Seschrock * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24fa9e4066Sahrens * Use is subject to license terms. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28fa9e4066Sahrens 29fa9e4066Sahrens /* 30fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 31fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 32fa9e4066Sahrens * pool. 33fa9e4066Sahrens */ 34fa9e4066Sahrens 35fa9e4066Sahrens #include <sys/zfs_context.h> 36ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 37fa9e4066Sahrens #include <sys/spa_impl.h> 38fa9e4066Sahrens #include <sys/zio.h> 39fa9e4066Sahrens #include <sys/zio_checksum.h> 40fa9e4066Sahrens #include <sys/zio_compress.h> 41fa9e4066Sahrens #include <sys/dmu.h> 42fa9e4066Sahrens #include <sys/dmu_tx.h> 43fa9e4066Sahrens #include <sys/zap.h> 44fa9e4066Sahrens #include <sys/zil.h> 45fa9e4066Sahrens #include <sys/vdev_impl.h> 46fa9e4066Sahrens #include <sys/metaslab.h> 47fa9e4066Sahrens #include <sys/uberblock_impl.h> 48fa9e4066Sahrens #include <sys/txg.h> 49fa9e4066Sahrens #include <sys/avl.h> 50fa9e4066Sahrens #include <sys/dmu_traverse.h> 51b1b8ab34Slling #include <sys/dmu_objset.h> 52fa9e4066Sahrens #include <sys/unique.h> 53fa9e4066Sahrens #include <sys/dsl_pool.h> 54b1b8ab34Slling #include <sys/dsl_dataset.h> 55fa9e4066Sahrens #include <sys/dsl_dir.h> 56fa9e4066Sahrens #include <sys/dsl_prop.h> 57b1b8ab34Slling #include <sys/dsl_synctask.h> 58fa9e4066Sahrens #include <sys/fs/zfs.h> 59fa9e4066Sahrens #include <sys/callb.h> 6095173954Sek #include <sys/systeminfo.h> 6195173954Sek #include <sys/sunddi.h> 62fa9e4066Sahrens 63*990b4856Slling #include "zfs_prop.h" 64*990b4856Slling 65416e0cd8Sek int zio_taskq_threads = 8; 66416e0cd8Sek 67*990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 68*990b4856Slling 69*990b4856Slling /* 70*990b4856Slling * ========================================================================== 71*990b4856Slling * SPA properties routines 72*990b4856Slling * ========================================================================== 73*990b4856Slling */ 74*990b4856Slling 75*990b4856Slling /* 76*990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 77*990b4856Slling */ 78*990b4856Slling static int 79*990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 80*990b4856Slling uint64_t intval, zprop_source_t src) 81*990b4856Slling { 82*990b4856Slling const char *propname = zpool_prop_to_name(prop); 83*990b4856Slling nvlist_t *propval; 84*990b4856Slling int err = 0; 85*990b4856Slling 86*990b4856Slling if (err = nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP)) 87*990b4856Slling return (err); 88*990b4856Slling 89*990b4856Slling if (err = nvlist_add_uint64(propval, ZPROP_SOURCE, src)) 90*990b4856Slling goto out; 91*990b4856Slling 92*990b4856Slling if (strval != NULL) { 93*990b4856Slling if (err = nvlist_add_string(propval, ZPROP_VALUE, strval)) 94*990b4856Slling goto out; 95*990b4856Slling } else { 96*990b4856Slling if (err = nvlist_add_uint64(propval, ZPROP_VALUE, intval)) 97*990b4856Slling goto out; 98*990b4856Slling } 99*990b4856Slling 100*990b4856Slling err = nvlist_add_nvlist(nvl, propname, propval); 101*990b4856Slling out: 102*990b4856Slling nvlist_free(propval); 103*990b4856Slling return (err); 104*990b4856Slling } 105*990b4856Slling 106*990b4856Slling /* 107*990b4856Slling * Get property values from the spa configuration. 108*990b4856Slling */ 109*990b4856Slling static int 110*990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 111*990b4856Slling { 112*990b4856Slling uint64_t size = spa_get_space(spa); 113*990b4856Slling uint64_t used = spa_get_alloc(spa); 114*990b4856Slling uint64_t cap, version; 115*990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 116*990b4856Slling int err; 117*990b4856Slling 118*990b4856Slling /* 119*990b4856Slling * readonly properties 120*990b4856Slling */ 121*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 122*990b4856Slling 0, src)) 123*990b4856Slling return (err); 124*990b4856Slling 125*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src)) 126*990b4856Slling return (err); 127*990b4856Slling 128*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src)) 129*990b4856Slling return (err); 130*990b4856Slling 131*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, 132*990b4856Slling size - used, src)) 133*990b4856Slling return (err); 134*990b4856Slling 135*990b4856Slling cap = (size == 0) ? 0 : (used * 100 / size); 136*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src)) 137*990b4856Slling return (err); 138*990b4856Slling 139*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, 140*990b4856Slling spa_guid(spa), src)) 141*990b4856Slling return (err); 142*990b4856Slling 143*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 144*990b4856Slling spa->spa_root_vdev->vdev_state, src)) 145*990b4856Slling return (err); 146*990b4856Slling 147*990b4856Slling /* 148*990b4856Slling * settable properties that are not stored in the pool property object. 149*990b4856Slling */ 150*990b4856Slling version = spa_version(spa); 151*990b4856Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 152*990b4856Slling src = ZPROP_SRC_DEFAULT; 153*990b4856Slling else 154*990b4856Slling src = ZPROP_SRC_LOCAL; 155*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, 156*990b4856Slling version, src)) 157*990b4856Slling return (err); 158*990b4856Slling 159*990b4856Slling if (spa->spa_root != NULL) { 160*990b4856Slling src = ZPROP_SRC_LOCAL; 161*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, 162*990b4856Slling spa->spa_root, 0, src)) 163*990b4856Slling return (err); 164*990b4856Slling } 165*990b4856Slling 166*990b4856Slling if (spa->spa_temporary == 167*990b4856Slling zpool_prop_default_numeric(ZPOOL_PROP_TEMPORARY)) 168*990b4856Slling src = ZPROP_SRC_DEFAULT; 169*990b4856Slling else 170*990b4856Slling src = ZPROP_SRC_LOCAL; 171*990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_TEMPORARY, NULL, 172*990b4856Slling spa->spa_temporary, src)) 173*990b4856Slling return (err); 174*990b4856Slling 175*990b4856Slling return (0); 176*990b4856Slling } 177*990b4856Slling 178*990b4856Slling /* 179*990b4856Slling * Get zpool property values. 180*990b4856Slling */ 181*990b4856Slling int 182*990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 183*990b4856Slling { 184*990b4856Slling zap_cursor_t zc; 185*990b4856Slling zap_attribute_t za; 186*990b4856Slling objset_t *mos = spa->spa_meta_objset; 187*990b4856Slling int err; 188*990b4856Slling 189*990b4856Slling if (err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)) 190*990b4856Slling return (err); 191*990b4856Slling 192*990b4856Slling /* 193*990b4856Slling * Get properties from the spa config. 194*990b4856Slling */ 195*990b4856Slling if (err = spa_prop_get_config(spa, nvp)) 196*990b4856Slling goto out; 197*990b4856Slling 198*990b4856Slling mutex_enter(&spa->spa_props_lock); 199*990b4856Slling /* If no pool property object, no more prop to get. */ 200*990b4856Slling if (spa->spa_pool_props_object == 0) { 201*990b4856Slling mutex_exit(&spa->spa_props_lock); 202*990b4856Slling return (0); 203*990b4856Slling } 204*990b4856Slling 205*990b4856Slling /* 206*990b4856Slling * Get properties from the MOS pool property object. 207*990b4856Slling */ 208*990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 209*990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 210*990b4856Slling zap_cursor_advance(&zc)) { 211*990b4856Slling uint64_t intval = 0; 212*990b4856Slling char *strval = NULL; 213*990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 214*990b4856Slling zpool_prop_t prop; 215*990b4856Slling 216*990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 217*990b4856Slling continue; 218*990b4856Slling 219*990b4856Slling switch (za.za_integer_length) { 220*990b4856Slling case 8: 221*990b4856Slling /* integer property */ 222*990b4856Slling if (za.za_first_integer != 223*990b4856Slling zpool_prop_default_numeric(prop)) 224*990b4856Slling src = ZPROP_SRC_LOCAL; 225*990b4856Slling 226*990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 227*990b4856Slling dsl_pool_t *dp; 228*990b4856Slling dsl_dataset_t *ds = NULL; 229*990b4856Slling 230*990b4856Slling dp = spa_get_dsl(spa); 231*990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 232*990b4856Slling if (err = dsl_dataset_open_obj(dp, 233*990b4856Slling za.za_first_integer, NULL, DS_MODE_NONE, 234*990b4856Slling FTAG, &ds)) { 235*990b4856Slling rw_exit(&dp->dp_config_rwlock); 236*990b4856Slling break; 237*990b4856Slling } 238*990b4856Slling 239*990b4856Slling strval = kmem_alloc( 240*990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 241*990b4856Slling KM_SLEEP); 242*990b4856Slling dsl_dataset_name(ds, strval); 243*990b4856Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 244*990b4856Slling rw_exit(&dp->dp_config_rwlock); 245*990b4856Slling } else { 246*990b4856Slling strval = NULL; 247*990b4856Slling intval = za.za_first_integer; 248*990b4856Slling } 249*990b4856Slling 250*990b4856Slling err = spa_prop_add_list(*nvp, prop, strval, 251*990b4856Slling intval, src); 252*990b4856Slling 253*990b4856Slling if (strval != NULL) 254*990b4856Slling kmem_free(strval, 255*990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 256*990b4856Slling 257*990b4856Slling break; 258*990b4856Slling 259*990b4856Slling case 1: 260*990b4856Slling /* string property */ 261*990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 262*990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 263*990b4856Slling za.za_name, 1, za.za_num_integers, strval); 264*990b4856Slling if (err) { 265*990b4856Slling kmem_free(strval, za.za_num_integers); 266*990b4856Slling break; 267*990b4856Slling } 268*990b4856Slling err = spa_prop_add_list(*nvp, prop, strval, 0, src); 269*990b4856Slling kmem_free(strval, za.za_num_integers); 270*990b4856Slling break; 271*990b4856Slling 272*990b4856Slling default: 273*990b4856Slling break; 274*990b4856Slling } 275*990b4856Slling } 276*990b4856Slling zap_cursor_fini(&zc); 277*990b4856Slling mutex_exit(&spa->spa_props_lock); 278*990b4856Slling out: 279*990b4856Slling if (err && err != ENOENT) { 280*990b4856Slling nvlist_free(*nvp); 281*990b4856Slling return (err); 282*990b4856Slling } 283*990b4856Slling 284*990b4856Slling return (0); 285*990b4856Slling } 286*990b4856Slling 287*990b4856Slling /* 288*990b4856Slling * Validate the given pool properties nvlist and modify the list 289*990b4856Slling * for the property values to be set. 290*990b4856Slling */ 291*990b4856Slling static int 292*990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 293*990b4856Slling { 294*990b4856Slling nvpair_t *elem; 295*990b4856Slling int error = 0, reset_bootfs = 0; 296*990b4856Slling uint64_t objnum; 297*990b4856Slling 298*990b4856Slling elem = NULL; 299*990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 300*990b4856Slling zpool_prop_t prop; 301*990b4856Slling char *propname, *strval; 302*990b4856Slling uint64_t intval; 303*990b4856Slling vdev_t *rvdev; 304*990b4856Slling char *vdev_type; 305*990b4856Slling objset_t *os; 306*990b4856Slling 307*990b4856Slling propname = nvpair_name(elem); 308*990b4856Slling 309*990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 310*990b4856Slling return (EINVAL); 311*990b4856Slling 312*990b4856Slling switch (prop) { 313*990b4856Slling case ZPOOL_PROP_VERSION: 314*990b4856Slling error = nvpair_value_uint64(elem, &intval); 315*990b4856Slling if (!error && 316*990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 317*990b4856Slling error = EINVAL; 318*990b4856Slling break; 319*990b4856Slling 320*990b4856Slling case ZPOOL_PROP_DELEGATION: 321*990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 322*990b4856Slling error = nvpair_value_uint64(elem, &intval); 323*990b4856Slling if (!error && intval > 1) 324*990b4856Slling error = EINVAL; 325*990b4856Slling break; 326*990b4856Slling 327*990b4856Slling case ZPOOL_PROP_BOOTFS: 328*990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 329*990b4856Slling error = ENOTSUP; 330*990b4856Slling break; 331*990b4856Slling } 332*990b4856Slling 333*990b4856Slling /* 334*990b4856Slling * A bootable filesystem can not be on a RAIDZ pool 335*990b4856Slling * nor a striped pool with more than 1 device. 336*990b4856Slling */ 337*990b4856Slling rvdev = spa->spa_root_vdev; 338*990b4856Slling vdev_type = 339*990b4856Slling rvdev->vdev_child[0]->vdev_ops->vdev_op_type; 340*990b4856Slling if (rvdev->vdev_children > 1 || 341*990b4856Slling strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 342*990b4856Slling strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 343*990b4856Slling error = ENOTSUP; 344*990b4856Slling break; 345*990b4856Slling } 346*990b4856Slling 347*990b4856Slling reset_bootfs = 1; 348*990b4856Slling 349*990b4856Slling error = nvpair_value_string(elem, &strval); 350*990b4856Slling 351*990b4856Slling if (!error) { 352*990b4856Slling if (strval == NULL || strval[0] == '\0') { 353*990b4856Slling objnum = zpool_prop_default_numeric( 354*990b4856Slling ZPOOL_PROP_BOOTFS); 355*990b4856Slling break; 356*990b4856Slling } 357*990b4856Slling 358*990b4856Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 359*990b4856Slling DS_MODE_STANDARD | DS_MODE_READONLY, &os)) 360*990b4856Slling break; 361*990b4856Slling objnum = dmu_objset_id(os); 362*990b4856Slling dmu_objset_close(os); 363*990b4856Slling } 364*990b4856Slling break; 365*990b4856Slling } 366*990b4856Slling 367*990b4856Slling if (error) 368*990b4856Slling break; 369*990b4856Slling } 370*990b4856Slling 371*990b4856Slling if (!error && reset_bootfs) { 372*990b4856Slling error = nvlist_remove(props, 373*990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 374*990b4856Slling 375*990b4856Slling if (!error) { 376*990b4856Slling error = nvlist_add_uint64(props, 377*990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 378*990b4856Slling } 379*990b4856Slling } 380*990b4856Slling 381*990b4856Slling return (error); 382*990b4856Slling } 383*990b4856Slling 384*990b4856Slling int 385*990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 386*990b4856Slling { 387*990b4856Slling int error; 388*990b4856Slling 389*990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 390*990b4856Slling return (error); 391*990b4856Slling 392*990b4856Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 393*990b4856Slling spa, nvp, 3)); 394*990b4856Slling } 395*990b4856Slling 396*990b4856Slling /* 397*990b4856Slling * If the bootfs property value is dsobj, clear it. 398*990b4856Slling */ 399*990b4856Slling void 400*990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 401*990b4856Slling { 402*990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 403*990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 404*990b4856Slling spa->spa_pool_props_object, 405*990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 406*990b4856Slling spa->spa_bootfs = 0; 407*990b4856Slling } 408*990b4856Slling } 409*990b4856Slling 410fa9e4066Sahrens /* 411fa9e4066Sahrens * ========================================================================== 412fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 413fa9e4066Sahrens * ========================================================================== 414fa9e4066Sahrens */ 415fa9e4066Sahrens 416ea8dc4b6Seschrock static int 417ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 418ea8dc4b6Seschrock { 419ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 420ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 421ea8dc4b6Seschrock int ret; 422ea8dc4b6Seschrock 423ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 424ea8dc4b6Seschrock sizeof (zbookmark_t)); 425ea8dc4b6Seschrock 426ea8dc4b6Seschrock if (ret < 0) 427ea8dc4b6Seschrock return (-1); 428ea8dc4b6Seschrock else if (ret > 0) 429ea8dc4b6Seschrock return (1); 430ea8dc4b6Seschrock else 431ea8dc4b6Seschrock return (0); 432ea8dc4b6Seschrock } 433ea8dc4b6Seschrock 434ea8dc4b6Seschrock /* 435ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 436ea8dc4b6Seschrock * re-initializes them in the process. 437ea8dc4b6Seschrock */ 438ea8dc4b6Seschrock void 439ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 440ea8dc4b6Seschrock { 441ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 442ea8dc4b6Seschrock 443ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 444ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 445ea8dc4b6Seschrock 446ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 447ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 448ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 449ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 450ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 451ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 452ea8dc4b6Seschrock } 453ea8dc4b6Seschrock 454fa9e4066Sahrens /* 455fa9e4066Sahrens * Activate an uninitialized pool. 456fa9e4066Sahrens */ 457fa9e4066Sahrens static void 458fa9e4066Sahrens spa_activate(spa_t *spa) 459fa9e4066Sahrens { 460fa9e4066Sahrens int t; 461fa9e4066Sahrens 462fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 463fa9e4066Sahrens 464fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 465fa9e4066Sahrens 466fa9e4066Sahrens spa->spa_normal_class = metaslab_class_create(); 4678654d025Sperrin spa->spa_log_class = metaslab_class_create(); 468fa9e4066Sahrens 469fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 470fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 471416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 472fa9e4066Sahrens TASKQ_PREPOPULATE); 473fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 474416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 475fa9e4066Sahrens TASKQ_PREPOPULATE); 476fa9e4066Sahrens } 477fa9e4066Sahrens 478fa9e4066Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 479fa9e4066Sahrens offsetof(vdev_t, vdev_dirty_node)); 480fa9e4066Sahrens 481fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 482fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 483ea8dc4b6Seschrock 484ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 485ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 486ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 487ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 488ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 489ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 490fa9e4066Sahrens } 491fa9e4066Sahrens 492fa9e4066Sahrens /* 493fa9e4066Sahrens * Opposite of spa_activate(). 494fa9e4066Sahrens */ 495fa9e4066Sahrens static void 496fa9e4066Sahrens spa_deactivate(spa_t *spa) 497fa9e4066Sahrens { 498fa9e4066Sahrens int t; 499fa9e4066Sahrens 500fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 501fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 502fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 503fa9e4066Sahrens 504fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 505fa9e4066Sahrens 506fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 507fa9e4066Sahrens 508fa9e4066Sahrens list_destroy(&spa->spa_dirty_list); 509fa9e4066Sahrens 510fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 511fa9e4066Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 512fa9e4066Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 513fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = NULL; 514fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = NULL; 515fa9e4066Sahrens } 516fa9e4066Sahrens 517fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 518fa9e4066Sahrens spa->spa_normal_class = NULL; 519fa9e4066Sahrens 5208654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 5218654d025Sperrin spa->spa_log_class = NULL; 5228654d025Sperrin 523ea8dc4b6Seschrock /* 524ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 525ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 526ea8dc4b6Seschrock */ 527ea8dc4b6Seschrock spa_errlog_drain(spa); 528ea8dc4b6Seschrock 529ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 530ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 531ea8dc4b6Seschrock 532fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 533fa9e4066Sahrens } 534fa9e4066Sahrens 535fa9e4066Sahrens /* 536fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 537fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 538fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 539fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 540fa9e4066Sahrens */ 54199653d4eSeschrock static int 54299653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 54399653d4eSeschrock uint_t id, int atype) 544fa9e4066Sahrens { 545fa9e4066Sahrens nvlist_t **child; 546fa9e4066Sahrens uint_t c, children; 54799653d4eSeschrock int error; 548fa9e4066Sahrens 54999653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 55099653d4eSeschrock return (error); 551fa9e4066Sahrens 55299653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 55399653d4eSeschrock return (0); 554fa9e4066Sahrens 555fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 556fa9e4066Sahrens &child, &children) != 0) { 55799653d4eSeschrock vdev_free(*vdp); 55899653d4eSeschrock *vdp = NULL; 55999653d4eSeschrock return (EINVAL); 560fa9e4066Sahrens } 561fa9e4066Sahrens 562fa9e4066Sahrens for (c = 0; c < children; c++) { 56399653d4eSeschrock vdev_t *vd; 56499653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 56599653d4eSeschrock atype)) != 0) { 56699653d4eSeschrock vdev_free(*vdp); 56799653d4eSeschrock *vdp = NULL; 56899653d4eSeschrock return (error); 569fa9e4066Sahrens } 570fa9e4066Sahrens } 571fa9e4066Sahrens 57299653d4eSeschrock ASSERT(*vdp != NULL); 57399653d4eSeschrock 57499653d4eSeschrock return (0); 575fa9e4066Sahrens } 576fa9e4066Sahrens 577fa9e4066Sahrens /* 578fa9e4066Sahrens * Opposite of spa_load(). 579fa9e4066Sahrens */ 580fa9e4066Sahrens static void 581fa9e4066Sahrens spa_unload(spa_t *spa) 582fa9e4066Sahrens { 58399653d4eSeschrock int i; 58499653d4eSeschrock 585ea8dc4b6Seschrock /* 586ea8dc4b6Seschrock * Stop async tasks. 587ea8dc4b6Seschrock */ 588ea8dc4b6Seschrock spa_async_suspend(spa); 589ea8dc4b6Seschrock 590fa9e4066Sahrens /* 591fa9e4066Sahrens * Stop syncing. 592fa9e4066Sahrens */ 593fa9e4066Sahrens if (spa->spa_sync_on) { 594fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 595fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 596fa9e4066Sahrens } 597fa9e4066Sahrens 598fa9e4066Sahrens /* 599fa9e4066Sahrens * Wait for any outstanding prefetch I/O to complete. 600fa9e4066Sahrens */ 601ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 602ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 603fa9e4066Sahrens 604fa9e4066Sahrens /* 605fa9e4066Sahrens * Close the dsl pool. 606fa9e4066Sahrens */ 607fa9e4066Sahrens if (spa->spa_dsl_pool) { 608fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 609fa9e4066Sahrens spa->spa_dsl_pool = NULL; 610fa9e4066Sahrens } 611fa9e4066Sahrens 612fa9e4066Sahrens /* 613fa9e4066Sahrens * Close all vdevs. 614fa9e4066Sahrens */ 6150e34b6a7Sbonwick if (spa->spa_root_vdev) 616fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 6170e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 618ea8dc4b6Seschrock 61999653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 62099653d4eSeschrock vdev_free(spa->spa_spares[i]); 62199653d4eSeschrock if (spa->spa_spares) { 62299653d4eSeschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 62399653d4eSeschrock spa->spa_spares = NULL; 62499653d4eSeschrock } 62599653d4eSeschrock if (spa->spa_sparelist) { 62699653d4eSeschrock nvlist_free(spa->spa_sparelist); 62799653d4eSeschrock spa->spa_sparelist = NULL; 62899653d4eSeschrock } 62999653d4eSeschrock 630ea8dc4b6Seschrock spa->spa_async_suspended = 0; 631fa9e4066Sahrens } 632fa9e4066Sahrens 63399653d4eSeschrock /* 63499653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 63599653d4eSeschrock * this pool. When this is called, we have some form of basic information in 63699653d4eSeschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 63799653d4eSeschrock * re-generate a more complete list including status information. 63899653d4eSeschrock */ 63999653d4eSeschrock static void 64099653d4eSeschrock spa_load_spares(spa_t *spa) 64199653d4eSeschrock { 64299653d4eSeschrock nvlist_t **spares; 64399653d4eSeschrock uint_t nspares; 64499653d4eSeschrock int i; 64539c23413Seschrock vdev_t *vd, *tvd; 64699653d4eSeschrock 64799653d4eSeschrock /* 64899653d4eSeschrock * First, close and free any existing spare vdevs. 64999653d4eSeschrock */ 65099653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) { 65139c23413Seschrock vd = spa->spa_spares[i]; 65239c23413Seschrock 65339c23413Seschrock /* Undo the call to spa_activate() below */ 65439c23413Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 65539c23413Seschrock tvd->vdev_isspare) 65639c23413Seschrock spa_spare_remove(tvd); 65739c23413Seschrock vdev_close(vd); 65839c23413Seschrock vdev_free(vd); 65999653d4eSeschrock } 66039c23413Seschrock 66199653d4eSeschrock if (spa->spa_spares) 66299653d4eSeschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 66399653d4eSeschrock 66499653d4eSeschrock if (spa->spa_sparelist == NULL) 66599653d4eSeschrock nspares = 0; 66699653d4eSeschrock else 66799653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 66899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 66999653d4eSeschrock 67099653d4eSeschrock spa->spa_nspares = (int)nspares; 67199653d4eSeschrock spa->spa_spares = NULL; 67299653d4eSeschrock 67399653d4eSeschrock if (nspares == 0) 67499653d4eSeschrock return; 67599653d4eSeschrock 67699653d4eSeschrock /* 67799653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 67839c23413Seschrock * process. For each spare, there is potentially two different vdev_t 67939c23413Seschrock * structures associated with it: one in the list of spares (used only 68039c23413Seschrock * for basic validation purposes) and one in the active vdev 68139c23413Seschrock * configuration (if it's spared in). During this phase we open and 68239c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 68339c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 68499653d4eSeschrock */ 68599653d4eSeschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 68699653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) { 68799653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 68899653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 68999653d4eSeschrock ASSERT(vd != NULL); 69099653d4eSeschrock 69199653d4eSeschrock spa->spa_spares[i] = vd; 69299653d4eSeschrock 69339c23413Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 69439c23413Seschrock if (!tvd->vdev_isspare) 69539c23413Seschrock spa_spare_add(tvd); 69639c23413Seschrock 69739c23413Seschrock /* 69839c23413Seschrock * We only mark the spare active if we were successfully 69939c23413Seschrock * able to load the vdev. Otherwise, importing a pool 70039c23413Seschrock * with a bad active spare would result in strange 70139c23413Seschrock * behavior, because multiple pool would think the spare 70239c23413Seschrock * is actively in use. 70339c23413Seschrock * 70439c23413Seschrock * There is a vulnerability here to an equally bizarre 70539c23413Seschrock * circumstance, where a dead active spare is later 70639c23413Seschrock * brought back to life (onlined or otherwise). Given 70739c23413Seschrock * the rarity of this scenario, and the extra complexity 70839c23413Seschrock * it adds, we ignore the possibility. 70939c23413Seschrock */ 71039c23413Seschrock if (!vdev_is_dead(tvd)) 71139c23413Seschrock spa_spare_activate(tvd); 71239c23413Seschrock } 71339c23413Seschrock 71499653d4eSeschrock if (vdev_open(vd) != 0) 71599653d4eSeschrock continue; 71699653d4eSeschrock 71799653d4eSeschrock vd->vdev_top = vd; 71899653d4eSeschrock (void) vdev_validate_spare(vd); 71999653d4eSeschrock } 72099653d4eSeschrock 72199653d4eSeschrock /* 72299653d4eSeschrock * Recompute the stashed list of spares, with status information 72399653d4eSeschrock * this time. 72499653d4eSeschrock */ 72599653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 72699653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 72799653d4eSeschrock 72899653d4eSeschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 72999653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 73099653d4eSeschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 73199653d4eSeschrock B_TRUE, B_TRUE); 73299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 73399653d4eSeschrock spares, spa->spa_nspares) == 0); 73499653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 73599653d4eSeschrock nvlist_free(spares[i]); 73699653d4eSeschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 73799653d4eSeschrock } 73899653d4eSeschrock 73999653d4eSeschrock static int 74099653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 74199653d4eSeschrock { 74299653d4eSeschrock dmu_buf_t *db; 74399653d4eSeschrock char *packed = NULL; 74499653d4eSeschrock size_t nvsize = 0; 74599653d4eSeschrock int error; 74699653d4eSeschrock *value = NULL; 74799653d4eSeschrock 74899653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 74999653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 75099653d4eSeschrock dmu_buf_rele(db, FTAG); 75199653d4eSeschrock 75299653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 75399653d4eSeschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 75499653d4eSeschrock if (error == 0) 75599653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 75699653d4eSeschrock kmem_free(packed, nvsize); 75799653d4eSeschrock 75899653d4eSeschrock return (error); 75999653d4eSeschrock } 76099653d4eSeschrock 7613d7072f8Seschrock /* 7623d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 7633d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 7643d7072f8Seschrock */ 7653d7072f8Seschrock static void 7663d7072f8Seschrock spa_check_removed(vdev_t *vd) 7673d7072f8Seschrock { 7683d7072f8Seschrock int c; 7693d7072f8Seschrock 7703d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) 7713d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 7723d7072f8Seschrock 7733d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 7743d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 7753d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 7763d7072f8Seschrock } 7773d7072f8Seschrock } 7783d7072f8Seschrock 779fa9e4066Sahrens /* 780fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 781ea8dc4b6Seschrock * source of configuration information. 782fa9e4066Sahrens */ 783fa9e4066Sahrens static int 784ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 785fa9e4066Sahrens { 786fa9e4066Sahrens int error = 0; 787fa9e4066Sahrens nvlist_t *nvroot = NULL; 788fa9e4066Sahrens vdev_t *rvd; 789fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 7900373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 791fa9e4066Sahrens uint64_t pool_guid; 79299653d4eSeschrock uint64_t version; 793fa9e4066Sahrens zio_t *zio; 7943d7072f8Seschrock uint64_t autoreplace = 0; 795fa9e4066Sahrens 796ea8dc4b6Seschrock spa->spa_load_state = state; 7970373e76bSbonwick 798fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 799a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 800ea8dc4b6Seschrock error = EINVAL; 801ea8dc4b6Seschrock goto out; 802ea8dc4b6Seschrock } 803fa9e4066Sahrens 80499653d4eSeschrock /* 80599653d4eSeschrock * Versioning wasn't explicitly added to the label until later, so if 80699653d4eSeschrock * it's not present treat it as the initial version. 80799653d4eSeschrock */ 80899653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 809e7437265Sahrens version = SPA_VERSION_INITIAL; 81099653d4eSeschrock 811a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 812a9926bf0Sbonwick &spa->spa_config_txg); 813a9926bf0Sbonwick 8140373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 815ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 816ea8dc4b6Seschrock error = EEXIST; 817ea8dc4b6Seschrock goto out; 818ea8dc4b6Seschrock } 819fa9e4066Sahrens 820b5989ec7Seschrock spa->spa_load_guid = pool_guid; 821b5989ec7Seschrock 822fa9e4066Sahrens /* 82399653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 82499653d4eSeschrock * value that will be returned by spa_version() since parsing the 82599653d4eSeschrock * configuration requires knowing the version number. 826fa9e4066Sahrens */ 827ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 82899653d4eSeschrock spa->spa_ubsync.ub_version = version; 82999653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 830ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 831fa9e4066Sahrens 83299653d4eSeschrock if (error != 0) 833ea8dc4b6Seschrock goto out; 834fa9e4066Sahrens 8350e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 836fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 837fa9e4066Sahrens 838fa9e4066Sahrens /* 839fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 840fa9e4066Sahrens */ 8410bf246f5Smc error = vdev_open(rvd); 8420bf246f5Smc if (error != 0) 843ea8dc4b6Seschrock goto out; 844fa9e4066Sahrens 845560e6e96Seschrock /* 846560e6e96Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 847560e6e96Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 848560e6e96Seschrock * flag. 849560e6e96Seschrock */ 850560e6e96Seschrock spa_config_enter(spa, RW_READER, FTAG); 851560e6e96Seschrock error = vdev_validate(rvd); 852560e6e96Seschrock spa_config_exit(spa, FTAG); 853560e6e96Seschrock 8540bf246f5Smc if (error != 0) 855560e6e96Seschrock goto out; 856560e6e96Seschrock 857560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 858560e6e96Seschrock error = ENXIO; 859560e6e96Seschrock goto out; 860560e6e96Seschrock } 861560e6e96Seschrock 862fa9e4066Sahrens /* 863fa9e4066Sahrens * Find the best uberblock. 864fa9e4066Sahrens */ 865fa9e4066Sahrens bzero(ub, sizeof (uberblock_t)); 866fa9e4066Sahrens 867fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, 868fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 869fa9e4066Sahrens vdev_uberblock_load(zio, rvd, ub); 870fa9e4066Sahrens error = zio_wait(zio); 871fa9e4066Sahrens 872fa9e4066Sahrens /* 873fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 874fa9e4066Sahrens */ 875fa9e4066Sahrens if (ub->ub_txg == 0) { 876eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 877eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 878ea8dc4b6Seschrock error = ENXIO; 879ea8dc4b6Seschrock goto out; 880ea8dc4b6Seschrock } 881ea8dc4b6Seschrock 882ea8dc4b6Seschrock /* 883ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 884ea8dc4b6Seschrock */ 885e7437265Sahrens if (ub->ub_version > SPA_VERSION) { 886eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 887eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 888ea8dc4b6Seschrock error = ENOTSUP; 889ea8dc4b6Seschrock goto out; 890fa9e4066Sahrens } 891fa9e4066Sahrens 892fa9e4066Sahrens /* 893fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 894fa9e4066Sahrens * incomplete configuration. 895fa9e4066Sahrens */ 896ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 897ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 898ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 899ea8dc4b6Seschrock error = ENXIO; 900ea8dc4b6Seschrock goto out; 901fa9e4066Sahrens } 902fa9e4066Sahrens 903fa9e4066Sahrens /* 904fa9e4066Sahrens * Initialize internal SPA structures. 905fa9e4066Sahrens */ 906fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 907fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 908fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 909ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 910ea8dc4b6Seschrock if (error) { 911ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 912ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 913ea8dc4b6Seschrock goto out; 914ea8dc4b6Seschrock } 915fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 916fa9e4066Sahrens 917ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 918fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 919ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 920ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 921ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 922ea8dc4b6Seschrock error = EIO; 923ea8dc4b6Seschrock goto out; 924ea8dc4b6Seschrock } 925fa9e4066Sahrens 926fa9e4066Sahrens if (!mosconfig) { 92799653d4eSeschrock nvlist_t *newconfig; 92895173954Sek uint64_t hostid; 929fa9e4066Sahrens 93099653d4eSeschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 931ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 932ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 933ea8dc4b6Seschrock error = EIO; 934ea8dc4b6Seschrock goto out; 935ea8dc4b6Seschrock } 936fa9e4066Sahrens 93795173954Sek if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 93895173954Sek &hostid) == 0) { 93995173954Sek char *hostname; 94095173954Sek unsigned long myhostid = 0; 94195173954Sek 94295173954Sek VERIFY(nvlist_lookup_string(newconfig, 94395173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 94495173954Sek 94595173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 94617194a52Slling if (hostid != 0 && myhostid != 0 && 94717194a52Slling (unsigned long)hostid != myhostid) { 94895173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 94995173954Sek "loaded as it was last accessed by " 95095173954Sek "another system (host: %s hostid: 0x%lx). " 95195173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 95295173954Sek spa->spa_name, hostname, 95395173954Sek (unsigned long)hostid); 95495173954Sek error = EBADF; 95595173954Sek goto out; 95695173954Sek } 95795173954Sek } 95895173954Sek 959fa9e4066Sahrens spa_config_set(spa, newconfig); 960fa9e4066Sahrens spa_unload(spa); 961fa9e4066Sahrens spa_deactivate(spa); 962fa9e4066Sahrens spa_activate(spa); 963fa9e4066Sahrens 964ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 965fa9e4066Sahrens } 966fa9e4066Sahrens 967ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 968fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 969ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 970ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 971ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 972ea8dc4b6Seschrock error = EIO; 973ea8dc4b6Seschrock goto out; 974ea8dc4b6Seschrock } 975fa9e4066Sahrens 97699653d4eSeschrock /* 97799653d4eSeschrock * Load the bit that tells us to use the new accounting function 97899653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 97999653d4eSeschrock * be present. 98099653d4eSeschrock */ 98199653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, 98299653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 98399653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate); 98499653d4eSeschrock if (error != 0 && error != ENOENT) { 98599653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 98699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 98799653d4eSeschrock error = EIO; 98899653d4eSeschrock goto out; 98999653d4eSeschrock } 99099653d4eSeschrock 991fa9e4066Sahrens /* 992ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 993ea8dc4b6Seschrock * not be present. 994fa9e4066Sahrens */ 995ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 996ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 997ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 998d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 999ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1000ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1001ea8dc4b6Seschrock error = EIO; 1002ea8dc4b6Seschrock goto out; 1003ea8dc4b6Seschrock } 1004ea8dc4b6Seschrock 1005ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1006ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1007ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1008ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 1009ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1010ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1011ea8dc4b6Seschrock error = EIO; 1012ea8dc4b6Seschrock goto out; 1013ea8dc4b6Seschrock } 1014ea8dc4b6Seschrock 101506eeb2adSek /* 101606eeb2adSek * Load the history object. If we have an older pool, this 101706eeb2adSek * will not be present. 101806eeb2adSek */ 101906eeb2adSek error = zap_lookup(spa->spa_meta_objset, 102006eeb2adSek DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 102106eeb2adSek sizeof (uint64_t), 1, &spa->spa_history); 102206eeb2adSek if (error != 0 && error != ENOENT) { 102306eeb2adSek vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 102406eeb2adSek VDEV_AUX_CORRUPT_DATA); 102506eeb2adSek error = EIO; 102606eeb2adSek goto out; 102706eeb2adSek } 102806eeb2adSek 102999653d4eSeschrock /* 103099653d4eSeschrock * Load any hot spares for this pool. 103199653d4eSeschrock */ 103299653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 103399653d4eSeschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 103499653d4eSeschrock if (error != 0 && error != ENOENT) { 103599653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 103699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 103799653d4eSeschrock error = EIO; 103899653d4eSeschrock goto out; 103999653d4eSeschrock } 104099653d4eSeschrock if (error == 0) { 1041e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 104299653d4eSeschrock if (load_nvlist(spa, spa->spa_spares_object, 104399653d4eSeschrock &spa->spa_sparelist) != 0) { 104499653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 104599653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 104699653d4eSeschrock error = EIO; 104799653d4eSeschrock goto out; 104899653d4eSeschrock } 104999653d4eSeschrock 105099653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 105199653d4eSeschrock spa_load_spares(spa); 105299653d4eSeschrock spa_config_exit(spa, FTAG); 105399653d4eSeschrock } 105499653d4eSeschrock 1055*990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1056ecd6cf80Smarks 1057b1b8ab34Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1058b1b8ab34Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1059b1b8ab34Slling 1060b1b8ab34Slling if (error && error != ENOENT) { 1061b1b8ab34Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1062b1b8ab34Slling VDEV_AUX_CORRUPT_DATA); 1063b1b8ab34Slling error = EIO; 1064b1b8ab34Slling goto out; 1065b1b8ab34Slling } 1066b1b8ab34Slling 1067b1b8ab34Slling if (error == 0) { 1068b1b8ab34Slling (void) zap_lookup(spa->spa_meta_objset, 1069b1b8ab34Slling spa->spa_pool_props_object, 10703d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1071b1b8ab34Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 10723d7072f8Seschrock (void) zap_lookup(spa->spa_meta_objset, 10733d7072f8Seschrock spa->spa_pool_props_object, 10743d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 10753d7072f8Seschrock sizeof (uint64_t), 1, &autoreplace); 1076ecd6cf80Smarks (void) zap_lookup(spa->spa_meta_objset, 1077ecd6cf80Smarks spa->spa_pool_props_object, 1078ecd6cf80Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1079ecd6cf80Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 1080b1b8ab34Slling } 1081b1b8ab34Slling 10823d7072f8Seschrock /* 10833d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 10843d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 10853d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 10863d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 10873d7072f8Seschrock * over. 10883d7072f8Seschrock */ 10893d7072f8Seschrock if (autoreplace) 10903d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 10913d7072f8Seschrock 1092ea8dc4b6Seschrock /* 1093560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1094ea8dc4b6Seschrock */ 1095560e6e96Seschrock vdev_load(rvd); 10960373e76bSbonwick 1097fa9e4066Sahrens /* 1098fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1099fa9e4066Sahrens */ 1100ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1101fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1102ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1103fa9e4066Sahrens 1104fa9e4066Sahrens /* 1105fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1106fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1107fa9e4066Sahrens */ 1108ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1109ea8dc4b6Seschrock error = ENXIO; 1110ea8dc4b6Seschrock goto out; 1111ea8dc4b6Seschrock } 1112fa9e4066Sahrens 1113ea8dc4b6Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 11145dabedeeSbonwick dmu_tx_t *tx; 11150373e76bSbonwick int need_update = B_FALSE; 11160373e76bSbonwick int c; 11175dabedeeSbonwick 11180373e76bSbonwick /* 11190373e76bSbonwick * Claim log blocks that haven't been committed yet. 11200373e76bSbonwick * This must all happen in a single txg. 11210373e76bSbonwick */ 11225dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1123fa9e4066Sahrens spa_first_txg(spa)); 11240b69c2f0Sahrens (void) dmu_objset_find(spa->spa_name, 11250b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1126fa9e4066Sahrens dmu_tx_commit(tx); 1127fa9e4066Sahrens 1128fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1129fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1130fa9e4066Sahrens 1131fa9e4066Sahrens /* 1132fa9e4066Sahrens * Wait for all claims to sync. 1133fa9e4066Sahrens */ 1134fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 11350e34b6a7Sbonwick 11360e34b6a7Sbonwick /* 11370373e76bSbonwick * If the config cache is stale, or we have uninitialized 11380373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 11390e34b6a7Sbonwick */ 11400373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 11410373e76bSbonwick state == SPA_LOAD_IMPORT) 11420373e76bSbonwick need_update = B_TRUE; 11430373e76bSbonwick 11440373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) 11450373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 11460373e76bSbonwick need_update = B_TRUE; 11470e34b6a7Sbonwick 11480e34b6a7Sbonwick /* 11490373e76bSbonwick * Update the config cache asychronously in case we're the 11500373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 11510e34b6a7Sbonwick */ 11520373e76bSbonwick if (need_update) 11530373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1154fa9e4066Sahrens } 1155fa9e4066Sahrens 1156ea8dc4b6Seschrock error = 0; 1157ea8dc4b6Seschrock out: 115899653d4eSeschrock if (error && error != EBADF) 1159ea8dc4b6Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 1160ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 1161ea8dc4b6Seschrock spa->spa_ena = 0; 1162ea8dc4b6Seschrock 1163ea8dc4b6Seschrock return (error); 1164fa9e4066Sahrens } 1165fa9e4066Sahrens 1166fa9e4066Sahrens /* 1167fa9e4066Sahrens * Pool Open/Import 1168fa9e4066Sahrens * 1169fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 1170fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1171fa9e4066Sahrens * case of an open, the pool configuration will exist in the 11723d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 1173fa9e4066Sahrens * 1174fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1175fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 1176fa9e4066Sahrens * ambiguous state. 1177fa9e4066Sahrens */ 1178fa9e4066Sahrens static int 1179fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1180fa9e4066Sahrens { 1181fa9e4066Sahrens spa_t *spa; 1182fa9e4066Sahrens int error; 1183fa9e4066Sahrens int loaded = B_FALSE; 1184fa9e4066Sahrens int locked = B_FALSE; 1185fa9e4066Sahrens 1186fa9e4066Sahrens *spapp = NULL; 1187fa9e4066Sahrens 1188fa9e4066Sahrens /* 1189fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 1190fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1191fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 1192fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 1193fa9e4066Sahrens */ 1194fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1195fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1196fa9e4066Sahrens locked = B_TRUE; 1197fa9e4066Sahrens } 1198fa9e4066Sahrens 1199fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1200fa9e4066Sahrens if (locked) 1201fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1202fa9e4066Sahrens return (ENOENT); 1203fa9e4066Sahrens } 1204fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1205fa9e4066Sahrens 1206fa9e4066Sahrens spa_activate(spa); 1207fa9e4066Sahrens 12080373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1209fa9e4066Sahrens 1210fa9e4066Sahrens if (error == EBADF) { 1211fa9e4066Sahrens /* 1212560e6e96Seschrock * If vdev_validate() returns failure (indicated by 1213560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 1214560e6e96Seschrock * that the pool has been exported or destroyed. If 1215560e6e96Seschrock * this is the case, the config cache is out of sync and 1216560e6e96Seschrock * we should remove the pool from the namespace. 1217fa9e4066Sahrens */ 121899653d4eSeschrock zfs_post_ok(spa, NULL); 1219fa9e4066Sahrens spa_unload(spa); 1220fa9e4066Sahrens spa_deactivate(spa); 1221fa9e4066Sahrens spa_remove(spa); 1222fa9e4066Sahrens spa_config_sync(); 1223fa9e4066Sahrens if (locked) 1224fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1225fa9e4066Sahrens return (ENOENT); 1226ea8dc4b6Seschrock } 1227ea8dc4b6Seschrock 1228ea8dc4b6Seschrock if (error) { 1229fa9e4066Sahrens /* 1230fa9e4066Sahrens * We can't open the pool, but we still have useful 1231fa9e4066Sahrens * information: the state of each vdev after the 1232fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 1233fa9e4066Sahrens */ 12340373e76bSbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 12350373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 1236fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1237fa9e4066Sahrens B_TRUE); 12380373e76bSbonwick spa_config_exit(spa, FTAG); 12390373e76bSbonwick } 1240fa9e4066Sahrens spa_unload(spa); 1241fa9e4066Sahrens spa_deactivate(spa); 1242ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 1243fa9e4066Sahrens if (locked) 1244fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1245fa9e4066Sahrens *spapp = NULL; 1246fa9e4066Sahrens return (error); 1247ea8dc4b6Seschrock } else { 1248ea8dc4b6Seschrock zfs_post_ok(spa, NULL); 1249ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 1250fa9e4066Sahrens } 1251fa9e4066Sahrens 1252fa9e4066Sahrens loaded = B_TRUE; 1253fa9e4066Sahrens } 1254fa9e4066Sahrens 1255fa9e4066Sahrens spa_open_ref(spa, tag); 12563d7072f8Seschrock 12573d7072f8Seschrock /* 12583d7072f8Seschrock * If we just loaded the pool, resilver anything that's out of date. 12593d7072f8Seschrock */ 12603d7072f8Seschrock if (loaded && (spa_mode & FWRITE)) 12613d7072f8Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 12623d7072f8Seschrock 1263fa9e4066Sahrens if (locked) 1264fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1265fa9e4066Sahrens 1266fa9e4066Sahrens *spapp = spa; 1267fa9e4066Sahrens 1268fa9e4066Sahrens if (config != NULL) { 1269ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1270fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1271ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1272fa9e4066Sahrens } 1273fa9e4066Sahrens 1274fa9e4066Sahrens return (0); 1275fa9e4066Sahrens } 1276fa9e4066Sahrens 1277fa9e4066Sahrens int 1278fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1279fa9e4066Sahrens { 1280fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1281fa9e4066Sahrens } 1282fa9e4066Sahrens 1283ea8dc4b6Seschrock /* 1284ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 1285ea8dc4b6Seschrock * preventing it from being exported or destroyed. 1286ea8dc4b6Seschrock */ 1287ea8dc4b6Seschrock spa_t * 1288ea8dc4b6Seschrock spa_inject_addref(char *name) 1289ea8dc4b6Seschrock { 1290ea8dc4b6Seschrock spa_t *spa; 1291ea8dc4b6Seschrock 1292ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1293ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 1294ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1295ea8dc4b6Seschrock return (NULL); 1296ea8dc4b6Seschrock } 1297ea8dc4b6Seschrock spa->spa_inject_ref++; 1298ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1299ea8dc4b6Seschrock 1300ea8dc4b6Seschrock return (spa); 1301ea8dc4b6Seschrock } 1302ea8dc4b6Seschrock 1303ea8dc4b6Seschrock void 1304ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 1305ea8dc4b6Seschrock { 1306ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1307ea8dc4b6Seschrock spa->spa_inject_ref--; 1308ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1309ea8dc4b6Seschrock } 1310ea8dc4b6Seschrock 131199653d4eSeschrock static void 131299653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 131399653d4eSeschrock { 131499653d4eSeschrock nvlist_t **spares; 131599653d4eSeschrock uint_t i, nspares; 131699653d4eSeschrock nvlist_t *nvroot; 131799653d4eSeschrock uint64_t guid; 131899653d4eSeschrock vdev_stat_t *vs; 131999653d4eSeschrock uint_t vsc; 132039c23413Seschrock uint64_t pool; 132199653d4eSeschrock 132299653d4eSeschrock if (spa->spa_nspares == 0) 132399653d4eSeschrock return; 132499653d4eSeschrock 132599653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 132699653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 132799653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 132899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 132999653d4eSeschrock if (nspares != 0) { 133099653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 133199653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 133299653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 133399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 133499653d4eSeschrock 133599653d4eSeschrock /* 133699653d4eSeschrock * Go through and find any spares which have since been 133799653d4eSeschrock * repurposed as an active spare. If this is the case, update 133899653d4eSeschrock * their status appropriately. 133999653d4eSeschrock */ 134099653d4eSeschrock for (i = 0; i < nspares; i++) { 134199653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 134299653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 134339c23413Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 134499653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 134599653d4eSeschrock spares[i], ZPOOL_CONFIG_STATS, 134699653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 134799653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 134899653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 134999653d4eSeschrock } 135099653d4eSeschrock } 135199653d4eSeschrock } 135299653d4eSeschrock } 135399653d4eSeschrock 1354fa9e4066Sahrens int 1355ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1356fa9e4066Sahrens { 1357fa9e4066Sahrens int error; 1358fa9e4066Sahrens spa_t *spa; 1359fa9e4066Sahrens 1360fa9e4066Sahrens *config = NULL; 1361fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 1362fa9e4066Sahrens 136399653d4eSeschrock if (spa && *config != NULL) { 1364ea8dc4b6Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 1365ea8dc4b6Seschrock spa_get_errlog_size(spa)) == 0); 1366ea8dc4b6Seschrock 136799653d4eSeschrock spa_add_spares(spa, *config); 136899653d4eSeschrock } 136999653d4eSeschrock 1370ea8dc4b6Seschrock /* 1371ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 1372ea8dc4b6Seschrock * and call spa_lookup() directly. 1373ea8dc4b6Seschrock */ 1374ea8dc4b6Seschrock if (altroot) { 1375ea8dc4b6Seschrock if (spa == NULL) { 1376ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1377ea8dc4b6Seschrock spa = spa_lookup(name); 1378ea8dc4b6Seschrock if (spa) 1379ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1380ea8dc4b6Seschrock else 1381ea8dc4b6Seschrock altroot[0] = '\0'; 1382ea8dc4b6Seschrock spa = NULL; 1383ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1384ea8dc4b6Seschrock } else { 1385ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1386ea8dc4b6Seschrock } 1387ea8dc4b6Seschrock } 1388ea8dc4b6Seschrock 1389fa9e4066Sahrens if (spa != NULL) 1390fa9e4066Sahrens spa_close(spa, FTAG); 1391fa9e4066Sahrens 1392fa9e4066Sahrens return (error); 1393fa9e4066Sahrens } 1394fa9e4066Sahrens 139599653d4eSeschrock /* 139699653d4eSeschrock * Validate that the 'spares' array is well formed. We must have an array of 139739c23413Seschrock * nvlists, each which describes a valid leaf vdev. If this is an import (mode 139839c23413Seschrock * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 139939c23413Seschrock * as they are well-formed. 140099653d4eSeschrock */ 140199653d4eSeschrock static int 140299653d4eSeschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 140399653d4eSeschrock { 140499653d4eSeschrock nvlist_t **spares; 140599653d4eSeschrock uint_t i, nspares; 140699653d4eSeschrock vdev_t *vd; 140799653d4eSeschrock int error; 140899653d4eSeschrock 140999653d4eSeschrock /* 141099653d4eSeschrock * It's acceptable to have no spares specified. 141199653d4eSeschrock */ 141299653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 141399653d4eSeschrock &spares, &nspares) != 0) 141499653d4eSeschrock return (0); 141599653d4eSeschrock 141699653d4eSeschrock if (nspares == 0) 141799653d4eSeschrock return (EINVAL); 141899653d4eSeschrock 141999653d4eSeschrock /* 142099653d4eSeschrock * Make sure the pool is formatted with a version that supports hot 142199653d4eSeschrock * spares. 142299653d4eSeschrock */ 1423e7437265Sahrens if (spa_version(spa) < SPA_VERSION_SPARES) 142499653d4eSeschrock return (ENOTSUP); 142599653d4eSeschrock 142639c23413Seschrock /* 142739c23413Seschrock * Set the pending spare list so we correctly handle device in-use 142839c23413Seschrock * checking. 142939c23413Seschrock */ 143039c23413Seschrock spa->spa_pending_spares = spares; 143139c23413Seschrock spa->spa_pending_nspares = nspares; 143239c23413Seschrock 143399653d4eSeschrock for (i = 0; i < nspares; i++) { 143499653d4eSeschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 143599653d4eSeschrock mode)) != 0) 143639c23413Seschrock goto out; 143799653d4eSeschrock 143899653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 143999653d4eSeschrock vdev_free(vd); 144039c23413Seschrock error = EINVAL; 144139c23413Seschrock goto out; 144299653d4eSeschrock } 144399653d4eSeschrock 144499653d4eSeschrock vd->vdev_top = vd; 144599653d4eSeschrock 144639c23413Seschrock if ((error = vdev_open(vd)) == 0 && 144739c23413Seschrock (error = vdev_label_init(vd, crtxg, 144839c23413Seschrock VDEV_LABEL_SPARE)) == 0) { 144939c23413Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 145039c23413Seschrock vd->vdev_guid) == 0); 145139c23413Seschrock } 145299653d4eSeschrock 145399653d4eSeschrock vdev_free(vd); 145439c23413Seschrock 145539c23413Seschrock if (error && mode != VDEV_ALLOC_SPARE) 145639c23413Seschrock goto out; 145739c23413Seschrock else 145839c23413Seschrock error = 0; 145999653d4eSeschrock } 146099653d4eSeschrock 146139c23413Seschrock out: 146239c23413Seschrock spa->spa_pending_spares = NULL; 146339c23413Seschrock spa->spa_pending_nspares = 0; 146439c23413Seschrock return (error); 146599653d4eSeschrock } 146699653d4eSeschrock 1467fa9e4066Sahrens /* 1468fa9e4066Sahrens * Pool Creation 1469fa9e4066Sahrens */ 1470fa9e4066Sahrens int 1471*990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 1472228975ccSek const char *history_str) 1473fa9e4066Sahrens { 1474fa9e4066Sahrens spa_t *spa; 1475*990b4856Slling char *altroot = NULL; 14760373e76bSbonwick vdev_t *rvd; 1477fa9e4066Sahrens dsl_pool_t *dp; 1478fa9e4066Sahrens dmu_tx_t *tx; 147999653d4eSeschrock int c, error = 0; 1480fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 148199653d4eSeschrock nvlist_t **spares; 148299653d4eSeschrock uint_t nspares; 1483*990b4856Slling uint64_t version; 1484fa9e4066Sahrens 1485fa9e4066Sahrens /* 1486fa9e4066Sahrens * If this pool already exists, return failure. 1487fa9e4066Sahrens */ 1488fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1489fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 1490fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1491fa9e4066Sahrens return (EEXIST); 1492fa9e4066Sahrens } 1493fa9e4066Sahrens 1494fa9e4066Sahrens /* 1495fa9e4066Sahrens * Allocate a new spa_t structure. 1496fa9e4066Sahrens */ 1497*990b4856Slling (void) nvlist_lookup_string(props, 1498*990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 14990373e76bSbonwick spa = spa_add(pool, altroot); 1500fa9e4066Sahrens spa_activate(spa); 1501fa9e4066Sahrens 1502fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 1503*990b4856Slling 1504*990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 1505*990b4856Slling spa_unload(spa); 1506*990b4856Slling spa_deactivate(spa); 1507*990b4856Slling spa_remove(spa); 1508*990b4856Slling return (error); 1509*990b4856Slling } 1510*990b4856Slling 1511*990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 1512*990b4856Slling &version) != 0) 1513*990b4856Slling version = SPA_VERSION; 1514*990b4856Slling ASSERT(version <= SPA_VERSION); 1515*990b4856Slling spa->spa_uberblock.ub_version = version; 1516fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1517fa9e4066Sahrens 15180373e76bSbonwick /* 15190373e76bSbonwick * Create the root vdev. 15200373e76bSbonwick */ 15210373e76bSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15220373e76bSbonwick 152399653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 15240373e76bSbonwick 152599653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 152699653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 15270373e76bSbonwick 152899653d4eSeschrock if (error == 0 && rvd->vdev_children == 0) 15290373e76bSbonwick error = EINVAL; 153099653d4eSeschrock 153199653d4eSeschrock if (error == 0 && 153299653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 153399653d4eSeschrock (error = spa_validate_spares(spa, nvroot, txg, 153499653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 153599653d4eSeschrock for (c = 0; c < rvd->vdev_children; c++) 153699653d4eSeschrock vdev_init(rvd->vdev_child[c], txg); 153799653d4eSeschrock vdev_config_dirty(rvd); 15380373e76bSbonwick } 15390373e76bSbonwick 15400373e76bSbonwick spa_config_exit(spa, FTAG); 1541fa9e4066Sahrens 154299653d4eSeschrock if (error != 0) { 1543fa9e4066Sahrens spa_unload(spa); 1544fa9e4066Sahrens spa_deactivate(spa); 1545fa9e4066Sahrens spa_remove(spa); 1546fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1547fa9e4066Sahrens return (error); 1548fa9e4066Sahrens } 1549fa9e4066Sahrens 155099653d4eSeschrock /* 155199653d4eSeschrock * Get the list of spares, if specified. 155299653d4eSeschrock */ 155399653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 155499653d4eSeschrock &spares, &nspares) == 0) { 155599653d4eSeschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 155699653d4eSeschrock KM_SLEEP) == 0); 155799653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 155899653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 155999653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 156099653d4eSeschrock spa_load_spares(spa); 156199653d4eSeschrock spa_config_exit(spa, FTAG); 156299653d4eSeschrock spa->spa_sync_spares = B_TRUE; 156399653d4eSeschrock } 156499653d4eSeschrock 1565fa9e4066Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1566fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1567fa9e4066Sahrens 1568fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 1569fa9e4066Sahrens 1570fa9e4066Sahrens /* 1571fa9e4066Sahrens * Create the pool config object. 1572fa9e4066Sahrens */ 1573fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1574fa9e4066Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1575fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1576fa9e4066Sahrens 1577ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1578fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1579ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 1580ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 1581ea8dc4b6Seschrock } 1582fa9e4066Sahrens 1583*990b4856Slling /* Newly created pools with the right version are always deflated. */ 1584*990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 1585*990b4856Slling spa->spa_deflate = TRUE; 1586*990b4856Slling if (zap_add(spa->spa_meta_objset, 1587*990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 1588*990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 1589*990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 1590*990b4856Slling } 159199653d4eSeschrock } 159299653d4eSeschrock 1593fa9e4066Sahrens /* 1594fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 1595fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 1596fa9e4066Sahrens * keeps changing. 1597fa9e4066Sahrens */ 1598fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1599fa9e4066Sahrens 1 << 14, tx); 1600fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1601fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 1602fa9e4066Sahrens 1603ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1604fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1605ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 1606ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 1607ea8dc4b6Seschrock } 1608fa9e4066Sahrens 160906eeb2adSek /* 161006eeb2adSek * Create the pool's history object. 161106eeb2adSek */ 1612*990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 1613*990b4856Slling spa_history_create_obj(spa, tx); 1614*990b4856Slling 1615*990b4856Slling /* 1616*990b4856Slling * Set pool properties. 1617*990b4856Slling */ 1618*990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 1619*990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1620*990b4856Slling spa->spa_temporary = zpool_prop_default_numeric(ZPOOL_PROP_TEMPORARY); 1621*990b4856Slling if (props) 1622*990b4856Slling spa_sync_props(spa, props, CRED(), tx); 162306eeb2adSek 1624fa9e4066Sahrens dmu_tx_commit(tx); 1625fa9e4066Sahrens 1626fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1627fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1628fa9e4066Sahrens 1629fa9e4066Sahrens /* 1630fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 1631fa9e4066Sahrens * bean counters are appropriately updated. 1632fa9e4066Sahrens */ 1633fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1634fa9e4066Sahrens 1635fa9e4066Sahrens spa_config_sync(); 1636fa9e4066Sahrens 1637*990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 1638228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 1639228975ccSek 1640fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1641fa9e4066Sahrens 1642fa9e4066Sahrens return (0); 1643fa9e4066Sahrens } 1644fa9e4066Sahrens 1645fa9e4066Sahrens /* 1646fa9e4066Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1647fa9e4066Sahrens * then call spa_load() to do the dirty work. 1648fa9e4066Sahrens */ 1649fa9e4066Sahrens int 1650*990b4856Slling spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 1651fa9e4066Sahrens { 1652fa9e4066Sahrens spa_t *spa; 1653*990b4856Slling char *altroot = NULL; 1654fa9e4066Sahrens int error; 165599653d4eSeschrock nvlist_t *nvroot; 165699653d4eSeschrock nvlist_t **spares; 165799653d4eSeschrock uint_t nspares; 1658fa9e4066Sahrens 1659fa9e4066Sahrens /* 1660fa9e4066Sahrens * If a pool with this name exists, return failure. 1661fa9e4066Sahrens */ 1662fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1663fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 1664fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1665fa9e4066Sahrens return (EEXIST); 1666fa9e4066Sahrens } 1667fa9e4066Sahrens 1668fa9e4066Sahrens /* 16690373e76bSbonwick * Create and initialize the spa structure. 1670fa9e4066Sahrens */ 1671*990b4856Slling (void) nvlist_lookup_string(props, 1672*990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 16730373e76bSbonwick spa = spa_add(pool, altroot); 1674fa9e4066Sahrens spa_activate(spa); 1675fa9e4066Sahrens 16765dabedeeSbonwick /* 16770373e76bSbonwick * Pass off the heavy lifting to spa_load(). 1678ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 1679ecc2d604Sbonwick * is actually the one to trust when doing an import. 16805dabedeeSbonwick */ 1681ecc2d604Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1682fa9e4066Sahrens 168399653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 168499653d4eSeschrock /* 168599653d4eSeschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 168699653d4eSeschrock * and conflicts with spa_has_spare(). 168799653d4eSeschrock */ 168899653d4eSeschrock if (spa->spa_sparelist) { 168999653d4eSeschrock nvlist_free(spa->spa_sparelist); 169099653d4eSeschrock spa->spa_sparelist = NULL; 169199653d4eSeschrock spa_load_spares(spa); 169299653d4eSeschrock } 169399653d4eSeschrock 169499653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 169599653d4eSeschrock &nvroot) == 0); 1696*990b4856Slling if (error == 0) { 169799653d4eSeschrock error = spa_validate_spares(spa, nvroot, -1ULL, 169899653d4eSeschrock VDEV_ALLOC_SPARE); 1699*990b4856Slling } 170099653d4eSeschrock spa_config_exit(spa, FTAG); 170199653d4eSeschrock 1702*990b4856Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 1703fa9e4066Sahrens spa_unload(spa); 1704fa9e4066Sahrens spa_deactivate(spa); 1705fa9e4066Sahrens spa_remove(spa); 1706fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1707fa9e4066Sahrens return (error); 1708fa9e4066Sahrens } 1709fa9e4066Sahrens 171099653d4eSeschrock /* 171199653d4eSeschrock * Override any spares as specified by the user, as these may have 171299653d4eSeschrock * correct device names/devids, etc. 171399653d4eSeschrock */ 171499653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 171599653d4eSeschrock &spares, &nspares) == 0) { 171699653d4eSeschrock if (spa->spa_sparelist) 171799653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, 171899653d4eSeschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 171999653d4eSeschrock else 172099653d4eSeschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 172199653d4eSeschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 172299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 172399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 172499653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 172599653d4eSeschrock spa_load_spares(spa); 172699653d4eSeschrock spa_config_exit(spa, FTAG); 172799653d4eSeschrock spa->spa_sync_spares = B_TRUE; 172899653d4eSeschrock } 172999653d4eSeschrock 17300373e76bSbonwick /* 17310373e76bSbonwick * Update the config cache to include the newly-imported pool. 17320373e76bSbonwick */ 1733de6628f0Sck if (spa_mode & FWRITE) 1734de6628f0Sck spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 17350373e76bSbonwick 1736fa9e4066Sahrens /* 1737fa9e4066Sahrens * Resilver anything that's out of date. 1738fa9e4066Sahrens */ 1739fa9e4066Sahrens if (spa_mode & FWRITE) 1740fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1741fa9e4066Sahrens 17423d7072f8Seschrock mutex_exit(&spa_namespace_lock); 17433d7072f8Seschrock 1744fa9e4066Sahrens return (0); 1745fa9e4066Sahrens } 1746fa9e4066Sahrens 1747fa9e4066Sahrens /* 1748fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1749fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 1750fa9e4066Sahrens */ 1751fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 1752fa9e4066Sahrens 1753fa9e4066Sahrens nvlist_t * 1754fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 1755fa9e4066Sahrens { 1756fa9e4066Sahrens nvlist_t *config = NULL; 1757fa9e4066Sahrens char *poolname; 1758fa9e4066Sahrens spa_t *spa; 1759fa9e4066Sahrens uint64_t state; 1760fa9e4066Sahrens 1761fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1762fa9e4066Sahrens return (NULL); 1763fa9e4066Sahrens 1764fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1765fa9e4066Sahrens return (NULL); 1766fa9e4066Sahrens 1767fa9e4066Sahrens /* 17680373e76bSbonwick * Create and initialize the spa structure. 1769fa9e4066Sahrens */ 17700373e76bSbonwick mutex_enter(&spa_namespace_lock); 17710373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1772fa9e4066Sahrens spa_activate(spa); 1773fa9e4066Sahrens 1774fa9e4066Sahrens /* 17750373e76bSbonwick * Pass off the heavy lifting to spa_load(). 1776ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 1777ecc2d604Sbonwick * is actually the one to trust when doing an import. 1778fa9e4066Sahrens */ 1779ecc2d604Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1780fa9e4066Sahrens 1781fa9e4066Sahrens /* 1782fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 1783fa9e4066Sahrens */ 1784fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 17850373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 1786fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 17870373e76bSbonwick spa_config_exit(spa, FTAG); 1788fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1789fa9e4066Sahrens poolname) == 0); 1790fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1791fa9e4066Sahrens state) == 0); 179295173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 179395173954Sek spa->spa_uberblock.ub_timestamp) == 0); 179499653d4eSeschrock 179599653d4eSeschrock /* 179699653d4eSeschrock * Add the list of hot spares. 179799653d4eSeschrock */ 179899653d4eSeschrock spa_add_spares(spa, config); 1799fa9e4066Sahrens } 1800fa9e4066Sahrens 1801fa9e4066Sahrens spa_unload(spa); 1802fa9e4066Sahrens spa_deactivate(spa); 1803fa9e4066Sahrens spa_remove(spa); 1804fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1805fa9e4066Sahrens 1806fa9e4066Sahrens return (config); 1807fa9e4066Sahrens } 1808fa9e4066Sahrens 1809fa9e4066Sahrens /* 1810fa9e4066Sahrens * Pool export/destroy 1811fa9e4066Sahrens * 1812fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1813fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1814fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 1815fa9e4066Sahrens * configuration from the cache afterwards. 1816fa9e4066Sahrens */ 1817fa9e4066Sahrens static int 181844cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1819fa9e4066Sahrens { 1820fa9e4066Sahrens spa_t *spa; 1821fa9e4066Sahrens 182244cd46caSbillm if (oldconfig) 182344cd46caSbillm *oldconfig = NULL; 182444cd46caSbillm 1825fa9e4066Sahrens if (!(spa_mode & FWRITE)) 1826fa9e4066Sahrens return (EROFS); 1827fa9e4066Sahrens 1828fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1829fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1830fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1831fa9e4066Sahrens return (ENOENT); 1832fa9e4066Sahrens } 1833fa9e4066Sahrens 1834ea8dc4b6Seschrock /* 1835ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 1836ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 1837ea8dc4b6Seschrock */ 1838ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 1839ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1840ea8dc4b6Seschrock spa_async_suspend(spa); 1841ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1842ea8dc4b6Seschrock spa_close(spa, FTAG); 1843ea8dc4b6Seschrock 1844fa9e4066Sahrens /* 1845fa9e4066Sahrens * The pool will be in core if it's openable, 1846fa9e4066Sahrens * in which case we can modify its state. 1847fa9e4066Sahrens */ 1848fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1849fa9e4066Sahrens /* 1850fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 1851fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 1852fa9e4066Sahrens */ 1853fa9e4066Sahrens spa_scrub_suspend(spa); 1854fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1855fa9e4066Sahrens 1856ea8dc4b6Seschrock /* 1857ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 1858ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 1859ea8dc4b6Seschrock * fault injection handlers. 1860ea8dc4b6Seschrock */ 1861ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 1862ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 1863ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1864fa9e4066Sahrens spa_scrub_resume(spa); 1865ea8dc4b6Seschrock spa_async_resume(spa); 1866fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1867fa9e4066Sahrens return (EBUSY); 1868fa9e4066Sahrens } 1869fa9e4066Sahrens 1870fa9e4066Sahrens spa_scrub_resume(spa); 1871fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1872fa9e4066Sahrens 1873fa9e4066Sahrens /* 1874fa9e4066Sahrens * We want this to be reflected on every label, 1875fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 1876fa9e4066Sahrens * final sync that pushes these changes out. 1877fa9e4066Sahrens */ 1878ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 18795dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 1880ea8dc4b6Seschrock spa->spa_state = new_state; 18810373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 1882ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 18835dabedeeSbonwick spa_config_exit(spa, FTAG); 1884ea8dc4b6Seschrock } 1885fa9e4066Sahrens } 1886fa9e4066Sahrens 18873d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 18883d7072f8Seschrock 1889fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1890fa9e4066Sahrens spa_unload(spa); 1891fa9e4066Sahrens spa_deactivate(spa); 1892fa9e4066Sahrens } 1893fa9e4066Sahrens 189444cd46caSbillm if (oldconfig && spa->spa_config) 189544cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 189644cd46caSbillm 1897ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 1898ea8dc4b6Seschrock spa_remove(spa); 1899ea8dc4b6Seschrock spa_config_sync(); 1900ea8dc4b6Seschrock } 1901fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1902fa9e4066Sahrens 1903fa9e4066Sahrens return (0); 1904fa9e4066Sahrens } 1905fa9e4066Sahrens 1906fa9e4066Sahrens /* 1907fa9e4066Sahrens * Destroy a storage pool. 1908fa9e4066Sahrens */ 1909fa9e4066Sahrens int 1910fa9e4066Sahrens spa_destroy(char *pool) 1911fa9e4066Sahrens { 191244cd46caSbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1913fa9e4066Sahrens } 1914fa9e4066Sahrens 1915fa9e4066Sahrens /* 1916fa9e4066Sahrens * Export a storage pool. 1917fa9e4066Sahrens */ 1918fa9e4066Sahrens int 191944cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig) 1920fa9e4066Sahrens { 192144cd46caSbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1922fa9e4066Sahrens } 1923fa9e4066Sahrens 1924ea8dc4b6Seschrock /* 1925ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 1926ea8dc4b6Seschrock * from the namespace in any way. 1927ea8dc4b6Seschrock */ 1928ea8dc4b6Seschrock int 1929ea8dc4b6Seschrock spa_reset(char *pool) 1930ea8dc4b6Seschrock { 193144cd46caSbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 1932ea8dc4b6Seschrock } 1933ea8dc4b6Seschrock 1934ea8dc4b6Seschrock 1935fa9e4066Sahrens /* 1936fa9e4066Sahrens * ========================================================================== 1937fa9e4066Sahrens * Device manipulation 1938fa9e4066Sahrens * ========================================================================== 1939fa9e4066Sahrens */ 1940fa9e4066Sahrens 1941fa9e4066Sahrens /* 19428654d025Sperrin * Add a device to a storage pool. 1943fa9e4066Sahrens */ 1944fa9e4066Sahrens int 1945fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1946fa9e4066Sahrens { 1947fa9e4066Sahrens uint64_t txg; 19480373e76bSbonwick int c, error; 1949fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 19500e34b6a7Sbonwick vdev_t *vd, *tvd; 195199653d4eSeschrock nvlist_t **spares; 195299653d4eSeschrock uint_t i, nspares; 1953fa9e4066Sahrens 1954fa9e4066Sahrens txg = spa_vdev_enter(spa); 1955fa9e4066Sahrens 195699653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 195799653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 195899653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 1959fa9e4066Sahrens 196039c23413Seschrock spa->spa_pending_vdev = vd; 196199653d4eSeschrock 196299653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 196399653d4eSeschrock &spares, &nspares) != 0) 196499653d4eSeschrock nspares = 0; 196599653d4eSeschrock 196639c23413Seschrock if (vd->vdev_children == 0 && nspares == 0) { 196739c23413Seschrock spa->spa_pending_vdev = NULL; 1968fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 196939c23413Seschrock } 1970fa9e4066Sahrens 197199653d4eSeschrock if (vd->vdev_children != 0) { 197239c23413Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 197339c23413Seschrock spa->spa_pending_vdev = NULL; 197499653d4eSeschrock return (spa_vdev_exit(spa, vd, txg, error)); 197599653d4eSeschrock } 197699653d4eSeschrock } 197799653d4eSeschrock 197839c23413Seschrock /* 197939c23413Seschrock * We must validate the spares after checking the children. Otherwise, 198039c23413Seschrock * vdev_inuse() will blindly overwrite the spare. 198139c23413Seschrock */ 198239c23413Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 198339c23413Seschrock VDEV_ALLOC_ADD)) != 0) { 198439c23413Seschrock spa->spa_pending_vdev = NULL; 198539c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 198639c23413Seschrock } 198739c23413Seschrock 198839c23413Seschrock spa->spa_pending_vdev = NULL; 198939c23413Seschrock 199039c23413Seschrock /* 199139c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 199239c23413Seschrock */ 199339c23413Seschrock for (c = 0; c < vd->vdev_children; c++) { 199439c23413Seschrock tvd = vd->vdev_child[c]; 199539c23413Seschrock vdev_remove_child(vd, tvd); 199639c23413Seschrock tvd->vdev_id = rvd->vdev_children; 199739c23413Seschrock vdev_add_child(rvd, tvd); 199839c23413Seschrock vdev_config_dirty(tvd); 199939c23413Seschrock } 200039c23413Seschrock 200199653d4eSeschrock if (nspares != 0) { 200299653d4eSeschrock if (spa->spa_sparelist != NULL) { 200399653d4eSeschrock nvlist_t **oldspares; 200499653d4eSeschrock uint_t oldnspares; 200599653d4eSeschrock nvlist_t **newspares; 200699653d4eSeschrock 200799653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 200899653d4eSeschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 200999653d4eSeschrock 201099653d4eSeschrock newspares = kmem_alloc(sizeof (void *) * 201199653d4eSeschrock (nspares + oldnspares), KM_SLEEP); 201299653d4eSeschrock for (i = 0; i < oldnspares; i++) 201399653d4eSeschrock VERIFY(nvlist_dup(oldspares[i], 201499653d4eSeschrock &newspares[i], KM_SLEEP) == 0); 201599653d4eSeschrock for (i = 0; i < nspares; i++) 201699653d4eSeschrock VERIFY(nvlist_dup(spares[i], 201799653d4eSeschrock &newspares[i + oldnspares], 201899653d4eSeschrock KM_SLEEP) == 0); 201999653d4eSeschrock 202099653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, 202199653d4eSeschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 202299653d4eSeschrock 202399653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 202499653d4eSeschrock ZPOOL_CONFIG_SPARES, newspares, 202599653d4eSeschrock nspares + oldnspares) == 0); 202699653d4eSeschrock for (i = 0; i < oldnspares + nspares; i++) 202799653d4eSeschrock nvlist_free(newspares[i]); 202899653d4eSeschrock kmem_free(newspares, (oldnspares + nspares) * 202999653d4eSeschrock sizeof (void *)); 203099653d4eSeschrock } else { 203199653d4eSeschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 203299653d4eSeschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 203399653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 203499653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 203599653d4eSeschrock } 203699653d4eSeschrock 203799653d4eSeschrock spa_load_spares(spa); 203899653d4eSeschrock spa->spa_sync_spares = B_TRUE; 2039fa9e4066Sahrens } 2040fa9e4066Sahrens 2041fa9e4066Sahrens /* 20420e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 20430e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 20440e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 20450e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 20460e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 20470e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 20480373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 20490e34b6a7Sbonwick * 20500e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 20510e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 20520e34b6a7Sbonwick * steps will be completed the next time we load the pool. 20530e34b6a7Sbonwick */ 20540373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 20550e34b6a7Sbonwick 20560373e76bSbonwick mutex_enter(&spa_namespace_lock); 20570373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 20580373e76bSbonwick mutex_exit(&spa_namespace_lock); 2059fa9e4066Sahrens 20600373e76bSbonwick return (0); 2061fa9e4066Sahrens } 2062fa9e4066Sahrens 2063fa9e4066Sahrens /* 2064fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 2065fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2066fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2067fa9e4066Sahrens * 2068fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 2069fa9e4066Sahrens * existing device; in this case the two devices are made into their own 20703d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2071fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2072fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 2073fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 2074fa9e4066Sahrens * is automatically detached. 2075fa9e4066Sahrens */ 2076fa9e4066Sahrens int 2077ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2078fa9e4066Sahrens { 2079fa9e4066Sahrens uint64_t txg, open_txg; 2080fa9e4066Sahrens int error; 2081fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2082fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 208399653d4eSeschrock vdev_ops_t *pvops; 20848654d025Sperrin int is_log; 2085fa9e4066Sahrens 2086fa9e4066Sahrens txg = spa_vdev_enter(spa); 2087fa9e4066Sahrens 2088ea8dc4b6Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 2089fa9e4066Sahrens 2090fa9e4066Sahrens if (oldvd == NULL) 2091fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2092fa9e4066Sahrens 20930e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 20940e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 20950e34b6a7Sbonwick 2096fa9e4066Sahrens pvd = oldvd->vdev_parent; 2097fa9e4066Sahrens 209899653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 20993d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 21003d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 21013d7072f8Seschrock 21023d7072f8Seschrock if (newrootvd->vdev_children != 1) 2103fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2104fa9e4066Sahrens 2105fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 2106fa9e4066Sahrens 2107fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2108fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2109fa9e4066Sahrens 211099653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2111fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2112fa9e4066Sahrens 21138654d025Sperrin /* 21148654d025Sperrin * Spares can't replace logs 21158654d025Sperrin */ 21168654d025Sperrin is_log = oldvd->vdev_islog; 21178654d025Sperrin if (is_log && newvd->vdev_isspare) 21188654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 21198654d025Sperrin 212099653d4eSeschrock if (!replacing) { 212199653d4eSeschrock /* 212299653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 212399653d4eSeschrock * vdev. 212499653d4eSeschrock */ 212599653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 212699653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 212799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 212899653d4eSeschrock 212999653d4eSeschrock pvops = &vdev_mirror_ops; 213099653d4eSeschrock } else { 213199653d4eSeschrock /* 213299653d4eSeschrock * Active hot spares can only be replaced by inactive hot 213399653d4eSeschrock * spares. 213499653d4eSeschrock */ 213599653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 213699653d4eSeschrock pvd->vdev_child[1] == oldvd && 213799653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 213899653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 213999653d4eSeschrock 214099653d4eSeschrock /* 214199653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 214299653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 214339c23413Seschrock * want to create a replacing vdev. The user is not allowed to 214439c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 214539c23413Seschrock * the same (spare replaces spare, non-spare replaces 214639c23413Seschrock * non-spare). 214799653d4eSeschrock */ 214899653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 214999653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 215039c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 215139c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 215239c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 215399653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 215499653d4eSeschrock newvd->vdev_isspare) 215599653d4eSeschrock pvops = &vdev_spare_ops; 215699653d4eSeschrock else 215799653d4eSeschrock pvops = &vdev_replacing_ops; 215899653d4eSeschrock } 215999653d4eSeschrock 21602a79c5feSlling /* 21612a79c5feSlling * Compare the new device size with the replaceable/attachable 21622a79c5feSlling * device size. 21632a79c5feSlling */ 21642a79c5feSlling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2165fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2166fa9e4066Sahrens 2167ecc2d604Sbonwick /* 2168ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 2169ecc2d604Sbonwick * than the top-level vdev. 2170ecc2d604Sbonwick */ 2171ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2172fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2173fa9e4066Sahrens 2174fa9e4066Sahrens /* 2175fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 2176fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2177fa9e4066Sahrens */ 2178fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2179fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 2180fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2181fa9e4066Sahrens KM_SLEEP); 2182fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2183fa9e4066Sahrens newvd->vdev_path, "old"); 2184fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 2185fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 2186fa9e4066Sahrens oldvd->vdev_devid = NULL; 2187fa9e4066Sahrens } 2188fa9e4066Sahrens } 2189fa9e4066Sahrens 2190fa9e4066Sahrens /* 219199653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 219299653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 2193fa9e4066Sahrens */ 2194fa9e4066Sahrens if (pvd->vdev_ops != pvops) 2195fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 2196fa9e4066Sahrens 2197fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2198fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 2199fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 2200fa9e4066Sahrens 2201fa9e4066Sahrens /* 2202fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 2203fa9e4066Sahrens */ 2204fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 2205fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 2206fa9e4066Sahrens vdev_add_child(pvd, newvd); 2207fa9e4066Sahrens 2208ea8dc4b6Seschrock /* 2209ea8dc4b6Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 2210ea8dc4b6Seschrock * the addition of newvd may have decreased our parent's asize. 2211ea8dc4b6Seschrock */ 2212ea8dc4b6Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 2213ea8dc4b6Seschrock 2214fa9e4066Sahrens tvd = newvd->vdev_top; 2215fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 2216fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2217fa9e4066Sahrens 2218fa9e4066Sahrens vdev_config_dirty(tvd); 2219fa9e4066Sahrens 2220fa9e4066Sahrens /* 2221fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2222fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2223fa9e4066Sahrens */ 2224fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2225fa9e4066Sahrens 2226fa9e4066Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2227fa9e4066Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2228fa9e4066Sahrens open_txg - TXG_INITIAL + 1); 2229fa9e4066Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2230fa9e4066Sahrens 223139c23413Seschrock if (newvd->vdev_isspare) 223239c23413Seschrock spa_spare_activate(newvd); 2233ea8dc4b6Seschrock 2234fa9e4066Sahrens /* 2235fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 2236fa9e4066Sahrens */ 2237ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2238fa9e4066Sahrens 2239fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2240fa9e4066Sahrens 2241fa9e4066Sahrens /* 22423d7072f8Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 22433d7072f8Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 2244fa9e4066Sahrens */ 22453d7072f8Seschrock mutex_enter(&spa_namespace_lock); 2246fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 22473d7072f8Seschrock mutex_exit(&spa_namespace_lock); 2248fa9e4066Sahrens 2249fa9e4066Sahrens return (0); 2250fa9e4066Sahrens } 2251fa9e4066Sahrens 2252fa9e4066Sahrens /* 2253fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 2254fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 2255fa9e4066Sahrens * is a replacing vdev. 2256fa9e4066Sahrens */ 2257fa9e4066Sahrens int 2258ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2259fa9e4066Sahrens { 2260fa9e4066Sahrens uint64_t txg; 2261fa9e4066Sahrens int c, t, error; 2262fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2263fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 226499653d4eSeschrock boolean_t unspare = B_FALSE; 226599653d4eSeschrock uint64_t unspare_guid; 2266fa9e4066Sahrens 2267fa9e4066Sahrens txg = spa_vdev_enter(spa); 2268fa9e4066Sahrens 2269ea8dc4b6Seschrock vd = vdev_lookup_by_guid(rvd, guid); 2270fa9e4066Sahrens 2271fa9e4066Sahrens if (vd == NULL) 2272fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2273fa9e4066Sahrens 22740e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 22750e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 22760e34b6a7Sbonwick 2277fa9e4066Sahrens pvd = vd->vdev_parent; 2278fa9e4066Sahrens 2279fa9e4066Sahrens /* 2280fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 228199653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 228299653d4eSeschrock * disk can be removed. 228399653d4eSeschrock */ 228499653d4eSeschrock if (replace_done) { 228599653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 228699653d4eSeschrock if (vd->vdev_id != 0) 228799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 228899653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 228999653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 229099653d4eSeschrock } 229199653d4eSeschrock } 229299653d4eSeschrock 229399653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 2294e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2295fa9e4066Sahrens 2296fa9e4066Sahrens /* 229799653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 2298fa9e4066Sahrens */ 2299fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 230099653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 230199653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 2302fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2303fa9e4066Sahrens 2304fa9e4066Sahrens /* 2305fa9e4066Sahrens * If there's only one replica, you can't detach it. 2306fa9e4066Sahrens */ 2307fa9e4066Sahrens if (pvd->vdev_children <= 1) 2308fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2309fa9e4066Sahrens 2310fa9e4066Sahrens /* 2311fa9e4066Sahrens * If all siblings have non-empty DTLs, this device may have the only 2312fa9e4066Sahrens * valid copy of the data, which means we cannot safely detach it. 2313fa9e4066Sahrens * 2314fa9e4066Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2315fa9e4066Sahrens * precise DTL check. 2316fa9e4066Sahrens */ 2317fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2318fa9e4066Sahrens uint64_t dirty; 2319fa9e4066Sahrens 2320fa9e4066Sahrens cvd = pvd->vdev_child[c]; 2321fa9e4066Sahrens if (cvd == vd) 2322fa9e4066Sahrens continue; 2323fa9e4066Sahrens if (vdev_is_dead(cvd)) 2324fa9e4066Sahrens continue; 2325fa9e4066Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2326fa9e4066Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2327fa9e4066Sahrens cvd->vdev_dtl_scrub.sm_space; 2328fa9e4066Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2329fa9e4066Sahrens if (!dirty) 2330fa9e4066Sahrens break; 2331fa9e4066Sahrens } 233299653d4eSeschrock 233399653d4eSeschrock /* 233499653d4eSeschrock * If we are a replacing or spare vdev, then we can always detach the 233599653d4eSeschrock * latter child, as that is how one cancels the operation. 233699653d4eSeschrock */ 233799653d4eSeschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 233899653d4eSeschrock c == pvd->vdev_children) 2339fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2340fa9e4066Sahrens 234199653d4eSeschrock /* 234299653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 234399653d4eSeschrock * that the spare should become a real disk, and be removed from the 234499653d4eSeschrock * active spare list for the pool. 234599653d4eSeschrock */ 234699653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 234799653d4eSeschrock vd->vdev_id == 0) 234899653d4eSeschrock unspare = B_TRUE; 234999653d4eSeschrock 2350fa9e4066Sahrens /* 2351fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 2352fa9e4066Sahrens * This must be done after all other error cases are handled, 2353fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 2354fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 2355fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 2356fa9e4066Sahrens * it's being detached! 2357fa9e4066Sahrens */ 235839c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2359fa9e4066Sahrens 2360fa9e4066Sahrens /* 2361fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 2362fa9e4066Sahrens */ 2363fa9e4066Sahrens vdev_remove_child(pvd, vd); 2364fa9e4066Sahrens vdev_compact_children(pvd); 2365fa9e4066Sahrens 2366fa9e4066Sahrens /* 2367fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 2368fa9e4066Sahrens */ 2369fa9e4066Sahrens cvd = pvd->vdev_child[0]; 2370fa9e4066Sahrens 237199653d4eSeschrock /* 237299653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 237399653d4eSeschrock * do it now, marking the vdev as no longer a spare in the process. We 237499653d4eSeschrock * must do this before vdev_remove_parent(), because that can change the 237599653d4eSeschrock * GUID if it creates a new toplevel GUID. 237699653d4eSeschrock */ 237799653d4eSeschrock if (unspare) { 237899653d4eSeschrock ASSERT(cvd->vdev_isspare); 237939c23413Seschrock spa_spare_remove(cvd); 238099653d4eSeschrock unspare_guid = cvd->vdev_guid; 238199653d4eSeschrock } 238299653d4eSeschrock 2383fa9e4066Sahrens /* 2384fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 2385fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 2386fa9e4066Sahrens */ 2387fa9e4066Sahrens if (pvd->vdev_children == 1) 2388fa9e4066Sahrens vdev_remove_parent(cvd); 2389fa9e4066Sahrens 2390fa9e4066Sahrens /* 2391fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 2392fa9e4066Sahrens * may have been the previous top-level vdev. 2393fa9e4066Sahrens */ 2394fa9e4066Sahrens tvd = cvd->vdev_top; 2395fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2396fa9e4066Sahrens 2397fa9e4066Sahrens /* 239839c23413Seschrock * Reevaluate the parent vdev state. 2399fa9e4066Sahrens */ 24003d7072f8Seschrock vdev_propagate_state(cvd); 2401fa9e4066Sahrens 2402fa9e4066Sahrens /* 240339c23413Seschrock * If the device we just detached was smaller than the others, it may be 240439c23413Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 240539c23413Seschrock * can't fail because the existing metaslabs are already in core, so 240639c23413Seschrock * there's nothing to read from disk. 2407fa9e4066Sahrens */ 2408ecc2d604Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2409fa9e4066Sahrens 2410fa9e4066Sahrens vdev_config_dirty(tvd); 2411fa9e4066Sahrens 2412fa9e4066Sahrens /* 241339c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 241439c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 241539c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 241639c23413Seschrock * prevent vd from being accessed after it's freed. 2417fa9e4066Sahrens */ 2418fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) 2419fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 2420ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 2421ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2422fa9e4066Sahrens 24233d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 24243d7072f8Seschrock 242599653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 242699653d4eSeschrock 242799653d4eSeschrock /* 242839c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 242939c23413Seschrock * then we want to go through and remove the device from the hot spare 243039c23413Seschrock * list of every other pool. 243199653d4eSeschrock */ 243299653d4eSeschrock if (unspare) { 243399653d4eSeschrock spa = NULL; 243499653d4eSeschrock mutex_enter(&spa_namespace_lock); 243599653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 243699653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 243799653d4eSeschrock continue; 243899653d4eSeschrock 243999653d4eSeschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 244099653d4eSeschrock } 244199653d4eSeschrock mutex_exit(&spa_namespace_lock); 244299653d4eSeschrock } 244399653d4eSeschrock 244499653d4eSeschrock return (error); 244599653d4eSeschrock } 244699653d4eSeschrock 244799653d4eSeschrock /* 244899653d4eSeschrock * Remove a device from the pool. Currently, this supports removing only hot 244999653d4eSeschrock * spares. 245099653d4eSeschrock */ 245199653d4eSeschrock int 245299653d4eSeschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 245399653d4eSeschrock { 245499653d4eSeschrock vdev_t *vd; 245599653d4eSeschrock nvlist_t **spares, *nv, **newspares; 245699653d4eSeschrock uint_t i, j, nspares; 245799653d4eSeschrock int ret = 0; 245899653d4eSeschrock 245999653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 246099653d4eSeschrock 246199653d4eSeschrock vd = spa_lookup_by_guid(spa, guid); 246299653d4eSeschrock 246399653d4eSeschrock nv = NULL; 246499653d4eSeschrock if (spa->spa_spares != NULL && 246599653d4eSeschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 246699653d4eSeschrock &spares, &nspares) == 0) { 246799653d4eSeschrock for (i = 0; i < nspares; i++) { 246899653d4eSeschrock uint64_t theguid; 246999653d4eSeschrock 247099653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 247199653d4eSeschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 247299653d4eSeschrock if (theguid == guid) { 247399653d4eSeschrock nv = spares[i]; 247499653d4eSeschrock break; 247599653d4eSeschrock } 247699653d4eSeschrock } 247799653d4eSeschrock } 247899653d4eSeschrock 247999653d4eSeschrock /* 248099653d4eSeschrock * We only support removing a hot spare, and only if it's not currently 248199653d4eSeschrock * in use in this pool. 248299653d4eSeschrock */ 248399653d4eSeschrock if (nv == NULL && vd == NULL) { 248499653d4eSeschrock ret = ENOENT; 248599653d4eSeschrock goto out; 248699653d4eSeschrock } 248799653d4eSeschrock 248899653d4eSeschrock if (nv == NULL && vd != NULL) { 248999653d4eSeschrock ret = ENOTSUP; 249099653d4eSeschrock goto out; 249199653d4eSeschrock } 249299653d4eSeschrock 249399653d4eSeschrock if (!unspare && nv != NULL && vd != NULL) { 249499653d4eSeschrock ret = EBUSY; 249599653d4eSeschrock goto out; 249699653d4eSeschrock } 249799653d4eSeschrock 249899653d4eSeschrock if (nspares == 1) { 249999653d4eSeschrock newspares = NULL; 250099653d4eSeschrock } else { 250199653d4eSeschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 250299653d4eSeschrock KM_SLEEP); 250399653d4eSeschrock for (i = 0, j = 0; i < nspares; i++) { 250499653d4eSeschrock if (spares[i] != nv) 250599653d4eSeschrock VERIFY(nvlist_dup(spares[i], 250699653d4eSeschrock &newspares[j++], KM_SLEEP) == 0); 250799653d4eSeschrock } 250899653d4eSeschrock } 250999653d4eSeschrock 251099653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 251199653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 251299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 251399653d4eSeschrock newspares, nspares - 1) == 0); 251499653d4eSeschrock for (i = 0; i < nspares - 1; i++) 251599653d4eSeschrock nvlist_free(newspares[i]); 251699653d4eSeschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 251799653d4eSeschrock spa_load_spares(spa); 251899653d4eSeschrock spa->spa_sync_spares = B_TRUE; 251999653d4eSeschrock 252099653d4eSeschrock out: 252199653d4eSeschrock spa_config_exit(spa, FTAG); 252299653d4eSeschrock 252399653d4eSeschrock return (ret); 2524fa9e4066Sahrens } 2525fa9e4066Sahrens 2526fa9e4066Sahrens /* 25273d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 25283d7072f8Seschrock * current spared, so we can detach it. 2529fa9e4066Sahrens */ 2530ea8dc4b6Seschrock static vdev_t * 25313d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2532fa9e4066Sahrens { 2533ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 2534fa9e4066Sahrens int c; 2535fa9e4066Sahrens 2536ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) { 25373d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 2538ea8dc4b6Seschrock if (oldvd != NULL) 2539ea8dc4b6Seschrock return (oldvd); 2540ea8dc4b6Seschrock } 2541fa9e4066Sahrens 25423d7072f8Seschrock /* 25433d7072f8Seschrock * Check for a completed replacement. 25443d7072f8Seschrock */ 2545fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 2546ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 2547ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 2548ea8dc4b6Seschrock 2549ea8dc4b6Seschrock mutex_enter(&newvd->vdev_dtl_lock); 2550ea8dc4b6Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 2551ea8dc4b6Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 2552ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 2553ea8dc4b6Seschrock return (oldvd); 2554fa9e4066Sahrens } 2555ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 2556fa9e4066Sahrens } 2557ea8dc4b6Seschrock 25583d7072f8Seschrock /* 25593d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 25603d7072f8Seschrock */ 25613d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 25623d7072f8Seschrock newvd = vd->vdev_child[0]; 25633d7072f8Seschrock oldvd = vd->vdev_child[1]; 25643d7072f8Seschrock 25653d7072f8Seschrock mutex_enter(&newvd->vdev_dtl_lock); 25663d7072f8Seschrock if (newvd->vdev_unspare && 25673d7072f8Seschrock newvd->vdev_dtl_map.sm_space == 0 && 25683d7072f8Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 25693d7072f8Seschrock newvd->vdev_unspare = 0; 25703d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 25713d7072f8Seschrock return (oldvd); 25723d7072f8Seschrock } 25733d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 25743d7072f8Seschrock } 25753d7072f8Seschrock 2576ea8dc4b6Seschrock return (NULL); 2577fa9e4066Sahrens } 2578fa9e4066Sahrens 2579ea8dc4b6Seschrock static void 25803d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 2581fa9e4066Sahrens { 2582ea8dc4b6Seschrock vdev_t *vd; 258399653d4eSeschrock vdev_t *pvd; 2584ea8dc4b6Seschrock uint64_t guid; 258599653d4eSeschrock uint64_t pguid = 0; 2586ea8dc4b6Seschrock 2587ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2588ea8dc4b6Seschrock 25893d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 2590ea8dc4b6Seschrock guid = vd->vdev_guid; 259199653d4eSeschrock /* 259299653d4eSeschrock * If we have just finished replacing a hot spared device, then 259399653d4eSeschrock * we need to detach the parent's first child (the original hot 259499653d4eSeschrock * spare) as well. 259599653d4eSeschrock */ 259699653d4eSeschrock pvd = vd->vdev_parent; 259799653d4eSeschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 259899653d4eSeschrock pvd->vdev_id == 0) { 259999653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 260099653d4eSeschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 260199653d4eSeschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 260299653d4eSeschrock } 2603ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2604ea8dc4b6Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 2605ea8dc4b6Seschrock return; 260699653d4eSeschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 260799653d4eSeschrock return; 2608ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2609fa9e4066Sahrens } 2610fa9e4066Sahrens 2611ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2612fa9e4066Sahrens } 2613fa9e4066Sahrens 2614c67d9675Seschrock /* 2615c67d9675Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 2616c67d9675Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 2617c67d9675Seschrock */ 2618c67d9675Seschrock int 2619c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 2620c67d9675Seschrock { 2621c67d9675Seschrock vdev_t *rvd, *vd; 2622c67d9675Seschrock uint64_t txg; 2623c67d9675Seschrock 2624c67d9675Seschrock rvd = spa->spa_root_vdev; 2625c67d9675Seschrock 2626c67d9675Seschrock txg = spa_vdev_enter(spa); 2627c67d9675Seschrock 262899653d4eSeschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 262999653d4eSeschrock /* 263099653d4eSeschrock * Determine if this is a reference to a hot spare. In that 263199653d4eSeschrock * case, update the path as stored in the spare list. 263299653d4eSeschrock */ 263399653d4eSeschrock nvlist_t **spares; 263499653d4eSeschrock uint_t i, nspares; 263599653d4eSeschrock if (spa->spa_sparelist != NULL) { 263699653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 263799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 263899653d4eSeschrock for (i = 0; i < nspares; i++) { 263999653d4eSeschrock uint64_t theguid; 264099653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 264199653d4eSeschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 264299653d4eSeschrock if (theguid == guid) 264399653d4eSeschrock break; 264499653d4eSeschrock } 264599653d4eSeschrock 264699653d4eSeschrock if (i == nspares) 264799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 264899653d4eSeschrock 264999653d4eSeschrock VERIFY(nvlist_add_string(spares[i], 265099653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0); 265199653d4eSeschrock spa_load_spares(spa); 265299653d4eSeschrock spa->spa_sync_spares = B_TRUE; 265399653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 265499653d4eSeschrock } else { 265599653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 265699653d4eSeschrock } 265799653d4eSeschrock } 2658c67d9675Seschrock 26590e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 26600e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 26610e34b6a7Sbonwick 2662c67d9675Seschrock spa_strfree(vd->vdev_path); 2663c67d9675Seschrock vd->vdev_path = spa_strdup(newpath); 2664c67d9675Seschrock 2665c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 2666c67d9675Seschrock 2667c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 2668c67d9675Seschrock } 2669c67d9675Seschrock 2670fa9e4066Sahrens /* 2671fa9e4066Sahrens * ========================================================================== 2672fa9e4066Sahrens * SPA Scrubbing 2673fa9e4066Sahrens * ========================================================================== 2674fa9e4066Sahrens */ 2675fa9e4066Sahrens 2676fa9e4066Sahrens static void 2677fa9e4066Sahrens spa_scrub_io_done(zio_t *zio) 2678fa9e4066Sahrens { 2679fa9e4066Sahrens spa_t *spa = zio->io_spa; 2680fa9e4066Sahrens 26810e8c6158Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2682fa9e4066Sahrens 2683fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2684ea8dc4b6Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 268544cd46caSbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2686ea8dc4b6Seschrock spa->spa_scrub_errors++; 2687fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 2688fa9e4066Sahrens vd->vdev_stat.vs_scrub_errors++; 2689fa9e4066Sahrens mutex_exit(&vd->vdev_stat_lock); 2690fa9e4066Sahrens } 269105b2b3b8Smishra 269205b2b3b8Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 2693ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 269405b2b3b8Smishra 269505b2b3b8Smishra ASSERT(spa->spa_scrub_inflight >= 0); 269605b2b3b8Smishra 2697ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 2698fa9e4066Sahrens } 2699fa9e4066Sahrens 2700fa9e4066Sahrens static void 2701ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 2702ea8dc4b6Seschrock zbookmark_t *zb) 2703fa9e4066Sahrens { 2704fa9e4066Sahrens size_t size = BP_GET_LSIZE(bp); 270505b2b3b8Smishra void *data; 2706fa9e4066Sahrens 2707fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 270805b2b3b8Smishra /* 270905b2b3b8Smishra * Do not give too much work to vdev(s). 271005b2b3b8Smishra */ 271105b2b3b8Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 271205b2b3b8Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 271305b2b3b8Smishra } 2714fa9e4066Sahrens spa->spa_scrub_inflight++; 2715fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2716fa9e4066Sahrens 27170e8c6158Smaybee data = arc_data_buf_alloc(size); 271805b2b3b8Smishra 2719ea8dc4b6Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 2720ea8dc4b6Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 2721ea8dc4b6Seschrock 2722d80c45e0Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 2723ea8dc4b6Seschrock 2724fa9e4066Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 2725ea8dc4b6Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2726fa9e4066Sahrens } 2727fa9e4066Sahrens 2728fa9e4066Sahrens /* ARGSUSED */ 2729fa9e4066Sahrens static int 2730fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2731fa9e4066Sahrens { 2732fa9e4066Sahrens blkptr_t *bp = &bc->bc_blkptr; 273344cd46caSbillm vdev_t *vd = spa->spa_root_vdev; 273444cd46caSbillm dva_t *dva = bp->blk_dva; 273544cd46caSbillm int needs_resilver = B_FALSE; 273644cd46caSbillm int d; 2737fa9e4066Sahrens 273844cd46caSbillm if (bc->bc_errno) { 2739fa9e4066Sahrens /* 2740fa9e4066Sahrens * We can't scrub this block, but we can continue to scrub 2741fa9e4066Sahrens * the rest of the pool. Note the error and move along. 2742fa9e4066Sahrens */ 2743fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2744fa9e4066Sahrens spa->spa_scrub_errors++; 2745fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2746fa9e4066Sahrens 274744cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 274844cd46caSbillm vd->vdev_stat.vs_scrub_errors++; 274944cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 2750fa9e4066Sahrens 2751fa9e4066Sahrens return (ERESTART); 2752fa9e4066Sahrens } 2753fa9e4066Sahrens 2754fa9e4066Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2755fa9e4066Sahrens 275644cd46caSbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 275744cd46caSbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 2758fa9e4066Sahrens 275944cd46caSbillm ASSERT(vd != NULL); 276044cd46caSbillm 276144cd46caSbillm /* 276244cd46caSbillm * Keep track of how much data we've examined so that 276344cd46caSbillm * zpool(1M) status can make useful progress reports. 276444cd46caSbillm */ 276544cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 276644cd46caSbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 276744cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 276844cd46caSbillm 276944cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 277044cd46caSbillm if (DVA_GET_GANG(&dva[d])) { 277144cd46caSbillm /* 277244cd46caSbillm * Gang members may be spread across multiple 277344cd46caSbillm * vdevs, so the best we can do is look at the 277444cd46caSbillm * pool-wide DTL. 277544cd46caSbillm * XXX -- it would be better to change our 277644cd46caSbillm * allocation policy to ensure that this can't 277744cd46caSbillm * happen. 277844cd46caSbillm */ 277944cd46caSbillm vd = spa->spa_root_vdev; 278044cd46caSbillm } 278144cd46caSbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 278244cd46caSbillm bp->blk_birth, 1)) 278344cd46caSbillm needs_resilver = B_TRUE; 2784fa9e4066Sahrens } 278544cd46caSbillm } 278644cd46caSbillm 278744cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2788fa9e4066Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 2789ea8dc4b6Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 279044cd46caSbillm else if (needs_resilver) 279144cd46caSbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 279244cd46caSbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2793fa9e4066Sahrens 2794fa9e4066Sahrens return (0); 2795fa9e4066Sahrens } 2796fa9e4066Sahrens 2797fa9e4066Sahrens static void 2798fa9e4066Sahrens spa_scrub_thread(spa_t *spa) 2799fa9e4066Sahrens { 2800fa9e4066Sahrens callb_cpr_t cprinfo; 2801fa9e4066Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2802fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2803fa9e4066Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2804fa9e4066Sahrens int error = 0; 2805fa9e4066Sahrens boolean_t complete; 2806fa9e4066Sahrens 2807fa9e4066Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2808fa9e4066Sahrens 2809f0aa80d4Sbonwick /* 2810f0aa80d4Sbonwick * If we're restarting due to a snapshot create/delete, 2811f0aa80d4Sbonwick * wait for that to complete. 2812f0aa80d4Sbonwick */ 2813f0aa80d4Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2814f0aa80d4Sbonwick 2815ea8dc4b6Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 2816ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2817ea8dc4b6Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 2818ea8dc4b6Seschrock 2819ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2820ea8dc4b6Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2821fa9e4066Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2822fa9e4066Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 2823ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2824fa9e4066Sahrens 2825fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2826fa9e4066Sahrens spa->spa_scrub_errors = 0; 2827fa9e4066Sahrens spa->spa_scrub_active = 1; 2828ea8dc4b6Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2829fa9e4066Sahrens 2830fa9e4066Sahrens while (!spa->spa_scrub_stop) { 2831fa9e4066Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 2832ea8dc4b6Seschrock while (spa->spa_scrub_suspended) { 2833fa9e4066Sahrens spa->spa_scrub_active = 0; 2834fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 2835fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2836fa9e4066Sahrens spa->spa_scrub_active = 1; 2837fa9e4066Sahrens } 2838fa9e4066Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2839fa9e4066Sahrens 2840fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 2841fa9e4066Sahrens break; 2842fa9e4066Sahrens 2843fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2844fa9e4066Sahrens error = traverse_more(th); 2845fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2846fa9e4066Sahrens if (error != EAGAIN) 2847fa9e4066Sahrens break; 2848fa9e4066Sahrens } 2849fa9e4066Sahrens 2850fa9e4066Sahrens while (spa->spa_scrub_inflight) 2851fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2852fa9e4066Sahrens 28535dabedeeSbonwick spa->spa_scrub_active = 0; 28545dabedeeSbonwick cv_broadcast(&spa->spa_scrub_cv); 28555dabedeeSbonwick 28565dabedeeSbonwick mutex_exit(&spa->spa_scrub_lock); 28575dabedeeSbonwick 28585dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 28595dabedeeSbonwick 28605dabedeeSbonwick mutex_enter(&spa->spa_scrub_lock); 28615dabedeeSbonwick 28625dabedeeSbonwick /* 28635dabedeeSbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 28645dabedeeSbonwick * AND the spa config lock to synchronize with any config changes 28655dabedeeSbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 28665dabedeeSbonwick */ 2867fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 2868fa9e4066Sahrens error = ERESTART; 2869fa9e4066Sahrens 2870ea8dc4b6Seschrock if (spa->spa_scrub_stop) 2871ea8dc4b6Seschrock error = EINTR; 2872ea8dc4b6Seschrock 2873fa9e4066Sahrens /* 2874ea8dc4b6Seschrock * Even if there were uncorrectable errors, we consider the scrub 2875ea8dc4b6Seschrock * completed. The downside is that if there is a transient error during 2876ea8dc4b6Seschrock * a resilver, we won't resilver the data properly to the target. But 2877ea8dc4b6Seschrock * if the damage is permanent (more likely) we will resilver forever, 2878ea8dc4b6Seschrock * which isn't really acceptable. Since there is enough information for 2879ea8dc4b6Seschrock * the user to know what has failed and why, this seems like a more 2880ea8dc4b6Seschrock * tractable approach. 2881fa9e4066Sahrens */ 2882ea8dc4b6Seschrock complete = (error == 0); 2883fa9e4066Sahrens 2884ea8dc4b6Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 2885ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2886fa9e4066Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2887fa9e4066Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2888fa9e4066Sahrens 2889fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2890fa9e4066Sahrens 2891fa9e4066Sahrens /* 2892fa9e4066Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2893fa9e4066Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2894fa9e4066Sahrens */ 2895fa9e4066Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2896fa9e4066Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2897fa9e4066Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 2898ea8dc4b6Seschrock spa_errlog_rotate(spa); 28995dabedeeSbonwick 29003d7072f8Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 29013d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 29023d7072f8Seschrock 2903ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2904fa9e4066Sahrens 2905fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2906fa9e4066Sahrens 2907ea8dc4b6Seschrock /* 2908ea8dc4b6Seschrock * We may have finished replacing a device. 2909ea8dc4b6Seschrock * Let the async thread assess this and handle the detach. 2910ea8dc4b6Seschrock */ 29113d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2912fa9e4066Sahrens 2913fa9e4066Sahrens /* 2914fa9e4066Sahrens * If we were told to restart, our final act is to start a new scrub. 2915fa9e4066Sahrens */ 2916fa9e4066Sahrens if (error == ERESTART) 2917ea8dc4b6Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 2918ea8dc4b6Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2919fa9e4066Sahrens 2920ea8dc4b6Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 2921ea8dc4b6Seschrock spa->spa_scrub_active = 0; 2922ea8dc4b6Seschrock spa->spa_scrub_thread = NULL; 2923ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_cv); 2924fa9e4066Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2925fa9e4066Sahrens thread_exit(); 2926fa9e4066Sahrens } 2927fa9e4066Sahrens 2928fa9e4066Sahrens void 2929fa9e4066Sahrens spa_scrub_suspend(spa_t *spa) 2930fa9e4066Sahrens { 2931fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2932ea8dc4b6Seschrock spa->spa_scrub_suspended++; 2933fa9e4066Sahrens while (spa->spa_scrub_active) { 2934fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 2935fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2936fa9e4066Sahrens } 2937fa9e4066Sahrens while (spa->spa_scrub_inflight) 2938fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2939fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2940fa9e4066Sahrens } 2941fa9e4066Sahrens 2942fa9e4066Sahrens void 2943fa9e4066Sahrens spa_scrub_resume(spa_t *spa) 2944fa9e4066Sahrens { 2945fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2946ea8dc4b6Seschrock ASSERT(spa->spa_scrub_suspended != 0); 2947ea8dc4b6Seschrock if (--spa->spa_scrub_suspended == 0) 2948fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 2949fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2950fa9e4066Sahrens } 2951fa9e4066Sahrens 2952fa9e4066Sahrens void 2953fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 2954fa9e4066Sahrens { 2955fa9e4066Sahrens /* 2956fa9e4066Sahrens * Something happened (e.g. snapshot create/delete) that means 2957fa9e4066Sahrens * we must restart any in-progress scrubs. The itinerary will 2958fa9e4066Sahrens * fix this properly. 2959fa9e4066Sahrens */ 2960fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2961fa9e4066Sahrens spa->spa_scrub_restart_txg = txg; 2962fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2963fa9e4066Sahrens } 2964fa9e4066Sahrens 2965ea8dc4b6Seschrock int 2966ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2967fa9e4066Sahrens { 2968fa9e4066Sahrens space_seg_t *ss; 2969fa9e4066Sahrens uint64_t mintxg, maxtxg; 2970fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2971fa9e4066Sahrens 2972bb8b5132Sek ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2973bb8b5132Sek ASSERT(!spa_config_held(spa, RW_WRITER)); 2974bb8b5132Sek 2975fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 2976fa9e4066Sahrens return (ENOTSUP); 2977fa9e4066Sahrens 2978ea8dc4b6Seschrock mutex_enter(&spa->spa_scrub_lock); 2979ea8dc4b6Seschrock 2980fa9e4066Sahrens /* 2981fa9e4066Sahrens * If there's a scrub or resilver already in progress, stop it. 2982fa9e4066Sahrens */ 2983fa9e4066Sahrens while (spa->spa_scrub_thread != NULL) { 2984fa9e4066Sahrens /* 2985fa9e4066Sahrens * Don't stop a resilver unless forced. 2986fa9e4066Sahrens */ 2987ea8dc4b6Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 2988ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 2989fa9e4066Sahrens return (EBUSY); 2990ea8dc4b6Seschrock } 2991fa9e4066Sahrens spa->spa_scrub_stop = 1; 2992fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 2993fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2994fa9e4066Sahrens } 2995fa9e4066Sahrens 2996fa9e4066Sahrens /* 2997fa9e4066Sahrens * Terminate the previous traverse. 2998fa9e4066Sahrens */ 2999fa9e4066Sahrens if (spa->spa_scrub_th != NULL) { 3000fa9e4066Sahrens traverse_fini(spa->spa_scrub_th); 3001fa9e4066Sahrens spa->spa_scrub_th = NULL; 3002fa9e4066Sahrens } 3003fa9e4066Sahrens 3004ea8dc4b6Seschrock if (rvd == NULL) { 3005ea8dc4b6Seschrock ASSERT(spa->spa_scrub_stop == 0); 3006ea8dc4b6Seschrock ASSERT(spa->spa_scrub_type == type); 3007ea8dc4b6Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 3008ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3009ea8dc4b6Seschrock return (0); 3010ea8dc4b6Seschrock } 3011fa9e4066Sahrens 3012fa9e4066Sahrens mintxg = TXG_INITIAL - 1; 3013fa9e4066Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 3014fa9e4066Sahrens 3015ea8dc4b6Seschrock mutex_enter(&rvd->vdev_dtl_lock); 3016fa9e4066Sahrens 3017ea8dc4b6Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 3018ea8dc4b6Seschrock /* 3019ea8dc4b6Seschrock * The pool-wide DTL is empty. 3020ecc2d604Sbonwick * If this is a resilver, there's nothing to do except 3021ecc2d604Sbonwick * check whether any in-progress replacements have completed. 3022ea8dc4b6Seschrock */ 3023ecc2d604Sbonwick if (type == POOL_SCRUB_RESILVER) { 3024ea8dc4b6Seschrock type = POOL_SCRUB_NONE; 30253d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3026ecc2d604Sbonwick } 3027ea8dc4b6Seschrock } else { 3028ea8dc4b6Seschrock /* 3029ea8dc4b6Seschrock * The pool-wide DTL is non-empty. 3030ea8dc4b6Seschrock * If this is a normal scrub, upgrade to a resilver instead. 3031ea8dc4b6Seschrock */ 3032ea8dc4b6Seschrock if (type == POOL_SCRUB_EVERYTHING) 3033ea8dc4b6Seschrock type = POOL_SCRUB_RESILVER; 3034ea8dc4b6Seschrock } 3035fa9e4066Sahrens 3036ea8dc4b6Seschrock if (type == POOL_SCRUB_RESILVER) { 3037fa9e4066Sahrens /* 3038fa9e4066Sahrens * Determine the resilvering boundaries. 3039fa9e4066Sahrens * 3040fa9e4066Sahrens * Note: (mintxg, maxtxg) is an open interval, 3041fa9e4066Sahrens * i.e. mintxg and maxtxg themselves are not included. 3042fa9e4066Sahrens * 3043fa9e4066Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 3044fa9e4066Sahrens * so we don't claim to resilver a txg that's still changing. 3045fa9e4066Sahrens */ 3046fa9e4066Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 3047ea8dc4b6Seschrock mintxg = ss->ss_start - 1; 3048fa9e4066Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 3049ea8dc4b6Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 30503d7072f8Seschrock 30513d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 3052fa9e4066Sahrens } 3053fa9e4066Sahrens 3054ea8dc4b6Seschrock mutex_exit(&rvd->vdev_dtl_lock); 3055ea8dc4b6Seschrock 3056ea8dc4b6Seschrock spa->spa_scrub_stop = 0; 3057ea8dc4b6Seschrock spa->spa_scrub_type = type; 3058ea8dc4b6Seschrock spa->spa_scrub_restart_txg = 0; 3059ea8dc4b6Seschrock 3060ea8dc4b6Seschrock if (type != POOL_SCRUB_NONE) { 3061ea8dc4b6Seschrock spa->spa_scrub_mintxg = mintxg; 3062fa9e4066Sahrens spa->spa_scrub_maxtxg = maxtxg; 3063fa9e4066Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 30640373e76bSbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 30650373e76bSbonwick ZIO_FLAG_CANFAIL); 3066fa9e4066Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 3067fa9e4066Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 3068fa9e4066Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 3069fa9e4066Sahrens } 3070fa9e4066Sahrens 3071ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3072ea8dc4b6Seschrock 3073fa9e4066Sahrens return (0); 3074fa9e4066Sahrens } 3075fa9e4066Sahrens 3076ea8dc4b6Seschrock /* 3077ea8dc4b6Seschrock * ========================================================================== 3078ea8dc4b6Seschrock * SPA async task processing 3079ea8dc4b6Seschrock * ========================================================================== 3080ea8dc4b6Seschrock */ 3081ea8dc4b6Seschrock 3082ea8dc4b6Seschrock static void 30833d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3084fa9e4066Sahrens { 3085ea8dc4b6Seschrock vdev_t *tvd; 3086ea8dc4b6Seschrock int c; 3087fa9e4066Sahrens 30883d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) { 30893d7072f8Seschrock tvd = vd->vdev_child[c]; 30903d7072f8Seschrock if (tvd->vdev_remove_wanted) { 30913d7072f8Seschrock tvd->vdev_remove_wanted = 0; 30923d7072f8Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 30933d7072f8Seschrock VDEV_AUX_NONE); 30943d7072f8Seschrock vdev_clear(spa, tvd); 30953d7072f8Seschrock vdev_config_dirty(tvd->vdev_top); 3096ea8dc4b6Seschrock } 30973d7072f8Seschrock spa_async_remove(spa, tvd); 3098ea8dc4b6Seschrock } 3099ea8dc4b6Seschrock } 3100fa9e4066Sahrens 3101ea8dc4b6Seschrock static void 3102ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 3103ea8dc4b6Seschrock { 3104ea8dc4b6Seschrock int tasks; 31053d7072f8Seschrock uint64_t txg; 3106ea8dc4b6Seschrock 3107ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 3108ea8dc4b6Seschrock 3109ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3110ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 3111ea8dc4b6Seschrock spa->spa_async_tasks = 0; 3112ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3113ea8dc4b6Seschrock 31140373e76bSbonwick /* 31150373e76bSbonwick * See if the config needs to be updated. 31160373e76bSbonwick */ 31170373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 31180373e76bSbonwick mutex_enter(&spa_namespace_lock); 31190373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 31200373e76bSbonwick mutex_exit(&spa_namespace_lock); 31210373e76bSbonwick } 31220373e76bSbonwick 3123ea8dc4b6Seschrock /* 31243d7072f8Seschrock * See if any devices need to be marked REMOVED. 3125ea8dc4b6Seschrock */ 31263d7072f8Seschrock if (tasks & SPA_ASYNC_REMOVE) { 31273d7072f8Seschrock txg = spa_vdev_enter(spa); 31283d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 31293d7072f8Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 31303d7072f8Seschrock } 3131ea8dc4b6Seschrock 3132ea8dc4b6Seschrock /* 3133ea8dc4b6Seschrock * If any devices are done replacing, detach them. 3134ea8dc4b6Seschrock */ 31353d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 31363d7072f8Seschrock spa_vdev_resilver_done(spa); 3137fa9e4066Sahrens 3138ea8dc4b6Seschrock /* 31393d7072f8Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 31403d7072f8Seschrock * scrub which can become a resilver), we need to hold 31413d7072f8Seschrock * spa_namespace_lock() because the sysevent we post via 31423d7072f8Seschrock * spa_event_notify() needs to get the name of the pool. 3143ea8dc4b6Seschrock */ 31443d7072f8Seschrock if (tasks & SPA_ASYNC_SCRUB) { 31453d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3146ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 31473d7072f8Seschrock mutex_exit(&spa_namespace_lock); 31483d7072f8Seschrock } 3149ea8dc4b6Seschrock 3150ea8dc4b6Seschrock /* 3151ea8dc4b6Seschrock * Kick off a resilver. 3152ea8dc4b6Seschrock */ 31533d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER) { 31543d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3155ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 31563d7072f8Seschrock mutex_exit(&spa_namespace_lock); 31573d7072f8Seschrock } 3158ea8dc4b6Seschrock 3159ea8dc4b6Seschrock /* 3160ea8dc4b6Seschrock * Let the world know that we're done. 3161ea8dc4b6Seschrock */ 3162ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3163ea8dc4b6Seschrock spa->spa_async_thread = NULL; 3164ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 3165ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3166ea8dc4b6Seschrock thread_exit(); 3167ea8dc4b6Seschrock } 3168ea8dc4b6Seschrock 3169ea8dc4b6Seschrock void 3170ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 3171ea8dc4b6Seschrock { 3172ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3173ea8dc4b6Seschrock spa->spa_async_suspended++; 3174ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 3175ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 3176ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3177ea8dc4b6Seschrock } 3178ea8dc4b6Seschrock 3179ea8dc4b6Seschrock void 3180ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 3181ea8dc4b6Seschrock { 3182ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3183ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 3184ea8dc4b6Seschrock spa->spa_async_suspended--; 3185ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3186ea8dc4b6Seschrock } 3187ea8dc4b6Seschrock 3188ea8dc4b6Seschrock static void 3189ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 3190ea8dc4b6Seschrock { 3191ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3192ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 31930373e76bSbonwick spa->spa_async_thread == NULL && 31940373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 3195ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 3196ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 3197ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3198ea8dc4b6Seschrock } 3199ea8dc4b6Seschrock 3200ea8dc4b6Seschrock void 3201ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 3202ea8dc4b6Seschrock { 3203ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3204ea8dc4b6Seschrock spa->spa_async_tasks |= task; 3205ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3206fa9e4066Sahrens } 3207fa9e4066Sahrens 3208fa9e4066Sahrens /* 3209fa9e4066Sahrens * ========================================================================== 3210fa9e4066Sahrens * SPA syncing routines 3211fa9e4066Sahrens * ========================================================================== 3212fa9e4066Sahrens */ 3213fa9e4066Sahrens 3214fa9e4066Sahrens static void 3215fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3216fa9e4066Sahrens { 3217fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3218fa9e4066Sahrens dmu_tx_t *tx; 3219fa9e4066Sahrens blkptr_t blk; 3220fa9e4066Sahrens uint64_t itor = 0; 3221fa9e4066Sahrens zio_t *zio; 3222fa9e4066Sahrens int error; 3223fa9e4066Sahrens uint8_t c = 1; 3224fa9e4066Sahrens 3225fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3226fa9e4066Sahrens 3227fa9e4066Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3228fa9e4066Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3229fa9e4066Sahrens 3230fa9e4066Sahrens error = zio_wait(zio); 3231fa9e4066Sahrens ASSERT3U(error, ==, 0); 3232fa9e4066Sahrens 3233fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3234fa9e4066Sahrens bplist_vacate(bpl, tx); 3235fa9e4066Sahrens 3236fa9e4066Sahrens /* 3237fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 3238fa9e4066Sahrens * (Usually only the first block is needed.) 3239fa9e4066Sahrens */ 3240fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3241fa9e4066Sahrens dmu_tx_commit(tx); 3242fa9e4066Sahrens } 3243fa9e4066Sahrens 3244fa9e4066Sahrens static void 324599653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 3246fa9e4066Sahrens { 3247fa9e4066Sahrens char *packed = NULL; 3248fa9e4066Sahrens size_t nvsize = 0; 3249fa9e4066Sahrens dmu_buf_t *db; 3250fa9e4066Sahrens 325199653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 3252fa9e4066Sahrens 3253fa9e4066Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 3254fa9e4066Sahrens 325599653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 3256ea8dc4b6Seschrock KM_SLEEP) == 0); 3257fa9e4066Sahrens 325899653d4eSeschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 3259fa9e4066Sahrens 3260fa9e4066Sahrens kmem_free(packed, nvsize); 3261fa9e4066Sahrens 326299653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3263fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 3264fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 3265ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 3266fa9e4066Sahrens } 3267fa9e4066Sahrens 326899653d4eSeschrock static void 326999653d4eSeschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 327099653d4eSeschrock { 327199653d4eSeschrock nvlist_t *nvroot; 327299653d4eSeschrock nvlist_t **spares; 327399653d4eSeschrock int i; 327499653d4eSeschrock 327599653d4eSeschrock if (!spa->spa_sync_spares) 327699653d4eSeschrock return; 327799653d4eSeschrock 327899653d4eSeschrock /* 327999653d4eSeschrock * Update the MOS nvlist describing the list of available spares. 328099653d4eSeschrock * spa_validate_spares() will have already made sure this nvlist is 32813d7072f8Seschrock * valid and the vdevs are labeled appropriately. 328299653d4eSeschrock */ 328399653d4eSeschrock if (spa->spa_spares_object == 0) { 328499653d4eSeschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 328599653d4eSeschrock DMU_OT_PACKED_NVLIST, 1 << 14, 328699653d4eSeschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 328799653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 328899653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 328999653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 329099653d4eSeschrock } 329199653d4eSeschrock 329299653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 329399653d4eSeschrock if (spa->spa_nspares == 0) { 329499653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 329599653d4eSeschrock NULL, 0) == 0); 329699653d4eSeschrock } else { 329799653d4eSeschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 329899653d4eSeschrock KM_SLEEP); 329999653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 330099653d4eSeschrock spares[i] = vdev_config_generate(spa, 330199653d4eSeschrock spa->spa_spares[i], B_FALSE, B_TRUE); 330299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 330399653d4eSeschrock spares, spa->spa_nspares) == 0); 330499653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 330599653d4eSeschrock nvlist_free(spares[i]); 330699653d4eSeschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 330799653d4eSeschrock } 330899653d4eSeschrock 330999653d4eSeschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 331006eeb2adSek nvlist_free(nvroot); 331199653d4eSeschrock 331299653d4eSeschrock spa->spa_sync_spares = B_FALSE; 331399653d4eSeschrock } 331499653d4eSeschrock 331599653d4eSeschrock static void 331699653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 331799653d4eSeschrock { 331899653d4eSeschrock nvlist_t *config; 331999653d4eSeschrock 332099653d4eSeschrock if (list_is_empty(&spa->spa_dirty_list)) 332199653d4eSeschrock return; 332299653d4eSeschrock 332399653d4eSeschrock config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 332499653d4eSeschrock 332599653d4eSeschrock if (spa->spa_config_syncing) 332699653d4eSeschrock nvlist_free(spa->spa_config_syncing); 332799653d4eSeschrock spa->spa_config_syncing = config; 332899653d4eSeschrock 332999653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 333099653d4eSeschrock } 333199653d4eSeschrock 3332*990b4856Slling /* 3333*990b4856Slling * Set zpool properties. 3334*990b4856Slling */ 3335b1b8ab34Slling static void 3336ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 3337b1b8ab34Slling { 3338b1b8ab34Slling spa_t *spa = arg1; 3339b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 3340*990b4856Slling nvlist_t *nvp = arg2; 3341*990b4856Slling nvpair_t *elem; 33423d7072f8Seschrock uint64_t intval; 3343*990b4856Slling char *strval; 3344*990b4856Slling zpool_prop_t prop; 3345*990b4856Slling const char *propname; 3346*990b4856Slling zprop_type_t proptype; 3347b1b8ab34Slling 3348*990b4856Slling elem = NULL; 3349*990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 3350*990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 3351*990b4856Slling case ZPOOL_PROP_VERSION: 3352*990b4856Slling /* 3353*990b4856Slling * Only set version for non-zpool-creation cases 3354*990b4856Slling * (set/import). spa_create() needs special care 3355*990b4856Slling * for version setting. 3356*990b4856Slling */ 3357*990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 3358*990b4856Slling VERIFY(nvpair_value_uint64(elem, 3359*990b4856Slling &intval) == 0); 3360*990b4856Slling ASSERT(intval <= SPA_VERSION); 3361*990b4856Slling ASSERT(intval >= spa_version(spa)); 3362*990b4856Slling spa->spa_uberblock.ub_version = intval; 3363*990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 3364*990b4856Slling } 3365ecd6cf80Smarks break; 3366*990b4856Slling 3367*990b4856Slling case ZPOOL_PROP_ALTROOT: 3368*990b4856Slling /* 3369*990b4856Slling * 'altroot' is a non-persistent property. It should 3370*990b4856Slling * have been set temporarily at creation or import time. 3371*990b4856Slling */ 3372*990b4856Slling ASSERT(spa->spa_root != NULL); 3373b1b8ab34Slling break; 33743d7072f8Seschrock 3375*990b4856Slling case ZPOOL_PROP_TEMPORARY: 3376*990b4856Slling /* 3377*990b4856Slling * 'temporary' is a non-persistant property. 3378*990b4856Slling */ 3379*990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 3380*990b4856Slling spa->spa_temporary = intval; 33813d7072f8Seschrock break; 3382*990b4856Slling 3383*990b4856Slling default: 3384*990b4856Slling /* 3385*990b4856Slling * Set pool property values in the poolprops mos object. 3386*990b4856Slling */ 3387*990b4856Slling mutex_enter(&spa->spa_props_lock); 3388*990b4856Slling if (spa->spa_pool_props_object == 0) { 3389*990b4856Slling objset_t *mos = spa->spa_meta_objset; 3390*990b4856Slling 3391*990b4856Slling VERIFY((spa->spa_pool_props_object = 3392*990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 3393*990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 3394*990b4856Slling 3395*990b4856Slling VERIFY(zap_update(mos, 3396*990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 3397*990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 3398*990b4856Slling == 0); 3399*990b4856Slling } 3400*990b4856Slling mutex_exit(&spa->spa_props_lock); 3401*990b4856Slling 3402*990b4856Slling /* normalize the property name */ 3403*990b4856Slling propname = zpool_prop_to_name(prop); 3404*990b4856Slling proptype = zpool_prop_get_type(prop); 3405*990b4856Slling 3406*990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 3407*990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 3408*990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 3409*990b4856Slling VERIFY(zap_update(mos, 3410*990b4856Slling spa->spa_pool_props_object, propname, 3411*990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 3412*990b4856Slling 3413*990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 3414*990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 3415*990b4856Slling 3416*990b4856Slling if (proptype == PROP_TYPE_INDEX) { 3417*990b4856Slling const char *unused; 3418*990b4856Slling VERIFY(zpool_prop_index_to_string( 3419*990b4856Slling prop, intval, &unused) == 0); 3420*990b4856Slling } 3421*990b4856Slling VERIFY(zap_update(mos, 3422*990b4856Slling spa->spa_pool_props_object, propname, 3423*990b4856Slling 8, 1, &intval, tx) == 0); 3424*990b4856Slling } else { 3425*990b4856Slling ASSERT(0); /* not allowed */ 3426*990b4856Slling } 3427*990b4856Slling 3428*990b4856Slling if (prop == ZPOOL_PROP_DELEGATION) 3429*990b4856Slling spa->spa_delegation = intval; 3430*990b4856Slling 3431*990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) 3432*990b4856Slling spa->spa_bootfs = intval; 3433*990b4856Slling } 3434*990b4856Slling 3435*990b4856Slling /* log internal history if this is not a zpool create */ 3436*990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 3437*990b4856Slling tx->tx_txg != TXG_INITIAL) { 3438*990b4856Slling spa_history_internal_log(LOG_POOL_PROPSET, 3439*990b4856Slling spa, tx, cr, "%s %lld %s", 3440*990b4856Slling nvpair_name(elem), intval, spa->spa_name); 3441b1b8ab34Slling } 3442b1b8ab34Slling } 3443b1b8ab34Slling } 3444b1b8ab34Slling 3445fa9e4066Sahrens /* 3446fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3447fa9e4066Sahrens * part of the process, so we iterate until it converges. 3448fa9e4066Sahrens */ 3449fa9e4066Sahrens void 3450fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 3451fa9e4066Sahrens { 3452fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3453fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 3454fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 34550373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 3456fa9e4066Sahrens vdev_t *vd; 3457fa9e4066Sahrens dmu_tx_t *tx; 3458fa9e4066Sahrens int dirty_vdevs; 3459fa9e4066Sahrens 3460fa9e4066Sahrens /* 3461fa9e4066Sahrens * Lock out configuration changes. 3462fa9e4066Sahrens */ 3463ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 3464fa9e4066Sahrens 3465fa9e4066Sahrens spa->spa_syncing_txg = txg; 3466fa9e4066Sahrens spa->spa_sync_pass = 0; 3467fa9e4066Sahrens 3468ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3469fa9e4066Sahrens 347099653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 347199653d4eSeschrock 347299653d4eSeschrock /* 3473e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 347499653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 347599653d4eSeschrock */ 3476e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 3477e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 347899653d4eSeschrock int i; 347999653d4eSeschrock 348099653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 348199653d4eSeschrock vd = rvd->vdev_child[i]; 348299653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 348399653d4eSeschrock break; 348499653d4eSeschrock } 348599653d4eSeschrock if (i == rvd->vdev_children) { 348699653d4eSeschrock spa->spa_deflate = TRUE; 348799653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 348899653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 348999653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 349099653d4eSeschrock } 349199653d4eSeschrock } 349299653d4eSeschrock 3493fa9e4066Sahrens /* 3494fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 3495fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 3496fa9e4066Sahrens * don't generate work on an otherwise idle system. 3497fa9e4066Sahrens */ 3498fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 34991615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 35001615a317Sek !txg_list_empty(&dp->dp_sync_tasks, txg)) 3501fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 3502fa9e4066Sahrens 3503fa9e4066Sahrens /* 3504fa9e4066Sahrens * Iterate to convergence. 3505fa9e4066Sahrens */ 3506fa9e4066Sahrens do { 3507fa9e4066Sahrens spa->spa_sync_pass++; 3508fa9e4066Sahrens 3509fa9e4066Sahrens spa_sync_config_object(spa, tx); 351099653d4eSeschrock spa_sync_spares(spa, tx); 3511ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 3512fa9e4066Sahrens dsl_pool_sync(dp, txg); 3513fa9e4066Sahrens 3514fa9e4066Sahrens dirty_vdevs = 0; 3515fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3516fa9e4066Sahrens vdev_sync(vd, txg); 3517fa9e4066Sahrens dirty_vdevs++; 3518fa9e4066Sahrens } 3519fa9e4066Sahrens 3520fa9e4066Sahrens bplist_sync(bpl, tx); 3521fa9e4066Sahrens } while (dirty_vdevs); 3522fa9e4066Sahrens 3523fa9e4066Sahrens bplist_close(bpl); 3524fa9e4066Sahrens 3525fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3526fa9e4066Sahrens 3527fa9e4066Sahrens /* 3528fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3529fa9e4066Sahrens * to commit the transaction group. 35300373e76bSbonwick * 35310373e76bSbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 35320373e76bSbonwick * Otherwise, pick a random top-level vdev that's known to be 35330373e76bSbonwick * visible in the config cache (see spa_vdev_add() for details). 35340373e76bSbonwick * If the write fails, try the next vdev until we're tried them all. 35350373e76bSbonwick */ 35360373e76bSbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 35370373e76bSbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 35380373e76bSbonwick } else { 35390373e76bSbonwick int children = rvd->vdev_children; 35400373e76bSbonwick int c0 = spa_get_random(children); 35410373e76bSbonwick int c; 35420373e76bSbonwick 35430373e76bSbonwick for (c = 0; c < children; c++) { 35440373e76bSbonwick vd = rvd->vdev_child[(c0 + c) % children]; 35450373e76bSbonwick if (vd->vdev_ms_array == 0) 35460373e76bSbonwick continue; 35470373e76bSbonwick if (vdev_config_sync(vd, txg) == 0) 35480373e76bSbonwick break; 35490373e76bSbonwick } 35500373e76bSbonwick if (c == children) 35510373e76bSbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 35520373e76bSbonwick } 35530373e76bSbonwick 355499653d4eSeschrock dmu_tx_commit(tx); 355599653d4eSeschrock 35560373e76bSbonwick /* 35570373e76bSbonwick * Clear the dirty config list. 3558fa9e4066Sahrens */ 35590373e76bSbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 35600373e76bSbonwick vdev_config_clean(vd); 35610373e76bSbonwick 35620373e76bSbonwick /* 35630373e76bSbonwick * Now that the new config has synced transactionally, 35640373e76bSbonwick * let it become visible to the config cache. 35650373e76bSbonwick */ 35660373e76bSbonwick if (spa->spa_config_syncing != NULL) { 35670373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 35680373e76bSbonwick spa->spa_config_txg = txg; 35690373e76bSbonwick spa->spa_config_syncing = NULL; 35700373e76bSbonwick } 3571fa9e4066Sahrens 3572fa9e4066Sahrens /* 3573fa9e4066Sahrens * Make a stable copy of the fully synced uberblock. 3574fa9e4066Sahrens * We use this as the root for pool traversals. 3575fa9e4066Sahrens */ 3576fa9e4066Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3577fa9e4066Sahrens 3578fa9e4066Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3579fa9e4066Sahrens 3580fa9e4066Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3581fa9e4066Sahrens spa->spa_traverse_wanted = 0; 3582fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 3583fa9e4066Sahrens rw_exit(&spa->spa_traverse_lock); 3584fa9e4066Sahrens 3585fa9e4066Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3586fa9e4066Sahrens 3587fa9e4066Sahrens /* 3588fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 3589fa9e4066Sahrens */ 3590fa9e4066Sahrens dsl_pool_zil_clean(dp); 3591fa9e4066Sahrens 3592fa9e4066Sahrens /* 3593fa9e4066Sahrens * Update usable space statistics. 3594fa9e4066Sahrens */ 3595fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3596fa9e4066Sahrens vdev_sync_done(vd, txg); 3597fa9e4066Sahrens 3598fa9e4066Sahrens /* 3599fa9e4066Sahrens * It had better be the case that we didn't dirty anything 360099653d4eSeschrock * since vdev_config_sync(). 3601fa9e4066Sahrens */ 3602fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3603fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3604fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3605fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 3606fa9e4066Sahrens 3607ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3608ea8dc4b6Seschrock 3609ea8dc4b6Seschrock /* 3610ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 3611ea8dc4b6Seschrock */ 3612ea8dc4b6Seschrock spa_async_dispatch(spa); 3613fa9e4066Sahrens } 3614fa9e4066Sahrens 3615fa9e4066Sahrens /* 3616fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3617fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3618fa9e4066Sahrens * sync. 3619fa9e4066Sahrens */ 3620fa9e4066Sahrens void 3621fa9e4066Sahrens spa_sync_allpools(void) 3622fa9e4066Sahrens { 3623fa9e4066Sahrens spa_t *spa = NULL; 3624fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3625fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 3626fa9e4066Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3627fa9e4066Sahrens continue; 3628fa9e4066Sahrens spa_open_ref(spa, FTAG); 3629fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3630fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3631fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3632fa9e4066Sahrens spa_close(spa, FTAG); 3633fa9e4066Sahrens } 3634fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3635fa9e4066Sahrens } 3636fa9e4066Sahrens 3637fa9e4066Sahrens /* 3638fa9e4066Sahrens * ========================================================================== 3639fa9e4066Sahrens * Miscellaneous routines 3640fa9e4066Sahrens * ========================================================================== 3641fa9e4066Sahrens */ 3642fa9e4066Sahrens 3643fa9e4066Sahrens /* 3644fa9e4066Sahrens * Remove all pools in the system. 3645fa9e4066Sahrens */ 3646fa9e4066Sahrens void 3647fa9e4066Sahrens spa_evict_all(void) 3648fa9e4066Sahrens { 3649fa9e4066Sahrens spa_t *spa; 3650fa9e4066Sahrens 3651fa9e4066Sahrens /* 3652fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 3653fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 3654fa9e4066Sahrens */ 3655fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3656fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 3657fa9e4066Sahrens /* 3658ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 3659ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 3660ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 3661fa9e4066Sahrens */ 3662fa9e4066Sahrens spa_open_ref(spa, FTAG); 3663fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3664ea8dc4b6Seschrock spa_async_suspend(spa); 3665fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3666bb8b5132Sek VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3667fa9e4066Sahrens spa_close(spa, FTAG); 3668fa9e4066Sahrens 3669fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3670fa9e4066Sahrens spa_unload(spa); 3671fa9e4066Sahrens spa_deactivate(spa); 3672fa9e4066Sahrens } 3673fa9e4066Sahrens spa_remove(spa); 3674fa9e4066Sahrens } 3675fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3676fa9e4066Sahrens } 3677ea8dc4b6Seschrock 3678ea8dc4b6Seschrock vdev_t * 3679ea8dc4b6Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 3680ea8dc4b6Seschrock { 3681ea8dc4b6Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 3682ea8dc4b6Seschrock } 3683eaca9bbdSeschrock 3684eaca9bbdSeschrock void 3685*990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 3686eaca9bbdSeschrock { 3687eaca9bbdSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 3688eaca9bbdSeschrock 3689eaca9bbdSeschrock /* 3690eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 3691eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 3692eaca9bbdSeschrock * possible. 3693eaca9bbdSeschrock */ 3694e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 3695*990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 3696eaca9bbdSeschrock 3697*990b4856Slling spa->spa_uberblock.ub_version = version; 3698eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 3699eaca9bbdSeschrock 3700eaca9bbdSeschrock spa_config_exit(spa, FTAG); 370199653d4eSeschrock 370299653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 370399653d4eSeschrock } 370499653d4eSeschrock 370599653d4eSeschrock boolean_t 370699653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 370799653d4eSeschrock { 370899653d4eSeschrock int i; 370939c23413Seschrock uint64_t spareguid; 371099653d4eSeschrock 371199653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 371299653d4eSeschrock if (spa->spa_spares[i]->vdev_guid == guid) 371399653d4eSeschrock return (B_TRUE); 371499653d4eSeschrock 371539c23413Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 371639c23413Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 371739c23413Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 371839c23413Seschrock spareguid == guid) 371939c23413Seschrock return (B_TRUE); 372039c23413Seschrock } 372139c23413Seschrock 372299653d4eSeschrock return (B_FALSE); 3723eaca9bbdSeschrock } 3724b1b8ab34Slling 37253d7072f8Seschrock /* 37263d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 37273d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 37283d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 37293d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 37303d7072f8Seschrock * or zdb as real changes. 37313d7072f8Seschrock */ 37323d7072f8Seschrock void 37333d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 37343d7072f8Seschrock { 37353d7072f8Seschrock #ifdef _KERNEL 37363d7072f8Seschrock sysevent_t *ev; 37373d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 37383d7072f8Seschrock sysevent_value_t value; 37393d7072f8Seschrock sysevent_id_t eid; 37403d7072f8Seschrock 37413d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 37423d7072f8Seschrock SE_SLEEP); 37433d7072f8Seschrock 37443d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 37453d7072f8Seschrock value.value.sv_string = spa_name(spa); 37463d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 37473d7072f8Seschrock goto done; 37483d7072f8Seschrock 37493d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 37503d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 37513d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 37523d7072f8Seschrock goto done; 37533d7072f8Seschrock 37543d7072f8Seschrock if (vd) { 37553d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 37563d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 37573d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 37583d7072f8Seschrock SE_SLEEP) != 0) 37593d7072f8Seschrock goto done; 37603d7072f8Seschrock 37613d7072f8Seschrock if (vd->vdev_path) { 37623d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 37633d7072f8Seschrock value.value.sv_string = vd->vdev_path; 37643d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 37653d7072f8Seschrock &value, SE_SLEEP) != 0) 37663d7072f8Seschrock goto done; 37673d7072f8Seschrock } 37683d7072f8Seschrock } 37693d7072f8Seschrock 37703d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 37713d7072f8Seschrock 37723d7072f8Seschrock done: 37733d7072f8Seschrock if (attr) 37743d7072f8Seschrock sysevent_free_attr(attr); 37753d7072f8Seschrock sysevent_free(ev); 37763d7072f8Seschrock #endif 37773d7072f8Seschrock } 3778