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 63990b4856Slling #include "zfs_prop.h" 64990b4856Slling 65416e0cd8Sek int zio_taskq_threads = 8; 66416e0cd8Sek 67990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 68990b4856Slling 69990b4856Slling /* 70990b4856Slling * ========================================================================== 71990b4856Slling * SPA properties routines 72990b4856Slling * ========================================================================== 73990b4856Slling */ 74990b4856Slling 75990b4856Slling /* 76990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 77990b4856Slling */ 78990b4856Slling static int 79990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 80990b4856Slling uint64_t intval, zprop_source_t src) 81990b4856Slling { 82990b4856Slling const char *propname = zpool_prop_to_name(prop); 83990b4856Slling nvlist_t *propval; 84990b4856Slling int err = 0; 85990b4856Slling 86990b4856Slling if (err = nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP)) 87990b4856Slling return (err); 88990b4856Slling 89990b4856Slling if (err = nvlist_add_uint64(propval, ZPROP_SOURCE, src)) 90990b4856Slling goto out; 91990b4856Slling 92990b4856Slling if (strval != NULL) { 93990b4856Slling if (err = nvlist_add_string(propval, ZPROP_VALUE, strval)) 94990b4856Slling goto out; 95990b4856Slling } else { 96990b4856Slling if (err = nvlist_add_uint64(propval, ZPROP_VALUE, intval)) 97990b4856Slling goto out; 98990b4856Slling } 99990b4856Slling 100990b4856Slling err = nvlist_add_nvlist(nvl, propname, propval); 101990b4856Slling out: 102990b4856Slling nvlist_free(propval); 103990b4856Slling return (err); 104990b4856Slling } 105990b4856Slling 106990b4856Slling /* 107990b4856Slling * Get property values from the spa configuration. 108990b4856Slling */ 109990b4856Slling static int 110990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 111990b4856Slling { 112990b4856Slling uint64_t size = spa_get_space(spa); 113990b4856Slling uint64_t used = spa_get_alloc(spa); 114990b4856Slling uint64_t cap, version; 115990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 116990b4856Slling int err; 117*2f8aaab3Seschrock char *cachefile; 118*2f8aaab3Seschrock size_t len; 119990b4856Slling 120990b4856Slling /* 121990b4856Slling * readonly properties 122990b4856Slling */ 123990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 124990b4856Slling 0, src)) 125990b4856Slling return (err); 126990b4856Slling 127990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src)) 128990b4856Slling return (err); 129990b4856Slling 130990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src)) 131990b4856Slling return (err); 132990b4856Slling 133990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, 134990b4856Slling size - used, src)) 135990b4856Slling return (err); 136990b4856Slling 137990b4856Slling cap = (size == 0) ? 0 : (used * 100 / size); 138990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src)) 139990b4856Slling return (err); 140990b4856Slling 141990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, 142990b4856Slling spa_guid(spa), src)) 143990b4856Slling return (err); 144990b4856Slling 145990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 146990b4856Slling spa->spa_root_vdev->vdev_state, src)) 147990b4856Slling return (err); 148990b4856Slling 149990b4856Slling /* 150990b4856Slling * settable properties that are not stored in the pool property object. 151990b4856Slling */ 152990b4856Slling version = spa_version(spa); 153990b4856Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 154990b4856Slling src = ZPROP_SRC_DEFAULT; 155990b4856Slling else 156990b4856Slling src = ZPROP_SRC_LOCAL; 157990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, 158990b4856Slling version, src)) 159990b4856Slling return (err); 160990b4856Slling 161990b4856Slling if (spa->spa_root != NULL) { 162990b4856Slling src = ZPROP_SRC_LOCAL; 163990b4856Slling if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, 164990b4856Slling spa->spa_root, 0, src)) 165990b4856Slling return (err); 166990b4856Slling } 167990b4856Slling 168*2f8aaab3Seschrock if (spa->spa_config_dir != NULL) { 169*2f8aaab3Seschrock if (strcmp(spa->spa_config_dir, "none") == 0) { 170*2f8aaab3Seschrock err = spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 171*2f8aaab3Seschrock spa->spa_config_dir, 0, ZPROP_SRC_LOCAL); 172*2f8aaab3Seschrock } else { 173*2f8aaab3Seschrock len = strlen(spa->spa_config_dir) + 174*2f8aaab3Seschrock strlen(spa->spa_config_file) + 2; 175*2f8aaab3Seschrock cachefile = kmem_alloc(len, KM_SLEEP); 176*2f8aaab3Seschrock (void) snprintf(cachefile, len, "%s/%s", 177*2f8aaab3Seschrock spa->spa_config_dir, spa->spa_config_file); 178*2f8aaab3Seschrock err = spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 179*2f8aaab3Seschrock cachefile, 0, ZPROP_SRC_LOCAL); 180*2f8aaab3Seschrock kmem_free(cachefile, len); 181*2f8aaab3Seschrock } 182*2f8aaab3Seschrock 183*2f8aaab3Seschrock if (err) 184*2f8aaab3Seschrock return (err); 185*2f8aaab3Seschrock } 186990b4856Slling 187990b4856Slling return (0); 188990b4856Slling } 189990b4856Slling 190990b4856Slling /* 191990b4856Slling * Get zpool property values. 192990b4856Slling */ 193990b4856Slling int 194990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 195990b4856Slling { 196990b4856Slling zap_cursor_t zc; 197990b4856Slling zap_attribute_t za; 198990b4856Slling objset_t *mos = spa->spa_meta_objset; 199990b4856Slling int err; 200990b4856Slling 201990b4856Slling if (err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)) 202990b4856Slling return (err); 203990b4856Slling 204990b4856Slling /* 205990b4856Slling * Get properties from the spa config. 206990b4856Slling */ 207990b4856Slling if (err = spa_prop_get_config(spa, nvp)) 208990b4856Slling goto out; 209990b4856Slling 210990b4856Slling mutex_enter(&spa->spa_props_lock); 211990b4856Slling /* If no pool property object, no more prop to get. */ 212990b4856Slling if (spa->spa_pool_props_object == 0) { 213990b4856Slling mutex_exit(&spa->spa_props_lock); 214990b4856Slling return (0); 215990b4856Slling } 216990b4856Slling 217990b4856Slling /* 218990b4856Slling * Get properties from the MOS pool property object. 219990b4856Slling */ 220990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 221990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 222990b4856Slling zap_cursor_advance(&zc)) { 223990b4856Slling uint64_t intval = 0; 224990b4856Slling char *strval = NULL; 225990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 226990b4856Slling zpool_prop_t prop; 227990b4856Slling 228990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 229990b4856Slling continue; 230990b4856Slling 231990b4856Slling switch (za.za_integer_length) { 232990b4856Slling case 8: 233990b4856Slling /* integer property */ 234990b4856Slling if (za.za_first_integer != 235990b4856Slling zpool_prop_default_numeric(prop)) 236990b4856Slling src = ZPROP_SRC_LOCAL; 237990b4856Slling 238990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 239990b4856Slling dsl_pool_t *dp; 240990b4856Slling dsl_dataset_t *ds = NULL; 241990b4856Slling 242990b4856Slling dp = spa_get_dsl(spa); 243990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 244990b4856Slling if (err = dsl_dataset_open_obj(dp, 245990b4856Slling za.za_first_integer, NULL, DS_MODE_NONE, 246990b4856Slling FTAG, &ds)) { 247990b4856Slling rw_exit(&dp->dp_config_rwlock); 248990b4856Slling break; 249990b4856Slling } 250990b4856Slling 251990b4856Slling strval = kmem_alloc( 252990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 253990b4856Slling KM_SLEEP); 254990b4856Slling dsl_dataset_name(ds, strval); 255990b4856Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 256990b4856Slling rw_exit(&dp->dp_config_rwlock); 257990b4856Slling } else { 258990b4856Slling strval = NULL; 259990b4856Slling intval = za.za_first_integer; 260990b4856Slling } 261990b4856Slling 262990b4856Slling err = spa_prop_add_list(*nvp, prop, strval, 263990b4856Slling intval, src); 264990b4856Slling 265990b4856Slling if (strval != NULL) 266990b4856Slling kmem_free(strval, 267990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 268990b4856Slling 269990b4856Slling break; 270990b4856Slling 271990b4856Slling case 1: 272990b4856Slling /* string property */ 273990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 274990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 275990b4856Slling za.za_name, 1, za.za_num_integers, strval); 276990b4856Slling if (err) { 277990b4856Slling kmem_free(strval, za.za_num_integers); 278990b4856Slling break; 279990b4856Slling } 280990b4856Slling err = spa_prop_add_list(*nvp, prop, strval, 0, src); 281990b4856Slling kmem_free(strval, za.za_num_integers); 282990b4856Slling break; 283990b4856Slling 284990b4856Slling default: 285990b4856Slling break; 286990b4856Slling } 287990b4856Slling } 288990b4856Slling zap_cursor_fini(&zc); 289990b4856Slling mutex_exit(&spa->spa_props_lock); 290990b4856Slling out: 291990b4856Slling if (err && err != ENOENT) { 292990b4856Slling nvlist_free(*nvp); 293990b4856Slling return (err); 294990b4856Slling } 295990b4856Slling 296990b4856Slling return (0); 297990b4856Slling } 298990b4856Slling 299990b4856Slling /* 300990b4856Slling * Validate the given pool properties nvlist and modify the list 301990b4856Slling * for the property values to be set. 302990b4856Slling */ 303990b4856Slling static int 304990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 305990b4856Slling { 306990b4856Slling nvpair_t *elem; 307990b4856Slling int error = 0, reset_bootfs = 0; 308990b4856Slling uint64_t objnum; 309990b4856Slling 310990b4856Slling elem = NULL; 311990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 312990b4856Slling zpool_prop_t prop; 313990b4856Slling char *propname, *strval; 314990b4856Slling uint64_t intval; 315990b4856Slling vdev_t *rvdev; 316990b4856Slling char *vdev_type; 317990b4856Slling objset_t *os; 318*2f8aaab3Seschrock char *slash; 319990b4856Slling 320990b4856Slling propname = nvpair_name(elem); 321990b4856Slling 322990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 323990b4856Slling return (EINVAL); 324990b4856Slling 325990b4856Slling switch (prop) { 326990b4856Slling case ZPOOL_PROP_VERSION: 327990b4856Slling error = nvpair_value_uint64(elem, &intval); 328990b4856Slling if (!error && 329990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 330990b4856Slling error = EINVAL; 331990b4856Slling break; 332990b4856Slling 333990b4856Slling case ZPOOL_PROP_DELEGATION: 334990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 335990b4856Slling error = nvpair_value_uint64(elem, &intval); 336990b4856Slling if (!error && intval > 1) 337990b4856Slling error = EINVAL; 338990b4856Slling break; 339990b4856Slling 340990b4856Slling case ZPOOL_PROP_BOOTFS: 341990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 342990b4856Slling error = ENOTSUP; 343990b4856Slling break; 344990b4856Slling } 345990b4856Slling 346990b4856Slling /* 347990b4856Slling * A bootable filesystem can not be on a RAIDZ pool 348990b4856Slling * nor a striped pool with more than 1 device. 349990b4856Slling */ 350990b4856Slling rvdev = spa->spa_root_vdev; 351990b4856Slling vdev_type = 352990b4856Slling rvdev->vdev_child[0]->vdev_ops->vdev_op_type; 353990b4856Slling if (rvdev->vdev_children > 1 || 354990b4856Slling strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 355990b4856Slling strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 356990b4856Slling error = ENOTSUP; 357990b4856Slling break; 358990b4856Slling } 359990b4856Slling 360990b4856Slling reset_bootfs = 1; 361990b4856Slling 362990b4856Slling error = nvpair_value_string(elem, &strval); 363990b4856Slling 364990b4856Slling if (!error) { 365990b4856Slling if (strval == NULL || strval[0] == '\0') { 366990b4856Slling objnum = zpool_prop_default_numeric( 367990b4856Slling ZPOOL_PROP_BOOTFS); 368990b4856Slling break; 369990b4856Slling } 370990b4856Slling 371990b4856Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 372990b4856Slling DS_MODE_STANDARD | DS_MODE_READONLY, &os)) 373990b4856Slling break; 374990b4856Slling objnum = dmu_objset_id(os); 375990b4856Slling dmu_objset_close(os); 376990b4856Slling } 377990b4856Slling break; 3780a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 3790a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 3800a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3810a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 3820a4e9518Sgw error = EINVAL; 3830a4e9518Sgw 3840a4e9518Sgw /* 3850a4e9518Sgw * This is a special case which only occurs when 3860a4e9518Sgw * the pool has completely failed. This allows 3870a4e9518Sgw * the user to change the in-core failmode property 3880a4e9518Sgw * without syncing it out to disk (I/Os might 3890a4e9518Sgw * currently be blocked). We do this by returning 3900a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 3910a4e9518Sgw * into thinking we encountered a property validation 3920a4e9518Sgw * error. 3930a4e9518Sgw */ 3940a4e9518Sgw if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3950a4e9518Sgw spa->spa_failmode = intval; 3960a4e9518Sgw error = EIO; 3970a4e9518Sgw } 3980a4e9518Sgw break; 399*2f8aaab3Seschrock 400*2f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 401*2f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 402*2f8aaab3Seschrock break; 403*2f8aaab3Seschrock 404*2f8aaab3Seschrock if (strval[0] == '\0') 405*2f8aaab3Seschrock break; 406*2f8aaab3Seschrock 407*2f8aaab3Seschrock if (strcmp(strval, "none") == 0) 408*2f8aaab3Seschrock break; 409*2f8aaab3Seschrock 410*2f8aaab3Seschrock if (strval[0] != '/') { 411*2f8aaab3Seschrock error = EINVAL; 412*2f8aaab3Seschrock break; 413*2f8aaab3Seschrock } 414*2f8aaab3Seschrock 415*2f8aaab3Seschrock slash = strrchr(strval, '/'); 416*2f8aaab3Seschrock ASSERT(slash != NULL); 417*2f8aaab3Seschrock 418*2f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 419*2f8aaab3Seschrock strcmp(slash, "/..") == 0) 420*2f8aaab3Seschrock error = EINVAL; 421*2f8aaab3Seschrock break; 422990b4856Slling } 423990b4856Slling 424990b4856Slling if (error) 425990b4856Slling break; 426990b4856Slling } 427990b4856Slling 428990b4856Slling if (!error && reset_bootfs) { 429990b4856Slling error = nvlist_remove(props, 430990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 431990b4856Slling 432990b4856Slling if (!error) { 433990b4856Slling error = nvlist_add_uint64(props, 434990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 435990b4856Slling } 436990b4856Slling } 437990b4856Slling 438990b4856Slling return (error); 439990b4856Slling } 440990b4856Slling 441990b4856Slling int 442990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 443990b4856Slling { 444990b4856Slling int error; 445990b4856Slling 446990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 447990b4856Slling return (error); 448990b4856Slling 449990b4856Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 450990b4856Slling spa, nvp, 3)); 451990b4856Slling } 452990b4856Slling 453990b4856Slling /* 454990b4856Slling * If the bootfs property value is dsobj, clear it. 455990b4856Slling */ 456990b4856Slling void 457990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 458990b4856Slling { 459990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 460990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 461990b4856Slling spa->spa_pool_props_object, 462990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 463990b4856Slling spa->spa_bootfs = 0; 464990b4856Slling } 465990b4856Slling } 466990b4856Slling 467fa9e4066Sahrens /* 468fa9e4066Sahrens * ========================================================================== 469fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 470fa9e4066Sahrens * ========================================================================== 471fa9e4066Sahrens */ 472fa9e4066Sahrens 473ea8dc4b6Seschrock static int 474ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 475ea8dc4b6Seschrock { 476ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 477ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 478ea8dc4b6Seschrock int ret; 479ea8dc4b6Seschrock 480ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 481ea8dc4b6Seschrock sizeof (zbookmark_t)); 482ea8dc4b6Seschrock 483ea8dc4b6Seschrock if (ret < 0) 484ea8dc4b6Seschrock return (-1); 485ea8dc4b6Seschrock else if (ret > 0) 486ea8dc4b6Seschrock return (1); 487ea8dc4b6Seschrock else 488ea8dc4b6Seschrock return (0); 489ea8dc4b6Seschrock } 490ea8dc4b6Seschrock 491ea8dc4b6Seschrock /* 492ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 493ea8dc4b6Seschrock * re-initializes them in the process. 494ea8dc4b6Seschrock */ 495ea8dc4b6Seschrock void 496ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 497ea8dc4b6Seschrock { 498ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 499ea8dc4b6Seschrock 500ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 501ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 502ea8dc4b6Seschrock 503ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 504ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 505ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 506ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 507ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 508ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 509ea8dc4b6Seschrock } 510ea8dc4b6Seschrock 511fa9e4066Sahrens /* 512fa9e4066Sahrens * Activate an uninitialized pool. 513fa9e4066Sahrens */ 514fa9e4066Sahrens static void 515fa9e4066Sahrens spa_activate(spa_t *spa) 516fa9e4066Sahrens { 517fa9e4066Sahrens int t; 518fa9e4066Sahrens 519fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 520fa9e4066Sahrens 521fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 522fa9e4066Sahrens 523fa9e4066Sahrens spa->spa_normal_class = metaslab_class_create(); 5248654d025Sperrin spa->spa_log_class = metaslab_class_create(); 525fa9e4066Sahrens 526fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 527fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 528416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 529fa9e4066Sahrens TASKQ_PREPOPULATE); 530fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 531416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 532fa9e4066Sahrens TASKQ_PREPOPULATE); 533fa9e4066Sahrens } 534fa9e4066Sahrens 535fa9e4066Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 536fa9e4066Sahrens offsetof(vdev_t, vdev_dirty_node)); 5370a4e9518Sgw list_create(&spa->spa_zio_list, sizeof (zio_t), 5380a4e9518Sgw offsetof(zio_t, zio_link_node)); 539fa9e4066Sahrens 540fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 541fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 542ea8dc4b6Seschrock 543ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 544ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 545ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 546ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 547ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 548ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 549fa9e4066Sahrens } 550fa9e4066Sahrens 551fa9e4066Sahrens /* 552fa9e4066Sahrens * Opposite of spa_activate(). 553fa9e4066Sahrens */ 554fa9e4066Sahrens static void 555fa9e4066Sahrens spa_deactivate(spa_t *spa) 556fa9e4066Sahrens { 557fa9e4066Sahrens int t; 558fa9e4066Sahrens 559fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 560fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 561fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 562fa9e4066Sahrens 563fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 564fa9e4066Sahrens 565fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 566fa9e4066Sahrens 567fa9e4066Sahrens list_destroy(&spa->spa_dirty_list); 5680a4e9518Sgw list_destroy(&spa->spa_zio_list); 569fa9e4066Sahrens 570fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 571fa9e4066Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 572fa9e4066Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 573fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = NULL; 574fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = NULL; 575fa9e4066Sahrens } 576fa9e4066Sahrens 577fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 578fa9e4066Sahrens spa->spa_normal_class = NULL; 579fa9e4066Sahrens 5808654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 5818654d025Sperrin spa->spa_log_class = NULL; 5828654d025Sperrin 583ea8dc4b6Seschrock /* 584ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 585ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 586ea8dc4b6Seschrock */ 587ea8dc4b6Seschrock spa_errlog_drain(spa); 588ea8dc4b6Seschrock 589ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 590ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 591ea8dc4b6Seschrock 592fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 593fa9e4066Sahrens } 594fa9e4066Sahrens 595fa9e4066Sahrens /* 596fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 597fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 598fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 599fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 600fa9e4066Sahrens */ 60199653d4eSeschrock static int 60299653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 60399653d4eSeschrock uint_t id, int atype) 604fa9e4066Sahrens { 605fa9e4066Sahrens nvlist_t **child; 606fa9e4066Sahrens uint_t c, children; 60799653d4eSeschrock int error; 608fa9e4066Sahrens 60999653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 61099653d4eSeschrock return (error); 611fa9e4066Sahrens 61299653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 61399653d4eSeschrock return (0); 614fa9e4066Sahrens 615fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 616fa9e4066Sahrens &child, &children) != 0) { 61799653d4eSeschrock vdev_free(*vdp); 61899653d4eSeschrock *vdp = NULL; 61999653d4eSeschrock return (EINVAL); 620fa9e4066Sahrens } 621fa9e4066Sahrens 622fa9e4066Sahrens for (c = 0; c < children; c++) { 62399653d4eSeschrock vdev_t *vd; 62499653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 62599653d4eSeschrock atype)) != 0) { 62699653d4eSeschrock vdev_free(*vdp); 62799653d4eSeschrock *vdp = NULL; 62899653d4eSeschrock return (error); 629fa9e4066Sahrens } 630fa9e4066Sahrens } 631fa9e4066Sahrens 63299653d4eSeschrock ASSERT(*vdp != NULL); 63399653d4eSeschrock 63499653d4eSeschrock return (0); 635fa9e4066Sahrens } 636fa9e4066Sahrens 637fa9e4066Sahrens /* 638fa9e4066Sahrens * Opposite of spa_load(). 639fa9e4066Sahrens */ 640fa9e4066Sahrens static void 641fa9e4066Sahrens spa_unload(spa_t *spa) 642fa9e4066Sahrens { 64399653d4eSeschrock int i; 64499653d4eSeschrock 645ea8dc4b6Seschrock /* 646ea8dc4b6Seschrock * Stop async tasks. 647ea8dc4b6Seschrock */ 648ea8dc4b6Seschrock spa_async_suspend(spa); 649ea8dc4b6Seschrock 650fa9e4066Sahrens /* 651fa9e4066Sahrens * Stop syncing. 652fa9e4066Sahrens */ 653fa9e4066Sahrens if (spa->spa_sync_on) { 654fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 655fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 656fa9e4066Sahrens } 657fa9e4066Sahrens 658fa9e4066Sahrens /* 659fa9e4066Sahrens * Wait for any outstanding prefetch I/O to complete. 660fa9e4066Sahrens */ 661ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 662ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 663fa9e4066Sahrens 664fa9e4066Sahrens /* 665fa9e4066Sahrens * Close the dsl pool. 666fa9e4066Sahrens */ 667fa9e4066Sahrens if (spa->spa_dsl_pool) { 668fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 669fa9e4066Sahrens spa->spa_dsl_pool = NULL; 670fa9e4066Sahrens } 671fa9e4066Sahrens 672fa9e4066Sahrens /* 673fa9e4066Sahrens * Close all vdevs. 674fa9e4066Sahrens */ 6750e34b6a7Sbonwick if (spa->spa_root_vdev) 676fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 6770e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 678ea8dc4b6Seschrock 67999653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 68099653d4eSeschrock vdev_free(spa->spa_spares[i]); 68199653d4eSeschrock if (spa->spa_spares) { 68299653d4eSeschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 68399653d4eSeschrock spa->spa_spares = NULL; 68499653d4eSeschrock } 68599653d4eSeschrock if (spa->spa_sparelist) { 68699653d4eSeschrock nvlist_free(spa->spa_sparelist); 68799653d4eSeschrock spa->spa_sparelist = NULL; 68899653d4eSeschrock } 68999653d4eSeschrock 690ea8dc4b6Seschrock spa->spa_async_suspended = 0; 691fa9e4066Sahrens } 692fa9e4066Sahrens 69399653d4eSeschrock /* 69499653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 69599653d4eSeschrock * this pool. When this is called, we have some form of basic information in 69699653d4eSeschrock * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 69799653d4eSeschrock * re-generate a more complete list including status information. 69899653d4eSeschrock */ 69999653d4eSeschrock static void 70099653d4eSeschrock spa_load_spares(spa_t *spa) 70199653d4eSeschrock { 70299653d4eSeschrock nvlist_t **spares; 70399653d4eSeschrock uint_t nspares; 70499653d4eSeschrock int i; 70539c23413Seschrock vdev_t *vd, *tvd; 70699653d4eSeschrock 70799653d4eSeschrock /* 70899653d4eSeschrock * First, close and free any existing spare vdevs. 70999653d4eSeschrock */ 71099653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) { 71139c23413Seschrock vd = spa->spa_spares[i]; 71239c23413Seschrock 71339c23413Seschrock /* Undo the call to spa_activate() below */ 71439c23413Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 71539c23413Seschrock tvd->vdev_isspare) 71639c23413Seschrock spa_spare_remove(tvd); 71739c23413Seschrock vdev_close(vd); 71839c23413Seschrock vdev_free(vd); 71999653d4eSeschrock } 72039c23413Seschrock 72199653d4eSeschrock if (spa->spa_spares) 72299653d4eSeschrock kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 72399653d4eSeschrock 72499653d4eSeschrock if (spa->spa_sparelist == NULL) 72599653d4eSeschrock nspares = 0; 72699653d4eSeschrock else 72799653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 72899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 72999653d4eSeschrock 73099653d4eSeschrock spa->spa_nspares = (int)nspares; 73199653d4eSeschrock spa->spa_spares = NULL; 73299653d4eSeschrock 73399653d4eSeschrock if (nspares == 0) 73499653d4eSeschrock return; 73599653d4eSeschrock 73699653d4eSeschrock /* 73799653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 73839c23413Seschrock * process. For each spare, there is potentially two different vdev_t 73939c23413Seschrock * structures associated with it: one in the list of spares (used only 74039c23413Seschrock * for basic validation purposes) and one in the active vdev 74139c23413Seschrock * configuration (if it's spared in). During this phase we open and 74239c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 74339c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 74499653d4eSeschrock */ 74599653d4eSeschrock spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 74699653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) { 74799653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 74899653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 74999653d4eSeschrock ASSERT(vd != NULL); 75099653d4eSeschrock 75199653d4eSeschrock spa->spa_spares[i] = vd; 75299653d4eSeschrock 75339c23413Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 75439c23413Seschrock if (!tvd->vdev_isspare) 75539c23413Seschrock spa_spare_add(tvd); 75639c23413Seschrock 75739c23413Seschrock /* 75839c23413Seschrock * We only mark the spare active if we were successfully 75939c23413Seschrock * able to load the vdev. Otherwise, importing a pool 76039c23413Seschrock * with a bad active spare would result in strange 76139c23413Seschrock * behavior, because multiple pool would think the spare 76239c23413Seschrock * is actively in use. 76339c23413Seschrock * 76439c23413Seschrock * There is a vulnerability here to an equally bizarre 76539c23413Seschrock * circumstance, where a dead active spare is later 76639c23413Seschrock * brought back to life (onlined or otherwise). Given 76739c23413Seschrock * the rarity of this scenario, and the extra complexity 76839c23413Seschrock * it adds, we ignore the possibility. 76939c23413Seschrock */ 77039c23413Seschrock if (!vdev_is_dead(tvd)) 77139c23413Seschrock spa_spare_activate(tvd); 77239c23413Seschrock } 77339c23413Seschrock 77499653d4eSeschrock if (vdev_open(vd) != 0) 77599653d4eSeschrock continue; 77699653d4eSeschrock 77799653d4eSeschrock vd->vdev_top = vd; 77899653d4eSeschrock (void) vdev_validate_spare(vd); 77999653d4eSeschrock } 78099653d4eSeschrock 78199653d4eSeschrock /* 78299653d4eSeschrock * Recompute the stashed list of spares, with status information 78399653d4eSeschrock * this time. 78499653d4eSeschrock */ 78599653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 78699653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 78799653d4eSeschrock 78899653d4eSeschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 78999653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 79099653d4eSeschrock spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 79199653d4eSeschrock B_TRUE, B_TRUE); 79299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 79399653d4eSeschrock spares, spa->spa_nspares) == 0); 79499653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 79599653d4eSeschrock nvlist_free(spares[i]); 79699653d4eSeschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 79799653d4eSeschrock } 79899653d4eSeschrock 79999653d4eSeschrock static int 80099653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 80199653d4eSeschrock { 80299653d4eSeschrock dmu_buf_t *db; 80399653d4eSeschrock char *packed = NULL; 80499653d4eSeschrock size_t nvsize = 0; 80599653d4eSeschrock int error; 80699653d4eSeschrock *value = NULL; 80799653d4eSeschrock 80899653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 80999653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 81099653d4eSeschrock dmu_buf_rele(db, FTAG); 81199653d4eSeschrock 81299653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 81399653d4eSeschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 81499653d4eSeschrock if (error == 0) 81599653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 81699653d4eSeschrock kmem_free(packed, nvsize); 81799653d4eSeschrock 81899653d4eSeschrock return (error); 81999653d4eSeschrock } 82099653d4eSeschrock 8213d7072f8Seschrock /* 8223d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 8233d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 8243d7072f8Seschrock */ 8253d7072f8Seschrock static void 8263d7072f8Seschrock spa_check_removed(vdev_t *vd) 8273d7072f8Seschrock { 8283d7072f8Seschrock int c; 8293d7072f8Seschrock 8303d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) 8313d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 8323d7072f8Seschrock 8333d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 8343d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 8353d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 8363d7072f8Seschrock } 8373d7072f8Seschrock } 8383d7072f8Seschrock 839fa9e4066Sahrens /* 840fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 841ea8dc4b6Seschrock * source of configuration information. 842fa9e4066Sahrens */ 843fa9e4066Sahrens static int 844ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 845fa9e4066Sahrens { 846fa9e4066Sahrens int error = 0; 847fa9e4066Sahrens nvlist_t *nvroot = NULL; 848fa9e4066Sahrens vdev_t *rvd; 849fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 8500373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 851fa9e4066Sahrens uint64_t pool_guid; 85299653d4eSeschrock uint64_t version; 853fa9e4066Sahrens zio_t *zio; 8543d7072f8Seschrock uint64_t autoreplace = 0; 855fa9e4066Sahrens 856ea8dc4b6Seschrock spa->spa_load_state = state; 8570373e76bSbonwick 858fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 859a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 860ea8dc4b6Seschrock error = EINVAL; 861ea8dc4b6Seschrock goto out; 862ea8dc4b6Seschrock } 863fa9e4066Sahrens 86499653d4eSeschrock /* 86599653d4eSeschrock * Versioning wasn't explicitly added to the label until later, so if 86699653d4eSeschrock * it's not present treat it as the initial version. 86799653d4eSeschrock */ 86899653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 869e7437265Sahrens version = SPA_VERSION_INITIAL; 87099653d4eSeschrock 871a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 872a9926bf0Sbonwick &spa->spa_config_txg); 873a9926bf0Sbonwick 8740373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 875ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 876ea8dc4b6Seschrock error = EEXIST; 877ea8dc4b6Seschrock goto out; 878ea8dc4b6Seschrock } 879fa9e4066Sahrens 880b5989ec7Seschrock spa->spa_load_guid = pool_guid; 881b5989ec7Seschrock 882fa9e4066Sahrens /* 88399653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 88499653d4eSeschrock * value that will be returned by spa_version() since parsing the 88599653d4eSeschrock * configuration requires knowing the version number. 886fa9e4066Sahrens */ 887ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 88899653d4eSeschrock spa->spa_ubsync.ub_version = version; 88999653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 890ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 891fa9e4066Sahrens 89299653d4eSeschrock if (error != 0) 893ea8dc4b6Seschrock goto out; 894fa9e4066Sahrens 8950e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 896fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 897fa9e4066Sahrens 898fa9e4066Sahrens /* 899fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 900fa9e4066Sahrens */ 9010bf246f5Smc error = vdev_open(rvd); 9020bf246f5Smc if (error != 0) 903ea8dc4b6Seschrock goto out; 904fa9e4066Sahrens 905560e6e96Seschrock /* 906560e6e96Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 907560e6e96Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 908560e6e96Seschrock * flag. 909560e6e96Seschrock */ 910560e6e96Seschrock spa_config_enter(spa, RW_READER, FTAG); 911560e6e96Seschrock error = vdev_validate(rvd); 912560e6e96Seschrock spa_config_exit(spa, FTAG); 913560e6e96Seschrock 9140bf246f5Smc if (error != 0) 915560e6e96Seschrock goto out; 916560e6e96Seschrock 917560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 918560e6e96Seschrock error = ENXIO; 919560e6e96Seschrock goto out; 920560e6e96Seschrock } 921560e6e96Seschrock 922fa9e4066Sahrens /* 923fa9e4066Sahrens * Find the best uberblock. 924fa9e4066Sahrens */ 925fa9e4066Sahrens bzero(ub, sizeof (uberblock_t)); 926fa9e4066Sahrens 927fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, 928fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 929fa9e4066Sahrens vdev_uberblock_load(zio, rvd, ub); 930fa9e4066Sahrens error = zio_wait(zio); 931fa9e4066Sahrens 932fa9e4066Sahrens /* 933fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 934fa9e4066Sahrens */ 935fa9e4066Sahrens if (ub->ub_txg == 0) { 936eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 937eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 938ea8dc4b6Seschrock error = ENXIO; 939ea8dc4b6Seschrock goto out; 940ea8dc4b6Seschrock } 941ea8dc4b6Seschrock 942ea8dc4b6Seschrock /* 943ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 944ea8dc4b6Seschrock */ 945e7437265Sahrens if (ub->ub_version > SPA_VERSION) { 946eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 947eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 948ea8dc4b6Seschrock error = ENOTSUP; 949ea8dc4b6Seschrock goto out; 950fa9e4066Sahrens } 951fa9e4066Sahrens 952fa9e4066Sahrens /* 953fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 954fa9e4066Sahrens * incomplete configuration. 955fa9e4066Sahrens */ 956ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 957ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 958ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 959ea8dc4b6Seschrock error = ENXIO; 960ea8dc4b6Seschrock goto out; 961fa9e4066Sahrens } 962fa9e4066Sahrens 963fa9e4066Sahrens /* 964fa9e4066Sahrens * Initialize internal SPA structures. 965fa9e4066Sahrens */ 966fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 967fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 968fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 969ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 970ea8dc4b6Seschrock if (error) { 971ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 972ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 973ea8dc4b6Seschrock goto out; 974ea8dc4b6Seschrock } 975fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 976fa9e4066Sahrens 977ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 978fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 979ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 980ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 981ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 982ea8dc4b6Seschrock error = EIO; 983ea8dc4b6Seschrock goto out; 984ea8dc4b6Seschrock } 985fa9e4066Sahrens 986fa9e4066Sahrens if (!mosconfig) { 98799653d4eSeschrock nvlist_t *newconfig; 98895173954Sek uint64_t hostid; 989fa9e4066Sahrens 99099653d4eSeschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 991ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 992ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 993ea8dc4b6Seschrock error = EIO; 994ea8dc4b6Seschrock goto out; 995ea8dc4b6Seschrock } 996fa9e4066Sahrens 99795173954Sek if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 99895173954Sek &hostid) == 0) { 99995173954Sek char *hostname; 100095173954Sek unsigned long myhostid = 0; 100195173954Sek 100295173954Sek VERIFY(nvlist_lookup_string(newconfig, 100395173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 100495173954Sek 100595173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 100617194a52Slling if (hostid != 0 && myhostid != 0 && 100717194a52Slling (unsigned long)hostid != myhostid) { 100895173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 100995173954Sek "loaded as it was last accessed by " 101095173954Sek "another system (host: %s hostid: 0x%lx). " 101195173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 101295173954Sek spa->spa_name, hostname, 101395173954Sek (unsigned long)hostid); 101495173954Sek error = EBADF; 101595173954Sek goto out; 101695173954Sek } 101795173954Sek } 101895173954Sek 1019fa9e4066Sahrens spa_config_set(spa, newconfig); 1020fa9e4066Sahrens spa_unload(spa); 1021fa9e4066Sahrens spa_deactivate(spa); 1022fa9e4066Sahrens spa_activate(spa); 1023fa9e4066Sahrens 1024ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 1025fa9e4066Sahrens } 1026fa9e4066Sahrens 1027ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1028fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1029ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 1030ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1031ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1032ea8dc4b6Seschrock error = EIO; 1033ea8dc4b6Seschrock goto out; 1034ea8dc4b6Seschrock } 1035fa9e4066Sahrens 103699653d4eSeschrock /* 103799653d4eSeschrock * Load the bit that tells us to use the new accounting function 103899653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 103999653d4eSeschrock * be present. 104099653d4eSeschrock */ 104199653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, 104299653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 104399653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate); 104499653d4eSeschrock if (error != 0 && error != ENOENT) { 104599653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 104699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 104799653d4eSeschrock error = EIO; 104899653d4eSeschrock goto out; 104999653d4eSeschrock } 105099653d4eSeschrock 1051fa9e4066Sahrens /* 1052ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1053ea8dc4b6Seschrock * not be present. 1054fa9e4066Sahrens */ 1055ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1056ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 1057ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 1058d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 1059ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1060ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1061ea8dc4b6Seschrock error = EIO; 1062ea8dc4b6Seschrock goto out; 1063ea8dc4b6Seschrock } 1064ea8dc4b6Seschrock 1065ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1066ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1067ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1068ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 1069ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1070ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1071ea8dc4b6Seschrock error = EIO; 1072ea8dc4b6Seschrock goto out; 1073ea8dc4b6Seschrock } 1074ea8dc4b6Seschrock 107506eeb2adSek /* 107606eeb2adSek * Load the history object. If we have an older pool, this 107706eeb2adSek * will not be present. 107806eeb2adSek */ 107906eeb2adSek error = zap_lookup(spa->spa_meta_objset, 108006eeb2adSek DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 108106eeb2adSek sizeof (uint64_t), 1, &spa->spa_history); 108206eeb2adSek if (error != 0 && error != ENOENT) { 108306eeb2adSek vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 108406eeb2adSek VDEV_AUX_CORRUPT_DATA); 108506eeb2adSek error = EIO; 108606eeb2adSek goto out; 108706eeb2adSek } 108806eeb2adSek 108999653d4eSeschrock /* 109099653d4eSeschrock * Load any hot spares for this pool. 109199653d4eSeschrock */ 109299653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 109399653d4eSeschrock DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 109499653d4eSeschrock if (error != 0 && error != ENOENT) { 109599653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 109699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 109799653d4eSeschrock error = EIO; 109899653d4eSeschrock goto out; 109999653d4eSeschrock } 110099653d4eSeschrock if (error == 0) { 1101e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 110299653d4eSeschrock if (load_nvlist(spa, spa->spa_spares_object, 110399653d4eSeschrock &spa->spa_sparelist) != 0) { 110499653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 110599653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 110699653d4eSeschrock error = EIO; 110799653d4eSeschrock goto out; 110899653d4eSeschrock } 110999653d4eSeschrock 111099653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 111199653d4eSeschrock spa_load_spares(spa); 111299653d4eSeschrock spa_config_exit(spa, FTAG); 111399653d4eSeschrock } 111499653d4eSeschrock 1115990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1116ecd6cf80Smarks 1117b1b8ab34Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1118b1b8ab34Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1119b1b8ab34Slling 1120b1b8ab34Slling if (error && error != ENOENT) { 1121b1b8ab34Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1122b1b8ab34Slling VDEV_AUX_CORRUPT_DATA); 1123b1b8ab34Slling error = EIO; 1124b1b8ab34Slling goto out; 1125b1b8ab34Slling } 1126b1b8ab34Slling 1127b1b8ab34Slling if (error == 0) { 1128b1b8ab34Slling (void) zap_lookup(spa->spa_meta_objset, 1129b1b8ab34Slling spa->spa_pool_props_object, 11303d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1131b1b8ab34Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 11323d7072f8Seschrock (void) zap_lookup(spa->spa_meta_objset, 11333d7072f8Seschrock spa->spa_pool_props_object, 11343d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 11353d7072f8Seschrock sizeof (uint64_t), 1, &autoreplace); 1136ecd6cf80Smarks (void) zap_lookup(spa->spa_meta_objset, 1137ecd6cf80Smarks spa->spa_pool_props_object, 1138ecd6cf80Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1139ecd6cf80Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 11400a4e9518Sgw (void) zap_lookup(spa->spa_meta_objset, 11410a4e9518Sgw spa->spa_pool_props_object, 11420a4e9518Sgw zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 11430a4e9518Sgw sizeof (uint64_t), 1, &spa->spa_failmode); 1144b1b8ab34Slling } 1145b1b8ab34Slling 11463d7072f8Seschrock /* 11473d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 11483d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 11493d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 11503d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 11513d7072f8Seschrock * over. 11523d7072f8Seschrock */ 11533d7072f8Seschrock if (autoreplace) 11543d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 11553d7072f8Seschrock 1156ea8dc4b6Seschrock /* 1157560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1158ea8dc4b6Seschrock */ 1159560e6e96Seschrock vdev_load(rvd); 11600373e76bSbonwick 1161fa9e4066Sahrens /* 1162fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1163fa9e4066Sahrens */ 1164ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1165fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1166ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1167fa9e4066Sahrens 1168fa9e4066Sahrens /* 1169fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1170fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1171fa9e4066Sahrens */ 1172ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1173ea8dc4b6Seschrock error = ENXIO; 1174ea8dc4b6Seschrock goto out; 1175ea8dc4b6Seschrock } 1176fa9e4066Sahrens 1177ea8dc4b6Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 11785dabedeeSbonwick dmu_tx_t *tx; 11790373e76bSbonwick int need_update = B_FALSE; 11800373e76bSbonwick int c; 11815dabedeeSbonwick 11820373e76bSbonwick /* 11830373e76bSbonwick * Claim log blocks that haven't been committed yet. 11840373e76bSbonwick * This must all happen in a single txg. 11850373e76bSbonwick */ 11865dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1187fa9e4066Sahrens spa_first_txg(spa)); 11880b69c2f0Sahrens (void) dmu_objset_find(spa->spa_name, 11890b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1190fa9e4066Sahrens dmu_tx_commit(tx); 1191fa9e4066Sahrens 1192fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1193fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1194fa9e4066Sahrens 1195fa9e4066Sahrens /* 1196fa9e4066Sahrens * Wait for all claims to sync. 1197fa9e4066Sahrens */ 1198fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 11990e34b6a7Sbonwick 12000e34b6a7Sbonwick /* 12010373e76bSbonwick * If the config cache is stale, or we have uninitialized 12020373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 12030e34b6a7Sbonwick */ 12040373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 12050373e76bSbonwick state == SPA_LOAD_IMPORT) 12060373e76bSbonwick need_update = B_TRUE; 12070373e76bSbonwick 12080373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) 12090373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 12100373e76bSbonwick need_update = B_TRUE; 12110e34b6a7Sbonwick 12120e34b6a7Sbonwick /* 12130373e76bSbonwick * Update the config cache asychronously in case we're the 12140373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 12150e34b6a7Sbonwick */ 12160373e76bSbonwick if (need_update) 12170373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1218fa9e4066Sahrens } 1219fa9e4066Sahrens 1220ea8dc4b6Seschrock error = 0; 1221ea8dc4b6Seschrock out: 122299653d4eSeschrock if (error && error != EBADF) 1223ea8dc4b6Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 1224ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 1225ea8dc4b6Seschrock spa->spa_ena = 0; 1226ea8dc4b6Seschrock 1227ea8dc4b6Seschrock return (error); 1228fa9e4066Sahrens } 1229fa9e4066Sahrens 1230fa9e4066Sahrens /* 1231fa9e4066Sahrens * Pool Open/Import 1232fa9e4066Sahrens * 1233fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 1234fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1235fa9e4066Sahrens * case of an open, the pool configuration will exist in the 12363d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 1237fa9e4066Sahrens * 1238fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1239fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 1240fa9e4066Sahrens * ambiguous state. 1241fa9e4066Sahrens */ 1242fa9e4066Sahrens static int 1243fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1244fa9e4066Sahrens { 1245fa9e4066Sahrens spa_t *spa; 1246fa9e4066Sahrens int error; 1247fa9e4066Sahrens int loaded = B_FALSE; 1248fa9e4066Sahrens int locked = B_FALSE; 1249fa9e4066Sahrens 1250fa9e4066Sahrens *spapp = NULL; 1251fa9e4066Sahrens 1252fa9e4066Sahrens /* 1253fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 1254fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1255fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 1256fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 1257fa9e4066Sahrens */ 1258fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1259fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1260fa9e4066Sahrens locked = B_TRUE; 1261fa9e4066Sahrens } 1262fa9e4066Sahrens 1263fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1264fa9e4066Sahrens if (locked) 1265fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1266fa9e4066Sahrens return (ENOENT); 1267fa9e4066Sahrens } 1268fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1269fa9e4066Sahrens 1270fa9e4066Sahrens spa_activate(spa); 1271fa9e4066Sahrens 12720373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1273fa9e4066Sahrens 1274fa9e4066Sahrens if (error == EBADF) { 1275fa9e4066Sahrens /* 1276560e6e96Seschrock * If vdev_validate() returns failure (indicated by 1277560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 1278560e6e96Seschrock * that the pool has been exported or destroyed. If 1279560e6e96Seschrock * this is the case, the config cache is out of sync and 1280560e6e96Seschrock * we should remove the pool from the namespace. 1281fa9e4066Sahrens */ 128299653d4eSeschrock zfs_post_ok(spa, NULL); 1283fa9e4066Sahrens spa_unload(spa); 1284fa9e4066Sahrens spa_deactivate(spa); 1285fa9e4066Sahrens spa_remove(spa); 1286fa9e4066Sahrens spa_config_sync(); 1287fa9e4066Sahrens if (locked) 1288fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1289fa9e4066Sahrens return (ENOENT); 1290ea8dc4b6Seschrock } 1291ea8dc4b6Seschrock 1292ea8dc4b6Seschrock if (error) { 1293fa9e4066Sahrens /* 1294fa9e4066Sahrens * We can't open the pool, but we still have useful 1295fa9e4066Sahrens * information: the state of each vdev after the 1296fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 1297fa9e4066Sahrens */ 12980373e76bSbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 12990373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 1300fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1301fa9e4066Sahrens B_TRUE); 13020373e76bSbonwick spa_config_exit(spa, FTAG); 13030373e76bSbonwick } 1304fa9e4066Sahrens spa_unload(spa); 1305fa9e4066Sahrens spa_deactivate(spa); 1306ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 1307fa9e4066Sahrens if (locked) 1308fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1309fa9e4066Sahrens *spapp = NULL; 1310fa9e4066Sahrens return (error); 1311ea8dc4b6Seschrock } else { 1312ea8dc4b6Seschrock zfs_post_ok(spa, NULL); 1313ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 1314fa9e4066Sahrens } 1315fa9e4066Sahrens 1316fa9e4066Sahrens loaded = B_TRUE; 1317fa9e4066Sahrens } 1318fa9e4066Sahrens 1319fa9e4066Sahrens spa_open_ref(spa, tag); 13203d7072f8Seschrock 13213d7072f8Seschrock /* 13223d7072f8Seschrock * If we just loaded the pool, resilver anything that's out of date. 13233d7072f8Seschrock */ 13243d7072f8Seschrock if (loaded && (spa_mode & FWRITE)) 13253d7072f8Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 13263d7072f8Seschrock 1327fa9e4066Sahrens if (locked) 1328fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1329fa9e4066Sahrens 1330fa9e4066Sahrens *spapp = spa; 1331fa9e4066Sahrens 1332fa9e4066Sahrens if (config != NULL) { 1333ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1334fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1335ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1336fa9e4066Sahrens } 1337fa9e4066Sahrens 1338fa9e4066Sahrens return (0); 1339fa9e4066Sahrens } 1340fa9e4066Sahrens 1341fa9e4066Sahrens int 1342fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1343fa9e4066Sahrens { 1344fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1345fa9e4066Sahrens } 1346fa9e4066Sahrens 1347ea8dc4b6Seschrock /* 1348ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 1349ea8dc4b6Seschrock * preventing it from being exported or destroyed. 1350ea8dc4b6Seschrock */ 1351ea8dc4b6Seschrock spa_t * 1352ea8dc4b6Seschrock spa_inject_addref(char *name) 1353ea8dc4b6Seschrock { 1354ea8dc4b6Seschrock spa_t *spa; 1355ea8dc4b6Seschrock 1356ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1357ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 1358ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1359ea8dc4b6Seschrock return (NULL); 1360ea8dc4b6Seschrock } 1361ea8dc4b6Seschrock spa->spa_inject_ref++; 1362ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1363ea8dc4b6Seschrock 1364ea8dc4b6Seschrock return (spa); 1365ea8dc4b6Seschrock } 1366ea8dc4b6Seschrock 1367ea8dc4b6Seschrock void 1368ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 1369ea8dc4b6Seschrock { 1370ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1371ea8dc4b6Seschrock spa->spa_inject_ref--; 1372ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1373ea8dc4b6Seschrock } 1374ea8dc4b6Seschrock 137599653d4eSeschrock static void 137699653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 137799653d4eSeschrock { 137899653d4eSeschrock nvlist_t **spares; 137999653d4eSeschrock uint_t i, nspares; 138099653d4eSeschrock nvlist_t *nvroot; 138199653d4eSeschrock uint64_t guid; 138299653d4eSeschrock vdev_stat_t *vs; 138399653d4eSeschrock uint_t vsc; 138439c23413Seschrock uint64_t pool; 138599653d4eSeschrock 138699653d4eSeschrock if (spa->spa_nspares == 0) 138799653d4eSeschrock return; 138899653d4eSeschrock 138999653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 139099653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 139199653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 139299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 139399653d4eSeschrock if (nspares != 0) { 139499653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 139599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 139699653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 139799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 139899653d4eSeschrock 139999653d4eSeschrock /* 140099653d4eSeschrock * Go through and find any spares which have since been 140199653d4eSeschrock * repurposed as an active spare. If this is the case, update 140299653d4eSeschrock * their status appropriately. 140399653d4eSeschrock */ 140499653d4eSeschrock for (i = 0; i < nspares; i++) { 140599653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 140699653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 140739c23413Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 140899653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 140999653d4eSeschrock spares[i], ZPOOL_CONFIG_STATS, 141099653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 141199653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 141299653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 141399653d4eSeschrock } 141499653d4eSeschrock } 141599653d4eSeschrock } 141699653d4eSeschrock } 141799653d4eSeschrock 1418fa9e4066Sahrens int 1419ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1420fa9e4066Sahrens { 1421fa9e4066Sahrens int error; 1422fa9e4066Sahrens spa_t *spa; 1423fa9e4066Sahrens 1424fa9e4066Sahrens *config = NULL; 1425fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 1426fa9e4066Sahrens 142799653d4eSeschrock if (spa && *config != NULL) { 1428ea8dc4b6Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 1429ea8dc4b6Seschrock spa_get_errlog_size(spa)) == 0); 1430ea8dc4b6Seschrock 143199653d4eSeschrock spa_add_spares(spa, *config); 143299653d4eSeschrock } 143399653d4eSeschrock 1434ea8dc4b6Seschrock /* 1435ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 1436ea8dc4b6Seschrock * and call spa_lookup() directly. 1437ea8dc4b6Seschrock */ 1438ea8dc4b6Seschrock if (altroot) { 1439ea8dc4b6Seschrock if (spa == NULL) { 1440ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1441ea8dc4b6Seschrock spa = spa_lookup(name); 1442ea8dc4b6Seschrock if (spa) 1443ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1444ea8dc4b6Seschrock else 1445ea8dc4b6Seschrock altroot[0] = '\0'; 1446ea8dc4b6Seschrock spa = NULL; 1447ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1448ea8dc4b6Seschrock } else { 1449ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1450ea8dc4b6Seschrock } 1451ea8dc4b6Seschrock } 1452ea8dc4b6Seschrock 1453fa9e4066Sahrens if (spa != NULL) 1454fa9e4066Sahrens spa_close(spa, FTAG); 1455fa9e4066Sahrens 1456fa9e4066Sahrens return (error); 1457fa9e4066Sahrens } 1458fa9e4066Sahrens 145999653d4eSeschrock /* 146099653d4eSeschrock * Validate that the 'spares' array is well formed. We must have an array of 146139c23413Seschrock * nvlists, each which describes a valid leaf vdev. If this is an import (mode 146239c23413Seschrock * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 146339c23413Seschrock * as they are well-formed. 146499653d4eSeschrock */ 146599653d4eSeschrock static int 146699653d4eSeschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 146799653d4eSeschrock { 146899653d4eSeschrock nvlist_t **spares; 146999653d4eSeschrock uint_t i, nspares; 147099653d4eSeschrock vdev_t *vd; 147199653d4eSeschrock int error; 147299653d4eSeschrock 147399653d4eSeschrock /* 147499653d4eSeschrock * It's acceptable to have no spares specified. 147599653d4eSeschrock */ 147699653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 147799653d4eSeschrock &spares, &nspares) != 0) 147899653d4eSeschrock return (0); 147999653d4eSeschrock 148099653d4eSeschrock if (nspares == 0) 148199653d4eSeschrock return (EINVAL); 148299653d4eSeschrock 148399653d4eSeschrock /* 148499653d4eSeschrock * Make sure the pool is formatted with a version that supports hot 148599653d4eSeschrock * spares. 148699653d4eSeschrock */ 1487e7437265Sahrens if (spa_version(spa) < SPA_VERSION_SPARES) 148899653d4eSeschrock return (ENOTSUP); 148999653d4eSeschrock 149039c23413Seschrock /* 149139c23413Seschrock * Set the pending spare list so we correctly handle device in-use 149239c23413Seschrock * checking. 149339c23413Seschrock */ 149439c23413Seschrock spa->spa_pending_spares = spares; 149539c23413Seschrock spa->spa_pending_nspares = nspares; 149639c23413Seschrock 149799653d4eSeschrock for (i = 0; i < nspares; i++) { 149899653d4eSeschrock if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 149999653d4eSeschrock mode)) != 0) 150039c23413Seschrock goto out; 150199653d4eSeschrock 150299653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 150399653d4eSeschrock vdev_free(vd); 150439c23413Seschrock error = EINVAL; 150539c23413Seschrock goto out; 150699653d4eSeschrock } 150799653d4eSeschrock 150899653d4eSeschrock vd->vdev_top = vd; 150999653d4eSeschrock 151039c23413Seschrock if ((error = vdev_open(vd)) == 0 && 151139c23413Seschrock (error = vdev_label_init(vd, crtxg, 151239c23413Seschrock VDEV_LABEL_SPARE)) == 0) { 151339c23413Seschrock VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 151439c23413Seschrock vd->vdev_guid) == 0); 151539c23413Seschrock } 151699653d4eSeschrock 151799653d4eSeschrock vdev_free(vd); 151839c23413Seschrock 151939c23413Seschrock if (error && mode != VDEV_ALLOC_SPARE) 152039c23413Seschrock goto out; 152139c23413Seschrock else 152239c23413Seschrock error = 0; 152399653d4eSeschrock } 152499653d4eSeschrock 152539c23413Seschrock out: 152639c23413Seschrock spa->spa_pending_spares = NULL; 152739c23413Seschrock spa->spa_pending_nspares = 0; 152839c23413Seschrock return (error); 152999653d4eSeschrock } 153099653d4eSeschrock 1531fa9e4066Sahrens /* 1532fa9e4066Sahrens * Pool Creation 1533fa9e4066Sahrens */ 1534fa9e4066Sahrens int 1535990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 1536228975ccSek const char *history_str) 1537fa9e4066Sahrens { 1538fa9e4066Sahrens spa_t *spa; 1539990b4856Slling char *altroot = NULL; 15400373e76bSbonwick vdev_t *rvd; 1541fa9e4066Sahrens dsl_pool_t *dp; 1542fa9e4066Sahrens dmu_tx_t *tx; 154399653d4eSeschrock int c, error = 0; 1544fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 154599653d4eSeschrock nvlist_t **spares; 154699653d4eSeschrock uint_t nspares; 1547990b4856Slling uint64_t version; 1548fa9e4066Sahrens 1549fa9e4066Sahrens /* 1550fa9e4066Sahrens * If this pool already exists, return failure. 1551fa9e4066Sahrens */ 1552fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1553fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 1554fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1555fa9e4066Sahrens return (EEXIST); 1556fa9e4066Sahrens } 1557fa9e4066Sahrens 1558fa9e4066Sahrens /* 1559fa9e4066Sahrens * Allocate a new spa_t structure. 1560fa9e4066Sahrens */ 1561990b4856Slling (void) nvlist_lookup_string(props, 1562990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 15630373e76bSbonwick spa = spa_add(pool, altroot); 1564fa9e4066Sahrens spa_activate(spa); 1565fa9e4066Sahrens 1566fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 1567990b4856Slling 1568990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 1569990b4856Slling spa_unload(spa); 1570990b4856Slling spa_deactivate(spa); 1571990b4856Slling spa_remove(spa); 1572990b4856Slling return (error); 1573990b4856Slling } 1574990b4856Slling 1575990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 1576990b4856Slling &version) != 0) 1577990b4856Slling version = SPA_VERSION; 1578990b4856Slling ASSERT(version <= SPA_VERSION); 1579990b4856Slling spa->spa_uberblock.ub_version = version; 1580fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1581fa9e4066Sahrens 15820373e76bSbonwick /* 15830373e76bSbonwick * Create the root vdev. 15840373e76bSbonwick */ 15850373e76bSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 15860373e76bSbonwick 158799653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 15880373e76bSbonwick 158999653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 159099653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 15910373e76bSbonwick 159299653d4eSeschrock if (error == 0 && rvd->vdev_children == 0) 15930373e76bSbonwick error = EINVAL; 159499653d4eSeschrock 159599653d4eSeschrock if (error == 0 && 159699653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 159799653d4eSeschrock (error = spa_validate_spares(spa, nvroot, txg, 159899653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 159999653d4eSeschrock for (c = 0; c < rvd->vdev_children; c++) 160099653d4eSeschrock vdev_init(rvd->vdev_child[c], txg); 160199653d4eSeschrock vdev_config_dirty(rvd); 16020373e76bSbonwick } 16030373e76bSbonwick 16040373e76bSbonwick spa_config_exit(spa, FTAG); 1605fa9e4066Sahrens 160699653d4eSeschrock if (error != 0) { 1607fa9e4066Sahrens spa_unload(spa); 1608fa9e4066Sahrens spa_deactivate(spa); 1609fa9e4066Sahrens spa_remove(spa); 1610fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1611fa9e4066Sahrens return (error); 1612fa9e4066Sahrens } 1613fa9e4066Sahrens 161499653d4eSeschrock /* 161599653d4eSeschrock * Get the list of spares, if specified. 161699653d4eSeschrock */ 161799653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 161899653d4eSeschrock &spares, &nspares) == 0) { 161999653d4eSeschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 162099653d4eSeschrock KM_SLEEP) == 0); 162199653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 162299653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 162399653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 162499653d4eSeschrock spa_load_spares(spa); 162599653d4eSeschrock spa_config_exit(spa, FTAG); 162699653d4eSeschrock spa->spa_sync_spares = B_TRUE; 162799653d4eSeschrock } 162899653d4eSeschrock 1629fa9e4066Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1630fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1631fa9e4066Sahrens 1632fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 1633fa9e4066Sahrens 1634fa9e4066Sahrens /* 1635fa9e4066Sahrens * Create the pool config object. 1636fa9e4066Sahrens */ 1637fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1638fa9e4066Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1639fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1640fa9e4066Sahrens 1641ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1642fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1643ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 1644ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 1645ea8dc4b6Seschrock } 1646fa9e4066Sahrens 1647990b4856Slling /* Newly created pools with the right version are always deflated. */ 1648990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 1649990b4856Slling spa->spa_deflate = TRUE; 1650990b4856Slling if (zap_add(spa->spa_meta_objset, 1651990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 1652990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 1653990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 1654990b4856Slling } 165599653d4eSeschrock } 165699653d4eSeschrock 1657fa9e4066Sahrens /* 1658fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 1659fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 1660fa9e4066Sahrens * keeps changing. 1661fa9e4066Sahrens */ 1662fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1663fa9e4066Sahrens 1 << 14, tx); 1664fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1665fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 1666fa9e4066Sahrens 1667ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1668fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1669ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 1670ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 1671ea8dc4b6Seschrock } 1672fa9e4066Sahrens 167306eeb2adSek /* 167406eeb2adSek * Create the pool's history object. 167506eeb2adSek */ 1676990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 1677990b4856Slling spa_history_create_obj(spa, tx); 1678990b4856Slling 1679990b4856Slling /* 1680990b4856Slling * Set pool properties. 1681990b4856Slling */ 1682990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 1683990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 16840a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 1685990b4856Slling if (props) 1686990b4856Slling spa_sync_props(spa, props, CRED(), tx); 168706eeb2adSek 1688fa9e4066Sahrens dmu_tx_commit(tx); 1689fa9e4066Sahrens 1690fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1691fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1692fa9e4066Sahrens 1693fa9e4066Sahrens /* 1694fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 1695fa9e4066Sahrens * bean counters are appropriately updated. 1696fa9e4066Sahrens */ 1697fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1698fa9e4066Sahrens 1699fa9e4066Sahrens spa_config_sync(); 1700fa9e4066Sahrens 1701990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 1702228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 1703228975ccSek 1704fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1705fa9e4066Sahrens 1706fa9e4066Sahrens return (0); 1707fa9e4066Sahrens } 1708fa9e4066Sahrens 1709fa9e4066Sahrens /* 1710fa9e4066Sahrens * Import the given pool into the system. We set up the necessary spa_t and 1711fa9e4066Sahrens * then call spa_load() to do the dirty work. 1712fa9e4066Sahrens */ 1713fa9e4066Sahrens int 1714990b4856Slling spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 1715fa9e4066Sahrens { 1716fa9e4066Sahrens spa_t *spa; 1717990b4856Slling char *altroot = NULL; 1718fa9e4066Sahrens int error; 171999653d4eSeschrock nvlist_t *nvroot; 172099653d4eSeschrock nvlist_t **spares; 172199653d4eSeschrock uint_t nspares; 1722fa9e4066Sahrens 1723fa9e4066Sahrens /* 1724fa9e4066Sahrens * If a pool with this name exists, return failure. 1725fa9e4066Sahrens */ 1726fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1727fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 1728fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1729fa9e4066Sahrens return (EEXIST); 1730fa9e4066Sahrens } 1731fa9e4066Sahrens 1732fa9e4066Sahrens /* 17330373e76bSbonwick * Create and initialize the spa structure. 1734fa9e4066Sahrens */ 1735990b4856Slling (void) nvlist_lookup_string(props, 1736990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 17370373e76bSbonwick spa = spa_add(pool, altroot); 1738fa9e4066Sahrens spa_activate(spa); 1739fa9e4066Sahrens 17405dabedeeSbonwick /* 17410373e76bSbonwick * Pass off the heavy lifting to spa_load(). 1742ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 1743ecc2d604Sbonwick * is actually the one to trust when doing an import. 17445dabedeeSbonwick */ 1745ecc2d604Sbonwick error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1746fa9e4066Sahrens 174799653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 174899653d4eSeschrock /* 174999653d4eSeschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 175099653d4eSeschrock * and conflicts with spa_has_spare(). 175199653d4eSeschrock */ 175299653d4eSeschrock if (spa->spa_sparelist) { 175399653d4eSeschrock nvlist_free(spa->spa_sparelist); 175499653d4eSeschrock spa->spa_sparelist = NULL; 175599653d4eSeschrock spa_load_spares(spa); 175699653d4eSeschrock } 175799653d4eSeschrock 175899653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 175999653d4eSeschrock &nvroot) == 0); 1760990b4856Slling if (error == 0) { 176199653d4eSeschrock error = spa_validate_spares(spa, nvroot, -1ULL, 176299653d4eSeschrock VDEV_ALLOC_SPARE); 1763990b4856Slling } 176499653d4eSeschrock spa_config_exit(spa, FTAG); 176599653d4eSeschrock 1766990b4856Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 1767fa9e4066Sahrens spa_unload(spa); 1768fa9e4066Sahrens spa_deactivate(spa); 1769fa9e4066Sahrens spa_remove(spa); 1770fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1771fa9e4066Sahrens return (error); 1772fa9e4066Sahrens } 1773fa9e4066Sahrens 177499653d4eSeschrock /* 177599653d4eSeschrock * Override any spares as specified by the user, as these may have 177699653d4eSeschrock * correct device names/devids, etc. 177799653d4eSeschrock */ 177899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 177999653d4eSeschrock &spares, &nspares) == 0) { 178099653d4eSeschrock if (spa->spa_sparelist) 178199653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, 178299653d4eSeschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 178399653d4eSeschrock else 178499653d4eSeschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 178599653d4eSeschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 178699653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 178799653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 178899653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 178999653d4eSeschrock spa_load_spares(spa); 179099653d4eSeschrock spa_config_exit(spa, FTAG); 179199653d4eSeschrock spa->spa_sync_spares = B_TRUE; 179299653d4eSeschrock } 179399653d4eSeschrock 17940373e76bSbonwick /* 17950373e76bSbonwick * Update the config cache to include the newly-imported pool. 17960373e76bSbonwick */ 1797de6628f0Sck if (spa_mode & FWRITE) 1798de6628f0Sck spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 17990373e76bSbonwick 1800fa9e4066Sahrens /* 1801fa9e4066Sahrens * Resilver anything that's out of date. 1802fa9e4066Sahrens */ 1803fa9e4066Sahrens if (spa_mode & FWRITE) 1804fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1805fa9e4066Sahrens 18063d7072f8Seschrock mutex_exit(&spa_namespace_lock); 18073d7072f8Seschrock 1808fa9e4066Sahrens return (0); 1809fa9e4066Sahrens } 1810fa9e4066Sahrens 1811fa9e4066Sahrens /* 1812fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 1813fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 1814fa9e4066Sahrens */ 1815fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 1816fa9e4066Sahrens 1817fa9e4066Sahrens nvlist_t * 1818fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 1819fa9e4066Sahrens { 1820fa9e4066Sahrens nvlist_t *config = NULL; 1821fa9e4066Sahrens char *poolname; 1822fa9e4066Sahrens spa_t *spa; 1823fa9e4066Sahrens uint64_t state; 1824fa9e4066Sahrens 1825fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1826fa9e4066Sahrens return (NULL); 1827fa9e4066Sahrens 1828fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1829fa9e4066Sahrens return (NULL); 1830fa9e4066Sahrens 1831fa9e4066Sahrens /* 18320373e76bSbonwick * Create and initialize the spa structure. 1833fa9e4066Sahrens */ 18340373e76bSbonwick mutex_enter(&spa_namespace_lock); 18350373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 1836fa9e4066Sahrens spa_activate(spa); 1837fa9e4066Sahrens 1838fa9e4066Sahrens /* 18390373e76bSbonwick * Pass off the heavy lifting to spa_load(). 1840ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 1841ecc2d604Sbonwick * is actually the one to trust when doing an import. 1842fa9e4066Sahrens */ 1843ecc2d604Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1844fa9e4066Sahrens 1845fa9e4066Sahrens /* 1846fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 1847fa9e4066Sahrens */ 1848fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 18490373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 1850fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 18510373e76bSbonwick spa_config_exit(spa, FTAG); 1852fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1853fa9e4066Sahrens poolname) == 0); 1854fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1855fa9e4066Sahrens state) == 0); 185695173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 185795173954Sek spa->spa_uberblock.ub_timestamp) == 0); 185899653d4eSeschrock 185999653d4eSeschrock /* 186099653d4eSeschrock * Add the list of hot spares. 186199653d4eSeschrock */ 186299653d4eSeschrock spa_add_spares(spa, config); 1863fa9e4066Sahrens } 1864fa9e4066Sahrens 1865fa9e4066Sahrens spa_unload(spa); 1866fa9e4066Sahrens spa_deactivate(spa); 1867fa9e4066Sahrens spa_remove(spa); 1868fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1869fa9e4066Sahrens 1870fa9e4066Sahrens return (config); 1871fa9e4066Sahrens } 1872fa9e4066Sahrens 1873fa9e4066Sahrens /* 1874fa9e4066Sahrens * Pool export/destroy 1875fa9e4066Sahrens * 1876fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 1877fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 1878fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 1879fa9e4066Sahrens * configuration from the cache afterwards. 1880fa9e4066Sahrens */ 1881fa9e4066Sahrens static int 188244cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1883fa9e4066Sahrens { 1884fa9e4066Sahrens spa_t *spa; 1885fa9e4066Sahrens 188644cd46caSbillm if (oldconfig) 188744cd46caSbillm *oldconfig = NULL; 188844cd46caSbillm 1889fa9e4066Sahrens if (!(spa_mode & FWRITE)) 1890fa9e4066Sahrens return (EROFS); 1891fa9e4066Sahrens 1892fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1893fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1894fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1895fa9e4066Sahrens return (ENOENT); 1896fa9e4066Sahrens } 1897fa9e4066Sahrens 1898ea8dc4b6Seschrock /* 1899ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 1900ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 1901ea8dc4b6Seschrock */ 1902ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 1903ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1904ea8dc4b6Seschrock spa_async_suspend(spa); 1905ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1906ea8dc4b6Seschrock spa_close(spa, FTAG); 1907ea8dc4b6Seschrock 1908fa9e4066Sahrens /* 1909fa9e4066Sahrens * The pool will be in core if it's openable, 1910fa9e4066Sahrens * in which case we can modify its state. 1911fa9e4066Sahrens */ 1912fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1913fa9e4066Sahrens /* 1914fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 1915fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 1916fa9e4066Sahrens */ 1917fa9e4066Sahrens spa_scrub_suspend(spa); 1918fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 1919fa9e4066Sahrens 1920ea8dc4b6Seschrock /* 1921ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 1922ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 1923ea8dc4b6Seschrock * fault injection handlers. 1924ea8dc4b6Seschrock */ 1925ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 1926ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 1927ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 1928fa9e4066Sahrens spa_scrub_resume(spa); 1929ea8dc4b6Seschrock spa_async_resume(spa); 1930fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1931fa9e4066Sahrens return (EBUSY); 1932fa9e4066Sahrens } 1933fa9e4066Sahrens 1934fa9e4066Sahrens spa_scrub_resume(spa); 1935fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1936fa9e4066Sahrens 1937fa9e4066Sahrens /* 1938fa9e4066Sahrens * We want this to be reflected on every label, 1939fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 1940fa9e4066Sahrens * final sync that pushes these changes out. 1941fa9e4066Sahrens */ 1942ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 19435dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 1944ea8dc4b6Seschrock spa->spa_state = new_state; 19450373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 1946ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 19475dabedeeSbonwick spa_config_exit(spa, FTAG); 1948ea8dc4b6Seschrock } 1949fa9e4066Sahrens } 1950fa9e4066Sahrens 19513d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 19523d7072f8Seschrock 1953fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1954fa9e4066Sahrens spa_unload(spa); 1955fa9e4066Sahrens spa_deactivate(spa); 1956fa9e4066Sahrens } 1957fa9e4066Sahrens 195844cd46caSbillm if (oldconfig && spa->spa_config) 195944cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 196044cd46caSbillm 1961ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 1962*2f8aaab3Seschrock spa_config_check(spa->spa_config_dir, 1963*2f8aaab3Seschrock spa->spa_config_file); 1964ea8dc4b6Seschrock spa_remove(spa); 1965ea8dc4b6Seschrock spa_config_sync(); 1966ea8dc4b6Seschrock } 1967fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1968fa9e4066Sahrens 1969fa9e4066Sahrens return (0); 1970fa9e4066Sahrens } 1971fa9e4066Sahrens 1972fa9e4066Sahrens /* 1973fa9e4066Sahrens * Destroy a storage pool. 1974fa9e4066Sahrens */ 1975fa9e4066Sahrens int 1976fa9e4066Sahrens spa_destroy(char *pool) 1977fa9e4066Sahrens { 197844cd46caSbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1979fa9e4066Sahrens } 1980fa9e4066Sahrens 1981fa9e4066Sahrens /* 1982fa9e4066Sahrens * Export a storage pool. 1983fa9e4066Sahrens */ 1984fa9e4066Sahrens int 198544cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig) 1986fa9e4066Sahrens { 198744cd46caSbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1988fa9e4066Sahrens } 1989fa9e4066Sahrens 1990ea8dc4b6Seschrock /* 1991ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 1992ea8dc4b6Seschrock * from the namespace in any way. 1993ea8dc4b6Seschrock */ 1994ea8dc4b6Seschrock int 1995ea8dc4b6Seschrock spa_reset(char *pool) 1996ea8dc4b6Seschrock { 199744cd46caSbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 1998ea8dc4b6Seschrock } 1999ea8dc4b6Seschrock 2000ea8dc4b6Seschrock 2001fa9e4066Sahrens /* 2002fa9e4066Sahrens * ========================================================================== 2003fa9e4066Sahrens * Device manipulation 2004fa9e4066Sahrens * ========================================================================== 2005fa9e4066Sahrens */ 2006fa9e4066Sahrens 2007fa9e4066Sahrens /* 20088654d025Sperrin * Add a device to a storage pool. 2009fa9e4066Sahrens */ 2010fa9e4066Sahrens int 2011fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2012fa9e4066Sahrens { 2013fa9e4066Sahrens uint64_t txg; 20140373e76bSbonwick int c, error; 2015fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 20160e34b6a7Sbonwick vdev_t *vd, *tvd; 201799653d4eSeschrock nvlist_t **spares; 201899653d4eSeschrock uint_t i, nspares; 2019fa9e4066Sahrens 2020fa9e4066Sahrens txg = spa_vdev_enter(spa); 2021fa9e4066Sahrens 202299653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 202399653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 202499653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 2025fa9e4066Sahrens 202639c23413Seschrock spa->spa_pending_vdev = vd; 202799653d4eSeschrock 202899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 202999653d4eSeschrock &spares, &nspares) != 0) 203099653d4eSeschrock nspares = 0; 203199653d4eSeschrock 203239c23413Seschrock if (vd->vdev_children == 0 && nspares == 0) { 203339c23413Seschrock spa->spa_pending_vdev = NULL; 2034fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 203539c23413Seschrock } 2036fa9e4066Sahrens 203799653d4eSeschrock if (vd->vdev_children != 0) { 203839c23413Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 203939c23413Seschrock spa->spa_pending_vdev = NULL; 204099653d4eSeschrock return (spa_vdev_exit(spa, vd, txg, error)); 204199653d4eSeschrock } 204299653d4eSeschrock } 204399653d4eSeschrock 204439c23413Seschrock /* 204539c23413Seschrock * We must validate the spares after checking the children. Otherwise, 204639c23413Seschrock * vdev_inuse() will blindly overwrite the spare. 204739c23413Seschrock */ 204839c23413Seschrock if ((error = spa_validate_spares(spa, nvroot, txg, 204939c23413Seschrock VDEV_ALLOC_ADD)) != 0) { 205039c23413Seschrock spa->spa_pending_vdev = NULL; 205139c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 205239c23413Seschrock } 205339c23413Seschrock 205439c23413Seschrock spa->spa_pending_vdev = NULL; 205539c23413Seschrock 205639c23413Seschrock /* 205739c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 205839c23413Seschrock */ 205939c23413Seschrock for (c = 0; c < vd->vdev_children; c++) { 206039c23413Seschrock tvd = vd->vdev_child[c]; 206139c23413Seschrock vdev_remove_child(vd, tvd); 206239c23413Seschrock tvd->vdev_id = rvd->vdev_children; 206339c23413Seschrock vdev_add_child(rvd, tvd); 206439c23413Seschrock vdev_config_dirty(tvd); 206539c23413Seschrock } 206639c23413Seschrock 206799653d4eSeschrock if (nspares != 0) { 206899653d4eSeschrock if (spa->spa_sparelist != NULL) { 206999653d4eSeschrock nvlist_t **oldspares; 207099653d4eSeschrock uint_t oldnspares; 207199653d4eSeschrock nvlist_t **newspares; 207299653d4eSeschrock 207399653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 207499653d4eSeschrock ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 207599653d4eSeschrock 207699653d4eSeschrock newspares = kmem_alloc(sizeof (void *) * 207799653d4eSeschrock (nspares + oldnspares), KM_SLEEP); 207899653d4eSeschrock for (i = 0; i < oldnspares; i++) 207999653d4eSeschrock VERIFY(nvlist_dup(oldspares[i], 208099653d4eSeschrock &newspares[i], KM_SLEEP) == 0); 208199653d4eSeschrock for (i = 0; i < nspares; i++) 208299653d4eSeschrock VERIFY(nvlist_dup(spares[i], 208399653d4eSeschrock &newspares[i + oldnspares], 208499653d4eSeschrock KM_SLEEP) == 0); 208599653d4eSeschrock 208699653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, 208799653d4eSeschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 208899653d4eSeschrock 208999653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 209099653d4eSeschrock ZPOOL_CONFIG_SPARES, newspares, 209199653d4eSeschrock nspares + oldnspares) == 0); 209299653d4eSeschrock for (i = 0; i < oldnspares + nspares; i++) 209399653d4eSeschrock nvlist_free(newspares[i]); 209499653d4eSeschrock kmem_free(newspares, (oldnspares + nspares) * 209599653d4eSeschrock sizeof (void *)); 209699653d4eSeschrock } else { 209799653d4eSeschrock VERIFY(nvlist_alloc(&spa->spa_sparelist, 209899653d4eSeschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 209999653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 210099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 210199653d4eSeschrock } 210299653d4eSeschrock 210399653d4eSeschrock spa_load_spares(spa); 210499653d4eSeschrock spa->spa_sync_spares = B_TRUE; 2105fa9e4066Sahrens } 2106fa9e4066Sahrens 2107fa9e4066Sahrens /* 21080e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 21090e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 21100e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 21110e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 21120e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 21130e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 21140373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 21150e34b6a7Sbonwick * 21160e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 21170e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 21180e34b6a7Sbonwick * steps will be completed the next time we load the pool. 21190e34b6a7Sbonwick */ 21200373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 21210e34b6a7Sbonwick 21220373e76bSbonwick mutex_enter(&spa_namespace_lock); 21230373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 21240373e76bSbonwick mutex_exit(&spa_namespace_lock); 2125fa9e4066Sahrens 21260373e76bSbonwick return (0); 2127fa9e4066Sahrens } 2128fa9e4066Sahrens 2129fa9e4066Sahrens /* 2130fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 2131fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2132fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2133fa9e4066Sahrens * 2134fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 2135fa9e4066Sahrens * existing device; in this case the two devices are made into their own 21363d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2137fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2138fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 2139fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 2140fa9e4066Sahrens * is automatically detached. 2141fa9e4066Sahrens */ 2142fa9e4066Sahrens int 2143ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2144fa9e4066Sahrens { 2145fa9e4066Sahrens uint64_t txg, open_txg; 2146fa9e4066Sahrens int error; 2147fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2148fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 214999653d4eSeschrock vdev_ops_t *pvops; 21508654d025Sperrin int is_log; 2151fa9e4066Sahrens 2152fa9e4066Sahrens txg = spa_vdev_enter(spa); 2153fa9e4066Sahrens 2154ea8dc4b6Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 2155fa9e4066Sahrens 2156fa9e4066Sahrens if (oldvd == NULL) 2157fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2158fa9e4066Sahrens 21590e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 21600e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 21610e34b6a7Sbonwick 2162fa9e4066Sahrens pvd = oldvd->vdev_parent; 2163fa9e4066Sahrens 216499653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 21653d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 21663d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 21673d7072f8Seschrock 21683d7072f8Seschrock if (newrootvd->vdev_children != 1) 2169fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2170fa9e4066Sahrens 2171fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 2172fa9e4066Sahrens 2173fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2174fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2175fa9e4066Sahrens 217699653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2177fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2178fa9e4066Sahrens 21798654d025Sperrin /* 21808654d025Sperrin * Spares can't replace logs 21818654d025Sperrin */ 21828654d025Sperrin is_log = oldvd->vdev_islog; 21838654d025Sperrin if (is_log && newvd->vdev_isspare) 21848654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 21858654d025Sperrin 218699653d4eSeschrock if (!replacing) { 218799653d4eSeschrock /* 218899653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 218999653d4eSeschrock * vdev. 219099653d4eSeschrock */ 219199653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 219299653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 219399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 219499653d4eSeschrock 219599653d4eSeschrock pvops = &vdev_mirror_ops; 219699653d4eSeschrock } else { 219799653d4eSeschrock /* 219899653d4eSeschrock * Active hot spares can only be replaced by inactive hot 219999653d4eSeschrock * spares. 220099653d4eSeschrock */ 220199653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 220299653d4eSeschrock pvd->vdev_child[1] == oldvd && 220399653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 220499653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 220599653d4eSeschrock 220699653d4eSeschrock /* 220799653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 220899653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 220939c23413Seschrock * want to create a replacing vdev. The user is not allowed to 221039c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 221139c23413Seschrock * the same (spare replaces spare, non-spare replaces 221239c23413Seschrock * non-spare). 221399653d4eSeschrock */ 221499653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 221599653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 221639c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 221739c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 221839c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 221999653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 222099653d4eSeschrock newvd->vdev_isspare) 222199653d4eSeschrock pvops = &vdev_spare_ops; 222299653d4eSeschrock else 222399653d4eSeschrock pvops = &vdev_replacing_ops; 222499653d4eSeschrock } 222599653d4eSeschrock 22262a79c5feSlling /* 22272a79c5feSlling * Compare the new device size with the replaceable/attachable 22282a79c5feSlling * device size. 22292a79c5feSlling */ 22302a79c5feSlling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2231fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2232fa9e4066Sahrens 2233ecc2d604Sbonwick /* 2234ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 2235ecc2d604Sbonwick * than the top-level vdev. 2236ecc2d604Sbonwick */ 2237ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2238fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2239fa9e4066Sahrens 2240fa9e4066Sahrens /* 2241fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 2242fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2243fa9e4066Sahrens */ 2244fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2245fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 2246fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2247fa9e4066Sahrens KM_SLEEP); 2248fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2249fa9e4066Sahrens newvd->vdev_path, "old"); 2250fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 2251fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 2252fa9e4066Sahrens oldvd->vdev_devid = NULL; 2253fa9e4066Sahrens } 2254fa9e4066Sahrens } 2255fa9e4066Sahrens 2256fa9e4066Sahrens /* 225799653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 225899653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 2259fa9e4066Sahrens */ 2260fa9e4066Sahrens if (pvd->vdev_ops != pvops) 2261fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 2262fa9e4066Sahrens 2263fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2264fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 2265fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 2266fa9e4066Sahrens 2267fa9e4066Sahrens /* 2268fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 2269fa9e4066Sahrens */ 2270fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 2271fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 2272fa9e4066Sahrens vdev_add_child(pvd, newvd); 2273fa9e4066Sahrens 2274ea8dc4b6Seschrock /* 2275ea8dc4b6Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 2276ea8dc4b6Seschrock * the addition of newvd may have decreased our parent's asize. 2277ea8dc4b6Seschrock */ 2278ea8dc4b6Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 2279ea8dc4b6Seschrock 2280fa9e4066Sahrens tvd = newvd->vdev_top; 2281fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 2282fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2283fa9e4066Sahrens 2284fa9e4066Sahrens vdev_config_dirty(tvd); 2285fa9e4066Sahrens 2286fa9e4066Sahrens /* 2287fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2288fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2289fa9e4066Sahrens */ 2290fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2291fa9e4066Sahrens 2292fa9e4066Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2293fa9e4066Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2294fa9e4066Sahrens open_txg - TXG_INITIAL + 1); 2295fa9e4066Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2296fa9e4066Sahrens 229739c23413Seschrock if (newvd->vdev_isspare) 229839c23413Seschrock spa_spare_activate(newvd); 2299ea8dc4b6Seschrock 2300fa9e4066Sahrens /* 2301fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 2302fa9e4066Sahrens */ 2303ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2304fa9e4066Sahrens 2305fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2306fa9e4066Sahrens 2307fa9e4066Sahrens /* 23083d7072f8Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 23093d7072f8Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 2310fa9e4066Sahrens */ 23113d7072f8Seschrock mutex_enter(&spa_namespace_lock); 2312fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 23133d7072f8Seschrock mutex_exit(&spa_namespace_lock); 2314fa9e4066Sahrens 2315fa9e4066Sahrens return (0); 2316fa9e4066Sahrens } 2317fa9e4066Sahrens 2318fa9e4066Sahrens /* 2319fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 2320fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 2321fa9e4066Sahrens * is a replacing vdev. 2322fa9e4066Sahrens */ 2323fa9e4066Sahrens int 2324ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2325fa9e4066Sahrens { 2326fa9e4066Sahrens uint64_t txg; 2327fa9e4066Sahrens int c, t, error; 2328fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2329fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 233099653d4eSeschrock boolean_t unspare = B_FALSE; 233199653d4eSeschrock uint64_t unspare_guid; 2332fa9e4066Sahrens 2333fa9e4066Sahrens txg = spa_vdev_enter(spa); 2334fa9e4066Sahrens 2335ea8dc4b6Seschrock vd = vdev_lookup_by_guid(rvd, guid); 2336fa9e4066Sahrens 2337fa9e4066Sahrens if (vd == NULL) 2338fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2339fa9e4066Sahrens 23400e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 23410e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 23420e34b6a7Sbonwick 2343fa9e4066Sahrens pvd = vd->vdev_parent; 2344fa9e4066Sahrens 2345fa9e4066Sahrens /* 2346fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 234799653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 234899653d4eSeschrock * disk can be removed. 234999653d4eSeschrock */ 235099653d4eSeschrock if (replace_done) { 235199653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 235299653d4eSeschrock if (vd->vdev_id != 0) 235399653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 235499653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 235599653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 235699653d4eSeschrock } 235799653d4eSeschrock } 235899653d4eSeschrock 235999653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 2360e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2361fa9e4066Sahrens 2362fa9e4066Sahrens /* 236399653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 2364fa9e4066Sahrens */ 2365fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 236699653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 236799653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 2368fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2369fa9e4066Sahrens 2370fa9e4066Sahrens /* 2371fa9e4066Sahrens * If there's only one replica, you can't detach it. 2372fa9e4066Sahrens */ 2373fa9e4066Sahrens if (pvd->vdev_children <= 1) 2374fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2375fa9e4066Sahrens 2376fa9e4066Sahrens /* 2377fa9e4066Sahrens * If all siblings have non-empty DTLs, this device may have the only 2378fa9e4066Sahrens * valid copy of the data, which means we cannot safely detach it. 2379fa9e4066Sahrens * 2380fa9e4066Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2381fa9e4066Sahrens * precise DTL check. 2382fa9e4066Sahrens */ 2383fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2384fa9e4066Sahrens uint64_t dirty; 2385fa9e4066Sahrens 2386fa9e4066Sahrens cvd = pvd->vdev_child[c]; 2387fa9e4066Sahrens if (cvd == vd) 2388fa9e4066Sahrens continue; 2389fa9e4066Sahrens if (vdev_is_dead(cvd)) 2390fa9e4066Sahrens continue; 2391fa9e4066Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2392fa9e4066Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2393fa9e4066Sahrens cvd->vdev_dtl_scrub.sm_space; 2394fa9e4066Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2395fa9e4066Sahrens if (!dirty) 2396fa9e4066Sahrens break; 2397fa9e4066Sahrens } 239899653d4eSeschrock 239999653d4eSeschrock /* 240099653d4eSeschrock * If we are a replacing or spare vdev, then we can always detach the 240199653d4eSeschrock * latter child, as that is how one cancels the operation. 240299653d4eSeschrock */ 240399653d4eSeschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 240499653d4eSeschrock c == pvd->vdev_children) 2405fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2406fa9e4066Sahrens 240799653d4eSeschrock /* 240899653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 240999653d4eSeschrock * that the spare should become a real disk, and be removed from the 241099653d4eSeschrock * active spare list for the pool. 241199653d4eSeschrock */ 241299653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 241399653d4eSeschrock vd->vdev_id == 0) 241499653d4eSeschrock unspare = B_TRUE; 241599653d4eSeschrock 2416fa9e4066Sahrens /* 2417fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 2418fa9e4066Sahrens * This must be done after all other error cases are handled, 2419fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 2420fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 2421fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 2422fa9e4066Sahrens * it's being detached! 2423fa9e4066Sahrens */ 242439c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2425fa9e4066Sahrens 2426fa9e4066Sahrens /* 2427fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 2428fa9e4066Sahrens */ 2429fa9e4066Sahrens vdev_remove_child(pvd, vd); 2430fa9e4066Sahrens vdev_compact_children(pvd); 2431fa9e4066Sahrens 2432fa9e4066Sahrens /* 2433fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 2434fa9e4066Sahrens */ 2435fa9e4066Sahrens cvd = pvd->vdev_child[0]; 2436fa9e4066Sahrens 243799653d4eSeschrock /* 243899653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 243999653d4eSeschrock * do it now, marking the vdev as no longer a spare in the process. We 244099653d4eSeschrock * must do this before vdev_remove_parent(), because that can change the 244199653d4eSeschrock * GUID if it creates a new toplevel GUID. 244299653d4eSeschrock */ 244399653d4eSeschrock if (unspare) { 244499653d4eSeschrock ASSERT(cvd->vdev_isspare); 244539c23413Seschrock spa_spare_remove(cvd); 244699653d4eSeschrock unspare_guid = cvd->vdev_guid; 244799653d4eSeschrock } 244899653d4eSeschrock 2449fa9e4066Sahrens /* 2450fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 2451fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 2452fa9e4066Sahrens */ 2453fa9e4066Sahrens if (pvd->vdev_children == 1) 2454fa9e4066Sahrens vdev_remove_parent(cvd); 2455fa9e4066Sahrens 2456fa9e4066Sahrens /* 2457fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 2458fa9e4066Sahrens * may have been the previous top-level vdev. 2459fa9e4066Sahrens */ 2460fa9e4066Sahrens tvd = cvd->vdev_top; 2461fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2462fa9e4066Sahrens 2463fa9e4066Sahrens /* 246439c23413Seschrock * Reevaluate the parent vdev state. 2465fa9e4066Sahrens */ 24663d7072f8Seschrock vdev_propagate_state(cvd); 2467fa9e4066Sahrens 2468fa9e4066Sahrens /* 246939c23413Seschrock * If the device we just detached was smaller than the others, it may be 247039c23413Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 247139c23413Seschrock * can't fail because the existing metaslabs are already in core, so 247239c23413Seschrock * there's nothing to read from disk. 2473fa9e4066Sahrens */ 2474ecc2d604Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2475fa9e4066Sahrens 2476fa9e4066Sahrens vdev_config_dirty(tvd); 2477fa9e4066Sahrens 2478fa9e4066Sahrens /* 247939c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 248039c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 248139c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 248239c23413Seschrock * prevent vd from being accessed after it's freed. 2483fa9e4066Sahrens */ 2484fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) 2485fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 2486ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 2487ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2488fa9e4066Sahrens 24893d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 24903d7072f8Seschrock 249199653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 249299653d4eSeschrock 249399653d4eSeschrock /* 249439c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 249539c23413Seschrock * then we want to go through and remove the device from the hot spare 249639c23413Seschrock * list of every other pool. 249799653d4eSeschrock */ 249899653d4eSeschrock if (unspare) { 249999653d4eSeschrock spa = NULL; 250099653d4eSeschrock mutex_enter(&spa_namespace_lock); 250199653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 250299653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 250399653d4eSeschrock continue; 250499653d4eSeschrock 250599653d4eSeschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 250699653d4eSeschrock } 250799653d4eSeschrock mutex_exit(&spa_namespace_lock); 250899653d4eSeschrock } 250999653d4eSeschrock 251099653d4eSeschrock return (error); 251199653d4eSeschrock } 251299653d4eSeschrock 251399653d4eSeschrock /* 251499653d4eSeschrock * Remove a device from the pool. Currently, this supports removing only hot 251599653d4eSeschrock * spares. 251699653d4eSeschrock */ 251799653d4eSeschrock int 251899653d4eSeschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 251999653d4eSeschrock { 252099653d4eSeschrock vdev_t *vd; 252199653d4eSeschrock nvlist_t **spares, *nv, **newspares; 252299653d4eSeschrock uint_t i, j, nspares; 252399653d4eSeschrock int ret = 0; 252499653d4eSeschrock 252599653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 252699653d4eSeschrock 252799653d4eSeschrock vd = spa_lookup_by_guid(spa, guid); 252899653d4eSeschrock 252999653d4eSeschrock nv = NULL; 253099653d4eSeschrock if (spa->spa_spares != NULL && 253199653d4eSeschrock nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 253299653d4eSeschrock &spares, &nspares) == 0) { 253399653d4eSeschrock for (i = 0; i < nspares; i++) { 253499653d4eSeschrock uint64_t theguid; 253599653d4eSeschrock 253699653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 253799653d4eSeschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 253899653d4eSeschrock if (theguid == guid) { 253999653d4eSeschrock nv = spares[i]; 254099653d4eSeschrock break; 254199653d4eSeschrock } 254299653d4eSeschrock } 254399653d4eSeschrock } 254499653d4eSeschrock 254599653d4eSeschrock /* 254699653d4eSeschrock * We only support removing a hot spare, and only if it's not currently 254799653d4eSeschrock * in use in this pool. 254899653d4eSeschrock */ 254999653d4eSeschrock if (nv == NULL && vd == NULL) { 255099653d4eSeschrock ret = ENOENT; 255199653d4eSeschrock goto out; 255299653d4eSeschrock } 255399653d4eSeschrock 255499653d4eSeschrock if (nv == NULL && vd != NULL) { 255599653d4eSeschrock ret = ENOTSUP; 255699653d4eSeschrock goto out; 255799653d4eSeschrock } 255899653d4eSeschrock 255999653d4eSeschrock if (!unspare && nv != NULL && vd != NULL) { 256099653d4eSeschrock ret = EBUSY; 256199653d4eSeschrock goto out; 256299653d4eSeschrock } 256399653d4eSeschrock 256499653d4eSeschrock if (nspares == 1) { 256599653d4eSeschrock newspares = NULL; 256699653d4eSeschrock } else { 256799653d4eSeschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 256899653d4eSeschrock KM_SLEEP); 256999653d4eSeschrock for (i = 0, j = 0; i < nspares; i++) { 257099653d4eSeschrock if (spares[i] != nv) 257199653d4eSeschrock VERIFY(nvlist_dup(spares[i], 257299653d4eSeschrock &newspares[j++], KM_SLEEP) == 0); 257399653d4eSeschrock } 257499653d4eSeschrock } 257599653d4eSeschrock 257699653d4eSeschrock VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 257799653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 257899653d4eSeschrock VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 257999653d4eSeschrock newspares, nspares - 1) == 0); 258099653d4eSeschrock for (i = 0; i < nspares - 1; i++) 258199653d4eSeschrock nvlist_free(newspares[i]); 258299653d4eSeschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 258399653d4eSeschrock spa_load_spares(spa); 258499653d4eSeschrock spa->spa_sync_spares = B_TRUE; 258599653d4eSeschrock 258699653d4eSeschrock out: 258799653d4eSeschrock spa_config_exit(spa, FTAG); 258899653d4eSeschrock 258999653d4eSeschrock return (ret); 2590fa9e4066Sahrens } 2591fa9e4066Sahrens 2592fa9e4066Sahrens /* 25933d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 25943d7072f8Seschrock * current spared, so we can detach it. 2595fa9e4066Sahrens */ 2596ea8dc4b6Seschrock static vdev_t * 25973d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 2598fa9e4066Sahrens { 2599ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 2600fa9e4066Sahrens int c; 2601fa9e4066Sahrens 2602ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) { 26033d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 2604ea8dc4b6Seschrock if (oldvd != NULL) 2605ea8dc4b6Seschrock return (oldvd); 2606ea8dc4b6Seschrock } 2607fa9e4066Sahrens 26083d7072f8Seschrock /* 26093d7072f8Seschrock * Check for a completed replacement. 26103d7072f8Seschrock */ 2611fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 2612ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 2613ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 2614ea8dc4b6Seschrock 2615ea8dc4b6Seschrock mutex_enter(&newvd->vdev_dtl_lock); 2616ea8dc4b6Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 2617ea8dc4b6Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 2618ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 2619ea8dc4b6Seschrock return (oldvd); 2620fa9e4066Sahrens } 2621ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 2622fa9e4066Sahrens } 2623ea8dc4b6Seschrock 26243d7072f8Seschrock /* 26253d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 26263d7072f8Seschrock */ 26273d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 26283d7072f8Seschrock newvd = vd->vdev_child[0]; 26293d7072f8Seschrock oldvd = vd->vdev_child[1]; 26303d7072f8Seschrock 26313d7072f8Seschrock mutex_enter(&newvd->vdev_dtl_lock); 26323d7072f8Seschrock if (newvd->vdev_unspare && 26333d7072f8Seschrock newvd->vdev_dtl_map.sm_space == 0 && 26343d7072f8Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 26353d7072f8Seschrock newvd->vdev_unspare = 0; 26363d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 26373d7072f8Seschrock return (oldvd); 26383d7072f8Seschrock } 26393d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 26403d7072f8Seschrock } 26413d7072f8Seschrock 2642ea8dc4b6Seschrock return (NULL); 2643fa9e4066Sahrens } 2644fa9e4066Sahrens 2645ea8dc4b6Seschrock static void 26463d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 2647fa9e4066Sahrens { 2648ea8dc4b6Seschrock vdev_t *vd; 264999653d4eSeschrock vdev_t *pvd; 2650ea8dc4b6Seschrock uint64_t guid; 265199653d4eSeschrock uint64_t pguid = 0; 2652ea8dc4b6Seschrock 2653ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2654ea8dc4b6Seschrock 26553d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 2656ea8dc4b6Seschrock guid = vd->vdev_guid; 265799653d4eSeschrock /* 265899653d4eSeschrock * If we have just finished replacing a hot spared device, then 265999653d4eSeschrock * we need to detach the parent's first child (the original hot 266099653d4eSeschrock * spare) as well. 266199653d4eSeschrock */ 266299653d4eSeschrock pvd = vd->vdev_parent; 266399653d4eSeschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 266499653d4eSeschrock pvd->vdev_id == 0) { 266599653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 266699653d4eSeschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 266799653d4eSeschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 266899653d4eSeschrock } 2669ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2670ea8dc4b6Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 2671ea8dc4b6Seschrock return; 267299653d4eSeschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 267399653d4eSeschrock return; 2674ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2675fa9e4066Sahrens } 2676fa9e4066Sahrens 2677ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2678fa9e4066Sahrens } 2679fa9e4066Sahrens 2680c67d9675Seschrock /* 2681c67d9675Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 2682c67d9675Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 2683c67d9675Seschrock */ 2684c67d9675Seschrock int 2685c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 2686c67d9675Seschrock { 2687c67d9675Seschrock vdev_t *rvd, *vd; 2688c67d9675Seschrock uint64_t txg; 2689c67d9675Seschrock 2690c67d9675Seschrock rvd = spa->spa_root_vdev; 2691c67d9675Seschrock 2692c67d9675Seschrock txg = spa_vdev_enter(spa); 2693c67d9675Seschrock 269499653d4eSeschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 269599653d4eSeschrock /* 269699653d4eSeschrock * Determine if this is a reference to a hot spare. In that 269799653d4eSeschrock * case, update the path as stored in the spare list. 269899653d4eSeschrock */ 269999653d4eSeschrock nvlist_t **spares; 270099653d4eSeschrock uint_t i, nspares; 270199653d4eSeschrock if (spa->spa_sparelist != NULL) { 270299653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 270399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 270499653d4eSeschrock for (i = 0; i < nspares; i++) { 270599653d4eSeschrock uint64_t theguid; 270699653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 270799653d4eSeschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 270899653d4eSeschrock if (theguid == guid) 270999653d4eSeschrock break; 271099653d4eSeschrock } 271199653d4eSeschrock 271299653d4eSeschrock if (i == nspares) 271399653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 271499653d4eSeschrock 271599653d4eSeschrock VERIFY(nvlist_add_string(spares[i], 271699653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0); 271799653d4eSeschrock spa_load_spares(spa); 271899653d4eSeschrock spa->spa_sync_spares = B_TRUE; 271999653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 272099653d4eSeschrock } else { 272199653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 272299653d4eSeschrock } 272399653d4eSeschrock } 2724c67d9675Seschrock 27250e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 27260e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 27270e34b6a7Sbonwick 2728c67d9675Seschrock spa_strfree(vd->vdev_path); 2729c67d9675Seschrock vd->vdev_path = spa_strdup(newpath); 2730c67d9675Seschrock 2731c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 2732c67d9675Seschrock 2733c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 2734c67d9675Seschrock } 2735c67d9675Seschrock 2736fa9e4066Sahrens /* 2737fa9e4066Sahrens * ========================================================================== 2738fa9e4066Sahrens * SPA Scrubbing 2739fa9e4066Sahrens * ========================================================================== 2740fa9e4066Sahrens */ 2741fa9e4066Sahrens 2742fa9e4066Sahrens static void 2743fa9e4066Sahrens spa_scrub_io_done(zio_t *zio) 2744fa9e4066Sahrens { 2745fa9e4066Sahrens spa_t *spa = zio->io_spa; 2746fa9e4066Sahrens 27470e8c6158Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 2748fa9e4066Sahrens 2749fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2750ea8dc4b6Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 275144cd46caSbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2752ea8dc4b6Seschrock spa->spa_scrub_errors++; 2753fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 2754fa9e4066Sahrens vd->vdev_stat.vs_scrub_errors++; 2755fa9e4066Sahrens mutex_exit(&vd->vdev_stat_lock); 2756fa9e4066Sahrens } 275705b2b3b8Smishra 275805b2b3b8Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 2759ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 276005b2b3b8Smishra 276105b2b3b8Smishra ASSERT(spa->spa_scrub_inflight >= 0); 276205b2b3b8Smishra 2763ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 2764fa9e4066Sahrens } 2765fa9e4066Sahrens 2766fa9e4066Sahrens static void 2767ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 2768ea8dc4b6Seschrock zbookmark_t *zb) 2769fa9e4066Sahrens { 2770fa9e4066Sahrens size_t size = BP_GET_LSIZE(bp); 277105b2b3b8Smishra void *data; 2772fa9e4066Sahrens 2773fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 277405b2b3b8Smishra /* 277505b2b3b8Smishra * Do not give too much work to vdev(s). 277605b2b3b8Smishra */ 277705b2b3b8Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 277805b2b3b8Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 277905b2b3b8Smishra } 2780fa9e4066Sahrens spa->spa_scrub_inflight++; 2781fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2782fa9e4066Sahrens 27830e8c6158Smaybee data = arc_data_buf_alloc(size); 278405b2b3b8Smishra 2785ea8dc4b6Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 2786ea8dc4b6Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 2787ea8dc4b6Seschrock 2788d80c45e0Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 2789ea8dc4b6Seschrock 2790fa9e4066Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 2791ea8dc4b6Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 2792fa9e4066Sahrens } 2793fa9e4066Sahrens 2794fa9e4066Sahrens /* ARGSUSED */ 2795fa9e4066Sahrens static int 2796fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2797fa9e4066Sahrens { 2798fa9e4066Sahrens blkptr_t *bp = &bc->bc_blkptr; 279944cd46caSbillm vdev_t *vd = spa->spa_root_vdev; 280044cd46caSbillm dva_t *dva = bp->blk_dva; 280144cd46caSbillm int needs_resilver = B_FALSE; 280244cd46caSbillm int d; 2803fa9e4066Sahrens 280444cd46caSbillm if (bc->bc_errno) { 2805fa9e4066Sahrens /* 2806fa9e4066Sahrens * We can't scrub this block, but we can continue to scrub 2807fa9e4066Sahrens * the rest of the pool. Note the error and move along. 2808fa9e4066Sahrens */ 2809fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2810fa9e4066Sahrens spa->spa_scrub_errors++; 2811fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2812fa9e4066Sahrens 281344cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 281444cd46caSbillm vd->vdev_stat.vs_scrub_errors++; 281544cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 2816fa9e4066Sahrens 2817fa9e4066Sahrens return (ERESTART); 2818fa9e4066Sahrens } 2819fa9e4066Sahrens 2820fa9e4066Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2821fa9e4066Sahrens 282244cd46caSbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 282344cd46caSbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 2824fa9e4066Sahrens 282544cd46caSbillm ASSERT(vd != NULL); 282644cd46caSbillm 282744cd46caSbillm /* 282844cd46caSbillm * Keep track of how much data we've examined so that 282944cd46caSbillm * zpool(1M) status can make useful progress reports. 283044cd46caSbillm */ 283144cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 283244cd46caSbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 283344cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 283444cd46caSbillm 283544cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 283644cd46caSbillm if (DVA_GET_GANG(&dva[d])) { 283744cd46caSbillm /* 283844cd46caSbillm * Gang members may be spread across multiple 283944cd46caSbillm * vdevs, so the best we can do is look at the 284044cd46caSbillm * pool-wide DTL. 284144cd46caSbillm * XXX -- it would be better to change our 284244cd46caSbillm * allocation policy to ensure that this can't 284344cd46caSbillm * happen. 284444cd46caSbillm */ 284544cd46caSbillm vd = spa->spa_root_vdev; 284644cd46caSbillm } 284744cd46caSbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 284844cd46caSbillm bp->blk_birth, 1)) 284944cd46caSbillm needs_resilver = B_TRUE; 2850fa9e4066Sahrens } 285144cd46caSbillm } 285244cd46caSbillm 285344cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2854fa9e4066Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 2855ea8dc4b6Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 285644cd46caSbillm else if (needs_resilver) 285744cd46caSbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 285844cd46caSbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2859fa9e4066Sahrens 2860fa9e4066Sahrens return (0); 2861fa9e4066Sahrens } 2862fa9e4066Sahrens 2863fa9e4066Sahrens static void 2864fa9e4066Sahrens spa_scrub_thread(spa_t *spa) 2865fa9e4066Sahrens { 2866fa9e4066Sahrens callb_cpr_t cprinfo; 2867fa9e4066Sahrens traverse_handle_t *th = spa->spa_scrub_th; 2868fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2869fa9e4066Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2870fa9e4066Sahrens int error = 0; 2871fa9e4066Sahrens boolean_t complete; 2872fa9e4066Sahrens 2873fa9e4066Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2874fa9e4066Sahrens 2875f0aa80d4Sbonwick /* 2876f0aa80d4Sbonwick * If we're restarting due to a snapshot create/delete, 2877f0aa80d4Sbonwick * wait for that to complete. 2878f0aa80d4Sbonwick */ 2879f0aa80d4Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 2880f0aa80d4Sbonwick 2881ea8dc4b6Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 2882ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2883ea8dc4b6Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 2884ea8dc4b6Seschrock 2885ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 2886ea8dc4b6Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 2887fa9e4066Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 2888fa9e4066Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 2889ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2890fa9e4066Sahrens 2891fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2892fa9e4066Sahrens spa->spa_scrub_errors = 0; 2893fa9e4066Sahrens spa->spa_scrub_active = 1; 2894ea8dc4b6Seschrock ASSERT(spa->spa_scrub_inflight == 0); 2895fa9e4066Sahrens 2896fa9e4066Sahrens while (!spa->spa_scrub_stop) { 2897fa9e4066Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 2898ea8dc4b6Seschrock while (spa->spa_scrub_suspended) { 2899fa9e4066Sahrens spa->spa_scrub_active = 0; 2900fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 2901fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2902fa9e4066Sahrens spa->spa_scrub_active = 1; 2903fa9e4066Sahrens } 2904fa9e4066Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2905fa9e4066Sahrens 2906fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 2907fa9e4066Sahrens break; 2908fa9e4066Sahrens 2909fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2910fa9e4066Sahrens error = traverse_more(th); 2911fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2912fa9e4066Sahrens if (error != EAGAIN) 2913fa9e4066Sahrens break; 2914fa9e4066Sahrens } 2915fa9e4066Sahrens 2916fa9e4066Sahrens while (spa->spa_scrub_inflight) 2917fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2918fa9e4066Sahrens 29195dabedeeSbonwick spa->spa_scrub_active = 0; 29205dabedeeSbonwick cv_broadcast(&spa->spa_scrub_cv); 29215dabedeeSbonwick 29225dabedeeSbonwick mutex_exit(&spa->spa_scrub_lock); 29235dabedeeSbonwick 29245dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 29255dabedeeSbonwick 29265dabedeeSbonwick mutex_enter(&spa->spa_scrub_lock); 29275dabedeeSbonwick 29285dabedeeSbonwick /* 29295dabedeeSbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 29305dabedeeSbonwick * AND the spa config lock to synchronize with any config changes 29315dabedeeSbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 29325dabedeeSbonwick */ 2933fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 2934fa9e4066Sahrens error = ERESTART; 2935fa9e4066Sahrens 2936ea8dc4b6Seschrock if (spa->spa_scrub_stop) 2937ea8dc4b6Seschrock error = EINTR; 2938ea8dc4b6Seschrock 2939fa9e4066Sahrens /* 2940ea8dc4b6Seschrock * Even if there were uncorrectable errors, we consider the scrub 2941ea8dc4b6Seschrock * completed. The downside is that if there is a transient error during 2942ea8dc4b6Seschrock * a resilver, we won't resilver the data properly to the target. But 2943ea8dc4b6Seschrock * if the damage is permanent (more likely) we will resilver forever, 2944ea8dc4b6Seschrock * which isn't really acceptable. Since there is enough information for 2945ea8dc4b6Seschrock * the user to know what has failed and why, this seems like a more 2946ea8dc4b6Seschrock * tractable approach. 2947fa9e4066Sahrens */ 2948ea8dc4b6Seschrock complete = (error == 0); 2949fa9e4066Sahrens 2950ea8dc4b6Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 2951ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2952fa9e4066Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2953fa9e4066Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2954fa9e4066Sahrens 2955fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 2956fa9e4066Sahrens 2957fa9e4066Sahrens /* 2958fa9e4066Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 2959fa9e4066Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 2960fa9e4066Sahrens */ 2961fa9e4066Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2962fa9e4066Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2963fa9e4066Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 2964ea8dc4b6Seschrock spa_errlog_rotate(spa); 29655dabedeeSbonwick 29663d7072f8Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 29673d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 29683d7072f8Seschrock 2969ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2970fa9e4066Sahrens 2971fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2972fa9e4066Sahrens 2973ea8dc4b6Seschrock /* 2974ea8dc4b6Seschrock * We may have finished replacing a device. 2975ea8dc4b6Seschrock * Let the async thread assess this and handle the detach. 2976ea8dc4b6Seschrock */ 29773d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2978fa9e4066Sahrens 2979fa9e4066Sahrens /* 2980fa9e4066Sahrens * If we were told to restart, our final act is to start a new scrub. 2981fa9e4066Sahrens */ 2982fa9e4066Sahrens if (error == ERESTART) 2983ea8dc4b6Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 2984ea8dc4b6Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2985fa9e4066Sahrens 2986ea8dc4b6Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 2987ea8dc4b6Seschrock spa->spa_scrub_active = 0; 2988ea8dc4b6Seschrock spa->spa_scrub_thread = NULL; 2989ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_cv); 2990fa9e4066Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2991fa9e4066Sahrens thread_exit(); 2992fa9e4066Sahrens } 2993fa9e4066Sahrens 2994fa9e4066Sahrens void 2995fa9e4066Sahrens spa_scrub_suspend(spa_t *spa) 2996fa9e4066Sahrens { 2997fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 2998ea8dc4b6Seschrock spa->spa_scrub_suspended++; 2999fa9e4066Sahrens while (spa->spa_scrub_active) { 3000fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3001fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3002fa9e4066Sahrens } 3003fa9e4066Sahrens while (spa->spa_scrub_inflight) 3004fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3005fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3006fa9e4066Sahrens } 3007fa9e4066Sahrens 3008fa9e4066Sahrens void 3009fa9e4066Sahrens spa_scrub_resume(spa_t *spa) 3010fa9e4066Sahrens { 3011fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3012ea8dc4b6Seschrock ASSERT(spa->spa_scrub_suspended != 0); 3013ea8dc4b6Seschrock if (--spa->spa_scrub_suspended == 0) 3014fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3015fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3016fa9e4066Sahrens } 3017fa9e4066Sahrens 3018fa9e4066Sahrens void 3019fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 3020fa9e4066Sahrens { 3021fa9e4066Sahrens /* 3022fa9e4066Sahrens * Something happened (e.g. snapshot create/delete) that means 3023fa9e4066Sahrens * we must restart any in-progress scrubs. The itinerary will 3024fa9e4066Sahrens * fix this properly. 3025fa9e4066Sahrens */ 3026fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3027fa9e4066Sahrens spa->spa_scrub_restart_txg = txg; 3028fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3029fa9e4066Sahrens } 3030fa9e4066Sahrens 3031ea8dc4b6Seschrock int 3032ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 3033fa9e4066Sahrens { 3034fa9e4066Sahrens space_seg_t *ss; 3035fa9e4066Sahrens uint64_t mintxg, maxtxg; 3036fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3037fa9e4066Sahrens 3038bb8b5132Sek ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3039bb8b5132Sek ASSERT(!spa_config_held(spa, RW_WRITER)); 3040bb8b5132Sek 3041fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3042fa9e4066Sahrens return (ENOTSUP); 3043fa9e4066Sahrens 3044ea8dc4b6Seschrock mutex_enter(&spa->spa_scrub_lock); 3045ea8dc4b6Seschrock 3046fa9e4066Sahrens /* 3047fa9e4066Sahrens * If there's a scrub or resilver already in progress, stop it. 3048fa9e4066Sahrens */ 3049fa9e4066Sahrens while (spa->spa_scrub_thread != NULL) { 3050fa9e4066Sahrens /* 3051fa9e4066Sahrens * Don't stop a resilver unless forced. 3052fa9e4066Sahrens */ 3053ea8dc4b6Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 3054ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3055fa9e4066Sahrens return (EBUSY); 3056ea8dc4b6Seschrock } 3057fa9e4066Sahrens spa->spa_scrub_stop = 1; 3058fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3059fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3060fa9e4066Sahrens } 3061fa9e4066Sahrens 3062fa9e4066Sahrens /* 3063fa9e4066Sahrens * Terminate the previous traverse. 3064fa9e4066Sahrens */ 3065fa9e4066Sahrens if (spa->spa_scrub_th != NULL) { 3066fa9e4066Sahrens traverse_fini(spa->spa_scrub_th); 3067fa9e4066Sahrens spa->spa_scrub_th = NULL; 3068fa9e4066Sahrens } 3069fa9e4066Sahrens 3070ea8dc4b6Seschrock if (rvd == NULL) { 3071ea8dc4b6Seschrock ASSERT(spa->spa_scrub_stop == 0); 3072ea8dc4b6Seschrock ASSERT(spa->spa_scrub_type == type); 3073ea8dc4b6Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 3074ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3075ea8dc4b6Seschrock return (0); 3076ea8dc4b6Seschrock } 3077fa9e4066Sahrens 3078fa9e4066Sahrens mintxg = TXG_INITIAL - 1; 3079fa9e4066Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 3080fa9e4066Sahrens 3081ea8dc4b6Seschrock mutex_enter(&rvd->vdev_dtl_lock); 3082fa9e4066Sahrens 3083ea8dc4b6Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 3084ea8dc4b6Seschrock /* 3085ea8dc4b6Seschrock * The pool-wide DTL is empty. 3086ecc2d604Sbonwick * If this is a resilver, there's nothing to do except 3087ecc2d604Sbonwick * check whether any in-progress replacements have completed. 3088ea8dc4b6Seschrock */ 3089ecc2d604Sbonwick if (type == POOL_SCRUB_RESILVER) { 3090ea8dc4b6Seschrock type = POOL_SCRUB_NONE; 30913d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3092ecc2d604Sbonwick } 3093ea8dc4b6Seschrock } else { 3094ea8dc4b6Seschrock /* 3095ea8dc4b6Seschrock * The pool-wide DTL is non-empty. 3096ea8dc4b6Seschrock * If this is a normal scrub, upgrade to a resilver instead. 3097ea8dc4b6Seschrock */ 3098ea8dc4b6Seschrock if (type == POOL_SCRUB_EVERYTHING) 3099ea8dc4b6Seschrock type = POOL_SCRUB_RESILVER; 3100ea8dc4b6Seschrock } 3101fa9e4066Sahrens 3102ea8dc4b6Seschrock if (type == POOL_SCRUB_RESILVER) { 3103fa9e4066Sahrens /* 3104fa9e4066Sahrens * Determine the resilvering boundaries. 3105fa9e4066Sahrens * 3106fa9e4066Sahrens * Note: (mintxg, maxtxg) is an open interval, 3107fa9e4066Sahrens * i.e. mintxg and maxtxg themselves are not included. 3108fa9e4066Sahrens * 3109fa9e4066Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 3110fa9e4066Sahrens * so we don't claim to resilver a txg that's still changing. 3111fa9e4066Sahrens */ 3112fa9e4066Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 3113ea8dc4b6Seschrock mintxg = ss->ss_start - 1; 3114fa9e4066Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 3115ea8dc4b6Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 31163d7072f8Seschrock 31173d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 3118fa9e4066Sahrens } 3119fa9e4066Sahrens 3120ea8dc4b6Seschrock mutex_exit(&rvd->vdev_dtl_lock); 3121ea8dc4b6Seschrock 3122ea8dc4b6Seschrock spa->spa_scrub_stop = 0; 3123ea8dc4b6Seschrock spa->spa_scrub_type = type; 3124ea8dc4b6Seschrock spa->spa_scrub_restart_txg = 0; 3125ea8dc4b6Seschrock 3126ea8dc4b6Seschrock if (type != POOL_SCRUB_NONE) { 3127ea8dc4b6Seschrock spa->spa_scrub_mintxg = mintxg; 3128fa9e4066Sahrens spa->spa_scrub_maxtxg = maxtxg; 3129fa9e4066Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 31300373e76bSbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 31310373e76bSbonwick ZIO_FLAG_CANFAIL); 3132fa9e4066Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 3133fa9e4066Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 3134fa9e4066Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 3135fa9e4066Sahrens } 3136fa9e4066Sahrens 3137ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3138ea8dc4b6Seschrock 3139fa9e4066Sahrens return (0); 3140fa9e4066Sahrens } 3141fa9e4066Sahrens 3142ea8dc4b6Seschrock /* 3143ea8dc4b6Seschrock * ========================================================================== 3144ea8dc4b6Seschrock * SPA async task processing 3145ea8dc4b6Seschrock * ========================================================================== 3146ea8dc4b6Seschrock */ 3147ea8dc4b6Seschrock 3148ea8dc4b6Seschrock static void 31493d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3150fa9e4066Sahrens { 3151ea8dc4b6Seschrock vdev_t *tvd; 3152ea8dc4b6Seschrock int c; 3153fa9e4066Sahrens 31543d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) { 31553d7072f8Seschrock tvd = vd->vdev_child[c]; 31563d7072f8Seschrock if (tvd->vdev_remove_wanted) { 31573d7072f8Seschrock tvd->vdev_remove_wanted = 0; 31583d7072f8Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 31593d7072f8Seschrock VDEV_AUX_NONE); 31600a4e9518Sgw vdev_clear(spa, tvd, B_TRUE); 31613d7072f8Seschrock vdev_config_dirty(tvd->vdev_top); 3162ea8dc4b6Seschrock } 31633d7072f8Seschrock spa_async_remove(spa, tvd); 3164ea8dc4b6Seschrock } 3165ea8dc4b6Seschrock } 3166fa9e4066Sahrens 3167ea8dc4b6Seschrock static void 3168ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 3169ea8dc4b6Seschrock { 3170ea8dc4b6Seschrock int tasks; 31713d7072f8Seschrock uint64_t txg; 3172ea8dc4b6Seschrock 3173ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 3174ea8dc4b6Seschrock 3175ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3176ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 3177ea8dc4b6Seschrock spa->spa_async_tasks = 0; 3178ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3179ea8dc4b6Seschrock 31800373e76bSbonwick /* 31810373e76bSbonwick * See if the config needs to be updated. 31820373e76bSbonwick */ 31830373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 31840373e76bSbonwick mutex_enter(&spa_namespace_lock); 31850373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 31860373e76bSbonwick mutex_exit(&spa_namespace_lock); 31870373e76bSbonwick } 31880373e76bSbonwick 3189ea8dc4b6Seschrock /* 31903d7072f8Seschrock * See if any devices need to be marked REMOVED. 31910a4e9518Sgw * 31920a4e9518Sgw * XXX - We avoid doing this when we are in 31930a4e9518Sgw * I/O failure state since spa_vdev_enter() grabs 31940a4e9518Sgw * the namespace lock and would not be able to obtain 31950a4e9518Sgw * the writer config lock. 3196ea8dc4b6Seschrock */ 31970a4e9518Sgw if (tasks & SPA_ASYNC_REMOVE && 31980a4e9518Sgw spa_state(spa) != POOL_STATE_IO_FAILURE) { 31993d7072f8Seschrock txg = spa_vdev_enter(spa); 32003d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 32013d7072f8Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 32023d7072f8Seschrock } 3203ea8dc4b6Seschrock 3204ea8dc4b6Seschrock /* 3205ea8dc4b6Seschrock * If any devices are done replacing, detach them. 3206ea8dc4b6Seschrock */ 32073d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 32083d7072f8Seschrock spa_vdev_resilver_done(spa); 3209fa9e4066Sahrens 3210ea8dc4b6Seschrock /* 32113d7072f8Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 32123d7072f8Seschrock * scrub which can become a resilver), we need to hold 32133d7072f8Seschrock * spa_namespace_lock() because the sysevent we post via 32143d7072f8Seschrock * spa_event_notify() needs to get the name of the pool. 3215ea8dc4b6Seschrock */ 32163d7072f8Seschrock if (tasks & SPA_ASYNC_SCRUB) { 32173d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3218ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 32193d7072f8Seschrock mutex_exit(&spa_namespace_lock); 32203d7072f8Seschrock } 3221ea8dc4b6Seschrock 3222ea8dc4b6Seschrock /* 3223ea8dc4b6Seschrock * Kick off a resilver. 3224ea8dc4b6Seschrock */ 32253d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER) { 32263d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3227ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 32283d7072f8Seschrock mutex_exit(&spa_namespace_lock); 32293d7072f8Seschrock } 3230ea8dc4b6Seschrock 3231ea8dc4b6Seschrock /* 3232ea8dc4b6Seschrock * Let the world know that we're done. 3233ea8dc4b6Seschrock */ 3234ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3235ea8dc4b6Seschrock spa->spa_async_thread = NULL; 3236ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 3237ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3238ea8dc4b6Seschrock thread_exit(); 3239ea8dc4b6Seschrock } 3240ea8dc4b6Seschrock 3241ea8dc4b6Seschrock void 3242ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 3243ea8dc4b6Seschrock { 3244ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3245ea8dc4b6Seschrock spa->spa_async_suspended++; 3246ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 3247ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 3248ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3249ea8dc4b6Seschrock } 3250ea8dc4b6Seschrock 3251ea8dc4b6Seschrock void 3252ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 3253ea8dc4b6Seschrock { 3254ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3255ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 3256ea8dc4b6Seschrock spa->spa_async_suspended--; 3257ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3258ea8dc4b6Seschrock } 3259ea8dc4b6Seschrock 3260ea8dc4b6Seschrock static void 3261ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 3262ea8dc4b6Seschrock { 3263ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3264ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 32650373e76bSbonwick spa->spa_async_thread == NULL && 32660373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 3267ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 3268ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 3269ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3270ea8dc4b6Seschrock } 3271ea8dc4b6Seschrock 3272ea8dc4b6Seschrock void 3273ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 3274ea8dc4b6Seschrock { 3275ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3276ea8dc4b6Seschrock spa->spa_async_tasks |= task; 3277ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3278fa9e4066Sahrens } 3279fa9e4066Sahrens 3280fa9e4066Sahrens /* 3281fa9e4066Sahrens * ========================================================================== 3282fa9e4066Sahrens * SPA syncing routines 3283fa9e4066Sahrens * ========================================================================== 3284fa9e4066Sahrens */ 3285fa9e4066Sahrens 3286fa9e4066Sahrens static void 3287fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3288fa9e4066Sahrens { 3289fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3290fa9e4066Sahrens dmu_tx_t *tx; 3291fa9e4066Sahrens blkptr_t blk; 3292fa9e4066Sahrens uint64_t itor = 0; 3293fa9e4066Sahrens zio_t *zio; 3294fa9e4066Sahrens int error; 3295fa9e4066Sahrens uint8_t c = 1; 3296fa9e4066Sahrens 3297fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3298fa9e4066Sahrens 3299fa9e4066Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3300fa9e4066Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3301fa9e4066Sahrens 3302fa9e4066Sahrens error = zio_wait(zio); 3303fa9e4066Sahrens ASSERT3U(error, ==, 0); 3304fa9e4066Sahrens 3305fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3306fa9e4066Sahrens bplist_vacate(bpl, tx); 3307fa9e4066Sahrens 3308fa9e4066Sahrens /* 3309fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 3310fa9e4066Sahrens * (Usually only the first block is needed.) 3311fa9e4066Sahrens */ 3312fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3313fa9e4066Sahrens dmu_tx_commit(tx); 3314fa9e4066Sahrens } 3315fa9e4066Sahrens 3316fa9e4066Sahrens static void 331799653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 3318fa9e4066Sahrens { 3319fa9e4066Sahrens char *packed = NULL; 3320fa9e4066Sahrens size_t nvsize = 0; 3321fa9e4066Sahrens dmu_buf_t *db; 3322fa9e4066Sahrens 332399653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 3324fa9e4066Sahrens 3325fa9e4066Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 3326fa9e4066Sahrens 332799653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 3328ea8dc4b6Seschrock KM_SLEEP) == 0); 3329fa9e4066Sahrens 333099653d4eSeschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 3331fa9e4066Sahrens 3332fa9e4066Sahrens kmem_free(packed, nvsize); 3333fa9e4066Sahrens 333499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3335fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 3336fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 3337ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 3338fa9e4066Sahrens } 3339fa9e4066Sahrens 334099653d4eSeschrock static void 334199653d4eSeschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 334299653d4eSeschrock { 334399653d4eSeschrock nvlist_t *nvroot; 334499653d4eSeschrock nvlist_t **spares; 334599653d4eSeschrock int i; 334699653d4eSeschrock 334799653d4eSeschrock if (!spa->spa_sync_spares) 334899653d4eSeschrock return; 334999653d4eSeschrock 335099653d4eSeschrock /* 335199653d4eSeschrock * Update the MOS nvlist describing the list of available spares. 335299653d4eSeschrock * spa_validate_spares() will have already made sure this nvlist is 33533d7072f8Seschrock * valid and the vdevs are labeled appropriately. 335499653d4eSeschrock */ 335599653d4eSeschrock if (spa->spa_spares_object == 0) { 335699653d4eSeschrock spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 335799653d4eSeschrock DMU_OT_PACKED_NVLIST, 1 << 14, 335899653d4eSeschrock DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 335999653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 336099653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 336199653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 336299653d4eSeschrock } 336399653d4eSeschrock 336499653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 336599653d4eSeschrock if (spa->spa_nspares == 0) { 336699653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 336799653d4eSeschrock NULL, 0) == 0); 336899653d4eSeschrock } else { 336999653d4eSeschrock spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 337099653d4eSeschrock KM_SLEEP); 337199653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 337299653d4eSeschrock spares[i] = vdev_config_generate(spa, 337399653d4eSeschrock spa->spa_spares[i], B_FALSE, B_TRUE); 337499653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 337599653d4eSeschrock spares, spa->spa_nspares) == 0); 337699653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 337799653d4eSeschrock nvlist_free(spares[i]); 337899653d4eSeschrock kmem_free(spares, spa->spa_nspares * sizeof (void *)); 337999653d4eSeschrock } 338099653d4eSeschrock 338199653d4eSeschrock spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 338206eeb2adSek nvlist_free(nvroot); 338399653d4eSeschrock 338499653d4eSeschrock spa->spa_sync_spares = B_FALSE; 338599653d4eSeschrock } 338699653d4eSeschrock 338799653d4eSeschrock static void 338899653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 338999653d4eSeschrock { 339099653d4eSeschrock nvlist_t *config; 339199653d4eSeschrock 339299653d4eSeschrock if (list_is_empty(&spa->spa_dirty_list)) 339399653d4eSeschrock return; 339499653d4eSeschrock 339599653d4eSeschrock config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 339699653d4eSeschrock 339799653d4eSeschrock if (spa->spa_config_syncing) 339899653d4eSeschrock nvlist_free(spa->spa_config_syncing); 339999653d4eSeschrock spa->spa_config_syncing = config; 340099653d4eSeschrock 340199653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 340299653d4eSeschrock } 340399653d4eSeschrock 3404990b4856Slling /* 3405990b4856Slling * Set zpool properties. 3406990b4856Slling */ 3407b1b8ab34Slling static void 3408ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 3409b1b8ab34Slling { 3410b1b8ab34Slling spa_t *spa = arg1; 3411b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 3412990b4856Slling nvlist_t *nvp = arg2; 3413990b4856Slling nvpair_t *elem; 34143d7072f8Seschrock uint64_t intval; 3415*2f8aaab3Seschrock char *strval, *slash; 3416990b4856Slling zpool_prop_t prop; 3417990b4856Slling const char *propname; 3418990b4856Slling zprop_type_t proptype; 3419b1b8ab34Slling 3420990b4856Slling elem = NULL; 3421990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 3422990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 3423990b4856Slling case ZPOOL_PROP_VERSION: 3424990b4856Slling /* 3425990b4856Slling * Only set version for non-zpool-creation cases 3426990b4856Slling * (set/import). spa_create() needs special care 3427990b4856Slling * for version setting. 3428990b4856Slling */ 3429990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 3430990b4856Slling VERIFY(nvpair_value_uint64(elem, 3431990b4856Slling &intval) == 0); 3432990b4856Slling ASSERT(intval <= SPA_VERSION); 3433990b4856Slling ASSERT(intval >= spa_version(spa)); 3434990b4856Slling spa->spa_uberblock.ub_version = intval; 3435990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 3436990b4856Slling } 3437ecd6cf80Smarks break; 3438990b4856Slling 3439990b4856Slling case ZPOOL_PROP_ALTROOT: 3440990b4856Slling /* 3441990b4856Slling * 'altroot' is a non-persistent property. It should 3442990b4856Slling * have been set temporarily at creation or import time. 3443990b4856Slling */ 3444990b4856Slling ASSERT(spa->spa_root != NULL); 3445b1b8ab34Slling break; 34463d7072f8Seschrock 3447*2f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 3448990b4856Slling /* 3449*2f8aaab3Seschrock * 'cachefile' is a non-persistent property, but note 3450*2f8aaab3Seschrock * an async request that the config cache needs to be 3451*2f8aaab3Seschrock * udpated. 3452990b4856Slling */ 3453*2f8aaab3Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 3454*2f8aaab3Seschrock if (spa->spa_config_dir) 3455*2f8aaab3Seschrock spa_strfree(spa->spa_config_dir); 3456*2f8aaab3Seschrock if (spa->spa_config_file) 3457*2f8aaab3Seschrock spa_strfree(spa->spa_config_file); 3458*2f8aaab3Seschrock 3459*2f8aaab3Seschrock if (strval[0] == '\0') { 3460*2f8aaab3Seschrock spa->spa_config_dir = NULL; 3461*2f8aaab3Seschrock spa->spa_config_file = NULL; 3462*2f8aaab3Seschrock } else if (strcmp(strval, "none") == 0) { 3463*2f8aaab3Seschrock spa->spa_config_dir = spa_strdup(strval); 3464*2f8aaab3Seschrock spa->spa_config_file = NULL; 3465*2f8aaab3Seschrock } else { 3466*2f8aaab3Seschrock slash = strrchr(strval, '/'); 3467*2f8aaab3Seschrock ASSERT(slash != NULL); 3468*2f8aaab3Seschrock *slash = '\0'; 3469*2f8aaab3Seschrock spa->spa_config_dir = spa_strdup(strval); 3470*2f8aaab3Seschrock spa->spa_config_file = spa_strdup(slash + 1); 3471*2f8aaab3Seschrock } 3472*2f8aaab3Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 34733d7072f8Seschrock break; 3474990b4856Slling default: 3475990b4856Slling /* 3476990b4856Slling * Set pool property values in the poolprops mos object. 3477990b4856Slling */ 3478990b4856Slling mutex_enter(&spa->spa_props_lock); 3479990b4856Slling if (spa->spa_pool_props_object == 0) { 3480990b4856Slling objset_t *mos = spa->spa_meta_objset; 3481990b4856Slling 3482990b4856Slling VERIFY((spa->spa_pool_props_object = 3483990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 3484990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 3485990b4856Slling 3486990b4856Slling VERIFY(zap_update(mos, 3487990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 3488990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 3489990b4856Slling == 0); 3490990b4856Slling } 3491990b4856Slling mutex_exit(&spa->spa_props_lock); 3492990b4856Slling 3493990b4856Slling /* normalize the property name */ 3494990b4856Slling propname = zpool_prop_to_name(prop); 3495990b4856Slling proptype = zpool_prop_get_type(prop); 3496990b4856Slling 3497990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 3498990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 3499990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 3500990b4856Slling VERIFY(zap_update(mos, 3501990b4856Slling spa->spa_pool_props_object, propname, 3502990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 3503990b4856Slling 3504990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 3505990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 3506990b4856Slling 3507990b4856Slling if (proptype == PROP_TYPE_INDEX) { 3508990b4856Slling const char *unused; 3509990b4856Slling VERIFY(zpool_prop_index_to_string( 3510990b4856Slling prop, intval, &unused) == 0); 3511990b4856Slling } 3512990b4856Slling VERIFY(zap_update(mos, 3513990b4856Slling spa->spa_pool_props_object, propname, 3514990b4856Slling 8, 1, &intval, tx) == 0); 3515990b4856Slling } else { 3516990b4856Slling ASSERT(0); /* not allowed */ 3517990b4856Slling } 3518990b4856Slling 35190a4e9518Sgw switch (prop) { 35200a4e9518Sgw case ZPOOL_PROP_DELEGATION: 3521990b4856Slling spa->spa_delegation = intval; 35220a4e9518Sgw break; 35230a4e9518Sgw case ZPOOL_PROP_BOOTFS: 3524990b4856Slling spa->spa_bootfs = intval; 35250a4e9518Sgw break; 35260a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 35270a4e9518Sgw spa->spa_failmode = intval; 35280a4e9518Sgw break; 35290a4e9518Sgw default: 35300a4e9518Sgw break; 35310a4e9518Sgw } 3532990b4856Slling } 3533990b4856Slling 3534990b4856Slling /* log internal history if this is not a zpool create */ 3535990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 3536990b4856Slling tx->tx_txg != TXG_INITIAL) { 3537990b4856Slling spa_history_internal_log(LOG_POOL_PROPSET, 3538990b4856Slling spa, tx, cr, "%s %lld %s", 3539990b4856Slling nvpair_name(elem), intval, spa->spa_name); 3540b1b8ab34Slling } 3541b1b8ab34Slling } 3542b1b8ab34Slling } 3543b1b8ab34Slling 3544fa9e4066Sahrens /* 3545fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3546fa9e4066Sahrens * part of the process, so we iterate until it converges. 3547fa9e4066Sahrens */ 3548fa9e4066Sahrens void 3549fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 3550fa9e4066Sahrens { 3551fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3552fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 3553fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 35540373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 3555fa9e4066Sahrens vdev_t *vd; 3556fa9e4066Sahrens dmu_tx_t *tx; 3557fa9e4066Sahrens int dirty_vdevs; 3558fa9e4066Sahrens 3559fa9e4066Sahrens /* 3560fa9e4066Sahrens * Lock out configuration changes. 3561fa9e4066Sahrens */ 3562ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 3563fa9e4066Sahrens 3564fa9e4066Sahrens spa->spa_syncing_txg = txg; 3565fa9e4066Sahrens spa->spa_sync_pass = 0; 3566fa9e4066Sahrens 3567ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3568fa9e4066Sahrens 356999653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 357099653d4eSeschrock 357199653d4eSeschrock /* 3572e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 357399653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 357499653d4eSeschrock */ 3575e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 3576e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 357799653d4eSeschrock int i; 357899653d4eSeschrock 357999653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 358099653d4eSeschrock vd = rvd->vdev_child[i]; 358199653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 358299653d4eSeschrock break; 358399653d4eSeschrock } 358499653d4eSeschrock if (i == rvd->vdev_children) { 358599653d4eSeschrock spa->spa_deflate = TRUE; 358699653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 358799653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 358899653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 358999653d4eSeschrock } 359099653d4eSeschrock } 359199653d4eSeschrock 3592fa9e4066Sahrens /* 3593fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 3594fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 3595fa9e4066Sahrens * don't generate work on an otherwise idle system. 3596fa9e4066Sahrens */ 3597fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 35981615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 35991615a317Sek !txg_list_empty(&dp->dp_sync_tasks, txg)) 3600fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 3601fa9e4066Sahrens 3602fa9e4066Sahrens /* 3603fa9e4066Sahrens * Iterate to convergence. 3604fa9e4066Sahrens */ 3605fa9e4066Sahrens do { 3606fa9e4066Sahrens spa->spa_sync_pass++; 3607fa9e4066Sahrens 3608fa9e4066Sahrens spa_sync_config_object(spa, tx); 360999653d4eSeschrock spa_sync_spares(spa, tx); 3610ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 3611fa9e4066Sahrens dsl_pool_sync(dp, txg); 3612fa9e4066Sahrens 3613fa9e4066Sahrens dirty_vdevs = 0; 3614fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3615fa9e4066Sahrens vdev_sync(vd, txg); 3616fa9e4066Sahrens dirty_vdevs++; 3617fa9e4066Sahrens } 3618fa9e4066Sahrens 3619fa9e4066Sahrens bplist_sync(bpl, tx); 3620fa9e4066Sahrens } while (dirty_vdevs); 3621fa9e4066Sahrens 3622fa9e4066Sahrens bplist_close(bpl); 3623fa9e4066Sahrens 3624fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3625fa9e4066Sahrens 3626fa9e4066Sahrens /* 3627fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3628fa9e4066Sahrens * to commit the transaction group. 36290373e76bSbonwick * 36300373e76bSbonwick * If there are any dirty vdevs, sync the uberblock to all vdevs. 36310373e76bSbonwick * Otherwise, pick a random top-level vdev that's known to be 36320373e76bSbonwick * visible in the config cache (see spa_vdev_add() for details). 36330373e76bSbonwick * If the write fails, try the next vdev until we're tried them all. 36340373e76bSbonwick */ 36350373e76bSbonwick if (!list_is_empty(&spa->spa_dirty_list)) { 36360373e76bSbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 36370373e76bSbonwick } else { 36380373e76bSbonwick int children = rvd->vdev_children; 36390373e76bSbonwick int c0 = spa_get_random(children); 36400373e76bSbonwick int c; 36410373e76bSbonwick 36420373e76bSbonwick for (c = 0; c < children; c++) { 36430373e76bSbonwick vd = rvd->vdev_child[(c0 + c) % children]; 36440373e76bSbonwick if (vd->vdev_ms_array == 0) 36450373e76bSbonwick continue; 36460373e76bSbonwick if (vdev_config_sync(vd, txg) == 0) 36470373e76bSbonwick break; 36480373e76bSbonwick } 36490373e76bSbonwick if (c == children) 36500373e76bSbonwick VERIFY(vdev_config_sync(rvd, txg) == 0); 36510373e76bSbonwick } 36520373e76bSbonwick 365399653d4eSeschrock dmu_tx_commit(tx); 365499653d4eSeschrock 36550373e76bSbonwick /* 36560373e76bSbonwick * Clear the dirty config list. 3657fa9e4066Sahrens */ 36580373e76bSbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 36590373e76bSbonwick vdev_config_clean(vd); 36600373e76bSbonwick 36610373e76bSbonwick /* 36620373e76bSbonwick * Now that the new config has synced transactionally, 36630373e76bSbonwick * let it become visible to the config cache. 36640373e76bSbonwick */ 36650373e76bSbonwick if (spa->spa_config_syncing != NULL) { 36660373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 36670373e76bSbonwick spa->spa_config_txg = txg; 36680373e76bSbonwick spa->spa_config_syncing = NULL; 36690373e76bSbonwick } 3670fa9e4066Sahrens 3671fa9e4066Sahrens /* 3672fa9e4066Sahrens * Make a stable copy of the fully synced uberblock. 3673fa9e4066Sahrens * We use this as the root for pool traversals. 3674fa9e4066Sahrens */ 3675fa9e4066Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3676fa9e4066Sahrens 3677fa9e4066Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3678fa9e4066Sahrens 3679fa9e4066Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3680fa9e4066Sahrens spa->spa_traverse_wanted = 0; 3681fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 3682fa9e4066Sahrens rw_exit(&spa->spa_traverse_lock); 3683fa9e4066Sahrens 3684fa9e4066Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3685fa9e4066Sahrens 3686fa9e4066Sahrens /* 3687fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 3688fa9e4066Sahrens */ 3689fa9e4066Sahrens dsl_pool_zil_clean(dp); 3690fa9e4066Sahrens 3691fa9e4066Sahrens /* 3692fa9e4066Sahrens * Update usable space statistics. 3693fa9e4066Sahrens */ 3694fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3695fa9e4066Sahrens vdev_sync_done(vd, txg); 3696fa9e4066Sahrens 3697fa9e4066Sahrens /* 3698fa9e4066Sahrens * It had better be the case that we didn't dirty anything 369999653d4eSeschrock * since vdev_config_sync(). 3700fa9e4066Sahrens */ 3701fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3702fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3703fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3704fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 3705fa9e4066Sahrens 3706ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3707ea8dc4b6Seschrock 3708ea8dc4b6Seschrock /* 3709ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 3710ea8dc4b6Seschrock */ 3711ea8dc4b6Seschrock spa_async_dispatch(spa); 3712fa9e4066Sahrens } 3713fa9e4066Sahrens 3714fa9e4066Sahrens /* 3715fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3716fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3717fa9e4066Sahrens * sync. 3718fa9e4066Sahrens */ 3719fa9e4066Sahrens void 3720fa9e4066Sahrens spa_sync_allpools(void) 3721fa9e4066Sahrens { 3722fa9e4066Sahrens spa_t *spa = NULL; 3723fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3724fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 3725fa9e4066Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3726fa9e4066Sahrens continue; 3727fa9e4066Sahrens spa_open_ref(spa, FTAG); 3728fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3729fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3730fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3731fa9e4066Sahrens spa_close(spa, FTAG); 3732fa9e4066Sahrens } 3733fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3734fa9e4066Sahrens } 3735fa9e4066Sahrens 3736fa9e4066Sahrens /* 3737fa9e4066Sahrens * ========================================================================== 3738fa9e4066Sahrens * Miscellaneous routines 3739fa9e4066Sahrens * ========================================================================== 3740fa9e4066Sahrens */ 3741fa9e4066Sahrens 3742fa9e4066Sahrens /* 3743fa9e4066Sahrens * Remove all pools in the system. 3744fa9e4066Sahrens */ 3745fa9e4066Sahrens void 3746fa9e4066Sahrens spa_evict_all(void) 3747fa9e4066Sahrens { 3748fa9e4066Sahrens spa_t *spa; 3749fa9e4066Sahrens 3750fa9e4066Sahrens /* 3751fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 3752fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 3753fa9e4066Sahrens */ 3754fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3755fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 3756fa9e4066Sahrens /* 3757ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 3758ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 3759ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 3760fa9e4066Sahrens */ 3761fa9e4066Sahrens spa_open_ref(spa, FTAG); 3762fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3763ea8dc4b6Seschrock spa_async_suspend(spa); 3764fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3765bb8b5132Sek VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3766fa9e4066Sahrens spa_close(spa, FTAG); 3767fa9e4066Sahrens 3768fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3769fa9e4066Sahrens spa_unload(spa); 3770fa9e4066Sahrens spa_deactivate(spa); 3771fa9e4066Sahrens } 3772fa9e4066Sahrens spa_remove(spa); 3773fa9e4066Sahrens } 3774fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3775fa9e4066Sahrens } 3776ea8dc4b6Seschrock 3777ea8dc4b6Seschrock vdev_t * 3778ea8dc4b6Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 3779ea8dc4b6Seschrock { 3780ea8dc4b6Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 3781ea8dc4b6Seschrock } 3782eaca9bbdSeschrock 3783eaca9bbdSeschrock void 3784990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 3785eaca9bbdSeschrock { 3786eaca9bbdSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 3787eaca9bbdSeschrock 3788eaca9bbdSeschrock /* 3789eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 3790eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 3791eaca9bbdSeschrock * possible. 3792eaca9bbdSeschrock */ 3793e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 3794990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 3795eaca9bbdSeschrock 3796990b4856Slling spa->spa_uberblock.ub_version = version; 3797eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 3798eaca9bbdSeschrock 3799eaca9bbdSeschrock spa_config_exit(spa, FTAG); 380099653d4eSeschrock 380199653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 380299653d4eSeschrock } 380399653d4eSeschrock 380499653d4eSeschrock boolean_t 380599653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 380699653d4eSeschrock { 380799653d4eSeschrock int i; 380839c23413Seschrock uint64_t spareguid; 380999653d4eSeschrock 381099653d4eSeschrock for (i = 0; i < spa->spa_nspares; i++) 381199653d4eSeschrock if (spa->spa_spares[i]->vdev_guid == guid) 381299653d4eSeschrock return (B_TRUE); 381399653d4eSeschrock 381439c23413Seschrock for (i = 0; i < spa->spa_pending_nspares; i++) { 381539c23413Seschrock if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 381639c23413Seschrock ZPOOL_CONFIG_GUID, &spareguid) == 0 && 381739c23413Seschrock spareguid == guid) 381839c23413Seschrock return (B_TRUE); 381939c23413Seschrock } 382039c23413Seschrock 382199653d4eSeschrock return (B_FALSE); 3822eaca9bbdSeschrock } 3823b1b8ab34Slling 38243d7072f8Seschrock /* 38253d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 38263d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 38273d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 38283d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 38293d7072f8Seschrock * or zdb as real changes. 38303d7072f8Seschrock */ 38313d7072f8Seschrock void 38323d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 38333d7072f8Seschrock { 38343d7072f8Seschrock #ifdef _KERNEL 38353d7072f8Seschrock sysevent_t *ev; 38363d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 38373d7072f8Seschrock sysevent_value_t value; 38383d7072f8Seschrock sysevent_id_t eid; 38393d7072f8Seschrock 38403d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 38413d7072f8Seschrock SE_SLEEP); 38423d7072f8Seschrock 38433d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 38443d7072f8Seschrock value.value.sv_string = spa_name(spa); 38453d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 38463d7072f8Seschrock goto done; 38473d7072f8Seschrock 38483d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 38493d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 38503d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 38513d7072f8Seschrock goto done; 38523d7072f8Seschrock 38533d7072f8Seschrock if (vd) { 38543d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 38553d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 38563d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 38573d7072f8Seschrock SE_SLEEP) != 0) 38583d7072f8Seschrock goto done; 38593d7072f8Seschrock 38603d7072f8Seschrock if (vd->vdev_path) { 38613d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 38623d7072f8Seschrock value.value.sv_string = vd->vdev_path; 38633d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 38643d7072f8Seschrock &value, SE_SLEEP) != 0) 38653d7072f8Seschrock goto done; 38663d7072f8Seschrock } 38673d7072f8Seschrock } 38683d7072f8Seschrock 38693d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 38703d7072f8Seschrock 38713d7072f8Seschrock done: 38723d7072f8Seschrock if (attr) 38733d7072f8Seschrock sysevent_free_attr(attr); 38743d7072f8Seschrock sysevent_free(ev); 38753d7072f8Seschrock #endif 38763d7072f8Seschrock } 3877