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 /* 23b01c3b58Seschrock * Copyright 2008 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> 59fa94a07fSbrendan #include <sys/arc.h> 60fa9e4066Sahrens #include <sys/callb.h> 6195173954Sek #include <sys/systeminfo.h> 6295173954Sek #include <sys/sunddi.h> 63*e7cbe64fSgw #include <sys/spa_boot.h> 64fa9e4066Sahrens 65990b4856Slling #include "zfs_prop.h" 66b7b97454Sperrin #include "zfs_comutil.h" 67990b4856Slling 68416e0cd8Sek int zio_taskq_threads = 8; 69416e0cd8Sek 70990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 71990b4856Slling 72990b4856Slling /* 73990b4856Slling * ========================================================================== 74990b4856Slling * SPA properties routines 75990b4856Slling * ========================================================================== 76990b4856Slling */ 77990b4856Slling 78990b4856Slling /* 79990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 80990b4856Slling */ 819d82f4f6Slling static void 82990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 83990b4856Slling uint64_t intval, zprop_source_t src) 84990b4856Slling { 85990b4856Slling const char *propname = zpool_prop_to_name(prop); 86990b4856Slling nvlist_t *propval; 87990b4856Slling 889d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 899d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 90990b4856Slling 919d82f4f6Slling if (strval != NULL) 929d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 939d82f4f6Slling else 949d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 95990b4856Slling 969d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 97990b4856Slling nvlist_free(propval); 98990b4856Slling } 99990b4856Slling 100990b4856Slling /* 101990b4856Slling * Get property values from the spa configuration. 102990b4856Slling */ 1039d82f4f6Slling static void 104990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 105990b4856Slling { 106990b4856Slling uint64_t size = spa_get_space(spa); 107990b4856Slling uint64_t used = spa_get_alloc(spa); 108990b4856Slling uint64_t cap, version; 109990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 1102f8aaab3Seschrock char *cachefile; 1112f8aaab3Seschrock size_t len; 112990b4856Slling 113990b4856Slling /* 114990b4856Slling * readonly properties 115990b4856Slling */ 1169d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src); 1179d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1189d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1199d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src); 120990b4856Slling 121990b4856Slling cap = (size == 0) ? 0 : (used * 100 / size); 1229d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 123990b4856Slling 1249d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1259d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1269d82f4f6Slling spa->spa_root_vdev->vdev_state, src); 127990b4856Slling 128990b4856Slling /* 129990b4856Slling * settable properties that are not stored in the pool property object. 130990b4856Slling */ 131990b4856Slling version = spa_version(spa); 132990b4856Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 133990b4856Slling src = ZPROP_SRC_DEFAULT; 134990b4856Slling else 135990b4856Slling src = ZPROP_SRC_LOCAL; 1369d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 137990b4856Slling 1389d82f4f6Slling if (spa->spa_root != NULL) 1399d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1409d82f4f6Slling 0, ZPROP_SRC_LOCAL); 141990b4856Slling 1422f8aaab3Seschrock if (spa->spa_config_dir != NULL) { 1432f8aaab3Seschrock if (strcmp(spa->spa_config_dir, "none") == 0) { 1449d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1452f8aaab3Seschrock spa->spa_config_dir, 0, ZPROP_SRC_LOCAL); 1462f8aaab3Seschrock } else { 1472f8aaab3Seschrock len = strlen(spa->spa_config_dir) + 1482f8aaab3Seschrock strlen(spa->spa_config_file) + 2; 1492f8aaab3Seschrock cachefile = kmem_alloc(len, KM_SLEEP); 1502f8aaab3Seschrock (void) snprintf(cachefile, len, "%s/%s", 1512f8aaab3Seschrock spa->spa_config_dir, spa->spa_config_file); 1529d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1532f8aaab3Seschrock cachefile, 0, ZPROP_SRC_LOCAL); 1542f8aaab3Seschrock kmem_free(cachefile, len); 1552f8aaab3Seschrock } 1562f8aaab3Seschrock } 157990b4856Slling } 158990b4856Slling 159990b4856Slling /* 160990b4856Slling * Get zpool property values. 161990b4856Slling */ 162990b4856Slling int 163990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 164990b4856Slling { 165990b4856Slling zap_cursor_t zc; 166990b4856Slling zap_attribute_t za; 167990b4856Slling objset_t *mos = spa->spa_meta_objset; 168990b4856Slling int err; 169990b4856Slling 1709d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 171990b4856Slling 172990b4856Slling /* 173990b4856Slling * Get properties from the spa config. 174990b4856Slling */ 1759d82f4f6Slling spa_prop_get_config(spa, nvp); 176990b4856Slling 177990b4856Slling mutex_enter(&spa->spa_props_lock); 178990b4856Slling /* If no pool property object, no more prop to get. */ 179990b4856Slling if (spa->spa_pool_props_object == 0) { 180990b4856Slling mutex_exit(&spa->spa_props_lock); 181990b4856Slling return (0); 182990b4856Slling } 183990b4856Slling 184990b4856Slling /* 185990b4856Slling * Get properties from the MOS pool property object. 186990b4856Slling */ 187990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 188990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 189990b4856Slling zap_cursor_advance(&zc)) { 190990b4856Slling uint64_t intval = 0; 191990b4856Slling char *strval = NULL; 192990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 193990b4856Slling zpool_prop_t prop; 194990b4856Slling 195990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 196990b4856Slling continue; 197990b4856Slling 198990b4856Slling switch (za.za_integer_length) { 199990b4856Slling case 8: 200990b4856Slling /* integer property */ 201990b4856Slling if (za.za_first_integer != 202990b4856Slling zpool_prop_default_numeric(prop)) 203990b4856Slling src = ZPROP_SRC_LOCAL; 204990b4856Slling 205990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 206990b4856Slling dsl_pool_t *dp; 207990b4856Slling dsl_dataset_t *ds = NULL; 208990b4856Slling 209990b4856Slling dp = spa_get_dsl(spa); 210990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 211990b4856Slling if (err = dsl_dataset_open_obj(dp, 212990b4856Slling za.za_first_integer, NULL, DS_MODE_NONE, 213990b4856Slling FTAG, &ds)) { 214990b4856Slling rw_exit(&dp->dp_config_rwlock); 215990b4856Slling break; 216990b4856Slling } 217990b4856Slling 218990b4856Slling strval = kmem_alloc( 219990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 220990b4856Slling KM_SLEEP); 221990b4856Slling dsl_dataset_name(ds, strval); 222990b4856Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 223990b4856Slling rw_exit(&dp->dp_config_rwlock); 224990b4856Slling } else { 225990b4856Slling strval = NULL; 226990b4856Slling intval = za.za_first_integer; 227990b4856Slling } 228990b4856Slling 2299d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 230990b4856Slling 231990b4856Slling if (strval != NULL) 232990b4856Slling kmem_free(strval, 233990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 234990b4856Slling 235990b4856Slling break; 236990b4856Slling 237990b4856Slling case 1: 238990b4856Slling /* string property */ 239990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 240990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 241990b4856Slling za.za_name, 1, za.za_num_integers, strval); 242990b4856Slling if (err) { 243990b4856Slling kmem_free(strval, za.za_num_integers); 244990b4856Slling break; 245990b4856Slling } 2469d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 247990b4856Slling kmem_free(strval, za.za_num_integers); 248990b4856Slling break; 249990b4856Slling 250990b4856Slling default: 251990b4856Slling break; 252990b4856Slling } 253990b4856Slling } 254990b4856Slling zap_cursor_fini(&zc); 255990b4856Slling mutex_exit(&spa->spa_props_lock); 256990b4856Slling out: 257990b4856Slling if (err && err != ENOENT) { 258990b4856Slling nvlist_free(*nvp); 2599d82f4f6Slling *nvp = NULL; 260990b4856Slling return (err); 261990b4856Slling } 262990b4856Slling 263990b4856Slling return (0); 264990b4856Slling } 265990b4856Slling 266990b4856Slling /* 267990b4856Slling * Validate the given pool properties nvlist and modify the list 268990b4856Slling * for the property values to be set. 269990b4856Slling */ 270990b4856Slling static int 271990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 272990b4856Slling { 273990b4856Slling nvpair_t *elem; 274990b4856Slling int error = 0, reset_bootfs = 0; 275990b4856Slling uint64_t objnum; 276990b4856Slling 277990b4856Slling elem = NULL; 278990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 279990b4856Slling zpool_prop_t prop; 280990b4856Slling char *propname, *strval; 281990b4856Slling uint64_t intval; 282990b4856Slling vdev_t *rvdev; 283990b4856Slling char *vdev_type; 284990b4856Slling objset_t *os; 2852f8aaab3Seschrock char *slash; 286990b4856Slling 287990b4856Slling propname = nvpair_name(elem); 288990b4856Slling 289990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 290990b4856Slling return (EINVAL); 291990b4856Slling 292990b4856Slling switch (prop) { 293990b4856Slling case ZPOOL_PROP_VERSION: 294990b4856Slling error = nvpair_value_uint64(elem, &intval); 295990b4856Slling if (!error && 296990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 297990b4856Slling error = EINVAL; 298990b4856Slling break; 299990b4856Slling 300990b4856Slling case ZPOOL_PROP_DELEGATION: 301990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 302990b4856Slling error = nvpair_value_uint64(elem, &intval); 303990b4856Slling if (!error && intval > 1) 304990b4856Slling error = EINVAL; 305990b4856Slling break; 306990b4856Slling 307990b4856Slling case ZPOOL_PROP_BOOTFS: 308990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 309990b4856Slling error = ENOTSUP; 310990b4856Slling break; 311990b4856Slling } 312990b4856Slling 313990b4856Slling /* 314990b4856Slling * A bootable filesystem can not be on a RAIDZ pool 315990b4856Slling * nor a striped pool with more than 1 device. 316990b4856Slling */ 317990b4856Slling rvdev = spa->spa_root_vdev; 318990b4856Slling vdev_type = 319990b4856Slling rvdev->vdev_child[0]->vdev_ops->vdev_op_type; 320990b4856Slling if (rvdev->vdev_children > 1 || 321990b4856Slling strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 322990b4856Slling strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 323990b4856Slling error = ENOTSUP; 324990b4856Slling break; 325990b4856Slling } 326990b4856Slling 327990b4856Slling reset_bootfs = 1; 328990b4856Slling 329990b4856Slling error = nvpair_value_string(elem, &strval); 330990b4856Slling 331990b4856Slling if (!error) { 332990b4856Slling if (strval == NULL || strval[0] == '\0') { 333990b4856Slling objnum = zpool_prop_default_numeric( 334990b4856Slling ZPOOL_PROP_BOOTFS); 335990b4856Slling break; 336990b4856Slling } 337990b4856Slling 338990b4856Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 339990b4856Slling DS_MODE_STANDARD | DS_MODE_READONLY, &os)) 340990b4856Slling break; 341990b4856Slling objnum = dmu_objset_id(os); 342990b4856Slling dmu_objset_close(os); 343990b4856Slling } 344990b4856Slling break; 3450a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 3460a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 3470a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3480a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 3490a4e9518Sgw error = EINVAL; 3500a4e9518Sgw 3510a4e9518Sgw /* 3520a4e9518Sgw * This is a special case which only occurs when 3530a4e9518Sgw * the pool has completely failed. This allows 3540a4e9518Sgw * the user to change the in-core failmode property 3550a4e9518Sgw * without syncing it out to disk (I/Os might 3560a4e9518Sgw * currently be blocked). We do this by returning 3570a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 3580a4e9518Sgw * into thinking we encountered a property validation 3590a4e9518Sgw * error. 3600a4e9518Sgw */ 3610a4e9518Sgw if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3620a4e9518Sgw spa->spa_failmode = intval; 3630a4e9518Sgw error = EIO; 3640a4e9518Sgw } 3650a4e9518Sgw break; 3662f8aaab3Seschrock 3672f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 3682f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3692f8aaab3Seschrock break; 3702f8aaab3Seschrock 3712f8aaab3Seschrock if (strval[0] == '\0') 3722f8aaab3Seschrock break; 3732f8aaab3Seschrock 3742f8aaab3Seschrock if (strcmp(strval, "none") == 0) 3752f8aaab3Seschrock break; 3762f8aaab3Seschrock 3772f8aaab3Seschrock if (strval[0] != '/') { 3782f8aaab3Seschrock error = EINVAL; 3792f8aaab3Seschrock break; 3802f8aaab3Seschrock } 3812f8aaab3Seschrock 3822f8aaab3Seschrock slash = strrchr(strval, '/'); 3832f8aaab3Seschrock ASSERT(slash != NULL); 3842f8aaab3Seschrock 3852f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3862f8aaab3Seschrock strcmp(slash, "/..") == 0) 3872f8aaab3Seschrock error = EINVAL; 3882f8aaab3Seschrock break; 389990b4856Slling } 390990b4856Slling 391990b4856Slling if (error) 392990b4856Slling break; 393990b4856Slling } 394990b4856Slling 395990b4856Slling if (!error && reset_bootfs) { 396990b4856Slling error = nvlist_remove(props, 397990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 398990b4856Slling 399990b4856Slling if (!error) { 400990b4856Slling error = nvlist_add_uint64(props, 401990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 402990b4856Slling } 403990b4856Slling } 404990b4856Slling 405990b4856Slling return (error); 406990b4856Slling } 407990b4856Slling 408990b4856Slling int 409990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 410990b4856Slling { 411990b4856Slling int error; 412990b4856Slling 413990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 414990b4856Slling return (error); 415990b4856Slling 416990b4856Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 417990b4856Slling spa, nvp, 3)); 418990b4856Slling } 419990b4856Slling 420990b4856Slling /* 421990b4856Slling * If the bootfs property value is dsobj, clear it. 422990b4856Slling */ 423990b4856Slling void 424990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 425990b4856Slling { 426990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 427990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 428990b4856Slling spa->spa_pool_props_object, 429990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 430990b4856Slling spa->spa_bootfs = 0; 431990b4856Slling } 432990b4856Slling } 433990b4856Slling 434fa9e4066Sahrens /* 435fa9e4066Sahrens * ========================================================================== 436fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 437fa9e4066Sahrens * ========================================================================== 438fa9e4066Sahrens */ 439fa9e4066Sahrens 440ea8dc4b6Seschrock static int 441ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 442ea8dc4b6Seschrock { 443ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 444ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 445ea8dc4b6Seschrock int ret; 446ea8dc4b6Seschrock 447ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 448ea8dc4b6Seschrock sizeof (zbookmark_t)); 449ea8dc4b6Seschrock 450ea8dc4b6Seschrock if (ret < 0) 451ea8dc4b6Seschrock return (-1); 452ea8dc4b6Seschrock else if (ret > 0) 453ea8dc4b6Seschrock return (1); 454ea8dc4b6Seschrock else 455ea8dc4b6Seschrock return (0); 456ea8dc4b6Seschrock } 457ea8dc4b6Seschrock 458ea8dc4b6Seschrock /* 459ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 460ea8dc4b6Seschrock * re-initializes them in the process. 461ea8dc4b6Seschrock */ 462ea8dc4b6Seschrock void 463ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 464ea8dc4b6Seschrock { 465ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 466ea8dc4b6Seschrock 467ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 468ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 469ea8dc4b6Seschrock 470ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 471ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 472ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 473ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 474ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 475ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 476ea8dc4b6Seschrock } 477ea8dc4b6Seschrock 478fa9e4066Sahrens /* 479fa9e4066Sahrens * Activate an uninitialized pool. 480fa9e4066Sahrens */ 481fa9e4066Sahrens static void 482fa9e4066Sahrens spa_activate(spa_t *spa) 483fa9e4066Sahrens { 484fa9e4066Sahrens int t; 485fa9e4066Sahrens 486fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 487fa9e4066Sahrens 488fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 489fa9e4066Sahrens 490fa9e4066Sahrens spa->spa_normal_class = metaslab_class_create(); 4918654d025Sperrin spa->spa_log_class = metaslab_class_create(); 492fa9e4066Sahrens 493fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 494fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 495416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 496fa9e4066Sahrens TASKQ_PREPOPULATE); 497fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 498416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 499fa9e4066Sahrens TASKQ_PREPOPULATE); 500fa9e4066Sahrens } 501fa9e4066Sahrens 502fa9e4066Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 503fa9e4066Sahrens offsetof(vdev_t, vdev_dirty_node)); 5040a4e9518Sgw list_create(&spa->spa_zio_list, sizeof (zio_t), 5050a4e9518Sgw offsetof(zio_t, zio_link_node)); 506fa9e4066Sahrens 507fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 508fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 509ea8dc4b6Seschrock 510ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 511ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 512ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 513ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 514ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 515ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 516fa9e4066Sahrens } 517fa9e4066Sahrens 518fa9e4066Sahrens /* 519fa9e4066Sahrens * Opposite of spa_activate(). 520fa9e4066Sahrens */ 521fa9e4066Sahrens static void 522fa9e4066Sahrens spa_deactivate(spa_t *spa) 523fa9e4066Sahrens { 524fa9e4066Sahrens int t; 525fa9e4066Sahrens 526fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 527fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 528fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 529fa9e4066Sahrens 530fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 531fa9e4066Sahrens 532fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 533fa9e4066Sahrens 534fa9e4066Sahrens list_destroy(&spa->spa_dirty_list); 5350a4e9518Sgw list_destroy(&spa->spa_zio_list); 536fa9e4066Sahrens 537fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 538fa9e4066Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 539fa9e4066Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 540fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = NULL; 541fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = NULL; 542fa9e4066Sahrens } 543fa9e4066Sahrens 544fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 545fa9e4066Sahrens spa->spa_normal_class = NULL; 546fa9e4066Sahrens 5478654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 5488654d025Sperrin spa->spa_log_class = NULL; 5498654d025Sperrin 550ea8dc4b6Seschrock /* 551ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 552ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 553ea8dc4b6Seschrock */ 554ea8dc4b6Seschrock spa_errlog_drain(spa); 555ea8dc4b6Seschrock 556ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 557ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 558ea8dc4b6Seschrock 559fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 560fa9e4066Sahrens } 561fa9e4066Sahrens 562fa9e4066Sahrens /* 563fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 564fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 565fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 566fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 567fa9e4066Sahrens */ 56899653d4eSeschrock static int 56999653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 57099653d4eSeschrock uint_t id, int atype) 571fa9e4066Sahrens { 572fa9e4066Sahrens nvlist_t **child; 573fa9e4066Sahrens uint_t c, children; 57499653d4eSeschrock int error; 575fa9e4066Sahrens 57699653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 57799653d4eSeschrock return (error); 578fa9e4066Sahrens 57999653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 58099653d4eSeschrock return (0); 581fa9e4066Sahrens 582fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 583fa9e4066Sahrens &child, &children) != 0) { 58499653d4eSeschrock vdev_free(*vdp); 58599653d4eSeschrock *vdp = NULL; 58699653d4eSeschrock return (EINVAL); 587fa9e4066Sahrens } 588fa9e4066Sahrens 589fa9e4066Sahrens for (c = 0; c < children; c++) { 59099653d4eSeschrock vdev_t *vd; 59199653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 59299653d4eSeschrock atype)) != 0) { 59399653d4eSeschrock vdev_free(*vdp); 59499653d4eSeschrock *vdp = NULL; 59599653d4eSeschrock return (error); 596fa9e4066Sahrens } 597fa9e4066Sahrens } 598fa9e4066Sahrens 59999653d4eSeschrock ASSERT(*vdp != NULL); 60099653d4eSeschrock 60199653d4eSeschrock return (0); 602fa9e4066Sahrens } 603fa9e4066Sahrens 604fa9e4066Sahrens /* 605fa9e4066Sahrens * Opposite of spa_load(). 606fa9e4066Sahrens */ 607fa9e4066Sahrens static void 608fa9e4066Sahrens spa_unload(spa_t *spa) 609fa9e4066Sahrens { 61099653d4eSeschrock int i; 61199653d4eSeschrock 612ea8dc4b6Seschrock /* 613ea8dc4b6Seschrock * Stop async tasks. 614ea8dc4b6Seschrock */ 615ea8dc4b6Seschrock spa_async_suspend(spa); 616ea8dc4b6Seschrock 617fa9e4066Sahrens /* 618fa9e4066Sahrens * Stop syncing. 619fa9e4066Sahrens */ 620fa9e4066Sahrens if (spa->spa_sync_on) { 621fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 622fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 623fa9e4066Sahrens } 624fa9e4066Sahrens 625fa9e4066Sahrens /* 626fa9e4066Sahrens * Wait for any outstanding prefetch I/O to complete. 627fa9e4066Sahrens */ 628ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 629ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 630fa9e4066Sahrens 631fa94a07fSbrendan /* 632fa94a07fSbrendan * Drop and purge level 2 cache 633fa94a07fSbrendan */ 634fa94a07fSbrendan spa_l2cache_drop(spa); 635fa94a07fSbrendan 636fa9e4066Sahrens /* 637fa9e4066Sahrens * Close the dsl pool. 638fa9e4066Sahrens */ 639fa9e4066Sahrens if (spa->spa_dsl_pool) { 640fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 641fa9e4066Sahrens spa->spa_dsl_pool = NULL; 642fa9e4066Sahrens } 643fa9e4066Sahrens 644fa9e4066Sahrens /* 645fa9e4066Sahrens * Close all vdevs. 646fa9e4066Sahrens */ 6470e34b6a7Sbonwick if (spa->spa_root_vdev) 648fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 6490e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 650ea8dc4b6Seschrock 651fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 652fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 653fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 654fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 655fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 656fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 65799653d4eSeschrock } 658fa94a07fSbrendan if (spa->spa_spares.sav_config) { 659fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 660fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 661fa94a07fSbrendan } 662fa94a07fSbrendan 663fa94a07fSbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 664fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 665fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 666fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 667fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 668fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 669fa94a07fSbrendan } 670fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 671fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 672fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 67399653d4eSeschrock } 67499653d4eSeschrock 675ea8dc4b6Seschrock spa->spa_async_suspended = 0; 676fa9e4066Sahrens } 677fa9e4066Sahrens 67899653d4eSeschrock /* 67999653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 68099653d4eSeschrock * this pool. When this is called, we have some form of basic information in 681fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 682fa94a07fSbrendan * then re-generate a more complete list including status information. 68399653d4eSeschrock */ 68499653d4eSeschrock static void 68599653d4eSeschrock spa_load_spares(spa_t *spa) 68699653d4eSeschrock { 68799653d4eSeschrock nvlist_t **spares; 68899653d4eSeschrock uint_t nspares; 68999653d4eSeschrock int i; 69039c23413Seschrock vdev_t *vd, *tvd; 69199653d4eSeschrock 69299653d4eSeschrock /* 69399653d4eSeschrock * First, close and free any existing spare vdevs. 69499653d4eSeschrock */ 695fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 696fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 69739c23413Seschrock 69839c23413Seschrock /* Undo the call to spa_activate() below */ 69939c23413Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 70039c23413Seschrock tvd->vdev_isspare) 70139c23413Seschrock spa_spare_remove(tvd); 70239c23413Seschrock vdev_close(vd); 70339c23413Seschrock vdev_free(vd); 70499653d4eSeschrock } 70539c23413Seschrock 706fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 707fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 708fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 70999653d4eSeschrock 710fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 71199653d4eSeschrock nspares = 0; 71299653d4eSeschrock else 713fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 71499653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 71599653d4eSeschrock 716fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 717fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 71899653d4eSeschrock 71999653d4eSeschrock if (nspares == 0) 72099653d4eSeschrock return; 72199653d4eSeschrock 72299653d4eSeschrock /* 72399653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 72439c23413Seschrock * process. For each spare, there is potentially two different vdev_t 72539c23413Seschrock * structures associated with it: one in the list of spares (used only 72639c23413Seschrock * for basic validation purposes) and one in the active vdev 72739c23413Seschrock * configuration (if it's spared in). During this phase we open and 72839c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 72939c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 73099653d4eSeschrock */ 731fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 732fa94a07fSbrendan KM_SLEEP); 733fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 73499653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 73599653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 73699653d4eSeschrock ASSERT(vd != NULL); 73799653d4eSeschrock 738fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 73999653d4eSeschrock 74039c23413Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 74139c23413Seschrock if (!tvd->vdev_isspare) 74239c23413Seschrock spa_spare_add(tvd); 74339c23413Seschrock 74439c23413Seschrock /* 74539c23413Seschrock * We only mark the spare active if we were successfully 74639c23413Seschrock * able to load the vdev. Otherwise, importing a pool 74739c23413Seschrock * with a bad active spare would result in strange 74839c23413Seschrock * behavior, because multiple pool would think the spare 74939c23413Seschrock * is actively in use. 75039c23413Seschrock * 75139c23413Seschrock * There is a vulnerability here to an equally bizarre 75239c23413Seschrock * circumstance, where a dead active spare is later 75339c23413Seschrock * brought back to life (onlined or otherwise). Given 75439c23413Seschrock * the rarity of this scenario, and the extra complexity 75539c23413Seschrock * it adds, we ignore the possibility. 75639c23413Seschrock */ 75739c23413Seschrock if (!vdev_is_dead(tvd)) 75839c23413Seschrock spa_spare_activate(tvd); 75939c23413Seschrock } 76039c23413Seschrock 76199653d4eSeschrock if (vdev_open(vd) != 0) 76299653d4eSeschrock continue; 76399653d4eSeschrock 76499653d4eSeschrock vd->vdev_top = vd; 765fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 766fa94a07fSbrendan spa_spare_add(vd); 76799653d4eSeschrock } 76899653d4eSeschrock 76999653d4eSeschrock /* 77099653d4eSeschrock * Recompute the stashed list of spares, with status information 77199653d4eSeschrock * this time. 77299653d4eSeschrock */ 773fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 77499653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 77599653d4eSeschrock 776fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 777fa94a07fSbrendan KM_SLEEP); 778fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 779fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 780fa94a07fSbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 781fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 782fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 783fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 78499653d4eSeschrock nvlist_free(spares[i]); 785fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 786fa94a07fSbrendan } 787fa94a07fSbrendan 788fa94a07fSbrendan /* 789fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 790fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 791fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 792fa94a07fSbrendan * then re-generate a more complete list including status information. 793fa94a07fSbrendan * Devices which are already active have their details maintained, and are 794fa94a07fSbrendan * not re-opened. 795fa94a07fSbrendan */ 796fa94a07fSbrendan static void 797fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 798fa94a07fSbrendan { 799fa94a07fSbrendan nvlist_t **l2cache; 800fa94a07fSbrendan uint_t nl2cache; 801fa94a07fSbrendan int i, j, oldnvdevs; 802fa94a07fSbrendan uint64_t guid; 803fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 804fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 805fa94a07fSbrendan 806fa94a07fSbrendan if (sav->sav_config != NULL) { 807fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 808fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 809fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 810fa94a07fSbrendan } else { 811fa94a07fSbrendan nl2cache = 0; 812fa94a07fSbrendan } 813fa94a07fSbrendan 814fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 815fa94a07fSbrendan oldnvdevs = sav->sav_count; 816fa94a07fSbrendan sav->sav_vdevs = NULL; 817fa94a07fSbrendan sav->sav_count = 0; 818fa94a07fSbrendan 819fa94a07fSbrendan /* 820fa94a07fSbrendan * Process new nvlist of vdevs. 821fa94a07fSbrendan */ 822fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 823fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 824fa94a07fSbrendan &guid) == 0); 825fa94a07fSbrendan 826fa94a07fSbrendan newvdevs[i] = NULL; 827fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 828fa94a07fSbrendan vd = oldvdevs[j]; 829fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 830fa94a07fSbrendan /* 831fa94a07fSbrendan * Retain previous vdev for add/remove ops. 832fa94a07fSbrendan */ 833fa94a07fSbrendan newvdevs[i] = vd; 834fa94a07fSbrendan oldvdevs[j] = NULL; 835fa94a07fSbrendan break; 836fa94a07fSbrendan } 837fa94a07fSbrendan } 838fa94a07fSbrendan 839fa94a07fSbrendan if (newvdevs[i] == NULL) { 840fa94a07fSbrendan /* 841fa94a07fSbrendan * Create new vdev 842fa94a07fSbrendan */ 843fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 844fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 845fa94a07fSbrendan ASSERT(vd != NULL); 846fa94a07fSbrendan newvdevs[i] = vd; 847fa94a07fSbrendan 848fa94a07fSbrendan /* 849fa94a07fSbrendan * Commit this vdev as an l2cache device, 850fa94a07fSbrendan * even if it fails to open. 851fa94a07fSbrendan */ 852fa94a07fSbrendan spa_l2cache_add(vd); 853fa94a07fSbrendan 854fa94a07fSbrendan if (vdev_open(vd) != 0) 855fa94a07fSbrendan continue; 856fa94a07fSbrendan 857fa94a07fSbrendan vd->vdev_top = vd; 858fa94a07fSbrendan (void) vdev_validate_aux(vd); 859fa94a07fSbrendan 860fa94a07fSbrendan if (!vdev_is_dead(vd)) { 861fa94a07fSbrendan uint64_t size; 862fa94a07fSbrendan size = vdev_get_rsize(vd); 863fa94a07fSbrendan ASSERT3U(size, >, 0); 864fa94a07fSbrendan if (spa_mode & FWRITE) { 865fa94a07fSbrendan l2arc_add_vdev(spa, vd, 866fa94a07fSbrendan VDEV_LABEL_START_SIZE, 867fa94a07fSbrendan size - VDEV_LABEL_START_SIZE); 868fa94a07fSbrendan } 869fa94a07fSbrendan spa_l2cache_activate(vd); 870fa94a07fSbrendan } 871fa94a07fSbrendan } 872fa94a07fSbrendan } 873fa94a07fSbrendan 874fa94a07fSbrendan /* 875fa94a07fSbrendan * Purge vdevs that were dropped 876fa94a07fSbrendan */ 877fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 878fa94a07fSbrendan uint64_t pool; 879fa94a07fSbrendan 880fa94a07fSbrendan vd = oldvdevs[i]; 881fa94a07fSbrendan if (vd != NULL) { 882fa94a07fSbrendan if (spa_mode & FWRITE && 883fa94a07fSbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 884fa94a07fSbrendan pool != 0ULL) { 885fa94a07fSbrendan l2arc_remove_vdev(vd); 886fa94a07fSbrendan } 887fa94a07fSbrendan (void) vdev_close(vd); 888fa94a07fSbrendan spa_l2cache_remove(vd); 889fa94a07fSbrendan } 890fa94a07fSbrendan } 891fa94a07fSbrendan 892fa94a07fSbrendan if (oldvdevs) 893fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 894fa94a07fSbrendan 895fa94a07fSbrendan if (sav->sav_config == NULL) 896fa94a07fSbrendan goto out; 897fa94a07fSbrendan 898fa94a07fSbrendan sav->sav_vdevs = newvdevs; 899fa94a07fSbrendan sav->sav_count = (int)nl2cache; 900fa94a07fSbrendan 901fa94a07fSbrendan /* 902fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 903fa94a07fSbrendan * information this time. 904fa94a07fSbrendan */ 905fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 906fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 907fa94a07fSbrendan 908fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 909fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 910fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 911fa94a07fSbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 912fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 913fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 914fa94a07fSbrendan out: 915fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 916fa94a07fSbrendan nvlist_free(l2cache[i]); 917fa94a07fSbrendan if (sav->sav_count) 918fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 91999653d4eSeschrock } 92099653d4eSeschrock 92199653d4eSeschrock static int 92299653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 92399653d4eSeschrock { 92499653d4eSeschrock dmu_buf_t *db; 92599653d4eSeschrock char *packed = NULL; 92699653d4eSeschrock size_t nvsize = 0; 92799653d4eSeschrock int error; 92899653d4eSeschrock *value = NULL; 92999653d4eSeschrock 93099653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 93199653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 93299653d4eSeschrock dmu_buf_rele(db, FTAG); 93399653d4eSeschrock 93499653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 93599653d4eSeschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 93699653d4eSeschrock if (error == 0) 93799653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 93899653d4eSeschrock kmem_free(packed, nvsize); 93999653d4eSeschrock 94099653d4eSeschrock return (error); 94199653d4eSeschrock } 94299653d4eSeschrock 9433d7072f8Seschrock /* 9443d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9453d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9463d7072f8Seschrock */ 9473d7072f8Seschrock static void 9483d7072f8Seschrock spa_check_removed(vdev_t *vd) 9493d7072f8Seschrock { 9503d7072f8Seschrock int c; 9513d7072f8Seschrock 9523d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) 9533d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 9543d7072f8Seschrock 9553d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9563d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9573d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9583d7072f8Seschrock } 9593d7072f8Seschrock } 9603d7072f8Seschrock 961fa9e4066Sahrens /* 962fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 963ea8dc4b6Seschrock * source of configuration information. 964fa9e4066Sahrens */ 965fa9e4066Sahrens static int 966ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 967fa9e4066Sahrens { 968fa9e4066Sahrens int error = 0; 969fa9e4066Sahrens nvlist_t *nvroot = NULL; 970fa9e4066Sahrens vdev_t *rvd; 971fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 9720373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 973fa9e4066Sahrens uint64_t pool_guid; 97499653d4eSeschrock uint64_t version; 975fa9e4066Sahrens zio_t *zio; 9763d7072f8Seschrock uint64_t autoreplace = 0; 977fa9e4066Sahrens 978ea8dc4b6Seschrock spa->spa_load_state = state; 9790373e76bSbonwick 980fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 981a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 982ea8dc4b6Seschrock error = EINVAL; 983ea8dc4b6Seschrock goto out; 984ea8dc4b6Seschrock } 985fa9e4066Sahrens 98699653d4eSeschrock /* 98799653d4eSeschrock * Versioning wasn't explicitly added to the label until later, so if 98899653d4eSeschrock * it's not present treat it as the initial version. 98999653d4eSeschrock */ 99099653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 991e7437265Sahrens version = SPA_VERSION_INITIAL; 99299653d4eSeschrock 993a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 994a9926bf0Sbonwick &spa->spa_config_txg); 995a9926bf0Sbonwick 9960373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 997ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 998ea8dc4b6Seschrock error = EEXIST; 999ea8dc4b6Seschrock goto out; 1000ea8dc4b6Seschrock } 1001fa9e4066Sahrens 1002b5989ec7Seschrock spa->spa_load_guid = pool_guid; 1003b5989ec7Seschrock 1004fa9e4066Sahrens /* 100599653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 100699653d4eSeschrock * value that will be returned by spa_version() since parsing the 100799653d4eSeschrock * configuration requires knowing the version number. 1008fa9e4066Sahrens */ 1009ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 101099653d4eSeschrock spa->spa_ubsync.ub_version = version; 101199653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 1012ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1013fa9e4066Sahrens 101499653d4eSeschrock if (error != 0) 1015ea8dc4b6Seschrock goto out; 1016fa9e4066Sahrens 10170e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1018fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 1019fa9e4066Sahrens 1020fa9e4066Sahrens /* 1021fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 1022fa9e4066Sahrens */ 10230bf246f5Smc error = vdev_open(rvd); 10240bf246f5Smc if (error != 0) 1025ea8dc4b6Seschrock goto out; 1026fa9e4066Sahrens 1027560e6e96Seschrock /* 1028560e6e96Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 1029560e6e96Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 1030560e6e96Seschrock * flag. 1031560e6e96Seschrock */ 1032560e6e96Seschrock spa_config_enter(spa, RW_READER, FTAG); 1033560e6e96Seschrock error = vdev_validate(rvd); 1034560e6e96Seschrock spa_config_exit(spa, FTAG); 1035560e6e96Seschrock 10360bf246f5Smc if (error != 0) 1037560e6e96Seschrock goto out; 1038560e6e96Seschrock 1039560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1040560e6e96Seschrock error = ENXIO; 1041560e6e96Seschrock goto out; 1042560e6e96Seschrock } 1043560e6e96Seschrock 1044fa9e4066Sahrens /* 1045fa9e4066Sahrens * Find the best uberblock. 1046fa9e4066Sahrens */ 1047fa9e4066Sahrens bzero(ub, sizeof (uberblock_t)); 1048fa9e4066Sahrens 1049fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, 1050fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1051fa9e4066Sahrens vdev_uberblock_load(zio, rvd, ub); 1052fa9e4066Sahrens error = zio_wait(zio); 1053fa9e4066Sahrens 1054fa9e4066Sahrens /* 1055fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 1056fa9e4066Sahrens */ 1057fa9e4066Sahrens if (ub->ub_txg == 0) { 1058eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1059eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 1060ea8dc4b6Seschrock error = ENXIO; 1061ea8dc4b6Seschrock goto out; 1062ea8dc4b6Seschrock } 1063ea8dc4b6Seschrock 1064ea8dc4b6Seschrock /* 1065ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 1066ea8dc4b6Seschrock */ 1067e7437265Sahrens if (ub->ub_version > SPA_VERSION) { 1068eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1069eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 1070ea8dc4b6Seschrock error = ENOTSUP; 1071ea8dc4b6Seschrock goto out; 1072fa9e4066Sahrens } 1073fa9e4066Sahrens 1074fa9e4066Sahrens /* 1075fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1076fa9e4066Sahrens * incomplete configuration. 1077fa9e4066Sahrens */ 1078ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 1079ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1080ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 1081ea8dc4b6Seschrock error = ENXIO; 1082ea8dc4b6Seschrock goto out; 1083fa9e4066Sahrens } 1084fa9e4066Sahrens 1085fa9e4066Sahrens /* 1086fa9e4066Sahrens * Initialize internal SPA structures. 1087fa9e4066Sahrens */ 1088fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1089fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1090fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 1091ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 1092ea8dc4b6Seschrock if (error) { 1093ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1094ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1095ea8dc4b6Seschrock goto out; 1096ea8dc4b6Seschrock } 1097fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1098fa9e4066Sahrens 1099ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1100fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1101ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 1102ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1103ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1104ea8dc4b6Seschrock error = EIO; 1105ea8dc4b6Seschrock goto out; 1106ea8dc4b6Seschrock } 1107fa9e4066Sahrens 1108fa9e4066Sahrens if (!mosconfig) { 110999653d4eSeschrock nvlist_t *newconfig; 111095173954Sek uint64_t hostid; 1111fa9e4066Sahrens 111299653d4eSeschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 1113ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1114ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1115ea8dc4b6Seschrock error = EIO; 1116ea8dc4b6Seschrock goto out; 1117ea8dc4b6Seschrock } 1118fa9e4066Sahrens 111995173954Sek if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 112095173954Sek &hostid) == 0) { 112195173954Sek char *hostname; 112295173954Sek unsigned long myhostid = 0; 112395173954Sek 112495173954Sek VERIFY(nvlist_lookup_string(newconfig, 112595173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 112695173954Sek 112795173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 112817194a52Slling if (hostid != 0 && myhostid != 0 && 112917194a52Slling (unsigned long)hostid != myhostid) { 113095173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 113195173954Sek "loaded as it was last accessed by " 113295173954Sek "another system (host: %s hostid: 0x%lx). " 113395173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 113495173954Sek spa->spa_name, hostname, 113595173954Sek (unsigned long)hostid); 113695173954Sek error = EBADF; 113795173954Sek goto out; 113895173954Sek } 113995173954Sek } 114095173954Sek 1141fa9e4066Sahrens spa_config_set(spa, newconfig); 1142fa9e4066Sahrens spa_unload(spa); 1143fa9e4066Sahrens spa_deactivate(spa); 1144fa9e4066Sahrens spa_activate(spa); 1145fa9e4066Sahrens 1146ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 1147fa9e4066Sahrens } 1148fa9e4066Sahrens 1149ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1150fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1151ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 1152ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1153ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1154ea8dc4b6Seschrock error = EIO; 1155ea8dc4b6Seschrock goto out; 1156ea8dc4b6Seschrock } 1157fa9e4066Sahrens 115899653d4eSeschrock /* 115999653d4eSeschrock * Load the bit that tells us to use the new accounting function 116099653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 116199653d4eSeschrock * be present. 116299653d4eSeschrock */ 116399653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, 116499653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 116599653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate); 116699653d4eSeschrock if (error != 0 && error != ENOENT) { 116799653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 116899653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 116999653d4eSeschrock error = EIO; 117099653d4eSeschrock goto out; 117199653d4eSeschrock } 117299653d4eSeschrock 1173fa9e4066Sahrens /* 1174ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1175ea8dc4b6Seschrock * not be present. 1176fa9e4066Sahrens */ 1177ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1178ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 1179ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 1180d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 1181ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1182ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1183ea8dc4b6Seschrock error = EIO; 1184ea8dc4b6Seschrock goto out; 1185ea8dc4b6Seschrock } 1186ea8dc4b6Seschrock 1187ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1188ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1189ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1190ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 1191ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1192ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1193ea8dc4b6Seschrock error = EIO; 1194ea8dc4b6Seschrock goto out; 1195ea8dc4b6Seschrock } 1196ea8dc4b6Seschrock 119706eeb2adSek /* 119806eeb2adSek * Load the history object. If we have an older pool, this 119906eeb2adSek * will not be present. 120006eeb2adSek */ 120106eeb2adSek error = zap_lookup(spa->spa_meta_objset, 120206eeb2adSek DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 120306eeb2adSek sizeof (uint64_t), 1, &spa->spa_history); 120406eeb2adSek if (error != 0 && error != ENOENT) { 120506eeb2adSek vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 120606eeb2adSek VDEV_AUX_CORRUPT_DATA); 120706eeb2adSek error = EIO; 120806eeb2adSek goto out; 120906eeb2adSek } 121006eeb2adSek 121199653d4eSeschrock /* 121299653d4eSeschrock * Load any hot spares for this pool. 121399653d4eSeschrock */ 121499653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1215fa94a07fSbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 121699653d4eSeschrock if (error != 0 && error != ENOENT) { 121799653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 121899653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 121999653d4eSeschrock error = EIO; 122099653d4eSeschrock goto out; 122199653d4eSeschrock } 122299653d4eSeschrock if (error == 0) { 1223e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 1224fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 1225fa94a07fSbrendan &spa->spa_spares.sav_config) != 0) { 122699653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 122799653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 122899653d4eSeschrock error = EIO; 122999653d4eSeschrock goto out; 123099653d4eSeschrock } 123199653d4eSeschrock 123299653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 123399653d4eSeschrock spa_load_spares(spa); 123499653d4eSeschrock spa_config_exit(spa, FTAG); 123599653d4eSeschrock } 123699653d4eSeschrock 1237fa94a07fSbrendan /* 1238fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 1239fa94a07fSbrendan */ 1240fa94a07fSbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1241fa94a07fSbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 1242fa94a07fSbrendan &spa->spa_l2cache.sav_object); 1243fa94a07fSbrendan if (error != 0 && error != ENOENT) { 1244fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1245fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1246fa94a07fSbrendan error = EIO; 1247fa94a07fSbrendan goto out; 1248fa94a07fSbrendan } 1249fa94a07fSbrendan if (error == 0) { 1250fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 1251fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 1252fa94a07fSbrendan &spa->spa_l2cache.sav_config) != 0) { 1253fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, 1254fa94a07fSbrendan VDEV_STATE_CANT_OPEN, 1255fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1256fa94a07fSbrendan error = EIO; 1257fa94a07fSbrendan goto out; 1258fa94a07fSbrendan } 1259fa94a07fSbrendan 1260fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 1261fa94a07fSbrendan spa_load_l2cache(spa); 1262fa94a07fSbrendan spa_config_exit(spa, FTAG); 1263fa94a07fSbrendan } 1264fa94a07fSbrendan 1265990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1266ecd6cf80Smarks 1267b1b8ab34Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1268b1b8ab34Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1269b1b8ab34Slling 1270b1b8ab34Slling if (error && error != ENOENT) { 1271b1b8ab34Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1272b1b8ab34Slling VDEV_AUX_CORRUPT_DATA); 1273b1b8ab34Slling error = EIO; 1274b1b8ab34Slling goto out; 1275b1b8ab34Slling } 1276b1b8ab34Slling 1277b1b8ab34Slling if (error == 0) { 1278b1b8ab34Slling (void) zap_lookup(spa->spa_meta_objset, 1279b1b8ab34Slling spa->spa_pool_props_object, 12803d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1281b1b8ab34Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 12823d7072f8Seschrock (void) zap_lookup(spa->spa_meta_objset, 12833d7072f8Seschrock spa->spa_pool_props_object, 12843d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 12853d7072f8Seschrock sizeof (uint64_t), 1, &autoreplace); 1286ecd6cf80Smarks (void) zap_lookup(spa->spa_meta_objset, 1287ecd6cf80Smarks spa->spa_pool_props_object, 1288ecd6cf80Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1289ecd6cf80Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 12900a4e9518Sgw (void) zap_lookup(spa->spa_meta_objset, 12910a4e9518Sgw spa->spa_pool_props_object, 12920a4e9518Sgw zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 12930a4e9518Sgw sizeof (uint64_t), 1, &spa->spa_failmode); 1294b1b8ab34Slling } 1295b1b8ab34Slling 12963d7072f8Seschrock /* 12973d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 12983d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 12993d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 13003d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 13013d7072f8Seschrock * over. 13023d7072f8Seschrock */ 1303b01c3b58Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13043d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 13053d7072f8Seschrock 1306ea8dc4b6Seschrock /* 1307560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1308ea8dc4b6Seschrock */ 1309560e6e96Seschrock vdev_load(rvd); 13100373e76bSbonwick 1311fa9e4066Sahrens /* 1312fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1313fa9e4066Sahrens */ 1314ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1315fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1316ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1317fa9e4066Sahrens 1318fa9e4066Sahrens /* 1319fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1320fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1321fa9e4066Sahrens */ 1322ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1323ea8dc4b6Seschrock error = ENXIO; 1324ea8dc4b6Seschrock goto out; 1325ea8dc4b6Seschrock } 1326fa9e4066Sahrens 1327ea8dc4b6Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13285dabedeeSbonwick dmu_tx_t *tx; 13290373e76bSbonwick int need_update = B_FALSE; 13300373e76bSbonwick int c; 13315dabedeeSbonwick 13320373e76bSbonwick /* 13330373e76bSbonwick * Claim log blocks that haven't been committed yet. 13340373e76bSbonwick * This must all happen in a single txg. 13350373e76bSbonwick */ 13365dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1337fa9e4066Sahrens spa_first_txg(spa)); 13380b69c2f0Sahrens (void) dmu_objset_find(spa->spa_name, 13390b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1340fa9e4066Sahrens dmu_tx_commit(tx); 1341fa9e4066Sahrens 1342fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1343fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1344fa9e4066Sahrens 1345fa9e4066Sahrens /* 1346fa9e4066Sahrens * Wait for all claims to sync. 1347fa9e4066Sahrens */ 1348fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13490e34b6a7Sbonwick 13500e34b6a7Sbonwick /* 13510373e76bSbonwick * If the config cache is stale, or we have uninitialized 13520373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 13530e34b6a7Sbonwick */ 13540373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 13550373e76bSbonwick state == SPA_LOAD_IMPORT) 13560373e76bSbonwick need_update = B_TRUE; 13570373e76bSbonwick 13580373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) 13590373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13600373e76bSbonwick need_update = B_TRUE; 13610e34b6a7Sbonwick 13620e34b6a7Sbonwick /* 13630373e76bSbonwick * Update the config cache asychronously in case we're the 13640373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 13650e34b6a7Sbonwick */ 13660373e76bSbonwick if (need_update) 13670373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1368fa9e4066Sahrens } 1369fa9e4066Sahrens 1370ea8dc4b6Seschrock error = 0; 1371ea8dc4b6Seschrock out: 137299653d4eSeschrock if (error && error != EBADF) 1373ea8dc4b6Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 1374ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 1375ea8dc4b6Seschrock spa->spa_ena = 0; 1376ea8dc4b6Seschrock 1377ea8dc4b6Seschrock return (error); 1378fa9e4066Sahrens } 1379fa9e4066Sahrens 1380fa9e4066Sahrens /* 1381fa9e4066Sahrens * Pool Open/Import 1382fa9e4066Sahrens * 1383fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 1384fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1385fa9e4066Sahrens * case of an open, the pool configuration will exist in the 13863d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 1387fa9e4066Sahrens * 1388fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1389fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 1390fa9e4066Sahrens * ambiguous state. 1391fa9e4066Sahrens */ 1392fa9e4066Sahrens static int 1393fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1394fa9e4066Sahrens { 1395fa9e4066Sahrens spa_t *spa; 1396fa9e4066Sahrens int error; 1397fa9e4066Sahrens int loaded = B_FALSE; 1398fa9e4066Sahrens int locked = B_FALSE; 1399fa9e4066Sahrens 1400fa9e4066Sahrens *spapp = NULL; 1401fa9e4066Sahrens 1402fa9e4066Sahrens /* 1403fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 1404fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1405fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 1406fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 1407fa9e4066Sahrens */ 1408fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1409fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1410fa9e4066Sahrens locked = B_TRUE; 1411fa9e4066Sahrens } 1412fa9e4066Sahrens 1413fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1414fa9e4066Sahrens if (locked) 1415fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1416fa9e4066Sahrens return (ENOENT); 1417fa9e4066Sahrens } 1418fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1419fa9e4066Sahrens 1420fa9e4066Sahrens spa_activate(spa); 1421fa9e4066Sahrens 14220373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1423fa9e4066Sahrens 1424fa9e4066Sahrens if (error == EBADF) { 1425fa9e4066Sahrens /* 1426560e6e96Seschrock * If vdev_validate() returns failure (indicated by 1427560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 1428560e6e96Seschrock * that the pool has been exported or destroyed. If 1429560e6e96Seschrock * this is the case, the config cache is out of sync and 1430560e6e96Seschrock * we should remove the pool from the namespace. 1431fa9e4066Sahrens */ 143299653d4eSeschrock zfs_post_ok(spa, NULL); 1433fa9e4066Sahrens spa_unload(spa); 1434fa9e4066Sahrens spa_deactivate(spa); 1435fa9e4066Sahrens spa_remove(spa); 1436fa9e4066Sahrens spa_config_sync(); 1437fa9e4066Sahrens if (locked) 1438fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1439fa9e4066Sahrens return (ENOENT); 1440ea8dc4b6Seschrock } 1441ea8dc4b6Seschrock 1442ea8dc4b6Seschrock if (error) { 1443fa9e4066Sahrens /* 1444fa9e4066Sahrens * We can't open the pool, but we still have useful 1445fa9e4066Sahrens * information: the state of each vdev after the 1446fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 1447fa9e4066Sahrens */ 14480373e76bSbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14490373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 1450fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1451fa9e4066Sahrens B_TRUE); 14520373e76bSbonwick spa_config_exit(spa, FTAG); 14530373e76bSbonwick } 1454fa9e4066Sahrens spa_unload(spa); 1455fa9e4066Sahrens spa_deactivate(spa); 1456ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 1457fa9e4066Sahrens if (locked) 1458fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1459fa9e4066Sahrens *spapp = NULL; 1460fa9e4066Sahrens return (error); 1461ea8dc4b6Seschrock } else { 1462ea8dc4b6Seschrock zfs_post_ok(spa, NULL); 1463ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 1464fa9e4066Sahrens } 1465fa9e4066Sahrens 1466fa9e4066Sahrens loaded = B_TRUE; 1467fa9e4066Sahrens } 1468fa9e4066Sahrens 1469fa9e4066Sahrens spa_open_ref(spa, tag); 14703d7072f8Seschrock 14713d7072f8Seschrock /* 14723d7072f8Seschrock * If we just loaded the pool, resilver anything that's out of date. 14733d7072f8Seschrock */ 14743d7072f8Seschrock if (loaded && (spa_mode & FWRITE)) 14753d7072f8Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 14763d7072f8Seschrock 1477fa9e4066Sahrens if (locked) 1478fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1479fa9e4066Sahrens 1480fa9e4066Sahrens *spapp = spa; 1481fa9e4066Sahrens 1482fa9e4066Sahrens if (config != NULL) { 1483ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1484fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1485ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1486fa9e4066Sahrens } 1487fa9e4066Sahrens 1488fa9e4066Sahrens return (0); 1489fa9e4066Sahrens } 1490fa9e4066Sahrens 1491fa9e4066Sahrens int 1492fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1493fa9e4066Sahrens { 1494fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1495fa9e4066Sahrens } 1496fa9e4066Sahrens 1497ea8dc4b6Seschrock /* 1498ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 1499ea8dc4b6Seschrock * preventing it from being exported or destroyed. 1500ea8dc4b6Seschrock */ 1501ea8dc4b6Seschrock spa_t * 1502ea8dc4b6Seschrock spa_inject_addref(char *name) 1503ea8dc4b6Seschrock { 1504ea8dc4b6Seschrock spa_t *spa; 1505ea8dc4b6Seschrock 1506ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1507ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 1508ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1509ea8dc4b6Seschrock return (NULL); 1510ea8dc4b6Seschrock } 1511ea8dc4b6Seschrock spa->spa_inject_ref++; 1512ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1513ea8dc4b6Seschrock 1514ea8dc4b6Seschrock return (spa); 1515ea8dc4b6Seschrock } 1516ea8dc4b6Seschrock 1517ea8dc4b6Seschrock void 1518ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 1519ea8dc4b6Seschrock { 1520ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1521ea8dc4b6Seschrock spa->spa_inject_ref--; 1522ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1523ea8dc4b6Seschrock } 1524ea8dc4b6Seschrock 1525fa94a07fSbrendan /* 1526fa94a07fSbrendan * Add spares device information to the nvlist. 1527fa94a07fSbrendan */ 152899653d4eSeschrock static void 152999653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 153099653d4eSeschrock { 153199653d4eSeschrock nvlist_t **spares; 153299653d4eSeschrock uint_t i, nspares; 153399653d4eSeschrock nvlist_t *nvroot; 153499653d4eSeschrock uint64_t guid; 153599653d4eSeschrock vdev_stat_t *vs; 153699653d4eSeschrock uint_t vsc; 153739c23413Seschrock uint64_t pool; 153899653d4eSeschrock 1539fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 154099653d4eSeschrock return; 154199653d4eSeschrock 154299653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 154399653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1544fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 154599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 154699653d4eSeschrock if (nspares != 0) { 154799653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 154899653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 154999653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 155099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 155199653d4eSeschrock 155299653d4eSeschrock /* 155399653d4eSeschrock * Go through and find any spares which have since been 155499653d4eSeschrock * repurposed as an active spare. If this is the case, update 155599653d4eSeschrock * their status appropriately. 155699653d4eSeschrock */ 155799653d4eSeschrock for (i = 0; i < nspares; i++) { 155899653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 155999653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 156039c23413Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 156199653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 156299653d4eSeschrock spares[i], ZPOOL_CONFIG_STATS, 156399653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 156499653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 156599653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 156699653d4eSeschrock } 156799653d4eSeschrock } 156899653d4eSeschrock } 156999653d4eSeschrock } 157099653d4eSeschrock 1571fa94a07fSbrendan /* 1572fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 1573fa94a07fSbrendan */ 1574fa94a07fSbrendan static void 1575fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 1576fa94a07fSbrendan { 1577fa94a07fSbrendan nvlist_t **l2cache; 1578fa94a07fSbrendan uint_t i, j, nl2cache; 1579fa94a07fSbrendan nvlist_t *nvroot; 1580fa94a07fSbrendan uint64_t guid; 1581fa94a07fSbrendan vdev_t *vd; 1582fa94a07fSbrendan vdev_stat_t *vs; 1583fa94a07fSbrendan uint_t vsc; 1584fa94a07fSbrendan 1585fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 1586fa94a07fSbrendan return; 1587fa94a07fSbrendan 1588fa94a07fSbrendan spa_config_enter(spa, RW_READER, FTAG); 1589fa94a07fSbrendan 1590fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 1591fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1592fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 1593fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1594fa94a07fSbrendan if (nl2cache != 0) { 1595fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 1596fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 1597fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 1598fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1599fa94a07fSbrendan 1600fa94a07fSbrendan /* 1601fa94a07fSbrendan * Update level 2 cache device stats. 1602fa94a07fSbrendan */ 1603fa94a07fSbrendan 1604fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1605fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 1606fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 1607fa94a07fSbrendan 1608fa94a07fSbrendan vd = NULL; 1609fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 1610fa94a07fSbrendan if (guid == 1611fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 1612fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 1613fa94a07fSbrendan break; 1614fa94a07fSbrendan } 1615fa94a07fSbrendan } 1616fa94a07fSbrendan ASSERT(vd != NULL); 1617fa94a07fSbrendan 1618fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 1619fa94a07fSbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 1620fa94a07fSbrendan vdev_get_stats(vd, vs); 1621fa94a07fSbrendan } 1622fa94a07fSbrendan } 1623fa94a07fSbrendan 1624fa94a07fSbrendan spa_config_exit(spa, FTAG); 1625fa94a07fSbrendan } 1626fa94a07fSbrendan 1627fa9e4066Sahrens int 1628ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1629fa9e4066Sahrens { 1630fa9e4066Sahrens int error; 1631fa9e4066Sahrens spa_t *spa; 1632fa9e4066Sahrens 1633fa9e4066Sahrens *config = NULL; 1634fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 1635fa9e4066Sahrens 163699653d4eSeschrock if (spa && *config != NULL) { 1637ea8dc4b6Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 1638ea8dc4b6Seschrock spa_get_errlog_size(spa)) == 0); 1639ea8dc4b6Seschrock 164099653d4eSeschrock spa_add_spares(spa, *config); 1641fa94a07fSbrendan spa_add_l2cache(spa, *config); 164299653d4eSeschrock } 164399653d4eSeschrock 1644ea8dc4b6Seschrock /* 1645ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 1646ea8dc4b6Seschrock * and call spa_lookup() directly. 1647ea8dc4b6Seschrock */ 1648ea8dc4b6Seschrock if (altroot) { 1649ea8dc4b6Seschrock if (spa == NULL) { 1650ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1651ea8dc4b6Seschrock spa = spa_lookup(name); 1652ea8dc4b6Seschrock if (spa) 1653ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1654ea8dc4b6Seschrock else 1655ea8dc4b6Seschrock altroot[0] = '\0'; 1656ea8dc4b6Seschrock spa = NULL; 1657ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1658ea8dc4b6Seschrock } else { 1659ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1660ea8dc4b6Seschrock } 1661ea8dc4b6Seschrock } 1662ea8dc4b6Seschrock 1663fa9e4066Sahrens if (spa != NULL) 1664fa9e4066Sahrens spa_close(spa, FTAG); 1665fa9e4066Sahrens 1666fa9e4066Sahrens return (error); 1667fa9e4066Sahrens } 1668fa9e4066Sahrens 166999653d4eSeschrock /* 1670fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 1671fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 1672fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 1673fa94a07fSbrendan * specified, as long as they are well-formed. 167499653d4eSeschrock */ 167599653d4eSeschrock static int 1676fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 1677fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 1678fa94a07fSbrendan vdev_labeltype_t label) 167999653d4eSeschrock { 1680fa94a07fSbrendan nvlist_t **dev; 1681fa94a07fSbrendan uint_t i, ndev; 168299653d4eSeschrock vdev_t *vd; 168399653d4eSeschrock int error; 168499653d4eSeschrock 168599653d4eSeschrock /* 1686fa94a07fSbrendan * It's acceptable to have no devs specified. 168799653d4eSeschrock */ 1688fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 168999653d4eSeschrock return (0); 169099653d4eSeschrock 1691fa94a07fSbrendan if (ndev == 0) 169299653d4eSeschrock return (EINVAL); 169399653d4eSeschrock 169499653d4eSeschrock /* 1695fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 1696fa94a07fSbrendan * device type. 169799653d4eSeschrock */ 1698fa94a07fSbrendan if (spa_version(spa) < version) 169999653d4eSeschrock return (ENOTSUP); 170099653d4eSeschrock 170139c23413Seschrock /* 1702fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 170339c23413Seschrock * checking. 170439c23413Seschrock */ 1705fa94a07fSbrendan sav->sav_pending = dev; 1706fa94a07fSbrendan sav->sav_npending = ndev; 170739c23413Seschrock 1708fa94a07fSbrendan for (i = 0; i < ndev; i++) { 1709fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 171099653d4eSeschrock mode)) != 0) 171139c23413Seschrock goto out; 171299653d4eSeschrock 171399653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 171499653d4eSeschrock vdev_free(vd); 171539c23413Seschrock error = EINVAL; 171639c23413Seschrock goto out; 171799653d4eSeschrock } 171899653d4eSeschrock 1719fa94a07fSbrendan /* 1720fa94a07fSbrendan * The L2ARC currently only supports disk devices. 1721fa94a07fSbrendan */ 1722fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 1723fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 1724fa94a07fSbrendan error = ENOTBLK; 1725fa94a07fSbrendan goto out; 1726fa94a07fSbrendan } 1727fa94a07fSbrendan 172899653d4eSeschrock vd->vdev_top = vd; 172999653d4eSeschrock 173039c23413Seschrock if ((error = vdev_open(vd)) == 0 && 1731fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 1732fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 173339c23413Seschrock vd->vdev_guid) == 0); 173439c23413Seschrock } 173599653d4eSeschrock 173699653d4eSeschrock vdev_free(vd); 173739c23413Seschrock 1738fa94a07fSbrendan if (error && 1739fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 174039c23413Seschrock goto out; 174139c23413Seschrock else 174239c23413Seschrock error = 0; 174399653d4eSeschrock } 174499653d4eSeschrock 174539c23413Seschrock out: 1746fa94a07fSbrendan sav->sav_pending = NULL; 1747fa94a07fSbrendan sav->sav_npending = 0; 174839c23413Seschrock return (error); 174999653d4eSeschrock } 175099653d4eSeschrock 1751fa94a07fSbrendan static int 1752fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 1753fa94a07fSbrendan { 1754fa94a07fSbrendan int error; 1755fa94a07fSbrendan 1756fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 1757fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 1758fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 1759fa94a07fSbrendan return (error); 1760fa94a07fSbrendan } 1761fa94a07fSbrendan 1762fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 1763fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 1764fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 1765fa94a07fSbrendan } 1766fa94a07fSbrendan 1767fa94a07fSbrendan static void 1768fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 1769fa94a07fSbrendan const char *config) 1770fa94a07fSbrendan { 1771fa94a07fSbrendan int i; 1772fa94a07fSbrendan 1773fa94a07fSbrendan if (sav->sav_config != NULL) { 1774fa94a07fSbrendan nvlist_t **olddevs; 1775fa94a07fSbrendan uint_t oldndevs; 1776fa94a07fSbrendan nvlist_t **newdevs; 1777fa94a07fSbrendan 1778fa94a07fSbrendan /* 1779fa94a07fSbrendan * Generate new dev list by concatentating with the 1780fa94a07fSbrendan * current dev list. 1781fa94a07fSbrendan */ 1782fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 1783fa94a07fSbrendan &olddevs, &oldndevs) == 0); 1784fa94a07fSbrendan 1785fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 1786fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 1787fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 1788fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 1789fa94a07fSbrendan KM_SLEEP) == 0); 1790fa94a07fSbrendan for (i = 0; i < ndevs; i++) 1791fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 1792fa94a07fSbrendan KM_SLEEP) == 0); 1793fa94a07fSbrendan 1794fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 1795fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1796fa94a07fSbrendan 1797fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1798fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 1799fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 1800fa94a07fSbrendan nvlist_free(newdevs[i]); 1801fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 1802fa94a07fSbrendan } else { 1803fa94a07fSbrendan /* 1804fa94a07fSbrendan * Generate a new dev list. 1805fa94a07fSbrendan */ 1806fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 1807fa94a07fSbrendan KM_SLEEP) == 0); 1808fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 1809fa94a07fSbrendan devs, ndevs) == 0); 1810fa94a07fSbrendan } 1811fa94a07fSbrendan } 1812fa94a07fSbrendan 1813fa94a07fSbrendan /* 1814fa94a07fSbrendan * Stop and drop level 2 ARC devices 1815fa94a07fSbrendan */ 1816fa94a07fSbrendan void 1817fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 1818fa94a07fSbrendan { 1819fa94a07fSbrendan vdev_t *vd; 1820fa94a07fSbrendan int i; 1821fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1822fa94a07fSbrendan 1823fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 1824fa94a07fSbrendan uint64_t pool; 1825fa94a07fSbrendan 1826fa94a07fSbrendan vd = sav->sav_vdevs[i]; 1827fa94a07fSbrendan ASSERT(vd != NULL); 1828fa94a07fSbrendan 1829fa94a07fSbrendan if (spa_mode & FWRITE && 1830fa94a07fSbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL) { 1831fa94a07fSbrendan l2arc_remove_vdev(vd); 1832fa94a07fSbrendan } 1833fa94a07fSbrendan if (vd->vdev_isl2cache) 1834fa94a07fSbrendan spa_l2cache_remove(vd); 1835fa94a07fSbrendan vdev_clear_stats(vd); 1836fa94a07fSbrendan (void) vdev_close(vd); 1837fa94a07fSbrendan } 1838fa94a07fSbrendan } 1839fa94a07fSbrendan 1840fa9e4066Sahrens /* 1841fa9e4066Sahrens * Pool Creation 1842fa9e4066Sahrens */ 1843fa9e4066Sahrens int 1844990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 1845228975ccSek const char *history_str) 1846fa9e4066Sahrens { 1847fa9e4066Sahrens spa_t *spa; 1848990b4856Slling char *altroot = NULL; 18490373e76bSbonwick vdev_t *rvd; 1850fa9e4066Sahrens dsl_pool_t *dp; 1851fa9e4066Sahrens dmu_tx_t *tx; 185299653d4eSeschrock int c, error = 0; 1853fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 1854fa94a07fSbrendan nvlist_t **spares, **l2cache; 1855fa94a07fSbrendan uint_t nspares, nl2cache; 1856990b4856Slling uint64_t version; 1857fa9e4066Sahrens 1858fa9e4066Sahrens /* 1859fa9e4066Sahrens * If this pool already exists, return failure. 1860fa9e4066Sahrens */ 1861fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1862fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 1863fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1864fa9e4066Sahrens return (EEXIST); 1865fa9e4066Sahrens } 1866fa9e4066Sahrens 1867fa9e4066Sahrens /* 1868fa9e4066Sahrens * Allocate a new spa_t structure. 1869fa9e4066Sahrens */ 1870990b4856Slling (void) nvlist_lookup_string(props, 1871990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18720373e76bSbonwick spa = spa_add(pool, altroot); 1873fa9e4066Sahrens spa_activate(spa); 1874fa9e4066Sahrens 1875fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 1876990b4856Slling 1877990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 1878990b4856Slling spa_unload(spa); 1879990b4856Slling spa_deactivate(spa); 1880990b4856Slling spa_remove(spa); 1881990b4856Slling return (error); 1882990b4856Slling } 1883990b4856Slling 1884990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 1885990b4856Slling &version) != 0) 1886990b4856Slling version = SPA_VERSION; 1887990b4856Slling ASSERT(version <= SPA_VERSION); 1888990b4856Slling spa->spa_uberblock.ub_version = version; 1889fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1890fa9e4066Sahrens 18910373e76bSbonwick /* 18920373e76bSbonwick * Create the root vdev. 18930373e76bSbonwick */ 18940373e76bSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 18950373e76bSbonwick 189699653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 18970373e76bSbonwick 189899653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 189999653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 19000373e76bSbonwick 1901b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 19020373e76bSbonwick error = EINVAL; 190399653d4eSeschrock 190499653d4eSeschrock if (error == 0 && 190599653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 1906fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 190799653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 190899653d4eSeschrock for (c = 0; c < rvd->vdev_children; c++) 190999653d4eSeschrock vdev_init(rvd->vdev_child[c], txg); 191099653d4eSeschrock vdev_config_dirty(rvd); 19110373e76bSbonwick } 19120373e76bSbonwick 19130373e76bSbonwick spa_config_exit(spa, FTAG); 1914fa9e4066Sahrens 191599653d4eSeschrock if (error != 0) { 1916fa9e4066Sahrens spa_unload(spa); 1917fa9e4066Sahrens spa_deactivate(spa); 1918fa9e4066Sahrens spa_remove(spa); 1919fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1920fa9e4066Sahrens return (error); 1921fa9e4066Sahrens } 1922fa9e4066Sahrens 192399653d4eSeschrock /* 192499653d4eSeschrock * Get the list of spares, if specified. 192599653d4eSeschrock */ 192699653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 192799653d4eSeschrock &spares, &nspares) == 0) { 1928fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 192999653d4eSeschrock KM_SLEEP) == 0); 1930fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 193199653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 193299653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 193399653d4eSeschrock spa_load_spares(spa); 193499653d4eSeschrock spa_config_exit(spa, FTAG); 1935fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 1936fa94a07fSbrendan } 1937fa94a07fSbrendan 1938fa94a07fSbrendan /* 1939fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 1940fa94a07fSbrendan */ 1941fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1942fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1943fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 1944fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 1945fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 1946fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 1947fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 1948fa94a07fSbrendan spa_load_l2cache(spa); 1949fa94a07fSbrendan spa_config_exit(spa, FTAG); 1950fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 195199653d4eSeschrock } 195299653d4eSeschrock 1953fa9e4066Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1954fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1955fa9e4066Sahrens 1956fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 1957fa9e4066Sahrens 1958fa9e4066Sahrens /* 1959fa9e4066Sahrens * Create the pool config object. 1960fa9e4066Sahrens */ 1961fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1962fa9e4066Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1963fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1964fa9e4066Sahrens 1965ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1966fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1967ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 1968ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 1969ea8dc4b6Seschrock } 1970fa9e4066Sahrens 1971990b4856Slling /* Newly created pools with the right version are always deflated. */ 1972990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 1973990b4856Slling spa->spa_deflate = TRUE; 1974990b4856Slling if (zap_add(spa->spa_meta_objset, 1975990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 1976990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 1977990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 1978990b4856Slling } 197999653d4eSeschrock } 198099653d4eSeschrock 1981fa9e4066Sahrens /* 1982fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 1983fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 1984fa9e4066Sahrens * keeps changing. 1985fa9e4066Sahrens */ 1986fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1987fa9e4066Sahrens 1 << 14, tx); 1988fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1989fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 1990fa9e4066Sahrens 1991ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1992fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1993ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 1994ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 1995ea8dc4b6Seschrock } 1996fa9e4066Sahrens 199706eeb2adSek /* 199806eeb2adSek * Create the pool's history object. 199906eeb2adSek */ 2000990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 2001990b4856Slling spa_history_create_obj(spa, tx); 2002990b4856Slling 2003990b4856Slling /* 2004990b4856Slling * Set pool properties. 2005990b4856Slling */ 2006990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 2007990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20080a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 2009990b4856Slling if (props) 2010990b4856Slling spa_sync_props(spa, props, CRED(), tx); 201106eeb2adSek 2012fa9e4066Sahrens dmu_tx_commit(tx); 2013fa9e4066Sahrens 2014fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2015fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2016fa9e4066Sahrens 2017fa9e4066Sahrens /* 2018fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 2019fa9e4066Sahrens * bean counters are appropriately updated. 2020fa9e4066Sahrens */ 2021fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2022fa9e4066Sahrens 2023fa9e4066Sahrens spa_config_sync(); 2024fa9e4066Sahrens 2025990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 2026228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 2027228975ccSek 2028fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2029fa9e4066Sahrens 2030fa9e4066Sahrens return (0); 2031fa9e4066Sahrens } 2032fa9e4066Sahrens 2033fa9e4066Sahrens /* 2034fa9e4066Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2035fa9e4066Sahrens * then call spa_load() to do the dirty work. 2036fa9e4066Sahrens */ 2037*e7cbe64fSgw static int 2038*e7cbe64fSgw spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 2039*e7cbe64fSgw boolean_t isroot) 2040fa9e4066Sahrens { 2041fa9e4066Sahrens spa_t *spa; 2042990b4856Slling char *altroot = NULL; 2043fa9e4066Sahrens int error; 204499653d4eSeschrock nvlist_t *nvroot; 2045fa94a07fSbrendan nvlist_t **spares, **l2cache; 2046fa94a07fSbrendan uint_t nspares, nl2cache; 2047*e7cbe64fSgw int mosconfig = isroot? B_FALSE : B_TRUE; 2048fa9e4066Sahrens 2049fa9e4066Sahrens /* 2050fa9e4066Sahrens * If a pool with this name exists, return failure. 2051fa9e4066Sahrens */ 2052fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2053fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 2054fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2055fa9e4066Sahrens return (EEXIST); 2056fa9e4066Sahrens } 2057fa9e4066Sahrens 2058fa9e4066Sahrens /* 20590373e76bSbonwick * Create and initialize the spa structure. 2060fa9e4066Sahrens */ 2061990b4856Slling (void) nvlist_lookup_string(props, 2062990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20630373e76bSbonwick spa = spa_add(pool, altroot); 2064fa9e4066Sahrens spa_activate(spa); 2065fa9e4066Sahrens 20665dabedeeSbonwick /* 20670373e76bSbonwick * Pass off the heavy lifting to spa_load(). 2068ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 2069ecc2d604Sbonwick * is actually the one to trust when doing an import. 20705dabedeeSbonwick */ 2071*e7cbe64fSgw error = spa_load(spa, config, SPA_LOAD_IMPORT, mosconfig); 2072fa9e4066Sahrens 207399653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 207499653d4eSeschrock /* 207599653d4eSeschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 207699653d4eSeschrock * and conflicts with spa_has_spare(). 207799653d4eSeschrock */ 2078*e7cbe64fSgw if (!isroot && spa->spa_spares.sav_config) { 2079fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 2080fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 208199653d4eSeschrock spa_load_spares(spa); 208299653d4eSeschrock } 2083*e7cbe64fSgw if (!isroot && spa->spa_l2cache.sav_config) { 2084fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 2085fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 2086fa94a07fSbrendan spa_load_l2cache(spa); 2087fa94a07fSbrendan } 208899653d4eSeschrock 208999653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 209099653d4eSeschrock &nvroot) == 0); 2091fa94a07fSbrendan if (error == 0) 2092fa94a07fSbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 2093fa94a07fSbrendan if (error == 0) 2094fa94a07fSbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 2095fa94a07fSbrendan VDEV_ALLOC_L2CACHE); 209699653d4eSeschrock spa_config_exit(spa, FTAG); 209799653d4eSeschrock 2098990b4856Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 2099fa9e4066Sahrens spa_unload(spa); 2100fa9e4066Sahrens spa_deactivate(spa); 2101fa9e4066Sahrens spa_remove(spa); 2102fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2103fa9e4066Sahrens return (error); 2104fa9e4066Sahrens } 2105fa9e4066Sahrens 210699653d4eSeschrock /* 2107fa94a07fSbrendan * Override any spares and level 2 cache devices as specified by 2108fa94a07fSbrendan * the user, as these may have correct device names/devids, etc. 210999653d4eSeschrock */ 211099653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 211199653d4eSeschrock &spares, &nspares) == 0) { 2112fa94a07fSbrendan if (spa->spa_spares.sav_config) 2113fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 211499653d4eSeschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 211599653d4eSeschrock else 2116fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 211799653d4eSeschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 2118fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 211999653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 212099653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 212199653d4eSeschrock spa_load_spares(spa); 212299653d4eSeschrock spa_config_exit(spa, FTAG); 2123fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2124fa94a07fSbrendan } 2125fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2126fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 2127fa94a07fSbrendan if (spa->spa_l2cache.sav_config) 2128fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 2129fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 2130fa94a07fSbrendan else 2131fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2132fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 2133fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2134fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2135fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 2136fa94a07fSbrendan spa_load_l2cache(spa); 2137fa94a07fSbrendan spa_config_exit(spa, FTAG); 2138fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 213999653d4eSeschrock } 214099653d4eSeschrock 21410373e76bSbonwick /* 21420373e76bSbonwick * Update the config cache to include the newly-imported pool. 21430373e76bSbonwick */ 2144de6628f0Sck if (spa_mode & FWRITE) 2145*e7cbe64fSgw spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21460373e76bSbonwick 2147fa9e4066Sahrens /* 2148fa9e4066Sahrens * Resilver anything that's out of date. 2149fa9e4066Sahrens */ 2150*e7cbe64fSgw if (!isroot && (spa_mode & FWRITE)) 2151fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 2152fa9e4066Sahrens 21533d7072f8Seschrock mutex_exit(&spa_namespace_lock); 21543d7072f8Seschrock 2155fa9e4066Sahrens return (0); 2156fa9e4066Sahrens } 2157fa9e4066Sahrens 2158*e7cbe64fSgw #ifdef _KERNEL 2159*e7cbe64fSgw /* 2160*e7cbe64fSgw * Build a "root" vdev for a top level vdev read in from a rootpool 2161*e7cbe64fSgw * device label. 2162*e7cbe64fSgw */ 2163*e7cbe64fSgw static void 2164*e7cbe64fSgw spa_build_rootpool_config(nvlist_t *config) 2165*e7cbe64fSgw { 2166*e7cbe64fSgw nvlist_t *nvtop, *nvroot; 2167*e7cbe64fSgw uint64_t pgid; 2168*e7cbe64fSgw 2169*e7cbe64fSgw /* 2170*e7cbe64fSgw * Add this top-level vdev to the child array. 2171*e7cbe64fSgw */ 2172*e7cbe64fSgw VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 2173*e7cbe64fSgw == 0); 2174*e7cbe64fSgw VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 2175*e7cbe64fSgw == 0); 2176*e7cbe64fSgw 2177*e7cbe64fSgw /* 2178*e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 2179*e7cbe64fSgw */ 2180*e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2181*e7cbe64fSgw VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 2182*e7cbe64fSgw == 0); 2183*e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 2184*e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 2185*e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2186*e7cbe64fSgw &nvtop, 1) == 0); 2187*e7cbe64fSgw 2188*e7cbe64fSgw /* 2189*e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 2190*e7cbe64fSgw * this pool's configuration (remove the old, add the new). 2191*e7cbe64fSgw */ 2192*e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 2193*e7cbe64fSgw nvlist_free(nvroot); 2194*e7cbe64fSgw } 2195*e7cbe64fSgw 2196*e7cbe64fSgw /* 2197*e7cbe64fSgw * Get the root pool information from the root disk, then import the root pool 2198*e7cbe64fSgw * during the system boot up time. 2199*e7cbe64fSgw */ 2200*e7cbe64fSgw extern nvlist_t *vdev_disk_read_rootlabel(char *); 2201*e7cbe64fSgw 2202*e7cbe64fSgw void 2203*e7cbe64fSgw spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf, 2204*e7cbe64fSgw uint64_t *besttxg) 2205*e7cbe64fSgw { 2206*e7cbe64fSgw nvlist_t *config; 2207*e7cbe64fSgw uint64_t txg; 2208*e7cbe64fSgw 2209*e7cbe64fSgw if ((config = vdev_disk_read_rootlabel(devpath)) == NULL) 2210*e7cbe64fSgw return; 2211*e7cbe64fSgw 2212*e7cbe64fSgw VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 2213*e7cbe64fSgw 2214*e7cbe64fSgw if (txg > *besttxg) { 2215*e7cbe64fSgw *besttxg = txg; 2216*e7cbe64fSgw if (*bestconf != NULL) 2217*e7cbe64fSgw nvlist_free(*bestconf); 2218*e7cbe64fSgw *bestconf = config; 2219*e7cbe64fSgw *bestdev = devpath; 2220*e7cbe64fSgw } 2221*e7cbe64fSgw } 2222*e7cbe64fSgw 2223*e7cbe64fSgw boolean_t 2224*e7cbe64fSgw spa_rootdev_validate(nvlist_t *nv) 2225*e7cbe64fSgw { 2226*e7cbe64fSgw uint64_t ival; 2227*e7cbe64fSgw 2228*e7cbe64fSgw if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 2229*e7cbe64fSgw nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 2230*e7cbe64fSgw nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 || 2231*e7cbe64fSgw nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 2232*e7cbe64fSgw return (B_FALSE); 2233*e7cbe64fSgw 2234*e7cbe64fSgw return (B_TRUE); 2235*e7cbe64fSgw } 2236*e7cbe64fSgw 2237*e7cbe64fSgw /* 2238*e7cbe64fSgw * Import a root pool. 2239*e7cbe64fSgw * 2240*e7cbe64fSgw * For x86. devpath_list will consist the physpath name of the vdev in a single 2241*e7cbe64fSgw * disk root pool or a list of physnames for the vdevs in a mirrored rootpool. 2242*e7cbe64fSgw * e.g. 2243*e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a" 2244*e7cbe64fSgw * 2245*e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 2246*e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 2247*e7cbe64fSgw * e.g. 2248*e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 2249*e7cbe64fSgw */ 2250*e7cbe64fSgw int 2251*e7cbe64fSgw spa_import_rootpool(char *devpath_list) 2252*e7cbe64fSgw { 2253*e7cbe64fSgw nvlist_t *conf = NULL; 2254*e7cbe64fSgw char *dev = NULL; 2255*e7cbe64fSgw char *pname; 2256*e7cbe64fSgw int error; 2257*e7cbe64fSgw 2258*e7cbe64fSgw /* 2259*e7cbe64fSgw * Get the vdev pathname and configuation from the most 2260*e7cbe64fSgw * recently updated vdev (highest txg). 2261*e7cbe64fSgw */ 2262*e7cbe64fSgw if (error = spa_get_rootconf(devpath_list, &dev, &conf)) 2263*e7cbe64fSgw goto msg_out; 2264*e7cbe64fSgw 2265*e7cbe64fSgw /* 2266*e7cbe64fSgw * Add type "root" vdev to the config. 2267*e7cbe64fSgw */ 2268*e7cbe64fSgw spa_build_rootpool_config(conf); 2269*e7cbe64fSgw 2270*e7cbe64fSgw VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 2271*e7cbe64fSgw 2272*e7cbe64fSgw error = spa_import_common(pname, conf, NULL, TRUE); 2273*e7cbe64fSgw if (error == EEXIST) 2274*e7cbe64fSgw error = 0; 2275*e7cbe64fSgw 2276*e7cbe64fSgw nvlist_free(conf); 2277*e7cbe64fSgw return (error); 2278*e7cbe64fSgw 2279*e7cbe64fSgw msg_out: 2280*e7cbe64fSgw cmn_err(CE_NOTE, "\n\n" 2281*e7cbe64fSgw " *************************************************** \n" 2282*e7cbe64fSgw " * This device is not bootable! * \n" 2283*e7cbe64fSgw " * It is either offlined or detached or faulted. * \n" 2284*e7cbe64fSgw " * Please try to boot from a different device. * \n" 2285*e7cbe64fSgw " *************************************************** \n\n"); 2286*e7cbe64fSgw 2287*e7cbe64fSgw return (error); 2288*e7cbe64fSgw } 2289*e7cbe64fSgw #endif 2290*e7cbe64fSgw 2291*e7cbe64fSgw /* 2292*e7cbe64fSgw * Import a non-root pool into the system. 2293*e7cbe64fSgw */ 2294*e7cbe64fSgw int 2295*e7cbe64fSgw spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 2296*e7cbe64fSgw { 2297*e7cbe64fSgw return (spa_import_common(pool, config, props, FALSE)); 2298*e7cbe64fSgw } 2299*e7cbe64fSgw 2300fa9e4066Sahrens /* 2301fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2302fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 2303fa9e4066Sahrens */ 2304fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 2305fa9e4066Sahrens 2306fa9e4066Sahrens nvlist_t * 2307fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 2308fa9e4066Sahrens { 2309fa9e4066Sahrens nvlist_t *config = NULL; 2310fa9e4066Sahrens char *poolname; 2311fa9e4066Sahrens spa_t *spa; 2312fa9e4066Sahrens uint64_t state; 2313fa9e4066Sahrens 2314fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2315fa9e4066Sahrens return (NULL); 2316fa9e4066Sahrens 2317fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2318fa9e4066Sahrens return (NULL); 2319fa9e4066Sahrens 2320fa9e4066Sahrens /* 23210373e76bSbonwick * Create and initialize the spa structure. 2322fa9e4066Sahrens */ 23230373e76bSbonwick mutex_enter(&spa_namespace_lock); 23240373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2325fa9e4066Sahrens spa_activate(spa); 2326fa9e4066Sahrens 2327fa9e4066Sahrens /* 23280373e76bSbonwick * Pass off the heavy lifting to spa_load(). 2329ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 2330ecc2d604Sbonwick * is actually the one to trust when doing an import. 2331fa9e4066Sahrens */ 2332ecc2d604Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2333fa9e4066Sahrens 2334fa9e4066Sahrens /* 2335fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 2336fa9e4066Sahrens */ 2337fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 23380373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 2339fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 23400373e76bSbonwick spa_config_exit(spa, FTAG); 2341fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2342fa9e4066Sahrens poolname) == 0); 2343fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2344fa9e4066Sahrens state) == 0); 234595173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 234695173954Sek spa->spa_uberblock.ub_timestamp) == 0); 234799653d4eSeschrock 2348*e7cbe64fSgw /* 2349*e7cbe64fSgw * If the bootfs property exists on this pool then we 2350*e7cbe64fSgw * copy it out so that external consumers can tell which 2351*e7cbe64fSgw * pools are bootable. 2352*e7cbe64fSgw */ 2353*e7cbe64fSgw if (spa->spa_bootfs) { 2354*e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2355*e7cbe64fSgw 2356*e7cbe64fSgw /* 2357*e7cbe64fSgw * We have to play games with the name since the 2358*e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 2359*e7cbe64fSgw */ 2360*e7cbe64fSgw if (dsl_dsobj_to_dsname(spa->spa_name, 2361*e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 2362*e7cbe64fSgw char *cp; 2363*e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2364*e7cbe64fSgw 2365*e7cbe64fSgw cp = strchr(tmpname, '/'); 2366*e7cbe64fSgw if (cp == NULL) { 2367*e7cbe64fSgw (void) strlcpy(dsname, tmpname, 2368*e7cbe64fSgw MAXPATHLEN); 2369*e7cbe64fSgw } else { 2370*e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 2371*e7cbe64fSgw "%s/%s", poolname, ++cp); 2372*e7cbe64fSgw } 2373*e7cbe64fSgw VERIFY(nvlist_add_string(config, 2374*e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 2375*e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 2376*e7cbe64fSgw } 2377*e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 2378*e7cbe64fSgw } 2379*e7cbe64fSgw 238099653d4eSeschrock /* 2381fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 238299653d4eSeschrock */ 238399653d4eSeschrock spa_add_spares(spa, config); 2384fa94a07fSbrendan spa_add_l2cache(spa, config); 2385fa9e4066Sahrens } 2386fa9e4066Sahrens 2387fa9e4066Sahrens spa_unload(spa); 2388fa9e4066Sahrens spa_deactivate(spa); 2389fa9e4066Sahrens spa_remove(spa); 2390fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2391fa9e4066Sahrens 2392fa9e4066Sahrens return (config); 2393fa9e4066Sahrens } 2394fa9e4066Sahrens 2395fa9e4066Sahrens /* 2396fa9e4066Sahrens * Pool export/destroy 2397fa9e4066Sahrens * 2398fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2399fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2400fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 2401fa9e4066Sahrens * configuration from the cache afterwards. 2402fa9e4066Sahrens */ 2403fa9e4066Sahrens static int 240444cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 2405fa9e4066Sahrens { 2406fa9e4066Sahrens spa_t *spa; 2407fa9e4066Sahrens 240844cd46caSbillm if (oldconfig) 240944cd46caSbillm *oldconfig = NULL; 241044cd46caSbillm 2411fa9e4066Sahrens if (!(spa_mode & FWRITE)) 2412fa9e4066Sahrens return (EROFS); 2413fa9e4066Sahrens 2414fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2415fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2416fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2417fa9e4066Sahrens return (ENOENT); 2418fa9e4066Sahrens } 2419fa9e4066Sahrens 2420ea8dc4b6Seschrock /* 2421ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 2422ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 2423ea8dc4b6Seschrock */ 2424ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 2425ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2426ea8dc4b6Seschrock spa_async_suspend(spa); 2427ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2428ea8dc4b6Seschrock spa_close(spa, FTAG); 2429ea8dc4b6Seschrock 2430fa9e4066Sahrens /* 2431fa9e4066Sahrens * The pool will be in core if it's openable, 2432fa9e4066Sahrens * in which case we can modify its state. 2433fa9e4066Sahrens */ 2434fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2435fa9e4066Sahrens /* 2436fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 2437fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 2438fa9e4066Sahrens */ 2439fa9e4066Sahrens spa_scrub_suspend(spa); 2440fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2441fa9e4066Sahrens 2442ea8dc4b6Seschrock /* 2443ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 2444ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 2445ea8dc4b6Seschrock * fault injection handlers. 2446ea8dc4b6Seschrock */ 2447ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 2448ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 2449ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 2450fa9e4066Sahrens spa_scrub_resume(spa); 2451ea8dc4b6Seschrock spa_async_resume(spa); 2452fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2453fa9e4066Sahrens return (EBUSY); 2454fa9e4066Sahrens } 2455fa9e4066Sahrens 2456fa9e4066Sahrens spa_scrub_resume(spa); 2457fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2458fa9e4066Sahrens 2459fa9e4066Sahrens /* 2460fa9e4066Sahrens * We want this to be reflected on every label, 2461fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 2462fa9e4066Sahrens * final sync that pushes these changes out. 2463fa9e4066Sahrens */ 2464ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 24655dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 2466ea8dc4b6Seschrock spa->spa_state = new_state; 24670373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 2468ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 24695dabedeeSbonwick spa_config_exit(spa, FTAG); 2470ea8dc4b6Seschrock } 2471fa9e4066Sahrens } 2472fa9e4066Sahrens 24733d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 24743d7072f8Seschrock 2475fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2476fa9e4066Sahrens spa_unload(spa); 2477fa9e4066Sahrens spa_deactivate(spa); 2478fa9e4066Sahrens } 2479fa9e4066Sahrens 248044cd46caSbillm if (oldconfig && spa->spa_config) 248144cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 248244cd46caSbillm 2483ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 24842f8aaab3Seschrock spa_config_check(spa->spa_config_dir, 24852f8aaab3Seschrock spa->spa_config_file); 2486ea8dc4b6Seschrock spa_remove(spa); 2487ea8dc4b6Seschrock spa_config_sync(); 2488ea8dc4b6Seschrock } 2489fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2490fa9e4066Sahrens 2491fa9e4066Sahrens return (0); 2492fa9e4066Sahrens } 2493fa9e4066Sahrens 2494fa9e4066Sahrens /* 2495fa9e4066Sahrens * Destroy a storage pool. 2496fa9e4066Sahrens */ 2497fa9e4066Sahrens int 2498fa9e4066Sahrens spa_destroy(char *pool) 2499fa9e4066Sahrens { 250044cd46caSbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 2501fa9e4066Sahrens } 2502fa9e4066Sahrens 2503fa9e4066Sahrens /* 2504fa9e4066Sahrens * Export a storage pool. 2505fa9e4066Sahrens */ 2506fa9e4066Sahrens int 250744cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig) 2508fa9e4066Sahrens { 250944cd46caSbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 2510fa9e4066Sahrens } 2511fa9e4066Sahrens 2512ea8dc4b6Seschrock /* 2513ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 2514ea8dc4b6Seschrock * from the namespace in any way. 2515ea8dc4b6Seschrock */ 2516ea8dc4b6Seschrock int 2517ea8dc4b6Seschrock spa_reset(char *pool) 2518ea8dc4b6Seschrock { 251944cd46caSbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 2520ea8dc4b6Seschrock } 2521ea8dc4b6Seschrock 2522ea8dc4b6Seschrock 2523fa9e4066Sahrens /* 2524fa9e4066Sahrens * ========================================================================== 2525fa9e4066Sahrens * Device manipulation 2526fa9e4066Sahrens * ========================================================================== 2527fa9e4066Sahrens */ 2528fa9e4066Sahrens 2529fa9e4066Sahrens /* 25308654d025Sperrin * Add a device to a storage pool. 2531fa9e4066Sahrens */ 2532fa9e4066Sahrens int 2533fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2534fa9e4066Sahrens { 2535fa9e4066Sahrens uint64_t txg; 25360373e76bSbonwick int c, error; 2537fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 25380e34b6a7Sbonwick vdev_t *vd, *tvd; 2539fa94a07fSbrendan nvlist_t **spares, **l2cache; 2540fa94a07fSbrendan uint_t nspares, nl2cache; 2541fa9e4066Sahrens 2542fa9e4066Sahrens txg = spa_vdev_enter(spa); 2543fa9e4066Sahrens 254499653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 254599653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 254699653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 2547fa9e4066Sahrens 254839c23413Seschrock spa->spa_pending_vdev = vd; 254999653d4eSeschrock 2550fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 2551fa94a07fSbrendan &nspares) != 0) 255299653d4eSeschrock nspares = 0; 255399653d4eSeschrock 2554fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 2555fa94a07fSbrendan &nl2cache) != 0) 2556fa94a07fSbrendan nl2cache = 0; 2557fa94a07fSbrendan 2558fa94a07fSbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 255939c23413Seschrock spa->spa_pending_vdev = NULL; 2560fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 256139c23413Seschrock } 2562fa9e4066Sahrens 256399653d4eSeschrock if (vd->vdev_children != 0) { 256439c23413Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 256539c23413Seschrock spa->spa_pending_vdev = NULL; 256699653d4eSeschrock return (spa_vdev_exit(spa, vd, txg, error)); 256799653d4eSeschrock } 256899653d4eSeschrock } 256999653d4eSeschrock 257039c23413Seschrock /* 2571fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 2572fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 257339c23413Seschrock */ 2574fa94a07fSbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 257539c23413Seschrock spa->spa_pending_vdev = NULL; 257639c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 257739c23413Seschrock } 257839c23413Seschrock 257939c23413Seschrock spa->spa_pending_vdev = NULL; 258039c23413Seschrock 258139c23413Seschrock /* 258239c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 258339c23413Seschrock */ 258439c23413Seschrock for (c = 0; c < vd->vdev_children; c++) { 258539c23413Seschrock tvd = vd->vdev_child[c]; 258639c23413Seschrock vdev_remove_child(vd, tvd); 258739c23413Seschrock tvd->vdev_id = rvd->vdev_children; 258839c23413Seschrock vdev_add_child(rvd, tvd); 258939c23413Seschrock vdev_config_dirty(tvd); 259039c23413Seschrock } 259139c23413Seschrock 259299653d4eSeschrock if (nspares != 0) { 2593fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 2594fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 259599653d4eSeschrock spa_load_spares(spa); 2596fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2597fa94a07fSbrendan } 2598fa94a07fSbrendan 2599fa94a07fSbrendan if (nl2cache != 0) { 2600fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 2601fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 2602fa94a07fSbrendan spa_load_l2cache(spa); 2603fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2604fa9e4066Sahrens } 2605fa9e4066Sahrens 2606fa9e4066Sahrens /* 26070e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 26080e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 26090e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 26100e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 26110e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 26120e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 26130373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 26140e34b6a7Sbonwick * 26150e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 26160e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 26170e34b6a7Sbonwick * steps will be completed the next time we load the pool. 26180e34b6a7Sbonwick */ 26190373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 26200e34b6a7Sbonwick 26210373e76bSbonwick mutex_enter(&spa_namespace_lock); 26220373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26230373e76bSbonwick mutex_exit(&spa_namespace_lock); 2624fa9e4066Sahrens 26250373e76bSbonwick return (0); 2626fa9e4066Sahrens } 2627fa9e4066Sahrens 2628fa9e4066Sahrens /* 2629fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 2630fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2631fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2632fa9e4066Sahrens * 2633fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 2634fa9e4066Sahrens * existing device; in this case the two devices are made into their own 26353d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2636fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2637fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 2638fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 2639fa9e4066Sahrens * is automatically detached. 2640fa9e4066Sahrens */ 2641fa9e4066Sahrens int 2642ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2643fa9e4066Sahrens { 2644fa9e4066Sahrens uint64_t txg, open_txg; 2645fa9e4066Sahrens int error; 2646fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2647fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 264899653d4eSeschrock vdev_ops_t *pvops; 26498654d025Sperrin int is_log; 2650fa9e4066Sahrens 2651fa9e4066Sahrens txg = spa_vdev_enter(spa); 2652fa9e4066Sahrens 2653ea8dc4b6Seschrock oldvd = vdev_lookup_by_guid(rvd, guid); 2654fa9e4066Sahrens 2655fa9e4066Sahrens if (oldvd == NULL) 2656fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2657fa9e4066Sahrens 26580e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 26590e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 26600e34b6a7Sbonwick 2661fa9e4066Sahrens pvd = oldvd->vdev_parent; 2662fa9e4066Sahrens 266399653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 26643d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 26653d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 26663d7072f8Seschrock 26673d7072f8Seschrock if (newrootvd->vdev_children != 1) 2668fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2669fa9e4066Sahrens 2670fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 2671fa9e4066Sahrens 2672fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2673fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2674fa9e4066Sahrens 267599653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2676fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2677fa9e4066Sahrens 26788654d025Sperrin /* 26798654d025Sperrin * Spares can't replace logs 26808654d025Sperrin */ 26818654d025Sperrin is_log = oldvd->vdev_islog; 26828654d025Sperrin if (is_log && newvd->vdev_isspare) 26838654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 26848654d025Sperrin 268599653d4eSeschrock if (!replacing) { 268699653d4eSeschrock /* 268799653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 268899653d4eSeschrock * vdev. 268999653d4eSeschrock */ 269099653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 269199653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 269299653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 269399653d4eSeschrock 269499653d4eSeschrock pvops = &vdev_mirror_ops; 269599653d4eSeschrock } else { 269699653d4eSeschrock /* 269799653d4eSeschrock * Active hot spares can only be replaced by inactive hot 269899653d4eSeschrock * spares. 269999653d4eSeschrock */ 270099653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 270199653d4eSeschrock pvd->vdev_child[1] == oldvd && 270299653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 270399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 270499653d4eSeschrock 270599653d4eSeschrock /* 270699653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 270799653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 270839c23413Seschrock * want to create a replacing vdev. The user is not allowed to 270939c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 271039c23413Seschrock * the same (spare replaces spare, non-spare replaces 271139c23413Seschrock * non-spare). 271299653d4eSeschrock */ 271399653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 271499653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 271539c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 271639c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 271739c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 271899653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 271999653d4eSeschrock newvd->vdev_isspare) 272099653d4eSeschrock pvops = &vdev_spare_ops; 272199653d4eSeschrock else 272299653d4eSeschrock pvops = &vdev_replacing_ops; 272399653d4eSeschrock } 272499653d4eSeschrock 27252a79c5feSlling /* 27262a79c5feSlling * Compare the new device size with the replaceable/attachable 27272a79c5feSlling * device size. 27282a79c5feSlling */ 27292a79c5feSlling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2730fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2731fa9e4066Sahrens 2732ecc2d604Sbonwick /* 2733ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 2734ecc2d604Sbonwick * than the top-level vdev. 2735ecc2d604Sbonwick */ 2736ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2737fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2738fa9e4066Sahrens 2739fa9e4066Sahrens /* 2740fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 2741fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2742fa9e4066Sahrens */ 2743fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2744fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 2745fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2746fa9e4066Sahrens KM_SLEEP); 2747fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2748fa9e4066Sahrens newvd->vdev_path, "old"); 2749fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 2750fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 2751fa9e4066Sahrens oldvd->vdev_devid = NULL; 2752fa9e4066Sahrens } 2753fa9e4066Sahrens } 2754fa9e4066Sahrens 2755fa9e4066Sahrens /* 275699653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 275799653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 2758fa9e4066Sahrens */ 2759fa9e4066Sahrens if (pvd->vdev_ops != pvops) 2760fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 2761fa9e4066Sahrens 2762fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2763fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 2764fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 2765fa9e4066Sahrens 2766fa9e4066Sahrens /* 2767fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 2768fa9e4066Sahrens */ 2769fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 2770fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 2771fa9e4066Sahrens vdev_add_child(pvd, newvd); 2772fa9e4066Sahrens 2773ea8dc4b6Seschrock /* 2774ea8dc4b6Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 2775ea8dc4b6Seschrock * the addition of newvd may have decreased our parent's asize. 2776ea8dc4b6Seschrock */ 2777ea8dc4b6Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 2778ea8dc4b6Seschrock 2779fa9e4066Sahrens tvd = newvd->vdev_top; 2780fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 2781fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2782fa9e4066Sahrens 2783fa9e4066Sahrens vdev_config_dirty(tvd); 2784fa9e4066Sahrens 2785fa9e4066Sahrens /* 2786fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2787fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2788fa9e4066Sahrens */ 2789fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2790fa9e4066Sahrens 2791fa9e4066Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2792fa9e4066Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2793fa9e4066Sahrens open_txg - TXG_INITIAL + 1); 2794fa9e4066Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2795fa9e4066Sahrens 279639c23413Seschrock if (newvd->vdev_isspare) 279739c23413Seschrock spa_spare_activate(newvd); 2798ea8dc4b6Seschrock 2799fa9e4066Sahrens /* 2800fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 2801fa9e4066Sahrens */ 2802ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2803fa9e4066Sahrens 2804fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2805fa9e4066Sahrens 2806fa9e4066Sahrens /* 28073d7072f8Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 28083d7072f8Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 2809fa9e4066Sahrens */ 28103d7072f8Seschrock mutex_enter(&spa_namespace_lock); 2811fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 28123d7072f8Seschrock mutex_exit(&spa_namespace_lock); 2813fa9e4066Sahrens 2814fa9e4066Sahrens return (0); 2815fa9e4066Sahrens } 2816fa9e4066Sahrens 2817fa9e4066Sahrens /* 2818fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 2819fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 2820fa9e4066Sahrens * is a replacing vdev. 2821fa9e4066Sahrens */ 2822fa9e4066Sahrens int 2823ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2824fa9e4066Sahrens { 2825fa9e4066Sahrens uint64_t txg; 2826fa9e4066Sahrens int c, t, error; 2827fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2828fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 282999653d4eSeschrock boolean_t unspare = B_FALSE; 283099653d4eSeschrock uint64_t unspare_guid; 2831fa9e4066Sahrens 2832fa9e4066Sahrens txg = spa_vdev_enter(spa); 2833fa9e4066Sahrens 2834ea8dc4b6Seschrock vd = vdev_lookup_by_guid(rvd, guid); 2835fa9e4066Sahrens 2836fa9e4066Sahrens if (vd == NULL) 2837fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2838fa9e4066Sahrens 28390e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 28400e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28410e34b6a7Sbonwick 2842fa9e4066Sahrens pvd = vd->vdev_parent; 2843fa9e4066Sahrens 2844fa9e4066Sahrens /* 2845fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 284699653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 284799653d4eSeschrock * disk can be removed. 284899653d4eSeschrock */ 284999653d4eSeschrock if (replace_done) { 285099653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 285199653d4eSeschrock if (vd->vdev_id != 0) 285299653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 285399653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 285499653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 285599653d4eSeschrock } 285699653d4eSeschrock } 285799653d4eSeschrock 285899653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 2859e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2860fa9e4066Sahrens 2861fa9e4066Sahrens /* 286299653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 2863fa9e4066Sahrens */ 2864fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 286599653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 286699653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 2867fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2868fa9e4066Sahrens 2869fa9e4066Sahrens /* 2870fa9e4066Sahrens * If there's only one replica, you can't detach it. 2871fa9e4066Sahrens */ 2872fa9e4066Sahrens if (pvd->vdev_children <= 1) 2873fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2874fa9e4066Sahrens 2875fa9e4066Sahrens /* 2876fa9e4066Sahrens * If all siblings have non-empty DTLs, this device may have the only 2877fa9e4066Sahrens * valid copy of the data, which means we cannot safely detach it. 2878fa9e4066Sahrens * 2879fa9e4066Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2880fa9e4066Sahrens * precise DTL check. 2881fa9e4066Sahrens */ 2882fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2883fa9e4066Sahrens uint64_t dirty; 2884fa9e4066Sahrens 2885fa9e4066Sahrens cvd = pvd->vdev_child[c]; 2886fa9e4066Sahrens if (cvd == vd) 2887fa9e4066Sahrens continue; 2888fa9e4066Sahrens if (vdev_is_dead(cvd)) 2889fa9e4066Sahrens continue; 2890fa9e4066Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2891fa9e4066Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2892fa9e4066Sahrens cvd->vdev_dtl_scrub.sm_space; 2893fa9e4066Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2894fa9e4066Sahrens if (!dirty) 2895fa9e4066Sahrens break; 2896fa9e4066Sahrens } 289799653d4eSeschrock 289899653d4eSeschrock /* 289999653d4eSeschrock * If we are a replacing or spare vdev, then we can always detach the 290099653d4eSeschrock * latter child, as that is how one cancels the operation. 290199653d4eSeschrock */ 290299653d4eSeschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 290399653d4eSeschrock c == pvd->vdev_children) 2904fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2905fa9e4066Sahrens 290699653d4eSeschrock /* 290799653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 290899653d4eSeschrock * that the spare should become a real disk, and be removed from the 290999653d4eSeschrock * active spare list for the pool. 291099653d4eSeschrock */ 291199653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 291299653d4eSeschrock vd->vdev_id == 0) 291399653d4eSeschrock unspare = B_TRUE; 291499653d4eSeschrock 2915fa9e4066Sahrens /* 2916fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 2917fa9e4066Sahrens * This must be done after all other error cases are handled, 2918fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 2919fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 2920fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 2921fa9e4066Sahrens * it's being detached! 2922fa9e4066Sahrens */ 292339c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2924fa9e4066Sahrens 2925fa9e4066Sahrens /* 2926fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 2927fa9e4066Sahrens */ 2928fa9e4066Sahrens vdev_remove_child(pvd, vd); 2929fa9e4066Sahrens vdev_compact_children(pvd); 2930fa9e4066Sahrens 2931fa9e4066Sahrens /* 2932fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 2933fa9e4066Sahrens */ 2934fa9e4066Sahrens cvd = pvd->vdev_child[0]; 2935fa9e4066Sahrens 293699653d4eSeschrock /* 293799653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 293899653d4eSeschrock * do it now, marking the vdev as no longer a spare in the process. We 293999653d4eSeschrock * must do this before vdev_remove_parent(), because that can change the 294099653d4eSeschrock * GUID if it creates a new toplevel GUID. 294199653d4eSeschrock */ 294299653d4eSeschrock if (unspare) { 294399653d4eSeschrock ASSERT(cvd->vdev_isspare); 294439c23413Seschrock spa_spare_remove(cvd); 294599653d4eSeschrock unspare_guid = cvd->vdev_guid; 294699653d4eSeschrock } 294799653d4eSeschrock 2948fa9e4066Sahrens /* 2949fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 2950fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 2951fa9e4066Sahrens */ 2952fa9e4066Sahrens if (pvd->vdev_children == 1) 2953fa9e4066Sahrens vdev_remove_parent(cvd); 2954fa9e4066Sahrens 2955fa9e4066Sahrens /* 2956fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 2957fa9e4066Sahrens * may have been the previous top-level vdev. 2958fa9e4066Sahrens */ 2959fa9e4066Sahrens tvd = cvd->vdev_top; 2960fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2961fa9e4066Sahrens 2962fa9e4066Sahrens /* 296339c23413Seschrock * Reevaluate the parent vdev state. 2964fa9e4066Sahrens */ 29653d7072f8Seschrock vdev_propagate_state(cvd); 2966fa9e4066Sahrens 2967fa9e4066Sahrens /* 296839c23413Seschrock * If the device we just detached was smaller than the others, it may be 296939c23413Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 297039c23413Seschrock * can't fail because the existing metaslabs are already in core, so 297139c23413Seschrock * there's nothing to read from disk. 2972fa9e4066Sahrens */ 2973ecc2d604Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2974fa9e4066Sahrens 2975fa9e4066Sahrens vdev_config_dirty(tvd); 2976fa9e4066Sahrens 2977fa9e4066Sahrens /* 297839c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 297939c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 298039c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 298139c23413Seschrock * prevent vd from being accessed after it's freed. 2982fa9e4066Sahrens */ 2983fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) 2984fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 2985ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 2986ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 2987fa9e4066Sahrens 29883d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 29893d7072f8Seschrock 299099653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 299199653d4eSeschrock 299299653d4eSeschrock /* 299339c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 299439c23413Seschrock * then we want to go through and remove the device from the hot spare 299539c23413Seschrock * list of every other pool. 299699653d4eSeschrock */ 299799653d4eSeschrock if (unspare) { 299899653d4eSeschrock spa = NULL; 299999653d4eSeschrock mutex_enter(&spa_namespace_lock); 300099653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 300199653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 300299653d4eSeschrock continue; 300399653d4eSeschrock 300499653d4eSeschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 300599653d4eSeschrock } 300699653d4eSeschrock mutex_exit(&spa_namespace_lock); 300799653d4eSeschrock } 300899653d4eSeschrock 300999653d4eSeschrock return (error); 301099653d4eSeschrock } 301199653d4eSeschrock 301299653d4eSeschrock /* 3013fa94a07fSbrendan * Remove a spares vdev from the nvlist config. 301499653d4eSeschrock */ 3015fa94a07fSbrendan static int 3016fa94a07fSbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 3017fa94a07fSbrendan nvlist_t **spares, int nspares, vdev_t *vd) 301899653d4eSeschrock { 3019fa94a07fSbrendan nvlist_t *nv, **newspares; 3020fa94a07fSbrendan int i, j; 302199653d4eSeschrock 302299653d4eSeschrock nv = NULL; 3023fa94a07fSbrendan for (i = 0; i < nspares; i++) { 3024fa94a07fSbrendan uint64_t theguid; 302599653d4eSeschrock 3026fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(spares[i], 3027fa94a07fSbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 3028fa94a07fSbrendan if (theguid == guid) { 3029fa94a07fSbrendan nv = spares[i]; 3030fa94a07fSbrendan break; 303199653d4eSeschrock } 303299653d4eSeschrock } 303399653d4eSeschrock 303499653d4eSeschrock /* 3035fa94a07fSbrendan * Only remove the hot spare if it's not currently in use in this pool. 303699653d4eSeschrock */ 3037fa94a07fSbrendan if (nv == NULL && vd == NULL) 3038fa94a07fSbrendan return (ENOENT); 303999653d4eSeschrock 3040fa94a07fSbrendan if (nv == NULL && vd != NULL) 3041fa94a07fSbrendan return (ENOTSUP); 304299653d4eSeschrock 3043fa94a07fSbrendan if (!unspare && nv != NULL && vd != NULL) 3044fa94a07fSbrendan return (EBUSY); 304599653d4eSeschrock 304699653d4eSeschrock if (nspares == 1) { 304799653d4eSeschrock newspares = NULL; 304899653d4eSeschrock } else { 304999653d4eSeschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 305099653d4eSeschrock KM_SLEEP); 305199653d4eSeschrock for (i = 0, j = 0; i < nspares; i++) { 305299653d4eSeschrock if (spares[i] != nv) 305399653d4eSeschrock VERIFY(nvlist_dup(spares[i], 305499653d4eSeschrock &newspares[j++], KM_SLEEP) == 0); 305599653d4eSeschrock } 305699653d4eSeschrock } 305799653d4eSeschrock 3058fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 305999653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3060fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3061fa94a07fSbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 306299653d4eSeschrock for (i = 0; i < nspares - 1; i++) 306399653d4eSeschrock nvlist_free(newspares[i]); 306499653d4eSeschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 3065fa94a07fSbrendan 3066fa94a07fSbrendan return (0); 3067fa94a07fSbrendan } 3068fa94a07fSbrendan 3069fa94a07fSbrendan /* 3070fa94a07fSbrendan * Remove an l2cache vdev from the nvlist config. 3071fa94a07fSbrendan */ 3072fa94a07fSbrendan static int 3073fa94a07fSbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 3074fa94a07fSbrendan int nl2cache, vdev_t *vd) 3075fa94a07fSbrendan { 3076fa94a07fSbrendan nvlist_t *nv, **newl2cache; 3077fa94a07fSbrendan int i, j; 3078fa94a07fSbrendan 3079fa94a07fSbrendan nv = NULL; 3080fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3081fa94a07fSbrendan uint64_t theguid; 3082fa94a07fSbrendan 3083fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3084fa94a07fSbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 3085fa94a07fSbrendan if (theguid == guid) { 3086fa94a07fSbrendan nv = l2cache[i]; 3087fa94a07fSbrendan break; 3088fa94a07fSbrendan } 3089fa94a07fSbrendan } 3090fa94a07fSbrendan 3091fa94a07fSbrendan if (vd == NULL) { 3092fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3093fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 3094fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3095fa94a07fSbrendan break; 3096fa94a07fSbrendan } 3097fa94a07fSbrendan } 3098fa94a07fSbrendan } 3099fa94a07fSbrendan 3100fa94a07fSbrendan if (nv == NULL && vd == NULL) 3101fa94a07fSbrendan return (ENOENT); 3102fa94a07fSbrendan 3103fa94a07fSbrendan if (nv == NULL && vd != NULL) 3104fa94a07fSbrendan return (ENOTSUP); 3105fa94a07fSbrendan 3106fa94a07fSbrendan if (nl2cache == 1) { 3107fa94a07fSbrendan newl2cache = NULL; 3108fa94a07fSbrendan } else { 3109fa94a07fSbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 3110fa94a07fSbrendan KM_SLEEP); 3111fa94a07fSbrendan for (i = 0, j = 0; i < nl2cache; i++) { 3112fa94a07fSbrendan if (l2cache[i] != nv) 3113fa94a07fSbrendan VERIFY(nvlist_dup(l2cache[i], 3114fa94a07fSbrendan &newl2cache[j++], KM_SLEEP) == 0); 3115fa94a07fSbrendan } 3116fa94a07fSbrendan } 3117fa94a07fSbrendan 3118fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 3119fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3120fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3121fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 3122fa94a07fSbrendan for (i = 0; i < nl2cache - 1; i++) 3123fa94a07fSbrendan nvlist_free(newl2cache[i]); 3124fa94a07fSbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 3125fa94a07fSbrendan 3126fa94a07fSbrendan return (0); 3127fa94a07fSbrendan } 3128fa94a07fSbrendan 3129fa94a07fSbrendan /* 3130fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 3131fa94a07fSbrendan * spares and level 2 ARC devices. 3132fa94a07fSbrendan */ 3133fa94a07fSbrendan int 3134fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 3135fa94a07fSbrendan { 3136fa94a07fSbrendan vdev_t *vd; 3137fa94a07fSbrendan nvlist_t **spares, **l2cache; 3138fa94a07fSbrendan uint_t nspares, nl2cache; 3139fa94a07fSbrendan int error = 0; 3140fa94a07fSbrendan 3141fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 3142fa94a07fSbrendan 3143fa94a07fSbrendan vd = spa_lookup_by_guid(spa, guid); 3144fa94a07fSbrendan 3145fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 3146fa94a07fSbrendan spa_spare_exists(guid, NULL) && 3147fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 3148fa94a07fSbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 3149fa94a07fSbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 3150fa94a07fSbrendan spares, nspares, vd)) != 0) 3151fa94a07fSbrendan goto out; 3152fa94a07fSbrendan spa_load_spares(spa); 3153fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3154fa94a07fSbrendan goto out; 3155fa94a07fSbrendan } 3156fa94a07fSbrendan 3157fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 3158fa94a07fSbrendan spa_l2cache_exists(guid, NULL) && 3159fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3160fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 3161fa94a07fSbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 3162fa94a07fSbrendan l2cache, nl2cache, vd)) != 0) 3163fa94a07fSbrendan goto out; 3164fa94a07fSbrendan spa_load_l2cache(spa); 3165fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3166fa94a07fSbrendan } 316799653d4eSeschrock 316899653d4eSeschrock out: 316999653d4eSeschrock spa_config_exit(spa, FTAG); 3170fa94a07fSbrendan return (error); 3171fa9e4066Sahrens } 3172fa9e4066Sahrens 3173fa9e4066Sahrens /* 31743d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 31753d7072f8Seschrock * current spared, so we can detach it. 3176fa9e4066Sahrens */ 3177ea8dc4b6Seschrock static vdev_t * 31783d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3179fa9e4066Sahrens { 3180ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 3181fa9e4066Sahrens int c; 3182fa9e4066Sahrens 3183ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) { 31843d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 3185ea8dc4b6Seschrock if (oldvd != NULL) 3186ea8dc4b6Seschrock return (oldvd); 3187ea8dc4b6Seschrock } 3188fa9e4066Sahrens 31893d7072f8Seschrock /* 31903d7072f8Seschrock * Check for a completed replacement. 31913d7072f8Seschrock */ 3192fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 3193ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 3194ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 3195ea8dc4b6Seschrock 3196ea8dc4b6Seschrock mutex_enter(&newvd->vdev_dtl_lock); 3197ea8dc4b6Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 3198ea8dc4b6Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 3199ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 3200ea8dc4b6Seschrock return (oldvd); 3201fa9e4066Sahrens } 3202ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 3203fa9e4066Sahrens } 3204ea8dc4b6Seschrock 32053d7072f8Seschrock /* 32063d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 32073d7072f8Seschrock */ 32083d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 32093d7072f8Seschrock newvd = vd->vdev_child[0]; 32103d7072f8Seschrock oldvd = vd->vdev_child[1]; 32113d7072f8Seschrock 32123d7072f8Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32133d7072f8Seschrock if (newvd->vdev_unspare && 32143d7072f8Seschrock newvd->vdev_dtl_map.sm_space == 0 && 32153d7072f8Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32163d7072f8Seschrock newvd->vdev_unspare = 0; 32173d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32183d7072f8Seschrock return (oldvd); 32193d7072f8Seschrock } 32203d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32213d7072f8Seschrock } 32223d7072f8Seschrock 3223ea8dc4b6Seschrock return (NULL); 3224fa9e4066Sahrens } 3225fa9e4066Sahrens 3226ea8dc4b6Seschrock static void 32273d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 3228fa9e4066Sahrens { 3229ea8dc4b6Seschrock vdev_t *vd; 323099653d4eSeschrock vdev_t *pvd; 3231ea8dc4b6Seschrock uint64_t guid; 323299653d4eSeschrock uint64_t pguid = 0; 3233ea8dc4b6Seschrock 3234ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 3235ea8dc4b6Seschrock 32363d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 3237ea8dc4b6Seschrock guid = vd->vdev_guid; 323899653d4eSeschrock /* 323999653d4eSeschrock * If we have just finished replacing a hot spared device, then 324099653d4eSeschrock * we need to detach the parent's first child (the original hot 324199653d4eSeschrock * spare) as well. 324299653d4eSeschrock */ 324399653d4eSeschrock pvd = vd->vdev_parent; 324499653d4eSeschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 324599653d4eSeschrock pvd->vdev_id == 0) { 324699653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 324799653d4eSeschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 324899653d4eSeschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 324999653d4eSeschrock } 3250ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3251ea8dc4b6Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 3252ea8dc4b6Seschrock return; 325399653d4eSeschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 325499653d4eSeschrock return; 3255ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 3256fa9e4066Sahrens } 3257fa9e4066Sahrens 3258ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3259fa9e4066Sahrens } 3260fa9e4066Sahrens 3261c67d9675Seschrock /* 3262c67d9675Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 3263c67d9675Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 3264c67d9675Seschrock */ 3265c67d9675Seschrock int 3266c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 3267c67d9675Seschrock { 3268c67d9675Seschrock vdev_t *rvd, *vd; 3269c67d9675Seschrock uint64_t txg; 3270c67d9675Seschrock 3271c67d9675Seschrock rvd = spa->spa_root_vdev; 3272c67d9675Seschrock 3273c67d9675Seschrock txg = spa_vdev_enter(spa); 3274c67d9675Seschrock 327599653d4eSeschrock if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 327699653d4eSeschrock /* 3277fa94a07fSbrendan * Determine if this is a reference to a hot spare or l2cache 3278fa94a07fSbrendan * device. If it is, update the path as stored in their 3279fa94a07fSbrendan * device list. 328099653d4eSeschrock */ 3281fa94a07fSbrendan nvlist_t **spares, **l2cache; 3282fa94a07fSbrendan uint_t i, nspares, nl2cache; 3283fa94a07fSbrendan 3284fa94a07fSbrendan if (spa->spa_spares.sav_config != NULL) { 3285fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array( 3286fa94a07fSbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 3287fa94a07fSbrendan &spares, &nspares) == 0); 328899653d4eSeschrock for (i = 0; i < nspares; i++) { 328999653d4eSeschrock uint64_t theguid; 329099653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 329199653d4eSeschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 3292fa94a07fSbrendan if (theguid == guid) { 3293fa94a07fSbrendan VERIFY(nvlist_add_string(spares[i], 3294fa94a07fSbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 3295fa94a07fSbrendan spa_load_spares(spa); 3296fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3297fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, 3298fa94a07fSbrendan 0)); 3299fa94a07fSbrendan } 330099653d4eSeschrock } 3301fa94a07fSbrendan } 330299653d4eSeschrock 3303fa94a07fSbrendan if (spa->spa_l2cache.sav_config != NULL) { 3304fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array( 3305fa94a07fSbrendan spa->spa_l2cache.sav_config, ZPOOL_CONFIG_L2CACHE, 3306fa94a07fSbrendan &l2cache, &nl2cache) == 0); 3307fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3308fa94a07fSbrendan uint64_t theguid; 3309fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3310fa94a07fSbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 3311fa94a07fSbrendan if (theguid == guid) { 3312fa94a07fSbrendan VERIFY(nvlist_add_string(l2cache[i], 3313fa94a07fSbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 3314fa94a07fSbrendan spa_load_l2cache(spa); 3315fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3316fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, 3317fa94a07fSbrendan 0)); 3318fa94a07fSbrendan } 3319fa94a07fSbrendan } 332099653d4eSeschrock } 3321fa94a07fSbrendan 3322fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 332399653d4eSeschrock } 3324c67d9675Seschrock 33250e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 33260e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33270e34b6a7Sbonwick 3328c67d9675Seschrock spa_strfree(vd->vdev_path); 3329c67d9675Seschrock vd->vdev_path = spa_strdup(newpath); 3330c67d9675Seschrock 3331c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 3332c67d9675Seschrock 3333c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 3334c67d9675Seschrock } 3335c67d9675Seschrock 3336fa9e4066Sahrens /* 3337fa9e4066Sahrens * ========================================================================== 3338fa9e4066Sahrens * SPA Scrubbing 3339fa9e4066Sahrens * ========================================================================== 3340fa9e4066Sahrens */ 3341fa9e4066Sahrens 3342fa9e4066Sahrens static void 3343fa9e4066Sahrens spa_scrub_io_done(zio_t *zio) 3344fa9e4066Sahrens { 3345fa9e4066Sahrens spa_t *spa = zio->io_spa; 3346fa9e4066Sahrens 33470e8c6158Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 3348fa9e4066Sahrens 3349fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3350ea8dc4b6Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 335144cd46caSbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 3352ea8dc4b6Seschrock spa->spa_scrub_errors++; 3353fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3354fa9e4066Sahrens vd->vdev_stat.vs_scrub_errors++; 3355fa9e4066Sahrens mutex_exit(&vd->vdev_stat_lock); 3356fa9e4066Sahrens } 335705b2b3b8Smishra 335805b2b3b8Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 3359ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 336005b2b3b8Smishra 336105b2b3b8Smishra ASSERT(spa->spa_scrub_inflight >= 0); 336205b2b3b8Smishra 3363ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3364fa9e4066Sahrens } 3365fa9e4066Sahrens 3366fa9e4066Sahrens static void 3367ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 3368ea8dc4b6Seschrock zbookmark_t *zb) 3369fa9e4066Sahrens { 3370fa9e4066Sahrens size_t size = BP_GET_LSIZE(bp); 337105b2b3b8Smishra void *data; 3372fa9e4066Sahrens 3373fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 337405b2b3b8Smishra /* 337505b2b3b8Smishra * Do not give too much work to vdev(s). 337605b2b3b8Smishra */ 337705b2b3b8Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 337805b2b3b8Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 337905b2b3b8Smishra } 3380fa9e4066Sahrens spa->spa_scrub_inflight++; 3381fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3382fa9e4066Sahrens 33830e8c6158Smaybee data = arc_data_buf_alloc(size); 338405b2b3b8Smishra 3385ea8dc4b6Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 3386ea8dc4b6Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 3387ea8dc4b6Seschrock 3388d80c45e0Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 3389ea8dc4b6Seschrock 3390fa9e4066Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 3391ea8dc4b6Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 3392fa9e4066Sahrens } 3393fa9e4066Sahrens 3394fa9e4066Sahrens /* ARGSUSED */ 3395fa9e4066Sahrens static int 3396fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 3397fa9e4066Sahrens { 3398fa9e4066Sahrens blkptr_t *bp = &bc->bc_blkptr; 339944cd46caSbillm vdev_t *vd = spa->spa_root_vdev; 340044cd46caSbillm dva_t *dva = bp->blk_dva; 340144cd46caSbillm int needs_resilver = B_FALSE; 340244cd46caSbillm int d; 3403fa9e4066Sahrens 340444cd46caSbillm if (bc->bc_errno) { 3405fa9e4066Sahrens /* 3406fa9e4066Sahrens * We can't scrub this block, but we can continue to scrub 3407fa9e4066Sahrens * the rest of the pool. Note the error and move along. 3408fa9e4066Sahrens */ 3409fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3410fa9e4066Sahrens spa->spa_scrub_errors++; 3411fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3412fa9e4066Sahrens 341344cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 341444cd46caSbillm vd->vdev_stat.vs_scrub_errors++; 341544cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 3416fa9e4066Sahrens 3417fa9e4066Sahrens return (ERESTART); 3418fa9e4066Sahrens } 3419fa9e4066Sahrens 3420fa9e4066Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 3421fa9e4066Sahrens 342244cd46caSbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 342344cd46caSbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 3424fa9e4066Sahrens 342544cd46caSbillm ASSERT(vd != NULL); 342644cd46caSbillm 342744cd46caSbillm /* 342844cd46caSbillm * Keep track of how much data we've examined so that 342944cd46caSbillm * zpool(1M) status can make useful progress reports. 343044cd46caSbillm */ 343144cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 343244cd46caSbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 343344cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 343444cd46caSbillm 343544cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 343644cd46caSbillm if (DVA_GET_GANG(&dva[d])) { 343744cd46caSbillm /* 343844cd46caSbillm * Gang members may be spread across multiple 343944cd46caSbillm * vdevs, so the best we can do is look at the 344044cd46caSbillm * pool-wide DTL. 344144cd46caSbillm * XXX -- it would be better to change our 344244cd46caSbillm * allocation policy to ensure that this can't 344344cd46caSbillm * happen. 344444cd46caSbillm */ 344544cd46caSbillm vd = spa->spa_root_vdev; 344644cd46caSbillm } 344744cd46caSbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 344844cd46caSbillm bp->blk_birth, 1)) 344944cd46caSbillm needs_resilver = B_TRUE; 3450fa9e4066Sahrens } 345144cd46caSbillm } 345244cd46caSbillm 345344cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 3454fa9e4066Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 3455ea8dc4b6Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 345644cd46caSbillm else if (needs_resilver) 345744cd46caSbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 345844cd46caSbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 3459fa9e4066Sahrens 3460fa9e4066Sahrens return (0); 3461fa9e4066Sahrens } 3462fa9e4066Sahrens 3463fa9e4066Sahrens static void 3464fa9e4066Sahrens spa_scrub_thread(spa_t *spa) 3465fa9e4066Sahrens { 3466fa9e4066Sahrens callb_cpr_t cprinfo; 3467fa9e4066Sahrens traverse_handle_t *th = spa->spa_scrub_th; 3468fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3469fa9e4066Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 3470fa9e4066Sahrens int error = 0; 3471fa9e4066Sahrens boolean_t complete; 3472fa9e4066Sahrens 3473fa9e4066Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 3474fa9e4066Sahrens 3475f0aa80d4Sbonwick /* 3476f0aa80d4Sbonwick * If we're restarting due to a snapshot create/delete, 3477f0aa80d4Sbonwick * wait for that to complete. 3478f0aa80d4Sbonwick */ 3479f0aa80d4Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 3480f0aa80d4Sbonwick 3481ea8dc4b6Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 3482ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 3483ea8dc4b6Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 3484ea8dc4b6Seschrock 3485ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 3486ea8dc4b6Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 3487fa9e4066Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 3488fa9e4066Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 3489ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3490fa9e4066Sahrens 3491fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3492fa9e4066Sahrens spa->spa_scrub_errors = 0; 3493fa9e4066Sahrens spa->spa_scrub_active = 1; 3494ea8dc4b6Seschrock ASSERT(spa->spa_scrub_inflight == 0); 3495fa9e4066Sahrens 3496fa9e4066Sahrens while (!spa->spa_scrub_stop) { 3497fa9e4066Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 3498ea8dc4b6Seschrock while (spa->spa_scrub_suspended) { 3499fa9e4066Sahrens spa->spa_scrub_active = 0; 3500fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3501fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3502fa9e4066Sahrens spa->spa_scrub_active = 1; 3503fa9e4066Sahrens } 3504fa9e4066Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 3505fa9e4066Sahrens 3506fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 3507fa9e4066Sahrens break; 3508fa9e4066Sahrens 3509fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3510fa9e4066Sahrens error = traverse_more(th); 3511fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3512fa9e4066Sahrens if (error != EAGAIN) 3513fa9e4066Sahrens break; 3514fa9e4066Sahrens } 3515fa9e4066Sahrens 3516fa9e4066Sahrens while (spa->spa_scrub_inflight) 3517fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3518fa9e4066Sahrens 35195dabedeeSbonwick spa->spa_scrub_active = 0; 35205dabedeeSbonwick cv_broadcast(&spa->spa_scrub_cv); 35215dabedeeSbonwick 35225dabedeeSbonwick mutex_exit(&spa->spa_scrub_lock); 35235dabedeeSbonwick 35245dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 35255dabedeeSbonwick 35265dabedeeSbonwick mutex_enter(&spa->spa_scrub_lock); 35275dabedeeSbonwick 35285dabedeeSbonwick /* 35295dabedeeSbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 35305dabedeeSbonwick * AND the spa config lock to synchronize with any config changes 35315dabedeeSbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 35325dabedeeSbonwick */ 3533fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 3534fa9e4066Sahrens error = ERESTART; 3535fa9e4066Sahrens 3536ea8dc4b6Seschrock if (spa->spa_scrub_stop) 3537ea8dc4b6Seschrock error = EINTR; 3538ea8dc4b6Seschrock 3539fa9e4066Sahrens /* 3540ea8dc4b6Seschrock * Even if there were uncorrectable errors, we consider the scrub 3541ea8dc4b6Seschrock * completed. The downside is that if there is a transient error during 3542ea8dc4b6Seschrock * a resilver, we won't resilver the data properly to the target. But 3543ea8dc4b6Seschrock * if the damage is permanent (more likely) we will resilver forever, 3544ea8dc4b6Seschrock * which isn't really acceptable. Since there is enough information for 3545ea8dc4b6Seschrock * the user to know what has failed and why, this seems like a more 3546ea8dc4b6Seschrock * tractable approach. 3547fa9e4066Sahrens */ 3548ea8dc4b6Seschrock complete = (error == 0); 3549fa9e4066Sahrens 3550ea8dc4b6Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 3551ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 3552fa9e4066Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 3553fa9e4066Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 3554fa9e4066Sahrens 3555fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3556fa9e4066Sahrens 3557fa9e4066Sahrens /* 3558fa9e4066Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 3559fa9e4066Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 3560fa9e4066Sahrens */ 3561fa9e4066Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 3562fa9e4066Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 3563fa9e4066Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 3564ea8dc4b6Seschrock spa_errlog_rotate(spa); 35655dabedeeSbonwick 35663d7072f8Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 35673d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 35683d7072f8Seschrock 3569ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3570fa9e4066Sahrens 3571fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3572fa9e4066Sahrens 3573ea8dc4b6Seschrock /* 3574ea8dc4b6Seschrock * We may have finished replacing a device. 3575ea8dc4b6Seschrock * Let the async thread assess this and handle the detach. 3576ea8dc4b6Seschrock */ 35773d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3578fa9e4066Sahrens 3579fa9e4066Sahrens /* 3580fa9e4066Sahrens * If we were told to restart, our final act is to start a new scrub. 3581fa9e4066Sahrens */ 3582fa9e4066Sahrens if (error == ERESTART) 3583ea8dc4b6Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 3584ea8dc4b6Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 3585fa9e4066Sahrens 3586ea8dc4b6Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 3587ea8dc4b6Seschrock spa->spa_scrub_active = 0; 3588ea8dc4b6Seschrock spa->spa_scrub_thread = NULL; 3589ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_cv); 3590fa9e4066Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 3591fa9e4066Sahrens thread_exit(); 3592fa9e4066Sahrens } 3593fa9e4066Sahrens 3594fa9e4066Sahrens void 3595fa9e4066Sahrens spa_scrub_suspend(spa_t *spa) 3596fa9e4066Sahrens { 3597fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3598ea8dc4b6Seschrock spa->spa_scrub_suspended++; 3599fa9e4066Sahrens while (spa->spa_scrub_active) { 3600fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3601fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3602fa9e4066Sahrens } 3603fa9e4066Sahrens while (spa->spa_scrub_inflight) 3604fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3605fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3606fa9e4066Sahrens } 3607fa9e4066Sahrens 3608fa9e4066Sahrens void 3609fa9e4066Sahrens spa_scrub_resume(spa_t *spa) 3610fa9e4066Sahrens { 3611fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3612ea8dc4b6Seschrock ASSERT(spa->spa_scrub_suspended != 0); 3613ea8dc4b6Seschrock if (--spa->spa_scrub_suspended == 0) 3614fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3615fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3616fa9e4066Sahrens } 3617fa9e4066Sahrens 3618fa9e4066Sahrens void 3619fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 3620fa9e4066Sahrens { 3621fa9e4066Sahrens /* 3622fa9e4066Sahrens * Something happened (e.g. snapshot create/delete) that means 3623fa9e4066Sahrens * we must restart any in-progress scrubs. The itinerary will 3624fa9e4066Sahrens * fix this properly. 3625fa9e4066Sahrens */ 3626fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3627fa9e4066Sahrens spa->spa_scrub_restart_txg = txg; 3628fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3629fa9e4066Sahrens } 3630fa9e4066Sahrens 3631ea8dc4b6Seschrock int 3632ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 3633fa9e4066Sahrens { 3634fa9e4066Sahrens space_seg_t *ss; 3635fa9e4066Sahrens uint64_t mintxg, maxtxg; 3636fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3637fa9e4066Sahrens 3638bb8b5132Sek ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3639bb8b5132Sek ASSERT(!spa_config_held(spa, RW_WRITER)); 3640bb8b5132Sek 3641fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3642fa9e4066Sahrens return (ENOTSUP); 3643fa9e4066Sahrens 3644ea8dc4b6Seschrock mutex_enter(&spa->spa_scrub_lock); 3645ea8dc4b6Seschrock 3646fa9e4066Sahrens /* 3647fa9e4066Sahrens * If there's a scrub or resilver already in progress, stop it. 3648fa9e4066Sahrens */ 3649fa9e4066Sahrens while (spa->spa_scrub_thread != NULL) { 3650fa9e4066Sahrens /* 3651fa9e4066Sahrens * Don't stop a resilver unless forced. 3652fa9e4066Sahrens */ 3653ea8dc4b6Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 3654ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3655fa9e4066Sahrens return (EBUSY); 3656ea8dc4b6Seschrock } 3657fa9e4066Sahrens spa->spa_scrub_stop = 1; 3658fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3659fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3660fa9e4066Sahrens } 3661fa9e4066Sahrens 3662fa9e4066Sahrens /* 3663fa9e4066Sahrens * Terminate the previous traverse. 3664fa9e4066Sahrens */ 3665fa9e4066Sahrens if (spa->spa_scrub_th != NULL) { 3666fa9e4066Sahrens traverse_fini(spa->spa_scrub_th); 3667fa9e4066Sahrens spa->spa_scrub_th = NULL; 3668fa9e4066Sahrens } 3669fa9e4066Sahrens 3670ea8dc4b6Seschrock if (rvd == NULL) { 3671ea8dc4b6Seschrock ASSERT(spa->spa_scrub_stop == 0); 3672ea8dc4b6Seschrock ASSERT(spa->spa_scrub_type == type); 3673ea8dc4b6Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 3674ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3675ea8dc4b6Seschrock return (0); 3676ea8dc4b6Seschrock } 3677fa9e4066Sahrens 3678fa9e4066Sahrens mintxg = TXG_INITIAL - 1; 3679fa9e4066Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 3680fa9e4066Sahrens 3681ea8dc4b6Seschrock mutex_enter(&rvd->vdev_dtl_lock); 3682fa9e4066Sahrens 3683ea8dc4b6Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 3684ea8dc4b6Seschrock /* 3685ea8dc4b6Seschrock * The pool-wide DTL is empty. 3686ecc2d604Sbonwick * If this is a resilver, there's nothing to do except 3687ecc2d604Sbonwick * check whether any in-progress replacements have completed. 3688ea8dc4b6Seschrock */ 3689ecc2d604Sbonwick if (type == POOL_SCRUB_RESILVER) { 3690ea8dc4b6Seschrock type = POOL_SCRUB_NONE; 36913d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3692ecc2d604Sbonwick } 3693ea8dc4b6Seschrock } else { 3694ea8dc4b6Seschrock /* 3695ea8dc4b6Seschrock * The pool-wide DTL is non-empty. 3696ea8dc4b6Seschrock * If this is a normal scrub, upgrade to a resilver instead. 3697ea8dc4b6Seschrock */ 3698ea8dc4b6Seschrock if (type == POOL_SCRUB_EVERYTHING) 3699ea8dc4b6Seschrock type = POOL_SCRUB_RESILVER; 3700ea8dc4b6Seschrock } 3701fa9e4066Sahrens 3702ea8dc4b6Seschrock if (type == POOL_SCRUB_RESILVER) { 3703fa9e4066Sahrens /* 3704fa9e4066Sahrens * Determine the resilvering boundaries. 3705fa9e4066Sahrens * 3706fa9e4066Sahrens * Note: (mintxg, maxtxg) is an open interval, 3707fa9e4066Sahrens * i.e. mintxg and maxtxg themselves are not included. 3708fa9e4066Sahrens * 3709fa9e4066Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 3710fa9e4066Sahrens * so we don't claim to resilver a txg that's still changing. 3711fa9e4066Sahrens */ 3712fa9e4066Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 3713ea8dc4b6Seschrock mintxg = ss->ss_start - 1; 3714fa9e4066Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 3715ea8dc4b6Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 37163d7072f8Seschrock 37173d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 3718fa9e4066Sahrens } 3719fa9e4066Sahrens 3720ea8dc4b6Seschrock mutex_exit(&rvd->vdev_dtl_lock); 3721ea8dc4b6Seschrock 3722ea8dc4b6Seschrock spa->spa_scrub_stop = 0; 3723ea8dc4b6Seschrock spa->spa_scrub_type = type; 3724ea8dc4b6Seschrock spa->spa_scrub_restart_txg = 0; 3725ea8dc4b6Seschrock 3726ea8dc4b6Seschrock if (type != POOL_SCRUB_NONE) { 3727ea8dc4b6Seschrock spa->spa_scrub_mintxg = mintxg; 3728fa9e4066Sahrens spa->spa_scrub_maxtxg = maxtxg; 3729fa9e4066Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 37300373e76bSbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 37310373e76bSbonwick ZIO_FLAG_CANFAIL); 3732fa9e4066Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 3733fa9e4066Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 3734fa9e4066Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 3735fa9e4066Sahrens } 3736fa9e4066Sahrens 3737ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3738ea8dc4b6Seschrock 3739fa9e4066Sahrens return (0); 3740fa9e4066Sahrens } 3741fa9e4066Sahrens 3742ea8dc4b6Seschrock /* 3743ea8dc4b6Seschrock * ========================================================================== 3744ea8dc4b6Seschrock * SPA async task processing 3745ea8dc4b6Seschrock * ========================================================================== 3746ea8dc4b6Seschrock */ 3747ea8dc4b6Seschrock 3748ea8dc4b6Seschrock static void 37493d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3750fa9e4066Sahrens { 3751ea8dc4b6Seschrock vdev_t *tvd; 3752ea8dc4b6Seschrock int c; 3753fa9e4066Sahrens 37543d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) { 37553d7072f8Seschrock tvd = vd->vdev_child[c]; 37563d7072f8Seschrock if (tvd->vdev_remove_wanted) { 37573d7072f8Seschrock tvd->vdev_remove_wanted = 0; 37583d7072f8Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 37593d7072f8Seschrock VDEV_AUX_NONE); 37600a4e9518Sgw vdev_clear(spa, tvd, B_TRUE); 37613d7072f8Seschrock vdev_config_dirty(tvd->vdev_top); 3762ea8dc4b6Seschrock } 37633d7072f8Seschrock spa_async_remove(spa, tvd); 3764ea8dc4b6Seschrock } 3765ea8dc4b6Seschrock } 3766fa9e4066Sahrens 3767ea8dc4b6Seschrock static void 3768ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 3769ea8dc4b6Seschrock { 3770ea8dc4b6Seschrock int tasks; 37713d7072f8Seschrock uint64_t txg; 3772ea8dc4b6Seschrock 3773ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 3774ea8dc4b6Seschrock 3775ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3776ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 3777ea8dc4b6Seschrock spa->spa_async_tasks = 0; 3778ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3779ea8dc4b6Seschrock 37800373e76bSbonwick /* 37810373e76bSbonwick * See if the config needs to be updated. 37820373e76bSbonwick */ 37830373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 37840373e76bSbonwick mutex_enter(&spa_namespace_lock); 37850373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37860373e76bSbonwick mutex_exit(&spa_namespace_lock); 37870373e76bSbonwick } 37880373e76bSbonwick 3789ea8dc4b6Seschrock /* 37903d7072f8Seschrock * See if any devices need to be marked REMOVED. 37910a4e9518Sgw * 37920a4e9518Sgw * XXX - We avoid doing this when we are in 37930a4e9518Sgw * I/O failure state since spa_vdev_enter() grabs 37940a4e9518Sgw * the namespace lock and would not be able to obtain 37950a4e9518Sgw * the writer config lock. 3796ea8dc4b6Seschrock */ 37970a4e9518Sgw if (tasks & SPA_ASYNC_REMOVE && 37980a4e9518Sgw spa_state(spa) != POOL_STATE_IO_FAILURE) { 37993d7072f8Seschrock txg = spa_vdev_enter(spa); 38003d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 38013d7072f8Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 38023d7072f8Seschrock } 3803ea8dc4b6Seschrock 3804ea8dc4b6Seschrock /* 3805ea8dc4b6Seschrock * If any devices are done replacing, detach them. 3806ea8dc4b6Seschrock */ 38073d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 38083d7072f8Seschrock spa_vdev_resilver_done(spa); 3809fa9e4066Sahrens 3810ea8dc4b6Seschrock /* 38113d7072f8Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 38123d7072f8Seschrock * scrub which can become a resilver), we need to hold 38133d7072f8Seschrock * spa_namespace_lock() because the sysevent we post via 38143d7072f8Seschrock * spa_event_notify() needs to get the name of the pool. 3815ea8dc4b6Seschrock */ 38163d7072f8Seschrock if (tasks & SPA_ASYNC_SCRUB) { 38173d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3818ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 38193d7072f8Seschrock mutex_exit(&spa_namespace_lock); 38203d7072f8Seschrock } 3821ea8dc4b6Seschrock 3822ea8dc4b6Seschrock /* 3823ea8dc4b6Seschrock * Kick off a resilver. 3824ea8dc4b6Seschrock */ 38253d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER) { 38263d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3827ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 38283d7072f8Seschrock mutex_exit(&spa_namespace_lock); 38293d7072f8Seschrock } 3830ea8dc4b6Seschrock 3831ea8dc4b6Seschrock /* 3832ea8dc4b6Seschrock * Let the world know that we're done. 3833ea8dc4b6Seschrock */ 3834ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3835ea8dc4b6Seschrock spa->spa_async_thread = NULL; 3836ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 3837ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3838ea8dc4b6Seschrock thread_exit(); 3839ea8dc4b6Seschrock } 3840ea8dc4b6Seschrock 3841ea8dc4b6Seschrock void 3842ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 3843ea8dc4b6Seschrock { 3844ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3845ea8dc4b6Seschrock spa->spa_async_suspended++; 3846ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 3847ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 3848ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3849ea8dc4b6Seschrock } 3850ea8dc4b6Seschrock 3851ea8dc4b6Seschrock void 3852ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 3853ea8dc4b6Seschrock { 3854ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3855ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 3856ea8dc4b6Seschrock spa->spa_async_suspended--; 3857ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3858ea8dc4b6Seschrock } 3859ea8dc4b6Seschrock 3860ea8dc4b6Seschrock static void 3861ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 3862ea8dc4b6Seschrock { 3863ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3864ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38650373e76bSbonwick spa->spa_async_thread == NULL && 38660373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 3867ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 3868ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 3869ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3870ea8dc4b6Seschrock } 3871ea8dc4b6Seschrock 3872ea8dc4b6Seschrock void 3873ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 3874ea8dc4b6Seschrock { 3875ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3876ea8dc4b6Seschrock spa->spa_async_tasks |= task; 3877ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3878fa9e4066Sahrens } 3879fa9e4066Sahrens 3880fa9e4066Sahrens /* 3881fa9e4066Sahrens * ========================================================================== 3882fa9e4066Sahrens * SPA syncing routines 3883fa9e4066Sahrens * ========================================================================== 3884fa9e4066Sahrens */ 3885fa9e4066Sahrens 3886fa9e4066Sahrens static void 3887fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3888fa9e4066Sahrens { 3889fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3890fa9e4066Sahrens dmu_tx_t *tx; 3891fa9e4066Sahrens blkptr_t blk; 3892fa9e4066Sahrens uint64_t itor = 0; 3893fa9e4066Sahrens zio_t *zio; 3894fa9e4066Sahrens int error; 3895fa9e4066Sahrens uint8_t c = 1; 3896fa9e4066Sahrens 3897fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3898fa9e4066Sahrens 3899fa9e4066Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3900fa9e4066Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3901fa9e4066Sahrens 3902fa9e4066Sahrens error = zio_wait(zio); 3903fa9e4066Sahrens ASSERT3U(error, ==, 0); 3904fa9e4066Sahrens 3905fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3906fa9e4066Sahrens bplist_vacate(bpl, tx); 3907fa9e4066Sahrens 3908fa9e4066Sahrens /* 3909fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 3910fa9e4066Sahrens * (Usually only the first block is needed.) 3911fa9e4066Sahrens */ 3912fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3913fa9e4066Sahrens dmu_tx_commit(tx); 3914fa9e4066Sahrens } 3915fa9e4066Sahrens 3916fa9e4066Sahrens static void 391799653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 3918fa9e4066Sahrens { 3919fa9e4066Sahrens char *packed = NULL; 3920fa9e4066Sahrens size_t nvsize = 0; 3921fa9e4066Sahrens dmu_buf_t *db; 3922fa9e4066Sahrens 392399653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 3924fa9e4066Sahrens 3925fa9e4066Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 3926fa9e4066Sahrens 392799653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 3928ea8dc4b6Seschrock KM_SLEEP) == 0); 3929fa9e4066Sahrens 393099653d4eSeschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 3931fa9e4066Sahrens 3932fa9e4066Sahrens kmem_free(packed, nvsize); 3933fa9e4066Sahrens 393499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3935fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 3936fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 3937ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 3938fa9e4066Sahrens } 3939fa9e4066Sahrens 394099653d4eSeschrock static void 3941fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 3942fa94a07fSbrendan const char *config, const char *entry) 394399653d4eSeschrock { 394499653d4eSeschrock nvlist_t *nvroot; 3945fa94a07fSbrendan nvlist_t **list; 394699653d4eSeschrock int i; 394799653d4eSeschrock 3948fa94a07fSbrendan if (!sav->sav_sync) 394999653d4eSeschrock return; 395099653d4eSeschrock 395199653d4eSeschrock /* 3952fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 3953fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 39543d7072f8Seschrock * valid and the vdevs are labeled appropriately. 395599653d4eSeschrock */ 3956fa94a07fSbrendan if (sav->sav_object == 0) { 3957fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 3958fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 3959fa94a07fSbrendan sizeof (uint64_t), tx); 396099653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 3961fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 3962fa94a07fSbrendan &sav->sav_object, tx) == 0); 396399653d4eSeschrock } 396499653d4eSeschrock 396599653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3966fa94a07fSbrendan if (sav->sav_count == 0) { 3967fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 396899653d4eSeschrock } else { 3969fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 3970fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3971fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 3972fa94a07fSbrendan B_FALSE, B_FALSE, B_TRUE); 3973fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 3974fa94a07fSbrendan sav->sav_count) == 0); 3975fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3976fa94a07fSbrendan nvlist_free(list[i]); 3977fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 397899653d4eSeschrock } 397999653d4eSeschrock 3980fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 398106eeb2adSek nvlist_free(nvroot); 398299653d4eSeschrock 3983fa94a07fSbrendan sav->sav_sync = B_FALSE; 398499653d4eSeschrock } 398599653d4eSeschrock 398699653d4eSeschrock static void 398799653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 398899653d4eSeschrock { 398999653d4eSeschrock nvlist_t *config; 399099653d4eSeschrock 399199653d4eSeschrock if (list_is_empty(&spa->spa_dirty_list)) 399299653d4eSeschrock return; 399399653d4eSeschrock 399499653d4eSeschrock config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 399599653d4eSeschrock 399699653d4eSeschrock if (spa->spa_config_syncing) 399799653d4eSeschrock nvlist_free(spa->spa_config_syncing); 399899653d4eSeschrock spa->spa_config_syncing = config; 399999653d4eSeschrock 400099653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 400199653d4eSeschrock } 400299653d4eSeschrock 4003990b4856Slling /* 4004990b4856Slling * Set zpool properties. 4005990b4856Slling */ 4006b1b8ab34Slling static void 4007ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 4008b1b8ab34Slling { 4009b1b8ab34Slling spa_t *spa = arg1; 4010b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 4011990b4856Slling nvlist_t *nvp = arg2; 4012990b4856Slling nvpair_t *elem; 40133d7072f8Seschrock uint64_t intval; 40142f8aaab3Seschrock char *strval, *slash; 4015990b4856Slling zpool_prop_t prop; 4016990b4856Slling const char *propname; 4017990b4856Slling zprop_type_t proptype; 4018b1b8ab34Slling 4019990b4856Slling elem = NULL; 4020990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 4021990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 4022990b4856Slling case ZPOOL_PROP_VERSION: 4023990b4856Slling /* 4024990b4856Slling * Only set version for non-zpool-creation cases 4025990b4856Slling * (set/import). spa_create() needs special care 4026990b4856Slling * for version setting. 4027990b4856Slling */ 4028990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 4029990b4856Slling VERIFY(nvpair_value_uint64(elem, 4030990b4856Slling &intval) == 0); 4031990b4856Slling ASSERT(intval <= SPA_VERSION); 4032990b4856Slling ASSERT(intval >= spa_version(spa)); 4033990b4856Slling spa->spa_uberblock.ub_version = intval; 4034990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 4035990b4856Slling } 4036ecd6cf80Smarks break; 4037990b4856Slling 4038990b4856Slling case ZPOOL_PROP_ALTROOT: 4039990b4856Slling /* 4040990b4856Slling * 'altroot' is a non-persistent property. It should 4041990b4856Slling * have been set temporarily at creation or import time. 4042990b4856Slling */ 4043990b4856Slling ASSERT(spa->spa_root != NULL); 4044b1b8ab34Slling break; 40453d7072f8Seschrock 40462f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4047990b4856Slling /* 40482f8aaab3Seschrock * 'cachefile' is a non-persistent property, but note 40492f8aaab3Seschrock * an async request that the config cache needs to be 40502f8aaab3Seschrock * udpated. 4051990b4856Slling */ 40522f8aaab3Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 40532f8aaab3Seschrock if (spa->spa_config_dir) 40542f8aaab3Seschrock spa_strfree(spa->spa_config_dir); 40552f8aaab3Seschrock if (spa->spa_config_file) 40562f8aaab3Seschrock spa_strfree(spa->spa_config_file); 40572f8aaab3Seschrock 40582f8aaab3Seschrock if (strval[0] == '\0') { 40592f8aaab3Seschrock spa->spa_config_dir = NULL; 40602f8aaab3Seschrock spa->spa_config_file = NULL; 40612f8aaab3Seschrock } else if (strcmp(strval, "none") == 0) { 40622f8aaab3Seschrock spa->spa_config_dir = spa_strdup(strval); 40632f8aaab3Seschrock spa->spa_config_file = NULL; 40642f8aaab3Seschrock } else { 40652c32020fSeschrock /* 40662c32020fSeschrock * If the cachefile is in the root directory, 40672c32020fSeschrock * we will end up with an empty string for 40682c32020fSeschrock * spa_config_dir. This value is only ever 40692c32020fSeschrock * used when concatenated with '/', so an empty 40702c32020fSeschrock * string still behaves correctly and keeps the 40712c32020fSeschrock * rest of the code simple. 40722c32020fSeschrock */ 40732f8aaab3Seschrock slash = strrchr(strval, '/'); 40742f8aaab3Seschrock ASSERT(slash != NULL); 40752f8aaab3Seschrock *slash = '\0'; 40762c32020fSeschrock if (strcmp(strval, spa_config_dir) == 0 && 40772c32020fSeschrock strcmp(slash + 1, ZPOOL_CACHE_FILE) == 0) { 40782c32020fSeschrock spa->spa_config_dir = NULL; 40792c32020fSeschrock spa->spa_config_file = NULL; 40802c32020fSeschrock } else { 40812c32020fSeschrock spa->spa_config_dir = 40822c32020fSeschrock spa_strdup(strval); 40832c32020fSeschrock spa->spa_config_file = 40842c32020fSeschrock spa_strdup(slash + 1); 40852c32020fSeschrock } 40862f8aaab3Seschrock } 40872f8aaab3Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 40883d7072f8Seschrock break; 4089990b4856Slling default: 4090990b4856Slling /* 4091990b4856Slling * Set pool property values in the poolprops mos object. 4092990b4856Slling */ 4093990b4856Slling mutex_enter(&spa->spa_props_lock); 4094990b4856Slling if (spa->spa_pool_props_object == 0) { 4095990b4856Slling objset_t *mos = spa->spa_meta_objset; 4096990b4856Slling 4097990b4856Slling VERIFY((spa->spa_pool_props_object = 4098990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 4099990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 4100990b4856Slling 4101990b4856Slling VERIFY(zap_update(mos, 4102990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 4103990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 4104990b4856Slling == 0); 4105990b4856Slling } 4106990b4856Slling mutex_exit(&spa->spa_props_lock); 4107990b4856Slling 4108990b4856Slling /* normalize the property name */ 4109990b4856Slling propname = zpool_prop_to_name(prop); 4110990b4856Slling proptype = zpool_prop_get_type(prop); 4111990b4856Slling 4112990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 4113990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 4114990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 4115990b4856Slling VERIFY(zap_update(mos, 4116990b4856Slling spa->spa_pool_props_object, propname, 4117990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 4118990b4856Slling 4119990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 4120990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 4121990b4856Slling 4122990b4856Slling if (proptype == PROP_TYPE_INDEX) { 4123990b4856Slling const char *unused; 4124990b4856Slling VERIFY(zpool_prop_index_to_string( 4125990b4856Slling prop, intval, &unused) == 0); 4126990b4856Slling } 4127990b4856Slling VERIFY(zap_update(mos, 4128990b4856Slling spa->spa_pool_props_object, propname, 4129990b4856Slling 8, 1, &intval, tx) == 0); 4130990b4856Slling } else { 4131990b4856Slling ASSERT(0); /* not allowed */ 4132990b4856Slling } 4133990b4856Slling 41340a4e9518Sgw switch (prop) { 41350a4e9518Sgw case ZPOOL_PROP_DELEGATION: 4136990b4856Slling spa->spa_delegation = intval; 41370a4e9518Sgw break; 41380a4e9518Sgw case ZPOOL_PROP_BOOTFS: 4139990b4856Slling spa->spa_bootfs = intval; 41400a4e9518Sgw break; 41410a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 41420a4e9518Sgw spa->spa_failmode = intval; 41430a4e9518Sgw break; 41440a4e9518Sgw default: 41450a4e9518Sgw break; 41460a4e9518Sgw } 4147990b4856Slling } 4148990b4856Slling 4149990b4856Slling /* log internal history if this is not a zpool create */ 4150990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 4151990b4856Slling tx->tx_txg != TXG_INITIAL) { 4152990b4856Slling spa_history_internal_log(LOG_POOL_PROPSET, 4153990b4856Slling spa, tx, cr, "%s %lld %s", 4154990b4856Slling nvpair_name(elem), intval, spa->spa_name); 4155b1b8ab34Slling } 4156b1b8ab34Slling } 4157b1b8ab34Slling } 4158b1b8ab34Slling 4159fa9e4066Sahrens /* 4160fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4161fa9e4066Sahrens * part of the process, so we iterate until it converges. 4162fa9e4066Sahrens */ 4163fa9e4066Sahrens void 4164fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 4165fa9e4066Sahrens { 4166fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4167fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 4168fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41690373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 4170fa9e4066Sahrens vdev_t *vd; 417117f17c2dSbonwick vdev_t *svd[SPA_DVAS_PER_BP]; 417217f17c2dSbonwick int svdcount = 0; 4173fa9e4066Sahrens dmu_tx_t *tx; 4174fa9e4066Sahrens int dirty_vdevs; 4175fa9e4066Sahrens 4176fa9e4066Sahrens /* 4177fa9e4066Sahrens * Lock out configuration changes. 4178fa9e4066Sahrens */ 4179ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 4180fa9e4066Sahrens 4181fa9e4066Sahrens spa->spa_syncing_txg = txg; 4182fa9e4066Sahrens spa->spa_sync_pass = 0; 4183fa9e4066Sahrens 4184ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4185fa9e4066Sahrens 418699653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 418799653d4eSeschrock 418899653d4eSeschrock /* 4189e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 419099653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 419199653d4eSeschrock */ 4192e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 4193e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 419499653d4eSeschrock int i; 419599653d4eSeschrock 419699653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 419799653d4eSeschrock vd = rvd->vdev_child[i]; 419899653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 419999653d4eSeschrock break; 420099653d4eSeschrock } 420199653d4eSeschrock if (i == rvd->vdev_children) { 420299653d4eSeschrock spa->spa_deflate = TRUE; 420399653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 420499653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 420599653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 420699653d4eSeschrock } 420799653d4eSeschrock } 420899653d4eSeschrock 4209fa9e4066Sahrens /* 4210fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 4211fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 4212fa9e4066Sahrens * don't generate work on an otherwise idle system. 4213fa9e4066Sahrens */ 4214fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 42151615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 42161615a317Sek !txg_list_empty(&dp->dp_sync_tasks, txg)) 4217fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 4218fa9e4066Sahrens 4219fa9e4066Sahrens /* 4220fa9e4066Sahrens * Iterate to convergence. 4221fa9e4066Sahrens */ 4222fa9e4066Sahrens do { 4223fa9e4066Sahrens spa->spa_sync_pass++; 4224fa9e4066Sahrens 4225fa9e4066Sahrens spa_sync_config_object(spa, tx); 4226fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 4227fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 4228fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 4229fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 4230ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 4231fa9e4066Sahrens dsl_pool_sync(dp, txg); 4232fa9e4066Sahrens 4233fa9e4066Sahrens dirty_vdevs = 0; 4234fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4235fa9e4066Sahrens vdev_sync(vd, txg); 4236fa9e4066Sahrens dirty_vdevs++; 4237fa9e4066Sahrens } 4238fa9e4066Sahrens 4239fa9e4066Sahrens bplist_sync(bpl, tx); 4240fa9e4066Sahrens } while (dirty_vdevs); 4241fa9e4066Sahrens 4242fa9e4066Sahrens bplist_close(bpl); 4243fa9e4066Sahrens 4244fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4245fa9e4066Sahrens 4246fa9e4066Sahrens /* 4247fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4248fa9e4066Sahrens * to commit the transaction group. 42490373e76bSbonwick * 425017f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 425117f17c2dSbonwick * random top-level vdevs that are known to be visible in the 425217f17c2dSbonwick * config cache (see spa_vdev_add() for details). If there *are* 425317f17c2dSbonwick * dirty vdevs -- or if the sync to our random subset fails -- 425417f17c2dSbonwick * then sync the uberblock to all vdevs. 42550373e76bSbonwick */ 425617f17c2dSbonwick if (list_is_empty(&spa->spa_dirty_list)) { 42570373e76bSbonwick int children = rvd->vdev_children; 42580373e76bSbonwick int c0 = spa_get_random(children); 42590373e76bSbonwick int c; 42600373e76bSbonwick 42610373e76bSbonwick for (c = 0; c < children; c++) { 42620373e76bSbonwick vd = rvd->vdev_child[(c0 + c) % children]; 426317f17c2dSbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 42640373e76bSbonwick continue; 426517f17c2dSbonwick svd[svdcount++] = vd; 426617f17c2dSbonwick if (svdcount == SPA_DVAS_PER_BP) 42670373e76bSbonwick break; 42680373e76bSbonwick } 42690373e76bSbonwick } 427017f17c2dSbonwick if (svdcount == 0 || vdev_config_sync(svd, svdcount, txg) != 0) 427117f17c2dSbonwick VERIFY3U(vdev_config_sync(rvd->vdev_child, 427217f17c2dSbonwick rvd->vdev_children, txg), ==, 0); 42730373e76bSbonwick 427499653d4eSeschrock dmu_tx_commit(tx); 427599653d4eSeschrock 42760373e76bSbonwick /* 42770373e76bSbonwick * Clear the dirty config list. 4278fa9e4066Sahrens */ 42790373e76bSbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 42800373e76bSbonwick vdev_config_clean(vd); 42810373e76bSbonwick 42820373e76bSbonwick /* 42830373e76bSbonwick * Now that the new config has synced transactionally, 42840373e76bSbonwick * let it become visible to the config cache. 42850373e76bSbonwick */ 42860373e76bSbonwick if (spa->spa_config_syncing != NULL) { 42870373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 42880373e76bSbonwick spa->spa_config_txg = txg; 42890373e76bSbonwick spa->spa_config_syncing = NULL; 42900373e76bSbonwick } 4291fa9e4066Sahrens 4292fa9e4066Sahrens /* 4293fa9e4066Sahrens * Make a stable copy of the fully synced uberblock. 4294fa9e4066Sahrens * We use this as the root for pool traversals. 4295fa9e4066Sahrens */ 4296fa9e4066Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 4297fa9e4066Sahrens 4298fa9e4066Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 4299fa9e4066Sahrens 4300fa9e4066Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 4301fa9e4066Sahrens spa->spa_traverse_wanted = 0; 4302fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 4303fa9e4066Sahrens rw_exit(&spa->spa_traverse_lock); 4304fa9e4066Sahrens 4305fa9e4066Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 4306fa9e4066Sahrens 4307fa9e4066Sahrens /* 4308fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 4309fa9e4066Sahrens */ 4310fa9e4066Sahrens dsl_pool_zil_clean(dp); 4311fa9e4066Sahrens 4312fa9e4066Sahrens /* 4313fa9e4066Sahrens * Update usable space statistics. 4314fa9e4066Sahrens */ 4315fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4316fa9e4066Sahrens vdev_sync_done(vd, txg); 4317fa9e4066Sahrens 4318fa9e4066Sahrens /* 4319fa9e4066Sahrens * It had better be the case that we didn't dirty anything 432099653d4eSeschrock * since vdev_config_sync(). 4321fa9e4066Sahrens */ 4322fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4323fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4324fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4325fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 4326fa9e4066Sahrens 4327ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 4328ea8dc4b6Seschrock 4329ea8dc4b6Seschrock /* 4330ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 4331ea8dc4b6Seschrock */ 4332ea8dc4b6Seschrock spa_async_dispatch(spa); 4333fa9e4066Sahrens } 4334fa9e4066Sahrens 4335fa9e4066Sahrens /* 4336fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4337fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4338fa9e4066Sahrens * sync. 4339fa9e4066Sahrens */ 4340fa9e4066Sahrens void 4341fa9e4066Sahrens spa_sync_allpools(void) 4342fa9e4066Sahrens { 4343fa9e4066Sahrens spa_t *spa = NULL; 4344fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4345fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 4346fa9e4066Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4347fa9e4066Sahrens continue; 4348fa9e4066Sahrens spa_open_ref(spa, FTAG); 4349fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4350fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4351fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4352fa9e4066Sahrens spa_close(spa, FTAG); 4353fa9e4066Sahrens } 4354fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4355fa9e4066Sahrens } 4356fa9e4066Sahrens 4357fa9e4066Sahrens /* 4358fa9e4066Sahrens * ========================================================================== 4359fa9e4066Sahrens * Miscellaneous routines 4360fa9e4066Sahrens * ========================================================================== 4361fa9e4066Sahrens */ 4362fa9e4066Sahrens 4363fa9e4066Sahrens /* 4364fa9e4066Sahrens * Remove all pools in the system. 4365fa9e4066Sahrens */ 4366fa9e4066Sahrens void 4367fa9e4066Sahrens spa_evict_all(void) 4368fa9e4066Sahrens { 4369fa9e4066Sahrens spa_t *spa; 4370fa9e4066Sahrens 4371fa9e4066Sahrens /* 4372fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 4373fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 4374fa9e4066Sahrens */ 4375fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4376fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 4377fa9e4066Sahrens /* 4378ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 4379ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 4380ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 4381fa9e4066Sahrens */ 4382fa9e4066Sahrens spa_open_ref(spa, FTAG); 4383fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4384ea8dc4b6Seschrock spa_async_suspend(spa); 4385fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4386bb8b5132Sek VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 4387fa9e4066Sahrens spa_close(spa, FTAG); 4388fa9e4066Sahrens 4389fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4390fa9e4066Sahrens spa_unload(spa); 4391fa9e4066Sahrens spa_deactivate(spa); 4392fa9e4066Sahrens } 4393fa9e4066Sahrens spa_remove(spa); 4394fa9e4066Sahrens } 4395fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4396fa9e4066Sahrens } 4397ea8dc4b6Seschrock 4398ea8dc4b6Seschrock vdev_t * 4399ea8dc4b6Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid) 4400ea8dc4b6Seschrock { 4401ea8dc4b6Seschrock return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 4402ea8dc4b6Seschrock } 4403eaca9bbdSeschrock 4404eaca9bbdSeschrock void 4405990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 4406eaca9bbdSeschrock { 4407eaca9bbdSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 4408eaca9bbdSeschrock 4409eaca9bbdSeschrock /* 4410eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 4411eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 4412eaca9bbdSeschrock * possible. 4413eaca9bbdSeschrock */ 4414e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 4415990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 4416eaca9bbdSeschrock 4417990b4856Slling spa->spa_uberblock.ub_version = version; 4418eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 4419eaca9bbdSeschrock 4420eaca9bbdSeschrock spa_config_exit(spa, FTAG); 442199653d4eSeschrock 442299653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 442399653d4eSeschrock } 442499653d4eSeschrock 442599653d4eSeschrock boolean_t 442699653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 442799653d4eSeschrock { 442899653d4eSeschrock int i; 442939c23413Seschrock uint64_t spareguid; 4430fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 443199653d4eSeschrock 4432fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 4433fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 443499653d4eSeschrock return (B_TRUE); 443599653d4eSeschrock 4436fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 4437fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 4438fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 443939c23413Seschrock return (B_TRUE); 444039c23413Seschrock } 444139c23413Seschrock 444299653d4eSeschrock return (B_FALSE); 4443eaca9bbdSeschrock } 4444b1b8ab34Slling 44453d7072f8Seschrock /* 44463d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44473d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44483d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44493d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44503d7072f8Seschrock * or zdb as real changes. 44513d7072f8Seschrock */ 44523d7072f8Seschrock void 44533d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 44543d7072f8Seschrock { 44553d7072f8Seschrock #ifdef _KERNEL 44563d7072f8Seschrock sysevent_t *ev; 44573d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 44583d7072f8Seschrock sysevent_value_t value; 44593d7072f8Seschrock sysevent_id_t eid; 44603d7072f8Seschrock 44613d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 44623d7072f8Seschrock SE_SLEEP); 44633d7072f8Seschrock 44643d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 44653d7072f8Seschrock value.value.sv_string = spa_name(spa); 44663d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 44673d7072f8Seschrock goto done; 44683d7072f8Seschrock 44693d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44703d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 44713d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 44723d7072f8Seschrock goto done; 44733d7072f8Seschrock 44743d7072f8Seschrock if (vd) { 44753d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44763d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 44773d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 44783d7072f8Seschrock SE_SLEEP) != 0) 44793d7072f8Seschrock goto done; 44803d7072f8Seschrock 44813d7072f8Seschrock if (vd->vdev_path) { 44823d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 44833d7072f8Seschrock value.value.sv_string = vd->vdev_path; 44843d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 44853d7072f8Seschrock &value, SE_SLEEP) != 0) 44863d7072f8Seschrock goto done; 44873d7072f8Seschrock } 44883d7072f8Seschrock } 44893d7072f8Seschrock 4490b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 4491b01c3b58Seschrock goto done; 4492b01c3b58Seschrock attr = NULL; 4493b01c3b58Seschrock 44943d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 44953d7072f8Seschrock 44963d7072f8Seschrock done: 44973d7072f8Seschrock if (attr) 44983d7072f8Seschrock sysevent_free_attr(attr); 44993d7072f8Seschrock sysevent_free(ev); 45003d7072f8Seschrock #endif 45013d7072f8Seschrock } 4502