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 /* 23379c004dSEric Schrock * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24fa9e4066Sahrens * Use is subject to license terms. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens /* 28fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 29fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 30fa9e4066Sahrens * pool. 31fa9e4066Sahrens */ 32fa9e4066Sahrens 33fa9e4066Sahrens #include <sys/zfs_context.h> 34ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 35fa9e4066Sahrens #include <sys/spa_impl.h> 36fa9e4066Sahrens #include <sys/zio.h> 37fa9e4066Sahrens #include <sys/zio_checksum.h> 38fa9e4066Sahrens #include <sys/zio_compress.h> 39fa9e4066Sahrens #include <sys/dmu.h> 40fa9e4066Sahrens #include <sys/dmu_tx.h> 41fa9e4066Sahrens #include <sys/zap.h> 42fa9e4066Sahrens #include <sys/zil.h> 43fa9e4066Sahrens #include <sys/vdev_impl.h> 44fa9e4066Sahrens #include <sys/metaslab.h> 45fa9e4066Sahrens #include <sys/uberblock_impl.h> 46fa9e4066Sahrens #include <sys/txg.h> 47fa9e4066Sahrens #include <sys/avl.h> 48fa9e4066Sahrens #include <sys/dmu_traverse.h> 49b1b8ab34Slling #include <sys/dmu_objset.h> 50fa9e4066Sahrens #include <sys/unique.h> 51fa9e4066Sahrens #include <sys/dsl_pool.h> 52b1b8ab34Slling #include <sys/dsl_dataset.h> 53fa9e4066Sahrens #include <sys/dsl_dir.h> 54fa9e4066Sahrens #include <sys/dsl_prop.h> 55b1b8ab34Slling #include <sys/dsl_synctask.h> 56fa9e4066Sahrens #include <sys/fs/zfs.h> 57fa94a07fSbrendan #include <sys/arc.h> 58fa9e4066Sahrens #include <sys/callb.h> 5995173954Sek #include <sys/systeminfo.h> 6095173954Sek #include <sys/sunddi.h> 61e7cbe64fSgw #include <sys/spa_boot.h> 62*573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 63fa9e4066Sahrens 645679c89fSjv #ifdef _KERNEL 655679c89fSjv #include <sys/zone.h> 665679c89fSjv #endif /* _KERNEL */ 675679c89fSjv 68990b4856Slling #include "zfs_prop.h" 69b7b97454Sperrin #include "zfs_comutil.h" 70990b4856Slling 712e0c549eSJonathan Adams enum zti_modes { 722e0c549eSJonathan Adams zti_mode_fixed, /* value is # of threads (min 1) */ 732e0c549eSJonathan Adams zti_mode_online_percent, /* value is % of online CPUs */ 742e0c549eSJonathan Adams zti_mode_tune, /* fill from zio_taskq_tune_* */ 752e0c549eSJonathan Adams zti_nmodes 76e14bb325SJeff Bonwick }; 77416e0cd8Sek 782e0c549eSJonathan Adams #define ZTI_THREAD_FIX(n) { zti_mode_fixed, (n) } 792e0c549eSJonathan Adams #define ZTI_THREAD_PCT(n) { zti_mode_online_percent, (n) } 802e0c549eSJonathan Adams #define ZTI_THREAD_TUNE { zti_mode_tune, 0 } 812e0c549eSJonathan Adams 822e0c549eSJonathan Adams #define ZTI_THREAD_ONE ZTI_THREAD_FIX(1) 832e0c549eSJonathan Adams 842e0c549eSJonathan Adams typedef struct zio_taskq_info { 852e0c549eSJonathan Adams const char *zti_name; 862e0c549eSJonathan Adams struct { 872e0c549eSJonathan Adams enum zti_modes zti_mode; 882e0c549eSJonathan Adams uint_t zti_value; 892e0c549eSJonathan Adams } zti_nthreads[ZIO_TASKQ_TYPES]; 902e0c549eSJonathan Adams } zio_taskq_info_t; 912e0c549eSJonathan Adams 922e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 932e0c549eSJonathan Adams "issue", "intr" 942e0c549eSJonathan Adams }; 952e0c549eSJonathan Adams 962e0c549eSJonathan Adams const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = { 972e0c549eSJonathan Adams /* ISSUE INTR */ 982e0c549eSJonathan Adams { "spa_zio_null", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 992e0c549eSJonathan Adams { "spa_zio_read", { ZTI_THREAD_FIX(8), ZTI_THREAD_TUNE } }, 1002e0c549eSJonathan Adams { "spa_zio_write", { ZTI_THREAD_TUNE, ZTI_THREAD_FIX(8) } }, 1012e0c549eSJonathan Adams { "spa_zio_free", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1022e0c549eSJonathan Adams { "spa_zio_claim", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1032e0c549eSJonathan Adams { "spa_zio_ioctl", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1042e0c549eSJonathan Adams }; 1052e0c549eSJonathan Adams 1062e0c549eSJonathan Adams enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent; 1072e0c549eSJonathan Adams uint_t zio_taskq_tune_value = 80; /* #threads = 80% of # online CPUs */ 1082e0c549eSJonathan Adams 109990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 11089a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 111990b4856Slling 112990b4856Slling /* 113990b4856Slling * ========================================================================== 114990b4856Slling * SPA properties routines 115990b4856Slling * ========================================================================== 116990b4856Slling */ 117990b4856Slling 118990b4856Slling /* 119990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 120990b4856Slling */ 1219d82f4f6Slling static void 122990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 123990b4856Slling uint64_t intval, zprop_source_t src) 124990b4856Slling { 125990b4856Slling const char *propname = zpool_prop_to_name(prop); 126990b4856Slling nvlist_t *propval; 127990b4856Slling 1289d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1299d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 130990b4856Slling 1319d82f4f6Slling if (strval != NULL) 1329d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1339d82f4f6Slling else 1349d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 135990b4856Slling 1369d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 137990b4856Slling nvlist_free(propval); 138990b4856Slling } 139990b4856Slling 140990b4856Slling /* 141990b4856Slling * Get property values from the spa configuration. 142990b4856Slling */ 1439d82f4f6Slling static void 144990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 145990b4856Slling { 146379c004dSEric Schrock uint64_t size; 147379c004dSEric Schrock uint64_t used; 148990b4856Slling uint64_t cap, version; 149990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 150c5904d13Seschrock spa_config_dirent_t *dp; 151990b4856Slling 152e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 153e14bb325SJeff Bonwick 154379c004dSEric Schrock if (spa->spa_root_vdev != NULL) { 155379c004dSEric Schrock size = spa_get_space(spa); 156379c004dSEric Schrock used = spa_get_alloc(spa); 157379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 158379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 159379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 160379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, 161379c004dSEric Schrock size - used, src); 162379c004dSEric Schrock 163379c004dSEric Schrock cap = (size == 0) ? 0 : (used * 100 / size); 164379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 165379c004dSEric Schrock 166379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 167379c004dSEric Schrock spa->spa_root_vdev->vdev_state, src); 168379c004dSEric Schrock 169379c004dSEric Schrock version = spa_version(spa); 170379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 171379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 172379c004dSEric Schrock else 173379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 174379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 175379c004dSEric Schrock } 176990b4856Slling 1779d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 178990b4856Slling 1799d82f4f6Slling if (spa->spa_root != NULL) 1809d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1819d82f4f6Slling 0, ZPROP_SRC_LOCAL); 182990b4856Slling 183c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 184c5904d13Seschrock if (dp->scd_path == NULL) { 1859d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 186c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 187c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1889d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 189c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1902f8aaab3Seschrock } 1912f8aaab3Seschrock } 192990b4856Slling } 193990b4856Slling 194990b4856Slling /* 195990b4856Slling * Get zpool property values. 196990b4856Slling */ 197990b4856Slling int 198990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 199990b4856Slling { 200990b4856Slling zap_cursor_t zc; 201990b4856Slling zap_attribute_t za; 202990b4856Slling objset_t *mos = spa->spa_meta_objset; 203990b4856Slling int err; 204990b4856Slling 2059d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 206990b4856Slling 207e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 208e14bb325SJeff Bonwick 209990b4856Slling /* 210990b4856Slling * Get properties from the spa config. 211990b4856Slling */ 2129d82f4f6Slling spa_prop_get_config(spa, nvp); 213990b4856Slling 214990b4856Slling /* If no pool property object, no more prop to get. */ 215990b4856Slling if (spa->spa_pool_props_object == 0) { 216990b4856Slling mutex_exit(&spa->spa_props_lock); 217990b4856Slling return (0); 218990b4856Slling } 219990b4856Slling 220990b4856Slling /* 221990b4856Slling * Get properties from the MOS pool property object. 222990b4856Slling */ 223990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 224990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 225990b4856Slling zap_cursor_advance(&zc)) { 226990b4856Slling uint64_t intval = 0; 227990b4856Slling char *strval = NULL; 228990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 229990b4856Slling zpool_prop_t prop; 230990b4856Slling 231990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 232990b4856Slling continue; 233990b4856Slling 234990b4856Slling switch (za.za_integer_length) { 235990b4856Slling case 8: 236990b4856Slling /* integer property */ 237990b4856Slling if (za.za_first_integer != 238990b4856Slling zpool_prop_default_numeric(prop)) 239990b4856Slling src = ZPROP_SRC_LOCAL; 240990b4856Slling 241990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 242990b4856Slling dsl_pool_t *dp; 243990b4856Slling dsl_dataset_t *ds = NULL; 244990b4856Slling 245990b4856Slling dp = spa_get_dsl(spa); 246990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 247745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 248745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 249990b4856Slling rw_exit(&dp->dp_config_rwlock); 250990b4856Slling break; 251990b4856Slling } 252990b4856Slling 253990b4856Slling strval = kmem_alloc( 254990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 255990b4856Slling KM_SLEEP); 256990b4856Slling dsl_dataset_name(ds, strval); 257745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 258990b4856Slling rw_exit(&dp->dp_config_rwlock); 259990b4856Slling } else { 260990b4856Slling strval = NULL; 261990b4856Slling intval = za.za_first_integer; 262990b4856Slling } 263990b4856Slling 2649d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 265990b4856Slling 266990b4856Slling if (strval != NULL) 267990b4856Slling kmem_free(strval, 268990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 269990b4856Slling 270990b4856Slling break; 271990b4856Slling 272990b4856Slling case 1: 273990b4856Slling /* string property */ 274990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 275990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 276990b4856Slling za.za_name, 1, za.za_num_integers, strval); 277990b4856Slling if (err) { 278990b4856Slling kmem_free(strval, za.za_num_integers); 279990b4856Slling break; 280990b4856Slling } 2819d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 282990b4856Slling kmem_free(strval, za.za_num_integers); 283990b4856Slling break; 284990b4856Slling 285990b4856Slling default: 286990b4856Slling break; 287990b4856Slling } 288990b4856Slling } 289990b4856Slling zap_cursor_fini(&zc); 290990b4856Slling mutex_exit(&spa->spa_props_lock); 291990b4856Slling out: 292990b4856Slling if (err && err != ENOENT) { 293990b4856Slling nvlist_free(*nvp); 2949d82f4f6Slling *nvp = NULL; 295990b4856Slling return (err); 296990b4856Slling } 297990b4856Slling 298990b4856Slling return (0); 299990b4856Slling } 300990b4856Slling 301990b4856Slling /* 302990b4856Slling * Validate the given pool properties nvlist and modify the list 303990b4856Slling * for the property values to be set. 304990b4856Slling */ 305990b4856Slling static int 306990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 307990b4856Slling { 308990b4856Slling nvpair_t *elem; 309990b4856Slling int error = 0, reset_bootfs = 0; 310990b4856Slling uint64_t objnum; 311990b4856Slling 312990b4856Slling elem = NULL; 313990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 314990b4856Slling zpool_prop_t prop; 315990b4856Slling char *propname, *strval; 316990b4856Slling uint64_t intval; 317990b4856Slling objset_t *os; 3182f8aaab3Seschrock char *slash; 319990b4856Slling 320990b4856Slling propname = nvpair_name(elem); 321990b4856Slling 322990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 323990b4856Slling return (EINVAL); 324990b4856Slling 325990b4856Slling switch (prop) { 326990b4856Slling case ZPOOL_PROP_VERSION: 327990b4856Slling error = nvpair_value_uint64(elem, &intval); 328990b4856Slling if (!error && 329990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 330990b4856Slling error = EINVAL; 331990b4856Slling break; 332990b4856Slling 333990b4856Slling case ZPOOL_PROP_DELEGATION: 334990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 335d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 336*573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 337990b4856Slling error = nvpair_value_uint64(elem, &intval); 338990b4856Slling if (!error && intval > 1) 339990b4856Slling error = EINVAL; 340990b4856Slling break; 341990b4856Slling 342990b4856Slling case ZPOOL_PROP_BOOTFS: 34325f89ee2SJeff Bonwick /* 34425f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 34525f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 34625f89ee2SJeff Bonwick * the bootfs property cannot be set. 34725f89ee2SJeff Bonwick */ 348990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 349990b4856Slling error = ENOTSUP; 350990b4856Slling break; 351990b4856Slling } 352990b4856Slling 353990b4856Slling /* 35415e6edf1Sgw * Make sure the vdev config is bootable 355990b4856Slling */ 35615e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 357990b4856Slling error = ENOTSUP; 358990b4856Slling break; 359990b4856Slling } 360990b4856Slling 361990b4856Slling reset_bootfs = 1; 362990b4856Slling 363990b4856Slling error = nvpair_value_string(elem, &strval); 364990b4856Slling 365990b4856Slling if (!error) { 36615e6edf1Sgw uint64_t compress; 36715e6edf1Sgw 368990b4856Slling if (strval == NULL || strval[0] == '\0') { 369990b4856Slling objnum = zpool_prop_default_numeric( 370990b4856Slling ZPOOL_PROP_BOOTFS); 371990b4856Slling break; 372990b4856Slling } 373990b4856Slling 374990b4856Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 375745cd3c5Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 376990b4856Slling break; 37715e6edf1Sgw 37815e6edf1Sgw /* We don't support gzip bootable datasets */ 37915e6edf1Sgw if ((error = dsl_prop_get_integer(strval, 38015e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 38115e6edf1Sgw &compress, NULL)) == 0 && 38215e6edf1Sgw !BOOTFS_COMPRESS_VALID(compress)) { 38315e6edf1Sgw error = ENOTSUP; 38415e6edf1Sgw } else { 38515e6edf1Sgw objnum = dmu_objset_id(os); 38615e6edf1Sgw } 387990b4856Slling dmu_objset_close(os); 388990b4856Slling } 389990b4856Slling break; 390e14bb325SJeff Bonwick 3910a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 3920a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 3930a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3940a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 3950a4e9518Sgw error = EINVAL; 3960a4e9518Sgw 3970a4e9518Sgw /* 3980a4e9518Sgw * This is a special case which only occurs when 3990a4e9518Sgw * the pool has completely failed. This allows 4000a4e9518Sgw * the user to change the in-core failmode property 4010a4e9518Sgw * without syncing it out to disk (I/Os might 4020a4e9518Sgw * currently be blocked). We do this by returning 4030a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 4040a4e9518Sgw * into thinking we encountered a property validation 4050a4e9518Sgw * error. 4060a4e9518Sgw */ 407e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 4080a4e9518Sgw spa->spa_failmode = intval; 4090a4e9518Sgw error = EIO; 4100a4e9518Sgw } 4110a4e9518Sgw break; 4122f8aaab3Seschrock 4132f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4142f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4152f8aaab3Seschrock break; 4162f8aaab3Seschrock 4172f8aaab3Seschrock if (strval[0] == '\0') 4182f8aaab3Seschrock break; 4192f8aaab3Seschrock 4202f8aaab3Seschrock if (strcmp(strval, "none") == 0) 4212f8aaab3Seschrock break; 4222f8aaab3Seschrock 4232f8aaab3Seschrock if (strval[0] != '/') { 4242f8aaab3Seschrock error = EINVAL; 4252f8aaab3Seschrock break; 4262f8aaab3Seschrock } 4272f8aaab3Seschrock 4282f8aaab3Seschrock slash = strrchr(strval, '/'); 4292f8aaab3Seschrock ASSERT(slash != NULL); 4302f8aaab3Seschrock 4312f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4322f8aaab3Seschrock strcmp(slash, "/..") == 0) 4332f8aaab3Seschrock error = EINVAL; 4342f8aaab3Seschrock break; 435990b4856Slling } 436990b4856Slling 437990b4856Slling if (error) 438990b4856Slling break; 439990b4856Slling } 440990b4856Slling 441990b4856Slling if (!error && reset_bootfs) { 442990b4856Slling error = nvlist_remove(props, 443990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 444990b4856Slling 445990b4856Slling if (!error) { 446990b4856Slling error = nvlist_add_uint64(props, 447990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 448990b4856Slling } 449990b4856Slling } 450990b4856Slling 451990b4856Slling return (error); 452990b4856Slling } 453990b4856Slling 454379c004dSEric Schrock void 455379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 456379c004dSEric Schrock { 457379c004dSEric Schrock char *cachefile; 458379c004dSEric Schrock spa_config_dirent_t *dp; 459379c004dSEric Schrock 460379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 461379c004dSEric Schrock &cachefile) != 0) 462379c004dSEric Schrock return; 463379c004dSEric Schrock 464379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 465379c004dSEric Schrock KM_SLEEP); 466379c004dSEric Schrock 467379c004dSEric Schrock if (cachefile[0] == '\0') 468379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 469379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 470379c004dSEric Schrock dp->scd_path = NULL; 471379c004dSEric Schrock else 472379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 473379c004dSEric Schrock 474379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 475379c004dSEric Schrock if (need_sync) 476379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 477379c004dSEric Schrock } 478379c004dSEric Schrock 479990b4856Slling int 480990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 481990b4856Slling { 482990b4856Slling int error; 483379c004dSEric Schrock nvpair_t *elem; 484379c004dSEric Schrock boolean_t need_sync = B_FALSE; 485379c004dSEric Schrock zpool_prop_t prop; 486990b4856Slling 487990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 488990b4856Slling return (error); 489990b4856Slling 490379c004dSEric Schrock elem = NULL; 491379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 492379c004dSEric Schrock if ((prop = zpool_name_to_prop( 493379c004dSEric Schrock nvpair_name(elem))) == ZPROP_INVAL) 494379c004dSEric Schrock return (EINVAL); 495379c004dSEric Schrock 496379c004dSEric Schrock if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 497379c004dSEric Schrock continue; 498379c004dSEric Schrock 499379c004dSEric Schrock need_sync = B_TRUE; 500379c004dSEric Schrock break; 501379c004dSEric Schrock } 502379c004dSEric Schrock 503379c004dSEric Schrock if (need_sync) 504379c004dSEric Schrock return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 505379c004dSEric Schrock spa, nvp, 3)); 506379c004dSEric Schrock else 507379c004dSEric Schrock return (0); 508990b4856Slling } 509990b4856Slling 510990b4856Slling /* 511990b4856Slling * If the bootfs property value is dsobj, clear it. 512990b4856Slling */ 513990b4856Slling void 514990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 515990b4856Slling { 516990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 517990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 518990b4856Slling spa->spa_pool_props_object, 519990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 520990b4856Slling spa->spa_bootfs = 0; 521990b4856Slling } 522990b4856Slling } 523990b4856Slling 524fa9e4066Sahrens /* 525fa9e4066Sahrens * ========================================================================== 526fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 527fa9e4066Sahrens * ========================================================================== 528fa9e4066Sahrens */ 529fa9e4066Sahrens 530ea8dc4b6Seschrock static int 531ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 532ea8dc4b6Seschrock { 533ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 534ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 535ea8dc4b6Seschrock int ret; 536ea8dc4b6Seschrock 537ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 538ea8dc4b6Seschrock sizeof (zbookmark_t)); 539ea8dc4b6Seschrock 540ea8dc4b6Seschrock if (ret < 0) 541ea8dc4b6Seschrock return (-1); 542ea8dc4b6Seschrock else if (ret > 0) 543ea8dc4b6Seschrock return (1); 544ea8dc4b6Seschrock else 545ea8dc4b6Seschrock return (0); 546ea8dc4b6Seschrock } 547ea8dc4b6Seschrock 548ea8dc4b6Seschrock /* 549ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 550ea8dc4b6Seschrock * re-initializes them in the process. 551ea8dc4b6Seschrock */ 552ea8dc4b6Seschrock void 553ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 554ea8dc4b6Seschrock { 555ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 556ea8dc4b6Seschrock 557ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 558ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 559ea8dc4b6Seschrock 560ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 561ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 562ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 563ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 564ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 565ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 566ea8dc4b6Seschrock } 567ea8dc4b6Seschrock 568fa9e4066Sahrens /* 569fa9e4066Sahrens * Activate an uninitialized pool. 570fa9e4066Sahrens */ 571fa9e4066Sahrens static void 5728ad4d6ddSJeff Bonwick spa_activate(spa_t *spa, int mode) 573fa9e4066Sahrens { 574fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 575fa9e4066Sahrens 576fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 5778ad4d6ddSJeff Bonwick spa->spa_mode = mode; 578fa9e4066Sahrens 579d6e555bdSGeorge Wilson spa->spa_normal_class = metaslab_class_create(zfs_metaslab_ops); 580d6e555bdSGeorge Wilson spa->spa_log_class = metaslab_class_create(zfs_metaslab_ops); 581fa9e4066Sahrens 582e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 5832e0c549eSJonathan Adams const zio_taskq_info_t *ztip = &zio_taskqs[t]; 584e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5852e0c549eSJonathan Adams enum zti_modes mode = ztip->zti_nthreads[q].zti_mode; 5862e0c549eSJonathan Adams uint_t value = ztip->zti_nthreads[q].zti_value; 5872e0c549eSJonathan Adams char name[32]; 5882e0c549eSJonathan Adams 5892e0c549eSJonathan Adams (void) snprintf(name, sizeof (name), 5902e0c549eSJonathan Adams "%s_%s", ztip->zti_name, zio_taskq_types[q]); 5912e0c549eSJonathan Adams 5922e0c549eSJonathan Adams if (mode == zti_mode_tune) { 5932e0c549eSJonathan Adams mode = zio_taskq_tune_mode; 5942e0c549eSJonathan Adams value = zio_taskq_tune_value; 5952e0c549eSJonathan Adams if (mode == zti_mode_tune) 5962e0c549eSJonathan Adams mode = zti_mode_online_percent; 5972e0c549eSJonathan Adams } 5982e0c549eSJonathan Adams 5992e0c549eSJonathan Adams switch (mode) { 6002e0c549eSJonathan Adams case zti_mode_fixed: 6012e0c549eSJonathan Adams ASSERT3U(value, >=, 1); 6022e0c549eSJonathan Adams value = MAX(value, 1); 6032e0c549eSJonathan Adams 6042e0c549eSJonathan Adams spa->spa_zio_taskq[t][q] = taskq_create(name, 6052e0c549eSJonathan Adams value, maxclsyspri, 50, INT_MAX, 6062e0c549eSJonathan Adams TASKQ_PREPOPULATE); 6072e0c549eSJonathan Adams break; 6082e0c549eSJonathan Adams 6092e0c549eSJonathan Adams case zti_mode_online_percent: 6102e0c549eSJonathan Adams spa->spa_zio_taskq[t][q] = taskq_create(name, 6112e0c549eSJonathan Adams value, maxclsyspri, 50, INT_MAX, 6122e0c549eSJonathan Adams TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 6132e0c549eSJonathan Adams break; 6142e0c549eSJonathan Adams 6152e0c549eSJonathan Adams case zti_mode_tune: 6162e0c549eSJonathan Adams default: 6172e0c549eSJonathan Adams panic("unrecognized mode for " 6182e0c549eSJonathan Adams "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) " 6192e0c549eSJonathan Adams "in spa_activate()", 6202e0c549eSJonathan Adams t, q, mode, value); 6212e0c549eSJonathan Adams break; 6222e0c549eSJonathan Adams } 623e14bb325SJeff Bonwick } 624fa9e4066Sahrens } 625fa9e4066Sahrens 626e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 627e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 628e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 629e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 630fa9e4066Sahrens 631fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 632fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 633ea8dc4b6Seschrock 634ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 635ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 636ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 637ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 638ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 639ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 640fa9e4066Sahrens } 641fa9e4066Sahrens 642fa9e4066Sahrens /* 643fa9e4066Sahrens * Opposite of spa_activate(). 644fa9e4066Sahrens */ 645fa9e4066Sahrens static void 646fa9e4066Sahrens spa_deactivate(spa_t *spa) 647fa9e4066Sahrens { 648fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 649fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 650fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 65125f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 652fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 653fa9e4066Sahrens 654fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 655fa9e4066Sahrens 656e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 657e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 658fa9e4066Sahrens 659e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 660e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 661e14bb325SJeff Bonwick taskq_destroy(spa->spa_zio_taskq[t][q]); 662e14bb325SJeff Bonwick spa->spa_zio_taskq[t][q] = NULL; 663e14bb325SJeff Bonwick } 664fa9e4066Sahrens } 665fa9e4066Sahrens 666fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 667fa9e4066Sahrens spa->spa_normal_class = NULL; 668fa9e4066Sahrens 6698654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 6708654d025Sperrin spa->spa_log_class = NULL; 6718654d025Sperrin 672ea8dc4b6Seschrock /* 673ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 674ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 675ea8dc4b6Seschrock */ 676ea8dc4b6Seschrock spa_errlog_drain(spa); 677ea8dc4b6Seschrock 678ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 679ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 680ea8dc4b6Seschrock 681fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 682fa9e4066Sahrens } 683fa9e4066Sahrens 684fa9e4066Sahrens /* 685fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 686fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 687fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 688fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 689fa9e4066Sahrens */ 69099653d4eSeschrock static int 69199653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 69299653d4eSeschrock uint_t id, int atype) 693fa9e4066Sahrens { 694fa9e4066Sahrens nvlist_t **child; 695*573ca77eSGeorge Wilson uint_t children; 69699653d4eSeschrock int error; 697fa9e4066Sahrens 69899653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 69999653d4eSeschrock return (error); 700fa9e4066Sahrens 70199653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 70299653d4eSeschrock return (0); 703fa9e4066Sahrens 704e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 705e14bb325SJeff Bonwick &child, &children); 706e14bb325SJeff Bonwick 707e14bb325SJeff Bonwick if (error == ENOENT) 708e14bb325SJeff Bonwick return (0); 709e14bb325SJeff Bonwick 710e14bb325SJeff Bonwick if (error) { 71199653d4eSeschrock vdev_free(*vdp); 71299653d4eSeschrock *vdp = NULL; 71399653d4eSeschrock return (EINVAL); 714fa9e4066Sahrens } 715fa9e4066Sahrens 716*573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 71799653d4eSeschrock vdev_t *vd; 71899653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 71999653d4eSeschrock atype)) != 0) { 72099653d4eSeschrock vdev_free(*vdp); 72199653d4eSeschrock *vdp = NULL; 72299653d4eSeschrock return (error); 723fa9e4066Sahrens } 724fa9e4066Sahrens } 725fa9e4066Sahrens 72699653d4eSeschrock ASSERT(*vdp != NULL); 72799653d4eSeschrock 72899653d4eSeschrock return (0); 729fa9e4066Sahrens } 730fa9e4066Sahrens 731fa9e4066Sahrens /* 732fa9e4066Sahrens * Opposite of spa_load(). 733fa9e4066Sahrens */ 734fa9e4066Sahrens static void 735fa9e4066Sahrens spa_unload(spa_t *spa) 736fa9e4066Sahrens { 73799653d4eSeschrock int i; 73899653d4eSeschrock 739e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 740e14bb325SJeff Bonwick 741ea8dc4b6Seschrock /* 742ea8dc4b6Seschrock * Stop async tasks. 743ea8dc4b6Seschrock */ 744ea8dc4b6Seschrock spa_async_suspend(spa); 745ea8dc4b6Seschrock 746fa9e4066Sahrens /* 747fa9e4066Sahrens * Stop syncing. 748fa9e4066Sahrens */ 749fa9e4066Sahrens if (spa->spa_sync_on) { 750fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 751fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 752fa9e4066Sahrens } 753fa9e4066Sahrens 754fa9e4066Sahrens /* 755e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 756fa9e4066Sahrens */ 75754d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 75854d692b7SGeorge Wilson (void) zio_wait(spa->spa_async_zio_root); 75954d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 76054d692b7SGeorge Wilson } 761fa9e4066Sahrens 762fa9e4066Sahrens /* 763fa9e4066Sahrens * Close the dsl pool. 764fa9e4066Sahrens */ 765fa9e4066Sahrens if (spa->spa_dsl_pool) { 766fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 767fa9e4066Sahrens spa->spa_dsl_pool = NULL; 768fa9e4066Sahrens } 769fa9e4066Sahrens 7708ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7718ad4d6ddSJeff Bonwick 7728ad4d6ddSJeff Bonwick /* 7738ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 7748ad4d6ddSJeff Bonwick */ 7758ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 7768ad4d6ddSJeff Bonwick 777fa9e4066Sahrens /* 778fa9e4066Sahrens * Close all vdevs. 779fa9e4066Sahrens */ 7800e34b6a7Sbonwick if (spa->spa_root_vdev) 781fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 7820e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 783ea8dc4b6Seschrock 784fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 785fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 786fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 787fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 788fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 789fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 79099653d4eSeschrock } 791fa94a07fSbrendan if (spa->spa_spares.sav_config) { 792fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 793fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 794fa94a07fSbrendan } 7952ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 796fa94a07fSbrendan 797fa94a07fSbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 798fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 799fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 800fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 801fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 802fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 803fa94a07fSbrendan } 804fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 805fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 806fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 80799653d4eSeschrock } 8082ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 80999653d4eSeschrock 810ea8dc4b6Seschrock spa->spa_async_suspended = 0; 8118ad4d6ddSJeff Bonwick 8128ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 813fa9e4066Sahrens } 814fa9e4066Sahrens 81599653d4eSeschrock /* 81699653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 81799653d4eSeschrock * this pool. When this is called, we have some form of basic information in 818fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 819fa94a07fSbrendan * then re-generate a more complete list including status information. 82099653d4eSeschrock */ 82199653d4eSeschrock static void 82299653d4eSeschrock spa_load_spares(spa_t *spa) 82399653d4eSeschrock { 82499653d4eSeschrock nvlist_t **spares; 82599653d4eSeschrock uint_t nspares; 82699653d4eSeschrock int i; 82739c23413Seschrock vdev_t *vd, *tvd; 82899653d4eSeschrock 829e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 830e14bb325SJeff Bonwick 83199653d4eSeschrock /* 83299653d4eSeschrock * First, close and free any existing spare vdevs. 83399653d4eSeschrock */ 834fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 835fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 83639c23413Seschrock 83739c23413Seschrock /* Undo the call to spa_activate() below */ 838c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 839c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 84039c23413Seschrock spa_spare_remove(tvd); 84139c23413Seschrock vdev_close(vd); 84239c23413Seschrock vdev_free(vd); 84399653d4eSeschrock } 84439c23413Seschrock 845fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 846fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 847fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 84899653d4eSeschrock 849fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 85099653d4eSeschrock nspares = 0; 85199653d4eSeschrock else 852fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 85399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 85499653d4eSeschrock 855fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 856fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 85799653d4eSeschrock 85899653d4eSeschrock if (nspares == 0) 85999653d4eSeschrock return; 86099653d4eSeschrock 86199653d4eSeschrock /* 86299653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 86339c23413Seschrock * process. For each spare, there is potentially two different vdev_t 86439c23413Seschrock * structures associated with it: one in the list of spares (used only 86539c23413Seschrock * for basic validation purposes) and one in the active vdev 86639c23413Seschrock * configuration (if it's spared in). During this phase we open and 86739c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 86839c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 86999653d4eSeschrock */ 870fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 871fa94a07fSbrendan KM_SLEEP); 872fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 87399653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 87499653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 87599653d4eSeschrock ASSERT(vd != NULL); 87699653d4eSeschrock 877fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 87899653d4eSeschrock 879c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 880c5904d13Seschrock B_FALSE)) != NULL) { 88139c23413Seschrock if (!tvd->vdev_isspare) 88239c23413Seschrock spa_spare_add(tvd); 88339c23413Seschrock 88439c23413Seschrock /* 88539c23413Seschrock * We only mark the spare active if we were successfully 88639c23413Seschrock * able to load the vdev. Otherwise, importing a pool 88739c23413Seschrock * with a bad active spare would result in strange 88839c23413Seschrock * behavior, because multiple pool would think the spare 88939c23413Seschrock * is actively in use. 89039c23413Seschrock * 89139c23413Seschrock * There is a vulnerability here to an equally bizarre 89239c23413Seschrock * circumstance, where a dead active spare is later 89339c23413Seschrock * brought back to life (onlined or otherwise). Given 89439c23413Seschrock * the rarity of this scenario, and the extra complexity 89539c23413Seschrock * it adds, we ignore the possibility. 89639c23413Seschrock */ 89739c23413Seschrock if (!vdev_is_dead(tvd)) 89839c23413Seschrock spa_spare_activate(tvd); 89939c23413Seschrock } 90039c23413Seschrock 901e14bb325SJeff Bonwick vd->vdev_top = vd; 9026809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 903e14bb325SJeff Bonwick 90499653d4eSeschrock if (vdev_open(vd) != 0) 90599653d4eSeschrock continue; 90699653d4eSeschrock 907fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 908fa94a07fSbrendan spa_spare_add(vd); 90999653d4eSeschrock } 91099653d4eSeschrock 91199653d4eSeschrock /* 91299653d4eSeschrock * Recompute the stashed list of spares, with status information 91399653d4eSeschrock * this time. 91499653d4eSeschrock */ 915fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 91699653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 91799653d4eSeschrock 918fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 919fa94a07fSbrendan KM_SLEEP); 920fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 921fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 922fa94a07fSbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 923fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 924fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 925fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 92699653d4eSeschrock nvlist_free(spares[i]); 927fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 928fa94a07fSbrendan } 929fa94a07fSbrendan 930fa94a07fSbrendan /* 931fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 932fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 933fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 934fa94a07fSbrendan * then re-generate a more complete list including status information. 935fa94a07fSbrendan * Devices which are already active have their details maintained, and are 936fa94a07fSbrendan * not re-opened. 937fa94a07fSbrendan */ 938fa94a07fSbrendan static void 939fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 940fa94a07fSbrendan { 941fa94a07fSbrendan nvlist_t **l2cache; 942fa94a07fSbrendan uint_t nl2cache; 943fa94a07fSbrendan int i, j, oldnvdevs; 944*573ca77eSGeorge Wilson uint64_t guid; 945fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 946fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 947fa94a07fSbrendan 948e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 949e14bb325SJeff Bonwick 950fa94a07fSbrendan if (sav->sav_config != NULL) { 951fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 952fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 953fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 954fa94a07fSbrendan } else { 955fa94a07fSbrendan nl2cache = 0; 956fa94a07fSbrendan } 957fa94a07fSbrendan 958fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 959fa94a07fSbrendan oldnvdevs = sav->sav_count; 960fa94a07fSbrendan sav->sav_vdevs = NULL; 961fa94a07fSbrendan sav->sav_count = 0; 962fa94a07fSbrendan 963fa94a07fSbrendan /* 964fa94a07fSbrendan * Process new nvlist of vdevs. 965fa94a07fSbrendan */ 966fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 967fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 968fa94a07fSbrendan &guid) == 0); 969fa94a07fSbrendan 970fa94a07fSbrendan newvdevs[i] = NULL; 971fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 972fa94a07fSbrendan vd = oldvdevs[j]; 973fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 974fa94a07fSbrendan /* 975fa94a07fSbrendan * Retain previous vdev for add/remove ops. 976fa94a07fSbrendan */ 977fa94a07fSbrendan newvdevs[i] = vd; 978fa94a07fSbrendan oldvdevs[j] = NULL; 979fa94a07fSbrendan break; 980fa94a07fSbrendan } 981fa94a07fSbrendan } 982fa94a07fSbrendan 983fa94a07fSbrendan if (newvdevs[i] == NULL) { 984fa94a07fSbrendan /* 985fa94a07fSbrendan * Create new vdev 986fa94a07fSbrendan */ 987fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 988fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 989fa94a07fSbrendan ASSERT(vd != NULL); 990fa94a07fSbrendan newvdevs[i] = vd; 991fa94a07fSbrendan 992fa94a07fSbrendan /* 993fa94a07fSbrendan * Commit this vdev as an l2cache device, 994fa94a07fSbrendan * even if it fails to open. 995fa94a07fSbrendan */ 996fa94a07fSbrendan spa_l2cache_add(vd); 997fa94a07fSbrendan 998c5904d13Seschrock vd->vdev_top = vd; 999c5904d13Seschrock vd->vdev_aux = sav; 1000c5904d13Seschrock 1001c5904d13Seschrock spa_l2cache_activate(vd); 1002c5904d13Seschrock 1003fa94a07fSbrendan if (vdev_open(vd) != 0) 1004fa94a07fSbrendan continue; 1005fa94a07fSbrendan 1006fa94a07fSbrendan (void) vdev_validate_aux(vd); 1007fa94a07fSbrendan 1008*573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1009*573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1010fa94a07fSbrendan } 1011fa94a07fSbrendan } 1012fa94a07fSbrendan 1013fa94a07fSbrendan /* 1014fa94a07fSbrendan * Purge vdevs that were dropped 1015fa94a07fSbrendan */ 1016fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1017fa94a07fSbrendan uint64_t pool; 1018fa94a07fSbrendan 1019fa94a07fSbrendan vd = oldvdevs[i]; 1020fa94a07fSbrendan if (vd != NULL) { 10218ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 10228ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1023fa94a07fSbrendan l2arc_remove_vdev(vd); 1024fa94a07fSbrendan (void) vdev_close(vd); 1025fa94a07fSbrendan spa_l2cache_remove(vd); 1026fa94a07fSbrendan } 1027fa94a07fSbrendan } 1028fa94a07fSbrendan 1029fa94a07fSbrendan if (oldvdevs) 1030fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1031fa94a07fSbrendan 1032fa94a07fSbrendan if (sav->sav_config == NULL) 1033fa94a07fSbrendan goto out; 1034fa94a07fSbrendan 1035fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1036fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1037fa94a07fSbrendan 1038fa94a07fSbrendan /* 1039fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1040fa94a07fSbrendan * information this time. 1041fa94a07fSbrendan */ 1042fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1043fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1044fa94a07fSbrendan 1045fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1046fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1047fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 1048fa94a07fSbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 1049fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1050fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1051fa94a07fSbrendan out: 1052fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1053fa94a07fSbrendan nvlist_free(l2cache[i]); 1054fa94a07fSbrendan if (sav->sav_count) 1055fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 105699653d4eSeschrock } 105799653d4eSeschrock 105899653d4eSeschrock static int 105999653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 106099653d4eSeschrock { 106199653d4eSeschrock dmu_buf_t *db; 106299653d4eSeschrock char *packed = NULL; 106399653d4eSeschrock size_t nvsize = 0; 106499653d4eSeschrock int error; 106599653d4eSeschrock *value = NULL; 106699653d4eSeschrock 106799653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 106899653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 106999653d4eSeschrock dmu_buf_rele(db, FTAG); 107099653d4eSeschrock 107199653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 10727bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 10737bfdf011SNeil Perrin DMU_READ_PREFETCH); 107499653d4eSeschrock if (error == 0) 107599653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 107699653d4eSeschrock kmem_free(packed, nvsize); 107799653d4eSeschrock 107899653d4eSeschrock return (error); 107999653d4eSeschrock } 108099653d4eSeschrock 10813d7072f8Seschrock /* 10823d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 10833d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 10843d7072f8Seschrock */ 10853d7072f8Seschrock static void 10863d7072f8Seschrock spa_check_removed(vdev_t *vd) 10873d7072f8Seschrock { 1088*573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 10893d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 10903d7072f8Seschrock 10913d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 10923d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 10933d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 10943d7072f8Seschrock } 10953d7072f8Seschrock } 10963d7072f8Seschrock 1097e6ca193dSGeorge Wilson /* 1098e6ca193dSGeorge Wilson * Load the slog device state from the config object since it's possible 1099e6ca193dSGeorge Wilson * that the label does not contain the most up-to-date information. 1100e6ca193dSGeorge Wilson */ 1101e6ca193dSGeorge Wilson void 1102e6ca193dSGeorge Wilson spa_load_log_state(spa_t *spa) 1103e6ca193dSGeorge Wilson { 1104e6ca193dSGeorge Wilson nvlist_t *nv, *nvroot, **child; 1105e6ca193dSGeorge Wilson uint64_t is_log; 1106*573ca77eSGeorge Wilson uint_t children; 1107e6ca193dSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 1108e6ca193dSGeorge Wilson 1109e6ca193dSGeorge Wilson VERIFY(load_nvlist(spa, spa->spa_config_object, &nv) == 0); 1110e6ca193dSGeorge Wilson VERIFY(nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1111e6ca193dSGeorge Wilson VERIFY(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1112e6ca193dSGeorge Wilson &child, &children) == 0); 1113e6ca193dSGeorge Wilson 1114*573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 1115e6ca193dSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 1116e6ca193dSGeorge Wilson 1117e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1118e6ca193dSGeorge Wilson &is_log) == 0 && is_log) 1119e6ca193dSGeorge Wilson vdev_load_log_state(tvd, child[c]); 1120e6ca193dSGeorge Wilson } 1121e6ca193dSGeorge Wilson nvlist_free(nv); 1122e6ca193dSGeorge Wilson } 1123e6ca193dSGeorge Wilson 1124b87f3af3Sperrin /* 1125b87f3af3Sperrin * Check for missing log devices 1126b87f3af3Sperrin */ 1127b87f3af3Sperrin int 1128b87f3af3Sperrin spa_check_logs(spa_t *spa) 1129b87f3af3Sperrin { 1130b87f3af3Sperrin switch (spa->spa_log_state) { 1131b87f3af3Sperrin case SPA_LOG_MISSING: 1132b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1133b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1134b87f3af3Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1135b87f3af3Sperrin DS_FIND_CHILDREN)) { 1136b87f3af3Sperrin spa->spa_log_state = SPA_LOG_MISSING; 1137b87f3af3Sperrin return (1); 1138b87f3af3Sperrin } 1139b87f3af3Sperrin break; 1140b87f3af3Sperrin } 1141b87f3af3Sperrin return (0); 1142b87f3af3Sperrin } 1143b87f3af3Sperrin 1144fa9e4066Sahrens /* 1145fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 1146ea8dc4b6Seschrock * source of configuration information. 1147fa9e4066Sahrens */ 1148fa9e4066Sahrens static int 1149ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 1150fa9e4066Sahrens { 1151fa9e4066Sahrens int error = 0; 1152fa9e4066Sahrens nvlist_t *nvroot = NULL; 1153fa9e4066Sahrens vdev_t *rvd; 1154fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 11550373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1156fa9e4066Sahrens uint64_t pool_guid; 115799653d4eSeschrock uint64_t version; 11583d7072f8Seschrock uint64_t autoreplace = 0; 11598ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 1160b87f3af3Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1161fa9e4066Sahrens 11628ad4d6ddSJeff Bonwick /* 11638ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 11648ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 11658ad4d6ddSJeff Bonwick */ 11668ad4d6ddSJeff Bonwick if (!mosconfig) 11678ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 11688ad4d6ddSJeff Bonwick 1169e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1170e14bb325SJeff Bonwick 1171ea8dc4b6Seschrock spa->spa_load_state = state; 11720373e76bSbonwick 1173fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1174a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 1175ea8dc4b6Seschrock error = EINVAL; 1176ea8dc4b6Seschrock goto out; 1177ea8dc4b6Seschrock } 1178fa9e4066Sahrens 117999653d4eSeschrock /* 118099653d4eSeschrock * Versioning wasn't explicitly added to the label until later, so if 118199653d4eSeschrock * it's not present treat it as the initial version. 118299653d4eSeschrock */ 118399653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 1184e7437265Sahrens version = SPA_VERSION_INITIAL; 118599653d4eSeschrock 1186a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 1187a9926bf0Sbonwick &spa->spa_config_txg); 1188a9926bf0Sbonwick 11890373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 1190ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 1191ea8dc4b6Seschrock error = EEXIST; 1192ea8dc4b6Seschrock goto out; 1193ea8dc4b6Seschrock } 1194fa9e4066Sahrens 1195b5989ec7Seschrock spa->spa_load_guid = pool_guid; 1196b5989ec7Seschrock 119754d692b7SGeorge Wilson /* 119854d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 119954d692b7SGeorge Wilson */ 120025f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 120125f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 120254d692b7SGeorge Wilson 1203fa9e4066Sahrens /* 120499653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 120599653d4eSeschrock * value that will be returned by spa_version() since parsing the 120699653d4eSeschrock * configuration requires knowing the version number. 1207fa9e4066Sahrens */ 1208e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 120999653d4eSeschrock spa->spa_ubsync.ub_version = version; 121099653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 1211e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1212fa9e4066Sahrens 121399653d4eSeschrock if (error != 0) 1214ea8dc4b6Seschrock goto out; 1215fa9e4066Sahrens 12160e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1217fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 1218fa9e4066Sahrens 1219fa9e4066Sahrens /* 1220fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 1221fa9e4066Sahrens */ 1222e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12230bf246f5Smc error = vdev_open(rvd); 1224e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 12250bf246f5Smc if (error != 0) 1226ea8dc4b6Seschrock goto out; 1227fa9e4066Sahrens 1228560e6e96Seschrock /* 122977e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 123077e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 123177e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 123277e3a39cSMark J Musante * that config. Otherwise, we're validating against the cached config 123377e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 123477e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 123577e3a39cSMark J Musante * the vdev config. 1236560e6e96Seschrock */ 123777e3a39cSMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 123877e3a39cSMark J Musante error = vdev_validate(rvd); 123977e3a39cSMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 124077e3a39cSMark J Musante if (error != 0) 124177e3a39cSMark J Musante goto out; 1242560e6e96Seschrock 1243560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1244560e6e96Seschrock error = ENXIO; 1245560e6e96Seschrock goto out; 1246560e6e96Seschrock } 1247560e6e96Seschrock 1248fa9e4066Sahrens /* 1249fa9e4066Sahrens * Find the best uberblock. 1250fa9e4066Sahrens */ 1251e14bb325SJeff Bonwick vdev_uberblock_load(NULL, rvd, ub); 1252fa9e4066Sahrens 1253fa9e4066Sahrens /* 1254fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 1255fa9e4066Sahrens */ 1256fa9e4066Sahrens if (ub->ub_txg == 0) { 1257eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1258eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 1259ea8dc4b6Seschrock error = ENXIO; 1260ea8dc4b6Seschrock goto out; 1261ea8dc4b6Seschrock } 1262ea8dc4b6Seschrock 1263ea8dc4b6Seschrock /* 1264ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 1265ea8dc4b6Seschrock */ 1266e7437265Sahrens if (ub->ub_version > SPA_VERSION) { 1267eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1268eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 1269ea8dc4b6Seschrock error = ENOTSUP; 1270ea8dc4b6Seschrock goto out; 1271fa9e4066Sahrens } 1272fa9e4066Sahrens 1273fa9e4066Sahrens /* 1274fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1275fa9e4066Sahrens * incomplete configuration. 1276fa9e4066Sahrens */ 1277ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 1278ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1279ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 1280ea8dc4b6Seschrock error = ENXIO; 1281ea8dc4b6Seschrock goto out; 1282fa9e4066Sahrens } 1283fa9e4066Sahrens 1284fa9e4066Sahrens /* 1285fa9e4066Sahrens * Initialize internal SPA structures. 1286fa9e4066Sahrens */ 1287fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1288fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1289fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 1290ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 1291ea8dc4b6Seschrock if (error) { 1292ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1293ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1294ea8dc4b6Seschrock goto out; 1295ea8dc4b6Seschrock } 1296fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1297fa9e4066Sahrens 1298ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1299fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1300ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 1301ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1302ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1303ea8dc4b6Seschrock error = EIO; 1304ea8dc4b6Seschrock goto out; 1305ea8dc4b6Seschrock } 1306fa9e4066Sahrens 1307fa9e4066Sahrens if (!mosconfig) { 130899653d4eSeschrock nvlist_t *newconfig; 130995173954Sek uint64_t hostid; 1310fa9e4066Sahrens 131199653d4eSeschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 1312ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1313ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1314ea8dc4b6Seschrock error = EIO; 1315ea8dc4b6Seschrock goto out; 1316ea8dc4b6Seschrock } 1317fa9e4066Sahrens 131877650510SLin Ling if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 131977650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 132095173954Sek char *hostname; 132195173954Sek unsigned long myhostid = 0; 132295173954Sek 132395173954Sek VERIFY(nvlist_lookup_string(newconfig, 132495173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 132595173954Sek 13265679c89fSjv #ifdef _KERNEL 13275679c89fSjv myhostid = zone_get_hostid(NULL); 13285679c89fSjv #else /* _KERNEL */ 13295679c89fSjv /* 13305679c89fSjv * We're emulating the system's hostid in userland, so 13315679c89fSjv * we can't use zone_get_hostid(). 13325679c89fSjv */ 133395173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 13345679c89fSjv #endif /* _KERNEL */ 133517194a52Slling if (hostid != 0 && myhostid != 0 && 13365679c89fSjv hostid != myhostid) { 133795173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 133895173954Sek "loaded as it was last accessed by " 133977650510SLin Ling "another system (host: %s hostid: 0x%lx). " 134095173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 1341e14bb325SJeff Bonwick spa_name(spa), hostname, 134295173954Sek (unsigned long)hostid); 134395173954Sek error = EBADF; 134495173954Sek goto out; 134595173954Sek } 134695173954Sek } 134795173954Sek 1348fa9e4066Sahrens spa_config_set(spa, newconfig); 1349fa9e4066Sahrens spa_unload(spa); 1350fa9e4066Sahrens spa_deactivate(spa); 13518ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 1352fa9e4066Sahrens 1353ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 1354fa9e4066Sahrens } 1355fa9e4066Sahrens 1356ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1357fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1358ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 1359ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1360ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1361ea8dc4b6Seschrock error = EIO; 1362ea8dc4b6Seschrock goto out; 1363ea8dc4b6Seschrock } 1364fa9e4066Sahrens 136599653d4eSeschrock /* 136699653d4eSeschrock * Load the bit that tells us to use the new accounting function 136799653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 136899653d4eSeschrock * be present. 136999653d4eSeschrock */ 137099653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, 137199653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 137299653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate); 137399653d4eSeschrock if (error != 0 && error != ENOENT) { 137499653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 137599653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 137699653d4eSeschrock error = EIO; 137799653d4eSeschrock goto out; 137899653d4eSeschrock } 137999653d4eSeschrock 1380fa9e4066Sahrens /* 1381ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1382ea8dc4b6Seschrock * not be present. 1383fa9e4066Sahrens */ 1384ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1385ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 1386ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 1387d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 1388ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1389ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1390ea8dc4b6Seschrock error = EIO; 1391ea8dc4b6Seschrock goto out; 1392ea8dc4b6Seschrock } 1393ea8dc4b6Seschrock 1394ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1395ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1396ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1397ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 1398ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1399ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1400ea8dc4b6Seschrock error = EIO; 1401ea8dc4b6Seschrock goto out; 1402ea8dc4b6Seschrock } 1403ea8dc4b6Seschrock 140406eeb2adSek /* 140506eeb2adSek * Load the history object. If we have an older pool, this 140606eeb2adSek * will not be present. 140706eeb2adSek */ 140806eeb2adSek error = zap_lookup(spa->spa_meta_objset, 140906eeb2adSek DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 141006eeb2adSek sizeof (uint64_t), 1, &spa->spa_history); 141106eeb2adSek if (error != 0 && error != ENOENT) { 141206eeb2adSek vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 141306eeb2adSek VDEV_AUX_CORRUPT_DATA); 141406eeb2adSek error = EIO; 141506eeb2adSek goto out; 141606eeb2adSek } 141706eeb2adSek 141899653d4eSeschrock /* 141999653d4eSeschrock * Load any hot spares for this pool. 142099653d4eSeschrock */ 142199653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1422fa94a07fSbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 142399653d4eSeschrock if (error != 0 && error != ENOENT) { 142499653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 142599653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 142699653d4eSeschrock error = EIO; 142799653d4eSeschrock goto out; 142899653d4eSeschrock } 142999653d4eSeschrock if (error == 0) { 1430e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 1431fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 1432fa94a07fSbrendan &spa->spa_spares.sav_config) != 0) { 143399653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 143499653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 143599653d4eSeschrock error = EIO; 143699653d4eSeschrock goto out; 143799653d4eSeschrock } 143899653d4eSeschrock 1439e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 144099653d4eSeschrock spa_load_spares(spa); 1441e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 144299653d4eSeschrock } 144399653d4eSeschrock 1444fa94a07fSbrendan /* 1445fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 1446fa94a07fSbrendan */ 1447fa94a07fSbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1448fa94a07fSbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 1449fa94a07fSbrendan &spa->spa_l2cache.sav_object); 1450fa94a07fSbrendan if (error != 0 && error != ENOENT) { 1451fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1452fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1453fa94a07fSbrendan error = EIO; 1454fa94a07fSbrendan goto out; 1455fa94a07fSbrendan } 1456fa94a07fSbrendan if (error == 0) { 1457fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 1458fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 1459fa94a07fSbrendan &spa->spa_l2cache.sav_config) != 0) { 1460fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, 1461fa94a07fSbrendan VDEV_STATE_CANT_OPEN, 1462fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1463fa94a07fSbrendan error = EIO; 1464fa94a07fSbrendan goto out; 1465fa94a07fSbrendan } 1466fa94a07fSbrendan 1467e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1468fa94a07fSbrendan spa_load_l2cache(spa); 1469e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1470fa94a07fSbrendan } 1471fa94a07fSbrendan 1472e6ca193dSGeorge Wilson spa_load_log_state(spa); 1473e6ca193dSGeorge Wilson 1474b87f3af3Sperrin if (spa_check_logs(spa)) { 1475b87f3af3Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1476b87f3af3Sperrin VDEV_AUX_BAD_LOG); 1477b87f3af3Sperrin error = ENXIO; 1478b87f3af3Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 1479b87f3af3Sperrin goto out; 1480b87f3af3Sperrin } 1481b87f3af3Sperrin 1482b87f3af3Sperrin 1483990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1484ecd6cf80Smarks 1485b1b8ab34Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1486b1b8ab34Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1487b1b8ab34Slling 1488b1b8ab34Slling if (error && error != ENOENT) { 1489b1b8ab34Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1490b1b8ab34Slling VDEV_AUX_CORRUPT_DATA); 1491b1b8ab34Slling error = EIO; 1492b1b8ab34Slling goto out; 1493b1b8ab34Slling } 1494b1b8ab34Slling 1495b1b8ab34Slling if (error == 0) { 1496b1b8ab34Slling (void) zap_lookup(spa->spa_meta_objset, 1497b1b8ab34Slling spa->spa_pool_props_object, 14983d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1499b1b8ab34Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 15003d7072f8Seschrock (void) zap_lookup(spa->spa_meta_objset, 15013d7072f8Seschrock spa->spa_pool_props_object, 15023d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 15033d7072f8Seschrock sizeof (uint64_t), 1, &autoreplace); 1504ecd6cf80Smarks (void) zap_lookup(spa->spa_meta_objset, 1505ecd6cf80Smarks spa->spa_pool_props_object, 1506ecd6cf80Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1507ecd6cf80Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 15080a4e9518Sgw (void) zap_lookup(spa->spa_meta_objset, 15090a4e9518Sgw spa->spa_pool_props_object, 15100a4e9518Sgw zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 15110a4e9518Sgw sizeof (uint64_t), 1, &spa->spa_failmode); 1512*573ca77eSGeorge Wilson (void) zap_lookup(spa->spa_meta_objset, 1513*573ca77eSGeorge Wilson spa->spa_pool_props_object, 1514*573ca77eSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND), 1515*573ca77eSGeorge Wilson sizeof (uint64_t), 1, &spa->spa_autoexpand); 1516b1b8ab34Slling } 1517b1b8ab34Slling 15183d7072f8Seschrock /* 15193d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 15203d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 15213d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 15223d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 15233d7072f8Seschrock * over. 15243d7072f8Seschrock */ 1525b01c3b58Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 15263d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 15273d7072f8Seschrock 1528ea8dc4b6Seschrock /* 1529560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1530ea8dc4b6Seschrock */ 1531560e6e96Seschrock vdev_load(rvd); 15320373e76bSbonwick 1533fa9e4066Sahrens /* 1534fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1535fa9e4066Sahrens */ 1536e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1537fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1538e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1539fa9e4066Sahrens 1540fa9e4066Sahrens /* 1541fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1542fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1543fa9e4066Sahrens */ 1544ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1545ea8dc4b6Seschrock error = ENXIO; 1546ea8dc4b6Seschrock goto out; 1547ea8dc4b6Seschrock } 1548fa9e4066Sahrens 15498ad4d6ddSJeff Bonwick if (spa_writeable(spa)) { 15505dabedeeSbonwick dmu_tx_t *tx; 15510373e76bSbonwick int need_update = B_FALSE; 15528ad4d6ddSJeff Bonwick 15538ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 15545dabedeeSbonwick 15550373e76bSbonwick /* 15560373e76bSbonwick * Claim log blocks that haven't been committed yet. 15570373e76bSbonwick * This must all happen in a single txg. 15580373e76bSbonwick */ 15595dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1560fa9e4066Sahrens spa_first_txg(spa)); 1561e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 15620b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1563fa9e4066Sahrens dmu_tx_commit(tx); 1564fa9e4066Sahrens 1565e6ca193dSGeorge Wilson spa->spa_log_state = SPA_LOG_GOOD; 1566fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1567fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1568fa9e4066Sahrens 1569fa9e4066Sahrens /* 1570fa9e4066Sahrens * Wait for all claims to sync. 1571fa9e4066Sahrens */ 1572fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 15730e34b6a7Sbonwick 15740e34b6a7Sbonwick /* 15750373e76bSbonwick * If the config cache is stale, or we have uninitialized 15760373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 15770e34b6a7Sbonwick */ 15780373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 15790373e76bSbonwick state == SPA_LOAD_IMPORT) 15800373e76bSbonwick need_update = B_TRUE; 15810373e76bSbonwick 15828ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 15830373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 15840373e76bSbonwick need_update = B_TRUE; 15850e34b6a7Sbonwick 15860e34b6a7Sbonwick /* 15870373e76bSbonwick * Update the config cache asychronously in case we're the 15880373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 15890e34b6a7Sbonwick */ 15900373e76bSbonwick if (need_update) 15910373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 15928ad4d6ddSJeff Bonwick 15938ad4d6ddSJeff Bonwick /* 15948ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 15958ad4d6ddSJeff Bonwick */ 15968ad4d6ddSJeff Bonwick if (vdev_resilver_needed(rvd, NULL, NULL)) 15978ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1598fa9e4066Sahrens } 1599fa9e4066Sahrens 1600ea8dc4b6Seschrock error = 0; 1601ea8dc4b6Seschrock out: 1602088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 160399653d4eSeschrock if (error && error != EBADF) 1604b87f3af3Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 1605ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 1606ea8dc4b6Seschrock spa->spa_ena = 0; 1607ea8dc4b6Seschrock 1608ea8dc4b6Seschrock return (error); 1609fa9e4066Sahrens } 1610fa9e4066Sahrens 1611fa9e4066Sahrens /* 1612fa9e4066Sahrens * Pool Open/Import 1613fa9e4066Sahrens * 1614fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 1615fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1616fa9e4066Sahrens * case of an open, the pool configuration will exist in the 16173d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 1618fa9e4066Sahrens * 1619fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1620fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 1621fa9e4066Sahrens * ambiguous state. 1622fa9e4066Sahrens */ 1623fa9e4066Sahrens static int 1624fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1625fa9e4066Sahrens { 1626fa9e4066Sahrens spa_t *spa; 1627fa9e4066Sahrens int error; 1628fa9e4066Sahrens int locked = B_FALSE; 1629fa9e4066Sahrens 1630fa9e4066Sahrens *spapp = NULL; 1631fa9e4066Sahrens 1632fa9e4066Sahrens /* 1633fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 1634fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1635fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 1636fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 1637fa9e4066Sahrens */ 1638fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1639fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1640fa9e4066Sahrens locked = B_TRUE; 1641fa9e4066Sahrens } 1642fa9e4066Sahrens 1643fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1644fa9e4066Sahrens if (locked) 1645fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1646fa9e4066Sahrens return (ENOENT); 1647fa9e4066Sahrens } 1648fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1649fa9e4066Sahrens 16508ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 1651fa9e4066Sahrens 16520373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1653fa9e4066Sahrens 1654fa9e4066Sahrens if (error == EBADF) { 1655fa9e4066Sahrens /* 1656560e6e96Seschrock * If vdev_validate() returns failure (indicated by 1657560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 1658560e6e96Seschrock * that the pool has been exported or destroyed. If 1659560e6e96Seschrock * this is the case, the config cache is out of sync and 1660560e6e96Seschrock * we should remove the pool from the namespace. 1661fa9e4066Sahrens */ 1662fa9e4066Sahrens spa_unload(spa); 1663fa9e4066Sahrens spa_deactivate(spa); 1664c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1665fa9e4066Sahrens spa_remove(spa); 1666fa9e4066Sahrens if (locked) 1667fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1668fa9e4066Sahrens return (ENOENT); 1669ea8dc4b6Seschrock } 1670ea8dc4b6Seschrock 1671ea8dc4b6Seschrock if (error) { 1672fa9e4066Sahrens /* 1673fa9e4066Sahrens * We can't open the pool, but we still have useful 1674fa9e4066Sahrens * information: the state of each vdev after the 1675fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 1676fa9e4066Sahrens */ 1677e14bb325SJeff Bonwick if (config != NULL && spa->spa_root_vdev != NULL) 1678fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1679fa9e4066Sahrens B_TRUE); 1680fa9e4066Sahrens spa_unload(spa); 1681fa9e4066Sahrens spa_deactivate(spa); 1682ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 1683fa9e4066Sahrens if (locked) 1684fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1685fa9e4066Sahrens *spapp = NULL; 1686fa9e4066Sahrens return (error); 1687ea8dc4b6Seschrock } else { 1688ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 1689fa9e4066Sahrens } 1690fa9e4066Sahrens } 1691fa9e4066Sahrens 1692fa9e4066Sahrens spa_open_ref(spa, tag); 16933d7072f8Seschrock 1694fa9e4066Sahrens if (locked) 1695fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1696fa9e4066Sahrens 1697fa9e4066Sahrens *spapp = spa; 1698fa9e4066Sahrens 1699e14bb325SJeff Bonwick if (config != NULL) 1700fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1701fa9e4066Sahrens 1702fa9e4066Sahrens return (0); 1703fa9e4066Sahrens } 1704fa9e4066Sahrens 1705fa9e4066Sahrens int 1706fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1707fa9e4066Sahrens { 1708fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1709fa9e4066Sahrens } 1710fa9e4066Sahrens 1711ea8dc4b6Seschrock /* 1712ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 1713ea8dc4b6Seschrock * preventing it from being exported or destroyed. 1714ea8dc4b6Seschrock */ 1715ea8dc4b6Seschrock spa_t * 1716ea8dc4b6Seschrock spa_inject_addref(char *name) 1717ea8dc4b6Seschrock { 1718ea8dc4b6Seschrock spa_t *spa; 1719ea8dc4b6Seschrock 1720ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1721ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 1722ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1723ea8dc4b6Seschrock return (NULL); 1724ea8dc4b6Seschrock } 1725ea8dc4b6Seschrock spa->spa_inject_ref++; 1726ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1727ea8dc4b6Seschrock 1728ea8dc4b6Seschrock return (spa); 1729ea8dc4b6Seschrock } 1730ea8dc4b6Seschrock 1731ea8dc4b6Seschrock void 1732ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 1733ea8dc4b6Seschrock { 1734ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1735ea8dc4b6Seschrock spa->spa_inject_ref--; 1736ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1737ea8dc4b6Seschrock } 1738ea8dc4b6Seschrock 1739fa94a07fSbrendan /* 1740fa94a07fSbrendan * Add spares device information to the nvlist. 1741fa94a07fSbrendan */ 174299653d4eSeschrock static void 174399653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 174499653d4eSeschrock { 174599653d4eSeschrock nvlist_t **spares; 174699653d4eSeschrock uint_t i, nspares; 174799653d4eSeschrock nvlist_t *nvroot; 174899653d4eSeschrock uint64_t guid; 174999653d4eSeschrock vdev_stat_t *vs; 175099653d4eSeschrock uint_t vsc; 175139c23413Seschrock uint64_t pool; 175299653d4eSeschrock 17536809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 17546809eb4eSEric Schrock 1755fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 175699653d4eSeschrock return; 175799653d4eSeschrock 175899653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 175999653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1760fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 176199653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 176299653d4eSeschrock if (nspares != 0) { 176399653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 176499653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 176599653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 176699653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 176799653d4eSeschrock 176899653d4eSeschrock /* 176999653d4eSeschrock * Go through and find any spares which have since been 177099653d4eSeschrock * repurposed as an active spare. If this is the case, update 177199653d4eSeschrock * their status appropriately. 177299653d4eSeschrock */ 177399653d4eSeschrock for (i = 0; i < nspares; i++) { 177499653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 177599653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 177689a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 177789a89ebfSlling pool != 0ULL) { 177899653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 177999653d4eSeschrock spares[i], ZPOOL_CONFIG_STATS, 178099653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 178199653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 178299653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 178399653d4eSeschrock } 178499653d4eSeschrock } 178599653d4eSeschrock } 178699653d4eSeschrock } 178799653d4eSeschrock 1788fa94a07fSbrendan /* 1789fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 1790fa94a07fSbrendan */ 1791fa94a07fSbrendan static void 1792fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 1793fa94a07fSbrendan { 1794fa94a07fSbrendan nvlist_t **l2cache; 1795fa94a07fSbrendan uint_t i, j, nl2cache; 1796fa94a07fSbrendan nvlist_t *nvroot; 1797fa94a07fSbrendan uint64_t guid; 1798fa94a07fSbrendan vdev_t *vd; 1799fa94a07fSbrendan vdev_stat_t *vs; 1800fa94a07fSbrendan uint_t vsc; 1801fa94a07fSbrendan 18026809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 18036809eb4eSEric Schrock 1804fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 1805fa94a07fSbrendan return; 1806fa94a07fSbrendan 1807fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 1808fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1809fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 1810fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1811fa94a07fSbrendan if (nl2cache != 0) { 1812fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 1813fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 1814fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 1815fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1816fa94a07fSbrendan 1817fa94a07fSbrendan /* 1818fa94a07fSbrendan * Update level 2 cache device stats. 1819fa94a07fSbrendan */ 1820fa94a07fSbrendan 1821fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1822fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 1823fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 1824fa94a07fSbrendan 1825fa94a07fSbrendan vd = NULL; 1826fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 1827fa94a07fSbrendan if (guid == 1828fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 1829fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 1830fa94a07fSbrendan break; 1831fa94a07fSbrendan } 1832fa94a07fSbrendan } 1833fa94a07fSbrendan ASSERT(vd != NULL); 1834fa94a07fSbrendan 1835fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 1836fa94a07fSbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 1837fa94a07fSbrendan vdev_get_stats(vd, vs); 1838fa94a07fSbrendan } 1839fa94a07fSbrendan } 1840fa94a07fSbrendan } 1841fa94a07fSbrendan 1842fa9e4066Sahrens int 1843ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1844fa9e4066Sahrens { 1845fa9e4066Sahrens int error; 1846fa9e4066Sahrens spa_t *spa; 1847fa9e4066Sahrens 1848fa9e4066Sahrens *config = NULL; 1849fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 1850fa9e4066Sahrens 18516809eb4eSEric Schrock if (spa != NULL) { 18526809eb4eSEric Schrock /* 18536809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 18546809eb4eSEric Schrock * or l2cache devices could change and the config would be 18556809eb4eSEric Schrock * self-inconsistent. 18566809eb4eSEric Schrock */ 18576809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 1858ea8dc4b6Seschrock 18596809eb4eSEric Schrock if (*config != NULL) { 1860e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 18616809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 18626809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 1863e14bb325SJeff Bonwick 18646809eb4eSEric Schrock if (spa_suspended(spa)) 18656809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 18666809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 18676809eb4eSEric Schrock spa->spa_failmode) == 0); 18686809eb4eSEric Schrock 18696809eb4eSEric Schrock spa_add_spares(spa, *config); 18706809eb4eSEric Schrock spa_add_l2cache(spa, *config); 18716809eb4eSEric Schrock } 187299653d4eSeschrock } 187399653d4eSeschrock 1874ea8dc4b6Seschrock /* 1875ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 1876ea8dc4b6Seschrock * and call spa_lookup() directly. 1877ea8dc4b6Seschrock */ 1878ea8dc4b6Seschrock if (altroot) { 1879ea8dc4b6Seschrock if (spa == NULL) { 1880ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1881ea8dc4b6Seschrock spa = spa_lookup(name); 1882ea8dc4b6Seschrock if (spa) 1883ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1884ea8dc4b6Seschrock else 1885ea8dc4b6Seschrock altroot[0] = '\0'; 1886ea8dc4b6Seschrock spa = NULL; 1887ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1888ea8dc4b6Seschrock } else { 1889ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1890ea8dc4b6Seschrock } 1891ea8dc4b6Seschrock } 1892ea8dc4b6Seschrock 18936809eb4eSEric Schrock if (spa != NULL) { 18946809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 1895fa9e4066Sahrens spa_close(spa, FTAG); 18966809eb4eSEric Schrock } 1897fa9e4066Sahrens 1898fa9e4066Sahrens return (error); 1899fa9e4066Sahrens } 1900fa9e4066Sahrens 190199653d4eSeschrock /* 1902fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 1903fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 1904fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 1905fa94a07fSbrendan * specified, as long as they are well-formed. 190699653d4eSeschrock */ 190799653d4eSeschrock static int 1908fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 1909fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 1910fa94a07fSbrendan vdev_labeltype_t label) 191199653d4eSeschrock { 1912fa94a07fSbrendan nvlist_t **dev; 1913fa94a07fSbrendan uint_t i, ndev; 191499653d4eSeschrock vdev_t *vd; 191599653d4eSeschrock int error; 191699653d4eSeschrock 1917e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1918e14bb325SJeff Bonwick 191999653d4eSeschrock /* 1920fa94a07fSbrendan * It's acceptable to have no devs specified. 192199653d4eSeschrock */ 1922fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 192399653d4eSeschrock return (0); 192499653d4eSeschrock 1925fa94a07fSbrendan if (ndev == 0) 192699653d4eSeschrock return (EINVAL); 192799653d4eSeschrock 192899653d4eSeschrock /* 1929fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 1930fa94a07fSbrendan * device type. 193199653d4eSeschrock */ 1932fa94a07fSbrendan if (spa_version(spa) < version) 193399653d4eSeschrock return (ENOTSUP); 193499653d4eSeschrock 193539c23413Seschrock /* 1936fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 193739c23413Seschrock * checking. 193839c23413Seschrock */ 1939fa94a07fSbrendan sav->sav_pending = dev; 1940fa94a07fSbrendan sav->sav_npending = ndev; 194139c23413Seschrock 1942fa94a07fSbrendan for (i = 0; i < ndev; i++) { 1943fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 194499653d4eSeschrock mode)) != 0) 194539c23413Seschrock goto out; 194699653d4eSeschrock 194799653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 194899653d4eSeschrock vdev_free(vd); 194939c23413Seschrock error = EINVAL; 195039c23413Seschrock goto out; 195199653d4eSeschrock } 195299653d4eSeschrock 1953fa94a07fSbrendan /* 1954e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 1955e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 1956fa94a07fSbrendan */ 1957e14bb325SJeff Bonwick #ifdef _KERNEL 1958fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 1959fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 1960fa94a07fSbrendan error = ENOTBLK; 1961fa94a07fSbrendan goto out; 1962fa94a07fSbrendan } 1963e14bb325SJeff Bonwick #endif 196499653d4eSeschrock vd->vdev_top = vd; 196599653d4eSeschrock 196639c23413Seschrock if ((error = vdev_open(vd)) == 0 && 1967fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 1968fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 196939c23413Seschrock vd->vdev_guid) == 0); 197039c23413Seschrock } 197199653d4eSeschrock 197299653d4eSeschrock vdev_free(vd); 197339c23413Seschrock 1974fa94a07fSbrendan if (error && 1975fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 197639c23413Seschrock goto out; 197739c23413Seschrock else 197839c23413Seschrock error = 0; 197999653d4eSeschrock } 198099653d4eSeschrock 198139c23413Seschrock out: 1982fa94a07fSbrendan sav->sav_pending = NULL; 1983fa94a07fSbrendan sav->sav_npending = 0; 198439c23413Seschrock return (error); 198599653d4eSeschrock } 198699653d4eSeschrock 1987fa94a07fSbrendan static int 1988fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 1989fa94a07fSbrendan { 1990fa94a07fSbrendan int error; 1991fa94a07fSbrendan 1992e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1993e14bb325SJeff Bonwick 1994fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 1995fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 1996fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 1997fa94a07fSbrendan return (error); 1998fa94a07fSbrendan } 1999fa94a07fSbrendan 2000fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2001fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 2002fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 2003fa94a07fSbrendan } 2004fa94a07fSbrendan 2005fa94a07fSbrendan static void 2006fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 2007fa94a07fSbrendan const char *config) 2008fa94a07fSbrendan { 2009fa94a07fSbrendan int i; 2010fa94a07fSbrendan 2011fa94a07fSbrendan if (sav->sav_config != NULL) { 2012fa94a07fSbrendan nvlist_t **olddevs; 2013fa94a07fSbrendan uint_t oldndevs; 2014fa94a07fSbrendan nvlist_t **newdevs; 2015fa94a07fSbrendan 2016fa94a07fSbrendan /* 2017fa94a07fSbrendan * Generate new dev list by concatentating with the 2018fa94a07fSbrendan * current dev list. 2019fa94a07fSbrendan */ 2020fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 2021fa94a07fSbrendan &olddevs, &oldndevs) == 0); 2022fa94a07fSbrendan 2023fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 2024fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 2025fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 2026fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 2027fa94a07fSbrendan KM_SLEEP) == 0); 2028fa94a07fSbrendan for (i = 0; i < ndevs; i++) 2029fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 2030fa94a07fSbrendan KM_SLEEP) == 0); 2031fa94a07fSbrendan 2032fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 2033fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 2034fa94a07fSbrendan 2035fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 2036fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 2037fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 2038fa94a07fSbrendan nvlist_free(newdevs[i]); 2039fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 2040fa94a07fSbrendan } else { 2041fa94a07fSbrendan /* 2042fa94a07fSbrendan * Generate a new dev list. 2043fa94a07fSbrendan */ 2044fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 2045fa94a07fSbrendan KM_SLEEP) == 0); 2046fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 2047fa94a07fSbrendan devs, ndevs) == 0); 2048fa94a07fSbrendan } 2049fa94a07fSbrendan } 2050fa94a07fSbrendan 2051fa94a07fSbrendan /* 2052fa94a07fSbrendan * Stop and drop level 2 ARC devices 2053fa94a07fSbrendan */ 2054fa94a07fSbrendan void 2055fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 2056fa94a07fSbrendan { 2057fa94a07fSbrendan vdev_t *vd; 2058fa94a07fSbrendan int i; 2059fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 2060fa94a07fSbrendan 2061fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 2062fa94a07fSbrendan uint64_t pool; 2063fa94a07fSbrendan 2064fa94a07fSbrendan vd = sav->sav_vdevs[i]; 2065fa94a07fSbrendan ASSERT(vd != NULL); 2066fa94a07fSbrendan 20678ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 20688ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 2069fa94a07fSbrendan l2arc_remove_vdev(vd); 2070fa94a07fSbrendan if (vd->vdev_isl2cache) 2071fa94a07fSbrendan spa_l2cache_remove(vd); 2072fa94a07fSbrendan vdev_clear_stats(vd); 2073fa94a07fSbrendan (void) vdev_close(vd); 2074fa94a07fSbrendan } 2075fa94a07fSbrendan } 2076fa94a07fSbrendan 2077fa9e4066Sahrens /* 2078fa9e4066Sahrens * Pool Creation 2079fa9e4066Sahrens */ 2080fa9e4066Sahrens int 2081990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 20820a48a24eStimh const char *history_str, nvlist_t *zplprops) 2083fa9e4066Sahrens { 2084fa9e4066Sahrens spa_t *spa; 2085990b4856Slling char *altroot = NULL; 20860373e76bSbonwick vdev_t *rvd; 2087fa9e4066Sahrens dsl_pool_t *dp; 2088fa9e4066Sahrens dmu_tx_t *tx; 2089*573ca77eSGeorge Wilson int error = 0; 2090fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 2091fa94a07fSbrendan nvlist_t **spares, **l2cache; 2092fa94a07fSbrendan uint_t nspares, nl2cache; 2093990b4856Slling uint64_t version; 2094fa9e4066Sahrens 2095fa9e4066Sahrens /* 2096fa9e4066Sahrens * If this pool already exists, return failure. 2097fa9e4066Sahrens */ 2098fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2099fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 2100fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2101fa9e4066Sahrens return (EEXIST); 2102fa9e4066Sahrens } 2103fa9e4066Sahrens 2104fa9e4066Sahrens /* 2105fa9e4066Sahrens * Allocate a new spa_t structure. 2106fa9e4066Sahrens */ 2107990b4856Slling (void) nvlist_lookup_string(props, 2108990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 21090373e76bSbonwick spa = spa_add(pool, altroot); 21108ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2111fa9e4066Sahrens 2112fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 2113990b4856Slling 2114990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 2115990b4856Slling spa_deactivate(spa); 2116990b4856Slling spa_remove(spa); 2117c5904d13Seschrock mutex_exit(&spa_namespace_lock); 2118990b4856Slling return (error); 2119990b4856Slling } 2120990b4856Slling 2121990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 2122990b4856Slling &version) != 0) 2123990b4856Slling version = SPA_VERSION; 2124990b4856Slling ASSERT(version <= SPA_VERSION); 2125990b4856Slling spa->spa_uberblock.ub_version = version; 2126fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2127fa9e4066Sahrens 212854d692b7SGeorge Wilson /* 212954d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 213054d692b7SGeorge Wilson */ 213125f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 213225f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 213354d692b7SGeorge Wilson 21340373e76bSbonwick /* 21350373e76bSbonwick * Create the root vdev. 21360373e76bSbonwick */ 2137e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21380373e76bSbonwick 213999653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 21400373e76bSbonwick 214199653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 214299653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 21430373e76bSbonwick 2144b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 21450373e76bSbonwick error = EINVAL; 214699653d4eSeschrock 214799653d4eSeschrock if (error == 0 && 214899653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 2149fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 215099653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 2151*573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 2152*573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 2153*573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 2154*573ca77eSGeorge Wilson } 21550373e76bSbonwick } 21560373e76bSbonwick 2157e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2158fa9e4066Sahrens 215999653d4eSeschrock if (error != 0) { 2160fa9e4066Sahrens spa_unload(spa); 2161fa9e4066Sahrens spa_deactivate(spa); 2162fa9e4066Sahrens spa_remove(spa); 2163fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2164fa9e4066Sahrens return (error); 2165fa9e4066Sahrens } 2166fa9e4066Sahrens 216799653d4eSeschrock /* 216899653d4eSeschrock * Get the list of spares, if specified. 216999653d4eSeschrock */ 217099653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 217199653d4eSeschrock &spares, &nspares) == 0) { 2172fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 217399653d4eSeschrock KM_SLEEP) == 0); 2174fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 217599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2176e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 217799653d4eSeschrock spa_load_spares(spa); 2178e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2179fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2180fa94a07fSbrendan } 2181fa94a07fSbrendan 2182fa94a07fSbrendan /* 2183fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 2184fa94a07fSbrendan */ 2185fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2186fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 2187fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2188fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 2189fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2190fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2191e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2192fa94a07fSbrendan spa_load_l2cache(spa); 2193e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2194fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 219599653d4eSeschrock } 219699653d4eSeschrock 21970a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2198fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2199fa9e4066Sahrens 2200fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 2201fa9e4066Sahrens 2202fa9e4066Sahrens /* 2203fa9e4066Sahrens * Create the pool config object. 2204fa9e4066Sahrens */ 2205fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 2206f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2207fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2208fa9e4066Sahrens 2209ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2210fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 2211ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 2212ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 2213ea8dc4b6Seschrock } 2214fa9e4066Sahrens 2215990b4856Slling /* Newly created pools with the right version are always deflated. */ 2216990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 2217990b4856Slling spa->spa_deflate = TRUE; 2218990b4856Slling if (zap_add(spa->spa_meta_objset, 2219990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 2220990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 2221990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 2222990b4856Slling } 222399653d4eSeschrock } 222499653d4eSeschrock 2225fa9e4066Sahrens /* 2226fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 2227fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 2228fa9e4066Sahrens * keeps changing. 2229fa9e4066Sahrens */ 2230fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2231fa9e4066Sahrens 1 << 14, tx); 2232fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2233fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 2234fa9e4066Sahrens 2235ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2236fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 2237ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 2238ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 2239ea8dc4b6Seschrock } 2240fa9e4066Sahrens 224106eeb2adSek /* 224206eeb2adSek * Create the pool's history object. 224306eeb2adSek */ 2244990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 2245990b4856Slling spa_history_create_obj(spa, tx); 2246990b4856Slling 2247990b4856Slling /* 2248990b4856Slling * Set pool properties. 2249990b4856Slling */ 2250990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 2251990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 22520a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 2253*573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 2254379c004dSEric Schrock if (props != NULL) { 2255379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 2256990b4856Slling spa_sync_props(spa, props, CRED(), tx); 2257379c004dSEric Schrock } 225806eeb2adSek 2259fa9e4066Sahrens dmu_tx_commit(tx); 2260fa9e4066Sahrens 2261fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2262fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2263fa9e4066Sahrens 2264fa9e4066Sahrens /* 2265fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 2266fa9e4066Sahrens * bean counters are appropriately updated. 2267fa9e4066Sahrens */ 2268fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2269fa9e4066Sahrens 2270c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2271fa9e4066Sahrens 2272990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 2273228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 2274228975ccSek 2275088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 2276088f3894Sahrens 2277daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 2278daaa36a7SGeorge Wilson 2279fa9e4066Sahrens return (0); 2280fa9e4066Sahrens } 2281fa9e4066Sahrens 2282e7cbe64fSgw #ifdef _KERNEL 2283e7cbe64fSgw /* 228421ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 228521ecdf64SLin Ling * during the system boot up time. 2286e7cbe64fSgw */ 228721ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 228821ecdf64SLin Ling 228921ecdf64SLin Ling static nvlist_t * 229021ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 2291e7cbe64fSgw { 229221ecdf64SLin Ling nvlist_t *config; 2293e7cbe64fSgw nvlist_t *nvtop, *nvroot; 2294e7cbe64fSgw uint64_t pgid; 2295e7cbe64fSgw 229621ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 229721ecdf64SLin Ling return (NULL); 229821ecdf64SLin Ling 2299e7cbe64fSgw /* 2300e7cbe64fSgw * Add this top-level vdev to the child array. 2301e7cbe64fSgw */ 230221ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 230321ecdf64SLin Ling &nvtop) == 0); 230421ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 230521ecdf64SLin Ling &pgid) == 0); 230621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 2307e7cbe64fSgw 2308e7cbe64fSgw /* 2309e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 2310e7cbe64fSgw */ 2311e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 231221ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 231321ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 2314e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 2315e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 2316e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2317e7cbe64fSgw &nvtop, 1) == 0); 2318e7cbe64fSgw 2319e7cbe64fSgw /* 2320e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 2321e7cbe64fSgw * this pool's configuration (remove the old, add the new). 2322e7cbe64fSgw */ 2323e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 2324e7cbe64fSgw nvlist_free(nvroot); 232521ecdf64SLin Ling return (config); 2326e7cbe64fSgw } 2327e7cbe64fSgw 2328e7cbe64fSgw /* 232921ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 233021ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 233121ecdf64SLin Ling * device has a more recent txg. 2332051aabe6Staylor */ 233321ecdf64SLin Ling static void 233421ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 2335051aabe6Staylor { 2336*573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 233721ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 2338051aabe6Staylor 233921ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 234021ecdf64SLin Ling nvlist_t *label; 234121ecdf64SLin Ling uint64_t label_txg; 2342051aabe6Staylor 234321ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 234421ecdf64SLin Ling &label) != 0) 234521ecdf64SLin Ling return; 2346051aabe6Staylor 234721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 234821ecdf64SLin Ling &label_txg) == 0); 2349051aabe6Staylor 235021ecdf64SLin Ling /* 235121ecdf64SLin Ling * Do we have a better boot device? 235221ecdf64SLin Ling */ 235321ecdf64SLin Ling if (label_txg > *txg) { 235421ecdf64SLin Ling *txg = label_txg; 235521ecdf64SLin Ling *avd = vd; 2356051aabe6Staylor } 235721ecdf64SLin Ling nvlist_free(label); 2358051aabe6Staylor } 2359051aabe6Staylor } 2360051aabe6Staylor 2361e7cbe64fSgw /* 2362e7cbe64fSgw * Import a root pool. 2363e7cbe64fSgw * 2364051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 2365051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 2366051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 2367e7cbe64fSgw * 2368e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 2369e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 2370e7cbe64fSgw * e.g. 2371e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 2372e7cbe64fSgw */ 2373e7cbe64fSgw int 2374051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 2375e7cbe64fSgw { 237621ecdf64SLin Ling spa_t *spa; 237721ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 237821ecdf64SLin Ling nvlist_t *config, *nvtop; 237921ecdf64SLin Ling uint64_t guid, txg; 2380e7cbe64fSgw char *pname; 2381e7cbe64fSgw int error; 2382e7cbe64fSgw 2383e7cbe64fSgw /* 238421ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 2385e7cbe64fSgw */ 238621ecdf64SLin Ling if ((config = spa_generate_rootconf(devpath, devid, &guid)) == NULL) { 238721ecdf64SLin Ling cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 238821ecdf64SLin Ling devpath); 238921ecdf64SLin Ling return (EIO); 239021ecdf64SLin Ling } 2391e7cbe64fSgw 239221ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 239321ecdf64SLin Ling &pname) == 0); 239421ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 2395e7cbe64fSgw 23966809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 23976809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 23986809eb4eSEric Schrock /* 23996809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 24006809eb4eSEric Schrock * can replace it with the correct config we just read in. 24016809eb4eSEric Schrock */ 24026809eb4eSEric Schrock spa_remove(spa); 24036809eb4eSEric Schrock } 24046809eb4eSEric Schrock 24056809eb4eSEric Schrock spa = spa_add(pname, NULL); 24066809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 2407e7cbe64fSgw 240821ecdf64SLin Ling /* 240921ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 241021ecdf64SLin Ling */ 241121ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 241221ecdf64SLin Ling &nvtop) == 0); 241321ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 241421ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 241521ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 241621ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 241721ecdf64SLin Ling if (error) { 241821ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 241921ecdf64SLin Ling nvlist_free(config); 242021ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 242121ecdf64SLin Ling pname); 242221ecdf64SLin Ling return (error); 242321ecdf64SLin Ling } 242421ecdf64SLin Ling 242521ecdf64SLin Ling /* 242621ecdf64SLin Ling * Get the boot vdev. 242721ecdf64SLin Ling */ 242821ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 242921ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 243021ecdf64SLin Ling (u_longlong_t)guid); 243121ecdf64SLin Ling error = ENOENT; 243221ecdf64SLin Ling goto out; 243321ecdf64SLin Ling } 2434e7cbe64fSgw 243521ecdf64SLin Ling /* 243621ecdf64SLin Ling * Determine if there is a better boot device. 243721ecdf64SLin Ling */ 243821ecdf64SLin Ling avd = bvd; 243921ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 244021ecdf64SLin Ling if (avd != bvd) { 244121ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 244221ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 244321ecdf64SLin Ling error = EINVAL; 244421ecdf64SLin Ling goto out; 244521ecdf64SLin Ling } 2446e7cbe64fSgw 244721ecdf64SLin Ling /* 244821ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 244921ecdf64SLin Ling * we're booting off the active spare. 245021ecdf64SLin Ling */ 245121ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 245221ecdf64SLin Ling !bvd->vdev_isspare) { 245321ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 245421ecdf64SLin Ling "try booting from '%s'", 245521ecdf64SLin Ling bvd->vdev_parent->vdev_child[1]->vdev_path); 245621ecdf64SLin Ling error = EINVAL; 245721ecdf64SLin Ling goto out; 245821ecdf64SLin Ling } 245921ecdf64SLin Ling 246021ecdf64SLin Ling VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 246121ecdf64SLin Ling error = 0; 246221ecdf64SLin Ling out: 246321ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 246421ecdf64SLin Ling vdev_free(rvd); 246521ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 246621ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 246721ecdf64SLin Ling 246821ecdf64SLin Ling nvlist_free(config); 2469e7cbe64fSgw return (error); 2470e7cbe64fSgw } 247121ecdf64SLin Ling 2472e7cbe64fSgw #endif 2473e7cbe64fSgw 2474e7cbe64fSgw /* 24756809eb4eSEric Schrock * Take a pool and insert it into the namespace as if it had been loaded at 24766809eb4eSEric Schrock * boot. 2477e7cbe64fSgw */ 2478e7cbe64fSgw int 24796809eb4eSEric Schrock spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 2480e7cbe64fSgw { 24816809eb4eSEric Schrock spa_t *spa; 24826809eb4eSEric Schrock char *altroot = NULL; 24836809eb4eSEric Schrock 24846809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 24856809eb4eSEric Schrock if (spa_lookup(pool) != NULL) { 24866809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 24876809eb4eSEric Schrock return (EEXIST); 24886809eb4eSEric Schrock } 24896809eb4eSEric Schrock 24906809eb4eSEric Schrock (void) nvlist_lookup_string(props, 24916809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 24926809eb4eSEric Schrock spa = spa_add(pool, altroot); 24936809eb4eSEric Schrock 24946809eb4eSEric Schrock VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 24956809eb4eSEric Schrock 24966809eb4eSEric Schrock if (props != NULL) 24976809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 24986809eb4eSEric Schrock 24996809eb4eSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE); 25006809eb4eSEric Schrock 25016809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 25026809eb4eSEric Schrock 25036809eb4eSEric Schrock return (0); 2504e7cbe64fSgw } 2505e7cbe64fSgw 25066809eb4eSEric Schrock /* 25076809eb4eSEric Schrock * Import a non-root pool into the system. 25086809eb4eSEric Schrock */ 2509c5904d13Seschrock int 25106809eb4eSEric Schrock spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 2511c5904d13Seschrock { 25126809eb4eSEric Schrock spa_t *spa; 25136809eb4eSEric Schrock char *altroot = NULL; 25146809eb4eSEric Schrock int error; 25156809eb4eSEric Schrock nvlist_t *nvroot; 25166809eb4eSEric Schrock nvlist_t **spares, **l2cache; 25176809eb4eSEric Schrock uint_t nspares, nl2cache; 25186809eb4eSEric Schrock 25196809eb4eSEric Schrock /* 25206809eb4eSEric Schrock * If a pool with this name exists, return failure. 25216809eb4eSEric Schrock */ 25226809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 25236809eb4eSEric Schrock if ((spa = spa_lookup(pool)) != NULL) { 25246809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 25256809eb4eSEric Schrock return (EEXIST); 25266809eb4eSEric Schrock } 25276809eb4eSEric Schrock 25286809eb4eSEric Schrock /* 25296809eb4eSEric Schrock * Create and initialize the spa structure. 25306809eb4eSEric Schrock */ 25316809eb4eSEric Schrock (void) nvlist_lookup_string(props, 25326809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 25336809eb4eSEric Schrock spa = spa_add(pool, altroot); 25346809eb4eSEric Schrock spa_activate(spa, spa_mode_global); 25356809eb4eSEric Schrock 253625f89ee2SJeff Bonwick /* 253725f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 253825f89ee2SJeff Bonwick */ 253925f89ee2SJeff Bonwick spa_async_suspend(spa); 254025f89ee2SJeff Bonwick 25416809eb4eSEric Schrock /* 25426809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 25436809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 25446809eb4eSEric Schrock * doing an import. 25456809eb4eSEric Schrock */ 25466809eb4eSEric Schrock error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 25476809eb4eSEric Schrock 25486809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 25496809eb4eSEric Schrock /* 25506809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 25516809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 25526809eb4eSEric Schrock */ 25536809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 25546809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 25556809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 25566809eb4eSEric Schrock spa_load_spares(spa); 25576809eb4eSEric Schrock } 25586809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 25596809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 25606809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 25616809eb4eSEric Schrock spa_load_l2cache(spa); 25626809eb4eSEric Schrock } 25636809eb4eSEric Schrock 25646809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 25656809eb4eSEric Schrock &nvroot) == 0); 25666809eb4eSEric Schrock if (error == 0) 25676809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 25686809eb4eSEric Schrock VDEV_ALLOC_SPARE); 25696809eb4eSEric Schrock if (error == 0) 25706809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 25716809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 25726809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 25736809eb4eSEric Schrock 25746809eb4eSEric Schrock if (props != NULL) 25756809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 25766809eb4eSEric Schrock 25776809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 25786809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 25796809eb4eSEric Schrock spa_unload(spa); 25806809eb4eSEric Schrock spa_deactivate(spa); 25816809eb4eSEric Schrock spa_remove(spa); 25826809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 25836809eb4eSEric Schrock return (error); 25846809eb4eSEric Schrock } 25856809eb4eSEric Schrock 258625f89ee2SJeff Bonwick spa_async_resume(spa); 258725f89ee2SJeff Bonwick 25886809eb4eSEric Schrock /* 25896809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 25906809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 25916809eb4eSEric Schrock */ 25926809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 25936809eb4eSEric Schrock &spares, &nspares) == 0) { 25946809eb4eSEric Schrock if (spa->spa_spares.sav_config) 25956809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 25966809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 25976809eb4eSEric Schrock else 25986809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 25996809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 26006809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 26016809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 26026809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26036809eb4eSEric Schrock spa_load_spares(spa); 26046809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 26056809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 26066809eb4eSEric Schrock } 26076809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 26086809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 26096809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 26106809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 26116809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 26126809eb4eSEric Schrock else 26136809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 26146809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 26156809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 26166809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 26176809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26186809eb4eSEric Schrock spa_load_l2cache(spa); 26196809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 26206809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 26216809eb4eSEric Schrock } 26226809eb4eSEric Schrock 26236809eb4eSEric Schrock if (spa_writeable(spa)) { 26246809eb4eSEric Schrock /* 26256809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 26266809eb4eSEric Schrock */ 26276809eb4eSEric Schrock spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, B_FALSE); 26286809eb4eSEric Schrock } 26296809eb4eSEric Schrock 2630*573ca77eSGeorge Wilson /* 2631*573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 2632*573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 2633*573ca77eSGeorge Wilson */ 2634*573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 2635*573ca77eSGeorge Wilson 26366809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 26376809eb4eSEric Schrock 26386809eb4eSEric Schrock return (0); 2639c5904d13Seschrock } 2640c5904d13Seschrock 2641c5904d13Seschrock 2642fa9e4066Sahrens /* 2643fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2644fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 2645fa9e4066Sahrens */ 2646fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 2647fa9e4066Sahrens 2648fa9e4066Sahrens nvlist_t * 2649fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 2650fa9e4066Sahrens { 2651fa9e4066Sahrens nvlist_t *config = NULL; 2652fa9e4066Sahrens char *poolname; 2653fa9e4066Sahrens spa_t *spa; 2654fa9e4066Sahrens uint64_t state; 26557b7154beSLin Ling int error; 2656fa9e4066Sahrens 2657fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2658fa9e4066Sahrens return (NULL); 2659fa9e4066Sahrens 2660fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2661fa9e4066Sahrens return (NULL); 2662fa9e4066Sahrens 2663fa9e4066Sahrens /* 26640373e76bSbonwick * Create and initialize the spa structure. 2665fa9e4066Sahrens */ 26660373e76bSbonwick mutex_enter(&spa_namespace_lock); 26670373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 26688ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 2669fa9e4066Sahrens 2670fa9e4066Sahrens /* 26710373e76bSbonwick * Pass off the heavy lifting to spa_load(). 2672ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 2673ecc2d604Sbonwick * is actually the one to trust when doing an import. 2674fa9e4066Sahrens */ 26757b7154beSLin Ling error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2676fa9e4066Sahrens 2677fa9e4066Sahrens /* 2678fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 2679fa9e4066Sahrens */ 2680fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 2681fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2682fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2683fa9e4066Sahrens poolname) == 0); 2684fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2685fa9e4066Sahrens state) == 0); 268695173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 268795173954Sek spa->spa_uberblock.ub_timestamp) == 0); 268899653d4eSeschrock 2689e7cbe64fSgw /* 2690e7cbe64fSgw * If the bootfs property exists on this pool then we 2691e7cbe64fSgw * copy it out so that external consumers can tell which 2692e7cbe64fSgw * pools are bootable. 2693e7cbe64fSgw */ 26947b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 2695e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2696e7cbe64fSgw 2697e7cbe64fSgw /* 2698e7cbe64fSgw * We have to play games with the name since the 2699e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 2700e7cbe64fSgw */ 2701e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 2702e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 2703e7cbe64fSgw char *cp; 2704e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2705e7cbe64fSgw 2706e7cbe64fSgw cp = strchr(tmpname, '/'); 2707e7cbe64fSgw if (cp == NULL) { 2708e7cbe64fSgw (void) strlcpy(dsname, tmpname, 2709e7cbe64fSgw MAXPATHLEN); 2710e7cbe64fSgw } else { 2711e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 2712e7cbe64fSgw "%s/%s", poolname, ++cp); 2713e7cbe64fSgw } 2714e7cbe64fSgw VERIFY(nvlist_add_string(config, 2715e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 2716e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 2717e7cbe64fSgw } 2718e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 2719e7cbe64fSgw } 2720e7cbe64fSgw 272199653d4eSeschrock /* 2722fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 272399653d4eSeschrock */ 27246809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 272599653d4eSeschrock spa_add_spares(spa, config); 2726fa94a07fSbrendan spa_add_l2cache(spa, config); 27276809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 2728fa9e4066Sahrens } 2729fa9e4066Sahrens 2730fa9e4066Sahrens spa_unload(spa); 2731fa9e4066Sahrens spa_deactivate(spa); 2732fa9e4066Sahrens spa_remove(spa); 2733fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2734fa9e4066Sahrens 2735fa9e4066Sahrens return (config); 2736fa9e4066Sahrens } 2737fa9e4066Sahrens 2738fa9e4066Sahrens /* 2739fa9e4066Sahrens * Pool export/destroy 2740fa9e4066Sahrens * 2741fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2742fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2743fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 2744394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 2745394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 2746fa9e4066Sahrens */ 2747fa9e4066Sahrens static int 274889a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 2749394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 2750fa9e4066Sahrens { 2751fa9e4066Sahrens spa_t *spa; 2752fa9e4066Sahrens 275344cd46caSbillm if (oldconfig) 275444cd46caSbillm *oldconfig = NULL; 275544cd46caSbillm 27568ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 2757fa9e4066Sahrens return (EROFS); 2758fa9e4066Sahrens 2759fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2760fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2761fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2762fa9e4066Sahrens return (ENOENT); 2763fa9e4066Sahrens } 2764fa9e4066Sahrens 2765ea8dc4b6Seschrock /* 2766ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 2767ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 2768ea8dc4b6Seschrock */ 2769ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 2770ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2771ea8dc4b6Seschrock spa_async_suspend(spa); 2772ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2773ea8dc4b6Seschrock spa_close(spa, FTAG); 2774ea8dc4b6Seschrock 2775fa9e4066Sahrens /* 2776fa9e4066Sahrens * The pool will be in core if it's openable, 2777fa9e4066Sahrens * in which case we can modify its state. 2778fa9e4066Sahrens */ 2779fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2780fa9e4066Sahrens /* 2781fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 2782fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 2783fa9e4066Sahrens */ 2784fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2785fa9e4066Sahrens 2786ea8dc4b6Seschrock /* 2787ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 2788ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 2789ea8dc4b6Seschrock * fault injection handlers. 2790ea8dc4b6Seschrock */ 2791ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 2792ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 2793ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 2794ea8dc4b6Seschrock spa_async_resume(spa); 2795fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2796fa9e4066Sahrens return (EBUSY); 2797fa9e4066Sahrens } 2798fa9e4066Sahrens 279989a89ebfSlling /* 280089a89ebfSlling * A pool cannot be exported if it has an active shared spare. 280189a89ebfSlling * This is to prevent other pools stealing the active spare 280289a89ebfSlling * from an exported pool. At user's own will, such pool can 280389a89ebfSlling * be forcedly exported. 280489a89ebfSlling */ 280589a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 280689a89ebfSlling spa_has_active_shared_spare(spa)) { 280789a89ebfSlling spa_async_resume(spa); 280889a89ebfSlling mutex_exit(&spa_namespace_lock); 280989a89ebfSlling return (EXDEV); 281089a89ebfSlling } 281189a89ebfSlling 2812fa9e4066Sahrens /* 2813fa9e4066Sahrens * We want this to be reflected on every label, 2814fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 2815fa9e4066Sahrens * final sync that pushes these changes out. 2816fa9e4066Sahrens */ 2817394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 2818e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2819ea8dc4b6Seschrock spa->spa_state = new_state; 28200373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 2821ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 2822e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2823ea8dc4b6Seschrock } 2824fa9e4066Sahrens } 2825fa9e4066Sahrens 28263d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 28273d7072f8Seschrock 2828fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2829fa9e4066Sahrens spa_unload(spa); 2830fa9e4066Sahrens spa_deactivate(spa); 2831fa9e4066Sahrens } 2832fa9e4066Sahrens 283344cd46caSbillm if (oldconfig && spa->spa_config) 283444cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 283544cd46caSbillm 2836ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 2837394ab0cbSGeorge Wilson if (!hardforce) 2838394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 2839ea8dc4b6Seschrock spa_remove(spa); 2840ea8dc4b6Seschrock } 2841fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2842fa9e4066Sahrens 2843fa9e4066Sahrens return (0); 2844fa9e4066Sahrens } 2845fa9e4066Sahrens 2846fa9e4066Sahrens /* 2847fa9e4066Sahrens * Destroy a storage pool. 2848fa9e4066Sahrens */ 2849fa9e4066Sahrens int 2850fa9e4066Sahrens spa_destroy(char *pool) 2851fa9e4066Sahrens { 2852394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 2853394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 2854fa9e4066Sahrens } 2855fa9e4066Sahrens 2856fa9e4066Sahrens /* 2857fa9e4066Sahrens * Export a storage pool. 2858fa9e4066Sahrens */ 2859fa9e4066Sahrens int 2860394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 2861394ab0cbSGeorge Wilson boolean_t hardforce) 2862fa9e4066Sahrens { 2863394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 2864394ab0cbSGeorge Wilson force, hardforce)); 2865fa9e4066Sahrens } 2866fa9e4066Sahrens 2867ea8dc4b6Seschrock /* 2868ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 2869ea8dc4b6Seschrock * from the namespace in any way. 2870ea8dc4b6Seschrock */ 2871ea8dc4b6Seschrock int 2872ea8dc4b6Seschrock spa_reset(char *pool) 2873ea8dc4b6Seschrock { 287489a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 2875394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 2876ea8dc4b6Seschrock } 2877ea8dc4b6Seschrock 2878fa9e4066Sahrens /* 2879fa9e4066Sahrens * ========================================================================== 2880fa9e4066Sahrens * Device manipulation 2881fa9e4066Sahrens * ========================================================================== 2882fa9e4066Sahrens */ 2883fa9e4066Sahrens 2884fa9e4066Sahrens /* 28858654d025Sperrin * Add a device to a storage pool. 2886fa9e4066Sahrens */ 2887fa9e4066Sahrens int 2888fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2889fa9e4066Sahrens { 2890fa9e4066Sahrens uint64_t txg; 28918ad4d6ddSJeff Bonwick int error; 2892fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 28930e34b6a7Sbonwick vdev_t *vd, *tvd; 2894fa94a07fSbrendan nvlist_t **spares, **l2cache; 2895fa94a07fSbrendan uint_t nspares, nl2cache; 2896fa9e4066Sahrens 2897fa9e4066Sahrens txg = spa_vdev_enter(spa); 2898fa9e4066Sahrens 289999653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 290099653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 290199653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 2902fa9e4066Sahrens 2903e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 290499653d4eSeschrock 2905fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 2906fa94a07fSbrendan &nspares) != 0) 290799653d4eSeschrock nspares = 0; 290899653d4eSeschrock 2909fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 2910fa94a07fSbrendan &nl2cache) != 0) 2911fa94a07fSbrendan nl2cache = 0; 2912fa94a07fSbrendan 2913e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 2914fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 2915fa9e4066Sahrens 2916e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 2917e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 2918e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 291999653d4eSeschrock 292039c23413Seschrock /* 2921fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 2922fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 292339c23413Seschrock */ 2924e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 292539c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 292639c23413Seschrock 292739c23413Seschrock /* 292839c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 292939c23413Seschrock */ 29308ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 293139c23413Seschrock tvd = vd->vdev_child[c]; 293239c23413Seschrock vdev_remove_child(vd, tvd); 293339c23413Seschrock tvd->vdev_id = rvd->vdev_children; 293439c23413Seschrock vdev_add_child(rvd, tvd); 293539c23413Seschrock vdev_config_dirty(tvd); 293639c23413Seschrock } 293739c23413Seschrock 293899653d4eSeschrock if (nspares != 0) { 2939fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 2940fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 294199653d4eSeschrock spa_load_spares(spa); 2942fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2943fa94a07fSbrendan } 2944fa94a07fSbrendan 2945fa94a07fSbrendan if (nl2cache != 0) { 2946fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 2947fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 2948fa94a07fSbrendan spa_load_l2cache(spa); 2949fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2950fa9e4066Sahrens } 2951fa9e4066Sahrens 2952fa9e4066Sahrens /* 29530e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 29540e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 29550e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 29560e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 29570e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 29580e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 29590373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 29600e34b6a7Sbonwick * 29610e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 29620e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 29630e34b6a7Sbonwick * steps will be completed the next time we load the pool. 29640e34b6a7Sbonwick */ 29650373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 29660e34b6a7Sbonwick 29670373e76bSbonwick mutex_enter(&spa_namespace_lock); 29680373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 29690373e76bSbonwick mutex_exit(&spa_namespace_lock); 2970fa9e4066Sahrens 29710373e76bSbonwick return (0); 2972fa9e4066Sahrens } 2973fa9e4066Sahrens 2974fa9e4066Sahrens /* 2975fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 2976fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2977fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2978fa9e4066Sahrens * 2979fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 2980fa9e4066Sahrens * existing device; in this case the two devices are made into their own 29813d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2982fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2983fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 2984fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 2985fa9e4066Sahrens * is automatically detached. 2986fa9e4066Sahrens */ 2987fa9e4066Sahrens int 2988ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2989fa9e4066Sahrens { 2990fa9e4066Sahrens uint64_t txg, open_txg; 2991fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2992fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 299399653d4eSeschrock vdev_ops_t *pvops; 29949b3f6b42SEric Kustarz dmu_tx_t *tx; 29959b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 29969b3f6b42SEric Kustarz int newvd_isspare; 29979b3f6b42SEric Kustarz int error; 2998fa9e4066Sahrens 2999fa9e4066Sahrens txg = spa_vdev_enter(spa); 3000fa9e4066Sahrens 3001c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3002fa9e4066Sahrens 3003fa9e4066Sahrens if (oldvd == NULL) 3004fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3005fa9e4066Sahrens 30060e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 30070e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30080e34b6a7Sbonwick 3009fa9e4066Sahrens pvd = oldvd->vdev_parent; 3010fa9e4066Sahrens 301199653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 30123d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 30133d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 30143d7072f8Seschrock 30153d7072f8Seschrock if (newrootvd->vdev_children != 1) 3016fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3017fa9e4066Sahrens 3018fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 3019fa9e4066Sahrens 3020fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3021fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3022fa9e4066Sahrens 302399653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3024fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3025fa9e4066Sahrens 30268654d025Sperrin /* 30278654d025Sperrin * Spares can't replace logs 30288654d025Sperrin */ 3029ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 30308654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30318654d025Sperrin 303299653d4eSeschrock if (!replacing) { 303399653d4eSeschrock /* 303499653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 303599653d4eSeschrock * vdev. 303699653d4eSeschrock */ 303799653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 303899653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 303999653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 304099653d4eSeschrock 304199653d4eSeschrock pvops = &vdev_mirror_ops; 304299653d4eSeschrock } else { 304399653d4eSeschrock /* 304499653d4eSeschrock * Active hot spares can only be replaced by inactive hot 304599653d4eSeschrock * spares. 304699653d4eSeschrock */ 304799653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 304899653d4eSeschrock pvd->vdev_child[1] == oldvd && 304999653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 305099653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 305199653d4eSeschrock 305299653d4eSeschrock /* 305399653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 305499653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 305539c23413Seschrock * want to create a replacing vdev. The user is not allowed to 305639c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 305739c23413Seschrock * the same (spare replaces spare, non-spare replaces 305839c23413Seschrock * non-spare). 305999653d4eSeschrock */ 306099653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 306199653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 306239c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 306339c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 306439c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 306599653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 306699653d4eSeschrock newvd->vdev_isspare) 306799653d4eSeschrock pvops = &vdev_spare_ops; 306899653d4eSeschrock else 306999653d4eSeschrock pvops = &vdev_replacing_ops; 307099653d4eSeschrock } 307199653d4eSeschrock 30722a79c5feSlling /* 3073*573ca77eSGeorge Wilson * Make sure the new device is big enough. 30742a79c5feSlling */ 3075*573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3076fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3077fa9e4066Sahrens 3078ecc2d604Sbonwick /* 3079ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 3080ecc2d604Sbonwick * than the top-level vdev. 3081ecc2d604Sbonwick */ 3082ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3083fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3084fa9e4066Sahrens 3085fa9e4066Sahrens /* 3086fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 3087fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3088fa9e4066Sahrens */ 3089fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3090fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 3091fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3092fa9e4066Sahrens KM_SLEEP); 3093fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3094fa9e4066Sahrens newvd->vdev_path, "old"); 3095fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 3096fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 3097fa9e4066Sahrens oldvd->vdev_devid = NULL; 3098fa9e4066Sahrens } 3099fa9e4066Sahrens } 3100fa9e4066Sahrens 3101fa9e4066Sahrens /* 310299653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 310399653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 3104fa9e4066Sahrens */ 3105fa9e4066Sahrens if (pvd->vdev_ops != pvops) 3106fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 3107fa9e4066Sahrens 3108fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3109fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 3110fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 3111fa9e4066Sahrens 3112fa9e4066Sahrens /* 3113fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 3114fa9e4066Sahrens */ 3115fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 3116fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 3117fa9e4066Sahrens vdev_add_child(pvd, newvd); 3118fa9e4066Sahrens 3119fa9e4066Sahrens tvd = newvd->vdev_top; 3120fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 3121fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3122fa9e4066Sahrens 3123fa9e4066Sahrens vdev_config_dirty(tvd); 3124fa9e4066Sahrens 3125fa9e4066Sahrens /* 3126fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3127fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3128fa9e4066Sahrens */ 3129fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3130fa9e4066Sahrens 31318ad4d6ddSJeff Bonwick vdev_dtl_dirty(newvd, DTL_MISSING, 31328ad4d6ddSJeff Bonwick TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3133fa9e4066Sahrens 31346809eb4eSEric Schrock if (newvd->vdev_isspare) { 313539c23413Seschrock spa_spare_activate(newvd); 31366809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 31376809eb4eSEric Schrock } 31386809eb4eSEric Schrock 3139e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 3140e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 31419b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 3142ea8dc4b6Seschrock 3143fa9e4066Sahrens /* 3144fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 3145fa9e4066Sahrens */ 3146ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3147fa9e4066Sahrens 3148fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3149fa9e4066Sahrens 31509b3f6b42SEric Kustarz tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 31519b3f6b42SEric Kustarz if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 31529b3f6b42SEric Kustarz spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 31539b3f6b42SEric Kustarz CRED(), "%s vdev=%s %s vdev=%s", 31549b3f6b42SEric Kustarz replacing && newvd_isspare ? "spare in" : 31559b3f6b42SEric Kustarz replacing ? "replace" : "attach", newvdpath, 31569b3f6b42SEric Kustarz replacing ? "for" : "to", oldvdpath); 31579b3f6b42SEric Kustarz dmu_tx_commit(tx); 31589b3f6b42SEric Kustarz } else { 31599b3f6b42SEric Kustarz dmu_tx_abort(tx); 31609b3f6b42SEric Kustarz } 31619b3f6b42SEric Kustarz 31629b3f6b42SEric Kustarz spa_strfree(oldvdpath); 31639b3f6b42SEric Kustarz spa_strfree(newvdpath); 31649b3f6b42SEric Kustarz 3165fa9e4066Sahrens /* 3166088f3894Sahrens * Kick off a resilver to update newvd. 3167fa9e4066Sahrens */ 3168088f3894Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3169fa9e4066Sahrens 3170fa9e4066Sahrens return (0); 3171fa9e4066Sahrens } 3172fa9e4066Sahrens 3173fa9e4066Sahrens /* 3174fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 3175fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 3176fa9e4066Sahrens * is a replacing vdev. 3177fa9e4066Sahrens */ 3178fa9e4066Sahrens int 31798ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3180fa9e4066Sahrens { 3181fa9e4066Sahrens uint64_t txg; 31828ad4d6ddSJeff Bonwick int error; 3183fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3184fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 318599653d4eSeschrock boolean_t unspare = B_FALSE; 318699653d4eSeschrock uint64_t unspare_guid; 3187bf82a41bSeschrock size_t len; 3188fa9e4066Sahrens 3189fa9e4066Sahrens txg = spa_vdev_enter(spa); 3190fa9e4066Sahrens 3191c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3192fa9e4066Sahrens 3193fa9e4066Sahrens if (vd == NULL) 3194fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3195fa9e4066Sahrens 31960e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 31970e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31980e34b6a7Sbonwick 3199fa9e4066Sahrens pvd = vd->vdev_parent; 3200fa9e4066Sahrens 32018ad4d6ddSJeff Bonwick /* 32028ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 32038ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 32048ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 32058ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 32068ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 32078ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 32088ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 32098ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 32108ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 32118ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 32128ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 32138ad4d6ddSJeff Bonwick */ 32148ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 32158ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 32168ad4d6ddSJeff Bonwick 3217fa9e4066Sahrens /* 3218fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 321999653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 322099653d4eSeschrock * disk can be removed. 322199653d4eSeschrock */ 322299653d4eSeschrock if (replace_done) { 322399653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 322499653d4eSeschrock if (vd->vdev_id != 0) 322599653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 322699653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 322799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 322899653d4eSeschrock } 322999653d4eSeschrock } 323099653d4eSeschrock 323199653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 3232e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3233fa9e4066Sahrens 3234fa9e4066Sahrens /* 323599653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 3236fa9e4066Sahrens */ 3237fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 323899653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 323999653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 3240fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3241fa9e4066Sahrens 3242fa9e4066Sahrens /* 32438ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 32448ad4d6ddSJeff Bonwick * we cannot safely detach it. 3245fa9e4066Sahrens */ 32468ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 3247fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3248fa9e4066Sahrens 32498ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 3250fa9e4066Sahrens 3251bf82a41bSeschrock /* 3252bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 3253bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 3254bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 3255bf82a41bSeschrock */ 3256bf82a41bSeschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 3257bf82a41bSeschrock pvd->vdev_child[0]->vdev_path != NULL && 3258bf82a41bSeschrock pvd->vdev_child[1]->vdev_path != NULL) { 3259bf82a41bSeschrock ASSERT(pvd->vdev_child[1] == vd); 3260bf82a41bSeschrock cvd = pvd->vdev_child[0]; 3261bf82a41bSeschrock len = strlen(vd->vdev_path); 3262bf82a41bSeschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 3263bf82a41bSeschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 3264bf82a41bSeschrock spa_strfree(cvd->vdev_path); 3265bf82a41bSeschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 3266bf82a41bSeschrock } 3267bf82a41bSeschrock } 3268bf82a41bSeschrock 326999653d4eSeschrock /* 327099653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 327199653d4eSeschrock * that the spare should become a real disk, and be removed from the 327299653d4eSeschrock * active spare list for the pool. 327399653d4eSeschrock */ 327499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 32758ad4d6ddSJeff Bonwick vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 327699653d4eSeschrock unspare = B_TRUE; 327799653d4eSeschrock 3278fa9e4066Sahrens /* 3279fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 3280fa9e4066Sahrens * This must be done after all other error cases are handled, 3281fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 3282fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 3283fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 3284fa9e4066Sahrens * it's being detached! 3285fa9e4066Sahrens */ 328639c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3287fa9e4066Sahrens 3288fa9e4066Sahrens /* 3289fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 3290fa9e4066Sahrens */ 3291fa9e4066Sahrens vdev_remove_child(pvd, vd); 3292fa9e4066Sahrens vdev_compact_children(pvd); 3293fa9e4066Sahrens 3294fa9e4066Sahrens /* 3295fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 3296fa9e4066Sahrens */ 3297fa9e4066Sahrens cvd = pvd->vdev_child[0]; 3298fa9e4066Sahrens 329999653d4eSeschrock /* 330099653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 33018ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 33028ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 33038ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 33048ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 33058ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 33068ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 330799653d4eSeschrock */ 330899653d4eSeschrock if (unspare) { 330999653d4eSeschrock ASSERT(cvd->vdev_isspare); 331039c23413Seschrock spa_spare_remove(cvd); 331199653d4eSeschrock unspare_guid = cvd->vdev_guid; 33128ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 331399653d4eSeschrock } 331499653d4eSeschrock 3315fa9e4066Sahrens /* 3316fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 3317fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 3318fa9e4066Sahrens */ 3319fa9e4066Sahrens if (pvd->vdev_children == 1) 3320fa9e4066Sahrens vdev_remove_parent(cvd); 3321fa9e4066Sahrens 3322fa9e4066Sahrens /* 3323fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 3324fa9e4066Sahrens * may have been the previous top-level vdev. 3325fa9e4066Sahrens */ 3326fa9e4066Sahrens tvd = cvd->vdev_top; 3327fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3328fa9e4066Sahrens 3329fa9e4066Sahrens /* 333039c23413Seschrock * Reevaluate the parent vdev state. 3331fa9e4066Sahrens */ 33323d7072f8Seschrock vdev_propagate_state(cvd); 3333fa9e4066Sahrens 3334fa9e4066Sahrens /* 3335*573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 3336*573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 3337*573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 3338*573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 3339*573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 3340fa9e4066Sahrens */ 3341*573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 3342*573ca77eSGeorge Wilson vdev_reopen(tvd); 3343*573ca77eSGeorge Wilson vdev_expand(tvd, txg); 3344*573ca77eSGeorge Wilson } 3345fa9e4066Sahrens 3346fa9e4066Sahrens vdev_config_dirty(tvd); 3347fa9e4066Sahrens 3348fa9e4066Sahrens /* 334939c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 335039c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 335139c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 335239c23413Seschrock * prevent vd from being accessed after it's freed. 3353fa9e4066Sahrens */ 33548ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 3355fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 3356ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 3357ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3358fa9e4066Sahrens 33593d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 33603d7072f8Seschrock 336199653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 336299653d4eSeschrock 336399653d4eSeschrock /* 336439c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 336539c23413Seschrock * then we want to go through and remove the device from the hot spare 336639c23413Seschrock * list of every other pool. 336799653d4eSeschrock */ 336899653d4eSeschrock if (unspare) { 33698ad4d6ddSJeff Bonwick spa_t *myspa = spa; 337099653d4eSeschrock spa = NULL; 337199653d4eSeschrock mutex_enter(&spa_namespace_lock); 337299653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 337399653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 337499653d4eSeschrock continue; 33758ad4d6ddSJeff Bonwick if (spa == myspa) 33768ad4d6ddSJeff Bonwick continue; 33779af0a4dfSJeff Bonwick spa_open_ref(spa, FTAG); 33789af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 337999653d4eSeschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 33809af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 33819af0a4dfSJeff Bonwick spa_close(spa, FTAG); 338299653d4eSeschrock } 338399653d4eSeschrock mutex_exit(&spa_namespace_lock); 338499653d4eSeschrock } 338599653d4eSeschrock 338699653d4eSeschrock return (error); 338799653d4eSeschrock } 338899653d4eSeschrock 3389e14bb325SJeff Bonwick static nvlist_t * 3390e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 339199653d4eSeschrock { 3392e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 3393e14bb325SJeff Bonwick uint64_t guid; 339499653d4eSeschrock 3395e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 3396e14bb325SJeff Bonwick &guid) == 0); 339799653d4eSeschrock 3398e14bb325SJeff Bonwick if (guid == target_guid) 3399e14bb325SJeff Bonwick return (nvpp[i]); 340099653d4eSeschrock } 340199653d4eSeschrock 3402e14bb325SJeff Bonwick return (NULL); 3403fa94a07fSbrendan } 3404fa94a07fSbrendan 3405e14bb325SJeff Bonwick static void 3406e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 3407e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 3408fa94a07fSbrendan { 3409e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 3410fa94a07fSbrendan 3411e14bb325SJeff Bonwick if (count > 1) 3412e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 3413fa94a07fSbrendan 3414e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 3415e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 3416e14bb325SJeff Bonwick continue; 3417e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 3418fa94a07fSbrendan } 3419fa94a07fSbrendan 3420e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 3421e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 3422fa94a07fSbrendan 3423e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 3424e14bb325SJeff Bonwick nvlist_free(newdev[i]); 3425fa94a07fSbrendan 3426e14bb325SJeff Bonwick if (count > 1) 3427e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 3428fa94a07fSbrendan } 3429fa94a07fSbrendan 3430fa94a07fSbrendan /* 3431fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 3432fa94a07fSbrendan * spares and level 2 ARC devices. 3433fa94a07fSbrendan */ 3434fa94a07fSbrendan int 3435fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 3436fa94a07fSbrendan { 3437fa94a07fSbrendan vdev_t *vd; 3438e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 3439fa94a07fSbrendan uint_t nspares, nl2cache; 34408ad4d6ddSJeff Bonwick uint64_t txg = 0; 3441fa94a07fSbrendan int error = 0; 34428ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 3443fa94a07fSbrendan 34448ad4d6ddSJeff Bonwick if (!locked) 34458ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 3446fa94a07fSbrendan 3447c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3448fa94a07fSbrendan 3449fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 3450fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 3451e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 3452e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 3453e14bb325SJeff Bonwick /* 3454e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 3455e14bb325SJeff Bonwick * in this pool. 3456e14bb325SJeff Bonwick */ 3457e14bb325SJeff Bonwick if (vd == NULL || unspare) { 3458e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 3459e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 3460e14bb325SJeff Bonwick spa_load_spares(spa); 3461e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 3462e14bb325SJeff Bonwick } else { 3463e14bb325SJeff Bonwick error = EBUSY; 3464e14bb325SJeff Bonwick } 3465e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 3466fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3467e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 3468e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 3469e14bb325SJeff Bonwick /* 3470e14bb325SJeff Bonwick * Cache devices can always be removed. 3471e14bb325SJeff Bonwick */ 3472e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 3473e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 3474fa94a07fSbrendan spa_load_l2cache(spa); 3475fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3476e14bb325SJeff Bonwick } else if (vd != NULL) { 3477e14bb325SJeff Bonwick /* 3478e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 3479e14bb325SJeff Bonwick */ 3480e14bb325SJeff Bonwick error = ENOTSUP; 3481e14bb325SJeff Bonwick } else { 3482e14bb325SJeff Bonwick /* 3483e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 3484e14bb325SJeff Bonwick */ 3485e14bb325SJeff Bonwick error = ENOENT; 3486fa94a07fSbrendan } 348799653d4eSeschrock 34888ad4d6ddSJeff Bonwick if (!locked) 34898ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 34908ad4d6ddSJeff Bonwick 34918ad4d6ddSJeff Bonwick return (error); 3492fa9e4066Sahrens } 3493fa9e4066Sahrens 3494fa9e4066Sahrens /* 34953d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 34963d7072f8Seschrock * current spared, so we can detach it. 3497fa9e4066Sahrens */ 3498ea8dc4b6Seschrock static vdev_t * 34993d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3500fa9e4066Sahrens { 3501ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 3502fa9e4066Sahrens 3503*573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 35043d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 3505ea8dc4b6Seschrock if (oldvd != NULL) 3506ea8dc4b6Seschrock return (oldvd); 3507ea8dc4b6Seschrock } 3508fa9e4066Sahrens 35093d7072f8Seschrock /* 35103d7072f8Seschrock * Check for a completed replacement. 35113d7072f8Seschrock */ 3512fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 3513ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 3514ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 3515ea8dc4b6Seschrock 35168ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 35178ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 3518ea8dc4b6Seschrock return (oldvd); 3519fa9e4066Sahrens } 3520ea8dc4b6Seschrock 35213d7072f8Seschrock /* 35223d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 35233d7072f8Seschrock */ 35243d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 35253d7072f8Seschrock newvd = vd->vdev_child[0]; 35263d7072f8Seschrock oldvd = vd->vdev_child[1]; 35273d7072f8Seschrock 35283d7072f8Seschrock if (newvd->vdev_unspare && 35298ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 35308ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) { 35313d7072f8Seschrock newvd->vdev_unspare = 0; 35323d7072f8Seschrock return (oldvd); 35333d7072f8Seschrock } 35343d7072f8Seschrock } 35353d7072f8Seschrock 3536ea8dc4b6Seschrock return (NULL); 3537fa9e4066Sahrens } 3538fa9e4066Sahrens 3539ea8dc4b6Seschrock static void 35403d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 3541fa9e4066Sahrens { 35428ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 35438ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 3544ea8dc4b6Seschrock 35458ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3546ea8dc4b6Seschrock 35473d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 35488ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 35498ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 3550ea8dc4b6Seschrock guid = vd->vdev_guid; 35518ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 35528ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 35538ad4d6ddSJeff Bonwick sguid = 0; 355499653d4eSeschrock /* 355599653d4eSeschrock * If we have just finished replacing a hot spared device, then 355699653d4eSeschrock * we need to detach the parent's first child (the original hot 355799653d4eSeschrock * spare) as well. 355899653d4eSeschrock */ 35598ad4d6ddSJeff Bonwick if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 356099653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 35618ad4d6ddSJeff Bonwick ASSERT(ppvd->vdev_children == 2); 35628ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 356399653d4eSeschrock } 35648ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 35658ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 3566ea8dc4b6Seschrock return; 35678ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 356899653d4eSeschrock return; 35698ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3570fa9e4066Sahrens } 3571fa9e4066Sahrens 35728ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3573fa9e4066Sahrens } 3574fa9e4066Sahrens 3575c67d9675Seschrock /* 35766809eb4eSEric Schrock * Update the stored path or FRU for this vdev. Dirty the vdev configuration, 35776809eb4eSEric Schrock * relying on spa_vdev_enter/exit() to synchronize the labels and cache. 3578c67d9675Seschrock */ 3579c67d9675Seschrock int 35806809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 35816809eb4eSEric Schrock boolean_t ispath) 3582c67d9675Seschrock { 3583c5904d13Seschrock vdev_t *vd; 3584c67d9675Seschrock uint64_t txg; 3585c67d9675Seschrock 3586c67d9675Seschrock txg = spa_vdev_enter(spa); 3587c67d9675Seschrock 35886809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3589fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 3590c67d9675Seschrock 35910e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35920e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35930e34b6a7Sbonwick 35946809eb4eSEric Schrock if (ispath) { 35956809eb4eSEric Schrock spa_strfree(vd->vdev_path); 35966809eb4eSEric Schrock vd->vdev_path = spa_strdup(value); 35976809eb4eSEric Schrock } else { 35986809eb4eSEric Schrock if (vd->vdev_fru != NULL) 35996809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 36006809eb4eSEric Schrock vd->vdev_fru = spa_strdup(value); 36016809eb4eSEric Schrock } 3602c67d9675Seschrock 3603c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 3604c67d9675Seschrock 3605c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 3606c67d9675Seschrock } 3607c67d9675Seschrock 36086809eb4eSEric Schrock int 36096809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 36106809eb4eSEric Schrock { 36116809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 36126809eb4eSEric Schrock } 36136809eb4eSEric Schrock 36146809eb4eSEric Schrock int 36156809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 36166809eb4eSEric Schrock { 36176809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 36186809eb4eSEric Schrock } 36196809eb4eSEric Schrock 3620fa9e4066Sahrens /* 3621fa9e4066Sahrens * ========================================================================== 3622fa9e4066Sahrens * SPA Scrubbing 3623fa9e4066Sahrens * ========================================================================== 3624fa9e4066Sahrens */ 3625fa9e4066Sahrens 3626ea8dc4b6Seschrock int 3627088f3894Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3628fa9e4066Sahrens { 3629e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 3630bb8b5132Sek 3631fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3632fa9e4066Sahrens return (ENOTSUP); 3633fa9e4066Sahrens 3634fa9e4066Sahrens /* 3635088f3894Sahrens * If a resilver was requested, but there is no DTL on a 3636088f3894Sahrens * writeable leaf device, we have nothing to do. 3637fa9e4066Sahrens */ 3638088f3894Sahrens if (type == POOL_SCRUB_RESILVER && 3639088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 3640088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3641ea8dc4b6Seschrock return (0); 3642ea8dc4b6Seschrock } 3643fa9e4066Sahrens 3644088f3894Sahrens if (type == POOL_SCRUB_EVERYTHING && 3645088f3894Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 3646088f3894Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 3647088f3894Sahrens return (EBUSY); 3648fa9e4066Sahrens 3649088f3894Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 3650088f3894Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 3651088f3894Sahrens } else if (type == POOL_SCRUB_NONE) { 3652088f3894Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 3653ea8dc4b6Seschrock } else { 3654088f3894Sahrens return (EINVAL); 3655fa9e4066Sahrens } 3656fa9e4066Sahrens } 3657fa9e4066Sahrens 3658ea8dc4b6Seschrock /* 3659ea8dc4b6Seschrock * ========================================================================== 3660ea8dc4b6Seschrock * SPA async task processing 3661ea8dc4b6Seschrock * ========================================================================== 3662ea8dc4b6Seschrock */ 3663ea8dc4b6Seschrock 3664ea8dc4b6Seschrock static void 36653d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3666fa9e4066Sahrens { 366749cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 366849cf58c0SBrendan Gregg - Sun Microsystems vd->vdev_remove_wanted = 0; 366949cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 3670e14bb325SJeff Bonwick vdev_clear(spa, vd); 3671e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3672ea8dc4b6Seschrock } 367349cf58c0SBrendan Gregg - Sun Microsystems 3674e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 367549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 3676ea8dc4b6Seschrock } 3677fa9e4066Sahrens 3678e14bb325SJeff Bonwick static void 3679e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 3680e14bb325SJeff Bonwick { 3681e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 3682e14bb325SJeff Bonwick vd->vdev_probe_wanted = 0; 3683e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 3684e14bb325SJeff Bonwick } 3685e14bb325SJeff Bonwick 3686e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3687e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 3688e14bb325SJeff Bonwick } 3689e14bb325SJeff Bonwick 3690*573ca77eSGeorge Wilson static void 3691*573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 3692*573ca77eSGeorge Wilson { 3693*573ca77eSGeorge Wilson sysevent_id_t eid; 3694*573ca77eSGeorge Wilson nvlist_t *attr; 3695*573ca77eSGeorge Wilson char *physpath; 3696*573ca77eSGeorge Wilson 3697*573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 3698*573ca77eSGeorge Wilson return; 3699*573ca77eSGeorge Wilson 3700*573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 3701*573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 3702*573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 3703*573ca77eSGeorge Wilson } 3704*573ca77eSGeorge Wilson 3705*573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 3706*573ca77eSGeorge Wilson return; 3707*573ca77eSGeorge Wilson 3708*573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 3709*573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 3710*573ca77eSGeorge Wilson 3711*573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3712*573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 3713*573ca77eSGeorge Wilson 3714*573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 3715*573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 3716*573ca77eSGeorge Wilson 3717*573ca77eSGeorge Wilson nvlist_free(attr); 3718*573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 3719*573ca77eSGeorge Wilson } 3720*573ca77eSGeorge Wilson 3721ea8dc4b6Seschrock static void 3722ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 3723ea8dc4b6Seschrock { 3724e14bb325SJeff Bonwick int tasks; 3725ea8dc4b6Seschrock 3726ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 3727ea8dc4b6Seschrock 3728ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3729ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 3730ea8dc4b6Seschrock spa->spa_async_tasks = 0; 3731ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3732ea8dc4b6Seschrock 37330373e76bSbonwick /* 37340373e76bSbonwick * See if the config needs to be updated. 37350373e76bSbonwick */ 37360373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 3737*573ca77eSGeorge Wilson uint64_t oldsz, space_update; 3738*573ca77eSGeorge Wilson 37390373e76bSbonwick mutex_enter(&spa_namespace_lock); 3740*573ca77eSGeorge Wilson oldsz = spa_get_space(spa); 37410373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 3742*573ca77eSGeorge Wilson space_update = spa_get_space(spa) - oldsz; 37430373e76bSbonwick mutex_exit(&spa_namespace_lock); 3744*573ca77eSGeorge Wilson 3745*573ca77eSGeorge Wilson /* 3746*573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 3747*573ca77eSGeorge Wilson * then log an internal history event. 3748*573ca77eSGeorge Wilson */ 3749*573ca77eSGeorge Wilson if (space_update) { 3750*573ca77eSGeorge Wilson dmu_tx_t *tx; 3751*573ca77eSGeorge Wilson 3752*573ca77eSGeorge Wilson tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 3753*573ca77eSGeorge Wilson if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 3754*573ca77eSGeorge Wilson spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 3755*573ca77eSGeorge Wilson spa, tx, CRED(), 3756*573ca77eSGeorge Wilson "pool '%s' size: %llu(+%llu)", 3757*573ca77eSGeorge Wilson spa_name(spa), spa_get_space(spa), 3758*573ca77eSGeorge Wilson space_update); 3759*573ca77eSGeorge Wilson dmu_tx_commit(tx); 3760*573ca77eSGeorge Wilson } else { 3761*573ca77eSGeorge Wilson dmu_tx_abort(tx); 3762*573ca77eSGeorge Wilson } 3763*573ca77eSGeorge Wilson } 37640373e76bSbonwick } 37650373e76bSbonwick 3766ea8dc4b6Seschrock /* 37673d7072f8Seschrock * See if any devices need to be marked REMOVED. 3768ea8dc4b6Seschrock */ 3769e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 3770e14bb325SJeff Bonwick spa_vdev_state_enter(spa); 37713d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 3772e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 377349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 3774e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 377549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 3776e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 3777e14bb325SJeff Bonwick } 3778e14bb325SJeff Bonwick 3779*573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 3780*573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3781*573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 3782*573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 3783*573ca77eSGeorge Wilson } 3784*573ca77eSGeorge Wilson 3785e14bb325SJeff Bonwick /* 3786e14bb325SJeff Bonwick * See if any devices need to be probed. 3787e14bb325SJeff Bonwick */ 3788e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 3789e14bb325SJeff Bonwick spa_vdev_state_enter(spa); 3790e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 3791e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 37923d7072f8Seschrock } 3793ea8dc4b6Seschrock 3794ea8dc4b6Seschrock /* 3795ea8dc4b6Seschrock * If any devices are done replacing, detach them. 3796ea8dc4b6Seschrock */ 37973d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 37983d7072f8Seschrock spa_vdev_resilver_done(spa); 3799fa9e4066Sahrens 3800ea8dc4b6Seschrock /* 3801ea8dc4b6Seschrock * Kick off a resilver. 3802ea8dc4b6Seschrock */ 3803088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 3804088f3894Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 3805ea8dc4b6Seschrock 3806ea8dc4b6Seschrock /* 3807ea8dc4b6Seschrock * Let the world know that we're done. 3808ea8dc4b6Seschrock */ 3809ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3810ea8dc4b6Seschrock spa->spa_async_thread = NULL; 3811ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 3812ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3813ea8dc4b6Seschrock thread_exit(); 3814ea8dc4b6Seschrock } 3815ea8dc4b6Seschrock 3816ea8dc4b6Seschrock void 3817ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 3818ea8dc4b6Seschrock { 3819ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3820ea8dc4b6Seschrock spa->spa_async_suspended++; 3821ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 3822ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 3823ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3824ea8dc4b6Seschrock } 3825ea8dc4b6Seschrock 3826ea8dc4b6Seschrock void 3827ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 3828ea8dc4b6Seschrock { 3829ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3830ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 3831ea8dc4b6Seschrock spa->spa_async_suspended--; 3832ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3833ea8dc4b6Seschrock } 3834ea8dc4b6Seschrock 3835ea8dc4b6Seschrock static void 3836ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 3837ea8dc4b6Seschrock { 3838ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3839ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38400373e76bSbonwick spa->spa_async_thread == NULL && 38410373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 3842ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 3843ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 3844ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3845ea8dc4b6Seschrock } 3846ea8dc4b6Seschrock 3847ea8dc4b6Seschrock void 3848ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 3849ea8dc4b6Seschrock { 3850ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3851ea8dc4b6Seschrock spa->spa_async_tasks |= task; 3852ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3853fa9e4066Sahrens } 3854fa9e4066Sahrens 3855fa9e4066Sahrens /* 3856fa9e4066Sahrens * ========================================================================== 3857fa9e4066Sahrens * SPA syncing routines 3858fa9e4066Sahrens * ========================================================================== 3859fa9e4066Sahrens */ 3860fa9e4066Sahrens 3861fa9e4066Sahrens static void 3862fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3863fa9e4066Sahrens { 3864fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3865fa9e4066Sahrens dmu_tx_t *tx; 3866fa9e4066Sahrens blkptr_t blk; 3867fa9e4066Sahrens uint64_t itor = 0; 3868fa9e4066Sahrens zio_t *zio; 3869fa9e4066Sahrens int error; 3870fa9e4066Sahrens uint8_t c = 1; 3871fa9e4066Sahrens 3872e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 3873fa9e4066Sahrens 3874e14bb325SJeff Bonwick while (bplist_iterate(bpl, &itor, &blk) == 0) { 3875e14bb325SJeff Bonwick ASSERT(blk.blk_birth < txg); 3876e14bb325SJeff Bonwick zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 3877e14bb325SJeff Bonwick ZIO_FLAG_MUSTSUCCEED)); 3878e14bb325SJeff Bonwick } 3879fa9e4066Sahrens 3880fa9e4066Sahrens error = zio_wait(zio); 3881fa9e4066Sahrens ASSERT3U(error, ==, 0); 3882fa9e4066Sahrens 3883fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3884fa9e4066Sahrens bplist_vacate(bpl, tx); 3885fa9e4066Sahrens 3886fa9e4066Sahrens /* 3887fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 3888fa9e4066Sahrens * (Usually only the first block is needed.) 3889fa9e4066Sahrens */ 3890fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3891fa9e4066Sahrens dmu_tx_commit(tx); 3892fa9e4066Sahrens } 3893fa9e4066Sahrens 3894fa9e4066Sahrens static void 389599653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 3896fa9e4066Sahrens { 3897fa9e4066Sahrens char *packed = NULL; 3898f7991ba4STim Haley size_t bufsize; 3899fa9e4066Sahrens size_t nvsize = 0; 3900fa9e4066Sahrens dmu_buf_t *db; 3901fa9e4066Sahrens 390299653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 3903fa9e4066Sahrens 3904f7991ba4STim Haley /* 3905f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 3906f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 3907f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 3908f7991ba4STim Haley */ 3909f7991ba4STim Haley bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 3910f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 3911fa9e4066Sahrens 391299653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 3913ea8dc4b6Seschrock KM_SLEEP) == 0); 3914f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 3915fa9e4066Sahrens 3916f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 3917fa9e4066Sahrens 3918f7991ba4STim Haley kmem_free(packed, bufsize); 3919fa9e4066Sahrens 392099653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3921fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 3922fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 3923ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 3924fa9e4066Sahrens } 3925fa9e4066Sahrens 392699653d4eSeschrock static void 3927fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 3928fa94a07fSbrendan const char *config, const char *entry) 392999653d4eSeschrock { 393099653d4eSeschrock nvlist_t *nvroot; 3931fa94a07fSbrendan nvlist_t **list; 393299653d4eSeschrock int i; 393399653d4eSeschrock 3934fa94a07fSbrendan if (!sav->sav_sync) 393599653d4eSeschrock return; 393699653d4eSeschrock 393799653d4eSeschrock /* 3938fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 3939fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 39403d7072f8Seschrock * valid and the vdevs are labeled appropriately. 394199653d4eSeschrock */ 3942fa94a07fSbrendan if (sav->sav_object == 0) { 3943fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 3944fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 3945fa94a07fSbrendan sizeof (uint64_t), tx); 394699653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 3947fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 3948fa94a07fSbrendan &sav->sav_object, tx) == 0); 394999653d4eSeschrock } 395099653d4eSeschrock 395199653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3952fa94a07fSbrendan if (sav->sav_count == 0) { 3953fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 395499653d4eSeschrock } else { 3955fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 3956fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3957fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 3958fa94a07fSbrendan B_FALSE, B_FALSE, B_TRUE); 3959fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 3960fa94a07fSbrendan sav->sav_count) == 0); 3961fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3962fa94a07fSbrendan nvlist_free(list[i]); 3963fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 396499653d4eSeschrock } 396599653d4eSeschrock 3966fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 396706eeb2adSek nvlist_free(nvroot); 396899653d4eSeschrock 3969fa94a07fSbrendan sav->sav_sync = B_FALSE; 397099653d4eSeschrock } 397199653d4eSeschrock 397299653d4eSeschrock static void 397399653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 397499653d4eSeschrock { 397599653d4eSeschrock nvlist_t *config; 397699653d4eSeschrock 3977e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 397899653d4eSeschrock return; 397999653d4eSeschrock 3980e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3981e14bb325SJeff Bonwick 3982e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 3983e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 3984e14bb325SJeff Bonwick 3985e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 398699653d4eSeschrock 398799653d4eSeschrock if (spa->spa_config_syncing) 398899653d4eSeschrock nvlist_free(spa->spa_config_syncing); 398999653d4eSeschrock spa->spa_config_syncing = config; 399099653d4eSeschrock 399199653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 399299653d4eSeschrock } 399399653d4eSeschrock 3994990b4856Slling /* 3995990b4856Slling * Set zpool properties. 3996990b4856Slling */ 3997b1b8ab34Slling static void 3998ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 3999b1b8ab34Slling { 4000b1b8ab34Slling spa_t *spa = arg1; 4001b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 4002990b4856Slling nvlist_t *nvp = arg2; 4003990b4856Slling nvpair_t *elem; 40043d7072f8Seschrock uint64_t intval; 4005c5904d13Seschrock char *strval; 4006990b4856Slling zpool_prop_t prop; 4007990b4856Slling const char *propname; 4008990b4856Slling zprop_type_t proptype; 4009b1b8ab34Slling 4010e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 4011e14bb325SJeff Bonwick 4012990b4856Slling elem = NULL; 4013990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 4014990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 4015990b4856Slling case ZPOOL_PROP_VERSION: 4016990b4856Slling /* 4017990b4856Slling * Only set version for non-zpool-creation cases 4018990b4856Slling * (set/import). spa_create() needs special care 4019990b4856Slling * for version setting. 4020990b4856Slling */ 4021990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 4022990b4856Slling VERIFY(nvpair_value_uint64(elem, 4023990b4856Slling &intval) == 0); 4024990b4856Slling ASSERT(intval <= SPA_VERSION); 4025990b4856Slling ASSERT(intval >= spa_version(spa)); 4026990b4856Slling spa->spa_uberblock.ub_version = intval; 4027990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 4028990b4856Slling } 4029ecd6cf80Smarks break; 4030990b4856Slling 4031990b4856Slling case ZPOOL_PROP_ALTROOT: 4032990b4856Slling /* 4033990b4856Slling * 'altroot' is a non-persistent property. It should 4034990b4856Slling * have been set temporarily at creation or import time. 4035990b4856Slling */ 4036990b4856Slling ASSERT(spa->spa_root != NULL); 4037b1b8ab34Slling break; 40383d7072f8Seschrock 40392f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4040990b4856Slling /* 4041379c004dSEric Schrock * 'cachefile' is also a non-persisitent property. 4042990b4856Slling */ 40433d7072f8Seschrock break; 4044990b4856Slling default: 4045990b4856Slling /* 4046990b4856Slling * Set pool property values in the poolprops mos object. 4047990b4856Slling */ 4048990b4856Slling if (spa->spa_pool_props_object == 0) { 4049990b4856Slling objset_t *mos = spa->spa_meta_objset; 4050990b4856Slling 4051990b4856Slling VERIFY((spa->spa_pool_props_object = 4052990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 4053990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 4054990b4856Slling 4055990b4856Slling VERIFY(zap_update(mos, 4056990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 4057990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 4058990b4856Slling == 0); 4059990b4856Slling } 4060990b4856Slling 4061990b4856Slling /* normalize the property name */ 4062990b4856Slling propname = zpool_prop_to_name(prop); 4063990b4856Slling proptype = zpool_prop_get_type(prop); 4064990b4856Slling 4065990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 4066990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 4067990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 4068990b4856Slling VERIFY(zap_update(mos, 4069990b4856Slling spa->spa_pool_props_object, propname, 4070990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 4071990b4856Slling 4072990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 4073990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 4074990b4856Slling 4075990b4856Slling if (proptype == PROP_TYPE_INDEX) { 4076990b4856Slling const char *unused; 4077990b4856Slling VERIFY(zpool_prop_index_to_string( 4078990b4856Slling prop, intval, &unused) == 0); 4079990b4856Slling } 4080990b4856Slling VERIFY(zap_update(mos, 4081990b4856Slling spa->spa_pool_props_object, propname, 4082990b4856Slling 8, 1, &intval, tx) == 0); 4083990b4856Slling } else { 4084990b4856Slling ASSERT(0); /* not allowed */ 4085990b4856Slling } 4086990b4856Slling 40870a4e9518Sgw switch (prop) { 40880a4e9518Sgw case ZPOOL_PROP_DELEGATION: 4089990b4856Slling spa->spa_delegation = intval; 40900a4e9518Sgw break; 40910a4e9518Sgw case ZPOOL_PROP_BOOTFS: 4092990b4856Slling spa->spa_bootfs = intval; 40930a4e9518Sgw break; 40940a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 40950a4e9518Sgw spa->spa_failmode = intval; 40960a4e9518Sgw break; 4097*573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 4098*573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 4099*573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4100*573ca77eSGeorge Wilson break; 41010a4e9518Sgw default: 41020a4e9518Sgw break; 41030a4e9518Sgw } 4104990b4856Slling } 4105990b4856Slling 4106990b4856Slling /* log internal history if this is not a zpool create */ 4107990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 4108990b4856Slling tx->tx_txg != TXG_INITIAL) { 4109990b4856Slling spa_history_internal_log(LOG_POOL_PROPSET, 4110990b4856Slling spa, tx, cr, "%s %lld %s", 4111e14bb325SJeff Bonwick nvpair_name(elem), intval, spa_name(spa)); 4112b1b8ab34Slling } 4113b1b8ab34Slling } 4114e14bb325SJeff Bonwick 4115e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 4116b1b8ab34Slling } 4117b1b8ab34Slling 4118fa9e4066Sahrens /* 4119fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4120fa9e4066Sahrens * part of the process, so we iterate until it converges. 4121fa9e4066Sahrens */ 4122fa9e4066Sahrens void 4123fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 4124fa9e4066Sahrens { 4125fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4126fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 4127fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41280373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 4129fa9e4066Sahrens vdev_t *vd; 4130fa9e4066Sahrens dmu_tx_t *tx; 4131fa9e4066Sahrens int dirty_vdevs; 4132e14bb325SJeff Bonwick int error; 4133fa9e4066Sahrens 4134fa9e4066Sahrens /* 4135fa9e4066Sahrens * Lock out configuration changes. 4136fa9e4066Sahrens */ 4137e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4138fa9e4066Sahrens 4139fa9e4066Sahrens spa->spa_syncing_txg = txg; 4140fa9e4066Sahrens spa->spa_sync_pass = 0; 4141fa9e4066Sahrens 4142e14bb325SJeff Bonwick /* 4143e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 4144e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 4145e14bb325SJeff Bonwick */ 4146e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 41478ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 41488ad4d6ddSJeff Bonwick /* 41498ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 41508ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 41518ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 41528ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 41538ad4d6ddSJeff Bonwick * into the root vdev tree. 41548ad4d6ddSJeff Bonwick */ 41558ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 41568ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 41578ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 41588ad4d6ddSJeff Bonwick vdev_state_clean(vd); 41598ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 41608ad4d6ddSJeff Bonwick } 41618ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 41628ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 4163e14bb325SJeff Bonwick } 4164e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 4165e14bb325SJeff Bonwick 4166ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4167fa9e4066Sahrens 416899653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 416999653d4eSeschrock 417099653d4eSeschrock /* 4171e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 417299653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 417399653d4eSeschrock */ 4174e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 4175e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 417699653d4eSeschrock int i; 417799653d4eSeschrock 417899653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 417999653d4eSeschrock vd = rvd->vdev_child[i]; 418099653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 418199653d4eSeschrock break; 418299653d4eSeschrock } 418399653d4eSeschrock if (i == rvd->vdev_children) { 418499653d4eSeschrock spa->spa_deflate = TRUE; 418599653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 418699653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 418799653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 418899653d4eSeschrock } 418999653d4eSeschrock } 419099653d4eSeschrock 4191088f3894Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 4192088f3894Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 4193088f3894Sahrens dsl_pool_create_origin(dp, tx); 4194088f3894Sahrens 4195088f3894Sahrens /* Keeping the origin open increases spa_minref */ 4196088f3894Sahrens spa->spa_minref += 3; 4197088f3894Sahrens } 4198088f3894Sahrens 4199088f3894Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 4200088f3894Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 4201088f3894Sahrens dsl_pool_upgrade_clones(dp, tx); 4202088f3894Sahrens } 4203088f3894Sahrens 4204fa9e4066Sahrens /* 4205fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 4206fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 4207fa9e4066Sahrens * don't generate work on an otherwise idle system. 4208fa9e4066Sahrens */ 4209fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 42101615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 42111615a317Sek !txg_list_empty(&dp->dp_sync_tasks, txg)) 4212fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 4213fa9e4066Sahrens 4214fa9e4066Sahrens /* 4215fa9e4066Sahrens * Iterate to convergence. 4216fa9e4066Sahrens */ 4217fa9e4066Sahrens do { 4218fa9e4066Sahrens spa->spa_sync_pass++; 4219fa9e4066Sahrens 4220fa9e4066Sahrens spa_sync_config_object(spa, tx); 4221fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 4222fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 4223fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 4224fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 4225ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 4226fa9e4066Sahrens dsl_pool_sync(dp, txg); 4227fa9e4066Sahrens 4228fa9e4066Sahrens dirty_vdevs = 0; 4229fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4230fa9e4066Sahrens vdev_sync(vd, txg); 4231fa9e4066Sahrens dirty_vdevs++; 4232fa9e4066Sahrens } 4233fa9e4066Sahrens 4234fa9e4066Sahrens bplist_sync(bpl, tx); 4235fa9e4066Sahrens } while (dirty_vdevs); 4236fa9e4066Sahrens 4237fa9e4066Sahrens bplist_close(bpl); 4238fa9e4066Sahrens 4239fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4240fa9e4066Sahrens 4241fa9e4066Sahrens /* 4242fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4243fa9e4066Sahrens * to commit the transaction group. 42440373e76bSbonwick * 424517f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 424617f17c2dSbonwick * random top-level vdevs that are known to be visible in the 4247e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 4248e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 42490373e76bSbonwick */ 4250e14bb325SJeff Bonwick for (;;) { 4251e14bb325SJeff Bonwick /* 4252e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 4253e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 4254e14bb325SJeff Bonwick */ 4255e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4256e14bb325SJeff Bonwick 4257e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 4258e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 4259e14bb325SJeff Bonwick int svdcount = 0; 4260e14bb325SJeff Bonwick int children = rvd->vdev_children; 4261e14bb325SJeff Bonwick int c0 = spa_get_random(children); 4262e14bb325SJeff Bonwick 4263*573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 4264e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 4265e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 4266e14bb325SJeff Bonwick continue; 4267e14bb325SJeff Bonwick svd[svdcount++] = vd; 4268e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 4269e14bb325SJeff Bonwick break; 4270e14bb325SJeff Bonwick } 42718956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 42728956713aSEric Schrock if (error != 0) 42738956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 42748956713aSEric Schrock B_TRUE); 4275e14bb325SJeff Bonwick } else { 4276e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 42778956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 42788956713aSEric Schrock if (error != 0) 42798956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 42808956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 42810373e76bSbonwick } 4282e14bb325SJeff Bonwick 4283e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 4284e14bb325SJeff Bonwick 4285e14bb325SJeff Bonwick if (error == 0) 4286e14bb325SJeff Bonwick break; 4287e14bb325SJeff Bonwick zio_suspend(spa, NULL); 4288e14bb325SJeff Bonwick zio_resume_wait(spa); 42890373e76bSbonwick } 429099653d4eSeschrock dmu_tx_commit(tx); 429199653d4eSeschrock 42920373e76bSbonwick /* 42930373e76bSbonwick * Clear the dirty config list. 4294fa9e4066Sahrens */ 4295e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 42960373e76bSbonwick vdev_config_clean(vd); 42970373e76bSbonwick 42980373e76bSbonwick /* 42990373e76bSbonwick * Now that the new config has synced transactionally, 43000373e76bSbonwick * let it become visible to the config cache. 43010373e76bSbonwick */ 43020373e76bSbonwick if (spa->spa_config_syncing != NULL) { 43030373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 43040373e76bSbonwick spa->spa_config_txg = txg; 43050373e76bSbonwick spa->spa_config_syncing = NULL; 43060373e76bSbonwick } 4307fa9e4066Sahrens 4308fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 4309fa9e4066Sahrens 4310fa9e4066Sahrens /* 4311fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 4312fa9e4066Sahrens */ 4313fa9e4066Sahrens dsl_pool_zil_clean(dp); 4314fa9e4066Sahrens 4315fa9e4066Sahrens /* 4316fa9e4066Sahrens * Update usable space statistics. 4317fa9e4066Sahrens */ 4318fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4319fa9e4066Sahrens vdev_sync_done(vd, txg); 4320fa9e4066Sahrens 4321fa9e4066Sahrens /* 4322fa9e4066Sahrens * It had better be the case that we didn't dirty anything 432399653d4eSeschrock * since vdev_config_sync(). 4324fa9e4066Sahrens */ 4325fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4326fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4327fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4328fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 4329fa9e4066Sahrens 4330e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 4331ea8dc4b6Seschrock 4332ea8dc4b6Seschrock /* 4333ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 4334ea8dc4b6Seschrock */ 4335ea8dc4b6Seschrock spa_async_dispatch(spa); 4336fa9e4066Sahrens } 4337fa9e4066Sahrens 4338fa9e4066Sahrens /* 4339fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4340fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4341fa9e4066Sahrens * sync. 4342fa9e4066Sahrens */ 4343fa9e4066Sahrens void 4344fa9e4066Sahrens spa_sync_allpools(void) 4345fa9e4066Sahrens { 4346fa9e4066Sahrens spa_t *spa = NULL; 4347fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4348fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 4349e14bb325SJeff Bonwick if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4350fa9e4066Sahrens continue; 4351fa9e4066Sahrens spa_open_ref(spa, FTAG); 4352fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4353fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4354fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4355fa9e4066Sahrens spa_close(spa, FTAG); 4356fa9e4066Sahrens } 4357fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4358fa9e4066Sahrens } 4359fa9e4066Sahrens 4360fa9e4066Sahrens /* 4361fa9e4066Sahrens * ========================================================================== 4362fa9e4066Sahrens * Miscellaneous routines 4363fa9e4066Sahrens * ========================================================================== 4364fa9e4066Sahrens */ 4365fa9e4066Sahrens 4366fa9e4066Sahrens /* 4367fa9e4066Sahrens * Remove all pools in the system. 4368fa9e4066Sahrens */ 4369fa9e4066Sahrens void 4370fa9e4066Sahrens spa_evict_all(void) 4371fa9e4066Sahrens { 4372fa9e4066Sahrens spa_t *spa; 4373fa9e4066Sahrens 4374fa9e4066Sahrens /* 4375fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 4376fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 4377fa9e4066Sahrens */ 4378fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4379fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 4380fa9e4066Sahrens /* 4381ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 4382ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 4383ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 4384fa9e4066Sahrens */ 4385fa9e4066Sahrens spa_open_ref(spa, FTAG); 4386fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4387ea8dc4b6Seschrock spa_async_suspend(spa); 4388fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4389fa9e4066Sahrens spa_close(spa, FTAG); 4390fa9e4066Sahrens 4391fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4392fa9e4066Sahrens spa_unload(spa); 4393fa9e4066Sahrens spa_deactivate(spa); 4394fa9e4066Sahrens } 4395fa9e4066Sahrens spa_remove(spa); 4396fa9e4066Sahrens } 4397fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4398fa9e4066Sahrens } 4399ea8dc4b6Seschrock 4400ea8dc4b6Seschrock vdev_t * 44016809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 4402ea8dc4b6Seschrock { 4403c5904d13Seschrock vdev_t *vd; 4404c5904d13Seschrock int i; 4405c5904d13Seschrock 4406c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 4407c5904d13Seschrock return (vd); 4408c5904d13Seschrock 44096809eb4eSEric Schrock if (aux) { 4410c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 4411c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 44126809eb4eSEric Schrock if (vd->vdev_guid == guid) 44136809eb4eSEric Schrock return (vd); 44146809eb4eSEric Schrock } 44156809eb4eSEric Schrock 44166809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 44176809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 4418c5904d13Seschrock if (vd->vdev_guid == guid) 4419c5904d13Seschrock return (vd); 4420c5904d13Seschrock } 4421c5904d13Seschrock } 4422c5904d13Seschrock 4423c5904d13Seschrock return (NULL); 4424ea8dc4b6Seschrock } 4425eaca9bbdSeschrock 4426eaca9bbdSeschrock void 4427990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 4428eaca9bbdSeschrock { 4429e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4430eaca9bbdSeschrock 4431eaca9bbdSeschrock /* 4432eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 4433eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 4434eaca9bbdSeschrock * possible. 4435eaca9bbdSeschrock */ 4436e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 4437990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 4438eaca9bbdSeschrock 4439990b4856Slling spa->spa_uberblock.ub_version = version; 4440eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 4441eaca9bbdSeschrock 4442e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 444399653d4eSeschrock 444499653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 444599653d4eSeschrock } 444699653d4eSeschrock 444799653d4eSeschrock boolean_t 444899653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 444999653d4eSeschrock { 445099653d4eSeschrock int i; 445139c23413Seschrock uint64_t spareguid; 4452fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 445399653d4eSeschrock 4454fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 4455fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 445699653d4eSeschrock return (B_TRUE); 445799653d4eSeschrock 4458fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 4459fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 4460fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 446139c23413Seschrock return (B_TRUE); 446239c23413Seschrock } 446339c23413Seschrock 446499653d4eSeschrock return (B_FALSE); 4465eaca9bbdSeschrock } 4466b1b8ab34Slling 446789a89ebfSlling /* 446889a89ebfSlling * Check if a pool has an active shared spare device. 446989a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 447089a89ebfSlling */ 447189a89ebfSlling static boolean_t 447289a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 447389a89ebfSlling { 447489a89ebfSlling int i, refcnt; 447589a89ebfSlling uint64_t pool; 447689a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 447789a89ebfSlling 447889a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 447989a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 448089a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 448189a89ebfSlling refcnt > 2) 448289a89ebfSlling return (B_TRUE); 448389a89ebfSlling } 448489a89ebfSlling 448589a89ebfSlling return (B_FALSE); 448689a89ebfSlling } 448789a89ebfSlling 44883d7072f8Seschrock /* 44893d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44903d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44913d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44923d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44933d7072f8Seschrock * or zdb as real changes. 44943d7072f8Seschrock */ 44953d7072f8Seschrock void 44963d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 44973d7072f8Seschrock { 44983d7072f8Seschrock #ifdef _KERNEL 44993d7072f8Seschrock sysevent_t *ev; 45003d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 45013d7072f8Seschrock sysevent_value_t value; 45023d7072f8Seschrock sysevent_id_t eid; 45033d7072f8Seschrock 45043d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 45053d7072f8Seschrock SE_SLEEP); 45063d7072f8Seschrock 45073d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 45083d7072f8Seschrock value.value.sv_string = spa_name(spa); 45093d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 45103d7072f8Seschrock goto done; 45113d7072f8Seschrock 45123d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 45133d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 45143d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 45153d7072f8Seschrock goto done; 45163d7072f8Seschrock 45173d7072f8Seschrock if (vd) { 45183d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 45193d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 45203d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 45213d7072f8Seschrock SE_SLEEP) != 0) 45223d7072f8Seschrock goto done; 45233d7072f8Seschrock 45243d7072f8Seschrock if (vd->vdev_path) { 45253d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 45263d7072f8Seschrock value.value.sv_string = vd->vdev_path; 45273d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 45283d7072f8Seschrock &value, SE_SLEEP) != 0) 45293d7072f8Seschrock goto done; 45303d7072f8Seschrock } 45313d7072f8Seschrock } 45323d7072f8Seschrock 4533b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 4534b01c3b58Seschrock goto done; 4535b01c3b58Seschrock attr = NULL; 4536b01c3b58Seschrock 45373d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 45383d7072f8Seschrock 45393d7072f8Seschrock done: 45403d7072f8Seschrock if (attr) 45413d7072f8Seschrock sysevent_free_attr(attr); 45423d7072f8Seschrock sysevent_free(ev); 45433d7072f8Seschrock #endif 45443d7072f8Seschrock } 4545