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 */ 25*5aeb9474SGarrett D'Amore /* 26*5aeb9474SGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 27*5aeb9474SGarrett D'Amore */ 28fa9e4066Sahrens 29fa9e4066Sahrens /* 30fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 31fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 32fa9e4066Sahrens * pool. 33fa9e4066Sahrens */ 34fa9e4066Sahrens 35fa9e4066Sahrens #include <sys/zfs_context.h> 36ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 37fa9e4066Sahrens #include <sys/spa_impl.h> 38fa9e4066Sahrens #include <sys/zio.h> 39fa9e4066Sahrens #include <sys/zio_checksum.h> 40fa9e4066Sahrens #include <sys/dmu.h> 41fa9e4066Sahrens #include <sys/dmu_tx.h> 42fa9e4066Sahrens #include <sys/zap.h> 43fa9e4066Sahrens #include <sys/zil.h> 44b24ab676SJeff Bonwick #include <sys/ddt.h> 45fa9e4066Sahrens #include <sys/vdev_impl.h> 46fa9e4066Sahrens #include <sys/metaslab.h> 4788ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 48fa9e4066Sahrens #include <sys/uberblock_impl.h> 49fa9e4066Sahrens #include <sys/txg.h> 50fa9e4066Sahrens #include <sys/avl.h> 51fa9e4066Sahrens #include <sys/dmu_traverse.h> 52b1b8ab34Slling #include <sys/dmu_objset.h> 53fa9e4066Sahrens #include <sys/unique.h> 54fa9e4066Sahrens #include <sys/dsl_pool.h> 55b1b8ab34Slling #include <sys/dsl_dataset.h> 56fa9e4066Sahrens #include <sys/dsl_dir.h> 57fa9e4066Sahrens #include <sys/dsl_prop.h> 58b1b8ab34Slling #include <sys/dsl_synctask.h> 59fa9e4066Sahrens #include <sys/fs/zfs.h> 60fa94a07fSbrendan #include <sys/arc.h> 61fa9e4066Sahrens #include <sys/callb.h> 6295173954Sek #include <sys/systeminfo.h> 63e7cbe64fSgw #include <sys/spa_boot.h> 64573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 653f9d6ad7SLin Ling #include <sys/dsl_scan.h> 66fa9e4066Sahrens 675679c89fSjv #ifdef _KERNEL 68dedec472SJack Meng #include <sys/bootprops.h> 6935a5a358SJonathan Adams #include <sys/callb.h> 7035a5a358SJonathan Adams #include <sys/cpupart.h> 7135a5a358SJonathan Adams #include <sys/pool.h> 7235a5a358SJonathan Adams #include <sys/sysdc.h> 7335a5a358SJonathan Adams #include <sys/zone.h> 745679c89fSjv #endif /* _KERNEL */ 755679c89fSjv 76990b4856Slling #include "zfs_prop.h" 77b7b97454Sperrin #include "zfs_comutil.h" 78990b4856Slling 7935a5a358SJonathan Adams typedef enum zti_modes { 802e0c549eSJonathan Adams zti_mode_fixed, /* value is # of threads (min 1) */ 812e0c549eSJonathan Adams zti_mode_online_percent, /* value is % of online CPUs */ 8235a5a358SJonathan Adams zti_mode_batch, /* cpu-intensive; value is ignored */ 8380eb36f2SGeorge Wilson zti_mode_null, /* don't create a taskq */ 842e0c549eSJonathan Adams zti_nmodes 8535a5a358SJonathan Adams } zti_modes_t; 86416e0cd8Sek 8780eb36f2SGeorge Wilson #define ZTI_FIX(n) { zti_mode_fixed, (n) } 8880eb36f2SGeorge Wilson #define ZTI_PCT(n) { zti_mode_online_percent, (n) } 8935a5a358SJonathan Adams #define ZTI_BATCH { zti_mode_batch, 0 } 9080eb36f2SGeorge Wilson #define ZTI_NULL { zti_mode_null, 0 } 912e0c549eSJonathan Adams 9280eb36f2SGeorge Wilson #define ZTI_ONE ZTI_FIX(1) 932e0c549eSJonathan Adams 942e0c549eSJonathan Adams typedef struct zio_taskq_info { 9580eb36f2SGeorge Wilson enum zti_modes zti_mode; 9680eb36f2SGeorge Wilson uint_t zti_value; 972e0c549eSJonathan Adams } zio_taskq_info_t; 982e0c549eSJonathan Adams 992e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 10035a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1012e0c549eSJonathan Adams }; 1022e0c549eSJonathan Adams 10380eb36f2SGeorge Wilson /* 10480eb36f2SGeorge Wilson * Define the taskq threads for the following I/O types: 10580eb36f2SGeorge Wilson * NULL, READ, WRITE, FREE, CLAIM, and IOCTL 10680eb36f2SGeorge Wilson */ 10780eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 10880eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 10980eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11035a5a358SJonathan Adams { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, 11135a5a358SJonathan Adams { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) }, 112c7cd2421SGeorge Wilson { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11380eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11480eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 1152e0c549eSJonathan Adams }; 1162e0c549eSJonathan Adams 1173f9d6ad7SLin Ling static dsl_syncfunc_t spa_sync_props; 11889a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1191195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1201195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1211195e687SMark J Musante char **ereport); 122cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 123990b4856Slling 12435a5a358SJonathan Adams uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 12535a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 12635a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 12735a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 12835a5a358SJonathan Adams 12935a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 13035a5a358SJonathan Adams 13135a5a358SJonathan Adams /* 13235a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 13335a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 13435a5a358SJonathan Adams */ 13535a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 13635a5a358SJonathan Adams 137990b4856Slling /* 138990b4856Slling * ========================================================================== 139990b4856Slling * SPA properties routines 140990b4856Slling * ========================================================================== 141990b4856Slling */ 142990b4856Slling 143990b4856Slling /* 144990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 145990b4856Slling */ 1469d82f4f6Slling static void 147990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 148990b4856Slling uint64_t intval, zprop_source_t src) 149990b4856Slling { 150990b4856Slling const char *propname = zpool_prop_to_name(prop); 151990b4856Slling nvlist_t *propval; 152990b4856Slling 1539d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1549d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 155990b4856Slling 1569d82f4f6Slling if (strval != NULL) 1579d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1589d82f4f6Slling else 1599d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 160990b4856Slling 1619d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 162990b4856Slling nvlist_free(propval); 163990b4856Slling } 164990b4856Slling 165990b4856Slling /* 166990b4856Slling * Get property values from the spa configuration. 167990b4856Slling */ 1689d82f4f6Slling static void 169990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 170990b4856Slling { 171379c004dSEric Schrock uint64_t size; 172485bbbf5SGeorge Wilson uint64_t alloc; 173990b4856Slling uint64_t cap, version; 174990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 175c5904d13Seschrock spa_config_dirent_t *dp; 176990b4856Slling 177e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 178e14bb325SJeff Bonwick 179379c004dSEric Schrock if (spa->spa_root_vdev != NULL) { 180485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 181b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 182379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 183379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 184485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 185485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 186485bbbf5SGeorge Wilson size - alloc, src); 187f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 188f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 189379c004dSEric Schrock 190485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 191379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 192379c004dSEric Schrock 193b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 194b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 195b24ab676SJeff Bonwick 196379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 197379c004dSEric Schrock spa->spa_root_vdev->vdev_state, src); 198379c004dSEric Schrock 199379c004dSEric Schrock version = spa_version(spa); 200379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 201379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 202379c004dSEric Schrock else 203379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 204379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 205379c004dSEric Schrock } 206990b4856Slling 2079d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 208990b4856Slling 2099d82f4f6Slling if (spa->spa_root != NULL) 2109d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2119d82f4f6Slling 0, ZPROP_SRC_LOCAL); 212990b4856Slling 213c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 214c5904d13Seschrock if (dp->scd_path == NULL) { 2159d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 216c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 217c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2189d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 219c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2202f8aaab3Seschrock } 2212f8aaab3Seschrock } 222990b4856Slling } 223990b4856Slling 224990b4856Slling /* 225990b4856Slling * Get zpool property values. 226990b4856Slling */ 227990b4856Slling int 228990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 229990b4856Slling { 230b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 231990b4856Slling zap_cursor_t zc; 232990b4856Slling zap_attribute_t za; 233990b4856Slling int err; 234990b4856Slling 2359d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 236990b4856Slling 237e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 238e14bb325SJeff Bonwick 239990b4856Slling /* 240990b4856Slling * Get properties from the spa config. 241990b4856Slling */ 2429d82f4f6Slling spa_prop_get_config(spa, nvp); 243990b4856Slling 244990b4856Slling /* If no pool property object, no more prop to get. */ 245afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 246990b4856Slling mutex_exit(&spa->spa_props_lock); 247990b4856Slling return (0); 248990b4856Slling } 249990b4856Slling 250990b4856Slling /* 251990b4856Slling * Get properties from the MOS pool property object. 252990b4856Slling */ 253990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 254990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 255990b4856Slling zap_cursor_advance(&zc)) { 256990b4856Slling uint64_t intval = 0; 257990b4856Slling char *strval = NULL; 258990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 259990b4856Slling zpool_prop_t prop; 260990b4856Slling 261990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 262990b4856Slling continue; 263990b4856Slling 264990b4856Slling switch (za.za_integer_length) { 265990b4856Slling case 8: 266990b4856Slling /* integer property */ 267990b4856Slling if (za.za_first_integer != 268990b4856Slling zpool_prop_default_numeric(prop)) 269990b4856Slling src = ZPROP_SRC_LOCAL; 270990b4856Slling 271990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 272990b4856Slling dsl_pool_t *dp; 273990b4856Slling dsl_dataset_t *ds = NULL; 274990b4856Slling 275990b4856Slling dp = spa_get_dsl(spa); 276990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 277745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 278745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 279990b4856Slling rw_exit(&dp->dp_config_rwlock); 280990b4856Slling break; 281990b4856Slling } 282990b4856Slling 283990b4856Slling strval = kmem_alloc( 284990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 285990b4856Slling KM_SLEEP); 286990b4856Slling dsl_dataset_name(ds, strval); 287745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 288990b4856Slling rw_exit(&dp->dp_config_rwlock); 289990b4856Slling } else { 290990b4856Slling strval = NULL; 291990b4856Slling intval = za.za_first_integer; 292990b4856Slling } 293990b4856Slling 2949d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 295990b4856Slling 296990b4856Slling if (strval != NULL) 297990b4856Slling kmem_free(strval, 298990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 299990b4856Slling 300990b4856Slling break; 301990b4856Slling 302990b4856Slling case 1: 303990b4856Slling /* string property */ 304990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 305990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 306990b4856Slling za.za_name, 1, za.za_num_integers, strval); 307990b4856Slling if (err) { 308990b4856Slling kmem_free(strval, za.za_num_integers); 309990b4856Slling break; 310990b4856Slling } 3119d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 312990b4856Slling kmem_free(strval, za.za_num_integers); 313990b4856Slling break; 314990b4856Slling 315990b4856Slling default: 316990b4856Slling break; 317990b4856Slling } 318990b4856Slling } 319990b4856Slling zap_cursor_fini(&zc); 320990b4856Slling mutex_exit(&spa->spa_props_lock); 321990b4856Slling out: 322990b4856Slling if (err && err != ENOENT) { 323990b4856Slling nvlist_free(*nvp); 3249d82f4f6Slling *nvp = NULL; 325990b4856Slling return (err); 326990b4856Slling } 327990b4856Slling 328990b4856Slling return (0); 329990b4856Slling } 330990b4856Slling 331990b4856Slling /* 332990b4856Slling * Validate the given pool properties nvlist and modify the list 333990b4856Slling * for the property values to be set. 334990b4856Slling */ 335990b4856Slling static int 336990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 337990b4856Slling { 338990b4856Slling nvpair_t *elem; 339990b4856Slling int error = 0, reset_bootfs = 0; 340990b4856Slling uint64_t objnum; 341990b4856Slling 342990b4856Slling elem = NULL; 343990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 344990b4856Slling zpool_prop_t prop; 345990b4856Slling char *propname, *strval; 346990b4856Slling uint64_t intval; 347990b4856Slling objset_t *os; 3482f8aaab3Seschrock char *slash; 349990b4856Slling 350990b4856Slling propname = nvpair_name(elem); 351990b4856Slling 352990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 353990b4856Slling return (EINVAL); 354990b4856Slling 355990b4856Slling switch (prop) { 356990b4856Slling case ZPOOL_PROP_VERSION: 357990b4856Slling error = nvpair_value_uint64(elem, &intval); 358990b4856Slling if (!error && 359990b4856Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 360990b4856Slling error = EINVAL; 361990b4856Slling break; 362990b4856Slling 363990b4856Slling case ZPOOL_PROP_DELEGATION: 364990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 365d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 366573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 367990b4856Slling error = nvpair_value_uint64(elem, &intval); 368990b4856Slling if (!error && intval > 1) 369990b4856Slling error = EINVAL; 370990b4856Slling break; 371990b4856Slling 372990b4856Slling case ZPOOL_PROP_BOOTFS: 37325f89ee2SJeff Bonwick /* 37425f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 37525f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 37625f89ee2SJeff Bonwick * the bootfs property cannot be set. 37725f89ee2SJeff Bonwick */ 378990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 379990b4856Slling error = ENOTSUP; 380990b4856Slling break; 381990b4856Slling } 382990b4856Slling 383990b4856Slling /* 38415e6edf1Sgw * Make sure the vdev config is bootable 385990b4856Slling */ 38615e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 387990b4856Slling error = ENOTSUP; 388990b4856Slling break; 389990b4856Slling } 390990b4856Slling 391990b4856Slling reset_bootfs = 1; 392990b4856Slling 393990b4856Slling error = nvpair_value_string(elem, &strval); 394990b4856Slling 395990b4856Slling if (!error) { 39615e6edf1Sgw uint64_t compress; 39715e6edf1Sgw 398990b4856Slling if (strval == NULL || strval[0] == '\0') { 399990b4856Slling objnum = zpool_prop_default_numeric( 400990b4856Slling ZPOOL_PROP_BOOTFS); 401990b4856Slling break; 402990b4856Slling } 403990b4856Slling 404503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 405990b4856Slling break; 40615e6edf1Sgw 407503ad85cSMatthew Ahrens /* Must be ZPL and not gzip compressed. */ 408503ad85cSMatthew Ahrens 409503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 410503ad85cSMatthew Ahrens error = ENOTSUP; 411503ad85cSMatthew Ahrens } else if ((error = dsl_prop_get_integer(strval, 41215e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 41315e6edf1Sgw &compress, NULL)) == 0 && 41415e6edf1Sgw !BOOTFS_COMPRESS_VALID(compress)) { 41515e6edf1Sgw error = ENOTSUP; 41615e6edf1Sgw } else { 41715e6edf1Sgw objnum = dmu_objset_id(os); 41815e6edf1Sgw } 419503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 420990b4856Slling } 421990b4856Slling break; 422e14bb325SJeff Bonwick 4230a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 4240a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 4250a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4260a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 4270a4e9518Sgw error = EINVAL; 4280a4e9518Sgw 4290a4e9518Sgw /* 4300a4e9518Sgw * This is a special case which only occurs when 4310a4e9518Sgw * the pool has completely failed. This allows 4320a4e9518Sgw * the user to change the in-core failmode property 4330a4e9518Sgw * without syncing it out to disk (I/Os might 4340a4e9518Sgw * currently be blocked). We do this by returning 4350a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 4360a4e9518Sgw * into thinking we encountered a property validation 4370a4e9518Sgw * error. 4380a4e9518Sgw */ 439e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 4400a4e9518Sgw spa->spa_failmode = intval; 4410a4e9518Sgw error = EIO; 4420a4e9518Sgw } 4430a4e9518Sgw break; 4442f8aaab3Seschrock 4452f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4462f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4472f8aaab3Seschrock break; 4482f8aaab3Seschrock 4492f8aaab3Seschrock if (strval[0] == '\0') 4502f8aaab3Seschrock break; 4512f8aaab3Seschrock 4522f8aaab3Seschrock if (strcmp(strval, "none") == 0) 4532f8aaab3Seschrock break; 4542f8aaab3Seschrock 4552f8aaab3Seschrock if (strval[0] != '/') { 4562f8aaab3Seschrock error = EINVAL; 4572f8aaab3Seschrock break; 4582f8aaab3Seschrock } 4592f8aaab3Seschrock 4602f8aaab3Seschrock slash = strrchr(strval, '/'); 4612f8aaab3Seschrock ASSERT(slash != NULL); 4622f8aaab3Seschrock 4632f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4642f8aaab3Seschrock strcmp(slash, "/..") == 0) 4652f8aaab3Seschrock error = EINVAL; 4662f8aaab3Seschrock break; 467b24ab676SJeff Bonwick 468b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 469b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 470b24ab676SJeff Bonwick error = ENOTSUP; 471b24ab676SJeff Bonwick else 472b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 473b24ab676SJeff Bonwick if (error == 0 && 474b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 475b24ab676SJeff Bonwick error = EINVAL; 476b24ab676SJeff Bonwick break; 477990b4856Slling } 478990b4856Slling 479990b4856Slling if (error) 480990b4856Slling break; 481990b4856Slling } 482990b4856Slling 483990b4856Slling if (!error && reset_bootfs) { 484990b4856Slling error = nvlist_remove(props, 485990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 486990b4856Slling 487990b4856Slling if (!error) { 488990b4856Slling error = nvlist_add_uint64(props, 489990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 490990b4856Slling } 491990b4856Slling } 492990b4856Slling 493990b4856Slling return (error); 494990b4856Slling } 495990b4856Slling 496379c004dSEric Schrock void 497379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 498379c004dSEric Schrock { 499379c004dSEric Schrock char *cachefile; 500379c004dSEric Schrock spa_config_dirent_t *dp; 501379c004dSEric Schrock 502379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 503379c004dSEric Schrock &cachefile) != 0) 504379c004dSEric Schrock return; 505379c004dSEric Schrock 506379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 507379c004dSEric Schrock KM_SLEEP); 508379c004dSEric Schrock 509379c004dSEric Schrock if (cachefile[0] == '\0') 510379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 511379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 512379c004dSEric Schrock dp->scd_path = NULL; 513379c004dSEric Schrock else 514379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 515379c004dSEric Schrock 516379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 517379c004dSEric Schrock if (need_sync) 518379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 519379c004dSEric Schrock } 520379c004dSEric Schrock 521990b4856Slling int 522990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 523990b4856Slling { 524990b4856Slling int error; 525379c004dSEric Schrock nvpair_t *elem; 526379c004dSEric Schrock boolean_t need_sync = B_FALSE; 527379c004dSEric Schrock zpool_prop_t prop; 528990b4856Slling 529990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 530990b4856Slling return (error); 531990b4856Slling 532379c004dSEric Schrock elem = NULL; 533379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 534379c004dSEric Schrock if ((prop = zpool_name_to_prop( 535379c004dSEric Schrock nvpair_name(elem))) == ZPROP_INVAL) 536379c004dSEric Schrock return (EINVAL); 537379c004dSEric Schrock 538f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 539f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 540f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 541379c004dSEric Schrock continue; 542379c004dSEric Schrock 543379c004dSEric Schrock need_sync = B_TRUE; 544379c004dSEric Schrock break; 545379c004dSEric Schrock } 546379c004dSEric Schrock 547379c004dSEric Schrock if (need_sync) 548379c004dSEric Schrock return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 549379c004dSEric Schrock spa, nvp, 3)); 550379c004dSEric Schrock else 551379c004dSEric Schrock return (0); 552990b4856Slling } 553990b4856Slling 554990b4856Slling /* 555990b4856Slling * If the bootfs property value is dsobj, clear it. 556990b4856Slling */ 557990b4856Slling void 558990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 559990b4856Slling { 560990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 561990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 562990b4856Slling spa->spa_pool_props_object, 563990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 564990b4856Slling spa->spa_bootfs = 0; 565990b4856Slling } 566990b4856Slling } 567990b4856Slling 568fa9e4066Sahrens /* 569fa9e4066Sahrens * ========================================================================== 570fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 571fa9e4066Sahrens * ========================================================================== 572fa9e4066Sahrens */ 573fa9e4066Sahrens 574ea8dc4b6Seschrock static int 575ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 576ea8dc4b6Seschrock { 577ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 578ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 579ea8dc4b6Seschrock int ret; 580ea8dc4b6Seschrock 581ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 582ea8dc4b6Seschrock sizeof (zbookmark_t)); 583ea8dc4b6Seschrock 584ea8dc4b6Seschrock if (ret < 0) 585ea8dc4b6Seschrock return (-1); 586ea8dc4b6Seschrock else if (ret > 0) 587ea8dc4b6Seschrock return (1); 588ea8dc4b6Seschrock else 589ea8dc4b6Seschrock return (0); 590ea8dc4b6Seschrock } 591ea8dc4b6Seschrock 592ea8dc4b6Seschrock /* 593ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 594ea8dc4b6Seschrock * re-initializes them in the process. 595ea8dc4b6Seschrock */ 596ea8dc4b6Seschrock void 597ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 598ea8dc4b6Seschrock { 599ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 600ea8dc4b6Seschrock 601ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 602ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 603ea8dc4b6Seschrock 604ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 605ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 606ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 607ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 608ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 609ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 610ea8dc4b6Seschrock } 611ea8dc4b6Seschrock 61235a5a358SJonathan Adams static taskq_t * 61335a5a358SJonathan Adams spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode, 61435a5a358SJonathan Adams uint_t value) 615fa9e4066Sahrens { 616*5aeb9474SGarrett D'Amore uint_t flags = 0; 61735a5a358SJonathan Adams boolean_t batch = B_FALSE; 618fa9e4066Sahrens 61935a5a358SJonathan Adams switch (mode) { 62035a5a358SJonathan Adams case zti_mode_null: 62135a5a358SJonathan Adams return (NULL); /* no taskq needed */ 622fa9e4066Sahrens 62335a5a358SJonathan Adams case zti_mode_fixed: 62435a5a358SJonathan Adams ASSERT3U(value, >=, 1); 62535a5a358SJonathan Adams value = MAX(value, 1); 62635a5a358SJonathan Adams break; 627fa9e4066Sahrens 62835a5a358SJonathan Adams case zti_mode_batch: 62935a5a358SJonathan Adams batch = B_TRUE; 63035a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT; 63135a5a358SJonathan Adams value = zio_taskq_batch_pct; 63235a5a358SJonathan Adams break; 63335a5a358SJonathan Adams 63435a5a358SJonathan Adams case zti_mode_online_percent: 63535a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT; 63635a5a358SJonathan Adams break; 63735a5a358SJonathan Adams 63835a5a358SJonathan Adams default: 63935a5a358SJonathan Adams panic("unrecognized mode for %s taskq (%u:%u) in " 64035a5a358SJonathan Adams "spa_activate()", 64135a5a358SJonathan Adams name, mode, value); 64235a5a358SJonathan Adams break; 64335a5a358SJonathan Adams } 64435a5a358SJonathan Adams 64535a5a358SJonathan Adams if (zio_taskq_sysdc && spa->spa_proc != &p0) { 64635a5a358SJonathan Adams if (batch) 64735a5a358SJonathan Adams flags |= TASKQ_DC_BATCH; 64835a5a358SJonathan Adams 64935a5a358SJonathan Adams return (taskq_create_sysdc(name, value, 50, INT_MAX, 65035a5a358SJonathan Adams spa->spa_proc, zio_taskq_basedc, flags)); 65135a5a358SJonathan Adams } 65235a5a358SJonathan Adams return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX, 65335a5a358SJonathan Adams spa->spa_proc, flags)); 65435a5a358SJonathan Adams } 65535a5a358SJonathan Adams 65635a5a358SJonathan Adams static void 65735a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 65835a5a358SJonathan Adams { 659e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 660e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 66180eb36f2SGeorge Wilson const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 66280eb36f2SGeorge Wilson enum zti_modes mode = ztip->zti_mode; 66380eb36f2SGeorge Wilson uint_t value = ztip->zti_value; 6642e0c549eSJonathan Adams char name[32]; 6652e0c549eSJonathan Adams 6662e0c549eSJonathan Adams (void) snprintf(name, sizeof (name), 66780eb36f2SGeorge Wilson "%s_%s", zio_type_name[t], zio_taskq_types[q]); 6682e0c549eSJonathan Adams 66935a5a358SJonathan Adams spa->spa_zio_taskq[t][q] = 67035a5a358SJonathan Adams spa_taskq_create(spa, name, mode, value); 67135a5a358SJonathan Adams } 67235a5a358SJonathan Adams } 67335a5a358SJonathan Adams } 67435a5a358SJonathan Adams 67535a5a358SJonathan Adams #ifdef _KERNEL 67635a5a358SJonathan Adams static void 67735a5a358SJonathan Adams spa_thread(void *arg) 67835a5a358SJonathan Adams { 67935a5a358SJonathan Adams callb_cpr_t cprinfo; 6802e0c549eSJonathan Adams 68135a5a358SJonathan Adams spa_t *spa = arg; 68235a5a358SJonathan Adams user_t *pu = PTOU(curproc); 6832e0c549eSJonathan Adams 68435a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 68535a5a358SJonathan Adams spa->spa_name); 6862e0c549eSJonathan Adams 68735a5a358SJonathan Adams ASSERT(curproc != &p0); 68835a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 68935a5a358SJonathan Adams "zpool-%s", spa->spa_name); 69035a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 6912e0c549eSJonathan Adams 69235a5a358SJonathan Adams /* bind this thread to the requested psrset */ 69335a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 69435a5a358SJonathan Adams pool_lock(); 69535a5a358SJonathan Adams mutex_enter(&cpu_lock); 69635a5a358SJonathan Adams mutex_enter(&pidlock); 69735a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 69880eb36f2SGeorge Wilson 69935a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 70035a5a358SJonathan Adams 0, NULL, NULL) == 0) { 70135a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 70235a5a358SJonathan Adams } else { 70335a5a358SJonathan Adams cmn_err(CE_WARN, 70435a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 70535a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 70635a5a358SJonathan Adams } 70735a5a358SJonathan Adams 70835a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 70935a5a358SJonathan Adams mutex_exit(&pidlock); 71035a5a358SJonathan Adams mutex_exit(&cpu_lock); 71135a5a358SJonathan Adams pool_unlock(); 71235a5a358SJonathan Adams } 71335a5a358SJonathan Adams 71435a5a358SJonathan Adams if (zio_taskq_sysdc) { 71535a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 71635a5a358SJonathan Adams } 71735a5a358SJonathan Adams 71835a5a358SJonathan Adams spa->spa_proc = curproc; 71935a5a358SJonathan Adams spa->spa_did = curthread->t_did; 72035a5a358SJonathan Adams 72135a5a358SJonathan Adams spa_create_zio_taskqs(spa); 72235a5a358SJonathan Adams 72335a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 72435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 72535a5a358SJonathan Adams 72635a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 72735a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 72835a5a358SJonathan Adams 72935a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 73035a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 73135a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 73235a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 73335a5a358SJonathan Adams 73435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 73535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 73635a5a358SJonathan Adams spa->spa_proc = &p0; 73735a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 73835a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 73935a5a358SJonathan Adams 74035a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 74135a5a358SJonathan Adams lwp_exit(); 74235a5a358SJonathan Adams } 74335a5a358SJonathan Adams #endif 74435a5a358SJonathan Adams 74535a5a358SJonathan Adams /* 74635a5a358SJonathan Adams * Activate an uninitialized pool. 74735a5a358SJonathan Adams */ 74835a5a358SJonathan Adams static void 74935a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 75035a5a358SJonathan Adams { 75135a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 75235a5a358SJonathan Adams 75335a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 75435a5a358SJonathan Adams spa->spa_mode = mode; 75535a5a358SJonathan Adams 75635a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 75735a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 75835a5a358SJonathan Adams 75935a5a358SJonathan Adams /* Try to create a covering process */ 76035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 76135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 76235a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 76335a5a358SJonathan Adams spa->spa_did = 0; 76435a5a358SJonathan Adams 76535a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 76635a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 76735a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 76835a5a358SJonathan Adams NULL, 0) == 0) { 76935a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 77035a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 77135a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 77235a5a358SJonathan Adams &spa->spa_proc_lock); 7732e0c549eSJonathan Adams } 77435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 77535a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 77635a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 77735a5a358SJonathan Adams } else { 77835a5a358SJonathan Adams #ifdef _KERNEL 77935a5a358SJonathan Adams cmn_err(CE_WARN, 78035a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 78135a5a358SJonathan Adams spa->spa_name); 78235a5a358SJonathan Adams #endif 783e14bb325SJeff Bonwick } 784fa9e4066Sahrens } 78535a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 78635a5a358SJonathan Adams 78735a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 78835a5a358SJonathan Adams if (spa->spa_proc == &p0) { 78935a5a358SJonathan Adams spa_create_zio_taskqs(spa); 79035a5a358SJonathan Adams } 791fa9e4066Sahrens 792e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 793e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 794e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 795e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 796fa9e4066Sahrens 797fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 798fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 799ea8dc4b6Seschrock 800ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 801ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 802ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 803ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 804ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 805ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 806fa9e4066Sahrens } 807fa9e4066Sahrens 808fa9e4066Sahrens /* 809fa9e4066Sahrens * Opposite of spa_activate(). 810fa9e4066Sahrens */ 811fa9e4066Sahrens static void 812fa9e4066Sahrens spa_deactivate(spa_t *spa) 813fa9e4066Sahrens { 814fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 815fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 816fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 81725f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 818fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 819fa9e4066Sahrens 820fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 821fa9e4066Sahrens 822e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 823e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 824fa9e4066Sahrens 825e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 826e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 82780eb36f2SGeorge Wilson if (spa->spa_zio_taskq[t][q] != NULL) 82880eb36f2SGeorge Wilson taskq_destroy(spa->spa_zio_taskq[t][q]); 829e14bb325SJeff Bonwick spa->spa_zio_taskq[t][q] = NULL; 830e14bb325SJeff Bonwick } 831fa9e4066Sahrens } 832fa9e4066Sahrens 833fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 834fa9e4066Sahrens spa->spa_normal_class = NULL; 835fa9e4066Sahrens 8368654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 8378654d025Sperrin spa->spa_log_class = NULL; 8388654d025Sperrin 839ea8dc4b6Seschrock /* 840ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 841ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 842ea8dc4b6Seschrock */ 843ea8dc4b6Seschrock spa_errlog_drain(spa); 844ea8dc4b6Seschrock 845ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 846ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 847ea8dc4b6Seschrock 848fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 84935a5a358SJonathan Adams 85035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 85135a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 85235a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 85335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 85435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 85535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 85635a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 85735a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 85835a5a358SJonathan Adams } 85935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 86035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 86135a5a358SJonathan Adams } 86235a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 86335a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 86435a5a358SJonathan Adams 86535a5a358SJonathan Adams /* 86635a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 86735a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 86835a5a358SJonathan Adams * it. 86935a5a358SJonathan Adams */ 87035a5a358SJonathan Adams if (spa->spa_did != 0) { 87135a5a358SJonathan Adams thread_join(spa->spa_did); 87235a5a358SJonathan Adams spa->spa_did = 0; 87335a5a358SJonathan Adams } 874fa9e4066Sahrens } 875fa9e4066Sahrens 876fa9e4066Sahrens /* 877fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 878fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 879fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 880fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 881fa9e4066Sahrens */ 88299653d4eSeschrock static int 88399653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 88499653d4eSeschrock uint_t id, int atype) 885fa9e4066Sahrens { 886fa9e4066Sahrens nvlist_t **child; 887573ca77eSGeorge Wilson uint_t children; 88899653d4eSeschrock int error; 889fa9e4066Sahrens 89099653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 89199653d4eSeschrock return (error); 892fa9e4066Sahrens 89399653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 89499653d4eSeschrock return (0); 895fa9e4066Sahrens 896e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 897e14bb325SJeff Bonwick &child, &children); 898e14bb325SJeff Bonwick 899e14bb325SJeff Bonwick if (error == ENOENT) 900e14bb325SJeff Bonwick return (0); 901e14bb325SJeff Bonwick 902e14bb325SJeff Bonwick if (error) { 90399653d4eSeschrock vdev_free(*vdp); 90499653d4eSeschrock *vdp = NULL; 90599653d4eSeschrock return (EINVAL); 906fa9e4066Sahrens } 907fa9e4066Sahrens 908573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 90999653d4eSeschrock vdev_t *vd; 91099653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 91199653d4eSeschrock atype)) != 0) { 91299653d4eSeschrock vdev_free(*vdp); 91399653d4eSeschrock *vdp = NULL; 91499653d4eSeschrock return (error); 915fa9e4066Sahrens } 916fa9e4066Sahrens } 917fa9e4066Sahrens 91899653d4eSeschrock ASSERT(*vdp != NULL); 91999653d4eSeschrock 92099653d4eSeschrock return (0); 921fa9e4066Sahrens } 922fa9e4066Sahrens 923fa9e4066Sahrens /* 924fa9e4066Sahrens * Opposite of spa_load(). 925fa9e4066Sahrens */ 926fa9e4066Sahrens static void 927fa9e4066Sahrens spa_unload(spa_t *spa) 928fa9e4066Sahrens { 92999653d4eSeschrock int i; 93099653d4eSeschrock 931e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 932e14bb325SJeff Bonwick 933ea8dc4b6Seschrock /* 934ea8dc4b6Seschrock * Stop async tasks. 935ea8dc4b6Seschrock */ 936ea8dc4b6Seschrock spa_async_suspend(spa); 937ea8dc4b6Seschrock 938fa9e4066Sahrens /* 939fa9e4066Sahrens * Stop syncing. 940fa9e4066Sahrens */ 941fa9e4066Sahrens if (spa->spa_sync_on) { 942fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 943fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 944fa9e4066Sahrens } 945fa9e4066Sahrens 946fa9e4066Sahrens /* 947e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 948fa9e4066Sahrens */ 94954d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 95054d692b7SGeorge Wilson (void) zio_wait(spa->spa_async_zio_root); 95154d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 95254d692b7SGeorge Wilson } 953fa9e4066Sahrens 954cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 955cde58dbcSMatthew Ahrens 956fa9e4066Sahrens /* 957fa9e4066Sahrens * Close the dsl pool. 958fa9e4066Sahrens */ 959fa9e4066Sahrens if (spa->spa_dsl_pool) { 960fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 961fa9e4066Sahrens spa->spa_dsl_pool = NULL; 962afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 963fa9e4066Sahrens } 964fa9e4066Sahrens 965b24ab676SJeff Bonwick ddt_unload(spa); 966b24ab676SJeff Bonwick 9678ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 9688ad4d6ddSJeff Bonwick 9698ad4d6ddSJeff Bonwick /* 9708ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 9718ad4d6ddSJeff Bonwick */ 9728ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 9738ad4d6ddSJeff Bonwick 974fa9e4066Sahrens /* 975fa9e4066Sahrens * Close all vdevs. 976fa9e4066Sahrens */ 9770e34b6a7Sbonwick if (spa->spa_root_vdev) 978fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 9790e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 980ea8dc4b6Seschrock 981fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 982fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 983fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 984fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 985fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 986fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 98799653d4eSeschrock } 988fa94a07fSbrendan if (spa->spa_spares.sav_config) { 989fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 990fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 991fa94a07fSbrendan } 9922ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 993fa94a07fSbrendan 994fa94a07fSbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 995fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 996fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 997fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 998fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 999fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1000fa94a07fSbrendan } 1001fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1002fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1003fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 100499653d4eSeschrock } 10052ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 100699653d4eSeschrock 1007ea8dc4b6Seschrock spa->spa_async_suspended = 0; 10088ad4d6ddSJeff Bonwick 10098ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1010fa9e4066Sahrens } 1011fa9e4066Sahrens 101299653d4eSeschrock /* 101399653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 101499653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1015fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1016fa94a07fSbrendan * then re-generate a more complete list including status information. 101799653d4eSeschrock */ 101899653d4eSeschrock static void 101999653d4eSeschrock spa_load_spares(spa_t *spa) 102099653d4eSeschrock { 102199653d4eSeschrock nvlist_t **spares; 102299653d4eSeschrock uint_t nspares; 102399653d4eSeschrock int i; 102439c23413Seschrock vdev_t *vd, *tvd; 102599653d4eSeschrock 1026e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1027e14bb325SJeff Bonwick 102899653d4eSeschrock /* 102999653d4eSeschrock * First, close and free any existing spare vdevs. 103099653d4eSeschrock */ 1031fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1032fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 103339c23413Seschrock 103439c23413Seschrock /* Undo the call to spa_activate() below */ 1035c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1036c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 103739c23413Seschrock spa_spare_remove(tvd); 103839c23413Seschrock vdev_close(vd); 103939c23413Seschrock vdev_free(vd); 104099653d4eSeschrock } 104139c23413Seschrock 1042fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1043fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1044fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 104599653d4eSeschrock 1046fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 104799653d4eSeschrock nspares = 0; 104899653d4eSeschrock else 1049fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 105099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 105199653d4eSeschrock 1052fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1053fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 105499653d4eSeschrock 105599653d4eSeschrock if (nspares == 0) 105699653d4eSeschrock return; 105799653d4eSeschrock 105899653d4eSeschrock /* 105999653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 106039c23413Seschrock * process. For each spare, there is potentially two different vdev_t 106139c23413Seschrock * structures associated with it: one in the list of spares (used only 106239c23413Seschrock * for basic validation purposes) and one in the active vdev 106339c23413Seschrock * configuration (if it's spared in). During this phase we open and 106439c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 106539c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 106699653d4eSeschrock */ 1067fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1068fa94a07fSbrendan KM_SLEEP); 1069fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 107099653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 107199653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 107299653d4eSeschrock ASSERT(vd != NULL); 107399653d4eSeschrock 1074fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 107599653d4eSeschrock 1076c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1077c5904d13Seschrock B_FALSE)) != NULL) { 107839c23413Seschrock if (!tvd->vdev_isspare) 107939c23413Seschrock spa_spare_add(tvd); 108039c23413Seschrock 108139c23413Seschrock /* 108239c23413Seschrock * We only mark the spare active if we were successfully 108339c23413Seschrock * able to load the vdev. Otherwise, importing a pool 108439c23413Seschrock * with a bad active spare would result in strange 108539c23413Seschrock * behavior, because multiple pool would think the spare 108639c23413Seschrock * is actively in use. 108739c23413Seschrock * 108839c23413Seschrock * There is a vulnerability here to an equally bizarre 108939c23413Seschrock * circumstance, where a dead active spare is later 109039c23413Seschrock * brought back to life (onlined or otherwise). Given 109139c23413Seschrock * the rarity of this scenario, and the extra complexity 109239c23413Seschrock * it adds, we ignore the possibility. 109339c23413Seschrock */ 109439c23413Seschrock if (!vdev_is_dead(tvd)) 109539c23413Seschrock spa_spare_activate(tvd); 109639c23413Seschrock } 109739c23413Seschrock 1098e14bb325SJeff Bonwick vd->vdev_top = vd; 10996809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1100e14bb325SJeff Bonwick 110199653d4eSeschrock if (vdev_open(vd) != 0) 110299653d4eSeschrock continue; 110399653d4eSeschrock 1104fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1105fa94a07fSbrendan spa_spare_add(vd); 110699653d4eSeschrock } 110799653d4eSeschrock 110899653d4eSeschrock /* 110999653d4eSeschrock * Recompute the stashed list of spares, with status information 111099653d4eSeschrock * this time. 111199653d4eSeschrock */ 1112fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 111399653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 111499653d4eSeschrock 1115fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1116fa94a07fSbrendan KM_SLEEP); 1117fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1118fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 11193f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1120fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1121fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1122fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 112399653d4eSeschrock nvlist_free(spares[i]); 1124fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1125fa94a07fSbrendan } 1126fa94a07fSbrendan 1127fa94a07fSbrendan /* 1128fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1129fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1130fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1131fa94a07fSbrendan * then re-generate a more complete list including status information. 1132fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1133fa94a07fSbrendan * not re-opened. 1134fa94a07fSbrendan */ 1135fa94a07fSbrendan static void 1136fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1137fa94a07fSbrendan { 1138fa94a07fSbrendan nvlist_t **l2cache; 1139fa94a07fSbrendan uint_t nl2cache; 1140fa94a07fSbrendan int i, j, oldnvdevs; 1141573ca77eSGeorge Wilson uint64_t guid; 1142fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1143fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1144fa94a07fSbrendan 1145e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1146e14bb325SJeff Bonwick 1147fa94a07fSbrendan if (sav->sav_config != NULL) { 1148fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1149fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1150fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1151fa94a07fSbrendan } else { 1152fa94a07fSbrendan nl2cache = 0; 1153fa94a07fSbrendan } 1154fa94a07fSbrendan 1155fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1156fa94a07fSbrendan oldnvdevs = sav->sav_count; 1157fa94a07fSbrendan sav->sav_vdevs = NULL; 1158fa94a07fSbrendan sav->sav_count = 0; 1159fa94a07fSbrendan 1160fa94a07fSbrendan /* 1161fa94a07fSbrendan * Process new nvlist of vdevs. 1162fa94a07fSbrendan */ 1163fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1164fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1165fa94a07fSbrendan &guid) == 0); 1166fa94a07fSbrendan 1167fa94a07fSbrendan newvdevs[i] = NULL; 1168fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1169fa94a07fSbrendan vd = oldvdevs[j]; 1170fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1171fa94a07fSbrendan /* 1172fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1173fa94a07fSbrendan */ 1174fa94a07fSbrendan newvdevs[i] = vd; 1175fa94a07fSbrendan oldvdevs[j] = NULL; 1176fa94a07fSbrendan break; 1177fa94a07fSbrendan } 1178fa94a07fSbrendan } 1179fa94a07fSbrendan 1180fa94a07fSbrendan if (newvdevs[i] == NULL) { 1181fa94a07fSbrendan /* 1182fa94a07fSbrendan * Create new vdev 1183fa94a07fSbrendan */ 1184fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1185fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1186fa94a07fSbrendan ASSERT(vd != NULL); 1187fa94a07fSbrendan newvdevs[i] = vd; 1188fa94a07fSbrendan 1189fa94a07fSbrendan /* 1190fa94a07fSbrendan * Commit this vdev as an l2cache device, 1191fa94a07fSbrendan * even if it fails to open. 1192fa94a07fSbrendan */ 1193fa94a07fSbrendan spa_l2cache_add(vd); 1194fa94a07fSbrendan 1195c5904d13Seschrock vd->vdev_top = vd; 1196c5904d13Seschrock vd->vdev_aux = sav; 1197c5904d13Seschrock 1198c5904d13Seschrock spa_l2cache_activate(vd); 1199c5904d13Seschrock 1200fa94a07fSbrendan if (vdev_open(vd) != 0) 1201fa94a07fSbrendan continue; 1202fa94a07fSbrendan 1203fa94a07fSbrendan (void) vdev_validate_aux(vd); 1204fa94a07fSbrendan 1205573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1206573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1207fa94a07fSbrendan } 1208fa94a07fSbrendan } 1209fa94a07fSbrendan 1210fa94a07fSbrendan /* 1211fa94a07fSbrendan * Purge vdevs that were dropped 1212fa94a07fSbrendan */ 1213fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1214fa94a07fSbrendan uint64_t pool; 1215fa94a07fSbrendan 1216fa94a07fSbrendan vd = oldvdevs[i]; 1217fa94a07fSbrendan if (vd != NULL) { 12188ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 12198ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1220fa94a07fSbrendan l2arc_remove_vdev(vd); 1221fa94a07fSbrendan (void) vdev_close(vd); 1222fa94a07fSbrendan spa_l2cache_remove(vd); 1223fa94a07fSbrendan } 1224fa94a07fSbrendan } 1225fa94a07fSbrendan 1226fa94a07fSbrendan if (oldvdevs) 1227fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1228fa94a07fSbrendan 1229fa94a07fSbrendan if (sav->sav_config == NULL) 1230fa94a07fSbrendan goto out; 1231fa94a07fSbrendan 1232fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1233fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1234fa94a07fSbrendan 1235fa94a07fSbrendan /* 1236fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1237fa94a07fSbrendan * information this time. 1238fa94a07fSbrendan */ 1239fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1240fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1241fa94a07fSbrendan 1242fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1243fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1244fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 12453f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1246fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1247fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1248fa94a07fSbrendan out: 1249fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1250fa94a07fSbrendan nvlist_free(l2cache[i]); 1251fa94a07fSbrendan if (sav->sav_count) 1252fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 125399653d4eSeschrock } 125499653d4eSeschrock 125599653d4eSeschrock static int 125699653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 125799653d4eSeschrock { 125899653d4eSeschrock dmu_buf_t *db; 125999653d4eSeschrock char *packed = NULL; 126099653d4eSeschrock size_t nvsize = 0; 126199653d4eSeschrock int error; 126299653d4eSeschrock *value = NULL; 126399653d4eSeschrock 126499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 126599653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 126699653d4eSeschrock dmu_buf_rele(db, FTAG); 126799653d4eSeschrock 126899653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 12697bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 12707bfdf011SNeil Perrin DMU_READ_PREFETCH); 127199653d4eSeschrock if (error == 0) 127299653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 127399653d4eSeschrock kmem_free(packed, nvsize); 127499653d4eSeschrock 127599653d4eSeschrock return (error); 127699653d4eSeschrock } 127799653d4eSeschrock 12783d7072f8Seschrock /* 12793d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 12803d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 12813d7072f8Seschrock */ 12823d7072f8Seschrock static void 12833d7072f8Seschrock spa_check_removed(vdev_t *vd) 12843d7072f8Seschrock { 1285573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 12863d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 12873d7072f8Seschrock 12883d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 12893d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 12903d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 12913d7072f8Seschrock } 12923d7072f8Seschrock } 12933d7072f8Seschrock 1294e6ca193dSGeorge Wilson /* 12954b964adaSGeorge Wilson * Validate the current config against the MOS config 1296e6ca193dSGeorge Wilson */ 12974b964adaSGeorge Wilson static boolean_t 12984b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1299e6ca193dSGeorge Wilson { 13004b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 13014b964adaSGeorge Wilson nvlist_t *nv; 13024b964adaSGeorge Wilson 13034b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 13044b964adaSGeorge Wilson 13054b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13064b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 13074b964adaSGeorge Wilson 13084b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1309e6ca193dSGeorge Wilson 131088ecc943SGeorge Wilson /* 13114b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 13124b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 13134b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 131488ecc943SGeorge Wilson */ 13154b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 13164b964adaSGeorge Wilson nvlist_t **child, *nv; 13174b964adaSGeorge Wilson uint64_t idx = 0; 13184b964adaSGeorge Wilson 13194b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 13204b964adaSGeorge Wilson KM_SLEEP); 13214b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1322e6ca193dSGeorge Wilson 13234b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 13244b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 13254b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 13264b964adaSGeorge Wilson 13274b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 13284b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 13294b964adaSGeorge Wilson mtvd->vdev_islog) 13304b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 13314b964adaSGeorge Wilson B_FALSE, 0); 13324b964adaSGeorge Wilson } 13334b964adaSGeorge Wilson 13344b964adaSGeorge Wilson if (idx) { 13354b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 13364b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 13374b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 13384b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 13394b964adaSGeorge Wilson 13404b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 13414b964adaSGeorge Wilson nvlist_free(child[i]); 13424b964adaSGeorge Wilson } 13434b964adaSGeorge Wilson nvlist_free(nv); 13444b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 13454b964adaSGeorge Wilson } 13464b964adaSGeorge Wilson 13474b964adaSGeorge Wilson /* 13484b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 13494b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 13504b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 13514b964adaSGeorge Wilson */ 135288ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 13534b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 13544b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 13554b964adaSGeorge Wilson 13564b964adaSGeorge Wilson /* 13574b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 13584b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 13594b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 13604b964adaSGeorge Wilson */ 13614b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 13624b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 13634b964adaSGeorge Wilson 13644b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 13654b964adaSGeorge Wilson continue; 13664b964adaSGeorge Wilson 13674b964adaSGeorge Wilson /* 13684b964adaSGeorge Wilson * Device specific actions. 13694b964adaSGeorge Wilson */ 13704b964adaSGeorge Wilson if (mtvd->vdev_islog) { 13714b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 13724b964adaSGeorge Wilson } else { 13734b964adaSGeorge Wilson /* 13744b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 13754b964adaSGeorge Wilson * support we should be able to handle 13764b964adaSGeorge Wilson * missing data devices by transitioning 13774b964adaSGeorge Wilson * the pool to readonly. 13784b964adaSGeorge Wilson */ 13794b964adaSGeorge Wilson continue; 13804b964adaSGeorge Wilson } 13814b964adaSGeorge Wilson 13824b964adaSGeorge Wilson /* 13834b964adaSGeorge Wilson * Swap the missing vdev with the data we were 13844b964adaSGeorge Wilson * able to obtain from the MOS config. 13854b964adaSGeorge Wilson */ 13864b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 13874b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 13884b964adaSGeorge Wilson 13894b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 13904b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 13914b964adaSGeorge Wilson 13924b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 13934b964adaSGeorge Wilson vdev_load(mtvd); 13944b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13954b964adaSGeorge Wilson 13964b964adaSGeorge Wilson vdev_reopen(rvd); 13974b964adaSGeorge Wilson } else if (mtvd->vdev_islog) { 13984b964adaSGeorge Wilson /* 13994b964adaSGeorge Wilson * Load the slog device's state from the MOS config 14004b964adaSGeorge Wilson * since it's possible that the label does not 14014b964adaSGeorge Wilson * contain the most up-to-date information. 14024b964adaSGeorge Wilson */ 14034b964adaSGeorge Wilson vdev_load_log_state(tvd, mtvd); 14044b964adaSGeorge Wilson vdev_reopen(tvd); 14054b964adaSGeorge Wilson } 1406e6ca193dSGeorge Wilson } 14074b964adaSGeorge Wilson vdev_free(mrvd); 140888ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 14094b964adaSGeorge Wilson 14104b964adaSGeorge Wilson /* 14114b964adaSGeorge Wilson * Ensure we were able to validate the config. 14124b964adaSGeorge Wilson */ 14134b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1414e6ca193dSGeorge Wilson } 1415e6ca193dSGeorge Wilson 1416b87f3af3Sperrin /* 1417b87f3af3Sperrin * Check for missing log devices 1418b87f3af3Sperrin */ 14194b964adaSGeorge Wilson static int 1420b87f3af3Sperrin spa_check_logs(spa_t *spa) 1421b87f3af3Sperrin { 1422b87f3af3Sperrin switch (spa->spa_log_state) { 1423b87f3af3Sperrin case SPA_LOG_MISSING: 1424b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1425b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1426b87f3af3Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1427b87f3af3Sperrin DS_FIND_CHILDREN)) { 14281195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1429b87f3af3Sperrin return (1); 1430b87f3af3Sperrin } 1431b87f3af3Sperrin break; 1432b87f3af3Sperrin } 1433b87f3af3Sperrin return (0); 1434b87f3af3Sperrin } 1435b87f3af3Sperrin 14361195e687SMark J Musante static boolean_t 14371195e687SMark J Musante spa_passivate_log(spa_t *spa) 14381195e687SMark J Musante { 14391195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 14401195e687SMark J Musante boolean_t slog_found = B_FALSE; 14411195e687SMark J Musante 14421195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 14431195e687SMark J Musante 14441195e687SMark J Musante if (!spa_has_slogs(spa)) 14451195e687SMark J Musante return (B_FALSE); 14461195e687SMark J Musante 14471195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 14481195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 14491195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 14501195e687SMark J Musante 14511195e687SMark J Musante if (tvd->vdev_islog) { 14521195e687SMark J Musante metaslab_group_passivate(mg); 14531195e687SMark J Musante slog_found = B_TRUE; 14541195e687SMark J Musante } 14551195e687SMark J Musante } 14561195e687SMark J Musante 14571195e687SMark J Musante return (slog_found); 14581195e687SMark J Musante } 14591195e687SMark J Musante 14601195e687SMark J Musante static void 14611195e687SMark J Musante spa_activate_log(spa_t *spa) 14621195e687SMark J Musante { 14631195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 14641195e687SMark J Musante 14651195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 14661195e687SMark J Musante 14671195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 14681195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 14691195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 14701195e687SMark J Musante 14711195e687SMark J Musante if (tvd->vdev_islog) 14721195e687SMark J Musante metaslab_group_activate(mg); 14731195e687SMark J Musante } 14741195e687SMark J Musante } 14751195e687SMark J Musante 14761195e687SMark J Musante int 14771195e687SMark J Musante spa_offline_log(spa_t *spa) 14781195e687SMark J Musante { 14791195e687SMark J Musante int error = 0; 14801195e687SMark J Musante 14811195e687SMark J Musante if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 14821195e687SMark J Musante NULL, DS_FIND_CHILDREN)) == 0) { 14831195e687SMark J Musante 14841195e687SMark J Musante /* 14851195e687SMark J Musante * We successfully offlined the log device, sync out the 14861195e687SMark J Musante * current txg so that the "stubby" block can be removed 14871195e687SMark J Musante * by zil_sync(). 14881195e687SMark J Musante */ 14891195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 14901195e687SMark J Musante } 14911195e687SMark J Musante return (error); 14921195e687SMark J Musante } 14931195e687SMark J Musante 1494b693757aSEric Schrock static void 1495b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1496b693757aSEric Schrock { 1497b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1498b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1499b693757aSEric Schrock } 1500b693757aSEric Schrock 1501b24ab676SJeff Bonwick void 1502b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1503b24ab676SJeff Bonwick { 1504b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1505b24ab676SJeff Bonwick 1506b24ab676SJeff Bonwick if (zio->io_error) 1507b24ab676SJeff Bonwick return; 1508b24ab676SJeff Bonwick 1509b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1510b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1511b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1512b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1513b24ab676SJeff Bonwick } 1514b24ab676SJeff Bonwick 1515468c413aSTim Haley typedef struct spa_load_error { 1516c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1517468c413aSTim Haley uint64_t sle_data_count; 1518468c413aSTim Haley } spa_load_error_t; 1519468c413aSTim Haley 1520468c413aSTim Haley static void 1521468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1522468c413aSTim Haley { 1523468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1524468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1525468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1526468c413aSTim Haley int error = zio->io_error; 1527468c413aSTim Haley 1528468c413aSTim Haley if (error) { 1529468c413aSTim Haley if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 1530468c413aSTim Haley type != DMU_OT_INTENT_LOG) 1531c8ee1847SVictor Latushkin atomic_add_64(&sle->sle_meta_count, 1); 1532468c413aSTim Haley else 1533468c413aSTim Haley atomic_add_64(&sle->sle_data_count, 1); 1534468c413aSTim Haley } 1535468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1536468c413aSTim Haley } 1537468c413aSTim Haley 1538468c413aSTim Haley /*ARGSUSED*/ 1539468c413aSTim Haley static int 1540b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 15413f9d6ad7SLin Ling arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1542468c413aSTim Haley { 1543468c413aSTim Haley if (bp != NULL) { 1544468c413aSTim Haley zio_t *rio = arg; 1545468c413aSTim Haley size_t size = BP_GET_PSIZE(bp); 1546468c413aSTim Haley void *data = zio_data_buf_alloc(size); 1547468c413aSTim Haley 1548468c413aSTim Haley zio_nowait(zio_read(rio, spa, bp, data, size, 1549468c413aSTim Haley spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1550468c413aSTim Haley ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1551468c413aSTim Haley ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1552468c413aSTim Haley } 1553468c413aSTim Haley return (0); 1554468c413aSTim Haley } 1555468c413aSTim Haley 1556468c413aSTim Haley static int 1557468c413aSTim Haley spa_load_verify(spa_t *spa) 1558468c413aSTim Haley { 1559468c413aSTim Haley zio_t *rio; 1560468c413aSTim Haley spa_load_error_t sle = { 0 }; 1561468c413aSTim Haley zpool_rewind_policy_t policy; 1562468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1563468c413aSTim Haley int error; 1564468c413aSTim Haley 1565c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1566c8ee1847SVictor Latushkin 1567c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1568c8ee1847SVictor Latushkin return (0); 1569c8ee1847SVictor Latushkin 1570468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1571468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1572468c413aSTim Haley 1573bbfd46c4SJeff Bonwick error = traverse_pool(spa, spa->spa_verify_min_txg, 1574bbfd46c4SJeff Bonwick TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 1575468c413aSTim Haley 1576468c413aSTim Haley (void) zio_wait(rio); 1577468c413aSTim Haley 1578c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1579468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1580468c413aSTim Haley 1581c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1582468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 15834b964adaSGeorge Wilson int64_t loss = 0; 15844b964adaSGeorge Wilson 1585468c413aSTim Haley verify_ok = B_TRUE; 1586468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1587468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 15884b964adaSGeorge Wilson 15894b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 15904b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 15914b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 15924b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 15934b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 15944b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 15954b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1596a33cae98STim Haley } else { 1597a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1598468c413aSTim Haley } 1599468c413aSTim Haley 1600468c413aSTim Haley if (error) { 1601468c413aSTim Haley if (error != ENXIO && error != EIO) 1602468c413aSTim Haley error = EIO; 1603468c413aSTim Haley return (error); 1604468c413aSTim Haley } 1605468c413aSTim Haley 1606468c413aSTim Haley return (verify_ok ? 0 : EIO); 1607468c413aSTim Haley } 1608468c413aSTim Haley 16091195e687SMark J Musante /* 16101195e687SMark J Musante * Find a value in the pool props object. 16111195e687SMark J Musante */ 16121195e687SMark J Musante static void 16131195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 16141195e687SMark J Musante { 16151195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 16161195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 16171195e687SMark J Musante } 16181195e687SMark J Musante 16191195e687SMark J Musante /* 16201195e687SMark J Musante * Find a value in the pool directory object. 16211195e687SMark J Musante */ 16221195e687SMark J Musante static int 16231195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 16241195e687SMark J Musante { 16251195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 16261195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 16271195e687SMark J Musante } 16281195e687SMark J Musante 16291195e687SMark J Musante static int 16301195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 16311195e687SMark J Musante { 16321195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 16331195e687SMark J Musante return (err); 16341195e687SMark J Musante } 16351195e687SMark J Musante 16361195e687SMark J Musante /* 16371195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 16381195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 16391195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 16401195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 16411195e687SMark J Musante * 16421195e687SMark J Musante * This function determines what to do with that list: either rejoin 16431195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 16441195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 16451195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 16461195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 16471195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 16481195e687SMark J Musante * 1649d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 1650d41c4376SMark J Musante * the original pool. 16511195e687SMark J Musante */ 16521195e687SMark J Musante static void 16531195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 16541195e687SMark J Musante { 16551195e687SMark J Musante uint_t extracted; 16561195e687SMark J Musante uint64_t *glist; 16571195e687SMark J Musante uint_t i, gcount; 16581195e687SMark J Musante nvlist_t *nvl; 16591195e687SMark J Musante vdev_t **vd; 16601195e687SMark J Musante boolean_t attempt_reopen; 16611195e687SMark J Musante 16621195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 16631195e687SMark J Musante return; 16641195e687SMark J Musante 16651195e687SMark J Musante /* check that the config is complete */ 16661195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 16671195e687SMark J Musante &glist, &gcount) != 0) 16681195e687SMark J Musante return; 16691195e687SMark J Musante 16701195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 16711195e687SMark J Musante 16721195e687SMark J Musante /* attempt to online all the vdevs & validate */ 16731195e687SMark J Musante attempt_reopen = B_TRUE; 16741195e687SMark J Musante for (i = 0; i < gcount; i++) { 16751195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 16761195e687SMark J Musante continue; 16771195e687SMark J Musante 16781195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 16791195e687SMark J Musante if (vd[i] == NULL) { 16801195e687SMark J Musante /* 16811195e687SMark J Musante * Don't bother attempting to reopen the disks; 16821195e687SMark J Musante * just do the split. 16831195e687SMark J Musante */ 16841195e687SMark J Musante attempt_reopen = B_FALSE; 16851195e687SMark J Musante } else { 16861195e687SMark J Musante /* attempt to re-online it */ 16871195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 16881195e687SMark J Musante } 16891195e687SMark J Musante } 16901195e687SMark J Musante 16911195e687SMark J Musante if (attempt_reopen) { 16921195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 16931195e687SMark J Musante 16941195e687SMark J Musante /* check each device to see what state it's in */ 16951195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 16961195e687SMark J Musante if (vd[i] != NULL && 16971195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 16981195e687SMark J Musante break; 16991195e687SMark J Musante ++extracted; 17001195e687SMark J Musante } 17011195e687SMark J Musante } 17021195e687SMark J Musante 17031195e687SMark J Musante /* 17041195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 17051195e687SMark J Musante * even attempted to look at them, then we split them off for 17061195e687SMark J Musante * good. 17071195e687SMark J Musante */ 17081195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 17091195e687SMark J Musante for (i = 0; i < gcount; i++) 17101195e687SMark J Musante if (vd[i] != NULL) 17111195e687SMark J Musante vdev_split(vd[i]); 17121195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 17131195e687SMark J Musante } 17141195e687SMark J Musante 17151195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 17161195e687SMark J Musante } 17171195e687SMark J Musante 17181195e687SMark J Musante static int 17191195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 17201195e687SMark J Musante boolean_t mosconfig) 17211195e687SMark J Musante { 17221195e687SMark J Musante nvlist_t *config = spa->spa_config; 17231195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 17241195e687SMark J Musante int error; 17251195e687SMark J Musante uint64_t pool_guid; 17261195e687SMark J Musante nvlist_t *nvl; 17271195e687SMark J Musante 17281195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 17291195e687SMark J Musante return (EINVAL); 17301195e687SMark J Musante 17311195e687SMark J Musante /* 17321195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 17331195e687SMark J Musante * it's not present treat it as the initial version. 17341195e687SMark J Musante */ 17351195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 17361195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 17371195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 17381195e687SMark J Musante 17391195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 17401195e687SMark J Musante &spa->spa_config_txg); 17411195e687SMark J Musante 17421195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 17431195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 17441195e687SMark J Musante error = EEXIST; 17451195e687SMark J Musante } else { 17461195e687SMark J Musante spa->spa_load_guid = pool_guid; 17471195e687SMark J Musante 17481195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 17491195e687SMark J Musante &nvl) == 0) { 17501195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 17511195e687SMark J Musante KM_SLEEP) == 0); 17521195e687SMark J Musante } 17531195e687SMark J Musante 175411027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 17551195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 17561195e687SMark J Musante mosconfig, &ereport); 17571195e687SMark J Musante } 17581195e687SMark J Musante 17591195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 176011027bc7STim Haley if (error) { 176111027bc7STim Haley if (error != EEXIST) { 176211027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 176311027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 176411027bc7STim Haley } 176511027bc7STim Haley if (error != EBADF) { 176611027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 176711027bc7STim Haley } 176811027bc7STim Haley } 17691195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 17701195e687SMark J Musante spa->spa_ena = 0; 17711195e687SMark J Musante 17721195e687SMark J Musante return (error); 17731195e687SMark J Musante } 17741195e687SMark J Musante 1775fa9e4066Sahrens /* 1776fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 1777ea8dc4b6Seschrock * source of configuration information. 1778fa9e4066Sahrens */ 1779fa9e4066Sahrens static int 17801195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 17811195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 17821195e687SMark J Musante char **ereport) 1783fa9e4066Sahrens { 1784fa9e4066Sahrens int error = 0; 1785871a9500SMark J Musante nvlist_t *nvroot = NULL; 1786fa9e4066Sahrens vdev_t *rvd; 1787fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 17884b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 17898ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 17901195e687SMark J Musante int parse; 1791cde58dbcSMatthew Ahrens uint64_t obj; 1792fa9e4066Sahrens 17938ad4d6ddSJeff Bonwick /* 17948ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 17958ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 17968ad4d6ddSJeff Bonwick */ 17978ad4d6ddSJeff Bonwick if (!mosconfig) 17988ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 17998ad4d6ddSJeff Bonwick 1800e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1801e14bb325SJeff Bonwick 1802ea8dc4b6Seschrock spa->spa_load_state = state; 18030373e76bSbonwick 18041195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 18051195e687SMark J Musante return (EINVAL); 1806fa9e4066Sahrens 18071195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 18081195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 1809b5989ec7Seschrock 181054d692b7SGeorge Wilson /* 181154d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 181254d692b7SGeorge Wilson */ 181325f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 181425f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 181554d692b7SGeorge Wilson 1816fa9e4066Sahrens /* 181799653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 181899653d4eSeschrock * value that will be returned by spa_version() since parsing the 181999653d4eSeschrock * configuration requires knowing the version number. 1820fa9e4066Sahrens */ 1821e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 18221195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 1823e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1824fa9e4066Sahrens 182599653d4eSeschrock if (error != 0) 18261195e687SMark J Musante return (error); 1827fa9e4066Sahrens 18280e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 18291195e687SMark J Musante 18301195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 18311195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 18321195e687SMark J Musante } 1833fa9e4066Sahrens 1834fa9e4066Sahrens /* 1835fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 1836fa9e4066Sahrens */ 1837e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 18380bf246f5Smc error = vdev_open(rvd); 1839e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 18400bf246f5Smc if (error != 0) 18411195e687SMark J Musante return (error); 1842fa9e4066Sahrens 1843560e6e96Seschrock /* 184477e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 184577e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 184677e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 18471195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 184877e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 184977e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 185077e3a39cSMark J Musante * the vdev config. 18511195e687SMark J Musante * 18521195e687SMark J Musante * If we're assembling a new pool that's been split off from an 18531195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 18541195e687SMark J Musante * validation for now. 1855560e6e96Seschrock */ 18561195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 18571195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 18581195e687SMark J Musante error = vdev_validate(rvd); 18591195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 1860560e6e96Seschrock 18611195e687SMark J Musante if (error != 0) 18621195e687SMark J Musante return (error); 18631195e687SMark J Musante 18641195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 18651195e687SMark J Musante return (ENXIO); 1866560e6e96Seschrock } 1867560e6e96Seschrock 1868fa9e4066Sahrens /* 1869fa9e4066Sahrens * Find the best uberblock. 1870fa9e4066Sahrens */ 1871e14bb325SJeff Bonwick vdev_uberblock_load(NULL, rvd, ub); 1872fa9e4066Sahrens 1873fa9e4066Sahrens /* 1874fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 1875fa9e4066Sahrens */ 18761195e687SMark J Musante if (ub->ub_txg == 0) 18771195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 1878ea8dc4b6Seschrock 1879ea8dc4b6Seschrock /* 1880ea8dc4b6Seschrock * If the pool is newer than the code, we can't open it. 1881ea8dc4b6Seschrock */ 18821195e687SMark J Musante if (ub->ub_version > SPA_VERSION) 18831195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 1884fa9e4066Sahrens 1885fa9e4066Sahrens /* 1886fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 18874b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 18884b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 18894b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 18904b964adaSGeorge Wilson * can handle missing vdevs. 1891fa9e4066Sahrens */ 18924b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 18934b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 18941195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 18951195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 18961195e687SMark J Musante 18971195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 18981195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 18991195e687SMark J Musante spa_try_repair(spa, config); 19001195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 19011195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 19021195e687SMark J Musante spa->spa_config_splitting = NULL; 1903fa9e4066Sahrens } 1904fa9e4066Sahrens 1905fa9e4066Sahrens /* 1906fa9e4066Sahrens * Initialize internal SPA structures. 1907fa9e4066Sahrens */ 1908fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1909fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 1910468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 1911c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 1912468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 1913468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 1914b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 19153f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 1916b24ab676SJeff Bonwick 1917ea8dc4b6Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 19181195e687SMark J Musante if (error) 19191195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1920fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1921fa9e4066Sahrens 19221195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 19231195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1924fa9e4066Sahrens 1925fa9e4066Sahrens if (!mosconfig) { 192695173954Sek uint64_t hostid; 1927871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 1928871a9500SMark J Musante 1929871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 1930871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1931fa9e4066Sahrens 193288ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 193377650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 193495173954Sek char *hostname; 193595173954Sek unsigned long myhostid = 0; 193695173954Sek 193788ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 193895173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 193995173954Sek 19405679c89fSjv #ifdef _KERNEL 19415679c89fSjv myhostid = zone_get_hostid(NULL); 19425679c89fSjv #else /* _KERNEL */ 19435679c89fSjv /* 19445679c89fSjv * We're emulating the system's hostid in userland, so 19455679c89fSjv * we can't use zone_get_hostid(). 19465679c89fSjv */ 194795173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 19485679c89fSjv #endif /* _KERNEL */ 194917194a52Slling if (hostid != 0 && myhostid != 0 && 19505679c89fSjv hostid != myhostid) { 1951871a9500SMark J Musante nvlist_free(nvconfig); 195295173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 195395173954Sek "loaded as it was last accessed by " 195477650510SLin Ling "another system (host: %s hostid: 0x%lx). " 195595173954Sek "See: http://www.sun.com/msg/ZFS-8000-EY", 1956e14bb325SJeff Bonwick spa_name(spa), hostname, 195795173954Sek (unsigned long)hostid); 19581195e687SMark J Musante return (EBADF); 195995173954Sek } 196095173954Sek } 1961c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 1962c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 1963c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 1964c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 196595173954Sek 196688ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 1967fa9e4066Sahrens spa_unload(spa); 1968fa9e4066Sahrens spa_deactivate(spa); 19698ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 1970fa9e4066Sahrens 19711195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 1972fa9e4066Sahrens } 1973fa9e4066Sahrens 1974cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 1975cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1976cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 1977cde58dbcSMatthew Ahrens if (error != 0) 19781195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1979fa9e4066Sahrens 198099653d4eSeschrock /* 198199653d4eSeschrock * Load the bit that tells us to use the new accounting function 198299653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 198399653d4eSeschrock * be present. 198499653d4eSeschrock */ 19851195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 19861195e687SMark J Musante if (error != 0 && error != ENOENT) 19871195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 198899653d4eSeschrock 19893f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 19903f9d6ad7SLin Ling &spa->spa_creation_version); 19913f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 19923f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 19933f9d6ad7SLin Ling 1994fa9e4066Sahrens /* 1995ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 1996ea8dc4b6Seschrock * not be present. 1997fa9e4066Sahrens */ 19981195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 19991195e687SMark J Musante if (error != 0 && error != ENOENT) 20001195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2001ea8dc4b6Seschrock 20021195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 20031195e687SMark J Musante &spa->spa_errlog_scrub); 20041195e687SMark J Musante if (error != 0 && error != ENOENT) 20051195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2006ea8dc4b6Seschrock 200706eeb2adSek /* 200806eeb2adSek * Load the history object. If we have an older pool, this 200906eeb2adSek * will not be present. 201006eeb2adSek */ 20111195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 20121195e687SMark J Musante if (error != 0 && error != ENOENT) 20131195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20141195e687SMark J Musante 20151195e687SMark J Musante /* 20161195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 20171195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 20181195e687SMark J Musante * devices. 20191195e687SMark J Musante */ 202006eeb2adSek 202199653d4eSeschrock /* 202299653d4eSeschrock * Load any hot spares for this pool. 202399653d4eSeschrock */ 20241195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 20251195e687SMark J Musante if (error != 0 && error != ENOENT) 20261195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20271195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2028e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2029fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 20301195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 20311195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 203299653d4eSeschrock 2033e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 203499653d4eSeschrock spa_load_spares(spa); 2035e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 20361195e687SMark J Musante } else if (error == 0) { 20371195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 203899653d4eSeschrock } 203999653d4eSeschrock 2040fa94a07fSbrendan /* 2041fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2042fa94a07fSbrendan */ 20431195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2044fa94a07fSbrendan &spa->spa_l2cache.sav_object); 20451195e687SMark J Musante if (error != 0 && error != ENOENT) 20461195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20471195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2048fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2049fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 20501195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 20511195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2052fa94a07fSbrendan 2053e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2054fa94a07fSbrendan spa_load_l2cache(spa); 2055e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 20561195e687SMark J Musante } else if (error == 0) { 20571195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2058fa94a07fSbrendan } 2059fa94a07fSbrendan 2060990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2061ecd6cf80Smarks 20621195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 20631195e687SMark J Musante if (error && error != ENOENT) 20641195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2065b1b8ab34Slling 2066b1b8ab34Slling if (error == 0) { 20671195e687SMark J Musante uint64_t autoreplace; 20681195e687SMark J Musante 20691195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 20701195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 20711195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 20721195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 20731195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 20741195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 20751195e687SMark J Musante &spa->spa_dedup_ditto); 20761195e687SMark J Musante 2077b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2078b1b8ab34Slling } 2079b1b8ab34Slling 20803d7072f8Seschrock /* 20813d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 20823d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 20833d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 20843d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 20853d7072f8Seschrock * over. 20863d7072f8Seschrock */ 2087b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 20883d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2089b693757aSEric Schrock /* 2090b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2091b693757aSEric Schrock * at this point we're using the spare definitions from 2092b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2093b693757aSEric Schrock */ 2094b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2095b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2096b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2097b693757aSEric Schrock } 2098b693757aSEric Schrock } 20993d7072f8Seschrock 2100ea8dc4b6Seschrock /* 2101560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2102ea8dc4b6Seschrock */ 2103560e6e96Seschrock vdev_load(rvd); 21040373e76bSbonwick 2105fa9e4066Sahrens /* 2106fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2107fa9e4066Sahrens */ 2108e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2109fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2110e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2111fa9e4066Sahrens 2112b24ab676SJeff Bonwick /* 2113b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2114b24ab676SJeff Bonwick */ 2115b24ab676SJeff Bonwick error = ddt_load(spa); 21161195e687SMark J Musante if (error != 0) 21171195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2118b24ab676SJeff Bonwick 2119485bbbf5SGeorge Wilson spa_update_dspace(spa); 2120485bbbf5SGeorge Wilson 2121b24ab676SJeff Bonwick /* 21224b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 21234b964adaSGeorge Wilson * information which might be missing. If we fail to validate 21244b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 21254b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 21264b964adaSGeorge Wilson * over. 2127b24ab676SJeff Bonwick */ 21281195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2129871a9500SMark J Musante nvlist_t *nvconfig; 2130871a9500SMark J Musante 2131871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2132871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2133871a9500SMark J Musante 21344b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 21354b964adaSGeorge Wilson nvlist_free(nvconfig); 21364b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 21374b964adaSGeorge Wilson ENXIO)); 21384b964adaSGeorge Wilson } 21391195e687SMark J Musante nvlist_free(nvconfig); 21401195e687SMark J Musante 21414b964adaSGeorge Wilson /* 21424b964adaSGeorge Wilson * Now that we've validate the config, check the state of the 21434b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 21444b964adaSGeorge Wilson * more toplevel vdevs are faulted. 21454b964adaSGeorge Wilson */ 21464b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 21474b964adaSGeorge Wilson return (ENXIO); 21484b964adaSGeorge Wilson 21491195e687SMark J Musante if (spa_check_logs(spa)) { 21501195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 21511195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 21521195e687SMark J Musante } 2153b24ab676SJeff Bonwick } 2154b24ab676SJeff Bonwick 21554b964adaSGeorge Wilson /* 21564b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 21574b964adaSGeorge Wilson * to start pushing transactions. 21584b964adaSGeorge Wilson */ 21594b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 21604b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 21614b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 21624b964adaSGeorge Wilson error)); 21634b964adaSGeorge Wilson } 21644b964adaSGeorge Wilson 2165468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2166468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 21675dabedeeSbonwick dmu_tx_t *tx; 21680373e76bSbonwick int need_update = B_FALSE; 21698ad4d6ddSJeff Bonwick 21708ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 21715dabedeeSbonwick 21720373e76bSbonwick /* 21730373e76bSbonwick * Claim log blocks that haven't been committed yet. 21740373e76bSbonwick * This must all happen in a single txg. 2175b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2176b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2177468c413aSTim Haley * Price of rollback is that we abandon the log. 21780373e76bSbonwick */ 2179b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2180b24ab676SJeff Bonwick 21815dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2182fa9e4066Sahrens spa_first_txg(spa)); 2183e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 21840b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2185fa9e4066Sahrens dmu_tx_commit(tx); 2186fa9e4066Sahrens 2187b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2188b24ab676SJeff Bonwick 21891195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2190fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2191fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2192fa9e4066Sahrens 2193fa9e4066Sahrens /* 2194b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2195b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2196b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2197b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2198b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2199fa9e4066Sahrens */ 2200b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 22010e34b6a7Sbonwick 22020e34b6a7Sbonwick /* 22030373e76bSbonwick * If the config cache is stale, or we have uninitialized 22040373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2205bc758434SLin Ling * 22064b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2207bc758434SLin Ling * in-core spa_config and update the disk labels. 22080e34b6a7Sbonwick */ 22090373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 22104b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 22114b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 22124b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 22130373e76bSbonwick need_update = B_TRUE; 22140373e76bSbonwick 22158ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 22160373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 22170373e76bSbonwick need_update = B_TRUE; 22180e34b6a7Sbonwick 22190e34b6a7Sbonwick /* 22200373e76bSbonwick * Update the config cache asychronously in case we're the 22210373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 22220e34b6a7Sbonwick */ 22230373e76bSbonwick if (need_update) 22240373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 22258ad4d6ddSJeff Bonwick 22268ad4d6ddSJeff Bonwick /* 22278ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 22288ad4d6ddSJeff Bonwick */ 22293f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 22303f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 22318ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2232503ad85cSMatthew Ahrens 2233503ad85cSMatthew Ahrens /* 2234503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2235503ad85cSMatthew Ahrens */ 2236503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2237503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2238ca45db41SChris Kirby 2239ca45db41SChris Kirby /* 2240ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2241ca45db41SChris Kirby */ 2242ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2243fa9e4066Sahrens } 2244fa9e4066Sahrens 22451195e687SMark J Musante return (0); 2246fa9e4066Sahrens } 2247fa9e4066Sahrens 2248468c413aSTim Haley static int 2249468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2250468c413aSTim Haley { 2251f9af39baSGeorge Wilson int mode = spa->spa_mode; 2252f9af39baSGeorge Wilson 2253468c413aSTim Haley spa_unload(spa); 2254468c413aSTim Haley spa_deactivate(spa); 2255468c413aSTim Haley 2256468c413aSTim Haley spa->spa_load_max_txg--; 2257468c413aSTim Haley 2258f9af39baSGeorge Wilson spa_activate(spa, mode); 2259468c413aSTim Haley spa_async_suspend(spa); 2260468c413aSTim Haley 22611195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2262468c413aSTim Haley } 2263468c413aSTim Haley 2264468c413aSTim Haley static int 2265468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2266c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2267468c413aSTim Haley { 2268468c413aSTim Haley nvlist_t *config = NULL; 2269468c413aSTim Haley int load_error, rewind_error; 2270c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2271468c413aSTim Haley uint64_t min_txg; 2272468c413aSTim Haley 2273a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2274468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 22751195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2276a33cae98STim Haley } else { 2277468c413aSTim Haley spa->spa_load_max_txg = max_request; 2278a33cae98STim Haley } 2279468c413aSTim Haley 22801195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 22811195e687SMark J Musante mosconfig); 2282468c413aSTim Haley if (load_error == 0) 2283468c413aSTim Haley return (0); 2284468c413aSTim Haley 2285468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2286468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2287468c413aSTim Haley 2288468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2289468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2290468c413aSTim Haley 2291c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2292468c413aSTim Haley nvlist_free(config); 2293468c413aSTim Haley return (load_error); 2294468c413aSTim Haley } 2295468c413aSTim Haley 2296468c413aSTim Haley /* Price of rolling back is discarding txgs, including log */ 2297468c413aSTim Haley if (state == SPA_LOAD_RECOVER) 22981195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2299468c413aSTim Haley 2300c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2301c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2302c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2303c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2304468c413aSTim Haley 2305c8ee1847SVictor Latushkin /* 2306c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2307c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2308c8ee1847SVictor Latushkin */ 2309c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2310c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2311c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2312468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2313468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 2314468c413aSTim Haley } 2315468c413aSTim Haley 2316468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 2317468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 2318468c413aSTim Haley 2319468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 2320468c413aSTim Haley spa_config_set(spa, config); 2321468c413aSTim Haley 2322468c413aSTim Haley return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 2323468c413aSTim Haley } 2324468c413aSTim Haley 2325fa9e4066Sahrens /* 2326fa9e4066Sahrens * Pool Open/Import 2327fa9e4066Sahrens * 2328fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 2329fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2330fa9e4066Sahrens * case of an open, the pool configuration will exist in the 23313d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 2332fa9e4066Sahrens * 2333fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2334fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 2335fa9e4066Sahrens * ambiguous state. 2336fa9e4066Sahrens */ 2337fa9e4066Sahrens static int 2338468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 2339468c413aSTim Haley nvlist_t **config) 2340fa9e4066Sahrens { 2341fa9e4066Sahrens spa_t *spa; 23424b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 2343fa9e4066Sahrens int error; 2344fa9e4066Sahrens int locked = B_FALSE; 2345fa9e4066Sahrens 2346fa9e4066Sahrens *spapp = NULL; 2347fa9e4066Sahrens 2348fa9e4066Sahrens /* 2349fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 2350fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2351fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 2352fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 2353fa9e4066Sahrens */ 2354fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2355fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2356fa9e4066Sahrens locked = B_TRUE; 2357fa9e4066Sahrens } 2358fa9e4066Sahrens 2359fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2360fa9e4066Sahrens if (locked) 2361fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2362fa9e4066Sahrens return (ENOENT); 2363fa9e4066Sahrens } 2364468c413aSTim Haley 2365fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 23664b44c88cSTim Haley zpool_rewind_policy_t policy; 23674b44c88cSTim Haley 23684b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 23694b44c88cSTim Haley &policy); 23704b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 23714b44c88cSTim Haley state = SPA_LOAD_RECOVER; 2372fa9e4066Sahrens 23738ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2374fa9e4066Sahrens 2375468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 2376468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2377468c413aSTim Haley 2378468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 2379c8ee1847SVictor Latushkin policy.zrp_request); 2380fa9e4066Sahrens 2381fa9e4066Sahrens if (error == EBADF) { 2382fa9e4066Sahrens /* 2383560e6e96Seschrock * If vdev_validate() returns failure (indicated by 2384560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 2385560e6e96Seschrock * that the pool has been exported or destroyed. If 2386560e6e96Seschrock * this is the case, the config cache is out of sync and 2387560e6e96Seschrock * we should remove the pool from the namespace. 2388fa9e4066Sahrens */ 2389fa9e4066Sahrens spa_unload(spa); 2390fa9e4066Sahrens spa_deactivate(spa); 2391c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2392fa9e4066Sahrens spa_remove(spa); 2393fa9e4066Sahrens if (locked) 2394fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2395fa9e4066Sahrens return (ENOENT); 2396ea8dc4b6Seschrock } 2397ea8dc4b6Seschrock 2398ea8dc4b6Seschrock if (error) { 2399fa9e4066Sahrens /* 2400fa9e4066Sahrens * We can't open the pool, but we still have useful 2401fa9e4066Sahrens * information: the state of each vdev after the 2402fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 2403fa9e4066Sahrens */ 24044b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 2405468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 2406468c413aSTim Haley KM_SLEEP) == 0); 24074b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 24084b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 24094b964adaSGeorge Wilson spa->spa_load_info) == 0); 24104b964adaSGeorge Wilson } 2411fa9e4066Sahrens spa_unload(spa); 2412fa9e4066Sahrens spa_deactivate(spa); 2413468c413aSTim Haley spa->spa_last_open_failed = error; 2414fa9e4066Sahrens if (locked) 2415fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2416fa9e4066Sahrens *spapp = NULL; 2417fa9e4066Sahrens return (error); 2418fa9e4066Sahrens } 2419fa9e4066Sahrens } 2420fa9e4066Sahrens 2421fa9e4066Sahrens spa_open_ref(spa, tag); 24223d7072f8Seschrock 2423468c413aSTim Haley if (config != NULL) 2424468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2425468c413aSTim Haley 24264b964adaSGeorge Wilson /* 24274b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 24284b964adaSGeorge Wilson * gathered while doing the load. 24294b964adaSGeorge Wilson */ 24304b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 24314b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 24324b964adaSGeorge Wilson spa->spa_load_info) == 0); 24334b964adaSGeorge Wilson } 24344b964adaSGeorge Wilson 2435a33cae98STim Haley if (locked) { 2436a33cae98STim Haley spa->spa_last_open_failed = 0; 2437a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 2438a33cae98STim Haley spa->spa_load_txg = 0; 2439fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2440a33cae98STim Haley } 2441fa9e4066Sahrens 2442fa9e4066Sahrens *spapp = spa; 2443fa9e4066Sahrens 2444fa9e4066Sahrens return (0); 2445fa9e4066Sahrens } 2446fa9e4066Sahrens 2447468c413aSTim Haley int 2448468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 2449468c413aSTim Haley nvlist_t **config) 2450468c413aSTim Haley { 2451468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 2452468c413aSTim Haley } 2453468c413aSTim Haley 2454fa9e4066Sahrens int 2455fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2456fa9e4066Sahrens { 2457468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 2458fa9e4066Sahrens } 2459fa9e4066Sahrens 2460ea8dc4b6Seschrock /* 2461ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 2462ea8dc4b6Seschrock * preventing it from being exported or destroyed. 2463ea8dc4b6Seschrock */ 2464ea8dc4b6Seschrock spa_t * 2465ea8dc4b6Seschrock spa_inject_addref(char *name) 2466ea8dc4b6Seschrock { 2467ea8dc4b6Seschrock spa_t *spa; 2468ea8dc4b6Seschrock 2469ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2470ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 2471ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2472ea8dc4b6Seschrock return (NULL); 2473ea8dc4b6Seschrock } 2474ea8dc4b6Seschrock spa->spa_inject_ref++; 2475ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2476ea8dc4b6Seschrock 2477ea8dc4b6Seschrock return (spa); 2478ea8dc4b6Seschrock } 2479ea8dc4b6Seschrock 2480ea8dc4b6Seschrock void 2481ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 2482ea8dc4b6Seschrock { 2483ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2484ea8dc4b6Seschrock spa->spa_inject_ref--; 2485ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2486ea8dc4b6Seschrock } 2487ea8dc4b6Seschrock 2488fa94a07fSbrendan /* 2489fa94a07fSbrendan * Add spares device information to the nvlist. 2490fa94a07fSbrendan */ 249199653d4eSeschrock static void 249299653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 249399653d4eSeschrock { 249499653d4eSeschrock nvlist_t **spares; 249599653d4eSeschrock uint_t i, nspares; 249699653d4eSeschrock nvlist_t *nvroot; 249799653d4eSeschrock uint64_t guid; 249899653d4eSeschrock vdev_stat_t *vs; 249999653d4eSeschrock uint_t vsc; 250039c23413Seschrock uint64_t pool; 250199653d4eSeschrock 25026809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 25036809eb4eSEric Schrock 2504fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 250599653d4eSeschrock return; 250699653d4eSeschrock 250799653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 250899653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2509fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 251099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 251199653d4eSeschrock if (nspares != 0) { 251299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 251399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 251499653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 251599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 251699653d4eSeschrock 251799653d4eSeschrock /* 251899653d4eSeschrock * Go through and find any spares which have since been 251999653d4eSeschrock * repurposed as an active spare. If this is the case, update 252099653d4eSeschrock * their status appropriately. 252199653d4eSeschrock */ 252299653d4eSeschrock for (i = 0; i < nspares; i++) { 252399653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 252499653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 252589a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 252689a89ebfSlling pool != 0ULL) { 252799653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 25283f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 252999653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 253099653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 253199653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 253299653d4eSeschrock } 253399653d4eSeschrock } 253499653d4eSeschrock } 253599653d4eSeschrock } 253699653d4eSeschrock 2537fa94a07fSbrendan /* 2538fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 2539fa94a07fSbrendan */ 2540fa94a07fSbrendan static void 2541fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 2542fa94a07fSbrendan { 2543fa94a07fSbrendan nvlist_t **l2cache; 2544fa94a07fSbrendan uint_t i, j, nl2cache; 2545fa94a07fSbrendan nvlist_t *nvroot; 2546fa94a07fSbrendan uint64_t guid; 2547fa94a07fSbrendan vdev_t *vd; 2548fa94a07fSbrendan vdev_stat_t *vs; 2549fa94a07fSbrendan uint_t vsc; 2550fa94a07fSbrendan 25516809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 25526809eb4eSEric Schrock 2553fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 2554fa94a07fSbrendan return; 2555fa94a07fSbrendan 2556fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 2557fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2558fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 2559fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2560fa94a07fSbrendan if (nl2cache != 0) { 2561fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 2562fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2563fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 2564fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2565fa94a07fSbrendan 2566fa94a07fSbrendan /* 2567fa94a07fSbrendan * Update level 2 cache device stats. 2568fa94a07fSbrendan */ 2569fa94a07fSbrendan 2570fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 2571fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 2572fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 2573fa94a07fSbrendan 2574fa94a07fSbrendan vd = NULL; 2575fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 2576fa94a07fSbrendan if (guid == 2577fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 2578fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 2579fa94a07fSbrendan break; 2580fa94a07fSbrendan } 2581fa94a07fSbrendan } 2582fa94a07fSbrendan ASSERT(vd != NULL); 2583fa94a07fSbrendan 2584fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 25853f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 25863f9d6ad7SLin Ling == 0); 2587fa94a07fSbrendan vdev_get_stats(vd, vs); 2588fa94a07fSbrendan } 2589fa94a07fSbrendan } 2590fa94a07fSbrendan } 2591fa94a07fSbrendan 2592fa9e4066Sahrens int 2593ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2594fa9e4066Sahrens { 2595fa9e4066Sahrens int error; 2596fa9e4066Sahrens spa_t *spa; 2597fa9e4066Sahrens 2598fa9e4066Sahrens *config = NULL; 2599468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 2600fa9e4066Sahrens 26016809eb4eSEric Schrock if (spa != NULL) { 26026809eb4eSEric Schrock /* 26036809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 26046809eb4eSEric Schrock * or l2cache devices could change and the config would be 26056809eb4eSEric Schrock * self-inconsistent. 26066809eb4eSEric Schrock */ 26076809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2608ea8dc4b6Seschrock 26096809eb4eSEric Schrock if (*config != NULL) { 261011027bc7STim Haley uint64_t loadtimes[2]; 261111027bc7STim Haley 261211027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 261311027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 261411027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 261511027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 261611027bc7STim Haley 2617e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 26186809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 26196809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 2620e14bb325SJeff Bonwick 26216809eb4eSEric Schrock if (spa_suspended(spa)) 26226809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 26236809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 26246809eb4eSEric Schrock spa->spa_failmode) == 0); 26256809eb4eSEric Schrock 26266809eb4eSEric Schrock spa_add_spares(spa, *config); 26276809eb4eSEric Schrock spa_add_l2cache(spa, *config); 26286809eb4eSEric Schrock } 262999653d4eSeschrock } 263099653d4eSeschrock 2631ea8dc4b6Seschrock /* 2632ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 2633ea8dc4b6Seschrock * and call spa_lookup() directly. 2634ea8dc4b6Seschrock */ 2635ea8dc4b6Seschrock if (altroot) { 2636ea8dc4b6Seschrock if (spa == NULL) { 2637ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2638ea8dc4b6Seschrock spa = spa_lookup(name); 2639ea8dc4b6Seschrock if (spa) 2640ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 2641ea8dc4b6Seschrock else 2642ea8dc4b6Seschrock altroot[0] = '\0'; 2643ea8dc4b6Seschrock spa = NULL; 2644ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2645ea8dc4b6Seschrock } else { 2646ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 2647ea8dc4b6Seschrock } 2648ea8dc4b6Seschrock } 2649ea8dc4b6Seschrock 26506809eb4eSEric Schrock if (spa != NULL) { 26516809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 2652fa9e4066Sahrens spa_close(spa, FTAG); 26536809eb4eSEric Schrock } 2654fa9e4066Sahrens 2655fa9e4066Sahrens return (error); 2656fa9e4066Sahrens } 2657fa9e4066Sahrens 265899653d4eSeschrock /* 2659fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 2660fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 2661fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 2662fa94a07fSbrendan * specified, as long as they are well-formed. 266399653d4eSeschrock */ 266499653d4eSeschrock static int 2665fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 2666fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 2667fa94a07fSbrendan vdev_labeltype_t label) 266899653d4eSeschrock { 2669fa94a07fSbrendan nvlist_t **dev; 2670fa94a07fSbrendan uint_t i, ndev; 267199653d4eSeschrock vdev_t *vd; 267299653d4eSeschrock int error; 267399653d4eSeschrock 2674e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2675e14bb325SJeff Bonwick 267699653d4eSeschrock /* 2677fa94a07fSbrendan * It's acceptable to have no devs specified. 267899653d4eSeschrock */ 2679fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 268099653d4eSeschrock return (0); 268199653d4eSeschrock 2682fa94a07fSbrendan if (ndev == 0) 268399653d4eSeschrock return (EINVAL); 268499653d4eSeschrock 268599653d4eSeschrock /* 2686fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 2687fa94a07fSbrendan * device type. 268899653d4eSeschrock */ 2689fa94a07fSbrendan if (spa_version(spa) < version) 269099653d4eSeschrock return (ENOTSUP); 269199653d4eSeschrock 269239c23413Seschrock /* 2693fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 269439c23413Seschrock * checking. 269539c23413Seschrock */ 2696fa94a07fSbrendan sav->sav_pending = dev; 2697fa94a07fSbrendan sav->sav_npending = ndev; 269839c23413Seschrock 2699fa94a07fSbrendan for (i = 0; i < ndev; i++) { 2700fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 270199653d4eSeschrock mode)) != 0) 270239c23413Seschrock goto out; 270399653d4eSeschrock 270499653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 270599653d4eSeschrock vdev_free(vd); 270639c23413Seschrock error = EINVAL; 270739c23413Seschrock goto out; 270899653d4eSeschrock } 270999653d4eSeschrock 2710fa94a07fSbrendan /* 2711e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 2712e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 2713fa94a07fSbrendan */ 2714e14bb325SJeff Bonwick #ifdef _KERNEL 2715fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 2716fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 2717fa94a07fSbrendan error = ENOTBLK; 2718fa94a07fSbrendan goto out; 2719fa94a07fSbrendan } 2720e14bb325SJeff Bonwick #endif 272199653d4eSeschrock vd->vdev_top = vd; 272299653d4eSeschrock 272339c23413Seschrock if ((error = vdev_open(vd)) == 0 && 2724fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 2725fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 272639c23413Seschrock vd->vdev_guid) == 0); 272739c23413Seschrock } 272899653d4eSeschrock 272999653d4eSeschrock vdev_free(vd); 273039c23413Seschrock 2731fa94a07fSbrendan if (error && 2732fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 273339c23413Seschrock goto out; 273439c23413Seschrock else 273539c23413Seschrock error = 0; 273699653d4eSeschrock } 273799653d4eSeschrock 273839c23413Seschrock out: 2739fa94a07fSbrendan sav->sav_pending = NULL; 2740fa94a07fSbrendan sav->sav_npending = 0; 274139c23413Seschrock return (error); 274299653d4eSeschrock } 274399653d4eSeschrock 2744fa94a07fSbrendan static int 2745fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 2746fa94a07fSbrendan { 2747fa94a07fSbrendan int error; 2748fa94a07fSbrendan 2749e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2750e14bb325SJeff Bonwick 2751fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2752fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 2753fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 2754fa94a07fSbrendan return (error); 2755fa94a07fSbrendan } 2756fa94a07fSbrendan 2757fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2758fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 2759fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 2760fa94a07fSbrendan } 2761fa94a07fSbrendan 2762fa94a07fSbrendan static void 2763fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 2764fa94a07fSbrendan const char *config) 2765fa94a07fSbrendan { 2766fa94a07fSbrendan int i; 2767fa94a07fSbrendan 2768fa94a07fSbrendan if (sav->sav_config != NULL) { 2769fa94a07fSbrendan nvlist_t **olddevs; 2770fa94a07fSbrendan uint_t oldndevs; 2771fa94a07fSbrendan nvlist_t **newdevs; 2772fa94a07fSbrendan 2773fa94a07fSbrendan /* 2774fa94a07fSbrendan * Generate new dev list by concatentating with the 2775fa94a07fSbrendan * current dev list. 2776fa94a07fSbrendan */ 2777fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 2778fa94a07fSbrendan &olddevs, &oldndevs) == 0); 2779fa94a07fSbrendan 2780fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 2781fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 2782fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 2783fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 2784fa94a07fSbrendan KM_SLEEP) == 0); 2785fa94a07fSbrendan for (i = 0; i < ndevs; i++) 2786fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 2787fa94a07fSbrendan KM_SLEEP) == 0); 2788fa94a07fSbrendan 2789fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 2790fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 2791fa94a07fSbrendan 2792fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 2793fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 2794fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 2795fa94a07fSbrendan nvlist_free(newdevs[i]); 2796fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 2797fa94a07fSbrendan } else { 2798fa94a07fSbrendan /* 2799fa94a07fSbrendan * Generate a new dev list. 2800fa94a07fSbrendan */ 2801fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 2802fa94a07fSbrendan KM_SLEEP) == 0); 2803fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 2804fa94a07fSbrendan devs, ndevs) == 0); 2805fa94a07fSbrendan } 2806fa94a07fSbrendan } 2807fa94a07fSbrendan 2808fa94a07fSbrendan /* 2809fa94a07fSbrendan * Stop and drop level 2 ARC devices 2810fa94a07fSbrendan */ 2811fa94a07fSbrendan void 2812fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 2813fa94a07fSbrendan { 2814fa94a07fSbrendan vdev_t *vd; 2815fa94a07fSbrendan int i; 2816fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 2817fa94a07fSbrendan 2818fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 2819fa94a07fSbrendan uint64_t pool; 2820fa94a07fSbrendan 2821fa94a07fSbrendan vd = sav->sav_vdevs[i]; 2822fa94a07fSbrendan ASSERT(vd != NULL); 2823fa94a07fSbrendan 28248ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 28258ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 2826fa94a07fSbrendan l2arc_remove_vdev(vd); 2827fa94a07fSbrendan if (vd->vdev_isl2cache) 2828fa94a07fSbrendan spa_l2cache_remove(vd); 2829fa94a07fSbrendan vdev_clear_stats(vd); 2830fa94a07fSbrendan (void) vdev_close(vd); 2831fa94a07fSbrendan } 2832fa94a07fSbrendan } 2833fa94a07fSbrendan 2834fa9e4066Sahrens /* 2835fa9e4066Sahrens * Pool Creation 2836fa9e4066Sahrens */ 2837fa9e4066Sahrens int 2838990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 28390a48a24eStimh const char *history_str, nvlist_t *zplprops) 2840fa9e4066Sahrens { 2841fa9e4066Sahrens spa_t *spa; 2842990b4856Slling char *altroot = NULL; 28430373e76bSbonwick vdev_t *rvd; 2844fa9e4066Sahrens dsl_pool_t *dp; 2845fa9e4066Sahrens dmu_tx_t *tx; 2846573ca77eSGeorge Wilson int error = 0; 2847fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 2848fa94a07fSbrendan nvlist_t **spares, **l2cache; 2849fa94a07fSbrendan uint_t nspares, nl2cache; 2850cde58dbcSMatthew Ahrens uint64_t version, obj; 2851fa9e4066Sahrens 2852fa9e4066Sahrens /* 2853fa9e4066Sahrens * If this pool already exists, return failure. 2854fa9e4066Sahrens */ 2855fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2856fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 2857fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2858fa9e4066Sahrens return (EEXIST); 2859fa9e4066Sahrens } 2860fa9e4066Sahrens 2861fa9e4066Sahrens /* 2862fa9e4066Sahrens * Allocate a new spa_t structure. 2863fa9e4066Sahrens */ 2864990b4856Slling (void) nvlist_lookup_string(props, 2865990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 2866468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 28678ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2868fa9e4066Sahrens 2869990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 2870990b4856Slling spa_deactivate(spa); 2871990b4856Slling spa_remove(spa); 2872c5904d13Seschrock mutex_exit(&spa_namespace_lock); 2873990b4856Slling return (error); 2874990b4856Slling } 2875990b4856Slling 2876990b4856Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 2877990b4856Slling &version) != 0) 2878990b4856Slling version = SPA_VERSION; 2879990b4856Slling ASSERT(version <= SPA_VERSION); 2880b24ab676SJeff Bonwick 2881b24ab676SJeff Bonwick spa->spa_first_txg = txg; 2882b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 2883990b4856Slling spa->spa_uberblock.ub_version = version; 2884fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2885fa9e4066Sahrens 288654d692b7SGeorge Wilson /* 288754d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 288854d692b7SGeorge Wilson */ 288925f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 289025f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 289154d692b7SGeorge Wilson 28920373e76bSbonwick /* 28930373e76bSbonwick * Create the root vdev. 28940373e76bSbonwick */ 2895e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 28960373e76bSbonwick 289799653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 28980373e76bSbonwick 289999653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 290099653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 29010373e76bSbonwick 2902b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 29030373e76bSbonwick error = EINVAL; 290499653d4eSeschrock 290599653d4eSeschrock if (error == 0 && 290699653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 2907fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 290899653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 2909573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 2910573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 2911573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 2912573ca77eSGeorge Wilson } 29130373e76bSbonwick } 29140373e76bSbonwick 2915e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2916fa9e4066Sahrens 291799653d4eSeschrock if (error != 0) { 2918fa9e4066Sahrens spa_unload(spa); 2919fa9e4066Sahrens spa_deactivate(spa); 2920fa9e4066Sahrens spa_remove(spa); 2921fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2922fa9e4066Sahrens return (error); 2923fa9e4066Sahrens } 2924fa9e4066Sahrens 292599653d4eSeschrock /* 292699653d4eSeschrock * Get the list of spares, if specified. 292799653d4eSeschrock */ 292899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 292999653d4eSeschrock &spares, &nspares) == 0) { 2930fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 293199653d4eSeschrock KM_SLEEP) == 0); 2932fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 293399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2934e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 293599653d4eSeschrock spa_load_spares(spa); 2936e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2937fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 2938fa94a07fSbrendan } 2939fa94a07fSbrendan 2940fa94a07fSbrendan /* 2941fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 2942fa94a07fSbrendan */ 2943fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2944fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 2945fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2946fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 2947fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2948fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2949e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2950fa94a07fSbrendan spa_load_l2cache(spa); 2951e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2952fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 295399653d4eSeschrock } 295499653d4eSeschrock 29550a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2956fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2957fa9e4066Sahrens 2958485bbbf5SGeorge Wilson /* 2959485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 2960485bbbf5SGeorge Wilson */ 2961485bbbf5SGeorge Wilson ddt_create(spa); 2962485bbbf5SGeorge Wilson 2963485bbbf5SGeorge Wilson spa_update_dspace(spa); 2964485bbbf5SGeorge Wilson 2965fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 2966fa9e4066Sahrens 2967fa9e4066Sahrens /* 2968fa9e4066Sahrens * Create the pool config object. 2969fa9e4066Sahrens */ 2970fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 2971f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2972fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2973fa9e4066Sahrens 2974ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 2975fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 2976ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 2977ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 2978ea8dc4b6Seschrock } 2979fa9e4066Sahrens 29803f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 29813f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 29823f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 29833f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 29843f9d6ad7SLin Ling } 29853f9d6ad7SLin Ling 2986990b4856Slling /* Newly created pools with the right version are always deflated. */ 2987990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 2988990b4856Slling spa->spa_deflate = TRUE; 2989990b4856Slling if (zap_add(spa->spa_meta_objset, 2990990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 2991990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 2992990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 2993990b4856Slling } 299499653d4eSeschrock } 299599653d4eSeschrock 2996fa9e4066Sahrens /* 2997cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 2998fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 2999fa9e4066Sahrens * keeps changing. 3000fa9e4066Sahrens */ 3001cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3002cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3003cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3004ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3005cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3006cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3007cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3008ea8dc4b6Seschrock } 3009cde58dbcSMatthew Ahrens VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3010cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3011fa9e4066Sahrens 301206eeb2adSek /* 301306eeb2adSek * Create the pool's history object. 301406eeb2adSek */ 3015990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3016990b4856Slling spa_history_create_obj(spa, tx); 3017990b4856Slling 3018990b4856Slling /* 3019990b4856Slling * Set pool properties. 3020990b4856Slling */ 3021990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3022990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 30230a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3024573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3025b24ab676SJeff Bonwick 3026379c004dSEric Schrock if (props != NULL) { 3027379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 30283f9d6ad7SLin Ling spa_sync_props(spa, props, tx); 3029379c004dSEric Schrock } 303006eeb2adSek 3031fa9e4066Sahrens dmu_tx_commit(tx); 3032fa9e4066Sahrens 3033fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3034fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3035fa9e4066Sahrens 3036fa9e4066Sahrens /* 3037fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3038fa9e4066Sahrens * bean counters are appropriately updated. 3039fa9e4066Sahrens */ 3040fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3041fa9e4066Sahrens 3042c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3043fa9e4066Sahrens 3044990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 3045228975ccSek (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 3046c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_CREATE); 3047228975ccSek 3048088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 3049088f3894Sahrens 3050daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3051daaa36a7SGeorge Wilson 3052fa9e4066Sahrens return (0); 3053fa9e4066Sahrens } 3054fa9e4066Sahrens 3055e7cbe64fSgw #ifdef _KERNEL 3056e7cbe64fSgw /* 305721ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 305821ecdf64SLin Ling * during the system boot up time. 3059e7cbe64fSgw */ 306021ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 306121ecdf64SLin Ling 306221ecdf64SLin Ling static nvlist_t * 306321ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3064e7cbe64fSgw { 306521ecdf64SLin Ling nvlist_t *config; 3066e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3067e7cbe64fSgw uint64_t pgid; 3068e7cbe64fSgw 306921ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 307021ecdf64SLin Ling return (NULL); 307121ecdf64SLin Ling 3072e7cbe64fSgw /* 3073e7cbe64fSgw * Add this top-level vdev to the child array. 3074e7cbe64fSgw */ 307521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 307621ecdf64SLin Ling &nvtop) == 0); 307721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 307821ecdf64SLin Ling &pgid) == 0); 307921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3080e7cbe64fSgw 3081e7cbe64fSgw /* 3082e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3083e7cbe64fSgw */ 3084e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 308521ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 308621ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3087e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3088e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3089e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3090e7cbe64fSgw &nvtop, 1) == 0); 3091e7cbe64fSgw 3092e7cbe64fSgw /* 3093e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3094e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3095e7cbe64fSgw */ 3096e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3097e7cbe64fSgw nvlist_free(nvroot); 309821ecdf64SLin Ling return (config); 3099e7cbe64fSgw } 3100e7cbe64fSgw 3101e7cbe64fSgw /* 310221ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 310321ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 310421ecdf64SLin Ling * device has a more recent txg. 3105051aabe6Staylor */ 310621ecdf64SLin Ling static void 310721ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3108051aabe6Staylor { 3109573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 311021ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3111051aabe6Staylor 311221ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 311321ecdf64SLin Ling nvlist_t *label; 311421ecdf64SLin Ling uint64_t label_txg; 3115051aabe6Staylor 311621ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 311721ecdf64SLin Ling &label) != 0) 311821ecdf64SLin Ling return; 3119051aabe6Staylor 312021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 312121ecdf64SLin Ling &label_txg) == 0); 3122051aabe6Staylor 312321ecdf64SLin Ling /* 312421ecdf64SLin Ling * Do we have a better boot device? 312521ecdf64SLin Ling */ 312621ecdf64SLin Ling if (label_txg > *txg) { 312721ecdf64SLin Ling *txg = label_txg; 312821ecdf64SLin Ling *avd = vd; 3129051aabe6Staylor } 313021ecdf64SLin Ling nvlist_free(label); 3131051aabe6Staylor } 3132051aabe6Staylor } 3133051aabe6Staylor 3134e7cbe64fSgw /* 3135e7cbe64fSgw * Import a root pool. 3136e7cbe64fSgw * 3137051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3138051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3139051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3140e7cbe64fSgw * 3141e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3142e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3143e7cbe64fSgw * e.g. 3144e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3145e7cbe64fSgw */ 3146e7cbe64fSgw int 3147051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3148e7cbe64fSgw { 314921ecdf64SLin Ling spa_t *spa; 315021ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 315121ecdf64SLin Ling nvlist_t *config, *nvtop; 315221ecdf64SLin Ling uint64_t guid, txg; 3153e7cbe64fSgw char *pname; 3154e7cbe64fSgw int error; 3155e7cbe64fSgw 3156e7cbe64fSgw /* 315721ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3158e7cbe64fSgw */ 3159dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3160dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3161dedec472SJack Meng if (config == NULL) { 3162dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3163dedec472SJack Meng /* iscsi boot */ 3164dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3165dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3166dedec472SJack Meng } 3167dedec472SJack Meng } 3168dedec472SJack Meng #endif 3169dedec472SJack Meng if (config == NULL) { 317021ecdf64SLin Ling cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 317121ecdf64SLin Ling devpath); 317221ecdf64SLin Ling return (EIO); 317321ecdf64SLin Ling } 3174e7cbe64fSgw 317521ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 317621ecdf64SLin Ling &pname) == 0); 317721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3178e7cbe64fSgw 31796809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 31806809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 31816809eb4eSEric Schrock /* 31826809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 31836809eb4eSEric Schrock * can replace it with the correct config we just read in. 31846809eb4eSEric Schrock */ 31856809eb4eSEric Schrock spa_remove(spa); 31866809eb4eSEric Schrock } 31876809eb4eSEric Schrock 3188468c413aSTim Haley spa = spa_add(pname, config, NULL); 31896809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 31904b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3191e7cbe64fSgw 319221ecdf64SLin Ling /* 319321ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 319421ecdf64SLin Ling */ 319521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 319621ecdf64SLin Ling &nvtop) == 0); 319721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 319821ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 319921ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 320021ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 320121ecdf64SLin Ling if (error) { 320221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 320321ecdf64SLin Ling nvlist_free(config); 320421ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 320521ecdf64SLin Ling pname); 320621ecdf64SLin Ling return (error); 320721ecdf64SLin Ling } 320821ecdf64SLin Ling 320921ecdf64SLin Ling /* 321021ecdf64SLin Ling * Get the boot vdev. 321121ecdf64SLin Ling */ 321221ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 321321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 321421ecdf64SLin Ling (u_longlong_t)guid); 321521ecdf64SLin Ling error = ENOENT; 321621ecdf64SLin Ling goto out; 321721ecdf64SLin Ling } 3218e7cbe64fSgw 321921ecdf64SLin Ling /* 322021ecdf64SLin Ling * Determine if there is a better boot device. 322121ecdf64SLin Ling */ 322221ecdf64SLin Ling avd = bvd; 322321ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 322421ecdf64SLin Ling if (avd != bvd) { 322521ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 322621ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 322721ecdf64SLin Ling error = EINVAL; 322821ecdf64SLin Ling goto out; 322921ecdf64SLin Ling } 3230e7cbe64fSgw 323121ecdf64SLin Ling /* 323221ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 323321ecdf64SLin Ling * we're booting off the active spare. 323421ecdf64SLin Ling */ 323521ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 323621ecdf64SLin Ling !bvd->vdev_isspare) { 323721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 323821ecdf64SLin Ling "try booting from '%s'", 3239cb04b873SMark J Musante bvd->vdev_parent-> 3240cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 324121ecdf64SLin Ling error = EINVAL; 324221ecdf64SLin Ling goto out; 324321ecdf64SLin Ling } 324421ecdf64SLin Ling 324521ecdf64SLin Ling error = 0; 3246c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 324721ecdf64SLin Ling out: 324821ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 324921ecdf64SLin Ling vdev_free(rvd); 325021ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 325121ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 325221ecdf64SLin Ling 325321ecdf64SLin Ling nvlist_free(config); 3254e7cbe64fSgw return (error); 3255e7cbe64fSgw } 325621ecdf64SLin Ling 3257e7cbe64fSgw #endif 3258e7cbe64fSgw 32596809eb4eSEric Schrock /* 32606809eb4eSEric Schrock * Import a non-root pool into the system. 32616809eb4eSEric Schrock */ 3262c5904d13Seschrock int 32634b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 3264c5904d13Seschrock { 32656809eb4eSEric Schrock spa_t *spa; 32666809eb4eSEric Schrock char *altroot = NULL; 3267468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 3268468c413aSTim Haley zpool_rewind_policy_t policy; 3269f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 3270f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 32716809eb4eSEric Schrock int error; 32726809eb4eSEric Schrock nvlist_t *nvroot; 32736809eb4eSEric Schrock nvlist_t **spares, **l2cache; 32746809eb4eSEric Schrock uint_t nspares, nl2cache; 32756809eb4eSEric Schrock 32766809eb4eSEric Schrock /* 32776809eb4eSEric Schrock * If a pool with this name exists, return failure. 32786809eb4eSEric Schrock */ 32796809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 32801195e687SMark J Musante if (spa_lookup(pool) != NULL) { 32816809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 32826809eb4eSEric Schrock return (EEXIST); 32836809eb4eSEric Schrock } 32846809eb4eSEric Schrock 32856809eb4eSEric Schrock /* 32866809eb4eSEric Schrock * Create and initialize the spa structure. 32876809eb4eSEric Schrock */ 32886809eb4eSEric Schrock (void) nvlist_lookup_string(props, 32896809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3290f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 3291f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3292f9af39baSGeorge Wilson if (readonly) 3293f9af39baSGeorge Wilson mode = FREAD; 3294468c413aSTim Haley spa = spa_add(pool, config, altroot); 32954b964adaSGeorge Wilson spa->spa_import_flags = flags; 32964b964adaSGeorge Wilson 32974b964adaSGeorge Wilson /* 32984b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 32994b964adaSGeorge Wilson * as if it had been loaded at boot. 33004b964adaSGeorge Wilson */ 33014b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 33024b964adaSGeorge Wilson if (props != NULL) 33034b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 33044b964adaSGeorge Wilson 33054b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 33064b964adaSGeorge Wilson 33074b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 33084b964adaSGeorge Wilson spa_history_log_version(spa, LOG_POOL_IMPORT); 33094b964adaSGeorge Wilson 33104b964adaSGeorge Wilson return (0); 33114b964adaSGeorge Wilson } 33124b964adaSGeorge Wilson 3313f9af39baSGeorge Wilson spa_activate(spa, mode); 33146809eb4eSEric Schrock 331525f89ee2SJeff Bonwick /* 331625f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 331725f89ee2SJeff Bonwick */ 331825f89ee2SJeff Bonwick spa_async_suspend(spa); 331925f89ee2SJeff Bonwick 33204b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 33214b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 33224b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 33234b964adaSGeorge Wilson 33246809eb4eSEric Schrock /* 33256809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 33266809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 33276809eb4eSEric Schrock * doing an import. 33286809eb4eSEric Schrock */ 3329468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3330468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 33314b964adaSGeorge Wilson 3332468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 3333c8ee1847SVictor Latushkin policy.zrp_request); 3334468c413aSTim Haley 3335468c413aSTim Haley /* 33364b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 33374b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 3338468c413aSTim Haley */ 33394b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 33404b964adaSGeorge Wilson spa->spa_load_info) == 0); 33416809eb4eSEric Schrock 33426809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33436809eb4eSEric Schrock /* 33446809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 33456809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 33466809eb4eSEric Schrock */ 33476809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 33486809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 33496809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 33506809eb4eSEric Schrock spa_load_spares(spa); 33516809eb4eSEric Schrock } 33526809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 33536809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 33546809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 33556809eb4eSEric Schrock spa_load_l2cache(spa); 33566809eb4eSEric Schrock } 33576809eb4eSEric Schrock 33586809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 33596809eb4eSEric Schrock &nvroot) == 0); 33606809eb4eSEric Schrock if (error == 0) 33616809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 33626809eb4eSEric Schrock VDEV_ALLOC_SPARE); 33636809eb4eSEric Schrock if (error == 0) 33646809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 33656809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 33666809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 33676809eb4eSEric Schrock 33686809eb4eSEric Schrock if (props != NULL) 33696809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 33706809eb4eSEric Schrock 33716809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 33726809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 33736809eb4eSEric Schrock spa_unload(spa); 33746809eb4eSEric Schrock spa_deactivate(spa); 33756809eb4eSEric Schrock spa_remove(spa); 33766809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 33776809eb4eSEric Schrock return (error); 33786809eb4eSEric Schrock } 33796809eb4eSEric Schrock 3380955ef359SLin Ling spa_async_resume(spa); 3381955ef359SLin Ling 33826809eb4eSEric Schrock /* 33836809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 33846809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 33856809eb4eSEric Schrock */ 33866809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 33876809eb4eSEric Schrock &spares, &nspares) == 0) { 33886809eb4eSEric Schrock if (spa->spa_spares.sav_config) 33896809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 33906809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 33916809eb4eSEric Schrock else 33926809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 33936809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 33946809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 33956809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 33966809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33976809eb4eSEric Schrock spa_load_spares(spa); 33986809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 33996809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 34006809eb4eSEric Schrock } 34016809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 34026809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 34036809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 34046809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 34056809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 34066809eb4eSEric Schrock else 34076809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 34086809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 34096809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 34106809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 34116809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 34126809eb4eSEric Schrock spa_load_l2cache(spa); 34136809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 34146809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 34156809eb4eSEric Schrock } 34166809eb4eSEric Schrock 3417b693757aSEric Schrock /* 3418b693757aSEric Schrock * Check for any removed devices. 3419b693757aSEric Schrock */ 3420b693757aSEric Schrock if (spa->spa_autoreplace) { 3421b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3422b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3423b693757aSEric Schrock } 3424b693757aSEric Schrock 34256809eb4eSEric Schrock if (spa_writeable(spa)) { 34266809eb4eSEric Schrock /* 34276809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 34286809eb4eSEric Schrock */ 3429bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 34306809eb4eSEric Schrock } 34316809eb4eSEric Schrock 3432573ca77eSGeorge Wilson /* 3433573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 3434573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 3435573ca77eSGeorge Wilson */ 3436573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 3437573ca77eSGeorge Wilson 34386809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 3439c8e1f6d2SMark J Musante spa_history_log_version(spa, LOG_POOL_IMPORT); 34406809eb4eSEric Schrock 34416809eb4eSEric Schrock return (0); 3442c5904d13Seschrock } 3443c5904d13Seschrock 3444fa9e4066Sahrens nvlist_t * 3445fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 3446fa9e4066Sahrens { 3447fa9e4066Sahrens nvlist_t *config = NULL; 3448fa9e4066Sahrens char *poolname; 3449fa9e4066Sahrens spa_t *spa; 3450fa9e4066Sahrens uint64_t state; 34517b7154beSLin Ling int error; 3452fa9e4066Sahrens 3453fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3454fa9e4066Sahrens return (NULL); 3455fa9e4066Sahrens 3456fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3457fa9e4066Sahrens return (NULL); 3458fa9e4066Sahrens 3459fa9e4066Sahrens /* 34600373e76bSbonwick * Create and initialize the spa structure. 3461fa9e4066Sahrens */ 34620373e76bSbonwick mutex_enter(&spa_namespace_lock); 3463468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 34648ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 3465fa9e4066Sahrens 3466fa9e4066Sahrens /* 34670373e76bSbonwick * Pass off the heavy lifting to spa_load(). 3468ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 3469ecc2d604Sbonwick * is actually the one to trust when doing an import. 3470fa9e4066Sahrens */ 34711195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3472fa9e4066Sahrens 3473fa9e4066Sahrens /* 3474fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 3475fa9e4066Sahrens */ 3476fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 3477fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3478fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3479fa9e4066Sahrens poolname) == 0); 3480fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3481fa9e4066Sahrens state) == 0); 348295173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 348395173954Sek spa->spa_uberblock.ub_timestamp) == 0); 348499653d4eSeschrock 3485e7cbe64fSgw /* 3486e7cbe64fSgw * If the bootfs property exists on this pool then we 3487e7cbe64fSgw * copy it out so that external consumers can tell which 3488e7cbe64fSgw * pools are bootable. 3489e7cbe64fSgw */ 34907b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 3491e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3492e7cbe64fSgw 3493e7cbe64fSgw /* 3494e7cbe64fSgw * We have to play games with the name since the 3495e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 3496e7cbe64fSgw */ 3497e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 3498e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 3499e7cbe64fSgw char *cp; 3500e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3501e7cbe64fSgw 3502e7cbe64fSgw cp = strchr(tmpname, '/'); 3503e7cbe64fSgw if (cp == NULL) { 3504e7cbe64fSgw (void) strlcpy(dsname, tmpname, 3505e7cbe64fSgw MAXPATHLEN); 3506e7cbe64fSgw } else { 3507e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 3508e7cbe64fSgw "%s/%s", poolname, ++cp); 3509e7cbe64fSgw } 3510e7cbe64fSgw VERIFY(nvlist_add_string(config, 3511e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 3512e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 3513e7cbe64fSgw } 3514e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 3515e7cbe64fSgw } 3516e7cbe64fSgw 351799653d4eSeschrock /* 3518fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 351999653d4eSeschrock */ 35206809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 352199653d4eSeschrock spa_add_spares(spa, config); 3522fa94a07fSbrendan spa_add_l2cache(spa, config); 35236809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3524fa9e4066Sahrens } 3525fa9e4066Sahrens 3526fa9e4066Sahrens spa_unload(spa); 3527fa9e4066Sahrens spa_deactivate(spa); 3528fa9e4066Sahrens spa_remove(spa); 3529fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3530fa9e4066Sahrens 3531fa9e4066Sahrens return (config); 3532fa9e4066Sahrens } 3533fa9e4066Sahrens 3534fa9e4066Sahrens /* 3535fa9e4066Sahrens * Pool export/destroy 3536fa9e4066Sahrens * 3537fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3538fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3539fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 3540394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 3541394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 3542fa9e4066Sahrens */ 3543fa9e4066Sahrens static int 354489a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 3545394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 3546fa9e4066Sahrens { 3547fa9e4066Sahrens spa_t *spa; 3548fa9e4066Sahrens 354944cd46caSbillm if (oldconfig) 355044cd46caSbillm *oldconfig = NULL; 355144cd46caSbillm 35528ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 3553fa9e4066Sahrens return (EROFS); 3554fa9e4066Sahrens 3555fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3556fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3557fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3558fa9e4066Sahrens return (ENOENT); 3559fa9e4066Sahrens } 3560fa9e4066Sahrens 3561ea8dc4b6Seschrock /* 3562ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 3563ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 3564ea8dc4b6Seschrock */ 3565ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 3566ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3567ea8dc4b6Seschrock spa_async_suspend(spa); 3568ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3569ea8dc4b6Seschrock spa_close(spa, FTAG); 3570ea8dc4b6Seschrock 3571fa9e4066Sahrens /* 3572fa9e4066Sahrens * The pool will be in core if it's openable, 3573fa9e4066Sahrens * in which case we can modify its state. 3574fa9e4066Sahrens */ 3575fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3576fa9e4066Sahrens /* 3577fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 3578fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 3579fa9e4066Sahrens */ 3580fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3581fa9e4066Sahrens 3582ea8dc4b6Seschrock /* 3583ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 3584ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 3585ea8dc4b6Seschrock * fault injection handlers. 3586ea8dc4b6Seschrock */ 3587ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 3588ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 3589ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 3590ea8dc4b6Seschrock spa_async_resume(spa); 3591fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3592fa9e4066Sahrens return (EBUSY); 3593fa9e4066Sahrens } 3594fa9e4066Sahrens 359589a89ebfSlling /* 359689a89ebfSlling * A pool cannot be exported if it has an active shared spare. 359789a89ebfSlling * This is to prevent other pools stealing the active spare 359889a89ebfSlling * from an exported pool. At user's own will, such pool can 359989a89ebfSlling * be forcedly exported. 360089a89ebfSlling */ 360189a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 360289a89ebfSlling spa_has_active_shared_spare(spa)) { 360389a89ebfSlling spa_async_resume(spa); 360489a89ebfSlling mutex_exit(&spa_namespace_lock); 360589a89ebfSlling return (EXDEV); 360689a89ebfSlling } 360789a89ebfSlling 3608fa9e4066Sahrens /* 3609fa9e4066Sahrens * We want this to be reflected on every label, 3610fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 3611fa9e4066Sahrens * final sync that pushes these changes out. 3612fa9e4066Sahrens */ 3613394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 3614e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3615ea8dc4b6Seschrock spa->spa_state = new_state; 36163f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 36173f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 3618ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 3619e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3620ea8dc4b6Seschrock } 3621fa9e4066Sahrens } 3622fa9e4066Sahrens 36233d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 36243d7072f8Seschrock 3625fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3626fa9e4066Sahrens spa_unload(spa); 3627fa9e4066Sahrens spa_deactivate(spa); 3628fa9e4066Sahrens } 3629fa9e4066Sahrens 363044cd46caSbillm if (oldconfig && spa->spa_config) 363144cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 363244cd46caSbillm 3633ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 3634394ab0cbSGeorge Wilson if (!hardforce) 3635394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 3636ea8dc4b6Seschrock spa_remove(spa); 3637ea8dc4b6Seschrock } 3638fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3639fa9e4066Sahrens 3640fa9e4066Sahrens return (0); 3641fa9e4066Sahrens } 3642fa9e4066Sahrens 3643fa9e4066Sahrens /* 3644fa9e4066Sahrens * Destroy a storage pool. 3645fa9e4066Sahrens */ 3646fa9e4066Sahrens int 3647fa9e4066Sahrens spa_destroy(char *pool) 3648fa9e4066Sahrens { 3649394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 3650394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 3651fa9e4066Sahrens } 3652fa9e4066Sahrens 3653fa9e4066Sahrens /* 3654fa9e4066Sahrens * Export a storage pool. 3655fa9e4066Sahrens */ 3656fa9e4066Sahrens int 3657394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 3658394ab0cbSGeorge Wilson boolean_t hardforce) 3659fa9e4066Sahrens { 3660394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 3661394ab0cbSGeorge Wilson force, hardforce)); 3662fa9e4066Sahrens } 3663fa9e4066Sahrens 3664ea8dc4b6Seschrock /* 3665ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 3666ea8dc4b6Seschrock * from the namespace in any way. 3667ea8dc4b6Seschrock */ 3668ea8dc4b6Seschrock int 3669ea8dc4b6Seschrock spa_reset(char *pool) 3670ea8dc4b6Seschrock { 367189a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 3672394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 3673ea8dc4b6Seschrock } 3674ea8dc4b6Seschrock 3675fa9e4066Sahrens /* 3676fa9e4066Sahrens * ========================================================================== 3677fa9e4066Sahrens * Device manipulation 3678fa9e4066Sahrens * ========================================================================== 3679fa9e4066Sahrens */ 3680fa9e4066Sahrens 3681fa9e4066Sahrens /* 36828654d025Sperrin * Add a device to a storage pool. 3683fa9e4066Sahrens */ 3684fa9e4066Sahrens int 3685fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3686fa9e4066Sahrens { 368788ecc943SGeorge Wilson uint64_t txg, id; 36888ad4d6ddSJeff Bonwick int error; 3689fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 36900e34b6a7Sbonwick vdev_t *vd, *tvd; 3691fa94a07fSbrendan nvlist_t **spares, **l2cache; 3692fa94a07fSbrendan uint_t nspares, nl2cache; 3693fa9e4066Sahrens 3694f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3695f9af39baSGeorge Wilson 3696fa9e4066Sahrens txg = spa_vdev_enter(spa); 3697fa9e4066Sahrens 369899653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 369999653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 370099653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 3701fa9e4066Sahrens 3702e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 370399653d4eSeschrock 3704fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 3705fa94a07fSbrendan &nspares) != 0) 370699653d4eSeschrock nspares = 0; 370799653d4eSeschrock 3708fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 3709fa94a07fSbrendan &nl2cache) != 0) 3710fa94a07fSbrendan nl2cache = 0; 3711fa94a07fSbrendan 3712e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 3713fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 3714fa9e4066Sahrens 3715e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 3716e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 3717e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 371899653d4eSeschrock 371939c23413Seschrock /* 3720fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 3721fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 372239c23413Seschrock */ 3723e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 372439c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 372539c23413Seschrock 372639c23413Seschrock /* 372739c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 372839c23413Seschrock */ 37298ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 373088ecc943SGeorge Wilson 373188ecc943SGeorge Wilson /* 373288ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 373388ecc943SGeorge Wilson */ 373488ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 373588ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 373688ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 373788ecc943SGeorge Wilson break; 373888ecc943SGeorge Wilson } 373988ecc943SGeorge Wilson } 374039c23413Seschrock tvd = vd->vdev_child[c]; 374139c23413Seschrock vdev_remove_child(vd, tvd); 374288ecc943SGeorge Wilson tvd->vdev_id = id; 374339c23413Seschrock vdev_add_child(rvd, tvd); 374439c23413Seschrock vdev_config_dirty(tvd); 374539c23413Seschrock } 374639c23413Seschrock 374799653d4eSeschrock if (nspares != 0) { 3748fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 3749fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 375099653d4eSeschrock spa_load_spares(spa); 3751fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3752fa94a07fSbrendan } 3753fa94a07fSbrendan 3754fa94a07fSbrendan if (nl2cache != 0) { 3755fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 3756fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 3757fa94a07fSbrendan spa_load_l2cache(spa); 3758fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3759fa9e4066Sahrens } 3760fa9e4066Sahrens 3761fa9e4066Sahrens /* 37620e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 37630e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 37640e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 37650e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 37660e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 37670e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 37680373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 37690e34b6a7Sbonwick * 37700e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 37710e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 37720e34b6a7Sbonwick * steps will be completed the next time we load the pool. 37730e34b6a7Sbonwick */ 37740373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 37750e34b6a7Sbonwick 37760373e76bSbonwick mutex_enter(&spa_namespace_lock); 37770373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37780373e76bSbonwick mutex_exit(&spa_namespace_lock); 3779fa9e4066Sahrens 37800373e76bSbonwick return (0); 3781fa9e4066Sahrens } 3782fa9e4066Sahrens 3783fa9e4066Sahrens /* 3784fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 3785fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3786fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3787fa9e4066Sahrens * 3788fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 3789fa9e4066Sahrens * existing device; in this case the two devices are made into their own 37903d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3791fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3792fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 3793fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 3794fa9e4066Sahrens * is automatically detached. 3795fa9e4066Sahrens */ 3796fa9e4066Sahrens int 3797ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3798fa9e4066Sahrens { 37993f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 3800fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3801fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 380299653d4eSeschrock vdev_ops_t *pvops; 38039b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 38049b3f6b42SEric Kustarz int newvd_isspare; 38059b3f6b42SEric Kustarz int error; 3806fa9e4066Sahrens 3807f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3808f9af39baSGeorge Wilson 3809fa9e4066Sahrens txg = spa_vdev_enter(spa); 3810fa9e4066Sahrens 3811c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3812fa9e4066Sahrens 3813fa9e4066Sahrens if (oldvd == NULL) 3814fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3815fa9e4066Sahrens 38160e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 38170e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 38180e34b6a7Sbonwick 3819fa9e4066Sahrens pvd = oldvd->vdev_parent; 3820fa9e4066Sahrens 382199653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 38223d7072f8Seschrock VDEV_ALLOC_ADD)) != 0) 38233d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 38243d7072f8Seschrock 38253d7072f8Seschrock if (newrootvd->vdev_children != 1) 3826fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3827fa9e4066Sahrens 3828fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 3829fa9e4066Sahrens 3830fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3831fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3832fa9e4066Sahrens 383399653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3834fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3835fa9e4066Sahrens 38368654d025Sperrin /* 38378654d025Sperrin * Spares can't replace logs 38388654d025Sperrin */ 3839ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 38408654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38418654d025Sperrin 384299653d4eSeschrock if (!replacing) { 384399653d4eSeschrock /* 384499653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 384599653d4eSeschrock * vdev. 384699653d4eSeschrock */ 384799653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 384899653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 384999653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 385099653d4eSeschrock 385199653d4eSeschrock pvops = &vdev_mirror_ops; 385299653d4eSeschrock } else { 385399653d4eSeschrock /* 385499653d4eSeschrock * Active hot spares can only be replaced by inactive hot 385599653d4eSeschrock * spares. 385699653d4eSeschrock */ 385799653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 3858cb04b873SMark J Musante oldvd->vdev_isspare && 385999653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 386099653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 386199653d4eSeschrock 386299653d4eSeschrock /* 386399653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 386499653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 386539c23413Seschrock * want to create a replacing vdev. The user is not allowed to 386639c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 386739c23413Seschrock * the same (spare replaces spare, non-spare replaces 386839c23413Seschrock * non-spare). 386999653d4eSeschrock */ 3870cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 3871cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 387299653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3873cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 3874cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 387539c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3876cb04b873SMark J Musante } 3877cb04b873SMark J Musante 3878cb04b873SMark J Musante if (newvd->vdev_isspare) 387999653d4eSeschrock pvops = &vdev_spare_ops; 388099653d4eSeschrock else 388199653d4eSeschrock pvops = &vdev_replacing_ops; 388299653d4eSeschrock } 388399653d4eSeschrock 38842a79c5feSlling /* 3885573ca77eSGeorge Wilson * Make sure the new device is big enough. 38862a79c5feSlling */ 3887573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3888fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3889fa9e4066Sahrens 3890ecc2d604Sbonwick /* 3891ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 3892ecc2d604Sbonwick * than the top-level vdev. 3893ecc2d604Sbonwick */ 3894ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3895fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3896fa9e4066Sahrens 3897fa9e4066Sahrens /* 3898fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 3899fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3900fa9e4066Sahrens */ 3901fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3902fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 3903fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3904fa9e4066Sahrens KM_SLEEP); 3905fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3906fa9e4066Sahrens newvd->vdev_path, "old"); 3907fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 3908fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 3909fa9e4066Sahrens oldvd->vdev_devid = NULL; 3910fa9e4066Sahrens } 3911fa9e4066Sahrens } 3912fa9e4066Sahrens 3913cb04b873SMark J Musante /* mark the device being resilvered */ 3914cb04b873SMark J Musante newvd->vdev_resilvering = B_TRUE; 3915cb04b873SMark J Musante 3916fa9e4066Sahrens /* 391799653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 391899653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 3919fa9e4066Sahrens */ 3920fa9e4066Sahrens if (pvd->vdev_ops != pvops) 3921fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 3922fa9e4066Sahrens 3923fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3924fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 3925fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 3926fa9e4066Sahrens 3927fa9e4066Sahrens /* 3928fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 3929fa9e4066Sahrens */ 3930fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 3931fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 393288ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 3933fa9e4066Sahrens vdev_add_child(pvd, newvd); 3934fa9e4066Sahrens 3935fa9e4066Sahrens tvd = newvd->vdev_top; 3936fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 3937fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 3938fa9e4066Sahrens 3939fa9e4066Sahrens vdev_config_dirty(tvd); 3940fa9e4066Sahrens 3941fa9e4066Sahrens /* 39423f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 39433f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 39443f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 3945fa9e4066Sahrens */ 39463f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 3947fa9e4066Sahrens 39483f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 39493f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 3950fa9e4066Sahrens 39516809eb4eSEric Schrock if (newvd->vdev_isspare) { 395239c23413Seschrock spa_spare_activate(newvd); 39536809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 39546809eb4eSEric Schrock } 39556809eb4eSEric Schrock 3956e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 3957e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 39589b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 3959ea8dc4b6Seschrock 3960fa9e4066Sahrens /* 3961fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 3962fa9e4066Sahrens */ 3963ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3964fa9e4066Sahrens 39653f9d6ad7SLin Ling /* 39663f9d6ad7SLin Ling * Restart the resilver 39673f9d6ad7SLin Ling */ 39683f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 39693f9d6ad7SLin Ling 39703f9d6ad7SLin Ling /* 39713f9d6ad7SLin Ling * Commit the config 39723f9d6ad7SLin Ling */ 39733f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 3974fa9e4066Sahrens 39753f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL, 39763f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 3977c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 3978c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 3979c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 39809b3f6b42SEric Kustarz 39819b3f6b42SEric Kustarz spa_strfree(oldvdpath); 39829b3f6b42SEric Kustarz spa_strfree(newvdpath); 39839b3f6b42SEric Kustarz 3984943e9869SLori Alt if (spa->spa_bootfs) 3985943e9869SLori Alt spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 3986943e9869SLori Alt 3987fa9e4066Sahrens return (0); 3988fa9e4066Sahrens } 3989fa9e4066Sahrens 3990fa9e4066Sahrens /* 3991fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 3992fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 3993fa9e4066Sahrens * is a replacing vdev. 3994fa9e4066Sahrens */ 3995fa9e4066Sahrens int 39968ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3997fa9e4066Sahrens { 3998fa9e4066Sahrens uint64_t txg; 39998ad4d6ddSJeff Bonwick int error; 4000fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4001fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 400299653d4eSeschrock boolean_t unspare = B_FALSE; 400399653d4eSeschrock uint64_t unspare_guid; 40041195e687SMark J Musante char *vdpath; 4005fa9e4066Sahrens 4006f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4007f9af39baSGeorge Wilson 4008fa9e4066Sahrens txg = spa_vdev_enter(spa); 4009fa9e4066Sahrens 4010c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4011fa9e4066Sahrens 4012fa9e4066Sahrens if (vd == NULL) 4013fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4014fa9e4066Sahrens 40150e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 40160e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 40170e34b6a7Sbonwick 4018fa9e4066Sahrens pvd = vd->vdev_parent; 4019fa9e4066Sahrens 40208ad4d6ddSJeff Bonwick /* 40218ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 40228ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 40238ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 40248ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 40258ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 40268ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 40278ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 40288ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 40298ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 40308ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 40318ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 40328ad4d6ddSJeff Bonwick */ 40338ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 40348ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 40358ad4d6ddSJeff Bonwick 4036fa9e4066Sahrens /* 4037cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 403899653d4eSeschrock */ 4039cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4040cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4041cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 404299653d4eSeschrock 404399653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4044e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4045fa9e4066Sahrens 4046fa9e4066Sahrens /* 404799653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4048fa9e4066Sahrens */ 4049fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 405099653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 405199653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4052fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4053fa9e4066Sahrens 4054fa9e4066Sahrens /* 40558ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 40568ad4d6ddSJeff Bonwick * we cannot safely detach it. 4057fa9e4066Sahrens */ 40588ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4059fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4060fa9e4066Sahrens 40618ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4062fa9e4066Sahrens 4063bf82a41bSeschrock /* 4064bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4065bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4066bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4067bf82a41bSeschrock */ 4068cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4069cb04b873SMark J Musante vd->vdev_path != NULL) { 4070cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4071cb04b873SMark J Musante 4072cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4073cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4074cb04b873SMark J Musante 4075cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4076cb04b873SMark J Musante continue; 4077cb04b873SMark J Musante 4078cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4079cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4080cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4081cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4082cb04b873SMark J Musante break; 4083cb04b873SMark J Musante } 4084bf82a41bSeschrock } 4085bf82a41bSeschrock } 4086bf82a41bSeschrock 408799653d4eSeschrock /* 408899653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 408999653d4eSeschrock * that the spare should become a real disk, and be removed from the 409099653d4eSeschrock * active spare list for the pool. 409199653d4eSeschrock */ 409299653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4093cb04b873SMark J Musante vd->vdev_id == 0 && 4094cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 409599653d4eSeschrock unspare = B_TRUE; 409699653d4eSeschrock 4097fa9e4066Sahrens /* 4098fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4099fa9e4066Sahrens * This must be done after all other error cases are handled, 4100fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4101fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4102fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4103fa9e4066Sahrens * it's being detached! 4104fa9e4066Sahrens */ 410539c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4106fa9e4066Sahrens 4107fa9e4066Sahrens /* 4108fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4109fa9e4066Sahrens */ 4110fa9e4066Sahrens vdev_remove_child(pvd, vd); 4111fa9e4066Sahrens vdev_compact_children(pvd); 4112fa9e4066Sahrens 4113fa9e4066Sahrens /* 4114fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4115fa9e4066Sahrens */ 4116cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4117fa9e4066Sahrens 411899653d4eSeschrock /* 411999653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 41208ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 41218ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 41228ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 41238ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 41248ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 41258ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 412699653d4eSeschrock */ 412799653d4eSeschrock if (unspare) { 412899653d4eSeschrock ASSERT(cvd->vdev_isspare); 412939c23413Seschrock spa_spare_remove(cvd); 413099653d4eSeschrock unspare_guid = cvd->vdev_guid; 41318ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4132cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 413399653d4eSeschrock } 413499653d4eSeschrock 4135fa9e4066Sahrens /* 4136fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4137fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4138fa9e4066Sahrens */ 4139cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4140cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4141cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4142fa9e4066Sahrens vdev_remove_parent(cvd); 4143cb04b873SMark J Musante cvd->vdev_resilvering = B_FALSE; 4144cb04b873SMark J Musante } 4145cb04b873SMark J Musante 4146fa9e4066Sahrens 4147fa9e4066Sahrens /* 4148fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4149fa9e4066Sahrens * may have been the previous top-level vdev. 4150fa9e4066Sahrens */ 4151fa9e4066Sahrens tvd = cvd->vdev_top; 4152fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4153fa9e4066Sahrens 4154fa9e4066Sahrens /* 415539c23413Seschrock * Reevaluate the parent vdev state. 4156fa9e4066Sahrens */ 41573d7072f8Seschrock vdev_propagate_state(cvd); 4158fa9e4066Sahrens 4159fa9e4066Sahrens /* 4160573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4161573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4162573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4163573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4164573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4165fa9e4066Sahrens */ 4166573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4167573ca77eSGeorge Wilson vdev_reopen(tvd); 4168573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4169573ca77eSGeorge Wilson } 4170fa9e4066Sahrens 4171fa9e4066Sahrens vdev_config_dirty(tvd); 4172fa9e4066Sahrens 4173fa9e4066Sahrens /* 417439c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 417539c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 417639c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 417739c23413Seschrock * prevent vd from being accessed after it's freed. 4178fa9e4066Sahrens */ 41791195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 41808ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4181fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4182ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4183ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4184fa9e4066Sahrens 41853d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 41863d7072f8Seschrock 4187cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4188cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4189cb04b873SMark J Musante 419099653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 419199653d4eSeschrock 41923f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL, 41931195e687SMark J Musante "vdev=%s", vdpath); 41941195e687SMark J Musante spa_strfree(vdpath); 41951195e687SMark J Musante 419699653d4eSeschrock /* 419739c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 419839c23413Seschrock * then we want to go through and remove the device from the hot spare 419939c23413Seschrock * list of every other pool. 420099653d4eSeschrock */ 420199653d4eSeschrock if (unspare) { 4202cb04b873SMark J Musante spa_t *altspa = NULL; 4203cb04b873SMark J Musante 420499653d4eSeschrock mutex_enter(&spa_namespace_lock); 4205cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 4206cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 4207cb04b873SMark J Musante altspa == spa) 420899653d4eSeschrock continue; 4209cb04b873SMark J Musante 4210cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 42119af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 4212cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 42139af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 4214cb04b873SMark J Musante spa_close(altspa, FTAG); 421599653d4eSeschrock } 421699653d4eSeschrock mutex_exit(&spa_namespace_lock); 4217cb04b873SMark J Musante 4218cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 4219cb04b873SMark J Musante spa_vdev_resilver_done(spa); 422099653d4eSeschrock } 422199653d4eSeschrock 4222cb04b873SMark J Musante /* all done with the spa; OK to release */ 4223cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 4224cb04b873SMark J Musante spa_close(spa, FTAG); 4225cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 4226cb04b873SMark J Musante 422799653d4eSeschrock return (error); 422899653d4eSeschrock } 422999653d4eSeschrock 42301195e687SMark J Musante /* 42311195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 42321195e687SMark J Musante */ 42331195e687SMark J Musante int 42341195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 42351195e687SMark J Musante nvlist_t *props, boolean_t exp) 42361195e687SMark J Musante { 42371195e687SMark J Musante int error = 0; 42381195e687SMark J Musante uint64_t txg, *glist; 42391195e687SMark J Musante spa_t *newspa; 42401195e687SMark J Musante uint_t c, children, lastlog; 42411195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 42421195e687SMark J Musante dmu_tx_t *tx; 42431195e687SMark J Musante char *altroot = NULL; 42441195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 42451195e687SMark J Musante boolean_t activate_slog; 42461195e687SMark J Musante 4247f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 42481195e687SMark J Musante 42491195e687SMark J Musante txg = spa_vdev_enter(spa); 42501195e687SMark J Musante 42511195e687SMark J Musante /* clear the log and flush everything up to now */ 42521195e687SMark J Musante activate_slog = spa_passivate_log(spa); 42531195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 42541195e687SMark J Musante error = spa_offline_log(spa); 42551195e687SMark J Musante txg = spa_vdev_config_enter(spa); 42561195e687SMark J Musante 42571195e687SMark J Musante if (activate_slog) 42581195e687SMark J Musante spa_activate_log(spa); 42591195e687SMark J Musante 42601195e687SMark J Musante if (error != 0) 42611195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 42621195e687SMark J Musante 42631195e687SMark J Musante /* check new spa name before going any further */ 42641195e687SMark J Musante if (spa_lookup(newname) != NULL) 42651195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 42661195e687SMark J Musante 42671195e687SMark J Musante /* 42681195e687SMark J Musante * scan through all the children to ensure they're all mirrors 42691195e687SMark J Musante */ 42701195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 42711195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 42721195e687SMark J Musante &children) != 0) 42731195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 42741195e687SMark J Musante 42751195e687SMark J Musante /* first, check to ensure we've got the right child count */ 42761195e687SMark J Musante rvd = spa->spa_root_vdev; 42771195e687SMark J Musante lastlog = 0; 42781195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 42791195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 42801195e687SMark J Musante 42811195e687SMark J Musante /* don't count the holes & logs as children */ 42821195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 42831195e687SMark J Musante if (lastlog == 0) 42841195e687SMark J Musante lastlog = c; 42851195e687SMark J Musante continue; 42861195e687SMark J Musante } 42871195e687SMark J Musante 42881195e687SMark J Musante lastlog = 0; 42891195e687SMark J Musante } 42901195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 42911195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 42921195e687SMark J Musante 42931195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 42941195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 42951195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 42961195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 42971195e687SMark J Musante 42981195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 42991195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 43001195e687SMark J Musante 43011195e687SMark J Musante /* then, loop over each vdev and validate it */ 43021195e687SMark J Musante for (c = 0; c < children; c++) { 43031195e687SMark J Musante uint64_t is_hole = 0; 43041195e687SMark J Musante 43051195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 43061195e687SMark J Musante &is_hole); 43071195e687SMark J Musante 43081195e687SMark J Musante if (is_hole != 0) { 43091195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 43101195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 43111195e687SMark J Musante continue; 43121195e687SMark J Musante } else { 43131195e687SMark J Musante error = EINVAL; 43141195e687SMark J Musante break; 43151195e687SMark J Musante } 43161195e687SMark J Musante } 43171195e687SMark J Musante 43181195e687SMark J Musante /* which disk is going to be split? */ 43191195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 43201195e687SMark J Musante &glist[c]) != 0) { 43211195e687SMark J Musante error = EINVAL; 43221195e687SMark J Musante break; 43231195e687SMark J Musante } 43241195e687SMark J Musante 43251195e687SMark J Musante /* look it up in the spa */ 43261195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 43271195e687SMark J Musante if (vml[c] == NULL) { 43281195e687SMark J Musante error = ENODEV; 43291195e687SMark J Musante break; 43301195e687SMark J Musante } 43311195e687SMark J Musante 43321195e687SMark J Musante /* make sure there's nothing stopping the split */ 43331195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 43341195e687SMark J Musante vml[c]->vdev_islog || 43351195e687SMark J Musante vml[c]->vdev_ishole || 43361195e687SMark J Musante vml[c]->vdev_isspare || 43371195e687SMark J Musante vml[c]->vdev_isl2cache || 43381195e687SMark J Musante !vdev_writeable(vml[c]) || 4339d41c4376SMark J Musante vml[c]->vdev_children != 0 || 43401195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 43411195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 43421195e687SMark J Musante error = EINVAL; 43431195e687SMark J Musante break; 43441195e687SMark J Musante } 43451195e687SMark J Musante 43461195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 43471195e687SMark J Musante error = EBUSY; 43481195e687SMark J Musante break; 43491195e687SMark J Musante } 43501195e687SMark J Musante 43511195e687SMark J Musante /* we need certain info from the top level */ 43521195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 43531195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 43541195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 43551195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 43561195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 43571195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 43581195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 43591195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 43601195e687SMark J Musante } 43611195e687SMark J Musante 43621195e687SMark J Musante if (error != 0) { 43631195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 43641195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 43651195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 43661195e687SMark J Musante } 43671195e687SMark J Musante 43681195e687SMark J Musante /* stop writers from using the disks */ 43691195e687SMark J Musante for (c = 0; c < children; c++) { 43701195e687SMark J Musante if (vml[c] != NULL) 43711195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 43721195e687SMark J Musante } 43731195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 43741195e687SMark J Musante 43751195e687SMark J Musante /* 43761195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 43771195e687SMark J Musante * will disappear once the config is regenerated. 43781195e687SMark J Musante */ 43791195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 43801195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 43811195e687SMark J Musante glist, children) == 0); 43821195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 43831195e687SMark J Musante 438498295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 43851195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 43861195e687SMark J Musante nvl) == 0); 438798295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 43881195e687SMark J Musante spa->spa_config_splitting = nvl; 43891195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 43901195e687SMark J Musante 43911195e687SMark J Musante /* configure and create the new pool */ 43921195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 43931195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 43941195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 43951195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 43961195e687SMark J Musante spa_version(spa)) == 0); 43971195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 43981195e687SMark J Musante spa->spa_config_txg) == 0); 43991195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 44001195e687SMark J Musante spa_generate_guid(NULL)) == 0); 44011195e687SMark J Musante (void) nvlist_lookup_string(props, 44021195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 44031195e687SMark J Musante 4404d41c4376SMark J Musante /* add the new pool to the namespace */ 44051195e687SMark J Musante newspa = spa_add(newname, config, altroot); 44061195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 44071195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 44081195e687SMark J Musante 44091195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 44101195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 44111195e687SMark J Musante 44121195e687SMark J Musante if (zio_injection_enabled) 44131195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 44141195e687SMark J Musante 44151195e687SMark J Musante spa_activate(newspa, spa_mode_global); 44161195e687SMark J Musante spa_async_suspend(newspa); 44171195e687SMark J Musante 44181195e687SMark J Musante /* create the new pool from the disks of the original pool */ 44191195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 44201195e687SMark J Musante if (error) 44211195e687SMark J Musante goto out; 44221195e687SMark J Musante 44231195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 44241195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 44251195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 44261195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 44271195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 44281195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 44291195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 44301195e687SMark J Musante B_TRUE)); 44311195e687SMark J Musante } 44321195e687SMark J Musante 44331195e687SMark J Musante /* set the props */ 44341195e687SMark J Musante if (props != NULL) { 44351195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 44361195e687SMark J Musante error = spa_prop_set(newspa, props); 44371195e687SMark J Musante if (error) 44381195e687SMark J Musante goto out; 44391195e687SMark J Musante } 44401195e687SMark J Musante 44411195e687SMark J Musante /* flush everything */ 44421195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 44431195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 44441195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 44451195e687SMark J Musante 44461195e687SMark J Musante if (zio_injection_enabled) 44471195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 44481195e687SMark J Musante 44491195e687SMark J Musante spa_async_resume(newspa); 44501195e687SMark J Musante 44511195e687SMark J Musante /* finally, update the original pool's config */ 44521195e687SMark J Musante txg = spa_vdev_config_enter(spa); 44531195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 44541195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 44551195e687SMark J Musante if (error != 0) 44561195e687SMark J Musante dmu_tx_abort(tx); 44571195e687SMark J Musante for (c = 0; c < children; c++) { 44581195e687SMark J Musante if (vml[c] != NULL) { 44591195e687SMark J Musante vdev_split(vml[c]); 44601195e687SMark J Musante if (error == 0) 44613f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_DETACH, 44623f9d6ad7SLin Ling spa, tx, "vdev=%s", 44631195e687SMark J Musante vml[c]->vdev_path); 44641195e687SMark J Musante vdev_free(vml[c]); 44651195e687SMark J Musante } 44661195e687SMark J Musante } 44671195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 44681195e687SMark J Musante spa->spa_config_splitting = NULL; 44691195e687SMark J Musante nvlist_free(nvl); 44701195e687SMark J Musante if (error == 0) 44711195e687SMark J Musante dmu_tx_commit(tx); 44721195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 44731195e687SMark J Musante 44741195e687SMark J Musante if (zio_injection_enabled) 44751195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 44761195e687SMark J Musante 44771195e687SMark J Musante /* split is complete; log a history record */ 44783f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL, 44791195e687SMark J Musante "split new pool %s from pool %s", newname, spa_name(spa)); 44801195e687SMark J Musante 44811195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 44821195e687SMark J Musante 44831195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 44841195e687SMark J Musante if (exp) 44851195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 44861195e687SMark J Musante B_FALSE, B_FALSE); 44871195e687SMark J Musante 44881195e687SMark J Musante return (error); 44891195e687SMark J Musante 44901195e687SMark J Musante out: 44911195e687SMark J Musante spa_unload(newspa); 44921195e687SMark J Musante spa_deactivate(newspa); 44931195e687SMark J Musante spa_remove(newspa); 44941195e687SMark J Musante 44951195e687SMark J Musante txg = spa_vdev_config_enter(spa); 449698295d61SMark J Musante 449798295d61SMark J Musante /* re-online all offlined disks */ 449898295d61SMark J Musante for (c = 0; c < children; c++) { 449998295d61SMark J Musante if (vml[c] != NULL) 450098295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 450198295d61SMark J Musante } 450298295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 450398295d61SMark J Musante 45041195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 45051195e687SMark J Musante spa->spa_config_splitting = NULL; 4506d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 45071195e687SMark J Musante 45081195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 45091195e687SMark J Musante return (error); 45101195e687SMark J Musante } 45111195e687SMark J Musante 4512e14bb325SJeff Bonwick static nvlist_t * 4513e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 451499653d4eSeschrock { 4515e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 4516e14bb325SJeff Bonwick uint64_t guid; 451799653d4eSeschrock 4518e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 4519e14bb325SJeff Bonwick &guid) == 0); 452099653d4eSeschrock 4521e14bb325SJeff Bonwick if (guid == target_guid) 4522e14bb325SJeff Bonwick return (nvpp[i]); 452399653d4eSeschrock } 452499653d4eSeschrock 4525e14bb325SJeff Bonwick return (NULL); 4526fa94a07fSbrendan } 4527fa94a07fSbrendan 4528e14bb325SJeff Bonwick static void 4529e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 4530e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 4531fa94a07fSbrendan { 4532e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 4533fa94a07fSbrendan 4534e14bb325SJeff Bonwick if (count > 1) 4535e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 4536fa94a07fSbrendan 4537e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 4538e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 4539e14bb325SJeff Bonwick continue; 4540e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 4541fa94a07fSbrendan } 4542fa94a07fSbrendan 4543e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 4544e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 4545fa94a07fSbrendan 4546e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 4547e14bb325SJeff Bonwick nvlist_free(newdev[i]); 4548fa94a07fSbrendan 4549e14bb325SJeff Bonwick if (count > 1) 4550e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 4551fa94a07fSbrendan } 4552fa94a07fSbrendan 455388ecc943SGeorge Wilson /* 455488ecc943SGeorge Wilson * Evacuate the device. 455588ecc943SGeorge Wilson */ 45563f9d6ad7SLin Ling static int 455788ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 455888ecc943SGeorge Wilson { 455988ecc943SGeorge Wilson uint64_t txg; 45603f9d6ad7SLin Ling int error = 0; 456188ecc943SGeorge Wilson 456288ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 456388ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 4564b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 456588ecc943SGeorge Wilson 456688ecc943SGeorge Wilson /* 456788ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 456888ecc943SGeorge Wilson * since we need to do I/O but we do keep the 456988ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 457088ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 457188ecc943SGeorge Wilson */ 457288ecc943SGeorge Wilson if (vd->vdev_islog) { 45733f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 45743f9d6ad7SLin Ling error = spa_offline_log(spa); 4575a1521560SJeff Bonwick } else { 45763f9d6ad7SLin Ling error = ENOTSUP; 457788ecc943SGeorge Wilson } 457888ecc943SGeorge Wilson 4579a1521560SJeff Bonwick if (error) 4580a1521560SJeff Bonwick return (error); 4581a1521560SJeff Bonwick 458288ecc943SGeorge Wilson /* 4583a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 4584a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 458588ecc943SGeorge Wilson */ 45863f9d6ad7SLin Ling ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 458788ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 458888ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 458988ecc943SGeorge Wilson vdev_dirty(vd, 0, NULL, txg); 459088ecc943SGeorge Wilson vdev_config_dirty(vd); 459188ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 459288ecc943SGeorge Wilson 459388ecc943SGeorge Wilson return (0); 459488ecc943SGeorge Wilson } 459588ecc943SGeorge Wilson 459688ecc943SGeorge Wilson /* 459788ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 459888ecc943SGeorge Wilson */ 45993f9d6ad7SLin Ling static void 4600a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 460188ecc943SGeorge Wilson { 460288ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 460388ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 460488ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 460588ecc943SGeorge Wilson 460688ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 460788ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4608b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 460988ecc943SGeorge Wilson 46103f9d6ad7SLin Ling /* 46113f9d6ad7SLin Ling * Only remove any devices which are empty. 46123f9d6ad7SLin Ling */ 46133f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 46143f9d6ad7SLin Ling return; 46153f9d6ad7SLin Ling 461688ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4617b24ab676SJeff Bonwick 4618b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 4619b24ab676SJeff Bonwick vdev_state_clean(vd); 4620b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 4621b24ab676SJeff Bonwick vdev_config_clean(vd); 4622b24ab676SJeff Bonwick 462388ecc943SGeorge Wilson vdev_free(vd); 462488ecc943SGeorge Wilson 462588ecc943SGeorge Wilson if (last_vdev) { 462688ecc943SGeorge Wilson vdev_compact_children(rvd); 462788ecc943SGeorge Wilson } else { 462888ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 462988ecc943SGeorge Wilson vdev_add_child(rvd, vd); 463088ecc943SGeorge Wilson } 4631fcbfa62bSLin Ling vdev_config_dirty(rvd); 4632fcbfa62bSLin Ling 4633fcbfa62bSLin Ling /* 4634fcbfa62bSLin Ling * Reassess the health of our root vdev. 4635fcbfa62bSLin Ling */ 4636fcbfa62bSLin Ling vdev_reopen(rvd); 463788ecc943SGeorge Wilson } 463888ecc943SGeorge Wilson 46393f9d6ad7SLin Ling /* 46403f9d6ad7SLin Ling * Remove a device from the pool - 46413f9d6ad7SLin Ling * 46423f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 46433f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 46443f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 46453f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 46463f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 46473f9d6ad7SLin Ling */ 46483f9d6ad7SLin Ling 4649fa94a07fSbrendan /* 4650fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 465188ecc943SGeorge Wilson * spares, slogs, and level 2 ARC devices. 4652fa94a07fSbrendan */ 4653fa94a07fSbrendan int 4654fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 4655fa94a07fSbrendan { 4656fa94a07fSbrendan vdev_t *vd; 4657a1521560SJeff Bonwick metaslab_group_t *mg; 4658e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 46598ad4d6ddSJeff Bonwick uint64_t txg = 0; 466088ecc943SGeorge Wilson uint_t nspares, nl2cache; 4661fa94a07fSbrendan int error = 0; 46628ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 4663fa94a07fSbrendan 4664f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4665f9af39baSGeorge Wilson 46668ad4d6ddSJeff Bonwick if (!locked) 46678ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 4668fa94a07fSbrendan 4669c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4670fa94a07fSbrendan 4671fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 4672fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 4673e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 4674e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 4675e14bb325SJeff Bonwick /* 4676e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 4677e14bb325SJeff Bonwick * in this pool. 4678e14bb325SJeff Bonwick */ 4679e14bb325SJeff Bonwick if (vd == NULL || unspare) { 4680e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 4681e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 4682e14bb325SJeff Bonwick spa_load_spares(spa); 4683e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 4684e14bb325SJeff Bonwick } else { 4685e14bb325SJeff Bonwick error = EBUSY; 4686e14bb325SJeff Bonwick } 4687e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 4688fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 4689e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 4690e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 4691e14bb325SJeff Bonwick /* 4692e14bb325SJeff Bonwick * Cache devices can always be removed. 4693e14bb325SJeff Bonwick */ 4694e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 4695e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 4696fa94a07fSbrendan spa_load_l2cache(spa); 4697fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 469888ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 469988ecc943SGeorge Wilson ASSERT(!locked); 4700b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 470188ecc943SGeorge Wilson 470288ecc943SGeorge Wilson /* 470388ecc943SGeorge Wilson * XXX - Once we have bp-rewrite this should 470488ecc943SGeorge Wilson * become the common case. 470588ecc943SGeorge Wilson */ 470688ecc943SGeorge Wilson 4707a1521560SJeff Bonwick mg = vd->vdev_mg; 4708a1521560SJeff Bonwick 470988ecc943SGeorge Wilson /* 4710a1521560SJeff Bonwick * Stop allocating from this vdev. 471188ecc943SGeorge Wilson */ 4712a1521560SJeff Bonwick metaslab_group_passivate(mg); 471388ecc943SGeorge Wilson 4714b24ab676SJeff Bonwick /* 4715b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 4716b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 4717b24ab676SJeff Bonwick */ 4718b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 4719b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 4720b24ab676SJeff Bonwick 4721a1521560SJeff Bonwick /* 4722a1521560SJeff Bonwick * Attempt to evacuate the vdev. 4723a1521560SJeff Bonwick */ 4724a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 4725a1521560SJeff Bonwick 472688ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 472788ecc943SGeorge Wilson 4728a1521560SJeff Bonwick /* 4729a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 4730a1521560SJeff Bonwick */ 4731a1521560SJeff Bonwick if (error) { 4732a1521560SJeff Bonwick metaslab_group_activate(mg); 4733a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 4734a1521560SJeff Bonwick } 4735a1521560SJeff Bonwick 4736a1521560SJeff Bonwick /* 4737a1521560SJeff Bonwick * Clean up the vdev namespace. 4738a1521560SJeff Bonwick */ 4739a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 474088ecc943SGeorge Wilson 4741e14bb325SJeff Bonwick } else if (vd != NULL) { 4742e14bb325SJeff Bonwick /* 4743e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 4744e14bb325SJeff Bonwick */ 4745e14bb325SJeff Bonwick error = ENOTSUP; 4746e14bb325SJeff Bonwick } else { 4747e14bb325SJeff Bonwick /* 4748e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 4749e14bb325SJeff Bonwick */ 4750e14bb325SJeff Bonwick error = ENOENT; 4751fa94a07fSbrendan } 475299653d4eSeschrock 47538ad4d6ddSJeff Bonwick if (!locked) 47548ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 47558ad4d6ddSJeff Bonwick 47568ad4d6ddSJeff Bonwick return (error); 4757fa9e4066Sahrens } 4758fa9e4066Sahrens 4759fa9e4066Sahrens /* 47603d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 47613d7072f8Seschrock * current spared, so we can detach it. 4762fa9e4066Sahrens */ 4763ea8dc4b6Seschrock static vdev_t * 47643d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 4765fa9e4066Sahrens { 4766ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 4767fa9e4066Sahrens 4768573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 47693d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 4770ea8dc4b6Seschrock if (oldvd != NULL) 4771ea8dc4b6Seschrock return (oldvd); 4772ea8dc4b6Seschrock } 4773fa9e4066Sahrens 47743d7072f8Seschrock /* 4775cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 4776cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 4777cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 4778cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 4779cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 4780cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 47813d7072f8Seschrock */ 4782cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 4783cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 4784cb04b873SMark J Musante 4785cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 4786ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 4787ea8dc4b6Seschrock 47888ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 4789e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 47908ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 4791ea8dc4b6Seschrock return (oldvd); 4792fa9e4066Sahrens } 4793ea8dc4b6Seschrock 47943d7072f8Seschrock /* 47953d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 47963d7072f8Seschrock */ 4797cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 4798cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 4799cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 4800cb04b873SMark J Musante 4801cb04b873SMark J Musante if (last->vdev_unspare) { 4802cb04b873SMark J Musante oldvd = first; 4803cb04b873SMark J Musante newvd = last; 4804cb04b873SMark J Musante } else if (first->vdev_unspare) { 4805cb04b873SMark J Musante oldvd = last; 4806cb04b873SMark J Musante newvd = first; 4807cb04b873SMark J Musante } else { 4808cb04b873SMark J Musante oldvd = NULL; 4809cb04b873SMark J Musante } 48103d7072f8Seschrock 4811cb04b873SMark J Musante if (oldvd != NULL && 48128ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 4813e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 4814cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 48153d7072f8Seschrock return (oldvd); 4816cb04b873SMark J Musante 4817cb04b873SMark J Musante /* 4818cb04b873SMark J Musante * If there are more than two spares attached to a disk, 4819cb04b873SMark J Musante * and those spares are not required, then we want to 4820cb04b873SMark J Musante * attempt to free them up now so that they can be used 4821cb04b873SMark J Musante * by other pools. Once we're back down to a single 4822cb04b873SMark J Musante * disk+spare, we stop removing them. 4823cb04b873SMark J Musante */ 4824cb04b873SMark J Musante if (vd->vdev_children > 2) { 4825cb04b873SMark J Musante newvd = vd->vdev_child[1]; 4826cb04b873SMark J Musante 4827cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 4828cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 4829cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 4830cb04b873SMark J Musante !vdev_dtl_required(newvd)) 4831cb04b873SMark J Musante return (newvd); 48323d7072f8Seschrock } 48333d7072f8Seschrock } 48343d7072f8Seschrock 4835ea8dc4b6Seschrock return (NULL); 4836fa9e4066Sahrens } 4837fa9e4066Sahrens 4838ea8dc4b6Seschrock static void 48393d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 4840fa9e4066Sahrens { 48418ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 48428ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 4843ea8dc4b6Seschrock 48448ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4845ea8dc4b6Seschrock 48463d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 48478ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 48488ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 4849ea8dc4b6Seschrock guid = vd->vdev_guid; 48508ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 48518ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 48528ad4d6ddSJeff Bonwick sguid = 0; 485399653d4eSeschrock /* 485499653d4eSeschrock * If we have just finished replacing a hot spared device, then 485599653d4eSeschrock * we need to detach the parent's first child (the original hot 485699653d4eSeschrock * spare) as well. 485799653d4eSeschrock */ 4858cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 4859cb04b873SMark J Musante ppvd->vdev_children == 2) { 486099653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 48618ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 486299653d4eSeschrock } 48638ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 48648ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 4865ea8dc4b6Seschrock return; 48668ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 486799653d4eSeschrock return; 48688ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4869fa9e4066Sahrens } 4870fa9e4066Sahrens 48718ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4872fa9e4066Sahrens } 4873fa9e4066Sahrens 4874c67d9675Seschrock /* 4875b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 4876c67d9675Seschrock */ 4877c67d9675Seschrock int 48786809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 48796809eb4eSEric Schrock boolean_t ispath) 4880c67d9675Seschrock { 4881c5904d13Seschrock vdev_t *vd; 4882208044b8SGeorge Wilson boolean_t sync = B_FALSE; 4883c67d9675Seschrock 4884f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4885f9af39baSGeorge Wilson 4886b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 4887c67d9675Seschrock 48886809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 4889b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 4890c67d9675Seschrock 48910e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 4892b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 48930e34b6a7Sbonwick 48946809eb4eSEric Schrock if (ispath) { 4895208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 4896208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 4897208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 4898208044b8SGeorge Wilson sync = B_TRUE; 4899208044b8SGeorge Wilson } 49006809eb4eSEric Schrock } else { 4901208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 4902208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 4903208044b8SGeorge Wilson sync = B_TRUE; 4904208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 49056809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 4906208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 4907208044b8SGeorge Wilson sync = B_TRUE; 4908208044b8SGeorge Wilson } 49096809eb4eSEric Schrock } 4910c67d9675Seschrock 4911208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 4912c67d9675Seschrock } 4913c67d9675Seschrock 49146809eb4eSEric Schrock int 49156809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 49166809eb4eSEric Schrock { 49176809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 49186809eb4eSEric Schrock } 49196809eb4eSEric Schrock 49206809eb4eSEric Schrock int 49216809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 49226809eb4eSEric Schrock { 49236809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 49246809eb4eSEric Schrock } 49256809eb4eSEric Schrock 4926fa9e4066Sahrens /* 4927fa9e4066Sahrens * ========================================================================== 49283f9d6ad7SLin Ling * SPA Scanning 4929fa9e4066Sahrens * ========================================================================== 4930fa9e4066Sahrens */ 4931fa9e4066Sahrens 4932ea8dc4b6Seschrock int 49333f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 4934fa9e4066Sahrens { 4935e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 49363f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 49373f9d6ad7SLin Ling return (EBUSY); 49383f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 49393f9d6ad7SLin Ling } 4940bb8b5132Sek 49413f9d6ad7SLin Ling int 49423f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 49433f9d6ad7SLin Ling { 49443f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 49453f9d6ad7SLin Ling 49463f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 4947fa9e4066Sahrens return (ENOTSUP); 4948fa9e4066Sahrens 4949fa9e4066Sahrens /* 4950088f3894Sahrens * If a resilver was requested, but there is no DTL on a 4951088f3894Sahrens * writeable leaf device, we have nothing to do. 4952fa9e4066Sahrens */ 49533f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 4954088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 4955088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 4956ea8dc4b6Seschrock return (0); 4957ea8dc4b6Seschrock } 4958fa9e4066Sahrens 49593f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 4960fa9e4066Sahrens } 4961fa9e4066Sahrens 4962ea8dc4b6Seschrock /* 4963ea8dc4b6Seschrock * ========================================================================== 4964ea8dc4b6Seschrock * SPA async task processing 4965ea8dc4b6Seschrock * ========================================================================== 4966ea8dc4b6Seschrock */ 4967ea8dc4b6Seschrock 4968ea8dc4b6Seschrock static void 49693d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4970fa9e4066Sahrens { 497149cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 497298d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 497398d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 497449cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 49751d713200SEric Schrock 49761d713200SEric Schrock /* 49771d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 49781d713200SEric Schrock * vdev_clear() as that will cause us to throw away 49791d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 49801d713200SEric Schrock * device, all of which is a waste. 49811d713200SEric Schrock */ 49821d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 49831d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 49841d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 49851d713200SEric Schrock 4986e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 4987ea8dc4b6Seschrock } 498849cf58c0SBrendan Gregg - Sun Microsystems 4989e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 499049cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 4991ea8dc4b6Seschrock } 4992fa9e4066Sahrens 4993e14bb325SJeff Bonwick static void 4994e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 4995e14bb325SJeff Bonwick { 4996e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 499798d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 4998e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 4999e14bb325SJeff Bonwick } 5000e14bb325SJeff Bonwick 5001e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5002e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5003e14bb325SJeff Bonwick } 5004e14bb325SJeff Bonwick 5005573ca77eSGeorge Wilson static void 5006573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5007573ca77eSGeorge Wilson { 5008573ca77eSGeorge Wilson sysevent_id_t eid; 5009573ca77eSGeorge Wilson nvlist_t *attr; 5010573ca77eSGeorge Wilson char *physpath; 5011573ca77eSGeorge Wilson 5012573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5013573ca77eSGeorge Wilson return; 5014573ca77eSGeorge Wilson 5015573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5016573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5017573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5018573ca77eSGeorge Wilson } 5019573ca77eSGeorge Wilson 5020573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5021573ca77eSGeorge Wilson return; 5022573ca77eSGeorge Wilson 5023573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5024573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5025573ca77eSGeorge Wilson 5026573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5027573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5028573ca77eSGeorge Wilson 5029573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5030573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5031573ca77eSGeorge Wilson 5032573ca77eSGeorge Wilson nvlist_free(attr); 5033573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5034573ca77eSGeorge Wilson } 5035573ca77eSGeorge Wilson 5036ea8dc4b6Seschrock static void 5037ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5038ea8dc4b6Seschrock { 5039e14bb325SJeff Bonwick int tasks; 5040ea8dc4b6Seschrock 5041ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5042ea8dc4b6Seschrock 5043ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5044ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5045ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5046ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5047ea8dc4b6Seschrock 50480373e76bSbonwick /* 50490373e76bSbonwick * See if the config needs to be updated. 50500373e76bSbonwick */ 50510373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5052b24ab676SJeff Bonwick uint64_t old_space, new_space; 5053573ca77eSGeorge Wilson 50540373e76bSbonwick mutex_enter(&spa_namespace_lock); 5055b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 50560373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5057b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 50580373e76bSbonwick mutex_exit(&spa_namespace_lock); 5059573ca77eSGeorge Wilson 5060573ca77eSGeorge Wilson /* 5061573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5062573ca77eSGeorge Wilson * then log an internal history event. 5063573ca77eSGeorge Wilson */ 5064b24ab676SJeff Bonwick if (new_space != old_space) { 50653f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_VDEV_ONLINE, 50663f9d6ad7SLin Ling spa, NULL, 5067c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5068b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5069573ca77eSGeorge Wilson } 50700373e76bSbonwick } 50710373e76bSbonwick 5072ea8dc4b6Seschrock /* 50733d7072f8Seschrock * See if any devices need to be marked REMOVED. 5074ea8dc4b6Seschrock */ 5075e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 50768f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 50773d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5078e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 507949cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5080e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 508149cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5082e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5083e14bb325SJeff Bonwick } 5084e14bb325SJeff Bonwick 5085573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5086573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5087573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5088573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5089573ca77eSGeorge Wilson } 5090573ca77eSGeorge Wilson 5091e14bb325SJeff Bonwick /* 5092e14bb325SJeff Bonwick * See if any devices need to be probed. 5093e14bb325SJeff Bonwick */ 5094e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 50958f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5096e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5097e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 50983d7072f8Seschrock } 5099ea8dc4b6Seschrock 5100ea8dc4b6Seschrock /* 5101ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5102ea8dc4b6Seschrock */ 51033d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 51043d7072f8Seschrock spa_vdev_resilver_done(spa); 5105fa9e4066Sahrens 5106ea8dc4b6Seschrock /* 5107ea8dc4b6Seschrock * Kick off a resilver. 5108ea8dc4b6Seschrock */ 5109088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 51103f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5111ea8dc4b6Seschrock 5112ea8dc4b6Seschrock /* 5113ea8dc4b6Seschrock * Let the world know that we're done. 5114ea8dc4b6Seschrock */ 5115ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5116ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5117ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5118ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5119ea8dc4b6Seschrock thread_exit(); 5120ea8dc4b6Seschrock } 5121ea8dc4b6Seschrock 5122ea8dc4b6Seschrock void 5123ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5124ea8dc4b6Seschrock { 5125ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5126ea8dc4b6Seschrock spa->spa_async_suspended++; 5127ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5128ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5129ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5130ea8dc4b6Seschrock } 5131ea8dc4b6Seschrock 5132ea8dc4b6Seschrock void 5133ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5134ea8dc4b6Seschrock { 5135ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5136ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5137ea8dc4b6Seschrock spa->spa_async_suspended--; 5138ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5139ea8dc4b6Seschrock } 5140ea8dc4b6Seschrock 5141ea8dc4b6Seschrock static void 5142ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5143ea8dc4b6Seschrock { 5144ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5145ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 51460373e76bSbonwick spa->spa_async_thread == NULL && 51470373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 5148ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5149ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5150ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5151ea8dc4b6Seschrock } 5152ea8dc4b6Seschrock 5153ea8dc4b6Seschrock void 5154ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5155ea8dc4b6Seschrock { 51563f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5157ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5158ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5159ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5160fa9e4066Sahrens } 5161fa9e4066Sahrens 5162fa9e4066Sahrens /* 5163fa9e4066Sahrens * ========================================================================== 5164fa9e4066Sahrens * SPA syncing routines 5165fa9e4066Sahrens * ========================================================================== 5166fa9e4066Sahrens */ 5167fa9e4066Sahrens 5168cde58dbcSMatthew Ahrens static int 5169cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5170cde58dbcSMatthew Ahrens { 5171cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5172cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5173cde58dbcSMatthew Ahrens return (0); 5174b24ab676SJeff Bonwick } 5175b24ab676SJeff Bonwick 5176cde58dbcSMatthew Ahrens static int 5177cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5178b24ab676SJeff Bonwick { 5179b24ab676SJeff Bonwick zio_t *zio = arg; 5180b24ab676SJeff Bonwick 5181b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5182b24ab676SJeff Bonwick zio->io_flags)); 5183cde58dbcSMatthew Ahrens return (0); 5184fa9e4066Sahrens } 5185fa9e4066Sahrens 5186fa9e4066Sahrens static void 518799653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 5188fa9e4066Sahrens { 5189fa9e4066Sahrens char *packed = NULL; 5190f7991ba4STim Haley size_t bufsize; 5191fa9e4066Sahrens size_t nvsize = 0; 5192fa9e4066Sahrens dmu_buf_t *db; 5193fa9e4066Sahrens 519499653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 5195fa9e4066Sahrens 5196f7991ba4STim Haley /* 5197f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 5198f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 5199f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 5200f7991ba4STim Haley */ 5201f7991ba4STim Haley bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 5202f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 5203fa9e4066Sahrens 520499653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 5205ea8dc4b6Seschrock KM_SLEEP) == 0); 5206f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5207fa9e4066Sahrens 5208f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5209fa9e4066Sahrens 5210f7991ba4STim Haley kmem_free(packed, bufsize); 5211fa9e4066Sahrens 521299653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5213fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5214fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5215ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5216fa9e4066Sahrens } 5217fa9e4066Sahrens 521899653d4eSeschrock static void 5219fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5220fa94a07fSbrendan const char *config, const char *entry) 522199653d4eSeschrock { 522299653d4eSeschrock nvlist_t *nvroot; 5223fa94a07fSbrendan nvlist_t **list; 522499653d4eSeschrock int i; 522599653d4eSeschrock 5226fa94a07fSbrendan if (!sav->sav_sync) 522799653d4eSeschrock return; 522899653d4eSeschrock 522999653d4eSeschrock /* 5230fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5231fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 52323d7072f8Seschrock * valid and the vdevs are labeled appropriately. 523399653d4eSeschrock */ 5234fa94a07fSbrendan if (sav->sav_object == 0) { 5235fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5236fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5237fa94a07fSbrendan sizeof (uint64_t), tx); 523899653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5239fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5240fa94a07fSbrendan &sav->sav_object, tx) == 0); 524199653d4eSeschrock } 524299653d4eSeschrock 524399653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5244fa94a07fSbrendan if (sav->sav_count == 0) { 5245fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 524699653d4eSeschrock } else { 5247fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5248fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5249fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 52503f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5251fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5252fa94a07fSbrendan sav->sav_count) == 0); 5253fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5254fa94a07fSbrendan nvlist_free(list[i]); 5255fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 525699653d4eSeschrock } 525799653d4eSeschrock 5258fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 525906eeb2adSek nvlist_free(nvroot); 526099653d4eSeschrock 5261fa94a07fSbrendan sav->sav_sync = B_FALSE; 526299653d4eSeschrock } 526399653d4eSeschrock 526499653d4eSeschrock static void 526599653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 526699653d4eSeschrock { 526799653d4eSeschrock nvlist_t *config; 526899653d4eSeschrock 5269e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 527099653d4eSeschrock return; 527199653d4eSeschrock 5272e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5273e14bb325SJeff Bonwick 5274e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 5275e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 5276e14bb325SJeff Bonwick 5277e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 527899653d4eSeschrock 527999653d4eSeschrock if (spa->spa_config_syncing) 528099653d4eSeschrock nvlist_free(spa->spa_config_syncing); 528199653d4eSeschrock spa->spa_config_syncing = config; 528299653d4eSeschrock 528399653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 528499653d4eSeschrock } 528599653d4eSeschrock 5286990b4856Slling /* 5287990b4856Slling * Set zpool properties. 5288990b4856Slling */ 5289b1b8ab34Slling static void 52903f9d6ad7SLin Ling spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 5291b1b8ab34Slling { 5292b1b8ab34Slling spa_t *spa = arg1; 5293b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 5294990b4856Slling nvlist_t *nvp = arg2; 5295990b4856Slling nvpair_t *elem; 52963d7072f8Seschrock uint64_t intval; 5297c5904d13Seschrock char *strval; 5298990b4856Slling zpool_prop_t prop; 5299990b4856Slling const char *propname; 5300990b4856Slling zprop_type_t proptype; 5301b1b8ab34Slling 5302e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 5303e14bb325SJeff Bonwick 5304990b4856Slling elem = NULL; 5305990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 5306990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 5307990b4856Slling case ZPOOL_PROP_VERSION: 5308990b4856Slling /* 5309990b4856Slling * Only set version for non-zpool-creation cases 5310990b4856Slling * (set/import). spa_create() needs special care 5311990b4856Slling * for version setting. 5312990b4856Slling */ 5313990b4856Slling if (tx->tx_txg != TXG_INITIAL) { 5314990b4856Slling VERIFY(nvpair_value_uint64(elem, 5315990b4856Slling &intval) == 0); 5316990b4856Slling ASSERT(intval <= SPA_VERSION); 5317990b4856Slling ASSERT(intval >= spa_version(spa)); 5318990b4856Slling spa->spa_uberblock.ub_version = intval; 5319990b4856Slling vdev_config_dirty(spa->spa_root_vdev); 5320990b4856Slling } 5321ecd6cf80Smarks break; 5322990b4856Slling 5323990b4856Slling case ZPOOL_PROP_ALTROOT: 5324990b4856Slling /* 5325990b4856Slling * 'altroot' is a non-persistent property. It should 5326990b4856Slling * have been set temporarily at creation or import time. 5327990b4856Slling */ 5328990b4856Slling ASSERT(spa->spa_root != NULL); 5329b1b8ab34Slling break; 53303d7072f8Seschrock 5331f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 53322f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5333990b4856Slling /* 5334f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 5335f9af39baSGeorge Wilson * properties. 5336990b4856Slling */ 53373d7072f8Seschrock break; 5338990b4856Slling default: 5339990b4856Slling /* 5340990b4856Slling * Set pool property values in the poolprops mos object. 5341990b4856Slling */ 5342990b4856Slling if (spa->spa_pool_props_object == 0) { 5343990b4856Slling VERIFY((spa->spa_pool_props_object = 5344990b4856Slling zap_create(mos, DMU_OT_POOL_PROPS, 5345990b4856Slling DMU_OT_NONE, 0, tx)) > 0); 5346990b4856Slling 5347990b4856Slling VERIFY(zap_update(mos, 5348990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 5349990b4856Slling 8, 1, &spa->spa_pool_props_object, tx) 5350990b4856Slling == 0); 5351990b4856Slling } 5352990b4856Slling 5353990b4856Slling /* normalize the property name */ 5354990b4856Slling propname = zpool_prop_to_name(prop); 5355990b4856Slling proptype = zpool_prop_get_type(prop); 5356990b4856Slling 5357990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 5358990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 5359990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 5360990b4856Slling VERIFY(zap_update(mos, 5361990b4856Slling spa->spa_pool_props_object, propname, 5362990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 5363990b4856Slling 5364990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 5365990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5366990b4856Slling 5367990b4856Slling if (proptype == PROP_TYPE_INDEX) { 5368990b4856Slling const char *unused; 5369990b4856Slling VERIFY(zpool_prop_index_to_string( 5370990b4856Slling prop, intval, &unused) == 0); 5371990b4856Slling } 5372990b4856Slling VERIFY(zap_update(mos, 5373990b4856Slling spa->spa_pool_props_object, propname, 5374990b4856Slling 8, 1, &intval, tx) == 0); 5375990b4856Slling } else { 5376990b4856Slling ASSERT(0); /* not allowed */ 5377990b4856Slling } 5378990b4856Slling 53790a4e9518Sgw switch (prop) { 53800a4e9518Sgw case ZPOOL_PROP_DELEGATION: 5381990b4856Slling spa->spa_delegation = intval; 53820a4e9518Sgw break; 53830a4e9518Sgw case ZPOOL_PROP_BOOTFS: 5384990b4856Slling spa->spa_bootfs = intval; 53850a4e9518Sgw break; 53860a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 53870a4e9518Sgw spa->spa_failmode = intval; 53880a4e9518Sgw break; 5389573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 5390573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 5391b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 5392b98131cfSEric Taylor spa_async_request(spa, 5393b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 5394573ca77eSGeorge Wilson break; 5395b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 5396b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 5397b24ab676SJeff Bonwick break; 53980a4e9518Sgw default: 53990a4e9518Sgw break; 54000a4e9518Sgw } 5401990b4856Slling } 5402990b4856Slling 5403990b4856Slling /* log internal history if this is not a zpool create */ 5404990b4856Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 5405990b4856Slling tx->tx_txg != TXG_INITIAL) { 54063f9d6ad7SLin Ling spa_history_log_internal(LOG_POOL_PROPSET, 54073f9d6ad7SLin Ling spa, tx, "%s %lld %s", 5408e14bb325SJeff Bonwick nvpair_name(elem), intval, spa_name(spa)); 5409b1b8ab34Slling } 5410b1b8ab34Slling } 5411e14bb325SJeff Bonwick 5412e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 5413b1b8ab34Slling } 5414b1b8ab34Slling 5415cde58dbcSMatthew Ahrens /* 5416cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 5417cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 5418cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 5419cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 5420cde58dbcSMatthew Ahrens * tasks. 5421cde58dbcSMatthew Ahrens */ 5422cde58dbcSMatthew Ahrens static void 5423cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 5424cde58dbcSMatthew Ahrens { 5425cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5426cde58dbcSMatthew Ahrens 5427cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 5428cde58dbcSMatthew Ahrens 5429cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 5430cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 5431cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 5432cde58dbcSMatthew Ahrens 5433cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 5434cde58dbcSMatthew Ahrens spa->spa_minref += 3; 5435cde58dbcSMatthew Ahrens } 5436cde58dbcSMatthew Ahrens 5437cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 5438cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 5439cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 5440cde58dbcSMatthew Ahrens } 5441cde58dbcSMatthew Ahrens 5442cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 5443cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 5444cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 5445cde58dbcSMatthew Ahrens 5446cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 5447cde58dbcSMatthew Ahrens spa->spa_minref += 3; 5448cde58dbcSMatthew Ahrens } 5449cde58dbcSMatthew Ahrens } 5450cde58dbcSMatthew Ahrens 5451fa9e4066Sahrens /* 5452fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 5453fa9e4066Sahrens * part of the process, so we iterate until it converges. 5454fa9e4066Sahrens */ 5455fa9e4066Sahrens void 5456fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 5457fa9e4066Sahrens { 5458fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5459fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 5460cde58dbcSMatthew Ahrens bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; 5461b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 54620373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 5463fa9e4066Sahrens vdev_t *vd; 5464fa9e4066Sahrens dmu_tx_t *tx; 5465e14bb325SJeff Bonwick int error; 5466fa9e4066Sahrens 5467f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 5468f9af39baSGeorge Wilson 5469fa9e4066Sahrens /* 5470fa9e4066Sahrens * Lock out configuration changes. 5471fa9e4066Sahrens */ 5472e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5473fa9e4066Sahrens 5474fa9e4066Sahrens spa->spa_syncing_txg = txg; 5475fa9e4066Sahrens spa->spa_sync_pass = 0; 5476fa9e4066Sahrens 5477e14bb325SJeff Bonwick /* 5478e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 5479e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 5480e14bb325SJeff Bonwick */ 5481e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 54828ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 54838ad4d6ddSJeff Bonwick /* 54848ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 54858ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 54868ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 54878ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 54888ad4d6ddSJeff Bonwick * into the root vdev tree. 54898ad4d6ddSJeff Bonwick */ 54908ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 54918ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 54928ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 54938ad4d6ddSJeff Bonwick vdev_state_clean(vd); 54948ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 54958ad4d6ddSJeff Bonwick } 54968ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 54978ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 5498e14bb325SJeff Bonwick } 5499e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 5500e14bb325SJeff Bonwick 550199653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 550299653d4eSeschrock 550399653d4eSeschrock /* 5504e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 550599653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 550699653d4eSeschrock */ 5507e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 5508e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 550999653d4eSeschrock int i; 551099653d4eSeschrock 551199653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 551299653d4eSeschrock vd = rvd->vdev_child[i]; 551399653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 551499653d4eSeschrock break; 551599653d4eSeschrock } 551699653d4eSeschrock if (i == rvd->vdev_children) { 551799653d4eSeschrock spa->spa_deflate = TRUE; 551899653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 551999653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 552099653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 552199653d4eSeschrock } 552299653d4eSeschrock } 552399653d4eSeschrock 5524fa9e4066Sahrens /* 55253f9d6ad7SLin Ling * If anything has changed in this txg, or if someone is waiting 55263f9d6ad7SLin Ling * for this txg to sync (eg, spa_vdev_remove()), push the 55273f9d6ad7SLin Ling * deferred frees from the previous txg. If not, leave them 55283f9d6ad7SLin Ling * alone so that we don't generate work on an otherwise idle 55293f9d6ad7SLin Ling * system. 5530fa9e4066Sahrens */ 5531fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 55321615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 55333f9d6ad7SLin Ling !txg_list_empty(&dp->dp_sync_tasks, txg) || 5534cde58dbcSMatthew Ahrens ((dsl_scan_active(dp->dp_scan) || 5535cde58dbcSMatthew Ahrens txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { 5536cde58dbcSMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 5537cde58dbcSMatthew Ahrens VERIFY3U(bpobj_iterate(defer_bpo, 5538cde58dbcSMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 5539cde58dbcSMatthew Ahrens VERIFY3U(zio_wait(zio), ==, 0); 5540cde58dbcSMatthew Ahrens } 5541fa9e4066Sahrens 5542fa9e4066Sahrens /* 5543fa9e4066Sahrens * Iterate to convergence. 5544fa9e4066Sahrens */ 5545fa9e4066Sahrens do { 5546b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 5547fa9e4066Sahrens 5548fa9e4066Sahrens spa_sync_config_object(spa, tx); 5549fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 5550fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 5551fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 5552fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 5553ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 5554fa9e4066Sahrens dsl_pool_sync(dp, txg); 5555fa9e4066Sahrens 5556b24ab676SJeff Bonwick if (pass <= SYNC_PASS_DEFERRED_FREE) { 5557b24ab676SJeff Bonwick zio_t *zio = zio_root(spa, NULL, NULL, 0); 5558cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, spa_free_sync_cb, 5559cde58dbcSMatthew Ahrens zio, tx); 5560b24ab676SJeff Bonwick VERIFY(zio_wait(zio) == 0); 5561b24ab676SJeff Bonwick } else { 5562cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 5563cde58dbcSMatthew Ahrens defer_bpo, tx); 5564fa9e4066Sahrens } 5565fa9e4066Sahrens 5566b24ab676SJeff Bonwick ddt_sync(spa, txg); 55673f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 5568afee20e4SGeorge Wilson 5569b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 5570b24ab676SJeff Bonwick vdev_sync(vd, txg); 5571b24ab676SJeff Bonwick 5572cde58dbcSMatthew Ahrens if (pass == 1) 5573cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 5574fa9e4066Sahrens 5575cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 5576fa9e4066Sahrens 5577fa9e4066Sahrens /* 5578fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 5579fa9e4066Sahrens * to commit the transaction group. 55800373e76bSbonwick * 558117f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 558217f17c2dSbonwick * random top-level vdevs that are known to be visible in the 5583e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 5584e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 55850373e76bSbonwick */ 5586e14bb325SJeff Bonwick for (;;) { 5587e14bb325SJeff Bonwick /* 5588e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 5589e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 5590e14bb325SJeff Bonwick */ 5591e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5592e14bb325SJeff Bonwick 5593e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 5594e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 5595e14bb325SJeff Bonwick int svdcount = 0; 5596e14bb325SJeff Bonwick int children = rvd->vdev_children; 5597e14bb325SJeff Bonwick int c0 = spa_get_random(children); 5598e14bb325SJeff Bonwick 5599573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 5600e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 5601e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 5602e14bb325SJeff Bonwick continue; 5603e14bb325SJeff Bonwick svd[svdcount++] = vd; 5604e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 5605e14bb325SJeff Bonwick break; 5606e14bb325SJeff Bonwick } 56078956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 56088956713aSEric Schrock if (error != 0) 56098956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 56108956713aSEric Schrock B_TRUE); 5611e14bb325SJeff Bonwick } else { 5612e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 56138956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 56148956713aSEric Schrock if (error != 0) 56158956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 56168956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 56170373e76bSbonwick } 5618e14bb325SJeff Bonwick 5619e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 5620e14bb325SJeff Bonwick 5621e14bb325SJeff Bonwick if (error == 0) 5622e14bb325SJeff Bonwick break; 5623e14bb325SJeff Bonwick zio_suspend(spa, NULL); 5624e14bb325SJeff Bonwick zio_resume_wait(spa); 56250373e76bSbonwick } 562699653d4eSeschrock dmu_tx_commit(tx); 562799653d4eSeschrock 56280373e76bSbonwick /* 56290373e76bSbonwick * Clear the dirty config list. 5630fa9e4066Sahrens */ 5631e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 56320373e76bSbonwick vdev_config_clean(vd); 56330373e76bSbonwick 56340373e76bSbonwick /* 56350373e76bSbonwick * Now that the new config has synced transactionally, 56360373e76bSbonwick * let it become visible to the config cache. 56370373e76bSbonwick */ 56380373e76bSbonwick if (spa->spa_config_syncing != NULL) { 56390373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 56400373e76bSbonwick spa->spa_config_txg = txg; 56410373e76bSbonwick spa->spa_config_syncing = NULL; 56420373e76bSbonwick } 5643fa9e4066Sahrens 5644fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 5645fa9e4066Sahrens 5646b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 5647fa9e4066Sahrens 5648fa9e4066Sahrens /* 5649fa9e4066Sahrens * Update usable space statistics. 5650fa9e4066Sahrens */ 5651fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 5652fa9e4066Sahrens vdev_sync_done(vd, txg); 5653fa9e4066Sahrens 5654485bbbf5SGeorge Wilson spa_update_dspace(spa); 5655485bbbf5SGeorge Wilson 5656fa9e4066Sahrens /* 5657fa9e4066Sahrens * It had better be the case that we didn't dirty anything 565899653d4eSeschrock * since vdev_config_sync(). 5659fa9e4066Sahrens */ 5660fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 5661fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 5662fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 5663b24ab676SJeff Bonwick 5664b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 5665fa9e4066Sahrens 5666e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 5667ea8dc4b6Seschrock 5668468c413aSTim Haley spa_handle_ignored_writes(spa); 5669468c413aSTim Haley 5670ea8dc4b6Seschrock /* 5671ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 5672ea8dc4b6Seschrock */ 5673ea8dc4b6Seschrock spa_async_dispatch(spa); 5674fa9e4066Sahrens } 5675fa9e4066Sahrens 5676fa9e4066Sahrens /* 5677fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 5678fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 5679fa9e4066Sahrens * sync. 5680fa9e4066Sahrens */ 5681fa9e4066Sahrens void 5682fa9e4066Sahrens spa_sync_allpools(void) 5683fa9e4066Sahrens { 5684fa9e4066Sahrens spa_t *spa = NULL; 5685fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5686fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 5687f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 5688f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 5689fa9e4066Sahrens continue; 5690fa9e4066Sahrens spa_open_ref(spa, FTAG); 5691fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5692fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 5693fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5694fa9e4066Sahrens spa_close(spa, FTAG); 5695fa9e4066Sahrens } 5696fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5697fa9e4066Sahrens } 5698fa9e4066Sahrens 5699fa9e4066Sahrens /* 5700fa9e4066Sahrens * ========================================================================== 5701fa9e4066Sahrens * Miscellaneous routines 5702fa9e4066Sahrens * ========================================================================== 5703fa9e4066Sahrens */ 5704fa9e4066Sahrens 5705fa9e4066Sahrens /* 5706fa9e4066Sahrens * Remove all pools in the system. 5707fa9e4066Sahrens */ 5708fa9e4066Sahrens void 5709fa9e4066Sahrens spa_evict_all(void) 5710fa9e4066Sahrens { 5711fa9e4066Sahrens spa_t *spa; 5712fa9e4066Sahrens 5713fa9e4066Sahrens /* 5714fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 5715fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 5716fa9e4066Sahrens */ 5717fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5718fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 5719fa9e4066Sahrens /* 5720ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 5721ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 5722ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 5723fa9e4066Sahrens */ 5724fa9e4066Sahrens spa_open_ref(spa, FTAG); 5725fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5726ea8dc4b6Seschrock spa_async_suspend(spa); 5727fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5728fa9e4066Sahrens spa_close(spa, FTAG); 5729fa9e4066Sahrens 5730fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 5731fa9e4066Sahrens spa_unload(spa); 5732fa9e4066Sahrens spa_deactivate(spa); 5733fa9e4066Sahrens } 5734fa9e4066Sahrens spa_remove(spa); 5735fa9e4066Sahrens } 5736fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5737fa9e4066Sahrens } 5738ea8dc4b6Seschrock 5739ea8dc4b6Seschrock vdev_t * 57406809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 5741ea8dc4b6Seschrock { 5742c5904d13Seschrock vdev_t *vd; 5743c5904d13Seschrock int i; 5744c5904d13Seschrock 5745c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 5746c5904d13Seschrock return (vd); 5747c5904d13Seschrock 57486809eb4eSEric Schrock if (aux) { 5749c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 5750c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 57516809eb4eSEric Schrock if (vd->vdev_guid == guid) 57526809eb4eSEric Schrock return (vd); 57536809eb4eSEric Schrock } 57546809eb4eSEric Schrock 57556809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 57566809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 5757c5904d13Seschrock if (vd->vdev_guid == guid) 5758c5904d13Seschrock return (vd); 5759c5904d13Seschrock } 5760c5904d13Seschrock } 5761c5904d13Seschrock 5762c5904d13Seschrock return (NULL); 5763ea8dc4b6Seschrock } 5764eaca9bbdSeschrock 5765eaca9bbdSeschrock void 5766990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 5767eaca9bbdSeschrock { 5768f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5769f9af39baSGeorge Wilson 5770e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5771eaca9bbdSeschrock 5772eaca9bbdSeschrock /* 5773eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 5774eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 5775eaca9bbdSeschrock * possible. 5776eaca9bbdSeschrock */ 5777e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 5778990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 5779eaca9bbdSeschrock 5780990b4856Slling spa->spa_uberblock.ub_version = version; 5781eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 5782eaca9bbdSeschrock 5783e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 578499653d4eSeschrock 578599653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 578699653d4eSeschrock } 578799653d4eSeschrock 578899653d4eSeschrock boolean_t 578999653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 579099653d4eSeschrock { 579199653d4eSeschrock int i; 579239c23413Seschrock uint64_t spareguid; 5793fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 579499653d4eSeschrock 5795fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5796fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 579799653d4eSeschrock return (B_TRUE); 579899653d4eSeschrock 5799fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 5800fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 5801fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 580239c23413Seschrock return (B_TRUE); 580339c23413Seschrock } 580439c23413Seschrock 580599653d4eSeschrock return (B_FALSE); 5806eaca9bbdSeschrock } 5807b1b8ab34Slling 580889a89ebfSlling /* 580989a89ebfSlling * Check if a pool has an active shared spare device. 581089a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 581189a89ebfSlling */ 581289a89ebfSlling static boolean_t 581389a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 581489a89ebfSlling { 581589a89ebfSlling int i, refcnt; 581689a89ebfSlling uint64_t pool; 581789a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 581889a89ebfSlling 581989a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 582089a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 582189a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 582289a89ebfSlling refcnt > 2) 582389a89ebfSlling return (B_TRUE); 582489a89ebfSlling } 582589a89ebfSlling 582689a89ebfSlling return (B_FALSE); 582789a89ebfSlling } 582889a89ebfSlling 58293d7072f8Seschrock /* 58303d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 58313d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 58323d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 58333d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 58343d7072f8Seschrock * or zdb as real changes. 58353d7072f8Seschrock */ 58363d7072f8Seschrock void 58373d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 58383d7072f8Seschrock { 58393d7072f8Seschrock #ifdef _KERNEL 58403d7072f8Seschrock sysevent_t *ev; 58413d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 58423d7072f8Seschrock sysevent_value_t value; 58433d7072f8Seschrock sysevent_id_t eid; 58443d7072f8Seschrock 58453d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 58463d7072f8Seschrock SE_SLEEP); 58473d7072f8Seschrock 58483d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 58493d7072f8Seschrock value.value.sv_string = spa_name(spa); 58503d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 58513d7072f8Seschrock goto done; 58523d7072f8Seschrock 58533d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 58543d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 58553d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 58563d7072f8Seschrock goto done; 58573d7072f8Seschrock 58583d7072f8Seschrock if (vd) { 58593d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 58603d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 58613d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 58623d7072f8Seschrock SE_SLEEP) != 0) 58633d7072f8Seschrock goto done; 58643d7072f8Seschrock 58653d7072f8Seschrock if (vd->vdev_path) { 58663d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 58673d7072f8Seschrock value.value.sv_string = vd->vdev_path; 58683d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 58693d7072f8Seschrock &value, SE_SLEEP) != 0) 58703d7072f8Seschrock goto done; 58713d7072f8Seschrock } 58723d7072f8Seschrock } 58733d7072f8Seschrock 5874b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 5875b01c3b58Seschrock goto done; 5876b01c3b58Seschrock attr = NULL; 5877b01c3b58Seschrock 58783d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 58793d7072f8Seschrock 58803d7072f8Seschrock done: 58813d7072f8Seschrock if (attr) 58823d7072f8Seschrock sysevent_free_attr(attr); 58833d7072f8Seschrock sysevent_free(ev); 58843d7072f8Seschrock #endif 58853d7072f8Seschrock } 5886