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. 241b497ab8SAdam H. Leventhal * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25b8289d24SDaniil Lunev * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved. 265aeb9474SGarrett D'Amore */ 27fa9e4066Sahrens 28fa9e4066Sahrens /* 293e30c24aSWill Andrews * SPA: Storage Pool Allocator 303e30c24aSWill Andrews * 31fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 32fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 33fa9e4066Sahrens * pool. 34fa9e4066Sahrens */ 35fa9e4066Sahrens 36fa9e4066Sahrens #include <sys/zfs_context.h> 37ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 38fa9e4066Sahrens #include <sys/spa_impl.h> 39fa9e4066Sahrens #include <sys/zio.h> 40fa9e4066Sahrens #include <sys/zio_checksum.h> 41fa9e4066Sahrens #include <sys/dmu.h> 42fa9e4066Sahrens #include <sys/dmu_tx.h> 43fa9e4066Sahrens #include <sys/zap.h> 44fa9e4066Sahrens #include <sys/zil.h> 45b24ab676SJeff Bonwick #include <sys/ddt.h> 46fa9e4066Sahrens #include <sys/vdev_impl.h> 47fa9e4066Sahrens #include <sys/metaslab.h> 4888ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 49fa9e4066Sahrens #include <sys/uberblock_impl.h> 50fa9e4066Sahrens #include <sys/txg.h> 51fa9e4066Sahrens #include <sys/avl.h> 52fa9e4066Sahrens #include <sys/dmu_traverse.h> 53b1b8ab34Slling #include <sys/dmu_objset.h> 54fa9e4066Sahrens #include <sys/unique.h> 55fa9e4066Sahrens #include <sys/dsl_pool.h> 56b1b8ab34Slling #include <sys/dsl_dataset.h> 57fa9e4066Sahrens #include <sys/dsl_dir.h> 58fa9e4066Sahrens #include <sys/dsl_prop.h> 59b1b8ab34Slling #include <sys/dsl_synctask.h> 60fa9e4066Sahrens #include <sys/fs/zfs.h> 61fa94a07fSbrendan #include <sys/arc.h> 62fa9e4066Sahrens #include <sys/callb.h> 6395173954Sek #include <sys/systeminfo.h> 64e7cbe64fSgw #include <sys/spa_boot.h> 65573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 663f9d6ad7SLin Ling #include <sys/dsl_scan.h> 67ad135b5dSChristopher Siden #include <sys/zfeature.h> 683b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 69fa9e4066Sahrens 705679c89fSjv #ifdef _KERNEL 71dedec472SJack Meng #include <sys/bootprops.h> 7235a5a358SJonathan Adams #include <sys/callb.h> 7335a5a358SJonathan Adams #include <sys/cpupart.h> 7435a5a358SJonathan Adams #include <sys/pool.h> 7535a5a358SJonathan Adams #include <sys/sysdc.h> 7635a5a358SJonathan Adams #include <sys/zone.h> 775679c89fSjv #endif /* _KERNEL */ 785679c89fSjv 79990b4856Slling #include "zfs_prop.h" 80b7b97454Sperrin #include "zfs_comutil.h" 81990b4856Slling 823cb69f73SWill Andrews /* 833cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 843cb69f73SWill Andrews * should be retried. 853cb69f73SWill Andrews */ 863cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300; 873cb69f73SWill Andrews 8835a5a358SJonathan Adams typedef enum zti_modes { 89ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 90ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 91ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 92ec94d322SAdam Leventhal ZTI_NMODES 9335a5a358SJonathan Adams } zti_modes_t; 94416e0cd8Sek 95ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 96ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 97ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 982e0c549eSJonathan Adams 99ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 100ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1012e0c549eSJonathan Adams 1022e0c549eSJonathan Adams typedef struct zio_taskq_info { 103ec94d322SAdam Leventhal zti_modes_t zti_mode; 10480eb36f2SGeorge Wilson uint_t zti_value; 105ec94d322SAdam Leventhal uint_t zti_count; 1062e0c549eSJonathan Adams } zio_taskq_info_t; 1072e0c549eSJonathan Adams 1082e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 10935a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1102e0c549eSJonathan Adams }; 1112e0c549eSJonathan Adams 11280eb36f2SGeorge Wilson /* 113ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 114ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 115ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 116ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 117ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 118ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 119ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 120ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 121ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 122ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 123ec94d322SAdam Leventhal * particular taskq is chosen at random. 124ec94d322SAdam Leventhal * 125ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 126ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 127ec94d322SAdam Leventhal * need to be handled with minimum delay. 12880eb36f2SGeorge Wilson */ 12980eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 13080eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 131ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1321b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 133ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 134ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 135ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 136ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1372e0c549eSJonathan Adams }; 1382e0c549eSJonathan Adams 1393b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1403b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 14189a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1421195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1431195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1441195e687SMark J Musante char **ereport); 145cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 146990b4856Slling 14769962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 14835a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 14935a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 15035a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 15135a5a358SJonathan Adams 15235a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 15301f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 15435a5a358SJonathan Adams 15535a5a358SJonathan Adams /* 15635a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 15735a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 15835a5a358SJonathan Adams */ 15935a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 16035a5a358SJonathan Adams 161990b4856Slling /* 162990b4856Slling * ========================================================================== 163990b4856Slling * SPA properties routines 164990b4856Slling * ========================================================================== 165990b4856Slling */ 166990b4856Slling 167990b4856Slling /* 168990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 169990b4856Slling */ 1709d82f4f6Slling static void 171990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 172990b4856Slling uint64_t intval, zprop_source_t src) 173990b4856Slling { 174990b4856Slling const char *propname = zpool_prop_to_name(prop); 175990b4856Slling nvlist_t *propval; 176990b4856Slling 1779d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1789d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 179990b4856Slling 1809d82f4f6Slling if (strval != NULL) 1819d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1829d82f4f6Slling else 1839d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 184990b4856Slling 1859d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 186990b4856Slling nvlist_free(propval); 187990b4856Slling } 188990b4856Slling 189990b4856Slling /* 190990b4856Slling * Get property values from the spa configuration. 191990b4856Slling */ 1929d82f4f6Slling static void 193990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 194990b4856Slling { 1954263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 196ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 1972e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 198990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 199c5904d13Seschrock spa_config_dirent_t *dp; 2002e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 201990b4856Slling 202e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 203e14bb325SJeff Bonwick 2044263d13fSGeorge Wilson if (rvd != NULL) { 205485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 206b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 207379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 208379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 209485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 210485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 211485bbbf5SGeorge Wilson size - alloc, src); 2124263d13fSGeorge Wilson 2132e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2142e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2152e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2162e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 217f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 218f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 219379c004dSEric Schrock 220485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 221379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 222379c004dSEric Schrock 223b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 224b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 225b24ab676SJeff Bonwick 226379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2274263d13fSGeorge Wilson rvd->vdev_state, src); 228379c004dSEric Schrock 229379c004dSEric Schrock version = spa_version(spa); 230379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 231379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 232379c004dSEric Schrock else 233379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 234379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 235379c004dSEric Schrock } 236990b4856Slling 237ad135b5dSChristopher Siden if (pool != NULL) { 238ad135b5dSChristopher Siden /* 239ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 240ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 241ad135b5dSChristopher Siden */ 2427fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 243ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 244*c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 245*c1379625SJustin T. Gibbs src); 246ad135b5dSChristopher Siden } else { 247ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 248ad135b5dSChristopher Siden NULL, 0, src); 249ad135b5dSChristopher Siden } 2507fd05ac4SMatthew Ahrens 2517fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 2527fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 253*c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 254*c1379625SJustin T. Gibbs src); 2557fd05ac4SMatthew Ahrens } else { 2567fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 2577fd05ac4SMatthew Ahrens NULL, 0, src); 2587fd05ac4SMatthew Ahrens } 259ad135b5dSChristopher Siden } 260ad135b5dSChristopher Siden 2619d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 262990b4856Slling 2638704186eSDan McDonald if (spa->spa_comment != NULL) { 2648704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2658704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2668704186eSDan McDonald } 2678704186eSDan McDonald 2689d82f4f6Slling if (spa->spa_root != NULL) 2699d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2709d82f4f6Slling 0, ZPROP_SRC_LOCAL); 271990b4856Slling 272b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 273b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 274b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 275b5152584SMatthew Ahrens } else { 276b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 277b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 278b5152584SMatthew Ahrens } 279b5152584SMatthew Ahrens 280c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 281c5904d13Seschrock if (dp->scd_path == NULL) { 2829d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 283c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 284c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2859d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 286c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2872f8aaab3Seschrock } 2882f8aaab3Seschrock } 289990b4856Slling } 290990b4856Slling 291990b4856Slling /* 292990b4856Slling * Get zpool property values. 293990b4856Slling */ 294990b4856Slling int 295990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 296990b4856Slling { 297b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 298990b4856Slling zap_cursor_t zc; 299990b4856Slling zap_attribute_t za; 300990b4856Slling int err; 301990b4856Slling 3029d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 303990b4856Slling 304e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 305e14bb325SJeff Bonwick 306990b4856Slling /* 307990b4856Slling * Get properties from the spa config. 308990b4856Slling */ 3099d82f4f6Slling spa_prop_get_config(spa, nvp); 310990b4856Slling 311990b4856Slling /* If no pool property object, no more prop to get. */ 312afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 313990b4856Slling mutex_exit(&spa->spa_props_lock); 314990b4856Slling return (0); 315990b4856Slling } 316990b4856Slling 317990b4856Slling /* 318990b4856Slling * Get properties from the MOS pool property object. 319990b4856Slling */ 320990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 321990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 322990b4856Slling zap_cursor_advance(&zc)) { 323990b4856Slling uint64_t intval = 0; 324990b4856Slling char *strval = NULL; 325990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 326990b4856Slling zpool_prop_t prop; 327990b4856Slling 328990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 329990b4856Slling continue; 330990b4856Slling 331990b4856Slling switch (za.za_integer_length) { 332990b4856Slling case 8: 333990b4856Slling /* integer property */ 334990b4856Slling if (za.za_first_integer != 335990b4856Slling zpool_prop_default_numeric(prop)) 336990b4856Slling src = ZPROP_SRC_LOCAL; 337990b4856Slling 338990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 339990b4856Slling dsl_pool_t *dp; 340990b4856Slling dsl_dataset_t *ds = NULL; 341990b4856Slling 342990b4856Slling dp = spa_get_dsl(spa); 3433b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 344745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 345745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 3463b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 347990b4856Slling break; 348990b4856Slling } 349990b4856Slling 350990b4856Slling strval = kmem_alloc( 351990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 352990b4856Slling KM_SLEEP); 353990b4856Slling dsl_dataset_name(ds, strval); 354745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 3553b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 356990b4856Slling } else { 357990b4856Slling strval = NULL; 358990b4856Slling intval = za.za_first_integer; 359990b4856Slling } 360990b4856Slling 3619d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 362990b4856Slling 363990b4856Slling if (strval != NULL) 364990b4856Slling kmem_free(strval, 365990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 366990b4856Slling 367990b4856Slling break; 368990b4856Slling 369990b4856Slling case 1: 370990b4856Slling /* string property */ 371990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 372990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 373990b4856Slling za.za_name, 1, za.za_num_integers, strval); 374990b4856Slling if (err) { 375990b4856Slling kmem_free(strval, za.za_num_integers); 376990b4856Slling break; 377990b4856Slling } 3789d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 379990b4856Slling kmem_free(strval, za.za_num_integers); 380990b4856Slling break; 381990b4856Slling 382990b4856Slling default: 383990b4856Slling break; 384990b4856Slling } 385990b4856Slling } 386990b4856Slling zap_cursor_fini(&zc); 387990b4856Slling mutex_exit(&spa->spa_props_lock); 388990b4856Slling out: 389990b4856Slling if (err && err != ENOENT) { 390990b4856Slling nvlist_free(*nvp); 3919d82f4f6Slling *nvp = NULL; 392990b4856Slling return (err); 393990b4856Slling } 394990b4856Slling 395990b4856Slling return (0); 396990b4856Slling } 397990b4856Slling 398990b4856Slling /* 399990b4856Slling * Validate the given pool properties nvlist and modify the list 400990b4856Slling * for the property values to be set. 401990b4856Slling */ 402990b4856Slling static int 403990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 404990b4856Slling { 405990b4856Slling nvpair_t *elem; 406990b4856Slling int error = 0, reset_bootfs = 0; 407d5285caeSGeorge Wilson uint64_t objnum = 0; 408ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 409990b4856Slling 410990b4856Slling elem = NULL; 411990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 412990b4856Slling uint64_t intval; 413ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 414ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 415ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 416ad135b5dSChristopher Siden 417ad135b5dSChristopher Siden switch (prop) { 418ad135b5dSChristopher Siden case ZPROP_INVAL: 419ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 420be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 421ad135b5dSChristopher Siden break; 422ad135b5dSChristopher Siden } 423990b4856Slling 424ad135b5dSChristopher Siden /* 425ad135b5dSChristopher Siden * Sanitize the input. 426ad135b5dSChristopher Siden */ 427ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 428be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 429ad135b5dSChristopher Siden break; 430ad135b5dSChristopher Siden } 431990b4856Slling 432ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 433be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 434ad135b5dSChristopher Siden break; 435ad135b5dSChristopher Siden } 436ad135b5dSChristopher Siden 437ad135b5dSChristopher Siden if (intval != 0) { 438be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 439ad135b5dSChristopher Siden break; 440ad135b5dSChristopher Siden } 441ad135b5dSChristopher Siden 442ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 443ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 444be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 445ad135b5dSChristopher Siden break; 446ad135b5dSChristopher Siden } 447ad135b5dSChristopher Siden 448ad135b5dSChristopher Siden has_feature = B_TRUE; 449ad135b5dSChristopher Siden break; 450990b4856Slling 451990b4856Slling case ZPOOL_PROP_VERSION: 452990b4856Slling error = nvpair_value_uint64(elem, &intval); 453990b4856Slling if (!error && 454ad135b5dSChristopher Siden (intval < spa_version(spa) || 455ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 456ad135b5dSChristopher Siden has_feature)) 457be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 458990b4856Slling break; 459990b4856Slling 460990b4856Slling case ZPOOL_PROP_DELEGATION: 461990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 462d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 463573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 464990b4856Slling error = nvpair_value_uint64(elem, &intval); 465990b4856Slling if (!error && intval > 1) 466be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 467990b4856Slling break; 468990b4856Slling 469990b4856Slling case ZPOOL_PROP_BOOTFS: 47025f89ee2SJeff Bonwick /* 47125f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 47225f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 47325f89ee2SJeff Bonwick * the bootfs property cannot be set. 47425f89ee2SJeff Bonwick */ 475990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 476be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 477990b4856Slling break; 478990b4856Slling } 479990b4856Slling 480990b4856Slling /* 48115e6edf1Sgw * Make sure the vdev config is bootable 482990b4856Slling */ 48315e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 484be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 485990b4856Slling break; 486990b4856Slling } 487990b4856Slling 488990b4856Slling reset_bootfs = 1; 489990b4856Slling 490990b4856Slling error = nvpair_value_string(elem, &strval); 491990b4856Slling 492990b4856Slling if (!error) { 493ad135b5dSChristopher Siden objset_t *os; 494b5152584SMatthew Ahrens uint64_t propval; 49515e6edf1Sgw 496990b4856Slling if (strval == NULL || strval[0] == '\0') { 497990b4856Slling objnum = zpool_prop_default_numeric( 498990b4856Slling ZPOOL_PROP_BOOTFS); 499990b4856Slling break; 500990b4856Slling } 501990b4856Slling 502503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 503990b4856Slling break; 50415e6edf1Sgw 505b5152584SMatthew Ahrens /* 506b5152584SMatthew Ahrens * Must be ZPL, and its property settings 507b5152584SMatthew Ahrens * must be supported by GRUB (compression 508b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 509b5152584SMatthew Ahrens */ 510503ad85cSMatthew Ahrens 511503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 512be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5133b2aab18SMatthew Ahrens } else if ((error = 5143b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 51515e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 516b5152584SMatthew Ahrens &propval)) == 0 && 517b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 518b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 519b5152584SMatthew Ahrens } else if ((error = 520b5152584SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 521b5152584SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE), 522b5152584SMatthew Ahrens &propval)) == 0 && 523b5152584SMatthew Ahrens propval > SPA_OLD_MAXBLOCKSIZE) { 524be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 52515e6edf1Sgw } else { 52615e6edf1Sgw objnum = dmu_objset_id(os); 52715e6edf1Sgw } 528503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 529990b4856Slling } 530990b4856Slling break; 531e14bb325SJeff Bonwick 5320a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5330a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5340a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5350a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 536be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5370a4e9518Sgw 5380a4e9518Sgw /* 5390a4e9518Sgw * This is a special case which only occurs when 5400a4e9518Sgw * the pool has completely failed. This allows 5410a4e9518Sgw * the user to change the in-core failmode property 5420a4e9518Sgw * without syncing it out to disk (I/Os might 5430a4e9518Sgw * currently be blocked). We do this by returning 5440a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5450a4e9518Sgw * into thinking we encountered a property validation 5460a4e9518Sgw * error. 5470a4e9518Sgw */ 548e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5490a4e9518Sgw spa->spa_failmode = intval; 550be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 5510a4e9518Sgw } 5520a4e9518Sgw break; 5532f8aaab3Seschrock 5542f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5552f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5562f8aaab3Seschrock break; 5572f8aaab3Seschrock 5582f8aaab3Seschrock if (strval[0] == '\0') 5592f8aaab3Seschrock break; 5602f8aaab3Seschrock 5612f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5622f8aaab3Seschrock break; 5632f8aaab3Seschrock 5642f8aaab3Seschrock if (strval[0] != '/') { 565be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5662f8aaab3Seschrock break; 5672f8aaab3Seschrock } 5682f8aaab3Seschrock 5692f8aaab3Seschrock slash = strrchr(strval, '/'); 5702f8aaab3Seschrock ASSERT(slash != NULL); 5712f8aaab3Seschrock 5722f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5732f8aaab3Seschrock strcmp(slash, "/..") == 0) 574be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5752f8aaab3Seschrock break; 576b24ab676SJeff Bonwick 5778704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5788704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5798704186eSDan McDonald break; 5808704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5818704186eSDan McDonald /* 5828704186eSDan McDonald * The kernel doesn't have an easy isprint() 5838704186eSDan McDonald * check. For this kernel check, we merely 5848704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5858704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5868704186eSDan McDonald */ 5878704186eSDan McDonald if (*check >= 0x7f) { 588be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5898704186eSDan McDonald break; 5908704186eSDan McDonald } 5918704186eSDan McDonald check++; 5928704186eSDan McDonald } 5938704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5948704186eSDan McDonald error = E2BIG; 5958704186eSDan McDonald break; 5968704186eSDan McDonald 597b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 598b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 599be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 600b24ab676SJeff Bonwick else 601b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 602b24ab676SJeff Bonwick if (error == 0 && 603b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 604be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 605b24ab676SJeff Bonwick break; 606990b4856Slling } 607990b4856Slling 608990b4856Slling if (error) 609990b4856Slling break; 610990b4856Slling } 611990b4856Slling 612990b4856Slling if (!error && reset_bootfs) { 613990b4856Slling error = nvlist_remove(props, 614990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 615990b4856Slling 616990b4856Slling if (!error) { 617990b4856Slling error = nvlist_add_uint64(props, 618990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 619990b4856Slling } 620990b4856Slling } 621990b4856Slling 622990b4856Slling return (error); 623990b4856Slling } 624990b4856Slling 625379c004dSEric Schrock void 626379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 627379c004dSEric Schrock { 628379c004dSEric Schrock char *cachefile; 629379c004dSEric Schrock spa_config_dirent_t *dp; 630379c004dSEric Schrock 631379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 632379c004dSEric Schrock &cachefile) != 0) 633379c004dSEric Schrock return; 634379c004dSEric Schrock 635379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 636379c004dSEric Schrock KM_SLEEP); 637379c004dSEric Schrock 638379c004dSEric Schrock if (cachefile[0] == '\0') 639379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 640379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 641379c004dSEric Schrock dp->scd_path = NULL; 642379c004dSEric Schrock else 643379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 644379c004dSEric Schrock 645379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 646379c004dSEric Schrock if (need_sync) 647379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 648379c004dSEric Schrock } 649379c004dSEric Schrock 650990b4856Slling int 651990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 652990b4856Slling { 653990b4856Slling int error; 654ad135b5dSChristopher Siden nvpair_t *elem = NULL; 655379c004dSEric Schrock boolean_t need_sync = B_FALSE; 656990b4856Slling 657990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 658990b4856Slling return (error); 659990b4856Slling 660379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 661ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 662379c004dSEric Schrock 663f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 664f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 665f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 666379c004dSEric Schrock continue; 667379c004dSEric Schrock 668ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 669ad135b5dSChristopher Siden uint64_t ver; 670ad135b5dSChristopher Siden 671ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 672ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 673ad135b5dSChristopher Siden } else { 674ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 675ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 676ad135b5dSChristopher Siden need_sync = B_TRUE; 677ad135b5dSChristopher Siden } 678ad135b5dSChristopher Siden 679ad135b5dSChristopher Siden /* Save time if the version is already set. */ 680ad135b5dSChristopher Siden if (ver == spa_version(spa)) 681ad135b5dSChristopher Siden continue; 682ad135b5dSChristopher Siden 683ad135b5dSChristopher Siden /* 684ad135b5dSChristopher Siden * In addition to the pool directory object, we might 685ad135b5dSChristopher Siden * create the pool properties object, the features for 686ad135b5dSChristopher Siden * read object, the features for write object, or the 687ad135b5dSChristopher Siden * feature descriptions object. 688ad135b5dSChristopher Siden */ 6893b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 6907d46dc6cSMatthew Ahrens spa_sync_version, &ver, 6917d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 692ad135b5dSChristopher Siden if (error) 693ad135b5dSChristopher Siden return (error); 694ad135b5dSChristopher Siden continue; 695ad135b5dSChristopher Siden } 696ad135b5dSChristopher Siden 697379c004dSEric Schrock need_sync = B_TRUE; 698379c004dSEric Schrock break; 699379c004dSEric Schrock } 700379c004dSEric Schrock 701ad135b5dSChristopher Siden if (need_sync) { 7023b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7037d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 704ad135b5dSChristopher Siden } 705ad135b5dSChristopher Siden 706ad135b5dSChristopher Siden return (0); 707990b4856Slling } 708990b4856Slling 709990b4856Slling /* 710990b4856Slling * If the bootfs property value is dsobj, clear it. 711990b4856Slling */ 712990b4856Slling void 713990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 714990b4856Slling { 715990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 716990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 717990b4856Slling spa->spa_pool_props_object, 718990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 719990b4856Slling spa->spa_bootfs = 0; 720990b4856Slling } 721990b4856Slling } 722990b4856Slling 723dfbb9432SGeorge Wilson /*ARGSUSED*/ 724dfbb9432SGeorge Wilson static int 7253b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 726dfbb9432SGeorge Wilson { 7273b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7283b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 729dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 730dfbb9432SGeorge Wilson uint64_t vdev_state; 731dfbb9432SGeorge Wilson 732dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 733dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 734dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 735dfbb9432SGeorge Wilson 736dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 737be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 738dfbb9432SGeorge Wilson 739dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 740dfbb9432SGeorge Wilson 741dfbb9432SGeorge Wilson return (0); 742dfbb9432SGeorge Wilson } 743dfbb9432SGeorge Wilson 744dfbb9432SGeorge Wilson static void 7453b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 746dfbb9432SGeorge Wilson { 7473b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7483b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 749dfbb9432SGeorge Wilson uint64_t oldguid; 750dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 751dfbb9432SGeorge Wilson 752dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 753dfbb9432SGeorge Wilson 754dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 755dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 756dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 757dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 758dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 759dfbb9432SGeorge Wilson 76020128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 761dfbb9432SGeorge Wilson oldguid, *newguid); 762dfbb9432SGeorge Wilson } 763dfbb9432SGeorge Wilson 764e9103aaeSGarrett D'Amore /* 765e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 766e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 767e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 768e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 769e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 770e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 771e9103aaeSGarrett D'Amore * sysevent to update any watchers. 772e9103aaeSGarrett D'Amore */ 773e9103aaeSGarrett D'Amore int 774e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 775e9103aaeSGarrett D'Amore { 776dfbb9432SGeorge Wilson int error; 777dfbb9432SGeorge Wilson uint64_t guid; 778e9103aaeSGarrett D'Amore 7792c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 780dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 781dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 782e9103aaeSGarrett D'Amore 7833b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 7847d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 785e9103aaeSGarrett D'Amore 786dfbb9432SGeorge Wilson if (error == 0) { 787dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 788dfbb9432SGeorge Wilson spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID); 789dfbb9432SGeorge Wilson } 790e9103aaeSGarrett D'Amore 791dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 7922c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 793e9103aaeSGarrett D'Amore 794dfbb9432SGeorge Wilson return (error); 795e9103aaeSGarrett D'Amore } 796e9103aaeSGarrett D'Amore 797fa9e4066Sahrens /* 798fa9e4066Sahrens * ========================================================================== 799fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 800fa9e4066Sahrens * ========================================================================== 801fa9e4066Sahrens */ 802fa9e4066Sahrens 803ea8dc4b6Seschrock static int 804ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 805ea8dc4b6Seschrock { 806ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 807ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 808ea8dc4b6Seschrock int ret; 809ea8dc4b6Seschrock 810ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8117802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 812ea8dc4b6Seschrock 813ea8dc4b6Seschrock if (ret < 0) 814ea8dc4b6Seschrock return (-1); 815ea8dc4b6Seschrock else if (ret > 0) 816ea8dc4b6Seschrock return (1); 817ea8dc4b6Seschrock else 818ea8dc4b6Seschrock return (0); 819ea8dc4b6Seschrock } 820ea8dc4b6Seschrock 821ea8dc4b6Seschrock /* 822ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 823ea8dc4b6Seschrock * re-initializes them in the process. 824ea8dc4b6Seschrock */ 825ea8dc4b6Seschrock void 826ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 827ea8dc4b6Seschrock { 828ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 829ea8dc4b6Seschrock 830ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 831ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 832ea8dc4b6Seschrock 833ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 834ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 835ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 836ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 837ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 838ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 839ea8dc4b6Seschrock } 840ea8dc4b6Seschrock 841ec94d322SAdam Leventhal static void 842ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 843fa9e4066Sahrens { 844ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 845ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 846ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 847ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 848ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 849ec94d322SAdam Leventhal char name[32]; 8505aeb9474SGarrett D'Amore uint_t flags = 0; 85135a5a358SJonathan Adams boolean_t batch = B_FALSE; 852fa9e4066Sahrens 853ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 854ec94d322SAdam Leventhal tqs->stqs_count = 0; 855ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 856ec94d322SAdam Leventhal return; 857ec94d322SAdam Leventhal } 858fa9e4066Sahrens 859ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 860fa9e4066Sahrens 861ec94d322SAdam Leventhal tqs->stqs_count = count; 862ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 86335a5a358SJonathan Adams 86469962b56SMatthew Ahrens switch (mode) { 86569962b56SMatthew Ahrens case ZTI_MODE_FIXED: 86669962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 86769962b56SMatthew Ahrens value = MAX(value, 1); 86869962b56SMatthew Ahrens break; 869ec94d322SAdam Leventhal 87069962b56SMatthew Ahrens case ZTI_MODE_BATCH: 87169962b56SMatthew Ahrens batch = B_TRUE; 87269962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 87369962b56SMatthew Ahrens value = zio_taskq_batch_pct; 87469962b56SMatthew Ahrens break; 875ec94d322SAdam Leventhal 87669962b56SMatthew Ahrens default: 87769962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 87869962b56SMatthew Ahrens "spa_activate()", 87969962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 88069962b56SMatthew Ahrens break; 88169962b56SMatthew Ahrens } 882ec94d322SAdam Leventhal 88369962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 88469962b56SMatthew Ahrens taskq_t *tq; 885ec94d322SAdam Leventhal 886ec94d322SAdam Leventhal if (count > 1) { 887ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 888ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 889ec94d322SAdam Leventhal } else { 890ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 891ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 892ec94d322SAdam Leventhal } 893ec94d322SAdam Leventhal 894ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 895ec94d322SAdam Leventhal if (batch) 896ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 897ec94d322SAdam Leventhal 898ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 899ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 900ec94d322SAdam Leventhal } else { 90169962b56SMatthew Ahrens pri_t pri = maxclsyspri; 90269962b56SMatthew Ahrens /* 90369962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 90469962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 90569962b56SMatthew Ahrens * than the other taskqs. 90669962b56SMatthew Ahrens */ 90769962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 90869962b56SMatthew Ahrens pri--; 90969962b56SMatthew Ahrens 91069962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 911ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 912ec94d322SAdam Leventhal } 913ec94d322SAdam Leventhal 914ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 915ec94d322SAdam Leventhal } 916ec94d322SAdam Leventhal } 917ec94d322SAdam Leventhal 918ec94d322SAdam Leventhal static void 919ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 920ec94d322SAdam Leventhal { 921ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 922ec94d322SAdam Leventhal 923ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 924ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 925ec94d322SAdam Leventhal return; 926ec94d322SAdam Leventhal } 927ec94d322SAdam Leventhal 928ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 929ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 930ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 93135a5a358SJonathan Adams } 93235a5a358SJonathan Adams 933ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 934ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 935ec94d322SAdam Leventhal } 93635a5a358SJonathan Adams 937ec94d322SAdam Leventhal /* 938ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 939ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 940ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 941ec94d322SAdam Leventhal * the low bits of gethrtime(). 942ec94d322SAdam Leventhal */ 943ec94d322SAdam Leventhal void 944ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 945ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 946ec94d322SAdam Leventhal { 947ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 948ec94d322SAdam Leventhal taskq_t *tq; 949ec94d322SAdam Leventhal 950ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 951ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 952ec94d322SAdam Leventhal 953ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 954ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 955ec94d322SAdam Leventhal } else { 956ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 95735a5a358SJonathan Adams } 958ec94d322SAdam Leventhal 959ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 96035a5a358SJonathan Adams } 96135a5a358SJonathan Adams 96235a5a358SJonathan Adams static void 96335a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 96435a5a358SJonathan Adams { 965e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 966e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 967ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 96835a5a358SJonathan Adams } 96935a5a358SJonathan Adams } 97035a5a358SJonathan Adams } 97135a5a358SJonathan Adams 97235a5a358SJonathan Adams #ifdef _KERNEL 97335a5a358SJonathan Adams static void 97435a5a358SJonathan Adams spa_thread(void *arg) 97535a5a358SJonathan Adams { 97635a5a358SJonathan Adams callb_cpr_t cprinfo; 9772e0c549eSJonathan Adams 97835a5a358SJonathan Adams spa_t *spa = arg; 97935a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9802e0c549eSJonathan Adams 98135a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 98235a5a358SJonathan Adams spa->spa_name); 9832e0c549eSJonathan Adams 98435a5a358SJonathan Adams ASSERT(curproc != &p0); 98535a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 98635a5a358SJonathan Adams "zpool-%s", spa->spa_name); 98735a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9882e0c549eSJonathan Adams 98935a5a358SJonathan Adams /* bind this thread to the requested psrset */ 99035a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 99135a5a358SJonathan Adams pool_lock(); 99235a5a358SJonathan Adams mutex_enter(&cpu_lock); 99335a5a358SJonathan Adams mutex_enter(&pidlock); 99435a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 99580eb36f2SGeorge Wilson 99635a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 99735a5a358SJonathan Adams 0, NULL, NULL) == 0) { 99835a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 99935a5a358SJonathan Adams } else { 100035a5a358SJonathan Adams cmn_err(CE_WARN, 100135a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 100235a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 100335a5a358SJonathan Adams } 100435a5a358SJonathan Adams 100535a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 100635a5a358SJonathan Adams mutex_exit(&pidlock); 100735a5a358SJonathan Adams mutex_exit(&cpu_lock); 100835a5a358SJonathan Adams pool_unlock(); 100935a5a358SJonathan Adams } 101035a5a358SJonathan Adams 101135a5a358SJonathan Adams if (zio_taskq_sysdc) { 101235a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 101335a5a358SJonathan Adams } 101435a5a358SJonathan Adams 101535a5a358SJonathan Adams spa->spa_proc = curproc; 101635a5a358SJonathan Adams spa->spa_did = curthread->t_did; 101735a5a358SJonathan Adams 101835a5a358SJonathan Adams spa_create_zio_taskqs(spa); 101935a5a358SJonathan Adams 102035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 102135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 102235a5a358SJonathan Adams 102335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 102435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 102535a5a358SJonathan Adams 102635a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 102735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 102835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 102935a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 103035a5a358SJonathan Adams 103135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 103235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 103335a5a358SJonathan Adams spa->spa_proc = &p0; 103435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 103535a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 103635a5a358SJonathan Adams 103735a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 103835a5a358SJonathan Adams lwp_exit(); 103935a5a358SJonathan Adams } 104035a5a358SJonathan Adams #endif 104135a5a358SJonathan Adams 104235a5a358SJonathan Adams /* 104335a5a358SJonathan Adams * Activate an uninitialized pool. 104435a5a358SJonathan Adams */ 104535a5a358SJonathan Adams static void 104635a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 104735a5a358SJonathan Adams { 104835a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 104935a5a358SJonathan Adams 105035a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 105135a5a358SJonathan Adams spa->spa_mode = mode; 105235a5a358SJonathan Adams 105335a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 105435a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 105535a5a358SJonathan Adams 105635a5a358SJonathan Adams /* Try to create a covering process */ 105735a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 105835a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 105935a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 106035a5a358SJonathan Adams spa->spa_did = 0; 106135a5a358SJonathan Adams 106235a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 106335a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 106435a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 106535a5a358SJonathan Adams NULL, 0) == 0) { 106635a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 106735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 106835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 106935a5a358SJonathan Adams &spa->spa_proc_lock); 10702e0c549eSJonathan Adams } 107135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 107235a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 107335a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 107435a5a358SJonathan Adams } else { 107535a5a358SJonathan Adams #ifdef _KERNEL 107635a5a358SJonathan Adams cmn_err(CE_WARN, 107735a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 107835a5a358SJonathan Adams spa->spa_name); 107935a5a358SJonathan Adams #endif 1080e14bb325SJeff Bonwick } 1081fa9e4066Sahrens } 108235a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 108335a5a358SJonathan Adams 108435a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 108535a5a358SJonathan Adams if (spa->spa_proc == &p0) { 108635a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108735a5a358SJonathan Adams } 1088fa9e4066Sahrens 1089e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1090e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1091e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1092e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1093fa9e4066Sahrens 1094fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 1095fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1096ea8dc4b6Seschrock 1097ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1098ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1099ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1100ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1101ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1102ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1103fa9e4066Sahrens } 1104fa9e4066Sahrens 1105fa9e4066Sahrens /* 1106fa9e4066Sahrens * Opposite of spa_activate(). 1107fa9e4066Sahrens */ 1108fa9e4066Sahrens static void 1109fa9e4066Sahrens spa_deactivate(spa_t *spa) 1110fa9e4066Sahrens { 1111fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1112fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1113fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 111425f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1115fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1116fa9e4066Sahrens 1117fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1118fa9e4066Sahrens 1119e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1120e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1121fa9e4066Sahrens 1122e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1123e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1124ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1125e14bb325SJeff Bonwick } 1126fa9e4066Sahrens } 1127fa9e4066Sahrens 1128fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1129fa9e4066Sahrens spa->spa_normal_class = NULL; 1130fa9e4066Sahrens 11318654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 11328654d025Sperrin spa->spa_log_class = NULL; 11338654d025Sperrin 1134ea8dc4b6Seschrock /* 1135ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1136ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1137ea8dc4b6Seschrock */ 1138ea8dc4b6Seschrock spa_errlog_drain(spa); 1139ea8dc4b6Seschrock 1140ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1141ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1142ea8dc4b6Seschrock 1143fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 114435a5a358SJonathan Adams 114535a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 114635a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 114735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 114835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 114935a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 115035a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 115135a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 115235a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 115335a5a358SJonathan Adams } 115435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 115535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 115635a5a358SJonathan Adams } 115735a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 115835a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 115935a5a358SJonathan Adams 116035a5a358SJonathan Adams /* 116135a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 116235a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 116335a5a358SJonathan Adams * it. 116435a5a358SJonathan Adams */ 116535a5a358SJonathan Adams if (spa->spa_did != 0) { 116635a5a358SJonathan Adams thread_join(spa->spa_did); 116735a5a358SJonathan Adams spa->spa_did = 0; 116835a5a358SJonathan Adams } 1169fa9e4066Sahrens } 1170fa9e4066Sahrens 1171fa9e4066Sahrens /* 1172fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1173fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1174fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1175fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1176fa9e4066Sahrens */ 117799653d4eSeschrock static int 117899653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 117999653d4eSeschrock uint_t id, int atype) 1180fa9e4066Sahrens { 1181fa9e4066Sahrens nvlist_t **child; 1182573ca77eSGeorge Wilson uint_t children; 118399653d4eSeschrock int error; 1184fa9e4066Sahrens 118599653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 118699653d4eSeschrock return (error); 1187fa9e4066Sahrens 118899653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 118999653d4eSeschrock return (0); 1190fa9e4066Sahrens 1191e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1192e14bb325SJeff Bonwick &child, &children); 1193e14bb325SJeff Bonwick 1194e14bb325SJeff Bonwick if (error == ENOENT) 1195e14bb325SJeff Bonwick return (0); 1196e14bb325SJeff Bonwick 1197e14bb325SJeff Bonwick if (error) { 119899653d4eSeschrock vdev_free(*vdp); 119999653d4eSeschrock *vdp = NULL; 1200be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1201fa9e4066Sahrens } 1202fa9e4066Sahrens 1203573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 120499653d4eSeschrock vdev_t *vd; 120599653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 120699653d4eSeschrock atype)) != 0) { 120799653d4eSeschrock vdev_free(*vdp); 120899653d4eSeschrock *vdp = NULL; 120999653d4eSeschrock return (error); 1210fa9e4066Sahrens } 1211fa9e4066Sahrens } 1212fa9e4066Sahrens 121399653d4eSeschrock ASSERT(*vdp != NULL); 121499653d4eSeschrock 121599653d4eSeschrock return (0); 1216fa9e4066Sahrens } 1217fa9e4066Sahrens 1218fa9e4066Sahrens /* 1219fa9e4066Sahrens * Opposite of spa_load(). 1220fa9e4066Sahrens */ 1221fa9e4066Sahrens static void 1222fa9e4066Sahrens spa_unload(spa_t *spa) 1223fa9e4066Sahrens { 122499653d4eSeschrock int i; 122599653d4eSeschrock 1226e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1227e14bb325SJeff Bonwick 1228ea8dc4b6Seschrock /* 1229ea8dc4b6Seschrock * Stop async tasks. 1230ea8dc4b6Seschrock */ 1231ea8dc4b6Seschrock spa_async_suspend(spa); 1232ea8dc4b6Seschrock 1233fa9e4066Sahrens /* 1234fa9e4066Sahrens * Stop syncing. 1235fa9e4066Sahrens */ 1236fa9e4066Sahrens if (spa->spa_sync_on) { 1237fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1238fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1239fa9e4066Sahrens } 1240fa9e4066Sahrens 1241fa9e4066Sahrens /* 1242e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1243fa9e4066Sahrens */ 124454d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 12456f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 12466f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 12476f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 124854d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 124954d692b7SGeorge Wilson } 1250fa9e4066Sahrens 1251cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1252cde58dbcSMatthew Ahrens 12530713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12540713e232SGeorge Wilson 12550713e232SGeorge Wilson /* 12560713e232SGeorge Wilson * Close all vdevs. 12570713e232SGeorge Wilson */ 12580713e232SGeorge Wilson if (spa->spa_root_vdev) 12590713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 12600713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 12610713e232SGeorge Wilson 1262fa9e4066Sahrens /* 1263fa9e4066Sahrens * Close the dsl pool. 1264fa9e4066Sahrens */ 1265fa9e4066Sahrens if (spa->spa_dsl_pool) { 1266fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1267fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1268afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1269fa9e4066Sahrens } 1270fa9e4066Sahrens 1271b24ab676SJeff Bonwick ddt_unload(spa); 1272b24ab676SJeff Bonwick 12738ad4d6ddSJeff Bonwick 12748ad4d6ddSJeff Bonwick /* 12758ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 12768ad4d6ddSJeff Bonwick */ 12778ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 12788ad4d6ddSJeff Bonwick 1279fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1280fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1281fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1282fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1283fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1284fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 128599653d4eSeschrock } 1286fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1287fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1288fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1289fa94a07fSbrendan } 12902ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1291fa94a07fSbrendan 1292cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1293cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1294fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1295cd0837ccSGeorge Wilson } 1296fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1297fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1298fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1299fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1300fa94a07fSbrendan } 1301fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1302fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1303fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 130499653d4eSeschrock } 13052ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 130699653d4eSeschrock 1307ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13088ad4d6ddSJeff Bonwick 13098704186eSDan McDonald if (spa->spa_comment != NULL) { 13108704186eSDan McDonald spa_strfree(spa->spa_comment); 13118704186eSDan McDonald spa->spa_comment = NULL; 13128704186eSDan McDonald } 13138704186eSDan McDonald 13148ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1315fa9e4066Sahrens } 1316fa9e4066Sahrens 131799653d4eSeschrock /* 131899653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 131999653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1320fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1321fa94a07fSbrendan * then re-generate a more complete list including status information. 132299653d4eSeschrock */ 132399653d4eSeschrock static void 132499653d4eSeschrock spa_load_spares(spa_t *spa) 132599653d4eSeschrock { 132699653d4eSeschrock nvlist_t **spares; 132799653d4eSeschrock uint_t nspares; 132899653d4eSeschrock int i; 132939c23413Seschrock vdev_t *vd, *tvd; 133099653d4eSeschrock 1331e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1332e14bb325SJeff Bonwick 133399653d4eSeschrock /* 133499653d4eSeschrock * First, close and free any existing spare vdevs. 133599653d4eSeschrock */ 1336fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1337fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 133839c23413Seschrock 133939c23413Seschrock /* Undo the call to spa_activate() below */ 1340c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1341c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 134239c23413Seschrock spa_spare_remove(tvd); 134339c23413Seschrock vdev_close(vd); 134439c23413Seschrock vdev_free(vd); 134599653d4eSeschrock } 134639c23413Seschrock 1347fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1348fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1349fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 135099653d4eSeschrock 1351fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 135299653d4eSeschrock nspares = 0; 135399653d4eSeschrock else 1354fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 135599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 135699653d4eSeschrock 1357fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1358fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 135999653d4eSeschrock 136099653d4eSeschrock if (nspares == 0) 136199653d4eSeschrock return; 136299653d4eSeschrock 136399653d4eSeschrock /* 136499653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 136539c23413Seschrock * process. For each spare, there is potentially two different vdev_t 136639c23413Seschrock * structures associated with it: one in the list of spares (used only 136739c23413Seschrock * for basic validation purposes) and one in the active vdev 136839c23413Seschrock * configuration (if it's spared in). During this phase we open and 136939c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 137039c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 137199653d4eSeschrock */ 1372fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1373fa94a07fSbrendan KM_SLEEP); 1374fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 137599653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 137699653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 137799653d4eSeschrock ASSERT(vd != NULL); 137899653d4eSeschrock 1379fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 138099653d4eSeschrock 1381c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1382c5904d13Seschrock B_FALSE)) != NULL) { 138339c23413Seschrock if (!tvd->vdev_isspare) 138439c23413Seschrock spa_spare_add(tvd); 138539c23413Seschrock 138639c23413Seschrock /* 138739c23413Seschrock * We only mark the spare active if we were successfully 138839c23413Seschrock * able to load the vdev. Otherwise, importing a pool 138939c23413Seschrock * with a bad active spare would result in strange 139039c23413Seschrock * behavior, because multiple pool would think the spare 139139c23413Seschrock * is actively in use. 139239c23413Seschrock * 139339c23413Seschrock * There is a vulnerability here to an equally bizarre 139439c23413Seschrock * circumstance, where a dead active spare is later 139539c23413Seschrock * brought back to life (onlined or otherwise). Given 139639c23413Seschrock * the rarity of this scenario, and the extra complexity 139739c23413Seschrock * it adds, we ignore the possibility. 139839c23413Seschrock */ 139939c23413Seschrock if (!vdev_is_dead(tvd)) 140039c23413Seschrock spa_spare_activate(tvd); 140139c23413Seschrock } 140239c23413Seschrock 1403e14bb325SJeff Bonwick vd->vdev_top = vd; 14046809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1405e14bb325SJeff Bonwick 140699653d4eSeschrock if (vdev_open(vd) != 0) 140799653d4eSeschrock continue; 140899653d4eSeschrock 1409fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1410fa94a07fSbrendan spa_spare_add(vd); 141199653d4eSeschrock } 141299653d4eSeschrock 141399653d4eSeschrock /* 141499653d4eSeschrock * Recompute the stashed list of spares, with status information 141599653d4eSeschrock * this time. 141699653d4eSeschrock */ 1417fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 141899653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 141999653d4eSeschrock 1420fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1421fa94a07fSbrendan KM_SLEEP); 1422fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1423fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 14243f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1425fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1426fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1427fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 142899653d4eSeschrock nvlist_free(spares[i]); 1429fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1430fa94a07fSbrendan } 1431fa94a07fSbrendan 1432fa94a07fSbrendan /* 1433fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1434fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1435fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1436fa94a07fSbrendan * then re-generate a more complete list including status information. 1437fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1438fa94a07fSbrendan * not re-opened. 1439fa94a07fSbrendan */ 1440fa94a07fSbrendan static void 1441fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1442fa94a07fSbrendan { 1443fa94a07fSbrendan nvlist_t **l2cache; 1444fa94a07fSbrendan uint_t nl2cache; 1445fa94a07fSbrendan int i, j, oldnvdevs; 1446573ca77eSGeorge Wilson uint64_t guid; 1447fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1448fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1449fa94a07fSbrendan 1450e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1451e14bb325SJeff Bonwick 1452fa94a07fSbrendan if (sav->sav_config != NULL) { 1453fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1454fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1455fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1456fa94a07fSbrendan } else { 1457fa94a07fSbrendan nl2cache = 0; 1458d5285caeSGeorge Wilson newvdevs = NULL; 1459fa94a07fSbrendan } 1460fa94a07fSbrendan 1461fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1462fa94a07fSbrendan oldnvdevs = sav->sav_count; 1463fa94a07fSbrendan sav->sav_vdevs = NULL; 1464fa94a07fSbrendan sav->sav_count = 0; 1465fa94a07fSbrendan 1466fa94a07fSbrendan /* 1467fa94a07fSbrendan * Process new nvlist of vdevs. 1468fa94a07fSbrendan */ 1469fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1470fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1471fa94a07fSbrendan &guid) == 0); 1472fa94a07fSbrendan 1473fa94a07fSbrendan newvdevs[i] = NULL; 1474fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1475fa94a07fSbrendan vd = oldvdevs[j]; 1476fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1477fa94a07fSbrendan /* 1478fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1479fa94a07fSbrendan */ 1480fa94a07fSbrendan newvdevs[i] = vd; 1481fa94a07fSbrendan oldvdevs[j] = NULL; 1482fa94a07fSbrendan break; 1483fa94a07fSbrendan } 1484fa94a07fSbrendan } 1485fa94a07fSbrendan 1486fa94a07fSbrendan if (newvdevs[i] == NULL) { 1487fa94a07fSbrendan /* 1488fa94a07fSbrendan * Create new vdev 1489fa94a07fSbrendan */ 1490fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1491fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1492fa94a07fSbrendan ASSERT(vd != NULL); 1493fa94a07fSbrendan newvdevs[i] = vd; 1494fa94a07fSbrendan 1495fa94a07fSbrendan /* 1496fa94a07fSbrendan * Commit this vdev as an l2cache device, 1497fa94a07fSbrendan * even if it fails to open. 1498fa94a07fSbrendan */ 1499fa94a07fSbrendan spa_l2cache_add(vd); 1500fa94a07fSbrendan 1501c5904d13Seschrock vd->vdev_top = vd; 1502c5904d13Seschrock vd->vdev_aux = sav; 1503c5904d13Seschrock 1504c5904d13Seschrock spa_l2cache_activate(vd); 1505c5904d13Seschrock 1506fa94a07fSbrendan if (vdev_open(vd) != 0) 1507fa94a07fSbrendan continue; 1508fa94a07fSbrendan 1509fa94a07fSbrendan (void) vdev_validate_aux(vd); 1510fa94a07fSbrendan 1511573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1512573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1513fa94a07fSbrendan } 1514fa94a07fSbrendan } 1515fa94a07fSbrendan 1516fa94a07fSbrendan /* 1517fa94a07fSbrendan * Purge vdevs that were dropped 1518fa94a07fSbrendan */ 1519fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1520fa94a07fSbrendan uint64_t pool; 1521fa94a07fSbrendan 1522fa94a07fSbrendan vd = oldvdevs[i]; 1523fa94a07fSbrendan if (vd != NULL) { 1524cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1525cd0837ccSGeorge Wilson 15268ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 15278ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1528fa94a07fSbrendan l2arc_remove_vdev(vd); 1529cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1530cd0837ccSGeorge Wilson vdev_free(vd); 1531fa94a07fSbrendan } 1532fa94a07fSbrendan } 1533fa94a07fSbrendan 1534fa94a07fSbrendan if (oldvdevs) 1535fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1536fa94a07fSbrendan 1537fa94a07fSbrendan if (sav->sav_config == NULL) 1538fa94a07fSbrendan goto out; 1539fa94a07fSbrendan 1540fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1541fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1542fa94a07fSbrendan 1543fa94a07fSbrendan /* 1544fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1545fa94a07fSbrendan * information this time. 1546fa94a07fSbrendan */ 1547fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1548fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1549fa94a07fSbrendan 1550fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1551fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1552fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15533f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1554fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1555fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1556fa94a07fSbrendan out: 1557fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1558fa94a07fSbrendan nvlist_free(l2cache[i]); 1559fa94a07fSbrendan if (sav->sav_count) 1560fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 156199653d4eSeschrock } 156299653d4eSeschrock 156399653d4eSeschrock static int 156499653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 156599653d4eSeschrock { 156699653d4eSeschrock dmu_buf_t *db; 156799653d4eSeschrock char *packed = NULL; 156899653d4eSeschrock size_t nvsize = 0; 156999653d4eSeschrock int error; 157099653d4eSeschrock *value = NULL; 157199653d4eSeschrock 157299653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 157399653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 157499653d4eSeschrock dmu_buf_rele(db, FTAG); 157599653d4eSeschrock 157699653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 15777bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 15787bfdf011SNeil Perrin DMU_READ_PREFETCH); 157999653d4eSeschrock if (error == 0) 158099653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 158199653d4eSeschrock kmem_free(packed, nvsize); 158299653d4eSeschrock 158399653d4eSeschrock return (error); 158499653d4eSeschrock } 158599653d4eSeschrock 15863d7072f8Seschrock /* 15873d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 15883d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 15893d7072f8Seschrock */ 15903d7072f8Seschrock static void 15913d7072f8Seschrock spa_check_removed(vdev_t *vd) 15923d7072f8Seschrock { 1593573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 15943d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 15953d7072f8Seschrock 1596efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1597efb4a871SYuri Pankov !vd->vdev_ishole) { 15983d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 15993d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 16003d7072f8Seschrock } 16013d7072f8Seschrock } 16023d7072f8Seschrock 1603e6ca193dSGeorge Wilson /* 16044b964adaSGeorge Wilson * Validate the current config against the MOS config 1605e6ca193dSGeorge Wilson */ 16064b964adaSGeorge Wilson static boolean_t 16074b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1608e6ca193dSGeorge Wilson { 16094b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 16104b964adaSGeorge Wilson nvlist_t *nv; 16114b964adaSGeorge Wilson 16124b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 16134b964adaSGeorge Wilson 16144b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16154b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 16164b964adaSGeorge Wilson 16174b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1618e6ca193dSGeorge Wilson 161988ecc943SGeorge Wilson /* 16204b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 16214b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 16224b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 162388ecc943SGeorge Wilson */ 16244b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 16254b964adaSGeorge Wilson nvlist_t **child, *nv; 16264b964adaSGeorge Wilson uint64_t idx = 0; 16274b964adaSGeorge Wilson 16284b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 16294b964adaSGeorge Wilson KM_SLEEP); 16304b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1631e6ca193dSGeorge Wilson 16324b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16334b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16344b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16354b964adaSGeorge Wilson 16364b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16374b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16384b964adaSGeorge Wilson mtvd->vdev_islog) 16394b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16404b964adaSGeorge Wilson B_FALSE, 0); 16414b964adaSGeorge Wilson } 16424b964adaSGeorge Wilson 16434b964adaSGeorge Wilson if (idx) { 16444b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16454b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16464b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 16474b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 16484b964adaSGeorge Wilson 16494b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 16504b964adaSGeorge Wilson nvlist_free(child[i]); 16514b964adaSGeorge Wilson } 16524b964adaSGeorge Wilson nvlist_free(nv); 16534b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 16544b964adaSGeorge Wilson } 16554b964adaSGeorge Wilson 16564b964adaSGeorge Wilson /* 16574b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 16584b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 16594b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 16604b964adaSGeorge Wilson */ 166188ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16624b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16634b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16644b964adaSGeorge Wilson 16654b964adaSGeorge Wilson /* 16664b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 16674b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 16684b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 16694b964adaSGeorge Wilson */ 16704b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16714b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 16724b964adaSGeorge Wilson 16734b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 16744b964adaSGeorge Wilson continue; 16754b964adaSGeorge Wilson 16764b964adaSGeorge Wilson /* 16774b964adaSGeorge Wilson * Device specific actions. 16784b964adaSGeorge Wilson */ 16794b964adaSGeorge Wilson if (mtvd->vdev_islog) { 16804b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 16814b964adaSGeorge Wilson } else { 16824b964adaSGeorge Wilson /* 16834b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 16844b964adaSGeorge Wilson * support we should be able to handle 16854b964adaSGeorge Wilson * missing data devices by transitioning 16864b964adaSGeorge Wilson * the pool to readonly. 16874b964adaSGeorge Wilson */ 16884b964adaSGeorge Wilson continue; 16894b964adaSGeorge Wilson } 16904b964adaSGeorge Wilson 16914b964adaSGeorge Wilson /* 16924b964adaSGeorge Wilson * Swap the missing vdev with the data we were 16934b964adaSGeorge Wilson * able to obtain from the MOS config. 16944b964adaSGeorge Wilson */ 16954b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 16964b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 16974b964adaSGeorge Wilson 16984b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 16994b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 17004b964adaSGeorge Wilson 17014b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17024b964adaSGeorge Wilson vdev_load(mtvd); 17034b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17044b964adaSGeorge Wilson 17054b964adaSGeorge Wilson vdev_reopen(rvd); 17064b964adaSGeorge Wilson } else if (mtvd->vdev_islog) { 17074b964adaSGeorge Wilson /* 17084b964adaSGeorge Wilson * Load the slog device's state from the MOS config 17094b964adaSGeorge Wilson * since it's possible that the label does not 17104b964adaSGeorge Wilson * contain the most up-to-date information. 17114b964adaSGeorge Wilson */ 17124b964adaSGeorge Wilson vdev_load_log_state(tvd, mtvd); 17134b964adaSGeorge Wilson vdev_reopen(tvd); 17144b964adaSGeorge Wilson } 1715e6ca193dSGeorge Wilson } 17164b964adaSGeorge Wilson vdev_free(mrvd); 171788ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17184b964adaSGeorge Wilson 17194b964adaSGeorge Wilson /* 17204b964adaSGeorge Wilson * Ensure we were able to validate the config. 17214b964adaSGeorge Wilson */ 17224b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1723e6ca193dSGeorge Wilson } 1724e6ca193dSGeorge Wilson 1725b87f3af3Sperrin /* 1726b87f3af3Sperrin * Check for missing log devices 1727b87f3af3Sperrin */ 17283b2aab18SMatthew Ahrens static boolean_t 1729b87f3af3Sperrin spa_check_logs(spa_t *spa) 1730b87f3af3Sperrin { 17313b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 17323b2aab18SMatthew Ahrens 1733b87f3af3Sperrin switch (spa->spa_log_state) { 1734b87f3af3Sperrin case SPA_LOG_MISSING: 1735b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1736b87f3af3Sperrin case SPA_LOG_UNKNOWN: 17373b2aab18SMatthew Ahrens rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain, 17383b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN) != 0); 17393b2aab18SMatthew Ahrens if (rv) 17401195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1741b87f3af3Sperrin break; 1742b87f3af3Sperrin } 17433b2aab18SMatthew Ahrens return (rv); 1744b87f3af3Sperrin } 1745b87f3af3Sperrin 17461195e687SMark J Musante static boolean_t 17471195e687SMark J Musante spa_passivate_log(spa_t *spa) 17481195e687SMark J Musante { 17491195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17501195e687SMark J Musante boolean_t slog_found = B_FALSE; 17511195e687SMark J Musante 17521195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17531195e687SMark J Musante 17541195e687SMark J Musante if (!spa_has_slogs(spa)) 17551195e687SMark J Musante return (B_FALSE); 17561195e687SMark J Musante 17571195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17581195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17591195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17601195e687SMark J Musante 17611195e687SMark J Musante if (tvd->vdev_islog) { 17621195e687SMark J Musante metaslab_group_passivate(mg); 17631195e687SMark J Musante slog_found = B_TRUE; 17641195e687SMark J Musante } 17651195e687SMark J Musante } 17661195e687SMark J Musante 17671195e687SMark J Musante return (slog_found); 17681195e687SMark J Musante } 17691195e687SMark J Musante 17701195e687SMark J Musante static void 17711195e687SMark J Musante spa_activate_log(spa_t *spa) 17721195e687SMark J Musante { 17731195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17741195e687SMark J Musante 17751195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17761195e687SMark J Musante 17771195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17781195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17791195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17801195e687SMark J Musante 17811195e687SMark J Musante if (tvd->vdev_islog) 17821195e687SMark J Musante metaslab_group_activate(mg); 17831195e687SMark J Musante } 17841195e687SMark J Musante } 17851195e687SMark J Musante 17861195e687SMark J Musante int 17871195e687SMark J Musante spa_offline_log(spa_t *spa) 17881195e687SMark J Musante { 17893b2aab18SMatthew Ahrens int error; 17901195e687SMark J Musante 17913b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 17923b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 17933b2aab18SMatthew Ahrens if (error == 0) { 17941195e687SMark J Musante /* 17951195e687SMark J Musante * We successfully offlined the log device, sync out the 17961195e687SMark J Musante * current txg so that the "stubby" block can be removed 17971195e687SMark J Musante * by zil_sync(). 17981195e687SMark J Musante */ 17991195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18001195e687SMark J Musante } 18011195e687SMark J Musante return (error); 18021195e687SMark J Musante } 18031195e687SMark J Musante 1804b693757aSEric Schrock static void 1805b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1806b693757aSEric Schrock { 1807b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1808b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1809b693757aSEric Schrock } 1810b693757aSEric Schrock 1811b24ab676SJeff Bonwick void 1812b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1813b24ab676SJeff Bonwick { 1814b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1815b24ab676SJeff Bonwick 1816b24ab676SJeff Bonwick if (zio->io_error) 1817b24ab676SJeff Bonwick return; 1818b24ab676SJeff Bonwick 1819b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1820b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1821b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1822b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1823b24ab676SJeff Bonwick } 1824b24ab676SJeff Bonwick 1825468c413aSTim Haley typedef struct spa_load_error { 1826c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1827468c413aSTim Haley uint64_t sle_data_count; 1828468c413aSTim Haley } spa_load_error_t; 1829468c413aSTim Haley 1830468c413aSTim Haley static void 1831468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1832468c413aSTim Haley { 1833468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1834468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1835468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1836468c413aSTim Haley int error = zio->io_error; 1837e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1838468c413aSTim Haley 1839468c413aSTim Haley if (error) { 1840ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1841468c413aSTim Haley type != DMU_OT_INTENT_LOG) 18421a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1843468c413aSTim Haley else 18441a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1845468c413aSTim Haley } 1846468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1847e42d2059SMatthew Ahrens 1848e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1849e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1850e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1851e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1852468c413aSTim Haley } 1853468c413aSTim Haley 1854e42d2059SMatthew Ahrens /* 1855e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1856e42d2059SMatthew Ahrens * a pool while importing it. 1857e42d2059SMatthew Ahrens */ 1858e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1859e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1860e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1861e42d2059SMatthew Ahrens 1862468c413aSTim Haley /*ARGSUSED*/ 1863468c413aSTim Haley static int 1864b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 18657802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1866468c413aSTim Haley { 1867e42d2059SMatthew Ahrens if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1868e42d2059SMatthew Ahrens return (0); 1869e42d2059SMatthew Ahrens /* 1870e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1871e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1872e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1873e42d2059SMatthew Ahrens */ 1874e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1875e42d2059SMatthew Ahrens return (0); 1876e42d2059SMatthew Ahrens if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data) 1877e42d2059SMatthew Ahrens return (0); 1878468c413aSTim Haley 1879e42d2059SMatthew Ahrens zio_t *rio = arg; 1880e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1881e42d2059SMatthew Ahrens void *data = zio_data_buf_alloc(size); 1882e42d2059SMatthew Ahrens 1883e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1884e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1885e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1886e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1887e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1888e42d2059SMatthew Ahrens 1889e42d2059SMatthew Ahrens zio_nowait(zio_read(rio, spa, bp, data, size, 1890e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1891e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1892e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1893468c413aSTim Haley return (0); 1894468c413aSTim Haley } 1895468c413aSTim Haley 1896468c413aSTim Haley static int 1897468c413aSTim Haley spa_load_verify(spa_t *spa) 1898468c413aSTim Haley { 1899468c413aSTim Haley zio_t *rio; 1900468c413aSTim Haley spa_load_error_t sle = { 0 }; 1901468c413aSTim Haley zpool_rewind_policy_t policy; 1902468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1903e42d2059SMatthew Ahrens int error = 0; 1904468c413aSTim Haley 1905c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1906c8ee1847SVictor Latushkin 1907c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1908c8ee1847SVictor Latushkin return (0); 1909c8ee1847SVictor Latushkin 1910468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1911468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1912468c413aSTim Haley 1913e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 1914e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 1915e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 1916e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 1917e42d2059SMatthew Ahrens } 1918468c413aSTim Haley 1919468c413aSTim Haley (void) zio_wait(rio); 1920468c413aSTim Haley 1921c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1922468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1923468c413aSTim Haley 1924c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1925468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 19264b964adaSGeorge Wilson int64_t loss = 0; 19274b964adaSGeorge Wilson 1928468c413aSTim Haley verify_ok = B_TRUE; 1929468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1930468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 19314b964adaSGeorge Wilson 19324b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 19334b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19344b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 19354b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 19364b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 19374b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19384b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1939a33cae98STim Haley } else { 1940a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1941468c413aSTim Haley } 1942468c413aSTim Haley 1943468c413aSTim Haley if (error) { 1944468c413aSTim Haley if (error != ENXIO && error != EIO) 1945be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1946468c413aSTim Haley return (error); 1947468c413aSTim Haley } 1948468c413aSTim Haley 1949468c413aSTim Haley return (verify_ok ? 0 : EIO); 1950468c413aSTim Haley } 1951468c413aSTim Haley 19521195e687SMark J Musante /* 19531195e687SMark J Musante * Find a value in the pool props object. 19541195e687SMark J Musante */ 19551195e687SMark J Musante static void 19561195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 19571195e687SMark J Musante { 19581195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 19591195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 19601195e687SMark J Musante } 19611195e687SMark J Musante 19621195e687SMark J Musante /* 19631195e687SMark J Musante * Find a value in the pool directory object. 19641195e687SMark J Musante */ 19651195e687SMark J Musante static int 19661195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 19671195e687SMark J Musante { 19681195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 19691195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 19701195e687SMark J Musante } 19711195e687SMark J Musante 19721195e687SMark J Musante static int 19731195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 19741195e687SMark J Musante { 19751195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 19761195e687SMark J Musante return (err); 19771195e687SMark J Musante } 19781195e687SMark J Musante 19791195e687SMark J Musante /* 19801195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 19811195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 19821195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 19831195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 19841195e687SMark J Musante * 19851195e687SMark J Musante * This function determines what to do with that list: either rejoin 19861195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 19871195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 19881195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 19891195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 19901195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 19911195e687SMark J Musante * 1992d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 1993d41c4376SMark J Musante * the original pool. 19941195e687SMark J Musante */ 19951195e687SMark J Musante static void 19961195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 19971195e687SMark J Musante { 19981195e687SMark J Musante uint_t extracted; 19991195e687SMark J Musante uint64_t *glist; 20001195e687SMark J Musante uint_t i, gcount; 20011195e687SMark J Musante nvlist_t *nvl; 20021195e687SMark J Musante vdev_t **vd; 20031195e687SMark J Musante boolean_t attempt_reopen; 20041195e687SMark J Musante 20051195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 20061195e687SMark J Musante return; 20071195e687SMark J Musante 20081195e687SMark J Musante /* check that the config is complete */ 20091195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 20101195e687SMark J Musante &glist, &gcount) != 0) 20111195e687SMark J Musante return; 20121195e687SMark J Musante 20131195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 20141195e687SMark J Musante 20151195e687SMark J Musante /* attempt to online all the vdevs & validate */ 20161195e687SMark J Musante attempt_reopen = B_TRUE; 20171195e687SMark J Musante for (i = 0; i < gcount; i++) { 20181195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 20191195e687SMark J Musante continue; 20201195e687SMark J Musante 20211195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 20221195e687SMark J Musante if (vd[i] == NULL) { 20231195e687SMark J Musante /* 20241195e687SMark J Musante * Don't bother attempting to reopen the disks; 20251195e687SMark J Musante * just do the split. 20261195e687SMark J Musante */ 20271195e687SMark J Musante attempt_reopen = B_FALSE; 20281195e687SMark J Musante } else { 20291195e687SMark J Musante /* attempt to re-online it */ 20301195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 20311195e687SMark J Musante } 20321195e687SMark J Musante } 20331195e687SMark J Musante 20341195e687SMark J Musante if (attempt_reopen) { 20351195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 20361195e687SMark J Musante 20371195e687SMark J Musante /* check each device to see what state it's in */ 20381195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 20391195e687SMark J Musante if (vd[i] != NULL && 20401195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 20411195e687SMark J Musante break; 20421195e687SMark J Musante ++extracted; 20431195e687SMark J Musante } 20441195e687SMark J Musante } 20451195e687SMark J Musante 20461195e687SMark J Musante /* 20471195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 20481195e687SMark J Musante * even attempted to look at them, then we split them off for 20491195e687SMark J Musante * good. 20501195e687SMark J Musante */ 20511195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 20521195e687SMark J Musante for (i = 0; i < gcount; i++) 20531195e687SMark J Musante if (vd[i] != NULL) 20541195e687SMark J Musante vdev_split(vd[i]); 20551195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 20561195e687SMark J Musante } 20571195e687SMark J Musante 20581195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 20591195e687SMark J Musante } 20601195e687SMark J Musante 20611195e687SMark J Musante static int 20621195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 20631195e687SMark J Musante boolean_t mosconfig) 20641195e687SMark J Musante { 20651195e687SMark J Musante nvlist_t *config = spa->spa_config; 20661195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 20678704186eSDan McDonald char *comment; 20681195e687SMark J Musante int error; 20691195e687SMark J Musante uint64_t pool_guid; 20701195e687SMark J Musante nvlist_t *nvl; 20711195e687SMark J Musante 20721195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 2073be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 20741195e687SMark J Musante 20758704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 20768704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 20778704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 20788704186eSDan McDonald 20791195e687SMark J Musante /* 20801195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 20811195e687SMark J Musante * it's not present treat it as the initial version. 20821195e687SMark J Musante */ 20831195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 20841195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 20851195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 20861195e687SMark J Musante 20871195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 20881195e687SMark J Musante &spa->spa_config_txg); 20891195e687SMark J Musante 20901195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 20911195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 2092be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 20931195e687SMark J Musante } else { 2094e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 20951195e687SMark J Musante 20961195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 20971195e687SMark J Musante &nvl) == 0) { 20981195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 20991195e687SMark J Musante KM_SLEEP) == 0); 21001195e687SMark J Musante } 21011195e687SMark J Musante 2102ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2103ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2104ad135b5dSChristopher Siden 210511027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 21061195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 21071195e687SMark J Musante mosconfig, &ereport); 21081195e687SMark J Musante } 21091195e687SMark J Musante 21101195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 211111027bc7STim Haley if (error) { 211211027bc7STim Haley if (error != EEXIST) { 211311027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 211411027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 211511027bc7STim Haley } 211611027bc7STim Haley if (error != EBADF) { 211711027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 211811027bc7STim Haley } 211911027bc7STim Haley } 21201195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 21211195e687SMark J Musante spa->spa_ena = 0; 21221195e687SMark J Musante 21231195e687SMark J Musante return (error); 21241195e687SMark J Musante } 21251195e687SMark J Musante 2126fa9e4066Sahrens /* 2127fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2128ea8dc4b6Seschrock * source of configuration information. 2129fa9e4066Sahrens */ 2130fa9e4066Sahrens static int 21311195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 21321195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 21331195e687SMark J Musante char **ereport) 2134fa9e4066Sahrens { 2135fa9e4066Sahrens int error = 0; 2136871a9500SMark J Musante nvlist_t *nvroot = NULL; 2137ad135b5dSChristopher Siden nvlist_t *label; 2138fa9e4066Sahrens vdev_t *rvd; 2139fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 21404b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 21418ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 21421195e687SMark J Musante int parse; 2143cde58dbcSMatthew Ahrens uint64_t obj; 2144ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2145fa9e4066Sahrens 21468ad4d6ddSJeff Bonwick /* 21478ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 21488ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 21498ad4d6ddSJeff Bonwick */ 21508ad4d6ddSJeff Bonwick if (!mosconfig) 21518ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 21528ad4d6ddSJeff Bonwick 2153e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2154e14bb325SJeff Bonwick 2155ea8dc4b6Seschrock spa->spa_load_state = state; 21560373e76bSbonwick 21571195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 2158be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2159fa9e4066Sahrens 21601195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 21611195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2162b5989ec7Seschrock 216354d692b7SGeorge Wilson /* 216454d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 216554d692b7SGeorge Wilson */ 21666f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 21676f834bc1SMatthew Ahrens KM_SLEEP); 21686f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 21696f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 21706f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 21716f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 21726f834bc1SMatthew Ahrens } 217354d692b7SGeorge Wilson 2174fa9e4066Sahrens /* 217599653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 217699653d4eSeschrock * value that will be returned by spa_version() since parsing the 217799653d4eSeschrock * configuration requires knowing the version number. 2178fa9e4066Sahrens */ 2179e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21801195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2181e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2182fa9e4066Sahrens 218399653d4eSeschrock if (error != 0) 21841195e687SMark J Musante return (error); 2185fa9e4066Sahrens 21860e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 21871195e687SMark J Musante 21881195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 21891195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 21901195e687SMark J Musante } 2191fa9e4066Sahrens 2192fa9e4066Sahrens /* 2193fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2194fa9e4066Sahrens */ 2195e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21960bf246f5Smc error = vdev_open(rvd); 2197e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 21980bf246f5Smc if (error != 0) 21991195e687SMark J Musante return (error); 2200fa9e4066Sahrens 2201560e6e96Seschrock /* 220277e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 220377e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 220477e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 22051195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 220677e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 220777e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 220877e3a39cSMark J Musante * the vdev config. 22091195e687SMark J Musante * 22101195e687SMark J Musante * If we're assembling a new pool that's been split off from an 22111195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 22121195e687SMark J Musante * validation for now. 2213560e6e96Seschrock */ 22141195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 22151195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2216d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 22171195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2218560e6e96Seschrock 22191195e687SMark J Musante if (error != 0) 22201195e687SMark J Musante return (error); 22211195e687SMark J Musante 22221195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2223be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 2224560e6e96Seschrock } 2225560e6e96Seschrock 2226fa9e4066Sahrens /* 2227fa9e4066Sahrens * Find the best uberblock. 2228fa9e4066Sahrens */ 2229ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2230fa9e4066Sahrens 2231fa9e4066Sahrens /* 2232fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2233fa9e4066Sahrens */ 2234ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2235ad135b5dSChristopher Siden nvlist_free(label); 22361195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2237ad135b5dSChristopher Siden } 2238ea8dc4b6Seschrock 2239ea8dc4b6Seschrock /* 2240ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2241ea8dc4b6Seschrock */ 2242ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2243ad135b5dSChristopher Siden nvlist_free(label); 22441195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2245ad135b5dSChristopher Siden } 2246ad135b5dSChristopher Siden 2247ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2248ad135b5dSChristopher Siden nvlist_t *features; 2249ad135b5dSChristopher Siden 2250ad135b5dSChristopher Siden /* 2251ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2252ad135b5dSChristopher Siden * MOS in the label, return failure. 2253ad135b5dSChristopher Siden */ 2254ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2255ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2256ad135b5dSChristopher Siden nvlist_free(label); 2257ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2258ad135b5dSChristopher Siden ENXIO)); 2259ad135b5dSChristopher Siden } 2260ad135b5dSChristopher Siden 2261ad135b5dSChristopher Siden /* 2262ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2263ad135b5dSChristopher Siden * from the label. 2264ad135b5dSChristopher Siden */ 2265ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2266ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2267ad135b5dSChristopher Siden } 2268ad135b5dSChristopher Siden 2269ad135b5dSChristopher Siden nvlist_free(label); 2270ad135b5dSChristopher Siden 2271ad135b5dSChristopher Siden /* 2272ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2273ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2274ad135b5dSChristopher Siden * cannot open a pool. 2275ad135b5dSChristopher Siden */ 2276ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2277ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2278ad135b5dSChristopher Siden 2279ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2280ad135b5dSChristopher Siden 0); 2281ad135b5dSChristopher Siden 2282ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2283ad135b5dSChristopher Siden NULL); nvp != NULL; 2284ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2285ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2286ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2287ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2288ad135b5dSChristopher Siden } 2289ad135b5dSChristopher Siden } 2290ad135b5dSChristopher Siden 2291ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2292ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2293ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2294ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2295ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2296ad135b5dSChristopher Siden ENOTSUP)); 2297ad135b5dSChristopher Siden } 2298ad135b5dSChristopher Siden 2299ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2300ad135b5dSChristopher Siden } 2301fa9e4066Sahrens 2302fa9e4066Sahrens /* 2303fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 23044b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 23054b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 23064b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 23074b964adaSGeorge Wilson * can handle missing vdevs. 2308fa9e4066Sahrens */ 23094b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 23104b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 23111195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 23121195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 23131195e687SMark J Musante 23141195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 23151195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 23161195e687SMark J Musante spa_try_repair(spa, config); 23171195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 23181195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 23191195e687SMark J Musante spa->spa_config_splitting = NULL; 2320fa9e4066Sahrens } 2321fa9e4066Sahrens 2322fa9e4066Sahrens /* 2323fa9e4066Sahrens * Initialize internal SPA structures. 2324fa9e4066Sahrens */ 2325fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2326fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2327468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2328c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2329468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2330468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2331b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 23323f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2333b24ab676SJeff Bonwick 2334ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 23351195e687SMark J Musante if (error) 23361195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2337fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2338fa9e4066Sahrens 23391195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 23401195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2341fa9e4066Sahrens 2342ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2343ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 234457221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2345ad135b5dSChristopher Siden 2346ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2347ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2348ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2349ad135b5dSChristopher Siden } 2350ad135b5dSChristopher Siden 2351ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2352ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2353ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2354ad135b5dSChristopher Siden } 2355ad135b5dSChristopher Siden 2356ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2357ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2358ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2359ad135b5dSChristopher Siden } 2360ad135b5dSChristopher Siden 236157221772SChristopher Siden enabled_feat = fnvlist_alloc(); 236257221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2363ad135b5dSChristopher Siden 23642acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 236557221772SChristopher Siden unsup_feat, enabled_feat)) 2366ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2367ad135b5dSChristopher Siden 2368ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 23692acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 237057221772SChristopher Siden unsup_feat, enabled_feat)) { 2371ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 237257221772SChristopher Siden } 2373ad135b5dSChristopher Siden } 2374ad135b5dSChristopher Siden 237557221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 237657221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 237757221772SChristopher Siden 2378ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 237957221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 238057221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2381ad135b5dSChristopher Siden } 2382ad135b5dSChristopher Siden 238357221772SChristopher Siden fnvlist_free(enabled_feat); 238457221772SChristopher Siden fnvlist_free(unsup_feat); 2385ad135b5dSChristopher Siden 2386ad135b5dSChristopher Siden if (!missing_feat_read) { 2387ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2388ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2389ad135b5dSChristopher Siden } 2390ad135b5dSChristopher Siden 2391ad135b5dSChristopher Siden /* 2392ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2393ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2394ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2395ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2396ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2397ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2398ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2399ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2400ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2401ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2402ad135b5dSChristopher Siden * available for open in read-only mode. 2403ad135b5dSChristopher Siden * 2404ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2405ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2406ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2407ad135b5dSChristopher Siden * userland in order to know whether to display the 2408ad135b5dSChristopher Siden * abovementioned note. 2409ad135b5dSChristopher Siden */ 2410ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2411ad135b5dSChristopher Siden spa_writeable(spa))) { 2412ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2413ad135b5dSChristopher Siden ENOTSUP)); 2414ad135b5dSChristopher Siden } 241543466aaeSMax Grossman 241643466aaeSMax Grossman /* 241743466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 241843466aaeSMax Grossman * cache during SPA initialization. 241943466aaeSMax Grossman */ 242043466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 242143466aaeSMax Grossman uint64_t refcount; 242243466aaeSMax Grossman 242343466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 242443466aaeSMax Grossman &spa_feature_table[i], &refcount); 242543466aaeSMax Grossman if (error == 0) { 242643466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 242743466aaeSMax Grossman } else if (error == ENOTSUP) { 242843466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 242943466aaeSMax Grossman SPA_FEATURE_DISABLED; 243043466aaeSMax Grossman } else { 243143466aaeSMax Grossman return (spa_vdev_err(rvd, 243243466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 243343466aaeSMax Grossman } 243443466aaeSMax Grossman } 243543466aaeSMax Grossman } 243643466aaeSMax Grossman 243743466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 243843466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 24395d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0) 244043466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2441ad135b5dSChristopher Siden } 2442ad135b5dSChristopher Siden 2443ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2444ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2445ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2446ad135b5dSChristopher Siden if (error != 0) 2447ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2448ad135b5dSChristopher Siden 2449fa9e4066Sahrens if (!mosconfig) { 245095173954Sek uint64_t hostid; 2451871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2452871a9500SMark J Musante 2453871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2454871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2455fa9e4066Sahrens 245688ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 245777650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 245895173954Sek char *hostname; 245995173954Sek unsigned long myhostid = 0; 246095173954Sek 246188ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 246295173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 246395173954Sek 24645679c89fSjv #ifdef _KERNEL 24655679c89fSjv myhostid = zone_get_hostid(NULL); 24665679c89fSjv #else /* _KERNEL */ 24675679c89fSjv /* 24685679c89fSjv * We're emulating the system's hostid in userland, so 24695679c89fSjv * we can't use zone_get_hostid(). 24705679c89fSjv */ 247195173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 24725679c89fSjv #endif /* _KERNEL */ 247317194a52Slling if (hostid != 0 && myhostid != 0 && 24745679c89fSjv hostid != myhostid) { 2475871a9500SMark J Musante nvlist_free(nvconfig); 247695173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 247795173954Sek "loaded as it was last accessed by " 247877650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2479654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2480e14bb325SJeff Bonwick spa_name(spa), hostname, 248195173954Sek (unsigned long)hostid); 2482be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 248395173954Sek } 248495173954Sek } 2485c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2486c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2487c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2488c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 248995173954Sek 249088ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2491fa9e4066Sahrens spa_unload(spa); 2492fa9e4066Sahrens spa_deactivate(spa); 24938ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2494fa9e4066Sahrens 24951195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2496fa9e4066Sahrens } 2497fa9e4066Sahrens 2498cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2499cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2500cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2501cde58dbcSMatthew Ahrens if (error != 0) 25021195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2503fa9e4066Sahrens 250499653d4eSeschrock /* 250599653d4eSeschrock * Load the bit that tells us to use the new accounting function 250699653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 250799653d4eSeschrock * be present. 250899653d4eSeschrock */ 25091195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 25101195e687SMark J Musante if (error != 0 && error != ENOENT) 25111195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 251299653d4eSeschrock 25133f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 25143f9d6ad7SLin Ling &spa->spa_creation_version); 25153f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 25163f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 25173f9d6ad7SLin Ling 2518fa9e4066Sahrens /* 2519ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2520ea8dc4b6Seschrock * not be present. 2521fa9e4066Sahrens */ 25221195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 25231195e687SMark J Musante if (error != 0 && error != ENOENT) 25241195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2525ea8dc4b6Seschrock 25261195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 25271195e687SMark J Musante &spa->spa_errlog_scrub); 25281195e687SMark J Musante if (error != 0 && error != ENOENT) 25291195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2530ea8dc4b6Seschrock 253106eeb2adSek /* 253206eeb2adSek * Load the history object. If we have an older pool, this 253306eeb2adSek * will not be present. 253406eeb2adSek */ 25351195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 25361195e687SMark J Musante if (error != 0 && error != ENOENT) 25371195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 25381195e687SMark J Musante 25391195e687SMark J Musante /* 25401195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 25411195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 25421195e687SMark J Musante * devices. 25431195e687SMark J Musante */ 254406eeb2adSek 254599653d4eSeschrock /* 254699653d4eSeschrock * Load any hot spares for this pool. 254799653d4eSeschrock */ 25481195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 25491195e687SMark J Musante if (error != 0 && error != ENOENT) 25501195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 25511195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2552e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2553fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 25541195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 25551195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 255699653d4eSeschrock 2557e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 255899653d4eSeschrock spa_load_spares(spa); 2559e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 25601195e687SMark J Musante } else if (error == 0) { 25611195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 256299653d4eSeschrock } 256399653d4eSeschrock 2564fa94a07fSbrendan /* 2565fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2566fa94a07fSbrendan */ 25671195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2568fa94a07fSbrendan &spa->spa_l2cache.sav_object); 25691195e687SMark J Musante if (error != 0 && error != ENOENT) 25701195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 25711195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2572fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2573fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 25741195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 25751195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2576fa94a07fSbrendan 2577e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2578fa94a07fSbrendan spa_load_l2cache(spa); 2579e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 25801195e687SMark J Musante } else if (error == 0) { 25811195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2582fa94a07fSbrendan } 2583fa94a07fSbrendan 2584990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2585ecd6cf80Smarks 25861195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 25871195e687SMark J Musante if (error && error != ENOENT) 25881195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2589b1b8ab34Slling 2590b1b8ab34Slling if (error == 0) { 25911195e687SMark J Musante uint64_t autoreplace; 25921195e687SMark J Musante 25931195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 25941195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 25951195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 25961195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 25971195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 25981195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 25991195e687SMark J Musante &spa->spa_dedup_ditto); 26001195e687SMark J Musante 2601b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2602b1b8ab34Slling } 2603b1b8ab34Slling 26043d7072f8Seschrock /* 26053d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 26063d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 26073d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 26083d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 26093d7072f8Seschrock * over. 26103d7072f8Seschrock */ 2611b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 26123d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2613b693757aSEric Schrock /* 2614b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2615b693757aSEric Schrock * at this point we're using the spare definitions from 2616b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2617b693757aSEric Schrock */ 2618b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2619b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2620b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2621b693757aSEric Schrock } 2622b693757aSEric Schrock } 26233d7072f8Seschrock 2624ea8dc4b6Seschrock /* 2625560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2626ea8dc4b6Seschrock */ 2627560e6e96Seschrock vdev_load(rvd); 26280373e76bSbonwick 2629fa9e4066Sahrens /* 2630fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2631fa9e4066Sahrens */ 2632e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2633fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2634e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2635fa9e4066Sahrens 2636b24ab676SJeff Bonwick /* 2637b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2638b24ab676SJeff Bonwick */ 2639b24ab676SJeff Bonwick error = ddt_load(spa); 26401195e687SMark J Musante if (error != 0) 26411195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2642b24ab676SJeff Bonwick 2643485bbbf5SGeorge Wilson spa_update_dspace(spa); 2644485bbbf5SGeorge Wilson 2645b24ab676SJeff Bonwick /* 26464b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 26474b964adaSGeorge Wilson * information which might be missing. If we fail to validate 26484b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 26494b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 26504b964adaSGeorge Wilson * over. 2651b24ab676SJeff Bonwick */ 26521195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2653871a9500SMark J Musante nvlist_t *nvconfig; 2654871a9500SMark J Musante 2655871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2656871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2657871a9500SMark J Musante 26584b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 26594b964adaSGeorge Wilson nvlist_free(nvconfig); 26604b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 26614b964adaSGeorge Wilson ENXIO)); 26624b964adaSGeorge Wilson } 26631195e687SMark J Musante nvlist_free(nvconfig); 26641195e687SMark J Musante 26654b964adaSGeorge Wilson /* 2666ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 26674b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 26684b964adaSGeorge Wilson * more toplevel vdevs are faulted. 26694b964adaSGeorge Wilson */ 26704b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2671be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 26724b964adaSGeorge Wilson 26731195e687SMark J Musante if (spa_check_logs(spa)) { 26741195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 26751195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 26761195e687SMark J Musante } 2677b24ab676SJeff Bonwick } 2678b24ab676SJeff Bonwick 2679ad135b5dSChristopher Siden if (missing_feat_write) { 2680ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2681ad135b5dSChristopher Siden 2682ad135b5dSChristopher Siden /* 2683ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2684ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2685ad135b5dSChristopher Siden * information and can return to userland. 2686ad135b5dSChristopher Siden */ 2687ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2688ad135b5dSChristopher Siden } 2689ad135b5dSChristopher Siden 26904b964adaSGeorge Wilson /* 26914b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 26924b964adaSGeorge Wilson * to start pushing transactions. 26934b964adaSGeorge Wilson */ 26944b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 26954b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 26964b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 26974b964adaSGeorge Wilson error)); 26984b964adaSGeorge Wilson } 26994b964adaSGeorge Wilson 2700468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2701468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 27025dabedeeSbonwick dmu_tx_t *tx; 27030373e76bSbonwick int need_update = B_FALSE; 27048ad4d6ddSJeff Bonwick 27058ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 27065dabedeeSbonwick 27070373e76bSbonwick /* 27080373e76bSbonwick * Claim log blocks that haven't been committed yet. 27090373e76bSbonwick * This must all happen in a single txg. 2710b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2711b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2712468c413aSTim Haley * Price of rollback is that we abandon the log. 27130373e76bSbonwick */ 2714b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2715b24ab676SJeff Bonwick 27165dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2717fa9e4066Sahrens spa_first_txg(spa)); 2718e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 27190b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2720fa9e4066Sahrens dmu_tx_commit(tx); 2721fa9e4066Sahrens 2722b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2723b24ab676SJeff Bonwick 27241195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2725fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2726fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2727fa9e4066Sahrens 2728fa9e4066Sahrens /* 2729b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2730b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2731b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2732b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2733b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2734fa9e4066Sahrens */ 2735b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 27360e34b6a7Sbonwick 27370e34b6a7Sbonwick /* 27380373e76bSbonwick * If the config cache is stale, or we have uninitialized 27390373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2740bc758434SLin Ling * 27414b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2742bc758434SLin Ling * in-core spa_config and update the disk labels. 27430e34b6a7Sbonwick */ 27440373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 27454b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 27464b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 27474b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 27480373e76bSbonwick need_update = B_TRUE; 27490373e76bSbonwick 27508ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 27510373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 27520373e76bSbonwick need_update = B_TRUE; 27530e34b6a7Sbonwick 27540e34b6a7Sbonwick /* 27550373e76bSbonwick * Update the config cache asychronously in case we're the 27560373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 27570e34b6a7Sbonwick */ 27580373e76bSbonwick if (need_update) 27590373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 27608ad4d6ddSJeff Bonwick 27618ad4d6ddSJeff Bonwick /* 27628ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 27638ad4d6ddSJeff Bonwick */ 27643f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 27653f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 27668ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2767503ad85cSMatthew Ahrens 27684445fffbSMatthew Ahrens /* 27694445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 27704445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 27714445fffbSMatthew Ahrens */ 27724445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 27734445fffbSMatthew Ahrens 2774503ad85cSMatthew Ahrens /* 2775503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2776503ad85cSMatthew Ahrens */ 2777503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2778503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2779ca45db41SChris Kirby 2780ca45db41SChris Kirby /* 2781ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2782ca45db41SChris Kirby */ 2783ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2784fa9e4066Sahrens } 2785fa9e4066Sahrens 27861195e687SMark J Musante return (0); 2787fa9e4066Sahrens } 2788fa9e4066Sahrens 2789468c413aSTim Haley static int 2790468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2791468c413aSTim Haley { 2792f9af39baSGeorge Wilson int mode = spa->spa_mode; 2793f9af39baSGeorge Wilson 2794468c413aSTim Haley spa_unload(spa); 2795468c413aSTim Haley spa_deactivate(spa); 2796468c413aSTim Haley 2797e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 2798468c413aSTim Haley 2799f9af39baSGeorge Wilson spa_activate(spa, mode); 2800468c413aSTim Haley spa_async_suspend(spa); 2801468c413aSTim Haley 28021195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2803468c413aSTim Haley } 2804468c413aSTim Haley 2805ad135b5dSChristopher Siden /* 2806ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2807ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2808ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2809ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2810ad135b5dSChristopher Siden * spa_load(). 2811ad135b5dSChristopher Siden */ 2812468c413aSTim Haley static int 2813468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2814c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2815468c413aSTim Haley { 2816ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2817468c413aSTim Haley nvlist_t *config = NULL; 2818468c413aSTim Haley int load_error, rewind_error; 2819c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2820468c413aSTim Haley uint64_t min_txg; 2821468c413aSTim Haley 2822a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2823468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 28241195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2825a33cae98STim Haley } else { 2826468c413aSTim Haley spa->spa_load_max_txg = max_request; 2827e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 2828e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 2829a33cae98STim Haley } 2830468c413aSTim Haley 28311195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 28321195e687SMark J Musante mosconfig); 2833468c413aSTim Haley if (load_error == 0) 2834468c413aSTim Haley return (0); 2835468c413aSTim Haley 2836468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2837468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2838468c413aSTim Haley 2839468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2840468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2841468c413aSTim Haley 2842c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2843468c413aSTim Haley nvlist_free(config); 2844468c413aSTim Haley return (load_error); 2845468c413aSTim Haley } 2846468c413aSTim Haley 2847ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2848ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 28491195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2850ad135b5dSChristopher Siden } else { 2851ad135b5dSChristopher Siden /* 2852ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2853ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 2854ad135b5dSChristopher Siden * to rewind. 2855ad135b5dSChristopher Siden */ 2856ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 2857ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2858ad135b5dSChristopher Siden } 2859468c413aSTim Haley 2860c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2861c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2862c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2863c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2864468c413aSTim Haley 2865c8ee1847SVictor Latushkin /* 2866c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2867c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2868c8ee1847SVictor Latushkin */ 2869c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2870c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2871c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2872468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2873468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 2874468c413aSTim Haley } 2875468c413aSTim Haley 2876468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 2877468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 2878468c413aSTim Haley 2879468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 2880468c413aSTim Haley spa_config_set(spa, config); 2881468c413aSTim Haley 2882ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2883ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 2884ad135b5dSChristopher Siden return (rewind_error); 2885ad135b5dSChristopher Siden } else { 2886ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 2887ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 2888ad135b5dSChristopher Siden spa->spa_load_info); 2889ad135b5dSChristopher Siden 2890ad135b5dSChristopher Siden /* Restore the initial load info */ 2891ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 2892ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 2893ad135b5dSChristopher Siden 2894ad135b5dSChristopher Siden return (load_error); 2895ad135b5dSChristopher Siden } 2896468c413aSTim Haley } 2897468c413aSTim Haley 2898fa9e4066Sahrens /* 2899fa9e4066Sahrens * Pool Open/Import 2900fa9e4066Sahrens * 2901fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 2902fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2903fa9e4066Sahrens * case of an open, the pool configuration will exist in the 29043d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 2905fa9e4066Sahrens * 2906fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2907fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 2908fa9e4066Sahrens * ambiguous state. 2909fa9e4066Sahrens */ 2910fa9e4066Sahrens static int 2911468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 2912468c413aSTim Haley nvlist_t **config) 2913fa9e4066Sahrens { 2914fa9e4066Sahrens spa_t *spa; 29154b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 2916fa9e4066Sahrens int error; 2917fa9e4066Sahrens int locked = B_FALSE; 2918fa9e4066Sahrens 2919fa9e4066Sahrens *spapp = NULL; 2920fa9e4066Sahrens 2921fa9e4066Sahrens /* 2922fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 2923fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2924fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 2925fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 2926fa9e4066Sahrens */ 2927fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2928fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2929fa9e4066Sahrens locked = B_TRUE; 2930fa9e4066Sahrens } 2931fa9e4066Sahrens 2932fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2933fa9e4066Sahrens if (locked) 2934fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2935be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 2936fa9e4066Sahrens } 2937468c413aSTim Haley 2938fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 29394b44c88cSTim Haley zpool_rewind_policy_t policy; 29404b44c88cSTim Haley 29414b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 29424b44c88cSTim Haley &policy); 29434b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 29444b44c88cSTim Haley state = SPA_LOAD_RECOVER; 2945fa9e4066Sahrens 29468ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2947fa9e4066Sahrens 2948468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 2949468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2950468c413aSTim Haley 2951468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 2952c8ee1847SVictor Latushkin policy.zrp_request); 2953fa9e4066Sahrens 2954fa9e4066Sahrens if (error == EBADF) { 2955fa9e4066Sahrens /* 2956560e6e96Seschrock * If vdev_validate() returns failure (indicated by 2957560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 2958560e6e96Seschrock * that the pool has been exported or destroyed. If 2959560e6e96Seschrock * this is the case, the config cache is out of sync and 2960560e6e96Seschrock * we should remove the pool from the namespace. 2961fa9e4066Sahrens */ 2962fa9e4066Sahrens spa_unload(spa); 2963fa9e4066Sahrens spa_deactivate(spa); 2964c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2965fa9e4066Sahrens spa_remove(spa); 2966fa9e4066Sahrens if (locked) 2967fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2968be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 2969ea8dc4b6Seschrock } 2970ea8dc4b6Seschrock 2971ea8dc4b6Seschrock if (error) { 2972fa9e4066Sahrens /* 2973fa9e4066Sahrens * We can't open the pool, but we still have useful 2974fa9e4066Sahrens * information: the state of each vdev after the 2975fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 2976fa9e4066Sahrens */ 29774b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 2978468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 2979468c413aSTim Haley KM_SLEEP) == 0); 29804b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 29814b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 29824b964adaSGeorge Wilson spa->spa_load_info) == 0); 29834b964adaSGeorge Wilson } 2984fa9e4066Sahrens spa_unload(spa); 2985fa9e4066Sahrens spa_deactivate(spa); 2986468c413aSTim Haley spa->spa_last_open_failed = error; 2987fa9e4066Sahrens if (locked) 2988fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2989fa9e4066Sahrens *spapp = NULL; 2990fa9e4066Sahrens return (error); 2991fa9e4066Sahrens } 2992fa9e4066Sahrens } 2993fa9e4066Sahrens 2994fa9e4066Sahrens spa_open_ref(spa, tag); 29953d7072f8Seschrock 2996468c413aSTim Haley if (config != NULL) 2997468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2998468c413aSTim Haley 29994b964adaSGeorge Wilson /* 30004b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 30014b964adaSGeorge Wilson * gathered while doing the load. 30024b964adaSGeorge Wilson */ 30034b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 30044b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 30054b964adaSGeorge Wilson spa->spa_load_info) == 0); 30064b964adaSGeorge Wilson } 30074b964adaSGeorge Wilson 3008a33cae98STim Haley if (locked) { 3009a33cae98STim Haley spa->spa_last_open_failed = 0; 3010a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3011a33cae98STim Haley spa->spa_load_txg = 0; 3012fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3013a33cae98STim Haley } 3014fa9e4066Sahrens 3015fa9e4066Sahrens *spapp = spa; 3016fa9e4066Sahrens 3017fa9e4066Sahrens return (0); 3018fa9e4066Sahrens } 3019fa9e4066Sahrens 3020468c413aSTim Haley int 3021468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3022468c413aSTim Haley nvlist_t **config) 3023468c413aSTim Haley { 3024468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3025468c413aSTim Haley } 3026468c413aSTim Haley 3027fa9e4066Sahrens int 3028fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3029fa9e4066Sahrens { 3030468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3031fa9e4066Sahrens } 3032fa9e4066Sahrens 3033ea8dc4b6Seschrock /* 3034ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3035ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3036ea8dc4b6Seschrock */ 3037ea8dc4b6Seschrock spa_t * 3038ea8dc4b6Seschrock spa_inject_addref(char *name) 3039ea8dc4b6Seschrock { 3040ea8dc4b6Seschrock spa_t *spa; 3041ea8dc4b6Seschrock 3042ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3043ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3044ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3045ea8dc4b6Seschrock return (NULL); 3046ea8dc4b6Seschrock } 3047ea8dc4b6Seschrock spa->spa_inject_ref++; 3048ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3049ea8dc4b6Seschrock 3050ea8dc4b6Seschrock return (spa); 3051ea8dc4b6Seschrock } 3052ea8dc4b6Seschrock 3053ea8dc4b6Seschrock void 3054ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3055ea8dc4b6Seschrock { 3056ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3057ea8dc4b6Seschrock spa->spa_inject_ref--; 3058ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3059ea8dc4b6Seschrock } 3060ea8dc4b6Seschrock 3061fa94a07fSbrendan /* 3062fa94a07fSbrendan * Add spares device information to the nvlist. 3063fa94a07fSbrendan */ 306499653d4eSeschrock static void 306599653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 306699653d4eSeschrock { 306799653d4eSeschrock nvlist_t **spares; 306899653d4eSeschrock uint_t i, nspares; 306999653d4eSeschrock nvlist_t *nvroot; 307099653d4eSeschrock uint64_t guid; 307199653d4eSeschrock vdev_stat_t *vs; 307299653d4eSeschrock uint_t vsc; 307339c23413Seschrock uint64_t pool; 307499653d4eSeschrock 30756809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 30766809eb4eSEric Schrock 3077fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 307899653d4eSeschrock return; 307999653d4eSeschrock 308099653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 308199653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3082fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 308399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 308499653d4eSeschrock if (nspares != 0) { 308599653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 308699653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 308799653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 308899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 308999653d4eSeschrock 309099653d4eSeschrock /* 309199653d4eSeschrock * Go through and find any spares which have since been 309299653d4eSeschrock * repurposed as an active spare. If this is the case, update 309399653d4eSeschrock * their status appropriately. 309499653d4eSeschrock */ 309599653d4eSeschrock for (i = 0; i < nspares; i++) { 309699653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 309799653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 309889a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 309989a89ebfSlling pool != 0ULL) { 310099653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 31013f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 310299653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 310399653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 310499653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 310599653d4eSeschrock } 310699653d4eSeschrock } 310799653d4eSeschrock } 310899653d4eSeschrock } 310999653d4eSeschrock 3110fa94a07fSbrendan /* 3111fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3112fa94a07fSbrendan */ 3113fa94a07fSbrendan static void 3114fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3115fa94a07fSbrendan { 3116fa94a07fSbrendan nvlist_t **l2cache; 3117fa94a07fSbrendan uint_t i, j, nl2cache; 3118fa94a07fSbrendan nvlist_t *nvroot; 3119fa94a07fSbrendan uint64_t guid; 3120fa94a07fSbrendan vdev_t *vd; 3121fa94a07fSbrendan vdev_stat_t *vs; 3122fa94a07fSbrendan uint_t vsc; 3123fa94a07fSbrendan 31246809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 31256809eb4eSEric Schrock 3126fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3127fa94a07fSbrendan return; 3128fa94a07fSbrendan 3129fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3130fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3131fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3132fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3133fa94a07fSbrendan if (nl2cache != 0) { 3134fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3135fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3136fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3137fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3138fa94a07fSbrendan 3139fa94a07fSbrendan /* 3140fa94a07fSbrendan * Update level 2 cache device stats. 3141fa94a07fSbrendan */ 3142fa94a07fSbrendan 3143fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3144fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3145fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3146fa94a07fSbrendan 3147fa94a07fSbrendan vd = NULL; 3148fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3149fa94a07fSbrendan if (guid == 3150fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3151fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3152fa94a07fSbrendan break; 3153fa94a07fSbrendan } 3154fa94a07fSbrendan } 3155fa94a07fSbrendan ASSERT(vd != NULL); 3156fa94a07fSbrendan 3157fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 31583f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 31593f9d6ad7SLin Ling == 0); 3160fa94a07fSbrendan vdev_get_stats(vd, vs); 3161fa94a07fSbrendan } 3162fa94a07fSbrendan } 3163fa94a07fSbrendan } 3164fa94a07fSbrendan 3165ad135b5dSChristopher Siden static void 3166ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3167ad135b5dSChristopher Siden { 3168ad135b5dSChristopher Siden nvlist_t *features; 3169ad135b5dSChristopher Siden zap_cursor_t zc; 3170ad135b5dSChristopher Siden zap_attribute_t za; 3171ad135b5dSChristopher Siden 3172ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3173ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3174ad135b5dSChristopher Siden 3175ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3176ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3177ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3178ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3179ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3180ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3181ad135b5dSChristopher Siden za.za_num_integers == 1); 3182b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3183ad135b5dSChristopher Siden za.za_first_integer)); 3184ad135b5dSChristopher Siden } 3185ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3186ad135b5dSChristopher Siden } 3187ad135b5dSChristopher Siden 3188ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3189ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3190ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3191ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3192ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3193ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3194ad135b5dSChristopher Siden za.za_num_integers == 1); 3195b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3196ad135b5dSChristopher Siden za.za_first_integer)); 3197ad135b5dSChristopher Siden } 3198ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3199ad135b5dSChristopher Siden } 3200ad135b5dSChristopher Siden 3201ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3202ad135b5dSChristopher Siden features) == 0); 3203ad135b5dSChristopher Siden nvlist_free(features); 3204ad135b5dSChristopher Siden } 3205ad135b5dSChristopher Siden 3206fa9e4066Sahrens int 3207ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3208ad135b5dSChristopher Siden char *altroot, size_t buflen) 3209fa9e4066Sahrens { 3210fa9e4066Sahrens int error; 3211fa9e4066Sahrens spa_t *spa; 3212fa9e4066Sahrens 3213fa9e4066Sahrens *config = NULL; 3214468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3215fa9e4066Sahrens 32166809eb4eSEric Schrock if (spa != NULL) { 32176809eb4eSEric Schrock /* 32186809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 32196809eb4eSEric Schrock * or l2cache devices could change and the config would be 32206809eb4eSEric Schrock * self-inconsistent. 32216809eb4eSEric Schrock */ 32226809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3223ea8dc4b6Seschrock 32246809eb4eSEric Schrock if (*config != NULL) { 322511027bc7STim Haley uint64_t loadtimes[2]; 322611027bc7STim Haley 322711027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 322811027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 322911027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 323011027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 323111027bc7STim Haley 3232e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 32336809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 32346809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3235e14bb325SJeff Bonwick 32366809eb4eSEric Schrock if (spa_suspended(spa)) 32376809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 32386809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 32396809eb4eSEric Schrock spa->spa_failmode) == 0); 32406809eb4eSEric Schrock 32416809eb4eSEric Schrock spa_add_spares(spa, *config); 32426809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3243ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 32446809eb4eSEric Schrock } 324599653d4eSeschrock } 324699653d4eSeschrock 3247ea8dc4b6Seschrock /* 3248ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3249ea8dc4b6Seschrock * and call spa_lookup() directly. 3250ea8dc4b6Seschrock */ 3251ea8dc4b6Seschrock if (altroot) { 3252ea8dc4b6Seschrock if (spa == NULL) { 3253ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3254ea8dc4b6Seschrock spa = spa_lookup(name); 3255ea8dc4b6Seschrock if (spa) 3256ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3257ea8dc4b6Seschrock else 3258ea8dc4b6Seschrock altroot[0] = '\0'; 3259ea8dc4b6Seschrock spa = NULL; 3260ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3261ea8dc4b6Seschrock } else { 3262ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3263ea8dc4b6Seschrock } 3264ea8dc4b6Seschrock } 3265ea8dc4b6Seschrock 32666809eb4eSEric Schrock if (spa != NULL) { 32676809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3268fa9e4066Sahrens spa_close(spa, FTAG); 32696809eb4eSEric Schrock } 3270fa9e4066Sahrens 3271fa9e4066Sahrens return (error); 3272fa9e4066Sahrens } 3273fa9e4066Sahrens 327499653d4eSeschrock /* 3275fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3276fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3277fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3278fa94a07fSbrendan * specified, as long as they are well-formed. 327999653d4eSeschrock */ 328099653d4eSeschrock static int 3281fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3282fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3283fa94a07fSbrendan vdev_labeltype_t label) 328499653d4eSeschrock { 3285fa94a07fSbrendan nvlist_t **dev; 3286fa94a07fSbrendan uint_t i, ndev; 328799653d4eSeschrock vdev_t *vd; 328899653d4eSeschrock int error; 328999653d4eSeschrock 3290e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3291e14bb325SJeff Bonwick 329299653d4eSeschrock /* 3293fa94a07fSbrendan * It's acceptable to have no devs specified. 329499653d4eSeschrock */ 3295fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 329699653d4eSeschrock return (0); 329799653d4eSeschrock 3298fa94a07fSbrendan if (ndev == 0) 3299be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 330099653d4eSeschrock 330199653d4eSeschrock /* 3302fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3303fa94a07fSbrendan * device type. 330499653d4eSeschrock */ 3305fa94a07fSbrendan if (spa_version(spa) < version) 3306be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 330799653d4eSeschrock 330839c23413Seschrock /* 3309fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 331039c23413Seschrock * checking. 331139c23413Seschrock */ 3312fa94a07fSbrendan sav->sav_pending = dev; 3313fa94a07fSbrendan sav->sav_npending = ndev; 331439c23413Seschrock 3315fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3316fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 331799653d4eSeschrock mode)) != 0) 331839c23413Seschrock goto out; 331999653d4eSeschrock 332099653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 332199653d4eSeschrock vdev_free(vd); 3322be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 332339c23413Seschrock goto out; 332499653d4eSeschrock } 332599653d4eSeschrock 3326fa94a07fSbrendan /* 3327e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3328e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3329fa94a07fSbrendan */ 3330e14bb325SJeff Bonwick #ifdef _KERNEL 3331fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3332fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3333be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 3334cd0837ccSGeorge Wilson vdev_free(vd); 3335fa94a07fSbrendan goto out; 3336fa94a07fSbrendan } 3337e14bb325SJeff Bonwick #endif 333899653d4eSeschrock vd->vdev_top = vd; 333999653d4eSeschrock 334039c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3341fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3342fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 334339c23413Seschrock vd->vdev_guid) == 0); 334439c23413Seschrock } 334599653d4eSeschrock 334699653d4eSeschrock vdev_free(vd); 334739c23413Seschrock 3348fa94a07fSbrendan if (error && 3349fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 335039c23413Seschrock goto out; 335139c23413Seschrock else 335239c23413Seschrock error = 0; 335399653d4eSeschrock } 335499653d4eSeschrock 335539c23413Seschrock out: 3356fa94a07fSbrendan sav->sav_pending = NULL; 3357fa94a07fSbrendan sav->sav_npending = 0; 335839c23413Seschrock return (error); 335999653d4eSeschrock } 336099653d4eSeschrock 3361fa94a07fSbrendan static int 3362fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3363fa94a07fSbrendan { 3364fa94a07fSbrendan int error; 3365fa94a07fSbrendan 3366e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3367e14bb325SJeff Bonwick 3368fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3369fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3370fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3371fa94a07fSbrendan return (error); 3372fa94a07fSbrendan } 3373fa94a07fSbrendan 3374fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3375fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3376fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3377fa94a07fSbrendan } 3378fa94a07fSbrendan 3379fa94a07fSbrendan static void 3380fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3381fa94a07fSbrendan const char *config) 3382fa94a07fSbrendan { 3383fa94a07fSbrendan int i; 3384fa94a07fSbrendan 3385fa94a07fSbrendan if (sav->sav_config != NULL) { 3386fa94a07fSbrendan nvlist_t **olddevs; 3387fa94a07fSbrendan uint_t oldndevs; 3388fa94a07fSbrendan nvlist_t **newdevs; 3389fa94a07fSbrendan 3390fa94a07fSbrendan /* 3391fa94a07fSbrendan * Generate new dev list by concatentating with the 3392fa94a07fSbrendan * current dev list. 3393fa94a07fSbrendan */ 3394fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3395fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3396fa94a07fSbrendan 3397fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3398fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3399fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3400fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3401fa94a07fSbrendan KM_SLEEP) == 0); 3402fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3403fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3404fa94a07fSbrendan KM_SLEEP) == 0); 3405fa94a07fSbrendan 3406fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3407fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3408fa94a07fSbrendan 3409fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3410fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3411fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3412fa94a07fSbrendan nvlist_free(newdevs[i]); 3413fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3414fa94a07fSbrendan } else { 3415fa94a07fSbrendan /* 3416fa94a07fSbrendan * Generate a new dev list. 3417fa94a07fSbrendan */ 3418fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3419fa94a07fSbrendan KM_SLEEP) == 0); 3420fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3421fa94a07fSbrendan devs, ndevs) == 0); 3422fa94a07fSbrendan } 3423fa94a07fSbrendan } 3424fa94a07fSbrendan 3425fa94a07fSbrendan /* 3426fa94a07fSbrendan * Stop and drop level 2 ARC devices 3427fa94a07fSbrendan */ 3428fa94a07fSbrendan void 3429fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3430fa94a07fSbrendan { 3431fa94a07fSbrendan vdev_t *vd; 3432fa94a07fSbrendan int i; 3433fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3434fa94a07fSbrendan 3435fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3436fa94a07fSbrendan uint64_t pool; 3437fa94a07fSbrendan 3438fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3439fa94a07fSbrendan ASSERT(vd != NULL); 3440fa94a07fSbrendan 34418ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 34428ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3443fa94a07fSbrendan l2arc_remove_vdev(vd); 3444fa94a07fSbrendan } 3445fa94a07fSbrendan } 3446fa94a07fSbrendan 3447fa9e4066Sahrens /* 3448fa9e4066Sahrens * Pool Creation 3449fa9e4066Sahrens */ 3450fa9e4066Sahrens int 3451990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 34524445fffbSMatthew Ahrens nvlist_t *zplprops) 3453fa9e4066Sahrens { 3454fa9e4066Sahrens spa_t *spa; 3455990b4856Slling char *altroot = NULL; 34560373e76bSbonwick vdev_t *rvd; 3457fa9e4066Sahrens dsl_pool_t *dp; 3458fa9e4066Sahrens dmu_tx_t *tx; 3459573ca77eSGeorge Wilson int error = 0; 3460fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3461fa94a07fSbrendan nvlist_t **spares, **l2cache; 3462fa94a07fSbrendan uint_t nspares, nl2cache; 3463cde58dbcSMatthew Ahrens uint64_t version, obj; 3464ad135b5dSChristopher Siden boolean_t has_features; 3465fa9e4066Sahrens 3466fa9e4066Sahrens /* 3467fa9e4066Sahrens * If this pool already exists, return failure. 3468fa9e4066Sahrens */ 3469fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3470fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3471fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3472be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 3473fa9e4066Sahrens } 3474fa9e4066Sahrens 3475fa9e4066Sahrens /* 3476fa9e4066Sahrens * Allocate a new spa_t structure. 3477fa9e4066Sahrens */ 3478990b4856Slling (void) nvlist_lookup_string(props, 3479990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3480468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 34818ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3482fa9e4066Sahrens 3483990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3484990b4856Slling spa_deactivate(spa); 3485990b4856Slling spa_remove(spa); 3486c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3487990b4856Slling return (error); 3488990b4856Slling } 3489990b4856Slling 3490ad135b5dSChristopher Siden has_features = B_FALSE; 3491ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3492ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3493ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3494ad135b5dSChristopher Siden has_features = B_TRUE; 3495ad135b5dSChristopher Siden } 3496ad135b5dSChristopher Siden 3497ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3498ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3499990b4856Slling version = SPA_VERSION; 3500ad135b5dSChristopher Siden } 3501ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3502b24ab676SJeff Bonwick 3503b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3504b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3505990b4856Slling spa->spa_uberblock.ub_version = version; 3506fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 3507fa9e4066Sahrens 350854d692b7SGeorge Wilson /* 350954d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 351054d692b7SGeorge Wilson */ 35116f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 35126f834bc1SMatthew Ahrens KM_SLEEP); 35136f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 35146f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 35156f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 35166f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 35176f834bc1SMatthew Ahrens } 351854d692b7SGeorge Wilson 35190373e76bSbonwick /* 35200373e76bSbonwick * Create the root vdev. 35210373e76bSbonwick */ 3522e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 35230373e76bSbonwick 352499653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 35250373e76bSbonwick 352699653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 352799653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 35280373e76bSbonwick 3529b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 3530be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 353199653d4eSeschrock 353299653d4eSeschrock if (error == 0 && 353399653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3534fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 353599653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3536573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3537573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3538573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3539573ca77eSGeorge Wilson } 35400373e76bSbonwick } 35410373e76bSbonwick 3542e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3543fa9e4066Sahrens 354499653d4eSeschrock if (error != 0) { 3545fa9e4066Sahrens spa_unload(spa); 3546fa9e4066Sahrens spa_deactivate(spa); 3547fa9e4066Sahrens spa_remove(spa); 3548fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3549fa9e4066Sahrens return (error); 3550fa9e4066Sahrens } 3551fa9e4066Sahrens 355299653d4eSeschrock /* 355399653d4eSeschrock * Get the list of spares, if specified. 355499653d4eSeschrock */ 355599653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 355699653d4eSeschrock &spares, &nspares) == 0) { 3557fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 355899653d4eSeschrock KM_SLEEP) == 0); 3559fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 356099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3561e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 356299653d4eSeschrock spa_load_spares(spa); 3563e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3564fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3565fa94a07fSbrendan } 3566fa94a07fSbrendan 3567fa94a07fSbrendan /* 3568fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3569fa94a07fSbrendan */ 3570fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3571fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3572fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3573fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3574fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3575fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3576e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3577fa94a07fSbrendan spa_load_l2cache(spa); 3578e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3579fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 358099653d4eSeschrock } 358199653d4eSeschrock 3582ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 35830a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3584fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3585ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3586fa9e4066Sahrens 3587485bbbf5SGeorge Wilson /* 3588485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3589485bbbf5SGeorge Wilson */ 3590485bbbf5SGeorge Wilson ddt_create(spa); 3591485bbbf5SGeorge Wilson 3592485bbbf5SGeorge Wilson spa_update_dspace(spa); 3593485bbbf5SGeorge Wilson 3594fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3595fa9e4066Sahrens 3596fa9e4066Sahrens /* 3597fa9e4066Sahrens * Create the pool config object. 3598fa9e4066Sahrens */ 3599fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3600f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3601fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3602fa9e4066Sahrens 3603ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3604fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3605ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3606ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3607ea8dc4b6Seschrock } 3608fa9e4066Sahrens 3609ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3610ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3611ad135b5dSChristopher Siden 36123f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 36133f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 36143f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 36153f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 36163f9d6ad7SLin Ling } 36173f9d6ad7SLin Ling 3618990b4856Slling /* Newly created pools with the right version are always deflated. */ 3619990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3620990b4856Slling spa->spa_deflate = TRUE; 3621990b4856Slling if (zap_add(spa->spa_meta_objset, 3622990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3623990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3624990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3625990b4856Slling } 362699653d4eSeschrock } 362799653d4eSeschrock 3628fa9e4066Sahrens /* 3629cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3630fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3631fa9e4066Sahrens * keeps changing. 3632fa9e4066Sahrens */ 3633cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3634cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3635cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3636ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3637cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3638cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3639cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3640ea8dc4b6Seschrock } 3641b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3642cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3643fa9e4066Sahrens 364406eeb2adSek /* 364506eeb2adSek * Create the pool's history object. 364606eeb2adSek */ 3647990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3648990b4856Slling spa_history_create_obj(spa, tx); 3649990b4856Slling 3650990b4856Slling /* 3651990b4856Slling * Set pool properties. 3652990b4856Slling */ 3653990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3654990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 36550a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3656573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3657b24ab676SJeff Bonwick 3658379c004dSEric Schrock if (props != NULL) { 3659379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 36603b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3661379c004dSEric Schrock } 366206eeb2adSek 3663fa9e4066Sahrens dmu_tx_commit(tx); 3664fa9e4066Sahrens 3665fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3666fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3667fa9e4066Sahrens 3668fa9e4066Sahrens /* 3669fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3670fa9e4066Sahrens * bean counters are appropriately updated. 3671fa9e4066Sahrens */ 3672fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3673fa9e4066Sahrens 3674c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3675fa9e4066Sahrens 36764445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3677228975ccSek 3678088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 3679088f3894Sahrens 3680daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3681daaa36a7SGeorge Wilson 3682fa9e4066Sahrens return (0); 3683fa9e4066Sahrens } 3684fa9e4066Sahrens 3685e7cbe64fSgw #ifdef _KERNEL 3686e7cbe64fSgw /* 368721ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 368821ecdf64SLin Ling * during the system boot up time. 3689e7cbe64fSgw */ 369021ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 369121ecdf64SLin Ling 369221ecdf64SLin Ling static nvlist_t * 369321ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3694e7cbe64fSgw { 369521ecdf64SLin Ling nvlist_t *config; 3696e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3697e7cbe64fSgw uint64_t pgid; 3698e7cbe64fSgw 369921ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 370021ecdf64SLin Ling return (NULL); 370121ecdf64SLin Ling 3702e7cbe64fSgw /* 3703e7cbe64fSgw * Add this top-level vdev to the child array. 3704e7cbe64fSgw */ 370521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 370621ecdf64SLin Ling &nvtop) == 0); 370721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 370821ecdf64SLin Ling &pgid) == 0); 370921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3710e7cbe64fSgw 3711e7cbe64fSgw /* 3712e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3713e7cbe64fSgw */ 3714e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 371521ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 371621ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3717e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3718e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3719e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3720e7cbe64fSgw &nvtop, 1) == 0); 3721e7cbe64fSgw 3722e7cbe64fSgw /* 3723e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3724e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3725e7cbe64fSgw */ 3726e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3727e7cbe64fSgw nvlist_free(nvroot); 372821ecdf64SLin Ling return (config); 3729e7cbe64fSgw } 3730e7cbe64fSgw 3731e7cbe64fSgw /* 373221ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 373321ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 373421ecdf64SLin Ling * device has a more recent txg. 3735051aabe6Staylor */ 373621ecdf64SLin Ling static void 373721ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3738051aabe6Staylor { 3739573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 374021ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3741051aabe6Staylor 374221ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 374321ecdf64SLin Ling nvlist_t *label; 374421ecdf64SLin Ling uint64_t label_txg; 3745051aabe6Staylor 374621ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 374721ecdf64SLin Ling &label) != 0) 374821ecdf64SLin Ling return; 3749051aabe6Staylor 375021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 375121ecdf64SLin Ling &label_txg) == 0); 3752051aabe6Staylor 375321ecdf64SLin Ling /* 375421ecdf64SLin Ling * Do we have a better boot device? 375521ecdf64SLin Ling */ 375621ecdf64SLin Ling if (label_txg > *txg) { 375721ecdf64SLin Ling *txg = label_txg; 375821ecdf64SLin Ling *avd = vd; 3759051aabe6Staylor } 376021ecdf64SLin Ling nvlist_free(label); 3761051aabe6Staylor } 3762051aabe6Staylor } 3763051aabe6Staylor 3764e7cbe64fSgw /* 3765e7cbe64fSgw * Import a root pool. 3766e7cbe64fSgw * 3767051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3768051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3769051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3770e7cbe64fSgw * 3771e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3772e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3773e7cbe64fSgw * e.g. 3774e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3775e7cbe64fSgw */ 3776e7cbe64fSgw int 3777051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3778e7cbe64fSgw { 377921ecdf64SLin Ling spa_t *spa; 378021ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 378121ecdf64SLin Ling nvlist_t *config, *nvtop; 378221ecdf64SLin Ling uint64_t guid, txg; 3783e7cbe64fSgw char *pname; 3784e7cbe64fSgw int error; 3785e7cbe64fSgw 3786e7cbe64fSgw /* 378721ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3788e7cbe64fSgw */ 3789dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3790dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3791dedec472SJack Meng if (config == NULL) { 3792dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3793dedec472SJack Meng /* iscsi boot */ 3794dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3795dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3796dedec472SJack Meng } 3797dedec472SJack Meng } 3798dedec472SJack Meng #endif 3799dedec472SJack Meng if (config == NULL) { 3800ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 380121ecdf64SLin Ling devpath); 3802be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 380321ecdf64SLin Ling } 3804e7cbe64fSgw 380521ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 380621ecdf64SLin Ling &pname) == 0); 380721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3808e7cbe64fSgw 38096809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 38106809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 38116809eb4eSEric Schrock /* 38126809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 38136809eb4eSEric Schrock * can replace it with the correct config we just read in. 38146809eb4eSEric Schrock */ 38156809eb4eSEric Schrock spa_remove(spa); 38166809eb4eSEric Schrock } 38176809eb4eSEric Schrock 3818468c413aSTim Haley spa = spa_add(pname, config, NULL); 38196809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 38204b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3821e7cbe64fSgw 382221ecdf64SLin Ling /* 382321ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 382421ecdf64SLin Ling */ 382521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 382621ecdf64SLin Ling &nvtop) == 0); 382721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 382821ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 382921ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 383021ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 383121ecdf64SLin Ling if (error) { 383221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 383321ecdf64SLin Ling nvlist_free(config); 383421ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 383521ecdf64SLin Ling pname); 383621ecdf64SLin Ling return (error); 383721ecdf64SLin Ling } 383821ecdf64SLin Ling 383921ecdf64SLin Ling /* 384021ecdf64SLin Ling * Get the boot vdev. 384121ecdf64SLin Ling */ 384221ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 384321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 384421ecdf64SLin Ling (u_longlong_t)guid); 3845be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 384621ecdf64SLin Ling goto out; 384721ecdf64SLin Ling } 3848e7cbe64fSgw 384921ecdf64SLin Ling /* 385021ecdf64SLin Ling * Determine if there is a better boot device. 385121ecdf64SLin Ling */ 385221ecdf64SLin Ling avd = bvd; 385321ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 385421ecdf64SLin Ling if (avd != bvd) { 385521ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 385621ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 3857be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 385821ecdf64SLin Ling goto out; 385921ecdf64SLin Ling } 3860e7cbe64fSgw 386121ecdf64SLin Ling /* 386221ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 386321ecdf64SLin Ling * we're booting off the active spare. 386421ecdf64SLin Ling */ 386521ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 386621ecdf64SLin Ling !bvd->vdev_isspare) { 386721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 386821ecdf64SLin Ling "try booting from '%s'", 3869cb04b873SMark J Musante bvd->vdev_parent-> 3870cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 3871be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 387221ecdf64SLin Ling goto out; 387321ecdf64SLin Ling } 387421ecdf64SLin Ling 387521ecdf64SLin Ling error = 0; 387621ecdf64SLin Ling out: 387721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 387821ecdf64SLin Ling vdev_free(rvd); 387921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 388021ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 388121ecdf64SLin Ling 388221ecdf64SLin Ling nvlist_free(config); 3883e7cbe64fSgw return (error); 3884e7cbe64fSgw } 388521ecdf64SLin Ling 3886e7cbe64fSgw #endif 3887e7cbe64fSgw 38886809eb4eSEric Schrock /* 38896809eb4eSEric Schrock * Import a non-root pool into the system. 38906809eb4eSEric Schrock */ 3891c5904d13Seschrock int 38924b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 3893c5904d13Seschrock { 38946809eb4eSEric Schrock spa_t *spa; 38956809eb4eSEric Schrock char *altroot = NULL; 3896468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 3897468c413aSTim Haley zpool_rewind_policy_t policy; 3898f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 3899f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 39006809eb4eSEric Schrock int error; 39016809eb4eSEric Schrock nvlist_t *nvroot; 39026809eb4eSEric Schrock nvlist_t **spares, **l2cache; 39036809eb4eSEric Schrock uint_t nspares, nl2cache; 39046809eb4eSEric Schrock 39056809eb4eSEric Schrock /* 39066809eb4eSEric Schrock * If a pool with this name exists, return failure. 39076809eb4eSEric Schrock */ 39086809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 39091195e687SMark J Musante if (spa_lookup(pool) != NULL) { 39106809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 3911be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 39126809eb4eSEric Schrock } 39136809eb4eSEric Schrock 39146809eb4eSEric Schrock /* 39156809eb4eSEric Schrock * Create and initialize the spa structure. 39166809eb4eSEric Schrock */ 39176809eb4eSEric Schrock (void) nvlist_lookup_string(props, 39186809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3919f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 3920f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3921f9af39baSGeorge Wilson if (readonly) 3922f9af39baSGeorge Wilson mode = FREAD; 3923468c413aSTim Haley spa = spa_add(pool, config, altroot); 39244b964adaSGeorge Wilson spa->spa_import_flags = flags; 39254b964adaSGeorge Wilson 39264b964adaSGeorge Wilson /* 39274b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 39284b964adaSGeorge Wilson * as if it had been loaded at boot. 39294b964adaSGeorge Wilson */ 39304b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 39314b964adaSGeorge Wilson if (props != NULL) 39324b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 39334b964adaSGeorge Wilson 39344b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 39354b964adaSGeorge Wilson 39364b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 39374b964adaSGeorge Wilson return (0); 39384b964adaSGeorge Wilson } 39394b964adaSGeorge Wilson 3940f9af39baSGeorge Wilson spa_activate(spa, mode); 39416809eb4eSEric Schrock 394225f89ee2SJeff Bonwick /* 394325f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 394425f89ee2SJeff Bonwick */ 394525f89ee2SJeff Bonwick spa_async_suspend(spa); 394625f89ee2SJeff Bonwick 39474b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 39484b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 39494b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 39504b964adaSGeorge Wilson 39516809eb4eSEric Schrock /* 39526809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 39536809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 39546809eb4eSEric Schrock * doing an import. 39556809eb4eSEric Schrock */ 3956468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3957468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 39584b964adaSGeorge Wilson 3959468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 3960c8ee1847SVictor Latushkin policy.zrp_request); 3961468c413aSTim Haley 3962468c413aSTim Haley /* 39634b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 39644b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 3965468c413aSTim Haley */ 39664b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 39674b964adaSGeorge Wilson spa->spa_load_info) == 0); 39686809eb4eSEric Schrock 39696809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 39706809eb4eSEric Schrock /* 39716809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 39726809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 39736809eb4eSEric Schrock */ 39746809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 39756809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 39766809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 39776809eb4eSEric Schrock spa_load_spares(spa); 39786809eb4eSEric Schrock } 39796809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 39806809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 39816809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 39826809eb4eSEric Schrock spa_load_l2cache(spa); 39836809eb4eSEric Schrock } 39846809eb4eSEric Schrock 39856809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 39866809eb4eSEric Schrock &nvroot) == 0); 39876809eb4eSEric Schrock if (error == 0) 39886809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 39896809eb4eSEric Schrock VDEV_ALLOC_SPARE); 39906809eb4eSEric Schrock if (error == 0) 39916809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 39926809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 39936809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 39946809eb4eSEric Schrock 39956809eb4eSEric Schrock if (props != NULL) 39966809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 39976809eb4eSEric Schrock 39986809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 39996809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 40006809eb4eSEric Schrock spa_unload(spa); 40016809eb4eSEric Schrock spa_deactivate(spa); 40026809eb4eSEric Schrock spa_remove(spa); 40036809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 40046809eb4eSEric Schrock return (error); 40056809eb4eSEric Schrock } 40066809eb4eSEric Schrock 4007955ef359SLin Ling spa_async_resume(spa); 4008955ef359SLin Ling 40096809eb4eSEric Schrock /* 40106809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 40116809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 40126809eb4eSEric Schrock */ 40136809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 40146809eb4eSEric Schrock &spares, &nspares) == 0) { 40156809eb4eSEric Schrock if (spa->spa_spares.sav_config) 40166809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 40176809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 40186809eb4eSEric Schrock else 40196809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 40206809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 40216809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 40226809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 40236809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 40246809eb4eSEric Schrock spa_load_spares(spa); 40256809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 40266809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 40276809eb4eSEric Schrock } 40286809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 40296809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 40306809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 40316809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 40326809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 40336809eb4eSEric Schrock else 40346809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 40356809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 40366809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 40376809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 40386809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 40396809eb4eSEric Schrock spa_load_l2cache(spa); 40406809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 40416809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 40426809eb4eSEric Schrock } 40436809eb4eSEric Schrock 4044b693757aSEric Schrock /* 4045b693757aSEric Schrock * Check for any removed devices. 4046b693757aSEric Schrock */ 4047b693757aSEric Schrock if (spa->spa_autoreplace) { 4048b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4049b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4050b693757aSEric Schrock } 4051b693757aSEric Schrock 40526809eb4eSEric Schrock if (spa_writeable(spa)) { 40536809eb4eSEric Schrock /* 40546809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 40556809eb4eSEric Schrock */ 4056bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 40576809eb4eSEric Schrock } 40586809eb4eSEric Schrock 4059573ca77eSGeorge Wilson /* 4060573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4061573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4062573ca77eSGeorge Wilson */ 4063573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4064573ca77eSGeorge Wilson 40656809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 40664445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 40676809eb4eSEric Schrock 40686809eb4eSEric Schrock return (0); 4069c5904d13Seschrock } 4070c5904d13Seschrock 4071fa9e4066Sahrens nvlist_t * 4072fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4073fa9e4066Sahrens { 4074fa9e4066Sahrens nvlist_t *config = NULL; 4075fa9e4066Sahrens char *poolname; 4076fa9e4066Sahrens spa_t *spa; 4077fa9e4066Sahrens uint64_t state; 40787b7154beSLin Ling int error; 4079fa9e4066Sahrens 4080fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4081fa9e4066Sahrens return (NULL); 4082fa9e4066Sahrens 4083fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4084fa9e4066Sahrens return (NULL); 4085fa9e4066Sahrens 4086fa9e4066Sahrens /* 40870373e76bSbonwick * Create and initialize the spa structure. 4088fa9e4066Sahrens */ 40890373e76bSbonwick mutex_enter(&spa_namespace_lock); 4090468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 40918ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4092fa9e4066Sahrens 4093fa9e4066Sahrens /* 40940373e76bSbonwick * Pass off the heavy lifting to spa_load(). 4095ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 4096ecc2d604Sbonwick * is actually the one to trust when doing an import. 4097fa9e4066Sahrens */ 40981195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 4099fa9e4066Sahrens 4100fa9e4066Sahrens /* 4101fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4102fa9e4066Sahrens */ 4103fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4104fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4105fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4106fa9e4066Sahrens poolname) == 0); 4107fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4108fa9e4066Sahrens state) == 0); 410995173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 411095173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4111ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4112ad135b5dSChristopher Siden spa->spa_load_info) == 0); 411399653d4eSeschrock 4114e7cbe64fSgw /* 4115e7cbe64fSgw * If the bootfs property exists on this pool then we 4116e7cbe64fSgw * copy it out so that external consumers can tell which 4117e7cbe64fSgw * pools are bootable. 4118e7cbe64fSgw */ 41197b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4120e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4121e7cbe64fSgw 4122e7cbe64fSgw /* 4123e7cbe64fSgw * We have to play games with the name since the 4124e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4125e7cbe64fSgw */ 4126e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4127e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4128e7cbe64fSgw char *cp; 4129e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4130e7cbe64fSgw 4131e7cbe64fSgw cp = strchr(tmpname, '/'); 4132e7cbe64fSgw if (cp == NULL) { 4133e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4134e7cbe64fSgw MAXPATHLEN); 4135e7cbe64fSgw } else { 4136e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4137e7cbe64fSgw "%s/%s", poolname, ++cp); 4138e7cbe64fSgw } 4139e7cbe64fSgw VERIFY(nvlist_add_string(config, 4140e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4141e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4142e7cbe64fSgw } 4143e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4144e7cbe64fSgw } 4145e7cbe64fSgw 414699653d4eSeschrock /* 4147fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 414899653d4eSeschrock */ 41496809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 415099653d4eSeschrock spa_add_spares(spa, config); 4151fa94a07fSbrendan spa_add_l2cache(spa, config); 41526809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4153fa9e4066Sahrens } 4154fa9e4066Sahrens 4155fa9e4066Sahrens spa_unload(spa); 4156fa9e4066Sahrens spa_deactivate(spa); 4157fa9e4066Sahrens spa_remove(spa); 4158fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4159fa9e4066Sahrens 4160fa9e4066Sahrens return (config); 4161fa9e4066Sahrens } 4162fa9e4066Sahrens 4163fa9e4066Sahrens /* 4164fa9e4066Sahrens * Pool export/destroy 4165fa9e4066Sahrens * 4166fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4167fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4168fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4169394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4170394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4171fa9e4066Sahrens */ 4172fa9e4066Sahrens static int 417389a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4174394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4175fa9e4066Sahrens { 4176fa9e4066Sahrens spa_t *spa; 4177fa9e4066Sahrens 417844cd46caSbillm if (oldconfig) 417944cd46caSbillm *oldconfig = NULL; 418044cd46caSbillm 41818ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4182be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 4183fa9e4066Sahrens 4184fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4185fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4186fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4187be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4188fa9e4066Sahrens } 4189fa9e4066Sahrens 4190ea8dc4b6Seschrock /* 4191ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4192ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4193ea8dc4b6Seschrock */ 4194ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4195ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4196ea8dc4b6Seschrock spa_async_suspend(spa); 4197ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4198ea8dc4b6Seschrock spa_close(spa, FTAG); 4199ea8dc4b6Seschrock 4200fa9e4066Sahrens /* 4201fa9e4066Sahrens * The pool will be in core if it's openable, 4202fa9e4066Sahrens * in which case we can modify its state. 4203fa9e4066Sahrens */ 4204fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4205fa9e4066Sahrens /* 4206fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4207fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4208fa9e4066Sahrens */ 4209fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4210fa9e4066Sahrens 4211ea8dc4b6Seschrock /* 4212ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4213ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4214ea8dc4b6Seschrock * fault injection handlers. 4215ea8dc4b6Seschrock */ 4216ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4217ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4218ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4219ea8dc4b6Seschrock spa_async_resume(spa); 4220fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4221be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 4222fa9e4066Sahrens } 4223fa9e4066Sahrens 422489a89ebfSlling /* 422589a89ebfSlling * A pool cannot be exported if it has an active shared spare. 422689a89ebfSlling * This is to prevent other pools stealing the active spare 422789a89ebfSlling * from an exported pool. At user's own will, such pool can 422889a89ebfSlling * be forcedly exported. 422989a89ebfSlling */ 423089a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 423189a89ebfSlling spa_has_active_shared_spare(spa)) { 423289a89ebfSlling spa_async_resume(spa); 423389a89ebfSlling mutex_exit(&spa_namespace_lock); 4234be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 423589a89ebfSlling } 423689a89ebfSlling 4237fa9e4066Sahrens /* 4238fa9e4066Sahrens * We want this to be reflected on every label, 4239fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4240fa9e4066Sahrens * final sync that pushes these changes out. 4241fa9e4066Sahrens */ 4242394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4243e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4244ea8dc4b6Seschrock spa->spa_state = new_state; 42453f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 42463f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4247ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4248e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4249ea8dc4b6Seschrock } 4250fa9e4066Sahrens } 4251fa9e4066Sahrens 42523d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 42533d7072f8Seschrock 4254fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4255fa9e4066Sahrens spa_unload(spa); 4256fa9e4066Sahrens spa_deactivate(spa); 4257fa9e4066Sahrens } 4258fa9e4066Sahrens 425944cd46caSbillm if (oldconfig && spa->spa_config) 426044cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 426144cd46caSbillm 4262ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4263394ab0cbSGeorge Wilson if (!hardforce) 4264394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4265ea8dc4b6Seschrock spa_remove(spa); 4266ea8dc4b6Seschrock } 4267fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4268fa9e4066Sahrens 4269fa9e4066Sahrens return (0); 4270fa9e4066Sahrens } 4271fa9e4066Sahrens 4272fa9e4066Sahrens /* 4273fa9e4066Sahrens * Destroy a storage pool. 4274fa9e4066Sahrens */ 4275fa9e4066Sahrens int 4276fa9e4066Sahrens spa_destroy(char *pool) 4277fa9e4066Sahrens { 4278394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4279394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4280fa9e4066Sahrens } 4281fa9e4066Sahrens 4282fa9e4066Sahrens /* 4283fa9e4066Sahrens * Export a storage pool. 4284fa9e4066Sahrens */ 4285fa9e4066Sahrens int 4286394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4287394ab0cbSGeorge Wilson boolean_t hardforce) 4288fa9e4066Sahrens { 4289394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4290394ab0cbSGeorge Wilson force, hardforce)); 4291fa9e4066Sahrens } 4292fa9e4066Sahrens 4293ea8dc4b6Seschrock /* 4294ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4295ea8dc4b6Seschrock * from the namespace in any way. 4296ea8dc4b6Seschrock */ 4297ea8dc4b6Seschrock int 4298ea8dc4b6Seschrock spa_reset(char *pool) 4299ea8dc4b6Seschrock { 430089a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4301394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4302ea8dc4b6Seschrock } 4303ea8dc4b6Seschrock 4304fa9e4066Sahrens /* 4305fa9e4066Sahrens * ========================================================================== 4306fa9e4066Sahrens * Device manipulation 4307fa9e4066Sahrens * ========================================================================== 4308fa9e4066Sahrens */ 4309fa9e4066Sahrens 4310fa9e4066Sahrens /* 43118654d025Sperrin * Add a device to a storage pool. 4312fa9e4066Sahrens */ 4313fa9e4066Sahrens int 4314fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4315fa9e4066Sahrens { 431688ecc943SGeorge Wilson uint64_t txg, id; 43178ad4d6ddSJeff Bonwick int error; 4318fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 43190e34b6a7Sbonwick vdev_t *vd, *tvd; 4320fa94a07fSbrendan nvlist_t **spares, **l2cache; 4321fa94a07fSbrendan uint_t nspares, nl2cache; 4322fa9e4066Sahrens 4323f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4324f9af39baSGeorge Wilson 4325fa9e4066Sahrens txg = spa_vdev_enter(spa); 4326fa9e4066Sahrens 432799653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 432899653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 432999653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4330fa9e4066Sahrens 4331e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 433299653d4eSeschrock 4333fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4334fa94a07fSbrendan &nspares) != 0) 433599653d4eSeschrock nspares = 0; 433699653d4eSeschrock 4337fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4338fa94a07fSbrendan &nl2cache) != 0) 4339fa94a07fSbrendan nl2cache = 0; 4340fa94a07fSbrendan 4341e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4342fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4343fa9e4066Sahrens 4344e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4345e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4346e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 434799653d4eSeschrock 434839c23413Seschrock /* 4349fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4350fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 435139c23413Seschrock */ 4352e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 435339c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 435439c23413Seschrock 435539c23413Seschrock /* 435639c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 435739c23413Seschrock */ 43588ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 435988ecc943SGeorge Wilson 436088ecc943SGeorge Wilson /* 436188ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 436288ecc943SGeorge Wilson */ 436388ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 436488ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 436588ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 436688ecc943SGeorge Wilson break; 436788ecc943SGeorge Wilson } 436888ecc943SGeorge Wilson } 436939c23413Seschrock tvd = vd->vdev_child[c]; 437039c23413Seschrock vdev_remove_child(vd, tvd); 437188ecc943SGeorge Wilson tvd->vdev_id = id; 437239c23413Seschrock vdev_add_child(rvd, tvd); 437339c23413Seschrock vdev_config_dirty(tvd); 437439c23413Seschrock } 437539c23413Seschrock 437699653d4eSeschrock if (nspares != 0) { 4377fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4378fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 437999653d4eSeschrock spa_load_spares(spa); 4380fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4381fa94a07fSbrendan } 4382fa94a07fSbrendan 4383fa94a07fSbrendan if (nl2cache != 0) { 4384fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4385fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4386fa94a07fSbrendan spa_load_l2cache(spa); 4387fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4388fa9e4066Sahrens } 4389fa9e4066Sahrens 4390fa9e4066Sahrens /* 43910e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 43920e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 43930e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 43940e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 43950e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 43960e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 43970373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 43980e34b6a7Sbonwick * 43990e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 44000e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 44010e34b6a7Sbonwick * steps will be completed the next time we load the pool. 44020e34b6a7Sbonwick */ 44030373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 44040e34b6a7Sbonwick 44050373e76bSbonwick mutex_enter(&spa_namespace_lock); 44060373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 44070373e76bSbonwick mutex_exit(&spa_namespace_lock); 4408fa9e4066Sahrens 44090373e76bSbonwick return (0); 4410fa9e4066Sahrens } 4411fa9e4066Sahrens 4412fa9e4066Sahrens /* 4413fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4414fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4415fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4416fa9e4066Sahrens * 4417fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4418fa9e4066Sahrens * existing device; in this case the two devices are made into their own 44193d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4420fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4421fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4422fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4423fa9e4066Sahrens * is automatically detached. 4424fa9e4066Sahrens */ 4425fa9e4066Sahrens int 4426ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4427fa9e4066Sahrens { 44283f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4429fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4430fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 443199653d4eSeschrock vdev_ops_t *pvops; 44329b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 44339b3f6b42SEric Kustarz int newvd_isspare; 44349b3f6b42SEric Kustarz int error; 4435fa9e4066Sahrens 4436f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4437f9af39baSGeorge Wilson 4438fa9e4066Sahrens txg = spa_vdev_enter(spa); 4439fa9e4066Sahrens 4440c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4441fa9e4066Sahrens 4442fa9e4066Sahrens if (oldvd == NULL) 4443fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4444fa9e4066Sahrens 44450e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 44460e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 44470e34b6a7Sbonwick 4448fa9e4066Sahrens pvd = oldvd->vdev_parent; 4449fa9e4066Sahrens 445099653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4451cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 44523d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 44533d7072f8Seschrock 44543d7072f8Seschrock if (newrootvd->vdev_children != 1) 4455fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4456fa9e4066Sahrens 4457fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4458fa9e4066Sahrens 4459fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4460fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4461fa9e4066Sahrens 446299653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4463fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4464fa9e4066Sahrens 44658654d025Sperrin /* 44668654d025Sperrin * Spares can't replace logs 44678654d025Sperrin */ 4468ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 44698654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 44708654d025Sperrin 447199653d4eSeschrock if (!replacing) { 447299653d4eSeschrock /* 447399653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 447499653d4eSeschrock * vdev. 447599653d4eSeschrock */ 447699653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 447799653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 447899653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 447999653d4eSeschrock 448099653d4eSeschrock pvops = &vdev_mirror_ops; 448199653d4eSeschrock } else { 448299653d4eSeschrock /* 448399653d4eSeschrock * Active hot spares can only be replaced by inactive hot 448499653d4eSeschrock * spares. 448599653d4eSeschrock */ 448699653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4487cb04b873SMark J Musante oldvd->vdev_isspare && 448899653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 448999653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 449099653d4eSeschrock 449199653d4eSeschrock /* 449299653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 449399653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 449439c23413Seschrock * want to create a replacing vdev. The user is not allowed to 449539c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 449639c23413Seschrock * the same (spare replaces spare, non-spare replaces 449739c23413Seschrock * non-spare). 449899653d4eSeschrock */ 4499cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4500cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 450199653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4502cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4503cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 450439c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4505cb04b873SMark J Musante } 4506cb04b873SMark J Musante 4507cb04b873SMark J Musante if (newvd->vdev_isspare) 450899653d4eSeschrock pvops = &vdev_spare_ops; 450999653d4eSeschrock else 451099653d4eSeschrock pvops = &vdev_replacing_ops; 451199653d4eSeschrock } 451299653d4eSeschrock 45132a79c5feSlling /* 4514573ca77eSGeorge Wilson * Make sure the new device is big enough. 45152a79c5feSlling */ 4516573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4517fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4518fa9e4066Sahrens 4519ecc2d604Sbonwick /* 4520ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4521ecc2d604Sbonwick * than the top-level vdev. 4522ecc2d604Sbonwick */ 4523ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4524fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4525fa9e4066Sahrens 4526fa9e4066Sahrens /* 4527fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4528fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4529fa9e4066Sahrens */ 4530fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4531fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4532fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4533fa9e4066Sahrens KM_SLEEP); 4534fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4535fa9e4066Sahrens newvd->vdev_path, "old"); 4536fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4537fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4538fa9e4066Sahrens oldvd->vdev_devid = NULL; 4539fa9e4066Sahrens } 4540fa9e4066Sahrens } 4541fa9e4066Sahrens 4542cb04b873SMark J Musante /* mark the device being resilvered */ 4543b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 4544cb04b873SMark J Musante 4545fa9e4066Sahrens /* 454699653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 454799653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4548fa9e4066Sahrens */ 4549fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4550fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4551fa9e4066Sahrens 4552fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4553fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4554fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4555fa9e4066Sahrens 4556fa9e4066Sahrens /* 4557fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4558fa9e4066Sahrens */ 4559fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4560fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 456188ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4562fa9e4066Sahrens vdev_add_child(pvd, newvd); 4563fa9e4066Sahrens 4564fa9e4066Sahrens tvd = newvd->vdev_top; 4565fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4566fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4567fa9e4066Sahrens 4568fa9e4066Sahrens vdev_config_dirty(tvd); 4569fa9e4066Sahrens 4570fa9e4066Sahrens /* 45713f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 45723f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 45733f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4574fa9e4066Sahrens */ 45753f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4576fa9e4066Sahrens 45773f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 45783f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4579fa9e4066Sahrens 45806809eb4eSEric Schrock if (newvd->vdev_isspare) { 458139c23413Seschrock spa_spare_activate(newvd); 45826809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 45836809eb4eSEric Schrock } 45846809eb4eSEric Schrock 4585e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4586e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 45879b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4588ea8dc4b6Seschrock 4589fa9e4066Sahrens /* 4590fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4591fa9e4066Sahrens */ 4592ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4593fa9e4066Sahrens 45943f9d6ad7SLin Ling /* 45950713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 45960713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 45970713e232SGeorge Wilson * respective datasets. 45983f9d6ad7SLin Ling */ 45993f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 46003f9d6ad7SLin Ling 46013f9d6ad7SLin Ling /* 46023f9d6ad7SLin Ling * Commit the config 46033f9d6ad7SLin Ling */ 46043f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4605fa9e4066Sahrens 46064445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 46073f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4608c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4609c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4610c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 46119b3f6b42SEric Kustarz 46129b3f6b42SEric Kustarz spa_strfree(oldvdpath); 46139b3f6b42SEric Kustarz spa_strfree(newvdpath); 46149b3f6b42SEric Kustarz 4615943e9869SLori Alt if (spa->spa_bootfs) 4616943e9869SLori Alt spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 4617943e9869SLori Alt 4618fa9e4066Sahrens return (0); 4619fa9e4066Sahrens } 4620fa9e4066Sahrens 4621fa9e4066Sahrens /* 4622fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4623f7170741SWill Andrews * 4624fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4625fa9e4066Sahrens * is a replacing vdev. 4626fa9e4066Sahrens */ 4627fa9e4066Sahrens int 46288ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4629fa9e4066Sahrens { 4630fa9e4066Sahrens uint64_t txg; 46318ad4d6ddSJeff Bonwick int error; 4632fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4633fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 463499653d4eSeschrock boolean_t unspare = B_FALSE; 4635d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 46361195e687SMark J Musante char *vdpath; 4637fa9e4066Sahrens 4638f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4639f9af39baSGeorge Wilson 4640fa9e4066Sahrens txg = spa_vdev_enter(spa); 4641fa9e4066Sahrens 4642c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4643fa9e4066Sahrens 4644fa9e4066Sahrens if (vd == NULL) 4645fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4646fa9e4066Sahrens 46470e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 46480e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 46490e34b6a7Sbonwick 4650fa9e4066Sahrens pvd = vd->vdev_parent; 4651fa9e4066Sahrens 46528ad4d6ddSJeff Bonwick /* 46538ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 46548ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 46558ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 46568ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 46578ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 46588ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 46598ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 46608ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 46618ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 46628ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 46638ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 46648ad4d6ddSJeff Bonwick */ 46658ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 46668ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 46678ad4d6ddSJeff Bonwick 4668fa9e4066Sahrens /* 4669cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 467099653d4eSeschrock */ 4671cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4672cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4673cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 467499653d4eSeschrock 467599653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4676e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4677fa9e4066Sahrens 4678fa9e4066Sahrens /* 467999653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4680fa9e4066Sahrens */ 4681fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 468299653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 468399653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4684fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4685fa9e4066Sahrens 4686fa9e4066Sahrens /* 46878ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 46888ad4d6ddSJeff Bonwick * we cannot safely detach it. 4689fa9e4066Sahrens */ 46908ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4691fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4692fa9e4066Sahrens 46938ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4694fa9e4066Sahrens 4695bf82a41bSeschrock /* 4696bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4697bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4698bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4699bf82a41bSeschrock */ 4700cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4701cb04b873SMark J Musante vd->vdev_path != NULL) { 4702cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4703cb04b873SMark J Musante 4704cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4705cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4706cb04b873SMark J Musante 4707cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4708cb04b873SMark J Musante continue; 4709cb04b873SMark J Musante 4710cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4711cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4712cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4713cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4714cb04b873SMark J Musante break; 4715cb04b873SMark J Musante } 4716bf82a41bSeschrock } 4717bf82a41bSeschrock } 4718bf82a41bSeschrock 471999653d4eSeschrock /* 472099653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 472199653d4eSeschrock * that the spare should become a real disk, and be removed from the 472299653d4eSeschrock * active spare list for the pool. 472399653d4eSeschrock */ 472499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4725cb04b873SMark J Musante vd->vdev_id == 0 && 4726cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 472799653d4eSeschrock unspare = B_TRUE; 472899653d4eSeschrock 4729fa9e4066Sahrens /* 4730fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4731fa9e4066Sahrens * This must be done after all other error cases are handled, 4732fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4733fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4734fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4735fa9e4066Sahrens * it's being detached! 4736fa9e4066Sahrens */ 473739c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4738fa9e4066Sahrens 4739fa9e4066Sahrens /* 4740fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4741fa9e4066Sahrens */ 4742fa9e4066Sahrens vdev_remove_child(pvd, vd); 4743fa9e4066Sahrens vdev_compact_children(pvd); 4744fa9e4066Sahrens 4745fa9e4066Sahrens /* 4746fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4747fa9e4066Sahrens */ 4748cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4749fa9e4066Sahrens 475099653d4eSeschrock /* 475199653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 47528ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 47538ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 47548ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 47558ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 47568ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 47578ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 475899653d4eSeschrock */ 475999653d4eSeschrock if (unspare) { 476099653d4eSeschrock ASSERT(cvd->vdev_isspare); 476139c23413Seschrock spa_spare_remove(cvd); 476299653d4eSeschrock unspare_guid = cvd->vdev_guid; 47638ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4764cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 476599653d4eSeschrock } 476699653d4eSeschrock 4767fa9e4066Sahrens /* 4768fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4769fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4770fa9e4066Sahrens */ 4771cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4772cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4773cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4774fa9e4066Sahrens vdev_remove_parent(cvd); 4775cb04b873SMark J Musante } 4776cb04b873SMark J Musante 4777fa9e4066Sahrens 4778fa9e4066Sahrens /* 4779fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4780fa9e4066Sahrens * may have been the previous top-level vdev. 4781fa9e4066Sahrens */ 4782fa9e4066Sahrens tvd = cvd->vdev_top; 4783fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4784fa9e4066Sahrens 4785fa9e4066Sahrens /* 478639c23413Seschrock * Reevaluate the parent vdev state. 4787fa9e4066Sahrens */ 47883d7072f8Seschrock vdev_propagate_state(cvd); 4789fa9e4066Sahrens 4790fa9e4066Sahrens /* 4791573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4792573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4793573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4794573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4795573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4796fa9e4066Sahrens */ 4797573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4798573ca77eSGeorge Wilson vdev_reopen(tvd); 4799573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4800573ca77eSGeorge Wilson } 4801fa9e4066Sahrens 4802fa9e4066Sahrens vdev_config_dirty(tvd); 4803fa9e4066Sahrens 4804fa9e4066Sahrens /* 480539c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 480639c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 480739c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 480839c23413Seschrock * prevent vd from being accessed after it's freed. 4809fa9e4066Sahrens */ 48101195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 48118ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4812fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4813ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4814ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4815fa9e4066Sahrens 48163d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 48173d7072f8Seschrock 4818cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4819cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4820cb04b873SMark J Musante 482199653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 482299653d4eSeschrock 48234445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 48241195e687SMark J Musante "vdev=%s", vdpath); 48251195e687SMark J Musante spa_strfree(vdpath); 48261195e687SMark J Musante 482799653d4eSeschrock /* 482839c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 482939c23413Seschrock * then we want to go through and remove the device from the hot spare 483039c23413Seschrock * list of every other pool. 483199653d4eSeschrock */ 483299653d4eSeschrock if (unspare) { 4833cb04b873SMark J Musante spa_t *altspa = NULL; 4834cb04b873SMark J Musante 483599653d4eSeschrock mutex_enter(&spa_namespace_lock); 4836cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 4837cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 4838cb04b873SMark J Musante altspa == spa) 483999653d4eSeschrock continue; 4840cb04b873SMark J Musante 4841cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 48429af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 4843cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 48449af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 4845cb04b873SMark J Musante spa_close(altspa, FTAG); 484699653d4eSeschrock } 484799653d4eSeschrock mutex_exit(&spa_namespace_lock); 4848cb04b873SMark J Musante 4849cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 4850cb04b873SMark J Musante spa_vdev_resilver_done(spa); 485199653d4eSeschrock } 485299653d4eSeschrock 4853cb04b873SMark J Musante /* all done with the spa; OK to release */ 4854cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 4855cb04b873SMark J Musante spa_close(spa, FTAG); 4856cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 4857cb04b873SMark J Musante 485899653d4eSeschrock return (error); 485999653d4eSeschrock } 486099653d4eSeschrock 48611195e687SMark J Musante /* 48621195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 48631195e687SMark J Musante */ 48641195e687SMark J Musante int 48651195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 48661195e687SMark J Musante nvlist_t *props, boolean_t exp) 48671195e687SMark J Musante { 48681195e687SMark J Musante int error = 0; 48691195e687SMark J Musante uint64_t txg, *glist; 48701195e687SMark J Musante spa_t *newspa; 48711195e687SMark J Musante uint_t c, children, lastlog; 48721195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 48731195e687SMark J Musante dmu_tx_t *tx; 48741195e687SMark J Musante char *altroot = NULL; 48751195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 48761195e687SMark J Musante boolean_t activate_slog; 48771195e687SMark J Musante 4878f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 48791195e687SMark J Musante 48801195e687SMark J Musante txg = spa_vdev_enter(spa); 48811195e687SMark J Musante 48821195e687SMark J Musante /* clear the log and flush everything up to now */ 48831195e687SMark J Musante activate_slog = spa_passivate_log(spa); 48841195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 48851195e687SMark J Musante error = spa_offline_log(spa); 48861195e687SMark J Musante txg = spa_vdev_config_enter(spa); 48871195e687SMark J Musante 48881195e687SMark J Musante if (activate_slog) 48891195e687SMark J Musante spa_activate_log(spa); 48901195e687SMark J Musante 48911195e687SMark J Musante if (error != 0) 48921195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 48931195e687SMark J Musante 48941195e687SMark J Musante /* check new spa name before going any further */ 48951195e687SMark J Musante if (spa_lookup(newname) != NULL) 48961195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 48971195e687SMark J Musante 48981195e687SMark J Musante /* 48991195e687SMark J Musante * scan through all the children to ensure they're all mirrors 49001195e687SMark J Musante */ 49011195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 49021195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 49031195e687SMark J Musante &children) != 0) 49041195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 49051195e687SMark J Musante 49061195e687SMark J Musante /* first, check to ensure we've got the right child count */ 49071195e687SMark J Musante rvd = spa->spa_root_vdev; 49081195e687SMark J Musante lastlog = 0; 49091195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 49101195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 49111195e687SMark J Musante 49121195e687SMark J Musante /* don't count the holes & logs as children */ 49131195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 49141195e687SMark J Musante if (lastlog == 0) 49151195e687SMark J Musante lastlog = c; 49161195e687SMark J Musante continue; 49171195e687SMark J Musante } 49181195e687SMark J Musante 49191195e687SMark J Musante lastlog = 0; 49201195e687SMark J Musante } 49211195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 49221195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 49231195e687SMark J Musante 49241195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 49251195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 49261195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 49271195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 49281195e687SMark J Musante 49291195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 49301195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 49311195e687SMark J Musante 49321195e687SMark J Musante /* then, loop over each vdev and validate it */ 49331195e687SMark J Musante for (c = 0; c < children; c++) { 49341195e687SMark J Musante uint64_t is_hole = 0; 49351195e687SMark J Musante 49361195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 49371195e687SMark J Musante &is_hole); 49381195e687SMark J Musante 49391195e687SMark J Musante if (is_hole != 0) { 49401195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 49411195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 49421195e687SMark J Musante continue; 49431195e687SMark J Musante } else { 4944be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 49451195e687SMark J Musante break; 49461195e687SMark J Musante } 49471195e687SMark J Musante } 49481195e687SMark J Musante 49491195e687SMark J Musante /* which disk is going to be split? */ 49501195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 49511195e687SMark J Musante &glist[c]) != 0) { 4952be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 49531195e687SMark J Musante break; 49541195e687SMark J Musante } 49551195e687SMark J Musante 49561195e687SMark J Musante /* look it up in the spa */ 49571195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 49581195e687SMark J Musante if (vml[c] == NULL) { 4959be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 49601195e687SMark J Musante break; 49611195e687SMark J Musante } 49621195e687SMark J Musante 49631195e687SMark J Musante /* make sure there's nothing stopping the split */ 49641195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 49651195e687SMark J Musante vml[c]->vdev_islog || 49661195e687SMark J Musante vml[c]->vdev_ishole || 49671195e687SMark J Musante vml[c]->vdev_isspare || 49681195e687SMark J Musante vml[c]->vdev_isl2cache || 49691195e687SMark J Musante !vdev_writeable(vml[c]) || 4970d41c4376SMark J Musante vml[c]->vdev_children != 0 || 49711195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 49721195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 4973be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 49741195e687SMark J Musante break; 49751195e687SMark J Musante } 49761195e687SMark J Musante 49771195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 4978be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 49791195e687SMark J Musante break; 49801195e687SMark J Musante } 49811195e687SMark J Musante 49821195e687SMark J Musante /* we need certain info from the top level */ 49831195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 49841195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 49851195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 49861195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 49871195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 49881195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 49891195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 49901195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 49911195e687SMark J Musante } 49921195e687SMark J Musante 49931195e687SMark J Musante if (error != 0) { 49941195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 49951195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 49961195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 49971195e687SMark J Musante } 49981195e687SMark J Musante 49991195e687SMark J Musante /* stop writers from using the disks */ 50001195e687SMark J Musante for (c = 0; c < children; c++) { 50011195e687SMark J Musante if (vml[c] != NULL) 50021195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 50031195e687SMark J Musante } 50041195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 50051195e687SMark J Musante 50061195e687SMark J Musante /* 50071195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 50081195e687SMark J Musante * will disappear once the config is regenerated. 50091195e687SMark J Musante */ 50101195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 50111195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 50121195e687SMark J Musante glist, children) == 0); 50131195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 50141195e687SMark J Musante 501598295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 50161195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 50171195e687SMark J Musante nvl) == 0); 501898295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 50191195e687SMark J Musante spa->spa_config_splitting = nvl; 50201195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 50211195e687SMark J Musante 50221195e687SMark J Musante /* configure and create the new pool */ 50231195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 50241195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 50251195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 50261195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 50271195e687SMark J Musante spa_version(spa)) == 0); 50281195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 50291195e687SMark J Musante spa->spa_config_txg) == 0); 50301195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 50311195e687SMark J Musante spa_generate_guid(NULL)) == 0); 50321195e687SMark J Musante (void) nvlist_lookup_string(props, 50331195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 50341195e687SMark J Musante 5035d41c4376SMark J Musante /* add the new pool to the namespace */ 50361195e687SMark J Musante newspa = spa_add(newname, config, altroot); 50371195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 50381195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 50391195e687SMark J Musante 50401195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 50411195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 50421195e687SMark J Musante 50431195e687SMark J Musante if (zio_injection_enabled) 50441195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 50451195e687SMark J Musante 50461195e687SMark J Musante spa_activate(newspa, spa_mode_global); 50471195e687SMark J Musante spa_async_suspend(newspa); 50481195e687SMark J Musante 50491195e687SMark J Musante /* create the new pool from the disks of the original pool */ 50501195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 50511195e687SMark J Musante if (error) 50521195e687SMark J Musante goto out; 50531195e687SMark J Musante 50541195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 50551195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 50561195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 50571195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 50581195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 50591195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 50601195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 50611195e687SMark J Musante B_TRUE)); 50621195e687SMark J Musante } 50631195e687SMark J Musante 50641195e687SMark J Musante /* set the props */ 50651195e687SMark J Musante if (props != NULL) { 50661195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 50671195e687SMark J Musante error = spa_prop_set(newspa, props); 50681195e687SMark J Musante if (error) 50691195e687SMark J Musante goto out; 50701195e687SMark J Musante } 50711195e687SMark J Musante 50721195e687SMark J Musante /* flush everything */ 50731195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 50741195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 50751195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 50761195e687SMark J Musante 50771195e687SMark J Musante if (zio_injection_enabled) 50781195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 50791195e687SMark J Musante 50801195e687SMark J Musante spa_async_resume(newspa); 50811195e687SMark J Musante 50821195e687SMark J Musante /* finally, update the original pool's config */ 50831195e687SMark J Musante txg = spa_vdev_config_enter(spa); 50841195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 50851195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 50861195e687SMark J Musante if (error != 0) 50871195e687SMark J Musante dmu_tx_abort(tx); 50881195e687SMark J Musante for (c = 0; c < children; c++) { 50891195e687SMark J Musante if (vml[c] != NULL) { 50901195e687SMark J Musante vdev_split(vml[c]); 50911195e687SMark J Musante if (error == 0) 50924445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 50934445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 50941195e687SMark J Musante vdev_free(vml[c]); 50951195e687SMark J Musante } 50961195e687SMark J Musante } 50971195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 50981195e687SMark J Musante spa->spa_config_splitting = NULL; 50991195e687SMark J Musante nvlist_free(nvl); 51001195e687SMark J Musante if (error == 0) 51011195e687SMark J Musante dmu_tx_commit(tx); 51021195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 51031195e687SMark J Musante 51041195e687SMark J Musante if (zio_injection_enabled) 51051195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 51061195e687SMark J Musante 51071195e687SMark J Musante /* split is complete; log a history record */ 51084445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 51094445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 51101195e687SMark J Musante 51111195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 51121195e687SMark J Musante 51131195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 51141195e687SMark J Musante if (exp) 51151195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 51161195e687SMark J Musante B_FALSE, B_FALSE); 51171195e687SMark J Musante 51181195e687SMark J Musante return (error); 51191195e687SMark J Musante 51201195e687SMark J Musante out: 51211195e687SMark J Musante spa_unload(newspa); 51221195e687SMark J Musante spa_deactivate(newspa); 51231195e687SMark J Musante spa_remove(newspa); 51241195e687SMark J Musante 51251195e687SMark J Musante txg = spa_vdev_config_enter(spa); 512698295d61SMark J Musante 512798295d61SMark J Musante /* re-online all offlined disks */ 512898295d61SMark J Musante for (c = 0; c < children; c++) { 512998295d61SMark J Musante if (vml[c] != NULL) 513098295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 513198295d61SMark J Musante } 513298295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 513398295d61SMark J Musante 51341195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 51351195e687SMark J Musante spa->spa_config_splitting = NULL; 5136d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 51371195e687SMark J Musante 51381195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 51391195e687SMark J Musante return (error); 51401195e687SMark J Musante } 51411195e687SMark J Musante 5142e14bb325SJeff Bonwick static nvlist_t * 5143e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 514499653d4eSeschrock { 5145e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5146e14bb325SJeff Bonwick uint64_t guid; 514799653d4eSeschrock 5148e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5149e14bb325SJeff Bonwick &guid) == 0); 515099653d4eSeschrock 5151e14bb325SJeff Bonwick if (guid == target_guid) 5152e14bb325SJeff Bonwick return (nvpp[i]); 515399653d4eSeschrock } 515499653d4eSeschrock 5155e14bb325SJeff Bonwick return (NULL); 5156fa94a07fSbrendan } 5157fa94a07fSbrendan 5158e14bb325SJeff Bonwick static void 5159e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 5160e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 5161fa94a07fSbrendan { 5162e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5163fa94a07fSbrendan 5164e14bb325SJeff Bonwick if (count > 1) 5165e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5166fa94a07fSbrendan 5167e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5168e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5169e14bb325SJeff Bonwick continue; 5170e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5171fa94a07fSbrendan } 5172fa94a07fSbrendan 5173e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5174e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5175fa94a07fSbrendan 5176e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5177e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5178fa94a07fSbrendan 5179e14bb325SJeff Bonwick if (count > 1) 5180e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5181fa94a07fSbrendan } 5182fa94a07fSbrendan 518388ecc943SGeorge Wilson /* 518488ecc943SGeorge Wilson * Evacuate the device. 518588ecc943SGeorge Wilson */ 51863f9d6ad7SLin Ling static int 518788ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 518888ecc943SGeorge Wilson { 518988ecc943SGeorge Wilson uint64_t txg; 51903f9d6ad7SLin Ling int error = 0; 519188ecc943SGeorge Wilson 519288ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 519388ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5194b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 519588ecc943SGeorge Wilson 519688ecc943SGeorge Wilson /* 519788ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 519888ecc943SGeorge Wilson * since we need to do I/O but we do keep the 519988ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 520088ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 520188ecc943SGeorge Wilson */ 520288ecc943SGeorge Wilson if (vd->vdev_islog) { 52033f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 52043f9d6ad7SLin Ling error = spa_offline_log(spa); 5205a1521560SJeff Bonwick } else { 5206be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 520788ecc943SGeorge Wilson } 520888ecc943SGeorge Wilson 5209a1521560SJeff Bonwick if (error) 5210a1521560SJeff Bonwick return (error); 5211a1521560SJeff Bonwick 521288ecc943SGeorge Wilson /* 5213a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5214a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 521588ecc943SGeorge Wilson */ 5216fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 521788ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 521888ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 52190713e232SGeorge Wilson vdev_dirty_leaves(vd, VDD_DTL, txg); 522088ecc943SGeorge Wilson vdev_config_dirty(vd); 522188ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 522288ecc943SGeorge Wilson 522388ecc943SGeorge Wilson return (0); 522488ecc943SGeorge Wilson } 522588ecc943SGeorge Wilson 522688ecc943SGeorge Wilson /* 522788ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 522888ecc943SGeorge Wilson */ 52293f9d6ad7SLin Ling static void 5230a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 523188ecc943SGeorge Wilson { 523288ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 523388ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 523488ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 523588ecc943SGeorge Wilson 523688ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 523788ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5238b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 523988ecc943SGeorge Wilson 52403f9d6ad7SLin Ling /* 52413f9d6ad7SLin Ling * Only remove any devices which are empty. 52423f9d6ad7SLin Ling */ 52433f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 52443f9d6ad7SLin Ling return; 52453f9d6ad7SLin Ling 524688ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5247b24ab676SJeff Bonwick 5248b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5249b24ab676SJeff Bonwick vdev_state_clean(vd); 5250b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5251b24ab676SJeff Bonwick vdev_config_clean(vd); 5252b24ab676SJeff Bonwick 525388ecc943SGeorge Wilson vdev_free(vd); 525488ecc943SGeorge Wilson 525588ecc943SGeorge Wilson if (last_vdev) { 525688ecc943SGeorge Wilson vdev_compact_children(rvd); 525788ecc943SGeorge Wilson } else { 525888ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 525988ecc943SGeorge Wilson vdev_add_child(rvd, vd); 526088ecc943SGeorge Wilson } 5261fcbfa62bSLin Ling vdev_config_dirty(rvd); 5262fcbfa62bSLin Ling 5263fcbfa62bSLin Ling /* 5264fcbfa62bSLin Ling * Reassess the health of our root vdev. 5265fcbfa62bSLin Ling */ 5266fcbfa62bSLin Ling vdev_reopen(rvd); 526788ecc943SGeorge Wilson } 526888ecc943SGeorge Wilson 52693f9d6ad7SLin Ling /* 52703f9d6ad7SLin Ling * Remove a device from the pool - 52713f9d6ad7SLin Ling * 52723f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 52733f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 52743f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 52753f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 52763f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 5277f7170741SWill Andrews * 5278f7170741SWill Andrews * Currently, this supports removing only hot spares, slogs, and level 2 ARC 5279f7170741SWill Andrews * devices. 5280fa94a07fSbrendan */ 5281fa94a07fSbrendan int 5282fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5283fa94a07fSbrendan { 5284fa94a07fSbrendan vdev_t *vd; 5285a1521560SJeff Bonwick metaslab_group_t *mg; 5286e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 52878ad4d6ddSJeff Bonwick uint64_t txg = 0; 528888ecc943SGeorge Wilson uint_t nspares, nl2cache; 5289fa94a07fSbrendan int error = 0; 52908ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5291fa94a07fSbrendan 5292f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5293f9af39baSGeorge Wilson 52948ad4d6ddSJeff Bonwick if (!locked) 52958ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5296fa94a07fSbrendan 5297c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5298fa94a07fSbrendan 5299fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5300fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5301e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5302e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5303e14bb325SJeff Bonwick /* 5304e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5305e14bb325SJeff Bonwick * in this pool. 5306e14bb325SJeff Bonwick */ 5307e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5308e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5309e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5310e14bb325SJeff Bonwick spa_load_spares(spa); 5311e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5312e14bb325SJeff Bonwick } else { 5313be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 5314e14bb325SJeff Bonwick } 5315e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5316fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5317e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5318e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5319e14bb325SJeff Bonwick /* 5320e14bb325SJeff Bonwick * Cache devices can always be removed. 5321e14bb325SJeff Bonwick */ 5322e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5323e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5324fa94a07fSbrendan spa_load_l2cache(spa); 5325fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 532688ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 532788ecc943SGeorge Wilson ASSERT(!locked); 5328b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 532988ecc943SGeorge Wilson 5330a1521560SJeff Bonwick mg = vd->vdev_mg; 5331a1521560SJeff Bonwick 533288ecc943SGeorge Wilson /* 5333a1521560SJeff Bonwick * Stop allocating from this vdev. 533488ecc943SGeorge Wilson */ 5335a1521560SJeff Bonwick metaslab_group_passivate(mg); 533688ecc943SGeorge Wilson 5337b24ab676SJeff Bonwick /* 5338b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5339b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5340b24ab676SJeff Bonwick */ 5341b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5342b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5343b24ab676SJeff Bonwick 5344a1521560SJeff Bonwick /* 5345a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5346a1521560SJeff Bonwick */ 5347a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5348a1521560SJeff Bonwick 534988ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 535088ecc943SGeorge Wilson 5351a1521560SJeff Bonwick /* 5352a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5353a1521560SJeff Bonwick */ 5354a1521560SJeff Bonwick if (error) { 5355a1521560SJeff Bonwick metaslab_group_activate(mg); 5356a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5357a1521560SJeff Bonwick } 5358a1521560SJeff Bonwick 5359a1521560SJeff Bonwick /* 5360a1521560SJeff Bonwick * Clean up the vdev namespace. 5361a1521560SJeff Bonwick */ 5362a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 536388ecc943SGeorge Wilson 5364e14bb325SJeff Bonwick } else if (vd != NULL) { 5365e14bb325SJeff Bonwick /* 5366e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5367e14bb325SJeff Bonwick */ 5368be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5369e14bb325SJeff Bonwick } else { 5370e14bb325SJeff Bonwick /* 5371e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5372e14bb325SJeff Bonwick */ 5373be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 5374fa94a07fSbrendan } 537599653d4eSeschrock 53768ad4d6ddSJeff Bonwick if (!locked) 53778ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 53788ad4d6ddSJeff Bonwick 53798ad4d6ddSJeff Bonwick return (error); 5380fa9e4066Sahrens } 5381fa9e4066Sahrens 5382fa9e4066Sahrens /* 53833d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 5384f7170741SWill Andrews * currently spared, so we can detach it. 5385fa9e4066Sahrens */ 5386ea8dc4b6Seschrock static vdev_t * 53873d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5388fa9e4066Sahrens { 5389ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5390fa9e4066Sahrens 5391573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 53923d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5393ea8dc4b6Seschrock if (oldvd != NULL) 5394ea8dc4b6Seschrock return (oldvd); 5395ea8dc4b6Seschrock } 5396fa9e4066Sahrens 53973d7072f8Seschrock /* 5398cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5399cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5400cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5401cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5402cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5403cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 54043d7072f8Seschrock */ 5405cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5406cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5407cb04b873SMark J Musante 5408cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5409ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5410ea8dc4b6Seschrock 54118ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5412e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 54138ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5414ea8dc4b6Seschrock return (oldvd); 5415fa9e4066Sahrens } 5416ea8dc4b6Seschrock 54173d7072f8Seschrock /* 54183d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 54193d7072f8Seschrock */ 5420cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5421cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5422cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5423cb04b873SMark J Musante 5424cb04b873SMark J Musante if (last->vdev_unspare) { 5425cb04b873SMark J Musante oldvd = first; 5426cb04b873SMark J Musante newvd = last; 5427cb04b873SMark J Musante } else if (first->vdev_unspare) { 5428cb04b873SMark J Musante oldvd = last; 5429cb04b873SMark J Musante newvd = first; 5430cb04b873SMark J Musante } else { 5431cb04b873SMark J Musante oldvd = NULL; 5432cb04b873SMark J Musante } 54333d7072f8Seschrock 5434cb04b873SMark J Musante if (oldvd != NULL && 54358ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5436e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5437cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 54383d7072f8Seschrock return (oldvd); 5439cb04b873SMark J Musante 5440cb04b873SMark J Musante /* 5441cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5442cb04b873SMark J Musante * and those spares are not required, then we want to 5443cb04b873SMark J Musante * attempt to free them up now so that they can be used 5444cb04b873SMark J Musante * by other pools. Once we're back down to a single 5445cb04b873SMark J Musante * disk+spare, we stop removing them. 5446cb04b873SMark J Musante */ 5447cb04b873SMark J Musante if (vd->vdev_children > 2) { 5448cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5449cb04b873SMark J Musante 5450cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5451cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5452cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5453cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5454cb04b873SMark J Musante return (newvd); 54553d7072f8Seschrock } 54563d7072f8Seschrock } 54573d7072f8Seschrock 5458ea8dc4b6Seschrock return (NULL); 5459fa9e4066Sahrens } 5460fa9e4066Sahrens 5461ea8dc4b6Seschrock static void 54623d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5463fa9e4066Sahrens { 54648ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 54658ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5466ea8dc4b6Seschrock 54678ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5468ea8dc4b6Seschrock 54693d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 54708ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 54718ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5472ea8dc4b6Seschrock guid = vd->vdev_guid; 54738ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 54748ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 54758ad4d6ddSJeff Bonwick sguid = 0; 547699653d4eSeschrock /* 547799653d4eSeschrock * If we have just finished replacing a hot spared device, then 547899653d4eSeschrock * we need to detach the parent's first child (the original hot 547999653d4eSeschrock * spare) as well. 548099653d4eSeschrock */ 5481cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5482cb04b873SMark J Musante ppvd->vdev_children == 2) { 548399653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 54848ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 548599653d4eSeschrock } 5486b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 5487b4952e17SGeorge Wilson 54888ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 54898ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5490ea8dc4b6Seschrock return; 54918ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 549299653d4eSeschrock return; 54938ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5494fa9e4066Sahrens } 5495fa9e4066Sahrens 54968ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5497fa9e4066Sahrens } 5498fa9e4066Sahrens 5499c67d9675Seschrock /* 5500b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5501c67d9675Seschrock */ 5502c67d9675Seschrock int 55036809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 55046809eb4eSEric Schrock boolean_t ispath) 5505c67d9675Seschrock { 5506c5904d13Seschrock vdev_t *vd; 5507208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5508c67d9675Seschrock 5509f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5510f9af39baSGeorge Wilson 5511b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5512c67d9675Seschrock 55136809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5514b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5515c67d9675Seschrock 55160e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5517b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 55180e34b6a7Sbonwick 55196809eb4eSEric Schrock if (ispath) { 5520208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5521208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5522208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5523208044b8SGeorge Wilson sync = B_TRUE; 5524208044b8SGeorge Wilson } 55256809eb4eSEric Schrock } else { 5526208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5527208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5528208044b8SGeorge Wilson sync = B_TRUE; 5529208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 55306809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5531208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5532208044b8SGeorge Wilson sync = B_TRUE; 5533208044b8SGeorge Wilson } 55346809eb4eSEric Schrock } 5535c67d9675Seschrock 5536208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5537c67d9675Seschrock } 5538c67d9675Seschrock 55396809eb4eSEric Schrock int 55406809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 55416809eb4eSEric Schrock { 55426809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 55436809eb4eSEric Schrock } 55446809eb4eSEric Schrock 55456809eb4eSEric Schrock int 55466809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 55476809eb4eSEric Schrock { 55486809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 55496809eb4eSEric Schrock } 55506809eb4eSEric Schrock 5551fa9e4066Sahrens /* 5552fa9e4066Sahrens * ========================================================================== 55533f9d6ad7SLin Ling * SPA Scanning 5554fa9e4066Sahrens * ========================================================================== 5555fa9e4066Sahrens */ 5556fa9e4066Sahrens 5557ea8dc4b6Seschrock int 55583f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5559fa9e4066Sahrens { 5560e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 55613f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5562be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 55633f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 55643f9d6ad7SLin Ling } 5565bb8b5132Sek 55663f9d6ad7SLin Ling int 55673f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 55683f9d6ad7SLin Ling { 55693f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 55703f9d6ad7SLin Ling 55713f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5572be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 5573fa9e4066Sahrens 5574fa9e4066Sahrens /* 5575088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5576088f3894Sahrens * writeable leaf device, we have nothing to do. 5577fa9e4066Sahrens */ 55783f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5579088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5580088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5581ea8dc4b6Seschrock return (0); 5582ea8dc4b6Seschrock } 5583fa9e4066Sahrens 55843f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5585fa9e4066Sahrens } 5586fa9e4066Sahrens 5587ea8dc4b6Seschrock /* 5588ea8dc4b6Seschrock * ========================================================================== 5589ea8dc4b6Seschrock * SPA async task processing 5590ea8dc4b6Seschrock * ========================================================================== 5591ea8dc4b6Seschrock */ 5592ea8dc4b6Seschrock 5593ea8dc4b6Seschrock static void 55943d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5595fa9e4066Sahrens { 559649cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 559798d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 559898d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 559949cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 56001d713200SEric Schrock 56011d713200SEric Schrock /* 56021d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 56031d713200SEric Schrock * vdev_clear() as that will cause us to throw away 56041d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 56051d713200SEric Schrock * device, all of which is a waste. 56061d713200SEric Schrock */ 56071d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 56081d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 56091d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 56101d713200SEric Schrock 5611e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5612ea8dc4b6Seschrock } 561349cf58c0SBrendan Gregg - Sun Microsystems 5614e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 561549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5616ea8dc4b6Seschrock } 5617fa9e4066Sahrens 5618e14bb325SJeff Bonwick static void 5619e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5620e14bb325SJeff Bonwick { 5621e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 562298d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5623e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5624e14bb325SJeff Bonwick } 5625e14bb325SJeff Bonwick 5626e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5627e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5628e14bb325SJeff Bonwick } 5629e14bb325SJeff Bonwick 5630573ca77eSGeorge Wilson static void 5631573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5632573ca77eSGeorge Wilson { 5633573ca77eSGeorge Wilson sysevent_id_t eid; 5634573ca77eSGeorge Wilson nvlist_t *attr; 5635573ca77eSGeorge Wilson char *physpath; 5636573ca77eSGeorge Wilson 5637573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5638573ca77eSGeorge Wilson return; 5639573ca77eSGeorge Wilson 5640573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5641573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5642573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5643573ca77eSGeorge Wilson } 5644573ca77eSGeorge Wilson 5645573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5646573ca77eSGeorge Wilson return; 5647573ca77eSGeorge Wilson 5648573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5649573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5650573ca77eSGeorge Wilson 5651573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5652573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5653573ca77eSGeorge Wilson 5654573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5655573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5656573ca77eSGeorge Wilson 5657573ca77eSGeorge Wilson nvlist_free(attr); 5658573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5659573ca77eSGeorge Wilson } 5660573ca77eSGeorge Wilson 5661ea8dc4b6Seschrock static void 5662ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5663ea8dc4b6Seschrock { 5664e14bb325SJeff Bonwick int tasks; 5665ea8dc4b6Seschrock 5666ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5667ea8dc4b6Seschrock 5668ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5669ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5670ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5671ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5672ea8dc4b6Seschrock 56730373e76bSbonwick /* 56740373e76bSbonwick * See if the config needs to be updated. 56750373e76bSbonwick */ 56760373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5677b24ab676SJeff Bonwick uint64_t old_space, new_space; 5678573ca77eSGeorge Wilson 56790373e76bSbonwick mutex_enter(&spa_namespace_lock); 5680b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 56810373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5682b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 56830373e76bSbonwick mutex_exit(&spa_namespace_lock); 5684573ca77eSGeorge Wilson 5685573ca77eSGeorge Wilson /* 5686573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5687573ca77eSGeorge Wilson * then log an internal history event. 5688573ca77eSGeorge Wilson */ 5689b24ab676SJeff Bonwick if (new_space != old_space) { 56904445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5691c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5692b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5693573ca77eSGeorge Wilson } 56940373e76bSbonwick } 56950373e76bSbonwick 5696ea8dc4b6Seschrock /* 56973d7072f8Seschrock * See if any devices need to be marked REMOVED. 5698ea8dc4b6Seschrock */ 5699e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 57008f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 57013d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5702e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 570349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5704e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 570549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5706e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5707e14bb325SJeff Bonwick } 5708e14bb325SJeff Bonwick 5709573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5710573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5711573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5712573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5713573ca77eSGeorge Wilson } 5714573ca77eSGeorge Wilson 5715e14bb325SJeff Bonwick /* 5716e14bb325SJeff Bonwick * See if any devices need to be probed. 5717e14bb325SJeff Bonwick */ 5718e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 57198f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5720e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5721e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 57223d7072f8Seschrock } 5723ea8dc4b6Seschrock 5724ea8dc4b6Seschrock /* 5725ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5726ea8dc4b6Seschrock */ 57273d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 57283d7072f8Seschrock spa_vdev_resilver_done(spa); 5729fa9e4066Sahrens 5730ea8dc4b6Seschrock /* 5731ea8dc4b6Seschrock * Kick off a resilver. 5732ea8dc4b6Seschrock */ 5733088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 57343f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5735ea8dc4b6Seschrock 5736ea8dc4b6Seschrock /* 5737ea8dc4b6Seschrock * Let the world know that we're done. 5738ea8dc4b6Seschrock */ 5739ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5740ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5741ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5742ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5743ea8dc4b6Seschrock thread_exit(); 5744ea8dc4b6Seschrock } 5745ea8dc4b6Seschrock 5746ea8dc4b6Seschrock void 5747ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5748ea8dc4b6Seschrock { 5749ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5750ea8dc4b6Seschrock spa->spa_async_suspended++; 5751ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5752ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5753ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5754ea8dc4b6Seschrock } 5755ea8dc4b6Seschrock 5756ea8dc4b6Seschrock void 5757ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5758ea8dc4b6Seschrock { 5759ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5760ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5761ea8dc4b6Seschrock spa->spa_async_suspended--; 5762ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5763ea8dc4b6Seschrock } 5764ea8dc4b6Seschrock 57653cb69f73SWill Andrews static boolean_t 57663cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 57673cb69f73SWill Andrews { 57683cb69f73SWill Andrews uint_t non_config_tasks; 57693cb69f73SWill Andrews uint_t config_task; 57703cb69f73SWill Andrews boolean_t config_task_suspended; 57713cb69f73SWill Andrews 57723cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 57733cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 57743cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 57753cb69f73SWill Andrews config_task_suspended = B_FALSE; 57763cb69f73SWill Andrews } else { 57773cb69f73SWill Andrews config_task_suspended = 57783cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 57793cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 57803cb69f73SWill Andrews } 57813cb69f73SWill Andrews 57823cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 57833cb69f73SWill Andrews } 57843cb69f73SWill Andrews 5785ea8dc4b6Seschrock static void 5786ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5787ea8dc4b6Seschrock { 5788ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 57893cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 57903cb69f73SWill Andrews !spa->spa_async_suspended && 57910373e76bSbonwick spa->spa_async_thread == NULL && 57923cb69f73SWill Andrews rootdir != NULL) 5793ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5794ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5795ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5796ea8dc4b6Seschrock } 5797ea8dc4b6Seschrock 5798ea8dc4b6Seschrock void 5799ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5800ea8dc4b6Seschrock { 58013f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5802ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5803ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5804ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5805fa9e4066Sahrens } 5806fa9e4066Sahrens 5807fa9e4066Sahrens /* 5808fa9e4066Sahrens * ========================================================================== 5809fa9e4066Sahrens * SPA syncing routines 5810fa9e4066Sahrens * ========================================================================== 5811fa9e4066Sahrens */ 5812fa9e4066Sahrens 5813cde58dbcSMatthew Ahrens static int 5814cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5815cde58dbcSMatthew Ahrens { 5816cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5817cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5818cde58dbcSMatthew Ahrens return (0); 5819b24ab676SJeff Bonwick } 5820b24ab676SJeff Bonwick 5821cde58dbcSMatthew Ahrens static int 5822cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5823b24ab676SJeff Bonwick { 5824b24ab676SJeff Bonwick zio_t *zio = arg; 5825b24ab676SJeff Bonwick 5826b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5827b24ab676SJeff Bonwick zio->io_flags)); 5828cde58dbcSMatthew Ahrens return (0); 5829fa9e4066Sahrens } 5830fa9e4066Sahrens 583169962b56SMatthew Ahrens /* 583269962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 583369962b56SMatthew Ahrens * amount of time spent syncing frees. 583469962b56SMatthew Ahrens */ 583569962b56SMatthew Ahrens static void 583669962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 583769962b56SMatthew Ahrens { 583869962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 583969962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 584069962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 584169962b56SMatthew Ahrens } 584269962b56SMatthew Ahrens 584369962b56SMatthew Ahrens /* 584469962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 584569962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 584669962b56SMatthew Ahrens */ 584769962b56SMatthew Ahrens static void 584869962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 584969962b56SMatthew Ahrens { 585069962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 585169962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 585269962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 585369962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 585469962b56SMatthew Ahrens } 585569962b56SMatthew Ahrens 585669962b56SMatthew Ahrens 5857fa9e4066Sahrens static void 585899653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 5859fa9e4066Sahrens { 5860fa9e4066Sahrens char *packed = NULL; 5861f7991ba4STim Haley size_t bufsize; 5862fa9e4066Sahrens size_t nvsize = 0; 5863fa9e4066Sahrens dmu_buf_t *db; 5864fa9e4066Sahrens 586599653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 5866fa9e4066Sahrens 5867f7991ba4STim Haley /* 5868f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 586943466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 5870f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 5871f7991ba4STim Haley */ 5872ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 5873f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 5874fa9e4066Sahrens 587599653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 5876ea8dc4b6Seschrock KM_SLEEP) == 0); 5877f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5878fa9e4066Sahrens 5879f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5880fa9e4066Sahrens 5881f7991ba4STim Haley kmem_free(packed, bufsize); 5882fa9e4066Sahrens 588399653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5884fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5885fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5886ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5887fa9e4066Sahrens } 5888fa9e4066Sahrens 588999653d4eSeschrock static void 5890fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5891fa94a07fSbrendan const char *config, const char *entry) 589299653d4eSeschrock { 589399653d4eSeschrock nvlist_t *nvroot; 5894fa94a07fSbrendan nvlist_t **list; 589599653d4eSeschrock int i; 589699653d4eSeschrock 5897fa94a07fSbrendan if (!sav->sav_sync) 589899653d4eSeschrock return; 589999653d4eSeschrock 590099653d4eSeschrock /* 5901fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5902fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 59033d7072f8Seschrock * valid and the vdevs are labeled appropriately. 590499653d4eSeschrock */ 5905fa94a07fSbrendan if (sav->sav_object == 0) { 5906fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5907fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5908fa94a07fSbrendan sizeof (uint64_t), tx); 590999653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5910fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5911fa94a07fSbrendan &sav->sav_object, tx) == 0); 591299653d4eSeschrock } 591399653d4eSeschrock 591499653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5915fa94a07fSbrendan if (sav->sav_count == 0) { 5916fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 591799653d4eSeschrock } else { 5918fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5919fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5920fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 59213f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5922fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5923fa94a07fSbrendan sav->sav_count) == 0); 5924fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5925fa94a07fSbrendan nvlist_free(list[i]); 5926fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 592799653d4eSeschrock } 592899653d4eSeschrock 5929fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 593006eeb2adSek nvlist_free(nvroot); 593199653d4eSeschrock 5932fa94a07fSbrendan sav->sav_sync = B_FALSE; 593399653d4eSeschrock } 593499653d4eSeschrock 593599653d4eSeschrock static void 593699653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 593799653d4eSeschrock { 593899653d4eSeschrock nvlist_t *config; 593999653d4eSeschrock 5940e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 594199653d4eSeschrock return; 594299653d4eSeschrock 5943e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5944e14bb325SJeff Bonwick 5945e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 5946e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 5947e14bb325SJeff Bonwick 594825345e46SGeorge Wilson /* 594925345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 595025345e46SGeorge Wilson * the config object gets updated with the correct version. 595125345e46SGeorge Wilson */ 595225345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 595325345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 595425345e46SGeorge Wilson spa->spa_uberblock.ub_version); 595525345e46SGeorge Wilson 5956e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 595799653d4eSeschrock 595899653d4eSeschrock if (spa->spa_config_syncing) 595999653d4eSeschrock nvlist_free(spa->spa_config_syncing); 596099653d4eSeschrock spa->spa_config_syncing = config; 596199653d4eSeschrock 596299653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 596399653d4eSeschrock } 596499653d4eSeschrock 5965ad135b5dSChristopher Siden static void 59663b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 5967ad135b5dSChristopher Siden { 59683b2aab18SMatthew Ahrens uint64_t *versionp = arg; 59693b2aab18SMatthew Ahrens uint64_t version = *versionp; 59703b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 5971ad135b5dSChristopher Siden 5972ad135b5dSChristopher Siden /* 5973ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 5974ad135b5dSChristopher Siden */ 5975ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 5976ad135b5dSChristopher Siden 597762eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 5978ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 5979ad135b5dSChristopher Siden 5980ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 5981ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 59824445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 5983ad135b5dSChristopher Siden } 5984ad135b5dSChristopher Siden 5985990b4856Slling /* 5986990b4856Slling * Set zpool properties. 5987990b4856Slling */ 5988b1b8ab34Slling static void 59893b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 5990b1b8ab34Slling { 59913b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 59923b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 5993b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 5994ad135b5dSChristopher Siden nvpair_t *elem = NULL; 5995b1b8ab34Slling 5996e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 5997e14bb325SJeff Bonwick 5998990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 5999ad135b5dSChristopher Siden uint64_t intval; 6000ad135b5dSChristopher Siden char *strval, *fname; 6001ad135b5dSChristopher Siden zpool_prop_t prop; 6002ad135b5dSChristopher Siden const char *propname; 6003ad135b5dSChristopher Siden zprop_type_t proptype; 60042acef22dSMatthew Ahrens spa_feature_t fid; 6005ad135b5dSChristopher Siden 6006990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 6007ad135b5dSChristopher Siden case ZPROP_INVAL: 6008ad135b5dSChristopher Siden /* 6009ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6010ad135b5dSChristopher Siden */ 6011ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6012ad135b5dSChristopher Siden 6013ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 60142acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6015ad135b5dSChristopher Siden 60162acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 60174445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 60184445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6019ad135b5dSChristopher Siden break; 6020ad135b5dSChristopher Siden 6021990b4856Slling case ZPOOL_PROP_VERSION: 60220713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6023990b4856Slling /* 6024ad135b5dSChristopher Siden * The version is synced seperatly before other 6025ad135b5dSChristopher Siden * properties and should be correct by now. 6026990b4856Slling */ 6027ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6028ecd6cf80Smarks break; 6029990b4856Slling 6030990b4856Slling case ZPOOL_PROP_ALTROOT: 6031990b4856Slling /* 6032990b4856Slling * 'altroot' is a non-persistent property. It should 6033990b4856Slling * have been set temporarily at creation or import time. 6034990b4856Slling */ 6035990b4856Slling ASSERT(spa->spa_root != NULL); 6036b1b8ab34Slling break; 60373d7072f8Seschrock 6038f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 60392f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6040990b4856Slling /* 6041f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6042f9af39baSGeorge Wilson * properties. 6043990b4856Slling */ 60443d7072f8Seschrock break; 60458704186eSDan McDonald case ZPOOL_PROP_COMMENT: 60460713e232SGeorge Wilson strval = fnvpair_value_string(elem); 60478704186eSDan McDonald if (spa->spa_comment != NULL) 60488704186eSDan McDonald spa_strfree(spa->spa_comment); 60498704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 60508704186eSDan McDonald /* 60518704186eSDan McDonald * We need to dirty the configuration on all the vdevs 60528704186eSDan McDonald * so that their labels get updated. It's unnecessary 60538704186eSDan McDonald * to do this for pool creation since the vdev's 60548704186eSDan McDonald * configuratoin has already been dirtied. 60558704186eSDan McDonald */ 60568704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 60578704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 60584445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 60594445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 60608704186eSDan McDonald break; 6061990b4856Slling default: 6062990b4856Slling /* 6063990b4856Slling * Set pool property values in the poolprops mos object. 6064990b4856Slling */ 6065990b4856Slling if (spa->spa_pool_props_object == 0) { 6066ad135b5dSChristopher Siden spa->spa_pool_props_object = 6067ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6068990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6069ad135b5dSChristopher Siden tx); 6070990b4856Slling } 6071990b4856Slling 6072990b4856Slling /* normalize the property name */ 6073990b4856Slling propname = zpool_prop_to_name(prop); 6074990b4856Slling proptype = zpool_prop_get_type(prop); 6075990b4856Slling 6076990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6077990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 60780713e232SGeorge Wilson strval = fnvpair_value_string(elem); 60790713e232SGeorge Wilson VERIFY0(zap_update(mos, 6080990b4856Slling spa->spa_pool_props_object, propname, 60810713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 60824445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 60834445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6084990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 60850713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6086990b4856Slling 6087990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6088990b4856Slling const char *unused; 60890713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 60900713e232SGeorge Wilson prop, intval, &unused)); 6091990b4856Slling } 60920713e232SGeorge Wilson VERIFY0(zap_update(mos, 6093990b4856Slling spa->spa_pool_props_object, propname, 60940713e232SGeorge Wilson 8, 1, &intval, tx)); 60954445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 60964445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6097990b4856Slling } else { 6098990b4856Slling ASSERT(0); /* not allowed */ 6099990b4856Slling } 6100990b4856Slling 61010a4e9518Sgw switch (prop) { 61020a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6103990b4856Slling spa->spa_delegation = intval; 61040a4e9518Sgw break; 61050a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6106990b4856Slling spa->spa_bootfs = intval; 61070a4e9518Sgw break; 61080a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 61090a4e9518Sgw spa->spa_failmode = intval; 61100a4e9518Sgw break; 6111573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6112573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6113b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6114b98131cfSEric Taylor spa_async_request(spa, 6115b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6116573ca77eSGeorge Wilson break; 6117b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6118b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6119b24ab676SJeff Bonwick break; 61200a4e9518Sgw default: 61210a4e9518Sgw break; 61220a4e9518Sgw } 6123990b4856Slling } 6124990b4856Slling 6125b1b8ab34Slling } 6126e14bb325SJeff Bonwick 6127e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6128b1b8ab34Slling } 6129b1b8ab34Slling 6130cde58dbcSMatthew Ahrens /* 6131cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6132cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6133cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6134cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6135cde58dbcSMatthew Ahrens * tasks. 6136cde58dbcSMatthew Ahrens */ 6137cde58dbcSMatthew Ahrens static void 6138cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6139cde58dbcSMatthew Ahrens { 6140cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6141cde58dbcSMatthew Ahrens 6142cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6143cde58dbcSMatthew Ahrens 61443b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 61453b2aab18SMatthew Ahrens 6146cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6147cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6148cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6149cde58dbcSMatthew Ahrens 6150cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6151cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6152cde58dbcSMatthew Ahrens } 6153cde58dbcSMatthew Ahrens 6154cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6155cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6156cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6157cde58dbcSMatthew Ahrens } 6158cde58dbcSMatthew Ahrens 6159cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6160cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6161cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6162cde58dbcSMatthew Ahrens 6163cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6164cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6165cde58dbcSMatthew Ahrens } 6166ad135b5dSChristopher Siden 6167ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6168ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6169ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6170ad135b5dSChristopher Siden } 6171b8289d24SDaniil Lunev 6172b8289d24SDaniil Lunev /* 6173b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6174b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6175b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6176b8289d24SDaniil Lunev * this feature active 6177b8289d24SDaniil Lunev */ 6178b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6179b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6180b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6181b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6182b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6183b8289d24SDaniil Lunev 6184b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6185b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6186b8289d24SDaniil Lunev } 61873b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6188cde58dbcSMatthew Ahrens } 6189cde58dbcSMatthew Ahrens 6190fa9e4066Sahrens /* 6191fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6192fa9e4066Sahrens * part of the process, so we iterate until it converges. 6193fa9e4066Sahrens */ 6194fa9e4066Sahrens void 6195fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6196fa9e4066Sahrens { 6197fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6198fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6199b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 62000373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6201fa9e4066Sahrens vdev_t *vd; 6202fa9e4066Sahrens dmu_tx_t *tx; 6203e14bb325SJeff Bonwick int error; 6204fa9e4066Sahrens 6205f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6206f9af39baSGeorge Wilson 6207fa9e4066Sahrens /* 6208fa9e4066Sahrens * Lock out configuration changes. 6209fa9e4066Sahrens */ 6210e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6211fa9e4066Sahrens 6212fa9e4066Sahrens spa->spa_syncing_txg = txg; 6213fa9e4066Sahrens spa->spa_sync_pass = 0; 6214fa9e4066Sahrens 6215e14bb325SJeff Bonwick /* 6216e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6217e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6218e14bb325SJeff Bonwick */ 6219e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 62208ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 62218ad4d6ddSJeff Bonwick /* 62228ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 62238ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 62248ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 62258ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 62268ad4d6ddSJeff Bonwick * into the root vdev tree. 62278ad4d6ddSJeff Bonwick */ 62288ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 62298ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 62308ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 62318ad4d6ddSJeff Bonwick vdev_state_clean(vd); 62328ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 62338ad4d6ddSJeff Bonwick } 62348ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 62358ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6236e14bb325SJeff Bonwick } 6237e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6238e14bb325SJeff Bonwick 623999653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 624099653d4eSeschrock 6241283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6242283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6243283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6244283b8460SGeorge.Wilson 624599653d4eSeschrock /* 6246e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 624799653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 624899653d4eSeschrock */ 6249e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6250e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 625199653d4eSeschrock int i; 625299653d4eSeschrock 625399653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 625499653d4eSeschrock vd = rvd->vdev_child[i]; 625599653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 625699653d4eSeschrock break; 625799653d4eSeschrock } 625899653d4eSeschrock if (i == rvd->vdev_children) { 625999653d4eSeschrock spa->spa_deflate = TRUE; 626099653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 626199653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 626299653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 626399653d4eSeschrock } 626499653d4eSeschrock } 626599653d4eSeschrock 6266fa9e4066Sahrens /* 6267fa9e4066Sahrens * Iterate to convergence. 6268fa9e4066Sahrens */ 6269fa9e4066Sahrens do { 6270b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6271fa9e4066Sahrens 6272fa9e4066Sahrens spa_sync_config_object(spa, tx); 6273fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6274fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6275fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6276fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6277ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6278fa9e4066Sahrens dsl_pool_sync(dp, txg); 6279fa9e4066Sahrens 628001f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 628169962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 6282b24ab676SJeff Bonwick } else { 6283231aab85SMatthew Ahrens /* 6284231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 6285231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 6286231aab85SMatthew Ahrens */ 6287231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 6288cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 628969962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 6290fa9e4066Sahrens } 6291fa9e4066Sahrens 6292b24ab676SJeff Bonwick ddt_sync(spa, txg); 62933f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6294afee20e4SGeorge Wilson 6295b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6296b24ab676SJeff Bonwick vdev_sync(vd, txg); 6297b24ab676SJeff Bonwick 6298231aab85SMatthew Ahrens if (pass == 1) { 6299cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6300231aab85SMatthew Ahrens ASSERT3U(txg, >=, 6301231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 6302231aab85SMatthew Ahrens /* 6303231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 6304231aab85SMatthew Ahrens * because we could have marked the MOS dirty 6305231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 6306231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 6307231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 6308231aab85SMatthew Ahrens * it is updated if we have synced out dirty 6309231aab85SMatthew Ahrens * data (though in this case the MOS will most 6310231aab85SMatthew Ahrens * likely also be dirty due to second order 6311231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 6312231aab85SMatthew Ahrens */ 6313231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 6314231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 6315231aab85SMatthew Ahrens /* 6316231aab85SMatthew Ahrens * Nothing changed on the first pass, 6317231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 6318231aab85SMatthew Ahrens * syncing deferred frees, so that we 6319231aab85SMatthew Ahrens * can keep this TXG as a no-op. 6320231aab85SMatthew Ahrens */ 6321231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 6322231aab85SMatthew Ahrens txg)); 6323231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6324231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 6325231aab85SMatthew Ahrens break; 6326231aab85SMatthew Ahrens } 6327231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 6328231aab85SMatthew Ahrens } 6329fa9e4066Sahrens 6330cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6331fa9e4066Sahrens 6332fa9e4066Sahrens /* 6333fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6334fa9e4066Sahrens * to commit the transaction group. 63350373e76bSbonwick * 633617f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 633717f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6338e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6339e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 63400373e76bSbonwick */ 6341e14bb325SJeff Bonwick for (;;) { 6342e14bb325SJeff Bonwick /* 6343e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6344e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6345e14bb325SJeff Bonwick */ 6346e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6347e14bb325SJeff Bonwick 6348e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6349e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6350e14bb325SJeff Bonwick int svdcount = 0; 6351e14bb325SJeff Bonwick int children = rvd->vdev_children; 6352e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6353e14bb325SJeff Bonwick 6354573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6355e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6356e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6357e14bb325SJeff Bonwick continue; 6358e14bb325SJeff Bonwick svd[svdcount++] = vd; 6359e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6360e14bb325SJeff Bonwick break; 6361e14bb325SJeff Bonwick } 63628956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 63638956713aSEric Schrock if (error != 0) 63648956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 63658956713aSEric Schrock B_TRUE); 6366e14bb325SJeff Bonwick } else { 6367e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 63688956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 63698956713aSEric Schrock if (error != 0) 63708956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 63718956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 63720373e76bSbonwick } 6373e14bb325SJeff Bonwick 6374dfbb9432SGeorge Wilson if (error == 0) 6375dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6376dfbb9432SGeorge Wilson 6377e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6378e14bb325SJeff Bonwick 6379e14bb325SJeff Bonwick if (error == 0) 6380e14bb325SJeff Bonwick break; 6381e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6382e14bb325SJeff Bonwick zio_resume_wait(spa); 63830373e76bSbonwick } 638499653d4eSeschrock dmu_tx_commit(tx); 638599653d4eSeschrock 6386283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6387283b8460SGeorge.Wilson 63880373e76bSbonwick /* 63890373e76bSbonwick * Clear the dirty config list. 6390fa9e4066Sahrens */ 6391e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 63920373e76bSbonwick vdev_config_clean(vd); 63930373e76bSbonwick 63940373e76bSbonwick /* 63950373e76bSbonwick * Now that the new config has synced transactionally, 63960373e76bSbonwick * let it become visible to the config cache. 63970373e76bSbonwick */ 63980373e76bSbonwick if (spa->spa_config_syncing != NULL) { 63990373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 64000373e76bSbonwick spa->spa_config_txg = txg; 64010373e76bSbonwick spa->spa_config_syncing = NULL; 64020373e76bSbonwick } 6403fa9e4066Sahrens 6404fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 6405fa9e4066Sahrens 6406b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6407fa9e4066Sahrens 6408fa9e4066Sahrens /* 6409fa9e4066Sahrens * Update usable space statistics. 6410fa9e4066Sahrens */ 6411fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6412fa9e4066Sahrens vdev_sync_done(vd, txg); 6413fa9e4066Sahrens 6414485bbbf5SGeorge Wilson spa_update_dspace(spa); 6415485bbbf5SGeorge Wilson 6416fa9e4066Sahrens /* 6417fa9e4066Sahrens * It had better be the case that we didn't dirty anything 641899653d4eSeschrock * since vdev_config_sync(). 6419fa9e4066Sahrens */ 6420fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6421fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6422fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6423b24ab676SJeff Bonwick 6424b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6425fa9e4066Sahrens 6426e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6427ea8dc4b6Seschrock 6428468c413aSTim Haley spa_handle_ignored_writes(spa); 6429468c413aSTim Haley 6430ea8dc4b6Seschrock /* 6431ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6432ea8dc4b6Seschrock */ 6433ea8dc4b6Seschrock spa_async_dispatch(spa); 6434fa9e4066Sahrens } 6435fa9e4066Sahrens 6436fa9e4066Sahrens /* 6437fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6438fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6439fa9e4066Sahrens * sync. 6440fa9e4066Sahrens */ 6441fa9e4066Sahrens void 6442fa9e4066Sahrens spa_sync_allpools(void) 6443fa9e4066Sahrens { 6444fa9e4066Sahrens spa_t *spa = NULL; 6445fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6446fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6447f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6448f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6449fa9e4066Sahrens continue; 6450fa9e4066Sahrens spa_open_ref(spa, FTAG); 6451fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6452fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6453fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6454fa9e4066Sahrens spa_close(spa, FTAG); 6455fa9e4066Sahrens } 6456fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6457fa9e4066Sahrens } 6458fa9e4066Sahrens 6459fa9e4066Sahrens /* 6460fa9e4066Sahrens * ========================================================================== 6461fa9e4066Sahrens * Miscellaneous routines 6462fa9e4066Sahrens * ========================================================================== 6463fa9e4066Sahrens */ 6464fa9e4066Sahrens 6465fa9e4066Sahrens /* 6466fa9e4066Sahrens * Remove all pools in the system. 6467fa9e4066Sahrens */ 6468fa9e4066Sahrens void 6469fa9e4066Sahrens spa_evict_all(void) 6470fa9e4066Sahrens { 6471fa9e4066Sahrens spa_t *spa; 6472fa9e4066Sahrens 6473fa9e4066Sahrens /* 6474fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6475fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6476fa9e4066Sahrens */ 6477fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6478fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6479fa9e4066Sahrens /* 6480ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6481ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6482ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6483fa9e4066Sahrens */ 6484fa9e4066Sahrens spa_open_ref(spa, FTAG); 6485fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6486ea8dc4b6Seschrock spa_async_suspend(spa); 6487fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6488fa9e4066Sahrens spa_close(spa, FTAG); 6489fa9e4066Sahrens 6490fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6491fa9e4066Sahrens spa_unload(spa); 6492fa9e4066Sahrens spa_deactivate(spa); 6493fa9e4066Sahrens } 6494fa9e4066Sahrens spa_remove(spa); 6495fa9e4066Sahrens } 6496fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6497fa9e4066Sahrens } 6498ea8dc4b6Seschrock 6499ea8dc4b6Seschrock vdev_t * 65006809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6501ea8dc4b6Seschrock { 6502c5904d13Seschrock vdev_t *vd; 6503c5904d13Seschrock int i; 6504c5904d13Seschrock 6505c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6506c5904d13Seschrock return (vd); 6507c5904d13Seschrock 65086809eb4eSEric Schrock if (aux) { 6509c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6510c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 65116809eb4eSEric Schrock if (vd->vdev_guid == guid) 65126809eb4eSEric Schrock return (vd); 65136809eb4eSEric Schrock } 65146809eb4eSEric Schrock 65156809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 65166809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6517c5904d13Seschrock if (vd->vdev_guid == guid) 6518c5904d13Seschrock return (vd); 6519c5904d13Seschrock } 6520c5904d13Seschrock } 6521c5904d13Seschrock 6522c5904d13Seschrock return (NULL); 6523ea8dc4b6Seschrock } 6524eaca9bbdSeschrock 6525eaca9bbdSeschrock void 6526990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6527eaca9bbdSeschrock { 6528f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6529f9af39baSGeorge Wilson 6530e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6531eaca9bbdSeschrock 6532eaca9bbdSeschrock /* 6533eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6534eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6535eaca9bbdSeschrock * possible. 6536eaca9bbdSeschrock */ 653762eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 65385d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 6539eaca9bbdSeschrock 6540990b4856Slling spa->spa_uberblock.ub_version = version; 6541eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6542eaca9bbdSeschrock 6543e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 654499653d4eSeschrock 654599653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 654699653d4eSeschrock } 654799653d4eSeschrock 654899653d4eSeschrock boolean_t 654999653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 655099653d4eSeschrock { 655199653d4eSeschrock int i; 655239c23413Seschrock uint64_t spareguid; 6553fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 655499653d4eSeschrock 6555fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6556fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 655799653d4eSeschrock return (B_TRUE); 655899653d4eSeschrock 6559fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6560fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6561fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 656239c23413Seschrock return (B_TRUE); 656339c23413Seschrock } 656439c23413Seschrock 656599653d4eSeschrock return (B_FALSE); 6566eaca9bbdSeschrock } 6567b1b8ab34Slling 656889a89ebfSlling /* 656989a89ebfSlling * Check if a pool has an active shared spare device. 657089a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 657189a89ebfSlling */ 657289a89ebfSlling static boolean_t 657389a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 657489a89ebfSlling { 657589a89ebfSlling int i, refcnt; 657689a89ebfSlling uint64_t pool; 657789a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 657889a89ebfSlling 657989a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 658089a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 658189a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 658289a89ebfSlling refcnt > 2) 658389a89ebfSlling return (B_TRUE); 658489a89ebfSlling } 658589a89ebfSlling 658689a89ebfSlling return (B_FALSE); 658789a89ebfSlling } 658889a89ebfSlling 65893d7072f8Seschrock /* 65903d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 65913d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 65923d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 65933d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 65943d7072f8Seschrock * or zdb as real changes. 65953d7072f8Seschrock */ 65963d7072f8Seschrock void 65973d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 65983d7072f8Seschrock { 65993d7072f8Seschrock #ifdef _KERNEL 66003d7072f8Seschrock sysevent_t *ev; 66013d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 66023d7072f8Seschrock sysevent_value_t value; 66033d7072f8Seschrock sysevent_id_t eid; 66043d7072f8Seschrock 66053d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 66063d7072f8Seschrock SE_SLEEP); 66073d7072f8Seschrock 66083d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 66093d7072f8Seschrock value.value.sv_string = spa_name(spa); 66103d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 66113d7072f8Seschrock goto done; 66123d7072f8Seschrock 66133d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 66143d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 66153d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 66163d7072f8Seschrock goto done; 66173d7072f8Seschrock 66183d7072f8Seschrock if (vd) { 66193d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 66203d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 66213d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 66223d7072f8Seschrock SE_SLEEP) != 0) 66233d7072f8Seschrock goto done; 66243d7072f8Seschrock 66253d7072f8Seschrock if (vd->vdev_path) { 66263d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 66273d7072f8Seschrock value.value.sv_string = vd->vdev_path; 66283d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 66293d7072f8Seschrock &value, SE_SLEEP) != 0) 66303d7072f8Seschrock goto done; 66313d7072f8Seschrock } 66323d7072f8Seschrock } 66333d7072f8Seschrock 6634b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6635b01c3b58Seschrock goto done; 6636b01c3b58Seschrock attr = NULL; 6637b01c3b58Seschrock 66383d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 66393d7072f8Seschrock 66403d7072f8Seschrock done: 66413d7072f8Seschrock if (attr) 66423d7072f8Seschrock sysevent_free_attr(attr); 66433d7072f8Seschrock sysevent_free(ev); 66443d7072f8Seschrock #endif 66453d7072f8Seschrock } 6646