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> 62573ca77eSGeorge 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: 336573ca77eSGeorge 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 374*503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 375990b4856Slling break; 37615e6edf1Sgw 377*503ad85cSMatthew Ahrens /* Must be ZPL and not gzip compressed. */ 378*503ad85cSMatthew Ahrens 379*503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 380*503ad85cSMatthew Ahrens error = ENOTSUP; 381*503ad85cSMatthew Ahrens } else if ((error = dsl_prop_get_integer(strval, 38215e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 38315e6edf1Sgw &compress, NULL)) == 0 && 38415e6edf1Sgw !BOOTFS_COMPRESS_VALID(compress)) { 38515e6edf1Sgw error = ENOTSUP; 38615e6edf1Sgw } else { 38715e6edf1Sgw objnum = dmu_objset_id(os); 38815e6edf1Sgw } 389*503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 390990b4856Slling } 391990b4856Slling break; 392e14bb325SJeff Bonwick 3930a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 3940a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 3950a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3960a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 3970a4e9518Sgw error = EINVAL; 3980a4e9518Sgw 3990a4e9518Sgw /* 4000a4e9518Sgw * This is a special case which only occurs when 4010a4e9518Sgw * the pool has completely failed. This allows 4020a4e9518Sgw * the user to change the in-core failmode property 4030a4e9518Sgw * without syncing it out to disk (I/Os might 4040a4e9518Sgw * currently be blocked). We do this by returning 4050a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 4060a4e9518Sgw * into thinking we encountered a property validation 4070a4e9518Sgw * error. 4080a4e9518Sgw */ 409e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 4100a4e9518Sgw spa->spa_failmode = intval; 4110a4e9518Sgw error = EIO; 4120a4e9518Sgw } 4130a4e9518Sgw break; 4142f8aaab3Seschrock 4152f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4162f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4172f8aaab3Seschrock break; 4182f8aaab3Seschrock 4192f8aaab3Seschrock if (strval[0] == '\0') 4202f8aaab3Seschrock break; 4212f8aaab3Seschrock 4222f8aaab3Seschrock if (strcmp(strval, "none") == 0) 4232f8aaab3Seschrock break; 4242f8aaab3Seschrock 4252f8aaab3Seschrock if (strval[0] != '/') { 4262f8aaab3Seschrock error = EINVAL; 4272f8aaab3Seschrock break; 4282f8aaab3Seschrock } 4292f8aaab3Seschrock 4302f8aaab3Seschrock slash = strrchr(strval, '/'); 4312f8aaab3Seschrock ASSERT(slash != NULL); 4322f8aaab3Seschrock 4332f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4342f8aaab3Seschrock strcmp(slash, "/..") == 0) 4352f8aaab3Seschrock error = EINVAL; 4362f8aaab3Seschrock break; 437990b4856Slling } 438990b4856Slling 439990b4856Slling if (error) 440990b4856Slling break; 441990b4856Slling } 442990b4856Slling 443990b4856Slling if (!error && reset_bootfs) { 444990b4856Slling error = nvlist_remove(props, 445990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 446990b4856Slling 447990b4856Slling if (!error) { 448990b4856Slling error = nvlist_add_uint64(props, 449990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 450990b4856Slling } 451990b4856Slling } 452990b4856Slling 453990b4856Slling return (error); 454990b4856Slling } 455990b4856Slling 456379c004dSEric Schrock void 457379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 458379c004dSEric Schrock { 459379c004dSEric Schrock char *cachefile; 460379c004dSEric Schrock spa_config_dirent_t *dp; 461379c004dSEric Schrock 462379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 463379c004dSEric Schrock &cachefile) != 0) 464379c004dSEric Schrock return; 465379c004dSEric Schrock 466379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 467379c004dSEric Schrock KM_SLEEP); 468379c004dSEric Schrock 469379c004dSEric Schrock if (cachefile[0] == '\0') 470379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 471379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 472379c004dSEric Schrock dp->scd_path = NULL; 473379c004dSEric Schrock else 474379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 475379c004dSEric Schrock 476379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 477379c004dSEric Schrock if (need_sync) 478379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 479379c004dSEric Schrock } 480379c004dSEric Schrock 481990b4856Slling int 482990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 483990b4856Slling { 484990b4856Slling int error; 485379c004dSEric Schrock nvpair_t *elem; 486379c004dSEric Schrock boolean_t need_sync = B_FALSE; 487379c004dSEric Schrock zpool_prop_t prop; 488990b4856Slling 489990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 490990b4856Slling return (error); 491990b4856Slling 492379c004dSEric Schrock elem = NULL; 493379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 494379c004dSEric Schrock if ((prop = zpool_name_to_prop( 495379c004dSEric Schrock nvpair_name(elem))) == ZPROP_INVAL) 496379c004dSEric Schrock return (EINVAL); 497379c004dSEric Schrock 498379c004dSEric Schrock if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 499379c004dSEric Schrock continue; 500379c004dSEric Schrock 501379c004dSEric Schrock need_sync = B_TRUE; 502379c004dSEric Schrock break; 503379c004dSEric Schrock } 504379c004dSEric Schrock 505379c004dSEric Schrock if (need_sync) 506379c004dSEric Schrock return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 507379c004dSEric Schrock spa, nvp, 3)); 508379c004dSEric Schrock else 509379c004dSEric Schrock return (0); 510990b4856Slling } 511990b4856Slling 512990b4856Slling /* 513990b4856Slling * If the bootfs property value is dsobj, clear it. 514990b4856Slling */ 515990b4856Slling void 516990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 517990b4856Slling { 518990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 519990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 520990b4856Slling spa->spa_pool_props_object, 521990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 522990b4856Slling spa->spa_bootfs = 0; 523990b4856Slling } 524990b4856Slling } 525990b4856Slling 526fa9e4066Sahrens /* 527fa9e4066Sahrens * ========================================================================== 528fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 529fa9e4066Sahrens * ========================================================================== 530fa9e4066Sahrens */ 531fa9e4066Sahrens 532ea8dc4b6Seschrock static int 533ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 534ea8dc4b6Seschrock { 535ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 536ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 537ea8dc4b6Seschrock int ret; 538ea8dc4b6Seschrock 539ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 540ea8dc4b6Seschrock sizeof (zbookmark_t)); 541ea8dc4b6Seschrock 542ea8dc4b6Seschrock if (ret < 0) 543ea8dc4b6Seschrock return (-1); 544ea8dc4b6Seschrock else if (ret > 0) 545ea8dc4b6Seschrock return (1); 546ea8dc4b6Seschrock else 547ea8dc4b6Seschrock return (0); 548ea8dc4b6Seschrock } 549ea8dc4b6Seschrock 550ea8dc4b6Seschrock /* 551ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 552ea8dc4b6Seschrock * re-initializes them in the process. 553ea8dc4b6Seschrock */ 554ea8dc4b6Seschrock void 555ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 556ea8dc4b6Seschrock { 557ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 558ea8dc4b6Seschrock 559ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 560ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 561ea8dc4b6Seschrock 562ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 563ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 564ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 565ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 566ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 567ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 568ea8dc4b6Seschrock } 569ea8dc4b6Seschrock 570fa9e4066Sahrens /* 571fa9e4066Sahrens * Activate an uninitialized pool. 572fa9e4066Sahrens */ 573fa9e4066Sahrens static void 5748ad4d6ddSJeff Bonwick spa_activate(spa_t *spa, int mode) 575fa9e4066Sahrens { 576fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 577fa9e4066Sahrens 578fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 5798ad4d6ddSJeff Bonwick spa->spa_mode = mode; 580fa9e4066Sahrens 581d6e555bdSGeorge Wilson spa->spa_normal_class = metaslab_class_create(zfs_metaslab_ops); 582d6e555bdSGeorge Wilson spa->spa_log_class = metaslab_class_create(zfs_metaslab_ops); 583fa9e4066Sahrens 584e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 5852e0c549eSJonathan Adams const zio_taskq_info_t *ztip = &zio_taskqs[t]; 586e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5872e0c549eSJonathan Adams enum zti_modes mode = ztip->zti_nthreads[q].zti_mode; 5882e0c549eSJonathan Adams uint_t value = ztip->zti_nthreads[q].zti_value; 5892e0c549eSJonathan Adams char name[32]; 5902e0c549eSJonathan Adams 5912e0c549eSJonathan Adams (void) snprintf(name, sizeof (name), 5922e0c549eSJonathan Adams "%s_%s", ztip->zti_name, zio_taskq_types[q]); 5932e0c549eSJonathan Adams 5942e0c549eSJonathan Adams if (mode == zti_mode_tune) { 5952e0c549eSJonathan Adams mode = zio_taskq_tune_mode; 5962e0c549eSJonathan Adams value = zio_taskq_tune_value; 5972e0c549eSJonathan Adams if (mode == zti_mode_tune) 5982e0c549eSJonathan Adams mode = zti_mode_online_percent; 5992e0c549eSJonathan Adams } 6002e0c549eSJonathan Adams 6012e0c549eSJonathan Adams switch (mode) { 6022e0c549eSJonathan Adams case zti_mode_fixed: 6032e0c549eSJonathan Adams ASSERT3U(value, >=, 1); 6042e0c549eSJonathan Adams value = MAX(value, 1); 6052e0c549eSJonathan Adams 6062e0c549eSJonathan Adams spa->spa_zio_taskq[t][q] = taskq_create(name, 6072e0c549eSJonathan Adams value, maxclsyspri, 50, INT_MAX, 6082e0c549eSJonathan Adams TASKQ_PREPOPULATE); 6092e0c549eSJonathan Adams break; 6102e0c549eSJonathan Adams 6112e0c549eSJonathan Adams case zti_mode_online_percent: 6122e0c549eSJonathan Adams spa->spa_zio_taskq[t][q] = taskq_create(name, 6132e0c549eSJonathan Adams value, maxclsyspri, 50, INT_MAX, 6142e0c549eSJonathan Adams TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 6152e0c549eSJonathan Adams break; 6162e0c549eSJonathan Adams 6172e0c549eSJonathan Adams case zti_mode_tune: 6182e0c549eSJonathan Adams default: 6192e0c549eSJonathan Adams panic("unrecognized mode for " 6202e0c549eSJonathan Adams "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) " 6212e0c549eSJonathan Adams "in spa_activate()", 6222e0c549eSJonathan Adams t, q, mode, value); 6232e0c549eSJonathan Adams break; 6242e0c549eSJonathan Adams } 625e14bb325SJeff Bonwick } 626fa9e4066Sahrens } 627fa9e4066Sahrens 628e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 629e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 630e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 631e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 632fa9e4066Sahrens 633fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 634fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 635ea8dc4b6Seschrock 636ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 637ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 638ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 639ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 640ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 641ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 642fa9e4066Sahrens } 643fa9e4066Sahrens 644fa9e4066Sahrens /* 645fa9e4066Sahrens * Opposite of spa_activate(). 646fa9e4066Sahrens */ 647fa9e4066Sahrens static void 648fa9e4066Sahrens spa_deactivate(spa_t *spa) 649fa9e4066Sahrens { 650fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 651fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 652fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 65325f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 654fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 655fa9e4066Sahrens 656fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 657fa9e4066Sahrens 658e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 659e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 660fa9e4066Sahrens 661e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 662e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 663e14bb325SJeff Bonwick taskq_destroy(spa->spa_zio_taskq[t][q]); 664e14bb325SJeff Bonwick spa->spa_zio_taskq[t][q] = NULL; 665e14bb325SJeff Bonwick } 666fa9e4066Sahrens } 667fa9e4066Sahrens 668fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 669fa9e4066Sahrens spa->spa_normal_class = NULL; 670fa9e4066Sahrens 6718654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 6728654d025Sperrin spa->spa_log_class = NULL; 6738654d025Sperrin 674ea8dc4b6Seschrock /* 675ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 676ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 677ea8dc4b6Seschrock */ 678ea8dc4b6Seschrock spa_errlog_drain(spa); 679ea8dc4b6Seschrock 680ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 681ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 682ea8dc4b6Seschrock 683fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 684fa9e4066Sahrens } 685fa9e4066Sahrens 686fa9e4066Sahrens /* 687fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 688fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 689fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 690fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 691fa9e4066Sahrens */ 69299653d4eSeschrock static int 69399653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 69499653d4eSeschrock uint_t id, int atype) 695fa9e4066Sahrens { 696fa9e4066Sahrens nvlist_t **child; 697573ca77eSGeorge Wilson uint_t children; 69899653d4eSeschrock int error; 699fa9e4066Sahrens 70099653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 70199653d4eSeschrock return (error); 702fa9e4066Sahrens 70399653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 70499653d4eSeschrock return (0); 705fa9e4066Sahrens 706e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 707e14bb325SJeff Bonwick &child, &children); 708e14bb325SJeff Bonwick 709e14bb325SJeff Bonwick if (error == ENOENT) 710e14bb325SJeff Bonwick return (0); 711e14bb325SJeff Bonwick 712e14bb325SJeff Bonwick if (error) { 71399653d4eSeschrock vdev_free(*vdp); 71499653d4eSeschrock *vdp = NULL; 71599653d4eSeschrock return (EINVAL); 716fa9e4066Sahrens } 717fa9e4066Sahrens 718573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 71999653d4eSeschrock vdev_t *vd; 72099653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 72199653d4eSeschrock atype)) != 0) { 72299653d4eSeschrock vdev_free(*vdp); 72399653d4eSeschrock *vdp = NULL; 72499653d4eSeschrock return (error); 725fa9e4066Sahrens } 726fa9e4066Sahrens } 727fa9e4066Sahrens 72899653d4eSeschrock ASSERT(*vdp != NULL); 72999653d4eSeschrock 73099653d4eSeschrock return (0); 731fa9e4066Sahrens } 732fa9e4066Sahrens 733fa9e4066Sahrens /* 734fa9e4066Sahrens * Opposite of spa_load(). 735fa9e4066Sahrens */ 736fa9e4066Sahrens static void 737fa9e4066Sahrens spa_unload(spa_t *spa) 738fa9e4066Sahrens { 73999653d4eSeschrock int i; 74099653d4eSeschrock 741e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 742e14bb325SJeff Bonwick 743ea8dc4b6Seschrock /* 744ea8dc4b6Seschrock * Stop async tasks. 745ea8dc4b6Seschrock */ 746ea8dc4b6Seschrock spa_async_suspend(spa); 747ea8dc4b6Seschrock 748fa9e4066Sahrens /* 749fa9e4066Sahrens * Stop syncing. 750fa9e4066Sahrens */ 751fa9e4066Sahrens if (spa->spa_sync_on) { 752fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 753fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 754fa9e4066Sahrens } 755fa9e4066Sahrens 756fa9e4066Sahrens /* 757e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 758fa9e4066Sahrens */ 75954d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 76054d692b7SGeorge Wilson (void) zio_wait(spa->spa_async_zio_root); 76154d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 76254d692b7SGeorge Wilson } 763fa9e4066Sahrens 764fa9e4066Sahrens /* 765fa9e4066Sahrens * Close the dsl pool. 766fa9e4066Sahrens */ 767fa9e4066Sahrens if (spa->spa_dsl_pool) { 768fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 769fa9e4066Sahrens spa->spa_dsl_pool = NULL; 770fa9e4066Sahrens } 771fa9e4066Sahrens 7728ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7738ad4d6ddSJeff Bonwick 7748ad4d6ddSJeff Bonwick /* 7758ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 7768ad4d6ddSJeff Bonwick */ 7778ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 7788ad4d6ddSJeff Bonwick 779fa9e4066Sahrens /* 780fa9e4066Sahrens * Close all vdevs. 781fa9e4066Sahrens */ 7820e34b6a7Sbonwick if (spa->spa_root_vdev) 783fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 7840e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 785ea8dc4b6Seschrock 786fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 787fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 788fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 789fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 790fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 791fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 79299653d4eSeschrock } 793fa94a07fSbrendan if (spa->spa_spares.sav_config) { 794fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 795fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 796fa94a07fSbrendan } 7972ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 798fa94a07fSbrendan 799fa94a07fSbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 800fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 801fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 802fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 803fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 804fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 805fa94a07fSbrendan } 806fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 807fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 808fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 80999653d4eSeschrock } 8102ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 81199653d4eSeschrock 812ea8dc4b6Seschrock spa->spa_async_suspended = 0; 8138ad4d6ddSJeff Bonwick 8148ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 815fa9e4066Sahrens } 816fa9e4066Sahrens 81799653d4eSeschrock /* 81899653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 81999653d4eSeschrock * this pool. When this is called, we have some form of basic information in 820fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 821fa94a07fSbrendan * then re-generate a more complete list including status information. 82299653d4eSeschrock */ 82399653d4eSeschrock static void 82499653d4eSeschrock spa_load_spares(spa_t *spa) 82599653d4eSeschrock { 82699653d4eSeschrock nvlist_t **spares; 82799653d4eSeschrock uint_t nspares; 82899653d4eSeschrock int i; 82939c23413Seschrock vdev_t *vd, *tvd; 83099653d4eSeschrock 831e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 832e14bb325SJeff Bonwick 83399653d4eSeschrock /* 83499653d4eSeschrock * First, close and free any existing spare vdevs. 83599653d4eSeschrock */ 836fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 837fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 83839c23413Seschrock 83939c23413Seschrock /* Undo the call to spa_activate() below */ 840c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 841c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 84239c23413Seschrock spa_spare_remove(tvd); 84339c23413Seschrock vdev_close(vd); 84439c23413Seschrock vdev_free(vd); 84599653d4eSeschrock } 84639c23413Seschrock 847fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 848fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 849fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 85099653d4eSeschrock 851fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 85299653d4eSeschrock nspares = 0; 85399653d4eSeschrock else 854fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 85599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 85699653d4eSeschrock 857fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 858fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 85999653d4eSeschrock 86099653d4eSeschrock if (nspares == 0) 86199653d4eSeschrock return; 86299653d4eSeschrock 86399653d4eSeschrock /* 86499653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 86539c23413Seschrock * process. For each spare, there is potentially two different vdev_t 86639c23413Seschrock * structures associated with it: one in the list of spares (used only 86739c23413Seschrock * for basic validation purposes) and one in the active vdev 86839c23413Seschrock * configuration (if it's spared in). During this phase we open and 86939c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 87039c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 87199653d4eSeschrock */ 872fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 873fa94a07fSbrendan KM_SLEEP); 874fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 87599653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 87699653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 87799653d4eSeschrock ASSERT(vd != NULL); 87899653d4eSeschrock 879fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 88099653d4eSeschrock 881c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 882c5904d13Seschrock B_FALSE)) != NULL) { 88339c23413Seschrock if (!tvd->vdev_isspare) 88439c23413Seschrock spa_spare_add(tvd); 88539c23413Seschrock 88639c23413Seschrock /* 88739c23413Seschrock * We only mark the spare active if we were successfully 88839c23413Seschrock * able to load the vdev. Otherwise, importing a pool 88939c23413Seschrock * with a bad active spare would result in strange 89039c23413Seschrock * behavior, because multiple pool would think the spare 89139c23413Seschrock * is actively in use. 89239c23413Seschrock * 89339c23413Seschrock * There is a vulnerability here to an equally bizarre 89439c23413Seschrock * circumstance, where a dead active spare is later 89539c23413Seschrock * brought back to life (onlined or otherwise). Given 89639c23413Seschrock * the rarity of this scenario, and the extra complexity 89739c23413Seschrock * it adds, we ignore the possibility. 89839c23413Seschrock */ 89939c23413Seschrock if (!vdev_is_dead(tvd)) 90039c23413Seschrock spa_spare_activate(tvd); 90139c23413Seschrock } 90239c23413Seschrock 903e14bb325SJeff Bonwick vd->vdev_top = vd; 9046809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 905e14bb325SJeff Bonwick 90699653d4eSeschrock if (vdev_open(vd) != 0) 90799653d4eSeschrock continue; 90899653d4eSeschrock 909fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 910fa94a07fSbrendan spa_spare_add(vd); 91199653d4eSeschrock } 91299653d4eSeschrock 91399653d4eSeschrock /* 91499653d4eSeschrock * Recompute the stashed list of spares, with status information 91599653d4eSeschrock * this time. 91699653d4eSeschrock */ 917fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 91899653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 91999653d4eSeschrock 920fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 921fa94a07fSbrendan KM_SLEEP); 922fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 923fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 924fa94a07fSbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 925fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 926fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 927fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 92899653d4eSeschrock nvlist_free(spares[i]); 929fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 930fa94a07fSbrendan } 931fa94a07fSbrendan 932fa94a07fSbrendan /* 933fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 934fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 935fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 936fa94a07fSbrendan * then re-generate a more complete list including status information. 937fa94a07fSbrendan * Devices which are already active have their details maintained, and are 938fa94a07fSbrendan * not re-opened. 939fa94a07fSbrendan */ 940fa94a07fSbrendan static void 941fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 942fa94a07fSbrendan { 943fa94a07fSbrendan nvlist_t **l2cache; 944fa94a07fSbrendan uint_t nl2cache; 945fa94a07fSbrendan int i, j, oldnvdevs; 946573ca77eSGeorge Wilson uint64_t guid; 947fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 948fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 949fa94a07fSbrendan 950e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 951e14bb325SJeff Bonwick 952fa94a07fSbrendan if (sav->sav_config != NULL) { 953fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 954fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 955fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 956fa94a07fSbrendan } else { 957fa94a07fSbrendan nl2cache = 0; 958fa94a07fSbrendan } 959fa94a07fSbrendan 960fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 961fa94a07fSbrendan oldnvdevs = sav->sav_count; 962fa94a07fSbrendan sav->sav_vdevs = NULL; 963fa94a07fSbrendan sav->sav_count = 0; 964fa94a07fSbrendan 965fa94a07fSbrendan /* 966fa94a07fSbrendan * Process new nvlist of vdevs. 967fa94a07fSbrendan */ 968fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 969fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 970fa94a07fSbrendan &guid) == 0); 971fa94a07fSbrendan 972fa94a07fSbrendan newvdevs[i] = NULL; 973fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 974fa94a07fSbrendan vd = oldvdevs[j]; 975fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 976fa94a07fSbrendan /* 977fa94a07fSbrendan * Retain previous vdev for add/remove ops. 978fa94a07fSbrendan */ 979fa94a07fSbrendan newvdevs[i] = vd; 980fa94a07fSbrendan oldvdevs[j] = NULL; 981fa94a07fSbrendan break; 982fa94a07fSbrendan } 983fa94a07fSbrendan } 984fa94a07fSbrendan 985fa94a07fSbrendan if (newvdevs[i] == NULL) { 986fa94a07fSbrendan /* 987fa94a07fSbrendan * Create new vdev 988fa94a07fSbrendan */ 989fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 990fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 991fa94a07fSbrendan ASSERT(vd != NULL); 992fa94a07fSbrendan newvdevs[i] = vd; 993fa94a07fSbrendan 994fa94a07fSbrendan /* 995fa94a07fSbrendan * Commit this vdev as an l2cache device, 996fa94a07fSbrendan * even if it fails to open. 997fa94a07fSbrendan */ 998fa94a07fSbrendan spa_l2cache_add(vd); 999fa94a07fSbrendan 1000c5904d13Seschrock vd->vdev_top = vd; 1001c5904d13Seschrock vd->vdev_aux = sav; 1002c5904d13Seschrock 1003c5904d13Seschrock spa_l2cache_activate(vd); 1004c5904d13Seschrock 1005fa94a07fSbrendan if (vdev_open(vd) != 0) 1006fa94a07fSbrendan continue; 1007fa94a07fSbrendan 1008fa94a07fSbrendan (void) vdev_validate_aux(vd); 1009fa94a07fSbrendan 1010573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1011573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1012fa94a07fSbrendan } 1013fa94a07fSbrendan } 1014fa94a07fSbrendan 1015fa94a07fSbrendan /* 1016fa94a07fSbrendan * Purge vdevs that were dropped 1017fa94a07fSbrendan */ 1018fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1019fa94a07fSbrendan uint64_t pool; 1020fa94a07fSbrendan 1021fa94a07fSbrendan vd = oldvdevs[i]; 1022fa94a07fSbrendan if (vd != NULL) { 10238ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 10248ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1025fa94a07fSbrendan l2arc_remove_vdev(vd); 1026fa94a07fSbrendan (void) vdev_close(vd); 1027fa94a07fSbrendan spa_l2cache_remove(vd); 1028fa94a07fSbrendan } 1029fa94a07fSbrendan } 1030fa94a07fSbrendan 1031fa94a07fSbrendan if (oldvdevs) 1032fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1033fa94a07fSbrendan 1034fa94a07fSbrendan if (sav->sav_config == NULL) 1035fa94a07fSbrendan goto out; 1036fa94a07fSbrendan 1037fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1038fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1039fa94a07fSbrendan 1040fa94a07fSbrendan /* 1041fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1042fa94a07fSbrendan * information this time. 1043fa94a07fSbrendan */ 1044fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1045fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1046fa94a07fSbrendan 1047fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1048fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1049fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 1050fa94a07fSbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 1051fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1052fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1053fa94a07fSbrendan out: 1054fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1055fa94a07fSbrendan nvlist_free(l2cache[i]); 1056fa94a07fSbrendan if (sav->sav_count) 1057fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 105899653d4eSeschrock } 105999653d4eSeschrock 106099653d4eSeschrock static int 106199653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 106299653d4eSeschrock { 106399653d4eSeschrock dmu_buf_t *db; 106499653d4eSeschrock char *packed = NULL; 106599653d4eSeschrock size_t nvsize = 0; 106699653d4eSeschrock int error; 106799653d4eSeschrock *value = NULL; 106899653d4eSeschrock 106999653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 107099653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 107199653d4eSeschrock dmu_buf_rele(db, FTAG); 107299653d4eSeschrock 107399653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 10747bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 10757bfdf011SNeil Perrin DMU_READ_PREFETCH); 107699653d4eSeschrock if (error == 0) 107799653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 107899653d4eSeschrock kmem_free(packed, nvsize); 107999653d4eSeschrock 108099653d4eSeschrock return (error); 108199653d4eSeschrock } 108299653d4eSeschrock 10833d7072f8Seschrock /* 10843d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 10853d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 10863d7072f8Seschrock */ 10873d7072f8Seschrock static void 10883d7072f8Seschrock spa_check_removed(vdev_t *vd) 10893d7072f8Seschrock { 1090573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 10913d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 10923d7072f8Seschrock 10933d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 10943d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 10953d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 10963d7072f8Seschrock } 10973d7072f8Seschrock } 10983d7072f8Seschrock 1099e6ca193dSGeorge Wilson /* 1100e6ca193dSGeorge Wilson * Load the slog device state from the config object since it's possible 1101e6ca193dSGeorge Wilson * that the label does not contain the most up-to-date information. 1102e6ca193dSGeorge Wilson */ 1103e6ca193dSGeorge Wilson void 1104e6ca193dSGeorge Wilson spa_load_log_state(spa_t *spa) 1105e6ca193dSGeorge Wilson { 1106e6ca193dSGeorge Wilson nvlist_t *nv, *nvroot, **child; 1107e6ca193dSGeorge Wilson uint64_t is_log; 1108573ca77eSGeorge Wilson uint_t children; 1109e6ca193dSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 1110e6ca193dSGeorge Wilson 1111e6ca193dSGeorge Wilson VERIFY(load_nvlist(spa, spa->spa_config_object, &nv) == 0); 1112e6ca193dSGeorge Wilson VERIFY(nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1113e6ca193dSGeorge Wilson VERIFY(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1114e6ca193dSGeorge Wilson &child, &children) == 0); 1115e6ca193dSGeorge Wilson 1116573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 1117e6ca193dSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 1118e6ca193dSGeorge Wilson 1119e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1120e6ca193dSGeorge Wilson &is_log) == 0 && is_log) 1121e6ca193dSGeorge Wilson vdev_load_log_state(tvd, child[c]); 1122e6ca193dSGeorge Wilson } 1123e6ca193dSGeorge Wilson nvlist_free(nv); 1124e6ca193dSGeorge Wilson } 1125e6ca193dSGeorge Wilson 1126b87f3af3Sperrin /* 1127b87f3af3Sperrin * Check for missing log devices 1128b87f3af3Sperrin */ 1129b87f3af3Sperrin int 1130b87f3af3Sperrin spa_check_logs(spa_t *spa) 1131b87f3af3Sperrin { 1132b87f3af3Sperrin switch (spa->spa_log_state) { 1133b87f3af3Sperrin case SPA_LOG_MISSING: 1134b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1135b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1136b87f3af3Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1137b87f3af3Sperrin DS_FIND_CHILDREN)) { 1138b87f3af3Sperrin spa->spa_log_state = SPA_LOG_MISSING; 1139b87f3af3Sperrin return (1); 1140b87f3af3Sperrin } 1141b87f3af3Sperrin break; 1142b87f3af3Sperrin } 1143b87f3af3Sperrin return (0); 1144b87f3af3Sperrin } 1145b87f3af3Sperrin 1146fa9e4066Sahrens /* 1147fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 1148ea8dc4b6Seschrock * source of configuration information. 1149fa9e4066Sahrens */ 1150fa9e4066Sahrens static int 1151ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 1152fa9e4066Sahrens { 1153fa9e4066Sahrens int error = 0; 1154fa9e4066Sahrens nvlist_t *nvroot = NULL; 1155fa9e4066Sahrens vdev_t *rvd; 1156fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 11570373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1158fa9e4066Sahrens uint64_t pool_guid; 115999653d4eSeschrock uint64_t version; 11603d7072f8Seschrock uint64_t autoreplace = 0; 11618ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 1162b87f3af3Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1163fa9e4066Sahrens 11648ad4d6ddSJeff Bonwick /* 11658ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 11668ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 11678ad4d6ddSJeff Bonwick */ 11688ad4d6ddSJeff Bonwick if (!mosconfig) 11698ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 11708ad4d6ddSJeff Bonwick 1171e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1172e14bb325SJeff Bonwick 1173ea8dc4b6Seschrock spa->spa_load_state = state; 11740373e76bSbonwick 1175fa9e4066Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1176a9926bf0Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 1177ea8dc4b6Seschrock error = EINVAL; 1178ea8dc4b6Seschrock goto out; 1179ea8dc4b6Seschrock } 1180fa9e4066Sahrens 118199653d4eSeschrock /* 118299653d4eSeschrock * Versioning wasn't explicitly added to the label until later, so if 118399653d4eSeschrock * it's not present treat it as the initial version. 118499653d4eSeschrock */ 118599653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 1186e7437265Sahrens version = SPA_VERSION_INITIAL; 118799653d4eSeschrock 1188a9926bf0Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 1189a9926bf0Sbonwick &spa->spa_config_txg); 1190a9926bf0Sbonwick 11910373e76bSbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 1192ea8dc4b6Seschrock spa_guid_exists(pool_guid, 0)) { 1193ea8dc4b6Seschrock error = EEXIST; 1194ea8dc4b6Seschrock goto out; 1195ea8dc4b6Seschrock } 1196fa9e4066Sahrens 1197b5989ec7Seschrock spa->spa_load_guid = pool_guid; 1198b5989ec7Seschrock 119954d692b7SGeorge Wilson /* 120054d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 120154d692b7SGeorge Wilson */ 120225f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 120325f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 120454d692b7SGeorge Wilson 1205fa9e4066Sahrens /* 120699653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 120799653d4eSeschrock * value that will be returned by spa_version() since parsing the 120899653d4eSeschrock * configuration requires knowing the version number. 1209fa9e4066Sahrens */ 1210e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 121199653d4eSeschrock spa->spa_ubsync.ub_version = version; 121299653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 1213e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1214fa9e4066Sahrens 121599653d4eSeschrock if (error != 0) 1216ea8dc4b6Seschrock goto out; 1217fa9e4066Sahrens 12180e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1219fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid); 1220fa9e4066Sahrens 1221fa9e4066Sahrens /* 1222fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 1223fa9e4066Sahrens */ 1224e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12250bf246f5Smc error = vdev_open(rvd); 1226e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 12270bf246f5Smc if (error != 0) 1228ea8dc4b6Seschrock goto out; 1229fa9e4066Sahrens 1230560e6e96Seschrock /* 123177e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 123277e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 123377e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 123477e3a39cSMark J Musante * that config. Otherwise, we're validating against the cached config 123577e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 123677e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 123777e3a39cSMark J Musante * the vdev config. 1238560e6e96Seschrock */ 123977e3a39cSMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 124077e3a39cSMark J Musante error = vdev_validate(rvd); 124177e3a39cSMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 124277e3a39cSMark J Musante if (error != 0) 124377e3a39cSMark J Musante goto out; 1244560e6e96Seschrock 1245560e6e96Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1246560e6e96Seschrock error = ENXIO; 1247560e6e96Seschrock goto out; 1248560e6e96Seschrock } 1249560e6e96Seschrock 1250fa9e4066Sahrens /* 1251fa9e4066Sahrens * Find the best uberblock. 1252fa9e4066Sahrens */ 1253e14bb325SJeff Bonwick vdev_uberblock_load(NULL, rvd, ub); 1254fa9e4066Sahrens 1255fa9e4066Sahrens /* 1256fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 1257fa9e4066Sahrens */ 1258fa9e4066Sahrens if (ub->ub_txg == 0) { 1259eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1260eaca9bbdSeschrock VDEV_AUX_CORRUPT_DATA); 1261ea8dc4b6Seschrock error = ENXIO; 1262ea8dc4b6Seschrock goto out; 1263ea8dc4b6Seschrock } 1264ea8dc4b6Seschrock 1265ea8dc4b6Seschrock /* 1266ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 1267ea8dc4b6Seschrock */ 1268e7437265Sahrens if (ub->ub_version > SPA_VERSION) { 1269eaca9bbdSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1270eaca9bbdSeschrock VDEV_AUX_VERSION_NEWER); 1271ea8dc4b6Seschrock error = ENOTSUP; 1272ea8dc4b6Seschrock goto out; 1273fa9e4066Sahrens } 1274fa9e4066Sahrens 1275fa9e4066Sahrens /* 1276fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1277fa9e4066Sahrens * incomplete configuration. 1278fa9e4066Sahrens */ 1279ecc2d604Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 1280ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1281ea8dc4b6Seschrock VDEV_AUX_BAD_GUID_SUM); 1282ea8dc4b6Seschrock error = ENXIO; 1283ea8dc4b6Seschrock goto out; 1284fa9e4066Sahrens } 1285fa9e4066Sahrens 1286fa9e4066Sahrens /* 1287fa9e4066Sahrens * Initialize internal SPA structures. 1288fa9e4066Sahrens */ 1289fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1290fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1291fa9e4066Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 1292ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 1293ea8dc4b6Seschrock if (error) { 1294ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1295ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1296ea8dc4b6Seschrock goto out; 1297ea8dc4b6Seschrock } 1298fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1299fa9e4066Sahrens 1300ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1301fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1302ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 1303ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1304ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1305ea8dc4b6Seschrock error = EIO; 1306ea8dc4b6Seschrock goto out; 1307ea8dc4b6Seschrock } 1308fa9e4066Sahrens 1309fa9e4066Sahrens if (!mosconfig) { 131099653d4eSeschrock nvlist_t *newconfig; 131195173954Sek uint64_t hostid; 1312fa9e4066Sahrens 131399653d4eSeschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 1314ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1315ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1316ea8dc4b6Seschrock error = EIO; 1317ea8dc4b6Seschrock goto out; 1318ea8dc4b6Seschrock } 1319fa9e4066Sahrens 132077650510SLin Ling if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 132177650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 132295173954Sek char *hostname; 132395173954Sek unsigned long myhostid = 0; 132495173954Sek 132595173954Sek VERIFY(nvlist_lookup_string(newconfig, 132695173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 132795173954Sek 13285679c89fSjv #ifdef _KERNEL 13295679c89fSjv myhostid = zone_get_hostid(NULL); 13305679c89fSjv #else /* _KERNEL */ 13315679c89fSjv /* 13325679c89fSjv * We're emulating the system's hostid in userland, so 13335679c89fSjv * we can't use zone_get_hostid(). 13345679c89fSjv */ 133595173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 13365679c89fSjv #endif /* _KERNEL */ 133717194a52Slling if (hostid != 0 && myhostid != 0 && 13385679c89fSjv hostid != myhostid) { 133995173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 134095173954Sek "loaded as it was last accessed by " 134177650510SLin Ling "another system (host: %s hostid: 0x%lx). " 134295173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 1343e14bb325SJeff Bonwick spa_name(spa), hostname, 134495173954Sek (unsigned long)hostid); 134595173954Sek error = EBADF; 134695173954Sek goto out; 134795173954Sek } 134895173954Sek } 134995173954Sek 1350fa9e4066Sahrens spa_config_set(spa, newconfig); 1351fa9e4066Sahrens spa_unload(spa); 1352fa9e4066Sahrens spa_deactivate(spa); 13538ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 1354fa9e4066Sahrens 1355ea8dc4b6Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 1356fa9e4066Sahrens } 1357fa9e4066Sahrens 1358ea8dc4b6Seschrock if (zap_lookup(spa->spa_meta_objset, 1359fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1360ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 1361ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1362ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1363ea8dc4b6Seschrock error = EIO; 1364ea8dc4b6Seschrock goto out; 1365ea8dc4b6Seschrock } 1366fa9e4066Sahrens 136799653d4eSeschrock /* 136899653d4eSeschrock * Load the bit that tells us to use the new accounting function 136999653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 137099653d4eSeschrock * be present. 137199653d4eSeschrock */ 137299653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, 137399653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 137499653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate); 137599653d4eSeschrock if (error != 0 && error != ENOENT) { 137699653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 137799653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 137899653d4eSeschrock error = EIO; 137999653d4eSeschrock goto out; 138099653d4eSeschrock } 138199653d4eSeschrock 1382fa9e4066Sahrens /* 1383ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1384ea8dc4b6Seschrock * not be present. 1385fa9e4066Sahrens */ 1386ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1387ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 1388ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 1389d80c45e0Sbonwick if (error != 0 && error != ENOENT) { 1390ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1391ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1392ea8dc4b6Seschrock error = EIO; 1393ea8dc4b6Seschrock goto out; 1394ea8dc4b6Seschrock } 1395ea8dc4b6Seschrock 1396ea8dc4b6Seschrock error = zap_lookup(spa->spa_meta_objset, 1397ea8dc4b6Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1398ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1399ea8dc4b6Seschrock if (error != 0 && error != ENOENT) { 1400ea8dc4b6Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1401ea8dc4b6Seschrock VDEV_AUX_CORRUPT_DATA); 1402ea8dc4b6Seschrock error = EIO; 1403ea8dc4b6Seschrock goto out; 1404ea8dc4b6Seschrock } 1405ea8dc4b6Seschrock 140606eeb2adSek /* 140706eeb2adSek * Load the history object. If we have an older pool, this 140806eeb2adSek * will not be present. 140906eeb2adSek */ 141006eeb2adSek error = zap_lookup(spa->spa_meta_objset, 141106eeb2adSek DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 141206eeb2adSek sizeof (uint64_t), 1, &spa->spa_history); 141306eeb2adSek if (error != 0 && error != ENOENT) { 141406eeb2adSek vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 141506eeb2adSek VDEV_AUX_CORRUPT_DATA); 141606eeb2adSek error = EIO; 141706eeb2adSek goto out; 141806eeb2adSek } 141906eeb2adSek 142099653d4eSeschrock /* 142199653d4eSeschrock * Load any hot spares for this pool. 142299653d4eSeschrock */ 142399653d4eSeschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1424fa94a07fSbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 142599653d4eSeschrock if (error != 0 && error != ENOENT) { 142699653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 142799653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 142899653d4eSeschrock error = EIO; 142999653d4eSeschrock goto out; 143099653d4eSeschrock } 143199653d4eSeschrock if (error == 0) { 1432e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 1433fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 1434fa94a07fSbrendan &spa->spa_spares.sav_config) != 0) { 143599653d4eSeschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 143699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 143799653d4eSeschrock error = EIO; 143899653d4eSeschrock goto out; 143999653d4eSeschrock } 144099653d4eSeschrock 1441e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 144299653d4eSeschrock spa_load_spares(spa); 1443e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 144499653d4eSeschrock } 144599653d4eSeschrock 1446fa94a07fSbrendan /* 1447fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 1448fa94a07fSbrendan */ 1449fa94a07fSbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1450fa94a07fSbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 1451fa94a07fSbrendan &spa->spa_l2cache.sav_object); 1452fa94a07fSbrendan if (error != 0 && error != ENOENT) { 1453fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1454fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1455fa94a07fSbrendan error = EIO; 1456fa94a07fSbrendan goto out; 1457fa94a07fSbrendan } 1458fa94a07fSbrendan if (error == 0) { 1459fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 1460fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 1461fa94a07fSbrendan &spa->spa_l2cache.sav_config) != 0) { 1462fa94a07fSbrendan vdev_set_state(rvd, B_TRUE, 1463fa94a07fSbrendan VDEV_STATE_CANT_OPEN, 1464fa94a07fSbrendan VDEV_AUX_CORRUPT_DATA); 1465fa94a07fSbrendan error = EIO; 1466fa94a07fSbrendan goto out; 1467fa94a07fSbrendan } 1468fa94a07fSbrendan 1469e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1470fa94a07fSbrendan spa_load_l2cache(spa); 1471e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1472fa94a07fSbrendan } 1473fa94a07fSbrendan 1474e6ca193dSGeorge Wilson spa_load_log_state(spa); 1475e6ca193dSGeorge Wilson 1476b87f3af3Sperrin if (spa_check_logs(spa)) { 1477b87f3af3Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1478b87f3af3Sperrin VDEV_AUX_BAD_LOG); 1479b87f3af3Sperrin error = ENXIO; 1480b87f3af3Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 1481b87f3af3Sperrin goto out; 1482b87f3af3Sperrin } 1483b87f3af3Sperrin 1484b87f3af3Sperrin 1485990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1486ecd6cf80Smarks 1487b1b8ab34Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1488b1b8ab34Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1489b1b8ab34Slling 1490b1b8ab34Slling if (error && error != ENOENT) { 1491b1b8ab34Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1492b1b8ab34Slling VDEV_AUX_CORRUPT_DATA); 1493b1b8ab34Slling error = EIO; 1494b1b8ab34Slling goto out; 1495b1b8ab34Slling } 1496b1b8ab34Slling 1497b1b8ab34Slling if (error == 0) { 1498b1b8ab34Slling (void) zap_lookup(spa->spa_meta_objset, 1499b1b8ab34Slling spa->spa_pool_props_object, 15003d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1501b1b8ab34Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 15023d7072f8Seschrock (void) zap_lookup(spa->spa_meta_objset, 15033d7072f8Seschrock spa->spa_pool_props_object, 15043d7072f8Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 15053d7072f8Seschrock sizeof (uint64_t), 1, &autoreplace); 1506ecd6cf80Smarks (void) zap_lookup(spa->spa_meta_objset, 1507ecd6cf80Smarks spa->spa_pool_props_object, 1508ecd6cf80Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1509ecd6cf80Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 15100a4e9518Sgw (void) zap_lookup(spa->spa_meta_objset, 15110a4e9518Sgw spa->spa_pool_props_object, 15120a4e9518Sgw zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 15130a4e9518Sgw sizeof (uint64_t), 1, &spa->spa_failmode); 1514573ca77eSGeorge Wilson (void) zap_lookup(spa->spa_meta_objset, 1515573ca77eSGeorge Wilson spa->spa_pool_props_object, 1516573ca77eSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND), 1517573ca77eSGeorge Wilson sizeof (uint64_t), 1, &spa->spa_autoexpand); 1518b1b8ab34Slling } 1519b1b8ab34Slling 15203d7072f8Seschrock /* 15213d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 15223d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 15233d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 15243d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 15253d7072f8Seschrock * over. 15263d7072f8Seschrock */ 1527b01c3b58Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 15283d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 15293d7072f8Seschrock 1530ea8dc4b6Seschrock /* 1531560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1532ea8dc4b6Seschrock */ 1533560e6e96Seschrock vdev_load(rvd); 15340373e76bSbonwick 1535fa9e4066Sahrens /* 1536fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1537fa9e4066Sahrens */ 1538e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1539fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1540e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1541fa9e4066Sahrens 1542fa9e4066Sahrens /* 1543fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1544fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1545fa9e4066Sahrens */ 1546ea8dc4b6Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1547ea8dc4b6Seschrock error = ENXIO; 1548ea8dc4b6Seschrock goto out; 1549ea8dc4b6Seschrock } 1550fa9e4066Sahrens 15518ad4d6ddSJeff Bonwick if (spa_writeable(spa)) { 15525dabedeeSbonwick dmu_tx_t *tx; 15530373e76bSbonwick int need_update = B_FALSE; 15548ad4d6ddSJeff Bonwick 15558ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 15565dabedeeSbonwick 15570373e76bSbonwick /* 15580373e76bSbonwick * Claim log blocks that haven't been committed yet. 15590373e76bSbonwick * This must all happen in a single txg. 15600373e76bSbonwick */ 15615dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1562fa9e4066Sahrens spa_first_txg(spa)); 1563e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 15640b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1565fa9e4066Sahrens dmu_tx_commit(tx); 1566fa9e4066Sahrens 1567e6ca193dSGeorge Wilson spa->spa_log_state = SPA_LOG_GOOD; 1568fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 1569fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 1570fa9e4066Sahrens 1571fa9e4066Sahrens /* 1572fa9e4066Sahrens * Wait for all claims to sync. 1573fa9e4066Sahrens */ 1574fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 15750e34b6a7Sbonwick 15760e34b6a7Sbonwick /* 15770373e76bSbonwick * If the config cache is stale, or we have uninitialized 15780373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 1579bc758434SLin Ling * 1580bc758434SLin Ling * If spa_load_verbatim is true, trust the current 1581bc758434SLin Ling * in-core spa_config and update the disk labels. 15820e34b6a7Sbonwick */ 15830373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 1584bc758434SLin Ling state == SPA_LOAD_IMPORT || spa->spa_load_verbatim) 15850373e76bSbonwick need_update = B_TRUE; 15860373e76bSbonwick 15878ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 15880373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 15890373e76bSbonwick need_update = B_TRUE; 15900e34b6a7Sbonwick 15910e34b6a7Sbonwick /* 15920373e76bSbonwick * Update the config cache asychronously in case we're the 15930373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 15940e34b6a7Sbonwick */ 15950373e76bSbonwick if (need_update) 15960373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 15978ad4d6ddSJeff Bonwick 15988ad4d6ddSJeff Bonwick /* 15998ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 16008ad4d6ddSJeff Bonwick */ 16018ad4d6ddSJeff Bonwick if (vdev_resilver_needed(rvd, NULL, NULL)) 16028ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1603*503ad85cSMatthew Ahrens 1604*503ad85cSMatthew Ahrens /* 1605*503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 1606*503ad85cSMatthew Ahrens */ 1607*503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 1608*503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 1609fa9e4066Sahrens } 1610fa9e4066Sahrens 1611ea8dc4b6Seschrock error = 0; 1612ea8dc4b6Seschrock out: 1613088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 161499653d4eSeschrock if (error && error != EBADF) 1615b87f3af3Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 1616ea8dc4b6Seschrock spa->spa_load_state = SPA_LOAD_NONE; 1617ea8dc4b6Seschrock spa->spa_ena = 0; 1618ea8dc4b6Seschrock 1619ea8dc4b6Seschrock return (error); 1620fa9e4066Sahrens } 1621fa9e4066Sahrens 1622fa9e4066Sahrens /* 1623fa9e4066Sahrens * Pool Open/Import 1624fa9e4066Sahrens * 1625fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 1626fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1627fa9e4066Sahrens * case of an open, the pool configuration will exist in the 16283d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 1629fa9e4066Sahrens * 1630fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1631fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 1632fa9e4066Sahrens * ambiguous state. 1633fa9e4066Sahrens */ 1634fa9e4066Sahrens static int 1635fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1636fa9e4066Sahrens { 1637fa9e4066Sahrens spa_t *spa; 1638fa9e4066Sahrens int error; 1639fa9e4066Sahrens int locked = B_FALSE; 1640fa9e4066Sahrens 1641fa9e4066Sahrens *spapp = NULL; 1642fa9e4066Sahrens 1643fa9e4066Sahrens /* 1644fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 1645fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1646fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 1647fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 1648fa9e4066Sahrens */ 1649fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1650fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1651fa9e4066Sahrens locked = B_TRUE; 1652fa9e4066Sahrens } 1653fa9e4066Sahrens 1654fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1655fa9e4066Sahrens if (locked) 1656fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1657fa9e4066Sahrens return (ENOENT); 1658fa9e4066Sahrens } 1659fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1660fa9e4066Sahrens 16618ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 1662fa9e4066Sahrens 16630373e76bSbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1664fa9e4066Sahrens 1665fa9e4066Sahrens if (error == EBADF) { 1666fa9e4066Sahrens /* 1667560e6e96Seschrock * If vdev_validate() returns failure (indicated by 1668560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 1669560e6e96Seschrock * that the pool has been exported or destroyed. If 1670560e6e96Seschrock * this is the case, the config cache is out of sync and 1671560e6e96Seschrock * we should remove the pool from the namespace. 1672fa9e4066Sahrens */ 1673fa9e4066Sahrens spa_unload(spa); 1674fa9e4066Sahrens spa_deactivate(spa); 1675c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1676fa9e4066Sahrens spa_remove(spa); 1677fa9e4066Sahrens if (locked) 1678fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1679fa9e4066Sahrens return (ENOENT); 1680ea8dc4b6Seschrock } 1681ea8dc4b6Seschrock 1682ea8dc4b6Seschrock if (error) { 1683fa9e4066Sahrens /* 1684fa9e4066Sahrens * We can't open the pool, but we still have useful 1685fa9e4066Sahrens * information: the state of each vdev after the 1686fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 1687fa9e4066Sahrens */ 1688e14bb325SJeff Bonwick if (config != NULL && spa->spa_root_vdev != NULL) 1689fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1690fa9e4066Sahrens B_TRUE); 1691fa9e4066Sahrens spa_unload(spa); 1692fa9e4066Sahrens spa_deactivate(spa); 1693ea8dc4b6Seschrock spa->spa_last_open_failed = B_TRUE; 1694fa9e4066Sahrens if (locked) 1695fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1696fa9e4066Sahrens *spapp = NULL; 1697fa9e4066Sahrens return (error); 1698ea8dc4b6Seschrock } else { 1699ea8dc4b6Seschrock spa->spa_last_open_failed = B_FALSE; 1700fa9e4066Sahrens } 1701fa9e4066Sahrens } 1702fa9e4066Sahrens 1703fa9e4066Sahrens spa_open_ref(spa, tag); 17043d7072f8Seschrock 1705fa9e4066Sahrens if (locked) 1706fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1707fa9e4066Sahrens 1708fa9e4066Sahrens *spapp = spa; 1709fa9e4066Sahrens 1710e14bb325SJeff Bonwick if (config != NULL) 1711fa9e4066Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1712fa9e4066Sahrens 1713fa9e4066Sahrens return (0); 1714fa9e4066Sahrens } 1715fa9e4066Sahrens 1716fa9e4066Sahrens int 1717fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1718fa9e4066Sahrens { 1719fa9e4066Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1720fa9e4066Sahrens } 1721fa9e4066Sahrens 1722ea8dc4b6Seschrock /* 1723ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 1724ea8dc4b6Seschrock * preventing it from being exported or destroyed. 1725ea8dc4b6Seschrock */ 1726ea8dc4b6Seschrock spa_t * 1727ea8dc4b6Seschrock spa_inject_addref(char *name) 1728ea8dc4b6Seschrock { 1729ea8dc4b6Seschrock spa_t *spa; 1730ea8dc4b6Seschrock 1731ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1732ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 1733ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1734ea8dc4b6Seschrock return (NULL); 1735ea8dc4b6Seschrock } 1736ea8dc4b6Seschrock spa->spa_inject_ref++; 1737ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1738ea8dc4b6Seschrock 1739ea8dc4b6Seschrock return (spa); 1740ea8dc4b6Seschrock } 1741ea8dc4b6Seschrock 1742ea8dc4b6Seschrock void 1743ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 1744ea8dc4b6Seschrock { 1745ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1746ea8dc4b6Seschrock spa->spa_inject_ref--; 1747ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1748ea8dc4b6Seschrock } 1749ea8dc4b6Seschrock 1750fa94a07fSbrendan /* 1751fa94a07fSbrendan * Add spares device information to the nvlist. 1752fa94a07fSbrendan */ 175399653d4eSeschrock static void 175499653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 175599653d4eSeschrock { 175699653d4eSeschrock nvlist_t **spares; 175799653d4eSeschrock uint_t i, nspares; 175899653d4eSeschrock nvlist_t *nvroot; 175999653d4eSeschrock uint64_t guid; 176099653d4eSeschrock vdev_stat_t *vs; 176199653d4eSeschrock uint_t vsc; 176239c23413Seschrock uint64_t pool; 176399653d4eSeschrock 17646809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 17656809eb4eSEric Schrock 1766fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 176799653d4eSeschrock return; 176899653d4eSeschrock 176999653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 177099653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1771fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 177299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 177399653d4eSeschrock if (nspares != 0) { 177499653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 177599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 177699653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 177799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 177899653d4eSeschrock 177999653d4eSeschrock /* 178099653d4eSeschrock * Go through and find any spares which have since been 178199653d4eSeschrock * repurposed as an active spare. If this is the case, update 178299653d4eSeschrock * their status appropriately. 178399653d4eSeschrock */ 178499653d4eSeschrock for (i = 0; i < nspares; i++) { 178599653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 178699653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 178789a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 178889a89ebfSlling pool != 0ULL) { 178999653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 179099653d4eSeschrock spares[i], ZPOOL_CONFIG_STATS, 179199653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 179299653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 179399653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 179499653d4eSeschrock } 179599653d4eSeschrock } 179699653d4eSeschrock } 179799653d4eSeschrock } 179899653d4eSeschrock 1799fa94a07fSbrendan /* 1800fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 1801fa94a07fSbrendan */ 1802fa94a07fSbrendan static void 1803fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 1804fa94a07fSbrendan { 1805fa94a07fSbrendan nvlist_t **l2cache; 1806fa94a07fSbrendan uint_t i, j, nl2cache; 1807fa94a07fSbrendan nvlist_t *nvroot; 1808fa94a07fSbrendan uint64_t guid; 1809fa94a07fSbrendan vdev_t *vd; 1810fa94a07fSbrendan vdev_stat_t *vs; 1811fa94a07fSbrendan uint_t vsc; 1812fa94a07fSbrendan 18136809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 18146809eb4eSEric Schrock 1815fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 1816fa94a07fSbrendan return; 1817fa94a07fSbrendan 1818fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 1819fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1820fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 1821fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1822fa94a07fSbrendan if (nl2cache != 0) { 1823fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 1824fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 1825fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 1826fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1827fa94a07fSbrendan 1828fa94a07fSbrendan /* 1829fa94a07fSbrendan * Update level 2 cache device stats. 1830fa94a07fSbrendan */ 1831fa94a07fSbrendan 1832fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1833fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 1834fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 1835fa94a07fSbrendan 1836fa94a07fSbrendan vd = NULL; 1837fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 1838fa94a07fSbrendan if (guid == 1839fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 1840fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 1841fa94a07fSbrendan break; 1842fa94a07fSbrendan } 1843fa94a07fSbrendan } 1844fa94a07fSbrendan ASSERT(vd != NULL); 1845fa94a07fSbrendan 1846fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 1847fa94a07fSbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 1848fa94a07fSbrendan vdev_get_stats(vd, vs); 1849fa94a07fSbrendan } 1850fa94a07fSbrendan } 1851fa94a07fSbrendan } 1852fa94a07fSbrendan 1853fa9e4066Sahrens int 1854ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1855fa9e4066Sahrens { 1856fa9e4066Sahrens int error; 1857fa9e4066Sahrens spa_t *spa; 1858fa9e4066Sahrens 1859fa9e4066Sahrens *config = NULL; 1860fa9e4066Sahrens error = spa_open_common(name, &spa, FTAG, config); 1861fa9e4066Sahrens 18626809eb4eSEric Schrock if (spa != NULL) { 18636809eb4eSEric Schrock /* 18646809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 18656809eb4eSEric Schrock * or l2cache devices could change and the config would be 18666809eb4eSEric Schrock * self-inconsistent. 18676809eb4eSEric Schrock */ 18686809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 1869ea8dc4b6Seschrock 18706809eb4eSEric Schrock if (*config != NULL) { 1871e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 18726809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 18736809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 1874e14bb325SJeff Bonwick 18756809eb4eSEric Schrock if (spa_suspended(spa)) 18766809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 18776809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 18786809eb4eSEric Schrock spa->spa_failmode) == 0); 18796809eb4eSEric Schrock 18806809eb4eSEric Schrock spa_add_spares(spa, *config); 18816809eb4eSEric Schrock spa_add_l2cache(spa, *config); 18826809eb4eSEric Schrock } 188399653d4eSeschrock } 188499653d4eSeschrock 1885ea8dc4b6Seschrock /* 1886ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 1887ea8dc4b6Seschrock * and call spa_lookup() directly. 1888ea8dc4b6Seschrock */ 1889ea8dc4b6Seschrock if (altroot) { 1890ea8dc4b6Seschrock if (spa == NULL) { 1891ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 1892ea8dc4b6Seschrock spa = spa_lookup(name); 1893ea8dc4b6Seschrock if (spa) 1894ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1895ea8dc4b6Seschrock else 1896ea8dc4b6Seschrock altroot[0] = '\0'; 1897ea8dc4b6Seschrock spa = NULL; 1898ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1899ea8dc4b6Seschrock } else { 1900ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 1901ea8dc4b6Seschrock } 1902ea8dc4b6Seschrock } 1903ea8dc4b6Seschrock 19046809eb4eSEric Schrock if (spa != NULL) { 19056809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 1906fa9e4066Sahrens spa_close(spa, FTAG); 19076809eb4eSEric Schrock } 1908fa9e4066Sahrens 1909fa9e4066Sahrens return (error); 1910fa9e4066Sahrens } 1911fa9e4066Sahrens 191299653d4eSeschrock /* 1913fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 1914fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 1915fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 1916fa94a07fSbrendan * specified, as long as they are well-formed. 191799653d4eSeschrock */ 191899653d4eSeschrock static int 1919fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 1920fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 1921fa94a07fSbrendan vdev_labeltype_t label) 192299653d4eSeschrock { 1923fa94a07fSbrendan nvlist_t **dev; 1924fa94a07fSbrendan uint_t i, ndev; 192599653d4eSeschrock vdev_t *vd; 192699653d4eSeschrock int error; 192799653d4eSeschrock 1928e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1929e14bb325SJeff Bonwick 193099653d4eSeschrock /* 1931fa94a07fSbrendan * It's acceptable to have no devs specified. 193299653d4eSeschrock */ 1933fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 193499653d4eSeschrock return (0); 193599653d4eSeschrock 1936fa94a07fSbrendan if (ndev == 0) 193799653d4eSeschrock return (EINVAL); 193899653d4eSeschrock 193999653d4eSeschrock /* 1940fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 1941fa94a07fSbrendan * device type. 194299653d4eSeschrock */ 1943fa94a07fSbrendan if (spa_version(spa) < version) 194499653d4eSeschrock return (ENOTSUP); 194599653d4eSeschrock 194639c23413Seschrock /* 1947fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 194839c23413Seschrock * checking. 194939c23413Seschrock */ 1950fa94a07fSbrendan sav->sav_pending = dev; 1951fa94a07fSbrendan sav->sav_npending = ndev; 195239c23413Seschrock 1953fa94a07fSbrendan for (i = 0; i < ndev; i++) { 1954fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 195599653d4eSeschrock mode)) != 0) 195639c23413Seschrock goto out; 195799653d4eSeschrock 195899653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 195999653d4eSeschrock vdev_free(vd); 196039c23413Seschrock error = EINVAL; 196139c23413Seschrock goto out; 196299653d4eSeschrock } 196399653d4eSeschrock 1964fa94a07fSbrendan /* 1965e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 1966e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 1967fa94a07fSbrendan */ 1968e14bb325SJeff Bonwick #ifdef _KERNEL 1969fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 1970fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 1971fa94a07fSbrendan error = ENOTBLK; 1972fa94a07fSbrendan goto out; 1973fa94a07fSbrendan } 1974e14bb325SJeff Bonwick #endif 197599653d4eSeschrock vd->vdev_top = vd; 197699653d4eSeschrock 197739c23413Seschrock if ((error = vdev_open(vd)) == 0 && 1978fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 1979fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 198039c23413Seschrock vd->vdev_guid) == 0); 198139c23413Seschrock } 198299653d4eSeschrock 198399653d4eSeschrock vdev_free(vd); 198439c23413Seschrock 1985fa94a07fSbrendan if (error && 1986fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 198739c23413Seschrock goto out; 198839c23413Seschrock else 198939c23413Seschrock error = 0; 199099653d4eSeschrock } 199199653d4eSeschrock 199239c23413Seschrock out: 1993fa94a07fSbrendan sav->sav_pending = NULL; 1994fa94a07fSbrendan sav->sav_npending = 0; 199539c23413Seschrock return (error); 199699653d4eSeschrock } 199799653d4eSeschrock 1998fa94a07fSbrendan static int 1999fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 2000fa94a07fSbrendan { 2001fa94a07fSbrendan int error; 2002fa94a07fSbrendan 2003e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2004e14bb325SJeff Bonwick 2005fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2006fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 2007fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 2008fa94a07fSbrendan return (error); 2009fa94a07fSbrendan } 2010fa94a07fSbrendan 2011fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2012fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 2013fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 2014fa94a07fSbrendan } 2015fa94a07fSbrendan 2016fa94a07fSbrendan static void 2017fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 2018fa94a07fSbrendan const char *config) 2019fa94a07fSbrendan { 2020fa94a07fSbrendan int i; 2021fa94a07fSbrendan 2022fa94a07fSbrendan if (sav->sav_config != NULL) { 2023fa94a07fSbrendan nvlist_t **olddevs; 2024fa94a07fSbrendan uint_t oldndevs; 2025fa94a07fSbrendan nvlist_t **newdevs; 2026fa94a07fSbrendan 2027fa94a07fSbrendan /* 2028fa94a07fSbrendan * Generate new dev list by concatentating with the 2029fa94a07fSbrendan * current dev list. 2030fa94a07fSbrendan */ 2031fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 2032fa94a07fSbrendan &olddevs, &oldndevs) == 0); 2033fa94a07fSbrendan 2034fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 2035fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 2036fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 2037fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 2038fa94a07fSbrendan KM_SLEEP) == 0); 2039fa94a07fSbrendan for (i = 0; i < ndevs; i++) 2040fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 2041fa94a07fSbrendan KM_SLEEP) == 0); 2042fa94a07fSbrendan 2043fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 2044fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 2045fa94a07fSbrendan 2046fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 2047fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 2048fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 2049fa94a07fSbrendan nvlist_free(newdevs[i]); 2050fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 2051fa94a07fSbrendan } else { 2052fa94a07fSbrendan /* 2053fa94a07fSbrendan * Generate a new dev list. 2054fa94a07fSbrendan */ 2055fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 2056fa94a07fSbrendan KM_SLEEP) == 0); 2057fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 2058fa94a07fSbrendan devs, ndevs) == 0); 2059fa94a07fSbrendan } 2060fa94a07fSbrendan } 2061fa94a07fSbrendan 2062fa94a07fSbrendan /* 2063fa94a07fSbrendan * Stop and drop level 2 ARC devices 2064fa94a07fSbrendan */ 2065fa94a07fSbrendan void 2066fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 2067fa94a07fSbrendan { 2068fa94a07fSbrendan vdev_t *vd; 2069fa94a07fSbrendan int i; 2070fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 2071fa94a07fSbrendan 2072fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 2073fa94a07fSbrendan uint64_t pool; 2074fa94a07fSbrendan 2075fa94a07fSbrendan vd = sav->sav_vdevs[i]; 2076fa94a07fSbrendan ASSERT(vd != NULL); 2077fa94a07fSbrendan 20788ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 20798ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 2080fa94a07fSbrendan l2arc_remove_vdev(vd); 2081fa94a07fSbrendan if (vd->vdev_isl2cache) 2082fa94a07fSbrendan spa_l2cache_remove(vd); 2083fa94a07fSbrendan vdev_clear_stats(vd); 2084fa94a07fSbrendan (void) vdev_close(vd); 2085fa94a07fSbrendan } 2086fa94a07fSbrendan } 2087fa94a07fSbrendan 2088fa9e4066Sahrens /* 2089fa9e4066Sahrens * Pool Creation 2090fa9e4066Sahrens */ 2091fa9e4066Sahrens int 2092990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 20930a48a24eStimh const char *history_str, nvlist_t *zplprops) 2094fa9e4066Sahrens { 2095fa9e4066Sahrens spa_t *spa; 2096990b4856Slling char *altroot = NULL; 20970373e76bSbonwick vdev_t *rvd; 2098fa9e4066Sahrens dsl_pool_t *dp; 2099fa9e4066Sahrens dmu_tx_t *tx; 2100573ca77eSGeorge Wilson int error = 0; 2101fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 2102fa94a07fSbrendan nvlist_t **spares, **l2cache; 2103fa94a07fSbrendan uint_t nspares, nl2cache; 2104990b4856Slling uint64_t version; 2105fa9e4066Sahrens 2106fa9e4066Sahrens /* 2107fa9e4066Sahrens * If this pool already exists, return failure. 2108fa9e4066Sahrens */ 2109fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2110fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 2111fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2112fa9e4066Sahrens return (EEXIST); 2113fa9e4066Sahrens } 2114fa9e4066Sahrens 2115fa9e4066Sahrens /* 2116fa9e4066Sahrens * Allocate a new spa_t structure. 2117fa9e4066Sahrens */ 2118990b4856Slling (void) nvlist_lookup_string(props, 2119990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 21200373e76bSbonwick spa = spa_add(pool, altroot); 21218ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2122fa9e4066Sahrens 2123fa9e4066Sahrens spa->spa_uberblock.ub_txg = txg - 1; 2124990b4856Slling 2125990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 2126990b4856Slling spa_deactivate(spa); 2127990b4856Slling spa_remove(spa); 2128c5904d13Seschrock mutex_exit(&spa_namespace_lock); 2129990b4856Slling return (error); 2130990b4856Slling } 2131990b4856Slling 2132990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 2133990b4856Slling &version) != 0) 2134990b4856Slling version = SPA_VERSION; 2135990b4856Slling ASSERT(version <= SPA_VERSION); 2136990b4856Slling spa->spa_uberblock.ub_version = version; 2137fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2138fa9e4066Sahrens 213954d692b7SGeorge Wilson /* 214054d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 214154d692b7SGeorge Wilson */ 214225f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 214325f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 214454d692b7SGeorge Wilson 21450373e76bSbonwick /* 21460373e76bSbonwick * Create the root vdev. 21470373e76bSbonwick */ 2148e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21490373e76bSbonwick 215099653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 21510373e76bSbonwick 215299653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 215399653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 21540373e76bSbonwick 2155b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 21560373e76bSbonwick error = EINVAL; 215799653d4eSeschrock 215899653d4eSeschrock if (error == 0 && 215999653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 2160fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 216199653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 2162573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 2163573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 2164573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 2165573ca77eSGeorge Wilson } 21660373e76bSbonwick } 21670373e76bSbonwick 2168e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2169fa9e4066Sahrens 217099653d4eSeschrock if (error != 0) { 2171fa9e4066Sahrens spa_unload(spa); 2172fa9e4066Sahrens spa_deactivate(spa); 2173fa9e4066Sahrens spa_remove(spa); 2174fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2175fa9e4066Sahrens return (error); 2176fa9e4066Sahrens } 2177fa9e4066Sahrens 217899653d4eSeschrock /* 217999653d4eSeschrock * Get the list of spares, if specified. 218099653d4eSeschrock */ 218199653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 218299653d4eSeschrock &spares, &nspares) == 0) { 2183fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 218499653d4eSeschrock KM_SLEEP) == 0); 2185fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 218699653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2187e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 218899653d4eSeschrock spa_load_spares(spa); 2189e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2190fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2191fa94a07fSbrendan } 2192fa94a07fSbrendan 2193fa94a07fSbrendan /* 2194fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 2195fa94a07fSbrendan */ 2196fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2197fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 2198fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2199fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 2200fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2201fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2202e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2203fa94a07fSbrendan spa_load_l2cache(spa); 2204e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2205fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 220699653d4eSeschrock } 220799653d4eSeschrock 22080a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2209fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2210fa9e4066Sahrens 2211fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 2212fa9e4066Sahrens 2213fa9e4066Sahrens /* 2214fa9e4066Sahrens * Create the pool config object. 2215fa9e4066Sahrens */ 2216fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 2217f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2218fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2219fa9e4066Sahrens 2220ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2221fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 2222ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 2223ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 2224ea8dc4b6Seschrock } 2225fa9e4066Sahrens 2226990b4856Slling /* Newly created pools with the right version are always deflated. */ 2227990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 2228990b4856Slling spa->spa_deflate = TRUE; 2229990b4856Slling if (zap_add(spa->spa_meta_objset, 2230990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 2231990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 2232990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 2233990b4856Slling } 223499653d4eSeschrock } 223599653d4eSeschrock 2236fa9e4066Sahrens /* 2237fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 2238fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 2239fa9e4066Sahrens * keeps changing. 2240fa9e4066Sahrens */ 2241fa9e4066Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2242fa9e4066Sahrens 1 << 14, tx); 2243fa9e4066Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2244fa9e4066Sahrens ZIO_COMPRESS_OFF, tx); 2245fa9e4066Sahrens 2246ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2247fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 2248ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 2249ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 2250ea8dc4b6Seschrock } 2251fa9e4066Sahrens 225206eeb2adSek /* 225306eeb2adSek * Create the pool's history object. 225406eeb2adSek */ 2255990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 2256990b4856Slling spa_history_create_obj(spa, tx); 2257990b4856Slling 2258990b4856Slling /* 2259990b4856Slling * Set pool properties. 2260990b4856Slling */ 2261990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 2262990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 22630a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 2264573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 2265379c004dSEric Schrock if (props != NULL) { 2266379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 2267990b4856Slling spa_sync_props(spa, props, CRED(), tx); 2268379c004dSEric Schrock } 226906eeb2adSek 2270fa9e4066Sahrens dmu_tx_commit(tx); 2271fa9e4066Sahrens 2272fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2273fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2274fa9e4066Sahrens 2275fa9e4066Sahrens /* 2276fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 2277fa9e4066Sahrens * bean counters are appropriately updated. 2278fa9e4066Sahrens */ 2279fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2280fa9e4066Sahrens 2281c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2282fa9e4066Sahrens 2283990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 2284228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 2285c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_CREATE); 2286228975ccSek 2287088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 2288088f3894Sahrens 2289daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 2290daaa36a7SGeorge Wilson 2291fa9e4066Sahrens return (0); 2292fa9e4066Sahrens } 2293fa9e4066Sahrens 2294e7cbe64fSgw #ifdef _KERNEL 2295e7cbe64fSgw /* 229621ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 229721ecdf64SLin Ling * during the system boot up time. 2298e7cbe64fSgw */ 229921ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 230021ecdf64SLin Ling 230121ecdf64SLin Ling static nvlist_t * 230221ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 2303e7cbe64fSgw { 230421ecdf64SLin Ling nvlist_t *config; 2305e7cbe64fSgw nvlist_t *nvtop, *nvroot; 2306e7cbe64fSgw uint64_t pgid; 2307e7cbe64fSgw 230821ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 230921ecdf64SLin Ling return (NULL); 231021ecdf64SLin Ling 2311e7cbe64fSgw /* 2312e7cbe64fSgw * Add this top-level vdev to the child array. 2313e7cbe64fSgw */ 231421ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 231521ecdf64SLin Ling &nvtop) == 0); 231621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 231721ecdf64SLin Ling &pgid) == 0); 231821ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 2319e7cbe64fSgw 2320e7cbe64fSgw /* 2321e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 2322e7cbe64fSgw */ 2323e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 232421ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 232521ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 2326e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 2327e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 2328e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2329e7cbe64fSgw &nvtop, 1) == 0); 2330e7cbe64fSgw 2331e7cbe64fSgw /* 2332e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 2333e7cbe64fSgw * this pool's configuration (remove the old, add the new). 2334e7cbe64fSgw */ 2335e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 2336e7cbe64fSgw nvlist_free(nvroot); 233721ecdf64SLin Ling return (config); 2338e7cbe64fSgw } 2339e7cbe64fSgw 2340e7cbe64fSgw /* 234121ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 234221ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 234321ecdf64SLin Ling * device has a more recent txg. 2344051aabe6Staylor */ 234521ecdf64SLin Ling static void 234621ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 2347051aabe6Staylor { 2348573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 234921ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 2350051aabe6Staylor 235121ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 235221ecdf64SLin Ling nvlist_t *label; 235321ecdf64SLin Ling uint64_t label_txg; 2354051aabe6Staylor 235521ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 235621ecdf64SLin Ling &label) != 0) 235721ecdf64SLin Ling return; 2358051aabe6Staylor 235921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 236021ecdf64SLin Ling &label_txg) == 0); 2361051aabe6Staylor 236221ecdf64SLin Ling /* 236321ecdf64SLin Ling * Do we have a better boot device? 236421ecdf64SLin Ling */ 236521ecdf64SLin Ling if (label_txg > *txg) { 236621ecdf64SLin Ling *txg = label_txg; 236721ecdf64SLin Ling *avd = vd; 2368051aabe6Staylor } 236921ecdf64SLin Ling nvlist_free(label); 2370051aabe6Staylor } 2371051aabe6Staylor } 2372051aabe6Staylor 2373e7cbe64fSgw /* 2374e7cbe64fSgw * Import a root pool. 2375e7cbe64fSgw * 2376051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 2377051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 2378051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 2379e7cbe64fSgw * 2380e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 2381e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 2382e7cbe64fSgw * e.g. 2383e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 2384e7cbe64fSgw */ 2385e7cbe64fSgw int 2386051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 2387e7cbe64fSgw { 238821ecdf64SLin Ling spa_t *spa; 238921ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 239021ecdf64SLin Ling nvlist_t *config, *nvtop; 239121ecdf64SLin Ling uint64_t guid, txg; 2392e7cbe64fSgw char *pname; 2393e7cbe64fSgw int error; 2394e7cbe64fSgw 2395e7cbe64fSgw /* 239621ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 2397e7cbe64fSgw */ 239821ecdf64SLin Ling if ((config = spa_generate_rootconf(devpath, devid, &guid)) == NULL) { 239921ecdf64SLin Ling cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 240021ecdf64SLin Ling devpath); 240121ecdf64SLin Ling return (EIO); 240221ecdf64SLin Ling } 2403e7cbe64fSgw 240421ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 240521ecdf64SLin Ling &pname) == 0); 240621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 2407e7cbe64fSgw 24086809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 24096809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 24106809eb4eSEric Schrock /* 24116809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 24126809eb4eSEric Schrock * can replace it with the correct config we just read in. 24136809eb4eSEric Schrock */ 24146809eb4eSEric Schrock spa_remove(spa); 24156809eb4eSEric Schrock } 24166809eb4eSEric Schrock 24176809eb4eSEric Schrock spa = spa_add(pname, NULL); 24186809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 2419bc758434SLin Ling spa->spa_load_verbatim = B_TRUE; 2420e7cbe64fSgw 242121ecdf64SLin Ling /* 242221ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 242321ecdf64SLin Ling */ 242421ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 242521ecdf64SLin Ling &nvtop) == 0); 242621ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 242721ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 242821ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 242921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 243021ecdf64SLin Ling if (error) { 243121ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 243221ecdf64SLin Ling nvlist_free(config); 243321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 243421ecdf64SLin Ling pname); 243521ecdf64SLin Ling return (error); 243621ecdf64SLin Ling } 243721ecdf64SLin Ling 243821ecdf64SLin Ling /* 243921ecdf64SLin Ling * Get the boot vdev. 244021ecdf64SLin Ling */ 244121ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 244221ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 244321ecdf64SLin Ling (u_longlong_t)guid); 244421ecdf64SLin Ling error = ENOENT; 244521ecdf64SLin Ling goto out; 244621ecdf64SLin Ling } 2447e7cbe64fSgw 244821ecdf64SLin Ling /* 244921ecdf64SLin Ling * Determine if there is a better boot device. 245021ecdf64SLin Ling */ 245121ecdf64SLin Ling avd = bvd; 245221ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 245321ecdf64SLin Ling if (avd != bvd) { 245421ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 245521ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 245621ecdf64SLin Ling error = EINVAL; 245721ecdf64SLin Ling goto out; 245821ecdf64SLin Ling } 2459e7cbe64fSgw 246021ecdf64SLin Ling /* 246121ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 246221ecdf64SLin Ling * we're booting off the active spare. 246321ecdf64SLin Ling */ 246421ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 246521ecdf64SLin Ling !bvd->vdev_isspare) { 246621ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 246721ecdf64SLin Ling "try booting from '%s'", 246821ecdf64SLin Ling bvd->vdev_parent->vdev_child[1]->vdev_path); 246921ecdf64SLin Ling error = EINVAL; 247021ecdf64SLin Ling goto out; 247121ecdf64SLin Ling } 247221ecdf64SLin Ling 247321ecdf64SLin Ling VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 247421ecdf64SLin Ling error = 0; 2475c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 247621ecdf64SLin Ling out: 247721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 247821ecdf64SLin Ling vdev_free(rvd); 247921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 248021ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 248121ecdf64SLin Ling 248221ecdf64SLin Ling nvlist_free(config); 2483e7cbe64fSgw return (error); 2484e7cbe64fSgw } 248521ecdf64SLin Ling 2486e7cbe64fSgw #endif 2487e7cbe64fSgw 2488e7cbe64fSgw /* 24896809eb4eSEric Schrock * Take a pool and insert it into the namespace as if it had been loaded at 24906809eb4eSEric Schrock * boot. 2491e7cbe64fSgw */ 2492e7cbe64fSgw int 24936809eb4eSEric Schrock spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 2494e7cbe64fSgw { 24956809eb4eSEric Schrock spa_t *spa; 24966809eb4eSEric Schrock char *altroot = NULL; 24976809eb4eSEric Schrock 24986809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 24996809eb4eSEric Schrock if (spa_lookup(pool) != NULL) { 25006809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 25016809eb4eSEric Schrock return (EEXIST); 25026809eb4eSEric Schrock } 25036809eb4eSEric Schrock 25046809eb4eSEric Schrock (void) nvlist_lookup_string(props, 25056809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 25066809eb4eSEric Schrock spa = spa_add(pool, altroot); 25076809eb4eSEric Schrock 2508bc758434SLin Ling spa->spa_load_verbatim = B_TRUE; 25094f0f5e5bSVictor Latushkin 25106809eb4eSEric Schrock VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 25116809eb4eSEric Schrock 25126809eb4eSEric Schrock if (props != NULL) 25136809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 25146809eb4eSEric Schrock 25156809eb4eSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE); 25166809eb4eSEric Schrock 25176809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 2518c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 25196809eb4eSEric Schrock 25206809eb4eSEric Schrock return (0); 2521e7cbe64fSgw } 2522e7cbe64fSgw 25236809eb4eSEric Schrock /* 25246809eb4eSEric Schrock * Import a non-root pool into the system. 25256809eb4eSEric Schrock */ 2526c5904d13Seschrock int 25276809eb4eSEric Schrock spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 2528c5904d13Seschrock { 25296809eb4eSEric Schrock spa_t *spa; 25306809eb4eSEric Schrock char *altroot = NULL; 25316809eb4eSEric Schrock int error; 25326809eb4eSEric Schrock nvlist_t *nvroot; 25336809eb4eSEric Schrock nvlist_t **spares, **l2cache; 25346809eb4eSEric Schrock uint_t nspares, nl2cache; 25356809eb4eSEric Schrock 25366809eb4eSEric Schrock /* 25376809eb4eSEric Schrock * If a pool with this name exists, return failure. 25386809eb4eSEric Schrock */ 25396809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 25406809eb4eSEric Schrock if ((spa = spa_lookup(pool)) != NULL) { 25416809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 25426809eb4eSEric Schrock return (EEXIST); 25436809eb4eSEric Schrock } 25446809eb4eSEric Schrock 25456809eb4eSEric Schrock /* 25466809eb4eSEric Schrock * Create and initialize the spa structure. 25476809eb4eSEric Schrock */ 25486809eb4eSEric Schrock (void) nvlist_lookup_string(props, 25496809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 25506809eb4eSEric Schrock spa = spa_add(pool, altroot); 25516809eb4eSEric Schrock spa_activate(spa, spa_mode_global); 25526809eb4eSEric Schrock 255325f89ee2SJeff Bonwick /* 255425f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 255525f89ee2SJeff Bonwick */ 255625f89ee2SJeff Bonwick spa_async_suspend(spa); 255725f89ee2SJeff Bonwick 25586809eb4eSEric Schrock /* 25596809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 25606809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 25616809eb4eSEric Schrock * doing an import. 25626809eb4eSEric Schrock */ 25636809eb4eSEric Schrock error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 25646809eb4eSEric Schrock 25656809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 25666809eb4eSEric Schrock /* 25676809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 25686809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 25696809eb4eSEric Schrock */ 25706809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 25716809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 25726809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 25736809eb4eSEric Schrock spa_load_spares(spa); 25746809eb4eSEric Schrock } 25756809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 25766809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 25776809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 25786809eb4eSEric Schrock spa_load_l2cache(spa); 25796809eb4eSEric Schrock } 25806809eb4eSEric Schrock 25816809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 25826809eb4eSEric Schrock &nvroot) == 0); 25836809eb4eSEric Schrock if (error == 0) 25846809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 25856809eb4eSEric Schrock VDEV_ALLOC_SPARE); 25866809eb4eSEric Schrock if (error == 0) 25876809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 25886809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 25896809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 25906809eb4eSEric Schrock 25916809eb4eSEric Schrock if (props != NULL) 25926809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 25936809eb4eSEric Schrock 25946809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 25956809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 25966809eb4eSEric Schrock spa_unload(spa); 25976809eb4eSEric Schrock spa_deactivate(spa); 25986809eb4eSEric Schrock spa_remove(spa); 25996809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 26006809eb4eSEric Schrock return (error); 26016809eb4eSEric Schrock } 26026809eb4eSEric Schrock 260325f89ee2SJeff Bonwick spa_async_resume(spa); 260425f89ee2SJeff Bonwick 26056809eb4eSEric Schrock /* 26066809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 26076809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 26086809eb4eSEric Schrock */ 26096809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 26106809eb4eSEric Schrock &spares, &nspares) == 0) { 26116809eb4eSEric Schrock if (spa->spa_spares.sav_config) 26126809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 26136809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 26146809eb4eSEric Schrock else 26156809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 26166809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 26176809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 26186809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 26196809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26206809eb4eSEric Schrock spa_load_spares(spa); 26216809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 26226809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 26236809eb4eSEric Schrock } 26246809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 26256809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 26266809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 26276809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 26286809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 26296809eb4eSEric Schrock else 26306809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 26316809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 26326809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 26336809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 26346809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26356809eb4eSEric Schrock spa_load_l2cache(spa); 26366809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 26376809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 26386809eb4eSEric Schrock } 26396809eb4eSEric Schrock 26406809eb4eSEric Schrock if (spa_writeable(spa)) { 26416809eb4eSEric Schrock /* 26426809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 26436809eb4eSEric Schrock */ 2644bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26456809eb4eSEric Schrock } 26466809eb4eSEric Schrock 2647573ca77eSGeorge Wilson /* 2648573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 2649573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 2650573ca77eSGeorge Wilson */ 2651573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 2652573ca77eSGeorge Wilson 26536809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 2654c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 26556809eb4eSEric Schrock 26566809eb4eSEric Schrock return (0); 2657c5904d13Seschrock } 2658c5904d13Seschrock 2659c5904d13Seschrock 2660fa9e4066Sahrens /* 2661fa9e4066Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2662fa9e4066Sahrens * to get the vdev stats associated with the imported devices. 2663fa9e4066Sahrens */ 2664fa9e4066Sahrens #define TRYIMPORT_NAME "$import" 2665fa9e4066Sahrens 2666fa9e4066Sahrens nvlist_t * 2667fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 2668fa9e4066Sahrens { 2669fa9e4066Sahrens nvlist_t *config = NULL; 2670fa9e4066Sahrens char *poolname; 2671fa9e4066Sahrens spa_t *spa; 2672fa9e4066Sahrens uint64_t state; 26737b7154beSLin Ling int error; 2674fa9e4066Sahrens 2675fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2676fa9e4066Sahrens return (NULL); 2677fa9e4066Sahrens 2678fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2679fa9e4066Sahrens return (NULL); 2680fa9e4066Sahrens 2681fa9e4066Sahrens /* 26820373e76bSbonwick * Create and initialize the spa structure. 2683fa9e4066Sahrens */ 26840373e76bSbonwick mutex_enter(&spa_namespace_lock); 26850373e76bSbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 26868ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 2687fa9e4066Sahrens 2688fa9e4066Sahrens /* 26890373e76bSbonwick * Pass off the heavy lifting to spa_load(). 2690ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 2691ecc2d604Sbonwick * is actually the one to trust when doing an import. 2692fa9e4066Sahrens */ 26937b7154beSLin Ling error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2694fa9e4066Sahrens 2695fa9e4066Sahrens /* 2696fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 2697fa9e4066Sahrens */ 2698fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 2699fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2700fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2701fa9e4066Sahrens poolname) == 0); 2702fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2703fa9e4066Sahrens state) == 0); 270495173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 270595173954Sek spa->spa_uberblock.ub_timestamp) == 0); 270699653d4eSeschrock 2707e7cbe64fSgw /* 2708e7cbe64fSgw * If the bootfs property exists on this pool then we 2709e7cbe64fSgw * copy it out so that external consumers can tell which 2710e7cbe64fSgw * pools are bootable. 2711e7cbe64fSgw */ 27127b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 2713e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2714e7cbe64fSgw 2715e7cbe64fSgw /* 2716e7cbe64fSgw * We have to play games with the name since the 2717e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 2718e7cbe64fSgw */ 2719e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 2720e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 2721e7cbe64fSgw char *cp; 2722e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2723e7cbe64fSgw 2724e7cbe64fSgw cp = strchr(tmpname, '/'); 2725e7cbe64fSgw if (cp == NULL) { 2726e7cbe64fSgw (void) strlcpy(dsname, tmpname, 2727e7cbe64fSgw MAXPATHLEN); 2728e7cbe64fSgw } else { 2729e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 2730e7cbe64fSgw "%s/%s", poolname, ++cp); 2731e7cbe64fSgw } 2732e7cbe64fSgw VERIFY(nvlist_add_string(config, 2733e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 2734e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 2735e7cbe64fSgw } 2736e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 2737e7cbe64fSgw } 2738e7cbe64fSgw 273999653d4eSeschrock /* 2740fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 274199653d4eSeschrock */ 27426809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 274399653d4eSeschrock spa_add_spares(spa, config); 2744fa94a07fSbrendan spa_add_l2cache(spa, config); 27456809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 2746fa9e4066Sahrens } 2747fa9e4066Sahrens 2748fa9e4066Sahrens spa_unload(spa); 2749fa9e4066Sahrens spa_deactivate(spa); 2750fa9e4066Sahrens spa_remove(spa); 2751fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2752fa9e4066Sahrens 2753fa9e4066Sahrens return (config); 2754fa9e4066Sahrens } 2755fa9e4066Sahrens 2756fa9e4066Sahrens /* 2757fa9e4066Sahrens * Pool export/destroy 2758fa9e4066Sahrens * 2759fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2760fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2761fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 2762394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 2763394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 2764fa9e4066Sahrens */ 2765fa9e4066Sahrens static int 276689a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 2767394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 2768fa9e4066Sahrens { 2769fa9e4066Sahrens spa_t *spa; 2770fa9e4066Sahrens 277144cd46caSbillm if (oldconfig) 277244cd46caSbillm *oldconfig = NULL; 277344cd46caSbillm 27748ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 2775fa9e4066Sahrens return (EROFS); 2776fa9e4066Sahrens 2777fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2778fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2779fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2780fa9e4066Sahrens return (ENOENT); 2781fa9e4066Sahrens } 2782fa9e4066Sahrens 2783ea8dc4b6Seschrock /* 2784ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 2785ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 2786ea8dc4b6Seschrock */ 2787ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 2788ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2789ea8dc4b6Seschrock spa_async_suspend(spa); 2790ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2791ea8dc4b6Seschrock spa_close(spa, FTAG); 2792ea8dc4b6Seschrock 2793fa9e4066Sahrens /* 2794fa9e4066Sahrens * The pool will be in core if it's openable, 2795fa9e4066Sahrens * in which case we can modify its state. 2796fa9e4066Sahrens */ 2797fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2798fa9e4066Sahrens /* 2799fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 2800fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 2801fa9e4066Sahrens */ 2802fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2803fa9e4066Sahrens 2804ea8dc4b6Seschrock /* 2805ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 2806ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 2807ea8dc4b6Seschrock * fault injection handlers. 2808ea8dc4b6Seschrock */ 2809ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 2810ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 2811ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 2812ea8dc4b6Seschrock spa_async_resume(spa); 2813fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2814fa9e4066Sahrens return (EBUSY); 2815fa9e4066Sahrens } 2816fa9e4066Sahrens 281789a89ebfSlling /* 281889a89ebfSlling * A pool cannot be exported if it has an active shared spare. 281989a89ebfSlling * This is to prevent other pools stealing the active spare 282089a89ebfSlling * from an exported pool. At user's own will, such pool can 282189a89ebfSlling * be forcedly exported. 282289a89ebfSlling */ 282389a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 282489a89ebfSlling spa_has_active_shared_spare(spa)) { 282589a89ebfSlling spa_async_resume(spa); 282689a89ebfSlling mutex_exit(&spa_namespace_lock); 282789a89ebfSlling return (EXDEV); 282889a89ebfSlling } 282989a89ebfSlling 2830fa9e4066Sahrens /* 2831fa9e4066Sahrens * We want this to be reflected on every label, 2832fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 2833fa9e4066Sahrens * final sync that pushes these changes out. 2834fa9e4066Sahrens */ 2835394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 2836e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2837ea8dc4b6Seschrock spa->spa_state = new_state; 28380373e76bSbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 2839ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 2840e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2841ea8dc4b6Seschrock } 2842fa9e4066Sahrens } 2843fa9e4066Sahrens 28443d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 28453d7072f8Seschrock 2846fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2847fa9e4066Sahrens spa_unload(spa); 2848fa9e4066Sahrens spa_deactivate(spa); 2849fa9e4066Sahrens } 2850fa9e4066Sahrens 285144cd46caSbillm if (oldconfig && spa->spa_config) 285244cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 285344cd46caSbillm 2854ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 2855394ab0cbSGeorge Wilson if (!hardforce) 2856394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 2857ea8dc4b6Seschrock spa_remove(spa); 2858ea8dc4b6Seschrock } 2859fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2860fa9e4066Sahrens 2861fa9e4066Sahrens return (0); 2862fa9e4066Sahrens } 2863fa9e4066Sahrens 2864fa9e4066Sahrens /* 2865fa9e4066Sahrens * Destroy a storage pool. 2866fa9e4066Sahrens */ 2867fa9e4066Sahrens int 2868fa9e4066Sahrens spa_destroy(char *pool) 2869fa9e4066Sahrens { 2870394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 2871394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 2872fa9e4066Sahrens } 2873fa9e4066Sahrens 2874fa9e4066Sahrens /* 2875fa9e4066Sahrens * Export a storage pool. 2876fa9e4066Sahrens */ 2877fa9e4066Sahrens int 2878394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 2879394ab0cbSGeorge Wilson boolean_t hardforce) 2880fa9e4066Sahrens { 2881394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 2882394ab0cbSGeorge Wilson force, hardforce)); 2883fa9e4066Sahrens } 2884fa9e4066Sahrens 2885ea8dc4b6Seschrock /* 2886ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 2887ea8dc4b6Seschrock * from the namespace in any way. 2888ea8dc4b6Seschrock */ 2889ea8dc4b6Seschrock int 2890ea8dc4b6Seschrock spa_reset(char *pool) 2891ea8dc4b6Seschrock { 289289a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 2893394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 2894ea8dc4b6Seschrock } 2895ea8dc4b6Seschrock 2896fa9e4066Sahrens /* 2897fa9e4066Sahrens * ========================================================================== 2898fa9e4066Sahrens * Device manipulation 2899fa9e4066Sahrens * ========================================================================== 2900fa9e4066Sahrens */ 2901fa9e4066Sahrens 2902fa9e4066Sahrens /* 29038654d025Sperrin * Add a device to a storage pool. 2904fa9e4066Sahrens */ 2905fa9e4066Sahrens int 2906fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2907fa9e4066Sahrens { 2908fa9e4066Sahrens uint64_t txg; 29098ad4d6ddSJeff Bonwick int error; 2910fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 29110e34b6a7Sbonwick vdev_t *vd, *tvd; 2912fa94a07fSbrendan nvlist_t **spares, **l2cache; 2913fa94a07fSbrendan uint_t nspares, nl2cache; 2914fa9e4066Sahrens 2915fa9e4066Sahrens txg = spa_vdev_enter(spa); 2916fa9e4066Sahrens 291799653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 291899653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 291999653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 2920fa9e4066Sahrens 2921e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 292299653d4eSeschrock 2923fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 2924fa94a07fSbrendan &nspares) != 0) 292599653d4eSeschrock nspares = 0; 292699653d4eSeschrock 2927fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 2928fa94a07fSbrendan &nl2cache) != 0) 2929fa94a07fSbrendan nl2cache = 0; 2930fa94a07fSbrendan 2931e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 2932fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 2933fa9e4066Sahrens 2934e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 2935e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 2936e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 293799653d4eSeschrock 293839c23413Seschrock /* 2939fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 2940fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 294139c23413Seschrock */ 2942e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 294339c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 294439c23413Seschrock 294539c23413Seschrock /* 294639c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 294739c23413Seschrock */ 29488ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 294939c23413Seschrock tvd = vd->vdev_child[c]; 295039c23413Seschrock vdev_remove_child(vd, tvd); 295139c23413Seschrock tvd->vdev_id = rvd->vdev_children; 295239c23413Seschrock vdev_add_child(rvd, tvd); 295339c23413Seschrock vdev_config_dirty(tvd); 295439c23413Seschrock } 295539c23413Seschrock 295699653d4eSeschrock if (nspares != 0) { 2957fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 2958fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 295999653d4eSeschrock spa_load_spares(spa); 2960fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2961fa94a07fSbrendan } 2962fa94a07fSbrendan 2963fa94a07fSbrendan if (nl2cache != 0) { 2964fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 2965fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 2966fa94a07fSbrendan spa_load_l2cache(spa); 2967fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2968fa9e4066Sahrens } 2969fa9e4066Sahrens 2970fa9e4066Sahrens /* 29710e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 29720e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 29730e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 29740e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 29750e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 29760e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 29770373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 29780e34b6a7Sbonwick * 29790e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 29800e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 29810e34b6a7Sbonwick * steps will be completed the next time we load the pool. 29820e34b6a7Sbonwick */ 29830373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 29840e34b6a7Sbonwick 29850373e76bSbonwick mutex_enter(&spa_namespace_lock); 29860373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 29870373e76bSbonwick mutex_exit(&spa_namespace_lock); 2988fa9e4066Sahrens 29890373e76bSbonwick return (0); 2990fa9e4066Sahrens } 2991fa9e4066Sahrens 2992fa9e4066Sahrens /* 2993fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 2994fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2995fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2996fa9e4066Sahrens * 2997fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 2998fa9e4066Sahrens * existing device; in this case the two devices are made into their own 29993d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3000fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3001fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 3002fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 3003fa9e4066Sahrens * is automatically detached. 3004fa9e4066Sahrens */ 3005fa9e4066Sahrens int 3006ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3007fa9e4066Sahrens { 3008fa9e4066Sahrens uint64_t txg, open_txg; 3009fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3010fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 301199653d4eSeschrock vdev_ops_t *pvops; 30129b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 30139b3f6b42SEric Kustarz int newvd_isspare; 30149b3f6b42SEric Kustarz int error; 3015fa9e4066Sahrens 3016fa9e4066Sahrens txg = spa_vdev_enter(spa); 3017fa9e4066Sahrens 3018c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3019fa9e4066Sahrens 3020fa9e4066Sahrens if (oldvd == NULL) 3021fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3022fa9e4066Sahrens 30230e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 30240e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30250e34b6a7Sbonwick 3026fa9e4066Sahrens pvd = oldvd->vdev_parent; 3027fa9e4066Sahrens 302899653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 30293d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 30303d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 30313d7072f8Seschrock 30323d7072f8Seschrock if (newrootvd->vdev_children != 1) 3033fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3034fa9e4066Sahrens 3035fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 3036fa9e4066Sahrens 3037fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3038fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3039fa9e4066Sahrens 304099653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3041fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3042fa9e4066Sahrens 30438654d025Sperrin /* 30448654d025Sperrin * Spares can't replace logs 30458654d025Sperrin */ 3046ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 30478654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30488654d025Sperrin 304999653d4eSeschrock if (!replacing) { 305099653d4eSeschrock /* 305199653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 305299653d4eSeschrock * vdev. 305399653d4eSeschrock */ 305499653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 305599653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 305699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 305799653d4eSeschrock 305899653d4eSeschrock pvops = &vdev_mirror_ops; 305999653d4eSeschrock } else { 306099653d4eSeschrock /* 306199653d4eSeschrock * Active hot spares can only be replaced by inactive hot 306299653d4eSeschrock * spares. 306399653d4eSeschrock */ 306499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 306599653d4eSeschrock pvd->vdev_child[1] == oldvd && 306699653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 306799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 306899653d4eSeschrock 306999653d4eSeschrock /* 307099653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 307199653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 307239c23413Seschrock * want to create a replacing vdev. The user is not allowed to 307339c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 307439c23413Seschrock * the same (spare replaces spare, non-spare replaces 307539c23413Seschrock * non-spare). 307699653d4eSeschrock */ 307799653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 307899653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 307939c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 308039c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 308139c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 308299653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 308399653d4eSeschrock newvd->vdev_isspare) 308499653d4eSeschrock pvops = &vdev_spare_ops; 308599653d4eSeschrock else 308699653d4eSeschrock pvops = &vdev_replacing_ops; 308799653d4eSeschrock } 308899653d4eSeschrock 30892a79c5feSlling /* 3090573ca77eSGeorge Wilson * Make sure the new device is big enough. 30912a79c5feSlling */ 3092573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3093fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3094fa9e4066Sahrens 3095ecc2d604Sbonwick /* 3096ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 3097ecc2d604Sbonwick * than the top-level vdev. 3098ecc2d604Sbonwick */ 3099ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3100fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3101fa9e4066Sahrens 3102fa9e4066Sahrens /* 3103fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 3104fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3105fa9e4066Sahrens */ 3106fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3107fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 3108fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3109fa9e4066Sahrens KM_SLEEP); 3110fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3111fa9e4066Sahrens newvd->vdev_path, "old"); 3112fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 3113fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 3114fa9e4066Sahrens oldvd->vdev_devid = NULL; 3115fa9e4066Sahrens } 3116fa9e4066Sahrens } 3117fa9e4066Sahrens 3118fa9e4066Sahrens /* 311999653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 312099653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 3121fa9e4066Sahrens */ 3122fa9e4066Sahrens if (pvd->vdev_ops != pvops) 3123fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 3124fa9e4066Sahrens 3125fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3126fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 3127fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 3128fa9e4066Sahrens 3129fa9e4066Sahrens /* 3130fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 3131fa9e4066Sahrens */ 3132fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 3133fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 3134fa9e4066Sahrens vdev_add_child(pvd, newvd); 3135fa9e4066Sahrens 3136fa9e4066Sahrens tvd = newvd->vdev_top; 3137fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 3138fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3139fa9e4066Sahrens 3140fa9e4066Sahrens vdev_config_dirty(tvd); 3141fa9e4066Sahrens 3142fa9e4066Sahrens /* 3143fa9e4066Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3144fa9e4066Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3145fa9e4066Sahrens */ 3146fa9e4066Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3147fa9e4066Sahrens 31488ad4d6ddSJeff Bonwick vdev_dtl_dirty(newvd, DTL_MISSING, 31498ad4d6ddSJeff Bonwick TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3150fa9e4066Sahrens 31516809eb4eSEric Schrock if (newvd->vdev_isspare) { 315239c23413Seschrock spa_spare_activate(newvd); 31536809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 31546809eb4eSEric Schrock } 31556809eb4eSEric Schrock 3156e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 3157e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 31589b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 3159ea8dc4b6Seschrock 3160fa9e4066Sahrens /* 3161fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 3162fa9e4066Sahrens */ 3163ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3164fa9e4066Sahrens 3165fa9e4066Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3166fa9e4066Sahrens 3167c8e1f6d2SMark J Musante spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL, 3168c8e1f6d2SMark J Musante CRED(), "%s vdev=%s %s vdev=%s", 3169c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 3170c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 3171c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 31729b3f6b42SEric Kustarz 31739b3f6b42SEric Kustarz spa_strfree(oldvdpath); 31749b3f6b42SEric Kustarz spa_strfree(newvdpath); 31759b3f6b42SEric Kustarz 3176fa9e4066Sahrens /* 3177088f3894Sahrens * Kick off a resilver to update newvd. 3178fa9e4066Sahrens */ 3179088f3894Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3180fa9e4066Sahrens 3181fa9e4066Sahrens return (0); 3182fa9e4066Sahrens } 3183fa9e4066Sahrens 3184fa9e4066Sahrens /* 3185fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 3186fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 3187fa9e4066Sahrens * is a replacing vdev. 3188fa9e4066Sahrens */ 3189fa9e4066Sahrens int 31908ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3191fa9e4066Sahrens { 3192fa9e4066Sahrens uint64_t txg; 31938ad4d6ddSJeff Bonwick int error; 3194fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3195fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 319699653d4eSeschrock boolean_t unspare = B_FALSE; 319799653d4eSeschrock uint64_t unspare_guid; 3198bf82a41bSeschrock size_t len; 3199fa9e4066Sahrens 3200fa9e4066Sahrens txg = spa_vdev_enter(spa); 3201fa9e4066Sahrens 3202c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3203fa9e4066Sahrens 3204fa9e4066Sahrens if (vd == NULL) 3205fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3206fa9e4066Sahrens 32070e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 32080e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 32090e34b6a7Sbonwick 3210fa9e4066Sahrens pvd = vd->vdev_parent; 3211fa9e4066Sahrens 32128ad4d6ddSJeff Bonwick /* 32138ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 32148ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 32158ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 32168ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 32178ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 32188ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 32198ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 32208ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 32218ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 32228ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 32238ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 32248ad4d6ddSJeff Bonwick */ 32258ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 32268ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 32278ad4d6ddSJeff Bonwick 3228fa9e4066Sahrens /* 3229fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 323099653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 323199653d4eSeschrock * disk can be removed. 323299653d4eSeschrock */ 323399653d4eSeschrock if (replace_done) { 323499653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 323599653d4eSeschrock if (vd->vdev_id != 0) 323699653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 323799653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 323899653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 323999653d4eSeschrock } 324099653d4eSeschrock } 324199653d4eSeschrock 324299653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 3243e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3244fa9e4066Sahrens 3245fa9e4066Sahrens /* 324699653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 3247fa9e4066Sahrens */ 3248fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 324999653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 325099653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 3251fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3252fa9e4066Sahrens 3253fa9e4066Sahrens /* 32548ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 32558ad4d6ddSJeff Bonwick * we cannot safely detach it. 3256fa9e4066Sahrens */ 32578ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 3258fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3259fa9e4066Sahrens 32608ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 3261fa9e4066Sahrens 3262bf82a41bSeschrock /* 3263bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 3264bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 3265bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 3266bf82a41bSeschrock */ 3267bf82a41bSeschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 3268bf82a41bSeschrock pvd->vdev_child[0]->vdev_path != NULL && 3269bf82a41bSeschrock pvd->vdev_child[1]->vdev_path != NULL) { 3270bf82a41bSeschrock ASSERT(pvd->vdev_child[1] == vd); 3271bf82a41bSeschrock cvd = pvd->vdev_child[0]; 3272bf82a41bSeschrock len = strlen(vd->vdev_path); 3273bf82a41bSeschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 3274bf82a41bSeschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 3275bf82a41bSeschrock spa_strfree(cvd->vdev_path); 3276bf82a41bSeschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 3277bf82a41bSeschrock } 3278bf82a41bSeschrock } 3279bf82a41bSeschrock 328099653d4eSeschrock /* 328199653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 328299653d4eSeschrock * that the spare should become a real disk, and be removed from the 328399653d4eSeschrock * active spare list for the pool. 328499653d4eSeschrock */ 328599653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 32868ad4d6ddSJeff Bonwick vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 328799653d4eSeschrock unspare = B_TRUE; 328899653d4eSeschrock 3289fa9e4066Sahrens /* 3290fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 3291fa9e4066Sahrens * This must be done after all other error cases are handled, 3292fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 3293fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 3294fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 3295fa9e4066Sahrens * it's being detached! 3296fa9e4066Sahrens */ 329739c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3298fa9e4066Sahrens 3299fa9e4066Sahrens /* 3300fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 3301fa9e4066Sahrens */ 3302fa9e4066Sahrens vdev_remove_child(pvd, vd); 3303fa9e4066Sahrens vdev_compact_children(pvd); 3304fa9e4066Sahrens 3305fa9e4066Sahrens /* 3306fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 3307fa9e4066Sahrens */ 3308fa9e4066Sahrens cvd = pvd->vdev_child[0]; 3309fa9e4066Sahrens 331099653d4eSeschrock /* 331199653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 33128ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 33138ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 33148ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 33158ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 33168ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 33178ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 331899653d4eSeschrock */ 331999653d4eSeschrock if (unspare) { 332099653d4eSeschrock ASSERT(cvd->vdev_isspare); 332139c23413Seschrock spa_spare_remove(cvd); 332299653d4eSeschrock unspare_guid = cvd->vdev_guid; 33238ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 332499653d4eSeschrock } 332599653d4eSeschrock 3326fa9e4066Sahrens /* 3327fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 3328fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 3329fa9e4066Sahrens */ 3330fa9e4066Sahrens if (pvd->vdev_children == 1) 3331fa9e4066Sahrens vdev_remove_parent(cvd); 3332fa9e4066Sahrens 3333fa9e4066Sahrens /* 3334fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 3335fa9e4066Sahrens * may have been the previous top-level vdev. 3336fa9e4066Sahrens */ 3337fa9e4066Sahrens tvd = cvd->vdev_top; 3338fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3339fa9e4066Sahrens 3340fa9e4066Sahrens /* 334139c23413Seschrock * Reevaluate the parent vdev state. 3342fa9e4066Sahrens */ 33433d7072f8Seschrock vdev_propagate_state(cvd); 3344fa9e4066Sahrens 3345fa9e4066Sahrens /* 3346573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 3347573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 3348573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 3349573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 3350573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 3351fa9e4066Sahrens */ 3352573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 3353573ca77eSGeorge Wilson vdev_reopen(tvd); 3354573ca77eSGeorge Wilson vdev_expand(tvd, txg); 3355573ca77eSGeorge Wilson } 3356fa9e4066Sahrens 3357fa9e4066Sahrens vdev_config_dirty(tvd); 3358fa9e4066Sahrens 3359fa9e4066Sahrens /* 336039c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 336139c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 336239c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 336339c23413Seschrock * prevent vd from being accessed after it's freed. 3364fa9e4066Sahrens */ 33658ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 3366fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 3367ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 3368ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3369fa9e4066Sahrens 33703d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 33713d7072f8Seschrock 337299653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 337399653d4eSeschrock 337499653d4eSeschrock /* 337539c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 337639c23413Seschrock * then we want to go through and remove the device from the hot spare 337739c23413Seschrock * list of every other pool. 337899653d4eSeschrock */ 337999653d4eSeschrock if (unspare) { 33808ad4d6ddSJeff Bonwick spa_t *myspa = spa; 338199653d4eSeschrock spa = NULL; 338299653d4eSeschrock mutex_enter(&spa_namespace_lock); 338399653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 338499653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 338599653d4eSeschrock continue; 33868ad4d6ddSJeff Bonwick if (spa == myspa) 33878ad4d6ddSJeff Bonwick continue; 33889af0a4dfSJeff Bonwick spa_open_ref(spa, FTAG); 33899af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 339099653d4eSeschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 33919af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 33929af0a4dfSJeff Bonwick spa_close(spa, FTAG); 339399653d4eSeschrock } 339499653d4eSeschrock mutex_exit(&spa_namespace_lock); 339599653d4eSeschrock } 339699653d4eSeschrock 339799653d4eSeschrock return (error); 339899653d4eSeschrock } 339999653d4eSeschrock 3400e14bb325SJeff Bonwick static nvlist_t * 3401e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 340299653d4eSeschrock { 3403e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 3404e14bb325SJeff Bonwick uint64_t guid; 340599653d4eSeschrock 3406e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 3407e14bb325SJeff Bonwick &guid) == 0); 340899653d4eSeschrock 3409e14bb325SJeff Bonwick if (guid == target_guid) 3410e14bb325SJeff Bonwick return (nvpp[i]); 341199653d4eSeschrock } 341299653d4eSeschrock 3413e14bb325SJeff Bonwick return (NULL); 3414fa94a07fSbrendan } 3415fa94a07fSbrendan 3416e14bb325SJeff Bonwick static void 3417e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 3418e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 3419fa94a07fSbrendan { 3420e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 3421fa94a07fSbrendan 3422e14bb325SJeff Bonwick if (count > 1) 3423e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 3424fa94a07fSbrendan 3425e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 3426e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 3427e14bb325SJeff Bonwick continue; 3428e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 3429fa94a07fSbrendan } 3430fa94a07fSbrendan 3431e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 3432e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 3433fa94a07fSbrendan 3434e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 3435e14bb325SJeff Bonwick nvlist_free(newdev[i]); 3436fa94a07fSbrendan 3437e14bb325SJeff Bonwick if (count > 1) 3438e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 3439fa94a07fSbrendan } 3440fa94a07fSbrendan 3441fa94a07fSbrendan /* 3442fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 3443fa94a07fSbrendan * spares and level 2 ARC devices. 3444fa94a07fSbrendan */ 3445fa94a07fSbrendan int 3446fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 3447fa94a07fSbrendan { 3448fa94a07fSbrendan vdev_t *vd; 3449e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 3450fa94a07fSbrendan uint_t nspares, nl2cache; 34518ad4d6ddSJeff Bonwick uint64_t txg = 0; 3452fa94a07fSbrendan int error = 0; 34538ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 3454fa94a07fSbrendan 34558ad4d6ddSJeff Bonwick if (!locked) 34568ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 3457fa94a07fSbrendan 3458c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3459fa94a07fSbrendan 3460fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 3461fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 3462e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 3463e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 3464e14bb325SJeff Bonwick /* 3465e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 3466e14bb325SJeff Bonwick * in this pool. 3467e14bb325SJeff Bonwick */ 3468e14bb325SJeff Bonwick if (vd == NULL || unspare) { 3469e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 3470e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 3471e14bb325SJeff Bonwick spa_load_spares(spa); 3472e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 3473e14bb325SJeff Bonwick } else { 3474e14bb325SJeff Bonwick error = EBUSY; 3475e14bb325SJeff Bonwick } 3476e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 3477fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3478e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 3479e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 3480e14bb325SJeff Bonwick /* 3481e14bb325SJeff Bonwick * Cache devices can always be removed. 3482e14bb325SJeff Bonwick */ 3483e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 3484e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 3485fa94a07fSbrendan spa_load_l2cache(spa); 3486fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3487e14bb325SJeff Bonwick } else if (vd != NULL) { 3488e14bb325SJeff Bonwick /* 3489e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 3490e14bb325SJeff Bonwick */ 3491e14bb325SJeff Bonwick error = ENOTSUP; 3492e14bb325SJeff Bonwick } else { 3493e14bb325SJeff Bonwick /* 3494e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 3495e14bb325SJeff Bonwick */ 3496e14bb325SJeff Bonwick error = ENOENT; 3497fa94a07fSbrendan } 349899653d4eSeschrock 34998ad4d6ddSJeff Bonwick if (!locked) 35008ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 35018ad4d6ddSJeff Bonwick 35028ad4d6ddSJeff Bonwick return (error); 3503fa9e4066Sahrens } 3504fa9e4066Sahrens 3505fa9e4066Sahrens /* 35063d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 35073d7072f8Seschrock * current spared, so we can detach it. 3508fa9e4066Sahrens */ 3509ea8dc4b6Seschrock static vdev_t * 35103d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3511fa9e4066Sahrens { 3512ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 3513fa9e4066Sahrens 3514573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 35153d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 3516ea8dc4b6Seschrock if (oldvd != NULL) 3517ea8dc4b6Seschrock return (oldvd); 3518ea8dc4b6Seschrock } 3519fa9e4066Sahrens 35203d7072f8Seschrock /* 35213d7072f8Seschrock * Check for a completed replacement. 35223d7072f8Seschrock */ 3523fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 3524ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 3525ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 3526ea8dc4b6Seschrock 35278ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 35288ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 3529ea8dc4b6Seschrock return (oldvd); 3530fa9e4066Sahrens } 3531ea8dc4b6Seschrock 35323d7072f8Seschrock /* 35333d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 35343d7072f8Seschrock */ 35353d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 35363d7072f8Seschrock newvd = vd->vdev_child[0]; 35373d7072f8Seschrock oldvd = vd->vdev_child[1]; 35383d7072f8Seschrock 35393d7072f8Seschrock if (newvd->vdev_unspare && 35408ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 35418ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) { 35423d7072f8Seschrock newvd->vdev_unspare = 0; 35433d7072f8Seschrock return (oldvd); 35443d7072f8Seschrock } 35453d7072f8Seschrock } 35463d7072f8Seschrock 3547ea8dc4b6Seschrock return (NULL); 3548fa9e4066Sahrens } 3549fa9e4066Sahrens 3550ea8dc4b6Seschrock static void 35513d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 3552fa9e4066Sahrens { 35538ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 35548ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 3555ea8dc4b6Seschrock 35568ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3557ea8dc4b6Seschrock 35583d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 35598ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 35608ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 3561ea8dc4b6Seschrock guid = vd->vdev_guid; 35628ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 35638ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 35648ad4d6ddSJeff Bonwick sguid = 0; 356599653d4eSeschrock /* 356699653d4eSeschrock * If we have just finished replacing a hot spared device, then 356799653d4eSeschrock * we need to detach the parent's first child (the original hot 356899653d4eSeschrock * spare) as well. 356999653d4eSeschrock */ 35708ad4d6ddSJeff Bonwick if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 357199653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 35728ad4d6ddSJeff Bonwick ASSERT(ppvd->vdev_children == 2); 35738ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 357499653d4eSeschrock } 35758ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 35768ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 3577ea8dc4b6Seschrock return; 35788ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 357999653d4eSeschrock return; 35808ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3581fa9e4066Sahrens } 3582fa9e4066Sahrens 35838ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3584fa9e4066Sahrens } 3585fa9e4066Sahrens 3586c67d9675Seschrock /* 35876809eb4eSEric Schrock * Update the stored path or FRU for this vdev. Dirty the vdev configuration, 35886809eb4eSEric Schrock * relying on spa_vdev_enter/exit() to synchronize the labels and cache. 3589c67d9675Seschrock */ 3590c67d9675Seschrock int 35916809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 35926809eb4eSEric Schrock boolean_t ispath) 3593c67d9675Seschrock { 3594c5904d13Seschrock vdev_t *vd; 3595c67d9675Seschrock uint64_t txg; 3596c67d9675Seschrock 3597c67d9675Seschrock txg = spa_vdev_enter(spa); 3598c67d9675Seschrock 35996809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3600fa94a07fSbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 3601c67d9675Seschrock 36020e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 36030e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 36040e34b6a7Sbonwick 36056809eb4eSEric Schrock if (ispath) { 36066809eb4eSEric Schrock spa_strfree(vd->vdev_path); 36076809eb4eSEric Schrock vd->vdev_path = spa_strdup(value); 36086809eb4eSEric Schrock } else { 36096809eb4eSEric Schrock if (vd->vdev_fru != NULL) 36106809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 36116809eb4eSEric Schrock vd->vdev_fru = spa_strdup(value); 36126809eb4eSEric Schrock } 3613c67d9675Seschrock 3614c67d9675Seschrock vdev_config_dirty(vd->vdev_top); 3615c67d9675Seschrock 3616c67d9675Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 3617c67d9675Seschrock } 3618c67d9675Seschrock 36196809eb4eSEric Schrock int 36206809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 36216809eb4eSEric Schrock { 36226809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 36236809eb4eSEric Schrock } 36246809eb4eSEric Schrock 36256809eb4eSEric Schrock int 36266809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 36276809eb4eSEric Schrock { 36286809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 36296809eb4eSEric Schrock } 36306809eb4eSEric Schrock 3631fa9e4066Sahrens /* 3632fa9e4066Sahrens * ========================================================================== 3633fa9e4066Sahrens * SPA Scrubbing 3634fa9e4066Sahrens * ========================================================================== 3635fa9e4066Sahrens */ 3636fa9e4066Sahrens 3637ea8dc4b6Seschrock int 3638088f3894Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3639fa9e4066Sahrens { 3640e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 3641bb8b5132Sek 3642fa9e4066Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3643fa9e4066Sahrens return (ENOTSUP); 3644fa9e4066Sahrens 3645fa9e4066Sahrens /* 3646088f3894Sahrens * If a resilver was requested, but there is no DTL on a 3647088f3894Sahrens * writeable leaf device, we have nothing to do. 3648fa9e4066Sahrens */ 3649088f3894Sahrens if (type == POOL_SCRUB_RESILVER && 3650088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 3651088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3652ea8dc4b6Seschrock return (0); 3653ea8dc4b6Seschrock } 3654fa9e4066Sahrens 3655088f3894Sahrens if (type == POOL_SCRUB_EVERYTHING && 3656088f3894Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 3657088f3894Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 3658088f3894Sahrens return (EBUSY); 3659fa9e4066Sahrens 3660088f3894Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 3661088f3894Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 3662088f3894Sahrens } else if (type == POOL_SCRUB_NONE) { 3663088f3894Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 3664ea8dc4b6Seschrock } else { 3665088f3894Sahrens return (EINVAL); 3666fa9e4066Sahrens } 3667fa9e4066Sahrens } 3668fa9e4066Sahrens 3669ea8dc4b6Seschrock /* 3670ea8dc4b6Seschrock * ========================================================================== 3671ea8dc4b6Seschrock * SPA async task processing 3672ea8dc4b6Seschrock * ========================================================================== 3673ea8dc4b6Seschrock */ 3674ea8dc4b6Seschrock 3675ea8dc4b6Seschrock static void 36763d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3677fa9e4066Sahrens { 367849cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 367949cf58c0SBrendan Gregg - Sun Microsystems vd->vdev_remove_wanted = 0; 368049cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 3681e14bb325SJeff Bonwick vdev_clear(spa, vd); 3682e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3683ea8dc4b6Seschrock } 368449cf58c0SBrendan Gregg - Sun Microsystems 3685e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 368649cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 3687ea8dc4b6Seschrock } 3688fa9e4066Sahrens 3689e14bb325SJeff Bonwick static void 3690e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 3691e14bb325SJeff Bonwick { 3692e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 3693e14bb325SJeff Bonwick vd->vdev_probe_wanted = 0; 3694e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 3695e14bb325SJeff Bonwick } 3696e14bb325SJeff Bonwick 3697e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3698e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 3699e14bb325SJeff Bonwick } 3700e14bb325SJeff Bonwick 3701573ca77eSGeorge Wilson static void 3702573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 3703573ca77eSGeorge Wilson { 3704573ca77eSGeorge Wilson sysevent_id_t eid; 3705573ca77eSGeorge Wilson nvlist_t *attr; 3706573ca77eSGeorge Wilson char *physpath; 3707573ca77eSGeorge Wilson 3708573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 3709573ca77eSGeorge Wilson return; 3710573ca77eSGeorge Wilson 3711573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 3712573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 3713573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 3714573ca77eSGeorge Wilson } 3715573ca77eSGeorge Wilson 3716573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 3717573ca77eSGeorge Wilson return; 3718573ca77eSGeorge Wilson 3719573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 3720573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 3721573ca77eSGeorge Wilson 3722573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3723573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 3724573ca77eSGeorge Wilson 3725573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 3726573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 3727573ca77eSGeorge Wilson 3728573ca77eSGeorge Wilson nvlist_free(attr); 3729573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 3730573ca77eSGeorge Wilson } 3731573ca77eSGeorge Wilson 3732ea8dc4b6Seschrock static void 3733ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 3734ea8dc4b6Seschrock { 3735e14bb325SJeff Bonwick int tasks; 3736ea8dc4b6Seschrock 3737ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 3738ea8dc4b6Seschrock 3739ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3740ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 3741ea8dc4b6Seschrock spa->spa_async_tasks = 0; 3742ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3743ea8dc4b6Seschrock 37440373e76bSbonwick /* 37450373e76bSbonwick * See if the config needs to be updated. 37460373e76bSbonwick */ 37470373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 3748573ca77eSGeorge Wilson uint64_t oldsz, space_update; 3749573ca77eSGeorge Wilson 37500373e76bSbonwick mutex_enter(&spa_namespace_lock); 3751573ca77eSGeorge Wilson oldsz = spa_get_space(spa); 37520373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 3753573ca77eSGeorge Wilson space_update = spa_get_space(spa) - oldsz; 37540373e76bSbonwick mutex_exit(&spa_namespace_lock); 3755573ca77eSGeorge Wilson 3756573ca77eSGeorge Wilson /* 3757573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 3758573ca77eSGeorge Wilson * then log an internal history event. 3759573ca77eSGeorge Wilson */ 3760573ca77eSGeorge Wilson if (space_update) { 3761c8e1f6d2SMark J Musante spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 3762c8e1f6d2SMark J Musante spa, NULL, CRED(), 3763c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 3764c8e1f6d2SMark J Musante spa_name(spa), spa_get_space(spa), 3765c8e1f6d2SMark J Musante space_update); 3766573ca77eSGeorge Wilson } 37670373e76bSbonwick } 37680373e76bSbonwick 3769ea8dc4b6Seschrock /* 37703d7072f8Seschrock * See if any devices need to be marked REMOVED. 3771ea8dc4b6Seschrock */ 3772e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 3773e14bb325SJeff Bonwick spa_vdev_state_enter(spa); 37743d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 3775e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 377649cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 3777e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 377849cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 3779e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 3780e14bb325SJeff Bonwick } 3781e14bb325SJeff Bonwick 3782573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 3783573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3784573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 3785573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 3786573ca77eSGeorge Wilson } 3787573ca77eSGeorge Wilson 3788e14bb325SJeff Bonwick /* 3789e14bb325SJeff Bonwick * See if any devices need to be probed. 3790e14bb325SJeff Bonwick */ 3791e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 3792e14bb325SJeff Bonwick spa_vdev_state_enter(spa); 3793e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 3794e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 37953d7072f8Seschrock } 3796ea8dc4b6Seschrock 3797ea8dc4b6Seschrock /* 3798ea8dc4b6Seschrock * If any devices are done replacing, detach them. 3799ea8dc4b6Seschrock */ 38003d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 38013d7072f8Seschrock spa_vdev_resilver_done(spa); 3802fa9e4066Sahrens 3803ea8dc4b6Seschrock /* 3804ea8dc4b6Seschrock * Kick off a resilver. 3805ea8dc4b6Seschrock */ 3806088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 3807088f3894Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 3808ea8dc4b6Seschrock 3809ea8dc4b6Seschrock /* 3810ea8dc4b6Seschrock * Let the world know that we're done. 3811ea8dc4b6Seschrock */ 3812ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3813ea8dc4b6Seschrock spa->spa_async_thread = NULL; 3814ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 3815ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3816ea8dc4b6Seschrock thread_exit(); 3817ea8dc4b6Seschrock } 3818ea8dc4b6Seschrock 3819ea8dc4b6Seschrock void 3820ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 3821ea8dc4b6Seschrock { 3822ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3823ea8dc4b6Seschrock spa->spa_async_suspended++; 3824ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 3825ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 3826ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3827ea8dc4b6Seschrock } 3828ea8dc4b6Seschrock 3829ea8dc4b6Seschrock void 3830ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 3831ea8dc4b6Seschrock { 3832ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3833ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 3834ea8dc4b6Seschrock spa->spa_async_suspended--; 3835ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3836ea8dc4b6Seschrock } 3837ea8dc4b6Seschrock 3838ea8dc4b6Seschrock static void 3839ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 3840ea8dc4b6Seschrock { 3841ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3842ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38430373e76bSbonwick spa->spa_async_thread == NULL && 38440373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 3845ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 3846ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 3847ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3848ea8dc4b6Seschrock } 3849ea8dc4b6Seschrock 3850ea8dc4b6Seschrock void 3851ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 3852ea8dc4b6Seschrock { 3853ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 3854ea8dc4b6Seschrock spa->spa_async_tasks |= task; 3855ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 3856fa9e4066Sahrens } 3857fa9e4066Sahrens 3858fa9e4066Sahrens /* 3859fa9e4066Sahrens * ========================================================================== 3860fa9e4066Sahrens * SPA syncing routines 3861fa9e4066Sahrens * ========================================================================== 3862fa9e4066Sahrens */ 3863fa9e4066Sahrens 3864fa9e4066Sahrens static void 3865fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3866fa9e4066Sahrens { 3867fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3868fa9e4066Sahrens dmu_tx_t *tx; 3869fa9e4066Sahrens blkptr_t blk; 3870fa9e4066Sahrens uint64_t itor = 0; 3871fa9e4066Sahrens zio_t *zio; 3872fa9e4066Sahrens int error; 3873fa9e4066Sahrens uint8_t c = 1; 3874fa9e4066Sahrens 3875e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 3876fa9e4066Sahrens 3877e14bb325SJeff Bonwick while (bplist_iterate(bpl, &itor, &blk) == 0) { 3878e14bb325SJeff Bonwick ASSERT(blk.blk_birth < txg); 3879e14bb325SJeff Bonwick zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 3880e14bb325SJeff Bonwick ZIO_FLAG_MUSTSUCCEED)); 3881e14bb325SJeff Bonwick } 3882fa9e4066Sahrens 3883fa9e4066Sahrens error = zio_wait(zio); 3884fa9e4066Sahrens ASSERT3U(error, ==, 0); 3885fa9e4066Sahrens 3886fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3887fa9e4066Sahrens bplist_vacate(bpl, tx); 3888fa9e4066Sahrens 3889fa9e4066Sahrens /* 3890fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 3891fa9e4066Sahrens * (Usually only the first block is needed.) 3892fa9e4066Sahrens */ 3893fa9e4066Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3894fa9e4066Sahrens dmu_tx_commit(tx); 3895fa9e4066Sahrens } 3896fa9e4066Sahrens 3897fa9e4066Sahrens static void 389899653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 3899fa9e4066Sahrens { 3900fa9e4066Sahrens char *packed = NULL; 3901f7991ba4STim Haley size_t bufsize; 3902fa9e4066Sahrens size_t nvsize = 0; 3903fa9e4066Sahrens dmu_buf_t *db; 3904fa9e4066Sahrens 390599653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 3906fa9e4066Sahrens 3907f7991ba4STim Haley /* 3908f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 3909f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 3910f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 3911f7991ba4STim Haley */ 3912f7991ba4STim Haley bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 3913f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 3914fa9e4066Sahrens 391599653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 3916ea8dc4b6Seschrock KM_SLEEP) == 0); 3917f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 3918fa9e4066Sahrens 3919f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 3920fa9e4066Sahrens 3921f7991ba4STim Haley kmem_free(packed, bufsize); 3922fa9e4066Sahrens 392399653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 3924fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 3925fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 3926ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 3927fa9e4066Sahrens } 3928fa9e4066Sahrens 392999653d4eSeschrock static void 3930fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 3931fa94a07fSbrendan const char *config, const char *entry) 393299653d4eSeschrock { 393399653d4eSeschrock nvlist_t *nvroot; 3934fa94a07fSbrendan nvlist_t **list; 393599653d4eSeschrock int i; 393699653d4eSeschrock 3937fa94a07fSbrendan if (!sav->sav_sync) 393899653d4eSeschrock return; 393999653d4eSeschrock 394099653d4eSeschrock /* 3941fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 3942fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 39433d7072f8Seschrock * valid and the vdevs are labeled appropriately. 394499653d4eSeschrock */ 3945fa94a07fSbrendan if (sav->sav_object == 0) { 3946fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 3947fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 3948fa94a07fSbrendan sizeof (uint64_t), tx); 394999653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 3950fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 3951fa94a07fSbrendan &sav->sav_object, tx) == 0); 395299653d4eSeschrock } 395399653d4eSeschrock 395499653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3955fa94a07fSbrendan if (sav->sav_count == 0) { 3956fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 395799653d4eSeschrock } else { 3958fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 3959fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3960fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 3961fa94a07fSbrendan B_FALSE, B_FALSE, B_TRUE); 3962fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 3963fa94a07fSbrendan sav->sav_count) == 0); 3964fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 3965fa94a07fSbrendan nvlist_free(list[i]); 3966fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 396799653d4eSeschrock } 396899653d4eSeschrock 3969fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 397006eeb2adSek nvlist_free(nvroot); 397199653d4eSeschrock 3972fa94a07fSbrendan sav->sav_sync = B_FALSE; 397399653d4eSeschrock } 397499653d4eSeschrock 397599653d4eSeschrock static void 397699653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 397799653d4eSeschrock { 397899653d4eSeschrock nvlist_t *config; 397999653d4eSeschrock 3980e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 398199653d4eSeschrock return; 398299653d4eSeschrock 3983e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3984e14bb325SJeff Bonwick 3985e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 3986e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 3987e14bb325SJeff Bonwick 3988e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 398999653d4eSeschrock 399099653d4eSeschrock if (spa->spa_config_syncing) 399199653d4eSeschrock nvlist_free(spa->spa_config_syncing); 399299653d4eSeschrock spa->spa_config_syncing = config; 399399653d4eSeschrock 399499653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 399599653d4eSeschrock } 399699653d4eSeschrock 3997990b4856Slling /* 3998990b4856Slling * Set zpool properties. 3999990b4856Slling */ 4000b1b8ab34Slling static void 4001ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 4002b1b8ab34Slling { 4003b1b8ab34Slling spa_t *spa = arg1; 4004b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 4005990b4856Slling nvlist_t *nvp = arg2; 4006990b4856Slling nvpair_t *elem; 40073d7072f8Seschrock uint64_t intval; 4008c5904d13Seschrock char *strval; 4009990b4856Slling zpool_prop_t prop; 4010990b4856Slling const char *propname; 4011990b4856Slling zprop_type_t proptype; 4012b1b8ab34Slling 4013e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 4014e14bb325SJeff Bonwick 4015990b4856Slling elem = NULL; 4016990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 4017990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 4018990b4856Slling case ZPOOL_PROP_VERSION: 4019990b4856Slling /* 4020990b4856Slling * Only set version for non-zpool-creation cases 4021990b4856Slling * (set/import). spa_create() needs special care 4022990b4856Slling * for version setting. 4023990b4856Slling */ 4024990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 4025990b4856Slling VERIFY(nvpair_value_uint64(elem, 4026990b4856Slling &intval) == 0); 4027990b4856Slling ASSERT(intval <= SPA_VERSION); 4028990b4856Slling ASSERT(intval >= spa_version(spa)); 4029990b4856Slling spa->spa_uberblock.ub_version = intval; 4030990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 4031990b4856Slling } 4032ecd6cf80Smarks break; 4033990b4856Slling 4034990b4856Slling case ZPOOL_PROP_ALTROOT: 4035990b4856Slling /* 4036990b4856Slling * 'altroot' is a non-persistent property. It should 4037990b4856Slling * have been set temporarily at creation or import time. 4038990b4856Slling */ 4039990b4856Slling ASSERT(spa->spa_root != NULL); 4040b1b8ab34Slling break; 40413d7072f8Seschrock 40422f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4043990b4856Slling /* 4044379c004dSEric Schrock * 'cachefile' is also a non-persisitent property. 4045990b4856Slling */ 40463d7072f8Seschrock break; 4047990b4856Slling default: 4048990b4856Slling /* 4049990b4856Slling * Set pool property values in the poolprops mos object. 4050990b4856Slling */ 4051990b4856Slling if (spa->spa_pool_props_object == 0) { 4052990b4856Slling objset_t *mos = spa->spa_meta_objset; 4053990b4856Slling 4054990b4856Slling VERIFY((spa->spa_pool_props_object = 4055990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 4056990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 4057990b4856Slling 4058990b4856Slling VERIFY(zap_update(mos, 4059990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 4060990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 4061990b4856Slling == 0); 4062990b4856Slling } 4063990b4856Slling 4064990b4856Slling /* normalize the property name */ 4065990b4856Slling propname = zpool_prop_to_name(prop); 4066990b4856Slling proptype = zpool_prop_get_type(prop); 4067990b4856Slling 4068990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 4069990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 4070990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 4071990b4856Slling VERIFY(zap_update(mos, 4072990b4856Slling spa->spa_pool_props_object, propname, 4073990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 4074990b4856Slling 4075990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 4076990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 4077990b4856Slling 4078990b4856Slling if (proptype == PROP_TYPE_INDEX) { 4079990b4856Slling const char *unused; 4080990b4856Slling VERIFY(zpool_prop_index_to_string( 4081990b4856Slling prop, intval, &unused) == 0); 4082990b4856Slling } 4083990b4856Slling VERIFY(zap_update(mos, 4084990b4856Slling spa->spa_pool_props_object, propname, 4085990b4856Slling 8, 1, &intval, tx) == 0); 4086990b4856Slling } else { 4087990b4856Slling ASSERT(0); /* not allowed */ 4088990b4856Slling } 4089990b4856Slling 40900a4e9518Sgw switch (prop) { 40910a4e9518Sgw case ZPOOL_PROP_DELEGATION: 4092990b4856Slling spa->spa_delegation = intval; 40930a4e9518Sgw break; 40940a4e9518Sgw case ZPOOL_PROP_BOOTFS: 4095990b4856Slling spa->spa_bootfs = intval; 40960a4e9518Sgw break; 40970a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 40980a4e9518Sgw spa->spa_failmode = intval; 40990a4e9518Sgw break; 4100573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 4101573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 4102573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4103573ca77eSGeorge Wilson break; 41040a4e9518Sgw default: 41050a4e9518Sgw break; 41060a4e9518Sgw } 4107990b4856Slling } 4108990b4856Slling 4109990b4856Slling /* log internal history if this is not a zpool create */ 4110990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 4111990b4856Slling tx->tx_txg != TXG_INITIAL) { 4112990b4856Slling spa_history_internal_log(LOG_POOL_PROPSET, 4113990b4856Slling spa, tx, cr, "%s %lld %s", 4114e14bb325SJeff Bonwick nvpair_name(elem), intval, spa_name(spa)); 4115b1b8ab34Slling } 4116b1b8ab34Slling } 4117e14bb325SJeff Bonwick 4118e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 4119b1b8ab34Slling } 4120b1b8ab34Slling 4121fa9e4066Sahrens /* 4122fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4123fa9e4066Sahrens * part of the process, so we iterate until it converges. 4124fa9e4066Sahrens */ 4125fa9e4066Sahrens void 4126fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 4127fa9e4066Sahrens { 4128fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4129fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 4130fa9e4066Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41310373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 4132fa9e4066Sahrens vdev_t *vd; 4133fa9e4066Sahrens dmu_tx_t *tx; 4134fa9e4066Sahrens int dirty_vdevs; 4135e14bb325SJeff Bonwick int error; 4136fa9e4066Sahrens 4137fa9e4066Sahrens /* 4138fa9e4066Sahrens * Lock out configuration changes. 4139fa9e4066Sahrens */ 4140e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4141fa9e4066Sahrens 4142fa9e4066Sahrens spa->spa_syncing_txg = txg; 4143fa9e4066Sahrens spa->spa_sync_pass = 0; 4144fa9e4066Sahrens 4145e14bb325SJeff Bonwick /* 4146e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 4147e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 4148e14bb325SJeff Bonwick */ 4149e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 41508ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 41518ad4d6ddSJeff Bonwick /* 41528ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 41538ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 41548ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 41558ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 41568ad4d6ddSJeff Bonwick * into the root vdev tree. 41578ad4d6ddSJeff Bonwick */ 41588ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 41598ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 41608ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 41618ad4d6ddSJeff Bonwick vdev_state_clean(vd); 41628ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 41638ad4d6ddSJeff Bonwick } 41648ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 41658ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 4166e14bb325SJeff Bonwick } 4167e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 4168e14bb325SJeff Bonwick 4169ea8dc4b6Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4170fa9e4066Sahrens 417199653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 417299653d4eSeschrock 417399653d4eSeschrock /* 4174e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 417599653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 417699653d4eSeschrock */ 4177e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 4178e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 417999653d4eSeschrock int i; 418099653d4eSeschrock 418199653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 418299653d4eSeschrock vd = rvd->vdev_child[i]; 418399653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 418499653d4eSeschrock break; 418599653d4eSeschrock } 418699653d4eSeschrock if (i == rvd->vdev_children) { 418799653d4eSeschrock spa->spa_deflate = TRUE; 418899653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 418999653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 419099653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 419199653d4eSeschrock } 419299653d4eSeschrock } 419399653d4eSeschrock 4194088f3894Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 4195088f3894Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 4196088f3894Sahrens dsl_pool_create_origin(dp, tx); 4197088f3894Sahrens 4198088f3894Sahrens /* Keeping the origin open increases spa_minref */ 4199088f3894Sahrens spa->spa_minref += 3; 4200088f3894Sahrens } 4201088f3894Sahrens 4202088f3894Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 4203088f3894Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 4204088f3894Sahrens dsl_pool_upgrade_clones(dp, tx); 4205088f3894Sahrens } 4206088f3894Sahrens 4207fa9e4066Sahrens /* 4208fa9e4066Sahrens * If anything has changed in this txg, push the deferred frees 4209fa9e4066Sahrens * from the previous txg. If not, leave them alone so that we 4210fa9e4066Sahrens * don't generate work on an otherwise idle system. 4211fa9e4066Sahrens */ 4212fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 42131615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 42141615a317Sek !txg_list_empty(&dp->dp_sync_tasks, txg)) 4215fa9e4066Sahrens spa_sync_deferred_frees(spa, txg); 4216fa9e4066Sahrens 4217fa9e4066Sahrens /* 4218fa9e4066Sahrens * Iterate to convergence. 4219fa9e4066Sahrens */ 4220fa9e4066Sahrens do { 4221fa9e4066Sahrens spa->spa_sync_pass++; 4222fa9e4066Sahrens 4223fa9e4066Sahrens spa_sync_config_object(spa, tx); 4224fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 4225fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 4226fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 4227fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 4228ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 4229fa9e4066Sahrens dsl_pool_sync(dp, txg); 4230fa9e4066Sahrens 4231fa9e4066Sahrens dirty_vdevs = 0; 4232fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4233fa9e4066Sahrens vdev_sync(vd, txg); 4234fa9e4066Sahrens dirty_vdevs++; 4235fa9e4066Sahrens } 4236fa9e4066Sahrens 4237fa9e4066Sahrens bplist_sync(bpl, tx); 4238fa9e4066Sahrens } while (dirty_vdevs); 4239fa9e4066Sahrens 4240fa9e4066Sahrens bplist_close(bpl); 4241fa9e4066Sahrens 4242fa9e4066Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4243fa9e4066Sahrens 4244fa9e4066Sahrens /* 4245fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4246fa9e4066Sahrens * to commit the transaction group. 42470373e76bSbonwick * 424817f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 424917f17c2dSbonwick * random top-level vdevs that are known to be visible in the 4250e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 4251e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 42520373e76bSbonwick */ 4253e14bb325SJeff Bonwick for (;;) { 4254e14bb325SJeff Bonwick /* 4255e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 4256e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 4257e14bb325SJeff Bonwick */ 4258e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4259e14bb325SJeff Bonwick 4260e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 4261e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 4262e14bb325SJeff Bonwick int svdcount = 0; 4263e14bb325SJeff Bonwick int children = rvd->vdev_children; 4264e14bb325SJeff Bonwick int c0 = spa_get_random(children); 4265e14bb325SJeff Bonwick 4266573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 4267e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 4268e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 4269e14bb325SJeff Bonwick continue; 4270e14bb325SJeff Bonwick svd[svdcount++] = vd; 4271e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 4272e14bb325SJeff Bonwick break; 4273e14bb325SJeff Bonwick } 42748956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 42758956713aSEric Schrock if (error != 0) 42768956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 42778956713aSEric Schrock B_TRUE); 4278e14bb325SJeff Bonwick } else { 4279e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 42808956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 42818956713aSEric Schrock if (error != 0) 42828956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 42838956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 42840373e76bSbonwick } 4285e14bb325SJeff Bonwick 4286e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 4287e14bb325SJeff Bonwick 4288e14bb325SJeff Bonwick if (error == 0) 4289e14bb325SJeff Bonwick break; 4290e14bb325SJeff Bonwick zio_suspend(spa, NULL); 4291e14bb325SJeff Bonwick zio_resume_wait(spa); 42920373e76bSbonwick } 429399653d4eSeschrock dmu_tx_commit(tx); 429499653d4eSeschrock 42950373e76bSbonwick /* 42960373e76bSbonwick * Clear the dirty config list. 4297fa9e4066Sahrens */ 4298e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 42990373e76bSbonwick vdev_config_clean(vd); 43000373e76bSbonwick 43010373e76bSbonwick /* 43020373e76bSbonwick * Now that the new config has synced transactionally, 43030373e76bSbonwick * let it become visible to the config cache. 43040373e76bSbonwick */ 43050373e76bSbonwick if (spa->spa_config_syncing != NULL) { 43060373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 43070373e76bSbonwick spa->spa_config_txg = txg; 43080373e76bSbonwick spa->spa_config_syncing = NULL; 43090373e76bSbonwick } 4310fa9e4066Sahrens 4311fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 4312fa9e4066Sahrens 4313fa9e4066Sahrens /* 4314fa9e4066Sahrens * Clean up the ZIL records for the synced txg. 4315fa9e4066Sahrens */ 4316fa9e4066Sahrens dsl_pool_zil_clean(dp); 4317fa9e4066Sahrens 4318fa9e4066Sahrens /* 4319fa9e4066Sahrens * Update usable space statistics. 4320fa9e4066Sahrens */ 4321fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4322fa9e4066Sahrens vdev_sync_done(vd, txg); 4323fa9e4066Sahrens 4324fa9e4066Sahrens /* 4325fa9e4066Sahrens * It had better be the case that we didn't dirty anything 432699653d4eSeschrock * since vdev_config_sync(). 4327fa9e4066Sahrens */ 4328fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4329fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4330fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4331fa9e4066Sahrens ASSERT(bpl->bpl_queue == NULL); 4332fa9e4066Sahrens 4333e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 4334ea8dc4b6Seschrock 4335ea8dc4b6Seschrock /* 4336ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 4337ea8dc4b6Seschrock */ 4338ea8dc4b6Seschrock spa_async_dispatch(spa); 4339fa9e4066Sahrens } 4340fa9e4066Sahrens 4341fa9e4066Sahrens /* 4342fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4343fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4344fa9e4066Sahrens * sync. 4345fa9e4066Sahrens */ 4346fa9e4066Sahrens void 4347fa9e4066Sahrens spa_sync_allpools(void) 4348fa9e4066Sahrens { 4349fa9e4066Sahrens spa_t *spa = NULL; 4350fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4351fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 4352e14bb325SJeff Bonwick if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4353fa9e4066Sahrens continue; 4354fa9e4066Sahrens spa_open_ref(spa, FTAG); 4355fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4356fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4357fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4358fa9e4066Sahrens spa_close(spa, FTAG); 4359fa9e4066Sahrens } 4360fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4361fa9e4066Sahrens } 4362fa9e4066Sahrens 4363fa9e4066Sahrens /* 4364fa9e4066Sahrens * ========================================================================== 4365fa9e4066Sahrens * Miscellaneous routines 4366fa9e4066Sahrens * ========================================================================== 4367fa9e4066Sahrens */ 4368fa9e4066Sahrens 4369fa9e4066Sahrens /* 4370fa9e4066Sahrens * Remove all pools in the system. 4371fa9e4066Sahrens */ 4372fa9e4066Sahrens void 4373fa9e4066Sahrens spa_evict_all(void) 4374fa9e4066Sahrens { 4375fa9e4066Sahrens spa_t *spa; 4376fa9e4066Sahrens 4377fa9e4066Sahrens /* 4378fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 4379fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 4380fa9e4066Sahrens */ 4381fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4382fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 4383fa9e4066Sahrens /* 4384ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 4385ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 4386ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 4387fa9e4066Sahrens */ 4388fa9e4066Sahrens spa_open_ref(spa, FTAG); 4389fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4390ea8dc4b6Seschrock spa_async_suspend(spa); 4391fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4392fa9e4066Sahrens spa_close(spa, FTAG); 4393fa9e4066Sahrens 4394fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4395fa9e4066Sahrens spa_unload(spa); 4396fa9e4066Sahrens spa_deactivate(spa); 4397fa9e4066Sahrens } 4398fa9e4066Sahrens spa_remove(spa); 4399fa9e4066Sahrens } 4400fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4401fa9e4066Sahrens } 4402ea8dc4b6Seschrock 4403ea8dc4b6Seschrock vdev_t * 44046809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 4405ea8dc4b6Seschrock { 4406c5904d13Seschrock vdev_t *vd; 4407c5904d13Seschrock int i; 4408c5904d13Seschrock 4409c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 4410c5904d13Seschrock return (vd); 4411c5904d13Seschrock 44126809eb4eSEric Schrock if (aux) { 4413c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 4414c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 44156809eb4eSEric Schrock if (vd->vdev_guid == guid) 44166809eb4eSEric Schrock return (vd); 44176809eb4eSEric Schrock } 44186809eb4eSEric Schrock 44196809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 44206809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 4421c5904d13Seschrock if (vd->vdev_guid == guid) 4422c5904d13Seschrock return (vd); 4423c5904d13Seschrock } 4424c5904d13Seschrock } 4425c5904d13Seschrock 4426c5904d13Seschrock return (NULL); 4427ea8dc4b6Seschrock } 4428eaca9bbdSeschrock 4429eaca9bbdSeschrock void 4430990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 4431eaca9bbdSeschrock { 4432e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4433eaca9bbdSeschrock 4434eaca9bbdSeschrock /* 4435eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 4436eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 4437eaca9bbdSeschrock * possible. 4438eaca9bbdSeschrock */ 4439e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 4440990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 4441eaca9bbdSeschrock 4442990b4856Slling spa->spa_uberblock.ub_version = version; 4443eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 4444eaca9bbdSeschrock 4445e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 444699653d4eSeschrock 444799653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 444899653d4eSeschrock } 444999653d4eSeschrock 445099653d4eSeschrock boolean_t 445199653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 445299653d4eSeschrock { 445399653d4eSeschrock int i; 445439c23413Seschrock uint64_t spareguid; 4455fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 445699653d4eSeschrock 4457fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 4458fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 445999653d4eSeschrock return (B_TRUE); 446099653d4eSeschrock 4461fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 4462fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 4463fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 446439c23413Seschrock return (B_TRUE); 446539c23413Seschrock } 446639c23413Seschrock 446799653d4eSeschrock return (B_FALSE); 4468eaca9bbdSeschrock } 4469b1b8ab34Slling 447089a89ebfSlling /* 447189a89ebfSlling * Check if a pool has an active shared spare device. 447289a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 447389a89ebfSlling */ 447489a89ebfSlling static boolean_t 447589a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 447689a89ebfSlling { 447789a89ebfSlling int i, refcnt; 447889a89ebfSlling uint64_t pool; 447989a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 448089a89ebfSlling 448189a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 448289a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 448389a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 448489a89ebfSlling refcnt > 2) 448589a89ebfSlling return (B_TRUE); 448689a89ebfSlling } 448789a89ebfSlling 448889a89ebfSlling return (B_FALSE); 448989a89ebfSlling } 449089a89ebfSlling 44913d7072f8Seschrock /* 44923d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44933d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44943d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44953d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44963d7072f8Seschrock * or zdb as real changes. 44973d7072f8Seschrock */ 44983d7072f8Seschrock void 44993d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 45003d7072f8Seschrock { 45013d7072f8Seschrock #ifdef _KERNEL 45023d7072f8Seschrock sysevent_t *ev; 45033d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 45043d7072f8Seschrock sysevent_value_t value; 45053d7072f8Seschrock sysevent_id_t eid; 45063d7072f8Seschrock 45073d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 45083d7072f8Seschrock SE_SLEEP); 45093d7072f8Seschrock 45103d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 45113d7072f8Seschrock value.value.sv_string = spa_name(spa); 45123d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 45133d7072f8Seschrock goto done; 45143d7072f8Seschrock 45153d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 45163d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 45173d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 45183d7072f8Seschrock goto done; 45193d7072f8Seschrock 45203d7072f8Seschrock if (vd) { 45213d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 45223d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 45233d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 45243d7072f8Seschrock SE_SLEEP) != 0) 45253d7072f8Seschrock goto done; 45263d7072f8Seschrock 45273d7072f8Seschrock if (vd->vdev_path) { 45283d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 45293d7072f8Seschrock value.value.sv_string = vd->vdev_path; 45303d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 45313d7072f8Seschrock &value, SE_SLEEP) != 0) 45323d7072f8Seschrock goto done; 45333d7072f8Seschrock } 45343d7072f8Seschrock } 45353d7072f8Seschrock 4536b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 4537b01c3b58Seschrock goto done; 4538b01c3b58Seschrock attr = NULL; 4539b01c3b58Seschrock 45403d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 45413d7072f8Seschrock 45423d7072f8Seschrock done: 45433d7072f8Seschrock if (attr) 45443d7072f8Seschrock sysevent_free_attr(attr); 45453d7072f8Seschrock sysevent_free(ev); 45463d7072f8Seschrock #endif 45473d7072f8Seschrock } 4548