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> 63e7cbe64fSgw #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; 110*c5904d13Seschrock spa_config_dirent_t *dp; 111990b4856Slling 112990b4856Slling /* 113990b4856Slling * readonly properties 114990b4856Slling */ 1159d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src); 1169d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1179d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1189d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src); 119990b4856Slling 120990b4856Slling cap = (size == 0) ? 0 : (used * 100 / size); 1219d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 122990b4856Slling 1239d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1249d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1259d82f4f6Slling spa->spa_root_vdev->vdev_state, src); 126990b4856Slling 127990b4856Slling /* 128990b4856Slling * settable properties that are not stored in the pool property object. 129990b4856Slling */ 130990b4856Slling version = spa_version(spa); 131990b4856Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 132990b4856Slling src = ZPROP_SRC_DEFAULT; 133990b4856Slling else 134990b4856Slling src = ZPROP_SRC_LOCAL; 1359d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 136990b4856Slling 1379d82f4f6Slling if (spa->spa_root != NULL) 1389d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1399d82f4f6Slling 0, ZPROP_SRC_LOCAL); 140990b4856Slling 141*c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 142*c5904d13Seschrock if (dp->scd_path == NULL) { 1439d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 144*c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 145*c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1469d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 147*c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1482f8aaab3Seschrock } 1492f8aaab3Seschrock } 150990b4856Slling } 151990b4856Slling 152990b4856Slling /* 153990b4856Slling * Get zpool property values. 154990b4856Slling */ 155990b4856Slling int 156990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 157990b4856Slling { 158990b4856Slling zap_cursor_t zc; 159990b4856Slling zap_attribute_t za; 160990b4856Slling objset_t *mos = spa->spa_meta_objset; 161990b4856Slling int err; 162990b4856Slling 1639d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 164990b4856Slling 165990b4856Slling /* 166990b4856Slling * Get properties from the spa config. 167990b4856Slling */ 1689d82f4f6Slling spa_prop_get_config(spa, nvp); 169990b4856Slling 170990b4856Slling mutex_enter(&spa->spa_props_lock); 171990b4856Slling /* If no pool property object, no more prop to get. */ 172990b4856Slling if (spa->spa_pool_props_object == 0) { 173990b4856Slling mutex_exit(&spa->spa_props_lock); 174990b4856Slling return (0); 175990b4856Slling } 176990b4856Slling 177990b4856Slling /* 178990b4856Slling * Get properties from the MOS pool property object. 179990b4856Slling */ 180990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 181990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 182990b4856Slling zap_cursor_advance(&zc)) { 183990b4856Slling uint64_t intval = 0; 184990b4856Slling char *strval = NULL; 185990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 186990b4856Slling zpool_prop_t prop; 187990b4856Slling 188990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 189990b4856Slling continue; 190990b4856Slling 191990b4856Slling switch (za.za_integer_length) { 192990b4856Slling case 8: 193990b4856Slling /* integer property */ 194990b4856Slling if (za.za_first_integer != 195990b4856Slling zpool_prop_default_numeric(prop)) 196990b4856Slling src = ZPROP_SRC_LOCAL; 197990b4856Slling 198990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 199990b4856Slling dsl_pool_t *dp; 200990b4856Slling dsl_dataset_t *ds = NULL; 201990b4856Slling 202990b4856Slling dp = spa_get_dsl(spa); 203990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 204990b4856Slling if (err = dsl_dataset_open_obj(dp, 205990b4856Slling za.za_first_integer, NULL, DS_MODE_NONE, 206990b4856Slling FTAG, &ds)) { 207990b4856Slling rw_exit(&dp->dp_config_rwlock); 208990b4856Slling break; 209990b4856Slling } 210990b4856Slling 211990b4856Slling strval = kmem_alloc( 212990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 213990b4856Slling KM_SLEEP); 214990b4856Slling dsl_dataset_name(ds, strval); 215990b4856Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 216990b4856Slling rw_exit(&dp->dp_config_rwlock); 217990b4856Slling } else { 218990b4856Slling strval = NULL; 219990b4856Slling intval = za.za_first_integer; 220990b4856Slling } 221990b4856Slling 2229d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 223990b4856Slling 224990b4856Slling if (strval != NULL) 225990b4856Slling kmem_free(strval, 226990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 227990b4856Slling 228990b4856Slling break; 229990b4856Slling 230990b4856Slling case 1: 231990b4856Slling /* string property */ 232990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 233990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 234990b4856Slling za.za_name, 1, za.za_num_integers, strval); 235990b4856Slling if (err) { 236990b4856Slling kmem_free(strval, za.za_num_integers); 237990b4856Slling break; 238990b4856Slling } 2399d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 240990b4856Slling kmem_free(strval, za.za_num_integers); 241990b4856Slling break; 242990b4856Slling 243990b4856Slling default: 244990b4856Slling break; 245990b4856Slling } 246990b4856Slling } 247990b4856Slling zap_cursor_fini(&zc); 248990b4856Slling mutex_exit(&spa->spa_props_lock); 249990b4856Slling out: 250990b4856Slling if (err && err != ENOENT) { 251990b4856Slling nvlist_free(*nvp); 2529d82f4f6Slling *nvp = NULL; 253990b4856Slling return (err); 254990b4856Slling } 255990b4856Slling 256990b4856Slling return (0); 257990b4856Slling } 258990b4856Slling 259990b4856Slling /* 260990b4856Slling * Validate the given pool properties nvlist and modify the list 261990b4856Slling * for the property values to be set. 262990b4856Slling */ 263990b4856Slling static int 264990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 265990b4856Slling { 266990b4856Slling nvpair_t *elem; 267990b4856Slling int error = 0, reset_bootfs = 0; 268990b4856Slling uint64_t objnum; 269990b4856Slling 270990b4856Slling elem = NULL; 271990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 272990b4856Slling zpool_prop_t prop; 273990b4856Slling char *propname, *strval; 274990b4856Slling uint64_t intval; 275990b4856Slling vdev_t *rvdev; 276990b4856Slling char *vdev_type; 277990b4856Slling objset_t *os; 2782f8aaab3Seschrock char *slash; 279990b4856Slling 280990b4856Slling propname = nvpair_name(elem); 281990b4856Slling 282990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 283990b4856Slling return (EINVAL); 284990b4856Slling 285990b4856Slling switch (prop) { 286990b4856Slling case ZPOOL_PROP_VERSION: 287990b4856Slling error = nvpair_value_uint64(elem, &intval); 288990b4856Slling if (!error && 289990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 290990b4856Slling error = EINVAL; 291990b4856Slling break; 292990b4856Slling 293990b4856Slling case ZPOOL_PROP_DELEGATION: 294990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 295990b4856Slling error = nvpair_value_uint64(elem, &intval); 296990b4856Slling if (!error && intval > 1) 297990b4856Slling error = EINVAL; 298990b4856Slling break; 299990b4856Slling 300990b4856Slling case ZPOOL_PROP_BOOTFS: 301990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 302990b4856Slling error = ENOTSUP; 303990b4856Slling break; 304990b4856Slling } 305990b4856Slling 306990b4856Slling /* 307990b4856Slling * A bootable filesystem can not be on a RAIDZ pool 308990b4856Slling * nor a striped pool with more than 1 device. 309990b4856Slling */ 310990b4856Slling rvdev = spa->spa_root_vdev; 311990b4856Slling vdev_type = 312990b4856Slling rvdev->vdev_child[0]->vdev_ops->vdev_op_type; 313990b4856Slling if (rvdev->vdev_children > 1 || 314990b4856Slling strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 315990b4856Slling strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 316990b4856Slling error = ENOTSUP; 317990b4856Slling break; 318990b4856Slling } 319990b4856Slling 320990b4856Slling reset_bootfs = 1; 321990b4856Slling 322990b4856Slling error = nvpair_value_string(elem, &strval); 323990b4856Slling 324990b4856Slling if (!error) { 325990b4856Slling if (strval == NULL || strval[0] == '\0') { 326990b4856Slling objnum = zpool_prop_default_numeric( 327990b4856Slling ZPOOL_PROP_BOOTFS); 328990b4856Slling break; 329990b4856Slling } 330990b4856Slling 331990b4856Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 332990b4856Slling DS_MODE_STANDARD | DS_MODE_READONLY, &os)) 333990b4856Slling break; 334990b4856Slling objnum = dmu_objset_id(os); 335990b4856Slling dmu_objset_close(os); 336990b4856Slling } 337990b4856Slling break; 3380a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 3390a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 3400a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3410a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 3420a4e9518Sgw error = EINVAL; 3430a4e9518Sgw 3440a4e9518Sgw /* 3450a4e9518Sgw * This is a special case which only occurs when 3460a4e9518Sgw * the pool has completely failed. This allows 3470a4e9518Sgw * the user to change the in-core failmode property 3480a4e9518Sgw * without syncing it out to disk (I/Os might 3490a4e9518Sgw * currently be blocked). We do this by returning 3500a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 3510a4e9518Sgw * into thinking we encountered a property validation 3520a4e9518Sgw * error. 3530a4e9518Sgw */ 3540a4e9518Sgw if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3550a4e9518Sgw spa->spa_failmode = intval; 3560a4e9518Sgw error = EIO; 3570a4e9518Sgw } 3580a4e9518Sgw break; 3592f8aaab3Seschrock 3602f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 3612f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3622f8aaab3Seschrock break; 3632f8aaab3Seschrock 3642f8aaab3Seschrock if (strval[0] == '\0') 3652f8aaab3Seschrock break; 3662f8aaab3Seschrock 3672f8aaab3Seschrock if (strcmp(strval, "none") == 0) 3682f8aaab3Seschrock break; 3692f8aaab3Seschrock 3702f8aaab3Seschrock if (strval[0] != '/') { 3712f8aaab3Seschrock error = EINVAL; 3722f8aaab3Seschrock break; 3732f8aaab3Seschrock } 3742f8aaab3Seschrock 3752f8aaab3Seschrock slash = strrchr(strval, '/'); 3762f8aaab3Seschrock ASSERT(slash != NULL); 3772f8aaab3Seschrock 3782f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3792f8aaab3Seschrock strcmp(slash, "/..") == 0) 3802f8aaab3Seschrock error = EINVAL; 3812f8aaab3Seschrock break; 382990b4856Slling } 383990b4856Slling 384990b4856Slling if (error) 385990b4856Slling break; 386990b4856Slling } 387990b4856Slling 388990b4856Slling if (!error && reset_bootfs) { 389990b4856Slling error = nvlist_remove(props, 390990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 391990b4856Slling 392990b4856Slling if (!error) { 393990b4856Slling error = nvlist_add_uint64(props, 394990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 395990b4856Slling } 396990b4856Slling } 397990b4856Slling 398990b4856Slling return (error); 399990b4856Slling } 400990b4856Slling 401990b4856Slling int 402990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 403990b4856Slling { 404990b4856Slling int error; 405990b4856Slling 406990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 407990b4856Slling return (error); 408990b4856Slling 409990b4856Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 410990b4856Slling spa, nvp, 3)); 411990b4856Slling } 412990b4856Slling 413990b4856Slling /* 414990b4856Slling * If the bootfs property value is dsobj, clear it. 415990b4856Slling */ 416990b4856Slling void 417990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 418990b4856Slling { 419990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 420990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 421990b4856Slling spa->spa_pool_props_object, 422990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 423990b4856Slling spa->spa_bootfs = 0; 424990b4856Slling } 425990b4856Slling } 426990b4856Slling 427fa9e4066Sahrens /* 428fa9e4066Sahrens * ========================================================================== 429fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 430fa9e4066Sahrens * ========================================================================== 431fa9e4066Sahrens */ 432fa9e4066Sahrens 433ea8dc4b6Seschrock static int 434ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 435ea8dc4b6Seschrock { 436ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 437ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 438ea8dc4b6Seschrock int ret; 439ea8dc4b6Seschrock 440ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 441ea8dc4b6Seschrock sizeof (zbookmark_t)); 442ea8dc4b6Seschrock 443ea8dc4b6Seschrock if (ret < 0) 444ea8dc4b6Seschrock return (-1); 445ea8dc4b6Seschrock else if (ret > 0) 446ea8dc4b6Seschrock return (1); 447ea8dc4b6Seschrock else 448ea8dc4b6Seschrock return (0); 449ea8dc4b6Seschrock } 450ea8dc4b6Seschrock 451ea8dc4b6Seschrock /* 452ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 453ea8dc4b6Seschrock * re-initializes them in the process. 454ea8dc4b6Seschrock */ 455ea8dc4b6Seschrock void 456ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 457ea8dc4b6Seschrock { 458ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 459ea8dc4b6Seschrock 460ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 461ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 462ea8dc4b6Seschrock 463ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 464ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 465ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 466ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 467ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 468ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 469ea8dc4b6Seschrock } 470ea8dc4b6Seschrock 471fa9e4066Sahrens /* 472fa9e4066Sahrens * Activate an uninitialized pool. 473fa9e4066Sahrens */ 474fa9e4066Sahrens static void 475fa9e4066Sahrens spa_activate(spa_t *spa) 476fa9e4066Sahrens { 477fa9e4066Sahrens int t; 478fa9e4066Sahrens 479fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 480fa9e4066Sahrens 481fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 482fa9e4066Sahrens 483fa9e4066Sahrens spa->spa_normal_class = metaslab_class_create(); 4848654d025Sperrin spa->spa_log_class = metaslab_class_create(); 485fa9e4066Sahrens 486fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 487fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 488416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 489fa9e4066Sahrens TASKQ_PREPOPULATE); 490fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 491416e0cd8Sek zio_taskq_threads, maxclsyspri, 50, INT_MAX, 492fa9e4066Sahrens TASKQ_PREPOPULATE); 493fa9e4066Sahrens } 494fa9e4066Sahrens 495fa9e4066Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 496fa9e4066Sahrens offsetof(vdev_t, vdev_dirty_node)); 4970a4e9518Sgw list_create(&spa->spa_zio_list, sizeof (zio_t), 4980a4e9518Sgw offsetof(zio_t, zio_link_node)); 499fa9e4066Sahrens 500fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 501fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 502ea8dc4b6Seschrock 503ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 504ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 505ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 506ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 507ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 508ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 509fa9e4066Sahrens } 510fa9e4066Sahrens 511fa9e4066Sahrens /* 512fa9e4066Sahrens * Opposite of spa_activate(). 513fa9e4066Sahrens */ 514fa9e4066Sahrens static void 515fa9e4066Sahrens spa_deactivate(spa_t *spa) 516fa9e4066Sahrens { 517fa9e4066Sahrens int t; 518fa9e4066Sahrens 519fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 520fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 521fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 522fa9e4066Sahrens 523fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 524fa9e4066Sahrens 525fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 526fa9e4066Sahrens 527fa9e4066Sahrens list_destroy(&spa->spa_dirty_list); 5280a4e9518Sgw list_destroy(&spa->spa_zio_list); 529fa9e4066Sahrens 530fa9e4066Sahrens for (t = 0; t < ZIO_TYPES; t++) { 531fa9e4066Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 532fa9e4066Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 533fa9e4066Sahrens spa->spa_zio_issue_taskq[t] = NULL; 534fa9e4066Sahrens spa->spa_zio_intr_taskq[t] = NULL; 535fa9e4066Sahrens } 536fa9e4066Sahrens 537fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 538fa9e4066Sahrens spa->spa_normal_class = NULL; 539fa9e4066Sahrens 5408654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 5418654d025Sperrin spa->spa_log_class = NULL; 5428654d025Sperrin 543ea8dc4b6Seschrock /* 544ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 545ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 546ea8dc4b6Seschrock */ 547ea8dc4b6Seschrock spa_errlog_drain(spa); 548ea8dc4b6Seschrock 549ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 550ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 551ea8dc4b6Seschrock 552fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 553fa9e4066Sahrens } 554fa9e4066Sahrens 555fa9e4066Sahrens /* 556fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 557fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 558fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 559fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 560fa9e4066Sahrens */ 56199653d4eSeschrock static int 56299653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 56399653d4eSeschrock uint_t id, int atype) 564fa9e4066Sahrens { 565fa9e4066Sahrens nvlist_t **child; 566fa9e4066Sahrens uint_t c, children; 56799653d4eSeschrock int error; 568fa9e4066Sahrens 56999653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 57099653d4eSeschrock return (error); 571fa9e4066Sahrens 57299653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 57399653d4eSeschrock return (0); 574fa9e4066Sahrens 575fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 576fa9e4066Sahrens &child, &children) != 0) { 57799653d4eSeschrock vdev_free(*vdp); 57899653d4eSeschrock *vdp = NULL; 57999653d4eSeschrock return (EINVAL); 580fa9e4066Sahrens } 581fa9e4066Sahrens 582fa9e4066Sahrens for (c = 0; c < children; c++) { 58399653d4eSeschrock vdev_t *vd; 58499653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 58599653d4eSeschrock atype)) != 0) { 58699653d4eSeschrock vdev_free(*vdp); 58799653d4eSeschrock *vdp = NULL; 58899653d4eSeschrock return (error); 589fa9e4066Sahrens } 590fa9e4066Sahrens } 591fa9e4066Sahrens 59299653d4eSeschrock ASSERT(*vdp != NULL); 59399653d4eSeschrock 59499653d4eSeschrock return (0); 595fa9e4066Sahrens } 596fa9e4066Sahrens 597fa9e4066Sahrens /* 598fa9e4066Sahrens * Opposite of spa_load(). 599fa9e4066Sahrens */ 600fa9e4066Sahrens static void 601fa9e4066Sahrens spa_unload(spa_t *spa) 602fa9e4066Sahrens { 60399653d4eSeschrock int i; 60499653d4eSeschrock 605ea8dc4b6Seschrock /* 606ea8dc4b6Seschrock * Stop async tasks. 607ea8dc4b6Seschrock */ 608ea8dc4b6Seschrock spa_async_suspend(spa); 609ea8dc4b6Seschrock 610fa9e4066Sahrens /* 611fa9e4066Sahrens * Stop syncing. 612fa9e4066Sahrens */ 613fa9e4066Sahrens if (spa->spa_sync_on) { 614fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 615fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 616fa9e4066Sahrens } 617fa9e4066Sahrens 618fa9e4066Sahrens /* 619fa9e4066Sahrens * Wait for any outstanding prefetch I/O to complete. 620fa9e4066Sahrens */ 621ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 622ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 623fa9e4066Sahrens 624fa94a07fSbrendan /* 625fa94a07fSbrendan * Drop and purge level 2 cache 626fa94a07fSbrendan */ 627fa94a07fSbrendan spa_l2cache_drop(spa); 628fa94a07fSbrendan 629fa9e4066Sahrens /* 630fa9e4066Sahrens * Close the dsl pool. 631fa9e4066Sahrens */ 632fa9e4066Sahrens if (spa->spa_dsl_pool) { 633fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 634fa9e4066Sahrens spa->spa_dsl_pool = NULL; 635fa9e4066Sahrens } 636fa9e4066Sahrens 637fa9e4066Sahrens /* 638fa9e4066Sahrens * Close all vdevs. 639fa9e4066Sahrens */ 6400e34b6a7Sbonwick if (spa->spa_root_vdev) 641fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 6420e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 643ea8dc4b6Seschrock 644fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 645fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 646fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 647fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 648fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 649fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 65099653d4eSeschrock } 651fa94a07fSbrendan if (spa->spa_spares.sav_config) { 652fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 653fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 654fa94a07fSbrendan } 655fa94a07fSbrendan 656fa94a07fSbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 657fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 658fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 659fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 660fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 661fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 662fa94a07fSbrendan } 663fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 664fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 665fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 66699653d4eSeschrock } 66799653d4eSeschrock 668ea8dc4b6Seschrock spa->spa_async_suspended = 0; 669fa9e4066Sahrens } 670fa9e4066Sahrens 67199653d4eSeschrock /* 67299653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 67399653d4eSeschrock * this pool. When this is called, we have some form of basic information in 674fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 675fa94a07fSbrendan * then re-generate a more complete list including status information. 67699653d4eSeschrock */ 67799653d4eSeschrock static void 67899653d4eSeschrock spa_load_spares(spa_t *spa) 67999653d4eSeschrock { 68099653d4eSeschrock nvlist_t **spares; 68199653d4eSeschrock uint_t nspares; 68299653d4eSeschrock int i; 68339c23413Seschrock vdev_t *vd, *tvd; 68499653d4eSeschrock 68599653d4eSeschrock /* 68699653d4eSeschrock * First, close and free any existing spare vdevs. 68799653d4eSeschrock */ 688fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 689fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 69039c23413Seschrock 69139c23413Seschrock /* Undo the call to spa_activate() below */ 692*c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 693*c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 69439c23413Seschrock spa_spare_remove(tvd); 69539c23413Seschrock vdev_close(vd); 69639c23413Seschrock vdev_free(vd); 69799653d4eSeschrock } 69839c23413Seschrock 699fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 700fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 701fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 70299653d4eSeschrock 703fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 70499653d4eSeschrock nspares = 0; 70599653d4eSeschrock else 706fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 70799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 70899653d4eSeschrock 709fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 710fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 71199653d4eSeschrock 71299653d4eSeschrock if (nspares == 0) 71399653d4eSeschrock return; 71499653d4eSeschrock 71599653d4eSeschrock /* 71699653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 71739c23413Seschrock * process. For each spare, there is potentially two different vdev_t 71839c23413Seschrock * structures associated with it: one in the list of spares (used only 71939c23413Seschrock * for basic validation purposes) and one in the active vdev 72039c23413Seschrock * configuration (if it's spared in). During this phase we open and 72139c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 72239c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 72399653d4eSeschrock */ 724fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 725fa94a07fSbrendan KM_SLEEP); 726fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 72799653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 72899653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 72999653d4eSeschrock ASSERT(vd != NULL); 73099653d4eSeschrock 731fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 73299653d4eSeschrock 733*c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 734*c5904d13Seschrock B_FALSE)) != NULL) { 73539c23413Seschrock if (!tvd->vdev_isspare) 73639c23413Seschrock spa_spare_add(tvd); 73739c23413Seschrock 73839c23413Seschrock /* 73939c23413Seschrock * We only mark the spare active if we were successfully 74039c23413Seschrock * able to load the vdev. Otherwise, importing a pool 74139c23413Seschrock * with a bad active spare would result in strange 74239c23413Seschrock * behavior, because multiple pool would think the spare 74339c23413Seschrock * is actively in use. 74439c23413Seschrock * 74539c23413Seschrock * There is a vulnerability here to an equally bizarre 74639c23413Seschrock * circumstance, where a dead active spare is later 74739c23413Seschrock * brought back to life (onlined or otherwise). Given 74839c23413Seschrock * the rarity of this scenario, and the extra complexity 74939c23413Seschrock * it adds, we ignore the possibility. 75039c23413Seschrock */ 75139c23413Seschrock if (!vdev_is_dead(tvd)) 75239c23413Seschrock spa_spare_activate(tvd); 75339c23413Seschrock } 75439c23413Seschrock 75599653d4eSeschrock if (vdev_open(vd) != 0) 75699653d4eSeschrock continue; 75799653d4eSeschrock 75899653d4eSeschrock vd->vdev_top = vd; 759fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 760fa94a07fSbrendan spa_spare_add(vd); 76199653d4eSeschrock } 76299653d4eSeschrock 76399653d4eSeschrock /* 76499653d4eSeschrock * Recompute the stashed list of spares, with status information 76599653d4eSeschrock * this time. 76699653d4eSeschrock */ 767fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 76899653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 76999653d4eSeschrock 770fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 771fa94a07fSbrendan KM_SLEEP); 772fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 773fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 774fa94a07fSbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 775fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 776fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 777fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 77899653d4eSeschrock nvlist_free(spares[i]); 779fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 780fa94a07fSbrendan } 781fa94a07fSbrendan 782fa94a07fSbrendan /* 783fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 784fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 785fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 786fa94a07fSbrendan * then re-generate a more complete list including status information. 787fa94a07fSbrendan * Devices which are already active have their details maintained, and are 788fa94a07fSbrendan * not re-opened. 789fa94a07fSbrendan */ 790fa94a07fSbrendan static void 791fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 792fa94a07fSbrendan { 793fa94a07fSbrendan nvlist_t **l2cache; 794fa94a07fSbrendan uint_t nl2cache; 795fa94a07fSbrendan int i, j, oldnvdevs; 796*c5904d13Seschrock uint64_t guid, size; 797fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 798fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 799fa94a07fSbrendan 800fa94a07fSbrendan if (sav->sav_config != NULL) { 801fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 802fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 803fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 804fa94a07fSbrendan } else { 805fa94a07fSbrendan nl2cache = 0; 806fa94a07fSbrendan } 807fa94a07fSbrendan 808fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 809fa94a07fSbrendan oldnvdevs = sav->sav_count; 810fa94a07fSbrendan sav->sav_vdevs = NULL; 811fa94a07fSbrendan sav->sav_count = 0; 812fa94a07fSbrendan 813fa94a07fSbrendan /* 814fa94a07fSbrendan * Process new nvlist of vdevs. 815fa94a07fSbrendan */ 816fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 817fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 818fa94a07fSbrendan &guid) == 0); 819fa94a07fSbrendan 820fa94a07fSbrendan newvdevs[i] = NULL; 821fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 822fa94a07fSbrendan vd = oldvdevs[j]; 823fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 824fa94a07fSbrendan /* 825fa94a07fSbrendan * Retain previous vdev for add/remove ops. 826fa94a07fSbrendan */ 827fa94a07fSbrendan newvdevs[i] = vd; 828fa94a07fSbrendan oldvdevs[j] = NULL; 829fa94a07fSbrendan break; 830fa94a07fSbrendan } 831fa94a07fSbrendan } 832fa94a07fSbrendan 833fa94a07fSbrendan if (newvdevs[i] == NULL) { 834fa94a07fSbrendan /* 835fa94a07fSbrendan * Create new vdev 836fa94a07fSbrendan */ 837fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 838fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 839fa94a07fSbrendan ASSERT(vd != NULL); 840fa94a07fSbrendan newvdevs[i] = vd; 841fa94a07fSbrendan 842fa94a07fSbrendan /* 843fa94a07fSbrendan * Commit this vdev as an l2cache device, 844fa94a07fSbrendan * even if it fails to open. 845fa94a07fSbrendan */ 846fa94a07fSbrendan spa_l2cache_add(vd); 847fa94a07fSbrendan 848*c5904d13Seschrock vd->vdev_top = vd; 849*c5904d13Seschrock vd->vdev_aux = sav; 850*c5904d13Seschrock 851*c5904d13Seschrock spa_l2cache_activate(vd); 852*c5904d13Seschrock 853fa94a07fSbrendan if (vdev_open(vd) != 0) 854fa94a07fSbrendan continue; 855fa94a07fSbrendan 856fa94a07fSbrendan (void) vdev_validate_aux(vd); 857fa94a07fSbrendan 858fa94a07fSbrendan if (!vdev_is_dead(vd)) { 859fa94a07fSbrendan size = vdev_get_rsize(vd); 860*c5904d13Seschrock l2arc_add_vdev(spa, vd, 861*c5904d13Seschrock VDEV_LABEL_START_SIZE, 862*c5904d13Seschrock size - VDEV_LABEL_START_SIZE); 863fa94a07fSbrendan } 864fa94a07fSbrendan } 865fa94a07fSbrendan } 866fa94a07fSbrendan 867fa94a07fSbrendan /* 868fa94a07fSbrendan * Purge vdevs that were dropped 869fa94a07fSbrendan */ 870fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 871fa94a07fSbrendan uint64_t pool; 872fa94a07fSbrendan 873fa94a07fSbrendan vd = oldvdevs[i]; 874fa94a07fSbrendan if (vd != NULL) { 875fa94a07fSbrendan if (spa_mode & FWRITE && 876fa94a07fSbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 877*c5904d13Seschrock pool != 0ULL && 878*c5904d13Seschrock l2arc_vdev_present(vd)) { 879fa94a07fSbrendan l2arc_remove_vdev(vd); 880fa94a07fSbrendan } 881fa94a07fSbrendan (void) vdev_close(vd); 882fa94a07fSbrendan spa_l2cache_remove(vd); 883fa94a07fSbrendan } 884fa94a07fSbrendan } 885fa94a07fSbrendan 886fa94a07fSbrendan if (oldvdevs) 887fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 888fa94a07fSbrendan 889fa94a07fSbrendan if (sav->sav_config == NULL) 890fa94a07fSbrendan goto out; 891fa94a07fSbrendan 892fa94a07fSbrendan sav->sav_vdevs = newvdevs; 893fa94a07fSbrendan sav->sav_count = (int)nl2cache; 894fa94a07fSbrendan 895fa94a07fSbrendan /* 896fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 897fa94a07fSbrendan * information this time. 898fa94a07fSbrendan */ 899fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 900fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 901fa94a07fSbrendan 902fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 903fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 904fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 905fa94a07fSbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 906fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 907fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 908fa94a07fSbrendan out: 909fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 910fa94a07fSbrendan nvlist_free(l2cache[i]); 911fa94a07fSbrendan if (sav->sav_count) 912fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 91399653d4eSeschrock } 91499653d4eSeschrock 91599653d4eSeschrock static int 91699653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 91799653d4eSeschrock { 91899653d4eSeschrock dmu_buf_t *db; 91999653d4eSeschrock char *packed = NULL; 92099653d4eSeschrock size_t nvsize = 0; 92199653d4eSeschrock int error; 92299653d4eSeschrock *value = NULL; 92399653d4eSeschrock 92499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 92599653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 92699653d4eSeschrock dmu_buf_rele(db, FTAG); 92799653d4eSeschrock 92899653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 92999653d4eSeschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 93099653d4eSeschrock if (error == 0) 93199653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 93299653d4eSeschrock kmem_free(packed, nvsize); 93399653d4eSeschrock 93499653d4eSeschrock return (error); 93599653d4eSeschrock } 93699653d4eSeschrock 9373d7072f8Seschrock /* 9383d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9393d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9403d7072f8Seschrock */ 9413d7072f8Seschrock static void 9423d7072f8Seschrock spa_check_removed(vdev_t *vd) 9433d7072f8Seschrock { 9443d7072f8Seschrock int c; 9453d7072f8Seschrock 9463d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) 9473d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 9483d7072f8Seschrock 9493d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9503d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9513d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9523d7072f8Seschrock } 9533d7072f8Seschrock } 9543d7072f8Seschrock 955fa9e4066Sahrens /* 956fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 957ea8dc4b6Seschrock * source of configuration information. 958fa9e4066Sahrens */ 959fa9e4066Sahrens static int 960ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 961fa9e4066Sahrens { 962fa9e4066Sahrens int error = 0; 963fa9e4066Sahrens nvlist_t *nvroot = NULL; 964fa9e4066Sahrens vdev_t *rvd; 965fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 9660373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 967fa9e4066Sahrens uint64_t pool_guid; 96899653d4eSeschrock uint64_t version; 969fa9e4066Sahrens zio_t *zio; 9703d7072f8Seschrock uint64_t autoreplace = 0; 971fa9e4066Sahrens 972ea8dc4b6Seschrock spa->spa_load_state = state; 9730373e76bSbonwick 974fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 975a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 976ea8dc4b6Seschrock error = EINVAL; 977ea8dc4b6Seschrock goto out; 978ea8dc4b6Seschrock } 979fa9e4066Sahrens 98099653d4eSeschrock /* 98199653d4eSeschrock * Versioning wasn't explicitly added to the label until later, so if 98299653d4eSeschrock * it's not present treat it as the initial version. 98399653d4eSeschrock */ 98499653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 985e7437265Sahrens version = SPA_VERSION_INITIAL; 98699653d4eSeschrock 987a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 988a9926bf0Sbonwick &spa->spa_config_txg); 989a9926bf0Sbonwick 9900373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 991ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 992ea8dc4b6Seschrock error = EEXIST; 993ea8dc4b6Seschrock goto out; 994ea8dc4b6Seschrock } 995fa9e4066Sahrens 996b5989ec7Seschrock spa->spa_load_guid = pool_guid; 997b5989ec7Seschrock 998fa9e4066Sahrens /* 99999653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 100099653d4eSeschrock * value that will be returned by spa_version() since parsing the 100199653d4eSeschrock * configuration requires knowing the version number. 1002fa9e4066Sahrens */ 1003ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 100499653d4eSeschrock spa->spa_ubsync.ub_version = version; 100599653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 1006ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1007fa9e4066Sahrens 100899653d4eSeschrock if (error != 0) 1009ea8dc4b6Seschrock goto out; 1010fa9e4066Sahrens 10110e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1012fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 1013fa9e4066Sahrens 1014fa9e4066Sahrens /* 1015fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 1016fa9e4066Sahrens */ 10170bf246f5Smc error = vdev_open(rvd); 10180bf246f5Smc if (error != 0) 1019ea8dc4b6Seschrock goto out; 1020fa9e4066Sahrens 1021560e6e96Seschrock /* 1022560e6e96Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 1023560e6e96Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 1024560e6e96Seschrock * flag. 1025560e6e96Seschrock */ 1026560e6e96Seschrock spa_config_enter(spa, RW_READER, FTAG); 1027560e6e96Seschrock error = vdev_validate(rvd); 1028560e6e96Seschrock spa_config_exit(spa, FTAG); 1029560e6e96Seschrock 10300bf246f5Smc if (error != 0) 1031560e6e96Seschrock goto out; 1032560e6e96Seschrock 1033560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1034560e6e96Seschrock error = ENXIO; 1035560e6e96Seschrock goto out; 1036560e6e96Seschrock } 1037560e6e96Seschrock 1038fa9e4066Sahrens /* 1039fa9e4066Sahrens * Find the best uberblock. 1040fa9e4066Sahrens */ 1041fa9e4066Sahrens bzero(ub, sizeof (uberblock_t)); 1042fa9e4066Sahrens 1043fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, 1044fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1045fa9e4066Sahrens vdev_uberblock_load(zio, rvd, ub); 1046fa9e4066Sahrens error = zio_wait(zio); 1047fa9e4066Sahrens 1048fa9e4066Sahrens /* 1049fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 1050fa9e4066Sahrens */ 1051fa9e4066Sahrens if (ub->ub_txg == 0) { 1052eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1053eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 1054ea8dc4b6Seschrock error = ENXIO; 1055ea8dc4b6Seschrock goto out; 1056ea8dc4b6Seschrock } 1057ea8dc4b6Seschrock 1058ea8dc4b6Seschrock /* 1059ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 1060ea8dc4b6Seschrock */ 1061e7437265Sahrens if (ub->ub_version > SPA_VERSION) { 1062eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1063eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 1064ea8dc4b6Seschrock error = ENOTSUP; 1065ea8dc4b6Seschrock goto out; 1066fa9e4066Sahrens } 1067fa9e4066Sahrens 1068fa9e4066Sahrens /* 1069fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1070fa9e4066Sahrens * incomplete configuration. 1071fa9e4066Sahrens */ 1072ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 1073ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1074ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 1075ea8dc4b6Seschrock error = ENXIO; 1076ea8dc4b6Seschrock goto out; 1077fa9e4066Sahrens } 1078fa9e4066Sahrens 1079fa9e4066Sahrens /* 1080fa9e4066Sahrens * Initialize internal SPA structures. 1081fa9e4066Sahrens */ 1082fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1083fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1084fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 1085ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 1086ea8dc4b6Seschrock if (error) { 1087ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1088ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1089ea8dc4b6Seschrock goto out; 1090ea8dc4b6Seschrock } 1091fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1092fa9e4066Sahrens 1093ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1094fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1095ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 1096ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1097ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1098ea8dc4b6Seschrock error = EIO; 1099ea8dc4b6Seschrock goto out; 1100ea8dc4b6Seschrock } 1101fa9e4066Sahrens 1102fa9e4066Sahrens if (!mosconfig) { 110399653d4eSeschrock nvlist_t *newconfig; 110495173954Sek uint64_t hostid; 1105fa9e4066Sahrens 110699653d4eSeschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 1107ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1108ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1109ea8dc4b6Seschrock error = EIO; 1110ea8dc4b6Seschrock goto out; 1111ea8dc4b6Seschrock } 1112fa9e4066Sahrens 111395173954Sek if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 111495173954Sek &hostid) == 0) { 111595173954Sek char *hostname; 111695173954Sek unsigned long myhostid = 0; 111795173954Sek 111895173954Sek VERIFY(nvlist_lookup_string(newconfig, 111995173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 112095173954Sek 112195173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 112217194a52Slling if (hostid != 0 && myhostid != 0 && 112317194a52Slling (unsigned long)hostid != myhostid) { 112495173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 112595173954Sek "loaded as it was last accessed by " 112695173954Sek "another system (host: %s hostid: 0x%lx). " 112795173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 112895173954Sek spa->spa_name, hostname, 112995173954Sek (unsigned long)hostid); 113095173954Sek error = EBADF; 113195173954Sek goto out; 113295173954Sek } 113395173954Sek } 113495173954Sek 1135fa9e4066Sahrens spa_config_set(spa, newconfig); 1136fa9e4066Sahrens spa_unload(spa); 1137fa9e4066Sahrens spa_deactivate(spa); 1138fa9e4066Sahrens spa_activate(spa); 1139fa9e4066Sahrens 1140ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 1141fa9e4066Sahrens } 1142fa9e4066Sahrens 1143ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1144fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1145ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 1146ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1147ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1148ea8dc4b6Seschrock error = EIO; 1149ea8dc4b6Seschrock goto out; 1150ea8dc4b6Seschrock } 1151fa9e4066Sahrens 115299653d4eSeschrock /* 115399653d4eSeschrock * Load the bit that tells us to use the new accounting function 115499653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 115599653d4eSeschrock * be present. 115699653d4eSeschrock */ 115799653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, 115899653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 115999653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate); 116099653d4eSeschrock if (error != 0 && error != ENOENT) { 116199653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 116299653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 116399653d4eSeschrock error = EIO; 116499653d4eSeschrock goto out; 116599653d4eSeschrock } 116699653d4eSeschrock 1167fa9e4066Sahrens /* 1168ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1169ea8dc4b6Seschrock * not be present. 1170fa9e4066Sahrens */ 1171ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1172ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 1173ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 1174d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 1175ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1176ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1177ea8dc4b6Seschrock error = EIO; 1178ea8dc4b6Seschrock goto out; 1179ea8dc4b6Seschrock } 1180ea8dc4b6Seschrock 1181ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1182ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1183ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1184ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 1185ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1186ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1187ea8dc4b6Seschrock error = EIO; 1188ea8dc4b6Seschrock goto out; 1189ea8dc4b6Seschrock } 1190ea8dc4b6Seschrock 119106eeb2adSek /* 119206eeb2adSek * Load the history object. If we have an older pool, this 119306eeb2adSek * will not be present. 119406eeb2adSek */ 119506eeb2adSek error = zap_lookup(spa->spa_meta_objset, 119606eeb2adSek DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 119706eeb2adSek sizeof (uint64_t), 1, &spa->spa_history); 119806eeb2adSek if (error != 0 && error != ENOENT) { 119906eeb2adSek vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 120006eeb2adSek VDEV_AUX_CORRUPT_DATA); 120106eeb2adSek error = EIO; 120206eeb2adSek goto out; 120306eeb2adSek } 120406eeb2adSek 120599653d4eSeschrock /* 120699653d4eSeschrock * Load any hot spares for this pool. 120799653d4eSeschrock */ 120899653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1209fa94a07fSbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 121099653d4eSeschrock if (error != 0 && error != ENOENT) { 121199653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 121299653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 121399653d4eSeschrock error = EIO; 121499653d4eSeschrock goto out; 121599653d4eSeschrock } 121699653d4eSeschrock if (error == 0) { 1217e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 1218fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 1219fa94a07fSbrendan &spa->spa_spares.sav_config) != 0) { 122099653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 122199653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 122299653d4eSeschrock error = EIO; 122399653d4eSeschrock goto out; 122499653d4eSeschrock } 122599653d4eSeschrock 122699653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 122799653d4eSeschrock spa_load_spares(spa); 122899653d4eSeschrock spa_config_exit(spa, FTAG); 122999653d4eSeschrock } 123099653d4eSeschrock 1231fa94a07fSbrendan /* 1232fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 1233fa94a07fSbrendan */ 1234fa94a07fSbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1235fa94a07fSbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 1236fa94a07fSbrendan &spa->spa_l2cache.sav_object); 1237fa94a07fSbrendan if (error != 0 && error != ENOENT) { 1238fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1239fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1240fa94a07fSbrendan error = EIO; 1241fa94a07fSbrendan goto out; 1242fa94a07fSbrendan } 1243fa94a07fSbrendan if (error == 0) { 1244fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 1245fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 1246fa94a07fSbrendan &spa->spa_l2cache.sav_config) != 0) { 1247fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, 1248fa94a07fSbrendan VDEV_STATE_CANT_OPEN, 1249fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1250fa94a07fSbrendan error = EIO; 1251fa94a07fSbrendan goto out; 1252fa94a07fSbrendan } 1253fa94a07fSbrendan 1254fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 1255fa94a07fSbrendan spa_load_l2cache(spa); 1256fa94a07fSbrendan spa_config_exit(spa, FTAG); 1257fa94a07fSbrendan } 1258fa94a07fSbrendan 1259990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1260ecd6cf80Smarks 1261b1b8ab34Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1262b1b8ab34Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1263b1b8ab34Slling 1264b1b8ab34Slling if (error && error != ENOENT) { 1265b1b8ab34Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1266b1b8ab34Slling VDEV_AUX_CORRUPT_DATA); 1267b1b8ab34Slling error = EIO; 1268b1b8ab34Slling goto out; 1269b1b8ab34Slling } 1270b1b8ab34Slling 1271b1b8ab34Slling if (error == 0) { 1272b1b8ab34Slling (void) zap_lookup(spa->spa_meta_objset, 1273b1b8ab34Slling spa->spa_pool_props_object, 12743d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1275b1b8ab34Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 12763d7072f8Seschrock (void) zap_lookup(spa->spa_meta_objset, 12773d7072f8Seschrock spa->spa_pool_props_object, 12783d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 12793d7072f8Seschrock sizeof (uint64_t), 1, &autoreplace); 1280ecd6cf80Smarks (void) zap_lookup(spa->spa_meta_objset, 1281ecd6cf80Smarks spa->spa_pool_props_object, 1282ecd6cf80Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1283ecd6cf80Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 12840a4e9518Sgw (void) zap_lookup(spa->spa_meta_objset, 12850a4e9518Sgw spa->spa_pool_props_object, 12860a4e9518Sgw zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 12870a4e9518Sgw sizeof (uint64_t), 1, &spa->spa_failmode); 1288b1b8ab34Slling } 1289b1b8ab34Slling 12903d7072f8Seschrock /* 12913d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 12923d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 12933d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 12943d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 12953d7072f8Seschrock * over. 12963d7072f8Seschrock */ 1297b01c3b58Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 12983d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 12993d7072f8Seschrock 1300ea8dc4b6Seschrock /* 1301560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1302ea8dc4b6Seschrock */ 1303560e6e96Seschrock vdev_load(rvd); 13040373e76bSbonwick 1305fa9e4066Sahrens /* 1306fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1307fa9e4066Sahrens */ 1308ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1309fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1310ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1311fa9e4066Sahrens 1312fa9e4066Sahrens /* 1313fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1314fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1315fa9e4066Sahrens */ 1316ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1317ea8dc4b6Seschrock error = ENXIO; 1318ea8dc4b6Seschrock goto out; 1319ea8dc4b6Seschrock } 1320fa9e4066Sahrens 1321ea8dc4b6Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13225dabedeeSbonwick dmu_tx_t *tx; 13230373e76bSbonwick int need_update = B_FALSE; 13240373e76bSbonwick int c; 13255dabedeeSbonwick 13260373e76bSbonwick /* 13270373e76bSbonwick * Claim log blocks that haven't been committed yet. 13280373e76bSbonwick * This must all happen in a single txg. 13290373e76bSbonwick */ 13305dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1331fa9e4066Sahrens spa_first_txg(spa)); 13320b69c2f0Sahrens (void) dmu_objset_find(spa->spa_name, 13330b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1334fa9e4066Sahrens dmu_tx_commit(tx); 1335fa9e4066Sahrens 1336fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1337fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1338fa9e4066Sahrens 1339fa9e4066Sahrens /* 1340fa9e4066Sahrens * Wait for all claims to sync. 1341fa9e4066Sahrens */ 1342fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13430e34b6a7Sbonwick 13440e34b6a7Sbonwick /* 13450373e76bSbonwick * If the config cache is stale, or we have uninitialized 13460373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 13470e34b6a7Sbonwick */ 13480373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 13490373e76bSbonwick state == SPA_LOAD_IMPORT) 13500373e76bSbonwick need_update = B_TRUE; 13510373e76bSbonwick 13520373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) 13530373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13540373e76bSbonwick need_update = B_TRUE; 13550e34b6a7Sbonwick 13560e34b6a7Sbonwick /* 13570373e76bSbonwick * Update the config cache asychronously in case we're the 13580373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 13590e34b6a7Sbonwick */ 13600373e76bSbonwick if (need_update) 13610373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1362fa9e4066Sahrens } 1363fa9e4066Sahrens 1364ea8dc4b6Seschrock error = 0; 1365ea8dc4b6Seschrock out: 136699653d4eSeschrock if (error && error != EBADF) 1367ea8dc4b6Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 1368ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 1369ea8dc4b6Seschrock spa->spa_ena = 0; 1370ea8dc4b6Seschrock 1371ea8dc4b6Seschrock return (error); 1372fa9e4066Sahrens } 1373fa9e4066Sahrens 1374fa9e4066Sahrens /* 1375fa9e4066Sahrens * Pool Open/Import 1376fa9e4066Sahrens * 1377fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 1378fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1379fa9e4066Sahrens * case of an open, the pool configuration will exist in the 13803d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 1381fa9e4066Sahrens * 1382fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1383fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 1384fa9e4066Sahrens * ambiguous state. 1385fa9e4066Sahrens */ 1386fa9e4066Sahrens static int 1387fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1388fa9e4066Sahrens { 1389fa9e4066Sahrens spa_t *spa; 1390fa9e4066Sahrens int error; 1391fa9e4066Sahrens int loaded = B_FALSE; 1392fa9e4066Sahrens int locked = B_FALSE; 1393fa9e4066Sahrens 1394fa9e4066Sahrens *spapp = NULL; 1395fa9e4066Sahrens 1396fa9e4066Sahrens /* 1397fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 1398fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1399fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 1400fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 1401fa9e4066Sahrens */ 1402fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1403fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1404fa9e4066Sahrens locked = B_TRUE; 1405fa9e4066Sahrens } 1406fa9e4066Sahrens 1407fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1408fa9e4066Sahrens if (locked) 1409fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1410fa9e4066Sahrens return (ENOENT); 1411fa9e4066Sahrens } 1412fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1413fa9e4066Sahrens 1414fa9e4066Sahrens spa_activate(spa); 1415fa9e4066Sahrens 14160373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1417fa9e4066Sahrens 1418fa9e4066Sahrens if (error == EBADF) { 1419fa9e4066Sahrens /* 1420560e6e96Seschrock * If vdev_validate() returns failure (indicated by 1421560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 1422560e6e96Seschrock * that the pool has been exported or destroyed. If 1423560e6e96Seschrock * this is the case, the config cache is out of sync and 1424560e6e96Seschrock * we should remove the pool from the namespace. 1425fa9e4066Sahrens */ 1426fa9e4066Sahrens spa_unload(spa); 1427fa9e4066Sahrens spa_deactivate(spa); 1428*c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1429fa9e4066Sahrens spa_remove(spa); 1430fa9e4066Sahrens if (locked) 1431fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1432fa9e4066Sahrens return (ENOENT); 1433ea8dc4b6Seschrock } 1434ea8dc4b6Seschrock 1435ea8dc4b6Seschrock if (error) { 1436fa9e4066Sahrens /* 1437fa9e4066Sahrens * We can't open the pool, but we still have useful 1438fa9e4066Sahrens * information: the state of each vdev after the 1439fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 1440fa9e4066Sahrens */ 14410373e76bSbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14420373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 1443fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1444fa9e4066Sahrens B_TRUE); 14450373e76bSbonwick spa_config_exit(spa, FTAG); 14460373e76bSbonwick } 1447fa9e4066Sahrens spa_unload(spa); 1448fa9e4066Sahrens spa_deactivate(spa); 1449ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 1450fa9e4066Sahrens if (locked) 1451fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1452fa9e4066Sahrens *spapp = NULL; 1453fa9e4066Sahrens return (error); 1454ea8dc4b6Seschrock } else { 1455ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 1456fa9e4066Sahrens } 1457fa9e4066Sahrens 1458fa9e4066Sahrens loaded = B_TRUE; 1459fa9e4066Sahrens } 1460fa9e4066Sahrens 1461fa9e4066Sahrens spa_open_ref(spa, tag); 14623d7072f8Seschrock 14633d7072f8Seschrock /* 14643d7072f8Seschrock * If we just loaded the pool, resilver anything that's out of date. 14653d7072f8Seschrock */ 14663d7072f8Seschrock if (loaded && (spa_mode & FWRITE)) 14673d7072f8Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 14683d7072f8Seschrock 1469fa9e4066Sahrens if (locked) 1470fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1471fa9e4066Sahrens 1472fa9e4066Sahrens *spapp = spa; 1473fa9e4066Sahrens 1474fa9e4066Sahrens if (config != NULL) { 1475ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1476fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1477ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1478fa9e4066Sahrens } 1479fa9e4066Sahrens 1480fa9e4066Sahrens return (0); 1481fa9e4066Sahrens } 1482fa9e4066Sahrens 1483fa9e4066Sahrens int 1484fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1485fa9e4066Sahrens { 1486fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1487fa9e4066Sahrens } 1488fa9e4066Sahrens 1489ea8dc4b6Seschrock /* 1490ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 1491ea8dc4b6Seschrock * preventing it from being exported or destroyed. 1492ea8dc4b6Seschrock */ 1493ea8dc4b6Seschrock spa_t * 1494ea8dc4b6Seschrock spa_inject_addref(char *name) 1495ea8dc4b6Seschrock { 1496ea8dc4b6Seschrock spa_t *spa; 1497ea8dc4b6Seschrock 1498ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1499ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 1500ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1501ea8dc4b6Seschrock return (NULL); 1502ea8dc4b6Seschrock } 1503ea8dc4b6Seschrock spa->spa_inject_ref++; 1504ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1505ea8dc4b6Seschrock 1506ea8dc4b6Seschrock return (spa); 1507ea8dc4b6Seschrock } 1508ea8dc4b6Seschrock 1509ea8dc4b6Seschrock void 1510ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 1511ea8dc4b6Seschrock { 1512ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1513ea8dc4b6Seschrock spa->spa_inject_ref--; 1514ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1515ea8dc4b6Seschrock } 1516ea8dc4b6Seschrock 1517fa94a07fSbrendan /* 1518fa94a07fSbrendan * Add spares device information to the nvlist. 1519fa94a07fSbrendan */ 152099653d4eSeschrock static void 152199653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 152299653d4eSeschrock { 152399653d4eSeschrock nvlist_t **spares; 152499653d4eSeschrock uint_t i, nspares; 152599653d4eSeschrock nvlist_t *nvroot; 152699653d4eSeschrock uint64_t guid; 152799653d4eSeschrock vdev_stat_t *vs; 152899653d4eSeschrock uint_t vsc; 152939c23413Seschrock uint64_t pool; 153099653d4eSeschrock 1531fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 153299653d4eSeschrock return; 153399653d4eSeschrock 153499653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 153599653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1536fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 153799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 153899653d4eSeschrock if (nspares != 0) { 153999653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 154099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 154199653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 154299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 154399653d4eSeschrock 154499653d4eSeschrock /* 154599653d4eSeschrock * Go through and find any spares which have since been 154699653d4eSeschrock * repurposed as an active spare. If this is the case, update 154799653d4eSeschrock * their status appropriately. 154899653d4eSeschrock */ 154999653d4eSeschrock for (i = 0; i < nspares; i++) { 155099653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 155199653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 155239c23413Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 155399653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 155499653d4eSeschrock spares[i], ZPOOL_CONFIG_STATS, 155599653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 155699653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 155799653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 155899653d4eSeschrock } 155999653d4eSeschrock } 156099653d4eSeschrock } 156199653d4eSeschrock } 156299653d4eSeschrock 1563fa94a07fSbrendan /* 1564fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 1565fa94a07fSbrendan */ 1566fa94a07fSbrendan static void 1567fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 1568fa94a07fSbrendan { 1569fa94a07fSbrendan nvlist_t **l2cache; 1570fa94a07fSbrendan uint_t i, j, nl2cache; 1571fa94a07fSbrendan nvlist_t *nvroot; 1572fa94a07fSbrendan uint64_t guid; 1573fa94a07fSbrendan vdev_t *vd; 1574fa94a07fSbrendan vdev_stat_t *vs; 1575fa94a07fSbrendan uint_t vsc; 1576fa94a07fSbrendan 1577fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 1578fa94a07fSbrendan return; 1579fa94a07fSbrendan 1580fa94a07fSbrendan spa_config_enter(spa, RW_READER, FTAG); 1581fa94a07fSbrendan 1582fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 1583fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1584fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 1585fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1586fa94a07fSbrendan if (nl2cache != 0) { 1587fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 1588fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 1589fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 1590fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1591fa94a07fSbrendan 1592fa94a07fSbrendan /* 1593fa94a07fSbrendan * Update level 2 cache device stats. 1594fa94a07fSbrendan */ 1595fa94a07fSbrendan 1596fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1597fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 1598fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 1599fa94a07fSbrendan 1600fa94a07fSbrendan vd = NULL; 1601fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 1602fa94a07fSbrendan if (guid == 1603fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 1604fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 1605fa94a07fSbrendan break; 1606fa94a07fSbrendan } 1607fa94a07fSbrendan } 1608fa94a07fSbrendan ASSERT(vd != NULL); 1609fa94a07fSbrendan 1610fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 1611fa94a07fSbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 1612fa94a07fSbrendan vdev_get_stats(vd, vs); 1613fa94a07fSbrendan } 1614fa94a07fSbrendan } 1615fa94a07fSbrendan 1616fa94a07fSbrendan spa_config_exit(spa, FTAG); 1617fa94a07fSbrendan } 1618fa94a07fSbrendan 1619fa9e4066Sahrens int 1620ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1621fa9e4066Sahrens { 1622fa9e4066Sahrens int error; 1623fa9e4066Sahrens spa_t *spa; 1624fa9e4066Sahrens 1625fa9e4066Sahrens *config = NULL; 1626fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 1627fa9e4066Sahrens 162899653d4eSeschrock if (spa && *config != NULL) { 1629ea8dc4b6Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 1630ea8dc4b6Seschrock spa_get_errlog_size(spa)) == 0); 1631ea8dc4b6Seschrock 163299653d4eSeschrock spa_add_spares(spa, *config); 1633fa94a07fSbrendan spa_add_l2cache(spa, *config); 163499653d4eSeschrock } 163599653d4eSeschrock 1636ea8dc4b6Seschrock /* 1637ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 1638ea8dc4b6Seschrock * and call spa_lookup() directly. 1639ea8dc4b6Seschrock */ 1640ea8dc4b6Seschrock if (altroot) { 1641ea8dc4b6Seschrock if (spa == NULL) { 1642ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1643ea8dc4b6Seschrock spa = spa_lookup(name); 1644ea8dc4b6Seschrock if (spa) 1645ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1646ea8dc4b6Seschrock else 1647ea8dc4b6Seschrock altroot[0] = '\0'; 1648ea8dc4b6Seschrock spa = NULL; 1649ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1650ea8dc4b6Seschrock } else { 1651ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1652ea8dc4b6Seschrock } 1653ea8dc4b6Seschrock } 1654ea8dc4b6Seschrock 1655fa9e4066Sahrens if (spa != NULL) 1656fa9e4066Sahrens spa_close(spa, FTAG); 1657fa9e4066Sahrens 1658fa9e4066Sahrens return (error); 1659fa9e4066Sahrens } 1660fa9e4066Sahrens 166199653d4eSeschrock /* 1662fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 1663fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 1664fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 1665fa94a07fSbrendan * specified, as long as they are well-formed. 166699653d4eSeschrock */ 166799653d4eSeschrock static int 1668fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 1669fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 1670fa94a07fSbrendan vdev_labeltype_t label) 167199653d4eSeschrock { 1672fa94a07fSbrendan nvlist_t **dev; 1673fa94a07fSbrendan uint_t i, ndev; 167499653d4eSeschrock vdev_t *vd; 167599653d4eSeschrock int error; 167699653d4eSeschrock 167799653d4eSeschrock /* 1678fa94a07fSbrendan * It's acceptable to have no devs specified. 167999653d4eSeschrock */ 1680fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 168199653d4eSeschrock return (0); 168299653d4eSeschrock 1683fa94a07fSbrendan if (ndev == 0) 168499653d4eSeschrock return (EINVAL); 168599653d4eSeschrock 168699653d4eSeschrock /* 1687fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 1688fa94a07fSbrendan * device type. 168999653d4eSeschrock */ 1690fa94a07fSbrendan if (spa_version(spa) < version) 169199653d4eSeschrock return (ENOTSUP); 169299653d4eSeschrock 169339c23413Seschrock /* 1694fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 169539c23413Seschrock * checking. 169639c23413Seschrock */ 1697fa94a07fSbrendan sav->sav_pending = dev; 1698fa94a07fSbrendan sav->sav_npending = ndev; 169939c23413Seschrock 1700fa94a07fSbrendan for (i = 0; i < ndev; i++) { 1701fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 170299653d4eSeschrock mode)) != 0) 170339c23413Seschrock goto out; 170499653d4eSeschrock 170599653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 170699653d4eSeschrock vdev_free(vd); 170739c23413Seschrock error = EINVAL; 170839c23413Seschrock goto out; 170999653d4eSeschrock } 171099653d4eSeschrock 1711fa94a07fSbrendan /* 1712fa94a07fSbrendan * The L2ARC currently only supports disk devices. 1713fa94a07fSbrendan */ 1714fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 1715fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 1716fa94a07fSbrendan error = ENOTBLK; 1717fa94a07fSbrendan goto out; 1718fa94a07fSbrendan } 1719fa94a07fSbrendan 172099653d4eSeschrock vd->vdev_top = vd; 172199653d4eSeschrock 172239c23413Seschrock if ((error = vdev_open(vd)) == 0 && 1723fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 1724fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 172539c23413Seschrock vd->vdev_guid) == 0); 172639c23413Seschrock } 172799653d4eSeschrock 172899653d4eSeschrock vdev_free(vd); 172939c23413Seschrock 1730fa94a07fSbrendan if (error && 1731fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 173239c23413Seschrock goto out; 173339c23413Seschrock else 173439c23413Seschrock error = 0; 173599653d4eSeschrock } 173699653d4eSeschrock 173739c23413Seschrock out: 1738fa94a07fSbrendan sav->sav_pending = NULL; 1739fa94a07fSbrendan sav->sav_npending = 0; 174039c23413Seschrock return (error); 174199653d4eSeschrock } 174299653d4eSeschrock 1743fa94a07fSbrendan static int 1744fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 1745fa94a07fSbrendan { 1746fa94a07fSbrendan int error; 1747fa94a07fSbrendan 1748fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 1749fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 1750fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 1751fa94a07fSbrendan return (error); 1752fa94a07fSbrendan } 1753fa94a07fSbrendan 1754fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 1755fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 1756fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 1757fa94a07fSbrendan } 1758fa94a07fSbrendan 1759fa94a07fSbrendan static void 1760fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 1761fa94a07fSbrendan const char *config) 1762fa94a07fSbrendan { 1763fa94a07fSbrendan int i; 1764fa94a07fSbrendan 1765fa94a07fSbrendan if (sav->sav_config != NULL) { 1766fa94a07fSbrendan nvlist_t **olddevs; 1767fa94a07fSbrendan uint_t oldndevs; 1768fa94a07fSbrendan nvlist_t **newdevs; 1769fa94a07fSbrendan 1770fa94a07fSbrendan /* 1771fa94a07fSbrendan * Generate new dev list by concatentating with the 1772fa94a07fSbrendan * current dev list. 1773fa94a07fSbrendan */ 1774fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 1775fa94a07fSbrendan &olddevs, &oldndevs) == 0); 1776fa94a07fSbrendan 1777fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 1778fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 1779fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 1780fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 1781fa94a07fSbrendan KM_SLEEP) == 0); 1782fa94a07fSbrendan for (i = 0; i < ndevs; i++) 1783fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 1784fa94a07fSbrendan KM_SLEEP) == 0); 1785fa94a07fSbrendan 1786fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 1787fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1788fa94a07fSbrendan 1789fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1790fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 1791fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 1792fa94a07fSbrendan nvlist_free(newdevs[i]); 1793fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 1794fa94a07fSbrendan } else { 1795fa94a07fSbrendan /* 1796fa94a07fSbrendan * Generate a new dev list. 1797fa94a07fSbrendan */ 1798fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 1799fa94a07fSbrendan KM_SLEEP) == 0); 1800fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 1801fa94a07fSbrendan devs, ndevs) == 0); 1802fa94a07fSbrendan } 1803fa94a07fSbrendan } 1804fa94a07fSbrendan 1805fa94a07fSbrendan /* 1806fa94a07fSbrendan * Stop and drop level 2 ARC devices 1807fa94a07fSbrendan */ 1808fa94a07fSbrendan void 1809fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 1810fa94a07fSbrendan { 1811fa94a07fSbrendan vdev_t *vd; 1812fa94a07fSbrendan int i; 1813fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1814fa94a07fSbrendan 1815fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 1816fa94a07fSbrendan uint64_t pool; 1817fa94a07fSbrendan 1818fa94a07fSbrendan vd = sav->sav_vdevs[i]; 1819fa94a07fSbrendan ASSERT(vd != NULL); 1820fa94a07fSbrendan 1821fa94a07fSbrendan if (spa_mode & FWRITE && 1822*c5904d13Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 1823*c5904d13Seschrock l2arc_vdev_present(vd)) { 1824fa94a07fSbrendan l2arc_remove_vdev(vd); 1825fa94a07fSbrendan } 1826fa94a07fSbrendan if (vd->vdev_isl2cache) 1827fa94a07fSbrendan spa_l2cache_remove(vd); 1828fa94a07fSbrendan vdev_clear_stats(vd); 1829fa94a07fSbrendan (void) vdev_close(vd); 1830fa94a07fSbrendan } 1831fa94a07fSbrendan } 1832fa94a07fSbrendan 1833fa9e4066Sahrens /* 1834fa9e4066Sahrens * Pool Creation 1835fa9e4066Sahrens */ 1836fa9e4066Sahrens int 1837990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 1838228975ccSek const char *history_str) 1839fa9e4066Sahrens { 1840fa9e4066Sahrens spa_t *spa; 1841990b4856Slling char *altroot = NULL; 18420373e76bSbonwick vdev_t *rvd; 1843fa9e4066Sahrens dsl_pool_t *dp; 1844fa9e4066Sahrens dmu_tx_t *tx; 184599653d4eSeschrock int c, error = 0; 1846fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 1847fa94a07fSbrendan nvlist_t **spares, **l2cache; 1848fa94a07fSbrendan uint_t nspares, nl2cache; 1849990b4856Slling uint64_t version; 1850fa9e4066Sahrens 1851fa9e4066Sahrens /* 1852fa9e4066Sahrens * If this pool already exists, return failure. 1853fa9e4066Sahrens */ 1854fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1855fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 1856fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1857fa9e4066Sahrens return (EEXIST); 1858fa9e4066Sahrens } 1859fa9e4066Sahrens 1860fa9e4066Sahrens /* 1861fa9e4066Sahrens * Allocate a new spa_t structure. 1862fa9e4066Sahrens */ 1863990b4856Slling (void) nvlist_lookup_string(props, 1864990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18650373e76bSbonwick spa = spa_add(pool, altroot); 1866fa9e4066Sahrens spa_activate(spa); 1867fa9e4066Sahrens 1868fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 1869990b4856Slling 1870990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 1871990b4856Slling spa_unload(spa); 1872990b4856Slling spa_deactivate(spa); 1873990b4856Slling spa_remove(spa); 1874*c5904d13Seschrock mutex_exit(&spa_namespace_lock); 1875990b4856Slling return (error); 1876990b4856Slling } 1877990b4856Slling 1878990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 1879990b4856Slling &version) != 0) 1880990b4856Slling version = SPA_VERSION; 1881990b4856Slling ASSERT(version <= SPA_VERSION); 1882990b4856Slling spa->spa_uberblock.ub_version = version; 1883fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1884fa9e4066Sahrens 18850373e76bSbonwick /* 18860373e76bSbonwick * Create the root vdev. 18870373e76bSbonwick */ 18880373e76bSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 18890373e76bSbonwick 189099653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 18910373e76bSbonwick 189299653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 189399653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 18940373e76bSbonwick 1895b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 18960373e76bSbonwick error = EINVAL; 189799653d4eSeschrock 189899653d4eSeschrock if (error == 0 && 189999653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 1900fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 190199653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 190299653d4eSeschrock for (c = 0; c < rvd->vdev_children; c++) 190399653d4eSeschrock vdev_init(rvd->vdev_child[c], txg); 190499653d4eSeschrock vdev_config_dirty(rvd); 19050373e76bSbonwick } 19060373e76bSbonwick 19070373e76bSbonwick spa_config_exit(spa, FTAG); 1908fa9e4066Sahrens 190999653d4eSeschrock if (error != 0) { 1910fa9e4066Sahrens spa_unload(spa); 1911fa9e4066Sahrens spa_deactivate(spa); 1912fa9e4066Sahrens spa_remove(spa); 1913fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1914fa9e4066Sahrens return (error); 1915fa9e4066Sahrens } 1916fa9e4066Sahrens 191799653d4eSeschrock /* 191899653d4eSeschrock * Get the list of spares, if specified. 191999653d4eSeschrock */ 192099653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 192199653d4eSeschrock &spares, &nspares) == 0) { 1922fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 192399653d4eSeschrock KM_SLEEP) == 0); 1924fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 192599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 192699653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 192799653d4eSeschrock spa_load_spares(spa); 192899653d4eSeschrock spa_config_exit(spa, FTAG); 1929fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 1930fa94a07fSbrendan } 1931fa94a07fSbrendan 1932fa94a07fSbrendan /* 1933fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 1934fa94a07fSbrendan */ 1935fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1936fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1937fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 1938fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 1939fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 1940fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 1941fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 1942fa94a07fSbrendan spa_load_l2cache(spa); 1943fa94a07fSbrendan spa_config_exit(spa, FTAG); 1944fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 194599653d4eSeschrock } 194699653d4eSeschrock 1947fa9e4066Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1948fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1949fa9e4066Sahrens 1950fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 1951fa9e4066Sahrens 1952fa9e4066Sahrens /* 1953fa9e4066Sahrens * Create the pool config object. 1954fa9e4066Sahrens */ 1955fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1956fa9e4066Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1957fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1958fa9e4066Sahrens 1959ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1960fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1961ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 1962ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 1963ea8dc4b6Seschrock } 1964fa9e4066Sahrens 1965990b4856Slling /* Newly created pools with the right version are always deflated. */ 1966990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 1967990b4856Slling spa->spa_deflate = TRUE; 1968990b4856Slling if (zap_add(spa->spa_meta_objset, 1969990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 1970990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 1971990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 1972990b4856Slling } 197399653d4eSeschrock } 197499653d4eSeschrock 1975fa9e4066Sahrens /* 1976fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 1977fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 1978fa9e4066Sahrens * keeps changing. 1979fa9e4066Sahrens */ 1980fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1981fa9e4066Sahrens 1 << 14, tx); 1982fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1983fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 1984fa9e4066Sahrens 1985ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 1986fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1987ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 1988ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 1989ea8dc4b6Seschrock } 1990fa9e4066Sahrens 199106eeb2adSek /* 199206eeb2adSek * Create the pool's history object. 199306eeb2adSek */ 1994990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 1995990b4856Slling spa_history_create_obj(spa, tx); 1996990b4856Slling 1997990b4856Slling /* 1998990b4856Slling * Set pool properties. 1999990b4856Slling */ 2000990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 2001990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20020a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 2003990b4856Slling if (props) 2004990b4856Slling spa_sync_props(spa, props, CRED(), tx); 200506eeb2adSek 2006fa9e4066Sahrens dmu_tx_commit(tx); 2007fa9e4066Sahrens 2008fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2009fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2010fa9e4066Sahrens 2011fa9e4066Sahrens /* 2012fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 2013fa9e4066Sahrens * bean counters are appropriately updated. 2014fa9e4066Sahrens */ 2015fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2016fa9e4066Sahrens 2017*c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2018fa9e4066Sahrens 2019990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 2020228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 2021228975ccSek 2022fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2023fa9e4066Sahrens 2024fa9e4066Sahrens return (0); 2025fa9e4066Sahrens } 2026fa9e4066Sahrens 2027fa9e4066Sahrens /* 2028fa9e4066Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2029fa9e4066Sahrens * then call spa_load() to do the dirty work. 2030fa9e4066Sahrens */ 2031e7cbe64fSgw static int 2032e7cbe64fSgw spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 2033*c5904d13Seschrock boolean_t isroot, boolean_t allowfaulted) 2034fa9e4066Sahrens { 2035fa9e4066Sahrens spa_t *spa; 2036990b4856Slling char *altroot = NULL; 2037*c5904d13Seschrock int error, loaderr; 203899653d4eSeschrock nvlist_t *nvroot; 2039fa94a07fSbrendan nvlist_t **spares, **l2cache; 2040fa94a07fSbrendan uint_t nspares, nl2cache; 2041e7cbe64fSgw int mosconfig = isroot? B_FALSE : B_TRUE; 2042fa9e4066Sahrens 2043fa9e4066Sahrens /* 2044fa9e4066Sahrens * If a pool with this name exists, return failure. 2045fa9e4066Sahrens */ 2046fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2047fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 2048fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2049fa9e4066Sahrens return (EEXIST); 2050fa9e4066Sahrens } 2051fa9e4066Sahrens 2052fa9e4066Sahrens /* 20530373e76bSbonwick * Create and initialize the spa structure. 2054fa9e4066Sahrens */ 2055990b4856Slling (void) nvlist_lookup_string(props, 2056990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20570373e76bSbonwick spa = spa_add(pool, altroot); 2058fa9e4066Sahrens spa_activate(spa); 2059fa9e4066Sahrens 2060*c5904d13Seschrock if (allowfaulted) 2061*c5904d13Seschrock spa->spa_import_faulted = B_TRUE; 2062*c5904d13Seschrock 20635dabedeeSbonwick /* 20640373e76bSbonwick * Pass off the heavy lifting to spa_load(). 2065ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 2066ecc2d604Sbonwick * is actually the one to trust when doing an import. 20675dabedeeSbonwick */ 2068*c5904d13Seschrock loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, mosconfig); 2069fa9e4066Sahrens 207099653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 207199653d4eSeschrock /* 207299653d4eSeschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 207399653d4eSeschrock * and conflicts with spa_has_spare(). 207499653d4eSeschrock */ 2075e7cbe64fSgw if (!isroot && spa->spa_spares.sav_config) { 2076fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 2077fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 207899653d4eSeschrock spa_load_spares(spa); 207999653d4eSeschrock } 2080e7cbe64fSgw if (!isroot && spa->spa_l2cache.sav_config) { 2081fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 2082fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 2083fa94a07fSbrendan spa_load_l2cache(spa); 2084fa94a07fSbrendan } 208599653d4eSeschrock 208699653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 208799653d4eSeschrock &nvroot) == 0); 2088fa94a07fSbrendan if (error == 0) 2089fa94a07fSbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 2090fa94a07fSbrendan if (error == 0) 2091fa94a07fSbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 2092fa94a07fSbrendan VDEV_ALLOC_L2CACHE); 209399653d4eSeschrock spa_config_exit(spa, FTAG); 209499653d4eSeschrock 2095990b4856Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 2096*c5904d13Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 2097*c5904d13Seschrock /* 2098*c5904d13Seschrock * If we failed to load the pool, but 'allowfaulted' is 2099*c5904d13Seschrock * set, then manually set the config as if the config 2100*c5904d13Seschrock * passed in was specified in the cache file. 2101*c5904d13Seschrock */ 2102*c5904d13Seschrock error = 0; 2103*c5904d13Seschrock spa->spa_import_faulted = B_FALSE; 2104*c5904d13Seschrock if (spa->spa_config == NULL) { 2105*c5904d13Seschrock spa_config_enter(spa, RW_READER, FTAG); 2106*c5904d13Seschrock spa->spa_config = spa_config_generate(spa, 2107*c5904d13Seschrock NULL, -1ULL, B_TRUE); 2108*c5904d13Seschrock spa_config_exit(spa, FTAG); 2109*c5904d13Seschrock } 2110*c5904d13Seschrock spa_unload(spa); 2111*c5904d13Seschrock spa_deactivate(spa); 2112*c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2113*c5904d13Seschrock } else { 2114*c5904d13Seschrock spa_unload(spa); 2115*c5904d13Seschrock spa_deactivate(spa); 2116*c5904d13Seschrock spa_remove(spa); 2117*c5904d13Seschrock } 2118fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2119fa9e4066Sahrens return (error); 2120fa9e4066Sahrens } 2121fa9e4066Sahrens 212299653d4eSeschrock /* 2123fa94a07fSbrendan * Override any spares and level 2 cache devices as specified by 2124fa94a07fSbrendan * the user, as these may have correct device names/devids, etc. 212599653d4eSeschrock */ 212699653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 212799653d4eSeschrock &spares, &nspares) == 0) { 2128fa94a07fSbrendan if (spa->spa_spares.sav_config) 2129fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 213099653d4eSeschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 213199653d4eSeschrock else 2132fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 213399653d4eSeschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 2134fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 213599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 213699653d4eSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 213799653d4eSeschrock spa_load_spares(spa); 213899653d4eSeschrock spa_config_exit(spa, FTAG); 2139fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2140fa94a07fSbrendan } 2141fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2142fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 2143fa94a07fSbrendan if (spa->spa_l2cache.sav_config) 2144fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 2145fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 2146fa94a07fSbrendan else 2147fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2148fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 2149fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2150fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2151fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 2152fa94a07fSbrendan spa_load_l2cache(spa); 2153fa94a07fSbrendan spa_config_exit(spa, FTAG); 2154fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 215599653d4eSeschrock } 215699653d4eSeschrock 2157*c5904d13Seschrock if (spa_mode & FWRITE) { 2158*c5904d13Seschrock /* 2159*c5904d13Seschrock * Update the config cache to include the newly-imported pool. 2160*c5904d13Seschrock */ 2161e7cbe64fSgw spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21620373e76bSbonwick 2163*c5904d13Seschrock /* 2164*c5904d13Seschrock * Resilver anything that's out of date. 2165*c5904d13Seschrock */ 2166*c5904d13Seschrock if (!isroot) 2167*c5904d13Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, 2168*c5904d13Seschrock B_TRUE) == 0); 2169*c5904d13Seschrock } 2170fa9e4066Sahrens 2171*c5904d13Seschrock spa->spa_import_faulted = B_FALSE; 21723d7072f8Seschrock mutex_exit(&spa_namespace_lock); 21733d7072f8Seschrock 2174fa9e4066Sahrens return (0); 2175fa9e4066Sahrens } 2176fa9e4066Sahrens 2177e7cbe64fSgw #ifdef _KERNEL 2178e7cbe64fSgw /* 2179e7cbe64fSgw * Build a "root" vdev for a top level vdev read in from a rootpool 2180e7cbe64fSgw * device label. 2181e7cbe64fSgw */ 2182e7cbe64fSgw static void 2183e7cbe64fSgw spa_build_rootpool_config(nvlist_t *config) 2184e7cbe64fSgw { 2185e7cbe64fSgw nvlist_t *nvtop, *nvroot; 2186e7cbe64fSgw uint64_t pgid; 2187e7cbe64fSgw 2188e7cbe64fSgw /* 2189e7cbe64fSgw * Add this top-level vdev to the child array. 2190e7cbe64fSgw */ 2191e7cbe64fSgw VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 2192e7cbe64fSgw == 0); 2193e7cbe64fSgw VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 2194e7cbe64fSgw == 0); 2195e7cbe64fSgw 2196e7cbe64fSgw /* 2197e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 2198e7cbe64fSgw */ 2199e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2200e7cbe64fSgw VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 2201e7cbe64fSgw == 0); 2202e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 2203e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 2204e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2205e7cbe64fSgw &nvtop, 1) == 0); 2206e7cbe64fSgw 2207e7cbe64fSgw /* 2208e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 2209e7cbe64fSgw * this pool's configuration (remove the old, add the new). 2210e7cbe64fSgw */ 2211e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 2212e7cbe64fSgw nvlist_free(nvroot); 2213e7cbe64fSgw } 2214e7cbe64fSgw 2215e7cbe64fSgw /* 2216e7cbe64fSgw * Get the root pool information from the root disk, then import the root pool 2217e7cbe64fSgw * during the system boot up time. 2218e7cbe64fSgw */ 2219e7cbe64fSgw extern nvlist_t *vdev_disk_read_rootlabel(char *); 2220e7cbe64fSgw 2221e7cbe64fSgw void 2222e7cbe64fSgw spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf, 2223e7cbe64fSgw uint64_t *besttxg) 2224e7cbe64fSgw { 2225e7cbe64fSgw nvlist_t *config; 2226e7cbe64fSgw uint64_t txg; 2227e7cbe64fSgw 2228e7cbe64fSgw if ((config = vdev_disk_read_rootlabel(devpath)) == NULL) 2229e7cbe64fSgw return; 2230e7cbe64fSgw 2231e7cbe64fSgw VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 2232e7cbe64fSgw 2233e7cbe64fSgw if (txg > *besttxg) { 2234e7cbe64fSgw *besttxg = txg; 2235e7cbe64fSgw if (*bestconf != NULL) 2236e7cbe64fSgw nvlist_free(*bestconf); 2237e7cbe64fSgw *bestconf = config; 2238e7cbe64fSgw *bestdev = devpath; 2239e7cbe64fSgw } 2240e7cbe64fSgw } 2241e7cbe64fSgw 2242e7cbe64fSgw boolean_t 2243e7cbe64fSgw spa_rootdev_validate(nvlist_t *nv) 2244e7cbe64fSgw { 2245e7cbe64fSgw uint64_t ival; 2246e7cbe64fSgw 2247e7cbe64fSgw if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 2248e7cbe64fSgw nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 2249e7cbe64fSgw nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 || 2250e7cbe64fSgw nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 2251e7cbe64fSgw return (B_FALSE); 2252e7cbe64fSgw 2253e7cbe64fSgw return (B_TRUE); 2254e7cbe64fSgw } 2255e7cbe64fSgw 2256e7cbe64fSgw /* 2257e7cbe64fSgw * Import a root pool. 2258e7cbe64fSgw * 2259e7cbe64fSgw * For x86. devpath_list will consist the physpath name of the vdev in a single 2260e7cbe64fSgw * disk root pool or a list of physnames for the vdevs in a mirrored rootpool. 2261e7cbe64fSgw * e.g. 2262e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a" 2263e7cbe64fSgw * 2264e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 2265e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 2266e7cbe64fSgw * e.g. 2267e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 2268e7cbe64fSgw */ 2269e7cbe64fSgw int 2270e7cbe64fSgw spa_import_rootpool(char *devpath_list) 2271e7cbe64fSgw { 2272e7cbe64fSgw nvlist_t *conf = NULL; 2273e7cbe64fSgw char *dev = NULL; 2274e7cbe64fSgw char *pname; 2275e7cbe64fSgw int error; 2276e7cbe64fSgw 2277e7cbe64fSgw /* 2278e7cbe64fSgw * Get the vdev pathname and configuation from the most 2279e7cbe64fSgw * recently updated vdev (highest txg). 2280e7cbe64fSgw */ 2281e7cbe64fSgw if (error = spa_get_rootconf(devpath_list, &dev, &conf)) 2282e7cbe64fSgw goto msg_out; 2283e7cbe64fSgw 2284e7cbe64fSgw /* 2285e7cbe64fSgw * Add type "root" vdev to the config. 2286e7cbe64fSgw */ 2287e7cbe64fSgw spa_build_rootpool_config(conf); 2288e7cbe64fSgw 2289e7cbe64fSgw VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 2290e7cbe64fSgw 2291*c5904d13Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_FALSE); 2292e7cbe64fSgw if (error == EEXIST) 2293e7cbe64fSgw error = 0; 2294e7cbe64fSgw 2295e7cbe64fSgw nvlist_free(conf); 2296e7cbe64fSgw return (error); 2297e7cbe64fSgw 2298e7cbe64fSgw msg_out: 2299e7cbe64fSgw cmn_err(CE_NOTE, "\n\n" 2300e7cbe64fSgw " *************************************************** \n" 2301e7cbe64fSgw " * This device is not bootable! * \n" 2302e7cbe64fSgw " * It is either offlined or detached or faulted. * \n" 2303e7cbe64fSgw " * Please try to boot from a different device. * \n" 2304e7cbe64fSgw " *************************************************** \n\n"); 2305e7cbe64fSgw 2306e7cbe64fSgw return (error); 2307e7cbe64fSgw } 2308e7cbe64fSgw #endif 2309e7cbe64fSgw 2310e7cbe64fSgw /* 2311e7cbe64fSgw * Import a non-root pool into the system. 2312e7cbe64fSgw */ 2313e7cbe64fSgw int 2314e7cbe64fSgw spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 2315e7cbe64fSgw { 2316*c5904d13Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 2317e7cbe64fSgw } 2318e7cbe64fSgw 2319*c5904d13Seschrock int 2320*c5904d13Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 2321*c5904d13Seschrock { 2322*c5904d13Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 2323*c5904d13Seschrock } 2324*c5904d13Seschrock 2325*c5904d13Seschrock 2326fa9e4066Sahrens /* 2327fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2328fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 2329fa9e4066Sahrens */ 2330fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 2331fa9e4066Sahrens 2332fa9e4066Sahrens nvlist_t * 2333fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 2334fa9e4066Sahrens { 2335fa9e4066Sahrens nvlist_t *config = NULL; 2336fa9e4066Sahrens char *poolname; 2337fa9e4066Sahrens spa_t *spa; 2338fa9e4066Sahrens uint64_t state; 2339fa9e4066Sahrens 2340fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2341fa9e4066Sahrens return (NULL); 2342fa9e4066Sahrens 2343fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2344fa9e4066Sahrens return (NULL); 2345fa9e4066Sahrens 2346fa9e4066Sahrens /* 23470373e76bSbonwick * Create and initialize the spa structure. 2348fa9e4066Sahrens */ 23490373e76bSbonwick mutex_enter(&spa_namespace_lock); 23500373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2351fa9e4066Sahrens spa_activate(spa); 2352fa9e4066Sahrens 2353fa9e4066Sahrens /* 23540373e76bSbonwick * Pass off the heavy lifting to spa_load(). 2355ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 2356ecc2d604Sbonwick * is actually the one to trust when doing an import. 2357fa9e4066Sahrens */ 2358ecc2d604Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2359fa9e4066Sahrens 2360fa9e4066Sahrens /* 2361fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 2362fa9e4066Sahrens */ 2363fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 23640373e76bSbonwick spa_config_enter(spa, RW_READER, FTAG); 2365fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 23660373e76bSbonwick spa_config_exit(spa, FTAG); 2367fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2368fa9e4066Sahrens poolname) == 0); 2369fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2370fa9e4066Sahrens state) == 0); 237195173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 237295173954Sek spa->spa_uberblock.ub_timestamp) == 0); 237399653d4eSeschrock 2374e7cbe64fSgw /* 2375e7cbe64fSgw * If the bootfs property exists on this pool then we 2376e7cbe64fSgw * copy it out so that external consumers can tell which 2377e7cbe64fSgw * pools are bootable. 2378e7cbe64fSgw */ 2379e7cbe64fSgw if (spa->spa_bootfs) { 2380e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2381e7cbe64fSgw 2382e7cbe64fSgw /* 2383e7cbe64fSgw * We have to play games with the name since the 2384e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 2385e7cbe64fSgw */ 2386e7cbe64fSgw if (dsl_dsobj_to_dsname(spa->spa_name, 2387e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 2388e7cbe64fSgw char *cp; 2389e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2390e7cbe64fSgw 2391e7cbe64fSgw cp = strchr(tmpname, '/'); 2392e7cbe64fSgw if (cp == NULL) { 2393e7cbe64fSgw (void) strlcpy(dsname, tmpname, 2394e7cbe64fSgw MAXPATHLEN); 2395e7cbe64fSgw } else { 2396e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 2397e7cbe64fSgw "%s/%s", poolname, ++cp); 2398e7cbe64fSgw } 2399e7cbe64fSgw VERIFY(nvlist_add_string(config, 2400e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 2401e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 2402e7cbe64fSgw } 2403e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 2404e7cbe64fSgw } 2405e7cbe64fSgw 240699653d4eSeschrock /* 2407fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 240899653d4eSeschrock */ 240999653d4eSeschrock spa_add_spares(spa, config); 2410fa94a07fSbrendan spa_add_l2cache(spa, config); 2411fa9e4066Sahrens } 2412fa9e4066Sahrens 2413fa9e4066Sahrens spa_unload(spa); 2414fa9e4066Sahrens spa_deactivate(spa); 2415fa9e4066Sahrens spa_remove(spa); 2416fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2417fa9e4066Sahrens 2418fa9e4066Sahrens return (config); 2419fa9e4066Sahrens } 2420fa9e4066Sahrens 2421fa9e4066Sahrens /* 2422fa9e4066Sahrens * Pool export/destroy 2423fa9e4066Sahrens * 2424fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2425fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2426fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 2427fa9e4066Sahrens * configuration from the cache afterwards. 2428fa9e4066Sahrens */ 2429fa9e4066Sahrens static int 243044cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 2431fa9e4066Sahrens { 2432fa9e4066Sahrens spa_t *spa; 2433fa9e4066Sahrens 243444cd46caSbillm if (oldconfig) 243544cd46caSbillm *oldconfig = NULL; 243644cd46caSbillm 2437fa9e4066Sahrens if (!(spa_mode & FWRITE)) 2438fa9e4066Sahrens return (EROFS); 2439fa9e4066Sahrens 2440fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2441fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2442fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2443fa9e4066Sahrens return (ENOENT); 2444fa9e4066Sahrens } 2445fa9e4066Sahrens 2446ea8dc4b6Seschrock /* 2447ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 2448ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 2449ea8dc4b6Seschrock */ 2450ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 2451ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2452ea8dc4b6Seschrock spa_async_suspend(spa); 2453ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2454ea8dc4b6Seschrock spa_close(spa, FTAG); 2455ea8dc4b6Seschrock 2456fa9e4066Sahrens /* 2457fa9e4066Sahrens * The pool will be in core if it's openable, 2458fa9e4066Sahrens * in which case we can modify its state. 2459fa9e4066Sahrens */ 2460fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2461fa9e4066Sahrens /* 2462fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 2463fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 2464fa9e4066Sahrens */ 2465fa9e4066Sahrens spa_scrub_suspend(spa); 2466fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2467fa9e4066Sahrens 2468ea8dc4b6Seschrock /* 2469ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 2470ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 2471ea8dc4b6Seschrock * fault injection handlers. 2472ea8dc4b6Seschrock */ 2473ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 2474ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 2475ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 2476fa9e4066Sahrens spa_scrub_resume(spa); 2477ea8dc4b6Seschrock spa_async_resume(spa); 2478fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2479fa9e4066Sahrens return (EBUSY); 2480fa9e4066Sahrens } 2481fa9e4066Sahrens 2482fa9e4066Sahrens spa_scrub_resume(spa); 2483fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2484fa9e4066Sahrens 2485fa9e4066Sahrens /* 2486fa9e4066Sahrens * We want this to be reflected on every label, 2487fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 2488fa9e4066Sahrens * final sync that pushes these changes out. 2489fa9e4066Sahrens */ 2490ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 24915dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 2492ea8dc4b6Seschrock spa->spa_state = new_state; 24930373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 2494ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 24955dabedeeSbonwick spa_config_exit(spa, FTAG); 2496ea8dc4b6Seschrock } 2497fa9e4066Sahrens } 2498fa9e4066Sahrens 24993d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 25003d7072f8Seschrock 2501fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2502fa9e4066Sahrens spa_unload(spa); 2503fa9e4066Sahrens spa_deactivate(spa); 2504fa9e4066Sahrens } 2505fa9e4066Sahrens 250644cd46caSbillm if (oldconfig && spa->spa_config) 250744cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 250844cd46caSbillm 2509ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 2510*c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2511ea8dc4b6Seschrock spa_remove(spa); 2512ea8dc4b6Seschrock } 2513fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2514fa9e4066Sahrens 2515fa9e4066Sahrens return (0); 2516fa9e4066Sahrens } 2517fa9e4066Sahrens 2518fa9e4066Sahrens /* 2519fa9e4066Sahrens * Destroy a storage pool. 2520fa9e4066Sahrens */ 2521fa9e4066Sahrens int 2522fa9e4066Sahrens spa_destroy(char *pool) 2523fa9e4066Sahrens { 252444cd46caSbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 2525fa9e4066Sahrens } 2526fa9e4066Sahrens 2527fa9e4066Sahrens /* 2528fa9e4066Sahrens * Export a storage pool. 2529fa9e4066Sahrens */ 2530fa9e4066Sahrens int 253144cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig) 2532fa9e4066Sahrens { 253344cd46caSbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 2534fa9e4066Sahrens } 2535fa9e4066Sahrens 2536ea8dc4b6Seschrock /* 2537ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 2538ea8dc4b6Seschrock * from the namespace in any way. 2539ea8dc4b6Seschrock */ 2540ea8dc4b6Seschrock int 2541ea8dc4b6Seschrock spa_reset(char *pool) 2542ea8dc4b6Seschrock { 254344cd46caSbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 2544ea8dc4b6Seschrock } 2545ea8dc4b6Seschrock 2546ea8dc4b6Seschrock 2547fa9e4066Sahrens /* 2548fa9e4066Sahrens * ========================================================================== 2549fa9e4066Sahrens * Device manipulation 2550fa9e4066Sahrens * ========================================================================== 2551fa9e4066Sahrens */ 2552fa9e4066Sahrens 2553fa9e4066Sahrens /* 25548654d025Sperrin * Add a device to a storage pool. 2555fa9e4066Sahrens */ 2556fa9e4066Sahrens int 2557fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2558fa9e4066Sahrens { 2559fa9e4066Sahrens uint64_t txg; 25600373e76bSbonwick int c, error; 2561fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 25620e34b6a7Sbonwick vdev_t *vd, *tvd; 2563fa94a07fSbrendan nvlist_t **spares, **l2cache; 2564fa94a07fSbrendan uint_t nspares, nl2cache; 2565fa9e4066Sahrens 2566fa9e4066Sahrens txg = spa_vdev_enter(spa); 2567fa9e4066Sahrens 256899653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 256999653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 257099653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 2571fa9e4066Sahrens 257239c23413Seschrock spa->spa_pending_vdev = vd; 257399653d4eSeschrock 2574fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 2575fa94a07fSbrendan &nspares) != 0) 257699653d4eSeschrock nspares = 0; 257799653d4eSeschrock 2578fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 2579fa94a07fSbrendan &nl2cache) != 0) 2580fa94a07fSbrendan nl2cache = 0; 2581fa94a07fSbrendan 2582fa94a07fSbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 258339c23413Seschrock spa->spa_pending_vdev = NULL; 2584fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 258539c23413Seschrock } 2586fa9e4066Sahrens 258799653d4eSeschrock if (vd->vdev_children != 0) { 258839c23413Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 258939c23413Seschrock spa->spa_pending_vdev = NULL; 259099653d4eSeschrock return (spa_vdev_exit(spa, vd, txg, error)); 259199653d4eSeschrock } 259299653d4eSeschrock } 259399653d4eSeschrock 259439c23413Seschrock /* 2595fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 2596fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 259739c23413Seschrock */ 2598fa94a07fSbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 259939c23413Seschrock spa->spa_pending_vdev = NULL; 260039c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 260139c23413Seschrock } 260239c23413Seschrock 260339c23413Seschrock spa->spa_pending_vdev = NULL; 260439c23413Seschrock 260539c23413Seschrock /* 260639c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 260739c23413Seschrock */ 260839c23413Seschrock for (c = 0; c < vd->vdev_children; c++) { 260939c23413Seschrock tvd = vd->vdev_child[c]; 261039c23413Seschrock vdev_remove_child(vd, tvd); 261139c23413Seschrock tvd->vdev_id = rvd->vdev_children; 261239c23413Seschrock vdev_add_child(rvd, tvd); 261339c23413Seschrock vdev_config_dirty(tvd); 261439c23413Seschrock } 261539c23413Seschrock 261699653d4eSeschrock if (nspares != 0) { 2617fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 2618fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 261999653d4eSeschrock spa_load_spares(spa); 2620fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2621fa94a07fSbrendan } 2622fa94a07fSbrendan 2623fa94a07fSbrendan if (nl2cache != 0) { 2624fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 2625fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 2626fa94a07fSbrendan spa_load_l2cache(spa); 2627fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2628fa9e4066Sahrens } 2629fa9e4066Sahrens 2630fa9e4066Sahrens /* 26310e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 26320e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 26330e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 26340e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 26350e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 26360e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 26370373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 26380e34b6a7Sbonwick * 26390e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 26400e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 26410e34b6a7Sbonwick * steps will be completed the next time we load the pool. 26420e34b6a7Sbonwick */ 26430373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 26440e34b6a7Sbonwick 26450373e76bSbonwick mutex_enter(&spa_namespace_lock); 26460373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26470373e76bSbonwick mutex_exit(&spa_namespace_lock); 2648fa9e4066Sahrens 26490373e76bSbonwick return (0); 2650fa9e4066Sahrens } 2651fa9e4066Sahrens 2652fa9e4066Sahrens /* 2653fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 2654fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2655fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2656fa9e4066Sahrens * 2657fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 2658fa9e4066Sahrens * existing device; in this case the two devices are made into their own 26593d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2660fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2661fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 2662fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 2663fa9e4066Sahrens * is automatically detached. 2664fa9e4066Sahrens */ 2665fa9e4066Sahrens int 2666ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2667fa9e4066Sahrens { 2668fa9e4066Sahrens uint64_t txg, open_txg; 2669fa9e4066Sahrens int error; 2670fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2671fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 267299653d4eSeschrock vdev_ops_t *pvops; 26738654d025Sperrin int is_log; 2674fa9e4066Sahrens 2675fa9e4066Sahrens txg = spa_vdev_enter(spa); 2676fa9e4066Sahrens 2677*c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2678fa9e4066Sahrens 2679fa9e4066Sahrens if (oldvd == NULL) 2680fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2681fa9e4066Sahrens 26820e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 26830e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 26840e34b6a7Sbonwick 2685fa9e4066Sahrens pvd = oldvd->vdev_parent; 2686fa9e4066Sahrens 268799653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 26883d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 26893d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 26903d7072f8Seschrock 26913d7072f8Seschrock if (newrootvd->vdev_children != 1) 2692fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2693fa9e4066Sahrens 2694fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 2695fa9e4066Sahrens 2696fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2697fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2698fa9e4066Sahrens 269999653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2700fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2701fa9e4066Sahrens 27028654d025Sperrin /* 27038654d025Sperrin * Spares can't replace logs 27048654d025Sperrin */ 27058654d025Sperrin is_log = oldvd->vdev_islog; 27068654d025Sperrin if (is_log && newvd->vdev_isspare) 27078654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27088654d025Sperrin 270999653d4eSeschrock if (!replacing) { 271099653d4eSeschrock /* 271199653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 271299653d4eSeschrock * vdev. 271399653d4eSeschrock */ 271499653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 271599653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 271699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 271799653d4eSeschrock 271899653d4eSeschrock pvops = &vdev_mirror_ops; 271999653d4eSeschrock } else { 272099653d4eSeschrock /* 272199653d4eSeschrock * Active hot spares can only be replaced by inactive hot 272299653d4eSeschrock * spares. 272399653d4eSeschrock */ 272499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 272599653d4eSeschrock pvd->vdev_child[1] == oldvd && 272699653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 272799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 272899653d4eSeschrock 272999653d4eSeschrock /* 273099653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 273199653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 273239c23413Seschrock * want to create a replacing vdev. The user is not allowed to 273339c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 273439c23413Seschrock * the same (spare replaces spare, non-spare replaces 273539c23413Seschrock * non-spare). 273699653d4eSeschrock */ 273799653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 273899653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 273939c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 274039c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 274139c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 274299653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 274399653d4eSeschrock newvd->vdev_isspare) 274499653d4eSeschrock pvops = &vdev_spare_ops; 274599653d4eSeschrock else 274699653d4eSeschrock pvops = &vdev_replacing_ops; 274799653d4eSeschrock } 274899653d4eSeschrock 27492a79c5feSlling /* 27502a79c5feSlling * Compare the new device size with the replaceable/attachable 27512a79c5feSlling * device size. 27522a79c5feSlling */ 27532a79c5feSlling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2754fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2755fa9e4066Sahrens 2756ecc2d604Sbonwick /* 2757ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 2758ecc2d604Sbonwick * than the top-level vdev. 2759ecc2d604Sbonwick */ 2760ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2761fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2762fa9e4066Sahrens 2763fa9e4066Sahrens /* 2764fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 2765fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2766fa9e4066Sahrens */ 2767fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2768fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 2769fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2770fa9e4066Sahrens KM_SLEEP); 2771fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2772fa9e4066Sahrens newvd->vdev_path, "old"); 2773fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 2774fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 2775fa9e4066Sahrens oldvd->vdev_devid = NULL; 2776fa9e4066Sahrens } 2777fa9e4066Sahrens } 2778fa9e4066Sahrens 2779fa9e4066Sahrens /* 278099653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 278199653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 2782fa9e4066Sahrens */ 2783fa9e4066Sahrens if (pvd->vdev_ops != pvops) 2784fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 2785fa9e4066Sahrens 2786fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2787fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 2788fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 2789fa9e4066Sahrens 2790fa9e4066Sahrens /* 2791fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 2792fa9e4066Sahrens */ 2793fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 2794fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 2795fa9e4066Sahrens vdev_add_child(pvd, newvd); 2796fa9e4066Sahrens 2797ea8dc4b6Seschrock /* 2798ea8dc4b6Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 2799ea8dc4b6Seschrock * the addition of newvd may have decreased our parent's asize. 2800ea8dc4b6Seschrock */ 2801ea8dc4b6Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 2802ea8dc4b6Seschrock 2803fa9e4066Sahrens tvd = newvd->vdev_top; 2804fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 2805fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2806fa9e4066Sahrens 2807fa9e4066Sahrens vdev_config_dirty(tvd); 2808fa9e4066Sahrens 2809fa9e4066Sahrens /* 2810fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2811fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2812fa9e4066Sahrens */ 2813fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2814fa9e4066Sahrens 2815fa9e4066Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2816fa9e4066Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2817fa9e4066Sahrens open_txg - TXG_INITIAL + 1); 2818fa9e4066Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2819fa9e4066Sahrens 282039c23413Seschrock if (newvd->vdev_isspare) 282139c23413Seschrock spa_spare_activate(newvd); 2822ea8dc4b6Seschrock 2823fa9e4066Sahrens /* 2824fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 2825fa9e4066Sahrens */ 2826ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2827fa9e4066Sahrens 2828fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2829fa9e4066Sahrens 2830fa9e4066Sahrens /* 28313d7072f8Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 28323d7072f8Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 2833fa9e4066Sahrens */ 28343d7072f8Seschrock mutex_enter(&spa_namespace_lock); 2835fa9e4066Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 28363d7072f8Seschrock mutex_exit(&spa_namespace_lock); 2837fa9e4066Sahrens 2838fa9e4066Sahrens return (0); 2839fa9e4066Sahrens } 2840fa9e4066Sahrens 2841fa9e4066Sahrens /* 2842fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 2843fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 2844fa9e4066Sahrens * is a replacing vdev. 2845fa9e4066Sahrens */ 2846fa9e4066Sahrens int 2847ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2848fa9e4066Sahrens { 2849fa9e4066Sahrens uint64_t txg; 2850fa9e4066Sahrens int c, t, error; 2851fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2852fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 285399653d4eSeschrock boolean_t unspare = B_FALSE; 285499653d4eSeschrock uint64_t unspare_guid; 2855fa9e4066Sahrens 2856fa9e4066Sahrens txg = spa_vdev_enter(spa); 2857fa9e4066Sahrens 2858*c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2859fa9e4066Sahrens 2860fa9e4066Sahrens if (vd == NULL) 2861fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2862fa9e4066Sahrens 28630e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 28640e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28650e34b6a7Sbonwick 2866fa9e4066Sahrens pvd = vd->vdev_parent; 2867fa9e4066Sahrens 2868fa9e4066Sahrens /* 2869fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 287099653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 287199653d4eSeschrock * disk can be removed. 287299653d4eSeschrock */ 287399653d4eSeschrock if (replace_done) { 287499653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 287599653d4eSeschrock if (vd->vdev_id != 0) 287699653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 287799653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 287899653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 287999653d4eSeschrock } 288099653d4eSeschrock } 288199653d4eSeschrock 288299653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 2883e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2884fa9e4066Sahrens 2885fa9e4066Sahrens /* 288699653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 2887fa9e4066Sahrens */ 2888fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 288999653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 289099653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 2891fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2892fa9e4066Sahrens 2893fa9e4066Sahrens /* 2894fa9e4066Sahrens * If there's only one replica, you can't detach it. 2895fa9e4066Sahrens */ 2896fa9e4066Sahrens if (pvd->vdev_children <= 1) 2897fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2898fa9e4066Sahrens 2899fa9e4066Sahrens /* 2900fa9e4066Sahrens * If all siblings have non-empty DTLs, this device may have the only 2901fa9e4066Sahrens * valid copy of the data, which means we cannot safely detach it. 2902fa9e4066Sahrens * 2903fa9e4066Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2904fa9e4066Sahrens * precise DTL check. 2905fa9e4066Sahrens */ 2906fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2907fa9e4066Sahrens uint64_t dirty; 2908fa9e4066Sahrens 2909fa9e4066Sahrens cvd = pvd->vdev_child[c]; 2910fa9e4066Sahrens if (cvd == vd) 2911fa9e4066Sahrens continue; 2912fa9e4066Sahrens if (vdev_is_dead(cvd)) 2913fa9e4066Sahrens continue; 2914fa9e4066Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2915fa9e4066Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2916fa9e4066Sahrens cvd->vdev_dtl_scrub.sm_space; 2917fa9e4066Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2918fa9e4066Sahrens if (!dirty) 2919fa9e4066Sahrens break; 2920fa9e4066Sahrens } 292199653d4eSeschrock 292299653d4eSeschrock /* 292399653d4eSeschrock * If we are a replacing or spare vdev, then we can always detach the 292499653d4eSeschrock * latter child, as that is how one cancels the operation. 292599653d4eSeschrock */ 292699653d4eSeschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 292799653d4eSeschrock c == pvd->vdev_children) 2928fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2929fa9e4066Sahrens 293099653d4eSeschrock /* 293199653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 293299653d4eSeschrock * that the spare should become a real disk, and be removed from the 293399653d4eSeschrock * active spare list for the pool. 293499653d4eSeschrock */ 293599653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 293699653d4eSeschrock vd->vdev_id == 0) 293799653d4eSeschrock unspare = B_TRUE; 293899653d4eSeschrock 2939fa9e4066Sahrens /* 2940fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 2941fa9e4066Sahrens * This must be done after all other error cases are handled, 2942fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 2943fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 2944fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 2945fa9e4066Sahrens * it's being detached! 2946fa9e4066Sahrens */ 294739c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2948fa9e4066Sahrens 2949fa9e4066Sahrens /* 2950fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 2951fa9e4066Sahrens */ 2952fa9e4066Sahrens vdev_remove_child(pvd, vd); 2953fa9e4066Sahrens vdev_compact_children(pvd); 2954fa9e4066Sahrens 2955fa9e4066Sahrens /* 2956fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 2957fa9e4066Sahrens */ 2958fa9e4066Sahrens cvd = pvd->vdev_child[0]; 2959fa9e4066Sahrens 296099653d4eSeschrock /* 296199653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 296299653d4eSeschrock * do it now, marking the vdev as no longer a spare in the process. We 296399653d4eSeschrock * must do this before vdev_remove_parent(), because that can change the 296499653d4eSeschrock * GUID if it creates a new toplevel GUID. 296599653d4eSeschrock */ 296699653d4eSeschrock if (unspare) { 296799653d4eSeschrock ASSERT(cvd->vdev_isspare); 296839c23413Seschrock spa_spare_remove(cvd); 296999653d4eSeschrock unspare_guid = cvd->vdev_guid; 297099653d4eSeschrock } 297199653d4eSeschrock 2972fa9e4066Sahrens /* 2973fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 2974fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 2975fa9e4066Sahrens */ 2976fa9e4066Sahrens if (pvd->vdev_children == 1) 2977fa9e4066Sahrens vdev_remove_parent(cvd); 2978fa9e4066Sahrens 2979fa9e4066Sahrens /* 2980fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 2981fa9e4066Sahrens * may have been the previous top-level vdev. 2982fa9e4066Sahrens */ 2983fa9e4066Sahrens tvd = cvd->vdev_top; 2984fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 2985fa9e4066Sahrens 2986fa9e4066Sahrens /* 298739c23413Seschrock * Reevaluate the parent vdev state. 2988fa9e4066Sahrens */ 29893d7072f8Seschrock vdev_propagate_state(cvd); 2990fa9e4066Sahrens 2991fa9e4066Sahrens /* 299239c23413Seschrock * If the device we just detached was smaller than the others, it may be 299339c23413Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 299439c23413Seschrock * can't fail because the existing metaslabs are already in core, so 299539c23413Seschrock * there's nothing to read from disk. 2996fa9e4066Sahrens */ 2997ecc2d604Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2998fa9e4066Sahrens 2999fa9e4066Sahrens vdev_config_dirty(tvd); 3000fa9e4066Sahrens 3001fa9e4066Sahrens /* 300239c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 300339c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 300439c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 300539c23413Seschrock * prevent vd from being accessed after it's freed. 3006fa9e4066Sahrens */ 3007fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) 3008fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 3009ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 3010ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3011fa9e4066Sahrens 30123d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 30133d7072f8Seschrock 301499653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 301599653d4eSeschrock 301699653d4eSeschrock /* 301739c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 301839c23413Seschrock * then we want to go through and remove the device from the hot spare 301939c23413Seschrock * list of every other pool. 302099653d4eSeschrock */ 302199653d4eSeschrock if (unspare) { 302299653d4eSeschrock spa = NULL; 302399653d4eSeschrock mutex_enter(&spa_namespace_lock); 302499653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 302599653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 302699653d4eSeschrock continue; 302799653d4eSeschrock 302899653d4eSeschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 302999653d4eSeschrock } 303099653d4eSeschrock mutex_exit(&spa_namespace_lock); 303199653d4eSeschrock } 303299653d4eSeschrock 303399653d4eSeschrock return (error); 303499653d4eSeschrock } 303599653d4eSeschrock 303699653d4eSeschrock /* 3037fa94a07fSbrendan * Remove a spares vdev from the nvlist config. 303899653d4eSeschrock */ 3039fa94a07fSbrendan static int 3040fa94a07fSbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 3041fa94a07fSbrendan nvlist_t **spares, int nspares, vdev_t *vd) 304299653d4eSeschrock { 3043fa94a07fSbrendan nvlist_t *nv, **newspares; 3044fa94a07fSbrendan int i, j; 304599653d4eSeschrock 304699653d4eSeschrock nv = NULL; 3047fa94a07fSbrendan for (i = 0; i < nspares; i++) { 3048fa94a07fSbrendan uint64_t theguid; 304999653d4eSeschrock 3050fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(spares[i], 3051fa94a07fSbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 3052fa94a07fSbrendan if (theguid == guid) { 3053fa94a07fSbrendan nv = spares[i]; 3054fa94a07fSbrendan break; 305599653d4eSeschrock } 305699653d4eSeschrock } 305799653d4eSeschrock 305899653d4eSeschrock /* 3059fa94a07fSbrendan * Only remove the hot spare if it's not currently in use in this pool. 306099653d4eSeschrock */ 3061fa94a07fSbrendan if (nv == NULL && vd == NULL) 3062fa94a07fSbrendan return (ENOENT); 306399653d4eSeschrock 3064fa94a07fSbrendan if (nv == NULL && vd != NULL) 3065fa94a07fSbrendan return (ENOTSUP); 306699653d4eSeschrock 3067fa94a07fSbrendan if (!unspare && nv != NULL && vd != NULL) 3068fa94a07fSbrendan return (EBUSY); 306999653d4eSeschrock 307099653d4eSeschrock if (nspares == 1) { 307199653d4eSeschrock newspares = NULL; 307299653d4eSeschrock } else { 307399653d4eSeschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 307499653d4eSeschrock KM_SLEEP); 307599653d4eSeschrock for (i = 0, j = 0; i < nspares; i++) { 307699653d4eSeschrock if (spares[i] != nv) 307799653d4eSeschrock VERIFY(nvlist_dup(spares[i], 307899653d4eSeschrock &newspares[j++], KM_SLEEP) == 0); 307999653d4eSeschrock } 308099653d4eSeschrock } 308199653d4eSeschrock 3082fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 308399653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 3084fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3085fa94a07fSbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 308699653d4eSeschrock for (i = 0; i < nspares - 1; i++) 308799653d4eSeschrock nvlist_free(newspares[i]); 308899653d4eSeschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 3089fa94a07fSbrendan 3090fa94a07fSbrendan return (0); 3091fa94a07fSbrendan } 3092fa94a07fSbrendan 3093fa94a07fSbrendan /* 3094fa94a07fSbrendan * Remove an l2cache vdev from the nvlist config. 3095fa94a07fSbrendan */ 3096fa94a07fSbrendan static int 3097fa94a07fSbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 3098fa94a07fSbrendan int nl2cache, vdev_t *vd) 3099fa94a07fSbrendan { 3100fa94a07fSbrendan nvlist_t *nv, **newl2cache; 3101fa94a07fSbrendan int i, j; 3102fa94a07fSbrendan 3103fa94a07fSbrendan nv = NULL; 3104fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3105fa94a07fSbrendan uint64_t theguid; 3106fa94a07fSbrendan 3107fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3108fa94a07fSbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 3109fa94a07fSbrendan if (theguid == guid) { 3110fa94a07fSbrendan nv = l2cache[i]; 3111fa94a07fSbrendan break; 3112fa94a07fSbrendan } 3113fa94a07fSbrendan } 3114fa94a07fSbrendan 3115fa94a07fSbrendan if (vd == NULL) { 3116fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3117fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 3118fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3119fa94a07fSbrendan break; 3120fa94a07fSbrendan } 3121fa94a07fSbrendan } 3122fa94a07fSbrendan } 3123fa94a07fSbrendan 3124fa94a07fSbrendan if (nv == NULL && vd == NULL) 3125fa94a07fSbrendan return (ENOENT); 3126fa94a07fSbrendan 3127fa94a07fSbrendan if (nv == NULL && vd != NULL) 3128fa94a07fSbrendan return (ENOTSUP); 3129fa94a07fSbrendan 3130fa94a07fSbrendan if (nl2cache == 1) { 3131fa94a07fSbrendan newl2cache = NULL; 3132fa94a07fSbrendan } else { 3133fa94a07fSbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 3134fa94a07fSbrendan KM_SLEEP); 3135fa94a07fSbrendan for (i = 0, j = 0; i < nl2cache; i++) { 3136fa94a07fSbrendan if (l2cache[i] != nv) 3137fa94a07fSbrendan VERIFY(nvlist_dup(l2cache[i], 3138fa94a07fSbrendan &newl2cache[j++], KM_SLEEP) == 0); 3139fa94a07fSbrendan } 3140fa94a07fSbrendan } 3141fa94a07fSbrendan 3142fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 3143fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3144fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3145fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 3146fa94a07fSbrendan for (i = 0; i < nl2cache - 1; i++) 3147fa94a07fSbrendan nvlist_free(newl2cache[i]); 3148fa94a07fSbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 3149fa94a07fSbrendan 3150fa94a07fSbrendan return (0); 3151fa94a07fSbrendan } 3152fa94a07fSbrendan 3153fa94a07fSbrendan /* 3154fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 3155fa94a07fSbrendan * spares and level 2 ARC devices. 3156fa94a07fSbrendan */ 3157fa94a07fSbrendan int 3158fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 3159fa94a07fSbrendan { 3160fa94a07fSbrendan vdev_t *vd; 3161fa94a07fSbrendan nvlist_t **spares, **l2cache; 3162fa94a07fSbrendan uint_t nspares, nl2cache; 3163fa94a07fSbrendan int error = 0; 3164fa94a07fSbrendan 3165fa94a07fSbrendan spa_config_enter(spa, RW_WRITER, FTAG); 3166fa94a07fSbrendan 3167*c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3168fa94a07fSbrendan 3169fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 3170fa94a07fSbrendan spa_spare_exists(guid, NULL) && 3171fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 3172fa94a07fSbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 3173fa94a07fSbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 3174fa94a07fSbrendan spares, nspares, vd)) != 0) 3175fa94a07fSbrendan goto out; 3176fa94a07fSbrendan spa_load_spares(spa); 3177fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3178fa94a07fSbrendan goto out; 3179fa94a07fSbrendan } 3180fa94a07fSbrendan 3181fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 3182fa94a07fSbrendan spa_l2cache_exists(guid, NULL) && 3183fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3184fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 3185fa94a07fSbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 3186fa94a07fSbrendan l2cache, nl2cache, vd)) != 0) 3187fa94a07fSbrendan goto out; 3188fa94a07fSbrendan spa_load_l2cache(spa); 3189fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3190fa94a07fSbrendan } 319199653d4eSeschrock 319299653d4eSeschrock out: 319399653d4eSeschrock spa_config_exit(spa, FTAG); 3194fa94a07fSbrendan return (error); 3195fa9e4066Sahrens } 3196fa9e4066Sahrens 3197fa9e4066Sahrens /* 31983d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 31993d7072f8Seschrock * current spared, so we can detach it. 3200fa9e4066Sahrens */ 3201ea8dc4b6Seschrock static vdev_t * 32023d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3203fa9e4066Sahrens { 3204ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 3205fa9e4066Sahrens int c; 3206fa9e4066Sahrens 3207ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) { 32083d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 3209ea8dc4b6Seschrock if (oldvd != NULL) 3210ea8dc4b6Seschrock return (oldvd); 3211ea8dc4b6Seschrock } 3212fa9e4066Sahrens 32133d7072f8Seschrock /* 32143d7072f8Seschrock * Check for a completed replacement. 32153d7072f8Seschrock */ 3216fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 3217ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 3218ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 3219ea8dc4b6Seschrock 3220ea8dc4b6Seschrock mutex_enter(&newvd->vdev_dtl_lock); 3221ea8dc4b6Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 3222ea8dc4b6Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 3223ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 3224ea8dc4b6Seschrock return (oldvd); 3225fa9e4066Sahrens } 3226ea8dc4b6Seschrock mutex_exit(&newvd->vdev_dtl_lock); 3227fa9e4066Sahrens } 3228ea8dc4b6Seschrock 32293d7072f8Seschrock /* 32303d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 32313d7072f8Seschrock */ 32323d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 32333d7072f8Seschrock newvd = vd->vdev_child[0]; 32343d7072f8Seschrock oldvd = vd->vdev_child[1]; 32353d7072f8Seschrock 32363d7072f8Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32373d7072f8Seschrock if (newvd->vdev_unspare && 32383d7072f8Seschrock newvd->vdev_dtl_map.sm_space == 0 && 32393d7072f8Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32403d7072f8Seschrock newvd->vdev_unspare = 0; 32413d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32423d7072f8Seschrock return (oldvd); 32433d7072f8Seschrock } 32443d7072f8Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32453d7072f8Seschrock } 32463d7072f8Seschrock 3247ea8dc4b6Seschrock return (NULL); 3248fa9e4066Sahrens } 3249fa9e4066Sahrens 3250ea8dc4b6Seschrock static void 32513d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 3252fa9e4066Sahrens { 3253ea8dc4b6Seschrock vdev_t *vd; 325499653d4eSeschrock vdev_t *pvd; 3255ea8dc4b6Seschrock uint64_t guid; 325699653d4eSeschrock uint64_t pguid = 0; 3257ea8dc4b6Seschrock 3258ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 3259ea8dc4b6Seschrock 32603d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 3261ea8dc4b6Seschrock guid = vd->vdev_guid; 326299653d4eSeschrock /* 326399653d4eSeschrock * If we have just finished replacing a hot spared device, then 326499653d4eSeschrock * we need to detach the parent's first child (the original hot 326599653d4eSeschrock * spare) as well. 326699653d4eSeschrock */ 326799653d4eSeschrock pvd = vd->vdev_parent; 326899653d4eSeschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 326999653d4eSeschrock pvd->vdev_id == 0) { 327099653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 327199653d4eSeschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 327299653d4eSeschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 327399653d4eSeschrock } 3274ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3275ea8dc4b6Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 3276ea8dc4b6Seschrock return; 327799653d4eSeschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 327899653d4eSeschrock return; 3279ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 3280fa9e4066Sahrens } 3281fa9e4066Sahrens 3282ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3283fa9e4066Sahrens } 3284fa9e4066Sahrens 3285c67d9675Seschrock /* 3286c67d9675Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 3287c67d9675Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 3288c67d9675Seschrock */ 3289c67d9675Seschrock int 3290c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 3291c67d9675Seschrock { 3292*c5904d13Seschrock vdev_t *vd; 3293c67d9675Seschrock uint64_t txg; 3294c67d9675Seschrock 3295c67d9675Seschrock txg = spa_vdev_enter(spa); 3296c67d9675Seschrock 3297*c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 329899653d4eSeschrock /* 3299*c5904d13Seschrock * Determine if this is a reference to a hot spare device. If 3300*c5904d13Seschrock * it is, update the path manually as there is no associated 3301*c5904d13Seschrock * vdev_t that can be synced to disk. 330299653d4eSeschrock */ 3303*c5904d13Seschrock nvlist_t **spares; 3304*c5904d13Seschrock uint_t i, nspares; 3305fa94a07fSbrendan 3306fa94a07fSbrendan if (spa->spa_spares.sav_config != NULL) { 3307fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array( 3308fa94a07fSbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 3309fa94a07fSbrendan &spares, &nspares) == 0); 331099653d4eSeschrock for (i = 0; i < nspares; i++) { 331199653d4eSeschrock uint64_t theguid; 331299653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 331399653d4eSeschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 3314fa94a07fSbrendan if (theguid == guid) { 3315fa94a07fSbrendan VERIFY(nvlist_add_string(spares[i], 3316fa94a07fSbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 3317fa94a07fSbrendan spa_load_spares(spa); 3318fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3319fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, 3320fa94a07fSbrendan 0)); 3321fa94a07fSbrendan } 332299653d4eSeschrock } 3323fa94a07fSbrendan } 332499653d4eSeschrock 3325fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 332699653d4eSeschrock } 3327c67d9675Seschrock 33280e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 33290e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33300e34b6a7Sbonwick 3331c67d9675Seschrock spa_strfree(vd->vdev_path); 3332c67d9675Seschrock vd->vdev_path = spa_strdup(newpath); 3333c67d9675Seschrock 3334c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 3335c67d9675Seschrock 3336c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 3337c67d9675Seschrock } 3338c67d9675Seschrock 3339fa9e4066Sahrens /* 3340fa9e4066Sahrens * ========================================================================== 3341fa9e4066Sahrens * SPA Scrubbing 3342fa9e4066Sahrens * ========================================================================== 3343fa9e4066Sahrens */ 3344fa9e4066Sahrens 3345fa9e4066Sahrens static void 3346fa9e4066Sahrens spa_scrub_io_done(zio_t *zio) 3347fa9e4066Sahrens { 3348fa9e4066Sahrens spa_t *spa = zio->io_spa; 3349fa9e4066Sahrens 33500e8c6158Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 3351fa9e4066Sahrens 3352fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3353ea8dc4b6Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 335444cd46caSbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 3355ea8dc4b6Seschrock spa->spa_scrub_errors++; 3356fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3357fa9e4066Sahrens vd->vdev_stat.vs_scrub_errors++; 3358fa9e4066Sahrens mutex_exit(&vd->vdev_stat_lock); 3359fa9e4066Sahrens } 336005b2b3b8Smishra 336105b2b3b8Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 3362ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 336305b2b3b8Smishra 336405b2b3b8Smishra ASSERT(spa->spa_scrub_inflight >= 0); 336505b2b3b8Smishra 3366ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3367fa9e4066Sahrens } 3368fa9e4066Sahrens 3369fa9e4066Sahrens static void 3370ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 3371ea8dc4b6Seschrock zbookmark_t *zb) 3372fa9e4066Sahrens { 3373fa9e4066Sahrens size_t size = BP_GET_LSIZE(bp); 337405b2b3b8Smishra void *data; 3375fa9e4066Sahrens 3376fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 337705b2b3b8Smishra /* 337805b2b3b8Smishra * Do not give too much work to vdev(s). 337905b2b3b8Smishra */ 338005b2b3b8Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 338105b2b3b8Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 338205b2b3b8Smishra } 3383fa9e4066Sahrens spa->spa_scrub_inflight++; 3384fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3385fa9e4066Sahrens 33860e8c6158Smaybee data = arc_data_buf_alloc(size); 338705b2b3b8Smishra 3388ea8dc4b6Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 3389ea8dc4b6Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 3390ea8dc4b6Seschrock 3391d80c45e0Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 3392ea8dc4b6Seschrock 3393fa9e4066Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 3394ea8dc4b6Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 3395fa9e4066Sahrens } 3396fa9e4066Sahrens 3397fa9e4066Sahrens /* ARGSUSED */ 3398fa9e4066Sahrens static int 3399fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 3400fa9e4066Sahrens { 3401fa9e4066Sahrens blkptr_t *bp = &bc->bc_blkptr; 340244cd46caSbillm vdev_t *vd = spa->spa_root_vdev; 340344cd46caSbillm dva_t *dva = bp->blk_dva; 340444cd46caSbillm int needs_resilver = B_FALSE; 340544cd46caSbillm int d; 3406fa9e4066Sahrens 340744cd46caSbillm if (bc->bc_errno) { 3408fa9e4066Sahrens /* 3409fa9e4066Sahrens * We can't scrub this block, but we can continue to scrub 3410fa9e4066Sahrens * the rest of the pool. Note the error and move along. 3411fa9e4066Sahrens */ 3412fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3413fa9e4066Sahrens spa->spa_scrub_errors++; 3414fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3415fa9e4066Sahrens 341644cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 341744cd46caSbillm vd->vdev_stat.vs_scrub_errors++; 341844cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 3419fa9e4066Sahrens 3420fa9e4066Sahrens return (ERESTART); 3421fa9e4066Sahrens } 3422fa9e4066Sahrens 3423fa9e4066Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 3424fa9e4066Sahrens 342544cd46caSbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 342644cd46caSbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 3427fa9e4066Sahrens 342844cd46caSbillm ASSERT(vd != NULL); 342944cd46caSbillm 343044cd46caSbillm /* 343144cd46caSbillm * Keep track of how much data we've examined so that 343244cd46caSbillm * zpool(1M) status can make useful progress reports. 343344cd46caSbillm */ 343444cd46caSbillm mutex_enter(&vd->vdev_stat_lock); 343544cd46caSbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 343644cd46caSbillm mutex_exit(&vd->vdev_stat_lock); 343744cd46caSbillm 343844cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 343944cd46caSbillm if (DVA_GET_GANG(&dva[d])) { 344044cd46caSbillm /* 344144cd46caSbillm * Gang members may be spread across multiple 344244cd46caSbillm * vdevs, so the best we can do is look at the 344344cd46caSbillm * pool-wide DTL. 344444cd46caSbillm * XXX -- it would be better to change our 344544cd46caSbillm * allocation policy to ensure that this can't 344644cd46caSbillm * happen. 344744cd46caSbillm */ 344844cd46caSbillm vd = spa->spa_root_vdev; 344944cd46caSbillm } 345044cd46caSbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 345144cd46caSbillm bp->blk_birth, 1)) 345244cd46caSbillm needs_resilver = B_TRUE; 3453fa9e4066Sahrens } 345444cd46caSbillm } 345544cd46caSbillm 345644cd46caSbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 3457fa9e4066Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 3458ea8dc4b6Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 345944cd46caSbillm else if (needs_resilver) 346044cd46caSbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 346144cd46caSbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 3462fa9e4066Sahrens 3463fa9e4066Sahrens return (0); 3464fa9e4066Sahrens } 3465fa9e4066Sahrens 3466fa9e4066Sahrens static void 3467fa9e4066Sahrens spa_scrub_thread(spa_t *spa) 3468fa9e4066Sahrens { 3469fa9e4066Sahrens callb_cpr_t cprinfo; 3470fa9e4066Sahrens traverse_handle_t *th = spa->spa_scrub_th; 3471fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3472fa9e4066Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 3473fa9e4066Sahrens int error = 0; 3474fa9e4066Sahrens boolean_t complete; 3475fa9e4066Sahrens 3476fa9e4066Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 3477fa9e4066Sahrens 3478f0aa80d4Sbonwick /* 3479f0aa80d4Sbonwick * If we're restarting due to a snapshot create/delete, 3480f0aa80d4Sbonwick * wait for that to complete. 3481f0aa80d4Sbonwick */ 3482f0aa80d4Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 3483f0aa80d4Sbonwick 3484ea8dc4b6Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 3485ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 3486ea8dc4b6Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 3487ea8dc4b6Seschrock 3488ea8dc4b6Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 3489ea8dc4b6Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 3490fa9e4066Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 3491fa9e4066Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 3492ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3493fa9e4066Sahrens 3494fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3495fa9e4066Sahrens spa->spa_scrub_errors = 0; 3496fa9e4066Sahrens spa->spa_scrub_active = 1; 3497ea8dc4b6Seschrock ASSERT(spa->spa_scrub_inflight == 0); 3498fa9e4066Sahrens 3499fa9e4066Sahrens while (!spa->spa_scrub_stop) { 3500fa9e4066Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 3501ea8dc4b6Seschrock while (spa->spa_scrub_suspended) { 3502fa9e4066Sahrens spa->spa_scrub_active = 0; 3503fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3504fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3505fa9e4066Sahrens spa->spa_scrub_active = 1; 3506fa9e4066Sahrens } 3507fa9e4066Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 3508fa9e4066Sahrens 3509fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 3510fa9e4066Sahrens break; 3511fa9e4066Sahrens 3512fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3513fa9e4066Sahrens error = traverse_more(th); 3514fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3515fa9e4066Sahrens if (error != EAGAIN) 3516fa9e4066Sahrens break; 3517fa9e4066Sahrens } 3518fa9e4066Sahrens 3519fa9e4066Sahrens while (spa->spa_scrub_inflight) 3520fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3521fa9e4066Sahrens 35225dabedeeSbonwick spa->spa_scrub_active = 0; 35235dabedeeSbonwick cv_broadcast(&spa->spa_scrub_cv); 35245dabedeeSbonwick 35255dabedeeSbonwick mutex_exit(&spa->spa_scrub_lock); 35265dabedeeSbonwick 35275dabedeeSbonwick spa_config_enter(spa, RW_WRITER, FTAG); 35285dabedeeSbonwick 35295dabedeeSbonwick mutex_enter(&spa->spa_scrub_lock); 35305dabedeeSbonwick 35315dabedeeSbonwick /* 35325dabedeeSbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 35335dabedeeSbonwick * AND the spa config lock to synchronize with any config changes 35345dabedeeSbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 35355dabedeeSbonwick */ 3536fa9e4066Sahrens if (spa->spa_scrub_restart_txg != 0) 3537fa9e4066Sahrens error = ERESTART; 3538fa9e4066Sahrens 3539ea8dc4b6Seschrock if (spa->spa_scrub_stop) 3540ea8dc4b6Seschrock error = EINTR; 3541ea8dc4b6Seschrock 3542fa9e4066Sahrens /* 3543ea8dc4b6Seschrock * Even if there were uncorrectable errors, we consider the scrub 3544ea8dc4b6Seschrock * completed. The downside is that if there is a transient error during 3545ea8dc4b6Seschrock * a resilver, we won't resilver the data properly to the target. But 3546ea8dc4b6Seschrock * if the damage is permanent (more likely) we will resilver forever, 3547ea8dc4b6Seschrock * which isn't really acceptable. Since there is enough information for 3548ea8dc4b6Seschrock * the user to know what has failed and why, this seems like a more 3549ea8dc4b6Seschrock * tractable approach. 3550fa9e4066Sahrens */ 3551ea8dc4b6Seschrock complete = (error == 0); 3552fa9e4066Sahrens 3553ea8dc4b6Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 3554ea8dc4b6Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 3555fa9e4066Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 3556fa9e4066Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 3557fa9e4066Sahrens 3558fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3559fa9e4066Sahrens 3560fa9e4066Sahrens /* 3561fa9e4066Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 3562fa9e4066Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 3563fa9e4066Sahrens */ 3564fa9e4066Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 3565fa9e4066Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 3566fa9e4066Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 3567ea8dc4b6Seschrock spa_errlog_rotate(spa); 35685dabedeeSbonwick 35693d7072f8Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 35703d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 35713d7072f8Seschrock 3572ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 3573fa9e4066Sahrens 3574fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3575fa9e4066Sahrens 3576ea8dc4b6Seschrock /* 3577ea8dc4b6Seschrock * We may have finished replacing a device. 3578ea8dc4b6Seschrock * Let the async thread assess this and handle the detach. 3579ea8dc4b6Seschrock */ 35803d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3581fa9e4066Sahrens 3582fa9e4066Sahrens /* 3583fa9e4066Sahrens * If we were told to restart, our final act is to start a new scrub. 3584fa9e4066Sahrens */ 3585fa9e4066Sahrens if (error == ERESTART) 3586ea8dc4b6Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 3587ea8dc4b6Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 3588fa9e4066Sahrens 3589ea8dc4b6Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 3590ea8dc4b6Seschrock spa->spa_scrub_active = 0; 3591ea8dc4b6Seschrock spa->spa_scrub_thread = NULL; 3592ea8dc4b6Seschrock cv_broadcast(&spa->spa_scrub_cv); 3593fa9e4066Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 3594fa9e4066Sahrens thread_exit(); 3595fa9e4066Sahrens } 3596fa9e4066Sahrens 3597fa9e4066Sahrens void 3598fa9e4066Sahrens spa_scrub_suspend(spa_t *spa) 3599fa9e4066Sahrens { 3600fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3601ea8dc4b6Seschrock spa->spa_scrub_suspended++; 3602fa9e4066Sahrens while (spa->spa_scrub_active) { 3603fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3604fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3605fa9e4066Sahrens } 3606fa9e4066Sahrens while (spa->spa_scrub_inflight) 3607fa9e4066Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3608fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3609fa9e4066Sahrens } 3610fa9e4066Sahrens 3611fa9e4066Sahrens void 3612fa9e4066Sahrens spa_scrub_resume(spa_t *spa) 3613fa9e4066Sahrens { 3614fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3615ea8dc4b6Seschrock ASSERT(spa->spa_scrub_suspended != 0); 3616ea8dc4b6Seschrock if (--spa->spa_scrub_suspended == 0) 3617fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3618fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3619fa9e4066Sahrens } 3620fa9e4066Sahrens 3621fa9e4066Sahrens void 3622fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 3623fa9e4066Sahrens { 3624fa9e4066Sahrens /* 3625fa9e4066Sahrens * Something happened (e.g. snapshot create/delete) that means 3626fa9e4066Sahrens * we must restart any in-progress scrubs. The itinerary will 3627fa9e4066Sahrens * fix this properly. 3628fa9e4066Sahrens */ 3629fa9e4066Sahrens mutex_enter(&spa->spa_scrub_lock); 3630fa9e4066Sahrens spa->spa_scrub_restart_txg = txg; 3631fa9e4066Sahrens mutex_exit(&spa->spa_scrub_lock); 3632fa9e4066Sahrens } 3633fa9e4066Sahrens 3634ea8dc4b6Seschrock int 3635ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 3636fa9e4066Sahrens { 3637fa9e4066Sahrens space_seg_t *ss; 3638fa9e4066Sahrens uint64_t mintxg, maxtxg; 3639fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3640fa9e4066Sahrens 3641bb8b5132Sek ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3642bb8b5132Sek ASSERT(!spa_config_held(spa, RW_WRITER)); 3643bb8b5132Sek 3644fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3645fa9e4066Sahrens return (ENOTSUP); 3646fa9e4066Sahrens 3647ea8dc4b6Seschrock mutex_enter(&spa->spa_scrub_lock); 3648ea8dc4b6Seschrock 3649fa9e4066Sahrens /* 3650fa9e4066Sahrens * If there's a scrub or resilver already in progress, stop it. 3651fa9e4066Sahrens */ 3652fa9e4066Sahrens while (spa->spa_scrub_thread != NULL) { 3653fa9e4066Sahrens /* 3654fa9e4066Sahrens * Don't stop a resilver unless forced. 3655fa9e4066Sahrens */ 3656ea8dc4b6Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 3657ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3658fa9e4066Sahrens return (EBUSY); 3659ea8dc4b6Seschrock } 3660fa9e4066Sahrens spa->spa_scrub_stop = 1; 3661fa9e4066Sahrens cv_broadcast(&spa->spa_scrub_cv); 3662fa9e4066Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3663fa9e4066Sahrens } 3664fa9e4066Sahrens 3665fa9e4066Sahrens /* 3666fa9e4066Sahrens * Terminate the previous traverse. 3667fa9e4066Sahrens */ 3668fa9e4066Sahrens if (spa->spa_scrub_th != NULL) { 3669fa9e4066Sahrens traverse_fini(spa->spa_scrub_th); 3670fa9e4066Sahrens spa->spa_scrub_th = NULL; 3671fa9e4066Sahrens } 3672fa9e4066Sahrens 3673ea8dc4b6Seschrock if (rvd == NULL) { 3674ea8dc4b6Seschrock ASSERT(spa->spa_scrub_stop == 0); 3675ea8dc4b6Seschrock ASSERT(spa->spa_scrub_type == type); 3676ea8dc4b6Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 3677ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3678ea8dc4b6Seschrock return (0); 3679ea8dc4b6Seschrock } 3680fa9e4066Sahrens 3681fa9e4066Sahrens mintxg = TXG_INITIAL - 1; 3682fa9e4066Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 3683fa9e4066Sahrens 3684ea8dc4b6Seschrock mutex_enter(&rvd->vdev_dtl_lock); 3685fa9e4066Sahrens 3686ea8dc4b6Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 3687ea8dc4b6Seschrock /* 3688ea8dc4b6Seschrock * The pool-wide DTL is empty. 3689ecc2d604Sbonwick * If this is a resilver, there's nothing to do except 3690ecc2d604Sbonwick * check whether any in-progress replacements have completed. 3691ea8dc4b6Seschrock */ 3692ecc2d604Sbonwick if (type == POOL_SCRUB_RESILVER) { 3693ea8dc4b6Seschrock type = POOL_SCRUB_NONE; 36943d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3695ecc2d604Sbonwick } 3696ea8dc4b6Seschrock } else { 3697ea8dc4b6Seschrock /* 3698ea8dc4b6Seschrock * The pool-wide DTL is non-empty. 3699ea8dc4b6Seschrock * If this is a normal scrub, upgrade to a resilver instead. 3700ea8dc4b6Seschrock */ 3701ea8dc4b6Seschrock if (type == POOL_SCRUB_EVERYTHING) 3702ea8dc4b6Seschrock type = POOL_SCRUB_RESILVER; 3703ea8dc4b6Seschrock } 3704fa9e4066Sahrens 3705ea8dc4b6Seschrock if (type == POOL_SCRUB_RESILVER) { 3706fa9e4066Sahrens /* 3707fa9e4066Sahrens * Determine the resilvering boundaries. 3708fa9e4066Sahrens * 3709fa9e4066Sahrens * Note: (mintxg, maxtxg) is an open interval, 3710fa9e4066Sahrens * i.e. mintxg and maxtxg themselves are not included. 3711fa9e4066Sahrens * 3712fa9e4066Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 3713fa9e4066Sahrens * so we don't claim to resilver a txg that's still changing. 3714fa9e4066Sahrens */ 3715fa9e4066Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 3716ea8dc4b6Seschrock mintxg = ss->ss_start - 1; 3717fa9e4066Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 3718ea8dc4b6Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 37193d7072f8Seschrock 37203d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 3721fa9e4066Sahrens } 3722fa9e4066Sahrens 3723ea8dc4b6Seschrock mutex_exit(&rvd->vdev_dtl_lock); 3724ea8dc4b6Seschrock 3725ea8dc4b6Seschrock spa->spa_scrub_stop = 0; 3726ea8dc4b6Seschrock spa->spa_scrub_type = type; 3727ea8dc4b6Seschrock spa->spa_scrub_restart_txg = 0; 3728ea8dc4b6Seschrock 3729ea8dc4b6Seschrock if (type != POOL_SCRUB_NONE) { 3730ea8dc4b6Seschrock spa->spa_scrub_mintxg = mintxg; 3731fa9e4066Sahrens spa->spa_scrub_maxtxg = maxtxg; 3732fa9e4066Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 37330373e76bSbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 37340373e76bSbonwick ZIO_FLAG_CANFAIL); 3735fa9e4066Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 3736fa9e4066Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 3737fa9e4066Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 3738fa9e4066Sahrens } 3739fa9e4066Sahrens 3740ea8dc4b6Seschrock mutex_exit(&spa->spa_scrub_lock); 3741ea8dc4b6Seschrock 3742fa9e4066Sahrens return (0); 3743fa9e4066Sahrens } 3744fa9e4066Sahrens 3745ea8dc4b6Seschrock /* 3746ea8dc4b6Seschrock * ========================================================================== 3747ea8dc4b6Seschrock * SPA async task processing 3748ea8dc4b6Seschrock * ========================================================================== 3749ea8dc4b6Seschrock */ 3750ea8dc4b6Seschrock 3751ea8dc4b6Seschrock static void 37523d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3753fa9e4066Sahrens { 3754ea8dc4b6Seschrock vdev_t *tvd; 3755ea8dc4b6Seschrock int c; 3756fa9e4066Sahrens 37573d7072f8Seschrock for (c = 0; c < vd->vdev_children; c++) { 37583d7072f8Seschrock tvd = vd->vdev_child[c]; 37593d7072f8Seschrock if (tvd->vdev_remove_wanted) { 37603d7072f8Seschrock tvd->vdev_remove_wanted = 0; 37613d7072f8Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 37623d7072f8Seschrock VDEV_AUX_NONE); 37630a4e9518Sgw vdev_clear(spa, tvd, B_TRUE); 37643d7072f8Seschrock vdev_config_dirty(tvd->vdev_top); 3765ea8dc4b6Seschrock } 37663d7072f8Seschrock spa_async_remove(spa, tvd); 3767ea8dc4b6Seschrock } 3768ea8dc4b6Seschrock } 3769fa9e4066Sahrens 3770ea8dc4b6Seschrock static void 3771ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 3772ea8dc4b6Seschrock { 3773ea8dc4b6Seschrock int tasks; 37743d7072f8Seschrock uint64_t txg; 3775ea8dc4b6Seschrock 3776ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 3777ea8dc4b6Seschrock 3778ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3779ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 3780ea8dc4b6Seschrock spa->spa_async_tasks = 0; 3781ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3782ea8dc4b6Seschrock 37830373e76bSbonwick /* 37840373e76bSbonwick * See if the config needs to be updated. 37850373e76bSbonwick */ 37860373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 37870373e76bSbonwick mutex_enter(&spa_namespace_lock); 37880373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37890373e76bSbonwick mutex_exit(&spa_namespace_lock); 37900373e76bSbonwick } 37910373e76bSbonwick 3792ea8dc4b6Seschrock /* 37933d7072f8Seschrock * See if any devices need to be marked REMOVED. 37940a4e9518Sgw * 37950a4e9518Sgw * XXX - We avoid doing this when we are in 37960a4e9518Sgw * I/O failure state since spa_vdev_enter() grabs 37970a4e9518Sgw * the namespace lock and would not be able to obtain 37980a4e9518Sgw * the writer config lock. 3799ea8dc4b6Seschrock */ 38000a4e9518Sgw if (tasks & SPA_ASYNC_REMOVE && 38010a4e9518Sgw spa_state(spa) != POOL_STATE_IO_FAILURE) { 38023d7072f8Seschrock txg = spa_vdev_enter(spa); 38033d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 38043d7072f8Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 38053d7072f8Seschrock } 3806ea8dc4b6Seschrock 3807ea8dc4b6Seschrock /* 3808ea8dc4b6Seschrock * If any devices are done replacing, detach them. 3809ea8dc4b6Seschrock */ 38103d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 38113d7072f8Seschrock spa_vdev_resilver_done(spa); 3812fa9e4066Sahrens 3813ea8dc4b6Seschrock /* 38143d7072f8Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 38153d7072f8Seschrock * scrub which can become a resilver), we need to hold 38163d7072f8Seschrock * spa_namespace_lock() because the sysevent we post via 38173d7072f8Seschrock * spa_event_notify() needs to get the name of the pool. 3818ea8dc4b6Seschrock */ 38193d7072f8Seschrock if (tasks & SPA_ASYNC_SCRUB) { 38203d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3821ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 38223d7072f8Seschrock mutex_exit(&spa_namespace_lock); 38233d7072f8Seschrock } 3824ea8dc4b6Seschrock 3825ea8dc4b6Seschrock /* 3826ea8dc4b6Seschrock * Kick off a resilver. 3827ea8dc4b6Seschrock */ 38283d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER) { 38293d7072f8Seschrock mutex_enter(&spa_namespace_lock); 3830ea8dc4b6Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 38313d7072f8Seschrock mutex_exit(&spa_namespace_lock); 38323d7072f8Seschrock } 3833ea8dc4b6Seschrock 3834ea8dc4b6Seschrock /* 3835ea8dc4b6Seschrock * Let the world know that we're done. 3836ea8dc4b6Seschrock */ 3837ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3838ea8dc4b6Seschrock spa->spa_async_thread = NULL; 3839ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 3840ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3841ea8dc4b6Seschrock thread_exit(); 3842ea8dc4b6Seschrock } 3843ea8dc4b6Seschrock 3844ea8dc4b6Seschrock void 3845ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 3846ea8dc4b6Seschrock { 3847ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3848ea8dc4b6Seschrock spa->spa_async_suspended++; 3849ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 3850ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 3851ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3852ea8dc4b6Seschrock } 3853ea8dc4b6Seschrock 3854ea8dc4b6Seschrock void 3855ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 3856ea8dc4b6Seschrock { 3857ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3858ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 3859ea8dc4b6Seschrock spa->spa_async_suspended--; 3860ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3861ea8dc4b6Seschrock } 3862ea8dc4b6Seschrock 3863ea8dc4b6Seschrock static void 3864ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 3865ea8dc4b6Seschrock { 3866ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3867ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38680373e76bSbonwick spa->spa_async_thread == NULL && 38690373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 3870ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 3871ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 3872ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3873ea8dc4b6Seschrock } 3874ea8dc4b6Seschrock 3875ea8dc4b6Seschrock void 3876ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 3877ea8dc4b6Seschrock { 3878ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3879ea8dc4b6Seschrock spa->spa_async_tasks |= task; 3880ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3881fa9e4066Sahrens } 3882fa9e4066Sahrens 3883fa9e4066Sahrens /* 3884fa9e4066Sahrens * ========================================================================== 3885fa9e4066Sahrens * SPA syncing routines 3886fa9e4066Sahrens * ========================================================================== 3887fa9e4066Sahrens */ 3888fa9e4066Sahrens 3889fa9e4066Sahrens static void 3890fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3891fa9e4066Sahrens { 3892fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3893fa9e4066Sahrens dmu_tx_t *tx; 3894fa9e4066Sahrens blkptr_t blk; 3895fa9e4066Sahrens uint64_t itor = 0; 3896fa9e4066Sahrens zio_t *zio; 3897fa9e4066Sahrens int error; 3898fa9e4066Sahrens uint8_t c = 1; 3899fa9e4066Sahrens 3900fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3901fa9e4066Sahrens 3902fa9e4066Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3903fa9e4066Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3904fa9e4066Sahrens 3905fa9e4066Sahrens error = zio_wait(zio); 3906fa9e4066Sahrens ASSERT3U(error, ==, 0); 3907fa9e4066Sahrens 3908fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3909fa9e4066Sahrens bplist_vacate(bpl, tx); 3910fa9e4066Sahrens 3911fa9e4066Sahrens /* 3912fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 3913fa9e4066Sahrens * (Usually only the first block is needed.) 3914fa9e4066Sahrens */ 3915fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3916fa9e4066Sahrens dmu_tx_commit(tx); 3917fa9e4066Sahrens } 3918fa9e4066Sahrens 3919fa9e4066Sahrens static void 392099653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 3921fa9e4066Sahrens { 3922fa9e4066Sahrens char *packed = NULL; 3923fa9e4066Sahrens size_t nvsize = 0; 3924fa9e4066Sahrens dmu_buf_t *db; 3925fa9e4066Sahrens 392699653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 3927fa9e4066Sahrens 3928fa9e4066Sahrens packed = kmem_alloc(nvsize, KM_SLEEP); 3929fa9e4066Sahrens 393099653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 3931ea8dc4b6Seschrock KM_SLEEP) == 0); 3932fa9e4066Sahrens 393399653d4eSeschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 3934fa9e4066Sahrens 3935fa9e4066Sahrens kmem_free(packed, nvsize); 3936fa9e4066Sahrens 393799653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3938fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 3939fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 3940ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 3941fa9e4066Sahrens } 3942fa9e4066Sahrens 394399653d4eSeschrock static void 3944fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 3945fa94a07fSbrendan const char *config, const char *entry) 394699653d4eSeschrock { 394799653d4eSeschrock nvlist_t *nvroot; 3948fa94a07fSbrendan nvlist_t **list; 394999653d4eSeschrock int i; 395099653d4eSeschrock 3951fa94a07fSbrendan if (!sav->sav_sync) 395299653d4eSeschrock return; 395399653d4eSeschrock 395499653d4eSeschrock /* 3955fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 3956fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 39573d7072f8Seschrock * valid and the vdevs are labeled appropriately. 395899653d4eSeschrock */ 3959fa94a07fSbrendan if (sav->sav_object == 0) { 3960fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 3961fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 3962fa94a07fSbrendan sizeof (uint64_t), tx); 396399653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 3964fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 3965fa94a07fSbrendan &sav->sav_object, tx) == 0); 396699653d4eSeschrock } 396799653d4eSeschrock 396899653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3969fa94a07fSbrendan if (sav->sav_count == 0) { 3970fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 397199653d4eSeschrock } else { 3972fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 3973fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3974fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 3975fa94a07fSbrendan B_FALSE, B_FALSE, B_TRUE); 3976fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 3977fa94a07fSbrendan sav->sav_count) == 0); 3978fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3979fa94a07fSbrendan nvlist_free(list[i]); 3980fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 398199653d4eSeschrock } 398299653d4eSeschrock 3983fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 398406eeb2adSek nvlist_free(nvroot); 398599653d4eSeschrock 3986fa94a07fSbrendan sav->sav_sync = B_FALSE; 398799653d4eSeschrock } 398899653d4eSeschrock 398999653d4eSeschrock static void 399099653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 399199653d4eSeschrock { 399299653d4eSeschrock nvlist_t *config; 399399653d4eSeschrock 399499653d4eSeschrock if (list_is_empty(&spa->spa_dirty_list)) 399599653d4eSeschrock return; 399699653d4eSeschrock 399799653d4eSeschrock config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 399899653d4eSeschrock 399999653d4eSeschrock if (spa->spa_config_syncing) 400099653d4eSeschrock nvlist_free(spa->spa_config_syncing); 400199653d4eSeschrock spa->spa_config_syncing = config; 400299653d4eSeschrock 400399653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 400499653d4eSeschrock } 400599653d4eSeschrock 4006990b4856Slling /* 4007990b4856Slling * Set zpool properties. 4008990b4856Slling */ 4009b1b8ab34Slling static void 4010ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 4011b1b8ab34Slling { 4012b1b8ab34Slling spa_t *spa = arg1; 4013b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 4014990b4856Slling nvlist_t *nvp = arg2; 4015990b4856Slling nvpair_t *elem; 40163d7072f8Seschrock uint64_t intval; 4017*c5904d13Seschrock char *strval; 4018990b4856Slling zpool_prop_t prop; 4019990b4856Slling const char *propname; 4020990b4856Slling zprop_type_t proptype; 4021*c5904d13Seschrock spa_config_dirent_t *dp; 4022b1b8ab34Slling 4023990b4856Slling elem = NULL; 4024990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 4025990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 4026990b4856Slling case ZPOOL_PROP_VERSION: 4027990b4856Slling /* 4028990b4856Slling * Only set version for non-zpool-creation cases 4029990b4856Slling * (set/import). spa_create() needs special care 4030990b4856Slling * for version setting. 4031990b4856Slling */ 4032990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 4033990b4856Slling VERIFY(nvpair_value_uint64(elem, 4034990b4856Slling &intval) == 0); 4035990b4856Slling ASSERT(intval <= SPA_VERSION); 4036990b4856Slling ASSERT(intval >= spa_version(spa)); 4037990b4856Slling spa->spa_uberblock.ub_version = intval; 4038990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 4039990b4856Slling } 4040ecd6cf80Smarks break; 4041990b4856Slling 4042990b4856Slling case ZPOOL_PROP_ALTROOT: 4043990b4856Slling /* 4044990b4856Slling * 'altroot' is a non-persistent property. It should 4045990b4856Slling * have been set temporarily at creation or import time. 4046990b4856Slling */ 4047990b4856Slling ASSERT(spa->spa_root != NULL); 4048b1b8ab34Slling break; 40493d7072f8Seschrock 40502f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4051990b4856Slling /* 40522f8aaab3Seschrock * 'cachefile' is a non-persistent property, but note 40532f8aaab3Seschrock * an async request that the config cache needs to be 40542f8aaab3Seschrock * udpated. 4055990b4856Slling */ 40562f8aaab3Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 4057*c5904d13Seschrock 4058*c5904d13Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 4059*c5904d13Seschrock KM_SLEEP); 4060*c5904d13Seschrock 4061*c5904d13Seschrock if (strval[0] == '\0') 4062*c5904d13Seschrock dp->scd_path = spa_strdup(spa_config_path); 4063*c5904d13Seschrock else if (strcmp(strval, "none") == 0) 4064*c5904d13Seschrock dp->scd_path = NULL; 4065*c5904d13Seschrock else 4066*c5904d13Seschrock dp->scd_path = spa_strdup(strval); 4067*c5904d13Seschrock 4068*c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp); 40692f8aaab3Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 40703d7072f8Seschrock break; 4071990b4856Slling default: 4072990b4856Slling /* 4073990b4856Slling * Set pool property values in the poolprops mos object. 4074990b4856Slling */ 4075990b4856Slling mutex_enter(&spa->spa_props_lock); 4076990b4856Slling if (spa->spa_pool_props_object == 0) { 4077990b4856Slling objset_t *mos = spa->spa_meta_objset; 4078990b4856Slling 4079990b4856Slling VERIFY((spa->spa_pool_props_object = 4080990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 4081990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 4082990b4856Slling 4083990b4856Slling VERIFY(zap_update(mos, 4084990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 4085990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 4086990b4856Slling == 0); 4087990b4856Slling } 4088990b4856Slling mutex_exit(&spa->spa_props_lock); 4089990b4856Slling 4090990b4856Slling /* normalize the property name */ 4091990b4856Slling propname = zpool_prop_to_name(prop); 4092990b4856Slling proptype = zpool_prop_get_type(prop); 4093990b4856Slling 4094990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 4095990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 4096990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 4097990b4856Slling VERIFY(zap_update(mos, 4098990b4856Slling spa->spa_pool_props_object, propname, 4099990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 4100990b4856Slling 4101990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 4102990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 4103990b4856Slling 4104990b4856Slling if (proptype == PROP_TYPE_INDEX) { 4105990b4856Slling const char *unused; 4106990b4856Slling VERIFY(zpool_prop_index_to_string( 4107990b4856Slling prop, intval, &unused) == 0); 4108990b4856Slling } 4109990b4856Slling VERIFY(zap_update(mos, 4110990b4856Slling spa->spa_pool_props_object, propname, 4111990b4856Slling 8, 1, &intval, tx) == 0); 4112990b4856Slling } else { 4113990b4856Slling ASSERT(0); /* not allowed */ 4114990b4856Slling } 4115990b4856Slling 41160a4e9518Sgw switch (prop) { 41170a4e9518Sgw case ZPOOL_PROP_DELEGATION: 4118990b4856Slling spa->spa_delegation = intval; 41190a4e9518Sgw break; 41200a4e9518Sgw case ZPOOL_PROP_BOOTFS: 4121990b4856Slling spa->spa_bootfs = intval; 41220a4e9518Sgw break; 41230a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 41240a4e9518Sgw spa->spa_failmode = intval; 41250a4e9518Sgw break; 41260a4e9518Sgw default: 41270a4e9518Sgw break; 41280a4e9518Sgw } 4129990b4856Slling } 4130990b4856Slling 4131990b4856Slling /* log internal history if this is not a zpool create */ 4132990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 4133990b4856Slling tx->tx_txg != TXG_INITIAL) { 4134990b4856Slling spa_history_internal_log(LOG_POOL_PROPSET, 4135990b4856Slling spa, tx, cr, "%s %lld %s", 4136990b4856Slling nvpair_name(elem), intval, spa->spa_name); 4137b1b8ab34Slling } 4138b1b8ab34Slling } 4139b1b8ab34Slling } 4140b1b8ab34Slling 4141fa9e4066Sahrens /* 4142fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4143fa9e4066Sahrens * part of the process, so we iterate until it converges. 4144fa9e4066Sahrens */ 4145fa9e4066Sahrens void 4146fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 4147fa9e4066Sahrens { 4148fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4149fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 4150fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41510373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 4152fa9e4066Sahrens vdev_t *vd; 4153fa9e4066Sahrens dmu_tx_t *tx; 4154fa9e4066Sahrens int dirty_vdevs; 4155fa9e4066Sahrens 4156fa9e4066Sahrens /* 4157fa9e4066Sahrens * Lock out configuration changes. 4158fa9e4066Sahrens */ 4159ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 4160fa9e4066Sahrens 4161fa9e4066Sahrens spa->spa_syncing_txg = txg; 4162fa9e4066Sahrens spa->spa_sync_pass = 0; 4163fa9e4066Sahrens 4164ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4165fa9e4066Sahrens 416699653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 416799653d4eSeschrock 416899653d4eSeschrock /* 4169e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 417099653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 417199653d4eSeschrock */ 4172e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 4173e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 417499653d4eSeschrock int i; 417599653d4eSeschrock 417699653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 417799653d4eSeschrock vd = rvd->vdev_child[i]; 417899653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 417999653d4eSeschrock break; 418099653d4eSeschrock } 418199653d4eSeschrock if (i == rvd->vdev_children) { 418299653d4eSeschrock spa->spa_deflate = TRUE; 418399653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 418499653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 418599653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 418699653d4eSeschrock } 418799653d4eSeschrock } 418899653d4eSeschrock 4189fa9e4066Sahrens /* 4190fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 4191fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 4192fa9e4066Sahrens * don't generate work on an otherwise idle system. 4193fa9e4066Sahrens */ 4194fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 41951615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 41961615a317Sek !txg_list_empty(&dp->dp_sync_tasks, txg)) 4197fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 4198fa9e4066Sahrens 4199fa9e4066Sahrens /* 4200fa9e4066Sahrens * Iterate to convergence. 4201fa9e4066Sahrens */ 4202fa9e4066Sahrens do { 4203fa9e4066Sahrens spa->spa_sync_pass++; 4204fa9e4066Sahrens 4205fa9e4066Sahrens spa_sync_config_object(spa, tx); 4206fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 4207fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 4208fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 4209fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 4210ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 4211fa9e4066Sahrens dsl_pool_sync(dp, txg); 4212fa9e4066Sahrens 4213fa9e4066Sahrens dirty_vdevs = 0; 4214fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4215fa9e4066Sahrens vdev_sync(vd, txg); 4216fa9e4066Sahrens dirty_vdevs++; 4217fa9e4066Sahrens } 4218fa9e4066Sahrens 4219fa9e4066Sahrens bplist_sync(bpl, tx); 4220fa9e4066Sahrens } while (dirty_vdevs); 4221fa9e4066Sahrens 4222fa9e4066Sahrens bplist_close(bpl); 4223fa9e4066Sahrens 4224fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4225fa9e4066Sahrens 4226fa9e4066Sahrens /* 4227fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4228fa9e4066Sahrens * to commit the transaction group. 42290373e76bSbonwick * 423017f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 423117f17c2dSbonwick * random top-level vdevs that are known to be visible in the 423217f17c2dSbonwick * config cache (see spa_vdev_add() for details). If there *are* 423317f17c2dSbonwick * dirty vdevs -- or if the sync to our random subset fails -- 423417f17c2dSbonwick * then sync the uberblock to all vdevs. 42350373e76bSbonwick */ 423617f17c2dSbonwick if (list_is_empty(&spa->spa_dirty_list)) { 423721bf64a7Sgw vdev_t *svd[SPA_DVAS_PER_BP]; 423821bf64a7Sgw int svdcount = 0; 42390373e76bSbonwick int children = rvd->vdev_children; 42400373e76bSbonwick int c0 = spa_get_random(children); 42410373e76bSbonwick int c; 42420373e76bSbonwick 42430373e76bSbonwick for (c = 0; c < children; c++) { 42440373e76bSbonwick vd = rvd->vdev_child[(c0 + c) % children]; 424517f17c2dSbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 42460373e76bSbonwick continue; 424717f17c2dSbonwick svd[svdcount++] = vd; 424817f17c2dSbonwick if (svdcount == SPA_DVAS_PER_BP) 42490373e76bSbonwick break; 42500373e76bSbonwick } 425121bf64a7Sgw vdev_config_sync(svd, svdcount, txg); 425221bf64a7Sgw } else { 425321bf64a7Sgw vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 42540373e76bSbonwick } 425599653d4eSeschrock dmu_tx_commit(tx); 425699653d4eSeschrock 42570373e76bSbonwick /* 42580373e76bSbonwick * Clear the dirty config list. 4259fa9e4066Sahrens */ 42600373e76bSbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 42610373e76bSbonwick vdev_config_clean(vd); 42620373e76bSbonwick 42630373e76bSbonwick /* 42640373e76bSbonwick * Now that the new config has synced transactionally, 42650373e76bSbonwick * let it become visible to the config cache. 42660373e76bSbonwick */ 42670373e76bSbonwick if (spa->spa_config_syncing != NULL) { 42680373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 42690373e76bSbonwick spa->spa_config_txg = txg; 42700373e76bSbonwick spa->spa_config_syncing = NULL; 42710373e76bSbonwick } 4272fa9e4066Sahrens 4273fa9e4066Sahrens /* 4274fa9e4066Sahrens * Make a stable copy of the fully synced uberblock. 4275fa9e4066Sahrens * We use this as the root for pool traversals. 4276fa9e4066Sahrens */ 4277fa9e4066Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 4278fa9e4066Sahrens 4279fa9e4066Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 4280fa9e4066Sahrens 4281fa9e4066Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 4282fa9e4066Sahrens spa->spa_traverse_wanted = 0; 4283fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 4284fa9e4066Sahrens rw_exit(&spa->spa_traverse_lock); 4285fa9e4066Sahrens 4286fa9e4066Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 4287fa9e4066Sahrens 4288fa9e4066Sahrens /* 4289fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 4290fa9e4066Sahrens */ 4291fa9e4066Sahrens dsl_pool_zil_clean(dp); 4292fa9e4066Sahrens 4293fa9e4066Sahrens /* 4294fa9e4066Sahrens * Update usable space statistics. 4295fa9e4066Sahrens */ 4296fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4297fa9e4066Sahrens vdev_sync_done(vd, txg); 4298fa9e4066Sahrens 4299fa9e4066Sahrens /* 4300fa9e4066Sahrens * It had better be the case that we didn't dirty anything 430199653d4eSeschrock * since vdev_config_sync(). 4302fa9e4066Sahrens */ 4303fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4304fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4305fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4306fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 4307fa9e4066Sahrens 4308ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 4309ea8dc4b6Seschrock 4310ea8dc4b6Seschrock /* 4311ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 4312ea8dc4b6Seschrock */ 4313ea8dc4b6Seschrock spa_async_dispatch(spa); 4314fa9e4066Sahrens } 4315fa9e4066Sahrens 4316fa9e4066Sahrens /* 4317fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4318fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4319fa9e4066Sahrens * sync. 4320fa9e4066Sahrens */ 4321fa9e4066Sahrens void 4322fa9e4066Sahrens spa_sync_allpools(void) 4323fa9e4066Sahrens { 4324fa9e4066Sahrens spa_t *spa = NULL; 4325fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4326fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 4327fa9e4066Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4328fa9e4066Sahrens continue; 4329fa9e4066Sahrens spa_open_ref(spa, FTAG); 4330fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4331fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4332fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4333fa9e4066Sahrens spa_close(spa, FTAG); 4334fa9e4066Sahrens } 4335fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4336fa9e4066Sahrens } 4337fa9e4066Sahrens 4338fa9e4066Sahrens /* 4339fa9e4066Sahrens * ========================================================================== 4340fa9e4066Sahrens * Miscellaneous routines 4341fa9e4066Sahrens * ========================================================================== 4342fa9e4066Sahrens */ 4343fa9e4066Sahrens 4344fa9e4066Sahrens /* 4345fa9e4066Sahrens * Remove all pools in the system. 4346fa9e4066Sahrens */ 4347fa9e4066Sahrens void 4348fa9e4066Sahrens spa_evict_all(void) 4349fa9e4066Sahrens { 4350fa9e4066Sahrens spa_t *spa; 4351fa9e4066Sahrens 4352fa9e4066Sahrens /* 4353fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 4354fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 4355fa9e4066Sahrens */ 4356fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4357fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 4358fa9e4066Sahrens /* 4359ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 4360ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 4361ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 4362fa9e4066Sahrens */ 4363fa9e4066Sahrens spa_open_ref(spa, FTAG); 4364fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4365ea8dc4b6Seschrock spa_async_suspend(spa); 4366fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4367bb8b5132Sek VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 4368fa9e4066Sahrens spa_close(spa, FTAG); 4369fa9e4066Sahrens 4370fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4371fa9e4066Sahrens spa_unload(spa); 4372fa9e4066Sahrens spa_deactivate(spa); 4373fa9e4066Sahrens } 4374fa9e4066Sahrens spa_remove(spa); 4375fa9e4066Sahrens } 4376fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4377fa9e4066Sahrens } 4378ea8dc4b6Seschrock 4379ea8dc4b6Seschrock vdev_t * 4380*c5904d13Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 4381ea8dc4b6Seschrock { 4382*c5904d13Seschrock vdev_t *vd; 4383*c5904d13Seschrock int i; 4384*c5904d13Seschrock 4385*c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 4386*c5904d13Seschrock return (vd); 4387*c5904d13Seschrock 4388*c5904d13Seschrock if (l2cache) { 4389*c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 4390*c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 4391*c5904d13Seschrock if (vd->vdev_guid == guid) 4392*c5904d13Seschrock return (vd); 4393*c5904d13Seschrock } 4394*c5904d13Seschrock } 4395*c5904d13Seschrock 4396*c5904d13Seschrock return (NULL); 4397ea8dc4b6Seschrock } 4398eaca9bbdSeschrock 4399eaca9bbdSeschrock void 4400990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 4401eaca9bbdSeschrock { 4402eaca9bbdSeschrock spa_config_enter(spa, RW_WRITER, FTAG); 4403eaca9bbdSeschrock 4404eaca9bbdSeschrock /* 4405eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 4406eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 4407eaca9bbdSeschrock * possible. 4408eaca9bbdSeschrock */ 4409e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 4410990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 4411eaca9bbdSeschrock 4412990b4856Slling spa->spa_uberblock.ub_version = version; 4413eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 4414eaca9bbdSeschrock 4415eaca9bbdSeschrock spa_config_exit(spa, FTAG); 441699653d4eSeschrock 441799653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 441899653d4eSeschrock } 441999653d4eSeschrock 442099653d4eSeschrock boolean_t 442199653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 442299653d4eSeschrock { 442399653d4eSeschrock int i; 442439c23413Seschrock uint64_t spareguid; 4425fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 442699653d4eSeschrock 4427fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 4428fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 442999653d4eSeschrock return (B_TRUE); 443099653d4eSeschrock 4431fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 4432fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 4433fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 443439c23413Seschrock return (B_TRUE); 443539c23413Seschrock } 443639c23413Seschrock 443799653d4eSeschrock return (B_FALSE); 4438eaca9bbdSeschrock } 4439b1b8ab34Slling 44403d7072f8Seschrock /* 44413d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44423d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44433d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44443d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44453d7072f8Seschrock * or zdb as real changes. 44463d7072f8Seschrock */ 44473d7072f8Seschrock void 44483d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 44493d7072f8Seschrock { 44503d7072f8Seschrock #ifdef _KERNEL 44513d7072f8Seschrock sysevent_t *ev; 44523d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 44533d7072f8Seschrock sysevent_value_t value; 44543d7072f8Seschrock sysevent_id_t eid; 44553d7072f8Seschrock 44563d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 44573d7072f8Seschrock SE_SLEEP); 44583d7072f8Seschrock 44593d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 44603d7072f8Seschrock value.value.sv_string = spa_name(spa); 44613d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 44623d7072f8Seschrock goto done; 44633d7072f8Seschrock 44643d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44653d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 44663d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 44673d7072f8Seschrock goto done; 44683d7072f8Seschrock 44693d7072f8Seschrock if (vd) { 44703d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44713d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 44723d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 44733d7072f8Seschrock SE_SLEEP) != 0) 44743d7072f8Seschrock goto done; 44753d7072f8Seschrock 44763d7072f8Seschrock if (vd->vdev_path) { 44773d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 44783d7072f8Seschrock value.value.sv_string = vd->vdev_path; 44793d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 44803d7072f8Seschrock &value, SE_SLEEP) != 0) 44813d7072f8Seschrock goto done; 44823d7072f8Seschrock } 44833d7072f8Seschrock } 44843d7072f8Seschrock 4485b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 4486b01c3b58Seschrock goto done; 4487b01c3b58Seschrock attr = NULL; 4488b01c3b58Seschrock 44893d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 44903d7072f8Seschrock 44913d7072f8Seschrock done: 44923d7072f8Seschrock if (attr) 44933d7072f8Seschrock sysevent_free_attr(attr); 44943d7072f8Seschrock sysevent_free(ev); 44953d7072f8Seschrock #endif 44963d7072f8Seschrock } 4497