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 /* 2398d1cbfeSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens /* 27fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 28fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 29fa9e4066Sahrens * pool. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <sys/zfs_context.h> 33ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 34fa9e4066Sahrens #include <sys/spa_impl.h> 35fa9e4066Sahrens #include <sys/zio.h> 36fa9e4066Sahrens #include <sys/zio_checksum.h> 37fa9e4066Sahrens #include <sys/dmu.h> 38fa9e4066Sahrens #include <sys/dmu_tx.h> 39fa9e4066Sahrens #include <sys/zap.h> 40fa9e4066Sahrens #include <sys/zil.h> 41b24ab676SJeff Bonwick #include <sys/ddt.h> 42fa9e4066Sahrens #include <sys/vdev_impl.h> 43fa9e4066Sahrens #include <sys/metaslab.h> 4488ecc943SGeorge Wilson #include <sys/metaslab_impl.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> 60e7cbe64fSgw #include <sys/spa_boot.h> 61573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 623f9d6ad7SLin Ling #include <sys/dsl_scan.h> 63fa9e4066Sahrens 645679c89fSjv #ifdef _KERNEL 65dedec472SJack Meng #include <sys/bootprops.h> 6635a5a358SJonathan Adams #include <sys/callb.h> 6735a5a358SJonathan Adams #include <sys/cpupart.h> 6835a5a358SJonathan Adams #include <sys/pool.h> 6935a5a358SJonathan Adams #include <sys/sysdc.h> 7035a5a358SJonathan Adams #include <sys/zone.h> 715679c89fSjv #endif /* _KERNEL */ 725679c89fSjv 73990b4856Slling #include "zfs_prop.h" 74b7b97454Sperrin #include "zfs_comutil.h" 75990b4856Slling 7635a5a358SJonathan Adams typedef enum zti_modes { 772e0c549eSJonathan Adams zti_mode_fixed, /* value is # of threads (min 1) */ 782e0c549eSJonathan Adams zti_mode_online_percent, /* value is % of online CPUs */ 7935a5a358SJonathan Adams zti_mode_batch, /* cpu-intensive; value is ignored */ 8080eb36f2SGeorge Wilson zti_mode_null, /* don't create a taskq */ 812e0c549eSJonathan Adams zti_nmodes 8235a5a358SJonathan Adams } zti_modes_t; 83416e0cd8Sek 8480eb36f2SGeorge Wilson #define ZTI_FIX(n) { zti_mode_fixed, (n) } 8580eb36f2SGeorge Wilson #define ZTI_PCT(n) { zti_mode_online_percent, (n) } 8635a5a358SJonathan Adams #define ZTI_BATCH { zti_mode_batch, 0 } 8780eb36f2SGeorge Wilson #define ZTI_NULL { zti_mode_null, 0 } 882e0c549eSJonathan Adams 8980eb36f2SGeorge Wilson #define ZTI_ONE ZTI_FIX(1) 902e0c549eSJonathan Adams 912e0c549eSJonathan Adams typedef struct zio_taskq_info { 9280eb36f2SGeorge Wilson enum zti_modes zti_mode; 9380eb36f2SGeorge Wilson uint_t zti_value; 942e0c549eSJonathan Adams } zio_taskq_info_t; 952e0c549eSJonathan Adams 962e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 9735a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 982e0c549eSJonathan Adams }; 992e0c549eSJonathan Adams 10080eb36f2SGeorge Wilson /* 10180eb36f2SGeorge Wilson * Define the taskq threads for the following I/O types: 10280eb36f2SGeorge Wilson * NULL, READ, WRITE, FREE, CLAIM, and IOCTL 10380eb36f2SGeorge Wilson */ 10480eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 10580eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 10680eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 10735a5a358SJonathan Adams { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, 10835a5a358SJonathan Adams { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) }, 109*c7cd2421SGeorge Wilson { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11080eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11180eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 1122e0c549eSJonathan Adams }; 1132e0c549eSJonathan Adams 1143f9d6ad7SLin Ling static dsl_syncfunc_t spa_sync_props; 11589a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1161195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1171195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1181195e687SMark J Musante char **ereport); 119990b4856Slling 12035a5a358SJonathan Adams uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 12135a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 12235a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 12335a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 12435a5a358SJonathan Adams 12535a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 12635a5a358SJonathan Adams 12735a5a358SJonathan Adams /* 12835a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 12935a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 13035a5a358SJonathan Adams */ 13135a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 13235a5a358SJonathan Adams 133990b4856Slling /* 134990b4856Slling * ========================================================================== 135990b4856Slling * SPA properties routines 136990b4856Slling * ========================================================================== 137990b4856Slling */ 138990b4856Slling 139990b4856Slling /* 140990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 141990b4856Slling */ 1429d82f4f6Slling static void 143990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 144990b4856Slling uint64_t intval, zprop_source_t src) 145990b4856Slling { 146990b4856Slling const char *propname = zpool_prop_to_name(prop); 147990b4856Slling nvlist_t *propval; 148990b4856Slling 1499d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1509d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 151990b4856Slling 1529d82f4f6Slling if (strval != NULL) 1539d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1549d82f4f6Slling else 1559d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 156990b4856Slling 1579d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 158990b4856Slling nvlist_free(propval); 159990b4856Slling } 160990b4856Slling 161990b4856Slling /* 162990b4856Slling * Get property values from the spa configuration. 163990b4856Slling */ 1649d82f4f6Slling static void 165990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 166990b4856Slling { 167379c004dSEric Schrock uint64_t size; 168485bbbf5SGeorge Wilson uint64_t alloc; 169990b4856Slling uint64_t cap, version; 170990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 171c5904d13Seschrock spa_config_dirent_t *dp; 172990b4856Slling 173e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 174e14bb325SJeff Bonwick 175379c004dSEric Schrock if (spa->spa_root_vdev != NULL) { 176485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 177b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 178379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 179379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 180485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 181485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 182485bbbf5SGeorge Wilson size - alloc, src); 183379c004dSEric Schrock 184485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 185379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 186379c004dSEric Schrock 187b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 188b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 189b24ab676SJeff Bonwick 190379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 191379c004dSEric Schrock spa->spa_root_vdev->vdev_state, src); 192379c004dSEric Schrock 193379c004dSEric Schrock version = spa_version(spa); 194379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 195379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 196379c004dSEric Schrock else 197379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 198379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 199379c004dSEric Schrock } 200990b4856Slling 2019d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 202990b4856Slling 2039d82f4f6Slling if (spa->spa_root != NULL) 2049d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2059d82f4f6Slling 0, ZPROP_SRC_LOCAL); 206990b4856Slling 207c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 208c5904d13Seschrock if (dp->scd_path == NULL) { 2099d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 210c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 211c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2129d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 213c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2142f8aaab3Seschrock } 2152f8aaab3Seschrock } 216990b4856Slling } 217990b4856Slling 218990b4856Slling /* 219990b4856Slling * Get zpool property values. 220990b4856Slling */ 221990b4856Slling int 222990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 223990b4856Slling { 224b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 225990b4856Slling zap_cursor_t zc; 226990b4856Slling zap_attribute_t za; 227990b4856Slling int err; 228990b4856Slling 2299d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 230990b4856Slling 231e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 232e14bb325SJeff Bonwick 233990b4856Slling /* 234990b4856Slling * Get properties from the spa config. 235990b4856Slling */ 2369d82f4f6Slling spa_prop_get_config(spa, nvp); 237990b4856Slling 238990b4856Slling /* If no pool property object, no more prop to get. */ 239afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 240990b4856Slling mutex_exit(&spa->spa_props_lock); 241990b4856Slling return (0); 242990b4856Slling } 243990b4856Slling 244990b4856Slling /* 245990b4856Slling * Get properties from the MOS pool property object. 246990b4856Slling */ 247990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 248990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 249990b4856Slling zap_cursor_advance(&zc)) { 250990b4856Slling uint64_t intval = 0; 251990b4856Slling char *strval = NULL; 252990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 253990b4856Slling zpool_prop_t prop; 254990b4856Slling 255990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 256990b4856Slling continue; 257990b4856Slling 258990b4856Slling switch (za.za_integer_length) { 259990b4856Slling case 8: 260990b4856Slling /* integer property */ 261990b4856Slling if (za.za_first_integer != 262990b4856Slling zpool_prop_default_numeric(prop)) 263990b4856Slling src = ZPROP_SRC_LOCAL; 264990b4856Slling 265990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 266990b4856Slling dsl_pool_t *dp; 267990b4856Slling dsl_dataset_t *ds = NULL; 268990b4856Slling 269990b4856Slling dp = spa_get_dsl(spa); 270990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 271745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 272745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 273990b4856Slling rw_exit(&dp->dp_config_rwlock); 274990b4856Slling break; 275990b4856Slling } 276990b4856Slling 277990b4856Slling strval = kmem_alloc( 278990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 279990b4856Slling KM_SLEEP); 280990b4856Slling dsl_dataset_name(ds, strval); 281745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 282990b4856Slling rw_exit(&dp->dp_config_rwlock); 283990b4856Slling } else { 284990b4856Slling strval = NULL; 285990b4856Slling intval = za.za_first_integer; 286990b4856Slling } 287990b4856Slling 2889d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 289990b4856Slling 290990b4856Slling if (strval != NULL) 291990b4856Slling kmem_free(strval, 292990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 293990b4856Slling 294990b4856Slling break; 295990b4856Slling 296990b4856Slling case 1: 297990b4856Slling /* string property */ 298990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 299990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 300990b4856Slling za.za_name, 1, za.za_num_integers, strval); 301990b4856Slling if (err) { 302990b4856Slling kmem_free(strval, za.za_num_integers); 303990b4856Slling break; 304990b4856Slling } 3059d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 306990b4856Slling kmem_free(strval, za.za_num_integers); 307990b4856Slling break; 308990b4856Slling 309990b4856Slling default: 310990b4856Slling break; 311990b4856Slling } 312990b4856Slling } 313990b4856Slling zap_cursor_fini(&zc); 314990b4856Slling mutex_exit(&spa->spa_props_lock); 315990b4856Slling out: 316990b4856Slling if (err && err != ENOENT) { 317990b4856Slling nvlist_free(*nvp); 3189d82f4f6Slling *nvp = NULL; 319990b4856Slling return (err); 320990b4856Slling } 321990b4856Slling 322990b4856Slling return (0); 323990b4856Slling } 324990b4856Slling 325990b4856Slling /* 326990b4856Slling * Validate the given pool properties nvlist and modify the list 327990b4856Slling * for the property values to be set. 328990b4856Slling */ 329990b4856Slling static int 330990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 331990b4856Slling { 332990b4856Slling nvpair_t *elem; 333990b4856Slling int error = 0, reset_bootfs = 0; 334990b4856Slling uint64_t objnum; 335990b4856Slling 336990b4856Slling elem = NULL; 337990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 338990b4856Slling zpool_prop_t prop; 339990b4856Slling char *propname, *strval; 340990b4856Slling uint64_t intval; 341990b4856Slling objset_t *os; 3422f8aaab3Seschrock char *slash; 343990b4856Slling 344990b4856Slling propname = nvpair_name(elem); 345990b4856Slling 346990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 347990b4856Slling return (EINVAL); 348990b4856Slling 349990b4856Slling switch (prop) { 350990b4856Slling case ZPOOL_PROP_VERSION: 351990b4856Slling error = nvpair_value_uint64(elem, &intval); 352990b4856Slling if (!error && 353990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 354990b4856Slling error = EINVAL; 355990b4856Slling break; 356990b4856Slling 357990b4856Slling case ZPOOL_PROP_DELEGATION: 358990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 359d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 360573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 361990b4856Slling error = nvpair_value_uint64(elem, &intval); 362990b4856Slling if (!error && intval > 1) 363990b4856Slling error = EINVAL; 364990b4856Slling break; 365990b4856Slling 366990b4856Slling case ZPOOL_PROP_BOOTFS: 36725f89ee2SJeff Bonwick /* 36825f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 36925f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 37025f89ee2SJeff Bonwick * the bootfs property cannot be set. 37125f89ee2SJeff Bonwick */ 372990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 373990b4856Slling error = ENOTSUP; 374990b4856Slling break; 375990b4856Slling } 376990b4856Slling 377990b4856Slling /* 37815e6edf1Sgw * Make sure the vdev config is bootable 379990b4856Slling */ 38015e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 381990b4856Slling error = ENOTSUP; 382990b4856Slling break; 383990b4856Slling } 384990b4856Slling 385990b4856Slling reset_bootfs = 1; 386990b4856Slling 387990b4856Slling error = nvpair_value_string(elem, &strval); 388990b4856Slling 389990b4856Slling if (!error) { 39015e6edf1Sgw uint64_t compress; 39115e6edf1Sgw 392990b4856Slling if (strval == NULL || strval[0] == '\0') { 393990b4856Slling objnum = zpool_prop_default_numeric( 394990b4856Slling ZPOOL_PROP_BOOTFS); 395990b4856Slling break; 396990b4856Slling } 397990b4856Slling 398503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 399990b4856Slling break; 40015e6edf1Sgw 401503ad85cSMatthew Ahrens /* Must be ZPL and not gzip compressed. */ 402503ad85cSMatthew Ahrens 403503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 404503ad85cSMatthew Ahrens error = ENOTSUP; 405503ad85cSMatthew Ahrens } else if ((error = dsl_prop_get_integer(strval, 40615e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 40715e6edf1Sgw &compress, NULL)) == 0 && 40815e6edf1Sgw !BOOTFS_COMPRESS_VALID(compress)) { 40915e6edf1Sgw error = ENOTSUP; 41015e6edf1Sgw } else { 41115e6edf1Sgw objnum = dmu_objset_id(os); 41215e6edf1Sgw } 413503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 414990b4856Slling } 415990b4856Slling break; 416e14bb325SJeff Bonwick 4170a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 4180a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 4190a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4200a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 4210a4e9518Sgw error = EINVAL; 4220a4e9518Sgw 4230a4e9518Sgw /* 4240a4e9518Sgw * This is a special case which only occurs when 4250a4e9518Sgw * the pool has completely failed. This allows 4260a4e9518Sgw * the user to change the in-core failmode property 4270a4e9518Sgw * without syncing it out to disk (I/Os might 4280a4e9518Sgw * currently be blocked). We do this by returning 4290a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 4300a4e9518Sgw * into thinking we encountered a property validation 4310a4e9518Sgw * error. 4320a4e9518Sgw */ 433e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 4340a4e9518Sgw spa->spa_failmode = intval; 4350a4e9518Sgw error = EIO; 4360a4e9518Sgw } 4370a4e9518Sgw break; 4382f8aaab3Seschrock 4392f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4402f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4412f8aaab3Seschrock break; 4422f8aaab3Seschrock 4432f8aaab3Seschrock if (strval[0] == '\0') 4442f8aaab3Seschrock break; 4452f8aaab3Seschrock 4462f8aaab3Seschrock if (strcmp(strval, "none") == 0) 4472f8aaab3Seschrock break; 4482f8aaab3Seschrock 4492f8aaab3Seschrock if (strval[0] != '/') { 4502f8aaab3Seschrock error = EINVAL; 4512f8aaab3Seschrock break; 4522f8aaab3Seschrock } 4532f8aaab3Seschrock 4542f8aaab3Seschrock slash = strrchr(strval, '/'); 4552f8aaab3Seschrock ASSERT(slash != NULL); 4562f8aaab3Seschrock 4572f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4582f8aaab3Seschrock strcmp(slash, "/..") == 0) 4592f8aaab3Seschrock error = EINVAL; 4602f8aaab3Seschrock break; 461b24ab676SJeff Bonwick 462b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 463b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 464b24ab676SJeff Bonwick error = ENOTSUP; 465b24ab676SJeff Bonwick else 466b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 467b24ab676SJeff Bonwick if (error == 0 && 468b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 469b24ab676SJeff Bonwick error = EINVAL; 470b24ab676SJeff Bonwick break; 471990b4856Slling } 472990b4856Slling 473990b4856Slling if (error) 474990b4856Slling break; 475990b4856Slling } 476990b4856Slling 477990b4856Slling if (!error && reset_bootfs) { 478990b4856Slling error = nvlist_remove(props, 479990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 480990b4856Slling 481990b4856Slling if (!error) { 482990b4856Slling error = nvlist_add_uint64(props, 483990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 484990b4856Slling } 485990b4856Slling } 486990b4856Slling 487990b4856Slling return (error); 488990b4856Slling } 489990b4856Slling 490379c004dSEric Schrock void 491379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 492379c004dSEric Schrock { 493379c004dSEric Schrock char *cachefile; 494379c004dSEric Schrock spa_config_dirent_t *dp; 495379c004dSEric Schrock 496379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 497379c004dSEric Schrock &cachefile) != 0) 498379c004dSEric Schrock return; 499379c004dSEric Schrock 500379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 501379c004dSEric Schrock KM_SLEEP); 502379c004dSEric Schrock 503379c004dSEric Schrock if (cachefile[0] == '\0') 504379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 505379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 506379c004dSEric Schrock dp->scd_path = NULL; 507379c004dSEric Schrock else 508379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 509379c004dSEric Schrock 510379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 511379c004dSEric Schrock if (need_sync) 512379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 513379c004dSEric Schrock } 514379c004dSEric Schrock 515990b4856Slling int 516990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 517990b4856Slling { 518990b4856Slling int error; 519379c004dSEric Schrock nvpair_t *elem; 520379c004dSEric Schrock boolean_t need_sync = B_FALSE; 521379c004dSEric Schrock zpool_prop_t prop; 522990b4856Slling 523990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 524990b4856Slling return (error); 525990b4856Slling 526379c004dSEric Schrock elem = NULL; 527379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 528379c004dSEric Schrock if ((prop = zpool_name_to_prop( 529379c004dSEric Schrock nvpair_name(elem))) == ZPROP_INVAL) 530379c004dSEric Schrock return (EINVAL); 531379c004dSEric Schrock 532379c004dSEric Schrock if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 533379c004dSEric Schrock continue; 534379c004dSEric Schrock 535379c004dSEric Schrock need_sync = B_TRUE; 536379c004dSEric Schrock break; 537379c004dSEric Schrock } 538379c004dSEric Schrock 539379c004dSEric Schrock if (need_sync) 540379c004dSEric Schrock return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 541379c004dSEric Schrock spa, nvp, 3)); 542379c004dSEric Schrock else 543379c004dSEric Schrock return (0); 544990b4856Slling } 545990b4856Slling 546990b4856Slling /* 547990b4856Slling * If the bootfs property value is dsobj, clear it. 548990b4856Slling */ 549990b4856Slling void 550990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 551990b4856Slling { 552990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 553990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 554990b4856Slling spa->spa_pool_props_object, 555990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 556990b4856Slling spa->spa_bootfs = 0; 557990b4856Slling } 558990b4856Slling } 559990b4856Slling 560fa9e4066Sahrens /* 561fa9e4066Sahrens * ========================================================================== 562fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 563fa9e4066Sahrens * ========================================================================== 564fa9e4066Sahrens */ 565fa9e4066Sahrens 566ea8dc4b6Seschrock static int 567ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 568ea8dc4b6Seschrock { 569ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 570ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 571ea8dc4b6Seschrock int ret; 572ea8dc4b6Seschrock 573ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 574ea8dc4b6Seschrock sizeof (zbookmark_t)); 575ea8dc4b6Seschrock 576ea8dc4b6Seschrock if (ret < 0) 577ea8dc4b6Seschrock return (-1); 578ea8dc4b6Seschrock else if (ret > 0) 579ea8dc4b6Seschrock return (1); 580ea8dc4b6Seschrock else 581ea8dc4b6Seschrock return (0); 582ea8dc4b6Seschrock } 583ea8dc4b6Seschrock 584ea8dc4b6Seschrock /* 585ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 586ea8dc4b6Seschrock * re-initializes them in the process. 587ea8dc4b6Seschrock */ 588ea8dc4b6Seschrock void 589ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 590ea8dc4b6Seschrock { 591ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 592ea8dc4b6Seschrock 593ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 594ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 595ea8dc4b6Seschrock 596ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 597ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 598ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 599ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 600ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 601ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 602ea8dc4b6Seschrock } 603ea8dc4b6Seschrock 60435a5a358SJonathan Adams static taskq_t * 60535a5a358SJonathan Adams spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode, 60635a5a358SJonathan Adams uint_t value) 607fa9e4066Sahrens { 60835a5a358SJonathan Adams uint_t flags = TASKQ_PREPOPULATE; 60935a5a358SJonathan Adams boolean_t batch = B_FALSE; 610fa9e4066Sahrens 61135a5a358SJonathan Adams switch (mode) { 61235a5a358SJonathan Adams case zti_mode_null: 61335a5a358SJonathan Adams return (NULL); /* no taskq needed */ 614fa9e4066Sahrens 61535a5a358SJonathan Adams case zti_mode_fixed: 61635a5a358SJonathan Adams ASSERT3U(value, >=, 1); 61735a5a358SJonathan Adams value = MAX(value, 1); 61835a5a358SJonathan Adams break; 619fa9e4066Sahrens 62035a5a358SJonathan Adams case zti_mode_batch: 62135a5a358SJonathan Adams batch = B_TRUE; 62235a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT; 62335a5a358SJonathan Adams value = zio_taskq_batch_pct; 62435a5a358SJonathan Adams break; 62535a5a358SJonathan Adams 62635a5a358SJonathan Adams case zti_mode_online_percent: 62735a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT; 62835a5a358SJonathan Adams break; 62935a5a358SJonathan Adams 63035a5a358SJonathan Adams default: 63135a5a358SJonathan Adams panic("unrecognized mode for %s taskq (%u:%u) in " 63235a5a358SJonathan Adams "spa_activate()", 63335a5a358SJonathan Adams name, mode, value); 63435a5a358SJonathan Adams break; 63535a5a358SJonathan Adams } 63635a5a358SJonathan Adams 63735a5a358SJonathan Adams if (zio_taskq_sysdc && spa->spa_proc != &p0) { 63835a5a358SJonathan Adams if (batch) 63935a5a358SJonathan Adams flags |= TASKQ_DC_BATCH; 64035a5a358SJonathan Adams 64135a5a358SJonathan Adams return (taskq_create_sysdc(name, value, 50, INT_MAX, 64235a5a358SJonathan Adams spa->spa_proc, zio_taskq_basedc, flags)); 64335a5a358SJonathan Adams } 64435a5a358SJonathan Adams return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX, 64535a5a358SJonathan Adams spa->spa_proc, flags)); 64635a5a358SJonathan Adams } 64735a5a358SJonathan Adams 64835a5a358SJonathan Adams static void 64935a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 65035a5a358SJonathan Adams { 651e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 652e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 65380eb36f2SGeorge Wilson const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 65480eb36f2SGeorge Wilson enum zti_modes mode = ztip->zti_mode; 65580eb36f2SGeorge Wilson uint_t value = ztip->zti_value; 6562e0c549eSJonathan Adams char name[32]; 6572e0c549eSJonathan Adams 6582e0c549eSJonathan Adams (void) snprintf(name, sizeof (name), 65980eb36f2SGeorge Wilson "%s_%s", zio_type_name[t], zio_taskq_types[q]); 6602e0c549eSJonathan Adams 66135a5a358SJonathan Adams spa->spa_zio_taskq[t][q] = 66235a5a358SJonathan Adams spa_taskq_create(spa, name, mode, value); 66335a5a358SJonathan Adams } 66435a5a358SJonathan Adams } 66535a5a358SJonathan Adams } 66635a5a358SJonathan Adams 66735a5a358SJonathan Adams #ifdef _KERNEL 66835a5a358SJonathan Adams static void 66935a5a358SJonathan Adams spa_thread(void *arg) 67035a5a358SJonathan Adams { 67135a5a358SJonathan Adams callb_cpr_t cprinfo; 6722e0c549eSJonathan Adams 67335a5a358SJonathan Adams spa_t *spa = arg; 67435a5a358SJonathan Adams user_t *pu = PTOU(curproc); 6752e0c549eSJonathan Adams 67635a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 67735a5a358SJonathan Adams spa->spa_name); 6782e0c549eSJonathan Adams 67935a5a358SJonathan Adams ASSERT(curproc != &p0); 68035a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 68135a5a358SJonathan Adams "zpool-%s", spa->spa_name); 68235a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 6832e0c549eSJonathan Adams 68435a5a358SJonathan Adams /* bind this thread to the requested psrset */ 68535a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 68635a5a358SJonathan Adams pool_lock(); 68735a5a358SJonathan Adams mutex_enter(&cpu_lock); 68835a5a358SJonathan Adams mutex_enter(&pidlock); 68935a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 69080eb36f2SGeorge Wilson 69135a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 69235a5a358SJonathan Adams 0, NULL, NULL) == 0) { 69335a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 69435a5a358SJonathan Adams } else { 69535a5a358SJonathan Adams cmn_err(CE_WARN, 69635a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 69735a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 69835a5a358SJonathan Adams } 69935a5a358SJonathan Adams 70035a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 70135a5a358SJonathan Adams mutex_exit(&pidlock); 70235a5a358SJonathan Adams mutex_exit(&cpu_lock); 70335a5a358SJonathan Adams pool_unlock(); 70435a5a358SJonathan Adams } 70535a5a358SJonathan Adams 70635a5a358SJonathan Adams if (zio_taskq_sysdc) { 70735a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 70835a5a358SJonathan Adams } 70935a5a358SJonathan Adams 71035a5a358SJonathan Adams spa->spa_proc = curproc; 71135a5a358SJonathan Adams spa->spa_did = curthread->t_did; 71235a5a358SJonathan Adams 71335a5a358SJonathan Adams spa_create_zio_taskqs(spa); 71435a5a358SJonathan Adams 71535a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 71635a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 71735a5a358SJonathan Adams 71835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 71935a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 72035a5a358SJonathan Adams 72135a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 72235a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 72335a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 72435a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 72535a5a358SJonathan Adams 72635a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 72735a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 72835a5a358SJonathan Adams spa->spa_proc = &p0; 72935a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 73035a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 73135a5a358SJonathan Adams 73235a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 73335a5a358SJonathan Adams lwp_exit(); 73435a5a358SJonathan Adams } 73535a5a358SJonathan Adams #endif 73635a5a358SJonathan Adams 73735a5a358SJonathan Adams /* 73835a5a358SJonathan Adams * Activate an uninitialized pool. 73935a5a358SJonathan Adams */ 74035a5a358SJonathan Adams static void 74135a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 74235a5a358SJonathan Adams { 74335a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 74435a5a358SJonathan Adams 74535a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 74635a5a358SJonathan Adams spa->spa_mode = mode; 74735a5a358SJonathan Adams 74835a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 74935a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 75035a5a358SJonathan Adams 75135a5a358SJonathan Adams /* Try to create a covering process */ 75235a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 75335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 75435a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 75535a5a358SJonathan Adams spa->spa_did = 0; 75635a5a358SJonathan Adams 75735a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 75835a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 75935a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 76035a5a358SJonathan Adams NULL, 0) == 0) { 76135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 76235a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 76335a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 76435a5a358SJonathan Adams &spa->spa_proc_lock); 7652e0c549eSJonathan Adams } 76635a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 76735a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 76835a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 76935a5a358SJonathan Adams } else { 77035a5a358SJonathan Adams #ifdef _KERNEL 77135a5a358SJonathan Adams cmn_err(CE_WARN, 77235a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 77335a5a358SJonathan Adams spa->spa_name); 77435a5a358SJonathan Adams #endif 775e14bb325SJeff Bonwick } 776fa9e4066Sahrens } 77735a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 77835a5a358SJonathan Adams 77935a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 78035a5a358SJonathan Adams if (spa->spa_proc == &p0) { 78135a5a358SJonathan Adams spa_create_zio_taskqs(spa); 78235a5a358SJonathan Adams } 783fa9e4066Sahrens 784e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 785e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 786e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 787e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 788fa9e4066Sahrens 789fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 790fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 791ea8dc4b6Seschrock 792ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 793ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 794ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 795ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 796ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 797ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 798fa9e4066Sahrens } 799fa9e4066Sahrens 800fa9e4066Sahrens /* 801fa9e4066Sahrens * Opposite of spa_activate(). 802fa9e4066Sahrens */ 803fa9e4066Sahrens static void 804fa9e4066Sahrens spa_deactivate(spa_t *spa) 805fa9e4066Sahrens { 806fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 807fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 808fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 80925f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 810fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 811fa9e4066Sahrens 812fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 813fa9e4066Sahrens 814e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 815e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 816fa9e4066Sahrens 817e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 818e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 81980eb36f2SGeorge Wilson if (spa->spa_zio_taskq[t][q] != NULL) 82080eb36f2SGeorge Wilson taskq_destroy(spa->spa_zio_taskq[t][q]); 821e14bb325SJeff Bonwick spa->spa_zio_taskq[t][q] = NULL; 822e14bb325SJeff Bonwick } 823fa9e4066Sahrens } 824fa9e4066Sahrens 825fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 826fa9e4066Sahrens spa->spa_normal_class = NULL; 827fa9e4066Sahrens 8288654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 8298654d025Sperrin spa->spa_log_class = NULL; 8308654d025Sperrin 831ea8dc4b6Seschrock /* 832ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 833ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 834ea8dc4b6Seschrock */ 835ea8dc4b6Seschrock spa_errlog_drain(spa); 836ea8dc4b6Seschrock 837ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 838ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 839ea8dc4b6Seschrock 840fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 84135a5a358SJonathan Adams 84235a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 84335a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 84435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 84535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 84635a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 84735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 84835a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 84935a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 85035a5a358SJonathan Adams } 85135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 85235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 85335a5a358SJonathan Adams } 85435a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 85535a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 85635a5a358SJonathan Adams 85735a5a358SJonathan Adams /* 85835a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 85935a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 86035a5a358SJonathan Adams * it. 86135a5a358SJonathan Adams */ 86235a5a358SJonathan Adams if (spa->spa_did != 0) { 86335a5a358SJonathan Adams thread_join(spa->spa_did); 86435a5a358SJonathan Adams spa->spa_did = 0; 86535a5a358SJonathan Adams } 866fa9e4066Sahrens } 867fa9e4066Sahrens 868fa9e4066Sahrens /* 869fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 870fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 871fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 872fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 873fa9e4066Sahrens */ 87499653d4eSeschrock static int 87599653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 87699653d4eSeschrock uint_t id, int atype) 877fa9e4066Sahrens { 878fa9e4066Sahrens nvlist_t **child; 879573ca77eSGeorge Wilson uint_t children; 88099653d4eSeschrock int error; 881fa9e4066Sahrens 88299653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 88399653d4eSeschrock return (error); 884fa9e4066Sahrens 88599653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 88699653d4eSeschrock return (0); 887fa9e4066Sahrens 888e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 889e14bb325SJeff Bonwick &child, &children); 890e14bb325SJeff Bonwick 891e14bb325SJeff Bonwick if (error == ENOENT) 892e14bb325SJeff Bonwick return (0); 893e14bb325SJeff Bonwick 894e14bb325SJeff Bonwick if (error) { 89599653d4eSeschrock vdev_free(*vdp); 89699653d4eSeschrock *vdp = NULL; 89799653d4eSeschrock return (EINVAL); 898fa9e4066Sahrens } 899fa9e4066Sahrens 900573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 90199653d4eSeschrock vdev_t *vd; 90299653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 90399653d4eSeschrock atype)) != 0) { 90499653d4eSeschrock vdev_free(*vdp); 90599653d4eSeschrock *vdp = NULL; 90699653d4eSeschrock return (error); 907fa9e4066Sahrens } 908fa9e4066Sahrens } 909fa9e4066Sahrens 91099653d4eSeschrock ASSERT(*vdp != NULL); 91199653d4eSeschrock 91299653d4eSeschrock return (0); 913fa9e4066Sahrens } 914fa9e4066Sahrens 915fa9e4066Sahrens /* 916fa9e4066Sahrens * Opposite of spa_load(). 917fa9e4066Sahrens */ 918fa9e4066Sahrens static void 919fa9e4066Sahrens spa_unload(spa_t *spa) 920fa9e4066Sahrens { 92199653d4eSeschrock int i; 92299653d4eSeschrock 923e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 924e14bb325SJeff Bonwick 925ea8dc4b6Seschrock /* 926ea8dc4b6Seschrock * Stop async tasks. 927ea8dc4b6Seschrock */ 928ea8dc4b6Seschrock spa_async_suspend(spa); 929ea8dc4b6Seschrock 930fa9e4066Sahrens /* 931fa9e4066Sahrens * Stop syncing. 932fa9e4066Sahrens */ 933fa9e4066Sahrens if (spa->spa_sync_on) { 934fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 935fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 936fa9e4066Sahrens } 937fa9e4066Sahrens 938fa9e4066Sahrens /* 939e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 940fa9e4066Sahrens */ 94154d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 94254d692b7SGeorge Wilson (void) zio_wait(spa->spa_async_zio_root); 94354d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 94454d692b7SGeorge Wilson } 945fa9e4066Sahrens 946fa9e4066Sahrens /* 947fa9e4066Sahrens * Close the dsl pool. 948fa9e4066Sahrens */ 949fa9e4066Sahrens if (spa->spa_dsl_pool) { 950fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 951fa9e4066Sahrens spa->spa_dsl_pool = NULL; 952afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 953fa9e4066Sahrens } 954fa9e4066Sahrens 955b24ab676SJeff Bonwick ddt_unload(spa); 956b24ab676SJeff Bonwick 9578ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 9588ad4d6ddSJeff Bonwick 9598ad4d6ddSJeff Bonwick /* 9608ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 9618ad4d6ddSJeff Bonwick */ 9628ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 9638ad4d6ddSJeff Bonwick 964fa9e4066Sahrens /* 965fa9e4066Sahrens * Close all vdevs. 966fa9e4066Sahrens */ 9670e34b6a7Sbonwick if (spa->spa_root_vdev) 968fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 9690e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 970ea8dc4b6Seschrock 971fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 972fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 973fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 974fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 975fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 976fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 97799653d4eSeschrock } 978fa94a07fSbrendan if (spa->spa_spares.sav_config) { 979fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 980fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 981fa94a07fSbrendan } 9822ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 983fa94a07fSbrendan 984fa94a07fSbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 985fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 986fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 987fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 988fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 989fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 990fa94a07fSbrendan } 991fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 992fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 993fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 99499653d4eSeschrock } 9952ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 99699653d4eSeschrock 997ea8dc4b6Seschrock spa->spa_async_suspended = 0; 9988ad4d6ddSJeff Bonwick 9998ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1000fa9e4066Sahrens } 1001fa9e4066Sahrens 100299653d4eSeschrock /* 100399653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 100499653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1005fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1006fa94a07fSbrendan * then re-generate a more complete list including status information. 100799653d4eSeschrock */ 100899653d4eSeschrock static void 100999653d4eSeschrock spa_load_spares(spa_t *spa) 101099653d4eSeschrock { 101199653d4eSeschrock nvlist_t **spares; 101299653d4eSeschrock uint_t nspares; 101399653d4eSeschrock int i; 101439c23413Seschrock vdev_t *vd, *tvd; 101599653d4eSeschrock 1016e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1017e14bb325SJeff Bonwick 101899653d4eSeschrock /* 101999653d4eSeschrock * First, close and free any existing spare vdevs. 102099653d4eSeschrock */ 1021fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1022fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 102339c23413Seschrock 102439c23413Seschrock /* Undo the call to spa_activate() below */ 1025c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1026c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 102739c23413Seschrock spa_spare_remove(tvd); 102839c23413Seschrock vdev_close(vd); 102939c23413Seschrock vdev_free(vd); 103099653d4eSeschrock } 103139c23413Seschrock 1032fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1033fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1034fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 103599653d4eSeschrock 1036fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 103799653d4eSeschrock nspares = 0; 103899653d4eSeschrock else 1039fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 104099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 104199653d4eSeschrock 1042fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1043fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 104499653d4eSeschrock 104599653d4eSeschrock if (nspares == 0) 104699653d4eSeschrock return; 104799653d4eSeschrock 104899653d4eSeschrock /* 104999653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 105039c23413Seschrock * process. For each spare, there is potentially two different vdev_t 105139c23413Seschrock * structures associated with it: one in the list of spares (used only 105239c23413Seschrock * for basic validation purposes) and one in the active vdev 105339c23413Seschrock * configuration (if it's spared in). During this phase we open and 105439c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 105539c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 105699653d4eSeschrock */ 1057fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1058fa94a07fSbrendan KM_SLEEP); 1059fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 106099653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 106199653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 106299653d4eSeschrock ASSERT(vd != NULL); 106399653d4eSeschrock 1064fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 106599653d4eSeschrock 1066c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1067c5904d13Seschrock B_FALSE)) != NULL) { 106839c23413Seschrock if (!tvd->vdev_isspare) 106939c23413Seschrock spa_spare_add(tvd); 107039c23413Seschrock 107139c23413Seschrock /* 107239c23413Seschrock * We only mark the spare active if we were successfully 107339c23413Seschrock * able to load the vdev. Otherwise, importing a pool 107439c23413Seschrock * with a bad active spare would result in strange 107539c23413Seschrock * behavior, because multiple pool would think the spare 107639c23413Seschrock * is actively in use. 107739c23413Seschrock * 107839c23413Seschrock * There is a vulnerability here to an equally bizarre 107939c23413Seschrock * circumstance, where a dead active spare is later 108039c23413Seschrock * brought back to life (onlined or otherwise). Given 108139c23413Seschrock * the rarity of this scenario, and the extra complexity 108239c23413Seschrock * it adds, we ignore the possibility. 108339c23413Seschrock */ 108439c23413Seschrock if (!vdev_is_dead(tvd)) 108539c23413Seschrock spa_spare_activate(tvd); 108639c23413Seschrock } 108739c23413Seschrock 1088e14bb325SJeff Bonwick vd->vdev_top = vd; 10896809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1090e14bb325SJeff Bonwick 109199653d4eSeschrock if (vdev_open(vd) != 0) 109299653d4eSeschrock continue; 109399653d4eSeschrock 1094fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1095fa94a07fSbrendan spa_spare_add(vd); 109699653d4eSeschrock } 109799653d4eSeschrock 109899653d4eSeschrock /* 109999653d4eSeschrock * Recompute the stashed list of spares, with status information 110099653d4eSeschrock * this time. 110199653d4eSeschrock */ 1102fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 110399653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 110499653d4eSeschrock 1105fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1106fa94a07fSbrendan KM_SLEEP); 1107fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1108fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 11093f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1110fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1111fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1112fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 111399653d4eSeschrock nvlist_free(spares[i]); 1114fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1115fa94a07fSbrendan } 1116fa94a07fSbrendan 1117fa94a07fSbrendan /* 1118fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1119fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1120fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1121fa94a07fSbrendan * then re-generate a more complete list including status information. 1122fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1123fa94a07fSbrendan * not re-opened. 1124fa94a07fSbrendan */ 1125fa94a07fSbrendan static void 1126fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1127fa94a07fSbrendan { 1128fa94a07fSbrendan nvlist_t **l2cache; 1129fa94a07fSbrendan uint_t nl2cache; 1130fa94a07fSbrendan int i, j, oldnvdevs; 1131573ca77eSGeorge Wilson uint64_t guid; 1132fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1133fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1134fa94a07fSbrendan 1135e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1136e14bb325SJeff Bonwick 1137fa94a07fSbrendan if (sav->sav_config != NULL) { 1138fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1139fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1140fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1141fa94a07fSbrendan } else { 1142fa94a07fSbrendan nl2cache = 0; 1143fa94a07fSbrendan } 1144fa94a07fSbrendan 1145fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1146fa94a07fSbrendan oldnvdevs = sav->sav_count; 1147fa94a07fSbrendan sav->sav_vdevs = NULL; 1148fa94a07fSbrendan sav->sav_count = 0; 1149fa94a07fSbrendan 1150fa94a07fSbrendan /* 1151fa94a07fSbrendan * Process new nvlist of vdevs. 1152fa94a07fSbrendan */ 1153fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1154fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1155fa94a07fSbrendan &guid) == 0); 1156fa94a07fSbrendan 1157fa94a07fSbrendan newvdevs[i] = NULL; 1158fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1159fa94a07fSbrendan vd = oldvdevs[j]; 1160fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1161fa94a07fSbrendan /* 1162fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1163fa94a07fSbrendan */ 1164fa94a07fSbrendan newvdevs[i] = vd; 1165fa94a07fSbrendan oldvdevs[j] = NULL; 1166fa94a07fSbrendan break; 1167fa94a07fSbrendan } 1168fa94a07fSbrendan } 1169fa94a07fSbrendan 1170fa94a07fSbrendan if (newvdevs[i] == NULL) { 1171fa94a07fSbrendan /* 1172fa94a07fSbrendan * Create new vdev 1173fa94a07fSbrendan */ 1174fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1175fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1176fa94a07fSbrendan ASSERT(vd != NULL); 1177fa94a07fSbrendan newvdevs[i] = vd; 1178fa94a07fSbrendan 1179fa94a07fSbrendan /* 1180fa94a07fSbrendan * Commit this vdev as an l2cache device, 1181fa94a07fSbrendan * even if it fails to open. 1182fa94a07fSbrendan */ 1183fa94a07fSbrendan spa_l2cache_add(vd); 1184fa94a07fSbrendan 1185c5904d13Seschrock vd->vdev_top = vd; 1186c5904d13Seschrock vd->vdev_aux = sav; 1187c5904d13Seschrock 1188c5904d13Seschrock spa_l2cache_activate(vd); 1189c5904d13Seschrock 1190fa94a07fSbrendan if (vdev_open(vd) != 0) 1191fa94a07fSbrendan continue; 1192fa94a07fSbrendan 1193fa94a07fSbrendan (void) vdev_validate_aux(vd); 1194fa94a07fSbrendan 1195573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1196573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1197fa94a07fSbrendan } 1198fa94a07fSbrendan } 1199fa94a07fSbrendan 1200fa94a07fSbrendan /* 1201fa94a07fSbrendan * Purge vdevs that were dropped 1202fa94a07fSbrendan */ 1203fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1204fa94a07fSbrendan uint64_t pool; 1205fa94a07fSbrendan 1206fa94a07fSbrendan vd = oldvdevs[i]; 1207fa94a07fSbrendan if (vd != NULL) { 12088ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 12098ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1210fa94a07fSbrendan l2arc_remove_vdev(vd); 1211fa94a07fSbrendan (void) vdev_close(vd); 1212fa94a07fSbrendan spa_l2cache_remove(vd); 1213fa94a07fSbrendan } 1214fa94a07fSbrendan } 1215fa94a07fSbrendan 1216fa94a07fSbrendan if (oldvdevs) 1217fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1218fa94a07fSbrendan 1219fa94a07fSbrendan if (sav->sav_config == NULL) 1220fa94a07fSbrendan goto out; 1221fa94a07fSbrendan 1222fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1223fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1224fa94a07fSbrendan 1225fa94a07fSbrendan /* 1226fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1227fa94a07fSbrendan * information this time. 1228fa94a07fSbrendan */ 1229fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1230fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1231fa94a07fSbrendan 1232fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1233fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1234fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 12353f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1236fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1237fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1238fa94a07fSbrendan out: 1239fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1240fa94a07fSbrendan nvlist_free(l2cache[i]); 1241fa94a07fSbrendan if (sav->sav_count) 1242fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 124399653d4eSeschrock } 124499653d4eSeschrock 124599653d4eSeschrock static int 124699653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 124799653d4eSeschrock { 124899653d4eSeschrock dmu_buf_t *db; 124999653d4eSeschrock char *packed = NULL; 125099653d4eSeschrock size_t nvsize = 0; 125199653d4eSeschrock int error; 125299653d4eSeschrock *value = NULL; 125399653d4eSeschrock 125499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 125599653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 125699653d4eSeschrock dmu_buf_rele(db, FTAG); 125799653d4eSeschrock 125899653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 12597bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 12607bfdf011SNeil Perrin DMU_READ_PREFETCH); 126199653d4eSeschrock if (error == 0) 126299653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 126399653d4eSeschrock kmem_free(packed, nvsize); 126499653d4eSeschrock 126599653d4eSeschrock return (error); 126699653d4eSeschrock } 126799653d4eSeschrock 12683d7072f8Seschrock /* 12693d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 12703d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 12713d7072f8Seschrock */ 12723d7072f8Seschrock static void 12733d7072f8Seschrock spa_check_removed(vdev_t *vd) 12743d7072f8Seschrock { 1275573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 12763d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 12773d7072f8Seschrock 12783d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 12793d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 12803d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 12813d7072f8Seschrock } 12823d7072f8Seschrock } 12833d7072f8Seschrock 1284e6ca193dSGeorge Wilson /* 1285e6ca193dSGeorge Wilson * Load the slog device state from the config object since it's possible 1286e6ca193dSGeorge Wilson * that the label does not contain the most up-to-date information. 1287e6ca193dSGeorge Wilson */ 1288e6ca193dSGeorge Wilson void 128988ecc943SGeorge Wilson spa_load_log_state(spa_t *spa, nvlist_t *nv) 1290e6ca193dSGeorge Wilson { 129188ecc943SGeorge Wilson vdev_t *ovd, *rvd = spa->spa_root_vdev; 1292e6ca193dSGeorge Wilson 129388ecc943SGeorge Wilson /* 129488ecc943SGeorge Wilson * Load the original root vdev tree from the passed config. 129588ecc943SGeorge Wilson */ 129688ecc943SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 129788ecc943SGeorge Wilson VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 1298e6ca193dSGeorge Wilson 129988ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 130088ecc943SGeorge Wilson vdev_t *cvd = rvd->vdev_child[c]; 130188ecc943SGeorge Wilson if (cvd->vdev_islog) 130288ecc943SGeorge Wilson vdev_load_log_state(cvd, ovd->vdev_child[c]); 1303e6ca193dSGeorge Wilson } 130488ecc943SGeorge Wilson vdev_free(ovd); 130588ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 1306e6ca193dSGeorge Wilson } 1307e6ca193dSGeorge Wilson 1308b87f3af3Sperrin /* 1309b87f3af3Sperrin * Check for missing log devices 1310b87f3af3Sperrin */ 1311b87f3af3Sperrin int 1312b87f3af3Sperrin spa_check_logs(spa_t *spa) 1313b87f3af3Sperrin { 1314b87f3af3Sperrin switch (spa->spa_log_state) { 1315b87f3af3Sperrin case SPA_LOG_MISSING: 1316b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1317b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1318b87f3af3Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1319b87f3af3Sperrin DS_FIND_CHILDREN)) { 13201195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1321b87f3af3Sperrin return (1); 1322b87f3af3Sperrin } 1323b87f3af3Sperrin break; 1324b87f3af3Sperrin } 1325b87f3af3Sperrin return (0); 1326b87f3af3Sperrin } 1327b87f3af3Sperrin 13281195e687SMark J Musante static boolean_t 13291195e687SMark J Musante spa_passivate_log(spa_t *spa) 13301195e687SMark J Musante { 13311195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 13321195e687SMark J Musante boolean_t slog_found = B_FALSE; 13331195e687SMark J Musante 13341195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 13351195e687SMark J Musante 13361195e687SMark J Musante if (!spa_has_slogs(spa)) 13371195e687SMark J Musante return (B_FALSE); 13381195e687SMark J Musante 13391195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 13401195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 13411195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 13421195e687SMark J Musante 13431195e687SMark J Musante if (tvd->vdev_islog) { 13441195e687SMark J Musante metaslab_group_passivate(mg); 13451195e687SMark J Musante slog_found = B_TRUE; 13461195e687SMark J Musante } 13471195e687SMark J Musante } 13481195e687SMark J Musante 13491195e687SMark J Musante return (slog_found); 13501195e687SMark J Musante } 13511195e687SMark J Musante 13521195e687SMark J Musante static void 13531195e687SMark J Musante spa_activate_log(spa_t *spa) 13541195e687SMark J Musante { 13551195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 13561195e687SMark J Musante 13571195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 13581195e687SMark J Musante 13591195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 13601195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 13611195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 13621195e687SMark J Musante 13631195e687SMark J Musante if (tvd->vdev_islog) 13641195e687SMark J Musante metaslab_group_activate(mg); 13651195e687SMark J Musante } 13661195e687SMark J Musante } 13671195e687SMark J Musante 13681195e687SMark J Musante int 13691195e687SMark J Musante spa_offline_log(spa_t *spa) 13701195e687SMark J Musante { 13711195e687SMark J Musante int error = 0; 13721195e687SMark J Musante 13731195e687SMark J Musante if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 13741195e687SMark J Musante NULL, DS_FIND_CHILDREN)) == 0) { 13751195e687SMark J Musante 13761195e687SMark J Musante /* 13771195e687SMark J Musante * We successfully offlined the log device, sync out the 13781195e687SMark J Musante * current txg so that the "stubby" block can be removed 13791195e687SMark J Musante * by zil_sync(). 13801195e687SMark J Musante */ 13811195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 13821195e687SMark J Musante } 13831195e687SMark J Musante return (error); 13841195e687SMark J Musante } 13851195e687SMark J Musante 1386b693757aSEric Schrock static void 1387b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1388b693757aSEric Schrock { 1389b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1390b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1391b693757aSEric Schrock } 1392b693757aSEric Schrock 1393b24ab676SJeff Bonwick void 1394b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1395b24ab676SJeff Bonwick { 1396b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1397b24ab676SJeff Bonwick 1398b24ab676SJeff Bonwick if (zio->io_error) 1399b24ab676SJeff Bonwick return; 1400b24ab676SJeff Bonwick 1401b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1402b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1403b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1404b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1405b24ab676SJeff Bonwick } 1406b24ab676SJeff Bonwick 1407468c413aSTim Haley typedef struct spa_load_error { 1408c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1409468c413aSTim Haley uint64_t sle_data_count; 1410468c413aSTim Haley } spa_load_error_t; 1411468c413aSTim Haley 1412468c413aSTim Haley static void 1413468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1414468c413aSTim Haley { 1415468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1416468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1417468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1418468c413aSTim Haley int error = zio->io_error; 1419468c413aSTim Haley 1420468c413aSTim Haley if (error) { 1421468c413aSTim Haley if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 1422468c413aSTim Haley type != DMU_OT_INTENT_LOG) 1423c8ee1847SVictor Latushkin atomic_add_64(&sle->sle_meta_count, 1); 1424468c413aSTim Haley else 1425468c413aSTim Haley atomic_add_64(&sle->sle_data_count, 1); 1426468c413aSTim Haley } 1427468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1428468c413aSTim Haley } 1429468c413aSTim Haley 1430468c413aSTim Haley /*ARGSUSED*/ 1431468c413aSTim Haley static int 1432b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 14333f9d6ad7SLin Ling arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1434468c413aSTim Haley { 1435468c413aSTim Haley if (bp != NULL) { 1436468c413aSTim Haley zio_t *rio = arg; 1437468c413aSTim Haley size_t size = BP_GET_PSIZE(bp); 1438468c413aSTim Haley void *data = zio_data_buf_alloc(size); 1439468c413aSTim Haley 1440468c413aSTim Haley zio_nowait(zio_read(rio, spa, bp, data, size, 1441468c413aSTim Haley spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1442468c413aSTim Haley ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1443468c413aSTim Haley ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1444468c413aSTim Haley } 1445468c413aSTim Haley return (0); 1446468c413aSTim Haley } 1447468c413aSTim Haley 1448468c413aSTim Haley static int 1449468c413aSTim Haley spa_load_verify(spa_t *spa) 1450468c413aSTim Haley { 1451468c413aSTim Haley zio_t *rio; 1452468c413aSTim Haley spa_load_error_t sle = { 0 }; 1453468c413aSTim Haley zpool_rewind_policy_t policy; 1454468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1455468c413aSTim Haley int error; 1456468c413aSTim Haley 1457c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1458c8ee1847SVictor Latushkin 1459c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1460c8ee1847SVictor Latushkin return (0); 1461c8ee1847SVictor Latushkin 1462468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1463468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1464468c413aSTim Haley 1465bbfd46c4SJeff Bonwick error = traverse_pool(spa, spa->spa_verify_min_txg, 1466bbfd46c4SJeff Bonwick TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 1467468c413aSTim Haley 1468468c413aSTim Haley (void) zio_wait(rio); 1469468c413aSTim Haley 1470c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1471468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1472468c413aSTim Haley 1473c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1474468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 1475468c413aSTim Haley verify_ok = B_TRUE; 1476468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1477468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 1478a33cae98STim Haley } else { 1479a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1480468c413aSTim Haley } 1481468c413aSTim Haley 1482468c413aSTim Haley if (error) { 1483468c413aSTim Haley if (error != ENXIO && error != EIO) 1484468c413aSTim Haley error = EIO; 1485468c413aSTim Haley return (error); 1486468c413aSTim Haley } 1487468c413aSTim Haley 1488468c413aSTim Haley return (verify_ok ? 0 : EIO); 1489468c413aSTim Haley } 1490468c413aSTim Haley 14911195e687SMark J Musante /* 14921195e687SMark J Musante * Find a value in the pool props object. 14931195e687SMark J Musante */ 14941195e687SMark J Musante static void 14951195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 14961195e687SMark J Musante { 14971195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 14981195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 14991195e687SMark J Musante } 15001195e687SMark J Musante 15011195e687SMark J Musante /* 15021195e687SMark J Musante * Find a value in the pool directory object. 15031195e687SMark J Musante */ 15041195e687SMark J Musante static int 15051195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 15061195e687SMark J Musante { 15071195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 15081195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 15091195e687SMark J Musante } 15101195e687SMark J Musante 15111195e687SMark J Musante static int 15121195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 15131195e687SMark J Musante { 15141195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 15151195e687SMark J Musante return (err); 15161195e687SMark J Musante } 15171195e687SMark J Musante 15181195e687SMark J Musante /* 15191195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 15201195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 15211195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 15221195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 15231195e687SMark J Musante * 15241195e687SMark J Musante * This function determines what to do with that list: either rejoin 15251195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 15261195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 15271195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 15281195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 15291195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 15301195e687SMark J Musante * 1531d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 1532d41c4376SMark J Musante * the original pool. 15331195e687SMark J Musante */ 15341195e687SMark J Musante static void 15351195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 15361195e687SMark J Musante { 15371195e687SMark J Musante uint_t extracted; 15381195e687SMark J Musante uint64_t *glist; 15391195e687SMark J Musante uint_t i, gcount; 15401195e687SMark J Musante nvlist_t *nvl; 15411195e687SMark J Musante vdev_t **vd; 15421195e687SMark J Musante boolean_t attempt_reopen; 15431195e687SMark J Musante 15441195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 15451195e687SMark J Musante return; 15461195e687SMark J Musante 15471195e687SMark J Musante /* check that the config is complete */ 15481195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 15491195e687SMark J Musante &glist, &gcount) != 0) 15501195e687SMark J Musante return; 15511195e687SMark J Musante 15521195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 15531195e687SMark J Musante 15541195e687SMark J Musante /* attempt to online all the vdevs & validate */ 15551195e687SMark J Musante attempt_reopen = B_TRUE; 15561195e687SMark J Musante for (i = 0; i < gcount; i++) { 15571195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 15581195e687SMark J Musante continue; 15591195e687SMark J Musante 15601195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 15611195e687SMark J Musante if (vd[i] == NULL) { 15621195e687SMark J Musante /* 15631195e687SMark J Musante * Don't bother attempting to reopen the disks; 15641195e687SMark J Musante * just do the split. 15651195e687SMark J Musante */ 15661195e687SMark J Musante attempt_reopen = B_FALSE; 15671195e687SMark J Musante } else { 15681195e687SMark J Musante /* attempt to re-online it */ 15691195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 15701195e687SMark J Musante } 15711195e687SMark J Musante } 15721195e687SMark J Musante 15731195e687SMark J Musante if (attempt_reopen) { 15741195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 15751195e687SMark J Musante 15761195e687SMark J Musante /* check each device to see what state it's in */ 15771195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 15781195e687SMark J Musante if (vd[i] != NULL && 15791195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 15801195e687SMark J Musante break; 15811195e687SMark J Musante ++extracted; 15821195e687SMark J Musante } 15831195e687SMark J Musante } 15841195e687SMark J Musante 15851195e687SMark J Musante /* 15861195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 15871195e687SMark J Musante * even attempted to look at them, then we split them off for 15881195e687SMark J Musante * good. 15891195e687SMark J Musante */ 15901195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 15911195e687SMark J Musante for (i = 0; i < gcount; i++) 15921195e687SMark J Musante if (vd[i] != NULL) 15931195e687SMark J Musante vdev_split(vd[i]); 15941195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 15951195e687SMark J Musante } 15961195e687SMark J Musante 15971195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 15981195e687SMark J Musante } 15991195e687SMark J Musante 16001195e687SMark J Musante static int 16011195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 16021195e687SMark J Musante boolean_t mosconfig) 16031195e687SMark J Musante { 16041195e687SMark J Musante nvlist_t *config = spa->spa_config; 16051195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 16061195e687SMark J Musante int error; 16071195e687SMark J Musante uint64_t pool_guid; 16081195e687SMark J Musante nvlist_t *nvl; 16091195e687SMark J Musante 16101195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 16111195e687SMark J Musante return (EINVAL); 16121195e687SMark J Musante 16131195e687SMark J Musante /* 16141195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 16151195e687SMark J Musante * it's not present treat it as the initial version. 16161195e687SMark J Musante */ 16171195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 16181195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 16191195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 16201195e687SMark J Musante 16211195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 16221195e687SMark J Musante &spa->spa_config_txg); 16231195e687SMark J Musante 16241195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 16251195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 16261195e687SMark J Musante error = EEXIST; 16271195e687SMark J Musante } else { 16281195e687SMark J Musante spa->spa_load_guid = pool_guid; 16291195e687SMark J Musante 16301195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 16311195e687SMark J Musante &nvl) == 0) { 16321195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 16331195e687SMark J Musante KM_SLEEP) == 0); 16341195e687SMark J Musante } 16351195e687SMark J Musante 16361195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 16371195e687SMark J Musante mosconfig, &ereport); 16381195e687SMark J Musante } 16391195e687SMark J Musante 16401195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 16411195e687SMark J Musante if (error && error != EBADF) 16421195e687SMark J Musante zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 16431195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 16441195e687SMark J Musante spa->spa_ena = 0; 16451195e687SMark J Musante 16461195e687SMark J Musante return (error); 16471195e687SMark J Musante } 16481195e687SMark J Musante 1649fa9e4066Sahrens /* 1650fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 1651ea8dc4b6Seschrock * source of configuration information. 1652fa9e4066Sahrens */ 1653fa9e4066Sahrens static int 16541195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 16551195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 16561195e687SMark J Musante char **ereport) 1657fa9e4066Sahrens { 1658fa9e4066Sahrens int error = 0; 1659871a9500SMark J Musante nvlist_t *nvroot = NULL; 1660fa9e4066Sahrens vdev_t *rvd; 1661fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 16620373e76bSbonwick uint64_t config_cache_txg = spa->spa_config_txg; 16638ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 16641195e687SMark J Musante int parse; 1665fa9e4066Sahrens 16668ad4d6ddSJeff Bonwick /* 16678ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 16688ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 16698ad4d6ddSJeff Bonwick */ 16708ad4d6ddSJeff Bonwick if (!mosconfig) 16718ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 16728ad4d6ddSJeff Bonwick 1673e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1674e14bb325SJeff Bonwick 1675ea8dc4b6Seschrock spa->spa_load_state = state; 16760373e76bSbonwick 16771195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 16781195e687SMark J Musante return (EINVAL); 1679fa9e4066Sahrens 16801195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 16811195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 1682b5989ec7Seschrock 168354d692b7SGeorge Wilson /* 168454d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 168554d692b7SGeorge Wilson */ 168625f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 168725f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 168854d692b7SGeorge Wilson 1689fa9e4066Sahrens /* 169099653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 169199653d4eSeschrock * value that will be returned by spa_version() since parsing the 169299653d4eSeschrock * configuration requires knowing the version number. 1693fa9e4066Sahrens */ 1694e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16951195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 1696e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1697fa9e4066Sahrens 169899653d4eSeschrock if (error != 0) 16991195e687SMark J Musante return (error); 1700fa9e4066Sahrens 17010e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 17021195e687SMark J Musante 17031195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 17041195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 17051195e687SMark J Musante } 1706fa9e4066Sahrens 1707fa9e4066Sahrens /* 1708fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 1709fa9e4066Sahrens */ 1710e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17110bf246f5Smc error = vdev_open(rvd); 1712e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 17130bf246f5Smc if (error != 0) 17141195e687SMark J Musante return (error); 1715fa9e4066Sahrens 1716560e6e96Seschrock /* 171777e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 171877e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 171977e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 17201195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 172177e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 172277e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 172377e3a39cSMark J Musante * the vdev config. 17241195e687SMark J Musante * 17251195e687SMark J Musante * If we're assembling a new pool that's been split off from an 17261195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 17271195e687SMark J Musante * validation for now. 1728560e6e96Seschrock */ 17291195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 17301195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17311195e687SMark J Musante error = vdev_validate(rvd); 17321195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 1733560e6e96Seschrock 17341195e687SMark J Musante if (error != 0) 17351195e687SMark J Musante return (error); 17361195e687SMark J Musante 17371195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 17381195e687SMark J Musante return (ENXIO); 1739560e6e96Seschrock } 1740560e6e96Seschrock 1741fa9e4066Sahrens /* 1742fa9e4066Sahrens * Find the best uberblock. 1743fa9e4066Sahrens */ 1744e14bb325SJeff Bonwick vdev_uberblock_load(NULL, rvd, ub); 1745fa9e4066Sahrens 1746fa9e4066Sahrens /* 1747fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 1748fa9e4066Sahrens */ 17491195e687SMark J Musante if (ub->ub_txg == 0) 17501195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 1751ea8dc4b6Seschrock 1752ea8dc4b6Seschrock /* 1753ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 1754ea8dc4b6Seschrock */ 17551195e687SMark J Musante if (ub->ub_version > SPA_VERSION) 17561195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 1757fa9e4066Sahrens 1758fa9e4066Sahrens /* 1759fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1760fa9e4066Sahrens * incomplete configuration. 1761fa9e4066Sahrens */ 17621195e687SMark J Musante if (mosconfig && type != SPA_IMPORT_ASSEMBLE && 17631195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 17641195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 17651195e687SMark J Musante 17661195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 17671195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17681195e687SMark J Musante spa_try_repair(spa, config); 17691195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 17701195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 17711195e687SMark J Musante spa->spa_config_splitting = NULL; 1772fa9e4066Sahrens } 1773fa9e4066Sahrens 1774fa9e4066Sahrens /* 1775fa9e4066Sahrens * Initialize internal SPA structures. 1776fa9e4066Sahrens */ 1777fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1778fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1779468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 1780c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 1781468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 1782468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 1783b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 17843f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 1785b24ab676SJeff Bonwick 1786ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 17871195e687SMark J Musante if (error) 17881195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1789fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1790fa9e4066Sahrens 17911195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 17921195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1793fa9e4066Sahrens 1794fa9e4066Sahrens if (!mosconfig) { 179595173954Sek uint64_t hostid; 1796871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 1797871a9500SMark J Musante 1798871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 1799871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1800fa9e4066Sahrens 180188ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 180277650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 180395173954Sek char *hostname; 180495173954Sek unsigned long myhostid = 0; 180595173954Sek 180688ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 180795173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 180895173954Sek 18095679c89fSjv #ifdef _KERNEL 18105679c89fSjv myhostid = zone_get_hostid(NULL); 18115679c89fSjv #else /* _KERNEL */ 18125679c89fSjv /* 18135679c89fSjv * We're emulating the system's hostid in userland, so 18145679c89fSjv * we can't use zone_get_hostid(). 18155679c89fSjv */ 181695173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 18175679c89fSjv #endif /* _KERNEL */ 181817194a52Slling if (hostid != 0 && myhostid != 0 && 18195679c89fSjv hostid != myhostid) { 1820871a9500SMark J Musante nvlist_free(nvconfig); 182195173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 182295173954Sek "loaded as it was last accessed by " 182377650510SLin Ling "another system (host: %s hostid: 0x%lx). " 182495173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 1825e14bb325SJeff Bonwick spa_name(spa), hostname, 182695173954Sek (unsigned long)hostid); 18271195e687SMark J Musante return (EBADF); 182895173954Sek } 182995173954Sek } 1830c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 1831c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 1832c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 1833c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 183495173954Sek 183588ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 1836fa9e4066Sahrens spa_unload(spa); 1837fa9e4066Sahrens spa_deactivate(spa); 18388ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 1839fa9e4066Sahrens 18401195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 1841fa9e4066Sahrens } 1842fa9e4066Sahrens 18431195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_SYNC_BPLIST, 18441195e687SMark J Musante &spa->spa_deferred_bplist_obj) != 0) 18451195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1846fa9e4066Sahrens 184799653d4eSeschrock /* 184899653d4eSeschrock * Load the bit that tells us to use the new accounting function 184999653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 185099653d4eSeschrock * be present. 185199653d4eSeschrock */ 18521195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 18531195e687SMark J Musante if (error != 0 && error != ENOENT) 18541195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 185599653d4eSeschrock 18563f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 18573f9d6ad7SLin Ling &spa->spa_creation_version); 18583f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 18593f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 18603f9d6ad7SLin Ling 1861fa9e4066Sahrens /* 1862ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1863ea8dc4b6Seschrock * not be present. 1864fa9e4066Sahrens */ 18651195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 18661195e687SMark J Musante if (error != 0 && error != ENOENT) 18671195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1868ea8dc4b6Seschrock 18691195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 18701195e687SMark J Musante &spa->spa_errlog_scrub); 18711195e687SMark J Musante if (error != 0 && error != ENOENT) 18721195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1873ea8dc4b6Seschrock 187406eeb2adSek /* 187506eeb2adSek * Load the history object. If we have an older pool, this 187606eeb2adSek * will not be present. 187706eeb2adSek */ 18781195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 18791195e687SMark J Musante if (error != 0 && error != ENOENT) 18801195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 18811195e687SMark J Musante 18821195e687SMark J Musante /* 18831195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 18841195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 18851195e687SMark J Musante * devices. 18861195e687SMark J Musante */ 188706eeb2adSek 188899653d4eSeschrock /* 188999653d4eSeschrock * Load any hot spares for this pool. 189099653d4eSeschrock */ 18911195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 18921195e687SMark J Musante if (error != 0 && error != ENOENT) 18931195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 18941195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 1895e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 1896fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 18971195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 18981195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 189999653d4eSeschrock 1900e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 190199653d4eSeschrock spa_load_spares(spa); 1902e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 19031195e687SMark J Musante } else if (error == 0) { 19041195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 190599653d4eSeschrock } 190699653d4eSeschrock 1907fa94a07fSbrendan /* 1908fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 1909fa94a07fSbrendan */ 19101195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 1911fa94a07fSbrendan &spa->spa_l2cache.sav_object); 19121195e687SMark J Musante if (error != 0 && error != ENOENT) 19131195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 19141195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 1915fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 1916fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 19171195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 19181195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1919fa94a07fSbrendan 1920e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1921fa94a07fSbrendan spa_load_l2cache(spa); 1922e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 19231195e687SMark J Musante } else if (error == 0) { 19241195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 1925fa94a07fSbrendan } 1926fa94a07fSbrendan 1927990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1928ecd6cf80Smarks 19291195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 19301195e687SMark J Musante if (error && error != ENOENT) 19311195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1932b1b8ab34Slling 1933b1b8ab34Slling if (error == 0) { 19341195e687SMark J Musante uint64_t autoreplace; 19351195e687SMark J Musante 19361195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 19371195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 19381195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 19391195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 19401195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 19411195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 19421195e687SMark J Musante &spa->spa_dedup_ditto); 19431195e687SMark J Musante 1944b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 1945b1b8ab34Slling } 1946b1b8ab34Slling 19473d7072f8Seschrock /* 19483d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 19493d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 19503d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 19513d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 19523d7072f8Seschrock * over. 19533d7072f8Seschrock */ 1954b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 19553d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 1956b693757aSEric Schrock /* 1957b693757aSEric Schrock * For the import case, this is done in spa_import(), because 1958b693757aSEric Schrock * at this point we're using the spare definitions from 1959b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 1960b693757aSEric Schrock */ 1961b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 1962b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 1963b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 1964b693757aSEric Schrock } 1965b693757aSEric Schrock } 19663d7072f8Seschrock 1967ea8dc4b6Seschrock /* 1968560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 1969ea8dc4b6Seschrock */ 1970560e6e96Seschrock vdev_load(rvd); 19710373e76bSbonwick 1972fa9e4066Sahrens /* 1973fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1974fa9e4066Sahrens */ 1975e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1976fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1977e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1978fa9e4066Sahrens 1979fa9e4066Sahrens /* 1980fa9e4066Sahrens * Check the state of the root vdev. If it can't be opened, it 1981fa9e4066Sahrens * indicates one or more toplevel vdevs are faulted. 1982fa9e4066Sahrens */ 19831195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 19841195e687SMark J Musante return (ENXIO); 1985fa9e4066Sahrens 1986b24ab676SJeff Bonwick /* 1987b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 1988b24ab676SJeff Bonwick */ 1989b24ab676SJeff Bonwick error = ddt_load(spa); 19901195e687SMark J Musante if (error != 0) 19911195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1992b24ab676SJeff Bonwick 1993485bbbf5SGeorge Wilson spa_update_dspace(spa); 1994485bbbf5SGeorge Wilson 1995468c413aSTim Haley if (state != SPA_LOAD_TRYIMPORT) { 1996468c413aSTim Haley error = spa_load_verify(spa); 19971195e687SMark J Musante if (error) 19981195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 19991195e687SMark J Musante error)); 2000468c413aSTim Haley } 2001468c413aSTim Haley 2002b24ab676SJeff Bonwick /* 20031195e687SMark J Musante * Load the intent log state and check log integrity. If we're 20041195e687SMark J Musante * assembling a pool from a split, the log is not transferred over. 2005b24ab676SJeff Bonwick */ 20061195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2007871a9500SMark J Musante nvlist_t *nvconfig; 2008871a9500SMark J Musante 2009871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2010871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2011871a9500SMark J Musante 20121195e687SMark J Musante VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE, 20131195e687SMark J Musante &nvroot) == 0); 20141195e687SMark J Musante spa_load_log_state(spa, nvroot); 20151195e687SMark J Musante nvlist_free(nvconfig); 20161195e687SMark J Musante 20171195e687SMark J Musante if (spa_check_logs(spa)) { 20181195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 20191195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 20201195e687SMark J Musante } 2021b24ab676SJeff Bonwick } 2022b24ab676SJeff Bonwick 2023468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2024468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 20255dabedeeSbonwick dmu_tx_t *tx; 20260373e76bSbonwick int need_update = B_FALSE; 20278ad4d6ddSJeff Bonwick 20288ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 20295dabedeeSbonwick 20300373e76bSbonwick /* 20310373e76bSbonwick * Claim log blocks that haven't been committed yet. 20320373e76bSbonwick * This must all happen in a single txg. 2033b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2034b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2035468c413aSTim Haley * Price of rollback is that we abandon the log. 20360373e76bSbonwick */ 2037b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2038b24ab676SJeff Bonwick 20395dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2040fa9e4066Sahrens spa_first_txg(spa)); 2041e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 20420b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2043fa9e4066Sahrens dmu_tx_commit(tx); 2044fa9e4066Sahrens 2045b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2046b24ab676SJeff Bonwick 20471195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2048fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2049fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2050fa9e4066Sahrens 2051fa9e4066Sahrens /* 2052b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2053b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2054b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2055b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2056b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2057fa9e4066Sahrens */ 2058b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 20590e34b6a7Sbonwick 20600e34b6a7Sbonwick /* 20610373e76bSbonwick * If the config cache is stale, or we have uninitialized 20620373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2063bc758434SLin Ling * 2064bc758434SLin Ling * If spa_load_verbatim is true, trust the current 2065bc758434SLin Ling * in-core spa_config and update the disk labels. 20660e34b6a7Sbonwick */ 20670373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 2068468c413aSTim Haley state == SPA_LOAD_IMPORT || spa->spa_load_verbatim || 2069468c413aSTim Haley state == SPA_LOAD_RECOVER) 20700373e76bSbonwick need_update = B_TRUE; 20710373e76bSbonwick 20728ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 20730373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 20740373e76bSbonwick need_update = B_TRUE; 20750e34b6a7Sbonwick 20760e34b6a7Sbonwick /* 20770373e76bSbonwick * Update the config cache asychronously in case we're the 20780373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 20790e34b6a7Sbonwick */ 20800373e76bSbonwick if (need_update) 20810373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 20828ad4d6ddSJeff Bonwick 20838ad4d6ddSJeff Bonwick /* 20848ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 20858ad4d6ddSJeff Bonwick */ 20863f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 20873f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 20888ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2089503ad85cSMatthew Ahrens 2090503ad85cSMatthew Ahrens /* 2091503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2092503ad85cSMatthew Ahrens */ 2093503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2094503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2095ca45db41SChris Kirby 2096ca45db41SChris Kirby /* 2097ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2098ca45db41SChris Kirby */ 2099ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2100fa9e4066Sahrens } 2101fa9e4066Sahrens 21021195e687SMark J Musante return (0); 2103fa9e4066Sahrens } 2104fa9e4066Sahrens 2105468c413aSTim Haley static int 2106468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2107468c413aSTim Haley { 2108468c413aSTim Haley spa_unload(spa); 2109468c413aSTim Haley spa_deactivate(spa); 2110468c413aSTim Haley 2111468c413aSTim Haley spa->spa_load_max_txg--; 2112468c413aSTim Haley 2113468c413aSTim Haley spa_activate(spa, spa_mode_global); 2114468c413aSTim Haley spa_async_suspend(spa); 2115468c413aSTim Haley 21161195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2117468c413aSTim Haley } 2118468c413aSTim Haley 2119468c413aSTim Haley static int 2120468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2121c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2122468c413aSTim Haley { 2123468c413aSTim Haley nvlist_t *config = NULL; 2124468c413aSTim Haley int load_error, rewind_error; 2125c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2126468c413aSTim Haley uint64_t min_txg; 2127468c413aSTim Haley 2128a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2129468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 21301195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2131a33cae98STim Haley } else { 2132468c413aSTim Haley spa->spa_load_max_txg = max_request; 2133a33cae98STim Haley } 2134468c413aSTim Haley 21351195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 21361195e687SMark J Musante mosconfig); 2137468c413aSTim Haley if (load_error == 0) 2138468c413aSTim Haley return (0); 2139468c413aSTim Haley 2140468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2141468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2142468c413aSTim Haley 2143468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2144468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2145468c413aSTim Haley 2146c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2147468c413aSTim Haley nvlist_free(config); 2148468c413aSTim Haley return (load_error); 2149468c413aSTim Haley } 2150468c413aSTim Haley 2151468c413aSTim Haley /* Price of rolling back is discarding txgs, including log */ 2152468c413aSTim Haley if (state == SPA_LOAD_RECOVER) 21531195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2154468c413aSTim Haley 2155c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2156c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2157c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2158c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2159468c413aSTim Haley 2160c8ee1847SVictor Latushkin /* 2161c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2162c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2163c8ee1847SVictor Latushkin */ 2164c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2165c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2166c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2167468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2168468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 2169468c413aSTim Haley } 2170468c413aSTim Haley 2171468c413aSTim Haley if (config) 2172468c413aSTim Haley spa_rewind_data_to_nvlist(spa, config); 2173468c413aSTim Haley 2174468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 2175468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 2176468c413aSTim Haley 2177468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 2178468c413aSTim Haley spa_config_set(spa, config); 2179468c413aSTim Haley 2180468c413aSTim Haley return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 2181468c413aSTim Haley } 2182468c413aSTim Haley 2183fa9e4066Sahrens /* 2184fa9e4066Sahrens * Pool Open/Import 2185fa9e4066Sahrens * 2186fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 2187fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2188fa9e4066Sahrens * case of an open, the pool configuration will exist in the 21893d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 2190fa9e4066Sahrens * 2191fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2192fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 2193fa9e4066Sahrens * ambiguous state. 2194fa9e4066Sahrens */ 2195fa9e4066Sahrens static int 2196468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 2197468c413aSTim Haley nvlist_t **config) 2198fa9e4066Sahrens { 2199fa9e4066Sahrens spa_t *spa; 2200fa9e4066Sahrens int error; 2201fa9e4066Sahrens int locked = B_FALSE; 2202fa9e4066Sahrens 2203fa9e4066Sahrens *spapp = NULL; 2204fa9e4066Sahrens 2205fa9e4066Sahrens /* 2206fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 2207fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2208fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 2209fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 2210fa9e4066Sahrens */ 2211fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2212fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2213fa9e4066Sahrens locked = B_TRUE; 2214fa9e4066Sahrens } 2215fa9e4066Sahrens 2216fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2217fa9e4066Sahrens if (locked) 2218fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2219fa9e4066Sahrens return (ENOENT); 2220fa9e4066Sahrens } 2221468c413aSTim Haley 2222fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 22234b44c88cSTim Haley spa_load_state_t state = SPA_LOAD_OPEN; 22244b44c88cSTim Haley zpool_rewind_policy_t policy; 22254b44c88cSTim Haley 22264b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 22274b44c88cSTim Haley &policy); 22284b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 22294b44c88cSTim Haley state = SPA_LOAD_RECOVER; 2230fa9e4066Sahrens 22318ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2232fa9e4066Sahrens 2233468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 2234468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2235468c413aSTim Haley 2236468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 2237c8ee1847SVictor Latushkin policy.zrp_request); 2238fa9e4066Sahrens 2239fa9e4066Sahrens if (error == EBADF) { 2240fa9e4066Sahrens /* 2241560e6e96Seschrock * If vdev_validate() returns failure (indicated by 2242560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 2243560e6e96Seschrock * that the pool has been exported or destroyed. If 2244560e6e96Seschrock * this is the case, the config cache is out of sync and 2245560e6e96Seschrock * we should remove the pool from the namespace. 2246fa9e4066Sahrens */ 2247fa9e4066Sahrens spa_unload(spa); 2248fa9e4066Sahrens spa_deactivate(spa); 2249c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2250fa9e4066Sahrens spa_remove(spa); 2251fa9e4066Sahrens if (locked) 2252fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2253fa9e4066Sahrens return (ENOENT); 2254ea8dc4b6Seschrock } 2255ea8dc4b6Seschrock 2256ea8dc4b6Seschrock if (error) { 2257fa9e4066Sahrens /* 2258fa9e4066Sahrens * We can't open the pool, but we still have useful 2259fa9e4066Sahrens * information: the state of each vdev after the 2260fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 2261fa9e4066Sahrens */ 2262468c413aSTim Haley if (config != NULL && spa->spa_config) 2263468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 2264468c413aSTim Haley KM_SLEEP) == 0); 2265fa9e4066Sahrens spa_unload(spa); 2266fa9e4066Sahrens spa_deactivate(spa); 2267468c413aSTim Haley spa->spa_last_open_failed = error; 2268fa9e4066Sahrens if (locked) 2269fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2270fa9e4066Sahrens *spapp = NULL; 2271fa9e4066Sahrens return (error); 2272fa9e4066Sahrens } 2273468c413aSTim Haley 2274fa9e4066Sahrens } 2275fa9e4066Sahrens 2276fa9e4066Sahrens spa_open_ref(spa, tag); 22773d7072f8Seschrock 2278468c413aSTim Haley 2279468c413aSTim Haley if (config != NULL) 2280468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2281468c413aSTim Haley 2282a33cae98STim Haley if (locked) { 2283a33cae98STim Haley spa->spa_last_open_failed = 0; 2284a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 2285a33cae98STim Haley spa->spa_load_txg = 0; 2286fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2287a33cae98STim Haley } 2288fa9e4066Sahrens 2289fa9e4066Sahrens *spapp = spa; 2290fa9e4066Sahrens 2291fa9e4066Sahrens return (0); 2292fa9e4066Sahrens } 2293fa9e4066Sahrens 2294468c413aSTim Haley int 2295468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 2296468c413aSTim Haley nvlist_t **config) 2297468c413aSTim Haley { 2298468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 2299468c413aSTim Haley } 2300468c413aSTim Haley 2301fa9e4066Sahrens int 2302fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2303fa9e4066Sahrens { 2304468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 2305fa9e4066Sahrens } 2306fa9e4066Sahrens 2307ea8dc4b6Seschrock /* 2308ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 2309ea8dc4b6Seschrock * preventing it from being exported or destroyed. 2310ea8dc4b6Seschrock */ 2311ea8dc4b6Seschrock spa_t * 2312ea8dc4b6Seschrock spa_inject_addref(char *name) 2313ea8dc4b6Seschrock { 2314ea8dc4b6Seschrock spa_t *spa; 2315ea8dc4b6Seschrock 2316ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2317ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 2318ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2319ea8dc4b6Seschrock return (NULL); 2320ea8dc4b6Seschrock } 2321ea8dc4b6Seschrock spa->spa_inject_ref++; 2322ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2323ea8dc4b6Seschrock 2324ea8dc4b6Seschrock return (spa); 2325ea8dc4b6Seschrock } 2326ea8dc4b6Seschrock 2327ea8dc4b6Seschrock void 2328ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 2329ea8dc4b6Seschrock { 2330ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2331ea8dc4b6Seschrock spa->spa_inject_ref--; 2332ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2333ea8dc4b6Seschrock } 2334ea8dc4b6Seschrock 2335fa94a07fSbrendan /* 2336fa94a07fSbrendan * Add spares device information to the nvlist. 2337fa94a07fSbrendan */ 233899653d4eSeschrock static void 233999653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 234099653d4eSeschrock { 234199653d4eSeschrock nvlist_t **spares; 234299653d4eSeschrock uint_t i, nspares; 234399653d4eSeschrock nvlist_t *nvroot; 234499653d4eSeschrock uint64_t guid; 234599653d4eSeschrock vdev_stat_t *vs; 234699653d4eSeschrock uint_t vsc; 234739c23413Seschrock uint64_t pool; 234899653d4eSeschrock 23496809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 23506809eb4eSEric Schrock 2351fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 235299653d4eSeschrock return; 235399653d4eSeschrock 235499653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 235599653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2356fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 235799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 235899653d4eSeschrock if (nspares != 0) { 235999653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 236099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 236199653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 236299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 236399653d4eSeschrock 236499653d4eSeschrock /* 236599653d4eSeschrock * Go through and find any spares which have since been 236699653d4eSeschrock * repurposed as an active spare. If this is the case, update 236799653d4eSeschrock * their status appropriately. 236899653d4eSeschrock */ 236999653d4eSeschrock for (i = 0; i < nspares; i++) { 237099653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 237199653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 237289a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 237389a89ebfSlling pool != 0ULL) { 237499653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 23753f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 237699653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 237799653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 237899653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 237999653d4eSeschrock } 238099653d4eSeschrock } 238199653d4eSeschrock } 238299653d4eSeschrock } 238399653d4eSeschrock 2384fa94a07fSbrendan /* 2385fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 2386fa94a07fSbrendan */ 2387fa94a07fSbrendan static void 2388fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 2389fa94a07fSbrendan { 2390fa94a07fSbrendan nvlist_t **l2cache; 2391fa94a07fSbrendan uint_t i, j, nl2cache; 2392fa94a07fSbrendan nvlist_t *nvroot; 2393fa94a07fSbrendan uint64_t guid; 2394fa94a07fSbrendan vdev_t *vd; 2395fa94a07fSbrendan vdev_stat_t *vs; 2396fa94a07fSbrendan uint_t vsc; 2397fa94a07fSbrendan 23986809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 23996809eb4eSEric Schrock 2400fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 2401fa94a07fSbrendan return; 2402fa94a07fSbrendan 2403fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 2404fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2405fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 2406fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2407fa94a07fSbrendan if (nl2cache != 0) { 2408fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 2409fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2410fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 2411fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2412fa94a07fSbrendan 2413fa94a07fSbrendan /* 2414fa94a07fSbrendan * Update level 2 cache device stats. 2415fa94a07fSbrendan */ 2416fa94a07fSbrendan 2417fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 2418fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 2419fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 2420fa94a07fSbrendan 2421fa94a07fSbrendan vd = NULL; 2422fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 2423fa94a07fSbrendan if (guid == 2424fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 2425fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 2426fa94a07fSbrendan break; 2427fa94a07fSbrendan } 2428fa94a07fSbrendan } 2429fa94a07fSbrendan ASSERT(vd != NULL); 2430fa94a07fSbrendan 2431fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 24323f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 24333f9d6ad7SLin Ling == 0); 2434fa94a07fSbrendan vdev_get_stats(vd, vs); 2435fa94a07fSbrendan } 2436fa94a07fSbrendan } 2437fa94a07fSbrendan } 2438fa94a07fSbrendan 2439fa9e4066Sahrens int 2440ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2441fa9e4066Sahrens { 2442fa9e4066Sahrens int error; 2443fa9e4066Sahrens spa_t *spa; 2444fa9e4066Sahrens 2445fa9e4066Sahrens *config = NULL; 2446468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 2447fa9e4066Sahrens 24486809eb4eSEric Schrock if (spa != NULL) { 24496809eb4eSEric Schrock /* 24506809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 24516809eb4eSEric Schrock * or l2cache devices could change and the config would be 24526809eb4eSEric Schrock * self-inconsistent. 24536809eb4eSEric Schrock */ 24546809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2455ea8dc4b6Seschrock 24566809eb4eSEric Schrock if (*config != NULL) { 2457e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 24586809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 24596809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 2460e14bb325SJeff Bonwick 24616809eb4eSEric Schrock if (spa_suspended(spa)) 24626809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 24636809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 24646809eb4eSEric Schrock spa->spa_failmode) == 0); 24656809eb4eSEric Schrock 24666809eb4eSEric Schrock spa_add_spares(spa, *config); 24676809eb4eSEric Schrock spa_add_l2cache(spa, *config); 24686809eb4eSEric Schrock } 246999653d4eSeschrock } 247099653d4eSeschrock 2471ea8dc4b6Seschrock /* 2472ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 2473ea8dc4b6Seschrock * and call spa_lookup() directly. 2474ea8dc4b6Seschrock */ 2475ea8dc4b6Seschrock if (altroot) { 2476ea8dc4b6Seschrock if (spa == NULL) { 2477ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2478ea8dc4b6Seschrock spa = spa_lookup(name); 2479ea8dc4b6Seschrock if (spa) 2480ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 2481ea8dc4b6Seschrock else 2482ea8dc4b6Seschrock altroot[0] = '\0'; 2483ea8dc4b6Seschrock spa = NULL; 2484ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2485ea8dc4b6Seschrock } else { 2486ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 2487ea8dc4b6Seschrock } 2488ea8dc4b6Seschrock } 2489ea8dc4b6Seschrock 24906809eb4eSEric Schrock if (spa != NULL) { 24916809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 2492fa9e4066Sahrens spa_close(spa, FTAG); 24936809eb4eSEric Schrock } 2494fa9e4066Sahrens 2495fa9e4066Sahrens return (error); 2496fa9e4066Sahrens } 2497fa9e4066Sahrens 249899653d4eSeschrock /* 2499fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 2500fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 2501fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 2502fa94a07fSbrendan * specified, as long as they are well-formed. 250399653d4eSeschrock */ 250499653d4eSeschrock static int 2505fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 2506fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 2507fa94a07fSbrendan vdev_labeltype_t label) 250899653d4eSeschrock { 2509fa94a07fSbrendan nvlist_t **dev; 2510fa94a07fSbrendan uint_t i, ndev; 251199653d4eSeschrock vdev_t *vd; 251299653d4eSeschrock int error; 251399653d4eSeschrock 2514e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2515e14bb325SJeff Bonwick 251699653d4eSeschrock /* 2517fa94a07fSbrendan * It's acceptable to have no devs specified. 251899653d4eSeschrock */ 2519fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 252099653d4eSeschrock return (0); 252199653d4eSeschrock 2522fa94a07fSbrendan if (ndev == 0) 252399653d4eSeschrock return (EINVAL); 252499653d4eSeschrock 252599653d4eSeschrock /* 2526fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 2527fa94a07fSbrendan * device type. 252899653d4eSeschrock */ 2529fa94a07fSbrendan if (spa_version(spa) < version) 253099653d4eSeschrock return (ENOTSUP); 253199653d4eSeschrock 253239c23413Seschrock /* 2533fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 253439c23413Seschrock * checking. 253539c23413Seschrock */ 2536fa94a07fSbrendan sav->sav_pending = dev; 2537fa94a07fSbrendan sav->sav_npending = ndev; 253839c23413Seschrock 2539fa94a07fSbrendan for (i = 0; i < ndev; i++) { 2540fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 254199653d4eSeschrock mode)) != 0) 254239c23413Seschrock goto out; 254399653d4eSeschrock 254499653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 254599653d4eSeschrock vdev_free(vd); 254639c23413Seschrock error = EINVAL; 254739c23413Seschrock goto out; 254899653d4eSeschrock } 254999653d4eSeschrock 2550fa94a07fSbrendan /* 2551e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 2552e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 2553fa94a07fSbrendan */ 2554e14bb325SJeff Bonwick #ifdef _KERNEL 2555fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 2556fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 2557fa94a07fSbrendan error = ENOTBLK; 2558fa94a07fSbrendan goto out; 2559fa94a07fSbrendan } 2560e14bb325SJeff Bonwick #endif 256199653d4eSeschrock vd->vdev_top = vd; 256299653d4eSeschrock 256339c23413Seschrock if ((error = vdev_open(vd)) == 0 && 2564fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 2565fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 256639c23413Seschrock vd->vdev_guid) == 0); 256739c23413Seschrock } 256899653d4eSeschrock 256999653d4eSeschrock vdev_free(vd); 257039c23413Seschrock 2571fa94a07fSbrendan if (error && 2572fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 257339c23413Seschrock goto out; 257439c23413Seschrock else 257539c23413Seschrock error = 0; 257699653d4eSeschrock } 257799653d4eSeschrock 257839c23413Seschrock out: 2579fa94a07fSbrendan sav->sav_pending = NULL; 2580fa94a07fSbrendan sav->sav_npending = 0; 258139c23413Seschrock return (error); 258299653d4eSeschrock } 258399653d4eSeschrock 2584fa94a07fSbrendan static int 2585fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 2586fa94a07fSbrendan { 2587fa94a07fSbrendan int error; 2588fa94a07fSbrendan 2589e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2590e14bb325SJeff Bonwick 2591fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2592fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 2593fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 2594fa94a07fSbrendan return (error); 2595fa94a07fSbrendan } 2596fa94a07fSbrendan 2597fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2598fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 2599fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 2600fa94a07fSbrendan } 2601fa94a07fSbrendan 2602fa94a07fSbrendan static void 2603fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 2604fa94a07fSbrendan const char *config) 2605fa94a07fSbrendan { 2606fa94a07fSbrendan int i; 2607fa94a07fSbrendan 2608fa94a07fSbrendan if (sav->sav_config != NULL) { 2609fa94a07fSbrendan nvlist_t **olddevs; 2610fa94a07fSbrendan uint_t oldndevs; 2611fa94a07fSbrendan nvlist_t **newdevs; 2612fa94a07fSbrendan 2613fa94a07fSbrendan /* 2614fa94a07fSbrendan * Generate new dev list by concatentating with the 2615fa94a07fSbrendan * current dev list. 2616fa94a07fSbrendan */ 2617fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 2618fa94a07fSbrendan &olddevs, &oldndevs) == 0); 2619fa94a07fSbrendan 2620fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 2621fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 2622fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 2623fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 2624fa94a07fSbrendan KM_SLEEP) == 0); 2625fa94a07fSbrendan for (i = 0; i < ndevs; i++) 2626fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 2627fa94a07fSbrendan KM_SLEEP) == 0); 2628fa94a07fSbrendan 2629fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 2630fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 2631fa94a07fSbrendan 2632fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 2633fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 2634fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 2635fa94a07fSbrendan nvlist_free(newdevs[i]); 2636fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 2637fa94a07fSbrendan } else { 2638fa94a07fSbrendan /* 2639fa94a07fSbrendan * Generate a new dev list. 2640fa94a07fSbrendan */ 2641fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 2642fa94a07fSbrendan KM_SLEEP) == 0); 2643fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 2644fa94a07fSbrendan devs, ndevs) == 0); 2645fa94a07fSbrendan } 2646fa94a07fSbrendan } 2647fa94a07fSbrendan 2648fa94a07fSbrendan /* 2649fa94a07fSbrendan * Stop and drop level 2 ARC devices 2650fa94a07fSbrendan */ 2651fa94a07fSbrendan void 2652fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 2653fa94a07fSbrendan { 2654fa94a07fSbrendan vdev_t *vd; 2655fa94a07fSbrendan int i; 2656fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 2657fa94a07fSbrendan 2658fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 2659fa94a07fSbrendan uint64_t pool; 2660fa94a07fSbrendan 2661fa94a07fSbrendan vd = sav->sav_vdevs[i]; 2662fa94a07fSbrendan ASSERT(vd != NULL); 2663fa94a07fSbrendan 26648ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 26658ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 2666fa94a07fSbrendan l2arc_remove_vdev(vd); 2667fa94a07fSbrendan if (vd->vdev_isl2cache) 2668fa94a07fSbrendan spa_l2cache_remove(vd); 2669fa94a07fSbrendan vdev_clear_stats(vd); 2670fa94a07fSbrendan (void) vdev_close(vd); 2671fa94a07fSbrendan } 2672fa94a07fSbrendan } 2673fa94a07fSbrendan 2674fa9e4066Sahrens /* 2675fa9e4066Sahrens * Pool Creation 2676fa9e4066Sahrens */ 2677fa9e4066Sahrens int 2678990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 26790a48a24eStimh const char *history_str, nvlist_t *zplprops) 2680fa9e4066Sahrens { 2681fa9e4066Sahrens spa_t *spa; 2682990b4856Slling char *altroot = NULL; 26830373e76bSbonwick vdev_t *rvd; 2684fa9e4066Sahrens dsl_pool_t *dp; 2685fa9e4066Sahrens dmu_tx_t *tx; 2686573ca77eSGeorge Wilson int error = 0; 2687fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 2688fa94a07fSbrendan nvlist_t **spares, **l2cache; 2689fa94a07fSbrendan uint_t nspares, nl2cache; 2690990b4856Slling uint64_t version; 2691fa9e4066Sahrens 2692fa9e4066Sahrens /* 2693fa9e4066Sahrens * If this pool already exists, return failure. 2694fa9e4066Sahrens */ 2695fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2696fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 2697fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2698fa9e4066Sahrens return (EEXIST); 2699fa9e4066Sahrens } 2700fa9e4066Sahrens 2701fa9e4066Sahrens /* 2702fa9e4066Sahrens * Allocate a new spa_t structure. 2703fa9e4066Sahrens */ 2704990b4856Slling (void) nvlist_lookup_string(props, 2705990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 2706468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 27078ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2708fa9e4066Sahrens 2709990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 2710990b4856Slling spa_deactivate(spa); 2711990b4856Slling spa_remove(spa); 2712c5904d13Seschrock mutex_exit(&spa_namespace_lock); 2713990b4856Slling return (error); 2714990b4856Slling } 2715990b4856Slling 2716990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 2717990b4856Slling &version) != 0) 2718990b4856Slling version = SPA_VERSION; 2719990b4856Slling ASSERT(version <= SPA_VERSION); 2720b24ab676SJeff Bonwick 2721b24ab676SJeff Bonwick spa->spa_first_txg = txg; 2722b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 2723990b4856Slling spa->spa_uberblock.ub_version = version; 2724fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2725fa9e4066Sahrens 272654d692b7SGeorge Wilson /* 272754d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 272854d692b7SGeorge Wilson */ 272925f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 273025f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 273154d692b7SGeorge Wilson 27320373e76bSbonwick /* 27330373e76bSbonwick * Create the root vdev. 27340373e76bSbonwick */ 2735e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27360373e76bSbonwick 273799653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 27380373e76bSbonwick 273999653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 274099653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 27410373e76bSbonwick 2742b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 27430373e76bSbonwick error = EINVAL; 274499653d4eSeschrock 274599653d4eSeschrock if (error == 0 && 274699653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 2747fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 274899653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 2749573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 2750573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 2751573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 2752573ca77eSGeorge Wilson } 27530373e76bSbonwick } 27540373e76bSbonwick 2755e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2756fa9e4066Sahrens 275799653d4eSeschrock if (error != 0) { 2758fa9e4066Sahrens spa_unload(spa); 2759fa9e4066Sahrens spa_deactivate(spa); 2760fa9e4066Sahrens spa_remove(spa); 2761fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2762fa9e4066Sahrens return (error); 2763fa9e4066Sahrens } 2764fa9e4066Sahrens 276599653d4eSeschrock /* 276699653d4eSeschrock * Get the list of spares, if specified. 276799653d4eSeschrock */ 276899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 276999653d4eSeschrock &spares, &nspares) == 0) { 2770fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 277199653d4eSeschrock KM_SLEEP) == 0); 2772fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 277399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2774e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 277599653d4eSeschrock spa_load_spares(spa); 2776e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2777fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2778fa94a07fSbrendan } 2779fa94a07fSbrendan 2780fa94a07fSbrendan /* 2781fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 2782fa94a07fSbrendan */ 2783fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2784fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 2785fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2786fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 2787fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2788fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2789e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2790fa94a07fSbrendan spa_load_l2cache(spa); 2791e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2792fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 279399653d4eSeschrock } 279499653d4eSeschrock 27950a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2796fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2797fa9e4066Sahrens 2798485bbbf5SGeorge Wilson /* 2799485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 2800485bbbf5SGeorge Wilson */ 2801485bbbf5SGeorge Wilson ddt_create(spa); 2802485bbbf5SGeorge Wilson 2803485bbbf5SGeorge Wilson spa_update_dspace(spa); 2804485bbbf5SGeorge Wilson 2805fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 2806fa9e4066Sahrens 2807fa9e4066Sahrens /* 2808fa9e4066Sahrens * Create the pool config object. 2809fa9e4066Sahrens */ 2810fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 2811f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2812fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2813fa9e4066Sahrens 2814ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2815fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 2816ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 2817ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 2818ea8dc4b6Seschrock } 2819fa9e4066Sahrens 28203f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 28213f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 28223f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 28233f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 28243f9d6ad7SLin Ling } 28253f9d6ad7SLin Ling 2826990b4856Slling /* Newly created pools with the right version are always deflated. */ 2827990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 2828990b4856Slling spa->spa_deflate = TRUE; 2829990b4856Slling if (zap_add(spa->spa_meta_objset, 2830990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 2831990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 2832990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 2833990b4856Slling } 283499653d4eSeschrock } 283599653d4eSeschrock 2836fa9e4066Sahrens /* 2837fa9e4066Sahrens * Create the deferred-free bplist object. Turn off compression 2838fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 2839fa9e4066Sahrens * keeps changing. 2840fa9e4066Sahrens */ 2841b24ab676SJeff Bonwick spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset, 2842fa9e4066Sahrens 1 << 14, tx); 2843b24ab676SJeff Bonwick dmu_object_set_compress(spa->spa_meta_objset, 2844b24ab676SJeff Bonwick spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx); 2845fa9e4066Sahrens 2846ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2847fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 2848b24ab676SJeff Bonwick sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) { 2849ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 2850ea8dc4b6Seschrock } 2851fa9e4066Sahrens 285206eeb2adSek /* 285306eeb2adSek * Create the pool's history object. 285406eeb2adSek */ 2855990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 2856990b4856Slling spa_history_create_obj(spa, tx); 2857990b4856Slling 2858990b4856Slling /* 2859990b4856Slling * Set pool properties. 2860990b4856Slling */ 2861990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 2862990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 28630a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 2864573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 2865b24ab676SJeff Bonwick 2866379c004dSEric Schrock if (props != NULL) { 2867379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 28683f9d6ad7SLin Ling spa_sync_props(spa, props, tx); 2869379c004dSEric Schrock } 287006eeb2adSek 2871fa9e4066Sahrens dmu_tx_commit(tx); 2872fa9e4066Sahrens 2873fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2874fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2875fa9e4066Sahrens 2876fa9e4066Sahrens /* 2877fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 2878fa9e4066Sahrens * bean counters are appropriately updated. 2879fa9e4066Sahrens */ 2880fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2881fa9e4066Sahrens 2882c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2883fa9e4066Sahrens 2884990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 2885228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 2886c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_CREATE); 2887228975ccSek 2888088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 2889088f3894Sahrens 2890daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 2891daaa36a7SGeorge Wilson 2892fa9e4066Sahrens return (0); 2893fa9e4066Sahrens } 2894fa9e4066Sahrens 2895e7cbe64fSgw #ifdef _KERNEL 2896e7cbe64fSgw /* 289721ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 289821ecdf64SLin Ling * during the system boot up time. 2899e7cbe64fSgw */ 290021ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 290121ecdf64SLin Ling 290221ecdf64SLin Ling static nvlist_t * 290321ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 2904e7cbe64fSgw { 290521ecdf64SLin Ling nvlist_t *config; 2906e7cbe64fSgw nvlist_t *nvtop, *nvroot; 2907e7cbe64fSgw uint64_t pgid; 2908e7cbe64fSgw 290921ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 291021ecdf64SLin Ling return (NULL); 291121ecdf64SLin Ling 2912e7cbe64fSgw /* 2913e7cbe64fSgw * Add this top-level vdev to the child array. 2914e7cbe64fSgw */ 291521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 291621ecdf64SLin Ling &nvtop) == 0); 291721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 291821ecdf64SLin Ling &pgid) == 0); 291921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 2920e7cbe64fSgw 2921e7cbe64fSgw /* 2922e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 2923e7cbe64fSgw */ 2924e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 292521ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 292621ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 2927e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 2928e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 2929e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2930e7cbe64fSgw &nvtop, 1) == 0); 2931e7cbe64fSgw 2932e7cbe64fSgw /* 2933e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 2934e7cbe64fSgw * this pool's configuration (remove the old, add the new). 2935e7cbe64fSgw */ 2936e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 2937e7cbe64fSgw nvlist_free(nvroot); 293821ecdf64SLin Ling return (config); 2939e7cbe64fSgw } 2940e7cbe64fSgw 2941e7cbe64fSgw /* 294221ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 294321ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 294421ecdf64SLin Ling * device has a more recent txg. 2945051aabe6Staylor */ 294621ecdf64SLin Ling static void 294721ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 2948051aabe6Staylor { 2949573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 295021ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 2951051aabe6Staylor 295221ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 295321ecdf64SLin Ling nvlist_t *label; 295421ecdf64SLin Ling uint64_t label_txg; 2955051aabe6Staylor 295621ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 295721ecdf64SLin Ling &label) != 0) 295821ecdf64SLin Ling return; 2959051aabe6Staylor 296021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 296121ecdf64SLin Ling &label_txg) == 0); 2962051aabe6Staylor 296321ecdf64SLin Ling /* 296421ecdf64SLin Ling * Do we have a better boot device? 296521ecdf64SLin Ling */ 296621ecdf64SLin Ling if (label_txg > *txg) { 296721ecdf64SLin Ling *txg = label_txg; 296821ecdf64SLin Ling *avd = vd; 2969051aabe6Staylor } 297021ecdf64SLin Ling nvlist_free(label); 2971051aabe6Staylor } 2972051aabe6Staylor } 2973051aabe6Staylor 2974e7cbe64fSgw /* 2975e7cbe64fSgw * Import a root pool. 2976e7cbe64fSgw * 2977051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 2978051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 2979051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 2980e7cbe64fSgw * 2981e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 2982e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 2983e7cbe64fSgw * e.g. 2984e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 2985e7cbe64fSgw */ 2986e7cbe64fSgw int 2987051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 2988e7cbe64fSgw { 298921ecdf64SLin Ling spa_t *spa; 299021ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 299121ecdf64SLin Ling nvlist_t *config, *nvtop; 299221ecdf64SLin Ling uint64_t guid, txg; 2993e7cbe64fSgw char *pname; 2994e7cbe64fSgw int error; 2995e7cbe64fSgw 2996e7cbe64fSgw /* 299721ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 2998e7cbe64fSgw */ 2999dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3000dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3001dedec472SJack Meng if (config == NULL) { 3002dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3003dedec472SJack Meng /* iscsi boot */ 3004dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3005dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3006dedec472SJack Meng } 3007dedec472SJack Meng } 3008dedec472SJack Meng #endif 3009dedec472SJack Meng if (config == NULL) { 301021ecdf64SLin Ling cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 301121ecdf64SLin Ling devpath); 301221ecdf64SLin Ling return (EIO); 301321ecdf64SLin Ling } 3014e7cbe64fSgw 301521ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 301621ecdf64SLin Ling &pname) == 0); 301721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3018e7cbe64fSgw 30196809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 30206809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 30216809eb4eSEric Schrock /* 30226809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 30236809eb4eSEric Schrock * can replace it with the correct config we just read in. 30246809eb4eSEric Schrock */ 30256809eb4eSEric Schrock spa_remove(spa); 30266809eb4eSEric Schrock } 30276809eb4eSEric Schrock 3028468c413aSTim Haley spa = spa_add(pname, config, NULL); 30296809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 3030bc758434SLin Ling spa->spa_load_verbatim = B_TRUE; 3031e7cbe64fSgw 303221ecdf64SLin Ling /* 303321ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 303421ecdf64SLin Ling */ 303521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 303621ecdf64SLin Ling &nvtop) == 0); 303721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 303821ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 303921ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 304021ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 304121ecdf64SLin Ling if (error) { 304221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 304321ecdf64SLin Ling nvlist_free(config); 304421ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 304521ecdf64SLin Ling pname); 304621ecdf64SLin Ling return (error); 304721ecdf64SLin Ling } 304821ecdf64SLin Ling 304921ecdf64SLin Ling /* 305021ecdf64SLin Ling * Get the boot vdev. 305121ecdf64SLin Ling */ 305221ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 305321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 305421ecdf64SLin Ling (u_longlong_t)guid); 305521ecdf64SLin Ling error = ENOENT; 305621ecdf64SLin Ling goto out; 305721ecdf64SLin Ling } 3058e7cbe64fSgw 305921ecdf64SLin Ling /* 306021ecdf64SLin Ling * Determine if there is a better boot device. 306121ecdf64SLin Ling */ 306221ecdf64SLin Ling avd = bvd; 306321ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 306421ecdf64SLin Ling if (avd != bvd) { 306521ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 306621ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 306721ecdf64SLin Ling error = EINVAL; 306821ecdf64SLin Ling goto out; 306921ecdf64SLin Ling } 3070e7cbe64fSgw 307121ecdf64SLin Ling /* 307221ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 307321ecdf64SLin Ling * we're booting off the active spare. 307421ecdf64SLin Ling */ 307521ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 307621ecdf64SLin Ling !bvd->vdev_isspare) { 307721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 307821ecdf64SLin Ling "try booting from '%s'", 307921ecdf64SLin Ling bvd->vdev_parent->vdev_child[1]->vdev_path); 308021ecdf64SLin Ling error = EINVAL; 308121ecdf64SLin Ling goto out; 308221ecdf64SLin Ling } 308321ecdf64SLin Ling 308421ecdf64SLin Ling error = 0; 3085c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 308621ecdf64SLin Ling out: 308721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 308821ecdf64SLin Ling vdev_free(rvd); 308921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 309021ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 309121ecdf64SLin Ling 309221ecdf64SLin Ling nvlist_free(config); 3093e7cbe64fSgw return (error); 3094e7cbe64fSgw } 309521ecdf64SLin Ling 3096e7cbe64fSgw #endif 3097e7cbe64fSgw 3098e7cbe64fSgw /* 30996809eb4eSEric Schrock * Take a pool and insert it into the namespace as if it had been loaded at 31006809eb4eSEric Schrock * boot. 3101e7cbe64fSgw */ 3102e7cbe64fSgw int 31036809eb4eSEric Schrock spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 3104e7cbe64fSgw { 31056809eb4eSEric Schrock spa_t *spa; 31066809eb4eSEric Schrock char *altroot = NULL; 31076809eb4eSEric Schrock 31086809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 31096809eb4eSEric Schrock if (spa_lookup(pool) != NULL) { 31106809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 31116809eb4eSEric Schrock return (EEXIST); 31126809eb4eSEric Schrock } 31136809eb4eSEric Schrock 31146809eb4eSEric Schrock (void) nvlist_lookup_string(props, 31156809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3116468c413aSTim Haley spa = spa_add(pool, config, altroot); 31176809eb4eSEric Schrock 3118468c413aSTim Haley spa->spa_load_verbatim = B_TRUE; 31196809eb4eSEric Schrock 31206809eb4eSEric Schrock if (props != NULL) 31216809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 31226809eb4eSEric Schrock 31236809eb4eSEric Schrock spa_config_sync(spa, B_FALSE, B_TRUE); 31246809eb4eSEric Schrock 31256809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 3126c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 31276809eb4eSEric Schrock 31286809eb4eSEric Schrock return (0); 3129e7cbe64fSgw } 3130e7cbe64fSgw 31316809eb4eSEric Schrock /* 31326809eb4eSEric Schrock * Import a non-root pool into the system. 31336809eb4eSEric Schrock */ 3134c5904d13Seschrock int 31356809eb4eSEric Schrock spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 3136c5904d13Seschrock { 31376809eb4eSEric Schrock spa_t *spa; 31386809eb4eSEric Schrock char *altroot = NULL; 3139468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 3140468c413aSTim Haley zpool_rewind_policy_t policy; 31416809eb4eSEric Schrock int error; 31426809eb4eSEric Schrock nvlist_t *nvroot; 31436809eb4eSEric Schrock nvlist_t **spares, **l2cache; 31446809eb4eSEric Schrock uint_t nspares, nl2cache; 31456809eb4eSEric Schrock 31466809eb4eSEric Schrock /* 31476809eb4eSEric Schrock * If a pool with this name exists, return failure. 31486809eb4eSEric Schrock */ 31496809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 31501195e687SMark J Musante if (spa_lookup(pool) != NULL) { 31516809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 31526809eb4eSEric Schrock return (EEXIST); 31536809eb4eSEric Schrock } 31546809eb4eSEric Schrock 3155468c413aSTim Haley zpool_get_rewind_policy(config, &policy); 3156468c413aSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 3157468c413aSTim Haley state = SPA_LOAD_RECOVER; 3158468c413aSTim Haley 31596809eb4eSEric Schrock /* 31606809eb4eSEric Schrock * Create and initialize the spa structure. 31616809eb4eSEric Schrock */ 31626809eb4eSEric Schrock (void) nvlist_lookup_string(props, 31636809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3164468c413aSTim Haley spa = spa_add(pool, config, altroot); 31656809eb4eSEric Schrock spa_activate(spa, spa_mode_global); 31666809eb4eSEric Schrock 316725f89ee2SJeff Bonwick /* 316825f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 316925f89ee2SJeff Bonwick */ 317025f89ee2SJeff Bonwick spa_async_suspend(spa); 317125f89ee2SJeff Bonwick 31726809eb4eSEric Schrock /* 31736809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 31746809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 31756809eb4eSEric Schrock * doing an import. 31766809eb4eSEric Schrock */ 3177468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3178468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3179468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 3180c8ee1847SVictor Latushkin policy.zrp_request); 3181468c413aSTim Haley 3182468c413aSTim Haley /* 3183468c413aSTim Haley * Propagate anything learned about failing or best txgs 3184468c413aSTim Haley * back to caller 3185468c413aSTim Haley */ 3186468c413aSTim Haley spa_rewind_data_to_nvlist(spa, config); 31876809eb4eSEric Schrock 31886809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 31896809eb4eSEric Schrock /* 31906809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 31916809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 31926809eb4eSEric Schrock */ 31936809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 31946809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 31956809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 31966809eb4eSEric Schrock spa_load_spares(spa); 31976809eb4eSEric Schrock } 31986809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 31996809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 32006809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 32016809eb4eSEric Schrock spa_load_l2cache(spa); 32026809eb4eSEric Schrock } 32036809eb4eSEric Schrock 32046809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 32056809eb4eSEric Schrock &nvroot) == 0); 32066809eb4eSEric Schrock if (error == 0) 32076809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 32086809eb4eSEric Schrock VDEV_ALLOC_SPARE); 32096809eb4eSEric Schrock if (error == 0) 32106809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 32116809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 32126809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 32136809eb4eSEric Schrock 32146809eb4eSEric Schrock if (props != NULL) 32156809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 32166809eb4eSEric Schrock 32176809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 32186809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 32196809eb4eSEric Schrock spa_unload(spa); 32206809eb4eSEric Schrock spa_deactivate(spa); 32216809eb4eSEric Schrock spa_remove(spa); 32226809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 32236809eb4eSEric Schrock return (error); 32246809eb4eSEric Schrock } 32256809eb4eSEric Schrock 32266809eb4eSEric Schrock /* 32276809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 32286809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 32296809eb4eSEric Schrock */ 32306809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 32316809eb4eSEric Schrock &spares, &nspares) == 0) { 32326809eb4eSEric Schrock if (spa->spa_spares.sav_config) 32336809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 32346809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 32356809eb4eSEric Schrock else 32366809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 32376809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 32386809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 32396809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 32406809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 32416809eb4eSEric Schrock spa_load_spares(spa); 32426809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 32436809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 32446809eb4eSEric Schrock } 32456809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 32466809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 32476809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 32486809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 32496809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 32506809eb4eSEric Schrock else 32516809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 32526809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 32536809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 32546809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 32556809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 32566809eb4eSEric Schrock spa_load_l2cache(spa); 32576809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 32586809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 32596809eb4eSEric Schrock } 32606809eb4eSEric Schrock 3261b693757aSEric Schrock /* 3262b693757aSEric Schrock * Check for any removed devices. 3263b693757aSEric Schrock */ 3264b693757aSEric Schrock if (spa->spa_autoreplace) { 3265b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3266b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3267b693757aSEric Schrock } 3268b693757aSEric Schrock 32696809eb4eSEric Schrock if (spa_writeable(spa)) { 32706809eb4eSEric Schrock /* 32716809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 32726809eb4eSEric Schrock */ 3273bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 32746809eb4eSEric Schrock } 32756809eb4eSEric Schrock 32763f9d6ad7SLin Ling spa_async_resume(spa); 32773f9d6ad7SLin Ling 3278573ca77eSGeorge Wilson /* 3279573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 3280573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 3281573ca77eSGeorge Wilson */ 3282573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 3283573ca77eSGeorge Wilson 32846809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 3285c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 32866809eb4eSEric Schrock 32876809eb4eSEric Schrock return (0); 3288c5904d13Seschrock } 3289c5904d13Seschrock 3290fa9e4066Sahrens nvlist_t * 3291fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 3292fa9e4066Sahrens { 3293fa9e4066Sahrens nvlist_t *config = NULL; 3294fa9e4066Sahrens char *poolname; 3295fa9e4066Sahrens spa_t *spa; 3296fa9e4066Sahrens uint64_t state; 32977b7154beSLin Ling int error; 3298fa9e4066Sahrens 3299fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3300fa9e4066Sahrens return (NULL); 3301fa9e4066Sahrens 3302fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3303fa9e4066Sahrens return (NULL); 3304fa9e4066Sahrens 3305fa9e4066Sahrens /* 33060373e76bSbonwick * Create and initialize the spa structure. 3307fa9e4066Sahrens */ 33080373e76bSbonwick mutex_enter(&spa_namespace_lock); 3309468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 33108ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 3311fa9e4066Sahrens 3312fa9e4066Sahrens /* 33130373e76bSbonwick * Pass off the heavy lifting to spa_load(). 3314ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 3315ecc2d604Sbonwick * is actually the one to trust when doing an import. 3316fa9e4066Sahrens */ 33171195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3318fa9e4066Sahrens 3319fa9e4066Sahrens /* 3320fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 3321fa9e4066Sahrens */ 3322fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 3323fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3324fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3325fa9e4066Sahrens poolname) == 0); 3326fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3327fa9e4066Sahrens state) == 0); 332895173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 332995173954Sek spa->spa_uberblock.ub_timestamp) == 0); 333099653d4eSeschrock 3331e7cbe64fSgw /* 3332e7cbe64fSgw * If the bootfs property exists on this pool then we 3333e7cbe64fSgw * copy it out so that external consumers can tell which 3334e7cbe64fSgw * pools are bootable. 3335e7cbe64fSgw */ 33367b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 3337e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3338e7cbe64fSgw 3339e7cbe64fSgw /* 3340e7cbe64fSgw * We have to play games with the name since the 3341e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 3342e7cbe64fSgw */ 3343e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 3344e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 3345e7cbe64fSgw char *cp; 3346e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3347e7cbe64fSgw 3348e7cbe64fSgw cp = strchr(tmpname, '/'); 3349e7cbe64fSgw if (cp == NULL) { 3350e7cbe64fSgw (void) strlcpy(dsname, tmpname, 3351e7cbe64fSgw MAXPATHLEN); 3352e7cbe64fSgw } else { 3353e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 3354e7cbe64fSgw "%s/%s", poolname, ++cp); 3355e7cbe64fSgw } 3356e7cbe64fSgw VERIFY(nvlist_add_string(config, 3357e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 3358e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 3359e7cbe64fSgw } 3360e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 3361e7cbe64fSgw } 3362e7cbe64fSgw 336399653d4eSeschrock /* 3364fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 336599653d4eSeschrock */ 33666809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 336799653d4eSeschrock spa_add_spares(spa, config); 3368fa94a07fSbrendan spa_add_l2cache(spa, config); 33696809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3370fa9e4066Sahrens } 3371fa9e4066Sahrens 3372fa9e4066Sahrens spa_unload(spa); 3373fa9e4066Sahrens spa_deactivate(spa); 3374fa9e4066Sahrens spa_remove(spa); 3375fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3376fa9e4066Sahrens 3377fa9e4066Sahrens return (config); 3378fa9e4066Sahrens } 3379fa9e4066Sahrens 3380fa9e4066Sahrens /* 3381fa9e4066Sahrens * Pool export/destroy 3382fa9e4066Sahrens * 3383fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3384fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3385fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 3386394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 3387394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 3388fa9e4066Sahrens */ 3389fa9e4066Sahrens static int 339089a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 3391394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 3392fa9e4066Sahrens { 3393fa9e4066Sahrens spa_t *spa; 3394fa9e4066Sahrens 339544cd46caSbillm if (oldconfig) 339644cd46caSbillm *oldconfig = NULL; 339744cd46caSbillm 33988ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 3399fa9e4066Sahrens return (EROFS); 3400fa9e4066Sahrens 3401fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3402fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3403fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3404fa9e4066Sahrens return (ENOENT); 3405fa9e4066Sahrens } 3406fa9e4066Sahrens 3407ea8dc4b6Seschrock /* 3408ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 3409ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 3410ea8dc4b6Seschrock */ 3411ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 3412ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3413ea8dc4b6Seschrock spa_async_suspend(spa); 3414ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3415ea8dc4b6Seschrock spa_close(spa, FTAG); 3416ea8dc4b6Seschrock 3417fa9e4066Sahrens /* 3418fa9e4066Sahrens * The pool will be in core if it's openable, 3419fa9e4066Sahrens * in which case we can modify its state. 3420fa9e4066Sahrens */ 3421fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3422fa9e4066Sahrens /* 3423fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 3424fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 3425fa9e4066Sahrens */ 3426fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3427fa9e4066Sahrens 3428ea8dc4b6Seschrock /* 3429ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 3430ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 3431ea8dc4b6Seschrock * fault injection handlers. 3432ea8dc4b6Seschrock */ 3433ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 3434ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 3435ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 3436ea8dc4b6Seschrock spa_async_resume(spa); 3437fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3438fa9e4066Sahrens return (EBUSY); 3439fa9e4066Sahrens } 3440fa9e4066Sahrens 344189a89ebfSlling /* 344289a89ebfSlling * A pool cannot be exported if it has an active shared spare. 344389a89ebfSlling * This is to prevent other pools stealing the active spare 344489a89ebfSlling * from an exported pool. At user's own will, such pool can 344589a89ebfSlling * be forcedly exported. 344689a89ebfSlling */ 344789a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 344889a89ebfSlling spa_has_active_shared_spare(spa)) { 344989a89ebfSlling spa_async_resume(spa); 345089a89ebfSlling mutex_exit(&spa_namespace_lock); 345189a89ebfSlling return (EXDEV); 345289a89ebfSlling } 345389a89ebfSlling 3454fa9e4066Sahrens /* 3455fa9e4066Sahrens * We want this to be reflected on every label, 3456fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 3457fa9e4066Sahrens * final sync that pushes these changes out. 3458fa9e4066Sahrens */ 3459394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 3460e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3461ea8dc4b6Seschrock spa->spa_state = new_state; 34623f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 34633f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 3464ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 3465e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3466ea8dc4b6Seschrock } 3467fa9e4066Sahrens } 3468fa9e4066Sahrens 34693d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 34703d7072f8Seschrock 3471fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3472fa9e4066Sahrens spa_unload(spa); 3473fa9e4066Sahrens spa_deactivate(spa); 3474fa9e4066Sahrens } 3475fa9e4066Sahrens 347644cd46caSbillm if (oldconfig && spa->spa_config) 347744cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 347844cd46caSbillm 3479ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 3480394ab0cbSGeorge Wilson if (!hardforce) 3481394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 3482ea8dc4b6Seschrock spa_remove(spa); 3483ea8dc4b6Seschrock } 3484fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3485fa9e4066Sahrens 3486fa9e4066Sahrens return (0); 3487fa9e4066Sahrens } 3488fa9e4066Sahrens 3489fa9e4066Sahrens /* 3490fa9e4066Sahrens * Destroy a storage pool. 3491fa9e4066Sahrens */ 3492fa9e4066Sahrens int 3493fa9e4066Sahrens spa_destroy(char *pool) 3494fa9e4066Sahrens { 3495394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 3496394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 3497fa9e4066Sahrens } 3498fa9e4066Sahrens 3499fa9e4066Sahrens /* 3500fa9e4066Sahrens * Export a storage pool. 3501fa9e4066Sahrens */ 3502fa9e4066Sahrens int 3503394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 3504394ab0cbSGeorge Wilson boolean_t hardforce) 3505fa9e4066Sahrens { 3506394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 3507394ab0cbSGeorge Wilson force, hardforce)); 3508fa9e4066Sahrens } 3509fa9e4066Sahrens 3510ea8dc4b6Seschrock /* 3511ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 3512ea8dc4b6Seschrock * from the namespace in any way. 3513ea8dc4b6Seschrock */ 3514ea8dc4b6Seschrock int 3515ea8dc4b6Seschrock spa_reset(char *pool) 3516ea8dc4b6Seschrock { 351789a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 3518394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 3519ea8dc4b6Seschrock } 3520ea8dc4b6Seschrock 3521fa9e4066Sahrens /* 3522fa9e4066Sahrens * ========================================================================== 3523fa9e4066Sahrens * Device manipulation 3524fa9e4066Sahrens * ========================================================================== 3525fa9e4066Sahrens */ 3526fa9e4066Sahrens 3527fa9e4066Sahrens /* 35288654d025Sperrin * Add a device to a storage pool. 3529fa9e4066Sahrens */ 3530fa9e4066Sahrens int 3531fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3532fa9e4066Sahrens { 353388ecc943SGeorge Wilson uint64_t txg, id; 35348ad4d6ddSJeff Bonwick int error; 3535fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 35360e34b6a7Sbonwick vdev_t *vd, *tvd; 3537fa94a07fSbrendan nvlist_t **spares, **l2cache; 3538fa94a07fSbrendan uint_t nspares, nl2cache; 3539fa9e4066Sahrens 3540fa9e4066Sahrens txg = spa_vdev_enter(spa); 3541fa9e4066Sahrens 354299653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 354399653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 354499653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 3545fa9e4066Sahrens 3546e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 354799653d4eSeschrock 3548fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 3549fa94a07fSbrendan &nspares) != 0) 355099653d4eSeschrock nspares = 0; 355199653d4eSeschrock 3552fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 3553fa94a07fSbrendan &nl2cache) != 0) 3554fa94a07fSbrendan nl2cache = 0; 3555fa94a07fSbrendan 3556e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 3557fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 3558fa9e4066Sahrens 3559e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 3560e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 3561e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 356299653d4eSeschrock 356339c23413Seschrock /* 3564fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 3565fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 356639c23413Seschrock */ 3567e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 356839c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 356939c23413Seschrock 357039c23413Seschrock /* 357139c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 357239c23413Seschrock */ 35738ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 357488ecc943SGeorge Wilson 357588ecc943SGeorge Wilson /* 357688ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 357788ecc943SGeorge Wilson */ 357888ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 357988ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 358088ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 358188ecc943SGeorge Wilson break; 358288ecc943SGeorge Wilson } 358388ecc943SGeorge Wilson } 358439c23413Seschrock tvd = vd->vdev_child[c]; 358539c23413Seschrock vdev_remove_child(vd, tvd); 358688ecc943SGeorge Wilson tvd->vdev_id = id; 358739c23413Seschrock vdev_add_child(rvd, tvd); 358839c23413Seschrock vdev_config_dirty(tvd); 358939c23413Seschrock } 359039c23413Seschrock 359199653d4eSeschrock if (nspares != 0) { 3592fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 3593fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 359499653d4eSeschrock spa_load_spares(spa); 3595fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3596fa94a07fSbrendan } 3597fa94a07fSbrendan 3598fa94a07fSbrendan if (nl2cache != 0) { 3599fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 3600fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 3601fa94a07fSbrendan spa_load_l2cache(spa); 3602fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3603fa9e4066Sahrens } 3604fa9e4066Sahrens 3605fa9e4066Sahrens /* 36060e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 36070e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 36080e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 36090e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 36100e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 36110e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 36120373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 36130e34b6a7Sbonwick * 36140e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 36150e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 36160e34b6a7Sbonwick * steps will be completed the next time we load the pool. 36170e34b6a7Sbonwick */ 36180373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 36190e34b6a7Sbonwick 36200373e76bSbonwick mutex_enter(&spa_namespace_lock); 36210373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 36220373e76bSbonwick mutex_exit(&spa_namespace_lock); 3623fa9e4066Sahrens 36240373e76bSbonwick return (0); 3625fa9e4066Sahrens } 3626fa9e4066Sahrens 3627fa9e4066Sahrens /* 3628fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 3629fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3630fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3631fa9e4066Sahrens * 3632fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 3633fa9e4066Sahrens * existing device; in this case the two devices are made into their own 36343d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3635fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3636fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 3637fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 3638fa9e4066Sahrens * is automatically detached. 3639fa9e4066Sahrens */ 3640fa9e4066Sahrens int 3641ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3642fa9e4066Sahrens { 36433f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 3644fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3645fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 364699653d4eSeschrock vdev_ops_t *pvops; 36479b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 36489b3f6b42SEric Kustarz int newvd_isspare; 36499b3f6b42SEric Kustarz int error; 3650fa9e4066Sahrens 3651fa9e4066Sahrens txg = spa_vdev_enter(spa); 3652fa9e4066Sahrens 3653c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3654fa9e4066Sahrens 3655fa9e4066Sahrens if (oldvd == NULL) 3656fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3657fa9e4066Sahrens 36580e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 36590e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 36600e34b6a7Sbonwick 3661fa9e4066Sahrens pvd = oldvd->vdev_parent; 3662fa9e4066Sahrens 366399653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 36643d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 36653d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 36663d7072f8Seschrock 36673d7072f8Seschrock if (newrootvd->vdev_children != 1) 3668fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3669fa9e4066Sahrens 3670fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 3671fa9e4066Sahrens 3672fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3673fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3674fa9e4066Sahrens 367599653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3676fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3677fa9e4066Sahrens 36788654d025Sperrin /* 36798654d025Sperrin * Spares can't replace logs 36808654d025Sperrin */ 3681ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 36828654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 36838654d025Sperrin 368499653d4eSeschrock if (!replacing) { 368599653d4eSeschrock /* 368699653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 368799653d4eSeschrock * vdev. 368899653d4eSeschrock */ 368999653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 369099653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 369199653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 369299653d4eSeschrock 369399653d4eSeschrock pvops = &vdev_mirror_ops; 369499653d4eSeschrock } else { 369599653d4eSeschrock /* 369699653d4eSeschrock * Active hot spares can only be replaced by inactive hot 369799653d4eSeschrock * spares. 369899653d4eSeschrock */ 369999653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 370099653d4eSeschrock pvd->vdev_child[1] == oldvd && 370199653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 370299653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 370399653d4eSeschrock 370499653d4eSeschrock /* 370599653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 370699653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 370739c23413Seschrock * want to create a replacing vdev. The user is not allowed to 370839c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 370939c23413Seschrock * the same (spare replaces spare, non-spare replaces 371039c23413Seschrock * non-spare). 371199653d4eSeschrock */ 371299653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) 371399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 371439c23413Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 371539c23413Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 371639c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 371799653d4eSeschrock else if (pvd->vdev_ops != &vdev_spare_ops && 371899653d4eSeschrock newvd->vdev_isspare) 371999653d4eSeschrock pvops = &vdev_spare_ops; 372099653d4eSeschrock else 372199653d4eSeschrock pvops = &vdev_replacing_ops; 372299653d4eSeschrock } 372399653d4eSeschrock 37242a79c5feSlling /* 3725573ca77eSGeorge Wilson * Make sure the new device is big enough. 37262a79c5feSlling */ 3727573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3728fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3729fa9e4066Sahrens 3730ecc2d604Sbonwick /* 3731ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 3732ecc2d604Sbonwick * than the top-level vdev. 3733ecc2d604Sbonwick */ 3734ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3735fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3736fa9e4066Sahrens 3737fa9e4066Sahrens /* 3738fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 3739fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3740fa9e4066Sahrens */ 3741fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3742fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 3743fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3744fa9e4066Sahrens KM_SLEEP); 3745fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3746fa9e4066Sahrens newvd->vdev_path, "old"); 3747fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 3748fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 3749fa9e4066Sahrens oldvd->vdev_devid = NULL; 3750fa9e4066Sahrens } 3751fa9e4066Sahrens } 3752fa9e4066Sahrens 3753fa9e4066Sahrens /* 375499653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 375599653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 3756fa9e4066Sahrens */ 3757fa9e4066Sahrens if (pvd->vdev_ops != pvops) 3758fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 3759fa9e4066Sahrens 3760fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3761fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 3762fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 3763fa9e4066Sahrens 3764fa9e4066Sahrens /* 3765fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 3766fa9e4066Sahrens */ 3767fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 3768fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 376988ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 3770fa9e4066Sahrens vdev_add_child(pvd, newvd); 3771fa9e4066Sahrens 3772fa9e4066Sahrens tvd = newvd->vdev_top; 3773fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 3774fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3775fa9e4066Sahrens 3776fa9e4066Sahrens vdev_config_dirty(tvd); 3777fa9e4066Sahrens 3778fa9e4066Sahrens /* 37793f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 37803f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 37813f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 3782fa9e4066Sahrens */ 37833f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 3784fa9e4066Sahrens 37853f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 37863f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 3787fa9e4066Sahrens 37886809eb4eSEric Schrock if (newvd->vdev_isspare) { 378939c23413Seschrock spa_spare_activate(newvd); 37906809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 37916809eb4eSEric Schrock } 37926809eb4eSEric Schrock 3793e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 3794e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 37959b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 3796ea8dc4b6Seschrock 3797fa9e4066Sahrens /* 3798fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 3799fa9e4066Sahrens */ 3800ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3801fa9e4066Sahrens 38023f9d6ad7SLin Ling /* 38033f9d6ad7SLin Ling * Restart the resilver 38043f9d6ad7SLin Ling */ 38053f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 38063f9d6ad7SLin Ling 38073f9d6ad7SLin Ling /* 38083f9d6ad7SLin Ling * Commit the config 38093f9d6ad7SLin Ling */ 38103f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 3811fa9e4066Sahrens 38123f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL, 38133f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 3814c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 3815c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 3816c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 38179b3f6b42SEric Kustarz 38189b3f6b42SEric Kustarz spa_strfree(oldvdpath); 38199b3f6b42SEric Kustarz spa_strfree(newvdpath); 38209b3f6b42SEric Kustarz 3821fa9e4066Sahrens return (0); 3822fa9e4066Sahrens } 3823fa9e4066Sahrens 3824fa9e4066Sahrens /* 3825fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 3826fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 3827fa9e4066Sahrens * is a replacing vdev. 3828fa9e4066Sahrens */ 3829fa9e4066Sahrens int 38308ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3831fa9e4066Sahrens { 3832fa9e4066Sahrens uint64_t txg; 38338ad4d6ddSJeff Bonwick int error; 3834fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3835fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 383699653d4eSeschrock boolean_t unspare = B_FALSE; 383799653d4eSeschrock uint64_t unspare_guid; 3838bf82a41bSeschrock size_t len; 38391195e687SMark J Musante char *vdpath; 3840fa9e4066Sahrens 3841fa9e4066Sahrens txg = spa_vdev_enter(spa); 3842fa9e4066Sahrens 3843c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3844fa9e4066Sahrens 3845fa9e4066Sahrens if (vd == NULL) 3846fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3847fa9e4066Sahrens 38480e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 38490e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 38500e34b6a7Sbonwick 3851fa9e4066Sahrens pvd = vd->vdev_parent; 3852fa9e4066Sahrens 38538ad4d6ddSJeff Bonwick /* 38548ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 38558ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 38568ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 38578ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 38588ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 38598ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 38608ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 38618ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 38628ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 38638ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 38648ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 38658ad4d6ddSJeff Bonwick */ 38668ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 38678ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 38688ad4d6ddSJeff Bonwick 3869fa9e4066Sahrens /* 3870fa9e4066Sahrens * If replace_done is specified, only remove this device if it's 387199653d4eSeschrock * the first child of a replacing vdev. For the 'spare' vdev, either 387299653d4eSeschrock * disk can be removed. 387399653d4eSeschrock */ 387499653d4eSeschrock if (replace_done) { 387599653d4eSeschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 387699653d4eSeschrock if (vd->vdev_id != 0) 387799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 387899653d4eSeschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 387999653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 388099653d4eSeschrock } 388199653d4eSeschrock } 388299653d4eSeschrock 388399653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 3884e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3885fa9e4066Sahrens 3886fa9e4066Sahrens /* 388799653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 3888fa9e4066Sahrens */ 3889fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 389099653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 389199653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 3892fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3893fa9e4066Sahrens 3894fa9e4066Sahrens /* 38958ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 38968ad4d6ddSJeff Bonwick * we cannot safely detach it. 3897fa9e4066Sahrens */ 38988ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 3899fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3900fa9e4066Sahrens 39018ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 3902fa9e4066Sahrens 3903bf82a41bSeschrock /* 3904bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 3905bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 3906bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 3907bf82a41bSeschrock */ 3908bf82a41bSeschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 3909bf82a41bSeschrock pvd->vdev_child[0]->vdev_path != NULL && 3910bf82a41bSeschrock pvd->vdev_child[1]->vdev_path != NULL) { 3911bf82a41bSeschrock ASSERT(pvd->vdev_child[1] == vd); 3912bf82a41bSeschrock cvd = pvd->vdev_child[0]; 3913bf82a41bSeschrock len = strlen(vd->vdev_path); 3914bf82a41bSeschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 3915bf82a41bSeschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 3916bf82a41bSeschrock spa_strfree(cvd->vdev_path); 3917bf82a41bSeschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 3918bf82a41bSeschrock } 3919bf82a41bSeschrock } 3920bf82a41bSeschrock 392199653d4eSeschrock /* 392299653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 392399653d4eSeschrock * that the spare should become a real disk, and be removed from the 392499653d4eSeschrock * active spare list for the pool. 392599653d4eSeschrock */ 392699653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 39278ad4d6ddSJeff Bonwick vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 392899653d4eSeschrock unspare = B_TRUE; 392999653d4eSeschrock 3930fa9e4066Sahrens /* 3931fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 3932fa9e4066Sahrens * This must be done after all other error cases are handled, 3933fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 3934fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 3935fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 3936fa9e4066Sahrens * it's being detached! 3937fa9e4066Sahrens */ 393839c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3939fa9e4066Sahrens 3940fa9e4066Sahrens /* 3941fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 3942fa9e4066Sahrens */ 3943fa9e4066Sahrens vdev_remove_child(pvd, vd); 3944fa9e4066Sahrens vdev_compact_children(pvd); 3945fa9e4066Sahrens 3946fa9e4066Sahrens /* 3947fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 3948fa9e4066Sahrens */ 3949fa9e4066Sahrens cvd = pvd->vdev_child[0]; 3950fa9e4066Sahrens 395199653d4eSeschrock /* 395299653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 39538ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 39548ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 39558ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 39568ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 39578ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 39588ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 395999653d4eSeschrock */ 396099653d4eSeschrock if (unspare) { 396199653d4eSeschrock ASSERT(cvd->vdev_isspare); 396239c23413Seschrock spa_spare_remove(cvd); 396399653d4eSeschrock unspare_guid = cvd->vdev_guid; 39648ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 396599653d4eSeschrock } 396699653d4eSeschrock 3967fa9e4066Sahrens /* 3968fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 3969fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 3970fa9e4066Sahrens */ 3971fa9e4066Sahrens if (pvd->vdev_children == 1) 3972fa9e4066Sahrens vdev_remove_parent(cvd); 3973fa9e4066Sahrens 3974fa9e4066Sahrens /* 3975fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 3976fa9e4066Sahrens * may have been the previous top-level vdev. 3977fa9e4066Sahrens */ 3978fa9e4066Sahrens tvd = cvd->vdev_top; 3979fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3980fa9e4066Sahrens 3981fa9e4066Sahrens /* 398239c23413Seschrock * Reevaluate the parent vdev state. 3983fa9e4066Sahrens */ 39843d7072f8Seschrock vdev_propagate_state(cvd); 3985fa9e4066Sahrens 3986fa9e4066Sahrens /* 3987573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 3988573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 3989573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 3990573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 3991573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 3992fa9e4066Sahrens */ 3993573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 3994573ca77eSGeorge Wilson vdev_reopen(tvd); 3995573ca77eSGeorge Wilson vdev_expand(tvd, txg); 3996573ca77eSGeorge Wilson } 3997fa9e4066Sahrens 3998fa9e4066Sahrens vdev_config_dirty(tvd); 3999fa9e4066Sahrens 4000fa9e4066Sahrens /* 400139c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 400239c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 400339c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 400439c23413Seschrock * prevent vd from being accessed after it's freed. 4005fa9e4066Sahrens */ 40061195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 40078ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4008fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4009ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4010ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4011fa9e4066Sahrens 40123d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 40133d7072f8Seschrock 401499653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 401599653d4eSeschrock 40163f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL, 40171195e687SMark J Musante "vdev=%s", vdpath); 40181195e687SMark J Musante spa_strfree(vdpath); 40191195e687SMark J Musante 402099653d4eSeschrock /* 402139c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 402239c23413Seschrock * then we want to go through and remove the device from the hot spare 402339c23413Seschrock * list of every other pool. 402499653d4eSeschrock */ 402599653d4eSeschrock if (unspare) { 40268ad4d6ddSJeff Bonwick spa_t *myspa = spa; 402799653d4eSeschrock spa = NULL; 402899653d4eSeschrock mutex_enter(&spa_namespace_lock); 402999653d4eSeschrock while ((spa = spa_next(spa)) != NULL) { 403099653d4eSeschrock if (spa->spa_state != POOL_STATE_ACTIVE) 403199653d4eSeschrock continue; 40328ad4d6ddSJeff Bonwick if (spa == myspa) 40338ad4d6ddSJeff Bonwick continue; 40349af0a4dfSJeff Bonwick spa_open_ref(spa, FTAG); 40359af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 40363f9d6ad7SLin Ling (void) spa_vdev_remove(spa, unspare_guid, 40373f9d6ad7SLin Ling B_TRUE); 40389af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 40399af0a4dfSJeff Bonwick spa_close(spa, FTAG); 404099653d4eSeschrock } 404199653d4eSeschrock mutex_exit(&spa_namespace_lock); 404299653d4eSeschrock } 404399653d4eSeschrock 404499653d4eSeschrock return (error); 404599653d4eSeschrock } 404699653d4eSeschrock 40471195e687SMark J Musante /* 40481195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 40491195e687SMark J Musante */ 40501195e687SMark J Musante int 40511195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 40521195e687SMark J Musante nvlist_t *props, boolean_t exp) 40531195e687SMark J Musante { 40541195e687SMark J Musante int error = 0; 40551195e687SMark J Musante uint64_t txg, *glist; 40561195e687SMark J Musante spa_t *newspa; 40571195e687SMark J Musante uint_t c, children, lastlog; 40581195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 40591195e687SMark J Musante dmu_tx_t *tx; 40601195e687SMark J Musante char *altroot = NULL; 40611195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 40621195e687SMark J Musante boolean_t activate_slog; 40631195e687SMark J Musante 40641195e687SMark J Musante if (!spa_writeable(spa)) 40651195e687SMark J Musante return (EROFS); 40661195e687SMark J Musante 40671195e687SMark J Musante txg = spa_vdev_enter(spa); 40681195e687SMark J Musante 40691195e687SMark J Musante /* clear the log and flush everything up to now */ 40701195e687SMark J Musante activate_slog = spa_passivate_log(spa); 40711195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 40721195e687SMark J Musante error = spa_offline_log(spa); 40731195e687SMark J Musante txg = spa_vdev_config_enter(spa); 40741195e687SMark J Musante 40751195e687SMark J Musante if (activate_slog) 40761195e687SMark J Musante spa_activate_log(spa); 40771195e687SMark J Musante 40781195e687SMark J Musante if (error != 0) 40791195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 40801195e687SMark J Musante 40811195e687SMark J Musante /* check new spa name before going any further */ 40821195e687SMark J Musante if (spa_lookup(newname) != NULL) 40831195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 40841195e687SMark J Musante 40851195e687SMark J Musante /* 40861195e687SMark J Musante * scan through all the children to ensure they're all mirrors 40871195e687SMark J Musante */ 40881195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 40891195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 40901195e687SMark J Musante &children) != 0) 40911195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 40921195e687SMark J Musante 40931195e687SMark J Musante /* first, check to ensure we've got the right child count */ 40941195e687SMark J Musante rvd = spa->spa_root_vdev; 40951195e687SMark J Musante lastlog = 0; 40961195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 40971195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 40981195e687SMark J Musante 40991195e687SMark J Musante /* don't count the holes & logs as children */ 41001195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 41011195e687SMark J Musante if (lastlog == 0) 41021195e687SMark J Musante lastlog = c; 41031195e687SMark J Musante continue; 41041195e687SMark J Musante } 41051195e687SMark J Musante 41061195e687SMark J Musante lastlog = 0; 41071195e687SMark J Musante } 41081195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 41091195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 41101195e687SMark J Musante 41111195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 41121195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 41131195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 41141195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 41151195e687SMark J Musante 41161195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 41171195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 41181195e687SMark J Musante 41191195e687SMark J Musante /* then, loop over each vdev and validate it */ 41201195e687SMark J Musante for (c = 0; c < children; c++) { 41211195e687SMark J Musante uint64_t is_hole = 0; 41221195e687SMark J Musante 41231195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 41241195e687SMark J Musante &is_hole); 41251195e687SMark J Musante 41261195e687SMark J Musante if (is_hole != 0) { 41271195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 41281195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 41291195e687SMark J Musante continue; 41301195e687SMark J Musante } else { 41311195e687SMark J Musante error = EINVAL; 41321195e687SMark J Musante break; 41331195e687SMark J Musante } 41341195e687SMark J Musante } 41351195e687SMark J Musante 41361195e687SMark J Musante /* which disk is going to be split? */ 41371195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 41381195e687SMark J Musante &glist[c]) != 0) { 41391195e687SMark J Musante error = EINVAL; 41401195e687SMark J Musante break; 41411195e687SMark J Musante } 41421195e687SMark J Musante 41431195e687SMark J Musante /* look it up in the spa */ 41441195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 41451195e687SMark J Musante if (vml[c] == NULL) { 41461195e687SMark J Musante error = ENODEV; 41471195e687SMark J Musante break; 41481195e687SMark J Musante } 41491195e687SMark J Musante 41501195e687SMark J Musante /* make sure there's nothing stopping the split */ 41511195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 41521195e687SMark J Musante vml[c]->vdev_islog || 41531195e687SMark J Musante vml[c]->vdev_ishole || 41541195e687SMark J Musante vml[c]->vdev_isspare || 41551195e687SMark J Musante vml[c]->vdev_isl2cache || 41561195e687SMark J Musante !vdev_writeable(vml[c]) || 4157d41c4376SMark J Musante vml[c]->vdev_children != 0 || 41581195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 41591195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 41601195e687SMark J Musante error = EINVAL; 41611195e687SMark J Musante break; 41621195e687SMark J Musante } 41631195e687SMark J Musante 41641195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 41651195e687SMark J Musante error = EBUSY; 41661195e687SMark J Musante break; 41671195e687SMark J Musante } 41681195e687SMark J Musante 41691195e687SMark J Musante /* we need certain info from the top level */ 41701195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 41711195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 41721195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 41731195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 41741195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 41751195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 41761195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 41771195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 41781195e687SMark J Musante } 41791195e687SMark J Musante 41801195e687SMark J Musante if (error != 0) { 41811195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 41821195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 41831195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 41841195e687SMark J Musante } 41851195e687SMark J Musante 41861195e687SMark J Musante /* stop writers from using the disks */ 41871195e687SMark J Musante for (c = 0; c < children; c++) { 41881195e687SMark J Musante if (vml[c] != NULL) 41891195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 41901195e687SMark J Musante } 41911195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 41921195e687SMark J Musante 41931195e687SMark J Musante /* 41941195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 41951195e687SMark J Musante * will disappear once the config is regenerated. 41961195e687SMark J Musante */ 41971195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 41981195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 41991195e687SMark J Musante glist, children) == 0); 42001195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 42011195e687SMark J Musante 420298295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 42031195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 42041195e687SMark J Musante nvl) == 0); 420598295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 42061195e687SMark J Musante spa->spa_config_splitting = nvl; 42071195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 42081195e687SMark J Musante 42091195e687SMark J Musante /* configure and create the new pool */ 42101195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 42111195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 42121195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 42131195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 42141195e687SMark J Musante spa_version(spa)) == 0); 42151195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 42161195e687SMark J Musante spa->spa_config_txg) == 0); 42171195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 42181195e687SMark J Musante spa_generate_guid(NULL)) == 0); 42191195e687SMark J Musante (void) nvlist_lookup_string(props, 42201195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 42211195e687SMark J Musante 4222d41c4376SMark J Musante /* add the new pool to the namespace */ 42231195e687SMark J Musante newspa = spa_add(newname, config, altroot); 42241195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 42251195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 42261195e687SMark J Musante 42271195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 42281195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 42291195e687SMark J Musante 42301195e687SMark J Musante if (zio_injection_enabled) 42311195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 42321195e687SMark J Musante 42331195e687SMark J Musante spa_activate(newspa, spa_mode_global); 42341195e687SMark J Musante spa_async_suspend(newspa); 42351195e687SMark J Musante 42361195e687SMark J Musante /* create the new pool from the disks of the original pool */ 42371195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 42381195e687SMark J Musante if (error) 42391195e687SMark J Musante goto out; 42401195e687SMark J Musante 42411195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 42421195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 42431195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 42441195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 42451195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 42461195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 42471195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 42481195e687SMark J Musante B_TRUE)); 42491195e687SMark J Musante } 42501195e687SMark J Musante 42511195e687SMark J Musante /* set the props */ 42521195e687SMark J Musante if (props != NULL) { 42531195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 42541195e687SMark J Musante error = spa_prop_set(newspa, props); 42551195e687SMark J Musante if (error) 42561195e687SMark J Musante goto out; 42571195e687SMark J Musante } 42581195e687SMark J Musante 42591195e687SMark J Musante /* flush everything */ 42601195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 42611195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 42621195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 42631195e687SMark J Musante 42641195e687SMark J Musante if (zio_injection_enabled) 42651195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 42661195e687SMark J Musante 42671195e687SMark J Musante spa_async_resume(newspa); 42681195e687SMark J Musante 42691195e687SMark J Musante /* finally, update the original pool's config */ 42701195e687SMark J Musante txg = spa_vdev_config_enter(spa); 42711195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 42721195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 42731195e687SMark J Musante if (error != 0) 42741195e687SMark J Musante dmu_tx_abort(tx); 42751195e687SMark J Musante for (c = 0; c < children; c++) { 42761195e687SMark J Musante if (vml[c] != NULL) { 42771195e687SMark J Musante vdev_split(vml[c]); 42781195e687SMark J Musante if (error == 0) 42793f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_DETACH, 42803f9d6ad7SLin Ling spa, tx, "vdev=%s", 42811195e687SMark J Musante vml[c]->vdev_path); 42821195e687SMark J Musante vdev_free(vml[c]); 42831195e687SMark J Musante } 42841195e687SMark J Musante } 42851195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 42861195e687SMark J Musante spa->spa_config_splitting = NULL; 42871195e687SMark J Musante nvlist_free(nvl); 42881195e687SMark J Musante if (error == 0) 42891195e687SMark J Musante dmu_tx_commit(tx); 42901195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 42911195e687SMark J Musante 42921195e687SMark J Musante if (zio_injection_enabled) 42931195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 42941195e687SMark J Musante 42951195e687SMark J Musante /* split is complete; log a history record */ 42963f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL, 42971195e687SMark J Musante "split new pool %s from pool %s", newname, spa_name(spa)); 42981195e687SMark J Musante 42991195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 43001195e687SMark J Musante 43011195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 43021195e687SMark J Musante if (exp) 43031195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 43041195e687SMark J Musante B_FALSE, B_FALSE); 43051195e687SMark J Musante 43061195e687SMark J Musante return (error); 43071195e687SMark J Musante 43081195e687SMark J Musante out: 43091195e687SMark J Musante spa_unload(newspa); 43101195e687SMark J Musante spa_deactivate(newspa); 43111195e687SMark J Musante spa_remove(newspa); 43121195e687SMark J Musante 43131195e687SMark J Musante txg = spa_vdev_config_enter(spa); 431498295d61SMark J Musante 431598295d61SMark J Musante /* re-online all offlined disks */ 431698295d61SMark J Musante for (c = 0; c < children; c++) { 431798295d61SMark J Musante if (vml[c] != NULL) 431898295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 431998295d61SMark J Musante } 432098295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 432198295d61SMark J Musante 43221195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 43231195e687SMark J Musante spa->spa_config_splitting = NULL; 4324d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 43251195e687SMark J Musante 43261195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 43271195e687SMark J Musante return (error); 43281195e687SMark J Musante } 43291195e687SMark J Musante 4330e14bb325SJeff Bonwick static nvlist_t * 4331e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 433299653d4eSeschrock { 4333e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 4334e14bb325SJeff Bonwick uint64_t guid; 433599653d4eSeschrock 4336e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 4337e14bb325SJeff Bonwick &guid) == 0); 433899653d4eSeschrock 4339e14bb325SJeff Bonwick if (guid == target_guid) 4340e14bb325SJeff Bonwick return (nvpp[i]); 434199653d4eSeschrock } 434299653d4eSeschrock 4343e14bb325SJeff Bonwick return (NULL); 4344fa94a07fSbrendan } 4345fa94a07fSbrendan 4346e14bb325SJeff Bonwick static void 4347e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 4348e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 4349fa94a07fSbrendan { 4350e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 4351fa94a07fSbrendan 4352e14bb325SJeff Bonwick if (count > 1) 4353e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 4354fa94a07fSbrendan 4355e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 4356e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 4357e14bb325SJeff Bonwick continue; 4358e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 4359fa94a07fSbrendan } 4360fa94a07fSbrendan 4361e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 4362e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 4363fa94a07fSbrendan 4364e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 4365e14bb325SJeff Bonwick nvlist_free(newdev[i]); 4366fa94a07fSbrendan 4367e14bb325SJeff Bonwick if (count > 1) 4368e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 4369fa94a07fSbrendan } 4370fa94a07fSbrendan 437188ecc943SGeorge Wilson /* 437288ecc943SGeorge Wilson * Evacuate the device. 437388ecc943SGeorge Wilson */ 43743f9d6ad7SLin Ling static int 437588ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 437688ecc943SGeorge Wilson { 437788ecc943SGeorge Wilson uint64_t txg; 43783f9d6ad7SLin Ling int error = 0; 437988ecc943SGeorge Wilson 438088ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 438188ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 4382b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 438388ecc943SGeorge Wilson 438488ecc943SGeorge Wilson /* 438588ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 438688ecc943SGeorge Wilson * since we need to do I/O but we do keep the 438788ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 438888ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 438988ecc943SGeorge Wilson */ 439088ecc943SGeorge Wilson if (vd->vdev_islog) { 43913f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 43923f9d6ad7SLin Ling error = spa_offline_log(spa); 4393a1521560SJeff Bonwick } else { 43943f9d6ad7SLin Ling error = ENOTSUP; 439588ecc943SGeorge Wilson } 439688ecc943SGeorge Wilson 4397a1521560SJeff Bonwick if (error) 4398a1521560SJeff Bonwick return (error); 4399a1521560SJeff Bonwick 440088ecc943SGeorge Wilson /* 4401a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 4402a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 440388ecc943SGeorge Wilson */ 44043f9d6ad7SLin Ling ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 440588ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 440688ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 440788ecc943SGeorge Wilson vdev_dirty(vd, 0, NULL, txg); 440888ecc943SGeorge Wilson vdev_config_dirty(vd); 440988ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 441088ecc943SGeorge Wilson 441188ecc943SGeorge Wilson return (0); 441288ecc943SGeorge Wilson } 441388ecc943SGeorge Wilson 441488ecc943SGeorge Wilson /* 441588ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 441688ecc943SGeorge Wilson */ 44173f9d6ad7SLin Ling static void 4418a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 441988ecc943SGeorge Wilson { 442088ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 442188ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 442288ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 442388ecc943SGeorge Wilson 442488ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 442588ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4426b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 442788ecc943SGeorge Wilson 44283f9d6ad7SLin Ling /* 44293f9d6ad7SLin Ling * Only remove any devices which are empty. 44303f9d6ad7SLin Ling */ 44313f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 44323f9d6ad7SLin Ling return; 44333f9d6ad7SLin Ling 443488ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4435b24ab676SJeff Bonwick 4436b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 4437b24ab676SJeff Bonwick vdev_state_clean(vd); 4438b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 4439b24ab676SJeff Bonwick vdev_config_clean(vd); 4440b24ab676SJeff Bonwick 444188ecc943SGeorge Wilson vdev_free(vd); 444288ecc943SGeorge Wilson 444388ecc943SGeorge Wilson if (last_vdev) { 444488ecc943SGeorge Wilson vdev_compact_children(rvd); 444588ecc943SGeorge Wilson } else { 444688ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 444788ecc943SGeorge Wilson vdev_add_child(rvd, vd); 444888ecc943SGeorge Wilson } 4449fcbfa62bSLin Ling vdev_config_dirty(rvd); 4450fcbfa62bSLin Ling 4451fcbfa62bSLin Ling /* 4452fcbfa62bSLin Ling * Reassess the health of our root vdev. 4453fcbfa62bSLin Ling */ 4454fcbfa62bSLin Ling vdev_reopen(rvd); 445588ecc943SGeorge Wilson } 445688ecc943SGeorge Wilson 44573f9d6ad7SLin Ling /* 44583f9d6ad7SLin Ling * Remove a device from the pool - 44593f9d6ad7SLin Ling * 44603f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 44613f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 44623f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 44633f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 44643f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 44653f9d6ad7SLin Ling */ 44663f9d6ad7SLin Ling 4467fa94a07fSbrendan /* 4468fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 446988ecc943SGeorge Wilson * spares, slogs, and level 2 ARC devices. 4470fa94a07fSbrendan */ 4471fa94a07fSbrendan int 4472fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 4473fa94a07fSbrendan { 4474fa94a07fSbrendan vdev_t *vd; 4475a1521560SJeff Bonwick metaslab_group_t *mg; 4476e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 44778ad4d6ddSJeff Bonwick uint64_t txg = 0; 447888ecc943SGeorge Wilson uint_t nspares, nl2cache; 4479fa94a07fSbrendan int error = 0; 44808ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 4481fa94a07fSbrendan 44828ad4d6ddSJeff Bonwick if (!locked) 44838ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 4484fa94a07fSbrendan 4485c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4486fa94a07fSbrendan 4487fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 4488fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 4489e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 4490e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 4491e14bb325SJeff Bonwick /* 4492e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 4493e14bb325SJeff Bonwick * in this pool. 4494e14bb325SJeff Bonwick */ 4495e14bb325SJeff Bonwick if (vd == NULL || unspare) { 4496e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 4497e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 4498e14bb325SJeff Bonwick spa_load_spares(spa); 4499e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 4500e14bb325SJeff Bonwick } else { 4501e14bb325SJeff Bonwick error = EBUSY; 4502e14bb325SJeff Bonwick } 4503e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 4504fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 4505e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 4506e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 4507e14bb325SJeff Bonwick /* 4508e14bb325SJeff Bonwick * Cache devices can always be removed. 4509e14bb325SJeff Bonwick */ 4510e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 4511e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 4512fa94a07fSbrendan spa_load_l2cache(spa); 4513fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 451488ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 451588ecc943SGeorge Wilson ASSERT(!locked); 4516b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 451788ecc943SGeorge Wilson 451888ecc943SGeorge Wilson /* 451988ecc943SGeorge Wilson * XXX - Once we have bp-rewrite this should 452088ecc943SGeorge Wilson * become the common case. 452188ecc943SGeorge Wilson */ 452288ecc943SGeorge Wilson 4523a1521560SJeff Bonwick mg = vd->vdev_mg; 4524a1521560SJeff Bonwick 452588ecc943SGeorge Wilson /* 4526a1521560SJeff Bonwick * Stop allocating from this vdev. 452788ecc943SGeorge Wilson */ 4528a1521560SJeff Bonwick metaslab_group_passivate(mg); 452988ecc943SGeorge Wilson 4530b24ab676SJeff Bonwick /* 4531b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 4532b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 4533b24ab676SJeff Bonwick */ 4534b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 4535b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 4536b24ab676SJeff Bonwick 4537a1521560SJeff Bonwick /* 4538a1521560SJeff Bonwick * Attempt to evacuate the vdev. 4539a1521560SJeff Bonwick */ 4540a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 4541a1521560SJeff Bonwick 454288ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 454388ecc943SGeorge Wilson 4544a1521560SJeff Bonwick /* 4545a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 4546a1521560SJeff Bonwick */ 4547a1521560SJeff Bonwick if (error) { 4548a1521560SJeff Bonwick metaslab_group_activate(mg); 4549a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 4550a1521560SJeff Bonwick } 4551a1521560SJeff Bonwick 4552a1521560SJeff Bonwick /* 4553a1521560SJeff Bonwick * Clean up the vdev namespace. 4554a1521560SJeff Bonwick */ 4555a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 455688ecc943SGeorge Wilson 4557e14bb325SJeff Bonwick } else if (vd != NULL) { 4558e14bb325SJeff Bonwick /* 4559e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 4560e14bb325SJeff Bonwick */ 4561e14bb325SJeff Bonwick error = ENOTSUP; 4562e14bb325SJeff Bonwick } else { 4563e14bb325SJeff Bonwick /* 4564e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 4565e14bb325SJeff Bonwick */ 4566e14bb325SJeff Bonwick error = ENOENT; 4567fa94a07fSbrendan } 456899653d4eSeschrock 45698ad4d6ddSJeff Bonwick if (!locked) 45708ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 45718ad4d6ddSJeff Bonwick 45728ad4d6ddSJeff Bonwick return (error); 4573fa9e4066Sahrens } 4574fa9e4066Sahrens 4575fa9e4066Sahrens /* 45763d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 45773d7072f8Seschrock * current spared, so we can detach it. 4578fa9e4066Sahrens */ 4579ea8dc4b6Seschrock static vdev_t * 45803d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 4581fa9e4066Sahrens { 4582ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 4583fa9e4066Sahrens 4584573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 45853d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 4586ea8dc4b6Seschrock if (oldvd != NULL) 4587ea8dc4b6Seschrock return (oldvd); 4588ea8dc4b6Seschrock } 4589fa9e4066Sahrens 45903d7072f8Seschrock /* 45913d7072f8Seschrock * Check for a completed replacement. 45923d7072f8Seschrock */ 4593fa9e4066Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 4594ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 4595ea8dc4b6Seschrock newvd = vd->vdev_child[1]; 4596ea8dc4b6Seschrock 45978ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 4598e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 45998ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 4600ea8dc4b6Seschrock return (oldvd); 4601fa9e4066Sahrens } 4602ea8dc4b6Seschrock 46033d7072f8Seschrock /* 46043d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 46053d7072f8Seschrock */ 46063d7072f8Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 46073d7072f8Seschrock newvd = vd->vdev_child[0]; 46083d7072f8Seschrock oldvd = vd->vdev_child[1]; 46093d7072f8Seschrock 46103d7072f8Seschrock if (newvd->vdev_unspare && 46118ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 4612e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 46138ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) { 46143d7072f8Seschrock newvd->vdev_unspare = 0; 46153d7072f8Seschrock return (oldvd); 46163d7072f8Seschrock } 46173d7072f8Seschrock } 46183d7072f8Seschrock 4619ea8dc4b6Seschrock return (NULL); 4620fa9e4066Sahrens } 4621fa9e4066Sahrens 4622ea8dc4b6Seschrock static void 46233d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 4624fa9e4066Sahrens { 46258ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 46268ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 4627ea8dc4b6Seschrock 46288ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4629ea8dc4b6Seschrock 46303d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 46318ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 46328ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 4633ea8dc4b6Seschrock guid = vd->vdev_guid; 46348ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 46358ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 46368ad4d6ddSJeff Bonwick sguid = 0; 463799653d4eSeschrock /* 463899653d4eSeschrock * If we have just finished replacing a hot spared device, then 463999653d4eSeschrock * we need to detach the parent's first child (the original hot 464099653d4eSeschrock * spare) as well. 464199653d4eSeschrock */ 46428ad4d6ddSJeff Bonwick if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 464399653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 46448ad4d6ddSJeff Bonwick ASSERT(ppvd->vdev_children == 2); 46458ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 464699653d4eSeschrock } 46478ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 46488ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 4649ea8dc4b6Seschrock return; 46508ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 465199653d4eSeschrock return; 46528ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4653fa9e4066Sahrens } 4654fa9e4066Sahrens 46558ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4656fa9e4066Sahrens } 4657fa9e4066Sahrens 4658c67d9675Seschrock /* 4659b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 4660c67d9675Seschrock */ 4661c67d9675Seschrock int 46626809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 46636809eb4eSEric Schrock boolean_t ispath) 4664c67d9675Seschrock { 4665c5904d13Seschrock vdev_t *vd; 4666208044b8SGeorge Wilson boolean_t sync = B_FALSE; 4667c67d9675Seschrock 4668b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 4669c67d9675Seschrock 46706809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 4671b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 4672c67d9675Seschrock 46730e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 4674b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 46750e34b6a7Sbonwick 46766809eb4eSEric Schrock if (ispath) { 4677208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 4678208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 4679208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 4680208044b8SGeorge Wilson sync = B_TRUE; 4681208044b8SGeorge Wilson } 46826809eb4eSEric Schrock } else { 4683208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 4684208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 4685208044b8SGeorge Wilson sync = B_TRUE; 4686208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 46876809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 4688208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 4689208044b8SGeorge Wilson sync = B_TRUE; 4690208044b8SGeorge Wilson } 46916809eb4eSEric Schrock } 4692c67d9675Seschrock 4693208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 4694c67d9675Seschrock } 4695c67d9675Seschrock 46966809eb4eSEric Schrock int 46976809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 46986809eb4eSEric Schrock { 46996809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 47006809eb4eSEric Schrock } 47016809eb4eSEric Schrock 47026809eb4eSEric Schrock int 47036809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 47046809eb4eSEric Schrock { 47056809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 47066809eb4eSEric Schrock } 47076809eb4eSEric Schrock 4708fa9e4066Sahrens /* 4709fa9e4066Sahrens * ========================================================================== 47103f9d6ad7SLin Ling * SPA Scanning 4711fa9e4066Sahrens * ========================================================================== 4712fa9e4066Sahrens */ 4713fa9e4066Sahrens 4714ea8dc4b6Seschrock int 47153f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 4716fa9e4066Sahrens { 4717e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 47183f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 47193f9d6ad7SLin Ling return (EBUSY); 47203f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 47213f9d6ad7SLin Ling } 4722bb8b5132Sek 47233f9d6ad7SLin Ling int 47243f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 47253f9d6ad7SLin Ling { 47263f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 47273f9d6ad7SLin Ling 47283f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 4729fa9e4066Sahrens return (ENOTSUP); 4730fa9e4066Sahrens 4731fa9e4066Sahrens /* 4732088f3894Sahrens * If a resilver was requested, but there is no DTL on a 4733088f3894Sahrens * writeable leaf device, we have nothing to do. 4734fa9e4066Sahrens */ 47353f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 4736088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 4737088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 4738ea8dc4b6Seschrock return (0); 4739ea8dc4b6Seschrock } 4740fa9e4066Sahrens 47413f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 4742fa9e4066Sahrens } 4743fa9e4066Sahrens 4744ea8dc4b6Seschrock /* 4745ea8dc4b6Seschrock * ========================================================================== 4746ea8dc4b6Seschrock * SPA async task processing 4747ea8dc4b6Seschrock * ========================================================================== 4748ea8dc4b6Seschrock */ 4749ea8dc4b6Seschrock 4750ea8dc4b6Seschrock static void 47513d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4752fa9e4066Sahrens { 475349cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 475498d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 475598d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 475649cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 47571d713200SEric Schrock 47581d713200SEric Schrock /* 47591d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 47601d713200SEric Schrock * vdev_clear() as that will cause us to throw away 47611d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 47621d713200SEric Schrock * device, all of which is a waste. 47631d713200SEric Schrock */ 47641d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 47651d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 47661d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 47671d713200SEric Schrock 4768e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 4769ea8dc4b6Seschrock } 477049cf58c0SBrendan Gregg - Sun Microsystems 4771e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 477249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 4773ea8dc4b6Seschrock } 4774fa9e4066Sahrens 4775e14bb325SJeff Bonwick static void 4776e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 4777e14bb325SJeff Bonwick { 4778e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 477998d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 4780e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 4781e14bb325SJeff Bonwick } 4782e14bb325SJeff Bonwick 4783e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 4784e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 4785e14bb325SJeff Bonwick } 4786e14bb325SJeff Bonwick 4787573ca77eSGeorge Wilson static void 4788573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 4789573ca77eSGeorge Wilson { 4790573ca77eSGeorge Wilson sysevent_id_t eid; 4791573ca77eSGeorge Wilson nvlist_t *attr; 4792573ca77eSGeorge Wilson char *physpath; 4793573ca77eSGeorge Wilson 4794573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 4795573ca77eSGeorge Wilson return; 4796573ca77eSGeorge Wilson 4797573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 4798573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 4799573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 4800573ca77eSGeorge Wilson } 4801573ca77eSGeorge Wilson 4802573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 4803573ca77eSGeorge Wilson return; 4804573ca77eSGeorge Wilson 4805573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 4806573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 4807573ca77eSGeorge Wilson 4808573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4809573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 4810573ca77eSGeorge Wilson 4811573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 4812573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 4813573ca77eSGeorge Wilson 4814573ca77eSGeorge Wilson nvlist_free(attr); 4815573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 4816573ca77eSGeorge Wilson } 4817573ca77eSGeorge Wilson 4818ea8dc4b6Seschrock static void 4819ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 4820ea8dc4b6Seschrock { 4821e14bb325SJeff Bonwick int tasks; 4822ea8dc4b6Seschrock 4823ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 4824ea8dc4b6Seschrock 4825ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 4826ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 4827ea8dc4b6Seschrock spa->spa_async_tasks = 0; 4828ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 4829ea8dc4b6Seschrock 48300373e76bSbonwick /* 48310373e76bSbonwick * See if the config needs to be updated. 48320373e76bSbonwick */ 48330373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 4834b24ab676SJeff Bonwick uint64_t old_space, new_space; 4835573ca77eSGeorge Wilson 48360373e76bSbonwick mutex_enter(&spa_namespace_lock); 4837b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 48380373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 4839b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 48400373e76bSbonwick mutex_exit(&spa_namespace_lock); 4841573ca77eSGeorge Wilson 4842573ca77eSGeorge Wilson /* 4843573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 4844573ca77eSGeorge Wilson * then log an internal history event. 4845573ca77eSGeorge Wilson */ 4846b24ab676SJeff Bonwick if (new_space != old_space) { 48473f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_ONLINE, 48483f9d6ad7SLin Ling spa, NULL, 4849c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 4850b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 4851573ca77eSGeorge Wilson } 48520373e76bSbonwick } 48530373e76bSbonwick 4854ea8dc4b6Seschrock /* 48553d7072f8Seschrock * See if any devices need to be marked REMOVED. 4856ea8dc4b6Seschrock */ 4857e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 48588f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 48593d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 4860e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 486149cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 4862e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 486349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 4864e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 4865e14bb325SJeff Bonwick } 4866e14bb325SJeff Bonwick 4867573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 4868573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4869573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 4870573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 4871573ca77eSGeorge Wilson } 4872573ca77eSGeorge Wilson 4873e14bb325SJeff Bonwick /* 4874e14bb325SJeff Bonwick * See if any devices need to be probed. 4875e14bb325SJeff Bonwick */ 4876e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 48778f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 4878e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 4879e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 48803d7072f8Seschrock } 4881ea8dc4b6Seschrock 4882ea8dc4b6Seschrock /* 4883ea8dc4b6Seschrock * If any devices are done replacing, detach them. 4884ea8dc4b6Seschrock */ 48853d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 48863d7072f8Seschrock spa_vdev_resilver_done(spa); 4887fa9e4066Sahrens 4888ea8dc4b6Seschrock /* 4889ea8dc4b6Seschrock * Kick off a resilver. 4890ea8dc4b6Seschrock */ 4891088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 48923f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 4893ea8dc4b6Seschrock 4894ea8dc4b6Seschrock /* 4895ea8dc4b6Seschrock * Let the world know that we're done. 4896ea8dc4b6Seschrock */ 4897ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 4898ea8dc4b6Seschrock spa->spa_async_thread = NULL; 4899ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 4900ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 4901ea8dc4b6Seschrock thread_exit(); 4902ea8dc4b6Seschrock } 4903ea8dc4b6Seschrock 4904ea8dc4b6Seschrock void 4905ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 4906ea8dc4b6Seschrock { 4907ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 4908ea8dc4b6Seschrock spa->spa_async_suspended++; 4909ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 4910ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 4911ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 4912ea8dc4b6Seschrock } 4913ea8dc4b6Seschrock 4914ea8dc4b6Seschrock void 4915ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 4916ea8dc4b6Seschrock { 4917ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 4918ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 4919ea8dc4b6Seschrock spa->spa_async_suspended--; 4920ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 4921ea8dc4b6Seschrock } 4922ea8dc4b6Seschrock 4923ea8dc4b6Seschrock static void 4924ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 4925ea8dc4b6Seschrock { 4926ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 4927ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 49280373e76bSbonwick spa->spa_async_thread == NULL && 49290373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 4930ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 4931ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 4932ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 4933ea8dc4b6Seschrock } 4934ea8dc4b6Seschrock 4935ea8dc4b6Seschrock void 4936ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 4937ea8dc4b6Seschrock { 49383f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 4939ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 4940ea8dc4b6Seschrock spa->spa_async_tasks |= task; 4941ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 4942fa9e4066Sahrens } 4943fa9e4066Sahrens 4944fa9e4066Sahrens /* 4945fa9e4066Sahrens * ========================================================================== 4946fa9e4066Sahrens * SPA syncing routines 4947fa9e4066Sahrens * ========================================================================== 4948fa9e4066Sahrens */ 4949fa9e4066Sahrens static void 4950b24ab676SJeff Bonwick spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg) 4951fa9e4066Sahrens { 4952fa9e4066Sahrens blkptr_t blk; 4953fa9e4066Sahrens uint64_t itor = 0; 4954fa9e4066Sahrens uint8_t c = 1; 4955fa9e4066Sahrens 4956e14bb325SJeff Bonwick while (bplist_iterate(bpl, &itor, &blk) == 0) { 4957e14bb325SJeff Bonwick ASSERT(blk.blk_birth < txg); 4958b24ab676SJeff Bonwick zio_free(spa, txg, &blk); 4959e14bb325SJeff Bonwick } 4960fa9e4066Sahrens 4961fa9e4066Sahrens bplist_vacate(bpl, tx); 4962fa9e4066Sahrens 4963fa9e4066Sahrens /* 4964fa9e4066Sahrens * Pre-dirty the first block so we sync to convergence faster. 4965fa9e4066Sahrens * (Usually only the first block is needed.) 4966fa9e4066Sahrens */ 4967b24ab676SJeff Bonwick dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx); 4968b24ab676SJeff Bonwick } 4969b24ab676SJeff Bonwick 4970b24ab676SJeff Bonwick static void 4971b24ab676SJeff Bonwick spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 4972b24ab676SJeff Bonwick { 4973b24ab676SJeff Bonwick zio_t *zio = arg; 4974b24ab676SJeff Bonwick 4975b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 4976b24ab676SJeff Bonwick zio->io_flags)); 4977fa9e4066Sahrens } 4978fa9e4066Sahrens 4979fa9e4066Sahrens static void 498099653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 4981fa9e4066Sahrens { 4982fa9e4066Sahrens char *packed = NULL; 4983f7991ba4STim Haley size_t bufsize; 4984fa9e4066Sahrens size_t nvsize = 0; 4985fa9e4066Sahrens dmu_buf_t *db; 4986fa9e4066Sahrens 498799653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 4988fa9e4066Sahrens 4989f7991ba4STim Haley /* 4990f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 4991f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 4992f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 4993f7991ba4STim Haley */ 4994f7991ba4STim Haley bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 4995f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 4996fa9e4066Sahrens 499799653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 4998ea8dc4b6Seschrock KM_SLEEP) == 0); 4999f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5000fa9e4066Sahrens 5001f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5002fa9e4066Sahrens 5003f7991ba4STim Haley kmem_free(packed, bufsize); 5004fa9e4066Sahrens 500599653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5006fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5007fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5008ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5009fa9e4066Sahrens } 5010fa9e4066Sahrens 501199653d4eSeschrock static void 5012fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5013fa94a07fSbrendan const char *config, const char *entry) 501499653d4eSeschrock { 501599653d4eSeschrock nvlist_t *nvroot; 5016fa94a07fSbrendan nvlist_t **list; 501799653d4eSeschrock int i; 501899653d4eSeschrock 5019fa94a07fSbrendan if (!sav->sav_sync) 502099653d4eSeschrock return; 502199653d4eSeschrock 502299653d4eSeschrock /* 5023fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5024fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 50253d7072f8Seschrock * valid and the vdevs are labeled appropriately. 502699653d4eSeschrock */ 5027fa94a07fSbrendan if (sav->sav_object == 0) { 5028fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5029fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5030fa94a07fSbrendan sizeof (uint64_t), tx); 503199653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5032fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5033fa94a07fSbrendan &sav->sav_object, tx) == 0); 503499653d4eSeschrock } 503599653d4eSeschrock 503699653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5037fa94a07fSbrendan if (sav->sav_count == 0) { 5038fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 503999653d4eSeschrock } else { 5040fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5041fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5042fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 50433f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5044fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5045fa94a07fSbrendan sav->sav_count) == 0); 5046fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5047fa94a07fSbrendan nvlist_free(list[i]); 5048fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 504999653d4eSeschrock } 505099653d4eSeschrock 5051fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 505206eeb2adSek nvlist_free(nvroot); 505399653d4eSeschrock 5054fa94a07fSbrendan sav->sav_sync = B_FALSE; 505599653d4eSeschrock } 505699653d4eSeschrock 505799653d4eSeschrock static void 505899653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 505999653d4eSeschrock { 506099653d4eSeschrock nvlist_t *config; 506199653d4eSeschrock 5062e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 506399653d4eSeschrock return; 506499653d4eSeschrock 5065e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5066e14bb325SJeff Bonwick 5067e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 5068e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 5069e14bb325SJeff Bonwick 5070e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 507199653d4eSeschrock 507299653d4eSeschrock if (spa->spa_config_syncing) 507399653d4eSeschrock nvlist_free(spa->spa_config_syncing); 507499653d4eSeschrock spa->spa_config_syncing = config; 507599653d4eSeschrock 507699653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 507799653d4eSeschrock } 507899653d4eSeschrock 5079990b4856Slling /* 5080990b4856Slling * Set zpool properties. 5081990b4856Slling */ 5082b1b8ab34Slling static void 50833f9d6ad7SLin Ling spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 5084b1b8ab34Slling { 5085b1b8ab34Slling spa_t *spa = arg1; 5086b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 5087990b4856Slling nvlist_t *nvp = arg2; 5088990b4856Slling nvpair_t *elem; 50893d7072f8Seschrock uint64_t intval; 5090c5904d13Seschrock char *strval; 5091990b4856Slling zpool_prop_t prop; 5092990b4856Slling const char *propname; 5093990b4856Slling zprop_type_t proptype; 5094b1b8ab34Slling 5095e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 5096e14bb325SJeff Bonwick 5097990b4856Slling elem = NULL; 5098990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 5099990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 5100990b4856Slling case ZPOOL_PROP_VERSION: 5101990b4856Slling /* 5102990b4856Slling * Only set version for non-zpool-creation cases 5103990b4856Slling * (set/import). spa_create() needs special care 5104990b4856Slling * for version setting. 5105990b4856Slling */ 5106990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 5107990b4856Slling VERIFY(nvpair_value_uint64(elem, 5108990b4856Slling &intval) == 0); 5109990b4856Slling ASSERT(intval <= SPA_VERSION); 5110990b4856Slling ASSERT(intval >= spa_version(spa)); 5111990b4856Slling spa->spa_uberblock.ub_version = intval; 5112990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 5113990b4856Slling } 5114ecd6cf80Smarks break; 5115990b4856Slling 5116990b4856Slling case ZPOOL_PROP_ALTROOT: 5117990b4856Slling /* 5118990b4856Slling * 'altroot' is a non-persistent property. It should 5119990b4856Slling * have been set temporarily at creation or import time. 5120990b4856Slling */ 5121990b4856Slling ASSERT(spa->spa_root != NULL); 5122b1b8ab34Slling break; 51233d7072f8Seschrock 51242f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5125990b4856Slling /* 5126379c004dSEric Schrock * 'cachefile' is also a non-persisitent property. 5127990b4856Slling */ 51283d7072f8Seschrock break; 5129990b4856Slling default: 5130990b4856Slling /* 5131990b4856Slling * Set pool property values in the poolprops mos object. 5132990b4856Slling */ 5133990b4856Slling if (spa->spa_pool_props_object == 0) { 5134990b4856Slling VERIFY((spa->spa_pool_props_object = 5135990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 5136990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 5137990b4856Slling 5138990b4856Slling VERIFY(zap_update(mos, 5139990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 5140990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 5141990b4856Slling == 0); 5142990b4856Slling } 5143990b4856Slling 5144990b4856Slling /* normalize the property name */ 5145990b4856Slling propname = zpool_prop_to_name(prop); 5146990b4856Slling proptype = zpool_prop_get_type(prop); 5147990b4856Slling 5148990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 5149990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 5150990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 5151990b4856Slling VERIFY(zap_update(mos, 5152990b4856Slling spa->spa_pool_props_object, propname, 5153990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 5154990b4856Slling 5155990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 5156990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5157990b4856Slling 5158990b4856Slling if (proptype == PROP_TYPE_INDEX) { 5159990b4856Slling const char *unused; 5160990b4856Slling VERIFY(zpool_prop_index_to_string( 5161990b4856Slling prop, intval, &unused) == 0); 5162990b4856Slling } 5163990b4856Slling VERIFY(zap_update(mos, 5164990b4856Slling spa->spa_pool_props_object, propname, 5165990b4856Slling 8, 1, &intval, tx) == 0); 5166990b4856Slling } else { 5167990b4856Slling ASSERT(0); /* not allowed */ 5168990b4856Slling } 5169990b4856Slling 51700a4e9518Sgw switch (prop) { 51710a4e9518Sgw case ZPOOL_PROP_DELEGATION: 5172990b4856Slling spa->spa_delegation = intval; 51730a4e9518Sgw break; 51740a4e9518Sgw case ZPOOL_PROP_BOOTFS: 5175990b4856Slling spa->spa_bootfs = intval; 51760a4e9518Sgw break; 51770a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 51780a4e9518Sgw spa->spa_failmode = intval; 51790a4e9518Sgw break; 5180573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 5181573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 5182b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 5183b98131cfSEric Taylor spa_async_request(spa, 5184b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 5185573ca77eSGeorge Wilson break; 5186b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 5187b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 5188b24ab676SJeff Bonwick break; 51890a4e9518Sgw default: 51900a4e9518Sgw break; 51910a4e9518Sgw } 5192990b4856Slling } 5193990b4856Slling 5194990b4856Slling /* log internal history if this is not a zpool create */ 5195990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 5196990b4856Slling tx->tx_txg != TXG_INITIAL) { 51973f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_PROPSET, 51983f9d6ad7SLin Ling spa, tx, "%s %lld %s", 5199e14bb325SJeff Bonwick nvpair_name(elem), intval, spa_name(spa)); 5200b1b8ab34Slling } 5201b1b8ab34Slling } 5202e14bb325SJeff Bonwick 5203e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 5204b1b8ab34Slling } 5205b1b8ab34Slling 5206fa9e4066Sahrens /* 5207fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 5208fa9e4066Sahrens * part of the process, so we iterate until it converges. 5209fa9e4066Sahrens */ 5210fa9e4066Sahrens void 5211fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 5212fa9e4066Sahrens { 5213fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5214fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 5215b24ab676SJeff Bonwick bplist_t *defer_bpl = &spa->spa_deferred_bplist; 5216b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 52170373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 5218fa9e4066Sahrens vdev_t *vd; 5219fa9e4066Sahrens dmu_tx_t *tx; 5220e14bb325SJeff Bonwick int error; 5221fa9e4066Sahrens 5222fa9e4066Sahrens /* 5223fa9e4066Sahrens * Lock out configuration changes. 5224fa9e4066Sahrens */ 5225e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5226fa9e4066Sahrens 5227fa9e4066Sahrens spa->spa_syncing_txg = txg; 5228fa9e4066Sahrens spa->spa_sync_pass = 0; 5229fa9e4066Sahrens 5230e14bb325SJeff Bonwick /* 5231e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 5232e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 5233e14bb325SJeff Bonwick */ 5234e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 52358ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 52368ad4d6ddSJeff Bonwick /* 52378ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 52388ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 52398ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 52408ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 52418ad4d6ddSJeff Bonwick * into the root vdev tree. 52428ad4d6ddSJeff Bonwick */ 52438ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 52448ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 52458ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 52468ad4d6ddSJeff Bonwick vdev_state_clean(vd); 52478ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 52488ad4d6ddSJeff Bonwick } 52498ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 52508ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 5251e14bb325SJeff Bonwick } 5252e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 5253e14bb325SJeff Bonwick 5254b24ab676SJeff Bonwick VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj)); 5255fa9e4066Sahrens 525699653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 525799653d4eSeschrock 525899653d4eSeschrock /* 5259e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 526099653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 526199653d4eSeschrock */ 5262e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 5263e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 526499653d4eSeschrock int i; 526599653d4eSeschrock 526699653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 526799653d4eSeschrock vd = rvd->vdev_child[i]; 526899653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 526999653d4eSeschrock break; 527099653d4eSeschrock } 527199653d4eSeschrock if (i == rvd->vdev_children) { 527299653d4eSeschrock spa->spa_deflate = TRUE; 527399653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 527499653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 527599653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 527699653d4eSeschrock } 527799653d4eSeschrock } 527899653d4eSeschrock 5279088f3894Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 5280088f3894Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 5281088f3894Sahrens dsl_pool_create_origin(dp, tx); 5282088f3894Sahrens 5283088f3894Sahrens /* Keeping the origin open increases spa_minref */ 5284088f3894Sahrens spa->spa_minref += 3; 5285088f3894Sahrens } 5286088f3894Sahrens 5287088f3894Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 5288088f3894Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 5289088f3894Sahrens dsl_pool_upgrade_clones(dp, tx); 5290088f3894Sahrens } 5291088f3894Sahrens 5292fa9e4066Sahrens /* 52933f9d6ad7SLin Ling * If anything has changed in this txg, or if someone is waiting 52943f9d6ad7SLin Ling * for this txg to sync (eg, spa_vdev_remove()), push the 52953f9d6ad7SLin Ling * deferred frees from the previous txg. If not, leave them 52963f9d6ad7SLin Ling * alone so that we don't generate work on an otherwise idle 52973f9d6ad7SLin Ling * system. 5298fa9e4066Sahrens */ 5299fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 53001615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 53013f9d6ad7SLin Ling !txg_list_empty(&dp->dp_sync_tasks, txg) || 53023f9d6ad7SLin Ling ((dp->dp_scan->scn_phys.scn_state == DSS_SCANNING || 53033f9d6ad7SLin Ling txg_sync_waiting(dp)) && !spa_shutting_down(spa))) 5304b24ab676SJeff Bonwick spa_sync_deferred_bplist(spa, defer_bpl, tx, txg); 5305fa9e4066Sahrens 5306fa9e4066Sahrens /* 5307fa9e4066Sahrens * Iterate to convergence. 5308fa9e4066Sahrens */ 5309fa9e4066Sahrens do { 5310b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 5311fa9e4066Sahrens 5312fa9e4066Sahrens spa_sync_config_object(spa, tx); 5313fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 5314fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 5315fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 5316fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 5317ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 5318fa9e4066Sahrens dsl_pool_sync(dp, txg); 5319fa9e4066Sahrens 5320b24ab676SJeff Bonwick if (pass <= SYNC_PASS_DEFERRED_FREE) { 5321b24ab676SJeff Bonwick zio_t *zio = zio_root(spa, NULL, NULL, 0); 5322b24ab676SJeff Bonwick bplist_sync(free_bpl, spa_sync_free, zio, tx); 5323b24ab676SJeff Bonwick VERIFY(zio_wait(zio) == 0); 5324b24ab676SJeff Bonwick } else { 5325b24ab676SJeff Bonwick bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx); 5326fa9e4066Sahrens } 5327fa9e4066Sahrens 5328b24ab676SJeff Bonwick ddt_sync(spa, txg); 53293f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 5330afee20e4SGeorge Wilson 5331b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 5332b24ab676SJeff Bonwick vdev_sync(vd, txg); 5333b24ab676SJeff Bonwick 5334b24ab676SJeff Bonwick } while (dmu_objset_is_dirty(mos, txg)); 5335fa9e4066Sahrens 53367a21607fSGeorge Wilson ASSERT(list_is_empty(&free_bpl->bpl_queue)); 5337fa9e4066Sahrens 5338b24ab676SJeff Bonwick bplist_close(defer_bpl); 5339fa9e4066Sahrens 5340fa9e4066Sahrens /* 5341fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 5342fa9e4066Sahrens * to commit the transaction group. 53430373e76bSbonwick * 534417f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 534517f17c2dSbonwick * random top-level vdevs that are known to be visible in the 5346e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 5347e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 53480373e76bSbonwick */ 5349e14bb325SJeff Bonwick for (;;) { 5350e14bb325SJeff Bonwick /* 5351e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 5352e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 5353e14bb325SJeff Bonwick */ 5354e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5355e14bb325SJeff Bonwick 5356e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 5357e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 5358e14bb325SJeff Bonwick int svdcount = 0; 5359e14bb325SJeff Bonwick int children = rvd->vdev_children; 5360e14bb325SJeff Bonwick int c0 = spa_get_random(children); 5361e14bb325SJeff Bonwick 5362573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 5363e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 5364e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 5365e14bb325SJeff Bonwick continue; 5366e14bb325SJeff Bonwick svd[svdcount++] = vd; 5367e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 5368e14bb325SJeff Bonwick break; 5369e14bb325SJeff Bonwick } 53708956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 53718956713aSEric Schrock if (error != 0) 53728956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 53738956713aSEric Schrock B_TRUE); 5374e14bb325SJeff Bonwick } else { 5375e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 53768956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 53778956713aSEric Schrock if (error != 0) 53788956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 53798956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 53800373e76bSbonwick } 5381e14bb325SJeff Bonwick 5382e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 5383e14bb325SJeff Bonwick 5384e14bb325SJeff Bonwick if (error == 0) 5385e14bb325SJeff Bonwick break; 5386e14bb325SJeff Bonwick zio_suspend(spa, NULL); 5387e14bb325SJeff Bonwick zio_resume_wait(spa); 53880373e76bSbonwick } 538999653d4eSeschrock dmu_tx_commit(tx); 539099653d4eSeschrock 53910373e76bSbonwick /* 53920373e76bSbonwick * Clear the dirty config list. 5393fa9e4066Sahrens */ 5394e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 53950373e76bSbonwick vdev_config_clean(vd); 53960373e76bSbonwick 53970373e76bSbonwick /* 53980373e76bSbonwick * Now that the new config has synced transactionally, 53990373e76bSbonwick * let it become visible to the config cache. 54000373e76bSbonwick */ 54010373e76bSbonwick if (spa->spa_config_syncing != NULL) { 54020373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 54030373e76bSbonwick spa->spa_config_txg = txg; 54040373e76bSbonwick spa->spa_config_syncing = NULL; 54050373e76bSbonwick } 5406fa9e4066Sahrens 5407fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 5408fa9e4066Sahrens 5409b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 5410fa9e4066Sahrens 5411fa9e4066Sahrens /* 5412fa9e4066Sahrens * Update usable space statistics. 5413fa9e4066Sahrens */ 5414fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 5415fa9e4066Sahrens vdev_sync_done(vd, txg); 5416fa9e4066Sahrens 5417485bbbf5SGeorge Wilson spa_update_dspace(spa); 5418485bbbf5SGeorge Wilson 5419fa9e4066Sahrens /* 5420fa9e4066Sahrens * It had better be the case that we didn't dirty anything 542199653d4eSeschrock * since vdev_config_sync(). 5422fa9e4066Sahrens */ 5423fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 5424fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 5425fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 54267a21607fSGeorge Wilson ASSERT(list_is_empty(&defer_bpl->bpl_queue)); 54277a21607fSGeorge Wilson ASSERT(list_is_empty(&free_bpl->bpl_queue)); 5428b24ab676SJeff Bonwick 5429b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 5430fa9e4066Sahrens 5431e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 5432ea8dc4b6Seschrock 5433468c413aSTim Haley spa_handle_ignored_writes(spa); 5434468c413aSTim Haley 5435ea8dc4b6Seschrock /* 5436ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 5437ea8dc4b6Seschrock */ 5438ea8dc4b6Seschrock spa_async_dispatch(spa); 5439fa9e4066Sahrens } 5440fa9e4066Sahrens 5441fa9e4066Sahrens /* 5442fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 5443fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 5444fa9e4066Sahrens * sync. 5445fa9e4066Sahrens */ 5446fa9e4066Sahrens void 5447fa9e4066Sahrens spa_sync_allpools(void) 5448fa9e4066Sahrens { 5449fa9e4066Sahrens spa_t *spa = NULL; 5450fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5451fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 5452e14bb325SJeff Bonwick if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 5453fa9e4066Sahrens continue; 5454fa9e4066Sahrens spa_open_ref(spa, FTAG); 5455fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5456fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 5457fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5458fa9e4066Sahrens spa_close(spa, FTAG); 5459fa9e4066Sahrens } 5460fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5461fa9e4066Sahrens } 5462fa9e4066Sahrens 5463fa9e4066Sahrens /* 5464fa9e4066Sahrens * ========================================================================== 5465fa9e4066Sahrens * Miscellaneous routines 5466fa9e4066Sahrens * ========================================================================== 5467fa9e4066Sahrens */ 5468fa9e4066Sahrens 5469fa9e4066Sahrens /* 5470fa9e4066Sahrens * Remove all pools in the system. 5471fa9e4066Sahrens */ 5472fa9e4066Sahrens void 5473fa9e4066Sahrens spa_evict_all(void) 5474fa9e4066Sahrens { 5475fa9e4066Sahrens spa_t *spa; 5476fa9e4066Sahrens 5477fa9e4066Sahrens /* 5478fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 5479fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 5480fa9e4066Sahrens */ 5481fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5482fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 5483fa9e4066Sahrens /* 5484ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 5485ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 5486ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 5487fa9e4066Sahrens */ 5488fa9e4066Sahrens spa_open_ref(spa, FTAG); 5489fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5490ea8dc4b6Seschrock spa_async_suspend(spa); 5491fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5492fa9e4066Sahrens spa_close(spa, FTAG); 5493fa9e4066Sahrens 5494fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 5495fa9e4066Sahrens spa_unload(spa); 5496fa9e4066Sahrens spa_deactivate(spa); 5497fa9e4066Sahrens } 5498fa9e4066Sahrens spa_remove(spa); 5499fa9e4066Sahrens } 5500fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5501fa9e4066Sahrens } 5502ea8dc4b6Seschrock 5503ea8dc4b6Seschrock vdev_t * 55046809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 5505ea8dc4b6Seschrock { 5506c5904d13Seschrock vdev_t *vd; 5507c5904d13Seschrock int i; 5508c5904d13Seschrock 5509c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 5510c5904d13Seschrock return (vd); 5511c5904d13Seschrock 55126809eb4eSEric Schrock if (aux) { 5513c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 5514c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 55156809eb4eSEric Schrock if (vd->vdev_guid == guid) 55166809eb4eSEric Schrock return (vd); 55176809eb4eSEric Schrock } 55186809eb4eSEric Schrock 55196809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 55206809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 5521c5904d13Seschrock if (vd->vdev_guid == guid) 5522c5904d13Seschrock return (vd); 5523c5904d13Seschrock } 5524c5904d13Seschrock } 5525c5904d13Seschrock 5526c5904d13Seschrock return (NULL); 5527ea8dc4b6Seschrock } 5528eaca9bbdSeschrock 5529eaca9bbdSeschrock void 5530990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 5531eaca9bbdSeschrock { 5532e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5533eaca9bbdSeschrock 5534eaca9bbdSeschrock /* 5535eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 5536eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 5537eaca9bbdSeschrock * possible. 5538eaca9bbdSeschrock */ 5539e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 5540990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 5541eaca9bbdSeschrock 5542990b4856Slling spa->spa_uberblock.ub_version = version; 5543eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 5544eaca9bbdSeschrock 5545e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 554699653d4eSeschrock 554799653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 554899653d4eSeschrock } 554999653d4eSeschrock 555099653d4eSeschrock boolean_t 555199653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 555299653d4eSeschrock { 555399653d4eSeschrock int i; 555439c23413Seschrock uint64_t spareguid; 5555fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 555699653d4eSeschrock 5557fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5558fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 555999653d4eSeschrock return (B_TRUE); 556099653d4eSeschrock 5561fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 5562fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 5563fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 556439c23413Seschrock return (B_TRUE); 556539c23413Seschrock } 556639c23413Seschrock 556799653d4eSeschrock return (B_FALSE); 5568eaca9bbdSeschrock } 5569b1b8ab34Slling 557089a89ebfSlling /* 557189a89ebfSlling * Check if a pool has an active shared spare device. 557289a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 557389a89ebfSlling */ 557489a89ebfSlling static boolean_t 557589a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 557689a89ebfSlling { 557789a89ebfSlling int i, refcnt; 557889a89ebfSlling uint64_t pool; 557989a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 558089a89ebfSlling 558189a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 558289a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 558389a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 558489a89ebfSlling refcnt > 2) 558589a89ebfSlling return (B_TRUE); 558689a89ebfSlling } 558789a89ebfSlling 558889a89ebfSlling return (B_FALSE); 558989a89ebfSlling } 559089a89ebfSlling 55913d7072f8Seschrock /* 55923d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 55933d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 55943d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 55953d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 55963d7072f8Seschrock * or zdb as real changes. 55973d7072f8Seschrock */ 55983d7072f8Seschrock void 55993d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 56003d7072f8Seschrock { 56013d7072f8Seschrock #ifdef _KERNEL 56023d7072f8Seschrock sysevent_t *ev; 56033d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 56043d7072f8Seschrock sysevent_value_t value; 56053d7072f8Seschrock sysevent_id_t eid; 56063d7072f8Seschrock 56073d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 56083d7072f8Seschrock SE_SLEEP); 56093d7072f8Seschrock 56103d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 56113d7072f8Seschrock value.value.sv_string = spa_name(spa); 56123d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 56133d7072f8Seschrock goto done; 56143d7072f8Seschrock 56153d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 56163d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 56173d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 56183d7072f8Seschrock goto done; 56193d7072f8Seschrock 56203d7072f8Seschrock if (vd) { 56213d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 56223d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 56233d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 56243d7072f8Seschrock SE_SLEEP) != 0) 56253d7072f8Seschrock goto done; 56263d7072f8Seschrock 56273d7072f8Seschrock if (vd->vdev_path) { 56283d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 56293d7072f8Seschrock value.value.sv_string = vd->vdev_path; 56303d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 56313d7072f8Seschrock &value, SE_SLEEP) != 0) 56323d7072f8Seschrock goto done; 56333d7072f8Seschrock } 56343d7072f8Seschrock } 56353d7072f8Seschrock 5636b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 5637b01c3b58Seschrock goto done; 5638b01c3b58Seschrock attr = NULL; 5639b01c3b58Seschrock 56403d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 56413d7072f8Seschrock 56423d7072f8Seschrock done: 56433d7072f8Seschrock if (attr) 56443d7072f8Seschrock sysevent_free_attr(attr); 56453d7072f8Seschrock sysevent_free(ev); 56463d7072f8Seschrock #endif 56473d7072f8Seschrock } 5648